src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 14 Nov 2013 22:26:49 -0800
changeset 7990 a05a48e493c0
parent 7970 6dc794be47aa
child 8083 3782a12331d6
permissions -rw-r--r--
Fixed bug 2240 - On OS/X after calling SDL_SetWindowBordered right mouse clicks no longer register

philhassey

On OS/X after calling SDL_SetWindowBordered right mouse clicks no longer register.

Steps to Reproduce:

1. Open a windowed window on OS/X. (With the border on.)

2. e.button.button will give values 1,2,3 depending on which mouse button I click.

3. Call SDL_SetWindowBordered to disable the border.

4. e.button.button will only give values 1,2. 3 (right mouse button) stops coming through.

Expected result:

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