src/video/cocoa/SDL_cocoawindow.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Tue, 30 Jul 2013 16:58:16 -0700
changeset 8727 6b4f9d40414b
parent 7636 d5167d0818b3
permissions -rw-r--r--
Mac: SDL_SetWindowPosition is now relative to the menubar.

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