Skip navigation.
 
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"?
FROM : Bob Smith
DATE : Mon Apr 09 23:12:09 2007

On Apr 8, 2007, at 8:36 AM, Jerry Krinock wrote:

> 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.


Not true; with -setNeedsDisplay: all you are doing is telling the 
view itself that it needs to be redrawn, but that redraw will occur 
in the area the view occupies when the next display cycle occurs, not 
at the time -setNeedsDisplay: is sent (so multiple -setNeedsDisplay: 
messages are redundant).  To get the behavior you are expecting, you 
need to inform the view's superview of the change in location, by 
telling the superview to redraw in the subview's old frame.  By 
moving the subview from one location to another you are exposing 
whatever part of the superview was underneath the subview at the old 
location, hence you have to tell the superview to redraw that area. 
You were expecting that to occur automatically when -setNeedsDisplay: 
is sent to a subview before it is moved, but that isn't the way it 
works.

>
> 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?


It is inefficient, you are redrawing the entire content view and 
discarding any optimizations.  The correct approach would be:

  [[someView superview] setNeedsDisplayInRect:[someView frame]];
  [someView setFrame:...];
  [someView setNeedsDisplay:YES];

  [theWindow displayIfNeeded];

Hope this helps!

Bob S.

Related mailsAuthorDate
mlHow to make NSWindow redraw vacated rects "ifNeeded"? Jerry Krinock Apr 8, 17:36
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Andrew Farmer Apr 8, 22:23
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Jerry Krinock Apr 9, 00:37
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Adam R. Maxwell Apr 9, 00:46
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Jerry Krinock Apr 9, 04:43
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Adam R. Maxwell Apr 9, 05:12
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Andrew Farmer Apr 9, 05:23
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Darkshadow Apr 9, 06:06
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Jerry Krinock Apr 9, 18:40
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Jerry Krinock Apr 9, 19:15
mlRe: How to make NSWindow redraw vacated rects "ifNeeded"? Bob Smith Apr 9, 23:12