How to make NSWindow redraw vacated rects "ifNeeded"?
How to make NSWindow redraw vacated rects "ifNeeded"?
- Subject: How to make NSWindow redraw vacated rects "ifNeeded"?
- From: Jerry Krinock <email@hidden>
- Date: Sun, 8 Apr 2007 08:36:44 -0700
I often find myself needing to sprinkle in what I feel are extra -
display messages to NSWindows to when I want them to update.
Yesterday I isolated a demo which gets me closer to understanding this.
In the following demo project, I create a window with an NSBox. I
send it a -setNeedsDisplay, then move it to a new location using -
setFrame:, and then -setNeedsDisplay again. My thinking is that I am
"marking" both the old and new rects as needing display, so that its
NSWindow will know to redraw both rects.
But then after telling the window to -displayIfNeeded, I get two
NSBox, one in the old location, and one in the new location.
Telling the window to -display, instead of -displayIfNeeded, gets the
desired result, but this seems inefficient. What's the correct idiom
to get a vacated rect redrawn?
Jerry Krinock
P.S. There is also a Really Weird way to solve the problem: If I -
orderFront: the window before adding the NSBox subview, then the
vacated rect gets redrawn by -displayIfNeeded as expected. A
possibly related mystery is that I find -orderFront: is needed to get
an NSProgressIndicator to start animating (with threaded animation).
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)noti {
// Create window
NSWindow* window = [[NSWindow alloc]
initWithContentRect:NSMakeRect(0,0,300,100)
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO] ;
[window center] ;
// Uncommenting this will fix the problem (Really Weird)!
// [window orderFront:self] ;
// Create a box and add to window
NSBox* box ;
box = [[NSBox alloc]
initWithFrame:NSMakeRect(20,10,260,40)] ;
[[window contentView] addSubview:box] ;
[window orderFront:self] ;
// Move box origin from y=10 up to y=50
// (a) First, mark the current rect for redrawing
// This does not seem to have any effect!
[box setNeedsDisplay:YES] ;
// (b) Move it
[box setFrame:NSMakeRect(20,50,260,40)] ;
// (c) Mark the new rect as needing redrawing
[box setNeedsDisplay:YES] ;
// Update the window
[window displayIfNeeded] ; // Not good enough!!
// Uncomment the next line and it works as desired
// [window display] ;
// Show for 3 seconds
[NSThread sleepUntilDate:
[NSDate dateWithTimeIntervalSinceNow:3.0]];
exit(0) ;
}
@end
_______________________________________________
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