Skip navigation.
 
mlRe: Initializing the menubar
FROM : Robert Nikander
DATE : Sun Nov 04 01:15:43 2007

Okay... I've got a menu to show up, but the behavior seems to depend 
on the location of the executable file, which I don't understand.

If I put the code below in a file called "cocoa.m" and compile it 
from the command line with:

    gcc -o cocoa cocoa.m -framework Cocoa

and I run it like this:

    ./cocoa

then no menu shows up.  But if I copy the binary into a bundle 
directory, like this:

    cp cocoa CocoaTest.app/Contents/MacOS/CocoaTest

and run that same binary, at the different location:

    ./CocoaTest.app/Contents/MacOS/CocoaTest

then the menu bar shows up.

Why the difference?  Does a cocoa binary have to run from within a 
bundle directory?

thanks,
Rob



---

#import <Cocoa/Cocoa.h>

int main()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSApplication *app = [NSApplication sharedApplication];

    id window = [[NSWindow alloc] initWithContentRect: NSMakeRect
(10,10,100,100)
                                  styleMask: NSTitledWindowMask | 
NSResizableWindowMask | NSClosableWindowMask | 
NSMiniaturizableWindowMask
                                  backing: NSBackingStoreBuffered
                                  defer: NO];

    NSMenu *mainMenu = [[NSMenu alloc] initWithTitle: @"MainMenu"];
    NSMenuItem *mi;
    NSMenu *m;
    mi = [mainMenu addItemWithTitle:@"Apple" action:NULL 
keyEquivalent:@""];
    m = [[NSMenu alloc] initWithTitle:@"Apple"];
    // strange hack
    [NSApp performSelector:@selector(setAppleMenu:) withObject: m];
    [mainMenu setSubmenu:m forItem:mi];
    mi = [m addItemWithTitle: @"Test Item"
            action: nil
            keyEquivalent: @""];

    [app setMainMenu: mainMenu];

    [window makeKeyAndOrderFront: window];

    [app run];
    [pool release];
}

Related mailsAuthorDate
mlInitializing the menubar without Interface Builder Robert Nikander Nov 3, 18:12
mlRe: Initializing the menubar without Interface Builder Jeff Johnson Nov 3, 18:37
mlRe: Initializing the menubar without Interface Builder Jeff Johnson Nov 3, 18:46
mlRe: Initializing the menubar without Interface Builder mmalc crawford Nov 3, 19:29
mlRe: Initializing the menubar without Interface Builder Robert Nikander Nov 3, 19:54
mlRe: Initializing the menubar without Interface Builder Robert Nikander Nov 3, 20:08
mlRe: Initializing the menubar without Interface Builder mmalc crawford Nov 4, 00:13
mlRe: Initializing the menubar Robert Nikander Nov 4, 01:15
mlRe: Initializing the menubar Eric Schlegel Nov 4, 17:25
mlRe: Initializing the menubar Robert Nikander Nov 4, 18:05