Skip navigation.
 
mlextra redrawing with [self setBounds:] in drawRect:
FROM : Nathan Vander Wilt
DATE : Tue Jan 29 02:43:01 2008

extra redrawing with [self setBounds:] in drawRect:


In my custom view, I set the bounds in my drawRect:
method before drawing. This is so that while the user
is resizing the window, the content stretches with the
view. I then added some code to draw a selection
rectangle as the user drags across the image.

Somehow this selection rectangle code triggers random
extra redrawing. As soon as I start dragging things go
weird. Dragging itself causes the whole view to
flicker, instead of just the invalidated portions.
Even when done dragging, the view keeps getting pairs
of drawRect calls *way* more often than normal: any
time the window is brought to front, any time the
window as a whole is moved, etc. If I resize the
window the spurious events stop, until I drag within
the view again.

If I call setBounds: only if ([self inLiveResize]),
then the problem goes away and the view only gets
asked to redraw when dragging or window resizing
invalidates it. But by my understanding, setBounds:
shouldn't cause extra redrawing in the first place, so
I don't think I've gotten to the root of the problem.

Is it kosher to call [self setBounds:] in drawRect:?
Why would a combination of setNeedsDisplayInRect: in
an event responder and setBounds: in the draw code
lead to the view getting extra drawRect: calls even
when the event responders are done being used?

thanks,
-natevw


Here's the relevant code (critique of any aspect
appreciated as well):

@interface CTPrototypeMapView : NSView {
@private
   NSArray* polygons;
   BOOL dragging;
   NSPoint startingDragLocation;
   NSPoint currentDragLocation;
}

@end


NSRect CTMakeRectFromPoints(NSPoint a, NSPoint b) {
   CGFloat origin_x, origin_y;
   CGFloat width, height;
   if (a.x < b.x) {
       origin_x = a.x;
       width = b.x - a.x;
   } else {
       origin_x = b.x;
       width = a.x - b.x;
   }
   if (a.y < b.y) {
       origin_y = a.y;
       height = b.y - a.y;
   } else {
       origin_y = b.y;
       height = a.y - b.y;
   }
   return NSMakeRect(origin_x, origin_y, width, height);
}

@implementation CTPrototypeMapView

- (void)invalidateCurrentDragArea {
   NSRect dragRect =
CTMakeRectFromPoints(startingDragLocation,
currentDragLocation);
   //NSRect marredRect = NSInsetRect(dragRect, -0.2,
-0.2);    // stroke extends a bit beyond passed rectangle
   NSRect marredRect = dragRect;
   [self setNeedsDisplayInRect: marredRect];
}

- (void)drawRect:(NSRect)rect {
   [self setBounds:NSMakeRect(-180, -90, 360, 180)];
   
   // ...draw a bunch of shapes...
   
   if (dragging) {
       NSRect dragRectangle =
CTMakeRectFromPoints(startingDragLocation,
currentDragLocation);
       NSBezierPath* shapeForDrag = [NSBezierPath
bezierPathWithRoundedRect:dragRectangle xRadius:4
yRadius:4];
       [shapeForDrag setLineWidth:0.2];
       [shapeForDrag stroke];
   }
}

- (void)dragTo:(NSPoint)location {
   [self invalidateCurrentDragArea];
   currentDragLocation = location;
   [self invalidateCurrentDragArea];
}

- (void)mouseDown:(NSEvent*)event {
   dragging = YES;
   NSPoint event_coordinates = [event locationInWindow];
   startingDragLocation = [self
convertPoint:event_coordinates fromView:nil];
}

- (void)mouseDragged:(NSEvent*)event {
   if (!dragging) return;
   NSPoint event_coordinates = [event locationInWindow];
   NSPoint newDragLocation = [self
convertPoint:event_coordinates fromView:nil];
   [self dragTo:newDragLocation];
}

- (void)mouseUp:(NSEvent*)event {
   dragging = NO;
   [self invalidateCurrentDragArea];
}

@end




      ____________________________________________________________________________________
ˇCapacidad ilimitada de almacenamiento en tu correo!
No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:                     
http://correo.espanol.yahoo.com/

Related mailsAuthorDate
mlextra redrawing with [self setBounds:] in drawRect: Nathan Vander Wilt Jan 29, 02:43
mlRe: extra redrawing with [self setBounds:] in drawRect: Jim Correia Jan 29, 03:32
mlRe: extra redrawing with [self setBounds:] in drawRect: Nathan Vander Wilt Jan 29, 03:58