src/video/cocoa/SDL_cocoaevents.m
changeset 7801 f00cc0a8cd5d
parent 7739 329c9da4bfeb
child 7857 6388f5229bb7
equal deleted inserted replaced
7800:76e4b6825efc 7801:f00cc0a8cd5d
    40 @interface NSApplication(NSAppleMenu)
    40 @interface NSApplication(NSAppleMenu)
    41 - (void)setAppleMenu:(NSMenu *)menu;
    41 - (void)setAppleMenu:(NSMenu *)menu;
    42 @end
    42 @end
    43 
    43 
    44 @interface SDLAppDelegate : NSObject {
    44 @interface SDLAppDelegate : NSObject {
       
    45 @public
    45     BOOL seenFirstActivate;
    46     BOOL seenFirstActivate;
    46 }
    47 }
    47 
    48 
    48 - (id)init;
    49 - (id)init;
    49 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
       
    50 - (void)applicationDidBecomeActive:(NSNotification *)aNotification;
       
    51 @end
    50 @end
    52 
    51 
    53 @implementation SDLAppDelegate : NSObject
    52 @implementation SDLAppDelegate : NSObject
    54 - (id)init
    53 - (id)init
    55 {
    54 {
    56     self = [super init];
    55     self = [super init];
    57 
    56 
    58     if (self) {
    57     if (self) {
    59         seenFirstActivate = NO;
    58         seenFirstActivate = NO;
       
    59         [[NSNotificationCenter defaultCenter] addObserver:self
       
    60                                                  selector:@selector(focusSomeWindow:)
       
    61                                                      name:NSApplicationDidBecomeActiveNotification
       
    62                                                    object:nil];
    60     }
    63     }
    61 
    64 
    62     return self;
    65     return self;
       
    66 }
       
    67 
       
    68 - (void)dealloc
       
    69 {
       
    70     [[NSNotificationCenter defaultCenter] removeObserver:self];
       
    71     [super dealloc];
    63 }
    72 }
    64 
    73 
    65 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    74 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    66 {
    75 {
    67     SDL_SendQuit();
    76     SDL_SendQuit();
    68     return NSTerminateCancel;
    77     return NSTerminateCancel;
    69 }
    78 }
    70 
    79 
    71 - (void)applicationDidBecomeActive:(NSNotification *)aNotification
    80 - (void)focusSomeWindow:(NSNotification *)aNotification
    72 {
    81 {
    73     /* HACK: Ignore the first call. The application gets a
    82     /* HACK: Ignore the first call. The application gets a
    74      * applicationDidBecomeActive: a little bit after the first window is
    83      * applicationDidBecomeActive: a little bit after the first window is
    75      * created, and if we don't ignore it, a window that has been created with
    84      * created, and if we don't ignore it, a window that has been created with
    76      * SDL_WINDOW_MINIZED will ~immediately be restored.
    85      * SDL_WINDOW_MINIZED will ~immediately be restored.
   109 {
   118 {
   110     return (BOOL)SDL_SendDropFile([filename UTF8String]);
   119     return (BOOL)SDL_SendDropFile([filename UTF8String]);
   111 }
   120 }
   112 @end
   121 @end
   113 
   122 
       
   123 static SDLAppDelegate *appDelegate = nil;
       
   124 
   114 static NSString *
   125 static NSString *
   115 GetApplicationName(void)
   126 GetApplicationName(void)
   116 {
   127 {
   117     NSDictionary *dict;
   128     NSDictionary *dict;
   118     NSString *appName = 0;
   129     NSString *appName = 0;
   233         [NSApp finishLaunching];
   244         [NSApp finishLaunching];
   234         NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"NO" forKey:@"AppleMomentumScrollSupported"];
   245         NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"NO" forKey:@"AppleMomentumScrollSupported"];
   235         [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
   246         [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
   236 
   247 
   237     }
   248     }
   238     if (NSApp && ![NSApp delegate]) {
   249     if (NSApp && !appDelegate) {
   239         [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
   250         appDelegate = [[SDLAppDelegate alloc] init];
       
   251 
       
   252         /* If someone else has an app delegate, it means we can't turn a
       
   253          * termination into SDL_Quit, and we can't handle application:openFile:
       
   254          */
       
   255         if (![NSApp delegate]) {
       
   256             [NSApp setDelegate:appDelegate];
       
   257         } else {
       
   258             appDelegate->seenFirstActivate = YES;
       
   259         }
   240     }
   260     }
   241     [pool release];
   261     [pool release];
   242 }
   262 }
   243 
   263 
   244 void
   264 void