src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga
Mon, 01 Sep 2008 16:04:20 +0000
changeset 2738 79c1bd651f04
parent 1962 c92e5f3e68d9
child 2859 99210400e8b9
permissions -rw-r--r--
Fixed a bunch of compile warnings on Mac OS X
     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 #include "../../events/SDL_events_c.h"
    26 
    27 /* setAppleMenu disappeared from the headers in 10.4 */
    28 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
    29 @interface NSApplication(NSAppleMenu)
    30 - (void)setAppleMenu:(NSMenu *)menu;
    31 @end
    32 #endif
    33 
    34 @implementation NSApplication(SDL)
    35 - (void)setRunning
    36 {
    37     _running = 1;
    38 }
    39 @end
    40 
    41 @interface SDLAppDelegate : NSObject
    42 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
    43 @end
    44 
    45 @implementation SDLAppDelegate : NSObject
    46 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    47 {
    48     SDL_SendQuit();
    49     return NSTerminateCancel;
    50 }
    51 @end
    52 
    53 static NSString *
    54 GetApplicationName(void)
    55 {
    56     NSDictionary *dict;
    57     NSString *appName = 0;
    58 
    59     /* Determine the application name */
    60     dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
    61     if (dict)
    62         appName = [dict objectForKey: @"CFBundleName"];
    63     
    64     if (![appName length])
    65         appName = [[NSProcessInfo processInfo] processName];
    66 
    67     return appName;
    68 }
    69 
    70 static void
    71 CreateApplicationMenus(void)
    72 {
    73     NSString *appName;
    74     NSString *title;
    75     NSMenu *appleMenu;
    76     NSMenu *windowMenu;
    77     NSMenuItem *menuItem;
    78     
    79     /* Create the main menu bar */
    80     [NSApp setMainMenu:[[NSMenu alloc] init]];
    81 
    82     /* Create the application menu */
    83     appName = GetApplicationName();
    84     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    85     
    86     /* Add menu items */
    87     title = [@"About " stringByAppendingString:appName];
    88     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
    89 
    90     [appleMenu addItem:[NSMenuItem separatorItem]];
    91 
    92     title = [@"Hide " stringByAppendingString:appName];
    93     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@/*"h"*/""];
    94 
    95     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@/*"h"*/""];
    96     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
    97 
    98     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
    99 
   100     [appleMenu addItem:[NSMenuItem separatorItem]];
   101 
   102     title = [@"Quit " stringByAppendingString:appName];
   103     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@/*"q"*/""];
   104     
   105     /* Put menu into the menubar */
   106     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
   107     [menuItem setSubmenu:appleMenu];
   108     [[NSApp mainMenu] addItem:menuItem];
   109     [menuItem release];
   110 
   111     /* Tell the application object that this is now the application menu */
   112     [NSApp setAppleMenu:appleMenu];
   113     [appleMenu release];
   114 
   115 
   116     /* Create the window menu */
   117     windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
   118     
   119     /* "Minimize" item */
   120     menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@/*"m"*/""];
   121     [windowMenu addItem:menuItem];
   122     [menuItem release];
   123     
   124     /* Put menu into the menubar */
   125     menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
   126     [menuItem setSubmenu:windowMenu];
   127     [[NSApp mainMenu] addItem:menuItem];
   128     [menuItem release];
   129     
   130     /* Tell the application object that this is now the window menu */
   131     [NSApp setWindowsMenu:windowMenu];
   132     [windowMenu release];
   133 }
   134 
   135 void
   136 Cocoa_RegisterApp(void)
   137 {
   138     ProcessSerialNumber psn;
   139     NSAutoreleasePool *pool;
   140 
   141     if (!GetCurrentProcess(&psn)) {
   142         TransformProcessType(&psn, kProcessTransformToForegroundApplication);
   143         SetFrontProcess(&psn);
   144     }
   145 
   146     pool = [[NSAutoreleasePool alloc] init];
   147     if (NSApp == nil) {
   148         [NSApplication sharedApplication];
   149 
   150         if ([NSApp mainMenu] == nil) {
   151             CreateApplicationMenus();
   152         }
   153         [NSApp finishLaunching];
   154     }
   155     if ([NSApp delegate] == nil) {
   156         [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
   157     }
   158     [NSApp setRunning];
   159     [pool release];
   160 }
   161 
   162 void
   163 Cocoa_PumpEvents(_THIS)
   164 {
   165     NSAutoreleasePool *pool;
   166 
   167     pool = [[NSAutoreleasePool alloc] init];
   168     while ([NSApp isRunning]) {
   169         NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
   170         if ( event == nil ) {
   171             break;
   172         }
   173         switch ([event type]) {
   174         case NSKeyDown:
   175         case NSKeyUp:
   176         case NSFlagsChanged:
   177             Cocoa_HandleKeyEvent(_this, event);
   178             /* Fall through to pass event to NSApp; er, nevermind... */
   179             /* FIXME: Find a way to stop the beeping, using delegate */
   180             break;
   181         default:
   182             [NSApp sendEvent:event];
   183             break;
   184         }
   185     }
   186     [pool release];
   187 }
   188 
   189 /* vi: set ts=4 sw=4 expandtab: */