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"
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;
33 @implementation NSApplication(SDL)
40 @interface SDLAppDelegate : NSObject
41 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
44 @implementation SDLAppDelegate : NSObject
45 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
48 return NSTerminateCancel;
53 GetApplicationName(void)
56 NSString *appName = 0;
58 /* Determine the application name */
59 dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
61 appName = [dict objectForKey: @"CFBundleName"];
63 if (![appName length])
64 appName = [[NSProcessInfo processInfo] processName];
70 CreateApplicationMenus(void)
78 /* Create the main menu bar */
79 [NSApp setMainMenu:[[NSMenu alloc] init]];
81 /* Create the application menu */
82 appName = GetApplicationName();
83 appleMenu = [[NSMenu alloc] initWithTitle:@""];
86 title = [@"About " stringByAppendingString:appName];
87 [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
89 [appleMenu addItem:[NSMenuItem separatorItem]];
91 title = [@"Hide " stringByAppendingString:appName];
92 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@/*"h"*/""];
94 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@/*"h"*/""];
95 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
97 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
99 [appleMenu addItem:[NSMenuItem separatorItem]];
101 title = [@"Quit " stringByAppendingString:appName];
102 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@/*"q"*/""];
104 /* Put menu into the menubar */
105 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
106 [menuItem setSubmenu:appleMenu];
107 [[NSApp mainMenu] addItem:menuItem];
110 /* Tell the application object that this is now the application menu */
111 [NSApp setAppleMenu:appleMenu];
115 /* Create the window menu */
116 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
118 /* "Minimize" item */
119 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@/*"m"*/""];
120 [windowMenu addItem:menuItem];
123 /* Put menu into the menubar */
124 menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
125 [menuItem setSubmenu:windowMenu];
126 [[NSApp mainMenu] addItem:menuItem];
129 /* Tell the application object that this is now the window menu */
130 [NSApp setWindowsMenu:windowMenu];
131 [windowMenu release];
135 Cocoa_RegisterApp(void)
137 ProcessSerialNumber psn;
138 NSAutoreleasePool *pool;
140 if (!GetCurrentProcess(&psn)) {
141 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
142 SetFrontProcess(&psn);
145 pool = [[NSAutoreleasePool alloc] init];
147 [NSApplication sharedApplication];
149 if ([NSApp mainMenu] == nil) {
150 CreateApplicationMenus();
152 [NSApp finishLaunching];
154 if ([NSApp delegate] == nil) {
155 [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
162 Cocoa_PumpEvents(_THIS)
164 NSAutoreleasePool *pool;
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 ) {
172 switch ([event type]) {
176 Cocoa_HandleKeyEvent(_this, event);
177 /* Fall through to pass event to NSApp; er, nevermind... */
178 /* FIXME: Find a way to stop the beeping, using delegate */
181 [NSApp sendEvent:event];
188 /* vi: set ts=4 sw=4 expandtab: */