How to add a pop-up menu to a NSToolbarItem?
Asked Answered
H

5

6

I'm trying to open a pop-up menu from a NSToolbarItem. I tried following this example but I can't use that class method because NSToolbar and NSToolbarItem inherit from NSObject and not from NSView.

Apart from creating a custom view, what is the best way to open a pop-up menu from a NSToolbarItem?

Hardware answered 30/9, 2009 at 18:5 Comment(0)
G
3

FYI: this post is long over but I'm just browsing and I have an easy method for this so I thought I'd give an answer in case someone else looks through it. I found that I can't drag a popup button directly to the toolbar in Interface Builder from the Library. However, I can drag a popup button from the window to the toolbar. So I create the popup button on the window first and then drag that to the toolbar... it works! The same with other objects.

Gog answered 2/4, 2010 at 23:18 Comment(3)
Nothing is over until there's a green checkmark, and sometimes not even then. ☺ Anyway, in 3.2.1, I wasn't able to do it the way you suggested, so I'll edit my answer to include the way I did it.Masterful
In 3.2.1 I can drag a popup button directly to the toolbar from the Library. However, I can still drag one from the window to the toolbar too. To do that make sure the toolbar isn't open by clicking the "Done" button if it's open. Then drag the popup button from the window onto the toolbar and hover over it for a moment... the toolbar will open where you can release the button in the toolbar. I think this method is easier because it seems to be easier to configure the button on the window rather than in the toolbar menu.Gog
regulus6633: Oh, you mean drag it into the Configure Toolbar sheet! I didn't even know it was possible to bring that up in IB (“what ‘Done’ button?”). Now that I've opened that sheet, I'm able to drag controls directly from the Library into it. And you're right that that's a lot easier.Masterful
S
4

Basically, you create something like an NSButton that has an NSMenu attached to it, then use NSToolbarItem's setView: method to embed the button in the toolbarItem.

Seafood answered 30/9, 2009 at 18:41 Comment(2)
Why not add the menu to a view and the view to the toolbar item?Hardware
@Rui isn't that what I suggested? :/Seafood
P
3

Just create an NSView in IB with your menu like you want it. Then in your window controller, add some code like this:

// This assumes you have a window property pointing to the window to which you'll
// add the toolbar. It also assumes you've connected the NSView to add to the
// toolbar to a member called toolbarView.

- (NSArray*)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
    return [NSArray arrayWithObject:@"myToolbarMenu"];
}

- (NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
    return [self toolbarAllowedItemIdentifiers:toolbar];
}

- (NSToolbarItem*)toolbar:(NSToolbar*)toolbar
    itemForItemIdentifier:(NSString*)str
willBeInsertedIntoToolbar:(BOOL)flag
{
    if ([str isEqualToString:@"myToolbarMenu"] == YES) {
        NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:str];
        [item setView:toolbarView];
        [item setMinSize:[toolbarView frame].size];
        [item setMaxSize:[toolbarView frame].size];
        return [item autorelease];  
    }
    return nil;
}

- (void)windowDidLoad {
    NSToolbar* toolbar = [[NSToolbar alloc] initWithIdentifier:@"myToolbar"];
    [toolbar setDelegate:self];
    [self.window setToolbar:[toolbar autorelease]];
}
Plasticine answered 30/9, 2009 at 18:39 Comment(0)
M
3

If you want an actual pop-up button for the toolbar item, set an NSPopUpButton as the toolbar item's view.

In Interface Builder 3.2.1 (I don't know when this ability was actually introduced), you can drill down to the toolbar in the hierarchical list of objects in the nib window, and drag a pop-up button from the Library palette into the toolbar in the list. IB will wrap the button in a toolbar item for you.

Masterful answered 30/9, 2009 at 23:59 Comment(0)
G
3

FYI: this post is long over but I'm just browsing and I have an easy method for this so I thought I'd give an answer in case someone else looks through it. I found that I can't drag a popup button directly to the toolbar in Interface Builder from the Library. However, I can drag a popup button from the window to the toolbar. So I create the popup button on the window first and then drag that to the toolbar... it works! The same with other objects.

Gog answered 2/4, 2010 at 23:18 Comment(3)
Nothing is over until there's a green checkmark, and sometimes not even then. ☺ Anyway, in 3.2.1, I wasn't able to do it the way you suggested, so I'll edit my answer to include the way I did it.Masterful
In 3.2.1 I can drag a popup button directly to the toolbar from the Library. However, I can still drag one from the window to the toolbar too. To do that make sure the toolbar isn't open by clicking the "Done" button if it's open. Then drag the popup button from the window onto the toolbar and hover over it for a moment... the toolbar will open where you can release the button in the toolbar. I think this method is easier because it seems to be easier to configure the button on the window rather than in the toolbar menu.Gog
regulus6633: Oh, you mean drag it into the Configure Toolbar sheet! I didn't even know it was possible to bring that up in IB (“what ‘Done’ button?”). Now that I've opened that sheet, I'm able to drag controls directly from the Library into it. And you're right that that's a lot easier.Masterful
A
2

Assuming menu is an NSMenu object and sender is a NSToolbarItem, then all you need to do is pass in the sender.view to show the menu. No need to add another view if you have already set up the NSToolbarItem via Interface Builder.

[NSMenu popUpContextMenu:menu
               withEvent:[NSApp currentEvent]
                 forView:sender.view];
Aurel answered 10/8, 2015 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.