src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 10:50:53 -0800
changeset 5371 fc3d3d580777
parent 5367 71fe70373e58
child 5374 dacb4f5e0dd3
permissions -rw-r--r--
Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
The only reason we need a custom view is to handle right mouse down.

Implemented mouse grabbing, although it's kind of clunky right now. I'll be adding a relative mode that will be smoother soon.
slouken@1933
     1
/*
slouken@1933
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@1933
     4
slouken@1933
     5
    This library is free software; you can redistribute it and/or
slouken@1933
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1933
     7
    License as published by the Free Software Foundation; either
slouken@1933
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1933
     9
slouken@1933
    10
    This library is distributed in the hope that it will be useful,
slouken@1933
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1933
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1933
    13
    Lesser General Public License for more details.
slouken@1933
    14
slouken@1933
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1933
    16
    License along with this library; if not, write to the Free Software
slouken@1933
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1933
    18
slouken@1933
    19
    Sam Lantinga
slouken@1933
    20
    slouken@libsdl.org
slouken@1933
    21
*/
slouken@1933
    22
#include "SDL_config.h"
slouken@1933
    23
slouken@1933
    24
#include "SDL_syswm.h"
slouken@1933
    25
#include "../SDL_sysvideo.h"
slouken@1933
    26
#include "../../events/SDL_keyboard_c.h"
slouken@1933
    27
#include "../../events/SDL_mouse_c.h"
slouken@4673
    28
#include "../../events/SDL_touch_c.h"
slouken@1933
    29
#include "../../events/SDL_windowevents_c.h"
slouken@1933
    30
#include "SDL_cocoavideo.h"
eligottlieb@4811
    31
#include "SDL_cocoashape.h"
gzjjgod@5057
    32
#include "SDL_cocoamouse.h"
slouken@1933
    33
slouken@1933
    34
static __inline__ void ConvertNSRect(NSRect *r)
slouken@1933
    35
{
slouken@1933
    36
    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
slouken@1933
    37
}
slouken@1933
    38
slouken@1933
    39
@implementation Cocoa_WindowListener
slouken@1933
    40
slouken@1933
    41
- (void)listen:(SDL_WindowData *)data
slouken@1933
    42
{
slouken@1933
    43
    NSNotificationCenter *center;
slouken@5371
    44
    NSWindow *window = data->nswindow;
slouken@5371
    45
    NSView *view = [window contentView];
slouken@1933
    46
slouken@1933
    47
    _data = data;
slouken@1933
    48
slouken@1933
    49
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    50
slouken@5371
    51
    [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:window];
slouken@5371
    52
    [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
slouken@5371
    53
    [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
slouken@5371
    54
    [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
slouken@5371
    55
    [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5371
    56
    [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
slouken@5371
    57
    [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
slouken@1933
    58
    [center addObserver:self selector:@selector(windowDidHide:) name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    59
    [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
slouken@1933
    60
slouken@5371
    61
    [window setNextResponder:self];
slouken@5371
    62
    [window setAcceptsMouseMovedEvents:YES];
slouken@5371
    63
slouken@5371
    64
    [view setNextResponder:self];
slouken@5371
    65
    [view addTrackingRect:[view visibleRect] owner:self userData:nil assumeInside:NO];
slouken@4927
    66
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
slouken@5371
    67
    [view setAcceptsTouchEvents:YES];
slouken@4925
    68
#endif
slouken@1933
    69
}
slouken@1933
    70
slouken@1933
    71
- (void)close
slouken@1933
    72
{
slouken@1933
    73
    NSNotificationCenter *center;
slouken@5371
    74
    NSWindow *window = _data->nswindow;
slouken@5371
    75
    NSView *view = [window contentView];
slouken@1933
    76
slouken@1933
    77
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    78
slouken@5371
    79
    [center removeObserver:self name:NSWindowDidExposeNotification object:window];
slouken@5371
    80
    [center removeObserver:self name:NSWindowDidMoveNotification object:window];
slouken@5371
    81
    [center removeObserver:self name:NSWindowDidResizeNotification object:window];
slouken@5371
    82
    [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
slouken@5371
    83
    [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5371
    84
    [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
slouken@5371
    85
    [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
slouken@1933
    86
    [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    87
    [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
slouken@5371
    88
slouken@5371
    89
    [window setNextResponder:nil];
slouken@5371
    90
    [view setNextResponder:nil];
slouken@1933
    91
}
slouken@1933
    92
slouken@1933
    93
- (BOOL)windowShouldClose:(id)sender
slouken@1933
    94
{
slouken@3685
    95
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
    96
    return NO;
slouken@1933
    97
}
slouken@1933
    98
slouken@1933
    99
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
   100
{
slouken@3685
   101
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
   102
}
slouken@1933
   103
slouken@1933
   104
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
   105
{
slouken@1933
   106
    int x, y;
slouken@3688
   107
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
slouken@1933
   108
    ConvertNSRect(&rect);
slouken@3507
   109
    x = (int)rect.origin.x;
slouken@3507
   110
    y = (int)rect.origin.y;
slouken@3685
   111
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   112
}
slouken@1933
   113
slouken@1933
   114
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   115
{
slouken@1933
   116
    int w, h;
slouken@3688
   117
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
slouken@1933
   118
    w = (int)rect.size.width;
slouken@1933
   119
    h = (int)rect.size.height;
slouken@4937
   120
    if (SDL_IsShapedWindow(_data->window))
slouken@4937
   121
        Cocoa_ResizeWindowShape(_data->window);
slouken@3685
   122
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@1933
   123
}
slouken@1933
   124
slouken@1933
   125
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   126
{
slouken@3685
   127
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   128
}
slouken@1933
   129
slouken@1933
   130
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   131
{
slouken@3685
   132
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   133
}
slouken@1933
   134
slouken@1933
   135
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   136
{
slouken@5367
   137
    SDL_Window *window = _data->window;
slouken@5367
   138
slouken@3688
   139
    /* We're going to get keyboard events, since we're key. */
slouken@5367
   140
    SDL_SetKeyboardFocus(window);
slouken@5367
   141
slouken@5367
   142
    /* If we just gained focus we need the updated mouse position */
slouken@5371
   143
    if (SDL_GetMouseFocus() == window) {
slouken@5367
   144
        NSPoint point;
slouken@5367
   145
        point = [NSEvent mouseLocation];
slouken@5367
   146
        point = [_data->nswindow convertScreenToBase:point];
slouken@5367
   147
        point.y = window->h - point.y;
slouken@5367
   148
        SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
slouken@5367
   149
    }
slouken@1962
   150
slouken@4503
   151
    /* Check to see if someone updated the clipboard */
slouken@4503
   152
    Cocoa_CheckClipboardUpdate(_data->videodata);
slouken@1933
   153
}
slouken@1933
   154
slouken@1933
   155
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   156
{
slouken@2059
   157
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   158
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   159
        SDL_SetMouseFocus(NULL);
slouken@2059
   160
    }
slouken@2059
   161
slouken@2059
   162
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   163
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   164
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   165
    }
slouken@1933
   166
}
slouken@1933
   167
slouken@1933
   168
- (void)windowDidHide:(NSNotification *)aNotification
slouken@1933
   169
{
slouken@3685
   170
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1933
   171
}
slouken@1933
   172
slouken@1933
   173
- (void)windowDidUnhide:(NSNotification *)aNotification
slouken@1933
   174
{
slouken@3685
   175
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1933
   176
}
slouken@1933
   177
slouken@1933
   178
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   179
{
slouken@1959
   180
    int button;
slouken@1933
   181
slouken@1959
   182
    switch ([theEvent buttonNumber]) {
slouken@1959
   183
    case 0:
slouken@1959
   184
        button = SDL_BUTTON_LEFT;
slouken@1959
   185
        break;
slouken@1959
   186
    case 1:
slouken@1959
   187
        button = SDL_BUTTON_RIGHT;
slouken@1959
   188
        break;
slouken@1959
   189
    case 2:
slouken@1959
   190
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   191
        break;
slouken@1959
   192
    default:
slouken@5061
   193
        button = [theEvent buttonNumber] + 1;
slouken@1959
   194
        break;
slouken@1959
   195
    }
slouken@4484
   196
    SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
slouken@1933
   197
}
slouken@1933
   198
slouken@1933
   199
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   200
{
slouken@1959
   201
    [self mouseDown:theEvent];
slouken@1933
   202
}
slouken@1933
   203
slouken@1933
   204
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   205
{
slouken@1959
   206
    [self mouseDown:theEvent];
slouken@1933
   207
}
slouken@1933
   208
slouken@1933
   209
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   210
{
slouken@1959
   211
    int button;
slouken@1933
   212
slouken@1959
   213
    switch ([theEvent buttonNumber]) {
slouken@1959
   214
    case 0:
slouken@1959
   215
        button = SDL_BUTTON_LEFT;
slouken@1959
   216
        break;
slouken@1959
   217
    case 1:
slouken@1959
   218
        button = SDL_BUTTON_RIGHT;
slouken@1959
   219
        break;
slouken@1959
   220
    case 2:
slouken@1959
   221
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   222
        break;
slouken@1959
   223
    default:
slouken@5061
   224
        button = [theEvent buttonNumber] + 1;
slouken@1959
   225
        break;
slouken@1959
   226
    }
slouken@4484
   227
    SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
slouken@1933
   228
}
slouken@1933
   229
slouken@1933
   230
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   231
{
slouken@1959
   232
    [self mouseUp:theEvent];
slouken@1933
   233
}
slouken@1933
   234
slouken@1933
   235
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   236
{
slouken@1959
   237
    [self mouseUp:theEvent];
slouken@1933
   238
}
slouken@1933
   239
slouken@5371
   240
- (void)mouseEntered:(NSEvent *)theEvent
slouken@5371
   241
{
slouken@5371
   242
    SDL_SetMouseFocus(_data->window);
slouken@5371
   243
}
slouken@5371
   244
slouken@5371
   245
- (void)mouseExited:(NSEvent *)theEvent
slouken@5371
   246
{
slouken@5371
   247
    SDL_Window *window = _data->window;
slouken@5371
   248
slouken@5371
   249
    if (SDL_GetMouseFocus() == window) {
slouken@5371
   250
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@5371
   251
            int x, y;
slouken@5371
   252
            NSPoint point;
slouken@5371
   253
            CGPoint cgpoint;
slouken@5371
   254
slouken@5371
   255
            point = [theEvent locationInWindow];
slouken@5371
   256
            point.y = window->h - point.y;
slouken@5371
   257
slouken@5371
   258
            SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
slouken@5371
   259
            SDL_GetMouseState(&x, &y);
slouken@5371
   260
            cgpoint.x = window->x + x;
slouken@5371
   261
            cgpoint.y = window->y + y;
slouken@5371
   262
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
   263
        } else {
slouken@5371
   264
            SDL_SetMouseFocus(NULL);
slouken@5371
   265
        }
slouken@5371
   266
    }
slouken@5371
   267
}
slouken@5371
   268
slouken@1933
   269
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   270
{
slouken@3685
   271
    SDL_Window *window = _data->window;
slouken@1933
   272
slouken@5371
   273
#ifdef RELATIVE_MOTION
slouken@5371
   274
    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
gzjjgod@5059
   275
        return;
slouken@5371
   276
    }
slouken@5371
   277
#endif
gzjjgod@5059
   278
slouken@5371
   279
    if (SDL_GetMouseFocus() == window) {
slouken@5371
   280
        NSPoint point;
slouken@5371
   281
slouken@5371
   282
        point = [theEvent locationInWindow];
slouken@5371
   283
        point.y = window->h - point.y;
slouken@5371
   284
slouken@4484
   285
        SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
slouken@2059
   286
    }
slouken@1933
   287
}
slouken@1933
   288
slouken@1957
   289
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   290
{
slouken@1957
   291
    [self mouseMoved:theEvent];
slouken@1957
   292
}
slouken@1957
   293
slouken@1958
   294
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   295
{
slouken@1958
   296
    [self mouseMoved:theEvent];
slouken@1958
   297
}
slouken@1958
   298
slouken@1958
   299
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   300
{
slouken@1958
   301
    [self mouseMoved:theEvent];
slouken@1958
   302
}
slouken@1958
   303
slouken@1933
   304
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   305
{
gzjjgod@5057
   306
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   307
}
slouken@3688
   308
slouken@4673
   309
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   310
{
slouken@4673
   311
    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
slouken@4673
   312
}
slouken@4673
   313
slouken@4673
   314
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   315
{
slouken@4673
   316
    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
slouken@4673
   317
}
slouken@4673
   318
slouken@4673
   319
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   320
{
slouken@4673
   321
    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
slouken@4673
   322
}
slouken@4673
   323
slouken@4673
   324
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
   325
{
slouken@4673
   326
    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
slouken@4673
   327
}
slouken@4673
   328
slouken@4673
   329
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
slouken@4673
   330
{
slouken@4927
   331
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
slouken@4680
   332
    NSSet *touches = 0;
slouken@4680
   333
    NSEnumerator *enumerator;
slouken@4680
   334
    NSTouch *touch;
slouken@4673
   335
slouken@4680
   336
    switch (type) {
slouken@4680
   337
        case COCOA_TOUCH_DOWN:
slouken@4680
   338
            touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
slouken@4680
   339
            break;
slouken@4680
   340
        case COCOA_TOUCH_UP:
slouken@4680
   341
        case COCOA_TOUCH_CANCELLED:
slouken@4680
   342
            touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
slouken@4680
   343
            break;
slouken@4680
   344
        case COCOA_TOUCH_MOVE:
slouken@4680
   345
            touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
slouken@4680
   346
            break;
slouken@4680
   347
    }
slouken@4680
   348
slouken@4680
   349
    enumerator = [touches objectEnumerator];
slouken@4680
   350
    touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   351
    while (touch) {
slouken@4680
   352
        SDL_TouchID touchId = (SDL_TouchID)[touch device];
slouken@4673
   353
        if (!SDL_GetTouch(touchId)) {
slouken@4673
   354
            SDL_Touch touch;
slouken@4673
   355
slouken@4673
   356
            touch.id = touchId;
slouken@4673
   357
            touch.x_min = 0;
slouken@4673
   358
            touch.x_max = 1;
jimtla@4678
   359
            touch.native_xres = touch.x_max - touch.x_min;
slouken@4673
   360
            touch.y_min = 0;
slouken@4673
   361
            touch.y_max = 1;
jimtla@4678
   362
            touch.native_yres = touch.y_max - touch.y_min;
slouken@4673
   363
            touch.pressure_min = 0;
slouken@4673
   364
            touch.pressure_max = 1;
jimtla@4678
   365
            touch.native_pressureres = touch.pressure_max - touch.pressure_min;
slouken@4673
   366
            
slouken@4673
   367
            if (SDL_AddTouch(&touch, "") < 0) {
slouken@4680
   368
                return;
slouken@4673
   369
            }
slouken@4673
   370
        } 
slouken@4687
   371
slouken@4687
   372
        SDL_FingerID fingerId = (SDL_FingerID)[touch identity];
slouken@4673
   373
        float x = [touch normalizedPosition].x;
slouken@4673
   374
        float y = [touch normalizedPosition].y;
slouken@5261
   375
        /* Make the origin the upper left instead of the lower left */
slouken@5261
   376
        y = 1.0f - y;
slouken@4687
   377
slouken@4673
   378
        switch (type) {
slouken@4673
   379
        case COCOA_TOUCH_DOWN:
slouken@4673
   380
            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
slouken@4673
   381
            break;
slouken@4673
   382
        case COCOA_TOUCH_UP:
slouken@4673
   383
        case COCOA_TOUCH_CANCELLED:
slouken@4673
   384
            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   385
            break;
slouken@4673
   386
        case COCOA_TOUCH_MOVE:
slouken@4673
   387
            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   388
            break;
slouken@4673
   389
        }
slouken@4673
   390
        
slouken@4673
   391
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   392
    }
slouken@4927
   393
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 */
slouken@1933
   394
}
slouken@1933
   395
slouken@1933
   396
@end
slouken@1933
   397
slouken@1973
   398
@interface SDLWindow : NSWindow
slouken@1973
   399
/* These are needed for borderless/fullscreen windows */
slouken@1973
   400
- (BOOL)canBecomeKeyWindow;
slouken@1973
   401
- (BOOL)canBecomeMainWindow;
slouken@1973
   402
@end
slouken@1973
   403
slouken@1973
   404
@implementation SDLWindow
slouken@1973
   405
- (BOOL)canBecomeKeyWindow
slouken@1973
   406
{
slouken@1973
   407
    return YES;
slouken@1973
   408
}
slouken@1973
   409
slouken@1973
   410
- (BOOL)canBecomeMainWindow
slouken@1973
   411
{
slouken@1973
   412
    return YES;
slouken@1973
   413
}
slouken@1973
   414
@end
slouken@1973
   415
slouken@5371
   416
@interface SDLView : NSView { }
gzjjgod@4915
   417
@end
gzjjgod@4915
   418
gzjjgod@4915
   419
@implementation SDLView
gzjjgod@4915
   420
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
   421
{
slouken@5371
   422
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
   423
}
gzjjgod@4915
   424
@end
gzjjgod@4915
   425
slouken@5249
   426
static unsigned int
slouken@5305
   427
GetWindowStyle(SDL_Window * window)
slouken@5249
   428
{
slouken@5249
   429
    unsigned int style;
slouken@5249
   430
slouken@5305
   431
	if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5249
   432
        style = NSBorderlessWindowMask;
slouken@5305
   433
	} else {
slouken@5305
   434
		if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@5305
   435
			style = NSBorderlessWindowMask;
slouken@5305
   436
		} else {
slouken@5305
   437
			style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@5305
   438
		}
slouken@5305
   439
		if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@5305
   440
			style |= NSResizableWindowMask;
slouken@5305
   441
		}
slouken@5305
   442
	}
slouken@5249
   443
    return style;
slouken@5249
   444
}
slouken@5249
   445
slouken@1933
   446
static int
slouken@1951
   447
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   448
{
slouken@1933
   449
    NSAutoreleasePool *pool;
slouken@1951
   450
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
   451
    SDL_WindowData *data;
slouken@1933
   452
slouken@1933
   453
    /* Allocate the window data */
slouken@5249
   454
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
   455
    if (!data) {
slouken@1933
   456
        SDL_OutOfMemory();
slouken@1933
   457
        return -1;
slouken@1933
   458
    }
slouken@3685
   459
    data->window = window;
slouken@3688
   460
    data->nswindow = nswindow;
slouken@1933
   461
    data->created = created;
slouken@1951
   462
    data->videodata = videodata;
slouken@1933
   463
slouken@1933
   464
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   465
slouken@1933
   466
    /* Create an event listener for the window */
slouken@1933
   467
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@1933
   468
slouken@1933
   469
    /* Fill in the SDL window with the window data */
slouken@1933
   470
    {
slouken@1933
   471
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@5371
   472
        NSView *contentView = [[SDLView alloc] initWithFrame:rect];
gzjjgod@4915
   473
        [nswindow setContentView: contentView];
gzjjgod@4915
   474
        [contentView release];
gzjjgod@4915
   475
slouken@1933
   476
        ConvertNSRect(&rect);
slouken@5261
   477
        window->x = (int)rect.origin.x;
slouken@5261
   478
        window->y = (int)rect.origin.y;
slouken@5261
   479
        window->w = (int)rect.size.width;
slouken@5261
   480
        window->h = (int)rect.size.height;
slouken@1933
   481
    }
slouken@5371
   482
slouken@5371
   483
    /* Set up the listener after we create the view */
slouken@5371
   484
    [data->listener listen:data];
slouken@5371
   485
slouken@1933
   486
    if ([nswindow isVisible]) {
slouken@1933
   487
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   488
    } else {
slouken@1933
   489
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   490
    }
slouken@1933
   491
    {
slouken@1933
   492
        unsigned int style = [nswindow styleMask];
slouken@1933
   493
slouken@1956
   494
        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
slouken@1933
   495
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   496
        } else {
slouken@1933
   497
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   498
        }
slouken@1933
   499
        if (style & NSResizableWindowMask) {
slouken@1933
   500
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   501
        } else {
slouken@1933
   502
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   503
        }
slouken@1933
   504
    }
slouken@1933
   505
    if ([nswindow isZoomed]) {
slouken@1933
   506
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   507
    } else {
slouken@1933
   508
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   509
    }
slouken@1933
   510
    if ([nswindow isMiniaturized]) {
slouken@1933
   511
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   512
    } else {
slouken@1933
   513
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   514
    }
slouken@1933
   515
    if ([nswindow isKeyWindow]) {
slouken@1933
   516
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   517
        SDL_SetKeyboardFocus(data->window);
slouken@1933
   518
slouken@1933
   519
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1933
   520
            /* FIXME */
slouken@1933
   521
        }
slouken@1933
   522
    }
slouken@1933
   523
slouken@1933
   524
    /* All done! */
slouken@1933
   525
    [pool release];
slouken@1933
   526
    window->driverdata = data;
slouken@1933
   527
    return 0;
slouken@1933
   528
}
slouken@1933
   529
slouken@1933
   530
int
slouken@1933
   531
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   532
{
slouken@3506
   533
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   534
    NSWindow *nswindow;
slouken@5246
   535
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1933
   536
    NSRect rect;
slouken@3528
   537
    SDL_Rect bounds;
slouken@1933
   538
    unsigned int style;
slouken@1933
   539
slouken@3528
   540
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@5249
   541
    if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@3528
   542
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@5246
   543
    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
slouken@3528
   544
        rect.origin.x = bounds.x;
slouken@3528
   545
    } else {
slouken@5246
   546
        rect.origin.x = window->x;
slouken@1933
   547
    }
slouken@5249
   548
    if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@3528
   549
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@5246
   550
    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->y)) {
slouken@3528
   551
        rect.origin.y = bounds.y;
slouken@3528
   552
    } else {
slouken@5246
   553
        rect.origin.y = window->y;
slouken@1933
   554
    }
slouken@1933
   555
    rect.size.width = window->w;
slouken@1933
   556
    rect.size.height = window->h;
slouken@1933
   557
    ConvertNSRect(&rect);
slouken@1933
   558
slouken@5305
   559
    style = GetWindowStyle(window);
slouken@1933
   560
slouken@3506
   561
    /* Figure out which screen to place this window */
slouken@3506
   562
    NSArray *screens = [NSScreen screens];
slouken@3506
   563
    NSScreen *screen = nil;
slouken@3506
   564
    NSScreen *candidate;
slouken@3525
   565
    int i, count = [screens count];
slouken@3525
   566
    for (i = 0; i < count; ++i) {
slouken@3529
   567
        candidate = [screens objectAtIndex:i];
slouken@3506
   568
        NSRect screenRect = [candidate frame];
slouken@3506
   569
        if (rect.origin.x >= screenRect.origin.x &&
slouken@3506
   570
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@3506
   571
            rect.origin.y >= screenRect.origin.y &&
slouken@3506
   572
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@3506
   573
            screen = candidate;
slouken@3506
   574
            rect.origin.x -= screenRect.origin.x;
slouken@3506
   575
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   576
        }
slouken@3506
   577
    }
slouken@3506
   578
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
slouken@1933
   579
slouken@1933
   580
    [pool release];
slouken@1933
   581
slouken@1951
   582
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@1933
   583
        [nswindow release];
slouken@1933
   584
        return -1;
slouken@1933
   585
    }
slouken@1933
   586
    return 0;
slouken@1933
   587
}
slouken@1933
   588
slouken@1933
   589
int
slouken@1933
   590
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   591
{
slouken@1933
   592
    NSAutoreleasePool *pool;
slouken@1933
   593
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   594
    NSString *title;
slouken@1933
   595
slouken@1933
   596
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   597
slouken@1933
   598
    /* Query the title from the existing window */
slouken@1933
   599
    title = [nswindow title];
slouken@1933
   600
    if (title) {
slouken@1933
   601
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   602
    }
slouken@1933
   603
slouken@1933
   604
    [pool release];
slouken@1933
   605
slouken@1951
   606
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   607
}
slouken@1933
   608
slouken@1933
   609
void
slouken@1933
   610
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   611
{
slouken@1937
   612
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   613
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   614
    NSString *string;
slouken@1933
   615
slouken@1956
   616
    if(window->title) {
slouken@1956
   617
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1956
   618
    } else {
slouken@1956
   619
        string = [[NSString alloc] init];
slouken@1956
   620
    }
slouken@1933
   621
    [nswindow setTitle:string];
slouken@1933
   622
    [string release];
slouken@1956
   623
slouken@1937
   624
    [pool release];
slouken@1933
   625
}
slouken@1933
   626
slouken@1933
   627
void
slouken@1933
   628
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   629
{
slouken@1937
   630
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   631
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@5246
   632
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1933
   633
    NSRect rect;
slouken@3528
   634
    SDL_Rect bounds;
slouken@1933
   635
slouken@3528
   636
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@5249
   637
    if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@3528
   638
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3075
   639
    } else {
slouken@5246
   640
        rect.origin.x = window->x;
slouken@3075
   641
    }
slouken@5249
   642
    if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@3528
   643
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3075
   644
    } else {
slouken@5246
   645
        rect.origin.y = window->y;
slouken@3075
   646
    }
slouken@1933
   647
    rect.size.width = window->w;
slouken@1933
   648
    rect.size.height = window->h;
slouken@1933
   649
    ConvertNSRect(&rect);
slouken@1933
   650
    rect = [nswindow frameRectForContentRect:rect];
slouken@1933
   651
    [nswindow setFrameOrigin:rect.origin];
slouken@1937
   652
    [pool release];
slouken@1933
   653
}
slouken@1933
   654
slouken@1933
   655
void
slouken@1933
   656
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   657
{
slouken@1937
   658
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   659
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   660
    NSSize size;
slouken@1933
   661
slouken@1933
   662
    size.width = window->w;
slouken@1933
   663
    size.height = window->h;
slouken@1933
   664
    [nswindow setContentSize:size];
slouken@1937
   665
    [pool release];
slouken@1933
   666
}
slouken@1933
   667
slouken@1933
   668
void
slouken@1933
   669
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   670
{
slouken@1937
   671
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   672
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   673
slouken@1956
   674
    if (![nswindow isMiniaturized]) {
slouken@1956
   675
        [nswindow makeKeyAndOrderFront:nil];
slouken@1956
   676
    }
slouken@1937
   677
    [pool release];
slouken@1933
   678
}
slouken@1933
   679
slouken@1933
   680
void
slouken@1933
   681
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   682
{
slouken@1956
   683
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   684
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   685
slouken@1956
   686
    [nswindow orderOut:nil];
slouken@1956
   687
    [pool release];
slouken@1933
   688
}
slouken@1933
   689
slouken@1933
   690
void
slouken@1933
   691
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   692
{
slouken@1937
   693
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   694
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   695
slouken@1933
   696
    [nswindow makeKeyAndOrderFront:nil];
slouken@1937
   697
    [pool release];
slouken@1933
   698
}
slouken@1933
   699
slouken@1933
   700
void
slouken@1933
   701
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   702
{
slouken@1937
   703
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   704
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   705
slouken@1956
   706
    [nswindow zoom:nil];
slouken@1937
   707
    [pool release];
slouken@1933
   708
}
slouken@1933
   709
slouken@1933
   710
void
slouken@1933
   711
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   712
{
slouken@1937
   713
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   714
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   715
slouken@1956
   716
    [nswindow miniaturize:nil];
slouken@1937
   717
    [pool release];
slouken@1933
   718
}
slouken@1933
   719
slouken@1933
   720
void
slouken@1933
   721
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   722
{
slouken@1956
   723
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   724
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   725
slouken@1956
   726
    if ([nswindow isMiniaturized]) {
slouken@1956
   727
        [nswindow deminiaturize:nil];
slouken@1956
   728
    } else if ([nswindow isZoomed]) {
slouken@1956
   729
        [nswindow zoom:nil];
slouken@1956
   730
    }
slouken@1956
   731
    [pool release];
slouken@1933
   732
}
slouken@1933
   733
slouken@1933
   734
void
slouken@5305
   735
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@5249
   736
{
slouken@5249
   737
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5249
   738
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5249
   739
    NSWindow *nswindow = data->nswindow;
slouken@5249
   740
    NSRect rect;
slouken@5249
   741
slouken@5305
   742
    if (fullscreen) {
slouken@5249
   743
        SDL_Rect bounds;
slouken@5249
   744
slouken@5249
   745
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@5249
   746
        rect.origin.x = bounds.x;
slouken@5249
   747
        rect.origin.y = bounds.y;
slouken@5249
   748
        rect.size.width = bounds.w;
slouken@5249
   749
        rect.size.height = bounds.h;
slouken@5249
   750
        ConvertNSRect(&rect);
slouken@5249
   751
slouken@5361
   752
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@5361
   753
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@5361
   754
        }
slouken@5361
   755
        [nswindow setFrameOrigin:rect.origin];
slouken@5261
   756
        [nswindow setContentSize:rect.size];
slouken@5249
   757
    } else {
slouken@5361
   758
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@5361
   759
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)GetWindowStyle(window)];
slouken@5361
   760
        }
slouken@5249
   761
slouken@5261
   762
        // This doesn't seem to do anything...
slouken@5261
   763
        //[nswindow setFrameOrigin:origin];
slouken@5249
   764
    }
slouken@5249
   765
slouken@5249
   766
#ifdef FULLSCREEN_TOGGLEABLE
slouken@5305
   767
    if (fullscreen) {
slouken@5249
   768
        /* OpenGL is rendering to the window, so make it visible! */
slouken@5249
   769
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@5249
   770
    } else {
slouken@5249
   771
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@5249
   772
    }
slouken@5249
   773
#endif
slouken@5249
   774
    [nswindow makeKeyAndOrderFront:nil];
slouken@5249
   775
slouken@5249
   776
    [pool release];
slouken@5249
   777
}
slouken@5249
   778
slouken@5371
   779
NSPoint origin;
slouken@5249
   780
void
slouken@1933
   781
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   782
{
slouken@5371
   783
#ifdef RELATIVE_MOTION
slouken@5371
   784
    /* FIXME: work in progress
slouken@5371
   785
        You set relative mode by using the following code in conjunction with
slouken@5371
   786
        CGDisplayHideCursor(kCGDirectMainDisplay) and
slouken@5371
   787
        CGDisplayShowCursor(kCGDirectMainDisplay)
slouken@5371
   788
    */
slouken@1933
   789
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1933
   790
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@5371
   791
        CGAssociateMouseAndMouseCursorPosition(NO);
slouken@1933
   792
    } else {
slouken@5371
   793
        CGAssociateMouseAndMouseCursorPosition(YES);
slouken@1933
   794
    }
slouken@5371
   795
#else
slouken@5371
   796
    /* Move the cursor to the nearest point in the window */
slouken@5371
   797
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@5371
   798
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@5371
   799
        int x, y;
slouken@5371
   800
        CGPoint cgpoint;
slouken@5371
   801
slouken@5371
   802
        SDL_GetMouseState(&x, &y);
slouken@5371
   803
        cgpoint.x = window->x + x;
slouken@5371
   804
        cgpoint.y = window->y + y;
slouken@5371
   805
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
   806
    }
slouken@5371
   807
#endif
slouken@1933
   808
}
slouken@1933
   809
slouken@1933
   810
void
slouken@1933
   811
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   812
{
slouken@1937
   813
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   814
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   815
slouken@1933
   816
    if (data) {
slouken@1933
   817
        [data->listener close];
slouken@1933
   818
        [data->listener release];
slouken@1933
   819
        if (data->created) {
slouken@3688
   820
            [data->nswindow close];
slouken@1933
   821
        }
slouken@1933
   822
        SDL_free(data);
slouken@1933
   823
    }
slouken@1937
   824
    [pool release];
slouken@1933
   825
}
slouken@1933
   826
slouken@1933
   827
SDL_bool
slouken@1933
   828
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   829
{
slouken@4900
   830
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   831
slouken@1933
   832
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
   833
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
   834
        info->info.cocoa.window = nswindow;
slouken@1933
   835
        return SDL_TRUE;
slouken@1933
   836
    } else {
slouken@1933
   837
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
   838
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
   839
        return SDL_FALSE;
slouken@1933
   840
    }
slouken@1933
   841
}
slouken@1933
   842
slouken@1933
   843
/* vi: set ts=4 sw=4 expandtab: */