src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 22 Oct 2009 04:46:11 +0000
changeset 3409 c8f580ebc96a
parent 3075 4c28a9655933
child 3414 1e45c3012a4f
permissions -rw-r--r--
Adam Strzelecki to SDL

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