src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Fri, 14 Sep 2012 11:36:36 -0400
changeset 6426 38ed71d9ae5f
parent 6422 fd0ac1b56115
child 6489 31449ea8f934
permissions -rwxr-xr-x
Restore Cocoa title bar caption when adding border to a window.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
     2
  Simple DirectMedia Layer
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6108
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
     7
  arising from the use of this software.
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    11
  freely, subject to the following restrictions:
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5502
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5873
diff changeset
    23
#if SDL_VIDEO_DRIVER_COCOA
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5873
diff changeset
    24
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include "SDL_syswm.h"
5398
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
    26
#include "SDL_timer.h"  /* For SDL_GetTicks() */
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "../SDL_sysvideo.h"
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "../../events/SDL_keyboard_c.h"
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#include "../../events/SDL_mouse_c.h"
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
    30
#include "../../events/SDL_touch_c.h"
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#include "../../events/SDL_windowevents_c.h"
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
#include "SDL_cocoavideo.h"
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 3688
diff changeset
    33
#include "SDL_cocoashape.h"
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 5056
diff changeset
    34
#include "SDL_cocoamouse.h"
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
5398
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
    36
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
    37
static Uint32 s_moveHack;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
    38
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
static __inline__ void ConvertNSRect(NSRect *r)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
{
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
@implementation Cocoa_WindowListener
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
- (void)listen:(SDL_WindowData *)data
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
{
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
    NSNotificationCenter *center;
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    49
    NSWindow *window = data->nswindow;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    50
    NSView *view = [window contentView];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
    _data = data;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
    center = [NSNotificationCenter defaultCenter];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
5374
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    56
    if ([window delegate] != nil) {
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    57
        [center addObserver:self selector:@selector(windowDidExpose:) name:NSWindowDidExposeNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    58
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    59
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    60
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    61
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    62
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    63
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    64
    } else {
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    65
        [window setDelegate:self];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    66
    }
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    68
    [window setNextResponder:self];
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    69
    [window setAcceptsMouseMovedEvents:YES];
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    70
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    71
    [view setNextResponder:self];
6108
f3c34d321289 Don't call -[NSView setAcceptsTouchEvents] if the OS doesn't provide it.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    72
5646
fe1df351ca3d Removed the MAC_OS_X_VERSION_10_x macros from the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5564
diff changeset
    73
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
6108
f3c34d321289 Don't call -[NSView setAcceptsTouchEvents] if the OS doesn't provide it.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    74
    if ([view respondsToSelector:@selector(setAcceptsTouchEvents:)]) {
f3c34d321289 Don't call -[NSView setAcceptsTouchEvents] if the OS doesn't provide it.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    75
        [view setAcceptsTouchEvents:YES];
f3c34d321289 Don't call -[NSView setAcceptsTouchEvents] if the OS doesn't provide it.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    76
    }
4925
514f811a4887 Removed support for 10.3.9
Sam Lantinga <slouken@libsdl.org>
parents: 4915
diff changeset
    77
#endif
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
- (void)close
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
{
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
    NSNotificationCenter *center;
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    83
    NSWindow *window = _data->nswindow;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    84
    NSView *view = [window contentView];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
    center = [NSNotificationCenter defaultCenter];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
5374
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    88
    if ([window delegate] != self) {
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    89
        [center removeObserver:self name:NSWindowDidExposeNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    90
        [center removeObserver:self name:NSWindowDidMoveNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    91
        [center removeObserver:self name:NSWindowDidResizeNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    92
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    93
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    94
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    95
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    96
    } else {
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    97
        [window setDelegate:nil];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    98
    }
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
    99
5374
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   100
    if ([window nextResponder] == self) {
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   101
        [window setNextResponder:nil];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   102
    }
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   103
    if ([view nextResponder] == self) {
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   104
        [view setNextResponder:nil];
dacb4f5e0dd3 We still want the delegate functionality for windowShouldClose()
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   105
    }
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
- (BOOL)windowShouldClose:(id)sender
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
{
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3530
diff changeset
   110
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
    return NO;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
- (void)windowDidExpose:(NSNotification *)aNotification
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
{
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3530
diff changeset
   116
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
- (void)windowDidMove:(NSNotification *)aNotification
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
{
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
    int x, y;
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   122
    SDL_VideoDevice *device = SDL_GetVideoDevice();
5398
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   123
    SDL_Window *window = _data->window;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   124
    NSWindow *nswindow = _data->nswindow;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   125
    NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
    ConvertNSRect(&rect);
5398
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   127
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   128
    if (s_moveHack) {
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   129
        SDL_bool blockMove = ((SDL_GetTicks() - s_moveHack) < 500);
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   130
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   131
        s_moveHack = 0;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   132
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   133
        if (blockMove) {
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   134
            /* Cocoa is adjusting the window in response to a mode change */
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   135
            rect.origin.x = window->x;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   136
            rect.origin.y = window->y;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   137
            ConvertNSRect(&rect);
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   138
            [nswindow setFrameOrigin:rect.origin];
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   139
            return;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   140
        }
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   141
    }
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   142
3507
3712547eac4f The window positions are relative to the origin of the windowing system (upper left of the primary display).
Sam Lantinga <slouken@libsdl.org>
parents: 3506
diff changeset
   143
    x = (int)rect.origin.x;
3712547eac4f The window positions are relative to the origin of the windowing system (upper left of the primary display).
Sam Lantinga <slouken@libsdl.org>
parents: 3506
diff changeset
   144
    y = (int)rect.origin.y;
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   145
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   146
    if (window == device->current_glwin) {
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   147
        [((NSOpenGLContext *) device->current_glctx) update];
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   148
    }
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   149
5398
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   150
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
- (void)windowDidResize:(NSNotification *)aNotification
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
{
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   155
    SDL_VideoDevice *device = SDL_GetVideoDevice();
6231
5eecf59b698f Fixed bug 1372 - OSX Window Maximize/Resize Doesn't Update Window Position
Sam Lantinga <slouken@libsdl.org>
parents: 6185
diff changeset
   156
    int x, y, w, h;
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   157
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
6231
5eecf59b698f Fixed bug 1372 - OSX Window Maximize/Resize Doesn't Update Window Position
Sam Lantinga <slouken@libsdl.org>
parents: 6185
diff changeset
   158
    ConvertNSRect(&rect);
5eecf59b698f Fixed bug 1372 - OSX Window Maximize/Resize Doesn't Update Window Position
Sam Lantinga <slouken@libsdl.org>
parents: 6185
diff changeset
   159
    x = (int)rect.origin.x;
5eecf59b698f Fixed bug 1372 - OSX Window Maximize/Resize Doesn't Update Window Position
Sam Lantinga <slouken@libsdl.org>
parents: 6185
diff changeset
   160
    y = (int)rect.origin.y;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
    w = (int)rect.size.width;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
    h = (int)rect.size.height;
4937
24d44c7c4c63 Don't crash when resizing non-shaped windows
Sam Lantinga <slouken@libsdl.org>
parents: 4927
diff changeset
   163
    if (SDL_IsShapedWindow(_data->window))
24d44c7c4c63 Don't crash when resizing non-shaped windows
Sam Lantinga <slouken@libsdl.org>
parents: 4927
diff changeset
   164
        Cocoa_ResizeWindowShape(_data->window);
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   165
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   166
    if (_data->window == device->current_glwin) {
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   167
        [((NSOpenGLContext *) device->current_glctx) update];
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   168
    }
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   169
6231
5eecf59b698f Fixed bug 1372 - OSX Window Maximize/Resize Doesn't Update Window Position
Sam Lantinga <slouken@libsdl.org>
parents: 6185
diff changeset
   170
    /* The window can move during a resize event, such as when maximizing
5eecf59b698f Fixed bug 1372 - OSX Window Maximize/Resize Doesn't Update Window Position
Sam Lantinga <slouken@libsdl.org>
parents: 6185
diff changeset
   171
       or resizing from a corner */
5eecf59b698f Fixed bug 1372 - OSX Window Maximize/Resize Doesn't Update Window Position
Sam Lantinga <slouken@libsdl.org>
parents: 6185
diff changeset
   172
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3530
diff changeset
   173
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
- (void)windowDidMiniaturize:(NSNotification *)aNotification
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
{
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3530
diff changeset
   178
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
{
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3530
diff changeset
   183
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
- (void)windowDidBecomeKey:(NSNotification *)aNotification
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
{
5367
71fe70373e58 Fixed bug 1105 (SDL_GetMouseState returns wrong location upon window re-activation)
Sam Lantinga <slouken@libsdl.org>
parents: 5361
diff changeset
   188
    SDL_Window *window = _data->window;
71fe70373e58 Fixed bug 1105 (SDL_GetMouseState returns wrong location upon window re-activation)
Sam Lantinga <slouken@libsdl.org>
parents: 5361
diff changeset
   189
2059
4685ccd33d0e Fixed mouse enter/leave events for a single window.
Sam Lantinga <slouken@libsdl.org>
parents: 1973
diff changeset
   190
    /* We're going to get keyboard events, since we're key. */
5367
71fe70373e58 Fixed bug 1105 (SDL_GetMouseState returns wrong location upon window re-activation)
Sam Lantinga <slouken@libsdl.org>
parents: 5361
diff changeset
   191
    SDL_SetKeyboardFocus(window);
71fe70373e58 Fixed bug 1105 (SDL_GetMouseState returns wrong location upon window re-activation)
Sam Lantinga <slouken@libsdl.org>
parents: 5361
diff changeset
   192
71fe70373e58 Fixed bug 1105 (SDL_GetMouseState returns wrong location upon window re-activation)
Sam Lantinga <slouken@libsdl.org>
parents: 5361
diff changeset
   193
    /* If we just gained focus we need the updated mouse position */
5396
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   194
    {
5367
71fe70373e58 Fixed bug 1105 (SDL_GetMouseState returns wrong location upon window re-activation)
Sam Lantinga <slouken@libsdl.org>
parents: 5361
diff changeset
   195
        NSPoint point;
5396
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   196
        int x, y;
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   197
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   198
        point = [_data->nswindow mouseLocationOutsideOfEventStream];
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   199
        x = (int)point.x;
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   200
        y = (int)(window->h - point.y);
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   201
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   202
        if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   203
            if (SDL_GetMouseFocus() != window) {
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   204
                [self mouseEntered:nil];
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   205
            }
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   206
            SDL_SendMouseMotion(window, 0, x, y);
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   207
        }
5367
71fe70373e58 Fixed bug 1105 (SDL_GetMouseState returns wrong location upon window re-activation)
Sam Lantinga <slouken@libsdl.org>
parents: 5361
diff changeset
   208
    }
1962
c92e5f3e68d9 Hack to stop the beeping
Sam Lantinga <slouken@libsdl.org>
parents: 1959
diff changeset
   209
4503
524dfefd554c Added an event when the clipboard is updated, triggered after the window gains the keyboard focus.
Sam Lantinga <slouken@libsdl.org>
parents: 4498
diff changeset
   210
    /* Check to see if someone updated the clipboard */
524dfefd554c Added an event when the clipboard is updated, triggered after the window gains the keyboard focus.
Sam Lantinga <slouken@libsdl.org>
parents: 4498
diff changeset
   211
    Cocoa_CheckClipboardUpdate(_data->videodata);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
- (void)windowDidResignKey:(NSNotification *)aNotification
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
{
2059
4685ccd33d0e Fixed mouse enter/leave events for a single window.
Sam Lantinga <slouken@libsdl.org>
parents: 1973
diff changeset
   216
    /* Some other window will get mouse events, since we're not key. */
4465
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3688
diff changeset
   217
    if (SDL_GetMouseFocus() == _data->window) {
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3688
diff changeset
   218
        SDL_SetMouseFocus(NULL);
2059
4685ccd33d0e Fixed mouse enter/leave events for a single window.
Sam Lantinga <slouken@libsdl.org>
parents: 1973
diff changeset
   219
    }
4685ccd33d0e Fixed mouse enter/leave events for a single window.
Sam Lantinga <slouken@libsdl.org>
parents: 1973
diff changeset
   220
4685ccd33d0e Fixed mouse enter/leave events for a single window.
Sam Lantinga <slouken@libsdl.org>
parents: 1973
diff changeset
   221
    /* Some other window will get keyboard events, since we're not key. */
4465
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3688
diff changeset
   222
    if (SDL_GetKeyboardFocus() == _data->window) {
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3688
diff changeset
   223
        SDL_SetKeyboardFocus(NULL);
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3688
diff changeset
   224
    }
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
- (void)mouseDown:(NSEvent *)theEvent
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
{
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   229
    int button;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   231
    switch ([theEvent buttonNumber]) {
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   232
    case 0:
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   233
        button = SDL_BUTTON_LEFT;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   234
        break;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   235
    case 1:
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   236
        button = SDL_BUTTON_RIGHT;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   237
        break;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   238
    case 2:
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   239
        button = SDL_BUTTON_MIDDLE;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   240
        break;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   241
    default:
5061
9e9940eae455 Fixed mouse button index for additional mouse buttons
Sam Lantinga <slouken@libsdl.org>
parents: 5059
diff changeset
   242
        button = [theEvent buttonNumber] + 1;
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   243
        break;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   244
    }
4484
9322f7db8603 Cleaned up the mouse window focus handling: you always pass in the relative window when sending a mouse event.
Sam Lantinga <slouken@libsdl.org>
parents: 4465
diff changeset
   245
    SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   246
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   247
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
- (void)rightMouseDown:(NSEvent *)theEvent
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
{
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   250
    [self mouseDown:theEvent];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
- (void)otherMouseDown:(NSEvent *)theEvent
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
{
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   255
    [self mouseDown:theEvent];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
- (void)mouseUp:(NSEvent *)theEvent
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
{
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   260
    int button;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   262
    switch ([theEvent buttonNumber]) {
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   263
    case 0:
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   264
        button = SDL_BUTTON_LEFT;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   265
        break;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   266
    case 1:
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   267
        button = SDL_BUTTON_RIGHT;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   268
        break;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   269
    case 2:
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   270
        button = SDL_BUTTON_MIDDLE;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   271
        break;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   272
    default:
5061
9e9940eae455 Fixed mouse button index for additional mouse buttons
Sam Lantinga <slouken@libsdl.org>
parents: 5059
diff changeset
   273
        button = [theEvent buttonNumber] + 1;
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   274
        break;
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   275
    }
4484
9322f7db8603 Cleaned up the mouse window focus handling: you always pass in the relative window when sending a mouse event.
Sam Lantinga <slouken@libsdl.org>
parents: 4465
diff changeset
   276
    SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   278
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   279
- (void)rightMouseUp:(NSEvent *)theEvent
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
{
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   281
    [self mouseUp:theEvent];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
- (void)otherMouseUp:(NSEvent *)theEvent
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
{
1959
25d6537feea4 Implemented Cocoa key event handling.
Sam Lantinga <slouken@libsdl.org>
parents: 1958
diff changeset
   286
    [self mouseUp:theEvent];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   289
- (void)mouseEntered:(NSEvent *)theEvent
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   290
{
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   291
    SDL_SetMouseFocus(_data->window);
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5375
diff changeset
   292
5388
6e586f65f8ef Update the cursor correctly when it enters and leaves the window.
Sam Lantinga <slouken@libsdl.org>
parents: 5380
diff changeset
   293
    SDL_SetCursor(NULL);
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   294
}
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   295
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   296
- (void)mouseExited:(NSEvent *)theEvent
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   297
{
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   298
    SDL_Window *window = _data->window;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   299
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   300
    if (SDL_GetMouseFocus() == window) {
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   301
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   302
            int x, y;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   303
            NSPoint point;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   304
            CGPoint cgpoint;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   305
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   306
            point = [theEvent locationInWindow];
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   307
            point.y = window->h - point.y;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   308
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   309
            SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   310
            SDL_GetMouseState(&x, &y);
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   311
            cgpoint.x = window->x + x;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   312
            cgpoint.y = window->y + y;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   313
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   314
        } else {
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   315
            SDL_SetMouseFocus(NULL);
6301
e8a69c5378e7 Change SDL_SetCursor to set the cursor back to the default cursor when the
Wim Looman <ghostunderscore@gmail.com>
parents: 6231
diff changeset
   316
            SDL_SetCursor(NULL);
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   317
        }
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   318
    }
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   319
}
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   320
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   321
- (void)mouseMoved:(NSEvent *)theEvent
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
{
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5404
diff changeset
   323
    SDL_Mouse *mouse = SDL_GetMouse();
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3530
diff changeset
   324
    SDL_Window *window = _data->window;
5396
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   325
    NSPoint point;
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   326
    int x, y;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5404
diff changeset
   328
    if (mouse->relative_mode) {
5059
00d1fef487c5 Fix double mouse motion in OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 5057
diff changeset
   329
        return;
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   330
    }
5059
00d1fef487c5 Fix double mouse motion in OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 5057
diff changeset
   331
5396
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   332
    point = [theEvent locationInWindow];
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   333
    x = (int)point.x;
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   334
    y = (int)(window->h - point.y);
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   335
5396
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   336
    if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   337
        if (SDL_GetMouseFocus() == window) {
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   338
            [self mouseExited:theEvent];
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   339
        }
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   340
    } else {
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   341
        if (SDL_GetMouseFocus() != window) {
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   342
            [self mouseEntered:theEvent];
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   343
        }
109b7c071f70 Tracking rectangles had some problems, it's easier to track things directly. (fixes bug 1149, 1147, 1146)
Sam Lantinga <slouken@libsdl.org>
parents: 5388
diff changeset
   344
        SDL_SendMouseMotion(window, 0, x, y);
2059
4685ccd33d0e Fixed mouse enter/leave events for a single window.
Sam Lantinga <slouken@libsdl.org>
parents: 1973
diff changeset
   345
    }
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   346
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   347
1957
c7803e4c21d3 Implemented Cocoa mouse wheel events
Sam Lantinga <slouken@libsdl.org>
parents: 1956
diff changeset
   348
- (void)mouseDragged:(NSEvent *)theEvent
c7803e4c21d3 Implemented Cocoa mouse wheel events
Sam Lantinga <slouken@libsdl.org>
parents: 1956
diff changeset
   349
{
c7803e4c21d3 Implemented Cocoa mouse wheel events
Sam Lantinga <slouken@libsdl.org>
parents: 1956
diff changeset
   350
    [self mouseMoved:theEvent];
c7803e4c21d3 Implemented Cocoa mouse wheel events
Sam Lantinga <slouken@libsdl.org>
parents: 1956
diff changeset
   351
}
c7803e4c21d3 Implemented Cocoa mouse wheel events
Sam Lantinga <slouken@libsdl.org>
parents: 1956
diff changeset
   352
1958
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   353
- (void)rightMouseDragged:(NSEvent *)theEvent
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   354
{
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   355
    [self mouseMoved:theEvent];
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   356
}
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   357
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   358
- (void)otherMouseDragged:(NSEvent *)theEvent
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   359
{
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   360
    [self mouseMoved:theEvent];
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   361
}
5fc6fb0fb605 Added right/other drag code for Cocoa windows
Sam Lantinga <slouken@libsdl.org>
parents: 1957
diff changeset
   362
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
- (void)scrollWheel:(NSEvent *)theEvent
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
{
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 5056
diff changeset
   365
    Cocoa_HandleMouseWheel(_data->window, theEvent);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   367
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   368
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   369
{
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   370
    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   371
}
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   372
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   373
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   374
{
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   375
    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   376
}
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   377
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   378
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   379
{
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   380
    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   381
}
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   382
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   383
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   384
{
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   385
    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   386
}
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   387
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   388
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   389
{
5646
fe1df351ca3d Removed the MAC_OS_X_VERSION_10_x macros from the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5564
diff changeset
   390
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
4680
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   391
    NSSet *touches = 0;
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   392
    NSEnumerator *enumerator;
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   393
    NSTouch *touch;
1957
c7803e4c21d3 Implemented Cocoa mouse wheel events
Sam Lantinga <slouken@libsdl.org>
parents: 1956
diff changeset
   394
4680
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   395
    switch (type) {
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   396
        case COCOA_TOUCH_DOWN:
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   397
            touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   398
            break;
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   399
        case COCOA_TOUCH_UP:
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   400
        case COCOA_TOUCH_CANCELLED:
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   401
            touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   402
            break;
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   403
        case COCOA_TOUCH_MOVE:
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   404
            touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   405
            break;
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   406
    }
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   407
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   408
    enumerator = [touches objectEnumerator];
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   409
    touch = (NSTouch*)[enumerator nextObject];
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   410
    while (touch) {
5873
be86fd8dca58 Fixed compiler warning on 32-bit Mac OS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 5646
diff changeset
   411
        const SDL_TouchID touchId = (SDL_TouchID) ((size_t) [touch device]);
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   412
        if (!SDL_GetTouch(touchId)) {
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   413
            SDL_Touch touch;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   414
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   415
            touch.id = touchId;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   416
            touch.x_min = 0;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   417
            touch.x_max = 1;
4678
f8431f66613d Added SDL_TouchID, SDL_FingerID, SDL_GestureID types. Converted to integer cioordinates (<- not working).
jimtla
parents: 4676
diff changeset
   418
            touch.native_xres = touch.x_max - touch.x_min;
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   419
            touch.y_min = 0;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   420
            touch.y_max = 1;
4678
f8431f66613d Added SDL_TouchID, SDL_FingerID, SDL_GestureID types. Converted to integer cioordinates (<- not working).
jimtla
parents: 4676
diff changeset
   421
            touch.native_yres = touch.y_max - touch.y_min;
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   422
            touch.pressure_min = 0;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   423
            touch.pressure_max = 1;
4678
f8431f66613d Added SDL_TouchID, SDL_FingerID, SDL_GestureID types. Converted to integer cioordinates (<- not working).
jimtla
parents: 4676
diff changeset
   424
            touch.native_pressureres = touch.pressure_max - touch.pressure_min;
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   425
            
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   426
            if (SDL_AddTouch(&touch, "") < 0) {
4680
229529693289 Fixed sending motion and finger up events
Sam Lantinga <slouken@libsdl.org>
parents: 4679
diff changeset
   427
                return;
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   428
            }
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   429
        } 
4687
257bdf117af8 Fixed so the origin of the touch events is the upper left.
Sam Lantinga <slouken@libsdl.org>
parents: 4680
diff changeset
   430
5873
be86fd8dca58 Fixed compiler warning on 32-bit Mac OS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 5646
diff changeset
   431
        const SDL_FingerID fingerId = (SDL_FingerID) ((size_t) [touch identity]);
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   432
        float x = [touch normalizedPosition].x;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   433
        float y = [touch normalizedPosition].y;
5261
595814f561f7 There is only one width and height for the window. If those are changed during the course of a fullscreen mode change, then they'll stay that size when returning to windowed mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5253
diff changeset
   434
        /* Make the origin the upper left instead of the lower left */
595814f561f7 There is only one width and height for the window. If those are changed during the course of a fullscreen mode change, then they'll stay that size when returning to windowed mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5253
diff changeset
   435
        y = 1.0f - y;
4687
257bdf117af8 Fixed so the origin of the touch events is the upper left.
Sam Lantinga <slouken@libsdl.org>
parents: 4680
diff changeset
   436
4673
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   437
        switch (type) {
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   438
        case COCOA_TOUCH_DOWN:
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   439
            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   440
            break;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   441
        case COCOA_TOUCH_UP:
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   442
        case COCOA_TOUCH_CANCELLED:
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   443
            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   444
            break;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   445
        case COCOA_TOUCH_MOVE:
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   446
            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   447
            break;
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   448
        }
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   449
        
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   450
        touch = (NSTouch*)[enumerator nextObject];
c17ac64abb70 Fixed the code so we receive Cocoa touch events
Sam Lantinga <slouken@libsdl.org>
parents: 4503
diff changeset
   451
    }
5646
fe1df351ca3d Removed the MAC_OS_X_VERSION_10_x macros from the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5564
diff changeset
   452
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 */
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   453
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   454
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   455
@end
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   456
1973
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   457
@interface SDLWindow : NSWindow
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   458
/* These are needed for borderless/fullscreen windows */
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   459
- (BOOL)canBecomeKeyWindow;
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   460
- (BOOL)canBecomeMainWindow;
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   461
@end
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   462
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   463
@implementation SDLWindow
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   464
- (BOOL)canBecomeKeyWindow
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   465
{
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   466
    return YES;
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   467
}
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   468
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   469
- (BOOL)canBecomeMainWindow
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   470
{
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   471
    return YES;
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   472
}
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   473
@end
81255f93dfcd Fixed fullscreen modes with Cocoa video driver.
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
   474
5379
78c071fe4c37 We can defer window creation.
Sam Lantinga <slouken@libsdl.org>
parents: 5378
diff changeset
   475
@interface SDLView : NSView
78c071fe4c37 We can defer window creation.
Sam Lantinga <slouken@libsdl.org>
parents: 5378
diff changeset
   476
/* The default implementation doesn't pass rightMouseDown to responder chain */
78c071fe4c37 We can defer window creation.
Sam Lantinga <slouken@libsdl.org>
parents: 5378
diff changeset
   477
- (void)rightMouseDown:(NSEvent *)theEvent;
4915
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   478
@end
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   479
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   480
@implementation SDLView
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   481
- (void)rightMouseDown:(NSEvent *)theEvent
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   482
{
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   483
    [[self nextResponder] rightMouseDown:theEvent];
4915
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   484
}
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   485
@end
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   486
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   487
static unsigned int
5305
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   488
GetWindowStyle(SDL_Window * window)
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   489
{
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   490
    unsigned int style;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   491
5305
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   492
	if (window->flags & SDL_WINDOW_FULLSCREEN) {
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   493
        style = NSBorderlessWindowMask;
5305
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   494
	} else {
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   495
		if (window->flags & SDL_WINDOW_BORDERLESS) {
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   496
			style = NSBorderlessWindowMask;
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   497
		} else {
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   498
			style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   499
		}
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   500
		if (window->flags & SDL_WINDOW_RESIZABLE) {
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   501
			style |= NSResizableWindowMask;
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   502
		}
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   503
	}
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   504
    return style;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   505
}
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   506
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   507
static int
1951
7177581dc9fa Initial work on X11 window code in.
Sam Lantinga <slouken@libsdl.org>
parents: 1937
diff changeset
   508
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   509
{
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   510
    NSAutoreleasePool *pool;
1951
7177581dc9fa Initial work on X11 window code in.
Sam Lantinga <slouken@libsdl.org>
parents: 1937
diff changeset
   511
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   512
    SDL_WindowData *data;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   513
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   514
    /* Allocate the window data */
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   515
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   516
    if (!data) {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   517
        SDL_OutOfMemory();
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   518
        return -1;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   519
    }
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3530
diff changeset
   520
    data->window = window;
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   521
    data->nswindow = nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   522
    data->created = created;
1951
7177581dc9fa Initial work on X11 window code in.
Sam Lantinga <slouken@libsdl.org>
parents: 1937
diff changeset
   523
    data->videodata = videodata;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   524
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   525
    pool = [[NSAutoreleasePool alloc] init];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   526
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   527
    /* Create an event listener for the window */
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   528
    data->listener = [[Cocoa_WindowListener alloc] init];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   529
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   530
    /* Fill in the SDL window with the window data */
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   531
    {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   532
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
6185
b91e7565e877 Fixed bug 1303 - SDL_CreateFromWindow duplicates window (Cocoa only)
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   533
        NSView *contentView = [ nswindow contentView ];
b91e7565e877 Fixed bug 1303 - SDL_CreateFromWindow duplicates window (Cocoa only)
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   534
        /* Create view if not already exists */
b91e7565e877 Fixed bug 1303 - SDL_CreateFromWindow duplicates window (Cocoa only)
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   535
        if (!contentView) {
b91e7565e877 Fixed bug 1303 - SDL_CreateFromWindow duplicates window (Cocoa only)
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   536
            contentView = [[SDLView alloc] initWithFrame:rect];
b91e7565e877 Fixed bug 1303 - SDL_CreateFromWindow duplicates window (Cocoa only)
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   537
            [nswindow setContentView: contentView];
b91e7565e877 Fixed bug 1303 - SDL_CreateFromWindow duplicates window (Cocoa only)
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   538
            [contentView release];
b91e7565e877 Fixed bug 1303 - SDL_CreateFromWindow duplicates window (Cocoa only)
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   539
        }
4915
834ce48a19c2 Fix right mouse event handling in Cocoa
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4902
diff changeset
   540
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   541
        ConvertNSRect(&rect);
5261
595814f561f7 There is only one width and height for the window. If those are changed during the course of a fullscreen mode change, then they'll stay that size when returning to windowed mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5253
diff changeset
   542
        window->x = (int)rect.origin.x;
595814f561f7 There is only one width and height for the window. If those are changed during the course of a fullscreen mode change, then they'll stay that size when returning to windowed mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5253
diff changeset
   543
        window->y = (int)rect.origin.y;
595814f561f7 There is only one width and height for the window. If those are changed during the course of a fullscreen mode change, then they'll stay that size when returning to windowed mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5253
diff changeset
   544
        window->w = (int)rect.size.width;
595814f561f7 There is only one width and height for the window. If those are changed during the course of a fullscreen mode change, then they'll stay that size when returning to windowed mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5253
diff changeset
   545
        window->h = (int)rect.size.height;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   546
    }
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   547
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   548
    /* Set up the listener after we create the view */
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   549
    [data->listener listen:data];
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   550
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   551
    if ([nswindow isVisible]) {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   552
        window->flags |= SDL_WINDOW_SHOWN;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   553
    } else {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   554
        window->flags &= ~SDL_WINDOW_SHOWN;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   555
    }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   556
    {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   557
        unsigned int style = [nswindow styleMask];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   558
5380
2de85077eb0b Simplified and unified the window creation process a little.
Sam Lantinga <slouken@libsdl.org>
parents: 5379
diff changeset
   559
        if (style == NSBorderlessWindowMask) {
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   560
            window->flags |= SDL_WINDOW_BORDERLESS;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   561
        } else {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   562
            window->flags &= ~SDL_WINDOW_BORDERLESS;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   563
        }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   564
        if (style & NSResizableWindowMask) {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   565
            window->flags |= SDL_WINDOW_RESIZABLE;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   566
        } else {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   567
            window->flags &= ~SDL_WINDOW_RESIZABLE;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   568
        }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   569
    }
5380
2de85077eb0b Simplified and unified the window creation process a little.
Sam Lantinga <slouken@libsdl.org>
parents: 5379
diff changeset
   570
    /* isZoomed always returns true if the window is not resizable */
2de85077eb0b Simplified and unified the window creation process a little.
Sam Lantinga <slouken@libsdl.org>
parents: 5379
diff changeset
   571
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   572
        window->flags |= SDL_WINDOW_MAXIMIZED;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   573
    } else {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   574
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   575
    }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   576
    if ([nswindow isMiniaturized]) {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   577
        window->flags |= SDL_WINDOW_MINIMIZED;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   578
    } else {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   579
        window->flags &= ~SDL_WINDOW_MINIMIZED;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   580
    }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   581
    if ([nswindow isKeyWindow]) {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   582
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
4465
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3688
diff changeset
   583
        SDL_SetKeyboardFocus(data->window);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   584
    }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   585
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   586
    /* All done! */
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   587
    [pool release];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   588
    window->driverdata = data;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   589
    return 0;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   590
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   591
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   592
int
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   593
Cocoa_CreateWindow(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   594
{
3506
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   595
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   596
    NSWindow *nswindow;
5246
58265e606e4e Window coordinates are in the global space and windows are not tied to a particular display.
Sam Lantinga <slouken@libsdl.org>
parents: 5061
diff changeset
   597
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   598
    NSRect rect;
3528
59ff7a2beb57 Added an API function to query geometry of multiple monitors:
Sam Lantinga <slouken@libsdl.org>
parents: 3525
diff changeset
   599
    SDL_Rect bounds;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   600
    unsigned int style;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   601
3528
59ff7a2beb57 Added an API function to query geometry of multiple monitors:
Sam Lantinga <slouken@libsdl.org>
parents: 3525
diff changeset
   602
    Cocoa_GetDisplayBounds(_this, display, &bounds);
5380
2de85077eb0b Simplified and unified the window creation process a little.
Sam Lantinga <slouken@libsdl.org>
parents: 5379
diff changeset
   603
    rect.origin.x = window->x;
2de85077eb0b Simplified and unified the window creation process a little.
Sam Lantinga <slouken@libsdl.org>
parents: 5379
diff changeset
   604
    rect.origin.y = window->y;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   605
    rect.size.width = window->w;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   606
    rect.size.height = window->h;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   607
    ConvertNSRect(&rect);
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   608
5305
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   609
    style = GetWindowStyle(window);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   610
3506
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   611
    /* Figure out which screen to place this window */
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   612
    NSArray *screens = [NSScreen screens];
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   613
    NSScreen *screen = nil;
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   614
    NSScreen *candidate;
3525
455a6c47d2c6 Fixed compilation on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 3517
diff changeset
   615
    int i, count = [screens count];
455a6c47d2c6 Fixed compilation on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 3517
diff changeset
   616
    for (i = 0; i < count; ++i) {
3529
09155ec3e291 Fixed crash
Sam Lantinga <slouken@libsdl.org>
parents: 3528
diff changeset
   617
        candidate = [screens objectAtIndex:i];
3506
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   618
        NSRect screenRect = [candidate frame];
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   619
        if (rect.origin.x >= screenRect.origin.x &&
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   620
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   621
            rect.origin.y >= screenRect.origin.y &&
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   622
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   623
            screen = candidate;
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   624
            rect.origin.x -= screenRect.origin.x;
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   625
            rect.origin.y -= screenRect.origin.y;
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   626
        }
e829b6098435 Added support for placing windows on different displays
Sam Lantinga <slouken@libsdl.org>
parents: 3414
diff changeset
   627
    }
5379
78c071fe4c37 We can defer window creation.
Sam Lantinga <slouken@libsdl.org>
parents: 5378
diff changeset
   628
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:YES screen:screen];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   629
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   630
    [pool release];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   631
1951
7177581dc9fa Initial work on X11 window code in.
Sam Lantinga <slouken@libsdl.org>
parents: 1937
diff changeset
   632
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   633
        [nswindow release];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   634
        return -1;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   635
    }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   636
    return 0;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   637
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   638
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   639
int
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   640
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   641
{
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   642
    NSAutoreleasePool *pool;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   643
    NSWindow *nswindow = (NSWindow *) data;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   644
    NSString *title;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   645
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   646
    pool = [[NSAutoreleasePool alloc] init];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   647
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   648
    /* Query the title from the existing window */
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   649
    title = [nswindow title];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   650
    if (title) {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   651
        window->title = SDL_strdup([title UTF8String]);
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   652
    }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   653
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   654
    [pool release];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   655
1951
7177581dc9fa Initial work on X11 window code in.
Sam Lantinga <slouken@libsdl.org>
parents: 1937
diff changeset
   656
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   657
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   658
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   659
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   660
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   661
{
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   662
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   663
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   664
    NSString *string;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   665
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   666
    if(window->title) {
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   667
        string = [[NSString alloc] initWithUTF8String:window->title];
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   668
    } else {
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   669
        string = [[NSString alloc] init];
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   670
    }
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   671
    [nswindow setTitle:string];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   672
    [string release];
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   673
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   674
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   675
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   676
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   677
void
5375
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   678
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   679
{
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   680
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   681
    NSImage *nsimage = Cocoa_CreateImage(icon);
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   682
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   683
    if (nsimage) {
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   684
        [NSApp setApplicationIconImage:nsimage];
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   685
    }
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   686
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   687
    [pool release];
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   688
}
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   689
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5374
diff changeset
   690
void
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   691
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   692
{
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   693
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   694
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   695
    NSRect rect;
5478
6b65ff3cee62 Fixed bug 1167 (SDL_WINDOWPOS_CENTERED doesn't work if used right after fullscreen -> windowed switch)
Sam Lantinga <slouken@libsdl.org>
parents: 5466
diff changeset
   696
    Uint32 moveHack;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   697
5478
6b65ff3cee62 Fixed bug 1167 (SDL_WINDOWPOS_CENTERED doesn't work if used right after fullscreen -> windowed switch)
Sam Lantinga <slouken@libsdl.org>
parents: 5466
diff changeset
   698
    rect.origin.x = window->x;
6b65ff3cee62 Fixed bug 1167 (SDL_WINDOWPOS_CENTERED doesn't work if used right after fullscreen -> windowed switch)
Sam Lantinga <slouken@libsdl.org>
parents: 5466
diff changeset
   699
    rect.origin.y = window->y;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   700
    rect.size.width = window->w;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   701
    rect.size.height = window->h;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   702
    ConvertNSRect(&rect);
5478
6b65ff3cee62 Fixed bug 1167 (SDL_WINDOWPOS_CENTERED doesn't work if used right after fullscreen -> windowed switch)
Sam Lantinga <slouken@libsdl.org>
parents: 5466
diff changeset
   703
6b65ff3cee62 Fixed bug 1167 (SDL_WINDOWPOS_CENTERED doesn't work if used right after fullscreen -> windowed switch)
Sam Lantinga <slouken@libsdl.org>
parents: 5466
diff changeset
   704
    moveHack = s_moveHack;
6b65ff3cee62 Fixed bug 1167 (SDL_WINDOWPOS_CENTERED doesn't work if used right after fullscreen -> windowed switch)
Sam Lantinga <slouken@libsdl.org>
parents: 5466
diff changeset
   705
    s_moveHack = 0;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   706
    [nswindow setFrameOrigin:rect.origin];
5478
6b65ff3cee62 Fixed bug 1167 (SDL_WINDOWPOS_CENTERED doesn't work if used right after fullscreen -> windowed switch)
Sam Lantinga <slouken@libsdl.org>
parents: 5466
diff changeset
   707
    s_moveHack = moveHack;
6b65ff3cee62 Fixed bug 1167 (SDL_WINDOWPOS_CENTERED doesn't work if used right after fullscreen -> windowed switch)
Sam Lantinga <slouken@libsdl.org>
parents: 5466
diff changeset
   708
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   709
    if (window == _this->current_glwin) {
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   710
        [((NSOpenGLContext *) _this->current_glctx) update];
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   711
    }
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   712
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   713
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   714
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   715
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   716
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   717
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   718
{
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   719
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   720
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   721
    NSWindow *nswindow = windata->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   722
    NSSize size;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   723
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   724
    size.width = window->w;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   725
    size.height = window->h;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   726
    [nswindow setContentSize:size];
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   727
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   728
    if (window == _this->current_glwin) {
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   729
        [((NSOpenGLContext *) _this->current_glctx) update];
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   730
    }
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   731
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   732
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   733
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   734
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   735
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   736
Cocoa_ShowWindow(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   737
{
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   738
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   739
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   740
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   741
    if (![nswindow isMiniaturized]) {
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   742
        [nswindow makeKeyAndOrderFront:nil];
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   743
    }
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   744
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   745
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   746
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   747
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   748
Cocoa_HideWindow(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   749
{
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   750
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   751
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   752
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   753
    [nswindow orderOut:nil];
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   754
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   755
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   756
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   757
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   758
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   759
{
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   760
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   761
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   762
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   763
    [nswindow makeKeyAndOrderFront:nil];
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   764
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   765
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   766
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   767
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   768
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   769
{
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   770
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   771
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   772
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   773
    [nswindow zoom:nil];
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   774
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   775
    if (window == _this->current_glwin) {
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   776
        [((NSOpenGLContext *) _this->current_glctx) update];
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   777
    }
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   778
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   779
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   780
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   781
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   782
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   783
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   784
{
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   785
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   786
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   787
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   788
    [nswindow miniaturize:nil];
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   789
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   790
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   791
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   792
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   793
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   794
{
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   795
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   796
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   797
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   798
    if ([nswindow isMiniaturized]) {
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   799
        [nswindow deminiaturize:nil];
5380
2de85077eb0b Simplified and unified the window creation process a little.
Sam Lantinga <slouken@libsdl.org>
parents: 5379
diff changeset
   800
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
1956
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   801
        [nswindow zoom:nil];
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   802
    }
ba0d62354872 Simplified driver window creation code.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   803
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   804
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   805
5400
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   806
static NSWindow *
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   807
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   808
{
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   809
    if (!data->created) {
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   810
        /* Don't mess with other people's windows... */
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   811
        return nswindow;
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   812
    }
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   813
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   814
    [data->listener close];
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   815
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:YES screen:[nswindow screen]];
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   816
    [data->nswindow setContentView:[nswindow contentView]];
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   817
    [data->listener listen:data];
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   818
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   819
    [nswindow close];
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   820
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   821
    return data->nswindow;
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   822
}
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   823
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   824
void
6422
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   825
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   826
{
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   827
    /* this message arrived in 10.6. You're out of luck on older OSes. */
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   828
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   829
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   830
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   831
    if ([nswindow respondsToSelector:@selector(setStyleMask:)]) {
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   832
        [nswindow setStyleMask:GetWindowStyle(window)];
6426
38ed71d9ae5f Restore Cocoa title bar caption when adding border to a window.
Ryan C. Gordon <icculus@icculus.org>
parents: 6422
diff changeset
   833
        if (bordered) {
38ed71d9ae5f Restore Cocoa title bar caption when adding border to a window.
Ryan C. Gordon <icculus@icculus.org>
parents: 6422
diff changeset
   834
            Cocoa_SetWindowTitle(_this, window);  // this got blanked out.
38ed71d9ae5f Restore Cocoa title bar caption when adding border to a window.
Ryan C. Gordon <icculus@icculus.org>
parents: 6422
diff changeset
   835
        }
6422
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   836
    }
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   837
    [pool release];
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   838
#endif
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   839
}
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   840
fd0ac1b56115 Added SDL_SetWindowBordered() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 6301
diff changeset
   841
void
5305
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   842
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   843
{
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   844
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   845
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   846
    NSWindow *nswindow = data->nswindow;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   847
    NSRect rect;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   848
5502
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   849
    /* The view responder chain gets messed with during setStyleMask */
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   850
    if ([[nswindow contentView] nextResponder] == data->listener) {
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   851
        [[nswindow contentView] setNextResponder:nil];
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   852
    }
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   853
5305
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   854
    if (fullscreen) {
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   855
        SDL_Rect bounds;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   856
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   857
        Cocoa_GetDisplayBounds(_this, display, &bounds);
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   858
        rect.origin.x = bounds.x;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   859
        rect.origin.y = bounds.y;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   860
        rect.size.width = bounds.w;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   861
        rect.size.height = bounds.h;
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   862
        ConvertNSRect(&rect);
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   863
5401
ccaccc76a196 Fix fullscreen origin on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5400
diff changeset
   864
        /* Hack to fix origin on Mac OS X 10.4 */
ccaccc76a196 Fix fullscreen origin on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5400
diff changeset
   865
        NSRect screenRect = [[nswindow screen] frame];
ccaccc76a196 Fix fullscreen origin on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5400
diff changeset
   866
        if (screenRect.size.height >= 1.0f) {
ccaccc76a196 Fix fullscreen origin on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5400
diff changeset
   867
            rect.origin.y += (screenRect.size.height - rect.size.height);
ccaccc76a196 Fix fullscreen origin on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5400
diff changeset
   868
        }
ccaccc76a196 Fix fullscreen origin on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5400
diff changeset
   869
5361
1d9e27849ed9 Fixed compiling on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5305
diff changeset
   870
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
1d9e27849ed9 Fixed compiling on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5305
diff changeset
   871
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
5400
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   872
        } else {
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   873
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
5361
1d9e27849ed9 Fixed compiling on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5305
diff changeset
   874
        }
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   875
    } else {
5398
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   876
        rect.origin.x = window->windowed.x;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   877
        rect.origin.y = window->windowed.y;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   878
        rect.size.width = window->windowed.w;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   879
        rect.size.height = window->windowed.h;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   880
        ConvertNSRect(&rect);
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   881
5361
1d9e27849ed9 Fixed compiling on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5305
diff changeset
   882
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
5378
738a96883fa6 Fixed compiler warning
Sam Lantinga <slouken@libsdl.org>
parents: 5376
diff changeset
   883
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
5400
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   884
        } else {
1df8b8a20191 Fixed bug 1141
Sam Lantinga <slouken@libsdl.org>
parents: 5399
diff changeset
   885
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
5361
1d9e27849ed9 Fixed compiling on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5305
diff changeset
   886
        }
5398
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   887
    }
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   888
5502
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   889
    /* The view responder chain gets messed with during setStyleMask */
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   890
    if ([[nswindow contentView] nextResponder] != data->listener) {
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   891
        [[nswindow contentView] setNextResponder:data->listener];
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   892
    }
ed5f117c949d Fixed bug: No right mouse button events during FullScreen Mac OS
Sam Lantinga <slouken@libsdl.org>
parents: 5478
diff changeset
   893
5398
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   894
    s_moveHack = 0;
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   895
    [nswindow setFrameOrigin:rect.origin];
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   896
    [nswindow setContentSize:rect.size];
83816c8a2b58 Restore the windowed position and size when coming back from fullscreen.
Sam Lantinga <slouken@libsdl.org>
parents: 5396
diff changeset
   897
    s_moveHack = SDL_GetTicks();
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   898
5399
1e2a82a66df8 Restore the window title when the title bar is re-added to the window.
Sam Lantinga <slouken@libsdl.org>
parents: 5398
diff changeset
   899
    /* When the window style changes the title is cleared */
1e2a82a66df8 Restore the window title when the title bar is re-added to the window.
Sam Lantinga <slouken@libsdl.org>
parents: 5398
diff changeset
   900
    if (!fullscreen) {
1e2a82a66df8 Restore the window title when the title bar is re-added to the window.
Sam Lantinga <slouken@libsdl.org>
parents: 5398
diff changeset
   901
        Cocoa_SetWindowTitle(_this, window);
1e2a82a66df8 Restore the window title when the title bar is re-added to the window.
Sam Lantinga <slouken@libsdl.org>
parents: 5398
diff changeset
   902
    }
1e2a82a66df8 Restore the window title when the title bar is re-added to the window.
Sam Lantinga <slouken@libsdl.org>
parents: 5398
diff changeset
   903
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   904
#ifdef FULLSCREEN_TOGGLEABLE
5305
75f5ff92ad08 Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   905
    if (fullscreen) {
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   906
        /* OpenGL is rendering to the window, so make it visible! */
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   907
        [nswindow setLevel:CGShieldingWindowLevel()];
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   908
    } else {
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   909
        [nswindow setLevel:kCGNormalWindowLevel];
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   910
    }
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   911
#endif
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   912
    [nswindow makeKeyAndOrderFront:nil];
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   913
5564
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   914
    if (window == _this->current_glwin) {
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   915
        [((NSOpenGLContext *) _this->current_glctx) update];
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   916
    }
7ed38a8ad304 Cocoa: Update the current GL context when its window moves or resizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   917
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   918
    [pool release];
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   919
}
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   920
5466
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   921
int
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   922
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   923
{
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   924
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   925
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   926
    const uint32_t tableSize = 256;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   927
    CGGammaValue redTable[tableSize];
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   928
    CGGammaValue greenTable[tableSize];
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   929
    CGGammaValue blueTable[tableSize];
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   930
    uint32_t i;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   931
    float inv65535 = 1.0f / 65535.0f;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   932
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   933
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   934
    for (i = 0; i < 256; i++) {
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   935
        redTable[i] = ramp[0*256+i] * inv65535;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   936
        greenTable[i] = ramp[1*256+i] * inv65535;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   937
        blueTable[i] = ramp[2*256+i] * inv65535;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   938
    }
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   939
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   940
    if (CGSetDisplayTransferByTable(display_id, tableSize,
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   941
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   942
        SDL_SetError("CGSetDisplayTransferByTable()");
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   943
        return -1;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   944
    }
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   945
    return 0;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   946
}
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   947
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   948
int
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   949
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   950
{
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   951
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   952
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   953
    const uint32_t tableSize = 256;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   954
    CGGammaValue redTable[tableSize];
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   955
    CGGammaValue greenTable[tableSize];
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   956
    CGGammaValue blueTable[tableSize];
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   957
    uint32_t i, tableCopied;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   958
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   959
    if (CGGetDisplayTransferByTable(display_id, tableSize,
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   960
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   961
        SDL_SetError("CGGetDisplayTransferByTable()");
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   962
        return -1;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   963
    }
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   964
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   965
    for (i = 0; i < tableCopied; i++) {
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   966
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   967
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   968
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   969
    }
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   970
    return 0;
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   971
}
006883d5fa51 Gamma support is back!
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
   972
5249
7a963be087ef Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.
Sam Lantinga <slouken@libsdl.org>
parents: 5246
diff changeset
   973
void
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   974
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   975
{
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   976
    /* Move the cursor to the nearest point in the window */
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   977
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   978
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   979
        int x, y;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   980
        CGPoint cgpoint;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   981
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   982
        SDL_GetMouseState(&x, &y);
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   983
        cgpoint.x = window->x + x;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   984
        cgpoint.y = window->y + y;
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   985
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5367
diff changeset
   986
    }
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   987
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   988
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   989
void
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   990
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   991
{
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   992
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   993
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   994
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   995
    if (data) {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   996
        [data->listener close];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   997
        [data->listener release];
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   998
        if (data->created) {
3688
6512cba48440 Fixed Cocoa and OpenGL builds
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   999
            [data->nswindow close];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1000
        }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1001
        SDL_free(data);
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1002
    }
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
  1003
    [pool release];
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1004
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1005
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1006
SDL_bool
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1007
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1008
{
4900
69d9db65f248 Merged the Windows custom window system hooks into the union used by X11.
Sam Lantinga <slouken@libsdl.org>
parents: 4862
diff changeset
  1009
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1010
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1011
    if (info->version.major <= SDL_MAJOR_VERSION) {
4900
69d9db65f248 Merged the Windows custom window system hooks into the union used by X11.
Sam Lantinga <slouken@libsdl.org>
parents: 4862
diff changeset
  1012
        info->subsystem = SDL_SYSWM_COCOA;
5056
8b7988f42fcb Added the ability to get the UIKit window through the SDL API.
Sam Lantinga <slouken@libsdl.org>
parents: 4937
diff changeset
  1013
        info->info.cocoa.window = nswindow;
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1014
        return SDL_TRUE;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1015
    } else {
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1016
        SDL_SetError("Application not compiled with SDL %d.%d\n",
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1017
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1018
        return SDL_FALSE;
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1019
    }
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1020
}
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1021
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5873
diff changeset
  1022
#endif /* SDL_VIDEO_DRIVER_COCOA */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5873
diff changeset
  1023
1933
7ee5297340f7 Implemented Cocoa window support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1024
/* vi: set ts=4 sw=4 expandtab: */