Subversion Repositories Games.Rick Dangerous

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 4
Line 1... Line 1...
1
/*   SDLMain.m - main entry point for our Cocoa-ized SDL app
1
// osx-sdlmain.m -- main entry point for our Cocoaized SDL app
2
       Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
-
 
3
       Non-NIB-Code & other changes: Max Horn <max@quendi.de>
-
 
4
 
-
 
5
    Feel free to customize this file to suit your needs
-
 
6
*/
-
 
7
 
2
 
-
 
3
#import <Cocoa/Cocoa.h>
-
 
4
#include "string.h"
8
#include "SDL.h"
5
#include "SDL.h"
9
#include "osx-sdlmain.h"
-
 
10
#include <sys/param.h> /* for MAXPATHLEN */
-
 
11
#include <unistd.h>
-
 
12
 
6
 
13
/* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
-
 
14
 but the method still is there and works. To avoid warnings, we declare
-
 
15
 it ourselves here. */
-
 
16
@interface NSApplication(SDL_Missing_Methods)
-
 
17
- (void)setAppleMenu:(NSMenu *)menu;
-
 
18
@end
-
 
19
 
7
 
20
/* Use this flag to determine whether we use SDLMain.nib or not */
8
// global variables used in this module only
21
#define         SDL_USE_NIB_FILE        0
9
static int global_argc = 0;
-
 
10
static char **global_argv = NULL;
-
 
11
static BOOL has_app_started = FALSE;
22
 
12
 
23
/* Use this flag to determine whether we use CPS (docking) or not */
-
 
24
#define         SDL_USE_CPS             1
-
 
25
#ifdef SDL_USE_CPS
-
 
26
/* Portions of CPS.h */
-
 
27
typedef struct CPSProcessSerNum
-
 
28
{
-
 
29
        UInt32          lo;
-
 
30
        UInt32          hi;
-
 
31
} CPSProcessSerNum;
-
 
32
 
13
 
33
extern OSErr    CPSGetCurrentProcess( CPSProcessSerNum *psn);
14
@interface SDLMainAppDelegate : NSObject
34
extern OSErr    CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
15
- (void) applicationDidFinishLaunching: (NSNotification *) notification;
35
extern OSErr    CPSSetFrontProcess( CPSProcessSerNum *psn);
16
- (BOOL) application: (NSApplication *) the_app openFile: (NSString *) filename;
-
 
17
@end
36
 
18
 
37
#endif /* SDL_USE_CPS */
-
 
38
 
-
 
39
static int    gArgc;
-
 
40
static char  **gArgv;
-
 
41
static BOOL   gFinderLaunch;
-
 
42
static BOOL   gCalledAppMainline = FALSE;
-
 
43
 
-
 
44
static NSString *getApplicationName(void)
19
@interface NSApplication (SDL_Missing_Methods)
45
{
-
 
46
    const NSDictionary *dict;
-
 
47
    NSString *appName = 0;
-
 
48
 
-
 
49
    /* Determine the application name */
-
 
50
    dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
20
// for some reaon, Apple removed setAppleMenu from the headers in 10.4, but the method still is there and works. To avoid warnings, we declare it ourselves here
51
    if (dict)
-
 
52
        appName = [dict objectForKey: @"CFBundleName"];
-
 
53
   
-
 
54
    if (![appName length])
-
 
55
        appName = [[NSProcessInfo processInfo] processName];
-
 
56
 
-
 
57
    return appName;
-
 
58
}
-
 
59
 
-
 
60
#if SDL_USE_NIB_FILE
-
 
61
/* A helper category for NSString */
-
 
62
@interface NSString (ReplaceSubString)
21
- (void) setAppleMenu: (NSMenu *) menu;
63
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
-
 
64
@end
22
@end
65
#endif
-
 
66
 
23
 
67
@interface NSApplication (SDLApplication)
24
@interface NSApplication (SDLApplication)
-
 
25
- (void) quitMenuCommand: (id) sender;
-
 
26
- (void) fullscreenMenuCommand: (id) sender;
-
 
27
- (void) visitwebsiteMenuCommand: (id) sender;
68
@end
28
@end
-
 
29
 
-
 
30
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69
 
31
 
70
@implementation NSApplication (SDLApplication)
32
@implementation NSApplication (SDLApplication)
71
/* Invoked from the Quit menu item */
-
 
-
 
33
 
72
- (void)terminate:(id)sender
34
- (void) quitMenuCommand: (id) sender
73
{
35
{
74
    /* Post a SDL_QUIT event */
36
   // this functin is invoked when the Quit menu item is clicked
75
    SDL_Event event;
-
 
76
    event.type = SDL_QUIT;
-
 
77
    SDL_PushEvent(&event);
-
 
78
}
-
 
79
@end
-
 
80
 
37
 
81
/* The main class of the application, the application's delegate */
-
 
82
@implementation SDLMain
-
 
83
 
-
 
84
/* Set the working directory to the .app's parent directory */
-
 
85
- (void) setupWorkingDirectory:(BOOL)shouldChdir
-
 
86
{
-
 
87
    if (shouldChdir)
38
   SDL_Event event;
88
    {
-
 
89
        char parentdir[MAXPATHLEN];
39
   event.type = SDL_QUIT;
90
        CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
-
 
91
        CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
-
 
92
        if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) {
-
 
93
            chdir(parentdir);   /* chdir to the binary app's parent */
40
   SDL_PushEvent (&event); // post a SDL_QUIT event
94
        }
-
 
95
        CFRelease(url);
-
 
96
        CFRelease(url2);
-
 
97
    }
-
 
98
}
41
}
99
 
42
 
100
#if SDL_USE_NIB_FILE
-
 
101
 
43
 
102
/* Fix menu to contain the real app name instead of "SDL App" */
-
 
103
- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
44
- (void) fullscreenMenuCommand: (id) sender
104
{
45
{
-
 
46
    // this functin is invoked when the Enter Full Screen menu item is clicked
105
    NSRange aRange;
47
   
106
    NSEnumerator *enumerator;
-
 
107
    NSMenuItem *menuItem;
48
    SDL_Event event;
108
 
49
 
109
    aRange = [[aMenu title] rangeOfString:@"SDL App"];
50
    event.type = SDL_KEYDOWN;
110
    if (aRange.length != 0)
51
    event.key.keysym.sym = SDLK_LALT;
111
        [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
52
    SDL_PushEvent (&event); // post a SDL_KEY event that simulates pressing the left ALT key
112
 
-
 
113
    enumerator = [[aMenu itemArray] objectEnumerator];
53
    event.type = SDL_KEYDOWN;
114
    while ((menuItem = [enumerator nextObject]))
54
    event.key.keysym.sym = SDLK_RETURN;
115
    {
-
 
116
        aRange = [[menuItem title] rangeOfString:@"SDL App"];
55
    SDL_PushEvent (&event); // post a SDL_KEY event that simulates pressing the main ENTER key
117
        if (aRange.length != 0)
56
    event.type = SDL_KEYUP;
-
 
57
    event.key.keysym.sym = SDLK_RETURN;
118
            [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
58
    SDL_PushEvent (&event); // post a SDL_KEY event that simulates releasing the main ENTER key
-
 
59
    event.type = SDL_KEYUP;
119
        if ([menuItem hasSubmenu])
60
    event.key.keysym.sym = SDLK_LALT;
120
            [self fixMenu:[menuItem submenu] withAppName:appName];
61
    SDL_PushEvent (&event); // post a SDL_KEY event that simulates releasing the left ALT key
121
    }
-
 
122
}
62
}
123
 
63
 
124
#else
-
 
125
 
-
 
126
static void setApplicationMenu(void)
64
- (void) visitwebsiteMenuCommand: (id) sender
127
{
65
{
128
    /* warning: this code is very odd */
-
 
129
    NSMenu *appleMenu;
-
 
130
    NSMenuItem *menuItem;
-
 
131
    NSString *title;
-
 
132
    NSString *appName;
-
 
133
   
-
 
134
    appName = getApplicationName();
-
 
135
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
-
 
136
   
-
 
137
    /* Add menu items */
-
 
138
    title = [@"About " stringByAppendingString:appName];
66
   // this functin is invoked when the Visit Website menu item is clicked
139
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
-
 
140
 
67
 
141
    [appleMenu addItem:[NSMenuItem separatorItem]];
68
   [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: @"http://www.pmbaty.com/rick/"]]; // visit the website!
-
 
69
}
142
 
70
 
143
    title = [@"Hide " stringByAppendingString:appName];
-
 
144
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
-
 
-
 
71
@end
145
 
72
 
146
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
73
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
-
 
148
 
74
 
149
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
75
@implementation SDLMainAppDelegate
150
 
76
 
151
    [appleMenu addItem:[NSMenuItem separatorItem]];
77
- (void) applicationDidFinishLaunching: (NSNotification *) notification
-
 
78
{
-
 
79
   // this function is called when the internal event loop has just started running
152
 
80
 
153
    title = [@"Quit " stringByAppendingString:appName];
81
   int status;
154
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
-
 
155
 
82
 
156
   
-
 
157
    /* Put menu into the menubar */
-
 
158
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
83
   has_app_started = TRUE; // remember the app has started (so we can no longer change argc and argv)
159
    [menuItem setSubmenu:appleMenu];
-
 
160
    [[NSApp mainMenu] addItem:menuItem];
84
   status = SDL_main (global_argc, global_argv); // only now that we know Cocoa has finished its business, we can run SDL_main() and pass it our command-line arguments
161
 
85
 
162
    /* Tell the application object that this is now the application menu */
-
 
163
    [NSApp setAppleMenu:appleMenu];
-
 
164
 
-
 
165
    /* Finally give up our references to the objects */
86
   exit (status); // when SDL_main returns, we can tell Cocoa to quit
166
    [appleMenu release];
-
 
167
    [menuItem release];
-
 
168
}
87
}
169
 
88
 
170
/* Create a window menu */
-
 
171
static void setupWindowMenu(void)
-
 
172
{
-
 
173
    NSMenu      *windowMenu;
-
 
174
    NSMenuItem  *windowMenuItem;
-
 
175
    NSMenuItem  *menuItem;
-
 
176
 
89
 
177
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
90
- (BOOL) application: (NSApplication *) the_app openFile: (NSString *) filename
-
 
91
{
-
 
92
    // this function catches document open requests and appends them to global argc and argv arrays, so they look like a command-line argument.
-
 
93
    // This lets us notice files when the app was launched by double-clicking a document, or when a document was dragged/dropped on the app's icon.
-
 
94
    // You need to have a CFBundleDocumentsType section in your Info.plist to get this message.
-
 
95
    // Once the app's main loop has started, this message can no longer be used (since it has already parsed its command-line arguments).
-
 
96
   
-
 
97
    char **new_argv;
-
 
98
   
-
 
99
    if (has_app_started)
-
 
100
        return (FALSE); // if the app has started, then argc and argv can no longer be modified
178
   
101
   
-
 
102
    // reallocate the global argv array to hold one parameter more
-
 
103
    new_argv = (char **) realloc (global_argv, (global_argc + 2) * sizeof (char *));
179
    /* "Minimize" item */
104
    if (new_argv == NULL)
180
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
105
        return (FALSE); // on realloc() failure, give up
181
    [windowMenu addItem:menuItem];
106
    global_argv = new_argv;
182
    [menuItem release];
107
    global_argc++;
183
   
108
   
184
    /* Put menu into the menubar */
-
 
185
    windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
109
    // now store the filename we're opening as a new command-line argument
186
    [windowMenuItem setSubmenu:windowMenu];
110
    global_argv[global_argc - 1] = strdup ([filename UTF8String]);
187
    [[NSApp mainMenu] addItem:windowMenuItem];
111
    global_argv[global_argc] = NULL;
188
   
112
   
189
    /* Tell the application object that this is now the window menu */
-
 
190
    [NSApp setWindowsMenu:windowMenu];
-
 
191
 
-
 
192
    /* Finally give up our references to the objects */
113
    return (TRUE); // message was handled successfully
193
    [windowMenu release];
-
 
194
    [windowMenuItem release];
-
 
195
}
114
}
-
 
115
@end
196
 
116
 
197
/* Replacement for NSApplicationMain */
-
 
198
static void CustomApplicationMain (int argc, char **argv)
-
 
199
{
-
 
200
    NSAutoreleasePool   *pool = [[NSAutoreleasePool alloc] init];
117
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201
    SDLMain                             *sdlMain;
-
 
202
 
118
 
203
    /* Ensure the application object is initialised */
-
 
204
    [NSApplication sharedApplication];
-
 
205
   
-
 
206
#ifdef SDL_USE_CPS
-
 
207
    {
-
 
208
        CPSProcessSerNum PSN;
-
 
209
        /* Tell the dock about us */
-
 
210
        if (!CPSGetCurrentProcess(&PSN))
-
 
211
            if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
-
 
212
                if (!CPSSetFrontProcess(&PSN))
-
 
213
                    [NSApplication sharedApplication];
-
 
214
    }
-
 
215
#endif /* SDL_USE_CPS */
-
 
216
 
-
 
217
    /* Set up the menubar */
-
 
218
    [NSApp setMainMenu:[[NSMenu alloc] init]];
-
 
219
    setApplicationMenu();
-
 
220
    setupWindowMenu();
-
 
221
 
-
 
222
    /* Create SDLMain and make it the app delegate */
-
 
223
    sdlMain = [[SDLMain alloc] init];
-
 
224
    [NSApp setDelegate:sdlMain];
-
 
225
   
-
 
226
    /* Start the main event loop */
-
 
227
    [NSApp run];
-
 
228
   
-
 
229
    [sdlMain release];
-
 
230
    [pool release];
-
 
231
}
-
 
232
 
119
 
-
 
120
#ifdef main
-
 
121
#undef main
233
#endif
122
#endif
234
 
123
 
235
 
124
 
236
/*
-
 
237
 * Catch document open requests...this lets us notice files when the app
-
 
238
 *  was launched by double-clicking a document, or when a document was
-
 
239
 *  dragged/dropped on the app's icon. You need to have a
-
 
240
 *  CFBundleDocumentsType section in your Info.plist to get this message,
-
 
241
 *  apparently.
-
 
242
 *
-
 
243
 * Files are added to gArgv, so to the app, they'll look like command line
-
 
244
 *  arguments. Previously, apps launched from the finder had nothing but
-
 
245
 *  an argv[0].
125
int main (int argc, char **argv)
246
 *
-
 
247
 * This message may be received multiple times to open several docs on launch.
-
 
248
 *
-
 
249
 * This message is ignored once the app's mainline has been called.
-
 
250
 */
-
 
251
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
-
 
252
{
126
{
253
    const char *temparg;
127
   // OS/X program entrypoint
254
    size_t arglen;
-
 
255
    char *arg;
-
 
256
    char **newargv;
-
 
257
 
128
 
258
    if (!gFinderLaunch)  /* MacOS is passing command line args. */
129
   SDLMainAppDelegate *sdlmain_delegate;
-
 
130
   NSAutoreleasePool *pool;
-
 
131
   NSMenu *submenu;
-
 
132
   NSMenuItem *menu_item;
-
 
133
   const NSDictionary *infoplist_dict;
-
 
134
   NSString *application_name;
259
        return FALSE;
135
   int arg_index;
260
 
136
 
-
 
137
   // copy the program arguments into global variables (discard arguments 1-n if we were launched by double-clicking)
-
 
138
   if ((argc >= 2) && (strncmp (argv[1], "-psn", 4) == 0))
261
    if (gCalledAppMainline)  /* app has started, ignore this document. */
139
      global_argc = 1; // we were launched by Finder, ignore all arguments except the executable name
-
 
140
   else
-
 
141
      global_argc = argc; // we were NOT launched by Finder (most probably by a shell), copy all arguments
-
 
142
   global_argv = (char **) malloc ((global_argc + 1) * sizeof (char *));
-
 
143
   for (arg_index = 0; arg_index < global_argc; arg_index++)
-
 
144
      global_argv[arg_index] = argv[arg_index];
262
        return FALSE;
145
   global_argv[arg_index] = NULL;
263
 
146
 
264
    temparg = [filename UTF8String];
-
 
265
    arglen = SDL_strlen(temparg) + 1;
147
   // allocate an autorelease pool and start Cocoa
266
    arg = (char *) SDL_malloc(arglen);
148
   pool = [[NSAutoreleasePool alloc] init];
267
    if (arg == NULL)
-
 
268
        return FALSE;
-
 
269
 
149
 
270
    newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
150
   // ensure the application object is initialised (FIXME: is this necessary ?)
271
    if (newargv == NULL)
151
   [NSApplication sharedApplication];
272
    {
-
 
273
        SDL_free(arg);
-
 
274
        return FALSE;
-
 
275
    }
-
 
276
    gArgv = newargv;
-
 
277
 
152
 
278
    SDL_strlcpy(arg, temparg, arglen);
153
   // figure out the application name, either from the Info.plist, or from the process name
279
    gArgv[gArgc++] = arg;
154
   application_name = NULL;
-
 
155
   infoplist_dict = (const NSDictionary *) CFBundleGetInfoDictionary (CFBundleGetMainBundle ()); // try to get the bundle's Info.plist
280
    gArgv[gArgc] = NULL;
156
   if (infoplist_dict)
-
 
157
      application_name = [infoplist_dict objectForKey: @"CFBundleName"]; // if it has, read the CFBundleName key value from it
281
    return TRUE;
158
   if ((application_name == NULL) || ([application_name length] == 0))
282
}
-
 
-
 
159
      application_name = [[NSProcessInfo processInfo] processName]; // else, or if the bundle name is empty, use the process name
283
 
160
 
-
 
161
   // create the application menu and populate it
-
 
162
   [NSApp setMainMenu: [[NSMenu alloc] init]];
284
 
163
 
-
 
164
   // create the [AppName] menu
-
 
165
   submenu = [[NSMenu alloc] initWithTitle: @""];
-
 
166
   [submenu addItemWithTitle: [@"About " stringByAppendingString: application_name] action: @selector(orderFrontStandardAboutPanel:) keyEquivalent: @""];
285
/* Called when the internal event loop has just started running */
167
   [submenu addItem: [NSMenuItem separatorItem]];
-
 
168
   [submenu addItemWithTitle: [@"Hide " stringByAppendingString: application_name] action: @selector(hide:) keyEquivalent: @"h"];
-
 
169
   [[submenu addItemWithTitle: @"Hide Others" action: @selector(hideOtherApplications:) keyEquivalent: @"h"] setKeyEquivalentModifierMask: (NSAlternateKeyMask|NSCommandKeyMask)];
286
- (void) applicationDidFinishLaunching: (NSNotification *) note
170
   [submenu addItemWithTitle: @"Show All" action: @selector(unhideAllApplications:) keyEquivalent: @""];
287
{
-
 
288
    int status;
171
   [submenu addItem: [NSMenuItem separatorItem]];
-
 
172
   [submenu addItemWithTitle: [@"Quit " stringByAppendingString: application_name] action: @selector(quitMenuCommand:) keyEquivalent: @"q"];
289
 
173
 
290
    /* Set the working directory to the .app's parent directory */
174
   // put the [AppName] menu in the menubar
-
 
175
   menu_item = [[NSMenuItem alloc] initWithTitle: @"" action: nil keyEquivalent: @""];
291
    [self setupWorkingDirectory:gFinderLaunch];
176
   [menu_item setSubmenu: submenu];
-
 
177
   [[NSApp mainMenu] addItem: menu_item]; // put menu into the menubar
-
 
178
   [NSApp setAppleMenu: submenu]; // tell the application object that this is now the application menu
-
 
179
   [menu_item release]; // finally give up our references to the objects
-
 
180
   [submenu release]; // finally give up our references to the objects
292
 
181
 
293
#if SDL_USE_NIB_FILE
182
   // create a [Window] menu
294
    /* Set the main menu to contain the real app name instead of "SDL App" */
183
   submenu = [[NSMenu alloc] initWithTitle: @"Window"];
-
 
184
   menu_item = [[NSMenuItem alloc] initWithTitle: @"Minimize" action: @selector(performMiniaturize:) keyEquivalent: @"m"];
-
 
185
   [submenu addItem: menu_item];
-
 
186
   menu_item = [[NSMenuItem alloc] initWithTitle: @"Enter Full Screen" action: @selector(fullscreenMenuCommand:) keyEquivalent: @"f"];
295
    [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
187
   [submenu addItem: menu_item];
296
#endif
188
   [menu_item release];
297
 
189
 
298
    /* Hand off to main application code */
190
   // put the [Window] menu in the menubar
-
 
191
   menu_item = [[NSMenuItem alloc] initWithTitle: @"Window" action: nil keyEquivalent: @""];
299
    gCalledAppMainline = TRUE;
192
   [menu_item setSubmenu: submenu];
-
 
193
   [[NSApp mainMenu] addItem: menu_item]; // put menu into the menubar
-
 
194
   [NSApp setWindowsMenu: submenu]; // tell the application object that this is now the window menu
-
 
195
   [menu_item release]; // finally give up our references to the objects
300
    status = SDL_main (gArgc, gArgv);
196
   [submenu release]; // finally give up our references to the objects
301
 
197
 
-
 
198
   // create a [Help] menu
-
 
199
   submenu = [[NSMenu alloc] initWithTitle: @"Help"];
302
    /* We're done, thank you for playing */
200
   [submenu addItem: [NSMenuItem separatorItem]];
-
 
201
   menu_item = [[NSMenuItem alloc] initWithTitle: @"How to play" action: @selector(visitwebsiteMenuCommand:) keyEquivalent: @"?"];
-
 
202
   [submenu addItem: menu_item];
-
 
203
   menu_item = [[NSMenuItem alloc] initWithTitle: @"Visit Website" action: @selector(visitwebsiteMenuCommand:) keyEquivalent: @"w"];
-
 
204
   [submenu addItem: menu_item];
303
    exit(status);
205
   [menu_item release];
304
}
-
 
305
@end
-
 
306
 
206
 
-
 
207
   // put the [Help] menu in the menubar
-
 
208
   menu_item = [[NSMenuItem alloc] initWithTitle: @"Help" action: nil keyEquivalent: @""];
-
 
209
   [menu_item setSubmenu: submenu];
-
 
210
   [[NSApp mainMenu] addItem: menu_item]; // put menu into the menubar
-
 
211
   [NSApp setHelpMenu: submenu]; // tell the application object that this is now the help menu
-
 
212
   [menu_item release]; // finally give up our references to the objects
-
 
213
   [submenu release]; // finally give up our references to the objects
307
 
214
 
-
 
215
   // create the SDLMain object and make it the app delegate
308
@implementation NSString (ReplaceSubString)
216
   sdlmain_delegate = [[SDLMainAppDelegate alloc] init];
-
 
217
   [NSApp setDelegate: (id) sdlmain_delegate];
309
 
218
 
310
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
-
 
311
{
-
 
312
    unsigned int bufferSize;
-
 
313
    unsigned int selfLen = [self length];
-
 
314
    unsigned int aStringLen = [aString length];
-
 
315
    unichar *buffer;
-
 
316
    NSRange localRange;
219
   // start the Cocoa event loop
317
    NSString *result;
220
   [NSApp run];
318
 
221
 
319
    bufferSize = selfLen + aStringLen - aRange.length;
222
   // finally give up our references to the objects when the Cocoa event loop exits
320
    buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar));
-
 
321
   
-
 
322
    /* Get first part into buffer */
-
 
323
    localRange.location = 0;
-
 
324
    localRange.length = aRange.location;
-
 
325
    [self getCharacters:buffer range:localRange];
-
 
326
   
-
 
327
    /* Get middle part into buffer */
-
 
328
    localRange.location = 0;
-
 
329
    localRange.length = aStringLen;
223
   [sdlmain_delegate release];
330
    [aString getCharacters:(buffer+aRange.location) range:localRange];
-
 
331
     
-
 
332
    /* Get last part into buffer */
-
 
333
    localRange.location = aRange.location + aRange.length;
-
 
334
    localRange.length = selfLen - localRange.location;
-
 
335
    [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
-
 
336
   
-
 
337
    /* Build output string */
-
 
338
    result = [NSString stringWithCharacters:buffer length:bufferSize];
-
 
339
   
-
 
340
    NSDeallocateMemoryPages(buffer, bufferSize);
-
 
341
   
-
 
342
    return result;
224
   [pool release];
343
}
-
 
344
 
225
 
345
@end
-
 
346
 
-
 
347
 
-
 
348
 
-
 
349
#ifdef main
-
 
350
#  undef main
-
 
351
#endif
-
 
352
 
-
 
353
 
-
 
354
/* Main entry point to executable - should *not* be SDL_main! */
-
 
355
int main (int argc, char **argv)
-
 
356
{
-
 
357
    /* Copy the arguments into a global variable */
-
 
358
    /* This is passed if we are launched by double-clicking */
-
 
359
    if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
-
 
360
        gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
-
 
361
        gArgv[0] = argv[0];
-
 
362
        gArgv[1] = NULL;
-
 
363
        gArgc = 1;
-
 
364
        gFinderLaunch = YES;
-
 
365
    } else {
-
 
366
        int i;
-
 
367
        gArgc = argc;
-
 
368
        gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
-
 
369
        for (i = 0; i <= argc; i++)
226
   return (0); // e finita la comedia!
370
            gArgv[i] = argv[i];
-
 
371
        gFinderLaunch = NO;
-
 
372
    }
-
 
373
 
-
 
374
#if SDL_USE_NIB_FILE
-
 
375
    NSApplicationMain (argc, argv);
-
 
376
#else
-
 
377
    CustomApplicationMain (argc, argv);
-
 
378
#endif
-
 
379
    return 0;
-
 
380
}
227
}
381
 
-