src/video/cocoa/SDL_cocoawindow.m
author jimtla
Fri, 30 Jul 2010 23:18:35 +0400
changeset 4676 99b4560b7aa1
parent 4675 641c13b0ce5f
child 4678 f8431f66613d
permissions -rw-r--r--
Upgraded touchId/fingerId to long. Changed position variables to floats.
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@4673
   294
    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
slouken@4673
   295
slouken@4673
   296
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@4673
   297
    NSTouch *touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   298
    while (touch) {
slouken@4673
   299
        long touchId = (long)[touch device];
slouken@4673
   300
        if (!SDL_GetTouch(touchId)) {
jimtla@4676
   301
	  printf("Adding touch: %li\n",touchId);
slouken@4673
   302
            SDL_Touch touch;
slouken@4673
   303
slouken@4673
   304
            touch.id = touchId;
slouken@4673
   305
            touch.x_min = 0;
slouken@4673
   306
            touch.x_max = 1;
slouken@4673
   307
            touch.xres = touch.x_max - touch.x_min;
slouken@4673
   308
            touch.y_min = 0;
slouken@4673
   309
            touch.y_max = 1;
slouken@4673
   310
            touch.yres = touch.y_max - touch.y_min;
slouken@4673
   311
            touch.pressure_min = 0;
slouken@4673
   312
            touch.pressure_max = 1;
slouken@4673
   313
            touch.pressureres = touch.pressure_max - touch.pressure_min;
slouken@4673
   314
            
slouken@4673
   315
            if (SDL_AddTouch(&touch, "") < 0) {
jim@4674
   316
                continue;
slouken@4673
   317
            }
jimtla@4676
   318
	    printf("Success, added touch: %lin",touchId);
slouken@4673
   319
        } 
slouken@4673
   320
        float x = [touch normalizedPosition].x;
slouken@4673
   321
        float y = [touch normalizedPosition].y;
slouken@4673
   322
        long fingerId = (long)[touch identity];
slouken@4673
   323
        switch (type) {
slouken@4673
   324
        case COCOA_TOUCH_DOWN:
slouken@4673
   325
            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
slouken@4673
   326
            break;
slouken@4673
   327
        case COCOA_TOUCH_UP:
slouken@4673
   328
        case COCOA_TOUCH_CANCELLED:
slouken@4673
   329
            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   330
            break;
slouken@4673
   331
        case COCOA_TOUCH_MOVE:
slouken@4673
   332
            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   333
            break;
slouken@4673
   334
        }
slouken@4673
   335
        
slouken@4673
   336
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   337
    }
slouken@4673
   338
}
slouken@4673
   339
slouken@1933
   340
@end
slouken@1933
   341
slouken@1973
   342
@interface SDLWindow : NSWindow
slouken@1973
   343
/* These are needed for borderless/fullscreen windows */
slouken@1973
   344
- (BOOL)canBecomeKeyWindow;
slouken@1973
   345
- (BOOL)canBecomeMainWindow;
slouken@1973
   346
@end
slouken@1973
   347
slouken@1973
   348
@implementation SDLWindow
slouken@1973
   349
- (BOOL)canBecomeKeyWindow
slouken@1973
   350
{
slouken@1973
   351
    return YES;
slouken@1973
   352
}
slouken@1973
   353
slouken@1973
   354
- (BOOL)canBecomeMainWindow
slouken@1973
   355
{
slouken@1973
   356
    return YES;
slouken@1973
   357
}
slouken@1973
   358
@end
slouken@1973
   359
slouken@1933
   360
static int
slouken@1951
   361
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   362
{
slouken@1933
   363
    NSAutoreleasePool *pool;
slouken@1951
   364
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3685
   365
    SDL_VideoDisplay *display = window->display;
slouken@3530
   366
    SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
slouken@1933
   367
    SDL_WindowData *data;
slouken@1933
   368
slouken@1933
   369
    /* Allocate the window data */
slouken@1933
   370
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1933
   371
    if (!data) {
slouken@1933
   372
        SDL_OutOfMemory();
slouken@1933
   373
        return -1;
slouken@1933
   374
    }
slouken@3685
   375
    data->window = window;
slouken@3688
   376
    data->nswindow = nswindow;
slouken@1933
   377
    data->created = created;
slouken@3506
   378
    data->display = displaydata->display;
slouken@1951
   379
    data->videodata = videodata;
slouken@1933
   380
slouken@1933
   381
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   382
slouken@1933
   383
    /* Create an event listener for the window */
slouken@1933
   384
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@1933
   385
    [data->listener listen:data];
slouken@1933
   386
slouken@1933
   387
    /* Fill in the SDL window with the window data */
slouken@1933
   388
    {
slouken@3530
   389
        SDL_Rect bounds;
slouken@1933
   390
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   391
        ConvertNSRect(&rect);
slouken@3530
   392
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3530
   393
        window->x = (int)rect.origin.x - bounds.x;
slouken@3530
   394
        window->y = (int)rect.origin.y - bounds.y;
slouken@1933
   395
        window->w = (int)rect.size.width;
slouken@1933
   396
        window->h = (int)rect.size.height;
slouken@1933
   397
    }
slouken@1933
   398
    if ([nswindow isVisible]) {
slouken@1933
   399
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   400
    } else {
slouken@1933
   401
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   402
    }
slouken@1933
   403
    {
slouken@1933
   404
        unsigned int style = [nswindow styleMask];
slouken@1933
   405
slouken@1956
   406
        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
slouken@1933
   407
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   408
        } else {
slouken@1933
   409
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   410
        }
slouken@1933
   411
        if (style & NSResizableWindowMask) {
slouken@1933
   412
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   413
        } else {
slouken@1933
   414
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   415
        }
slouken@1933
   416
    }
slouken@1933
   417
    if ([nswindow isZoomed]) {
slouken@1933
   418
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   419
    } else {
slouken@1933
   420
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   421
    }
slouken@1933
   422
    if ([nswindow isMiniaturized]) {
slouken@1933
   423
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   424
    } else {
slouken@1933
   425
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   426
    }
slouken@1933
   427
    if ([nswindow isKeyWindow]) {
slouken@1933
   428
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   429
        SDL_SetKeyboardFocus(data->window);
slouken@1933
   430
slouken@1933
   431
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1933
   432
            /* FIXME */
slouken@1933
   433
        }
slouken@1933
   434
    }
slouken@1933
   435
slouken@1933
   436
    /* All done! */
slouken@1933
   437
    [pool release];
slouken@1933
   438
    window->driverdata = data;
slouken@1933
   439
    return 0;
slouken@1933
   440
}
slouken@1933
   441
slouken@1933
   442
int
slouken@1933
   443
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   444
{
slouken@3506
   445
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   446
    NSWindow *nswindow;
slouken@3685
   447
    SDL_VideoDisplay *display = window->display;
slouken@1933
   448
    NSRect rect;
slouken@3528
   449
    SDL_Rect bounds;
slouken@1933
   450
    unsigned int style;
slouken@1933
   451
slouken@3528
   452
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   453
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   454
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   455
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3528
   456
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   457
        rect.origin.x = bounds.x;
slouken@3528
   458
    } else {
slouken@3530
   459
        rect.origin.x = bounds.x + window->x;
slouken@1933
   460
    }
slouken@3075
   461
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   462
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   463
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3528
   464
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   465
        rect.origin.y = bounds.y;
slouken@3528
   466
    } else {
slouken@3530
   467
        rect.origin.y = bounds.y + window->y;
slouken@1933
   468
    }
slouken@1933
   469
    rect.size.width = window->w;
slouken@1933
   470
    rect.size.height = window->h;
slouken@1933
   471
    ConvertNSRect(&rect);
slouken@1933
   472
slouken@1933
   473
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1933
   474
        style = NSBorderlessWindowMask;
slouken@1933
   475
    } else {
slouken@1933
   476
        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@1933
   477
    }
slouken@1933
   478
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1933
   479
        style |= NSResizableWindowMask;
slouken@1933
   480
    }
slouken@1933
   481
slouken@3506
   482
    /* Figure out which screen to place this window */
slouken@3506
   483
    NSArray *screens = [NSScreen screens];
slouken@3506
   484
    NSScreen *screen = nil;
slouken@3506
   485
    NSScreen *candidate;
slouken@3525
   486
    int i, count = [screens count];
slouken@3525
   487
    for (i = 0; i < count; ++i) {
slouken@3529
   488
        candidate = [screens objectAtIndex:i];
slouken@3506
   489
        NSRect screenRect = [candidate frame];
slouken@3506
   490
        if (rect.origin.x >= screenRect.origin.x &&
slouken@3506
   491
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@3506
   492
            rect.origin.y >= screenRect.origin.y &&
slouken@3506
   493
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@3506
   494
            screen = candidate;
slouken@3506
   495
            rect.origin.x -= screenRect.origin.x;
slouken@3506
   496
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   497
        }
slouken@3506
   498
    }
slouken@3506
   499
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
slouken@1933
   500
slouken@1933
   501
    [pool release];
slouken@1933
   502
slouken@1951
   503
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@1933
   504
        [nswindow release];
slouken@1933
   505
        return -1;
slouken@1933
   506
    }
slouken@1933
   507
    return 0;
slouken@1933
   508
}
slouken@1933
   509
slouken@1933
   510
int
slouken@1933
   511
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   512
{
slouken@1933
   513
    NSAutoreleasePool *pool;
slouken@1933
   514
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   515
    NSString *title;
slouken@1933
   516
slouken@1933
   517
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   518
slouken@1933
   519
    /* Query the title from the existing window */
slouken@1933
   520
    title = [nswindow title];
slouken@1933
   521
    if (title) {
slouken@1933
   522
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   523
    }
slouken@1933
   524
slouken@1933
   525
    [pool release];
slouken@1933
   526
slouken@1951
   527
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   528
}
slouken@1933
   529
slouken@1933
   530
void
slouken@1933
   531
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   532
{
slouken@1937
   533
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   534
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   535
    NSString *string;
slouken@1933
   536
slouken@1956
   537
    if(window->title) {
slouken@1956
   538
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1956
   539
    } else {
slouken@1956
   540
        string = [[NSString alloc] init];
slouken@1956
   541
    }
slouken@1933
   542
    [nswindow setTitle:string];
slouken@1933
   543
    [string release];
slouken@1956
   544
slouken@1937
   545
    [pool release];
slouken@1933
   546
}
slouken@1933
   547
slouken@1933
   548
void
slouken@1933
   549
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   550
{
slouken@1937
   551
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   552
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@3685
   553
    SDL_VideoDisplay *display = window->display;
slouken@1933
   554
    NSRect rect;
slouken@3528
   555
    SDL_Rect bounds;
slouken@1933
   556
slouken@3528
   557
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   558
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   559
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   560
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3075
   561
    } else {
slouken@3530
   562
        rect.origin.x = bounds.x + window->x;
slouken@3075
   563
    }
slouken@3075
   564
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   565
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   566
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3075
   567
    } else {
slouken@3530
   568
        rect.origin.y = bounds.y + window->y;
slouken@3075
   569
    }
slouken@1933
   570
    rect.size.width = window->w;
slouken@1933
   571
    rect.size.height = window->h;
slouken@1933
   572
    ConvertNSRect(&rect);
slouken@1933
   573
    rect = [nswindow frameRectForContentRect:rect];
slouken@1933
   574
    [nswindow setFrameOrigin:rect.origin];
slouken@1937
   575
    [pool release];
slouken@1933
   576
}
slouken@1933
   577
slouken@1933
   578
void
slouken@1933
   579
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   580
{
slouken@1937
   581
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   582
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   583
    NSSize size;
slouken@1933
   584
slouken@1933
   585
    size.width = window->w;
slouken@1933
   586
    size.height = window->h;
slouken@1933
   587
    [nswindow setContentSize:size];
slouken@1937
   588
    [pool release];
slouken@1933
   589
}
slouken@1933
   590
slouken@1933
   591
void
slouken@1933
   592
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   593
{
slouken@1937
   594
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   595
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   596
slouken@1956
   597
    if (![nswindow isMiniaturized]) {
slouken@1956
   598
        [nswindow makeKeyAndOrderFront:nil];
slouken@1956
   599
    }
slouken@1937
   600
    [pool release];
slouken@1933
   601
}
slouken@1933
   602
slouken@1933
   603
void
slouken@1933
   604
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   605
{
slouken@1956
   606
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   607
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   608
slouken@1956
   609
    [nswindow orderOut:nil];
slouken@1956
   610
    [pool release];
slouken@1933
   611
}
slouken@1933
   612
slouken@1933
   613
void
slouken@1933
   614
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   615
{
slouken@1937
   616
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   617
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   618
slouken@1933
   619
    [nswindow makeKeyAndOrderFront:nil];
slouken@1937
   620
    [pool release];
slouken@1933
   621
}
slouken@1933
   622
slouken@1933
   623
void
slouken@1933
   624
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   625
{
slouken@1937
   626
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   627
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   628
slouken@1956
   629
    [nswindow zoom:nil];
slouken@1937
   630
    [pool release];
slouken@1933
   631
}
slouken@1933
   632
slouken@1933
   633
void
slouken@1933
   634
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   635
{
slouken@1937
   636
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   637
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   638
slouken@1956
   639
    [nswindow miniaturize:nil];
slouken@1937
   640
    [pool release];
slouken@1933
   641
}
slouken@1933
   642
slouken@1933
   643
void
slouken@1933
   644
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   645
{
slouken@1956
   646
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   647
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   648
slouken@1956
   649
    if ([nswindow isMiniaturized]) {
slouken@1956
   650
        [nswindow deminiaturize:nil];
slouken@1956
   651
    } else if ([nswindow isZoomed]) {
slouken@1956
   652
        [nswindow zoom:nil];
slouken@1956
   653
    }
slouken@1956
   654
    [pool release];
slouken@1933
   655
}
slouken@1933
   656
slouken@1933
   657
void
slouken@1933
   658
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   659
{
slouken@1933
   660
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1933
   661
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1933
   662
        /* FIXME: Grab mouse */
slouken@1933
   663
    } else {
slouken@1933
   664
        /* FIXME: Release mouse */
slouken@1933
   665
    }
slouken@1933
   666
}
slouken@1933
   667
slouken@1933
   668
void
slouken@1933
   669
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   670
{
slouken@1937
   671
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   672
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   673
slouken@1933
   674
    if (data) {
slouken@1933
   675
        [data->listener close];
slouken@1933
   676
        [data->listener release];
slouken@1933
   677
        if (data->created) {
slouken@3688
   678
            [data->nswindow close];
slouken@1933
   679
        }
slouken@1933
   680
        SDL_free(data);
slouken@1933
   681
    }
slouken@1937
   682
    [pool release];
slouken@1933
   683
}
slouken@1933
   684
slouken@1933
   685
SDL_bool
slouken@1933
   686
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   687
{
slouken@4465
   688
    //NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   689
slouken@1933
   690
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1933
   691
        //info->window = nswindow;
slouken@1933
   692
        return SDL_TRUE;
slouken@1933
   693
    } else {
slouken@1933
   694
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
   695
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
   696
        return SDL_FALSE;
slouken@1933
   697
    }
slouken@1933
   698
}
slouken@1933
   699
slouken@1933
   700
/* vi: set ts=4 sw=4 expandtab: */