Re: Drawing in a NSView out side of drawRect

2008-12-23 Thread David Alter
Thanks to everyone that has responded to me. It has been a big help.


  I want to draw in a NSView but not when drawRect is called.

 Others have pointed this out, but I want to reiterate: no you don't.
 Trying to draw outside of drawRect: will only lead to pain. (It is
 useful, on rare occasions. You are not experiencing one of them.) Work
 with the view machinery. Call -setNeedsDisplay:.


I understand that the preferred method of doing thing is do your drawing in
drawRect:. I would like to understand better what are the issues and draw
backs to using lockFocus and then drawing.

thanks
-dave
___

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


Re: Drawing in a NSView out side of drawRect

2008-12-23 Thread Michael Ash
On Tue, Dec 23, 2008 at 12:44 PM, David Alter alterconsult...@gmail.com wrote:
 Thanks to everyone that has responded to me. It has been a big help.

  I want to draw in a NSView but not when drawRect is called.

 Others have pointed this out, but I want to reiterate: no you don't.
 Trying to draw outside of drawRect: will only lead to pain. (It is
 useful, on rare occasions. You are not experiencing one of them.) Work
 with the view machinery. Call -setNeedsDisplay:.

 I understand that the preferred method of doing thing is do your drawing in
 drawRect:. I would like to understand better what are the issues and draw
 backs to using lockFocus and then drawing.

The major problem is that the system can ask your view to redisplay at
any time. For example, your view might get redisplayed because the
user resized your window, or deminimized it, or even just moved a
window that's sitting on top of it (although in OS X, that last one
doesn't actually apply).

So your view needs to be prepared to completely recreate its content
at any time using the drawRect: method.

Given that requirement, using -lockFocus *usually* means something is
wrong. If the code that you have inside the lock/unlockFocus pair is
not duplicated in -drawRect:, then you won't be able to redraw your
contents on demand, and your view could end up looking very strange to
the user. If it *is* duplicated in -drawRect:, well, code duplication
is a bad thing, and anyway if the necessary stuff is in -drawRect:
then you can simply mark the view as needing to be redisplayed instead
of drawing directly.

There's also an efficiency advantage to not using lockFocus. Multiple
invalidations to the same view within the same event processing cycle
will result in a single call to -drawRect: and a single flush of the
window to the screen. This coalescing can't be done when calling
lockFocus.

Mike
___

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


Re: Drawing in a NSView out side of drawRect

2008-12-22 Thread David Alter

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

 Right now you're getting the current graphics context, which is purely
 arbitrary. Nothing has set it yet, so you're getting whatever happened
 to be hanging around.


Is there a way to create a NSGraphicsContext for the view?




___

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


Re: Drawing in a NSView out side of drawRect

2008-12-22 Thread Michael Ash
On Mon, Dec 22, 2008 at 4:22 PM, David Alter alterconsult...@gmail.com wrote:
  CGContextRef myContext =
  (CGContextRef)[[NSGraphicsContext currentContext]graphicsPort];

 Right now you're getting the current graphics context, which is purely
 arbitrary. Nothing has set it yet, so you're getting whatever happened
 to be hanging around.

 Is there a way to create a NSGraphicsContext for the view?

The paragraph immediately following the one you quoted describes how.

Mike
___

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


Drawing in a NSView out side of drawRect

2008-12-19 Thread David Alter
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.

thanks for the help
-dave
___

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


Re: Drawing in a NSView out side of drawRect

2008-12-19 Thread Ricky Sharp


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


Re: Drawing in a NSView out side of drawRect

2008-12-19 Thread Graham Cox


On 20 Dec 2008, at 11:20 am, David Alter wrote:


I want to draw in a NSView but not when drawRect is called.


Why? There are very few situations when this is required or appropriate.


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?


You need to flush it to the screen. But don't - it's just not the  
right way to do drawing.


To test this out I have sub classed NSView and overloaded mouseDown.  
I added

the following code.



Store the rects to be drawn in a list - your view could own this list  
for simplicity while you are experimenting. Then invalidate the rect  
you need to repaint in mouse down. -drawRect: then just iterates over  
the list and draws those rects that intersect the update area.


This is way, way easier than trying to fudge around drawing in a non- 
standard manner. Learn it and love it.


hth,


Graham


___

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


Re: Drawing in a NSView out side of drawRect

2008-12-19 Thread Michael Ash
On Fri, Dec 19, 2008 at 7:20 PM, David Alter alterconsult...@gmail.com wrote:
 I want to draw in a NSView but not when drawRect is called.

Others have pointed this out, but I want to reiterate: no you don't.
Trying to draw outside of drawRect: will only lead to pain. (It is
useful, on rare occasions. You are not experiencing one of them.) Work
with the view machinery. Call -setNeedsDisplay:.

However, I'll also point out why your code currently doesn't work,
just for posterity:

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

Right now you're getting the current graphics context, which is purely
arbitrary. Nothing has set it yet, so you're getting whatever happened
to be hanging around.

[self lockFocus];

Now you lock focus, which sets the current context to the one for your
view. Since you grab the context before you set it, things don't work
very well.

Mike
___

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