> Where do you add it to the main menu?

I'm trying to use NSApplication's setAppleMenu --- the application menu isn't 
visible as part of the main menu (the call is at the end of my routine, below). 
Note that I do a setMainMenu later in my startup sequence. My understanding is 
that OSX has it's own private NSMenu for the application and system menus, and 
combines the mainmenu supplied with those private submenus. So it seems to be a 
battle to get at the semi-private menus, which is a bit maddening.


On Oct 16, 2008, at 5:45 PM, Russ wrote:

> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to