I have an API that allows users to create popup menus with sub menus, and I'm having problems detecting the selected item when the user clicks on an option that belongs to a sub menu.
So, the display and "construction" of the popup menu is correct and it works fine. Starting from a NSMenu I add a few NSMenuItems, then for some NSMenuItems I add a new NSMenu with a few NSMenuItems more.
The problem begins when I click on an item that belongs to a submenu, the selectedItem I get is always nil. It works fine for the main menu. Here's how I show the menu:
NSRect frame = NSMakeRect(mp.origin.x + 10, mp.origin.y + 10, 1, 1);
NSPopUpButtonCell *cell = [[NSPopUpButtonCell alloc] initTextCell: #"" pullsDown: NO];
[cell setAutoenablesItems: NO];
[cell setAltersStateOfSelectedItem: NO];
[cell setMenu: mainMenu];
[cell selectItem: Nil];
[cell performClickWithFrame: frame inView: [window initialFirstResponder]];
NSMenuItem *xpto = [cell selectedItem];
Related
I am trying to attach an NSMenu item to a NSStatusItem to have a menu when clicking on my Menu Bar App for Mac OS.
I am new to Mac programming and I searched tutorials on the Web. However, all the material I found involves the usage of the file Xib to add the NSMenu and linking it to the existing code. However, I don't have such a file in my project, it only includes the storyboard file.
I hope you can help.
Cheers
You can create a menu programmatically and set it to NSStatusItem like this.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:30];
_statusItem.image = [NSImage imageNamed:#"..."];
// create menu
NSMenu *menu = [[NSMenu alloc] initWithTitle:#""];
NSMenuItem *item1 = [[NSMenuItem alloc] initWithTitle:#"menu1" action:#selector(menu1Action:) keyEquivalent:#""];
NSMenuItem *item2 = [[NSMenuItem alloc] initWithTitle:#"menu2" action:#selector(menu2Action:) keyEquivalent:#""];
[menu addItem:item1];
[menu addItem:item2];
[_statusItem setMenu:menu]; // attach
}
Of course, you can use NSMenu as outlet. To do that, drag NSMenu to Application Scene in the storyboard, and connect it to AppDelegate's outlet.
I'm making popup tooltip in NSWindow, like following XCode tooltip
If user press a button, popup is shown. It is easy.
But after that, if user press any button in this window, popup should be hidden.
But if user press button, nswindow's mousedown: isn't be called. so nswindowcontroller can not receive that event.
How can nswindow can detect all event in window's region?
You can create a contextMenu for small window, that opens on your action.
*NOTE: in the image, that is a custom view, not a contextMenu.*
- (IBAction)button:(id)sender {
NSRect frame = [(NSButton *)sender frame];
NSPoint menuOrigin = [[(NSButton *)sender superview] convertPoint:NSMakePoint(frame.origin.x+80, frame.origin.y+frame.size.height-10)
toView:nil];
NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
location:menuOrigin
modifierFlags:NSLeftMouseDownMask // 0x100
timestamp:0.0
windowNumber:[[(NSButton *)sender window] windowNumber]
context:[[(NSButton *)sender window] graphicsContext]
eventNumber:0
clickCount:1
pressure:1];
NSMenu *menu = [[NSMenu alloc] init];
[menu setAutoenablesItems:NO];
[menu insertItemWithTitle:#"Add Favorite"
action:#selector(addFavorite:)
keyEquivalent:#""
atIndex:0];
[menu insertItem:[NSMenuItem separatorItem] atIndex:1];
[menu insertItemWithTitle:#"Manage Favorite"
action:#selector(manageFavorite:)
keyEquivalent:#""
atIndex:2];
[NSMenu popUpContextMenu:menu withEvent:event forView:(NSButton *)sender];
}
-(IBAction)addFavorite:(id)sender{
NSLog(#"add");
}
-(IBAction)manageFavorite:(id)sender{
NSLog(#"mangage");
}
I have this application that shows an item in the system's status bar, and one of the items is a custom view with a NSTextField and a NSButton. When the user clicks on the status bar item, it shows the menu, the user inputs some text and presses the button. This triggers an action that displays a window.
The problem I'm having now is, when the button is pressed it does trigger the action, but the menu remains visible. I want to hide the menu, because the action has already been processed.
I've searched through the API, but couldn't find how to do it.
Any ideas?
This is how I'm creating the menu:
NSStatusBar *bar = [NSStatusBar systemStatusBar];
self.statusItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[statusItem setImage:[NSImage imageNamed:#"icon_status_bar.png"]];
[statusItem setHighlightMode:YES];
NSMenuItem *textInputItem = [[NSMenuItem alloc] initWithTitle:#"" action:nil keyEquivalent:#""];
[textInputItem setView:myCustomView]; // created on the Nib file...
NSMenu *menu = [[NSMenu alloc] initWithTitle:NSLocalizedString(#"statusBarMenuTitle", #"")];
[menu addItem:textInputItem];
[statusItem setMenu:menu];
[textInputItem release];
[menu release];
It's not obvious in the docs, but [menu cancelTracking] is what you want.
cancelTracking
Dismisses the menu and ends all menu tracking.
- (void)cancelTracking
I want to add a drop down menu in one of the entries in the NSMenu Item. (eg. If you click on the Battery indicator on Finder bar, it has an option for Show->Icon,Time,Percentage).
Now I add a MenuItem using the following code:
menuItem = [menu addItemWithTitle:#"Start"
action:#selector(start:) keyEquivalent:#""];
[menuItem setTarget:self];
How do I add a submenu Item with this drop down list ? Thanks.
This is how I add a submenu to an NSMenu item:
NSMenuItem *mainItem = [[NSMenuItem alloc] init];
[mainItem setTitle:#"Main item"];
NSMenu *submenu = [[NSMenu alloc] init];
[submenu addItemWithTitle:#"Sub item" action:nil keyEquivalent:#""];
[mainItem setSubmenu:submenu];
Got it working. Created a NSPopuButton with contents from an array and then used that here.
[menu setSubmenu:[(NSPopupButton *array) menu] forItem:menuItem];
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.
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];