views:

233

answers:

1

I have a window which has a custom NSView and has a bottom bar with controls on it, one of which is an NSColorWheel.

For simplicity sake the Window is 332px high, with the custom NSView being 300px high and the bottom bar being 32px high.

The bottom bar is created as part of my awakeFromNib when the app loads the window using the following code:

[[self window] setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge];
[[self window] setContentBorderThickness: 32.0 forEdge: NSMinYEdge];

In my custom NSView class I fill the rectangle with color. Everything works fine when the app loads using the following in my NSView class:

- (void)drawRect:(NSRect)dirtyRect
{
    dirtyRect = [self bounds];

    NSColor * mNewColor = [NSColor blackColor];
    [mNewColor set];
    [NSBezierPath fillRect:dirtyRect];
}

However, if I subsequently call a method that changes the color of the custom NSView when a color wheel in the bottom bar is changed, the bottom bar gets overwritten with the color. The following code illustrates this method (this code is in the custom NSView class:

- (void)changeBackgroundColor:(NSNotification *)notification
{
    NSLog(@"Changed background color");

    NSRect mRect = [self bounds];
    NSColor * mNewColor = [theColorWell color];
    [mNewColor set];
    [NSBezierPath fillRect:mRect];
    [self setNeedsDisplay:YES];
}

Resizing the window instantly corrects the problem, but obviously I don't want the user to have to resize the window for an obvious bug!

What I don't understand is why my bounds appear to be mapping to the parent window and not the custom NSView when I call setNeedsDisplay and yet the bound correctly adjust when I resize the window using the mouse (even if just by 1 pixel).

Do I somehow need to account for the bottom bar on the redraw?

Any and all help much appreciated.

+1  A: 

You should do all your drawing in the drawRect: method of your custom NSView. Cocoa automatically sets up the graphics context for you when it calls this method - things may not draw correctly if you perform drawing operations in other methods.

Your code in drawRect: could set the colour to the the current background colour as specified by your NSColorWell and fill the dirtyRect rectangle with this.

Then in the other method just call [self setNeedsDisplay:YES]; and then drawRect: will automatically be called to redraw the view.

See here for more information: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html#//apple_ref/doc/uid/TP40002978-CH7-SW4 (in particular the Drawing View Content section)

wipolar
Perfect - worked a charm. I didn't realize that the `drawRect` was being called by `setNeedsDisplay` as specified in the docs ;-) Somehow I had it in my head that `drawRect` was an initialization method only.
Hooligancat