src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Wed, 18 Jan 2012 22:22:54 -0500
changeset 6231 5eecf59b698f
parent 6185 b91e7565e877
child 6301 e8a69c5378e7
permissions -rwxr-xr-x
Fixed bug 1372 - OSX Window Maximize/Resize Doesn't Update Window Position

Alex Nankervis 2012-01-15 14:20:01 PST

SDL_cocoawindow.m, windowDidResize needs to also send a window move event.
Depending on the corner you resize a window from, or when maximizing a window,
the window position will change. Discovered this when creating a maximized
window and found that the window position was stuck at the un-maximized
window's value.

Diff with fix attached.
slouken@1933
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1933
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1933
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1933
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1933
    20
*/
slouken@1933
    21
#include "SDL_config.h"
slouken@1933
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    24
slouken@1933
    25
#include "SDL_syswm.h"
slouken@5398
    26
#include "SDL_timer.h"  /* For SDL_GetTicks() */
slouken@1933
    27
#include "../SDL_sysvideo.h"
slouken@1933
    28
#include "../../events/SDL_keyboard_c.h"
slouken@1933
    29
#include "../../events/SDL_mouse_c.h"
slouken@4673
    30
#include "../../events/SDL_touch_c.h"
slouken@1933
    31
#include "../../events/SDL_windowevents_c.h"
slouken@1933
    32
#include "SDL_cocoavideo.h"
eligottlieb@4811
    33
#include "SDL_cocoashape.h"
gzjjgod@5057
    34
#include "SDL_cocoamouse.h"
slouken@1933
    35
slouken@5398
    36
slouken@5398
    37
static Uint32 s_moveHack;
slouken@5398
    38
slouken@1933
    39
static __inline__ void ConvertNSRect(NSRect *r)
slouken@1933
    40
{
slouken@1933
    41
    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
slouken@1933
    42
}
slouken@1933
    43
slouken@1933
    44
@implementation Cocoa_WindowListener
slouken@1933
    45
slouken@1933
    46
- (void)listen:(SDL_WindowData *)data
slouken@1933
    47
{
slouken@1933
    48
    NSNotificationCenter *center;
slouken@5371
    49
    NSWindow *window = data->nswindow;
slouken@5371
    50
    NSView *view = [window contentView];
slouken@1933
    51
slouken@1933
    52
    _data = data;
slouken@1933
    53
slouken@1933
    54
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    55
slouken@5374
    56
    if ([window delegate] != nil) {
slouken@5374
    57
        [center addObserver:self selector:@selector(windowDidExpose:) name:NSWindowDidExposeNotification object:window];
slouken@5374
    58
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
slouken@5374
    59
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
slouken@5374
    60
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
slouken@5374
    61
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5374
    62
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
slouken@5374
    63
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
slouken@5374
    64
    } else {
slouken@5374
    65
        [window setDelegate:self];
slouken@5374
    66
    }
slouken@1933
    67
slouken@5371
    68
    [window setNextResponder:self];
slouken@5371
    69
    [window setAcceptsMouseMovedEvents:YES];
slouken@5371
    70
slouken@5371
    71
    [view setNextResponder:self];
icculus@6108
    72
icculus@5646
    73
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
icculus@6108
    74
    if ([view respondsToSelector:@selector(setAcceptsTouchEvents:)]) {
icculus@6108
    75
        [view setAcceptsTouchEvents:YES];
icculus@6108
    76
    }
slouken@4925
    77
#endif
slouken@1933
    78
}
slouken@1933
    79
slouken@1933
    80
- (void)close
slouken@1933
    81
{
slouken@1933
    82
    NSNotificationCenter *center;
slouken@5371
    83
    NSWindow *window = _data->nswindow;
slouken@5371
    84
    NSView *view = [window contentView];
slouken@1933
    85
slouken@1933
    86
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    87
slouken@5374
    88
    if ([window delegate] != self) {
slouken@5374
    89
        [center removeObserver:self name:NSWindowDidExposeNotification object:window];
slouken@5374
    90
        [center removeObserver:self name:NSWindowDidMoveNotification object:window];
slouken@5374
    91
        [center removeObserver:self name:NSWindowDidResizeNotification object:window];
slouken@5374
    92
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
slouken@5374
    93
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5374
    94
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
slouken@5374
    95
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
slouken@5374
    96
    } else {
slouken@5374
    97
        [window setDelegate:nil];
slouken@5374
    98
    }
slouken@5371
    99
slouken@5374
   100
    if ([window nextResponder] == self) {
slouken@5374
   101
        [window setNextResponder:nil];
slouken@5374
   102
    }
slouken@5374
   103
    if ([view nextResponder] == self) {
slouken@5374
   104
        [view setNextResponder:nil];
slouken@5374
   105
    }
slouken@1933
   106
}
slouken@1933
   107
slouken@1933
   108
- (BOOL)windowShouldClose:(id)sender
slouken@1933
   109
{
slouken@3685
   110
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
   111
    return NO;
slouken@1933
   112
}
slouken@1933
   113
slouken@1933
   114
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
   115
{
slouken@3685
   116
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
   117
}
slouken@1933
   118
slouken@1933
   119
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
   120
{
slouken@1933
   121
    int x, y;
icculus@5564
   122
    SDL_VideoDevice *device = SDL_GetVideoDevice();
slouken@5398
   123
    SDL_Window *window = _data->window;
slouken@5398
   124
    NSWindow *nswindow = _data->nswindow;
slouken@5398
   125
    NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   126
    ConvertNSRect(&rect);
slouken@5398
   127
slouken@5398
   128
    if (s_moveHack) {
slouken@5398
   129
        SDL_bool blockMove = ((SDL_GetTicks() - s_moveHack) < 500);
slouken@5398
   130
slouken@5398
   131
        s_moveHack = 0;
slouken@5398
   132
slouken@5398
   133
        if (blockMove) {
slouken@5398
   134
            /* Cocoa is adjusting the window in response to a mode change */
slouken@5398
   135
            rect.origin.x = window->x;
slouken@5398
   136
            rect.origin.y = window->y;
slouken@5398
   137
            ConvertNSRect(&rect);
slouken@5398
   138
            [nswindow setFrameOrigin:rect.origin];
slouken@5398
   139
            return;
slouken@5398
   140
        }
slouken@5398
   141
    }
slouken@5398
   142
slouken@3507
   143
    x = (int)rect.origin.x;
slouken@3507
   144
    y = (int)rect.origin.y;
icculus@5564
   145
icculus@5564
   146
    if (window == device->current_glwin) {
icculus@5564
   147
        [((NSOpenGLContext *) device->current_glctx) update];
icculus@5564
   148
    }
icculus@5564
   149
slouken@5398
   150
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   151
}
slouken@1933
   152
slouken@1933
   153
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   154
{
icculus@5564
   155
    SDL_VideoDevice *device = SDL_GetVideoDevice();
slouken@6231
   156
    int x, y, w, h;
slouken@3688
   157
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
slouken@6231
   158
    ConvertNSRect(&rect);
slouken@6231
   159
    x = (int)rect.origin.x;
slouken@6231
   160
    y = (int)rect.origin.y;
slouken@1933
   161
    w = (int)rect.size.width;
slouken@1933
   162
    h = (int)rect.size.height;
slouken@4937
   163
    if (SDL_IsShapedWindow(_data->window))
slouken@4937
   164
        Cocoa_ResizeWindowShape(_data->window);
icculus@5564
   165
icculus@5564
   166
    if (_data->window == device->current_glwin) {
icculus@5564
   167
        [((NSOpenGLContext *) device->current_glctx) update];
icculus@5564
   168
    }
icculus@5564
   169
slouken@6231
   170
    /* The window can move during a resize event, such as when maximizing
slouken@6231
   171
       or resizing from a corner */
slouken@6231
   172
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@3685
   173
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@1933
   174
}
slouken@1933
   175
slouken@1933
   176
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   177
{
slouken@3685
   178
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   179
}
slouken@1933
   180
slouken@1933
   181
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   182
{
slouken@3685
   183
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   184
}
slouken@1933
   185
slouken@1933
   186
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   187
{
slouken@5367
   188
    SDL_Window *window = _data->window;
slouken@5367
   189
slouken@3688
   190
    /* We're going to get keyboard events, since we're key. */
slouken@5367
   191
    SDL_SetKeyboardFocus(window);
slouken@5367
   192
slouken@5367
   193
    /* If we just gained focus we need the updated mouse position */
slouken@5396
   194
    {
slouken@5367
   195
        NSPoint point;
slouken@5396
   196
        int x, y;
slouken@5396
   197
slouken@5396
   198
        point = [_data->nswindow mouseLocationOutsideOfEventStream];
slouken@5396
   199
        x = (int)point.x;
slouken@5396
   200
        y = (int)(window->h - point.y);
slouken@5396
   201
slouken@5396
   202
        if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
slouken@5396
   203
            if (SDL_GetMouseFocus() != window) {
slouken@5396
   204
                [self mouseEntered:nil];
slouken@5396
   205
            }
slouken@5396
   206
            SDL_SendMouseMotion(window, 0, x, y);
slouken@5396
   207
        }
slouken@5367
   208
    }
slouken@1962
   209
slouken@4503
   210
    /* Check to see if someone updated the clipboard */
slouken@4503
   211
    Cocoa_CheckClipboardUpdate(_data->videodata);
slouken@1933
   212
}
slouken@1933
   213
slouken@1933
   214
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   215
{
slouken@2059
   216
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   217
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   218
        SDL_SetMouseFocus(NULL);
slouken@2059
   219
    }
slouken@2059
   220
slouken@2059
   221
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   222
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   223
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   224
    }
slouken@1933
   225
}
slouken@1933
   226
slouken@1933
   227
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   228
{
slouken@1959
   229
    int button;
slouken@1933
   230
slouken@1959
   231
    switch ([theEvent buttonNumber]) {
slouken@1959
   232
    case 0:
slouken@1959
   233
        button = SDL_BUTTON_LEFT;
slouken@1959
   234
        break;
slouken@1959
   235
    case 1:
slouken@1959
   236
        button = SDL_BUTTON_RIGHT;
slouken@1959
   237
        break;
slouken@1959
   238
    case 2:
slouken@1959
   239
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   240
        break;
slouken@1959
   241
    default:
slouken@5061
   242
        button = [theEvent buttonNumber] + 1;
slouken@1959
   243
        break;
slouken@1959
   244
    }
slouken@4484
   245
    SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
slouken@1933
   246
}
slouken@1933
   247
slouken@1933
   248
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   249
{
slouken@1959
   250
    [self mouseDown:theEvent];
slouken@1933
   251
}
slouken@1933
   252
slouken@1933
   253
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   254
{
slouken@1959
   255
    [self mouseDown:theEvent];
slouken@1933
   256
}
slouken@1933
   257
slouken@1933
   258
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   259
{
slouken@1959
   260
    int button;
slouken@1933
   261
slouken@1959
   262
    switch ([theEvent buttonNumber]) {
slouken@1959
   263
    case 0:
slouken@1959
   264
        button = SDL_BUTTON_LEFT;
slouken@1959
   265
        break;
slouken@1959
   266
    case 1:
slouken@1959
   267
        button = SDL_BUTTON_RIGHT;
slouken@1959
   268
        break;
slouken@1959
   269
    case 2:
slouken@1959
   270
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   271
        break;
slouken@1959
   272
    default:
slouken@5061
   273
        button = [theEvent buttonNumber] + 1;
slouken@1959
   274
        break;
slouken@1959
   275
    }
slouken@4484
   276
    SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
slouken@1933
   277
}
slouken@1933
   278
slouken@1933
   279
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   280
{
slouken@1959
   281
    [self mouseUp:theEvent];
slouken@1933
   282
}
slouken@1933
   283
slouken@1933
   284
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   285
{
slouken@1959
   286
    [self mouseUp:theEvent];
slouken@1933
   287
}
slouken@1933
   288
slouken@5371
   289
- (void)mouseEntered:(NSEvent *)theEvent
slouken@5371
   290
{
slouken@5371
   291
    SDL_SetMouseFocus(_data->window);
slouken@5376
   292
slouken@5388
   293
    SDL_SetCursor(NULL);
slouken@5371
   294
}
slouken@5371
   295
slouken@5371
   296
- (void)mouseExited:(NSEvent *)theEvent
slouken@5371
   297
{
slouken@5371
   298
    SDL_Window *window = _data->window;
slouken@5371
   299
slouken@5371
   300
    if (SDL_GetMouseFocus() == window) {
slouken@5371
   301
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@5371
   302
            int x, y;
slouken@5371
   303
            NSPoint point;
slouken@5371
   304
            CGPoint cgpoint;
slouken@5371
   305
slouken@5371
   306
            point = [theEvent locationInWindow];
slouken@5371
   307
            point.y = window->h - point.y;
slouken@5371
   308
slouken@5371
   309
            SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
slouken@5371
   310
            SDL_GetMouseState(&x, &y);
slouken@5371
   311
            cgpoint.x = window->x + x;
slouken@5371
   312
            cgpoint.y = window->y + y;
slouken@5371
   313
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
   314
        } else {
slouken@5371
   315
            SDL_SetMouseFocus(NULL);
slouken@5388
   316
slouken@5388
   317
            [[NSCursor arrowCursor] set];
slouken@5388
   318
            [NSCursor unhide];
slouken@5371
   319
        }
slouken@5371
   320
    }
slouken@5371
   321
}
slouken@5371
   322
slouken@1933
   323
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   324
{
slouken@5406
   325
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   326
    SDL_Window *window = _data->window;
slouken@5396
   327
    NSPoint point;
slouken@5396
   328
    int x, y;
slouken@1933
   329
slouken@5406
   330
    if (mouse->relative_mode) {
gzjjgod@5059
   331
        return;
slouken@5371
   332
    }
gzjjgod@5059
   333
slouken@5396
   334
    point = [theEvent locationInWindow];
slouken@5396
   335
    x = (int)point.x;
slouken@5396
   336
    y = (int)(window->h - point.y);
slouken@5371
   337
slouken@5396
   338
    if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@5396
   339
        if (SDL_GetMouseFocus() == window) {
slouken@5396
   340
            [self mouseExited:theEvent];
slouken@5396
   341
        }
slouken@5396
   342
    } else {
slouken@5396
   343
        if (SDL_GetMouseFocus() != window) {
slouken@5396
   344
            [self mouseEntered:theEvent];
slouken@5396
   345
        }
slouken@5396
   346
        SDL_SendMouseMotion(window, 0, x, y);
slouken@2059
   347
    }
slouken@1933
   348
}
slouken@1933
   349
slouken@1957
   350
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   351
{
slouken@1957
   352
    [self mouseMoved:theEvent];
slouken@1957
   353
}
slouken@1957
   354
slouken@1958
   355
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   356
{
slouken@1958
   357
    [self mouseMoved:theEvent];
slouken@1958
   358
}
slouken@1958
   359
slouken@1958
   360
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   361
{
slouken@1958
   362
    [self mouseMoved:theEvent];
slouken@1958
   363
}
slouken@1958
   364
slouken@1933
   365
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   366
{
gzjjgod@5057
   367
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   368
}
slouken@3688
   369
slouken@4673
   370
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   371
{
slouken@4673
   372
    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
slouken@4673
   373
}
slouken@4673
   374
slouken@4673
   375
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   376
{
slouken@4673
   377
    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
slouken@4673
   378
}
slouken@4673
   379
slouken@4673
   380
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   381
{
slouken@4673
   382
    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
slouken@4673
   383
}
slouken@4673
   384
slouken@4673
   385
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
   386
{
slouken@4673
   387
    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
slouken@4673
   388
}
slouken@4673
   389
slouken@4673
   390
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
slouken@4673
   391
{
icculus@5646
   392
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
slouken@4680
   393
    NSSet *touches = 0;
slouken@4680
   394
    NSEnumerator *enumerator;
slouken@4680
   395
    NSTouch *touch;
slouken@4673
   396
slouken@4680
   397
    switch (type) {
slouken@4680
   398
        case COCOA_TOUCH_DOWN:
slouken@4680
   399
            touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
slouken@4680
   400
            break;
slouken@4680
   401
        case COCOA_TOUCH_UP:
slouken@4680
   402
        case COCOA_TOUCH_CANCELLED:
slouken@4680
   403
            touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
slouken@4680
   404
            break;
slouken@4680
   405
        case COCOA_TOUCH_MOVE:
slouken@4680
   406
            touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
slouken@4680
   407
            break;
slouken@4680
   408
    }
slouken@4680
   409
slouken@4680
   410
    enumerator = [touches objectEnumerator];
slouken@4680
   411
    touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   412
    while (touch) {
icculus@5873
   413
        const SDL_TouchID touchId = (SDL_TouchID) ((size_t) [touch device]);
slouken@4673
   414
        if (!SDL_GetTouch(touchId)) {
slouken@4673
   415
            SDL_Touch touch;
slouken@4673
   416
slouken@4673
   417
            touch.id = touchId;
slouken@4673
   418
            touch.x_min = 0;
slouken@4673
   419
            touch.x_max = 1;
jimtla@4678
   420
            touch.native_xres = touch.x_max - touch.x_min;
slouken@4673
   421
            touch.y_min = 0;
slouken@4673
   422
            touch.y_max = 1;
jimtla@4678
   423
            touch.native_yres = touch.y_max - touch.y_min;
slouken@4673
   424
            touch.pressure_min = 0;
slouken@4673
   425
            touch.pressure_max = 1;
jimtla@4678
   426
            touch.native_pressureres = touch.pressure_max - touch.pressure_min;
slouken@4673
   427
            
slouken@4673
   428
            if (SDL_AddTouch(&touch, "") < 0) {
slouken@4680
   429
                return;
slouken@4673
   430
            }
slouken@4673
   431
        } 
slouken@4687
   432
icculus@5873
   433
        const SDL_FingerID fingerId = (SDL_FingerID) ((size_t) [touch identity]);
slouken@4673
   434
        float x = [touch normalizedPosition].x;
slouken@4673
   435
        float y = [touch normalizedPosition].y;
slouken@5261
   436
        /* Make the origin the upper left instead of the lower left */
slouken@5261
   437
        y = 1.0f - y;
slouken@4687
   438
slouken@4673
   439
        switch (type) {
slouken@4673
   440
        case COCOA_TOUCH_DOWN:
slouken@4673
   441
            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
slouken@4673
   442
            break;
slouken@4673
   443
        case COCOA_TOUCH_UP:
slouken@4673
   444
        case COCOA_TOUCH_CANCELLED:
slouken@4673
   445
            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   446
            break;
slouken@4673
   447
        case COCOA_TOUCH_MOVE:
slouken@4673
   448
            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   449
            break;
slouken@4673
   450
        }
slouken@4673
   451
        
slouken@4673
   452
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   453
    }
icculus@5646
   454
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 */
slouken@1933
   455
}
slouken@1933
   456
slouken@1933
   457
@end
slouken@1933
   458
slouken@1973
   459
@interface SDLWindow : NSWindow
slouken@1973
   460
/* These are needed for borderless/fullscreen windows */
slouken@1973
   461
- (BOOL)canBecomeKeyWindow;
slouken@1973
   462
- (BOOL)canBecomeMainWindow;
slouken@1973
   463
@end
slouken@1973
   464
slouken@1973
   465
@implementation SDLWindow
slouken@1973
   466
- (BOOL)canBecomeKeyWindow
slouken@1973
   467
{
slouken@1973
   468
    return YES;
slouken@1973
   469
}
slouken@1973
   470
slouken@1973
   471
- (BOOL)canBecomeMainWindow
slouken@1973
   472
{
slouken@1973
   473
    return YES;
slouken@1973
   474
}
slouken@1973
   475
@end
slouken@1973
   476
slouken@5379
   477
@interface SDLView : NSView
slouken@5379
   478
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
   479
- (void)rightMouseDown:(NSEvent *)theEvent;
gzjjgod@4915
   480
@end
gzjjgod@4915
   481
gzjjgod@4915
   482
@implementation SDLView
gzjjgod@4915
   483
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
   484
{
slouken@5371
   485
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
   486
}
gzjjgod@4915
   487
@end
gzjjgod@4915
   488
slouken@5249
   489
static unsigned int
slouken@5305
   490
GetWindowStyle(SDL_Window * window)
slouken@5249
   491
{
slouken@5249
   492
    unsigned int style;
slouken@5249
   493
slouken@5305
   494
	if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5249
   495
        style = NSBorderlessWindowMask;
slouken@5305
   496
	} else {
slouken@5305
   497
		if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@5305
   498
			style = NSBorderlessWindowMask;
slouken@5305
   499
		} else {
slouken@5305
   500
			style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@5305
   501
		}
slouken@5305
   502
		if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@5305
   503
			style |= NSResizableWindowMask;
slouken@5305
   504
		}
slouken@5305
   505
	}
slouken@5249
   506
    return style;
slouken@5249
   507
}
slouken@5249
   508
slouken@1933
   509
static int
slouken@1951
   510
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   511
{
slouken@1933
   512
    NSAutoreleasePool *pool;
slouken@1951
   513
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
   514
    SDL_WindowData *data;
slouken@1933
   515
slouken@1933
   516
    /* Allocate the window data */
slouken@5249
   517
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
   518
    if (!data) {
slouken@1933
   519
        SDL_OutOfMemory();
slouken@1933
   520
        return -1;
slouken@1933
   521
    }
slouken@3685
   522
    data->window = window;
slouken@3688
   523
    data->nswindow = nswindow;
slouken@1933
   524
    data->created = created;
slouken@1951
   525
    data->videodata = videodata;
slouken@1933
   526
slouken@1933
   527
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   528
slouken@1933
   529
    /* Create an event listener for the window */
slouken@1933
   530
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@1933
   531
slouken@1933
   532
    /* Fill in the SDL window with the window data */
slouken@1933
   533
    {
slouken@1933
   534
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6185
   535
        NSView *contentView = [ nswindow contentView ];
slouken@6185
   536
        /* Create view if not already exists */
slouken@6185
   537
        if (!contentView) {
slouken@6185
   538
            contentView = [[SDLView alloc] initWithFrame:rect];
slouken@6185
   539
            [nswindow setContentView: contentView];
slouken@6185
   540
            [contentView release];
slouken@6185
   541
        }
gzjjgod@4915
   542
slouken@1933
   543
        ConvertNSRect(&rect);
slouken@5261
   544
        window->x = (int)rect.origin.x;
slouken@5261
   545
        window->y = (int)rect.origin.y;
slouken@5261
   546
        window->w = (int)rect.size.width;
slouken@5261
   547
        window->h = (int)rect.size.height;
slouken@1933
   548
    }
slouken@5371
   549
slouken@5371
   550
    /* Set up the listener after we create the view */
slouken@5371
   551
    [data->listener listen:data];
slouken@5371
   552
slouken@1933
   553
    if ([nswindow isVisible]) {
slouken@1933
   554
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   555
    } else {
slouken@1933
   556
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   557
    }
slouken@1933
   558
    {
slouken@1933
   559
        unsigned int style = [nswindow styleMask];
slouken@1933
   560
slouken@5380
   561
        if (style == NSBorderlessWindowMask) {
slouken@1933
   562
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   563
        } else {
slouken@1933
   564
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   565
        }
slouken@1933
   566
        if (style & NSResizableWindowMask) {
slouken@1933
   567
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   568
        } else {
slouken@1933
   569
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   570
        }
slouken@1933
   571
    }
slouken@5380
   572
    /* isZoomed always returns true if the window is not resizable */
slouken@5380
   573
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@1933
   574
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   575
    } else {
slouken@1933
   576
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   577
    }
slouken@1933
   578
    if ([nswindow isMiniaturized]) {
slouken@1933
   579
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   580
    } else {
slouken@1933
   581
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   582
    }
slouken@1933
   583
    if ([nswindow isKeyWindow]) {
slouken@1933
   584
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   585
        SDL_SetKeyboardFocus(data->window);
slouken@1933
   586
    }
slouken@1933
   587
slouken@1933
   588
    /* All done! */
slouken@1933
   589
    [pool release];
slouken@1933
   590
    window->driverdata = data;
slouken@1933
   591
    return 0;
slouken@1933
   592
}
slouken@1933
   593
slouken@1933
   594
int
slouken@1933
   595
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   596
{
slouken@3506
   597
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   598
    NSWindow *nswindow;
slouken@5246
   599
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1933
   600
    NSRect rect;
slouken@3528
   601
    SDL_Rect bounds;
slouken@1933
   602
    unsigned int style;
slouken@1933
   603
slouken@3528
   604
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@5380
   605
    rect.origin.x = window->x;
slouken@5380
   606
    rect.origin.y = window->y;
slouken@1933
   607
    rect.size.width = window->w;
slouken@1933
   608
    rect.size.height = window->h;
slouken@1933
   609
    ConvertNSRect(&rect);
slouken@1933
   610
slouken@5305
   611
    style = GetWindowStyle(window);
slouken@1933
   612
slouken@3506
   613
    /* Figure out which screen to place this window */
slouken@3506
   614
    NSArray *screens = [NSScreen screens];
slouken@3506
   615
    NSScreen *screen = nil;
slouken@3506
   616
    NSScreen *candidate;
slouken@3525
   617
    int i, count = [screens count];
slouken@3525
   618
    for (i = 0; i < count; ++i) {
slouken@3529
   619
        candidate = [screens objectAtIndex:i];
slouken@3506
   620
        NSRect screenRect = [candidate frame];
slouken@3506
   621
        if (rect.origin.x >= screenRect.origin.x &&
slouken@3506
   622
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@3506
   623
            rect.origin.y >= screenRect.origin.y &&
slouken@3506
   624
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@3506
   625
            screen = candidate;
slouken@3506
   626
            rect.origin.x -= screenRect.origin.x;
slouken@3506
   627
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   628
        }
slouken@3506
   629
    }
slouken@5379
   630
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:YES screen:screen];
slouken@1933
   631
slouken@1933
   632
    [pool release];
slouken@1933
   633
slouken@1951
   634
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@1933
   635
        [nswindow release];
slouken@1933
   636
        return -1;
slouken@1933
   637
    }
slouken@1933
   638
    return 0;
slouken@1933
   639
}
slouken@1933
   640
slouken@1933
   641
int
slouken@1933
   642
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   643
{
slouken@1933
   644
    NSAutoreleasePool *pool;
slouken@1933
   645
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   646
    NSString *title;
slouken@1933
   647
slouken@1933
   648
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   649
slouken@1933
   650
    /* Query the title from the existing window */
slouken@1933
   651
    title = [nswindow title];
slouken@1933
   652
    if (title) {
slouken@1933
   653
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   654
    }
slouken@1933
   655
slouken@1933
   656
    [pool release];
slouken@1933
   657
slouken@1951
   658
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   659
}
slouken@1933
   660
slouken@1933
   661
void
slouken@1933
   662
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   663
{
slouken@1937
   664
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   665
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   666
    NSString *string;
slouken@1933
   667
slouken@1956
   668
    if(window->title) {
slouken@1956
   669
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1956
   670
    } else {
slouken@1956
   671
        string = [[NSString alloc] init];
slouken@1956
   672
    }
slouken@1933
   673
    [nswindow setTitle:string];
slouken@1933
   674
    [string release];
slouken@1956
   675
slouken@1937
   676
    [pool release];
slouken@1933
   677
}
slouken@1933
   678
slouken@1933
   679
void
slouken@5375
   680
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@5375
   681
{
slouken@5375
   682
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5375
   683
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
   684
slouken@5375
   685
    if (nsimage) {
slouken@5375
   686
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
   687
    }
slouken@5375
   688
slouken@5375
   689
    [pool release];
slouken@5375
   690
}
slouken@5375
   691
slouken@5375
   692
void
slouken@1933
   693
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   694
{
slouken@1937
   695
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   696
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   697
    NSRect rect;
slouken@5478
   698
    Uint32 moveHack;
slouken@1933
   699
slouken@5478
   700
    rect.origin.x = window->x;
slouken@5478
   701
    rect.origin.y = window->y;
slouken@1933
   702
    rect.size.width = window->w;
slouken@1933
   703
    rect.size.height = window->h;
slouken@1933
   704
    ConvertNSRect(&rect);
slouken@5478
   705
slouken@5478
   706
    moveHack = s_moveHack;
slouken@5478
   707
    s_moveHack = 0;
slouken@1933
   708
    [nswindow setFrameOrigin:rect.origin];
slouken@5478
   709
    s_moveHack = moveHack;
slouken@5478
   710
icculus@5564
   711
    if (window == _this->current_glwin) {
icculus@5564
   712
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   713
    }
icculus@5564
   714
slouken@1937
   715
    [pool release];
slouken@1933
   716
}
slouken@1933
   717
slouken@1933
   718
void
slouken@1933
   719
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   720
{
slouken@1937
   721
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
icculus@5564
   722
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
icculus@5564
   723
    NSWindow *nswindow = windata->nswindow;
slouken@1933
   724
    NSSize size;
slouken@1933
   725
slouken@1933
   726
    size.width = window->w;
slouken@1933
   727
    size.height = window->h;
slouken@1933
   728
    [nswindow setContentSize:size];
icculus@5564
   729
icculus@5564
   730
    if (window == _this->current_glwin) {
icculus@5564
   731
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   732
    }
icculus@5564
   733
slouken@1937
   734
    [pool release];
slouken@1933
   735
}
slouken@1933
   736
slouken@1933
   737
void
slouken@1933
   738
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   739
{
slouken@1937
   740
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   741
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   742
slouken@1956
   743
    if (![nswindow isMiniaturized]) {
slouken@1956
   744
        [nswindow makeKeyAndOrderFront:nil];
slouken@1956
   745
    }
slouken@1937
   746
    [pool release];
slouken@1933
   747
}
slouken@1933
   748
slouken@1933
   749
void
slouken@1933
   750
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   751
{
slouken@1956
   752
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   753
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   754
slouken@1956
   755
    [nswindow orderOut:nil];
slouken@1956
   756
    [pool release];
slouken@1933
   757
}
slouken@1933
   758
slouken@1933
   759
void
slouken@1933
   760
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   761
{
slouken@1937
   762
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   763
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   764
slouken@1933
   765
    [nswindow makeKeyAndOrderFront:nil];
slouken@1937
   766
    [pool release];
slouken@1933
   767
}
slouken@1933
   768
slouken@1933
   769
void
slouken@1933
   770
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   771
{
slouken@1937
   772
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   773
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   774
slouken@1956
   775
    [nswindow zoom:nil];
icculus@5564
   776
icculus@5564
   777
    if (window == _this->current_glwin) {
icculus@5564
   778
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   779
    }
icculus@5564
   780
slouken@1937
   781
    [pool release];
slouken@1933
   782
}
slouken@1933
   783
slouken@1933
   784
void
slouken@1933
   785
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   786
{
slouken@1937
   787
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   788
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   789
slouken@1956
   790
    [nswindow miniaturize:nil];
slouken@1937
   791
    [pool release];
slouken@1933
   792
}
slouken@1933
   793
slouken@1933
   794
void
slouken@1933
   795
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   796
{
slouken@1956
   797
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   798
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   799
slouken@1956
   800
    if ([nswindow isMiniaturized]) {
slouken@1956
   801
        [nswindow deminiaturize:nil];
slouken@5380
   802
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@1956
   803
        [nswindow zoom:nil];
slouken@1956
   804
    }
slouken@1956
   805
    [pool release];
slouken@1933
   806
}
slouken@1933
   807
slouken@5400
   808
static NSWindow *
slouken@5400
   809
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
slouken@5400
   810
{
slouken@5400
   811
    if (!data->created) {
slouken@5400
   812
        /* Don't mess with other people's windows... */
slouken@5400
   813
        return nswindow;
slouken@5400
   814
    }
slouken@5400
   815
slouken@5400
   816
    [data->listener close];
slouken@5400
   817
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:YES screen:[nswindow screen]];
slouken@5400
   818
    [data->nswindow setContentView:[nswindow contentView]];
slouken@5400
   819
    [data->listener listen:data];
slouken@5400
   820
slouken@5400
   821
    [nswindow close];
slouken@5400
   822
slouken@5400
   823
    return data->nswindow;
slouken@5400
   824
}
slouken@5400
   825
slouken@1933
   826
void
slouken@5305
   827
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@5249
   828
{
slouken@5249
   829
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5249
   830
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5249
   831
    NSWindow *nswindow = data->nswindow;
slouken@5249
   832
    NSRect rect;
slouken@5249
   833
slouken@5502
   834
    /* The view responder chain gets messed with during setStyleMask */
slouken@5502
   835
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@5502
   836
        [[nswindow contentView] setNextResponder:nil];
slouken@5502
   837
    }
slouken@5502
   838
slouken@5305
   839
    if (fullscreen) {
slouken@5249
   840
        SDL_Rect bounds;
slouken@5249
   841
slouken@5249
   842
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@5249
   843
        rect.origin.x = bounds.x;
slouken@5249
   844
        rect.origin.y = bounds.y;
slouken@5249
   845
        rect.size.width = bounds.w;
slouken@5249
   846
        rect.size.height = bounds.h;
slouken@5249
   847
        ConvertNSRect(&rect);
slouken@5249
   848
slouken@5401
   849
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@5401
   850
        NSRect screenRect = [[nswindow screen] frame];
slouken@5401
   851
        if (screenRect.size.height >= 1.0f) {
slouken@5401
   852
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
   853
        }
slouken@5401
   854
slouken@5361
   855
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@5361
   856
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@5400
   857
        } else {
slouken@5400
   858
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
slouken@5361
   859
        }
slouken@5249
   860
    } else {
slouken@5398
   861
        rect.origin.x = window->windowed.x;
slouken@5398
   862
        rect.origin.y = window->windowed.y;
slouken@5398
   863
        rect.size.width = window->windowed.w;
slouken@5398
   864
        rect.size.height = window->windowed.h;
slouken@5398
   865
        ConvertNSRect(&rect);
slouken@5398
   866
slouken@5361
   867
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@5378
   868
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
slouken@5400
   869
        } else {
slouken@5400
   870
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
slouken@5361
   871
        }
slouken@5398
   872
    }
slouken@5249
   873
slouken@5502
   874
    /* The view responder chain gets messed with during setStyleMask */
slouken@5502
   875
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@5502
   876
        [[nswindow contentView] setNextResponder:data->listener];
slouken@5502
   877
    }
slouken@5502
   878
slouken@5398
   879
    s_moveHack = 0;
slouken@5398
   880
    [nswindow setFrameOrigin:rect.origin];
slouken@5398
   881
    [nswindow setContentSize:rect.size];
slouken@5398
   882
    s_moveHack = SDL_GetTicks();
slouken@5249
   883
slouken@5399
   884
    /* When the window style changes the title is cleared */
slouken@5399
   885
    if (!fullscreen) {
slouken@5399
   886
        Cocoa_SetWindowTitle(_this, window);
slouken@5399
   887
    }
slouken@5399
   888
slouken@5249
   889
#ifdef FULLSCREEN_TOGGLEABLE
slouken@5305
   890
    if (fullscreen) {
slouken@5249
   891
        /* OpenGL is rendering to the window, so make it visible! */
slouken@5249
   892
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@5249
   893
    } else {
slouken@5249
   894
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@5249
   895
    }
slouken@5249
   896
#endif
slouken@5249
   897
    [nswindow makeKeyAndOrderFront:nil];
slouken@5249
   898
icculus@5564
   899
    if (window == _this->current_glwin) {
icculus@5564
   900
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   901
    }
icculus@5564
   902
slouken@5249
   903
    [pool release];
slouken@5249
   904
}
slouken@5249
   905
slouken@5466
   906
int
slouken@5466
   907
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
   908
{
slouken@5466
   909
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
   910
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
   911
    const uint32_t tableSize = 256;
slouken@5466
   912
    CGGammaValue redTable[tableSize];
slouken@5466
   913
    CGGammaValue greenTable[tableSize];
slouken@5466
   914
    CGGammaValue blueTable[tableSize];
slouken@5466
   915
    uint32_t i;
slouken@5466
   916
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
   917
slouken@5466
   918
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
   919
    for (i = 0; i < 256; i++) {
slouken@5466
   920
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
   921
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
   922
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
   923
    }
slouken@5466
   924
slouken@5466
   925
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
   926
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
slouken@5466
   927
        SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
   928
        return -1;
slouken@5466
   929
    }
slouken@5466
   930
    return 0;
slouken@5466
   931
}
slouken@5466
   932
slouken@5466
   933
int
slouken@5466
   934
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
   935
{
slouken@5466
   936
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
   937
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
   938
    const uint32_t tableSize = 256;
slouken@5466
   939
    CGGammaValue redTable[tableSize];
slouken@5466
   940
    CGGammaValue greenTable[tableSize];
slouken@5466
   941
    CGGammaValue blueTable[tableSize];
slouken@5466
   942
    uint32_t i, tableCopied;
slouken@5466
   943
slouken@5466
   944
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
   945
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
slouken@5466
   946
        SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
   947
        return -1;
slouken@5466
   948
    }
slouken@5466
   949
slouken@5466
   950
    for (i = 0; i < tableCopied; i++) {
slouken@5466
   951
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
   952
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
   953
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
   954
    }
slouken@5466
   955
    return 0;
slouken@5466
   956
}
slouken@5466
   957
slouken@5249
   958
void
slouken@1933
   959
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   960
{
slouken@5371
   961
    /* Move the cursor to the nearest point in the window */
slouken@5371
   962
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@5371
   963
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@5371
   964
        int x, y;
slouken@5371
   965
        CGPoint cgpoint;
slouken@5371
   966
slouken@5371
   967
        SDL_GetMouseState(&x, &y);
slouken@5371
   968
        cgpoint.x = window->x + x;
slouken@5371
   969
        cgpoint.y = window->y + y;
slouken@5371
   970
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
   971
    }
slouken@1933
   972
}
slouken@1933
   973
slouken@1933
   974
void
slouken@1933
   975
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   976
{
slouken@1937
   977
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   978
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   979
slouken@1933
   980
    if (data) {
slouken@1933
   981
        [data->listener close];
slouken@1933
   982
        [data->listener release];
slouken@1933
   983
        if (data->created) {
slouken@3688
   984
            [data->nswindow close];
slouken@1933
   985
        }
slouken@1933
   986
        SDL_free(data);
slouken@1933
   987
    }
slouken@1937
   988
    [pool release];
slouken@1933
   989
}
slouken@1933
   990
slouken@1933
   991
SDL_bool
slouken@1933
   992
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   993
{
slouken@4900
   994
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   995
slouken@1933
   996
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
   997
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
   998
        info->info.cocoa.window = nswindow;
slouken@1933
   999
        return SDL_TRUE;
slouken@1933
  1000
    } else {
slouken@1933
  1001
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1002
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1003
        return SDL_FALSE;
slouken@1933
  1004
    }
slouken@1933
  1005
}
slouken@1933
  1006
slouken@6044
  1007
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1008
slouken@1933
  1009
/* vi: set ts=4 sw=4 expandtab: */