comparison src/main/macosx/SDLMain.m @ 221:50620ec9c86a

*** empty log message ***
author Sam Lantinga <slouken@libsdl.org>
date Fri, 02 Nov 2001 18:12:52 +0000
parents ba9e0fcc2ae2
children 4e9044b378fd
comparison
equal deleted inserted replaced
220:7861d904fb77 221:50620ec9c86a
1 /* SDLMain.m - main entry point for our Cocoa-ized SDL app 1 /* SDLMain.m - main entry point for our Cocoa-ized SDL app
2 Darrell Walisser - dwaliss1@purdue.edu 2 Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
3 Non-NIB-Code & other changes: Max Horn <max@quendi.de>
3 4
4 Feel free to customize this file to suit your needs 5 Feel free to customize this file to suit your needs
5 */ 6 */
6 7
7 #import "SDL.h" 8 #import "SDL.h"
8 #import "SDLMain.h" 9 #import "SDLMain.h"
9 #import <sys/param.h> /* for MAXPATHLEN */ 10 #import <sys/param.h> /* for MAXPATHLEN */
10 #import <unistd.h> 11 #import <unistd.h>
11 12
13 /* Use this flag to determine whether we use SDLMain.nib or not */
14 #define SDL_USE_NIB_FILE 0
15
16
12 static int gArgc; 17 static int gArgc;
13 static char **gArgv; 18 static char **gArgv;
14 static NSString *gAppName = 0;
15 static BOOL gFinderLaunch; 19 static BOOL gFinderLaunch;
16 20
21 #if SDL_USE_NIB_FILE
22 /* A helper category for NSString */
17 @interface NSString (ReplaceSubString) 23 @interface NSString (ReplaceSubString)
18 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString; 24 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
19 @end 25 @end
20 26 #else
21 27 /* An internal Apple class used to setup Apple menus */
22 /* The main class of the application, the application's delegate */ 28 @interface NSAppleMenuController:NSObject {}
23 @implementation SDLMain 29 - (void)controlMenu:(NSMenu *)aMenu;
24 30 @end
31 #endif
32
33 @interface SDLApplication : NSApplication
34 @end
35
36 @implementation SDLApplication
25 /* Invoked from the Quit menu item */ 37 /* Invoked from the Quit menu item */
26 - (void) quit:(id)sender 38 - (void)terminate:(id)sender
27 { 39 {
40 /* Post a SDL_QUIT event */
28 SDL_Event event; 41 SDL_Event event;
29 event.type = SDL_QUIT; 42 event.type = SDL_QUIT;
30 SDL_PushEvent(&event); 43 SDL_PushEvent(&event);
31 } 44 }
32 45 @end
33 /* Invoked from the Make Full-Screen menu item */ 46
34 - (void) makeFullscreen:(id)sender 47
35 { 48 /* The main class of the application, the application's delegate */
36 /* TODO */ 49 @implementation SDLMain
37 }
38 50
39 /* Set the working directory to the .app's parent directory */ 51 /* Set the working directory to the .app's parent directory */
40 - (void) setupWorkingDirectory:(BOOL)shouldChdir 52 - (void) setupWorkingDirectory:(BOOL)shouldChdir
41 { 53 {
42 char parentdir[MAXPATHLEN]; 54 char parentdir[MAXPATHLEN];
56 if (shouldChdir) 68 if (shouldChdir)
57 { 69 {
58 assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */ 70 assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */
59 assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */ 71 assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */
60 } 72 }
61 /* gAppName = [ NSString stringWithCString: c ]; */ 73 }
62 } 74
75 #if SDL_USE_NIB_FILE
63 76
64 /* Fix menu to contain the real app name instead of "SDL App" */ 77 /* Fix menu to contain the real app name instead of "SDL App" */
65 - (void) fixMenu:(NSMenu *)aMenu 78 - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
66 { 79 {
67 NSRange aRange; 80 NSRange aRange;
68 NSEnumerator *enumerator; 81 NSEnumerator *enumerator;
69 NSMenuItem *menuItem; 82 NSMenuItem *menuItem;
70 83
71 aRange = [[aMenu title] rangeOfString:@"SDL App"]; 84 aRange = [[aMenu title] rangeOfString:@"SDL App"];
72 if (aRange.length != 0) 85 if (aRange.length != 0)
73 [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:gAppName]]; 86 [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
74 87
75 enumerator = [[aMenu itemArray] objectEnumerator]; 88 enumerator = [[aMenu itemArray] objectEnumerator];
76 while ((menuItem = [enumerator nextObject])) 89 while ((menuItem = [enumerator nextObject]))
77 { 90 {
78 aRange = [[menuItem title] rangeOfString:@"SDL App"]; 91 aRange = [[menuItem title] rangeOfString:@"SDL App"];
79 if (aRange.length != 0) 92 if (aRange.length != 0)
80 [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:gAppName]]; 93 [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
81 if ([menuItem hasSubmenu]) 94 if ([menuItem hasSubmenu])
82 [self fixMenu: [menuItem submenu]]; 95 [self fixMenu:[menuItem submenu] withAppName:appName];
83 } 96 }
84 [ aMenu sizeToFit ]; 97 [ aMenu sizeToFit ];
85 } 98 }
86 99
100 #else
101
102 void setupAppleMenu(void)
103 {
104 /* warning: this code is very odd */
105 NSAppleMenuController *appleMenuController;
106 NSMenu *appleMenu;
107 NSMenuItem *appleMenuItem;
108
109 appleMenuController = [[NSAppleMenuController alloc] init];
110 appleMenu = [[NSMenu alloc] initWithTitle:@""];
111 appleMenuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
112
113 [appleMenuItem setSubmenu:appleMenu];
114
115 /* yes, we do need to add it and then remove it --
116 if you don't add it, it doesn't get displayed
117 if you don't remove it, you have an extra, titleless item in the menubar
118 when you remove it, it appears to stick around
119 very, very odd */
120 [[NSApp mainMenu] addItem:appleMenuItem];
121 [appleMenuController controlMenu:appleMenu];
122 [[NSApp mainMenu] removeItem:appleMenuItem];
123 [appleMenu release];
124 [appleMenuItem release];
125 }
126
127 /* Create a window menu */
128 void setupWindowMenu(void)
129 {
130 NSMenu *windowMenu;
131 NSMenuItem *windowMenuItem;
132 NSMenuItem *menuItem;
133
134
135 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
136
137 /* "Minimize" item */
138 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
139 [windowMenu addItem:menuItem];
140 [menuItem release];
141
142 /* Put menu into the menubar */
143 windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
144 [windowMenuItem setSubmenu:windowMenu];
145 [[NSApp mainMenu] addItem:windowMenuItem];
146
147 /* Tell the application object that this is now the window menu */
148 [NSApp setWindowsMenu:windowMenu];
149
150 /* Finally give up our references to the objects */
151 [windowMenu release];
152 [windowMenuItem release];
153 }
154
155 /* Replacement for NSApplicationMain */
156 void CustomApplicationMain (argc, argv)
157 {
158 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
159 SDLMain *sdlMain;
160
161 /* Ensure the application object is initialised */
162 [SDLApplication sharedApplication];
163
164 /* Set up the menubar */
165 [NSApp setMainMenu:[[NSMenu alloc] init]];
166 setupAppleMenu();
167 setupWindowMenu();
168
169 /* Create SDLMain and make it the app delegate */
170 sdlMain = [[SDLMain alloc] init];
171 [NSApp setDelegate:sdlMain];
172
173 /* Start the main event loop */
174 [NSApp run];
175
176 [sdlMain release];
177 [pool release];
178 }
179
180 #endif
181
87 /* Called when the internal event loop has just started running */ 182 /* Called when the internal event loop has just started running */
88 - (void) applicationDidFinishLaunching: (NSNotification *) note 183 - (void) applicationDidFinishLaunching: (NSNotification *) note
89 { 184 {
90 int status; 185 int status;
91 186
92 /* Set the working directory to the .app's parent directory */ 187 /* Set the working directory to the .app's parent directory */
93 [ self setupWorkingDirectory: gFinderLaunch ]; 188 [self setupWorkingDirectory:gFinderLaunch];
94 189
190 #if SDL_USE_NIB_FILE
95 /* Set the main menu to contain the real app name instead of "SDL App" */ 191 /* Set the main menu to contain the real app name instead of "SDL App" */
96 gAppName = [ [ NSBundle mainBundle ] bundleIdentifier ]; 192 [self fixMenu:[NSApp mainMenu] withAppName:[[NSProcessInfo processInfo] processName]];
97 [ self fixMenu: [ NSApp mainMenu ] ]; 193 #endif
98 194
99 /* Hand off to main application code */ 195 /* Hand off to main application code */
100 status = SDL_main (gArgc, gArgv); 196 status = SDL_main (gArgc, gArgv);
101 197
102 /* We're done, thank you for playing */ 198 /* We're done, thank you for playing */
117 NSString *result; 213 NSString *result;
118 214
119 bufferSize = selfLen + aStringLen - aRange.length; 215 bufferSize = selfLen + aStringLen - aRange.length;
120 buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar)); 216 buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
121 217
122 // Get first part into buffer 218 /* Get first part into buffer */
123 localRange.location = 0; 219 localRange.location = 0;
124 localRange.length = aRange.location; 220 localRange.length = aRange.location;
125 [self getCharacters:buffer range:localRange]; 221 [self getCharacters:buffer range:localRange];
126 222
127 // Get middle part into buffer 223 /* Get middle part into buffer */
128 localRange.location = 0; 224 localRange.location = 0;
129 localRange.length = aStringLen; 225 localRange.length = aStringLen;
130 [aString getCharacters:(buffer+aRange.location) range:localRange]; 226 [aString getCharacters:(buffer+aRange.location) range:localRange];
131 227
132 // Get last part into buffer 228 /* Get last part into buffer */
133 localRange.location = aRange.location + aRange.length; 229 localRange.location = aRange.location + aRange.length;
134 localRange.length = selfLen - localRange.location; 230 localRange.length = selfLen - localRange.location;
135 [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; 231 [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
136 232
137 // Build output string 233 /* Build output string */
138 result = [NSString stringWithCharacters:buffer length:bufferSize]; 234 result = [NSString stringWithCharacters:buffer length:bufferSize];
139 235
140 NSDeallocateMemoryPages(buffer, bufferSize); 236 NSDeallocateMemoryPages(buffer, bufferSize);
141 237
142 return result; 238 return result;
143 } 239 }
144 240
145 @end 241 @end
242
146 243
147 244
148 #ifdef main 245 #ifdef main
149 # undef main 246 # undef main
150 #endif 247 #endif
151 248
152 /* Main entry point to executible - should *not* be SDL_main! */ 249
153 int main (int argc, char **argv) { 250 /* Main entry point to executable - should *not* be SDL_main! */
251 int main (int argc, char **argv)
252 {
154 253
155 /* Copy the arguments into a global variable */ 254 /* Copy the arguments into a global variable */
156 int i; 255 int i;
157 256
158 /* This is passed if we are launched by double-clicking */ 257 /* This is passed if we are launched by double-clicking */
163 gArgc = argc; 262 gArgc = argc;
164 gFinderLaunch = NO; 263 gFinderLaunch = NO;
165 } 264 }
166 gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1)); 265 gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1));
167 assert (gArgv != NULL); 266 assert (gArgv != NULL);
168 for (i = 0; i < gArgc; i++) { 267 for (i = 0; i < gArgc; i++)
169 gArgv[i] = argv[i]; 268 gArgv[i] = argv[i];
170 }
171 gArgv[i] = NULL; 269 gArgv[i] = NULL;
172 270
271 #if SDL_USE_NIB_FILE
272 [SDLApplication poseAsClass:[NSApplication class]];
173 NSApplicationMain (argc, argv); 273 NSApplicationMain (argc, argv);
274 #else
275 CustomApplicationMain (argc, argv);
276 #endif
174 return 0; 277 return 0;
175 } 278 }