Adding/removing menus to/from menu bar (was: Problems with NSMenu...)
Adding/removing menus to/from menu bar (was: Problems with NSMenu...)
- Subject: Adding/removing menus to/from menu bar (was: Problems with NSMenu...)
- From: Andreas Schweizer <email@hidden>
- Date: Fri, 7 Sep 2001 20:39:44 +0200
Hi,
the method in my previous post caused the title of the application menu
to be set to 'Apple'. To avoid this, the original menu item has to be
used instead of a copy. The following two methods take care of that:
// workaround for pre-10.1
- (void)_addItemToMainMenu:(NSMenuItem *)item
{
NSMenu *mainMenu = [NSApp mainMenu];
NSMenu *newMenu;
NSMenuItem *curItem;
int i;
if (mainMenu) {
newMenu = [[NSMenu alloc] initWithTitle:@"MainMenu"];
// move all menu items from the current mainMenu to our newMenu
for (i=[mainMenu numberOfItems]-1; i>=0; i--) {
curItem = [[mainMenu itemAtIndex:i] retain];
[mainMenu removeItem:curItem];
[newMenu insertItem:curItem atIndex:0];
[curItem release];
}
// add item that user actually asked us to add
[newMenu addItem:item];
[NSApp setMainMenu:newMenu];
}
}
// workaround for pre-10.1
- (void)_removeItemFromMainMenu:(NSMenuItem *)item
{
NSMenu *mainMenu = [NSApp mainMenu];
NSMenu *newMenu;
NSMenuItem *curItem;
int i;
if (mainMenu) {
newMenu = [[NSMenu alloc] initWithTitle:@"MainMenu"];
// move all menu items from the current mainMenu to our newMenu
// except the one that the user has asked us to remove
for (i=[mainMenu numberOfItems]-1; i>=0; i--) {
curItem = [[mainMenu itemAtIndex:i] retain];
[mainMenu removeItem:curItem];
if (curItem != item) [newMenu insertItem:curItem atIndex:0];
[curItem release];
}
[NSApp setMainMenu:newMenu];
}
}
Nice weekend,
Andy