Thursday, November 12, 2009

Dropdown box popup menu

A button can be customized to show popup menu. The dropdown button code I have taken it from Apples example code for ButtonMadness.

///DropDownButton.h


#import "Cocoa/Cocoa.h"


@interface DropDownButton : NSButton
{
NSPopUpButtonCell* popUpCell;
}

- (void)setUsesMenu:(BOOL)flag;
- (BOOL)usesMenu;

@end

///DropDownButton.m
#import "DropDownButton.h"

@implementation DropDownButton

// -------------------------------------------------------------------------------
// awakeFromNib:
// -------------------------------------------------------------------------------
- (void)awakeFromNib
{
//NSLog(@"DropDownButton awakeFromNib");
if ([self menu] != nil)
{
[self setUsesMenu:YES];
}
}

// -------------------------------------------------------------------------------
// dealloc:
// -------------------------------------------------------------------------------
- (void)dealloc
{
[popUpCell release];
[super dealloc];
}

// -------------------------------------------------------------------------------
// setUsesMenu:flag
// -------------------------------------------------------------------------------
- (void)setUsesMenu:(BOOL)flag
{
if (popUpCell == nil && flag)
{
popUpCell = [[NSPopUpButtonCell alloc] initTextCell:@""];
[popUpCell setPullsDown:YES];
[popUpCell setPreferredEdge:NSMaxYEdge];
}
else if (popUpCell != nil && !flag)
{
[popUpCell release];
popUpCell = nil;
}
}

// -------------------------------------------------------------------------------
// usesMenu:
// -------------------------------------------------------------------------------
- (BOOL)usesMenu
{
return (popUpCell != nil);
}

// -------------------------------------------------------------------------------
// runPopUp:theEvent
// -------------------------------------------------------------------------------
- (void)runPopUp:(NSEvent*)theEvent
{
//NSLog(@"Button runPopUp");

// create the menu the popup will use
NSMenu* popUpMenu = [[self menu] copy];
[popUpMenu insertItemWithTitle:@"" action:NULL keyEquivalent:@"" atIndex:0]; // blank item at top
[popUpCell setMenu:popUpMenu];

// and show it
[popUpCell performClickWithFrame:[self bounds] inView:self];

[popUpMenu release];

[self setNeedsDisplay: YES];
}

// -------------------------------------------------------------------------------
// mouseDown:theEvent
// -------------------------------------------------------------------------------
- (void)mouseDown:(NSEvent*)theEvent
{
//NSLog(@"Button mouseDown");
if ([self usesMenu])
{
[self runPopUp:theEvent];
}
else
{
[super mouseDown:theEvent];
}
}

////DropDownButtonController.h
@class DropDownButton;
@interface DropDownButtonController : NSObject
{
IBOutlet DropDownButton *mDropDownButton;

}

- (IBAction)menuItem1Handler:(id)sender;
- (IBAction)menuItem2Handler:(id)sender;
@end

////DropDownButtonController.m
#import "DropDownButton.h"

// -------------------------------------------------------------------------------
// awakeFromNib:
// -------------------------------------------------------------------------------

- (void)awakeFromNib
{
NSMenu *dropDownMenu = [[NSMenu alloc] init];
NSInteger index =0;
NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"MenuItem1", nil)
action:@selector(menuItem1Handler:)
keyEquivalent:@""];
[dropDownMenu insertItem:menuItem atIndex:index++];
[menuItem release];

menuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"MenuItem2", nil)
action:@selector(menuItem2Handler:)
keyEquivalent:@""];
[dropDownMenu insertItem:menuItem atIndex:index++];
[menuItem release];

[mDropDownButton setMenu:[[dropDownMenu copy] autorelease]];
[dropDownMenu release];

[mDropDownButton setUsesMenu:YES];
}

// -------------------------------------------------------------------------------
//
menuItem1Handler
// -------------------------------------------------------------------------------

- (IBAction)menuItem1Handler:(id)sender
{
//Here you can get the menuitem properties since it is nothing but a button
NSLog(@"MenuText:[%@]", [sender title]);
}

// -------------------------------------------------------------------------------
//
menuItem2Handler
// -------------------------------------------------------------------------------

- (IBAction)menuItem2Handler:(id)sender
{
}