Re: drawRect problem
Re: drawRect problem
- Subject: Re: drawRect problem
- From: Uli Kusterer <email@hidden>
- Date: Sun, 25 Feb 2007 21:29:18 +0100
Am 23.02.2007 um 20:26 schrieb Ken Tozier:
- (void) sampleMouse:(NSTimer *) inTimer
{
NSPoint windowPoint = [[self window]
mouseLocationOutsideOfEventStream];
// make sure the point has changed since the last sample
if (NSEqualPoints(windowPoint, lastPoint) == NO)
{
// convert from window coordinates to view coordinates
NSPoint viewPoint = [self convertPoint: windowPoint fromView:
[self superview]];//[[self window] contentView]];
// save windowPoint point for future reference
lastPoint = windowPoint;
// see if viewPoint is in usable rect
if (NSPointInRect(viewPoint, usableRect) == YES)
[self hilightCell: [self cellForPoint: viewPoint]];
else
[self hilightCell: nil];
}
}
I'm not sure you want to use a timer for a mouseOver. Use a mouse
tracking region. That way you're notified whenever the mouse moves to
another view, and you don't burn cycles checking NSEqualPoints()
needlessly.
- (void) hilightCell:(id) inCell
{
if ((inCell == nil) && (lastCell != nil))
{
NSRect cellRect = [self rectForCell: lastCell];
NSLog(@"unhilighting cell: %i, rect: %@", [cells indexOfObject:
lastCell], [NSDictionary dictionaryWithNSRect: cellRect]);
// unhighlight mouse cell
[lastCell mouseEntered: nil];
[self setNeedsDisplayInRect: cellRect];
}
else if (inCell != lastCell)
{
NSRect cellRect = [self rectForCell: inCell];
NSLog(@"hilighting cell: %i, rect: %@", [cells indexOfObject:
inCell], [NSDictionary dictionaryWithNSRect: cellRect]);
lastCell = inCell;
// we're no longer in the cell
[lastCell mouseEntered: nil];
[self setNeedsDisplayInRect: cellRect];
}
}
Yup, this should just work.
- (void) drawRect:(NSRect) rect
{
float oldHeight = height,
oldWidth = width;
// update the geometry
[self updateGeometry];
BOOL sizeChanged = ((oldHeight == height) && (oldWidth ==
width)) ? NO : YES ;
[backColor set];
if (sizeChanged == YES)
{
// this reflows the cells based on the view width
NSEnumerator *enumerator = [cells objectEnumerator];
id cell;
NSRect curFrame = [self frame],
cellRect = NSMakeRect(margin, margin, cellWidth, cellHeight),
newFrame = NSMakeRect(curFrame.origin.x, curFrame.origin.y,
curFrame.size.width, height);
float max = [self maxX];
NSRectFillUsingOperation(newFrame, NSCompositeSourceOver);
while (cell = [enumerator nextObject])
{
if (cellRect.origin.x + cellWidth + horizontalSpacing > max)
{
cellRect.origin.x = margin;
cellRect.origin.y += cellHeight + verticalSpacing;
}
if (active)
[cell mouseEntered: nil];
[cell drawWithFrame: cellRect
inView: self];
cellRect.origin.x += cellWidth + horizontalSpacing;
}
}
else if (lastCell != nil)
{
NSLog(@"drawing mouse cell");
NSRect cellFrame = [self rectForCell: lastCell];
NSRectFillUsingOperation(cellFrame, NSCompositeSourceOver);
[lastCell drawWithFrame: cellFrame inView: self];
lastCell = nil;
}
}
Not sure I understand this code, but what you want to do is simply
only redraw cells that intersect the rect that you get passed as a
parameter to drawRect. There's also a method that lets you get more
precise information about what you want to redraw, i.e. let you find
out which subsections of this huge rect to redraw. I can't look up
the names right now, but I'm sure you can find them somewhere in NSView.
I'm also not sure you want to set lastCell to NIL in drawRect. That
means that your unhighlighting code doesn't have a cell to
unhighlight anymore :-S
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden