src/video/cocoa/SDL_cocoawindow.m
author Jjgod Jiang <gzjjgod@gmail.com>
Sun, 09 May 2010 12:58:58 +0800
changeset 4915 834ce48a19c2
parent 4902 50d0bff24d81
child 4925 514f811a4887
permissions -rw-r--r--
Fix right mouse event handling in Cocoa

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