Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPopUpButton Active Image

I want to create an NSPopUpButton with a custom active image. I have two images, one for inactive, and another for active. In interface builder I set the Image and Alt. Image for the NSPopUpButton. The Image is showing correctly but when I click on the button it displays the standard dark button state instead of the Alt. Image.

Here is a screen shot of the interface builder panel: http://cl.ly/0D2c0Y2y0f1Z462d311X

How can I setup the NSPopUpButton to display my alternate image when it's clicked?

like image 420
keegan3d Avatar asked Jan 10 '11 07:01

keegan3d


1 Answers

A developer from the Apple Dev forums pointed me in the right direction: https://devforums.apple.com/message/364824

Here's what I came up with as a subclass of NSPopUpButtonCell that respects the alternate image from IB:

- (void)drawImageWithFrame:(NSRect)cellRect inView:(NSView *)controlView{
    NSImage *image = self.image;
    if([self isHighlighted] && self.alternateImage){
        image = self.alternateImage;
    }

    //TODO: respect -(NSCellImagePosition)imagePosition
    NSRect imageRect = NSZeroRect;
    imageRect.origin.y = (CGFloat)round(cellRect.size.height*0.5f-image.size.height*0.5f);
    imageRect.origin.x = (CGFloat)round(cellRect.size.width*0.5f-image.size.width*0.5f);
    imageRect.size = image.size;

    [image drawInRect:imageRect
                 fromRect:NSZeroRect
                operation:NSCompositeSourceOver 
                 fraction:1.0f 
           respectFlipped:YES 
                    hints:nil];    
}
like image 128
keegan3d Avatar answered Sep 18 '22 21:09

keegan3d