On Dec 19, 2008, at 6:20 PM, David Alter wrote:
I want to draw in a NSView but not when drawRect is called. To do this I understand that I need to call lockFocus before drawing and unlockFocus after. The drawing appears to happen but it is not until I deactivate the window do I see my results. How can I get it to refresh once I have done my
drawing?
To test this out I have sub classed NSView and overloaded mouseDown. I added
the following code.

- (void)mouseDown:(NSEvent *)theEvent ;

{

   NSPoint loc = [[self window] mouseLocationOutsideOfEventStream];

   loc = [self convertPoint:loc fromView:[[self window] contentView]];

    CGContextRef myContext =
(CGContextRef)[[NSGraphicsContext currentContext]graphicsPort];

   [self lockFocus];

   CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);

   CGContextFillRect (myContext, CGRectMake (loc.x, loc.y, 10, 10 ));

   [self unlockFocus];

}

This will draw a box for each mouse click, but not until I deactivate my
window.

What you should do is do all drawing in drawRect:. What happens when your drawRect: is called? I bet you won't see any of the rects you had when clicking around.
Instead, maintain a list of rects that need to be drawn.  mouseDown:  
then simply adds a new rect to the list and calls setNeedsDisplay:.   
Or, if you profile things and need more speed, setNeedsDisplayInRect:
If you want things such that only one rect ever is drawn, just have  
mouseDown: set the values of that single rect and still call  
setNeedsDisplay:
___________________________________________________________
Ricky A. Sharp         mailto:rsh...@instantinteractive.com
Instant Interactive(tm)   http://www.instantinteractive.com



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please 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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to