src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Tue, 26 May 2015 11:38:04 -0400
changeset 9628 065e4ddc8753
parent 9623 b381999d8944
child 9643 413b222c4b64
permissions -rw-r--r--
Cocoa: Fixed relative mouse mode when app loses/regains focus (thanks, Eric!).

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