src/video/cocoa/SDL_cocoawindow.m
author Jørgen P. Tjernø <jorgen@uberent.com>
Mon, 24 Nov 2014 11:46:20 -0800
changeset 9237 2cc90bb31777
parent 9236 a845edf98a80
child 9419 9763f689bced
permissions -rw-r--r--
Mac: Add drag & drop support.

Fixes bug https://bugzilla.libsdl.org/show_bug.cgi?id=2757

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