2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 #include "SDL_cocoavideo.h"
25 #include "../../events/SDL_events_c.h"
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;
34 @implementation NSApplication(SDL)
41 @interface SDLAppDelegate : NSObject
42 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
45 @implementation SDLAppDelegate : NSObject
46 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
49 return NSTerminateCancel;
54 GetApplicationName(void)
57 NSString *appName = 0;
59 /* Determine the application name */
60 dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
62 appName = [dict objectForKey: @"CFBundleName"];
64 if (![appName length])
65 appName = [[NSProcessInfo processInfo] processName];
71 CreateApplicationMenus(void)
79 /* Create the main menu bar */
80 [NSApp setMainMenu:[[NSMenu alloc] init]];
82 /* Create the application menu */
83 appName = GetApplicationName();
84 appleMenu = [[NSMenu alloc] initWithTitle:@""];
87 title = [@"About " stringByAppendingString:appName];
88 [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
90 [appleMenu addItem:[NSMenuItem separatorItem]];
92 title = [@"Hide " stringByAppendingString:appName];
93 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@/*"h"*/""];
95 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@/*"h"*/""];
96 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
98 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
100 [appleMenu addItem:[NSMenuItem separatorItem]];
102 title = [@"Quit " stringByAppendingString:appName];
103 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@/*"q"*/""];
105 /* Put menu into the menubar */
106 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
107 [menuItem setSubmenu:appleMenu];
108 [[NSApp mainMenu] addItem:menuItem];
111 /* Tell the application object that this is now the application menu */
112 [NSApp setAppleMenu:appleMenu];
116 /* Create the window menu */
117 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
119 /* "Minimize" item */
120 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@/*"m"*/""];
121 [windowMenu addItem:menuItem];
124 /* Put menu into the menubar */
125 menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
126 [menuItem setSubmenu:windowMenu];
127 [[NSApp mainMenu] addItem:menuItem];
130 /* Tell the application object that this is now the window menu */
131 [NSApp setWindowsMenu:windowMenu];
132 [windowMenu release];
136 Cocoa_RegisterApp(void)
138 ProcessSerialNumber psn;
139 NSAutoreleasePool *pool;
141 if (!GetCurrentProcess(&psn)) {
142 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
143 SetFrontProcess(&psn);
146 pool = [[NSAutoreleasePool alloc] init];
148 [NSApplication sharedApplication];
150 if ([NSApp mainMenu] == nil) {
151 CreateApplicationMenus();
153 [NSApp finishLaunching];
155 if ([NSApp delegate] == nil) {
156 [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
163 Cocoa_PumpEvents(_THIS)
165 NSAutoreleasePool *pool;
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 ) {
173 switch ([event type]) {
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 */
182 [NSApp sendEvent:event];
189 /* vi: set ts=4 sw=4 expandtab: */