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