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