hedgewars/SDLMain.m
changeset 2565 54296af65fe9
parent 1999 a28dcfe658e3
child 2666 2b8c8f16421e
equal deleted inserted replaced
2562:0421e9f59774 2565:54296af65fe9
     3        Non-NIB-Code & other changes: Max Horn <max@quendi.de>
     3        Non-NIB-Code & other changes: Max Horn <max@quendi.de>
     4 
     4 
     5     Feel free to customize this file to suit your needs
     5     Feel free to customize this file to suit your needs
     6 */
     6 */
     7 
     7 
     8 #import "SDL.h"
     8 #include "SDL.h"
     9 #import "SDLMain.h"
     9 #include "SDLMain.h"
    10 #import <sys/param.h> /* for MAXPATHLEN */
    10 #include <sys/param.h> /* for MAXPATHLEN */
    11 #import <unistd.h>
    11 #include <unistd.h>
    12 
    12 
    13 /* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
    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
    14  but the method still is there and works. To avoid warnings, we declare
    15  it ourselves here. */
    15  it ourselves here. */
    16 @interface NSApplication(SDL_Missing_Methods)
    16 @interface NSApplication(SDL_Missing_Methods)
    41 static BOOL   gFinderLaunch;
    41 static BOOL   gFinderLaunch;
    42 static BOOL   gCalledAppMainline = FALSE;
    42 static BOOL   gCalledAppMainline = FALSE;
    43 
    43 
    44 static NSString *getApplicationName(void)
    44 static NSString *getApplicationName(void)
    45 {
    45 {
    46     NSDictionary *dict;
    46     const NSDictionary *dict;
    47     NSString *appName = 0;
    47     NSString *appName = 0;
    48 
    48 
    49     /* Determine the application name */
    49     /* Determine the application name */
    50     dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
    50     dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
    51     if (dict)
    51     if (dict)
    52         appName = [dict objectForKey: @"CFBundleName"];
    52         appName = [dict objectForKey: @"CFBundleName"];
    53     
    53     
    54     if (![appName length])
    54     if (![appName length])
    55         appName = [[NSProcessInfo processInfo] processName];
    55         appName = [[NSProcessInfo processInfo] processName];
    62 @interface NSString (ReplaceSubString)
    62 @interface NSString (ReplaceSubString)
    63 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
    63 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
    64 @end
    64 @end
    65 #endif
    65 #endif
    66 
    66 
    67 @interface SDLApplication : NSApplication
    67 @interface NSApplication (SDLApplication)
    68 @end
    68 @end
    69 
    69 
    70 @implementation SDLApplication
    70 @implementation NSApplication (SDLApplication)
    71 /* Invoked from the Quit menu item */
    71 /* Invoked from the Quit menu item */
    72 - (void)terminate:(id)sender
    72 - (void)terminate:(id)sender
    73 {
    73 {
    74     /* Post a SDL_QUIT event */
    74     /* Post a SDL_QUIT event */
    75     SDL_Event event;
    75     SDL_Event event;
    85 - (void) setupWorkingDirectory:(BOOL)shouldChdir
    85 - (void) setupWorkingDirectory:(BOOL)shouldChdir
    86 {
    86 {
    87     if (shouldChdir)
    87     if (shouldChdir)
    88     {
    88     {
    89         char parentdir[MAXPATHLEN];
    89         char parentdir[MAXPATHLEN];
    90 		CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
    90         CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
    91 		CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
    91         CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
    92 		if (CFURLGetFileSystemRepresentation(url2, true, (UInt8 *)parentdir, MAXPATHLEN)) {
    92         if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) {
    93 	        assert ( chdir (parentdir) == 0 );   /* chdir to the binary app's parent */
    93             chdir(parentdir);   /* chdir to the binary app's parent */
    94 		}
    94         }
    95 		CFRelease(url);
    95         CFRelease(url);
    96 		CFRelease(url2);
    96         CFRelease(url2);
    97 	}
    97     }
    98 
       
    99 }
    98 }
   100 
    99 
   101 #if SDL_USE_NIB_FILE
   100 #if SDL_USE_NIB_FILE
   102 
   101 
   103 /* Fix menu to contain the real app name instead of "SDL App" */
   102 /* Fix menu to contain the real app name instead of "SDL App" */
   118         if (aRange.length != 0)
   117         if (aRange.length != 0)
   119             [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
   118             [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
   120         if ([menuItem hasSubmenu])
   119         if ([menuItem hasSubmenu])
   121             [self fixMenu:[menuItem submenu] withAppName:appName];
   120             [self fixMenu:[menuItem submenu] withAppName:appName];
   122     }
   121     }
   123     [ aMenu sizeToFit ];
       
   124 }
   122 }
   125 
   123 
   126 #else
   124 #else
   127 
   125 
   128 static void setApplicationMenu(void)
   126 static void setApplicationMenu(void)
   201 {
   199 {
   202     NSAutoreleasePool	*pool = [[NSAutoreleasePool alloc] init];
   200     NSAutoreleasePool	*pool = [[NSAutoreleasePool alloc] init];
   203     SDLMain				*sdlMain;
   201     SDLMain				*sdlMain;
   204 
   202 
   205     /* Ensure the application object is initialised */
   203     /* Ensure the application object is initialised */
   206     [SDLApplication sharedApplication];
   204     [NSApplication sharedApplication];
   207     
   205     
   208 #ifdef SDL_USE_CPS
   206 #ifdef SDL_USE_CPS
   209     {
   207     {
   210         CPSProcessSerNum PSN;
   208         CPSProcessSerNum PSN;
   211         /* Tell the dock about us */
   209         /* Tell the dock about us */
   212         if (!CPSGetCurrentProcess(&PSN))
   210         if (!CPSGetCurrentProcess(&PSN))
   213             if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
   211             if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
   214                 if (!CPSSetFrontProcess(&PSN))
   212                 if (!CPSSetFrontProcess(&PSN))
   215                     [SDLApplication sharedApplication];
   213                     [NSApplication sharedApplication];
   216     }
   214     }
   217 #endif /* SDL_USE_CPS */
   215 #endif /* SDL_USE_CPS */
   218 
   216 
   219     /* Set up the menubar */
   217     /* Set up the menubar */
   220     [NSApp setMainMenu:[[NSMenu alloc] init]];
   218     [NSApp setMainMenu:[[NSMenu alloc] init]];
   317     unichar *buffer;
   315     unichar *buffer;
   318     NSRange localRange;
   316     NSRange localRange;
   319     NSString *result;
   317     NSString *result;
   320 
   318 
   321     bufferSize = selfLen + aStringLen - aRange.length;
   319     bufferSize = selfLen + aStringLen - aRange.length;
   322     buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
   320     buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar));
   323     
   321     
   324     /* Get first part into buffer */
   322     /* Get first part into buffer */
   325     localRange.location = 0;
   323     localRange.location = 0;
   326     localRange.length = aRange.location;
   324     localRange.length = aRange.location;
   327     [self getCharacters:buffer range:localRange];
   325     [self getCharacters:buffer range:localRange];
   372             gArgv[i] = argv[i];
   370             gArgv[i] = argv[i];
   373         gFinderLaunch = NO;
   371         gFinderLaunch = NO;
   374     }
   372     }
   375 
   373 
   376 #if SDL_USE_NIB_FILE
   374 #if SDL_USE_NIB_FILE
   377     [SDLApplication poseAsClass:[NSApplication class]];
       
   378     NSApplicationMain (argc, argv);
   375     NSApplicationMain (argc, argv);
   379 #else
   376 #else
   380     CustomApplicationMain (argc, argv);
   377     CustomApplicationMain (argc, argv);
   381 #endif
   378 #endif
   382     return 0;
   379     return 0;