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