2010-09-07 15:34:51 +00:00
|
|
|
//========================================================================
|
2011-03-06 00:46:39 +00:00
|
|
|
// GLFW - An OpenGL library
|
2010-09-07 15:34:51 +00:00
|
|
|
// Platform: Cocoa/NSOpenGL
|
2010-09-07 15:41:26 +00:00
|
|
|
// API Version: 3.0
|
2010-09-07 15:34:51 +00:00
|
|
|
// WWW: http://www.glfw.org/
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
|
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would
|
|
|
|
// be appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not
|
|
|
|
// be misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
2012-02-15 00:44:55 +00:00
|
|
|
// Needed for _NSGetProgname
|
|
|
|
#include <crt_externs.h>
|
|
|
|
|
2010-09-16 01:05:55 +00:00
|
|
|
|
2013-05-02 16:16:23 +00:00
|
|
|
// Enter fullscreen mode
|
|
|
|
//
|
|
|
|
static void enterFullscreenMode(_GLFWwindow* window)
|
|
|
|
{
|
|
|
|
if ([window->ns.view isInFullScreenMode])
|
|
|
|
return;
|
|
|
|
|
|
|
|
_glfwSetVideoMode(window->monitor, &window->videoMode);
|
|
|
|
|
|
|
|
[window->ns.view enterFullScreenMode:window->monitor->ns.screen
|
|
|
|
withOptions:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave fullscreen mode
|
|
|
|
//
|
|
|
|
static void leaveFullscreenMode(_GLFWwindow* window)
|
|
|
|
{
|
|
|
|
if (![window->ns.view isInFullScreenMode])
|
|
|
|
return;
|
|
|
|
|
|
|
|
_glfwRestoreVideoMode(window->monitor);
|
|
|
|
|
|
|
|
// Exit full screen after the video restore to avoid a nasty display
|
|
|
|
// flickering during the fade
|
|
|
|
[window->ns.view exitFullScreenModeWithOptions:nil];
|
|
|
|
}
|
|
|
|
|
2013-05-22 16:03:54 +00:00
|
|
|
// Transforms the specified y-coordinate between the CG display and NS screen
|
|
|
|
// coordinate systems
|
|
|
|
//
|
|
|
|
static float transformY(float y)
|
|
|
|
{
|
|
|
|
const float height = CGDisplayBounds(CGMainDisplayID()).size.height;
|
|
|
|
return height - y;
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:16:23 +00:00
|
|
|
|
2013-02-04 12:22:10 +00:00
|
|
|
//------------------------------------------------------------------------
|
2010-09-07 15:34:51 +00:00
|
|
|
// Delegate for window related notifications
|
2013-02-04 12:22:10 +00:00
|
|
|
//------------------------------------------------------------------------
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
@interface GLFWWindowDelegate : NSObject
|
2010-09-15 16:57:25 +00:00
|
|
|
{
|
|
|
|
_GLFWwindow* window;
|
|
|
|
}
|
|
|
|
|
2010-09-16 22:39:35 +00:00
|
|
|
- (id)initWithGlfwWindow:(_GLFWwindow *)initWndow;
|
2010-09-15 16:57:25 +00:00
|
|
|
|
2010-09-07 15:34:51 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GLFWWindowDelegate
|
|
|
|
|
2013-04-21 20:46:35 +00:00
|
|
|
static void centerCursor(_GLFWwindow *window)
|
2013-04-16 22:35:14 +00:00
|
|
|
{
|
2013-04-21 20:46:35 +00:00
|
|
|
int width, height;
|
|
|
|
_glfwPlatformGetWindowSize(window, &width, &height);
|
|
|
|
_glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0);
|
2013-04-16 22:35:14 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 22:39:35 +00:00
|
|
|
- (id)initWithGlfwWindow:(_GLFWwindow *)initWindow
|
2010-09-15 16:57:25 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil)
|
|
|
|
window = initWindow;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)windowShouldClose:(id)sender
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-08-10 11:31:15 +00:00
|
|
|
_glfwInputWindowCloseRequest(window);
|
2010-09-07 15:34:51 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidResize:(NSNotification *)notification
|
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->nsgl.context update];
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-01-24 18:30:31 +00:00
|
|
|
int width, height;
|
|
|
|
_glfwPlatformGetWindowSize(window, &width, &height);
|
|
|
|
_glfwInputWindowSize(window, width, height);
|
2013-04-21 15:39:10 +00:00
|
|
|
_glfwInputWindowDamage(window);
|
2013-04-16 22:35:14 +00:00
|
|
|
|
2013-04-21 20:46:35 +00:00
|
|
|
if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
|
|
|
centerCursor(window);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 22:25:09 +00:00
|
|
|
- (void)windowDidMove:(NSNotification *)notification
|
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->nsgl.context update];
|
2010-09-16 22:25:09 +00:00
|
|
|
|
2013-01-24 18:30:31 +00:00
|
|
|
int x, y;
|
|
|
|
_glfwPlatformGetWindowPos(window, &x, &y);
|
|
|
|
_glfwInputWindowPos(window, x, y);
|
2013-04-16 22:35:14 +00:00
|
|
|
|
2013-04-21 20:46:35 +00:00
|
|
|
if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
|
|
|
centerCursor(window);
|
2010-09-16 22:25:09 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 22:39:35 +00:00
|
|
|
- (void)windowDidMiniaturize:(NSNotification *)notification
|
2010-09-16 01:05:55 +00:00
|
|
|
{
|
2011-10-09 15:10:40 +00:00
|
|
|
_glfwInputWindowIconify(window, GL_TRUE);
|
2010-09-16 01:05:55 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 22:39:35 +00:00
|
|
|
- (void)windowDidDeminiaturize:(NSNotification *)notification
|
2010-09-16 01:05:55 +00:00
|
|
|
{
|
2013-05-02 16:16:23 +00:00
|
|
|
if (window->monitor)
|
|
|
|
enterFullscreenMode(window);
|
|
|
|
|
2011-10-09 15:10:40 +00:00
|
|
|
_glfwInputWindowIconify(window, GL_FALSE);
|
2010-09-16 01:05:55 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 22:39:35 +00:00
|
|
|
- (void)windowDidBecomeKey:(NSNotification *)notification
|
2010-09-16 01:05:55 +00:00
|
|
|
{
|
2010-09-28 00:30:05 +00:00
|
|
|
_glfwInputWindowFocus(window, GL_TRUE);
|
2013-04-16 22:35:14 +00:00
|
|
|
|
2013-04-21 20:46:35 +00:00
|
|
|
if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
|
|
|
centerCursor(window);
|
2010-09-16 01:05:55 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 22:39:35 +00:00
|
|
|
- (void)windowDidResignKey:(NSNotification *)notification
|
2010-09-16 01:05:55 +00:00
|
|
|
{
|
2010-09-28 00:30:05 +00:00
|
|
|
_glfwInputWindowFocus(window, GL_FALSE);
|
2010-09-16 01:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2012-03-07 14:04:14 +00:00
|
|
|
|
2013-02-04 12:22:10 +00:00
|
|
|
//------------------------------------------------------------------------
|
2010-09-16 01:05:55 +00:00
|
|
|
// Delegate for application related notifications
|
2013-02-04 12:22:10 +00:00
|
|
|
//------------------------------------------------------------------------
|
2010-09-16 01:05:55 +00:00
|
|
|
|
|
|
|
@interface GLFWApplicationDelegate : NSObject
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GLFWApplicationDelegate
|
|
|
|
|
2010-09-07 15:34:51 +00:00
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
|
|
|
|
{
|
2010-09-16 01:05:55 +00:00
|
|
|
_GLFWwindow* window;
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
for (window = _glfw.windowListHead; window; window = window->next)
|
2012-08-10 11:31:15 +00:00
|
|
|
_glfwInputWindowCloseRequest(window);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
|
2012-08-21 19:18:09 +00:00
|
|
|
- (void)applicationDidHide:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
_GLFWwindow* window;
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
for (window = _glfw.windowListHead; window; window = window->next)
|
2012-08-21 19:18:09 +00:00
|
|
|
_glfwInputWindowVisibility(window, GL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidUnhide:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
_GLFWwindow* window;
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
for (window = _glfw.windowListHead; window; window = window->next)
|
2012-08-21 19:35:42 +00:00
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
if ([window->ns.object isVisible])
|
2012-08-21 19:35:42 +00:00
|
|
|
_glfwInputWindowVisibility(window, GL_TRUE);
|
|
|
|
}
|
2012-08-21 19:18:09 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 15:34:51 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
// Converts a Mac OS X keycode to a GLFW keycode
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2010-09-15 16:57:25 +00:00
|
|
|
static int convertMacKeyCode(unsigned int macKeyCode)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-03-07 14:13:41 +00:00
|
|
|
// Keyboard symbol translation table
|
|
|
|
// TODO: Need to find mappings for F13-F15, volume down/up/mute, and eject.
|
|
|
|
static const unsigned int table[128] =
|
|
|
|
{
|
|
|
|
/* 00 */ GLFW_KEY_A,
|
|
|
|
/* 01 */ GLFW_KEY_S,
|
|
|
|
/* 02 */ GLFW_KEY_D,
|
|
|
|
/* 03 */ GLFW_KEY_F,
|
|
|
|
/* 04 */ GLFW_KEY_H,
|
|
|
|
/* 05 */ GLFW_KEY_G,
|
|
|
|
/* 06 */ GLFW_KEY_Z,
|
|
|
|
/* 07 */ GLFW_KEY_X,
|
|
|
|
/* 08 */ GLFW_KEY_C,
|
|
|
|
/* 09 */ GLFW_KEY_V,
|
|
|
|
/* 0a */ GLFW_KEY_GRAVE_ACCENT,
|
|
|
|
/* 0b */ GLFW_KEY_B,
|
|
|
|
/* 0c */ GLFW_KEY_Q,
|
|
|
|
/* 0d */ GLFW_KEY_W,
|
|
|
|
/* 0e */ GLFW_KEY_E,
|
|
|
|
/* 0f */ GLFW_KEY_R,
|
|
|
|
/* 10 */ GLFW_KEY_Y,
|
|
|
|
/* 11 */ GLFW_KEY_T,
|
|
|
|
/* 12 */ GLFW_KEY_1,
|
|
|
|
/* 13 */ GLFW_KEY_2,
|
|
|
|
/* 14 */ GLFW_KEY_3,
|
|
|
|
/* 15 */ GLFW_KEY_4,
|
|
|
|
/* 16 */ GLFW_KEY_6,
|
|
|
|
/* 17 */ GLFW_KEY_5,
|
|
|
|
/* 18 */ GLFW_KEY_EQUAL,
|
|
|
|
/* 19 */ GLFW_KEY_9,
|
|
|
|
/* 1a */ GLFW_KEY_7,
|
|
|
|
/* 1b */ GLFW_KEY_MINUS,
|
|
|
|
/* 1c */ GLFW_KEY_8,
|
|
|
|
/* 1d */ GLFW_KEY_0,
|
|
|
|
/* 1e */ GLFW_KEY_RIGHT_BRACKET,
|
|
|
|
/* 1f */ GLFW_KEY_O,
|
|
|
|
/* 20 */ GLFW_KEY_U,
|
|
|
|
/* 21 */ GLFW_KEY_LEFT_BRACKET,
|
|
|
|
/* 22 */ GLFW_KEY_I,
|
|
|
|
/* 23 */ GLFW_KEY_P,
|
|
|
|
/* 24 */ GLFW_KEY_ENTER,
|
|
|
|
/* 25 */ GLFW_KEY_L,
|
|
|
|
/* 26 */ GLFW_KEY_J,
|
|
|
|
/* 27 */ GLFW_KEY_APOSTROPHE,
|
|
|
|
/* 28 */ GLFW_KEY_K,
|
|
|
|
/* 29 */ GLFW_KEY_SEMICOLON,
|
|
|
|
/* 2a */ GLFW_KEY_BACKSLASH,
|
|
|
|
/* 2b */ GLFW_KEY_COMMA,
|
|
|
|
/* 2c */ GLFW_KEY_SLASH,
|
|
|
|
/* 2d */ GLFW_KEY_N,
|
|
|
|
/* 2e */ GLFW_KEY_M,
|
|
|
|
/* 2f */ GLFW_KEY_PERIOD,
|
|
|
|
/* 30 */ GLFW_KEY_TAB,
|
|
|
|
/* 31 */ GLFW_KEY_SPACE,
|
|
|
|
/* 32 */ GLFW_KEY_WORLD_1,
|
|
|
|
/* 33 */ GLFW_KEY_BACKSPACE,
|
|
|
|
/* 34 */ -1,
|
|
|
|
/* 35 */ GLFW_KEY_ESCAPE,
|
|
|
|
/* 36 */ GLFW_KEY_RIGHT_SUPER,
|
|
|
|
/* 37 */ GLFW_KEY_LEFT_SUPER,
|
|
|
|
/* 38 */ GLFW_KEY_LEFT_SHIFT,
|
|
|
|
/* 39 */ GLFW_KEY_CAPS_LOCK,
|
|
|
|
/* 3a */ GLFW_KEY_LEFT_ALT,
|
|
|
|
/* 3b */ GLFW_KEY_LEFT_CONTROL,
|
|
|
|
/* 3c */ GLFW_KEY_RIGHT_SHIFT,
|
|
|
|
/* 3d */ GLFW_KEY_RIGHT_ALT,
|
|
|
|
/* 3e */ GLFW_KEY_RIGHT_CONTROL,
|
|
|
|
/* 3f */ -1, /* Function */
|
|
|
|
/* 40 */ GLFW_KEY_F17,
|
|
|
|
/* 41 */ GLFW_KEY_KP_DECIMAL,
|
|
|
|
/* 42 */ -1,
|
|
|
|
/* 43 */ GLFW_KEY_KP_MULTIPLY,
|
|
|
|
/* 44 */ -1,
|
|
|
|
/* 45 */ GLFW_KEY_KP_ADD,
|
|
|
|
/* 46 */ -1,
|
|
|
|
/* 47 */ GLFW_KEY_NUM_LOCK, /* Really KeypadClear... */
|
|
|
|
/* 48 */ -1, /* VolumeUp */
|
|
|
|
/* 49 */ -1, /* VolumeDown */
|
|
|
|
/* 4a */ -1, /* Mute */
|
|
|
|
/* 4b */ GLFW_KEY_KP_DIVIDE,
|
|
|
|
/* 4c */ GLFW_KEY_KP_ENTER,
|
|
|
|
/* 4d */ -1,
|
|
|
|
/* 4e */ GLFW_KEY_KP_SUBTRACT,
|
|
|
|
/* 4f */ GLFW_KEY_F18,
|
|
|
|
/* 50 */ GLFW_KEY_F19,
|
|
|
|
/* 51 */ GLFW_KEY_KP_EQUAL,
|
|
|
|
/* 52 */ GLFW_KEY_KP_0,
|
|
|
|
/* 53 */ GLFW_KEY_KP_1,
|
|
|
|
/* 54 */ GLFW_KEY_KP_2,
|
|
|
|
/* 55 */ GLFW_KEY_KP_3,
|
|
|
|
/* 56 */ GLFW_KEY_KP_4,
|
|
|
|
/* 57 */ GLFW_KEY_KP_5,
|
|
|
|
/* 58 */ GLFW_KEY_KP_6,
|
|
|
|
/* 59 */ GLFW_KEY_KP_7,
|
|
|
|
/* 5a */ GLFW_KEY_F20,
|
|
|
|
/* 5b */ GLFW_KEY_KP_8,
|
|
|
|
/* 5c */ GLFW_KEY_KP_9,
|
|
|
|
/* 5d */ -1,
|
|
|
|
/* 5e */ -1,
|
|
|
|
/* 5f */ -1,
|
|
|
|
/* 60 */ GLFW_KEY_F5,
|
|
|
|
/* 61 */ GLFW_KEY_F6,
|
|
|
|
/* 62 */ GLFW_KEY_F7,
|
|
|
|
/* 63 */ GLFW_KEY_F3,
|
|
|
|
/* 64 */ GLFW_KEY_F8,
|
|
|
|
/* 65 */ GLFW_KEY_F9,
|
|
|
|
/* 66 */ -1,
|
|
|
|
/* 67 */ GLFW_KEY_F11,
|
|
|
|
/* 68 */ -1,
|
2013-02-21 17:39:22 +00:00
|
|
|
/* 69 */ GLFW_KEY_PRINT_SCREEN,
|
2012-03-07 14:13:41 +00:00
|
|
|
/* 6a */ GLFW_KEY_F16,
|
|
|
|
/* 6b */ GLFW_KEY_F14,
|
|
|
|
/* 6c */ -1,
|
|
|
|
/* 6d */ GLFW_KEY_F10,
|
|
|
|
/* 6e */ -1,
|
|
|
|
/* 6f */ GLFW_KEY_F12,
|
|
|
|
/* 70 */ -1,
|
|
|
|
/* 71 */ GLFW_KEY_F15,
|
|
|
|
/* 72 */ GLFW_KEY_INSERT, /* Really Help... */
|
|
|
|
/* 73 */ GLFW_KEY_HOME,
|
|
|
|
/* 74 */ GLFW_KEY_PAGE_UP,
|
|
|
|
/* 75 */ GLFW_KEY_DELETE,
|
|
|
|
/* 76 */ GLFW_KEY_F4,
|
|
|
|
/* 77 */ GLFW_KEY_END,
|
|
|
|
/* 78 */ GLFW_KEY_F2,
|
|
|
|
/* 79 */ GLFW_KEY_PAGE_DOWN,
|
|
|
|
/* 7a */ GLFW_KEY_F1,
|
|
|
|
/* 7b */ GLFW_KEY_LEFT,
|
|
|
|
/* 7c */ GLFW_KEY_RIGHT,
|
|
|
|
/* 7d */ GLFW_KEY_DOWN,
|
|
|
|
/* 7e */ GLFW_KEY_UP,
|
|
|
|
/* 7f */ -1,
|
|
|
|
};
|
|
|
|
|
2010-09-15 16:57:25 +00:00
|
|
|
if (macKeyCode >= 128)
|
2010-09-07 15:34:51 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-03-07 14:13:41 +00:00
|
|
|
return table[macKeyCode];
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 14:04:14 +00:00
|
|
|
|
2013-02-04 12:22:10 +00:00
|
|
|
//------------------------------------------------------------------------
|
2010-09-07 15:34:51 +00:00
|
|
|
// Content view class for the GLFW window
|
2013-02-04 12:22:10 +00:00
|
|
|
//------------------------------------------------------------------------
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
@interface GLFWContentView : NSView
|
2010-09-15 16:57:25 +00:00
|
|
|
{
|
|
|
|
_GLFWwindow* window;
|
2012-03-26 12:46:42 +00:00
|
|
|
NSTrackingArea* trackingArea;
|
2010-09-15 16:57:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 22:39:35 +00:00
|
|
|
- (id)initWithGlfwWindow:(_GLFWwindow *)initWindow;
|
2010-09-15 16:57:25 +00:00
|
|
|
|
2010-09-07 15:34:51 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GLFWContentView
|
|
|
|
|
2013-04-16 22:37:06 +00:00
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
if (self == [GLFWContentView class])
|
|
|
|
{
|
2013-04-21 20:46:35 +00:00
|
|
|
if (_glfw.ns.cursor == nil)
|
|
|
|
{
|
|
|
|
NSImage* data = [[NSImage alloc] initWithSize:NSMakeSize(1, 1)];
|
|
|
|
_glfw.ns.cursor = [[NSCursor alloc] initWithImage:data
|
|
|
|
hotSpot:NSZeroPoint];
|
|
|
|
[data release];
|
|
|
|
}
|
2013-04-16 22:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-16 22:39:35 +00:00
|
|
|
- (id)initWithGlfwWindow:(_GLFWwindow *)initWindow
|
2010-09-15 16:57:25 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil)
|
2012-03-26 12:46:42 +00:00
|
|
|
{
|
2010-09-15 16:57:25 +00:00
|
|
|
window = initWindow;
|
2012-03-26 12:46:42 +00:00
|
|
|
trackingArea = nil;
|
|
|
|
|
|
|
|
[self updateTrackingAreas];
|
|
|
|
}
|
2010-09-15 16:57:25 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-03-26 12:46:42 +00:00
|
|
|
-(void)dealloc
|
|
|
|
{
|
|
|
|
[trackingArea release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2010-09-07 15:34:51 +00:00
|
|
|
- (BOOL)isOpaque
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canBecomeKeyView
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)acceptsFirstResponder
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDown:(NSEvent *)event
|
|
|
|
{
|
2010-09-15 16:57:25 +00:00
|
|
|
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDragged:(NSEvent *)event
|
|
|
|
{
|
|
|
|
[self mouseMoved:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseUp:(NSEvent *)event
|
|
|
|
{
|
2010-09-15 16:57:25 +00:00
|
|
|
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseMoved:(NSEvent *)event
|
|
|
|
{
|
2011-09-06 11:55:29 +00:00
|
|
|
if (window->cursorMode == GLFW_CURSOR_CAPTURED)
|
2011-10-09 15:10:40 +00:00
|
|
|
_glfwInputCursorMotion(window, [event deltaX], [event deltaY]);
|
2010-09-07 15:34:51 +00:00
|
|
|
else
|
|
|
|
{
|
2013-02-11 22:10:49 +00:00
|
|
|
const NSRect contentRect =
|
|
|
|
[window->ns.object contentRectForFrameRect:[window->ns.object frame]];
|
2012-07-01 22:36:20 +00:00
|
|
|
const NSPoint p = [event locationInWindow];
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-04-04 14:16:21 +00:00
|
|
|
_glfwInputCursorMotion(window, p.x, contentRect.size.height - p.y);
|
2011-05-21 20:50:25 +00:00
|
|
|
}
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)rightMouseDown:(NSEvent *)event
|
|
|
|
{
|
2010-09-15 16:57:25 +00:00
|
|
|
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)rightMouseDragged:(NSEvent *)event
|
|
|
|
{
|
|
|
|
[self mouseMoved:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)rightMouseUp:(NSEvent *)event
|
|
|
|
{
|
2010-09-15 16:57:25 +00:00
|
|
|
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_RIGHT, GLFW_RELEASE);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherMouseDown:(NSEvent *)event
|
|
|
|
{
|
2010-09-15 16:57:25 +00:00
|
|
|
_glfwInputMouseClick(window, [event buttonNumber], GLFW_PRESS);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherMouseDragged:(NSEvent *)event
|
|
|
|
{
|
|
|
|
[self mouseMoved:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherMouseUp:(NSEvent *)event
|
|
|
|
{
|
2010-09-15 16:57:25 +00:00
|
|
|
_glfwInputMouseClick(window, [event buttonNumber], GLFW_RELEASE);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 12:46:42 +00:00
|
|
|
- (void)mouseExited:(NSEvent *)event
|
|
|
|
{
|
|
|
|
_glfwInputCursorEnter(window, GL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseEntered:(NSEvent *)event
|
|
|
|
{
|
|
|
|
_glfwInputCursorEnter(window, GL_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateTrackingAreas
|
|
|
|
{
|
|
|
|
if (trackingArea != nil)
|
|
|
|
{
|
|
|
|
[self removeTrackingArea:trackingArea];
|
|
|
|
[trackingArea release];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited |
|
|
|
|
NSTrackingActiveAlways |
|
|
|
|
NSTrackingInVisibleRect;
|
|
|
|
|
|
|
|
trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
|
|
|
|
options:options
|
|
|
|
owner:self
|
|
|
|
userInfo:nil];
|
|
|
|
|
|
|
|
[self addTrackingArea:trackingArea];
|
2012-08-02 15:45:52 +00:00
|
|
|
[super updateTrackingAreas];
|
2012-03-26 12:46:42 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 15:34:51 +00:00
|
|
|
- (void)keyDown:(NSEvent *)event
|
|
|
|
{
|
2013-03-07 16:37:07 +00:00
|
|
|
_glfwInputKey(window, convertMacKeyCode([event keyCode]), GLFW_PRESS);
|
2013-01-12 16:06:35 +00:00
|
|
|
|
|
|
|
if ([event modifierFlags] & NSCommandKeyMask)
|
2013-04-30 17:02:31 +00:00
|
|
|
return;
|
2013-01-12 16:06:35 +00:00
|
|
|
|
2013-04-30 17:02:31 +00:00
|
|
|
NSString* characters = [event characters];
|
|
|
|
NSUInteger i, length = [characters length];
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
_glfwInputChar(window, [characters characterAtIndex:i]);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)flagsChanged:(NSEvent *)event
|
|
|
|
{
|
2013-04-21 19:54:33 +00:00
|
|
|
int action, key;
|
2010-09-15 16:57:25 +00:00
|
|
|
unsigned int newModifierFlags =
|
2013-04-08 01:04:10 +00:00
|
|
|
[event modifierFlags] & NSDeviceIndependentModifierFlagsMask;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
if (newModifierFlags > window->ns.modifierFlags)
|
2013-04-21 19:54:33 +00:00
|
|
|
action = GLFW_PRESS;
|
2010-09-07 15:34:51 +00:00
|
|
|
else
|
2013-04-21 19:54:33 +00:00
|
|
|
action = GLFW_RELEASE;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
window->ns.modifierFlags = newModifierFlags;
|
2012-03-07 14:10:53 +00:00
|
|
|
|
|
|
|
key = convertMacKeyCode([event keyCode]);
|
|
|
|
if (key != -1)
|
2013-04-21 19:54:33 +00:00
|
|
|
_glfwInputKey(window, key, action);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)keyUp:(NSEvent *)event
|
|
|
|
{
|
2013-03-07 16:37:07 +00:00
|
|
|
_glfwInputKey(window, convertMacKeyCode([event keyCode]), GLFW_RELEASE);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scrollWheel:(NSEvent *)event
|
|
|
|
{
|
2012-03-28 20:39:48 +00:00
|
|
|
double deltaX = [event deltaX];
|
|
|
|
double deltaY = [event deltaY];
|
|
|
|
|
|
|
|
if (fabs(deltaX) > 0.0 || fabs(deltaY) > 0.0)
|
|
|
|
_glfwInputScroll(window, deltaX, deltaY);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 22:37:06 +00:00
|
|
|
- (void)resetCursorRects
|
|
|
|
{
|
2013-04-21 22:12:25 +00:00
|
|
|
[self discardCursorRects];
|
2013-04-21 20:46:35 +00:00
|
|
|
[self addCursorRect:[self bounds] cursor:_glfw.ns.cursor];
|
2013-04-16 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 15:34:51 +00:00
|
|
|
@end
|
|
|
|
|
2012-03-07 14:04:14 +00:00
|
|
|
|
2013-04-08 13:16:32 +00:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// GLFW window class
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
@interface GLFWWindow : NSWindow {}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GLFWWindow
|
|
|
|
|
|
|
|
- (BOOL)canBecomeKeyWindow
|
|
|
|
{
|
|
|
|
// Required for NSBorderlessWindowMask windows
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2013-02-04 12:22:10 +00:00
|
|
|
//------------------------------------------------------------------------
|
2012-02-15 00:44:55 +00:00
|
|
|
// GLFW application class
|
2013-02-04 12:22:10 +00:00
|
|
|
//------------------------------------------------------------------------
|
2012-02-15 00:44:55 +00:00
|
|
|
|
|
|
|
@interface GLFWApplication : NSApplication
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GLFWApplication
|
|
|
|
|
|
|
|
// From http://cocoadev.com/index.pl?GameKeyboardHandlingAlmost
|
|
|
|
// This works around an AppKit bug, where key up events while holding
|
|
|
|
// down the command key don't get sent to the key window.
|
|
|
|
- (void)sendEvent:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([event type] == NSKeyUp && ([event modifierFlags] & NSCommandKeyMask))
|
|
|
|
[[self keyWindow] sendEvent:event];
|
|
|
|
else
|
|
|
|
[super sendEvent:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-02-24 21:04:54 +00:00
|
|
|
#if defined(_GLFW_USE_MENUBAR)
|
|
|
|
|
2012-02-15 00:44:55 +00:00
|
|
|
// Try to figure out what the calling application is called
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-02-15 00:44:55 +00:00
|
|
|
static NSString* findAppName(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
|
|
|
|
|
|
|
|
// Keys to search for as potential application names
|
|
|
|
NSString* GLFWNameKeys[] =
|
|
|
|
{
|
|
|
|
@"CFBundleDisplayName",
|
|
|
|
@"CFBundleName",
|
|
|
|
@"CFBundleExecutable",
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(GLFWNameKeys) / sizeof(GLFWNameKeys[0]); i++)
|
|
|
|
{
|
|
|
|
id name = [infoDictionary objectForKey:GLFWNameKeys[i]];
|
|
|
|
if (name &&
|
|
|
|
[name isKindOfClass:[NSString class]] &&
|
|
|
|
![@"" isEqualToString:name])
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 13:38:18 +00:00
|
|
|
// If we get here, the application is unbundled
|
2012-02-15 00:44:55 +00:00
|
|
|
ProcessSerialNumber psn = { 0, kCurrentProcess };
|
|
|
|
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
|
|
|
|
|
|
|
|
// Having the app in front of the terminal window is also generally
|
|
|
|
// handy. There is an NSApplication API to do this, but...
|
|
|
|
SetFrontProcess(&psn);
|
|
|
|
|
|
|
|
char** progname = _NSGetProgname();
|
|
|
|
if (progname && *progname)
|
|
|
|
return [NSString stringWithUTF8String:*progname];
|
|
|
|
|
|
|
|
// Really shouldn't get here
|
|
|
|
return @"GLFW Application";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the menu bar (manually)
|
|
|
|
// This is nasty, nasty stuff -- calls to undocumented semi-private APIs that
|
|
|
|
// could go away at any moment, lots of stuff that really should be
|
|
|
|
// localize(d|able), etc. Loading a nib would save us this horror, but that
|
|
|
|
// doesn't seem like a good thing to require of GLFW's clients.
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-03-05 19:30:59 +00:00
|
|
|
static void createMenuBar(void)
|
2012-02-15 00:44:55 +00:00
|
|
|
{
|
|
|
|
NSString* appName = findAppName();
|
|
|
|
|
|
|
|
NSMenu* bar = [[NSMenu alloc] init];
|
|
|
|
[NSApp setMainMenu:bar];
|
|
|
|
|
|
|
|
NSMenuItem* appMenuItem =
|
|
|
|
[bar addItemWithTitle:@"" action:NULL keyEquivalent:@""];
|
|
|
|
NSMenu* appMenu = [[NSMenu alloc] init];
|
|
|
|
[appMenuItem setSubmenu:appMenu];
|
|
|
|
|
|
|
|
[appMenu addItemWithTitle:[NSString stringWithFormat:@"About %@", appName]
|
|
|
|
action:@selector(orderFrontStandardAboutPanel:)
|
|
|
|
keyEquivalent:@""];
|
|
|
|
[appMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
NSMenu* servicesMenu = [[NSMenu alloc] init];
|
|
|
|
[NSApp setServicesMenu:servicesMenu];
|
|
|
|
[[appMenu addItemWithTitle:@"Services"
|
|
|
|
action:NULL
|
|
|
|
keyEquivalent:@""] setSubmenu:servicesMenu];
|
|
|
|
[appMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
[appMenu addItemWithTitle:[NSString stringWithFormat:@"Hide %@", appName]
|
|
|
|
action:@selector(hide:)
|
|
|
|
keyEquivalent:@"h"];
|
|
|
|
[[appMenu addItemWithTitle:@"Hide Others"
|
|
|
|
action:@selector(hideOtherApplications:)
|
|
|
|
keyEquivalent:@"h"]
|
|
|
|
setKeyEquivalentModifierMask:NSAlternateKeyMask | NSCommandKeyMask];
|
|
|
|
[appMenu addItemWithTitle:@"Show All"
|
|
|
|
action:@selector(unhideAllApplications:)
|
|
|
|
keyEquivalent:@""];
|
|
|
|
[appMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
[appMenu addItemWithTitle:[NSString stringWithFormat:@"Quit %@", appName]
|
|
|
|
action:@selector(terminate:)
|
|
|
|
keyEquivalent:@"q"];
|
|
|
|
|
|
|
|
NSMenuItem* windowMenuItem =
|
|
|
|
[bar addItemWithTitle:@"" action:NULL keyEquivalent:@""];
|
|
|
|
NSMenu* windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
|
|
|
|
[NSApp setWindowsMenu:windowMenu];
|
|
|
|
[windowMenuItem setSubmenu:windowMenu];
|
|
|
|
|
|
|
|
[windowMenu addItemWithTitle:@"Miniaturize"
|
|
|
|
action:@selector(performMiniaturize:)
|
|
|
|
keyEquivalent:@"m"];
|
|
|
|
[windowMenu addItemWithTitle:@"Zoom"
|
|
|
|
action:@selector(performZoom:)
|
|
|
|
keyEquivalent:@""];
|
|
|
|
[windowMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
[windowMenu addItemWithTitle:@"Bring All to Front"
|
|
|
|
action:@selector(arrangeInFront:)
|
|
|
|
keyEquivalent:@""];
|
|
|
|
|
2012-03-05 19:09:06 +00:00
|
|
|
// Prior to Snow Leopard, we need to use this oddly-named semi-private API
|
|
|
|
// to get the application menu working properly.
|
2012-03-05 21:05:40 +00:00
|
|
|
[NSApp performSelector:@selector(setAppleMenu:) withObject:appMenu];
|
2012-02-15 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 18:45:21 +00:00
|
|
|
#endif /* _GLFW_USE_MENUBAR */
|
|
|
|
|
2012-02-15 00:44:55 +00:00
|
|
|
// Initialize the Cocoa Application Kit
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-02-26 02:24:42 +00:00
|
|
|
static GLboolean initializeAppKit(void)
|
2012-02-15 00:44:55 +00:00
|
|
|
{
|
|
|
|
if (NSApp)
|
|
|
|
return GL_TRUE;
|
|
|
|
|
|
|
|
// Implicitly create shared NSApplication instance
|
|
|
|
[GLFWApplication sharedApplication];
|
|
|
|
|
2013-01-24 18:45:21 +00:00
|
|
|
#if defined(_GLFW_USE_MENUBAR)
|
2012-10-17 15:11:56 +00:00
|
|
|
// Menu bar setup must go between sharedApplication above and
|
|
|
|
// finishLaunching below, in order to properly emulate the behavior
|
|
|
|
// of NSApplicationMain
|
2012-03-05 19:30:59 +00:00
|
|
|
createMenuBar();
|
2013-01-24 18:45:21 +00:00
|
|
|
#endif
|
2012-02-15 00:44:55 +00:00
|
|
|
|
|
|
|
[NSApp finishLaunching];
|
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-01-31 16:53:19 +00:00
|
|
|
// Create the Cocoa window
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-01-31 16:53:19 +00:00
|
|
|
static GLboolean createWindow(_GLFWwindow* window,
|
|
|
|
const _GLFWwndconfig* wndconfig)
|
|
|
|
{
|
|
|
|
unsigned int styleMask = 0;
|
|
|
|
|
2013-04-08 13:16:32 +00:00
|
|
|
if (wndconfig->monitor || !wndconfig->decorated)
|
2012-09-27 19:37:36 +00:00
|
|
|
styleMask = NSBorderlessWindowMask;
|
|
|
|
else
|
2012-01-31 16:53:19 +00:00
|
|
|
{
|
|
|
|
styleMask = NSTitledWindowMask | NSClosableWindowMask |
|
|
|
|
NSMiniaturizableWindowMask;
|
|
|
|
|
|
|
|
if (wndconfig->resizable)
|
|
|
|
styleMask |= NSResizableWindowMask;
|
|
|
|
}
|
|
|
|
|
2013-04-08 01:07:52 +00:00
|
|
|
window->ns.object = [[GLFWWindow alloc]
|
2013-02-11 22:10:49 +00:00
|
|
|
initWithContentRect:NSMakeRect(0, 0, wndconfig->width, wndconfig->height)
|
2012-01-31 16:53:19 +00:00
|
|
|
styleMask:styleMask
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:NO];
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
if (window->ns.object == nil)
|
2012-01-31 16:53:19 +00:00
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to create window");
|
2012-01-31 16:53:19 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
window->ns.view = [[GLFWContentView alloc] initWithGlfwWindow:window];
|
2012-03-26 13:29:01 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object setTitle:[NSString stringWithUTF8String:wndconfig->title]];
|
|
|
|
[window->ns.object setContentView:window->ns.view];
|
|
|
|
[window->ns.object setDelegate:window->ns.delegate];
|
|
|
|
[window->ns.object setAcceptsMouseMovedEvents:YES];
|
2013-04-21 22:12:25 +00:00
|
|
|
[window->ns.object disableCursorRects];
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object center];
|
2012-01-31 16:53:19 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
if ([window->ns.object respondsToSelector:@selector(setRestorable:)])
|
|
|
|
[window->ns.object setRestorable:NO];
|
2012-07-02 13:24:02 +00:00
|
|
|
|
2012-01-31 16:53:19 +00:00
|
|
|
return GL_TRUE;
|
|
|
|
}
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-03-07 14:04:14 +00:00
|
|
|
|
2012-01-31 16:53:19 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW platform API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-08-06 15:56:41 +00:00
|
|
|
int _glfwPlatformCreateWindow(_GLFWwindow* window,
|
|
|
|
const _GLFWwndconfig* wndconfig,
|
|
|
|
const _GLFWfbconfig* fbconfig)
|
2012-01-31 16:53:19 +00:00
|
|
|
{
|
2012-02-26 02:24:42 +00:00
|
|
|
if (!initializeAppKit())
|
2012-02-15 00:44:55 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
|
2012-08-26 13:38:18 +00:00
|
|
|
// There can only be one application delegate, but we allocate it the
|
|
|
|
// first time a window is created to keep all window code in this file
|
2013-01-02 00:40:42 +00:00
|
|
|
if (_glfw.ns.delegate == nil)
|
2012-01-31 16:53:19 +00:00
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.ns.delegate = [[GLFWApplicationDelegate alloc] init];
|
|
|
|
if (_glfw.ns.delegate == nil)
|
2012-01-31 16:53:19 +00:00
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Cocoa: Failed to create application delegate");
|
2012-01-31 16:53:19 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
[NSApp setDelegate:_glfw.ns.delegate];
|
2012-01-31 16:53:19 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
window->ns.delegate = [[GLFWWindowDelegate alloc] initWithGlfwWindow:window];
|
|
|
|
if (window->ns.delegate == nil)
|
2012-01-31 16:53:19 +00:00
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Cocoa: Failed to create window delegate");
|
2012-01-31 16:53:19 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't use accumulation buffer support; it's not accelerated
|
|
|
|
// Aux buffers probably aren't accelerated either
|
|
|
|
|
|
|
|
if (!createWindow(window, wndconfig))
|
|
|
|
return GL_FALSE;
|
|
|
|
|
2012-12-29 23:58:18 +00:00
|
|
|
if (!_glfwCreateContext(window, wndconfig, fbconfig))
|
2012-01-31 16:53:19 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
|
2013-04-30 17:40:29 +00:00
|
|
|
[window->nsgl.context setView:window->ns.view];
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-09-27 19:37:36 +00:00
|
|
|
if (wndconfig->monitor)
|
2013-05-02 16:16:23 +00:00
|
|
|
enterFullscreenMode(window);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
NSPoint point = [[NSCursor currentCursor] hotSpot];
|
2011-10-13 13:20:59 +00:00
|
|
|
window->cursorPosX = point.x;
|
|
|
|
window->cursorPosY = point.y;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-08-06 15:56:41 +00:00
|
|
|
void _glfwPlatformDestroyWindow(_GLFWwindow* window)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object orderOut:nil];
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-09-27 19:37:36 +00:00
|
|
|
if (window->monitor)
|
2013-05-02 16:16:23 +00:00
|
|
|
leaveFullscreenMode(window);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-12-29 23:58:18 +00:00
|
|
|
_glfwDestroyContext(window);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object setDelegate:nil];
|
|
|
|
[window->ns.delegate release];
|
|
|
|
window->ns.delegate = nil;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.view release];
|
|
|
|
window->ns.view = nil;
|
2012-03-26 13:29:01 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object close];
|
|
|
|
window->ns.object = nil;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 16:57:25 +00:00
|
|
|
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char *title)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object setTitle:[NSString stringWithUTF8String:title]];
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 18:30:31 +00:00
|
|
|
void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos)
|
|
|
|
{
|
|
|
|
const NSRect contentRect =
|
|
|
|
[window->ns.object contentRectForFrameRect:[window->ns.object frame]];
|
|
|
|
|
|
|
|
if (xpos)
|
|
|
|
*xpos = contentRect.origin.x;
|
|
|
|
if (ypos)
|
2013-05-22 19:37:41 +00:00
|
|
|
*ypos = transformY(contentRect.origin.y + contentRect.size.height);
|
2013-01-24 18:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _glfwPlatformSetWindowPos(_GLFWwindow* window, int x, int y)
|
|
|
|
{
|
|
|
|
const NSRect frameRect =
|
|
|
|
[window->ns.object frameRectForContentRect:NSMakeRect(x, y, 0, 0)];
|
|
|
|
[window->ns.object setFrameOrigin:frameRect.origin];
|
|
|
|
}
|
|
|
|
|
2012-11-25 13:53:33 +00:00
|
|
|
void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
|
|
|
|
{
|
2013-01-24 18:30:31 +00:00
|
|
|
const NSRect contentRect =
|
2012-11-25 13:53:33 +00:00
|
|
|
[window->ns.object contentRectForFrameRect:[window->ns.object frame]];
|
|
|
|
|
|
|
|
if (width)
|
|
|
|
*width = contentRect.size.width;
|
|
|
|
if (height)
|
|
|
|
*height = contentRect.size.height;
|
|
|
|
}
|
|
|
|
|
2010-09-15 16:57:25 +00:00
|
|
|
void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object setContentSize:NSMakeSize(width, height)];
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 16:57:25 +00:00
|
|
|
void _glfwPlatformIconifyWindow(_GLFWwindow* window)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2013-05-02 16:16:23 +00:00
|
|
|
if (window->monitor)
|
|
|
|
leaveFullscreenMode(window);
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object miniaturize:nil];
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 16:57:25 +00:00
|
|
|
void _glfwPlatformRestoreWindow(_GLFWwindow* window)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object deminiaturize:nil];
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 18:01:57 +00:00
|
|
|
void _glfwPlatformShowWindow(_GLFWwindow* window)
|
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object makeKeyAndOrderFront:nil];
|
2012-08-21 19:35:42 +00:00
|
|
|
_glfwInputWindowVisibility(window, GL_TRUE);
|
2012-08-21 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _glfwPlatformHideWindow(_GLFWwindow* window)
|
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
[window->ns.object orderOut:nil];
|
2012-08-21 19:35:42 +00:00
|
|
|
_glfwInputWindowVisibility(window, GL_FALSE);
|
2012-08-21 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 16:57:25 +00:00
|
|
|
void _glfwPlatformPollEvents(void)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-10-21 14:17:20 +00:00
|
|
|
for (;;)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-10-21 14:17:20 +00:00
|
|
|
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
|
|
|
|
untilDate:[NSDate distantPast]
|
|
|
|
inMode:NSDefaultRunLoopMode
|
|
|
|
dequeue:YES];
|
|
|
|
if (event == nil)
|
|
|
|
break;
|
|
|
|
|
|
|
|
[NSApp sendEvent:event];
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
[_glfw.ns.autoreleasePool drain];
|
|
|
|
_glfw.ns.autoreleasePool = [[NSAutoreleasePool alloc] init];
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 03:42:47 +00:00
|
|
|
void _glfwPlatformWaitEvents(void)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
|
|
|
// I wanted to pass NO to dequeue:, and rely on PollEvents to
|
|
|
|
// dequeue and send. For reasons not at all clear to me, passing
|
|
|
|
// NO to dequeue: causes this method never to return.
|
|
|
|
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
|
|
|
|
untilDate:[NSDate distantFuture]
|
|
|
|
inMode:NSDefaultRunLoopMode
|
|
|
|
dequeue:YES];
|
|
|
|
[NSApp sendEvent:event];
|
|
|
|
|
|
|
|
_glfwPlatformPollEvents();
|
|
|
|
}
|
|
|
|
|
2013-03-27 23:30:08 +00:00
|
|
|
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-09-27 19:37:36 +00:00
|
|
|
if (window->monitor)
|
2012-07-02 13:23:36 +00:00
|
|
|
{
|
2013-05-22 16:03:54 +00:00
|
|
|
CGDisplayMoveCursorToPoint(window->monitor->ns.displayID,
|
|
|
|
CGPointMake(x, y));
|
2012-07-02 13:23:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-11 22:10:49 +00:00
|
|
|
const NSRect contentRect =
|
|
|
|
[window->ns.object contentRectForFrameRect:[window->ns.object frame]];
|
2013-05-22 16:03:54 +00:00
|
|
|
const NSPoint localPoint = NSMakePoint(x, contentRect.size.height - y - 1);
|
|
|
|
const NSPoint globalPoint = [window->ns.object convertBaseToScreen:localPoint];
|
|
|
|
|
|
|
|
CGWarpMouseCursorPosition(CGPointMake(globalPoint.x,
|
|
|
|
transformY(globalPoint.y)));
|
2012-07-02 13:23:36 +00:00
|
|
|
}
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 15:38:00 +00:00
|
|
|
void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
|
|
|
|
{
|
2013-04-21 22:38:51 +00:00
|
|
|
if (mode == GLFW_CURSOR_HIDDEN)
|
|
|
|
{
|
|
|
|
[window->ns.object enableCursorRects];
|
|
|
|
[window->ns.object invalidateCursorRectsForView:window->ns.view];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[window->ns.object disableCursorRects];
|
|
|
|
[window->ns.object invalidateCursorRectsForView:window->ns.view];
|
2013-04-16 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-21 22:38:51 +00:00
|
|
|
if (mode == GLFW_CURSOR_CAPTURED)
|
2011-09-06 15:38:00 +00:00
|
|
|
{
|
2013-04-21 22:38:51 +00:00
|
|
|
CGAssociateMouseAndMouseCursorPosition(false);
|
|
|
|
|
|
|
|
if (!_glfw.ns.cursorHidden)
|
|
|
|
{
|
|
|
|
[NSCursor hide];
|
|
|
|
_glfw.ns.cursorHidden = GL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CGAssociateMouseAndMouseCursorPosition(true);
|
|
|
|
|
|
|
|
if (_glfw.ns.cursorHidden)
|
|
|
|
{
|
|
|
|
[NSCursor unhide];
|
|
|
|
_glfw.ns.cursorHidden = GL_FALSE;
|
|
|
|
}
|
2011-09-06 15:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 21:38:14 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW native API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GLFWAPI id glfwGetCocoaWindow(GLFWwindow* handle)
|
|
|
|
{
|
|
|
|
_GLFWwindow* window = (_GLFWwindow*) handle;
|
2013-02-19 23:28:08 +00:00
|
|
|
_GLFW_REQUIRE_INIT_OR_RETURN(nil);
|
2013-01-15 21:38:14 +00:00
|
|
|
return window->ns.object;
|
|
|
|
}
|
|
|
|
|