src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 02 Jun 2014 09:09:40 -0700
changeset 8801 98173814b673
parent 8798 2703c0c19f45
child 8809 054ec268f363
permissions -rw-r--r--
Fixed bug 2534 - Mac: black bar at top of screen in SDL_WINDOW_FULLSCREEN mode

Alex Szpakowski

Patch to fix the y component of the position of fullscreen windows in OS X.

In Mac OS X with the latest Mercurial code, when a window is in exclusive-fullscreen the y component of its position is offset by the same amount that is normally taken up by the menubar, resulting in a black bar at the top of the screen.

The recent changes to the internal ConvertNSRect function make it treat the bottom of the menubar as 0 for the y component of window positions, even when the window is fullscreen and 'above' the menubar.

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