src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 29 Jul 2006 21:51:00 +0000
changeset 1956 ba0d62354872
parent 1937 05e88d266921
child 1959 25d6537feea4
permissions -rw-r--r--
Simplified driver window creation code.
Implemented several Cocoa window functions
slouken@1931
     1
/*
slouken@1931
     2
    SDL - Simple DirectMedia Layer
slouken@1931
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1931
     4
slouken@1931
     5
    This library is free software; you can redistribute it and/or
slouken@1931
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1931
     7
    License as published by the Free Software Foundation; either
slouken@1931
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1931
     9
slouken@1931
    10
    This library is distributed in the hope that it will be useful,
slouken@1931
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1931
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1931
    13
    Lesser General Public License for more details.
slouken@1931
    14
slouken@1931
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1931
    16
    License along with this library; if not, write to the Free Software
slouken@1931
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1931
    18
slouken@1931
    19
    Sam Lantinga
slouken@1931
    20
    slouken@libsdl.org
slouken@1931
    21
*/
slouken@1931
    22
#include "SDL_config.h"
slouken@1931
    23
slouken@1931
    24
#include "SDL_cocoavideo.h"
slouken@1931
    25
slouken@1931
    26
/* setAppleMenu disappeared from the headers in 10.4 */
slouken@1931
    27
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
slouken@1931
    28
@interface NSApplication(NSAppleMenu)
slouken@1931
    29
- (void)setAppleMenu:(NSMenu *)menu;
slouken@1931
    30
@end
slouken@1931
    31
#endif
slouken@1931
    32
slouken@1936
    33
@implementation NSApplication(SDL)
slouken@1936
    34
- (void)setRunning
slouken@1931
    35
{
slouken@1931
    36
    _running = 1;
slouken@1931
    37
}
slouken@1931
    38
@end
slouken@1931
    39
slouken@1937
    40
@interface SDLAppDelegate : NSObject
slouken@1937
    41
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
slouken@1937
    42
@end
slouken@1937
    43
slouken@1937
    44
@implementation SDLAppDelegate : NSObject
slouken@1937
    45
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
slouken@1937
    46
{
slouken@1937
    47
    SDL_SendQuit();
slouken@1937
    48
    return NSTerminateCancel;
slouken@1937
    49
}
slouken@1937
    50
@end
slouken@1937
    51
slouken@1931
    52
static NSString *
slouken@1931
    53
GetApplicationName(void)
slouken@1931
    54
{
slouken@1931
    55
    NSDictionary *dict;
slouken@1931
    56
    NSString *appName = 0;
slouken@1931
    57
slouken@1931
    58
    /* Determine the application name */
slouken@1931
    59
    dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
slouken@1931
    60
    if (dict)
slouken@1931
    61
        appName = [dict objectForKey: @"CFBundleName"];
slouken@1931
    62
    
slouken@1931
    63
    if (![appName length])
slouken@1931
    64
        appName = [[NSProcessInfo processInfo] processName];
slouken@1931
    65
slouken@1931
    66
    return appName;
slouken@1931
    67
}
slouken@1931
    68
slouken@1931
    69
static void
slouken@1931
    70
CreateApplicationMenus(void)
slouken@1931
    71
{
slouken@1931
    72
    NSString *appName;
slouken@1931
    73
    NSString *title;
slouken@1931
    74
    NSMenu *appleMenu;
slouken@1931
    75
    NSMenu *windowMenu;
slouken@1931
    76
    NSMenuItem *menuItem;
slouken@1931
    77
    
slouken@1931
    78
    /* Create the main menu bar */
slouken@1931
    79
    [NSApp setMainMenu:[[NSMenu alloc] init]];
slouken@1931
    80
slouken@1931
    81
    /* Create the application menu */
slouken@1931
    82
    appName = GetApplicationName();
slouken@1931
    83
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@1931
    84
    
slouken@1931
    85
    /* Add menu items */
slouken@1931
    86
    title = [@"About " stringByAppendingString:appName];
slouken@1931
    87
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
slouken@1931
    88
slouken@1931
    89
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
    90
slouken@1931
    91
    title = [@"Hide " stringByAppendingString:appName];
slouken@1931
    92
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
slouken@1931
    93
slouken@1931
    94
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
slouken@1931
    95
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
slouken@1931
    96
slouken@1931
    97
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
slouken@1931
    98
slouken@1931
    99
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   100
slouken@1931
   101
    title = [@"Quit " stringByAppendingString:appName];
slouken@1931
   102
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
slouken@1931
   103
    
slouken@1931
   104
    /* Put menu into the menubar */
slouken@1931
   105
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
slouken@1931
   106
    [menuItem setSubmenu:appleMenu];
slouken@1931
   107
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   108
    [menuItem release];
slouken@1931
   109
slouken@1931
   110
    /* Tell the application object that this is now the application menu */
slouken@1931
   111
    [NSApp setAppleMenu:appleMenu];
slouken@1931
   112
    [appleMenu release];
slouken@1931
   113
slouken@1931
   114
slouken@1931
   115
    /* Create the window menu */
slouken@1931
   116
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
slouken@1931
   117
    
slouken@1931
   118
    /* "Minimize" item */
slouken@1931
   119
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
slouken@1931
   120
    [windowMenu addItem:menuItem];
slouken@1931
   121
    [menuItem release];
slouken@1931
   122
    
slouken@1931
   123
    /* Put menu into the menubar */
slouken@1931
   124
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
slouken@1931
   125
    [menuItem setSubmenu:windowMenu];
slouken@1931
   126
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   127
    [menuItem release];
slouken@1931
   128
    
slouken@1931
   129
    /* Tell the application object that this is now the window menu */
slouken@1931
   130
    [NSApp setWindowsMenu:windowMenu];
slouken@1931
   131
    [windowMenu release];
slouken@1931
   132
}
slouken@1931
   133
slouken@1931
   134
void
slouken@1931
   135
Cocoa_RegisterApp(void)
slouken@1931
   136
{
slouken@1931
   137
    ProcessSerialNumber psn;
slouken@1931
   138
    NSAutoreleasePool *pool;
slouken@1931
   139
slouken@1931
   140
    if (!GetCurrentProcess(&psn)) {
slouken@1931
   141
        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
slouken@1931
   142
        SetFrontProcess(&psn);
slouken@1931
   143
    }
slouken@1931
   144
slouken@1931
   145
    pool = [[NSAutoreleasePool alloc] init];
slouken@1931
   146
    if (NSApp == nil) {
slouken@1936
   147
        [NSApplication sharedApplication];
slouken@1931
   148
slouken@1931
   149
        if ([NSApp mainMenu] == nil) {
slouken@1931
   150
            CreateApplicationMenus();
slouken@1931
   151
        }
slouken@1931
   152
        [NSApp finishLaunching];
slouken@1931
   153
    }
slouken@1937
   154
    if ([NSApp delegate] == nil) {
slouken@1937
   155
        [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
slouken@1937
   156
    }
slouken@1936
   157
    [NSApp setRunning];
slouken@1931
   158
    [pool release];
slouken@1931
   159
}
slouken@1931
   160
slouken@1931
   161
void
slouken@1931
   162
Cocoa_PumpEvents(_THIS)
slouken@1931
   163
{
slouken@1931
   164
    NSAutoreleasePool *pool;
slouken@1931
   165
slouken@1931
   166
    pool = [[NSAutoreleasePool alloc] init];
slouken@1932
   167
    while ([NSApp isRunning]) {
slouken@1931
   168
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
slouken@1931
   169
        if ( event == nil ) {
slouken@1931
   170
            break;
slouken@1931
   171
        }
slouken@1931
   172
        [NSApp sendEvent:event];
slouken@1931
   173
    }
slouken@1931
   174
    [pool release];
slouken@1931
   175
}
slouken@1931
   176
slouken@1931
   177
/* vi: set ts=4 sw=4 expandtab: */