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