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