BWToolkit / BWToolbarPullDownItem.m

//
//  BWToolbarPullDownItem.m
//  BWToolkit
//
//  Created by Mike Abdullah on 14/01/2010.
//  All code is provided under the New BSD license.
//

#import "BWToolbarPullDownItem.h"

#import "BWIWorkPopUpButton.h"


@implementation BWToolbarPullDownItem

- (id)initWithItemIdentifier:(NSString *)itemIdentifier;
{
    [super initWithItemIdentifier:itemIdentifier];
    
    // Prepare basic pull-down button
    _popUpButton = [[BWIWorkPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:YES];
    NSPopUpButtonCell *cell = [_popUpButton cell];
    
    [_popUpButton setBezelStyle:NSSmallSquareBezelStyle];   // otherwise menu covers bottom bit of button
    [cell setArrowPosition:NSPopUpArrowAtBottom];
    [_popUpButton setBordered:NO];
    [cell setImageScaling:NSImageScaleProportionallyDown];
    
    [_popUpButton setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
    [cell setControlSize:NSSmallControlSize];
    
    
    // Ensure there is a single item to generate cell appearance from
    if ([[_popUpButton itemArray] count] == 0) [_popUpButton addItemWithTitle:@""];
    
    [self setView:_popUpButton];
    
    return self;
}

- (void)dealloc
{
    [_popUpButton release];
    
    [super dealloc];
}

#pragma mark Properties

- (void)setImage:(NSImage *)anImage
{
    // NSToolbarItem would normally forward this on to its view. That would be fine, but NSPopUpButton ignores -setImage: so handle it ourself
    [[[self popUpButton] itemAtIndex:0] setImage:anImage];
}

- (NSSize)minSize
{
    NSSize result;
    result.height = ([[self toolbar] sizeMode] == NSToolbarSizeModeSmall) ? 24.0f : 32.0f;
    result.width = result.height + 8;   // to account for arrow
    
    return result;
}

@synthesize popUpButton = _popUpButton;

- (NSMenuItem *)menuFormRepresentation
{
    NSMenuItem *result = [super menuFormRepresentation];
    
    // Use a submenu to display the button's items
    NSMenu *submenu = [[[self popUpButton] menu] copy];
    [result setSubmenu:submenu];
    [submenu release];
            
    [result setAction:[self action]]; // otherwise submenu ony appears after mouse is held down
    
    return result;
}

@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.