Search code examples
cocoansmenunsbutton

How to create NSButton with delayed NSMenu?


In Cocoa i want to create an nsbutton with delayed menu. i.e., When clicked it should call the action method and when kept in pressed state for 2 seconds it should display a nsmenu.

It is similar to "Build Active Target" button present in Xcode toolbar.

Regards,

Dhanaraj.


Solution

  • It's a NSPopUpButton.. Here is how I create it in my app.

    NSToolbarItem* item = [[[NSToolbarItem alloc] initWithItemIdentifier:identifier] autorelease];
    [item setLabel:label];
    [item setPaletteLabel:label];
    [item setToolTip:tooltip];
    
    NSPopUpButton* button = [[[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 200, 24) pullsDown:NO] autorelease];
    NSMenu* menu = [button menu];
    // insert code here, that adds NSMenuItems to the menu
    
    [button setTarget:self];
    [button setAction:@selector(menuAction:)];
    [[button cell] setBezelStyle:NSTexturedRoundedBezelStyle];
    [[button cell] setArrowPosition:NSPopUpArrowAtBottom];
    [[button cell] setFont:[NSFont systemFontOfSize:14]];
    [item setView:button];