[Openmcl-devel] erasing under Windows
raffaelcavallaro at mac.com
Tue Aug 16 16:43:27 CDT 2011
On Aug 16, 2011, at 5:00 PM, Greg Bennett wrote:
> I assume that I am not thinking
> correctly about the updating of a view through the removal of an existing
Cocoa windows have no "memory" of what has been drawn into them. If their contents seem stable it only because the windowing system hasn't yet seen the need to redraw that portion of their contents. If it has - for example, on Mac OS, if you resize your window in between your line drawing - you'll see that portions of it seem to be "erased."
The Cocoa drawing model assumes that your window's contentView has a drawRect: method. This method will be called by the Cocoa framework every time the windowing system calculates that some portion of your view needs redrawing, which can of course be triggered by your code explicitly telling it that the view needs to be redrawn.
So you need a drawRect: method that somehow manages to "remember" what you've drawn previously, and manages to "change" some things when the view is redrawn.
There are a couple of obvious ways to do this:
1. Just redraw everything every time drawRect: is called. If you want to "remove" an item, just don't draw it that time around.
2. if you have a relatively stable background, draw it into an NSImage:
then, in your drawRect: method, draw that NSImage to your view, then draw whatever changes on top.
If you want things to work the way you seem to expect them to, you need to write a subclass of NSView and/or a subclass of NSWindow that used this NSView subclass that remembers what's been drawn to it, possibly using the NSImage method in number 2 above. Then you'd need to explicitly "clear" a region to erase previous drawing.
Alternatively, you create an NSView subclass with your own drawing commands, for example #'draw-line which simply creates and caches a thunk that calls the corresponding NSBezierPath method. Then your NSView subclass's drawRect: method would simply iterate over this cache calling each thunk. You'd then have the ability to delete individual drawing commands, change their parameters (if you cached these separately), change their drawing order, etc.
raffaelcavallaro at me.com
More information about the Openmcl-devel