Re: NSButtonCell darkened when state is NSOnState [SOLVED]
Re: NSButtonCell darkened when state is NSOnState [SOLVED]
- Subject: Re: NSButtonCell darkened when state is NSOnState [SOLVED]
- From: Stephan Burlot <email@hidden>
- Date: Tue, 16 Nov 2004 10:20:24 +0100
To perpetuate my tradition of answering my own posts, this is what I
found:
Didn't find an obvious solution, so what I did is to use the alternate
image to display a darkened version of the button when
state==NSONState.
The darkened image is calculated in awakeFromNib, using a modified
version of some code by Chris Giordano found on cocoabuilder (search
"Composing a darker image").
NSEnumerator *enumerator = [[cropButtonMatrix cells]
objectEnumerator];
while( aCell = [enumerator nextObject] )
{
darkenedImage = [NSImage highlightedImageForImage:[aCell image]];
[aCell setAlternateImage:darkenedImage];
[aCell setShowsStateBy:NSContentsCellMask];
[aCell setHighlightsBy:NSNoCellMask];
}
and highlightedImageForImage is in my NSImage Additions:
//
========================================================================
===========
// creates copy of image, with highlighting applied
+ (NSImage *) highlightedImageForImage:(NSImage *)image
{
NSImage * newImage;
NSSize newSize;
if (!image) return nil;
newSize = [image size];
newImage = [[[NSImage alloc] initWithSize:newSize] autorelease];
[newImage lockFocus];
[image drawAtPoint:NSZeroPoint fromRect:NSMakeRect(0, 0,
newSize.width, newSize.height) operation:NSCompositeSourceOver
fraction:1.0];
[[[NSColor blackColor] colorWithAlphaComponent: .3] set];
NSRectFillUsingOperation(NSMakeRect(0, 0, newSize.width,
newSize.height), NSCompositeSourceAtop);
[newImage unlockFocus];
return newImage;
}
I'm thinking of starting my own mailing list, so I can ask questions
and answer to myself. I'm looking for a moderator. ;-)
Stephan
Le 15 nov. 04, à 22:20, Stephan Burlot a écrit :
Hi,
I have a NSMatrix of buttons with image.
The NSMatrix is set to radio mode in IB.
Buttons are set to Square button, and behavior to "Push On/Push Off"
I want that when a button is selected, the image appears darkened.
Using trial and error, I achieved good result when the button is
clicked by setting:
[[cropButtonMatrix cellWithTag:0] setShowsStateBy:NSNoCellMask];
[[cropButtonMatrix cellWithTag:0] setHighlightsBy:NSNoCellMask];
But the state of the button is not shown when the mouse is released,
i.e. nothing is drawn in the cell to show its state.
I have tried all the constants available for setShowStateBy without
success.
What is the magical incantation to tell the NSButtonCell to display
its image darkened when it's on?
Any ideas?
Stephan Burlot
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden