My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ImplementationOutline  
How we are going to implement this.
Updated Feb 4, 2010 by gzjj...@gmail.com

initialize a NSApplication:

int main(int argc, const char *argv[])
{
    NSAutoreleasePool *pool;
        
    pool = [NSAutoreleasePool new];

    [NSApplication sharedApplication];
    [NSApp setDelegate:[MyAppDelegate new]];
 
    // create menu here..

    [pool release];
    [NSApp run];

    return 0;
}

Menu creation:

NSMenu APIs matched Carbon Menu Manager APIs perfectly. Let's see how to create an application menu:

static void setApplicationMenu(void)
{
    NSMenu *appleMenu, *services;
    NSMenuItem *menuItem;
    NSString *title;
    NSString *appName;
    
    appName = @"hello";
    appleMenu = [[NSMenu alloc] initWithTitle:@""];

    /* Add menu items */
    title = [@"About " stringByAppendingString:appName];
    [appleMenu addItemWithTitle:title 
                         action:@selector(orderFrontStandardAboutPanel:) 
                  keyEquivalent:@""];

    [appleMenu addItem:[NSMenuItem separatorItem]];

    // Services Menu
    services = [[[NSMenu alloc] init] autorelease];
    [appleMenu addItemWithTitle:@"Services" 
                         action:nil
                  keyEquivalent:@""];
    [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];

    // Hide AppName
    title = [@"Hide " stringByAppendingString:appName];
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];

    // Hide Others
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
                                                  action:@selector(hideOtherApplications:) 
                                           keyEquivalent:@"h"];
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];

    // Show All
    [appleMenu addItemWithTitle:@"Show All" 
                         action:@selector(unhideAllApplications:) 
                  keyEquivalent:@""];

    [appleMenu addItem:[NSMenuItem separatorItem]];

    // Quit AppName
    title = [@"Quit " stringByAppendingString:appName];
    [appleMenu addItemWithTitle:title 
                         action:@selector(terminate:) 
                  keyEquivalent:@"q"];

    /* Put menu into the menubar */
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" 
                                          action:nil 
                                   keyEquivalent:@""];
    [menuItem setSubmenu: appleMenu];
    [[NSApp mainMenu] addItem: menuItem];

    /* Tell the application object that this is now the application menu */
    [NSApp setAppleMenu: appleMenu];
    [NSApp setServicesMenu: services];

    /* Finally give up our references to the objects */
    [appleMenu release];
    [menuItem release];
}

window creation:

Done in applicationDidFinishLaunching: of NSApplication delegate. The APIs are also quite similar with Carbon Window Manager.

@implementation MyAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    createWindow(400, 400);
}
@end

static int createWindow(int width, int height)
{
    NSRect contentRect;
    unsigned int style;

    contentRect = NSMakeRect(0, 0, width, height);
    style = NSTitledWindowMask | 
    NSMiniaturizableWindowMask | 
          NSClosableWindowMask | 
         NSResizableWindowMask;

    g_window = [[NSWindow alloc] initWithContentRect:contentRect 
                                           styleMask:style
                                             backing:NSBackingStoreBuffered 
                                               defer:NO];
    [g_window center];
    [g_window setViewsNeedDisplay:NO];
    [g_window setTitle:@"gVIM on Macintosh"];
    [g_window makeKeyAndOrderFront:nil];
    return 1;
}

Support text input:

Subclass NSView and implement NSTextInput protocol.

Comment by panda...@gmail.com, Nov 4, 2010

能否实现iPad版本的


Sign in to add a comment
Powered by Google Project Hosting