views:

465

answers:

2

I'm going through a tutorial on drawing a custom [shaped] window with cocoa by subclassing NSWindow.

The tutorial states that in initializer developer should do the following:

[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];

So i'm wondering what is the differnce between these two messages and why are they needed both since their result is the same.

References: tutorial can be found here.

+4  A: 

I guess that the first message is required because the drawing system needs to know whether it should bother updating views that lie behind yours. For example, if a window in another application (behind your window) updates (say text appears etc) the windowing system would not normally need to redraw it, but since your window is transparent it does in this case.

Ira Cooke
+1. In non-transparent windows you use `[self setOpaque:YES];` to speed up drawing.
Abizern
+1  A: 

So i'm wondering what is the differnce between these two messages and why are they needed both since their result is the same.

They're not the same. Look at the documentation for the opaque property: It's how you tell NSView that you're going to draw in your entire bounds, completely covering anything below your view. If this isn't true (if you don't cover the entire bounds, or you don't always draw at 100% opacity), then your view is not opaque, and you should leave that property set to NO.

If you set your view's background color to clearColor (which is simply a color with 0% opacity), and don't draw at 100% opacity over the entire background, then your view is not opaque. On the other hand, it is possible to have clearColor as your background and then completely draw over it, in which case your view is opaque and should set itself as such.

Peter Hosey