src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Mon, 22 Jul 2013 20:55:07 -0400
changeset 7534 f0f5143b27cf
parent 7507 885b4aab4190
child 7535 c515121a3df7
permissions -rw-r--r--
Cocoa: Make the next-highest window gain focus when a window is closing.

(if the closed window wasn't the foreground, this is effectively a no-op.)
slouken@1933
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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;
jorgen@7087
    53
    observingVisible = YES;
jorgen@7087
    54
    wasVisible = [window isVisible];
slouken@1933
    55
slouken@1933
    56
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    57
slouken@5374
    58
    if ([window delegate] != nil) {
slouken@5374
    59
        [center addObserver:self selector:@selector(windowDidExpose:) name:NSWindowDidExposeNotification object:window];
slouken@5374
    60
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
slouken@5374
    61
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
slouken@5374
    62
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
slouken@5374
    63
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5374
    64
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
slouken@5374
    65
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
slouken@5374
    66
    } else {
slouken@5374
    67
        [window setDelegate:self];
slouken@5374
    68
    }
slouken@1933
    69
slouken@7191
    70
    /* Haven't found a delegate / notification that triggers when the window is
slouken@7191
    71
     * ordered out (is not visible any more). You can be ordered out without
slouken@7191
    72
     * minimizing, so DidMiniaturize doesn't work. (e.g. -[NSWindow orderOut:])
slouken@7191
    73
     */
jorgen@7084
    74
    [window addObserver:self
jorgen@7084
    75
             forKeyPath:@"visible"
jorgen@7084
    76
                options:NSKeyValueObservingOptionNew
jorgen@7084
    77
                context:NULL];
jorgen@7084
    78
slouken@5371
    79
    [window setNextResponder:self];
slouken@5371
    80
    [window setAcceptsMouseMovedEvents:YES];
slouken@5371
    81
slouken@5371
    82
    [view setNextResponder:self];
icculus@6108
    83
icculus@6108
    84
    if ([view respondsToSelector:@selector(setAcceptsTouchEvents:)]) {
icculus@6108
    85
        [view setAcceptsTouchEvents:YES];
icculus@6108
    86
    }
slouken@1933
    87
}
slouken@1933
    88
jorgen@7084
    89
- (void)observeValueForKeyPath:(NSString *)keyPath
jorgen@7084
    90
                      ofObject:(id)object
jorgen@7084
    91
                        change:(NSDictionary *)change
jorgen@7084
    92
                       context:(void *)context
jorgen@7084
    93
{
jorgen@7087
    94
    if (!observingVisible) {
jorgen@7087
    95
        return;
jorgen@7087
    96
    }
jorgen@7087
    97
jorgen@7084
    98
    if (object == _data->nswindow && [keyPath isEqualToString:@"visible"]) {
jorgen@7084
    99
        int newVisibility = [[change objectForKey:@"new"] intValue];
jorgen@7084
   100
        if (newVisibility) {
jorgen@7084
   101
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
jorgen@7084
   102
        } else {
jorgen@7084
   103
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
jorgen@7084
   104
        }
jorgen@7084
   105
    }
jorgen@7084
   106
}
jorgen@7084
   107
jorgen@7087
   108
-(void) pauseVisibleObservation
jorgen@7087
   109
{
jorgen@7087
   110
    observingVisible = NO;
jorgen@7087
   111
    wasVisible = [_data->nswindow isVisible];
jorgen@7087
   112
}
jorgen@7087
   113
jorgen@7087
   114
-(void) resumeVisibleObservation
jorgen@7087
   115
{
jorgen@7087
   116
    BOOL isVisible = [_data->nswindow isVisible];
jorgen@7087
   117
    observingVisible = YES;
jorgen@7087
   118
    if (wasVisible != isVisible) {
jorgen@7087
   119
        if (isVisible) {
jorgen@7087
   120
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
jorgen@7087
   121
        } else {
jorgen@7087
   122
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
jorgen@7087
   123
        }
jorgen@7087
   124
jorgen@7087
   125
        wasVisible = isVisible;
jorgen@7087
   126
    }
jorgen@7087
   127
}
jorgen@7087
   128
slouken@1933
   129
- (void)close
slouken@1933
   130
{
slouken@1933
   131
    NSNotificationCenter *center;
slouken@5371
   132
    NSWindow *window = _data->nswindow;
slouken@5371
   133
    NSView *view = [window contentView];
icculus@7534
   134
    NSArray *windows = nil;
slouken@1933
   135
slouken@1933
   136
    center = [NSNotificationCenter defaultCenter];
slouken@1933
   137
slouken@5374
   138
    if ([window delegate] != self) {
slouken@5374
   139
        [center removeObserver:self name:NSWindowDidExposeNotification object:window];
slouken@5374
   140
        [center removeObserver:self name:NSWindowDidMoveNotification object:window];
slouken@5374
   141
        [center removeObserver:self name:NSWindowDidResizeNotification object:window];
slouken@5374
   142
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
slouken@5374
   143
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5374
   144
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
slouken@5374
   145
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
slouken@5374
   146
    } else {
slouken@5374
   147
        [window setDelegate:nil];
slouken@5374
   148
    }
slouken@5371
   149
jorgen@7084
   150
    [window removeObserver:self
jorgen@7084
   151
                forKeyPath:@"visible"];
jorgen@7084
   152
slouken@5374
   153
    if ([window nextResponder] == self) {
slouken@5374
   154
        [window setNextResponder:nil];
slouken@5374
   155
    }
slouken@5374
   156
    if ([view nextResponder] == self) {
slouken@5374
   157
        [view setNextResponder:nil];
slouken@5374
   158
    }
icculus@7534
   159
icculus@7534
   160
    /* Make the next window in the z-order Key. If we weren't the foreground
icculus@7534
   161
       when closed, this is a no-op. */
icculus@7534
   162
    windows = [NSApp orderedWindows];
icculus@7534
   163
    if ([windows count] > 0) {
icculus@7534
   164
        NSWindow *win = (NSWindow *) [windows objectAtIndex:0];
icculus@7534
   165
        [win makeKeyAndOrderFront:self];
icculus@7534
   166
    }
slouken@1933
   167
}
slouken@1933
   168
slouken@1933
   169
- (BOOL)windowShouldClose:(id)sender
slouken@1933
   170
{
slouken@3685
   171
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
   172
    return NO;
slouken@1933
   173
}
slouken@1933
   174
slouken@1933
   175
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
   176
{
slouken@3685
   177
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
   178
}
slouken@1933
   179
slouken@1933
   180
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
   181
{
slouken@1933
   182
    int x, y;
icculus@5564
   183
    SDL_VideoDevice *device = SDL_GetVideoDevice();
slouken@5398
   184
    SDL_Window *window = _data->window;
slouken@5398
   185
    NSWindow *nswindow = _data->nswindow;
slouken@5398
   186
    NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   187
    ConvertNSRect(&rect);
slouken@5398
   188
slouken@5398
   189
    if (s_moveHack) {
slouken@5398
   190
        SDL_bool blockMove = ((SDL_GetTicks() - s_moveHack) < 500);
slouken@5398
   191
slouken@5398
   192
        s_moveHack = 0;
slouken@5398
   193
slouken@5398
   194
        if (blockMove) {
slouken@5398
   195
            /* Cocoa is adjusting the window in response to a mode change */
slouken@5398
   196
            rect.origin.x = window->x;
slouken@5398
   197
            rect.origin.y = window->y;
slouken@5398
   198
            ConvertNSRect(&rect);
slouken@5398
   199
            [nswindow setFrameOrigin:rect.origin];
slouken@5398
   200
            return;
slouken@5398
   201
        }
slouken@5398
   202
    }
slouken@5398
   203
slouken@3507
   204
    x = (int)rect.origin.x;
slouken@3507
   205
    y = (int)rect.origin.y;
icculus@5564
   206
icculus@5564
   207
    if (window == device->current_glwin) {
icculus@5564
   208
        [((NSOpenGLContext *) device->current_glctx) update];
icculus@5564
   209
    }
icculus@5564
   210
slouken@5398
   211
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   212
}
slouken@1933
   213
slouken@1933
   214
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   215
{
icculus@5564
   216
    SDL_VideoDevice *device = SDL_GetVideoDevice();
slouken@6231
   217
    int x, y, w, h;
slouken@3688
   218
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
slouken@6231
   219
    ConvertNSRect(&rect);
slouken@6231
   220
    x = (int)rect.origin.x;
slouken@6231
   221
    y = (int)rect.origin.y;
slouken@1933
   222
    w = (int)rect.size.width;
slouken@1933
   223
    h = (int)rect.size.height;
slouken@4937
   224
    if (SDL_IsShapedWindow(_data->window))
slouken@4937
   225
        Cocoa_ResizeWindowShape(_data->window);
icculus@5564
   226
icculus@5564
   227
    if (_data->window == device->current_glwin) {
icculus@5564
   228
        [((NSOpenGLContext *) device->current_glctx) update];
icculus@5564
   229
    }
icculus@5564
   230
slouken@6231
   231
    /* The window can move during a resize event, such as when maximizing
slouken@6231
   232
       or resizing from a corner */
slouken@6231
   233
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@3685
   234
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@1933
   235
}
slouken@1933
   236
slouken@1933
   237
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   238
{
slouken@3685
   239
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   240
}
slouken@1933
   241
slouken@1933
   242
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   243
{
slouken@3685
   244
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   245
}
slouken@1933
   246
slouken@1933
   247
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   248
{
slouken@5367
   249
    SDL_Window *window = _data->window;
jorgen@7271
   250
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5367
   251
slouken@3688
   252
    /* We're going to get keyboard events, since we're key. */
slouken@5367
   253
    SDL_SetKeyboardFocus(window);
slouken@5367
   254
slouken@5367
   255
    /* If we just gained focus we need the updated mouse position */
jorgen@7271
   256
    if (!mouse->relative_mode) {
slouken@5367
   257
        NSPoint point;
slouken@5396
   258
        int x, y;
slouken@5396
   259
slouken@5396
   260
        point = [_data->nswindow mouseLocationOutsideOfEventStream];
slouken@5396
   261
        x = (int)point.x;
slouken@5396
   262
        y = (int)(window->h - point.y);
slouken@5396
   263
slouken@5396
   264
        if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
slouken@6950
   265
            SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@5396
   266
        }
slouken@5367
   267
    }
slouken@1962
   268
slouken@4503
   269
    /* Check to see if someone updated the clipboard */
slouken@4503
   270
    Cocoa_CheckClipboardUpdate(_data->videodata);
slouken@1933
   271
}
slouken@1933
   272
slouken@1933
   273
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   274
{
slouken@2059
   275
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   276
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   277
        SDL_SetMouseFocus(NULL);
slouken@2059
   278
    }
slouken@2059
   279
slouken@2059
   280
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   281
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   282
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   283
    }
slouken@1933
   284
}
slouken@1933
   285
slouken@7191
   286
/* We'll respond to key events by doing nothing so we don't beep.
slouken@7191
   287
 * We could handle key messages here, but we lose some in the NSApp dispatch,
slouken@7191
   288
 * where they get converted to action messages, etc.
slouken@7191
   289
 */
slouken@6514
   290
- (void)flagsChanged:(NSEvent *)theEvent
slouken@6514
   291
{
slouken@7191
   292
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   293
}
slouken@6514
   294
- (void)keyDown:(NSEvent *)theEvent
slouken@6514
   295
{
slouken@7191
   296
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   297
}
slouken@6514
   298
- (void)keyUp:(NSEvent *)theEvent
slouken@6514
   299
{
slouken@7191
   300
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   301
}
slouken@6514
   302
slouken@7191
   303
/* We'll respond to selectors by doing nothing so we don't beep.
slouken@7191
   304
 * The escape key gets converted to a "cancel" selector, etc.
slouken@7191
   305
 */
slouken@6514
   306
- (void)doCommandBySelector:(SEL)aSelector
slouken@6514
   307
{
slouken@7191
   308
    /*NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));*/
slouken@6514
   309
}
slouken@6514
   310
slouken@1933
   311
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   312
{
slouken@1959
   313
    int button;
slouken@1933
   314
slouken@1959
   315
    switch ([theEvent buttonNumber]) {
slouken@1959
   316
    case 0:
slouken@1959
   317
        button = SDL_BUTTON_LEFT;
slouken@1959
   318
        break;
slouken@1959
   319
    case 1:
slouken@1959
   320
        button = SDL_BUTTON_RIGHT;
slouken@1959
   321
        break;
slouken@1959
   322
    case 2:
slouken@1959
   323
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   324
        break;
slouken@1959
   325
    default:
slouken@5061
   326
        button = [theEvent buttonNumber] + 1;
slouken@1959
   327
        break;
slouken@1959
   328
    }
slouken@6950
   329
    SDL_SendMouseButton(_data->window, 0, SDL_PRESSED, button);
slouken@1933
   330
}
slouken@1933
   331
slouken@1933
   332
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   333
{
slouken@1959
   334
    [self mouseDown:theEvent];
slouken@1933
   335
}
slouken@1933
   336
slouken@1933
   337
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   338
{
slouken@1959
   339
    [self mouseDown:theEvent];
slouken@1933
   340
}
slouken@1933
   341
slouken@1933
   342
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   343
{
slouken@1959
   344
    int button;
slouken@1933
   345
slouken@1959
   346
    switch ([theEvent buttonNumber]) {
slouken@1959
   347
    case 0:
slouken@1959
   348
        button = SDL_BUTTON_LEFT;
slouken@1959
   349
        break;
slouken@1959
   350
    case 1:
slouken@1959
   351
        button = SDL_BUTTON_RIGHT;
slouken@1959
   352
        break;
slouken@1959
   353
    case 2:
slouken@1959
   354
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   355
        break;
slouken@1959
   356
    default:
slouken@5061
   357
        button = [theEvent buttonNumber] + 1;
slouken@1959
   358
        break;
slouken@1959
   359
    }
slouken@6950
   360
    SDL_SendMouseButton(_data->window, 0, SDL_RELEASED, button);
slouken@1933
   361
}
slouken@1933
   362
slouken@1933
   363
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   364
{
slouken@1959
   365
    [self mouseUp:theEvent];
slouken@1933
   366
}
slouken@1933
   367
slouken@1933
   368
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   369
{
slouken@1959
   370
    [self mouseUp:theEvent];
slouken@1933
   371
}
slouken@1933
   372
slouken@1933
   373
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   374
{
slouken@5406
   375
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   376
    SDL_Window *window = _data->window;
slouken@5396
   377
    NSPoint point;
slouken@5396
   378
    int x, y;
slouken@1933
   379
slouken@5406
   380
    if (mouse->relative_mode) {
gzjjgod@5059
   381
        return;
slouken@5371
   382
    }
gzjjgod@5059
   383
slouken@5396
   384
    point = [theEvent locationInWindow];
slouken@5396
   385
    x = (int)point.x;
slouken@5396
   386
    y = (int)(window->h - point.y);
slouken@5371
   387
slouken@5396
   388
    if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   389
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@6666
   390
            CGPoint cgpoint;
slouken@6666
   391
slouken@6666
   392
            if (x < 0) {
slouken@6666
   393
                x = 0;
slouken@6666
   394
            } else if (x >= window->w) {
slouken@6666
   395
                x = window->w - 1;
slouken@6666
   396
            }
slouken@6666
   397
            if (y < 0) {
slouken@6666
   398
                y = 0;
slouken@6666
   399
            } else if (y >= window->h) {
slouken@6666
   400
                y = window->h - 1;
slouken@6666
   401
            }
slouken@6666
   402
slouken@6666
   403
            cgpoint.x = window->x + x;
slouken@6666
   404
            cgpoint.y = window->y + y;
jorgen@7098
   405
jorgen@7113
   406
            /* According to the docs, this was deprecated in 10.6, but it's still
jorgen@7113
   407
             * around. The substitute requires a CGEventSource, but I'm not entirely
jorgen@7113
   408
             * sure how we'd procure the right one for this event.
jorgen@7098
   409
             */
jorgen@7113
   410
            CGSetLocalEventsSuppressionInterval(0.0);
slouken@6666
   411
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
jorgen@7113
   412
            CGSetLocalEventsSuppressionInterval(0.25);
slouken@5396
   413
        }
slouken@2059
   414
    }
slouken@6950
   415
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
   416
}
slouken@1933
   417
slouken@1957
   418
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   419
{
slouken@1957
   420
    [self mouseMoved:theEvent];
slouken@1957
   421
}
slouken@1957
   422
slouken@1958
   423
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   424
{
slouken@1958
   425
    [self mouseMoved:theEvent];
slouken@1958
   426
}
slouken@1958
   427
slouken@1958
   428
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   429
{
slouken@1958
   430
    [self mouseMoved:theEvent];
slouken@1958
   431
}
slouken@1958
   432
slouken@1933
   433
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   434
{
gzjjgod@5057
   435
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   436
}
slouken@3688
   437
slouken@4673
   438
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   439
{
slouken@4673
   440
    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
slouken@4673
   441
}
slouken@4673
   442
slouken@4673
   443
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   444
{
slouken@4673
   445
    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
slouken@4673
   446
}
slouken@4673
   447
slouken@4673
   448
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   449
{
slouken@4673
   450
    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
slouken@4673
   451
}
slouken@4673
   452
slouken@4673
   453
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
   454
{
slouken@4673
   455
    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
slouken@4673
   456
}
slouken@4673
   457
slouken@4673
   458
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
slouken@4673
   459
{
slouken@4680
   460
    NSSet *touches = 0;
slouken@4680
   461
    NSEnumerator *enumerator;
slouken@4680
   462
    NSTouch *touch;
slouken@4673
   463
slouken@4680
   464
    switch (type) {
slouken@4680
   465
        case COCOA_TOUCH_DOWN:
slouken@4680
   466
            touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
slouken@4680
   467
            break;
slouken@4680
   468
        case COCOA_TOUCH_UP:
slouken@4680
   469
        case COCOA_TOUCH_CANCELLED:
slouken@4680
   470
            touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
slouken@4680
   471
            break;
slouken@4680
   472
        case COCOA_TOUCH_MOVE:
slouken@4680
   473
            touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
slouken@4680
   474
            break;
slouken@4680
   475
    }
slouken@4680
   476
slouken@4680
   477
    enumerator = [touches objectEnumerator];
slouken@4680
   478
    touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   479
    while (touch) {
slouken@6953
   480
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@4673
   481
        if (!SDL_GetTouch(touchId)) {
slouken@6951
   482
            if (SDL_AddTouch(touchId, "") < 0) {
slouken@4680
   483
                return;
slouken@4673
   484
            }
slouken@7191
   485
        }
slouken@4687
   486
slouken@6953
   487
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
   488
        float x = [touch normalizedPosition].x;
slouken@4673
   489
        float y = [touch normalizedPosition].y;
slouken@5261
   490
        /* Make the origin the upper left instead of the lower left */
slouken@5261
   491
        y = 1.0f - y;
slouken@4687
   492
slouken@4673
   493
        switch (type) {
slouken@4673
   494
        case COCOA_TOUCH_DOWN:
slouken@6951
   495
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
   496
            break;
slouken@4673
   497
        case COCOA_TOUCH_UP:
slouken@4673
   498
        case COCOA_TOUCH_CANCELLED:
slouken@6951
   499
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
   500
            break;
slouken@4673
   501
        case COCOA_TOUCH_MOVE:
slouken@6951
   502
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
   503
            break;
slouken@4673
   504
        }
slouken@6951
   505
slouken@4673
   506
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   507
    }
slouken@1933
   508
}
slouken@1933
   509
slouken@1933
   510
@end
slouken@1933
   511
slouken@1973
   512
@interface SDLWindow : NSWindow
slouken@1973
   513
/* These are needed for borderless/fullscreen windows */
slouken@1973
   514
- (BOOL)canBecomeKeyWindow;
slouken@1973
   515
- (BOOL)canBecomeMainWindow;
slouken@1973
   516
@end
slouken@1973
   517
slouken@1973
   518
@implementation SDLWindow
slouken@1973
   519
- (BOOL)canBecomeKeyWindow
slouken@1973
   520
{
slouken@1973
   521
    return YES;
slouken@1973
   522
}
slouken@1973
   523
slouken@1973
   524
- (BOOL)canBecomeMainWindow
slouken@1973
   525
{
slouken@1973
   526
    return YES;
slouken@1973
   527
}
slouken@1973
   528
@end
slouken@1973
   529
slouken@5379
   530
@interface SDLView : NSView
jorgen@7158
   531
slouken@5379
   532
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
   533
- (void)rightMouseDown:(NSEvent *)theEvent;
gzjjgod@4915
   534
@end
gzjjgod@4915
   535
gzjjgod@4915
   536
@implementation SDLView
gzjjgod@4915
   537
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
   538
{
slouken@5371
   539
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
   540
}
jorgen@7158
   541
jorgen@7158
   542
- (void)resetCursorRects
jorgen@7158
   543
{
jorgen@7158
   544
    [super resetCursorRects];
jorgen@7158
   545
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
   546
jorgen@7270
   547
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
   548
        [self addCursorRect:[self bounds]
jorgen@7158
   549
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
   550
    } else {
jorgen@7158
   551
        [self addCursorRect:[self bounds]
jorgen@7158
   552
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
   553
    }
jorgen@7158
   554
}
gzjjgod@4915
   555
@end
gzjjgod@4915
   556
slouken@5249
   557
static unsigned int
slouken@5305
   558
GetWindowStyle(SDL_Window * window)
slouken@5249
   559
{
slouken@5249
   560
    unsigned int style;
slouken@5249
   561
slouken@7191
   562
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5249
   563
        style = NSBorderlessWindowMask;
slouken@7191
   564
    } else {
slouken@7191
   565
        if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@7191
   566
            style = NSBorderlessWindowMask;
slouken@7191
   567
        } else {
slouken@7191
   568
            style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@7191
   569
        }
slouken@7191
   570
        if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@7191
   571
            style |= NSResizableWindowMask;
slouken@7191
   572
        }
slouken@7191
   573
    }
slouken@5249
   574
    return style;
slouken@5249
   575
}
slouken@5249
   576
slouken@1933
   577
static int
slouken@1951
   578
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   579
{
slouken@6848
   580
    NSAutoreleasePool *pool;
slouken@1951
   581
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
   582
    SDL_WindowData *data;
slouken@1933
   583
slouken@1933
   584
    /* Allocate the window data */
slouken@5249
   585
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
   586
    if (!data) {
icculus@7037
   587
        return SDL_OutOfMemory();
slouken@1933
   588
    }
slouken@3685
   589
    data->window = window;
slouken@3688
   590
    data->nswindow = nswindow;
slouken@1933
   591
    data->created = created;
slouken@1951
   592
    data->videodata = videodata;
slouken@1933
   593
slouken@6848
   594
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   595
slouken@6848
   596
    /* Create an event listener for the window */
slouken@6848
   597
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
   598
slouken@6848
   599
    /* Fill in the SDL window with the window data */
slouken@6848
   600
    {
slouken@6848
   601
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
   602
        ConvertNSRect(&rect);
slouken@6848
   603
        window->x = (int)rect.origin.x;
slouken@6848
   604
        window->y = (int)rect.origin.y;
slouken@6848
   605
        window->w = (int)rect.size.width;
slouken@6848
   606
        window->h = (int)rect.size.height;
slouken@6848
   607
    }
slouken@6848
   608
slouken@6848
   609
    /* Set up the listener after we create the view */
slouken@6848
   610
    [data->listener listen:data];
slouken@6848
   611
slouken@6848
   612
    if ([nswindow isVisible]) {
slouken@6848
   613
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
   614
    } else {
slouken@6848
   615
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
   616
    }
jorgen@7084
   617
slouken@6848
   618
    {
slouken@6848
   619
        unsigned int style = [nswindow styleMask];
slouken@6848
   620
slouken@6848
   621
        if (style == NSBorderlessWindowMask) {
slouken@6848
   622
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
   623
        } else {
slouken@6848
   624
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
   625
        }
slouken@6848
   626
        if (style & NSResizableWindowMask) {
slouken@6848
   627
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
   628
        } else {
slouken@6848
   629
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
   630
        }
slouken@6848
   631
    }
jorgen@7084
   632
slouken@6848
   633
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
   634
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
   635
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
   636
    } else {
slouken@6848
   637
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
   638
    }
jorgen@7084
   639
slouken@6848
   640
    if ([nswindow isMiniaturized]) {
slouken@6848
   641
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
   642
    } else {
slouken@6848
   643
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
   644
    }
jorgen@7084
   645
slouken@6848
   646
    if ([nswindow isKeyWindow]) {
slouken@6848
   647
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
   648
        SDL_SetKeyboardFocus(data->window);
slouken@6848
   649
    }
slouken@1933
   650
jorgen@7085
   651
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
   652
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
   653
     */
jorgen@7085
   654
    [nswindow setOneShot:NO];
jorgen@7085
   655
slouken@6848
   656
    /* All done! */
slouken@6848
   657
    [pool release];
slouken@6848
   658
    window->driverdata = data;
slouken@6848
   659
    return 0;
slouken@1933
   660
}
slouken@1933
   661
slouken@1933
   662
int
slouken@1933
   663
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   664
{
slouken@6848
   665
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   666
    NSWindow *nswindow;
slouken@6848
   667
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
   668
    NSRect rect;
slouken@6848
   669
    SDL_Rect bounds;
slouken@6848
   670
    unsigned int style;
slouken@1933
   671
slouken@6848
   672
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
   673
    rect.origin.x = window->x;
slouken@6848
   674
    rect.origin.y = window->y;
slouken@6848
   675
    rect.size.width = window->w;
slouken@6848
   676
    rect.size.height = window->h;
slouken@6848
   677
    ConvertNSRect(&rect);
slouken@1933
   678
slouken@6848
   679
    style = GetWindowStyle(window);
slouken@1933
   680
slouken@6848
   681
    /* Figure out which screen to place this window */
slouken@6848
   682
    NSArray *screens = [NSScreen screens];
slouken@6848
   683
    NSScreen *screen = nil;
slouken@6848
   684
    NSScreen *candidate;
slouken@6848
   685
    int i, count = [screens count];
slouken@6848
   686
    for (i = 0; i < count; ++i) {
slouken@6848
   687
        candidate = [screens objectAtIndex:i];
slouken@6848
   688
        NSRect screenRect = [candidate frame];
slouken@6848
   689
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
   690
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
   691
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
   692
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
   693
            screen = candidate;
slouken@6848
   694
            rect.origin.x -= screenRect.origin.x;
slouken@6848
   695
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   696
        }
slouken@6848
   697
    }
jorgen@7085
   698
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
icculus@7205
   699
    [nswindow setBackgroundColor:[NSColor blackColor]];
alexey@6832
   700
slouken@7191
   701
    /* Create a default view for this window */
slouken@6848
   702
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
   703
    NSView *contentView = [[SDLView alloc] initWithFrame:rect];
slouken@6848
   704
    [nswindow setContentView: contentView];
slouken@6848
   705
    [contentView release];
alexey@6832
   706
slouken@6848
   707
    [pool release];
slouken@6848
   708
slouken@6848
   709
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
   710
        [nswindow release];
slouken@6848
   711
        return -1;
slouken@3506
   712
    }
slouken@6848
   713
    return 0;
slouken@1933
   714
}
slouken@1933
   715
slouken@1933
   716
int
slouken@1933
   717
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   718
{
slouken@6848
   719
    NSAutoreleasePool *pool;
slouken@1933
   720
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   721
    NSString *title;
slouken@1933
   722
slouken@6848
   723
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   724
slouken@6848
   725
    /* Query the title from the existing window */
slouken@6848
   726
    title = [nswindow title];
slouken@6848
   727
    if (title) {
slouken@6848
   728
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   729
    }
slouken@1933
   730
slouken@6848
   731
    [pool release];
slouken@6848
   732
slouken@1951
   733
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   734
}
slouken@1933
   735
slouken@1933
   736
void
slouken@1933
   737
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   738
{
slouken@6848
   739
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   740
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   741
    NSString *string;
slouken@1933
   742
slouken@6848
   743
    if(window->title) {
slouken@6848
   744
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@6848
   745
    } else {
slouken@6848
   746
        string = [[NSString alloc] init];
slouken@1956
   747
    }
slouken@6848
   748
    [nswindow setTitle:string];
slouken@6848
   749
    [string release];
slouken@6848
   750
slouken@6848
   751
    [pool release];
slouken@1933
   752
}
slouken@1933
   753
slouken@1933
   754
void
slouken@5375
   755
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@5375
   756
{
slouken@6848
   757
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   758
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
   759
slouken@6848
   760
    if (nsimage) {
slouken@6848
   761
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
   762
    }
slouken@6848
   763
slouken@6848
   764
    [pool release];
slouken@5375
   765
}
slouken@5375
   766
slouken@5375
   767
void
slouken@1933
   768
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   769
{
slouken@6848
   770
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   771
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   772
    NSRect rect;
slouken@6848
   773
    Uint32 moveHack;
slouken@1933
   774
slouken@6848
   775
    rect.origin.x = window->x;
slouken@6848
   776
    rect.origin.y = window->y;
slouken@6848
   777
    rect.size.width = window->w;
slouken@6848
   778
    rect.size.height = window->h;
slouken@6848
   779
    ConvertNSRect(&rect);
slouken@5478
   780
slouken@6848
   781
    moveHack = s_moveHack;
slouken@6848
   782
    s_moveHack = 0;
slouken@6848
   783
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
   784
    s_moveHack = moveHack;
slouken@5478
   785
slouken@6848
   786
    if (window == _this->current_glwin) {
slouken@6848
   787
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   788
    }
slouken@6848
   789
slouken@6848
   790
    [pool release];
slouken@1933
   791
}
slouken@1933
   792
slouken@1933
   793
void
slouken@1933
   794
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   795
{
slouken@6848
   796
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   797
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
   798
    NSWindow *nswindow = windata->nswindow;
slouken@6848
   799
    NSSize size;
slouken@1933
   800
slouken@6848
   801
    size.width = window->w;
slouken@6848
   802
    size.height = window->h;
slouken@6848
   803
    [nswindow setContentSize:size];
icculus@5564
   804
slouken@6848
   805
    if (window == _this->current_glwin) {
slouken@6848
   806
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   807
    }
slouken@6848
   808
slouken@6848
   809
    [pool release];
slouken@1933
   810
}
slouken@1933
   811
slouken@1933
   812
void
stopiccot@6681
   813
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
stopiccot@6681
   814
{
slouken@6848
   815
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   816
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
   817
slouken@6848
   818
    NSSize minSize;
slouken@6848
   819
    minSize.width = window->min_w;
slouken@6848
   820
    minSize.height = window->min_h;
slouken@7191
   821
slouken@6848
   822
    [windata->nswindow setContentMinSize:minSize];
slouken@7191
   823
slouken@6848
   824
    [pool release];
slouken@6788
   825
}
slouken@6788
   826
slouken@6788
   827
void
slouken@6788
   828
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slouken@6788
   829
{
slouken@6848
   830
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   831
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
   832
slouken@6848
   833
    NSSize maxSize;
slouken@6848
   834
    maxSize.width = window->max_w;
slouken@6848
   835
    maxSize.height = window->max_h;
slouken@7191
   836
slouken@6848
   837
    [windata->nswindow setContentMaxSize:maxSize];
slouken@7191
   838
slouken@6848
   839
    [pool release];
stopiccot@6681
   840
}
stopiccot@6681
   841
stopiccot@6681
   842
void
slouken@1933
   843
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   844
{
slouken@6848
   845
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7087
   846
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
   847
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
   848
slouken@6848
   849
    if (![nswindow isMiniaturized]) {
jorgen@7087
   850
        [windowData->listener pauseVisibleObservation];
slouken@6848
   851
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
   852
        [windowData->listener resumeVisibleObservation];
slouken@1956
   853
    }
slouken@6848
   854
    [pool release];
slouken@1933
   855
}
slouken@1933
   856
slouken@1933
   857
void
slouken@1933
   858
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   859
{
slouken@6848
   860
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   861
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   862
slouken@6848
   863
    [nswindow orderOut:nil];
slouken@6848
   864
    [pool release];
slouken@1933
   865
}
slouken@1933
   866
slouken@1933
   867
void
slouken@1933
   868
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   869
{
slouken@6848
   870
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7087
   871
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
   872
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
   873
jorgen@7469
   874
    // makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
jorgen@7469
   875
    // a minimized or hidden window, so check for that before showing it.
jorgen@7087
   876
    [windowData->listener pauseVisibleObservation];
jorgen@7469
   877
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
jorgen@7469
   878
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
   879
    }
jorgen@7087
   880
    [windowData->listener resumeVisibleObservation];
jorgen@7087
   881
slouken@6848
   882
    [pool release];
slouken@1933
   883
}
slouken@1933
   884
slouken@1933
   885
void
slouken@1933
   886
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   887
{
slouken@6848
   888
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   889
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   890
slouken@6848
   891
    [nswindow zoom:nil];
slouken@6848
   892
slouken@6848
   893
    if (window == _this->current_glwin) {
slouken@6848
   894
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   895
    }
slouken@6848
   896
slouken@6848
   897
    [pool release];
slouken@1933
   898
}
slouken@1933
   899
slouken@1933
   900
void
slouken@1933
   901
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   902
{
slouken@6848
   903
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   904
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   905
slouken@6848
   906
    [nswindow miniaturize:nil];
slouken@6848
   907
    [pool release];
slouken@1933
   908
}
slouken@1933
   909
slouken@1933
   910
void
slouken@1933
   911
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   912
{
slouken@6848
   913
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   914
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   915
slouken@6848
   916
    if ([nswindow isMiniaturized]) {
slouken@6848
   917
        [nswindow deminiaturize:nil];
slouken@6848
   918
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
   919
        [nswindow zoom:nil];
slouken@1956
   920
    }
slouken@6848
   921
    [pool release];
slouken@1933
   922
}
slouken@1933
   923
slouken@5400
   924
static NSWindow *
slouken@5400
   925
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
slouken@5400
   926
{
slouken@5400
   927
    if (!data->created) {
slouken@5400
   928
        /* Don't mess with other people's windows... */
slouken@5400
   929
        return nswindow;
slouken@5400
   930
    }
slouken@5400
   931
slouken@5400
   932
    [data->listener close];
jorgen@7085
   933
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]];
slouken@5400
   934
    [data->nswindow setContentView:[nswindow contentView]];
jorgen@7085
   935
    /* See comment in SetupWindowData. */
jorgen@7085
   936
    [data->nswindow setOneShot:NO];
slouken@5400
   937
    [data->listener listen:data];
slouken@5400
   938
slouken@5400
   939
    [nswindow close];
slouken@5400
   940
slouken@5400
   941
    return data->nswindow;
slouken@5400
   942
}
slouken@5400
   943
slouken@1933
   944
void
icculus@6422
   945
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   946
{
slouken@6848
   947
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   948
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   949
    if ([nswindow respondsToSelector:@selector(setStyleMask:)]) {
slouken@6848
   950
        [nswindow setStyleMask:GetWindowStyle(window)];
slouken@6848
   951
        if (bordered) {
slouken@7191
   952
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
   953
        }
icculus@6422
   954
    }
slouken@6848
   955
    [pool release];
icculus@6422
   956
}
icculus@6422
   957
icculus@6422
   958
void
slouken@5305
   959
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@5249
   960
{
slouken@6848
   961
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   962
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
   963
    NSWindow *nswindow = data->nswindow;
slouken@6848
   964
    NSRect rect;
slouken@5249
   965
slouken@6848
   966
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
   967
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
   968
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
   969
    }
slouken@6848
   970
slouken@6848
   971
    if (fullscreen) {
slouken@6848
   972
        SDL_Rect bounds;
slouken@6848
   973
slouken@6848
   974
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
   975
        rect.origin.x = bounds.x;
slouken@6848
   976
        rect.origin.y = bounds.y;
slouken@6848
   977
        rect.size.width = bounds.w;
slouken@6848
   978
        rect.size.height = bounds.h;
slouken@6848
   979
        ConvertNSRect(&rect);
slouken@6848
   980
slouken@6848
   981
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
   982
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
   983
        if (screenRect.size.height >= 1.0f) {
slouken@6848
   984
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
   985
        }
slouken@5401
   986
slouken@6848
   987
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
   988
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@6848
   989
        } else {
slouken@6848
   990
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
slouken@6848
   991
        }
slouken@6848
   992
    } else {
slouken@6848
   993
        rect.origin.x = window->windowed.x;
slouken@6848
   994
        rect.origin.y = window->windowed.y;
slouken@6848
   995
        rect.size.width = window->windowed.w;
slouken@6848
   996
        rect.size.height = window->windowed.h;
slouken@6848
   997
        ConvertNSRect(&rect);
alexey@6832
   998
slouken@6848
   999
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1000
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
slouken@5400
  1001
        } else {
slouken@6848
  1002
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
slouken@5361
  1003
        }
slouken@5398
  1004
    }
slouken@6848
  1005
slouken@6848
  1006
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1007
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1008
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1009
    }
slouken@6848
  1010
slouken@6848
  1011
    s_moveHack = 0;
slouken@6848
  1012
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1013
    [nswindow setContentSize:rect.size];
slouken@6848
  1014
    s_moveHack = SDL_GetTicks();
slouken@6848
  1015
slouken@6848
  1016
    /* When the window style changes the title is cleared */
slouken@6848
  1017
    if (!fullscreen) {
slouken@6848
  1018
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1019
    }
slouken@6848
  1020
slouken@6848
  1021
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1022
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1023
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1024
    } else {
slouken@6848
  1025
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1026
    }
jorgen@7087
  1027
jorgen@7087
  1028
    [data->listener pauseVisibleObservation];
slouken@6848
  1029
    [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1030
    [data->listener resumeVisibleObservation];
slouken@6848
  1031
slouken@6848
  1032
    if (window == _this->current_glwin) {
slouken@6848
  1033
        [((NSOpenGLContext *) _this->current_glctx) update];
slouken@6848
  1034
    }
slouken@6848
  1035
slouken@6848
  1036
    [pool release];
slouken@5249
  1037
}
slouken@5249
  1038
slouken@5466
  1039
int
slouken@5466
  1040
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1041
{
slouken@5466
  1042
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1043
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1044
    const uint32_t tableSize = 256;
slouken@5466
  1045
    CGGammaValue redTable[tableSize];
slouken@5466
  1046
    CGGammaValue greenTable[tableSize];
slouken@5466
  1047
    CGGammaValue blueTable[tableSize];
slouken@5466
  1048
    uint32_t i;
slouken@5466
  1049
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1050
slouken@5466
  1051
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1052
    for (i = 0; i < 256; i++) {
slouken@5466
  1053
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1054
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1055
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1056
    }
slouken@5466
  1057
slouken@5466
  1058
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1059
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1060
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1061
    }
slouken@5466
  1062
    return 0;
slouken@5466
  1063
}
slouken@5466
  1064
slouken@5466
  1065
int
slouken@5466
  1066
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1067
{
slouken@5466
  1068
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1069
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1070
    const uint32_t tableSize = 256;
slouken@5466
  1071
    CGGammaValue redTable[tableSize];
slouken@5466
  1072
    CGGammaValue greenTable[tableSize];
slouken@5466
  1073
    CGGammaValue blueTable[tableSize];
slouken@5466
  1074
    uint32_t i, tableCopied;
slouken@5466
  1075
slouken@5466
  1076
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1077
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1078
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1079
    }
slouken@5466
  1080
slouken@5466
  1081
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1082
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1083
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1084
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1085
    }
slouken@5466
  1086
    return 0;
slouken@5466
  1087
}
slouken@5466
  1088
slouken@5249
  1089
void
slouken@6662
  1090
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1091
{
slouken@5371
  1092
    /* Move the cursor to the nearest point in the window */
slouken@6662
  1093
    if (grabbed) {
slouken@5371
  1094
        int x, y;
slouken@5371
  1095
        CGPoint cgpoint;
slouken@5371
  1096
slouken@5371
  1097
        SDL_GetMouseState(&x, &y);
slouken@5371
  1098
        cgpoint.x = window->x + x;
slouken@5371
  1099
        cgpoint.y = window->y + y;
slouken@5371
  1100
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1101
    }
slouken@7191
  1102
slouken@6784
  1103
    if ( window->flags & SDL_WINDOW_FULLSCREEN ) {
slouken@7191
  1104
        SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6755
  1105
slouken@7191
  1106
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@7191
  1107
            /* OpenGL is rendering to the window, so make it visible! */
slouken@7191
  1108
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1109
        } else {
slouken@7191
  1110
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1111
        }
slouken@7191
  1112
    }
slouken@1933
  1113
}
slouken@1933
  1114
slouken@1933
  1115
void
slouken@1933
  1116
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
  1117
{
slouken@6848
  1118
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1119
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1120
slouken@6848
  1121
    if (data) {
slouken@6848
  1122
        [data->listener close];
slouken@6848
  1123
        [data->listener release];
slouken@6848
  1124
        if (data->created) {
slouken@6848
  1125
            [data->nswindow close];
slouken@1933
  1126
        }
slouken@6848
  1127
        SDL_free(data);
slouken@1933
  1128
    }
slouken@6848
  1129
    [pool release];
slouken@1933
  1130
}
slouken@1933
  1131
slouken@1933
  1132
SDL_bool
slouken@1933
  1133
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1134
{
slouken@4900
  1135
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1136
slouken@1933
  1137
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1138
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1139
        info->info.cocoa.window = nswindow;
slouken@1933
  1140
        return SDL_TRUE;
slouken@1933
  1141
    } else {
slouken@1933
  1142
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1143
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1144
        return SDL_FALSE;
slouken@1933
  1145
    }
slouken@1933
  1146
}
slouken@1933
  1147
slouken@6044
  1148
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1149
slouken@1933
  1150
/* vi: set ts=4 sw=4 expandtab: */