Skip to content

Instantly share code, notes, and snippets.

@dchest
Created April 27, 2009 00:24
Show Gist options
  • Save dchest/102229 to your computer and use it in GitHub Desktop.
Save dchest/102229 to your computer and use it in GitHub Desktop.
NSPopUpButton sublass for use in toolbars (to get toolbar item with drop-down menu)
// Public domain code
#import <Cocoa/Cocoa.h>
@interface CRToolPopUpButton : NSPopUpButton {
NSControlSize controlSize;
IBOutlet NSToolbarItem *toolbarItem;
}
@property(assign) NSControlSize controlSize;
@property(retain) NSToolbarItem *toolbarItem;
@end
// Public domain code
#import "CRToolPopUpButton.h"
@implementation CRToolPopUpButton
@synthesize controlSize;
@synthesize toolbarItem;
- (id)initWithFrame:(NSRect)frame;
{
if (![super initWithFrame:frame])
return nil;
controlSize = NSRegularControlSize;
return self;
}
- (id)initWithCoder:(NSCoder*)coder;
{
if (![super initWithCoder:coder])
return nil;
controlSize = NSRegularControlSize;
return self;
}
- (void)setControlSize:(NSControlSize)newSize;
{
[toolbarItem setMinSize:NSMakeSize(32.0, 5.0)];
controlSize = newSize;
NSRect frame = [self frame];
if (controlSize == NSSmallControlSize)
frame.size = NSMakeSize(24.0, 24.0);
else
frame.size = NSMakeSize(32.0, 32.0);
[self setFrame:frame];
}
- (void)drawRect:(NSRect)rect;
{
NSRect imageRect = NSMakeRect(0, 0, 32, 32);
// Center in rect
NSRect canvasRect = rect;
if (controlSize == NSSmallControlSize)
canvasRect.size = NSMakeSize(24, 24);
else
canvasRect.size = NSMakeSize(32, 32);
canvasRect.origin.x += (rect.size.width - canvasRect.size.width)/2;
canvasRect.origin.y += (rect.size.height - canvasRect.size.height)/2;
[[self image] setFlipped:YES];
NSCompositingOperation compositing = ([[self cell] isHighlighted]) ? NSCompositePlusDarker : NSCompositeSourceOver;
[[self image] drawInRect:canvasRect fromRect:imageRect operation:compositing fraction:1.0];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment