src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 09 May 2010 20:47:22 -0700
changeset 4465 3e69e077cb95
parent 3688 6512cba48440
child 4484 9322f7db8603
permissions -rw-r--r--
Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.

Plus, this lets me start implementing cursor support.
slouken@1933
     1
/*
slouken@1933
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@1933
    34
    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
slouken@1933
    35
}
slouken@1933
    36
slouken@1933
    37
@implementation Cocoa_WindowListener
slouken@1933
    38
slouken@1933
    39
- (void)listen:(SDL_WindowData *)data
slouken@1933
    40
{
slouken@1933
    41
    NSNotificationCenter *center;
slouken@1933
    42
slouken@1933
    43
    _data = data;
slouken@1933
    44
slouken@1933
    45
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    46
slouken@3688
    47
    [_data->nswindow setNextResponder:self];
slouken@3688
    48
    if ([_data->nswindow delegate] != nil) {
slouken@3688
    49
        [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->nswindow];
slouken@3688
    50
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->nswindow];
slouken@3688
    51
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->nswindow];
slouken@3688
    52
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->nswindow];
slouken@3688
    53
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->nswindow];
slouken@3688
    54
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->nswindow];
slouken@3688
    55
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->nswindow];
slouken@1933
    56
    } else {
slouken@3688
    57
        [_data->nswindow setDelegate:self];
slouken@1933
    58
    }
slouken@1933
    59
    [center addObserver:self selector:@selector(windowDidHide:) name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    60
    [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
slouken@1933
    61
slouken@3688
    62
    [_data->nswindow setAcceptsMouseMovedEvents:YES];
slouken@1933
    63
}
slouken@1933
    64
slouken@1933
    65
- (void)close
slouken@1933
    66
{
slouken@1933
    67
    NSNotificationCenter *center;
slouken@1933
    68
slouken@1933
    69
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    70
slouken@3688
    71
    [_data->nswindow setNextResponder:nil];
slouken@3688
    72
    if ([_data->nswindow delegate] != self) {
slouken@3688
    73
        [center removeObserver:self name:NSWindowDidExposeNotification object:_data->nswindow];
slouken@3688
    74
        [center removeObserver:self name:NSWindowDidMoveNotification object:_data->nswindow];
slouken@3688
    75
        [center removeObserver:self name:NSWindowDidResizeNotification object:_data->nswindow];
slouken@3688
    76
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->nswindow];
slouken@3688
    77
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->nswindow];
slouken@3688
    78
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->nswindow];
slouken@3688
    79
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->nswindow];
slouken@1933
    80
    } else {
slouken@3688
    81
        [_data->nswindow setDelegate:nil];
slouken@1933
    82
    }
slouken@1933
    83
    [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    84
    [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
slouken@1933
    85
}
slouken@1933
    86
slouken@1933
    87
- (BOOL)windowShouldClose:(id)sender
slouken@1933
    88
{
slouken@3685
    89
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
    90
    return NO;
slouken@1933
    91
}
slouken@1933
    92
slouken@1933
    93
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
    94
{
slouken@3685
    95
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
    96
}
slouken@1933
    97
slouken@1933
    98
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
    99
{
slouken@1933
   100
    int x, y;
slouken@3688
   101
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
slouken@1933
   102
    ConvertNSRect(&rect);
slouken@3507
   103
    x = (int)rect.origin.x;
slouken@3507
   104
    y = (int)rect.origin.y;
slouken@3685
   105
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   106
}
slouken@1933
   107
slouken@1933
   108
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   109
{
slouken@1933
   110
    int w, h;
slouken@3688
   111
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
slouken@1933
   112
    w = (int)rect.size.width;
slouken@1933
   113
    h = (int)rect.size.height;
slouken@3685
   114
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@1933
   115
}
slouken@1933
   116
slouken@1933
   117
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   118
{
slouken@3685
   119
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   120
}
slouken@1933
   121
slouken@1933
   122
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   123
{
slouken@3685
   124
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   125
}
slouken@1933
   126
slouken@1933
   127
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   128
{
slouken@2059
   129
    /* We're going to get keyboard events, since we're key. */
slouken@4465
   130
    SDL_SetKeyboardFocus(_data->window);
slouken@1933
   131
}
slouken@1933
   132
slouken@1933
   133
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   134
{
slouken@2059
   135
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   136
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   137
        SDL_SetMouseFocus(NULL);
slouken@2059
   138
    }
slouken@2059
   139
slouken@2059
   140
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   141
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   142
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   143
    }
slouken@1933
   144
}
slouken@1933
   145
slouken@1933
   146
- (void)windowDidHide:(NSNotification *)aNotification
slouken@1933
   147
{
slouken@3685
   148
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1933
   149
}
slouken@1933
   150
slouken@1933
   151
- (void)windowDidUnhide:(NSNotification *)aNotification
slouken@1933
   152
{
slouken@3685
   153
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1933
   154
}
slouken@1933
   155
slouken@1933
   156
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   157
{
slouken@1959
   158
    int button;
slouken@1933
   159
slouken@1959
   160
    switch ([theEvent buttonNumber]) {
slouken@1959
   161
    case 0:
slouken@1959
   162
        button = SDL_BUTTON_LEFT;
slouken@1959
   163
        break;
slouken@1959
   164
    case 1:
slouken@1959
   165
        button = SDL_BUTTON_RIGHT;
slouken@1959
   166
        break;
slouken@1959
   167
    case 2:
slouken@1959
   168
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   169
        break;
slouken@1959
   170
    default:
slouken@1959
   171
        button = [theEvent buttonNumber];
slouken@1959
   172
        break;
slouken@1959
   173
    }
slouken@4465
   174
    SDL_SendMouseButton(SDL_PRESSED, button);
slouken@1933
   175
}
slouken@1933
   176
slouken@1933
   177
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   178
{
slouken@1959
   179
    [self mouseDown:theEvent];
slouken@1933
   180
}
slouken@1933
   181
slouken@1933
   182
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   183
{
slouken@1959
   184
    [self mouseDown:theEvent];
slouken@1933
   185
}
slouken@1933
   186
slouken@1933
   187
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   188
{
slouken@1959
   189
    int button;
slouken@1933
   190
slouken@1959
   191
    switch ([theEvent buttonNumber]) {
slouken@1959
   192
    case 0:
slouken@1959
   193
        button = SDL_BUTTON_LEFT;
slouken@1959
   194
        break;
slouken@1959
   195
    case 1:
slouken@1959
   196
        button = SDL_BUTTON_RIGHT;
slouken@1959
   197
        break;
slouken@1959
   198
    case 2:
slouken@1959
   199
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   200
        break;
slouken@1959
   201
    default:
slouken@1959
   202
        button = [theEvent buttonNumber];
slouken@1959
   203
        break;
slouken@1959
   204
    }
slouken@4465
   205
    SDL_SendMouseButton(SDL_RELEASED, button);
slouken@1933
   206
}
slouken@1933
   207
slouken@1933
   208
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   209
{
slouken@1959
   210
    [self mouseUp:theEvent];
slouken@1933
   211
}
slouken@1933
   212
slouken@1933
   213
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   214
{
slouken@1959
   215
    [self mouseUp:theEvent];
slouken@1933
   216
}
slouken@1933
   217
slouken@1933
   218
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   219
{
slouken@3685
   220
    SDL_Window *window = _data->window;
slouken@1933
   221
    NSPoint point;
slouken@1933
   222
slouken@3517
   223
    point = [theEvent locationInWindow];
slouken@3517
   224
    point.y = window->h - point.y;
slouken@3507
   225
    if ( point.x < 0 || point.x >= window->w ||
slouken@3507
   226
         point.y < 0 || point.y >= window->h ) {
slouken@4465
   227
        if (SDL_GetMouseFocus() == window) {
slouken@4465
   228
            SDL_SetMouseFocus(NULL);
slouken@2059
   229
        }
slouken@2059
   230
    } else {
slouken@4465
   231
        SDL_SetMouseFocus(_data->window);
slouken@4465
   232
        SDL_SendMouseMotion(0, (int)point.x, (int)point.y);
slouken@2059
   233
    }
slouken@1933
   234
}
slouken@1933
   235
slouken@1957
   236
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   237
{
slouken@1957
   238
    [self mouseMoved:theEvent];
slouken@1957
   239
}
slouken@1957
   240
slouken@1958
   241
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   242
{
slouken@1958
   243
    [self mouseMoved:theEvent];
slouken@1958
   244
}
slouken@1958
   245
slouken@1958
   246
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   247
{
slouken@1958
   248
    [self mouseMoved:theEvent];
slouken@1958
   249
}
slouken@1958
   250
slouken@1933
   251
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   252
{
slouken@4465
   253
    SDL_SendMouseWheel((int)([theEvent deltaX]+0.9f), (int)([theEvent deltaY]+0.9f));
slouken@1933
   254
}
slouken@1933
   255
slouken@1933
   256
@end
slouken@1933
   257
slouken@1973
   258
@interface SDLWindow : NSWindow
slouken@1973
   259
/* These are needed for borderless/fullscreen windows */
slouken@1973
   260
- (BOOL)canBecomeKeyWindow;
slouken@1973
   261
- (BOOL)canBecomeMainWindow;
slouken@1973
   262
@end
slouken@1973
   263
slouken@1973
   264
@implementation SDLWindow
slouken@1973
   265
- (BOOL)canBecomeKeyWindow
slouken@1973
   266
{
slouken@1973
   267
    return YES;
slouken@1973
   268
}
slouken@1973
   269
slouken@1973
   270
- (BOOL)canBecomeMainWindow
slouken@1973
   271
{
slouken@1973
   272
    return YES;
slouken@1973
   273
}
slouken@1973
   274
@end
slouken@1973
   275
slouken@1933
   276
static int
slouken@1951
   277
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   278
{
slouken@1933
   279
    NSAutoreleasePool *pool;
slouken@1951
   280
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3685
   281
    SDL_VideoDisplay *display = window->display;
slouken@3530
   282
    SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
slouken@1933
   283
    SDL_WindowData *data;
slouken@1933
   284
slouken@1933
   285
    /* Allocate the window data */
slouken@1933
   286
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1933
   287
    if (!data) {
slouken@1933
   288
        SDL_OutOfMemory();
slouken@1933
   289
        return -1;
slouken@1933
   290
    }
slouken@3685
   291
    data->window = window;
slouken@3688
   292
    data->nswindow = nswindow;
slouken@1933
   293
    data->created = created;
slouken@3506
   294
    data->display = displaydata->display;
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@3530
   305
        SDL_Rect bounds;
slouken@1933
   306
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   307
        ConvertNSRect(&rect);
slouken@3530
   308
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3530
   309
        window->x = (int)rect.origin.x - bounds.x;
slouken@3530
   310
        window->y = (int)rect.origin.y - bounds.y;
slouken@1933
   311
        window->w = (int)rect.size.width;
slouken@1933
   312
        window->h = (int)rect.size.height;
slouken@1933
   313
    }
slouken@1933
   314
    if ([nswindow isVisible]) {
slouken@1933
   315
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   316
    } else {
slouken@1933
   317
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   318
    }
slouken@1933
   319
    {
slouken@1933
   320
        unsigned int style = [nswindow styleMask];
slouken@1933
   321
slouken@1956
   322
        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
slouken@1933
   323
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   324
        } else {
slouken@1933
   325
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   326
        }
slouken@1933
   327
        if (style & NSResizableWindowMask) {
slouken@1933
   328
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   329
        } else {
slouken@1933
   330
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   331
        }
slouken@1933
   332
    }
slouken@1933
   333
    if ([nswindow isZoomed]) {
slouken@1933
   334
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   335
    } else {
slouken@1933
   336
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   337
    }
slouken@1933
   338
    if ([nswindow isMiniaturized]) {
slouken@1933
   339
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   340
    } else {
slouken@1933
   341
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   342
    }
slouken@1933
   343
    if ([nswindow isKeyWindow]) {
slouken@1933
   344
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   345
        SDL_SetKeyboardFocus(data->window);
slouken@1933
   346
slouken@1933
   347
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1933
   348
            /* FIXME */
slouken@1933
   349
        }
slouken@1933
   350
    }
slouken@1933
   351
slouken@1933
   352
    /* All done! */
slouken@1933
   353
    [pool release];
slouken@1933
   354
    window->driverdata = data;
slouken@1933
   355
    return 0;
slouken@1933
   356
}
slouken@1933
   357
slouken@1933
   358
int
slouken@1933
   359
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   360
{
slouken@3506
   361
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   362
    NSWindow *nswindow;
slouken@3685
   363
    SDL_VideoDisplay *display = window->display;
slouken@1933
   364
    NSRect rect;
slouken@3528
   365
    SDL_Rect bounds;
slouken@1933
   366
    unsigned int style;
slouken@1933
   367
slouken@3528
   368
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   369
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   370
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   371
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3528
   372
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   373
        rect.origin.x = bounds.x;
slouken@3528
   374
    } else {
slouken@3530
   375
        rect.origin.x = bounds.x + window->x;
slouken@1933
   376
    }
slouken@3075
   377
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   378
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   379
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3528
   380
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   381
        rect.origin.y = bounds.y;
slouken@3528
   382
    } else {
slouken@3530
   383
        rect.origin.y = bounds.y + window->y;
slouken@1933
   384
    }
slouken@1933
   385
    rect.size.width = window->w;
slouken@1933
   386
    rect.size.height = window->h;
slouken@1933
   387
    ConvertNSRect(&rect);
slouken@1933
   388
slouken@1933
   389
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1933
   390
        style = NSBorderlessWindowMask;
slouken@1933
   391
    } else {
slouken@1933
   392
        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@1933
   393
    }
slouken@1933
   394
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1933
   395
        style |= NSResizableWindowMask;
slouken@1933
   396
    }
slouken@1933
   397
slouken@3506
   398
    /* Figure out which screen to place this window */
slouken@3506
   399
    NSArray *screens = [NSScreen screens];
slouken@3506
   400
    NSScreen *screen = nil;
slouken@3506
   401
    NSScreen *candidate;
slouken@3525
   402
    int i, count = [screens count];
slouken@3525
   403
    for (i = 0; i < count; ++i) {
slouken@3529
   404
        candidate = [screens objectAtIndex:i];
slouken@3506
   405
        NSRect screenRect = [candidate frame];
slouken@3506
   406
        if (rect.origin.x >= screenRect.origin.x &&
slouken@3506
   407
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@3506
   408
            rect.origin.y >= screenRect.origin.y &&
slouken@3506
   409
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@3506
   410
            screen = candidate;
slouken@3506
   411
            rect.origin.x -= screenRect.origin.x;
slouken@3506
   412
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   413
        }
slouken@3506
   414
    }
slouken@3506
   415
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
slouken@1933
   416
slouken@1933
   417
    [pool release];
slouken@1933
   418
slouken@1951
   419
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@1933
   420
        [nswindow release];
slouken@1933
   421
        return -1;
slouken@1933
   422
    }
slouken@1933
   423
    return 0;
slouken@1933
   424
}
slouken@1933
   425
slouken@1933
   426
int
slouken@1933
   427
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   428
{
slouken@1933
   429
    NSAutoreleasePool *pool;
slouken@1933
   430
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   431
    NSString *title;
slouken@1933
   432
slouken@1933
   433
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   434
slouken@1933
   435
    /* Query the title from the existing window */
slouken@1933
   436
    title = [nswindow title];
slouken@1933
   437
    if (title) {
slouken@1933
   438
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   439
    }
slouken@1933
   440
slouken@1933
   441
    [pool release];
slouken@1933
   442
slouken@1951
   443
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   444
}
slouken@1933
   445
slouken@1933
   446
void
slouken@1933
   447
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   448
{
slouken@1937
   449
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   450
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   451
    NSString *string;
slouken@1933
   452
slouken@1956
   453
    if(window->title) {
slouken@1956
   454
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1956
   455
    } else {
slouken@1956
   456
        string = [[NSString alloc] init];
slouken@1956
   457
    }
slouken@1933
   458
    [nswindow setTitle:string];
slouken@1933
   459
    [string release];
slouken@1956
   460
slouken@1937
   461
    [pool release];
slouken@1933
   462
}
slouken@1933
   463
slouken@1933
   464
void
slouken@1933
   465
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   466
{
slouken@1937
   467
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   468
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@3685
   469
    SDL_VideoDisplay *display = window->display;
slouken@1933
   470
    NSRect rect;
slouken@3528
   471
    SDL_Rect bounds;
slouken@1933
   472
slouken@3528
   473
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   474
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   475
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   476
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3075
   477
    } else {
slouken@3530
   478
        rect.origin.x = bounds.x + window->x;
slouken@3075
   479
    }
slouken@3075
   480
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   481
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   482
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3075
   483
    } else {
slouken@3530
   484
        rect.origin.y = bounds.y + window->y;
slouken@3075
   485
    }
slouken@1933
   486
    rect.size.width = window->w;
slouken@1933
   487
    rect.size.height = window->h;
slouken@1933
   488
    ConvertNSRect(&rect);
slouken@1933
   489
    rect = [nswindow frameRectForContentRect:rect];
slouken@1933
   490
    [nswindow setFrameOrigin:rect.origin];
slouken@1937
   491
    [pool release];
slouken@1933
   492
}
slouken@1933
   493
slouken@1933
   494
void
slouken@1933
   495
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   496
{
slouken@1937
   497
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   498
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   499
    NSSize size;
slouken@1933
   500
slouken@1933
   501
    size.width = window->w;
slouken@1933
   502
    size.height = window->h;
slouken@1933
   503
    [nswindow setContentSize:size];
slouken@1937
   504
    [pool release];
slouken@1933
   505
}
slouken@1933
   506
slouken@1933
   507
void
slouken@1933
   508
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   509
{
slouken@1937
   510
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   511
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   512
slouken@1956
   513
    if (![nswindow isMiniaturized]) {
slouken@1956
   514
        [nswindow makeKeyAndOrderFront:nil];
slouken@1956
   515
    }
slouken@1937
   516
    [pool release];
slouken@1933
   517
}
slouken@1933
   518
slouken@1933
   519
void
slouken@1933
   520
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   521
{
slouken@1956
   522
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   523
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   524
slouken@1956
   525
    [nswindow orderOut:nil];
slouken@1956
   526
    [pool release];
slouken@1933
   527
}
slouken@1933
   528
slouken@1933
   529
void
slouken@1933
   530
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   531
{
slouken@1937
   532
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   533
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   534
slouken@1933
   535
    [nswindow makeKeyAndOrderFront:nil];
slouken@1937
   536
    [pool release];
slouken@1933
   537
}
slouken@1933
   538
slouken@1933
   539
void
slouken@1933
   540
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   541
{
slouken@1937
   542
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   543
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   544
slouken@1956
   545
    [nswindow zoom:nil];
slouken@1937
   546
    [pool release];
slouken@1933
   547
}
slouken@1933
   548
slouken@1933
   549
void
slouken@1933
   550
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   551
{
slouken@1937
   552
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   553
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   554
slouken@1956
   555
    [nswindow miniaturize:nil];
slouken@1937
   556
    [pool release];
slouken@1933
   557
}
slouken@1933
   558
slouken@1933
   559
void
slouken@1933
   560
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   561
{
slouken@1956
   562
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   563
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   564
slouken@1956
   565
    if ([nswindow isMiniaturized]) {
slouken@1956
   566
        [nswindow deminiaturize:nil];
slouken@1956
   567
    } else if ([nswindow isZoomed]) {
slouken@1956
   568
        [nswindow zoom:nil];
slouken@1956
   569
    }
slouken@1956
   570
    [pool release];
slouken@1933
   571
}
slouken@1933
   572
slouken@1933
   573
void
slouken@1933
   574
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   575
{
slouken@1933
   576
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1933
   577
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1933
   578
        /* FIXME: Grab mouse */
slouken@1933
   579
    } else {
slouken@1933
   580
        /* FIXME: Release mouse */
slouken@1933
   581
    }
slouken@1933
   582
}
slouken@1933
   583
slouken@1933
   584
void
slouken@1933
   585
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   586
{
slouken@1937
   587
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   588
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   589
slouken@1933
   590
    if (data) {
slouken@1933
   591
        [data->listener close];
slouken@1933
   592
        [data->listener release];
slouken@1933
   593
        if (data->created) {
slouken@3688
   594
            [data->nswindow close];
slouken@1933
   595
        }
slouken@1933
   596
        SDL_free(data);
slouken@1933
   597
    }
slouken@1937
   598
    [pool release];
slouken@1933
   599
}
slouken@1933
   600
slouken@1933
   601
SDL_bool
slouken@1933
   602
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   603
{
slouken@4465
   604
    //NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   605
slouken@1933
   606
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1933
   607
        //info->window = nswindow;
slouken@1933
   608
        return SDL_TRUE;
slouken@1933
   609
    } else {
slouken@1933
   610
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
   611
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
   612
        return SDL_FALSE;
slouken@1933
   613
    }
slouken@1933
   614
}
slouken@1933
   615
slouken@1933
   616
/* vi: set ts=4 sw=4 expandtab: */