Re: Creating a Cocoa Window in code
Re: Creating a Cocoa Window in code
- Subject: Re: Creating a Cocoa Window in code
- From: "Felipe Monteiro de Carvalho" <email@hidden>
- Date: Fri, 18 Jan 2008 17:08:28 +0100
On Jan 18, 2008 3:00 PM, mmalc crawford <email@hidden> wrote:
> This may be of some help:
> <http://lapcatsoftware.com/blog/2007/11/25/working-without-a-nib-part-6-working-without-a-xib/
Yes, I read it, but there is still something wrong with my code. In
this latest version I have it almost working. Now the Quit button
appears and also works. But I have 2 "Quit cocoawindow" buttons, with
exactly the same name and shortcut key. One works, and the other
doesn't =/ and I can't seam to find what is wrong.
Any ideas? Here is my code, thanks:
#import <Cocoa/Cocoa.h>
void CreateApplicationMenu(NSMenu* menu)
{
NSString * applicationName = [NSLocalizedString(@"cocoawindow", @"The
name of this application") retain];
NSMenuItem * item;
item = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
NSLocalizedString(@"About", nil), applicationName]
action:@selector(orderFrontStandardAboutPanel:)
keyEquivalent:@""];
[item setTarget:NSApp];
[menu addItem:[NSMenuItem separatorItem]];
item = [menu addItemWithTitle:NSLocalizedString(@"Preferences...", nil)
action:NULL
keyEquivalent:@","];
[menu addItem:[NSMenuItem separatorItem]];
item = [menu addItemWithTitle:NSLocalizedString(@"Services", nil)
action:NULL
keyEquivalent:@""];
NSMenu * servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"]
autorelease];
[menu setSubmenu:servicesMenu forItem:item];
[NSApp setServicesMenu:servicesMenu];
[menu addItem:[NSMenuItem separatorItem]];
item = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
NSLocalizedString(@"Hide", nil), applicationName]
action:@selector(hide:)
keyEquivalent:@"h"];
[item setTarget:NSApp];
item = [menu addItemWithTitle:NSLocalizedString(@"Hide Others", nil)
action:@selector(hideOtherApplications:)
keyEquivalent:@"h"];
[item setKeyEquivalentModifierMask:NSCommandKeyMask | NSAlternateKeyMask];
[item setTarget:NSApp];
item = [menu addItemWithTitle:NSLocalizedString(@"Show All", nil)
action:@selector(unhideAllApplications:)
keyEquivalent:@""];
[item setTarget:NSApp];
[menu addItem:[NSMenuItem separatorItem]];
item = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
NSLocalizedString(@"Quit", nil), applicationName]
action:@selector(terminate:)
keyEquivalent:@"q"];
[item setTarget:NSApp];
}
void CreateMenu(void)
{
NSMenu * mainMenu = [[[NSMenu alloc] initWithTitle:@"MainMenu"] autorelease];
NSMenuItem * item;
NSMenu * submenu;
// The titles of the menu items are for identification purposes only
and shouldn't be localized.
// The strings in the menu bar come from the submenu titles,
// except for the application menu, whose title is ignored at runtime.
item = [mainMenu addItemWithTitle:@"Apple" action:NULL keyEquivalent:@""];
submenu = [[[NSMenu alloc] initWithTitle:@"Apple"] autorelease];
[NSApp performSelector:@selector(setAppleMenu:) withObject:submenu];
CreateApplicationMenu(submenu);
[mainMenu setSubmenu:submenu forItem:item];
[NSApp setMainMenu:mainMenu];
}
int main(int argc, char *argv[])
{
// Creates an autorelease pool
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// Loads the application
NSApplicationLoad();
// Creates a simple window
NSRect MainWindowRect = NSMakeRect(300, 300, 300, 500);
NSWindow* MainWindow = [[NSWindow alloc] initWithContentRect: MainWindowRect
styleMask: NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[MainWindow orderFrontRegardless];
// Creates the application object
[NSApplication sharedApplication];
CreateMenu();
// Enters main message loop
[NSApp run];
// Call release method from object pool
[pool release];
return 0;
}
--
Felipe Monteiro de Carvalho
_______________________________________________
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