src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 08 Jan 2012 00:39:41 -0500
changeset 6185 b91e7565e877
parent 6138 4c64952a58fb
child 6231 5eecf59b698f
permissions -rwxr-xr-x
Fixed bug 1303 - SDL_CreateFromWindow duplicates window (Cocoa only)

Jens Köhler 2011-09-09 04:47:40 PDT

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