Re: menu bar without nib
Re: menu bar without nib
- Subject: Re: menu bar without nib
- From: j o a r <email@hidden>
- Date: Wed, 31 Dec 2003 10:45:44 +0100
On 2003-12-31, at 05.01, Dmitry Markman wrote:
>
is there a way to set up menu bar with Cocoa but without nib file
I think that you need to do it really early. I'm doing it something
like this:
main.m========================================
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id mainController = [[[MainController alloc] init] autorelease];
[[NSApplication sharedApplication] setDelegate: mainController];
// It's not certain that you need to load a nib file here at all?
// [NSBundle loadNibNamed: @"MyApp" owner: [NSApplication
sharedApplication]];
// This needs to be done this early, or else the application menu
will
// not be set properly on Mac OS X (at least not on Mac OS X 10.3).
[mainController buildMainMenu];
// Start the main event loop
[[NSApplication sharedApplication] run];
[pool release];
return 0;
}
mainController.m==============================
- (void) buildApplicationMenu
{
[self setMainMenu: [NSMenu menuWithTitle: @"" submenus: [NSArray
arrayWithObjects:
[self applicationMenu],
[self editMenu],
[self windowMenu],
[self helpMenu],
nil]]];
[self setAppleMenu: [self applicationMenu]];
[self setServicesMenu: [[[self applicationMenu] itemWithTitle:
kServices] submenu]];
}
==============================================
OK - so obviously I have a couple of convenience methods for menu
creation that I use in the method above. I'm sure you can figure out
how they are implemented.
As an example, this is how the application menu could be created:
mainController.m==============================
NSString * const kServices = @"Services";
- (NSMenu *) applicationMenu
{
static NSMenu *applicationMenu = nil;
if (applicationMenu == nil)
{
// Weak reference, retained by the main menu
applicationMenu = [NSMenu menuWithTitle: @"MyApp" menuItems: [NSArray
arrayWithObjects:
[NSMenuItem itemWithTitle: @"About MyApp" action: @selector(about:)
target: [self delegate]],
[NSMenuItem itemWithTitle: @"Preferences..." action:
@selector(preferences:) target: [self delegate] keyEquivalent: @","],
[NSMenuItem separatorItem],
[NSMenuItem itemWithTitle: kServices submenuWithItems: [NSArray
array]],
[NSMenuItem separatorItem],
[NSMenuItem itemWithTitle: @"Hide Application" action:
@selector(hide:) keyEquivalent: @"h"],
[NSMenuItem itemWithTitle: @"Hide Others" action:
@selector(hideOtherApplications:)],
[NSMenuItem itemWithTitle: @"Show All" action:
@selector(unhideAllApplications:)],
[NSMenuItem separatorItem],
[NSMenuItem itemWithTitle: @"Quit MyApp" action:
@selector(terminate:) keyEquivalent: @"q"],
nil]];
}
return applicationMenu;
}
==============================================
Again, more convenience methods used - either implement them, or
replace them with the standard methods from the Kit.
j o a r
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.