Re: Button confusion
Re: Button confusion
- Subject: Re: Button confusion
- From: Darrin Cardani <email@hidden>
- Date: Tue, 1 Jun 2004 15:44:57 -0500
At 9:20 PM +0100 6/1/04, Jeremy Dronfield wrote:
Using borderless buttons with custom icons is tricky, if you want to
get a really pro look in your UI. To get the best results, it's best
to cut out the default Cocoa button display for the disabled state.
For example, look at the back-and-forward navigation buttons in
Safari or the Finder: they use three custom images: one "normal",
one for "pressed" and one "disabled". I've implemented the same
thing in an application of mine. I found that, to achieve the
neat-n-tidy display characteristics of the Safari buttons, I had to
avoid using -setEnabled: to switch the buttons off and on. When
disabling them, I send them -setTarget:nil and
-setImage:disabledImage AND -setAlternateImage:disabledImage. Then,
to re-enable them, -setTarget:self (or whatever), plus
-setImage:normalImage and -setAlternateImage:pressedImage.
Wow, I was hoping not to have to go to all that trouble. Not that
it's very hard, just that it doesn't seem like I should have to. It
doesn't seem the default behavior is correct for any situation, and
it should probably be doing the above by default.
However, in starting to do the above, I discovered something
interesting. I solved the problem by making each button a custom
subclass of NSButton that I call ImageButton. I only had to write 1
method:
- (void)drawRect:(NSRect)rect {
if ([ self isEnabled]) {
[ super drawRect:rect ];
} else {
[[NSColor whiteColor] set];
NSRectFill (rect);
[ super drawRect:rect ];
}
}
That solved the problem. I figured out from the weird ghosting that
was happening, that all NSButton was doing to draw the disabled
button was to draw it with 50% opacity against the background. So by
drawing the background as white (or whatever the appropriate
background color is) first, I get normally disabled buttons. It even
solves the initial state problem for some reason. Weird!
Thanks,
Darrin
--
Darrin Cardani - email@hidden
President, Buena Software, Inc.
<
http://www.buena.com/>
Video, Image and Audio Processing Development
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.