src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Wed, 28 May 2014 01:22:47 -0400
changeset 8935 9d2f0236322b
parent 8931 44d8a2f4b431
child 8953 dc80dc0bd22e
permissions -rw-r--r--
Changed drag area API to a hit-testing API.

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