src/video/cocoa/SDL_cocoawindow.m
author Alex Szpakowski <slime73@gmail.com>
Tue, 05 May 2015 19:01:55 -0300
changeset 9587 18979eac0a35
parent 9474 067b78126485
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Replaced all remaining uses of NSAutoreleasePool with @autoreleasepool blocks (bugzilla #2680.)
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"
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();
slouken@9086
   534
    if (mouse->relative_mode && !mouse->relative_mode_warp && ![self isMoving]) {
jorgen@8260
   535
        mouse->SetRelativeMouseMode(SDL_TRUE);
jorgen@8260
   536
    }
slouken@5367
   537
slouken@3688
   538
    /* We're going to get keyboard events, since we're key. */
slouken@5367
   539
    SDL_SetKeyboardFocus(window);
slouken@5367
   540
slouken@5367
   541
    /* If we just gained focus we need the updated mouse position */
jorgen@7271
   542
    if (!mouse->relative_mode) {
slouken@5367
   543
        NSPoint point;
slouken@5396
   544
        int x, y;
slouken@5396
   545
slouken@5396
   546
        point = [_data->nswindow mouseLocationOutsideOfEventStream];
slouken@5396
   547
        x = (int)point.x;
slouken@5396
   548
        y = (int)(window->h - point.y);
slouken@5396
   549
slouken@5396
   550
        if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
slouken@6950
   551
            SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@5396
   552
        }
slouken@5367
   553
    }
slouken@1962
   554
slouken@4503
   555
    /* Check to see if someone updated the clipboard */
slouken@4503
   556
    Cocoa_CheckClipboardUpdate(_data->videodata);
icculus@8288
   557
icculus@8293
   558
    if ((isFullscreenSpace) && ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)) {
icculus@8288
   559
        [NSMenu setMenuBarVisible:NO];
icculus@8288
   560
    }
slouken@1933
   561
}
slouken@1933
   562
slouken@1933
   563
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   564
{
jorgen@8260
   565
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@9086
   566
    if (mouse->relative_mode && !mouse->relative_mode_warp) {
jorgen@8260
   567
        mouse->SetRelativeMouseMode(SDL_FALSE);
jorgen@8260
   568
    }
jorgen@8260
   569
slouken@2059
   570
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   571
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   572
        SDL_SetMouseFocus(NULL);
slouken@2059
   573
    }
slouken@2059
   574
slouken@2059
   575
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   576
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   577
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   578
    }
icculus@8288
   579
icculus@8288
   580
    if (isFullscreenSpace) {
icculus@8288
   581
        [NSMenu setMenuBarVisible:YES];
icculus@8288
   582
    }
slouken@1933
   583
}
slouken@1933
   584
slouken@7952
   585
- (void)windowWillEnterFullScreen:(NSNotification *)aNotification
slouken@7952
   586
{
slouken@7952
   587
    SDL_Window *window = _data->window;
slouken@7952
   588
slouken@7990
   589
    SetWindowStyle(window, (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask));
slouken@7965
   590
slouken@7967
   591
    isFullscreenSpace = YES;
slouken@7952
   592
    inFullscreenTransition = YES;
slouken@7952
   593
}
slouken@7952
   594
slouken@7952
   595
- (void)windowDidEnterFullScreen:(NSNotification *)aNotification
slouken@7952
   596
{
slouken@7965
   597
    SDL_Window *window = _data->window;
slouken@7965
   598
slouken@7952
   599
    inFullscreenTransition = NO;
slouken@7961
   600
slouken@7963
   601
    if (pendingWindowOperation == PENDING_OPERATION_LEAVE_FULLSCREEN) {
slouken@7963
   602
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   603
        [self setFullscreenSpace:NO];
slouken@7961
   604
    } else {
icculus@8284
   605
        if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8284
   606
            [NSMenu setMenuBarVisible:NO];
icculus@8284
   607
        }
icculus@8284
   608
slouken@7963
   609
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7965
   610
        /* Force the size change event in case it was delivered earlier
slouken@7965
   611
           while the window was still animating into place.
slouken@7965
   612
         */
slouken@7965
   613
        window->w = 0;
slouken@7965
   614
        window->h = 0;
slouken@7961
   615
        [self windowDidResize:aNotification];
slouken@7961
   616
    }
slouken@7952
   617
}
slouken@7952
   618
slouken@7952
   619
- (void)windowWillExitFullScreen:(NSNotification *)aNotification
slouken@7952
   620
{
slouken@7964
   621
    SDL_Window *window = _data->window;
slouken@7964
   622
slouken@7990
   623
    SetWindowStyle(window, GetWindowStyle(window));
slouken@7964
   624
slouken@7967
   625
    isFullscreenSpace = NO;
slouken@7952
   626
    inFullscreenTransition = YES;
slouken@7952
   627
}
slouken@7952
   628
slouken@7952
   629
- (void)windowDidExitFullScreen:(NSNotification *)aNotification
slouken@7952
   630
{
slouken@7965
   631
    SDL_Window *window = _data->window;
slouken@7952
   632
    NSWindow *nswindow = _data->nswindow;
slouken@7952
   633
slouken@7952
   634
    inFullscreenTransition = NO;
slouken@7961
   635
slouken@8618
   636
    [nswindow setLevel:kCGNormalWindowLevel];
slouken@8618
   637
slouken@7963
   638
    if (pendingWindowOperation == PENDING_OPERATION_ENTER_FULLSCREEN) {
slouken@7963
   639
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   640
        [self setFullscreenSpace:YES];
slouken@7963
   641
    } else if (pendingWindowOperation == PENDING_OPERATION_MINIMIZE) {
slouken@7963
   642
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7963
   643
        [nswindow miniaturize:nil];
slouken@7961
   644
    } else {
icculus@8291
   645
        /* Adjust the fullscreen toggle button and readd menu now that we're here. */
icculus@8291
   646
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8291
   647
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
icculus@8291
   648
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
icculus@8291
   649
        } else {
icculus@8284
   650
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorManaged];
icculus@8284
   651
        }
icculus@8291
   652
        [NSMenu setMenuBarVisible:YES];
icculus@8284
   653
slouken@7963
   654
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7965
   655
        /* Force the size change event in case it was delivered earlier
slouken@7965
   656
           while the window was still animating into place.
slouken@7965
   657
         */
slouken@7965
   658
        window->w = 0;
slouken@7965
   659
        window->h = 0;
slouken@7961
   660
        [self windowDidResize:aNotification];
icculus@8622
   661
slouken@8623
   662
        /* FIXME: Why does the window get hidden? */
slouken@8623
   663
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@8623
   664
            Cocoa_ShowWindow(SDL_GetVideoDevice(), window);
slouken@8623
   665
        }
slouken@7961
   666
    }
slouken@7952
   667
}
slouken@7952
   668
icculus@8291
   669
-(NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
icculus@8291
   670
{
icculus@8291
   671
    if ((_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8291
   672
        return NSApplicationPresentationFullScreen | NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
icculus@8291
   673
    } else {
icculus@8291
   674
        return proposedOptions;
icculus@8291
   675
    }
icculus@8291
   676
}
icculus@8291
   677
icculus@8291
   678
slouken@7191
   679
/* We'll respond to key events by doing nothing so we don't beep.
slouken@7191
   680
 * We could handle key messages here, but we lose some in the NSApp dispatch,
slouken@7191
   681
 * where they get converted to action messages, etc.
slouken@7191
   682
 */
slouken@6514
   683
- (void)flagsChanged:(NSEvent *)theEvent
slouken@6514
   684
{
slouken@7191
   685
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   686
}
slouken@6514
   687
- (void)keyDown:(NSEvent *)theEvent
slouken@6514
   688
{
slouken@7191
   689
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   690
}
slouken@6514
   691
- (void)keyUp:(NSEvent *)theEvent
slouken@6514
   692
{
slouken@7191
   693
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   694
}
slouken@6514
   695
slouken@7191
   696
/* We'll respond to selectors by doing nothing so we don't beep.
slouken@7191
   697
 * The escape key gets converted to a "cancel" selector, etc.
slouken@7191
   698
 */
slouken@6514
   699
- (void)doCommandBySelector:(SEL)aSelector
slouken@6514
   700
{
slouken@7191
   701
    /*NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));*/
slouken@6514
   702
}
slouken@6514
   703
icculus@8935
   704
- (BOOL)processHitTest:(NSEvent *)theEvent
icculus@8931
   705
{
icculus@8935
   706
    SDL_assert(isDragAreaRunning == [_data->nswindow isMovableByWindowBackground]);
icculus@8931
   707
icculus@8935
   708
    if (_data->window->hit_test) {  /* if no hit-test, skip this. */
icculus@8931
   709
        const NSPoint location = [theEvent locationInWindow];
icculus@8931
   710
        const SDL_Point point = { (int) location.x, _data->window->h - (((int) location.y)-1) };
icculus@8935
   711
        const SDL_HitTestResult rc = _data->window->hit_test(_data->window, &point, _data->window->hit_test_data);
icculus@8935
   712
        if (rc == SDL_HITTEST_DRAGGABLE) {
icculus@8935
   713
            if (!isDragAreaRunning) {
icculus@8935
   714
                isDragAreaRunning = YES;
icculus@8935
   715
                [_data->nswindow setMovableByWindowBackground:YES];
icculus@8931
   716
            }
icculus@8935
   717
            return YES;  /* dragging! */
icculus@8931
   718
        }
icculus@8931
   719
    }
icculus@8931
   720
icculus@8931
   721
    if (isDragAreaRunning) {
icculus@8931
   722
        isDragAreaRunning = NO;
icculus@8931
   723
        [_data->nswindow setMovableByWindowBackground:NO];
icculus@8931
   724
        return YES;  /* was dragging, drop event. */
icculus@8931
   725
    }
icculus@8931
   726
icculus@8935
   727
    return NO;  /* not a special area, carry on. */
icculus@8931
   728
}
icculus@8931
   729
slouken@1933
   730
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   731
{
slouken@1959
   732
    int button;
slouken@1933
   733
icculus@8935
   734
    if ([self processHitTest:theEvent]) {
icculus@8931
   735
        return;  /* dragging, drop event. */
icculus@8931
   736
    }
icculus@8931
   737
slouken@1959
   738
    switch ([theEvent buttonNumber]) {
slouken@1959
   739
    case 0:
slouken@7915
   740
        if (([theEvent modifierFlags] & NSControlKeyMask) &&
slouken@7915
   741
		    GetHintCtrlClickEmulateRightClick()) {
slouken@7740
   742
            wasCtrlLeft = YES;
slouken@7740
   743
            button = SDL_BUTTON_RIGHT;
slouken@7740
   744
        } else {
slouken@7740
   745
            wasCtrlLeft = NO;
slouken@7740
   746
            button = SDL_BUTTON_LEFT;
slouken@7740
   747
        }
slouken@1959
   748
        break;
slouken@1959
   749
    case 1:
slouken@1959
   750
        button = SDL_BUTTON_RIGHT;
slouken@1959
   751
        break;
slouken@1959
   752
    case 2:
slouken@1959
   753
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   754
        break;
slouken@1959
   755
    default:
slouken@5061
   756
        button = [theEvent buttonNumber] + 1;
slouken@1959
   757
        break;
slouken@1959
   758
    }
slouken@6950
   759
    SDL_SendMouseButton(_data->window, 0, SDL_PRESSED, button);
slouken@1933
   760
}
slouken@1933
   761
slouken@1933
   762
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   763
{
slouken@1959
   764
    [self mouseDown:theEvent];
slouken@1933
   765
}
slouken@1933
   766
slouken@1933
   767
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   768
{
slouken@1959
   769
    [self mouseDown:theEvent];
slouken@1933
   770
}
slouken@1933
   771
slouken@1933
   772
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   773
{
slouken@1959
   774
    int button;
slouken@1933
   775
icculus@8935
   776
    if ([self processHitTest:theEvent]) {
icculus@8931
   777
        return;  /* stopped dragging, drop event. */
icculus@8931
   778
    }
icculus@8931
   779
slouken@1959
   780
    switch ([theEvent buttonNumber]) {
slouken@1959
   781
    case 0:
slouken@7740
   782
        if (wasCtrlLeft) {
slouken@7740
   783
            button = SDL_BUTTON_RIGHT;
slouken@7740
   784
            wasCtrlLeft = NO;
slouken@7740
   785
        } else {
slouken@7740
   786
            button = SDL_BUTTON_LEFT;
slouken@7740
   787
        }
slouken@1959
   788
        break;
slouken@1959
   789
    case 1:
slouken@1959
   790
        button = SDL_BUTTON_RIGHT;
slouken@1959
   791
        break;
slouken@1959
   792
    case 2:
slouken@1959
   793
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   794
        break;
slouken@1959
   795
    default:
slouken@5061
   796
        button = [theEvent buttonNumber] + 1;
slouken@1959
   797
        break;
slouken@1959
   798
    }
slouken@6950
   799
    SDL_SendMouseButton(_data->window, 0, SDL_RELEASED, button);
slouken@1933
   800
}
slouken@1933
   801
slouken@1933
   802
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   803
{
slouken@1959
   804
    [self mouseUp:theEvent];
slouken@1933
   805
}
slouken@1933
   806
slouken@1933
   807
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   808
{
slouken@1959
   809
    [self mouseUp:theEvent];
slouken@1933
   810
}
slouken@1933
   811
slouken@1933
   812
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   813
{
slouken@5406
   814
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   815
    SDL_Window *window = _data->window;
slouken@5396
   816
    NSPoint point;
slouken@5396
   817
    int x, y;
slouken@1933
   818
icculus@8935
   819
    if ([self processHitTest:theEvent]) {
icculus@8931
   820
        return;  /* dragging, drop event. */
icculus@8931
   821
    }
icculus@8931
   822
slouken@5406
   823
    if (mouse->relative_mode) {
gzjjgod@5059
   824
        return;
slouken@5371
   825
    }
gzjjgod@5059
   826
slouken@5396
   827
    point = [theEvent locationInWindow];
slouken@5396
   828
    x = (int)point.x;
slouken@5396
   829
    y = (int)(window->h - point.y);
slouken@5371
   830
icculus@8929
   831
    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@8929
   832
        if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   833
            if (x < 0) {
slouken@6666
   834
                x = 0;
slouken@6666
   835
            } else if (x >= window->w) {
slouken@6666
   836
                x = window->w - 1;
slouken@6666
   837
            }
slouken@6666
   838
            if (y < 0) {
slouken@6666
   839
                y = 0;
slouken@6666
   840
            } else if (y >= window->h) {
slouken@6666
   841
                y = window->h - 1;
slouken@6666
   842
            }
slouken@6666
   843
jorgen@7593
   844
#if !SDL_MAC_NO_SANDBOX
jorgen@8260
   845
            CGPoint cgpoint;
jorgen@8260
   846
jorgen@7593
   847
            /* When SDL_MAC_NO_SANDBOX is set, this is handled by
jorgen@7593
   848
             * SDL_cocoamousetap.m.
jorgen@7593
   849
             */
jorgen@7593
   850
slouken@6666
   851
            cgpoint.x = window->x + x;
slouken@6666
   852
            cgpoint.y = window->y + y;
jorgen@7098
   853
jorgen@7113
   854
            /* According to the docs, this was deprecated in 10.6, but it's still
jorgen@7113
   855
             * around. The substitute requires a CGEventSource, but I'm not entirely
jorgen@7113
   856
             * sure how we'd procure the right one for this event.
jorgen@7098
   857
             */
jorgen@7113
   858
            CGSetLocalEventsSuppressionInterval(0.0);
slouken@6666
   859
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
jorgen@7113
   860
            CGSetLocalEventsSuppressionInterval(0.25);
jorgen@8261
   861
jorgen@8261
   862
            Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@7593
   863
#endif
slouken@5396
   864
        }
slouken@2059
   865
    }
slouken@6950
   866
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
   867
}
slouken@1933
   868
slouken@1957
   869
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   870
{
slouken@1957
   871
    [self mouseMoved:theEvent];
slouken@1957
   872
}
slouken@1957
   873
slouken@1958
   874
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   875
{
slouken@1958
   876
    [self mouseMoved:theEvent];
slouken@1958
   877
}
slouken@1958
   878
slouken@1958
   879
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   880
{
slouken@1958
   881
    [self mouseMoved:theEvent];
slouken@1958
   882
}
slouken@1958
   883
slouken@1933
   884
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   885
{
gzjjgod@5057
   886
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   887
}
slouken@3688
   888
slouken@4673
   889
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   890
{
urkle@9236
   891
    NSSet *touches = [theEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil];
urkle@9236
   892
    int existingTouchCount = 0;
urkle@9236
   893
urkle@9236
   894
    for (NSTouch* touch in touches) {
urkle@9236
   895
        if ([touch phase] != NSTouchPhaseBegan) {
urkle@9236
   896
            existingTouchCount++;
urkle@9236
   897
        }
urkle@9236
   898
    }
urkle@9236
   899
    if (existingTouchCount == 0) {
urkle@9236
   900
        SDL_TouchID touchID = (SDL_TouchID)(intptr_t)[[touches anyObject] device];
urkle@9236
   901
        int numFingers = SDL_GetNumTouchFingers(touchID);
urkle@9236
   902
        DLog("Reset Lost Fingers: %d", numFingers);
urkle@9236
   903
        for (--numFingers; numFingers >= 0; --numFingers) {
urkle@9236
   904
            SDL_Finger* finger = SDL_GetTouchFinger(touchID, numFingers);
urkle@9236
   905
            SDL_SendTouch(touchID, finger->id, SDL_FALSE, 0, 0, 0);
urkle@9236
   906
        }
urkle@9236
   907
    }
urkle@9236
   908
urkle@9236
   909
    DLog("Began Fingers: %lu .. existing: %d", (unsigned long)[touches count], existingTouchCount);
slouken@8986
   910
    [self handleTouches:NSTouchPhaseBegan withEvent:theEvent];
slouken@4673
   911
}
slouken@4673
   912
slouken@4673
   913
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   914
{
slouken@8986
   915
    [self handleTouches:NSTouchPhaseMoved withEvent:theEvent];
slouken@4673
   916
}
slouken@4673
   917
slouken@4673
   918
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   919
{
slouken@8986
   920
    [self handleTouches:NSTouchPhaseEnded withEvent:theEvent];
slouken@4673
   921
}
slouken@4673
   922
slouken@4673
   923
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
   924
{
slouken@8986
   925
    [self handleTouches:NSTouchPhaseCancelled withEvent:theEvent];
slouken@4673
   926
}
slouken@4673
   927
slouken@8986
   928
- (void)handleTouches:(NSTouchPhase) phase withEvent:(NSEvent *) theEvent
slouken@4673
   929
{
slouken@8986
   930
    NSSet *touches = [theEvent touchesMatchingPhase:phase inView:nil];
slouken@4673
   931
slouken@8986
   932
    for (NSTouch *touch in touches) {
slouken@6953
   933
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@4673
   934
        if (!SDL_GetTouch(touchId)) {
slouken@6951
   935
            if (SDL_AddTouch(touchId, "") < 0) {
slouken@4680
   936
                return;
slouken@4673
   937
            }
slouken@7191
   938
        }
slouken@4687
   939
slouken@6953
   940
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
   941
        float x = [touch normalizedPosition].x;
slouken@4673
   942
        float y = [touch normalizedPosition].y;
slouken@5261
   943
        /* Make the origin the upper left instead of the lower left */
slouken@5261
   944
        y = 1.0f - y;
slouken@4687
   945
slouken@8986
   946
        switch (phase) {
slouken@8986
   947
        case NSTouchPhaseBegan:
slouken@6951
   948
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
   949
            break;
slouken@8986
   950
        case NSTouchPhaseEnded:
slouken@8986
   951
        case NSTouchPhaseCancelled:
slouken@6951
   952
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
   953
            break;
slouken@8986
   954
        case NSTouchPhaseMoved:
slouken@6951
   955
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
   956
            break;
slouken@8986
   957
        default:
slouken@8986
   958
            break;
slouken@4673
   959
        }
slouken@4673
   960
    }
slouken@1933
   961
}
slouken@1933
   962
slouken@1933
   963
@end
slouken@1933
   964
slouken@5379
   965
@interface SDLView : NSView
jorgen@7158
   966
slouken@5379
   967
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
   968
- (void)rightMouseDown:(NSEvent *)theEvent;
icculus@8931
   969
- (BOOL)mouseDownCanMoveWindow;
gzjjgod@4915
   970
@end
gzjjgod@4915
   971
gzjjgod@4915
   972
@implementation SDLView
gzjjgod@4915
   973
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
   974
{
slouken@5371
   975
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
   976
}
jorgen@7158
   977
icculus@8931
   978
- (BOOL)mouseDownCanMoveWindow
icculus@8931
   979
{
icculus@8931
   980
    /* Always say YES, but this doesn't do anything until we call
icculus@8931
   981
       -[NSWindow setMovableByWindowBackground:YES], which we ninja-toggle
icculus@8931
   982
       during mouse events when we're using a drag area. */
icculus@8931
   983
    return YES;
icculus@8931
   984
}
icculus@8931
   985
jorgen@7158
   986
- (void)resetCursorRects
jorgen@7158
   987
{
jorgen@7158
   988
    [super resetCursorRects];
jorgen@7158
   989
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
   990
jorgen@7270
   991
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
   992
        [self addCursorRect:[self bounds]
jorgen@7158
   993
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
   994
    } else {
jorgen@7158
   995
        [self addCursorRect:[self bounds]
jorgen@7158
   996
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
   997
    }
jorgen@7158
   998
}
gzjjgod@4915
   999
@end
gzjjgod@4915
  1000
slouken@1933
  1001
static int
slouken@1951
  1002
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slime73@9587
  1003
{ @autoreleasepool
slouken@1933
  1004
{
slouken@1951
  1005
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
  1006
    SDL_WindowData *data;
slouken@1933
  1007
slouken@1933
  1008
    /* Allocate the window data */
slouken@8926
  1009
    window->driverdata = data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
  1010
    if (!data) {
icculus@7037
  1011
        return SDL_OutOfMemory();
slouken@1933
  1012
    }
slouken@3685
  1013
    data->window = window;
slouken@3688
  1014
    data->nswindow = nswindow;
slouken@1933
  1015
    data->created = created;
slouken@1951
  1016
    data->videodata = videodata;
jorgen@7595
  1017
    data->nscontexts = [[NSMutableArray alloc] init];
slouken@1933
  1018
slouken@6848
  1019
    /* Create an event listener for the window */
slouken@6848
  1020
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
  1021
slouken@6848
  1022
    /* Fill in the SDL window with the window data */
slouken@6848
  1023
    {
slouken@6848
  1024
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@8801
  1025
        ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@6848
  1026
        window->x = (int)rect.origin.x;
slouken@6848
  1027
        window->y = (int)rect.origin.y;
slouken@6848
  1028
        window->w = (int)rect.size.width;
slouken@6848
  1029
        window->h = (int)rect.size.height;
slouken@6848
  1030
    }
slouken@6848
  1031
slouken@6848
  1032
    /* Set up the listener after we create the view */
slouken@6848
  1033
    [data->listener listen:data];
slouken@6848
  1034
slouken@6848
  1035
    if ([nswindow isVisible]) {
slouken@6848
  1036
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
  1037
    } else {
slouken@6848
  1038
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
  1039
    }
jorgen@7084
  1040
slouken@6848
  1041
    {
slouken@6848
  1042
        unsigned int style = [nswindow styleMask];
slouken@6848
  1043
slouken@6848
  1044
        if (style == NSBorderlessWindowMask) {
slouken@6848
  1045
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
  1046
        } else {
slouken@6848
  1047
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
  1048
        }
slouken@6848
  1049
        if (style & NSResizableWindowMask) {
slouken@6848
  1050
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
  1051
        } else {
slouken@6848
  1052
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
  1053
        }
slouken@6848
  1054
    }
jorgen@7084
  1055
slouken@6848
  1056
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
  1057
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1058
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
  1059
    } else {
slouken@6848
  1060
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
  1061
    }
jorgen@7084
  1062
slouken@6848
  1063
    if ([nswindow isMiniaturized]) {
slouken@6848
  1064
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
  1065
    } else {
slouken@6848
  1066
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
  1067
    }
jorgen@7084
  1068
slouken@6848
  1069
    if ([nswindow isKeyWindow]) {
slouken@6848
  1070
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
  1071
        SDL_SetKeyboardFocus(data->window);
slouken@6848
  1072
    }
slouken@1933
  1073
jorgen@7085
  1074
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
  1075
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
  1076
     */
jorgen@7085
  1077
    [nswindow setOneShot:NO];
jorgen@7085
  1078
slouken@6848
  1079
    /* All done! */
slouken@6848
  1080
    window->driverdata = data;
slouken@6848
  1081
    return 0;
slime73@9587
  1082
}}
slouken@1933
  1083
slouken@1933
  1084
int
slouken@1933
  1085
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slime73@9587
  1086
{ @autoreleasepool
slouken@1933
  1087
{
icculus@8295
  1088
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6848
  1089
    NSWindow *nswindow;
slouken@6848
  1090
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
  1091
    NSRect rect;
slouken@6848
  1092
    SDL_Rect bounds;
slouken@6848
  1093
    unsigned int style;
slouken@8986
  1094
    NSArray *screens = [NSScreen screens];
slouken@1933
  1095
slouken@6848
  1096
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1097
    rect.origin.x = window->x;
slouken@6848
  1098
    rect.origin.y = window->y;
slouken@6848
  1099
    rect.size.width = window->w;
slouken@6848
  1100
    rect.size.height = window->h;
slouken@8986
  1101
    ConvertNSRect([screens objectAtIndex:0], (window->flags & FULLSCREEN_MASK), &rect);
slouken@1933
  1102
slouken@6848
  1103
    style = GetWindowStyle(window);
slouken@1933
  1104
slouken@6848
  1105
    /* Figure out which screen to place this window */
slouken@6848
  1106
    NSScreen *screen = nil;
slouken@8986
  1107
    for (NSScreen *candidate in screens) {
slouken@6848
  1108
        NSRect screenRect = [candidate frame];
slouken@6848
  1109
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
  1110
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
  1111
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
  1112
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
  1113
            screen = candidate;
slouken@6848
  1114
            rect.origin.x -= screenRect.origin.x;
slouken@6848
  1115
            rect.origin.y -= screenRect.origin.y;
slouken@3506
  1116
        }
slouken@6848
  1117
    }
slouken@7946
  1118
slouken@7946
  1119
    @try {
slouken@7946
  1120
        nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
slouken@7946
  1121
    }
slouken@7946
  1122
    @catch (NSException *e) {
slime73@9587
  1123
        return SDL_SetError("%s", [[e reason] UTF8String]);
slouken@7946
  1124
    }
icculus@7205
  1125
    [nswindow setBackgroundColor:[NSColor blackColor]];
icculus@8284
  1126
icculus@8295
  1127
    if (videodata->allow_spaces) {
slouken@8986
  1128
        SDL_assert(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6);
icculus@8295
  1129
        SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);
icculus@8284
  1130
        /* we put FULLSCREEN_DESKTOP windows in their own Space, without a toggle button or menubar, later */
icculus@8284
  1131
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8284
  1132
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
slouken@7968
  1133
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7968
  1134
        }
slouken@7955
  1135
    }
alexey@6832
  1136
slouken@7191
  1137
    /* Create a default view for this window */
slouken@6848
  1138
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
  1139
    NSView *contentView = [[SDLView alloc] initWithFrame:rect];
urkle@7746
  1140
slouken@7955
  1141
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
urkle@7746
  1142
        if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
urkle@7746
  1143
            [contentView setWantsBestResolutionOpenGLSurface:YES];
urkle@7746
  1144
        }
urkle@7746
  1145
    }
urkle@7746
  1146
slouken@6848
  1147
    [nswindow setContentView: contentView];
slouken@6848
  1148
    [contentView release];
alexey@6832
  1149
jorgen@9237
  1150
    /* Allow files and folders to be dragged onto the window by users */
jorgen@9237
  1151
    [nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
jorgen@9237
  1152
slouken@6848
  1153
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
  1154
        [nswindow release];
slouken@6848
  1155
        return -1;
slouken@3506
  1156
    }
slouken@6848
  1157
    return 0;
slime73@9587
  1158
}}
slouken@1933
  1159
slouken@1933
  1160
int
slouken@1933
  1161
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slime73@9587
  1162
{ @autoreleasepool
slouken@1933
  1163
{
slouken@1933
  1164
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
  1165
    NSString *title;
slouken@1933
  1166
slouken@6848
  1167
    /* Query the title from the existing window */
slouken@6848
  1168
    title = [nswindow title];
slouken@6848
  1169
    if (title) {
slouken@6848
  1170
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
  1171
    }
slouken@1933
  1172
slouken@1951
  1173
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slime73@9587
  1174
}}
slouken@1933
  1175
slouken@1933
  1176
void
slouken@1933
  1177
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slime73@9587
  1178
{ @autoreleasepool
slouken@1933
  1179
{
slouken@6848
  1180
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
icculus@9474
  1181
    NSString *string = [[NSString alloc] initWithUTF8String:window->title];
slouken@6848
  1182
    [nswindow setTitle:string];
slouken@6848
  1183
    [string release];
slime73@9587
  1184
}}
slouken@1933
  1185
slouken@1933
  1186
void
slouken@5375
  1187
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slime73@9587
  1188
{ @autoreleasepool
slouken@5375
  1189
{
slouken@6848
  1190
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
  1191
slouken@6848
  1192
    if (nsimage) {
slouken@6848
  1193
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
  1194
    }
slime73@9587
  1195
}}
slouken@5375
  1196
slouken@5375
  1197
void
slouken@1933
  1198
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slime73@9587
  1199
{ @autoreleasepool
slouken@1933
  1200
{
jorgen@7594
  1201
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1202
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1203
    NSRect rect;
slouken@6848
  1204
    Uint32 moveHack;
slouken@1933
  1205
slouken@6848
  1206
    rect.origin.x = window->x;
slouken@6848
  1207
    rect.origin.y = window->y;
slouken@6848
  1208
    rect.size.width = window->w;
slouken@6848
  1209
    rect.size.height = window->h;
slouken@8801
  1210
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@5478
  1211
slouken@6848
  1212
    moveHack = s_moveHack;
slouken@6848
  1213
    s_moveHack = 0;
slouken@6848
  1214
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1215
    s_moveHack = moveHack;
slouken@5478
  1216
jorgen@7595
  1217
    ScheduleContextUpdates(windata);
slime73@9587
  1218
}}
slouken@1933
  1219
slouken@1933
  1220
void
slouken@1933
  1221
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slime73@9587
  1222
{ @autoreleasepool
slouken@1933
  1223
{
slouken@6848
  1224
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
  1225
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1226
    NSSize size;
slouken@1933
  1227
slouken@6848
  1228
    size.width = window->w;
slouken@6848
  1229
    size.height = window->h;
slouken@6848
  1230
    [nswindow setContentSize:size];
icculus@5564
  1231
jorgen@7595
  1232
    ScheduleContextUpdates(windata);
slime73@9587
  1233
}}
slouken@1933
  1234
slouken@1933
  1235
void
stopiccot@6681
  1236
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
slime73@9587
  1237
{ @autoreleasepool
stopiccot@6681
  1238
{
slouken@6848
  1239
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1240
slouken@6848
  1241
    NSSize minSize;
slouken@6848
  1242
    minSize.width = window->min_w;
slouken@6848
  1243
    minSize.height = window->min_h;
slouken@7191
  1244
slouken@6848
  1245
    [windata->nswindow setContentMinSize:minSize];
slime73@9587
  1246
}}
slouken@6788
  1247
slouken@6788
  1248
void
slouken@6788
  1249
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slime73@9587
  1250
{ @autoreleasepool
slouken@6788
  1251
{
slouken@6848
  1252
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1253
slouken@6848
  1254
    NSSize maxSize;
slouken@6848
  1255
    maxSize.width = window->max_w;
slouken@6848
  1256
    maxSize.height = window->max_h;
slouken@7191
  1257
slouken@6848
  1258
    [windata->nswindow setContentMaxSize:maxSize];
slime73@9587
  1259
}}
stopiccot@6681
  1260
stopiccot@6681
  1261
void
slouken@1933
  1262
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slime73@9587
  1263
{ @autoreleasepool
slouken@1933
  1264
{
jorgen@7087
  1265
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1266
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
  1267
slouken@6848
  1268
    if (![nswindow isMiniaturized]) {
jorgen@7087
  1269
        [windowData->listener pauseVisibleObservation];
slouken@6848
  1270
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1271
        [windowData->listener resumeVisibleObservation];
slouken@1956
  1272
    }
slime73@9587
  1273
}}
slouken@1933
  1274
slouken@1933
  1275
void
slouken@1933
  1276
Cocoa_HideWindow(_THIS, SDL_Window * window)
slime73@9587
  1277
{ @autoreleasepool
slouken@1933
  1278
{
slouken@6848
  1279
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1280
slouken@6848
  1281
    [nswindow orderOut:nil];
slime73@9587
  1282
}}
slouken@1933
  1283
slouken@1933
  1284
void
slouken@1933
  1285
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slime73@9587
  1286
{ @autoreleasepool
slouken@1933
  1287
{
jorgen@7087
  1288
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1289
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
  1290
slouken@7771
  1291
    /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
slouken@7771
  1292
       a minimized or hidden window, so check for that before showing it.
slouken@7771
  1293
     */
jorgen@7087
  1294
    [windowData->listener pauseVisibleObservation];
jorgen@7469
  1295
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
alfred@9041
  1296
        [NSApp activateIgnoringOtherApps:YES];
jorgen@7469
  1297
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
  1298
    }
jorgen@7087
  1299
    [windowData->listener resumeVisibleObservation];
slime73@9587
  1300
}}
slouken@1933
  1301
slouken@1933
  1302
void
slouken@1933
  1303
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1304
{ @autoreleasepool
slouken@1933
  1305
{
jorgen@7594
  1306
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1307
    NSWindow *nswindow = windata->nswindow;
slouken@1933
  1308
slouken@6848
  1309
    [nswindow zoom:nil];
slouken@6848
  1310
jorgen@7595
  1311
    ScheduleContextUpdates(windata);
slime73@9587
  1312
}}
slouken@1933
  1313
slouken@1933
  1314
void
slouken@1933
  1315
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1316
{ @autoreleasepool
slouken@1933
  1317
{
slouken@7963
  1318
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7963
  1319
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1320
slouken@7968
  1321
    if ([data->listener isInFullscreenSpaceTransition]) {
slouken@7963
  1322
        [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
slouken@7963
  1323
    } else {
slouken@7963
  1324
        [nswindow miniaturize:nil];
slouken@7963
  1325
    }
slime73@9587
  1326
}}
slouken@1933
  1327
slouken@1933
  1328
void
slouken@1933
  1329
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slime73@9587
  1330
{ @autoreleasepool
slouken@1933
  1331
{
slouken@6848
  1332
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1333
slouken@6848
  1334
    if ([nswindow isMiniaturized]) {
slouken@6848
  1335
        [nswindow deminiaturize:nil];
slouken@6848
  1336
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1337
        [nswindow zoom:nil];
slouken@1956
  1338
    }
slime73@9587
  1339
}}
slouken@1933
  1340
slouken@5400
  1341
static NSWindow *
slouken@5400
  1342
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
slouken@5400
  1343
{
slouken@5400
  1344
    if (!data->created) {
slouken@5400
  1345
        /* Don't mess with other people's windows... */
slouken@5400
  1346
        return nswindow;
slouken@5400
  1347
    }
slouken@5400
  1348
slouken@5400
  1349
    [data->listener close];
jorgen@7085
  1350
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]];
slouken@5400
  1351
    [data->nswindow setContentView:[nswindow contentView]];
jorgen@9237
  1352
    [data->nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
jorgen@7085
  1353
    /* See comment in SetupWindowData. */
jorgen@7085
  1354
    [data->nswindow setOneShot:NO];
slouken@5400
  1355
    [data->listener listen:data];
slouken@5400
  1356
slouken@5400
  1357
    [nswindow close];
slouken@5400
  1358
slouken@5400
  1359
    return data->nswindow;
slouken@5400
  1360
}
slouken@5400
  1361
slouken@1933
  1362
void
icculus@6422
  1363
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
slime73@9587
  1364
{ @autoreleasepool
icculus@6422
  1365
{
slouken@7990
  1366
    if (SetWindowStyle(window, GetWindowStyle(window))) {
slouken@6848
  1367
        if (bordered) {
slouken@7191
  1368
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
  1369
        }
icculus@6422
  1370
    }
slime73@9587
  1371
}}
icculus@6422
  1372
slouken@7952
  1373
slouken@7968
  1374
void
slouken@7968
  1375
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slime73@9587
  1376
{ @autoreleasepool
slouken@7952
  1377
{
slouken@6848
  1378
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
  1379
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1380
    NSRect rect;
slouken@5249
  1381
slouken@6848
  1382
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1383
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
  1384
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
  1385
    }
slouken@6848
  1386
slouken@6848
  1387
    if (fullscreen) {
slouken@6848
  1388
        SDL_Rect bounds;
slouken@6848
  1389
slouken@6848
  1390
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1391
        rect.origin.x = bounds.x;
slouken@6848
  1392
        rect.origin.y = bounds.y;
slouken@6848
  1393
        rect.size.width = bounds.w;
slouken@6848
  1394
        rect.size.height = bounds.h;
slouken@8801
  1395
        ConvertNSRect([nswindow screen], fullscreen, &rect);
slouken@6848
  1396
slouken@6848
  1397
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
  1398
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
  1399
        if (screenRect.size.height >= 1.0f) {
slouken@6848
  1400
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
  1401
        }
slouken@5401
  1402
slouken@6848
  1403
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1404
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@6848
  1405
        } else {
slouken@6848
  1406
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
slouken@6848
  1407
        }
slouken@6848
  1408
    } else {
slouken@6848
  1409
        rect.origin.x = window->windowed.x;
slouken@6848
  1410
        rect.origin.y = window->windowed.y;
slouken@6848
  1411
        rect.size.width = window->windowed.w;
slouken@6848
  1412
        rect.size.height = window->windowed.h;
slouken@8801
  1413
        ConvertNSRect([nswindow screen], fullscreen, &rect);
alexey@6832
  1414
slouken@6848
  1415
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1416
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
slouken@5400
  1417
        } else {
slouken@6848
  1418
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
slouken@5361
  1419
        }
slouken@5398
  1420
    }
slouken@6848
  1421
slouken@6848
  1422
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1423
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1424
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1425
    }
slouken@6848
  1426
slouken@6848
  1427
    s_moveHack = 0;
slouken@7873
  1428
    [nswindow setContentSize:rect.size];
slouken@6848
  1429
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1430
    s_moveHack = SDL_GetTicks();
slouken@6848
  1431
slouken@6848
  1432
    /* When the window style changes the title is cleared */
slouken@6848
  1433
    if (!fullscreen) {
slouken@6848
  1434
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1435
    }
slouken@6848
  1436
slouken@6848
  1437
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1438
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1439
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1440
    } else {
slouken@6848
  1441
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1442
    }
jorgen@7087
  1443
jorgen@7636
  1444
    if ([nswindow isVisible] || fullscreen) {
jorgen@7636
  1445
        [data->listener pauseVisibleObservation];
jorgen@7636
  1446
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7636
  1447
        [data->listener resumeVisibleObservation];
jorgen@7636
  1448
    }
slouken@6848
  1449
jorgen@7595
  1450
    ScheduleContextUpdates(data);
slime73@9587
  1451
}}
slouken@5249
  1452
slouken@5466
  1453
int
slouken@5466
  1454
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1455
{
slouken@5466
  1456
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1457
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1458
    const uint32_t tableSize = 256;
slouken@5466
  1459
    CGGammaValue redTable[tableSize];
slouken@5466
  1460
    CGGammaValue greenTable[tableSize];
slouken@5466
  1461
    CGGammaValue blueTable[tableSize];
slouken@5466
  1462
    uint32_t i;
slouken@5466
  1463
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1464
slouken@5466
  1465
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1466
    for (i = 0; i < 256; i++) {
slouken@5466
  1467
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1468
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1469
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1470
    }
slouken@5466
  1471
slouken@5466
  1472
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1473
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1474
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1475
    }
slouken@5466
  1476
    return 0;
slouken@5466
  1477
}
slouken@5466
  1478
slouken@5466
  1479
int
slouken@5466
  1480
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1481
{
slouken@5466
  1482
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1483
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1484
    const uint32_t tableSize = 256;
slouken@5466
  1485
    CGGammaValue redTable[tableSize];
slouken@5466
  1486
    CGGammaValue greenTable[tableSize];
slouken@5466
  1487
    CGGammaValue blueTable[tableSize];
slouken@5466
  1488
    uint32_t i, tableCopied;
slouken@5466
  1489
slouken@5466
  1490
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1491
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1492
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1493
    }
slouken@5466
  1494
slouken@5466
  1495
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1496
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1497
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1498
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1499
    }
slouken@5466
  1500
    return 0;
slouken@5466
  1501
}
slouken@5466
  1502
slouken@5249
  1503
void
slouken@6662
  1504
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1505
{
slouken@5371
  1506
    /* Move the cursor to the nearest point in the window */
jorgen@8260
  1507
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@8260
  1508
    if (grabbed && data && ![data->listener isMoving]) {
slouken@5371
  1509
        int x, y;
slouken@5371
  1510
        CGPoint cgpoint;
slouken@5371
  1511
slouken@5371
  1512
        SDL_GetMouseState(&x, &y);
slouken@5371
  1513
        cgpoint.x = window->x + x;
slouken@5371
  1514
        cgpoint.y = window->y + y;
jorgen@8261
  1515
jorgen@8261
  1516
        Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@8261
  1517
jorgen@8261
  1518
        DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
slouken@5371
  1519
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1520
    }
slouken@7191
  1521
icculus@8652
  1522
    if ( data && (window->flags & SDL_WINDOW_FULLSCREEN) ) {
slouken@7191
  1523
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@7191
  1524
            /* OpenGL is rendering to the window, so make it visible! */
slouken@7191
  1525
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1526
        } else {
slouken@7191
  1527
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1528
        }
slouken@7191
  1529
    }
slouken@1933
  1530
}
slouken@1933
  1531
slouken@1933
  1532
void
slouken@1933
  1533
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slime73@9587
  1534
{ @autoreleasepool
slouken@1933
  1535
{
slouken@6848
  1536
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1537
slouken@6848
  1538
    if (data) {
slouken@6848
  1539
        [data->listener close];
slouken@6848
  1540
        [data->listener release];
slouken@6848
  1541
        if (data->created) {
slouken@6848
  1542
            [data->nswindow close];
slouken@1933
  1543
        }
jorgen@7595
  1544
jorgen@7595
  1545
        NSArray *contexts = [[data->nscontexts copy] autorelease];
jorgen@7595
  1546
        for (SDLOpenGLContext *context in contexts) {
jorgen@7595
  1547
            /* Calling setWindow:NULL causes the context to remove itself from the context list. */            
jorgen@7595
  1548
            [context setWindow:NULL];
jorgen@7595
  1549
        }
jorgen@7595
  1550
        [data->nscontexts release];
jorgen@7595
  1551
slouken@6848
  1552
        SDL_free(data);
slouken@1933
  1553
    }
slouken@8978
  1554
    window->driverdata = NULL;
slime73@9587
  1555
}}
slouken@1933
  1556
slouken@1933
  1557
SDL_bool
slouken@1933
  1558
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1559
{
slouken@4900
  1560
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1561
slouken@1933
  1562
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1563
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1564
        info->info.cocoa.window = nswindow;
slouken@1933
  1565
        return SDL_TRUE;
slouken@1933
  1566
    } else {
slouken@1933
  1567
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1568
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1569
        return SDL_FALSE;
slouken@1933
  1570
    }
slouken@1933
  1571
}
slouken@1933
  1572
slouken@7968
  1573
SDL_bool
slouken@7969
  1574
Cocoa_IsWindowInFullscreenSpace(SDL_Window * window)
slouken@7969
  1575
{
slouken@7969
  1576
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7969
  1577
slouken@7969
  1578
    if ([data->listener isInFullscreenSpace]) {
slouken@7969
  1579
        return SDL_TRUE;
slouken@7969
  1580
    } else {
slouken@7969
  1581
        return SDL_FALSE;
slouken@7969
  1582
    }
slouken@7969
  1583
}
slouken@7969
  1584
slouken@7969
  1585
SDL_bool
slouken@7968
  1586
Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state)
slime73@9587
  1587
{ @autoreleasepool
slouken@7968
  1588
{
slouken@7968
  1589
    SDL_bool succeeded = SDL_FALSE;
slouken@7968
  1590
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7968
  1591
slouken@7968
  1592
    if ([data->listener setFullscreenSpace:(state ? YES : NO)]) {
slouken@7968
  1593
        succeeded = SDL_TRUE;
slouken@8810
  1594
slouken@8810
  1595
        /* Wait for the transition to complete, so application changes
slouken@8810
  1596
           take effect properly (e.g. setting the window size, etc.)
slouken@8810
  1597
         */
slouken@8810
  1598
        const int limit = 10000;
slouken@8810
  1599
        int count = 0;
slouken@8810
  1600
        while ([data->listener isInFullscreenSpaceTransition]) {
slouken@8810
  1601
            if ( ++count == limit ) {
slouken@8810
  1602
                /* Uh oh, transition isn't completing. Should we assert? */
slouken@8810
  1603
                break;
slouken@8810
  1604
            }
slouken@8810
  1605
            SDL_Delay(1);
slouken@8810
  1606
            SDL_PumpEvents();
slouken@8810
  1607
        }
slouken@7968
  1608
    }
slouken@7968
  1609
slouken@7968
  1610
    return succeeded;
slime73@9587
  1611
}}
slouken@7968
  1612
icculus@8931
  1613
int
icculus@8935
  1614
Cocoa_SetWindowHitTest(SDL_Window * window, SDL_bool enabled)
icculus@8931
  1615
{
icculus@8931
  1616
    return 0;  /* just succeed, the real work is done elsewhere. */
icculus@8931
  1617
}
icculus@8931
  1618
slouken@6044
  1619
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1620
slouken@1933
  1621
/* vi: set ts=4 sw=4 expandtab: */