src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 May 2013 23:30:08 -0700
changeset 7202 373d9e4e8ad9
parent 7200 54c9f310f7c0
child 7205 b1ca5be78064
permissions -rw-r--r--
Fixed bug 1148 - SDL window white upon first appearing

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