comparison src/main/macosx/SDLMain.m @ 158:4382c38dfbee

Date: Tue, 21 Aug 2001 03:50:01 +0200 From: Max Horn <max@quendi.de> Subject: New patch for OS X Attached a .patch file for SDL/OSX with some nice bug fixes / enhancments. * fixes the activation issues, which also caused the window to be always drawn like an inactive. The close/minimize widgets now are animated properly, too. * the menu items are automatically adjusted to use the app name instead of just "SDL App". I did this so that we really can use one central SDLMain.nib file, w/o requiring developers to make a copy of it and adjust it. * libSDLMain now contains the proper cocoa code, not as before the carbon code. This means apps no longer have to carry a copy of SDLMain.m/SDLMain.h * revamped configure.in to properly build a Cocoa/Quartz SDL lib, not a Carbon based SDL lib
author Sam Lantinga <slouken@libsdl.org>
date Tue, 21 Aug 2001 07:19:59 +0000
parents bd6b0a910a65
children e92aa316c517
comparison
equal deleted inserted replaced
157:de04f423389a 158:4382c38dfbee
9 #import <sys/param.h> /* for MAXPATHLEN */ 9 #import <sys/param.h> /* for MAXPATHLEN */
10 #import <unistd.h> 10 #import <unistd.h>
11 11
12 static int gArgc; 12 static int gArgc;
13 static char **gArgv; 13 static char **gArgv;
14 static NSString *gAppName = 0;
15
16 @interface NSString (ReplaceSubString)
17 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
18 @end
19
14 20
15 /* The main class of the application, the application's delegate */ 21 /* The main class of the application, the application's delegate */
16 @implementation SDLMain 22 @implementation SDLMain
17 23
18 /* Invoked from the Quit menu item */ 24 /* Invoked from the Quit menu item */
19 - (void) quit:(id)sender 25 - (void) quit:(id)sender
20 { 26 {
21 SDL_Event event; 27 SDL_Event event;
22 event.type = SDL_QUIT; 28 event.type = SDL_QUIT;
23 SDL_PushEvent(&event); 29 SDL_PushEvent(&event);
24 } 30 }
25 31
26 /* Set the working directory to the .app's parent directory */ 32 /* Set the working directory to the .app's parent directory */
27 - (void) setupWorkingDirectory 33 - (void) setupWorkingDirectory
28 { 34 {
29 char parentdir[MAXPATHLEN]; 35 char parentdir[MAXPATHLEN];
30 char *c; 36 char *c;
31 37
32 strncpy ( parentdir, gArgv[0], MAXPATHLEN ); 38 strncpy ( parentdir, gArgv[0], sizeof(parentdir) );
33 c = (char*) parentdir; 39 c = (char*) parentdir;
34 40
35 while (*c != '\0') /* go to end */ 41 while (*c != '\0') /* go to end */
36 c++; 42 c++;
37 43
38 while (*c != '/') /* back up to parent */ 44 while (*c != '/') /* back up to parent */
39 c--; 45 c--;
40 46
41 *c = '\0'; /* cut off last part (binary name) */ 47 *c++ = '\0'; /* cut off last part (binary name) */
42 48
43 assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */ 49 assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */
44 assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */ 50 assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */
51
52 gAppName = [ NSString stringWithCString: c ];
53 }
54
55 /* Fix menu to contain the real app name instead of "SDL App" */
56 - (void) fixMenu:(NSMenu *)aMenu
57 {
58 NSRange aRange;
59 NSEnumerator *enumerator;
60 NSMenuItem *menuItem;
61
62 aRange = [[aMenu title] rangeOfString:@"SDL App"];
63 if (aRange.length != 0)
64 [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:gAppName]];
65
66 enumerator = [[aMenu itemArray] objectEnumerator];
67 while ((menuItem = [enumerator nextObject]))
68 {
69 aRange = [[menuItem title] rangeOfString:@"SDL App"];
70 if (aRange.length != 0)
71 [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:gAppName]];
72 if ([menuItem hasSubmenu])
73 [self fixMenu: [menuItem submenu]];
74 }
75 [ aMenu sizeToFit ];
45 } 76 }
46 77
47 /* Called when the internal event loop has just started running */ 78 /* Called when the internal event loop has just started running */
48 - (void) applicationDidFinishLaunching: (NSNotification *) note 79 - (void) applicationDidFinishLaunching: (NSNotification *) note
49 { 80 {
50 int status; 81 int status;
51 82
52 /* Set the working directory to the .app's parent directory */ 83 /* Set the working directory to the .app's parent directory */
53 [ self setupWorkingDirectory ]; 84 [ self setupWorkingDirectory ];
54 85
86 /* Set the main menu to contain the real app name instead of "SDL App" */
87 [ self fixMenu: [ NSApp mainMenu ] ];
88
55 /* Hand off to main application code */ 89 /* Hand off to main application code */
56 status = SDL_main (gArgc, gArgv); 90 status = SDL_main (gArgc, gArgv);
57 91
58 /* We're done, thank you for playing */ 92 /* We're done, thank you for playing */
59 exit(status); 93 exit(status);
60 } 94 }
61 @end 95 @end
96
97
98 @implementation NSString (ReplaceSubString)
99
100 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
101 {
102 unsigned int bufferSize;
103 unsigned int selfLen = [self length];
104 unsigned int aStringLen = [aString length];
105 unichar *buffer;
106 NSRange localRange;
107 NSString *result;
108
109 bufferSize = selfLen + aStringLen - aRange.length;
110 buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
111
112 // Get first part into buffer
113 localRange.location = 0;
114 localRange.length = aRange.location;
115 [self getCharacters:buffer range:localRange];
116
117 // Get middle part into buffer
118 localRange.location = 0;
119 localRange.length = aStringLen;
120 [aString getCharacters:(buffer+aRange.location) range:localRange];
121
122 // Get last part into buffer
123 localRange.location = aRange.location + aRange.length;
124 localRange.length = selfLen - localRange.location;
125 [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
126
127 // Build output string
128 result = [NSString stringWithCharacters:buffer length:bufferSize];
129
130 NSDeallocateMemoryPages(buffer, bufferSize);
131
132 return result;
133 }
134
135 @end
136
62 137
63 #ifdef main 138 #ifdef main
64 # undef main 139 # undef main
65 #endif 140 #endif
66 141