• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSMenu icon
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMenu icon


  • Subject: Re: NSMenu icon
  • From: Ron Fleckner <email@hidden>
  • Date: Thu, 25 Dec 2008 08:44:03 +1100


On 25/12/2008, at 8:11 AM, John Nairn wrote:

I wanted to make one of the main menus be an icon (like the "Script" icon in many applications). I tried to due using Apple sample code (MenuItemView) and I can insert image in items, but not in the MenuItem used for the title. How is that done?

Here is the code. The first section creates item for the menu bar and adds it to the main menu. The second section adds one item to the menu. The image works for the item in the menu, but not for the display in the menu bar:

// create new menu from scratch trying to use an image
NSMenuItem* newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"Custom Menu" action:NULL keyEquivalent:@""];
NSMenu* newMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@"Custom"];
[newItem setEnabled:YES];
NSImageView *menuImage=[[NSImageView alloc] initWithFrame:NSMakeRect(0.,0.,16.,16.)];
[menuImage setImage:[[NSImage imageNamed:@"bar_image.tiff"] retain]];
[newItem setView: menuImage];
[newItem setSubmenu:newMenu];
[newMenu release];
[[NSApp mainMenu] insertItem:newItem atIndex:[[NSApp mainMenu] numberOfItems]-1];
[newItem release];

// insert image in the first item in the menu
newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"Custom Item 1" action:@selector(menuItem1Action:) keyEquivalent:@""];
[newItem setEnabled:YES];
menuImage=[[NSImageView alloc] initWithFrame:NSMakeRect(0.,0.,32.,32.)];
[menuImage setImage:[[NSImage imageNamed:@"item_image.tiff"] retain]];
[newItem setView: menuImage];
[newItem setTarget:self];
[newMenu addItem:newItem];
[newItem release];


---------------
John Nairn
GEDitCOM - Genealogy Software for the Macintosh
http://www.geditcom.com

Hi John,

I've done this (on Tiger, still works in Leopard). The only real difference I can see in your code is that you use an NSImageView to contain the image. The relevant parts of my code:

- (BOOL)addScriptMenu
{

BOOL success = NO;
NSMenuItem *newItem;
NSString *title = nil;

NSImage *scriptImage = [NSImage imageNamed:@"scripticon"];
if (!scriptImage)
{
title = @"Scripts";
//NSLog(@"scripticon.tiff not found.");
}
else
title = @"";

// Make the menu
newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:title action:NULL keyEquivalent:@""];
[newItem setImage:scriptImage];
[newItem setTag:SCRIPTMENUTAG];
scriptMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:title];
[newItem setSubmenu:scriptMenu];


// Add the menu and it's submenu to the app's main menu
[[NSApp mainMenu] insertItem:newItem atIndex:insertionIndex];
[newItem release];

// Recursively fills menus with script files or script app files in all subdirectories
// of the Scripts menu folder.
[self fillMenu:scriptMenu withDirectory:scriptsFolderPath];

if (scriptMenu)
success = YES;
return success;
}



HTH, Ron _______________________________________________

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


References: 
 >NSMenu icon (From: John Nairn <email@hidden>)

  • Prev by Date: NSMenu icon
  • Next by Date: Re: Thread crashing problem
  • Previous by thread: NSMenu icon
  • Next by thread: about trackingArea & cursorUpdate
  • Index(es):
    • Date
    • Thread