src/video/cocoa/SDL_cocoaevents.m
changeset 1931 103c6fec2a60
child 1932 dc864bcabcc4
equal deleted inserted replaced
1930:9483df98e011 1931:103c6fec2a60
       
     1 /*
       
     2     SDL - Simple DirectMedia Layer
       
     3     Copyright (C) 1997-2006 Sam Lantinga
       
     4 
       
     5     This library is free software; you can redistribute it and/or
       
     6     modify it under the terms of the GNU Lesser General Public
       
     7     License as published by the Free Software Foundation; either
       
     8     version 2.1 of the License, or (at your option) any later version.
       
     9 
       
    10     This library is distributed in the hope that it will be useful,
       
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13     Lesser General Public License for more details.
       
    14 
       
    15     You should have received a copy of the GNU Lesser General Public
       
    16     License along with this library; if not, write to the Free Software
       
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    18 
       
    19     Sam Lantinga
       
    20     slouken@libsdl.org
       
    21 */
       
    22 #include "SDL_config.h"
       
    23 
       
    24 #include "SDL_cocoavideo.h"
       
    25 
       
    26 /* setAppleMenu disappeared from the headers in 10.4 */
       
    27 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
       
    28 @interface NSApplication(NSAppleMenu)
       
    29 - (void)setAppleMenu:(NSMenu *)menu;
       
    30 @end
       
    31 #endif
       
    32 
       
    33 @interface SDLApplication : NSApplication
       
    34 {
       
    35 }
       
    36 - (void)finishLaunching;
       
    37 @end
       
    38 
       
    39 @implementation SDLApplication
       
    40 
       
    41 - (void)finishLaunching
       
    42 {
       
    43     [super finishLaunching];
       
    44     _running = 1;
       
    45 }
       
    46 
       
    47 @end
       
    48 
       
    49 static NSString *
       
    50 GetApplicationName(void)
       
    51 {
       
    52     NSDictionary *dict;
       
    53     NSString *appName = 0;
       
    54 
       
    55     /* Determine the application name */
       
    56     dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
       
    57     if (dict)
       
    58         appName = [dict objectForKey: @"CFBundleName"];
       
    59     
       
    60     if (![appName length])
       
    61         appName = [[NSProcessInfo processInfo] processName];
       
    62 
       
    63     return appName;
       
    64 }
       
    65 
       
    66 static void
       
    67 CreateApplicationMenus(void)
       
    68 {
       
    69     NSString *appName;
       
    70     NSString *title;
       
    71     NSMenu *appleMenu;
       
    72     NSMenu *windowMenu;
       
    73     NSMenuItem *menuItem;
       
    74     
       
    75     /* Create the main menu bar */
       
    76     [NSApp setMainMenu:[[NSMenu alloc] init]];
       
    77 
       
    78     /* Create the application menu */
       
    79     appName = GetApplicationName();
       
    80     appleMenu = [[NSMenu alloc] initWithTitle:@""];
       
    81     
       
    82     /* Add menu items */
       
    83     title = [@"About " stringByAppendingString:appName];
       
    84     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
       
    85 
       
    86     [appleMenu addItem:[NSMenuItem separatorItem]];
       
    87 
       
    88     title = [@"Hide " stringByAppendingString:appName];
       
    89     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
       
    90 
       
    91     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
       
    92     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
       
    93 
       
    94     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
       
    95 
       
    96     [appleMenu addItem:[NSMenuItem separatorItem]];
       
    97 
       
    98     title = [@"Quit " stringByAppendingString:appName];
       
    99     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
       
   100     
       
   101     /* Put menu into the menubar */
       
   102     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
       
   103     [menuItem setSubmenu:appleMenu];
       
   104     [[NSApp mainMenu] addItem:menuItem];
       
   105     [menuItem release];
       
   106 
       
   107     /* Tell the application object that this is now the application menu */
       
   108     [NSApp setAppleMenu:appleMenu];
       
   109     [appleMenu release];
       
   110 
       
   111 
       
   112     /* Create the window menu */
       
   113     windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
       
   114     
       
   115     /* "Minimize" item */
       
   116     menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
       
   117     [windowMenu addItem:menuItem];
       
   118     [menuItem release];
       
   119     
       
   120     /* Put menu into the menubar */
       
   121     menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
       
   122     [menuItem setSubmenu:windowMenu];
       
   123     [[NSApp mainMenu] addItem:menuItem];
       
   124     [menuItem release];
       
   125     
       
   126     /* Tell the application object that this is now the window menu */
       
   127     [NSApp setWindowsMenu:windowMenu];
       
   128     [windowMenu release];
       
   129 }
       
   130 
       
   131 void
       
   132 Cocoa_RegisterApp(void)
       
   133 {
       
   134     ProcessSerialNumber psn;
       
   135     NSAutoreleasePool *pool;
       
   136 
       
   137     if (!GetCurrentProcess(&psn)) {
       
   138         TransformProcessType(&psn, kProcessTransformToForegroundApplication);
       
   139         SetFrontProcess(&psn);
       
   140     }
       
   141 
       
   142     pool = [[NSAutoreleasePool alloc] init];
       
   143     if (NSApp == nil) {
       
   144         [SDLApplication sharedApplication];
       
   145 
       
   146         if ([NSApp mainMenu] == nil) {
       
   147             CreateApplicationMenus();
       
   148         }
       
   149         [NSApp finishLaunching];
       
   150     }
       
   151     [pool release];
       
   152 }
       
   153 
       
   154 void
       
   155 Cocoa_PumpEvents(_THIS)
       
   156 {
       
   157     NSAutoreleasePool *pool;
       
   158 
       
   159     pool = [[NSAutoreleasePool alloc] init];
       
   160     for ( ; [NSApp isRunning]; ) {
       
   161         NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
       
   162         if ( event == nil ) {
       
   163             break;
       
   164         }
       
   165         [NSApp sendEvent:event];
       
   166     }
       
   167     [pool release];
       
   168 }
       
   169 
       
   170 /* vi: set ts=4 sw=4 expandtab: */