src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jul 2006 05:03:02 +0000
changeset 1933 7ee5297340f7
child 1936 83946ee0ff1f
permissions -rw-r--r--
Implemented Cocoa window support
slouken@1933
     1
/*
slouken@1933
     2
    SDL - Simple DirectMedia Layer
slouken@1933
     3
    Copyright (C) 1997-2006 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@1933
    28
#include "../../events/SDL_windowevents_c.h"
slouken@1933
    29
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@1933
    47
    [_data->window setNextResponder:self];
slouken@1933
    48
    if ([_data->window delegate] != nil) {
slouken@1933
    49
        [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->window];
slouken@1933
    50
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->window];
slouken@1933
    51
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->window];
slouken@1933
    52
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->window];
slouken@1933
    53
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->window];
slouken@1933
    54
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->window];
slouken@1933
    55
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->window];
slouken@1933
    56
    } else {
slouken@1933
    57
        [_data->window 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@1933
    62
    [_data->window setAcceptsMouseMovedEvents:YES];
slouken@1933
    63
}
slouken@1933
    64
slouken@1933
    65
- (void)close
slouken@1933
    66
{
slouken@1933
    67
    NSNotificationCenter *center;
slouken@1933
    68
slouken@1933
    69
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    70
slouken@1933
    71
    [_data->window setNextResponder:nil];
slouken@1933
    72
    if ([_data->window delegate] != self) {
slouken@1933
    73
        [center removeObserver:self name:NSWindowDidExposeNotification object:_data->window];
slouken@1933
    74
        [center removeObserver:self name:NSWindowDidMoveNotification object:_data->window];
slouken@1933
    75
        [center removeObserver:self name:NSWindowDidResizeNotification object:_data->window];
slouken@1933
    76
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->window];
slouken@1933
    77
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->window];
slouken@1933
    78
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->window];
slouken@1933
    79
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->window];
slouken@1933
    80
    } else {
slouken@1933
    81
        [_data->window setDelegate:nil];
slouken@1933
    82
    }
slouken@1933
    83
    [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    84
    [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
slouken@1933
    85
}
slouken@1933
    86
slouken@1933
    87
- (BOOL)windowShouldClose:(id)sender
slouken@1933
    88
{
slouken@1933
    89
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
    90
    return NO;
slouken@1933
    91
}
slouken@1933
    92
slouken@1933
    93
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
    94
{
slouken@1933
    95
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
    96
}
slouken@1933
    97
slouken@1933
    98
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
    99
{
slouken@1933
   100
    int x, y;
slouken@1933
   101
    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
slouken@1933
   102
    ConvertNSRect(&rect);
slouken@1933
   103
    x = (int)rect.origin.x;
slouken@1933
   104
    y = (int)rect.origin.y;
slouken@1933
   105
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   106
}
slouken@1933
   107
slouken@1933
   108
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   109
{
slouken@1933
   110
    int w, h;
slouken@1933
   111
    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
slouken@1933
   112
    w = (int)rect.size.width;
slouken@1933
   113
    h = (int)rect.size.height;
slouken@1933
   114
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@1933
   115
}
slouken@1933
   116
slouken@1933
   117
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   118
{
slouken@1933
   119
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   120
}
slouken@1933
   121
slouken@1933
   122
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   123
{
slouken@1933
   124
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   125
}
slouken@1933
   126
slouken@1933
   127
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   128
{
slouken@1933
   129
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@1933
   130
}
slouken@1933
   131
slouken@1933
   132
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   133
{
slouken@1933
   134
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@1933
   135
}
slouken@1933
   136
slouken@1933
   137
- (void)windowDidHide:(NSNotification *)aNotification
slouken@1933
   138
{
slouken@1933
   139
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1933
   140
}
slouken@1933
   141
slouken@1933
   142
- (void)windowDidUnhide:(NSNotification *)aNotification
slouken@1933
   143
{
slouken@1933
   144
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1933
   145
}
slouken@1933
   146
slouken@1933
   147
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   148
{
slouken@1933
   149
    int index;
slouken@1933
   150
slouken@1933
   151
    index = _data->videodata->mouse;
slouken@1933
   152
    SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@1933
   153
}
slouken@1933
   154
slouken@1933
   155
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   156
{
slouken@1933
   157
    int index;
slouken@1933
   158
slouken@1933
   159
    index = _data->videodata->mouse;
slouken@1933
   160
    SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_RIGHT);
slouken@1933
   161
}
slouken@1933
   162
slouken@1933
   163
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   164
{
slouken@1933
   165
    int index;
slouken@1933
   166
slouken@1933
   167
    index = _data->videodata->mouse;
slouken@1933
   168
    SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_MIDDLE);
slouken@1933
   169
}
slouken@1933
   170
slouken@1933
   171
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   172
{
slouken@1933
   173
    int index;
slouken@1933
   174
slouken@1933
   175
    index = _data->videodata->mouse;
slouken@1933
   176
    SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@1933
   177
}
slouken@1933
   178
slouken@1933
   179
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   180
{
slouken@1933
   181
    int index;
slouken@1933
   182
slouken@1933
   183
    index = _data->videodata->mouse;
slouken@1933
   184
    SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_RIGHT);
slouken@1933
   185
}
slouken@1933
   186
slouken@1933
   187
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   188
{
slouken@1933
   189
    int index;
slouken@1933
   190
slouken@1933
   191
    index = _data->videodata->mouse;
slouken@1933
   192
    SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_MIDDLE);
slouken@1933
   193
}
slouken@1933
   194
slouken@1933
   195
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   196
{
slouken@1933
   197
    int index;
slouken@1933
   198
    SDL_Mouse *mouse;
slouken@1933
   199
    NSPoint point;
slouken@1933
   200
    NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
slouken@1933
   201
slouken@1933
   202
    index = _data->videodata->mouse;
slouken@1933
   203
    mouse = SDL_GetMouse(index);
slouken@1933
   204
    if (mouse->focus != _data->windowID) {
slouken@1933
   205
        SDL_SetMouseFocus(index, _data->windowID);
slouken@1933
   206
    }
slouken@1933
   207
slouken@1933
   208
    point = [NSEvent mouseLocation];
slouken@1933
   209
    point.x = point.x - rect.origin.x;
slouken@1933
   210
    point.y = rect.size.height - (point.y - rect.origin.y);
slouken@1933
   211
    SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y);
slouken@1933
   212
}
slouken@1933
   213
slouken@1933
   214
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   215
{
slouken@1933
   216
fprintf(stderr, "scrollWheel\n");
slouken@1933
   217
}
slouken@1933
   218
slouken@1933
   219
- (void)mouseEntered:(NSEvent *)theEvent
slouken@1933
   220
{
slouken@1933
   221
fprintf(stderr, "mouseEntered\n");
slouken@1933
   222
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_ENTER, 0, 0);
slouken@1933
   223
}
slouken@1933
   224
slouken@1933
   225
- (void)mouseExited:(NSEvent *)theEvent
slouken@1933
   226
{
slouken@1933
   227
fprintf(stderr, "mouseExited\n");
slouken@1933
   228
    SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_LEAVE, 0, 0);
slouken@1933
   229
}
slouken@1933
   230
slouken@1933
   231
- (void)keyDown:(NSEvent *)theEvent
slouken@1933
   232
{
slouken@1933
   233
fprintf(stderr, "keyDown\n");
slouken@1933
   234
}
slouken@1933
   235
slouken@1933
   236
- (void)keyUp:(NSEvent *)theEvent
slouken@1933
   237
{
slouken@1933
   238
fprintf(stderr, "keyUp\n");
slouken@1933
   239
}
slouken@1933
   240
slouken@1933
   241
@end
slouken@1933
   242
slouken@1933
   243
static int
slouken@1933
   244
SetupWindowData(SDL_Window * window, NSWindow *nswindow, BOOL created)
slouken@1933
   245
{
slouken@1933
   246
    NSAutoreleasePool *pool;
slouken@1933
   247
    SDL_WindowData *data;
slouken@1933
   248
slouken@1933
   249
    /* Allocate the window data */
slouken@1933
   250
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1933
   251
    if (!data) {
slouken@1933
   252
        SDL_OutOfMemory();
slouken@1933
   253
        return -1;
slouken@1933
   254
    }
slouken@1933
   255
    data->windowID = window->id;
slouken@1933
   256
    data->window = nswindow;
slouken@1933
   257
    data->created = created;
slouken@1933
   258
    data->videodata = (SDL_VideoData *) SDL_GetVideoDevice()->driverdata;
slouken@1933
   259
slouken@1933
   260
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   261
slouken@1933
   262
    /* Create an event listener for the window */
slouken@1933
   263
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@1933
   264
    [data->listener listen:data];
slouken@1933
   265
slouken@1933
   266
    /* Fill in the SDL window with the window data */
slouken@1933
   267
    {
slouken@1933
   268
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   269
        ConvertNSRect(&rect);
slouken@1933
   270
        window->x = (int)rect.origin.x;
slouken@1933
   271
        window->y = (int)rect.origin.y;
slouken@1933
   272
        window->w = (int)rect.size.width;
slouken@1933
   273
        window->h = (int)rect.size.height;
slouken@1933
   274
    }
slouken@1933
   275
    if ([nswindow isVisible]) {
slouken@1933
   276
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   277
    } else {
slouken@1933
   278
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   279
    }
slouken@1933
   280
    {
slouken@1933
   281
        unsigned int style = [nswindow styleMask];
slouken@1933
   282
slouken@1933
   283
        if (style == NSBorderlessWindowMask) {
slouken@1933
   284
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   285
        } else {
slouken@1933
   286
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   287
        }
slouken@1933
   288
        if (style & NSResizableWindowMask) {
slouken@1933
   289
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   290
        } else {
slouken@1933
   291
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   292
        }
slouken@1933
   293
    }
slouken@1933
   294
    if ([nswindow isZoomed]) {
slouken@1933
   295
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   296
    } else {
slouken@1933
   297
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   298
    }
slouken@1933
   299
    if ([nswindow isMiniaturized]) {
slouken@1933
   300
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   301
    } else {
slouken@1933
   302
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   303
    }
slouken@1933
   304
    if ([nswindow isKeyWindow]) {
slouken@1933
   305
        int index = data->videodata->keyboard;
slouken@1933
   306
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1933
   307
        SDL_SetKeyboardFocus(index, data->windowID);
slouken@1933
   308
slouken@1933
   309
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1933
   310
            /* FIXME */
slouken@1933
   311
        }
slouken@1933
   312
    }
slouken@1933
   313
slouken@1933
   314
    /* All done! */
slouken@1933
   315
    [pool release];
slouken@1933
   316
    window->driverdata = data;
slouken@1933
   317
    return 0;
slouken@1933
   318
}
slouken@1933
   319
slouken@1933
   320
int
slouken@1933
   321
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   322
{
slouken@1933
   323
    NSAutoreleasePool *pool;
slouken@1933
   324
    NSWindow *nswindow;
slouken@1933
   325
    NSRect rect;
slouken@1933
   326
    unsigned int style;
slouken@1933
   327
    NSString *title;
slouken@1933
   328
slouken@1933
   329
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   330
slouken@1933
   331
    if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
slouken@1933
   332
        window->x == SDL_WINDOWPOS_CENTERED) {
slouken@1933
   333
        rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
slouken@1933
   334
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@1933
   335
        rect.origin.x = 0;
slouken@1933
   336
    } else {
slouken@1933
   337
        rect.origin.x = window->x;
slouken@1933
   338
    }
slouken@1933
   339
    if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
slouken@1933
   340
        window->y == SDL_WINDOWPOS_CENTERED) {
slouken@1933
   341
        rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
slouken@1933
   342
    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
slouken@1933
   343
        rect.origin.y = 0;
slouken@1933
   344
    } else {
slouken@1933
   345
        rect.origin.y = window->y;
slouken@1933
   346
    }
slouken@1933
   347
    rect.size.width = window->w;
slouken@1933
   348
    rect.size.height = window->h;
slouken@1933
   349
    ConvertNSRect(&rect);
slouken@1933
   350
slouken@1933
   351
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1933
   352
        style = NSBorderlessWindowMask;
slouken@1933
   353
    } else {
slouken@1933
   354
        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@1933
   355
    }
slouken@1933
   356
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1933
   357
        style |= NSResizableWindowMask;
slouken@1933
   358
    }
slouken@1933
   359
slouken@1933
   360
    nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
slouken@1933
   361
slouken@1933
   362
    if (window->flags & SDL_WINDOW_SHOWN) {
slouken@1933
   363
        [nswindow makeKeyAndOrderFront:nil];
slouken@1933
   364
    }
slouken@1933
   365
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@1933
   366
        [nswindow performZoom:nil];
slouken@1933
   367
    }
slouken@1933
   368
    if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@1933
   369
        [nswindow performMiniaturize:nil];
slouken@1933
   370
    }
slouken@1933
   371
slouken@1933
   372
    if (window->title) {
slouken@1933
   373
        title = [[NSString alloc] initWithUTF8String:window->title];
slouken@1933
   374
        [nswindow setTitle:title];
slouken@1933
   375
        [nswindow setMiniwindowTitle:title];
slouken@1933
   376
        [title release];
slouken@1933
   377
    }
slouken@1933
   378
slouken@1933
   379
    [pool release];
slouken@1933
   380
slouken@1933
   381
    if (SetupWindowData(window, nswindow, YES) < 0) {
slouken@1933
   382
        [nswindow release];
slouken@1933
   383
        [pool release];
slouken@1933
   384
        return -1;
slouken@1933
   385
    }
slouken@1933
   386
#ifdef SDL_VIDEO_OPENGL
slouken@1933
   387
    /*
slouken@1933
   388
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1933
   389
        if (Cocoa_GL_SetupWindow(_this, window) < 0) {
slouken@1933
   390
            Cocoa_DestroyWindow(_this, window);
slouken@1933
   391
            return -1;
slouken@1933
   392
        }
slouken@1933
   393
    }
slouken@1933
   394
    */
slouken@1933
   395
#endif
slouken@1933
   396
    return 0;
slouken@1933
   397
}
slouken@1933
   398
slouken@1933
   399
int
slouken@1933
   400
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   401
{
slouken@1933
   402
    NSAutoreleasePool *pool;
slouken@1933
   403
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   404
    NSString *title;
slouken@1933
   405
    int status;
slouken@1933
   406
slouken@1933
   407
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   408
slouken@1933
   409
    /* Query the title from the existing window */
slouken@1933
   410
    title = [nswindow title];
slouken@1933
   411
    if (title) {
slouken@1933
   412
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   413
    }
slouken@1933
   414
slouken@1933
   415
    [pool release];
slouken@1933
   416
slouken@1933
   417
    return SetupWindowData(window, nswindow, NO);
slouken@1933
   418
}
slouken@1933
   419
slouken@1933
   420
void
slouken@1933
   421
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   422
{
slouken@1933
   423
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   424
    NSString *string;
slouken@1933
   425
slouken@1933
   426
    string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1933
   427
    [nswindow setTitle:string];
slouken@1933
   428
    [nswindow setMiniwindowTitle:string];
slouken@1933
   429
    [string release];
slouken@1933
   430
}
slouken@1933
   431
slouken@1933
   432
void
slouken@1933
   433
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   434
{
slouken@1933
   435
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   436
    NSRect rect;
slouken@1933
   437
slouken@1933
   438
    rect.origin.x = window->x;
slouken@1933
   439
    rect.origin.y = window->y;
slouken@1933
   440
    rect.size.width = window->w;
slouken@1933
   441
    rect.size.height = window->h;
slouken@1933
   442
    ConvertNSRect(&rect);
slouken@1933
   443
    rect = [nswindow frameRectForContentRect:rect];
slouken@1933
   444
    [nswindow setFrameOrigin:rect.origin];
slouken@1933
   445
}
slouken@1933
   446
slouken@1933
   447
void
slouken@1933
   448
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   449
{
slouken@1933
   450
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   451
    NSSize size;
slouken@1933
   452
slouken@1933
   453
    size.width = window->w;
slouken@1933
   454
    size.height = window->h;
slouken@1933
   455
    [nswindow setContentSize:size];
slouken@1933
   456
}
slouken@1933
   457
slouken@1933
   458
void
slouken@1933
   459
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   460
{
slouken@1933
   461
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   462
slouken@1933
   463
    [nswindow makeKeyAndOrderFront:nil];
slouken@1933
   464
}
slouken@1933
   465
slouken@1933
   466
void
slouken@1933
   467
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   468
{
slouken@1933
   469
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   470
slouken@1933
   471
    /* FIXME */
slouken@1933
   472
}
slouken@1933
   473
slouken@1933
   474
void
slouken@1933
   475
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   476
{
slouken@1933
   477
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   478
slouken@1933
   479
    [nswindow makeKeyAndOrderFront:nil];
slouken@1933
   480
}
slouken@1933
   481
slouken@1933
   482
void
slouken@1933
   483
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   484
{
slouken@1933
   485
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   486
slouken@1933
   487
    [nswindow performZoom:nil];
slouken@1933
   488
}
slouken@1933
   489
slouken@1933
   490
void
slouken@1933
   491
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   492
{
slouken@1933
   493
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   494
slouken@1933
   495
    [nswindow performMiniaturize:nil];
slouken@1933
   496
}
slouken@1933
   497
slouken@1933
   498
void
slouken@1933
   499
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   500
{
slouken@1933
   501
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   502
slouken@1933
   503
    /* FIXME */
slouken@1933
   504
}
slouken@1933
   505
slouken@1933
   506
void
slouken@1933
   507
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   508
{
slouken@1933
   509
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1933
   510
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1933
   511
        /* FIXME: Grab mouse */
slouken@1933
   512
    } else {
slouken@1933
   513
        /* FIXME: Release mouse */
slouken@1933
   514
    }
slouken@1933
   515
}
slouken@1933
   516
slouken@1933
   517
void
slouken@1933
   518
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   519
{
slouken@1933
   520
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   521
slouken@1933
   522
    if (data) {
slouken@1933
   523
        NSAutoreleasePool *pool;
slouken@1933
   524
#ifdef SDL_VIDEO_OPENGL
slouken@1933
   525
        /*
slouken@1933
   526
        if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1933
   527
            Cocoa_GL_CleanupWindow(_this, window);
slouken@1933
   528
        }
slouken@1933
   529
        */
slouken@1933
   530
#endif
slouken@1933
   531
        pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   532
        [data->listener close];
slouken@1933
   533
        [data->listener release];
slouken@1933
   534
        if (data->created) {
slouken@1933
   535
            [data->window close];
slouken@1933
   536
        }
slouken@1933
   537
        SDL_free(data);
slouken@1933
   538
        [pool release];
slouken@1933
   539
    }
slouken@1933
   540
}
slouken@1933
   541
slouken@1933
   542
SDL_bool
slouken@1933
   543
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   544
{
slouken@1933
   545
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
slouken@1933
   546
slouken@1933
   547
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1933
   548
        //info->window = nswindow;
slouken@1933
   549
        return SDL_TRUE;
slouken@1933
   550
    } else {
slouken@1933
   551
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
   552
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
   553
        return SDL_FALSE;
slouken@1933
   554
    }
slouken@1933
   555
}
slouken@1933
   556
slouken@1933
   557
/* vi: set ts=4 sw=4 expandtab: */