src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 30 Jul 2006 05:18:33 +0000
changeset 1959 25d6537feea4
parent 1958 5fc6fb0fb605
child 1962 c92e5f3e68d9
permissions -rw-r--r--
Implemented Cocoa key event handling.
slouken@1933
     1
/*
slouken@1933
     2
    SDL - Simple DirectMedia Layer
slouken@1933
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1933
     4
slouken@1933
     5
    This library is free software; you can redistribute it and/or
slouken@1933
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1933
     7
    License as published by the Free Software Foundation; either
slouken@1933
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1933
     9
slouken@1933
    10
    This library is distributed in the hope that it will be useful,
slouken@1933
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1933
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1933
    13
    Lesser General Public License for more details.
slouken@1933
    14
slouken@1933
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1933
    16
    License along with this library; if not, write to the Free Software
slouken@1933
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1933
    18
slouken@1933
    19
    Sam Lantinga
slouken@1933
    20
    slouken@libsdl.org
slouken@1933
    21
*/
slouken@1933
    22
#include "SDL_config.h"
slouken@1933
    23
slouken@1933
    24
#include "SDL_syswm.h"
slouken@1933
    25
#include "../SDL_sysvideo.h"
slouken@1933
    26
#include "../../events/SDL_keyboard_c.h"
slouken@1933
    27
#include "../../events/SDL_mouse_c.h"
slouken@1933
    28
#include "../../events/SDL_windowevents_c.h"
slouken@1933
    29
slouken@1933
    30
#include "SDL_cocoavideo.h"
slouken@1933
    31
slouken@1933
    32
static __inline__ void ConvertNSRect(NSRect *r)
slouken@1933
    33
{
slouken@1936
    34
    /* FIXME: Cache the display used for this window */
slouken@1933
    35
    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
slouken@1933
    36
}
slouken@1933
    37
slouken@1933
    38
@implementation Cocoa_WindowListener
slouken@1933
    39
slouken@1933
    40
- (void)listen:(SDL_WindowData *)data
slouken@1933
    41
{
slouken@1933
    42
    NSNotificationCenter *center;
slouken@1933
    43
slouken@1933
    44
    _data = data;
slouken@1933
    45
slouken@1933
    46
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    47
slouken@1933
    48
    [_data->window setNextResponder:self];
slouken@1933
    49
    if ([_data->window delegate] != nil) {
slouken@1933
    50
        [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->window];
slouken@1933
    51
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->window];
slouken@1933
    52
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->window];
slouken@1933
    53
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->window];
slouken@1933
    54
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->window];
slouken@1933
    55
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->window];
slouken@1933
    56
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->window];
slouken@1933
    57
    } else {
slouken@1933
    58
        [_data->window setDelegate:self];
slouken@1933
    59
    }
slouken@1933
    60
    [center addObserver:self selector:@selector(windowDidHide:) name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    61
    [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
slouken@1933
    62
slouken@1933
    63
    [_data->window setAcceptsMouseMovedEvents:YES];
slouken@1933
    64
}
slouken@1933
    65
slouken@1933
    66
- (void)close
slouken@1933
    67
{
slouken@1933
    68
    NSNotificationCenter *center;
slouken@1933
    69
slouken@1933
    70
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    71
slouken@1933
    72
    [_data->window setNextResponder:nil];
slouken@1933
    73
    if ([_data->window delegate] != self) {
slouken@1933
    74
        [center removeObserver:self name:NSWindowDidExposeNotification object:_data->window];
slouken@1933
    75
        [center removeObserver:self name:NSWindowDidMoveNotification object:_data->window];
slouken@1933
    76
        [center removeObserver:self name:NSWindowDidResizeNotification object:_data->window];
slouken@1933
    77
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->window];
slouken@1933
    78
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->window];
slouken@1933
    79
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->window];
slouken@1933
    80
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->window];
slouken@1933
    81
    } else {
slouken@1933
    82
        [_data->window setDelegate:nil];
slouken@1933
    83
    }
slouken@1933
    84
    [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    85
    [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
slouken@1933
    86
}
slouken@1933
    87
slouken@1933
    88
- (BOOL)windowShouldClose:(id)sender
slouken@1933
    89
{
slouken@1933
    90
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
    91
    return NO;
slouken@1933
    92
}
slouken@1933
    93
slouken@1933
    94
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
    95
{
slouken@1933
    96
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
    97
}
slouken@1933
    98
slouken@1933
    99
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
   100
{
slouken@1933
   101
    int x, y;
slouken@1933
   102
    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
slouken@1933
   103
    ConvertNSRect(&rect);
slouken@1933
   104
    x = (int)rect.origin.x;
slouken@1933
   105
    y = (int)rect.origin.y;
slouken@1933
   106
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   107
}
slouken@1933
   108
slouken@1933
   109
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   110
{
slouken@1933
   111
    int w, h;
slouken@1933
   112
    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
slouken@1933
   113
    w = (int)rect.size.width;
slouken@1933
   114
    h = (int)rect.size.height;
slouken@1933
   115
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@1933
   116
}
slouken@1933
   117
slouken@1933
   118
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   119
{
slouken@1933
   120
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   121
}
slouken@1933
   122
slouken@1933
   123
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   124
{
slouken@1933
   125
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   126
}
slouken@1933
   127
slouken@1933
   128
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   129
{
slouken@1933
   130
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@1933
   131
}
slouken@1933
   132
slouken@1933
   133
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   134
{
slouken@1933
   135
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@1933
   136
}
slouken@1933
   137
slouken@1933
   138
- (void)windowDidHide:(NSNotification *)aNotification
slouken@1933
   139
{
slouken@1933
   140
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1933
   141
}
slouken@1933
   142
slouken@1933
   143
- (void)windowDidUnhide:(NSNotification *)aNotification
slouken@1933
   144
{
slouken@1933
   145
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1933
   146
}
slouken@1933
   147
slouken@1933
   148
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   149
{
slouken@1933
   150
    int index;
slouken@1959
   151
    int button;
slouken@1933
   152
slouken@1933
   153
    index = _data->videodata->mouse;
slouken@1959
   154
    switch ([theEvent buttonNumber]) {
slouken@1959
   155
    case 0:
slouken@1959
   156
        button = SDL_BUTTON_LEFT;
slouken@1959
   157
        break;
slouken@1959
   158
    case 1:
slouken@1959
   159
        button = SDL_BUTTON_RIGHT;
slouken@1959
   160
        break;
slouken@1959
   161
    case 2:
slouken@1959
   162
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   163
        break;
slouken@1959
   164
    default:
slouken@1959
   165
        button = [theEvent buttonNumber];
slouken@1959
   166
        break;
slouken@1959
   167
    }
slouken@1959
   168
    SDL_SendMouseButton(index, SDL_PRESSED, button);
slouken@1933
   169
}
slouken@1933
   170
slouken@1933
   171
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   172
{
slouken@1959
   173
    [self mouseDown:theEvent];
slouken@1933
   174
}
slouken@1933
   175
slouken@1933
   176
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   177
{
slouken@1959
   178
    [self mouseDown:theEvent];
slouken@1933
   179
}
slouken@1933
   180
slouken@1933
   181
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   182
{
slouken@1933
   183
    int index;
slouken@1959
   184
    int button;
slouken@1933
   185
slouken@1933
   186
    index = _data->videodata->mouse;
slouken@1959
   187
    switch ([theEvent buttonNumber]) {
slouken@1959
   188
    case 0:
slouken@1959
   189
        button = SDL_BUTTON_LEFT;
slouken@1959
   190
        break;
slouken@1959
   191
    case 1:
slouken@1959
   192
        button = SDL_BUTTON_RIGHT;
slouken@1959
   193
        break;
slouken@1959
   194
    case 2:
slouken@1959
   195
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   196
        break;
slouken@1959
   197
    default:
slouken@1959
   198
        button = [theEvent buttonNumber];
slouken@1959
   199
        break;
slouken@1959
   200
    }
slouken@1959
   201
    SDL_SendMouseButton(index, SDL_RELEASED, button);
slouken@1933
   202
}
slouken@1933
   203
slouken@1933
   204
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   205
{
slouken@1959
   206
    [self mouseUp:theEvent];
slouken@1933
   207
}
slouken@1933
   208
slouken@1933
   209
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   210
{
slouken@1959
   211
    [self mouseUp:theEvent];
slouken@1933
   212
}
slouken@1933
   213
slouken@1933
   214
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   215
{
slouken@1956
   216
    SDL_Window *window = SDL_GetWindowFromID(_data->windowID);
slouken@1933
   217
    int index;
slouken@1933
   218
    SDL_Mouse *mouse;
slouken@1933
   219
    NSPoint point;
slouken@1933
   220
    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
slouken@1933
   221
slouken@1933
   222
    index = _data->videodata->mouse;
slouken@1933
   223
    mouse = SDL_GetMouse(index);
slouken@1933
   224
    if (mouse->focus != _data->windowID) {
slouken@1933
   225
        SDL_SetMouseFocus(index, _data->windowID);
slouken@1933
   226
    }
slouken@1933
   227
slouken@1933
   228
    point = [NSEvent mouseLocation];
slouken@1933
   229
    point.x = point.x - rect.origin.x;
slouken@1933
   230
    point.y = rect.size.height - (point.y - rect.origin.y);
slouken@1933
   231
    SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y);
slouken@1933
   232
}
slouken@1933
   233
slouken@1957
   234
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   235
{
slouken@1957
   236
    [self mouseMoved:theEvent];
slouken@1957
   237
}
slouken@1957
   238
slouken@1958
   239
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   240
{
slouken@1958
   241
    [self mouseMoved:theEvent];
slouken@1958
   242
}
slouken@1958
   243
slouken@1958
   244
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   245
{
slouken@1958
   246
    [self mouseMoved:theEvent];
slouken@1958
   247
}
slouken@1958
   248
slouken@1933
   249
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   250
{
slouken@1957
   251
    int index;
slouken@1957
   252
slouken@1957
   253
    index = _data->videodata->mouse;
slouken@1957
   254
    SDL_SendMouseWheel(index, (int)([theEvent deltaY]+0.9f));
slouken@1933
   255
}
slouken@1933
   256
slouken@1933
   257
- (void)keyDown:(NSEvent *)theEvent
slouken@1933
   258
{
slouken@1959
   259
    int index;
slouken@1959
   260
slouken@1959
   261
    index = _data->videodata->keyboard;
slouken@1933
   262
fprintf(stderr, "keyDown\n");
slouken@1959
   263
    const char *text = [[theEvent characters] UTF8String];
slouken@1959
   264
    if(text && *text) {
slouken@1959
   265
        SDL_SendKeyboardText(index, text);
slouken@1959
   266
    }
slouken@1933
   267
}
slouken@1933
   268
slouken@1933
   269
- (void)keyUp:(NSEvent *)theEvent
slouken@1933
   270
{
slouken@1959
   271
    int index;
slouken@1959
   272
slouken@1959
   273
    index = _data->videodata->keyboard;
slouken@1933
   274
fprintf(stderr, "keyUp\n");
slouken@1933
   275
}
slouken@1933
   276
slouken@1933
   277
@end
slouken@1933
   278
slouken@1933
   279
static int
slouken@1951
   280
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   281
{
slouken@1933
   282
    NSAutoreleasePool *pool;
slouken@1951
   283
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
   284
    SDL_WindowData *data;
slouken@1933
   285
slouken@1933
   286
    /* Allocate the window data */
slouken@1933
   287
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1933
   288
    if (!data) {
slouken@1933
   289
        SDL_OutOfMemory();
slouken@1933
   290
        return -1;
slouken@1933
   291
    }
slouken@1933
   292
    data->windowID = window->id;
slouken@1933
   293
    data->window = nswindow;
slouken@1933
   294
    data->created = created;
slouken@1951
   295
    data->videodata = videodata;
slouken@1933
   296
slouken@1933
   297
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   298
slouken@1933
   299
    /* Create an event listener for the window */
slouken@1933
   300
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@1933
   301
    [data->listener listen:data];
slouken@1933
   302
slouken@1933
   303
    /* Fill in the SDL window with the window data */
slouken@1933
   304
    {
slouken@1933
   305
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   306
        ConvertNSRect(&rect);
slouken@1933
   307
        window->x = (int)rect.origin.x;
slouken@1933
   308
        window->y = (int)rect.origin.y;
slouken@1933
   309
        window->w = (int)rect.size.width;
slouken@1933
   310
        window->h = (int)rect.size.height;
slouken@1933
   311
    }
slouken@1933
   312
    if ([nswindow isVisible]) {
slouken@1933
   313
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   314
    } else {
slouken@1933
   315
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   316
    }
slouken@1933
   317
    {
slouken@1933
   318
        unsigned int style = [nswindow styleMask];
slouken@1933
   319
slouken@1956
   320
        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
slouken@1933
   321
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   322
        } else {
slouken@1933
   323
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   324
        }
slouken@1933
   325
        if (style & NSResizableWindowMask) {
slouken@1933
   326
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   327
        } else {
slouken@1933
   328
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   329
        }
slouken@1933
   330
    }
slouken@1933
   331
    if ([nswindow isZoomed]) {
slouken@1933
   332
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   333
    } else {
slouken@1933
   334
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   335
    }
slouken@1933
   336
    if ([nswindow isMiniaturized]) {
slouken@1933
   337
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   338
    } else {
slouken@1933
   339
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   340
    }
slouken@1933
   341
    if ([nswindow isKeyWindow]) {
slouken@1933
   342
        int index = data->videodata->keyboard;
slouken@1933
   343
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1933
   344
        SDL_SetKeyboardFocus(index, data->windowID);
slouken@1933
   345
slouken@1933
   346
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1933
   347
            /* FIXME */
slouken@1933
   348
        }
slouken@1933
   349
    }
slouken@1933
   350
slouken@1933
   351
    /* All done! */
slouken@1933
   352
    [pool release];
slouken@1933
   353
    window->driverdata = data;
slouken@1933
   354
    return 0;
slouken@1933
   355
}
slouken@1933
   356
slouken@1933
   357
int
slouken@1933
   358
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   359
{
slouken@1933
   360
    NSAutoreleasePool *pool;
slouken@1933
   361
    NSWindow *nswindow;
slouken@1933
   362
    NSRect rect;
slouken@1933
   363
    unsigned int style;
slouken@1933
   364
    NSString *title;
slouken@1936
   365
    int status;
slouken@1933
   366
slouken@1933
   367
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   368
slouken@1956
   369
    if (window->x == SDL_WINDOWPOS_CENTERED) {
slouken@1933
   370
        rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
slouken@1933
   371
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@1933
   372
        rect.origin.x = 0;
slouken@1933
   373
    } else {
slouken@1933
   374
        rect.origin.x = window->x;
slouken@1933
   375
    }
slouken@1956
   376
    if (window->y == SDL_WINDOWPOS_CENTERED) {
slouken@1933
   377
        rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
slouken@1933
   378
    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
slouken@1933
   379
        rect.origin.y = 0;
slouken@1933
   380
    } else {
slouken@1933
   381
        rect.origin.y = window->y;
slouken@1933
   382
    }
slouken@1933
   383
    rect.size.width = window->w;
slouken@1933
   384
    rect.size.height = window->h;
slouken@1933
   385
    ConvertNSRect(&rect);
slouken@1933
   386
slouken@1933
   387
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1933
   388
        style = NSBorderlessWindowMask;
slouken@1933
   389
    } else {
slouken@1933
   390
        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@1933
   391
    }
slouken@1933
   392
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1933
   393
        style |= NSResizableWindowMask;
slouken@1933
   394
    }
slouken@1933
   395
slouken@1933
   396
    nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
slouken@1933
   397
slouken@1933
   398
    [pool release];
slouken@1933
   399
slouken@1951
   400
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@1933
   401
        [nswindow release];
slouken@1933
   402
        return -1;
slouken@1933
   403
    }
slouken@1952
   404
#ifdef SDL_VIDEO_OPENGL_CGL
slouken@1933
   405
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1933
   406
        if (Cocoa_GL_SetupWindow(_this, window) < 0) {
slouken@1933
   407
            Cocoa_DestroyWindow(_this, window);
slouken@1933
   408
            return -1;
slouken@1933
   409
        }
slouken@1933
   410
    }
slouken@1933
   411
#endif
slouken@1933
   412
    return 0;
slouken@1933
   413
}
slouken@1933
   414
slouken@1933
   415
int
slouken@1933
   416
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   417
{
slouken@1933
   418
    NSAutoreleasePool *pool;
slouken@1933
   419
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   420
    NSString *title;
slouken@1933
   421
    int status;
slouken@1933
   422
slouken@1933
   423
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   424
slouken@1933
   425
    /* Query the title from the existing window */
slouken@1933
   426
    title = [nswindow title];
slouken@1933
   427
    if (title) {
slouken@1933
   428
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   429
    }
slouken@1933
   430
slouken@1933
   431
    [pool release];
slouken@1933
   432
slouken@1951
   433
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   434
}
slouken@1933
   435
slouken@1933
   436
void
slouken@1933
   437
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   438
{
slouken@1937
   439
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   440
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   441
    NSString *string;
slouken@1933
   442
slouken@1956
   443
    if(window->title) {
slouken@1956
   444
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1956
   445
    } else {
slouken@1956
   446
        string = [[NSString alloc] init];
slouken@1956
   447
    }
slouken@1933
   448
    [nswindow setTitle:string];
slouken@1933
   449
    [string release];
slouken@1956
   450
slouken@1937
   451
    [pool release];
slouken@1933
   452
}
slouken@1933
   453
slouken@1933
   454
void
slouken@1933
   455
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   456
{
slouken@1937
   457
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   458
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   459
    NSRect rect;
slouken@1933
   460
slouken@1933
   461
    rect.origin.x = window->x;
slouken@1933
   462
    rect.origin.y = window->y;
slouken@1933
   463
    rect.size.width = window->w;
slouken@1933
   464
    rect.size.height = window->h;
slouken@1933
   465
    ConvertNSRect(&rect);
slouken@1933
   466
    rect = [nswindow frameRectForContentRect:rect];
slouken@1933
   467
    [nswindow setFrameOrigin:rect.origin];
slouken@1937
   468
    [pool release];
slouken@1933
   469
}
slouken@1933
   470
slouken@1933
   471
void
slouken@1933
   472
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   473
{
slouken@1937
   474
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   475
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   476
    NSSize size;
slouken@1933
   477
slouken@1933
   478
    size.width = window->w;
slouken@1933
   479
    size.height = window->h;
slouken@1933
   480
    [nswindow setContentSize:size];
slouken@1937
   481
    [pool release];
slouken@1933
   482
}
slouken@1933
   483
slouken@1933
   484
void
slouken@1933
   485
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   486
{
slouken@1937
   487
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   488
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   489
slouken@1956
   490
    if (![nswindow isMiniaturized]) {
slouken@1956
   491
        [nswindow makeKeyAndOrderFront:nil];
slouken@1956
   492
    }
slouken@1937
   493
    [pool release];
slouken@1933
   494
}
slouken@1933
   495
slouken@1933
   496
void
slouken@1933
   497
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   498
{
slouken@1956
   499
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   500
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   501
slouken@1956
   502
    [nswindow orderOut:nil];
slouken@1956
   503
    [pool release];
slouken@1933
   504
}
slouken@1933
   505
slouken@1933
   506
void
slouken@1933
   507
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   508
{
slouken@1937
   509
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   510
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   511
slouken@1933
   512
    [nswindow makeKeyAndOrderFront:nil];
slouken@1937
   513
    [pool release];
slouken@1933
   514
}
slouken@1933
   515
slouken@1933
   516
void
slouken@1933
   517
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   518
{
slouken@1937
   519
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   520
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   521
slouken@1956
   522
    [nswindow zoom:nil];
slouken@1937
   523
    [pool release];
slouken@1933
   524
}
slouken@1933
   525
slouken@1933
   526
void
slouken@1933
   527
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   528
{
slouken@1937
   529
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   530
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   531
slouken@1956
   532
    [nswindow miniaturize:nil];
slouken@1937
   533
    [pool release];
slouken@1933
   534
}
slouken@1933
   535
slouken@1933
   536
void
slouken@1933
   537
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   538
{
slouken@1956
   539
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   540
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   541
slouken@1956
   542
    if ([nswindow isMiniaturized]) {
slouken@1956
   543
        [nswindow deminiaturize:nil];
slouken@1956
   544
    } else if ([nswindow isZoomed]) {
slouken@1956
   545
        [nswindow zoom:nil];
slouken@1956
   546
    }
slouken@1956
   547
    [pool release];
slouken@1933
   548
}
slouken@1933
   549
slouken@1933
   550
void
slouken@1933
   551
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   552
{
slouken@1933
   553
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1933
   554
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1933
   555
        /* FIXME: Grab mouse */
slouken@1933
   556
    } else {
slouken@1933
   557
        /* FIXME: Release mouse */
slouken@1933
   558
    }
slouken@1933
   559
}
slouken@1933
   560
slouken@1933
   561
void
slouken@1933
   562
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   563
{
slouken@1937
   564
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   565
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   566
slouken@1933
   567
    if (data) {
slouken@1952
   568
#ifdef SDL_VIDEO_OPENGL_CGL
slouken@1933
   569
        if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1933
   570
            Cocoa_GL_CleanupWindow(_this, window);
slouken@1933
   571
        }
slouken@1933
   572
#endif
slouken@1933
   573
        [data->listener close];
slouken@1933
   574
        [data->listener release];
slouken@1933
   575
        if (data->created) {
slouken@1933
   576
            [data->window close];
slouken@1933
   577
        }
slouken@1933
   578
        SDL_free(data);
slouken@1933
   579
    }
slouken@1937
   580
    [pool release];
slouken@1933
   581
}
slouken@1933
   582
slouken@1933
   583
SDL_bool
slouken@1933
   584
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   585
{
slouken@1933
   586
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   587
slouken@1933
   588
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1933
   589
        //info->window = nswindow;
slouken@1933
   590
        return SDL_TRUE;
slouken@1933
   591
    } else {
slouken@1933
   592
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
   593
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
   594
        return SDL_FALSE;
slouken@1933
   595
    }
slouken@1933
   596
}
slouken@1933
   597
slouken@1933
   598
/* vi: set ts=4 sw=4 expandtab: */