Hand-building an Application Menu
Hand-building an Application Menu
- Subject: Hand-building an Application Menu
- From: Russ <email@hidden>
- Date: Thu, 16 Oct 2008 15:45:20 -0700 (PDT)
My menus are generated programmatically, not with a NIB (non-negotiable). That works fine. I've been trying to get a correct Application menu, though, without success after a wasted day. The code I have is based on stuff I have found on the web and seems to make sense in principle. It all runs, but the app menu remains missing (the 1st time run) or incomplete (succeeding runs). Code follows, it gets called right after setting up the shared NSApp, before starting the run loop. There are no error messages or crashes---it is an expensive no-op.
Thanks.
void SetAppMenu(int aID, int pID, const char *appname)
{
NSMenu *appmenu, *srvmenu;
NSString *nstr, *astr, *pstr, *hstr, *sstr, *qstr;
NSMenuItem *itm, *aitm, *pitm, *sitm;
if (!menu_delegate)
menu_delegate = [[myMenuDelegate alloc] init];
nstr = [NSString stringWithFormat:@"%s", appname];
astr = [NSString stringWithFormat:@"About %s", appname];
pstr = [NSString stringWithFormat:@"Preferences%C", (unichar)0x2026];
hstr = [NSString stringWithFormat:@"Hide %s", appname];
qstr = [NSString stringWithFormat:@"Quit %s", appname];
sstr = @"Services";
appmenu = setupMenu(nstr);
srvmenu = setupMenu(sstr);
aitm = [[NSMenuItem alloc] initWithTitle:astr
action:@selector(about:) keyEquivalent:@""];
[aitm setTarget:menu_delegate];
[aitm setTag:aID];
[appmenu addItem:aitm];
[appmenu addItem:[NSMenuItem separatorItem]];
pitm = [[NSMenuItem alloc] initWithTitle:pstr
action:@selector(preferences:) keyEquivalent:@","];
[pitm setTarget:menu_delegate];
[pitm setTag:pID];
[appmenu addItem:pitm];
[appmenu addItem:[NSMenuItem separatorItem]];
sitm = [[NSMenuItem alloc] initWithTitle:sstr action:0 keyEquivalent:@""];
[sitm setSubmenu:srvmenu];
[appmenu addItem:sitm];
[appmenu addItem:[NSMenuItem separatorItem]];
itm = [[NSMenuItem alloc] initWithTitle:hstr
action:@selector(hide:) keyEquivalent: @"h"];
[itm setTarget:menu_delegate];
[appmenu addItem:itm];
itm = [[NSMenuItem alloc] initWithTitle: @"Hide Others"
action:@selector(hideOtherApplications:) keyEquivalent:@""];
[itm setTarget:menu_delegate];
[appmenu addItem:itm];
itm = [[NSMenuItem alloc] initWithTitle: @"Show All"
action:@selector(unhideAllApplications:) keyEquivalent:@""];
[itm setTarget:menu_delegate];
[appmenu addItem:itm];
[appmenu addItem:[NSMenuItem separatorItem]];
itm = [[NSMenuItem alloc] initWithTitle:qstr
action:@selector(terminate:) keyEquivalent:@"q"];
[appmenu addItem:itm];
// Does this: [NSApp setAppleMenu:appmenu];
[NSApp performSelector:@selector(setAppleMenu:) withObject:appmenu];
[NSApp setServicesMenu:srvmenu];
}
NSMenu *setupMenu(NSString *title) // title shouldn't be Null
{
NSMenu *men = [[NSMenu alloc] initWithTitle:title];
[men setAutoenablesItems:NO];
if (!menu_delegate)
menu_delegate = [[myMenuDelegate alloc] init];
[men setDelegate:menu_delegate];
return men;
}
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden