src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 29 Jul 2006 21:51:00 +0000
changeset 1956 ba0d62354872
parent 1937 05e88d266921
child 1959 25d6537feea4
permissions -rw-r--r--
Simplified driver window creation code.
Implemented several Cocoa window functions
     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 @implementation NSApplication(SDL)
    34 - (void)setRunning
    35 {
    36     _running = 1;
    37 }
    38 @end
    39 
    40 @interface SDLAppDelegate : NSObject
    41 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
    42 @end
    43 
    44 @implementation SDLAppDelegate : NSObject
    45 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    46 {
    47     SDL_SendQuit();
    48     return NSTerminateCancel;
    49 }
    50 @end
    51 
    52 static NSString *
    53 GetApplicationName(void)
    54 {
    55     NSDictionary *dict;
    56     NSString *appName = 0;
    57 
    58     /* Determine the application name */
    59     dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
    60     if (dict)
    61         appName = [dict objectForKey: @"CFBundleName"];
    62     
    63     if (![appName length])
    64         appName = [[NSProcessInfo processInfo] processName];
    65 
    66     return appName;
    67 }
    68 
    69 static void
    70 CreateApplicationMenus(void)
    71 {
    72     NSString *appName;
    73     NSString *title;
    74     NSMenu *appleMenu;
    75     NSMenu *windowMenu;
    76     NSMenuItem *menuItem;
    77     
    78     /* Create the main menu bar */
    79     [NSApp setMainMenu:[[NSMenu alloc] init]];
    80 
    81     /* Create the application menu */
    82     appName = GetApplicationName();
    83     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    84     
    85     /* Add menu items */
    86     title = [@"About " stringByAppendingString:appName];
    87     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
    88 
    89     [appleMenu addItem:[NSMenuItem separatorItem]];
    90 
    91     title = [@"Hide " stringByAppendingString:appName];
    92     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
    93 
    94     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
    95     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
    96 
    97     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
    98 
    99     [appleMenu addItem:[NSMenuItem separatorItem]];
   100 
   101     title = [@"Quit " stringByAppendingString:appName];
   102     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
   103     
   104     /* Put menu into the menubar */
   105     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
   106     [menuItem setSubmenu:appleMenu];
   107     [[NSApp mainMenu] addItem:menuItem];
   108     [menuItem release];
   109 
   110     /* Tell the application object that this is now the application menu */
   111     [NSApp setAppleMenu:appleMenu];
   112     [appleMenu release];
   113 
   114 
   115     /* Create the window menu */
   116     windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
   117     
   118     /* "Minimize" item */
   119     menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
   120     [windowMenu addItem:menuItem];
   121     [menuItem release];
   122     
   123     /* Put menu into the menubar */
   124     menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
   125     [menuItem setSubmenu:windowMenu];
   126     [[NSApp mainMenu] addItem:menuItem];
   127     [menuItem release];
   128     
   129     /* Tell the application object that this is now the window menu */
   130     [NSApp setWindowsMenu:windowMenu];
   131     [windowMenu release];
   132 }
   133 
   134 void
   135 Cocoa_RegisterApp(void)
   136 {
   137     ProcessSerialNumber psn;
   138     NSAutoreleasePool *pool;
   139 
   140     if (!GetCurrentProcess(&psn)) {
   141         TransformProcessType(&psn, kProcessTransformToForegroundApplication);
   142         SetFrontProcess(&psn);
   143     }
   144 
   145     pool = [[NSAutoreleasePool alloc] init];
   146     if (NSApp == nil) {
   147         [NSApplication sharedApplication];
   148 
   149         if ([NSApp mainMenu] == nil) {
   150             CreateApplicationMenus();
   151         }
   152         [NSApp finishLaunching];
   153     }
   154     if ([NSApp delegate] == nil) {
   155         [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
   156     }
   157     [NSApp setRunning];
   158     [pool release];
   159 }
   160 
   161 void
   162 Cocoa_PumpEvents(_THIS)
   163 {
   164     NSAutoreleasePool *pool;
   165 
   166     pool = [[NSAutoreleasePool alloc] init];
   167     while ([NSApp isRunning]) {
   168         NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
   169         if ( event == nil ) {
   170             break;
   171         }
   172         [NSApp sendEvent:event];
   173     }
   174     [pool release];
   175 }
   176 
   177 /* vi: set ts=4 sw=4 expandtab: */