src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Tue, 27 May 2014 01:27:42 -0400
changeset 8931 44d8a2f4b431
parent 8929 7b459b49fa92
child 8935 9d2f0236322b
permissions -rw-r--r--
First shot at SDL_SetWindowDragAreas().

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