src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 08 Jul 2010 22:54:03 -0700
changeset 4503 524dfefd554c
parent 4498 3d91e31fcf71
child 4673 c17ac64abb70
permissions -rw-r--r--
Added an event when the clipboard is updated, triggered after the window gains the keyboard focus.
slouken@1933
     1
/*
slouken@1933
     2
    SDL - Simple DirectMedia Layer
slouken@4498
     3
    Copyright (C) 1997-2010 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@4503
   131
slouken@4503
   132
    /* Check to see if someone updated the clipboard */
slouken@4503
   133
    Cocoa_CheckClipboardUpdate(_data->videodata);
slouken@1933
   134
}
slouken@1933
   135
slouken@1933
   136
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   137
{
slouken@2059
   138
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   139
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   140
        SDL_SetMouseFocus(NULL);
slouken@2059
   141
    }
slouken@2059
   142
slouken@2059
   143
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   144
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   145
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   146
    }
slouken@1933
   147
}
slouken@1933
   148
slouken@1933
   149
- (void)windowDidHide:(NSNotification *)aNotification
slouken@1933
   150
{
slouken@3685
   151
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1933
   152
}
slouken@1933
   153
slouken@1933
   154
- (void)windowDidUnhide:(NSNotification *)aNotification
slouken@1933
   155
{
slouken@3685
   156
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1933
   157
}
slouken@1933
   158
slouken@1933
   159
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   160
{
slouken@1959
   161
    int button;
slouken@1933
   162
slouken@1959
   163
    switch ([theEvent buttonNumber]) {
slouken@1959
   164
    case 0:
slouken@1959
   165
        button = SDL_BUTTON_LEFT;
slouken@1959
   166
        break;
slouken@1959
   167
    case 1:
slouken@1959
   168
        button = SDL_BUTTON_RIGHT;
slouken@1959
   169
        break;
slouken@1959
   170
    case 2:
slouken@1959
   171
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   172
        break;
slouken@1959
   173
    default:
slouken@1959
   174
        button = [theEvent buttonNumber];
slouken@1959
   175
        break;
slouken@1959
   176
    }
slouken@4484
   177
    SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
slouken@1933
   178
}
slouken@1933
   179
slouken@1933
   180
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   181
{
slouken@1959
   182
    [self mouseDown:theEvent];
slouken@1933
   183
}
slouken@1933
   184
slouken@1933
   185
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   186
{
slouken@1959
   187
    [self mouseDown:theEvent];
slouken@1933
   188
}
slouken@1933
   189
slouken@1933
   190
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   191
{
slouken@1959
   192
    int button;
slouken@1933
   193
slouken@1959
   194
    switch ([theEvent buttonNumber]) {
slouken@1959
   195
    case 0:
slouken@1959
   196
        button = SDL_BUTTON_LEFT;
slouken@1959
   197
        break;
slouken@1959
   198
    case 1:
slouken@1959
   199
        button = SDL_BUTTON_RIGHT;
slouken@1959
   200
        break;
slouken@1959
   201
    case 2:
slouken@1959
   202
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   203
        break;
slouken@1959
   204
    default:
slouken@1959
   205
        button = [theEvent buttonNumber];
slouken@1959
   206
        break;
slouken@1959
   207
    }
slouken@4484
   208
    SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
slouken@1933
   209
}
slouken@1933
   210
slouken@1933
   211
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   212
{
slouken@1959
   213
    [self mouseUp:theEvent];
slouken@1933
   214
}
slouken@1933
   215
slouken@1933
   216
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   217
{
slouken@1959
   218
    [self mouseUp:theEvent];
slouken@1933
   219
}
slouken@1933
   220
slouken@1933
   221
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   222
{
slouken@3685
   223
    SDL_Window *window = _data->window;
slouken@1933
   224
    NSPoint point;
slouken@1933
   225
slouken@3517
   226
    point = [theEvent locationInWindow];
slouken@3517
   227
    point.y = window->h - point.y;
slouken@3507
   228
    if ( point.x < 0 || point.x >= window->w ||
slouken@3507
   229
         point.y < 0 || point.y >= window->h ) {
slouken@4465
   230
        if (SDL_GetMouseFocus() == window) {
slouken@4465
   231
            SDL_SetMouseFocus(NULL);
slouken@2059
   232
        }
slouken@2059
   233
    } else {
slouken@4484
   234
        SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
slouken@2059
   235
    }
slouken@1933
   236
}
slouken@1933
   237
slouken@1957
   238
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   239
{
slouken@1957
   240
    [self mouseMoved:theEvent];
slouken@1957
   241
}
slouken@1957
   242
slouken@1958
   243
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   244
{
slouken@1958
   245
    [self mouseMoved:theEvent];
slouken@1958
   246
}
slouken@1958
   247
slouken@1958
   248
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   249
{
slouken@1958
   250
    [self mouseMoved:theEvent];
slouken@1958
   251
}
slouken@1958
   252
slouken@1933
   253
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   254
{
slouken@4484
   255
    float x = [theEvent deltaX];
slouken@4484
   256
    float y = [theEvent deltaY];
slouken@4484
   257
slouken@4484
   258
    if (x > 0) {
slouken@4484
   259
        x += 0.9f;
slouken@4484
   260
    } else if (x < 0) {
slouken@4484
   261
        x -= 0.9f;
slouken@4484
   262
    }
slouken@4484
   263
    if (y > 0) {
slouken@4484
   264
        y += 0.9f;
slouken@4484
   265
    } else if (y < 0) {
slouken@4484
   266
        y -= 0.9f;
slouken@4484
   267
    }
slouken@4484
   268
    SDL_SendMouseWheel(_data->window, (int)x, (int)y);
slouken@1933
   269
}
slouken@1933
   270
slouken@1933
   271
@end
slouken@1933
   272
slouken@1973
   273
@interface SDLWindow : NSWindow
slouken@1973
   274
/* These are needed for borderless/fullscreen windows */
slouken@1973
   275
- (BOOL)canBecomeKeyWindow;
slouken@1973
   276
- (BOOL)canBecomeMainWindow;
slouken@1973
   277
@end
slouken@1973
   278
slouken@1973
   279
@implementation SDLWindow
slouken@1973
   280
- (BOOL)canBecomeKeyWindow
slouken@1973
   281
{
slouken@1973
   282
    return YES;
slouken@1973
   283
}
slouken@1973
   284
slouken@1973
   285
- (BOOL)canBecomeMainWindow
slouken@1973
   286
{
slouken@1973
   287
    return YES;
slouken@1973
   288
}
slouken@1973
   289
@end
slouken@1973
   290
slouken@1933
   291
static int
slouken@1951
   292
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   293
{
slouken@1933
   294
    NSAutoreleasePool *pool;
slouken@1951
   295
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3685
   296
    SDL_VideoDisplay *display = window->display;
slouken@3530
   297
    SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
slouken@1933
   298
    SDL_WindowData *data;
slouken@1933
   299
slouken@1933
   300
    /* Allocate the window data */
slouken@1933
   301
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1933
   302
    if (!data) {
slouken@1933
   303
        SDL_OutOfMemory();
slouken@1933
   304
        return -1;
slouken@1933
   305
    }
slouken@3685
   306
    data->window = window;
slouken@3688
   307
    data->nswindow = nswindow;
slouken@1933
   308
    data->created = created;
slouken@3506
   309
    data->display = displaydata->display;
slouken@1951
   310
    data->videodata = videodata;
slouken@1933
   311
slouken@1933
   312
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   313
slouken@1933
   314
    /* Create an event listener for the window */
slouken@1933
   315
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@1933
   316
    [data->listener listen:data];
slouken@1933
   317
slouken@1933
   318
    /* Fill in the SDL window with the window data */
slouken@1933
   319
    {
slouken@3530
   320
        SDL_Rect bounds;
slouken@1933
   321
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   322
        ConvertNSRect(&rect);
slouken@3530
   323
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3530
   324
        window->x = (int)rect.origin.x - bounds.x;
slouken@3530
   325
        window->y = (int)rect.origin.y - bounds.y;
slouken@1933
   326
        window->w = (int)rect.size.width;
slouken@1933
   327
        window->h = (int)rect.size.height;
slouken@1933
   328
    }
slouken@1933
   329
    if ([nswindow isVisible]) {
slouken@1933
   330
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   331
    } else {
slouken@1933
   332
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   333
    }
slouken@1933
   334
    {
slouken@1933
   335
        unsigned int style = [nswindow styleMask];
slouken@1933
   336
slouken@1956
   337
        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
slouken@1933
   338
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   339
        } else {
slouken@1933
   340
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   341
        }
slouken@1933
   342
        if (style & NSResizableWindowMask) {
slouken@1933
   343
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   344
        } else {
slouken@1933
   345
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   346
        }
slouken@1933
   347
    }
slouken@1933
   348
    if ([nswindow isZoomed]) {
slouken@1933
   349
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   350
    } else {
slouken@1933
   351
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   352
    }
slouken@1933
   353
    if ([nswindow isMiniaturized]) {
slouken@1933
   354
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   355
    } else {
slouken@1933
   356
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   357
    }
slouken@1933
   358
    if ([nswindow isKeyWindow]) {
slouken@1933
   359
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   360
        SDL_SetKeyboardFocus(data->window);
slouken@1933
   361
slouken@1933
   362
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1933
   363
            /* FIXME */
slouken@1933
   364
        }
slouken@1933
   365
    }
slouken@1933
   366
slouken@1933
   367
    /* All done! */
slouken@1933
   368
    [pool release];
slouken@1933
   369
    window->driverdata = data;
slouken@1933
   370
    return 0;
slouken@1933
   371
}
slouken@1933
   372
slouken@1933
   373
int
slouken@1933
   374
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   375
{
slouken@3506
   376
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   377
    NSWindow *nswindow;
slouken@3685
   378
    SDL_VideoDisplay *display = window->display;
slouken@1933
   379
    NSRect rect;
slouken@3528
   380
    SDL_Rect bounds;
slouken@1933
   381
    unsigned int style;
slouken@1933
   382
slouken@3528
   383
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   384
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   385
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   386
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3528
   387
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   388
        rect.origin.x = bounds.x;
slouken@3528
   389
    } else {
slouken@3530
   390
        rect.origin.x = bounds.x + window->x;
slouken@1933
   391
    }
slouken@3075
   392
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   393
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   394
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3528
   395
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   396
        rect.origin.y = bounds.y;
slouken@3528
   397
    } else {
slouken@3530
   398
        rect.origin.y = bounds.y + window->y;
slouken@1933
   399
    }
slouken@1933
   400
    rect.size.width = window->w;
slouken@1933
   401
    rect.size.height = window->h;
slouken@1933
   402
    ConvertNSRect(&rect);
slouken@1933
   403
slouken@1933
   404
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1933
   405
        style = NSBorderlessWindowMask;
slouken@1933
   406
    } else {
slouken@1933
   407
        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@1933
   408
    }
slouken@1933
   409
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1933
   410
        style |= NSResizableWindowMask;
slouken@1933
   411
    }
slouken@1933
   412
slouken@3506
   413
    /* Figure out which screen to place this window */
slouken@3506
   414
    NSArray *screens = [NSScreen screens];
slouken@3506
   415
    NSScreen *screen = nil;
slouken@3506
   416
    NSScreen *candidate;
slouken@3525
   417
    int i, count = [screens count];
slouken@3525
   418
    for (i = 0; i < count; ++i) {
slouken@3529
   419
        candidate = [screens objectAtIndex:i];
slouken@3506
   420
        NSRect screenRect = [candidate frame];
slouken@3506
   421
        if (rect.origin.x >= screenRect.origin.x &&
slouken@3506
   422
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@3506
   423
            rect.origin.y >= screenRect.origin.y &&
slouken@3506
   424
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@3506
   425
            screen = candidate;
slouken@3506
   426
            rect.origin.x -= screenRect.origin.x;
slouken@3506
   427
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   428
        }
slouken@3506
   429
    }
slouken@3506
   430
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
slouken@1933
   431
slouken@1933
   432
    [pool release];
slouken@1933
   433
slouken@1951
   434
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@1933
   435
        [nswindow release];
slouken@1933
   436
        return -1;
slouken@1933
   437
    }
slouken@1933
   438
    return 0;
slouken@1933
   439
}
slouken@1933
   440
slouken@1933
   441
int
slouken@1933
   442
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   443
{
slouken@1933
   444
    NSAutoreleasePool *pool;
slouken@1933
   445
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   446
    NSString *title;
slouken@1933
   447
slouken@1933
   448
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   449
slouken@1933
   450
    /* Query the title from the existing window */
slouken@1933
   451
    title = [nswindow title];
slouken@1933
   452
    if (title) {
slouken@1933
   453
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   454
    }
slouken@1933
   455
slouken@1933
   456
    [pool release];
slouken@1933
   457
slouken@1951
   458
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   459
}
slouken@1933
   460
slouken@1933
   461
void
slouken@1933
   462
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   463
{
slouken@1937
   464
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   465
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   466
    NSString *string;
slouken@1933
   467
slouken@1956
   468
    if(window->title) {
slouken@1956
   469
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1956
   470
    } else {
slouken@1956
   471
        string = [[NSString alloc] init];
slouken@1956
   472
    }
slouken@1933
   473
    [nswindow setTitle:string];
slouken@1933
   474
    [string release];
slouken@1956
   475
slouken@1937
   476
    [pool release];
slouken@1933
   477
}
slouken@1933
   478
slouken@1933
   479
void
slouken@1933
   480
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   481
{
slouken@1937
   482
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   483
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@3685
   484
    SDL_VideoDisplay *display = window->display;
slouken@1933
   485
    NSRect rect;
slouken@3528
   486
    SDL_Rect bounds;
slouken@1933
   487
slouken@3528
   488
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   489
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   490
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   491
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3075
   492
    } else {
slouken@3530
   493
        rect.origin.x = bounds.x + window->x;
slouken@3075
   494
    }
slouken@3075
   495
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   496
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   497
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3075
   498
    } else {
slouken@3530
   499
        rect.origin.y = bounds.y + window->y;
slouken@3075
   500
    }
slouken@1933
   501
    rect.size.width = window->w;
slouken@1933
   502
    rect.size.height = window->h;
slouken@1933
   503
    ConvertNSRect(&rect);
slouken@1933
   504
    rect = [nswindow frameRectForContentRect:rect];
slouken@1933
   505
    [nswindow setFrameOrigin:rect.origin];
slouken@1937
   506
    [pool release];
slouken@1933
   507
}
slouken@1933
   508
slouken@1933
   509
void
slouken@1933
   510
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   511
{
slouken@1937
   512
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   513
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   514
    NSSize size;
slouken@1933
   515
slouken@1933
   516
    size.width = window->w;
slouken@1933
   517
    size.height = window->h;
slouken@1933
   518
    [nswindow setContentSize:size];
slouken@1937
   519
    [pool release];
slouken@1933
   520
}
slouken@1933
   521
slouken@1933
   522
void
slouken@1933
   523
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   524
{
slouken@1937
   525
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   526
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   527
slouken@1956
   528
    if (![nswindow isMiniaturized]) {
slouken@1956
   529
        [nswindow makeKeyAndOrderFront:nil];
slouken@1956
   530
    }
slouken@1937
   531
    [pool release];
slouken@1933
   532
}
slouken@1933
   533
slouken@1933
   534
void
slouken@1933
   535
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   536
{
slouken@1956
   537
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   538
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   539
slouken@1956
   540
    [nswindow orderOut:nil];
slouken@1956
   541
    [pool release];
slouken@1933
   542
}
slouken@1933
   543
slouken@1933
   544
void
slouken@1933
   545
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   546
{
slouken@1937
   547
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   548
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   549
slouken@1933
   550
    [nswindow makeKeyAndOrderFront:nil];
slouken@1937
   551
    [pool release];
slouken@1933
   552
}
slouken@1933
   553
slouken@1933
   554
void
slouken@1933
   555
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   556
{
slouken@1937
   557
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   558
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   559
slouken@1956
   560
    [nswindow zoom:nil];
slouken@1937
   561
    [pool release];
slouken@1933
   562
}
slouken@1933
   563
slouken@1933
   564
void
slouken@1933
   565
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   566
{
slouken@1937
   567
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   568
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   569
slouken@1956
   570
    [nswindow miniaturize:nil];
slouken@1937
   571
    [pool release];
slouken@1933
   572
}
slouken@1933
   573
slouken@1933
   574
void
slouken@1933
   575
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   576
{
slouken@1956
   577
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   578
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   579
slouken@1956
   580
    if ([nswindow isMiniaturized]) {
slouken@1956
   581
        [nswindow deminiaturize:nil];
slouken@1956
   582
    } else if ([nswindow isZoomed]) {
slouken@1956
   583
        [nswindow zoom:nil];
slouken@1956
   584
    }
slouken@1956
   585
    [pool release];
slouken@1933
   586
}
slouken@1933
   587
slouken@1933
   588
void
slouken@1933
   589
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   590
{
slouken@1933
   591
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1933
   592
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1933
   593
        /* FIXME: Grab mouse */
slouken@1933
   594
    } else {
slouken@1933
   595
        /* FIXME: Release mouse */
slouken@1933
   596
    }
slouken@1933
   597
}
slouken@1933
   598
slouken@1933
   599
void
slouken@1933
   600
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   601
{
slouken@1937
   602
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   603
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   604
slouken@1933
   605
    if (data) {
slouken@1933
   606
        [data->listener close];
slouken@1933
   607
        [data->listener release];
slouken@1933
   608
        if (data->created) {
slouken@3688
   609
            [data->nswindow close];
slouken@1933
   610
        }
slouken@1933
   611
        SDL_free(data);
slouken@1933
   612
    }
slouken@1937
   613
    [pool release];
slouken@1933
   614
}
slouken@1933
   615
slouken@1933
   616
SDL_bool
slouken@1933
   617
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   618
{
slouken@4465
   619
    //NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   620
slouken@1933
   621
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1933
   622
        //info->window = nswindow;
slouken@1933
   623
        return SDL_TRUE;
slouken@1933
   624
    } else {
slouken@1933
   625
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
   626
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
   627
        return SDL_FALSE;
slouken@1933
   628
    }
slouken@1933
   629
}
slouken@1933
   630
slouken@1933
   631
/* vi: set ts=4 sw=4 expandtab: */