glfw/src/win32_window.c

1365 lines
43 KiB
C
Raw Normal View History

2010-09-07 15:34:51 +00:00
//========================================================================
// GLFW 3.1 Win32 - www.glfw.org
2010-09-07 15:34:51 +00:00
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2006-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"
2010-09-13 23:05:03 +00:00
#include <stdlib.h>
#include <malloc.h>
2012-09-12 19:37:36 +00:00
#include <windowsx.h>
2013-12-22 15:47:03 +00:00
#include <shellapi.h>
2010-09-13 21:50:04 +00:00
2013-05-30 15:19:12 +00:00
#define _GLFW_KEY_INVALID -2
// Updates the cursor clip rect
//
static void updateClipRect(_GLFWwindow* window)
{
RECT clipRect;
GetClientRect(window->win32.handle, &clipRect);
ClientToScreen(window->win32.handle, (POINT*) &clipRect.left);
ClientToScreen(window->win32.handle, (POINT*) &clipRect.right);
ClipCursor(&clipRect);
}
2014-01-15 12:21:13 +00:00
// Hide the mouse cursor
2013-02-04 12:22:10 +00:00
//
static void hideCursor(_GLFWwindow* window)
{
2013-03-11 21:57:39 +00:00
POINT pos;
ReleaseCapture();
ClipCursor(NULL);
2013-04-14 13:36:10 +00:00
if (window->win32.cursorHidden)
{
ShowCursor(TRUE);
window->win32.cursorHidden = GL_FALSE;
}
2013-03-11 21:57:39 +00:00
if (GetCursorPos(&pos))
{
if (WindowFromPoint(pos) == window->win32.handle)
SetCursor(NULL);
2013-04-14 13:36:10 +00:00
}
}
2014-01-15 12:21:13 +00:00
// Disable the mouse cursor
2013-02-04 12:22:10 +00:00
//
2014-01-15 12:21:13 +00:00
static void disableCursor(_GLFWwindow* window)
{
2013-04-14 13:36:10 +00:00
if (!window->win32.cursorHidden)
{
ShowCursor(FALSE);
window->win32.cursorHidden = GL_TRUE;
}
updateClipRect(window);
SetCapture(window->win32.handle);
}
2014-01-15 12:21:13 +00:00
// Restores the mouse cursor
2013-02-04 12:22:10 +00:00
//
2014-01-15 12:21:13 +00:00
static void restoreCursor(_GLFWwindow* window)
{
2013-03-11 21:57:39 +00:00
POINT pos;
2012-10-30 16:37:34 +00:00
ReleaseCapture();
ClipCursor(NULL);
2013-04-14 13:36:10 +00:00
if (window->win32.cursorHidden)
{
ShowCursor(TRUE);
window->win32.cursorHidden = GL_FALSE;
}
2013-03-11 21:57:39 +00:00
if (GetCursorPos(&pos))
{
if (WindowFromPoint(pos) == window->win32.handle)
{
if (window->cursor)
SetCursor(window->cursor->win32.handle);
else
SetCursor(LoadCursorW(NULL, IDC_ARROW));
}
2013-03-11 21:57:39 +00:00
}
}
// Retrieves and translates modifier keys
//
static int getKeyMods(void)
{
int mods = 0;
if (GetKeyState(VK_SHIFT) & (1 << 31))
mods |= GLFW_MOD_SHIFT;
if (GetKeyState(VK_CONTROL) & (1 << 31))
mods |= GLFW_MOD_CONTROL;
if (GetKeyState(VK_MENU) & (1 << 31))
mods |= GLFW_MOD_ALT;
if ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & (1 << 31))
mods |= GLFW_MOD_SUPER;
return mods;
}
// Retrieves and translates modifier keys
//
static int getAsyncKeyMods(void)
{
int mods = 0;
if (GetAsyncKeyState(VK_SHIFT) & (1 << 31))
mods |= GLFW_MOD_SHIFT;
if (GetAsyncKeyState(VK_CONTROL) & (1 << 31))
mods |= GLFW_MOD_CONTROL;
if (GetAsyncKeyState(VK_MENU) & (1 << 31))
mods |= GLFW_MOD_ALT;
if ((GetAsyncKeyState(VK_LWIN) | GetAsyncKeyState(VK_RWIN)) & (1 << 31))
mods |= GLFW_MOD_SUPER;
return mods;
}
2010-09-07 15:34:51 +00:00
// Translates a Windows key to the corresponding GLFW key
2013-02-04 12:22:10 +00:00
//
2010-09-10 20:03:36 +00:00
static int translateKey(WPARAM wParam, LPARAM lParam)
2010-09-07 15:34:51 +00:00
{
2013-02-20 16:44:16 +00:00
// Check for numeric keypad keys
// NOTE: This way we always force "NumLock = ON", which is intentional since
2013-11-10 13:03:07 +00:00
// the returned key code should correspond to a physical location.
2013-04-17 13:29:17 +00:00
if ((HIWORD(lParam) & 0x100) == 0)
2010-09-07 15:34:51 +00:00
{
2013-04-17 13:29:17 +00:00
switch (MapVirtualKey(HIWORD(lParam) & 0xFF, 1))
2010-09-07 15:34:51 +00:00
{
case VK_INSERT: return GLFW_KEY_KP_0;
case VK_END: return GLFW_KEY_KP_1;
case VK_DOWN: return GLFW_KEY_KP_2;
case VK_NEXT: return GLFW_KEY_KP_3;
case VK_LEFT: return GLFW_KEY_KP_4;
case VK_CLEAR: return GLFW_KEY_KP_5;
case VK_RIGHT: return GLFW_KEY_KP_6;
case VK_HOME: return GLFW_KEY_KP_7;
case VK_UP: return GLFW_KEY_KP_8;
case VK_PRIOR: return GLFW_KEY_KP_9;
case VK_DIVIDE: return GLFW_KEY_KP_DIVIDE;
case VK_MULTIPLY: return GLFW_KEY_KP_MULTIPLY;
case VK_SUBTRACT: return GLFW_KEY_KP_SUBTRACT;
case VK_ADD: return GLFW_KEY_KP_ADD;
case VK_DELETE: return GLFW_KEY_KP_DECIMAL;
default: break;
2010-09-07 15:34:51 +00:00
}
}
switch (HIWORD(lParam) & 0xFF)
{
// handle printable chars except space in a language independent way,
// using scancodes rather than virtual keys
// as virtual keys are language dependent.
// Printable keys are mapped according to US layout.
// Row 0:
case 0x29: return GLFW_KEY_GRAVE_ACCENT;
case 0x02: return GLFW_KEY_1;
case 0x03: return GLFW_KEY_2;
case 0x04: return GLFW_KEY_3;
case 0x05: return GLFW_KEY_4;
case 0x06: return GLFW_KEY_5;
case 0x07: return GLFW_KEY_6;
case 0x08: return GLFW_KEY_7;
case 0x09: return GLFW_KEY_8;
case 0x0A: return GLFW_KEY_9;
case 0x0B: return GLFW_KEY_0;
case 0x0C: return GLFW_KEY_MINUS;
case 0x0D: return GLFW_KEY_EQUAL;
// Row 1:
case 0x10: return GLFW_KEY_Q;
case 0x11: return GLFW_KEY_W;
case 0x12: return GLFW_KEY_E;
case 0x13: return GLFW_KEY_R;
case 0x14: return GLFW_KEY_T;
case 0x15: return GLFW_KEY_Y;
case 0x16: return GLFW_KEY_U;
case 0x17: return GLFW_KEY_I;
case 0x18: return GLFW_KEY_O;
case 0x19: return GLFW_KEY_P;
case 0x1A: return GLFW_KEY_LEFT_BRACKET;
case 0x1B: return GLFW_KEY_RIGHT_BRACKET;
// We do not map 0x2B as this is only on US - use vKeys for this to prevent confusion with 0x56
// Row 2:
case 0x1E: return GLFW_KEY_A;
case 0x1F: return GLFW_KEY_S;
case 0x20: return GLFW_KEY_D;
case 0x21: return GLFW_KEY_F;
case 0x22: return GLFW_KEY_G;
case 0x23: return GLFW_KEY_H;
case 0x24: return GLFW_KEY_J;
case 0x25: return GLFW_KEY_K;
case 0x26: return GLFW_KEY_L;
case 0x27: return GLFW_KEY_SEMICOLON;
case 0x28: return GLFW_KEY_APOSTROPHE;
// Row 3:
case 0x2C: return GLFW_KEY_Z;
case 0x2D: return GLFW_KEY_X;
case 0x2E: return GLFW_KEY_C;
case 0x2F: return GLFW_KEY_V;
case 0x30: return GLFW_KEY_B;
2014-03-26 16:31:01 +00:00
case 0x31: return GLFW_KEY_N;
case 0x32: return GLFW_KEY_M;
case 0x33: return GLFW_KEY_COMMA;
case 0x34: return GLFW_KEY_PERIOD;
case 0x35: return GLFW_KEY_SLASH;
default: break;
}
2010-09-07 15:34:51 +00:00
// Check which key was pressed or released
2010-09-10 20:03:36 +00:00
switch (wParam)
2010-09-07 15:34:51 +00:00
{
// The SHIFT keys require special handling
case VK_SHIFT:
{
// Compare scan code for this key with that of VK_RSHIFT in
// order to determine which shift key was pressed (left or
// right)
2013-04-17 13:29:17 +00:00
const DWORD scancode = MapVirtualKey(VK_RSHIFT, 0);
2013-06-12 17:57:55 +00:00
if ((DWORD) ((lParam & 0x01ff0000) >> 16) == scancode)
return GLFW_KEY_RIGHT_SHIFT;
2010-09-07 15:34:51 +00:00
return GLFW_KEY_LEFT_SHIFT;
2010-09-07 15:34:51 +00:00
}
// The CTRL keys require special handling
case VK_CONTROL:
{
2013-04-17 13:29:17 +00:00
MSG next;
2013-04-21 14:28:08 +00:00
DWORD time;
2013-04-17 13:29:17 +00:00
2010-09-07 15:34:51 +00:00
// Is this an extended key (i.e. right key)?
2010-09-10 20:03:36 +00:00
if (lParam & 0x01000000)
return GLFW_KEY_RIGHT_CONTROL;
2010-09-07 15:34:51 +00:00
// Here is a trick: "Alt Gr" sends LCTRL, then RALT. We only
// want the RALT message, so we try to see if the next message
// is a RALT message. In that case, this is a false LCTRL!
2013-04-21 14:28:08 +00:00
time = GetMessageTime();
2013-04-17 13:29:17 +00:00
if (PeekMessageW(&next, NULL, 0, 0, PM_NOREMOVE))
2010-09-07 15:34:51 +00:00
{
2013-04-17 13:29:17 +00:00
if (next.message == WM_KEYDOWN ||
2013-06-05 11:11:00 +00:00
next.message == WM_SYSKEYDOWN ||
next.message == WM_KEYUP ||
next.message == WM_SYSKEYUP)
2010-09-07 15:34:51 +00:00
{
2013-04-17 13:29:17 +00:00
if (next.wParam == VK_MENU &&
(next.lParam & 0x01000000) &&
next.time == time)
2010-09-07 15:34:51 +00:00
{
// Next message is a RALT down message, which
2013-04-17 13:29:17 +00:00
// means that this is not a proper LCTRL message
2013-05-30 15:19:12 +00:00
return _GLFW_KEY_INVALID;
2010-09-07 15:34:51 +00:00
}
}
}
return GLFW_KEY_LEFT_CONTROL;
2010-09-07 15:34:51 +00:00
}
// The ALT keys require special handling
case VK_MENU:
{
// Is this an extended key (i.e. right key)?
2010-09-10 20:03:36 +00:00
if (lParam & 0x01000000)
return GLFW_KEY_RIGHT_ALT;
2010-09-07 15:34:51 +00:00
return GLFW_KEY_LEFT_ALT;
2010-09-07 15:34:51 +00:00
}
// The ENTER keys require special handling
case VK_RETURN:
{
// Is this an extended key (i.e. right key)?
2010-09-10 20:03:36 +00:00
if (lParam & 0x01000000)
2010-09-07 15:34:51 +00:00
return GLFW_KEY_KP_ENTER;
return GLFW_KEY_ENTER;
}
// Funcion keys (non-printable keys)
case VK_ESCAPE: return GLFW_KEY_ESCAPE;
2010-09-07 15:34:51 +00:00
case VK_TAB: return GLFW_KEY_TAB;
case VK_BACK: return GLFW_KEY_BACKSPACE;
case VK_HOME: return GLFW_KEY_HOME;
case VK_END: return GLFW_KEY_END;
case VK_PRIOR: return GLFW_KEY_PAGE_UP;
case VK_NEXT: return GLFW_KEY_PAGE_DOWN;
2010-09-07 15:34:51 +00:00
case VK_INSERT: return GLFW_KEY_INSERT;
case VK_DELETE: return GLFW_KEY_DELETE;
2010-09-07 15:34:51 +00:00
case VK_LEFT: return GLFW_KEY_LEFT;
case VK_UP: return GLFW_KEY_UP;
case VK_RIGHT: return GLFW_KEY_RIGHT;
case VK_DOWN: return GLFW_KEY_DOWN;
case VK_F1: return GLFW_KEY_F1;
case VK_F2: return GLFW_KEY_F2;
case VK_F3: return GLFW_KEY_F3;
case VK_F4: return GLFW_KEY_F4;
case VK_F5: return GLFW_KEY_F5;
case VK_F6: return GLFW_KEY_F6;
case VK_F7: return GLFW_KEY_F7;
case VK_F8: return GLFW_KEY_F8;
case VK_F9: return GLFW_KEY_F9;
case VK_F10: return GLFW_KEY_F10;
case VK_F11: return GLFW_KEY_F11;
case VK_F12: return GLFW_KEY_F12;
case VK_F13: return GLFW_KEY_F13;
case VK_F14: return GLFW_KEY_F14;
case VK_F15: return GLFW_KEY_F15;
case VK_F16: return GLFW_KEY_F16;
case VK_F17: return GLFW_KEY_F17;
case VK_F18: return GLFW_KEY_F18;
case VK_F19: return GLFW_KEY_F19;
case VK_F20: return GLFW_KEY_F20;
case VK_F21: return GLFW_KEY_F21;
case VK_F22: return GLFW_KEY_F22;
case VK_F23: return GLFW_KEY_F23;
case VK_F24: return GLFW_KEY_F24;
case VK_NUMLOCK: return GLFW_KEY_NUM_LOCK;
case VK_CAPITAL: return GLFW_KEY_CAPS_LOCK;
case VK_SNAPSHOT: return GLFW_KEY_PRINT_SCREEN;
case VK_SCROLL: return GLFW_KEY_SCROLL_LOCK;
case VK_PAUSE: return GLFW_KEY_PAUSE;
case VK_LWIN: return GLFW_KEY_LEFT_SUPER;
case VK_RWIN: return GLFW_KEY_RIGHT_SUPER;
case VK_APPS: return GLFW_KEY_MENU;
2010-09-07 15:34:51 +00:00
// Numeric keypad
case VK_NUMPAD0: return GLFW_KEY_KP_0;
case VK_NUMPAD1: return GLFW_KEY_KP_1;
case VK_NUMPAD2: return GLFW_KEY_KP_2;
case VK_NUMPAD3: return GLFW_KEY_KP_3;
case VK_NUMPAD4: return GLFW_KEY_KP_4;
case VK_NUMPAD5: return GLFW_KEY_KP_5;
case VK_NUMPAD6: return GLFW_KEY_KP_6;
case VK_NUMPAD7: return GLFW_KEY_KP_7;
case VK_NUMPAD8: return GLFW_KEY_KP_8;
case VK_NUMPAD9: return GLFW_KEY_KP_9;
case VK_DIVIDE: return GLFW_KEY_KP_DIVIDE;
case VK_MULTIPLY: return GLFW_KEY_KP_MULTIPLY;
case VK_SUBTRACT: return GLFW_KEY_KP_SUBTRACT;
case VK_ADD: return GLFW_KEY_KP_ADD;
case VK_DECIMAL: return GLFW_KEY_KP_DECIMAL;
// Printable keys are mapped according to US layout
case VK_SPACE: return GLFW_KEY_SPACE;
case 0xDC: return GLFW_KEY_BACKSLASH;
case 0xDF: return GLFW_KEY_WORLD_1;
case 0xE2: return GLFW_KEY_WORLD_2;
default: break;
2010-09-07 15:34:51 +00:00
}
2013-05-30 15:19:12 +00:00
// No matching translation was found
return GLFW_KEY_UNKNOWN;
2010-09-07 15:34:51 +00:00
}
// Window callback function (handles window events)
2013-02-04 12:22:10 +00:00
//
2010-09-10 20:03:36 +00:00
static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
2010-09-07 15:34:51 +00:00
{
_GLFWwindow* window = (_GLFWwindow*) GetWindowLongPtrW(hWnd, 0);
2010-09-12 14:26:00 +00:00
2010-09-10 20:03:36 +00:00
switch (uMsg)
2010-09-07 15:34:51 +00:00
{
2010-09-13 23:05:03 +00:00
case WM_CREATE:
{
CREATESTRUCTW* cs = (CREATESTRUCTW*) lParam;
SetWindowLongPtrW(hWnd, 0, (LONG_PTR) cs->lpCreateParams);
2010-09-13 23:05:03 +00:00
break;
}
2010-09-07 15:34:51 +00:00
case WM_ACTIVATE:
{
// Window was (de)focused and/or (de)iconified
2010-09-07 15:34:51 +00:00
BOOL focused = LOWORD(wParam) != WA_INACTIVE;
BOOL iconified = HIWORD(wParam) ? TRUE : FALSE;
2010-09-07 15:34:51 +00:00
if (focused && iconified)
2010-09-07 15:34:51 +00:00
{
if (window->iconified && _glfw.focusedWindow != window)
{
// This is a workaround for window restoration using the
// Win+D hot key leading to windows being told they're
// focused and iconified and then never told they're
// restored
iconified = FALSE;
}
else
{
// This is a workaround for window iconification using the
// taskbar leading to windows being told they're focused and
// iconified and then never told they're defocused
focused = FALSE;
}
}
if (!focused && _glfw.focusedWindow == window)
{
// The window was defocused (or iconified, see above)
2010-09-07 15:34:51 +00:00
if (window->cursorMode != GLFW_CURSOR_NORMAL)
2014-01-15 12:21:13 +00:00
restoreCursor(window);
2012-09-27 19:37:36 +00:00
if (window->monitor)
2010-09-07 15:34:51 +00:00
{
2010-09-10 20:03:36 +00:00
if (!iconified)
2010-09-07 15:34:51 +00:00
{
// Iconify the (on top, borderless, oddly positioned)
// window or the user will be annoyed
_glfwPlatformIconifyWindow(window);
2010-09-07 15:34:51 +00:00
}
_glfwRestoreVideoMode(window->monitor);
2010-09-07 15:34:51 +00:00
}
}
else if (focused && _glfw.focusedWindow != window)
2010-09-07 15:34:51 +00:00
{
// The window was focused
2014-01-15 12:21:13 +00:00
if (window->cursorMode != GLFW_CURSOR_NORMAL)
_glfwPlatformApplyCursorMode(window);
2010-09-07 15:34:51 +00:00
2012-09-27 19:37:36 +00:00
if (window->monitor)
_glfwSetVideoMode(window->monitor, &window->videoMode);
}
2010-09-07 15:34:51 +00:00
_glfwInputWindowFocus(window, focused);
_glfwInputWindowIconify(window, iconified);
2010-09-07 15:34:51 +00:00
return 0;
}
case WM_ACTIVATEAPP:
{
if (!wParam && IsIconic(hWnd))
{
// This is a workaround for full screen windows losing focus
// through Alt+Tab leading to windows being told they're
// unfocused and restored and then never told they're iconified
_glfwInputWindowIconify(window, GL_TRUE);
}
return 0;
}
case WM_SHOWWINDOW:
{
_glfwInputWindowVisibility(window, wParam ? GL_TRUE : GL_FALSE);
break;
}
2010-09-07 15:34:51 +00:00
case WM_SYSCOMMAND:
{
2010-09-10 20:03:36 +00:00
switch (wParam & 0xfff0)
2010-09-07 15:34:51 +00:00
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
{
2012-09-27 19:37:36 +00:00
if (window->monitor)
2010-09-07 15:34:51 +00:00
{
2010-09-10 20:03:36 +00:00
// We are running in fullscreen mode, so disallow
// screen saver and screen blanking
2010-09-07 15:34:51 +00:00
return 0;
}
else
break;
}
// User trying to access application menu using ALT?
case SC_KEYMENU:
return 0;
}
break;
}
case WM_CLOSE:
{
_glfwInputWindowCloseRequest(window);
2010-09-07 15:34:51 +00:00
return 0;
}
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
{
const int scancode = (lParam >> 16) & 0x1ff;
2013-05-30 15:19:12 +00:00
const int key = translateKey(wParam, lParam);
if (key == _GLFW_KEY_INVALID)
break;
_glfwInputKey(window, key, scancode, GLFW_PRESS, getKeyMods());
break;
2010-11-15 19:21:09 +00:00
}
2010-09-07 15:34:51 +00:00
2012-09-19 11:17:53 +00:00
case WM_CHAR:
case WM_SYSCHAR:
2012-09-19 11:17:53 +00:00
{
2013-03-08 13:19:40 +00:00
_glfwInputChar(window, (unsigned int) wParam);
2012-09-19 11:17:53 +00:00
return 0;
}
2013-04-10 22:59:21 +00:00
case WM_UNICHAR:
{
// This message is not sent by Windows, but is sent by some
// third-party input method engines
if (wParam == UNICODE_NOCHAR)
{
// Returning TRUE here announces support for this message
return TRUE;
}
_glfwInputChar(window, (unsigned int) wParam);
return FALSE;
}
2010-09-07 15:34:51 +00:00
case WM_KEYUP:
case WM_SYSKEYUP:
{
const int mods = getKeyMods();
const int scancode = (lParam >> 16) & 0x1ff;
2013-05-30 15:19:12 +00:00
const int key = translateKey(wParam, lParam);
if (key == _GLFW_KEY_INVALID)
break;
2010-09-10 20:03:36 +00:00
if (wParam == VK_SHIFT)
2010-09-07 15:34:51 +00:00
{
2013-06-05 13:17:16 +00:00
// Release both Shift keys on Shift up event, as only one event
// is sent even if both keys are released
2013-05-30 15:19:12 +00:00
_glfwInputKey(window, GLFW_KEY_LEFT_SHIFT, scancode, GLFW_RELEASE, mods);
_glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, scancode, GLFW_RELEASE, mods);
2010-09-07 15:34:51 +00:00
}
else if (wParam == VK_SNAPSHOT)
{
// Key down is not reported for the print screen key
2013-05-30 15:19:12 +00:00
_glfwInputKey(window, key, scancode, GLFW_PRESS, mods);
_glfwInputKey(window, key, scancode, GLFW_RELEASE, mods);
}
2010-09-07 15:34:51 +00:00
else
2013-05-30 15:19:12 +00:00
_glfwInputKey(window, key, scancode, GLFW_RELEASE, mods);
2010-09-07 15:34:51 +00:00
break;
2010-09-07 15:34:51 +00:00
}
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
2010-09-10 20:03:36 +00:00
{
const int mods = getKeyMods();
2010-09-07 15:34:51 +00:00
SetCapture(hWnd);
2010-09-10 20:03:36 +00:00
if (uMsg == WM_LBUTTONDOWN)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, mods);
else if (uMsg == WM_RBUTTONDOWN)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS, mods);
else if (uMsg == WM_MBUTTONDOWN)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_PRESS, mods);
else
2010-09-07 15:34:51 +00:00
{
if (HIWORD(wParam) == XBUTTON1)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_4, GLFW_PRESS, mods);
else if (HIWORD(wParam) == XBUTTON2)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_5, GLFW_PRESS, mods);
2010-09-10 20:03:36 +00:00
return TRUE;
}
2010-09-07 15:34:51 +00:00
return 0;
2010-09-10 20:03:36 +00:00
}
case WM_LBUTTONUP:
2010-09-07 15:34:51 +00:00
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
2010-09-10 20:03:36 +00:00
{
const int mods = getKeyMods();
2010-09-07 15:34:51 +00:00
ReleaseCapture();
2010-09-10 20:03:36 +00:00
if (uMsg == WM_LBUTTONUP)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE, mods);
else if (uMsg == WM_RBUTTONUP)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_RIGHT, GLFW_RELEASE, mods);
else if (uMsg == WM_MBUTTONUP)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_RELEASE, mods);
else
2010-09-07 15:34:51 +00:00
{
if (HIWORD(wParam) == XBUTTON1)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_4, GLFW_RELEASE, mods);
else if (HIWORD(wParam) == XBUTTON2)
_glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_5, GLFW_RELEASE, mods);
return TRUE;
2010-09-07 15:34:51 +00:00
}
2010-09-10 20:03:36 +00:00
return 0;
2010-09-07 15:34:51 +00:00
}
case WM_MOUSEMOVE:
{
2013-02-20 16:44:16 +00:00
const int newCursorX = GET_X_LPARAM(lParam);
const int newCursorY = GET_Y_LPARAM(lParam);
2010-09-07 15:34:51 +00:00
if (newCursorX != window->win32.oldCursorX ||
newCursorY != window->win32.oldCursorY)
2010-09-07 15:34:51 +00:00
{
int x, y;
if (window->cursorMode == GLFW_CURSOR_DISABLED)
2010-09-07 15:34:51 +00:00
{
if (_glfw.focusedWindow != window)
return 0;
x = newCursorX - window->win32.oldCursorX;
y = newCursorY - window->win32.oldCursorY;
2010-09-07 15:34:51 +00:00
}
else
{
x = newCursorX;
y = newCursorY;
2010-09-07 15:34:51 +00:00
}
2010-09-10 20:03:36 +00:00
window->win32.oldCursorX = newCursorX;
window->win32.oldCursorY = newCursorY;
window->win32.cursorCentered = GL_FALSE;
2010-09-07 15:34:51 +00:00
_glfwInputCursorMotion(window, x, y);
2010-09-07 15:34:51 +00:00
}
2010-09-10 20:03:36 +00:00
if (!window->win32.cursorInside)
2012-03-22 12:17:44 +00:00
{
TRACKMOUSEEVENT tme;
ZeroMemory(&tme, sizeof(tme));
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = window->win32.handle;
2012-03-22 12:17:44 +00:00
TrackMouseEvent(&tme);
window->win32.cursorInside = GL_TRUE;
2012-03-22 12:17:44 +00:00
_glfwInputCursorEnter(window, GL_TRUE);
}
return 0;
}
case WM_MOUSELEAVE:
{
window->win32.cursorInside = GL_FALSE;
2012-03-22 12:17:44 +00:00
_glfwInputCursorEnter(window, GL_FALSE);
2010-09-07 15:34:51 +00:00
return 0;
}
case WM_MOUSEWHEEL:
{
2012-03-28 19:54:09 +00:00
_glfwInputScroll(window, 0.0, (SHORT) HIWORD(wParam) / (double) WHEEL_DELTA);
return 0;
}
2010-09-10 20:03:36 +00:00
case WM_MOUSEHWHEEL:
{
// This message is only sent on Windows Vista and later
2012-03-28 19:54:09 +00:00
_glfwInputScroll(window, (SHORT) HIWORD(wParam) / (double) WHEEL_DELTA, 0.0);
2010-09-12 14:26:00 +00:00
return 0;
2010-09-07 15:34:51 +00:00
}
case WM_SIZE:
{
2014-01-15 12:21:13 +00:00
if (_glfw.focusedWindow == window)
{
2014-01-15 12:21:13 +00:00
if (window->cursorMode == GLFW_CURSOR_DISABLED)
updateClipRect(window);
}
2010-09-07 15:34:51 +00:00
_glfwInputFramebufferSize(window, LOWORD(lParam), HIWORD(lParam));
_glfwInputWindowSize(window, LOWORD(lParam), HIWORD(lParam));
2010-09-07 15:34:51 +00:00
return 0;
}
case WM_MOVE:
{
2014-01-15 12:21:13 +00:00
if (_glfw.focusedWindow == window)
{
2014-01-15 12:21:13 +00:00
if (window->cursorMode == GLFW_CURSOR_DISABLED)
updateClipRect(window);
}
2013-11-13 11:47:44 +00:00
// NOTE: This cannot use LOWORD/HIWORD recommended by MSDN, as
// those macros do not handle negative window positions correctly
_glfwInputWindowPos(window,
GET_X_LPARAM(lParam),
GET_Y_LPARAM(lParam));
2010-09-07 15:34:51 +00:00
return 0;
}
case WM_PAINT:
{
_glfwInputWindowDamage(window);
2010-09-07 15:34:51 +00:00
break;
}
2013-03-11 21:57:39 +00:00
case WM_SETCURSOR:
{
2014-01-15 12:21:13 +00:00
if (_glfw.focusedWindow == window && LOWORD(lParam) == HTCLIENT)
2013-03-11 21:57:39 +00:00
{
2014-01-15 12:21:13 +00:00
if (window->cursorMode == GLFW_CURSOR_HIDDEN ||
window->cursorMode == GLFW_CURSOR_DISABLED)
{
SetCursor(NULL);
return TRUE;
}
else if (window->cursor)
{
SetCursor(window->cursor->win32.handle);
return TRUE;
}
2013-03-11 21:57:39 +00:00
}
break;
}
case WM_DEVICECHANGE:
{
if (DBT_DEVNODES_CHANGED == wParam)
{
2012-09-12 17:35:52 +00:00
_glfwInputMonitorChange();
return TRUE;
}
break;
}
2013-03-11 19:54:32 +00:00
case WM_DWMCOMPOSITIONCHANGED:
{
if (_glfwIsCompositionEnabled())
{
_GLFWwindow* previous = _glfwPlatformGetCurrentContext();
_glfwPlatformMakeContextCurrent(window);
_glfwPlatformSwapInterval(0);
_glfwPlatformMakeContextCurrent(previous);
}
// TODO: Restore vsync if compositing was disabled
break;
}
2013-12-22 15:38:56 +00:00
2013-07-10 09:42:14 +00:00
case WM_DROPFILES:
{
2013-12-22 15:38:56 +00:00
HDROP hDrop = (HDROP) wParam;
POINT pt;
2013-12-22 18:28:46 +00:00
int i;
const int count = DragQueryFileW(hDrop, 0xffffffff, NULL, 0);
2013-12-22 18:28:46 +00:00
char** names = calloc(count, sizeof(char*));
2013-12-22 15:38:56 +00:00
// Move the mouse to the position of the drop
DragQueryPoint(hDrop, &pt);
_glfwInputCursorMotion(window, pt.x, pt.y);
2013-12-22 18:28:46 +00:00
for (i = 0; i < count; i++)
2013-12-22 15:38:56 +00:00
{
const UINT length = DragQueryFileW(hDrop, i, NULL, 0);
2013-12-22 18:28:46 +00:00
WCHAR* buffer = calloc(length + 1, sizeof(WCHAR));
2013-12-22 15:38:56 +00:00
DragQueryFileW(hDrop, i, buffer, length + 1);
2013-12-22 18:28:46 +00:00
names[i] = _glfwCreateUTF8FromWideString(buffer);
2013-12-22 15:38:56 +00:00
2013-12-22 18:28:46 +00:00
free(buffer);
2013-12-22 15:38:56 +00:00
}
2013-12-22 18:28:46 +00:00
_glfwInputDrop(window, count, (const char**) names);
for (i = 0; i < count; i++)
free(names[i]);
free(names);
2013-12-22 15:38:56 +00:00
DragFinish(hDrop);
2014-02-10 23:56:52 +00:00
return 0;
2013-07-10 09:42:14 +00:00
}
2010-09-07 15:34:51 +00:00
}
2010-09-10 20:03:36 +00:00
return DefWindowProc(hWnd, uMsg, wParam, lParam);
2010-09-07 15:34:51 +00:00
}
// Translate client window size to full window size (including window borders)
2013-02-04 12:22:10 +00:00
//
2010-09-12 14:26:00 +00:00
static void getFullWindowSize(_GLFWwindow* window,
int clientWidth, int clientHeight,
2010-09-10 20:03:36 +00:00
int* fullWidth, int* fullHeight)
2010-09-07 15:34:51 +00:00
{
2013-02-20 16:44:16 +00:00
RECT rect = { 0, 0, clientWidth, clientHeight };
2013-04-16 21:19:58 +00:00
AdjustWindowRectEx(&rect, window->win32.dwStyle,
FALSE, window->win32.dwExStyle);
2013-02-20 16:44:16 +00:00
*fullWidth = rect.right - rect.left;
*fullHeight = rect.bottom - rect.top;
2010-09-07 15:34:51 +00:00
}
// Registers the GLFW window class
2013-02-04 12:22:10 +00:00
//
2010-09-10 20:03:36 +00:00
static ATOM registerWindowClass(void)
2010-09-07 15:34:51 +00:00
{
2014-03-06 22:21:13 +00:00
WNDCLASSW wc;
2010-09-14 00:17:18 +00:00
ATOM classAtom;
2010-09-07 15:34:51 +00:00
// Set window class parameters
2013-04-16 21:19:58 +00:00
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) windowProc;
wc.cbClsExtra = 0; // No extra class data
wc.cbWndExtra = sizeof(void*) + sizeof(int); // Make room for one pointer
wc.hInstance = GetModuleHandleW(NULL);
wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
2013-04-16 21:19:58 +00:00
wc.hbrBackground = NULL; // No background
wc.lpszMenuName = NULL; // No menu
wc.lpszClassName = _GLFW_WNDCLASSNAME;
2010-09-07 15:34:51 +00:00
// Load user-provided icon if available
wc.hIcon = LoadIconW(GetModuleHandleW(NULL), L"GLFW_ICON");
2010-09-10 20:03:36 +00:00
if (!wc.hIcon)
2010-09-07 15:34:51 +00:00
{
2013-04-16 21:19:58 +00:00
// No user-provided icon found, load default icon
wc.hIcon = LoadIconW(NULL, IDI_WINLOGO);
2010-09-07 15:34:51 +00:00
}
classAtom = RegisterClassW(&wc);
2010-09-14 00:17:18 +00:00
if (!classAtom)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Failed to register window class");
2010-09-14 00:17:18 +00:00
return 0;
}
return classAtom;
2010-09-07 15:34:51 +00:00
}
// Creates the GLFW window and rendering context
2013-02-04 12:22:10 +00:00
//
2010-09-12 14:26:00 +00:00
static int createWindow(_GLFWwindow* window,
const _GLFWwndconfig* wndconfig,
const _GLFWctxconfig* ctxconfig,
2010-09-10 20:03:36 +00:00
const _GLFWfbconfig* fbconfig)
2010-09-07 15:34:51 +00:00
{
2013-02-20 15:00:53 +00:00
int xpos, ypos, fullWidth, fullHeight;
2012-02-03 19:34:24 +00:00
WCHAR* wideTitle;
2010-09-07 15:34:51 +00:00
window->win32.dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
window->win32.dwExStyle = WS_EX_APPWINDOW;
2010-09-07 15:34:51 +00:00
2012-09-27 19:37:36 +00:00
if (window->monitor)
{
window->win32.dwStyle |= WS_POPUP;
2013-02-20 15:00:53 +00:00
_glfwPlatformGetMonitorPos(wndconfig->monitor, &xpos, &ypos);
fullWidth = wndconfig->width;
fullHeight = wndconfig->height;
}
2010-09-07 15:34:51 +00:00
else
{
2013-04-08 13:16:32 +00:00
if (wndconfig->decorated)
2010-09-07 15:34:51 +00:00
{
2013-04-08 13:16:32 +00:00
window->win32.dwStyle |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
2010-09-07 15:34:51 +00:00
2013-04-08 13:16:32 +00:00
if (wndconfig->resizable)
{
window->win32.dwStyle |= WS_MAXIMIZEBOX | WS_SIZEBOX;
window->win32.dwExStyle |= WS_EX_WINDOWEDGE;
}
}
else
window->win32.dwStyle |= WS_POPUP;
2013-02-20 15:00:53 +00:00
xpos = CW_USEDEFAULT;
ypos = CW_USEDEFAULT;
2010-09-10 20:03:36 +00:00
getFullWindowSize(window,
wndconfig->width, wndconfig->height,
&fullWidth, &fullHeight);
}
wideTitle = _glfwCreateWideStringFromUTF8(wndconfig->title);
2012-02-03 19:34:24 +00:00
if (!wideTitle)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Failed to convert title to wide string");
2012-02-07 15:21:19 +00:00
return GL_FALSE;
2012-02-03 19:34:24 +00:00
}
window->win32.handle = CreateWindowExW(window->win32.dwExStyle,
_GLFW_WNDCLASSNAME,
wideTitle,
window->win32.dwStyle,
xpos, ypos,
fullWidth, fullHeight,
NULL, // No parent window
NULL, // No window menu
GetModuleHandleW(NULL),
window); // Pass object to WM_CREATE
2010-09-12 14:26:00 +00:00
free(wideTitle);
2013-12-22 15:38:56 +00:00
if (_glfw_ChangeWindowMessageFilterEx)
{
_glfw_ChangeWindowMessageFilterEx(window->win32.handle,
WM_DROPFILES, MSGFLT_ALLOW, NULL);
_glfw_ChangeWindowMessageFilterEx(window->win32.handle,
WM_COPYDATA, MSGFLT_ALLOW, NULL);
_glfw_ChangeWindowMessageFilterEx(window->win32.handle,
WM_COPYGLOBALDATA, MSGFLT_ALLOW, NULL);
}
2013-12-22 15:38:56 +00:00
DragAcceptFiles(window->win32.handle, TRUE);
if (!window->win32.handle)
2010-09-10 20:03:36 +00:00
{
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to create window");
2010-09-07 15:34:51 +00:00
return GL_FALSE;
}
if (!_glfwCreateContext(window, ctxconfig, fbconfig))
return GL_FALSE;
2010-09-07 15:34:51 +00:00
return GL_TRUE;
}
// Destroys the GLFW window and rendering context
2013-02-04 12:22:10 +00:00
//
2010-09-13 21:50:04 +00:00
static void destroyWindow(_GLFWwindow* window)
2010-09-07 15:34:51 +00:00
{
_glfwDestroyContext(window);
2010-09-14 00:54:05 +00:00
if (window->win32.handle)
2010-09-07 15:34:51 +00:00
{
DestroyWindow(window->win32.handle);
window->win32.handle = NULL;
2010-09-07 15:34:51 +00:00
}
}
2010-09-10 20:03:36 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 15:34:51 +00:00
int _glfwPlatformCreateWindow(_GLFWwindow* window,
const _GLFWwndconfig* wndconfig,
const _GLFWctxconfig* ctxconfig,
const _GLFWfbconfig* fbconfig)
2010-09-07 15:34:51 +00:00
{
2012-12-13 21:43:23 +00:00
int status;
2010-09-07 15:34:51 +00:00
if (!_glfw.win32.classAtom)
2010-09-14 00:17:18 +00:00
{
_glfw.win32.classAtom = registerWindowClass();
if (!_glfw.win32.classAtom)
2010-09-14 00:17:18 +00:00
return GL_FALSE;
}
2010-09-07 15:34:51 +00:00
if (!createWindow(window, wndconfig, ctxconfig, fbconfig))
2010-09-07 15:34:51 +00:00
return GL_FALSE;
status = _glfwAnalyzeContext(window, ctxconfig, fbconfig);
2012-12-13 21:43:23 +00:00
if (status == _GLFW_RECREATION_IMPOSSIBLE)
return GL_FALSE;
2010-09-07 15:34:51 +00:00
2012-12-13 21:43:23 +00:00
if (status == _GLFW_RECREATION_REQUIRED)
2010-09-07 15:34:51 +00:00
{
// Some window hints require us to re-create the context using WGL
// extensions retrieved through the current context, as we cannot check
// for WGL extensions or retrieve WGL entry points before we have a
// current context (actually until we have implicitly loaded the ICD)
// Yes, this is strange, and yes, this is the proper way on Win32
// As Windows only allows you to set the pixel format once for a
// window, we need to destroy the current window and create a new one
// to be able to use the new pixel format
// Technically, it may be possible to keep the old window around if
// we're just creating an OpenGL 3.0+ context with the same pixel
2011-09-06 12:52:42 +00:00
// format, but it's not worth the added code complexity
2010-09-07 15:34:51 +00:00
// First we clear the current context (the one we just created)
// This is usually done by glfwDestroyWindow, but as we're not doing
2013-10-28 13:50:33 +00:00
// full GLFW window destruction, it's duplicated here
_glfwPlatformMakeContextCurrent(NULL);
// Next destroy the Win32 window and WGL context (without resetting or
// destroying the GLFW window object)
2010-09-13 21:50:04 +00:00
destroyWindow(window);
2010-09-07 15:34:51 +00:00
// ...and then create them again, this time with better APIs
if (!createWindow(window, wndconfig, ctxconfig, fbconfig))
2010-09-07 15:34:51 +00:00
return GL_FALSE;
}
2012-09-27 19:37:36 +00:00
if (window->monitor)
2010-09-07 15:34:51 +00:00
{
if (!_glfwSetVideoMode(window->monitor, &window->videoMode))
return GL_FALSE;
2010-09-07 15:34:51 +00:00
// Place the window above all topmost windows
2012-09-23 13:08:43 +00:00
_glfwPlatformShowWindow(window);
SetWindowPos(window->win32.handle, HWND_TOPMOST, 0,0,0,0,
2010-09-10 20:03:36 +00:00
SWP_NOMOVE | SWP_NOSIZE);
2010-09-07 15:34:51 +00:00
}
return GL_TRUE;
}
void _glfwPlatformDestroyWindow(_GLFWwindow* window)
2010-09-07 15:34:51 +00:00
{
2010-09-13 21:50:04 +00:00
destroyWindow(window);
2010-09-07 15:34:51 +00:00
2012-09-27 19:37:36 +00:00
if (window->monitor)
_glfwRestoreVideoMode(window->monitor);
2010-09-07 15:34:51 +00:00
}
2010-09-12 14:26:00 +00:00
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
2010-09-07 15:34:51 +00:00
{
WCHAR* wideTitle = _glfwCreateWideStringFromUTF8(title);
2012-02-03 19:34:24 +00:00
if (!wideTitle)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Failed to convert title to wide string");
2012-02-03 19:34:24 +00:00
return;
}
SetWindowTextW(window->win32.handle, wideTitle);
2012-02-07 13:58:58 +00:00
free(wideTitle);
2010-09-07 15:34:51 +00:00
}
void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos)
{
POINT pos = { 0, 0 };
ClientToScreen(window->win32.handle, &pos);
if (xpos)
*xpos = pos.x;
if (ypos)
*ypos = pos.y;
}
void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos)
{
RECT rect = { xpos, ypos, xpos, ypos };
AdjustWindowRectEx(&rect, window->win32.dwStyle,
FALSE, window->win32.dwExStyle);
SetWindowPos(window->win32.handle, NULL, rect.left, rect.top, 0, 0,
SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
}
void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
{
RECT area;
GetClientRect(window->win32.handle, &area);
if (width)
*width = area.right;
if (height)
*height = area.bottom;
}
2010-09-12 14:26:00 +00:00
void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
2010-09-07 15:34:51 +00:00
{
2012-09-27 19:37:36 +00:00
if (window->monitor)
2010-09-07 15:34:51 +00:00
{
GLFWvidmode mode;
2013-03-12 16:25:33 +00:00
_glfwSetVideoMode(window->monitor, &window->videoMode);
_glfwPlatformGetVideoMode(window->monitor, &mode);
2013-03-12 16:25:33 +00:00
SetWindowPos(window->win32.handle, HWND_TOP,
0, 0, mode.width, mode.height,
SWP_NOMOVE);
2013-04-17 13:29:17 +00:00
}
2010-09-07 15:34:51 +00:00
else
{
2013-03-12 16:25:33 +00:00
int fullWidth, fullHeight;
getFullWindowSize(window, width, height, &fullWidth, &fullHeight);
2010-09-07 15:34:51 +00:00
2013-03-12 16:25:33 +00:00
SetWindowPos(window->win32.handle, HWND_TOP,
0, 0, fullWidth, fullHeight,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOZORDER);
2010-09-07 15:34:51 +00:00
}
}
void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height)
{
_glfwPlatformGetWindowSize(window, width, height);
}
2014-03-25 20:30:13 +00:00
void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
int* left, int* top,
int* right, int* bottom)
{
RECT rect;
int width, height;
_glfwPlatformGetWindowSize(window, &width, &height);
SetRect(&rect, 0, 0, width, height);
AdjustWindowRectEx(&rect, window->win32.dwStyle,
FALSE, window->win32.dwExStyle);
if (left)
*left = -rect.left;
if (top)
*top = -rect.top;
if (right)
*right = rect.right - width;
if (bottom)
*bottom = rect.bottom - height;
}
2010-09-12 14:26:00 +00:00
void _glfwPlatformIconifyWindow(_GLFWwindow* window)
2010-09-07 15:34:51 +00:00
{
ShowWindow(window->win32.handle, SW_MINIMIZE);
2010-09-07 15:34:51 +00:00
}
2010-09-12 14:26:00 +00:00
void _glfwPlatformRestoreWindow(_GLFWwindow* window)
2010-09-07 15:34:51 +00:00
{
ShowWindow(window->win32.handle, SW_RESTORE);
2010-09-07 15:34:51 +00:00
}
void _glfwPlatformShowWindow(_GLFWwindow* window)
{
ShowWindow(window->win32.handle, SW_SHOWNORMAL);
BringWindowToTop(window->win32.handle);
SetForegroundWindow(window->win32.handle);
SetFocus(window->win32.handle);
}
void _glfwPlatformHideWindow(_GLFWwindow* window)
{
ShowWindow(window->win32.handle, SW_HIDE);
}
2010-09-10 20:03:36 +00:00
void _glfwPlatformPollEvents(void)
2010-09-07 15:34:51 +00:00
{
MSG msg;
2010-09-13 21:50:04 +00:00
_GLFWwindow* window;
2010-09-07 15:34:51 +00:00
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
2010-09-07 15:34:51 +00:00
{
2012-09-19 11:17:53 +00:00
if (msg.message == WM_QUIT)
2010-09-07 15:34:51 +00:00
{
2012-09-19 11:17:53 +00:00
// Treat WM_QUIT as a close on all windows
2010-09-13 21:50:04 +00:00
window = _glfw.windowListHead;
2012-09-19 11:17:53 +00:00
while (window)
2010-09-10 20:03:36 +00:00
{
2012-09-19 11:17:53 +00:00
_glfwInputWindowCloseRequest(window);
window = window->next;
2010-09-10 20:03:36 +00:00
}
2010-09-07 15:34:51 +00:00
}
2012-09-19 11:17:53 +00:00
else
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
2012-09-19 11:17:53 +00:00
}
2010-09-07 15:34:51 +00:00
}
window = _glfw.focusedWindow;
2010-09-13 21:50:04 +00:00
if (window)
2010-09-07 15:34:51 +00:00
{
2013-02-20 16:44:16 +00:00
// LSHIFT/RSHIFT fixup (keys tend to "stick" without this fix)
// This is the only async event handling in GLFW, but it solves some
// nasty problems
{
const int mods = getAsyncKeyMods();
2010-09-07 15:34:51 +00:00
2013-02-20 16:44:16 +00:00
// Get current state of left and right shift keys
const int lshiftDown = (GetAsyncKeyState(VK_LSHIFT) >> 15) & 1;
const int rshiftDown = (GetAsyncKeyState(VK_RSHIFT) >> 15) & 1;
2010-09-07 15:34:51 +00:00
2013-02-20 16:44:16 +00:00
// See if this differs from our belief of what has happened
// (we only have to check for lost key up events)
2013-04-17 13:29:17 +00:00
if (!lshiftDown && window->key[GLFW_KEY_LEFT_SHIFT] == 1)
2013-05-30 15:19:12 +00:00
_glfwInputKey(window, GLFW_KEY_LEFT_SHIFT, 0, GLFW_RELEASE, mods);
2010-09-10 20:03:36 +00:00
2013-04-17 13:29:17 +00:00
if (!rshiftDown && window->key[GLFW_KEY_RIGHT_SHIFT] == 1)
2013-05-30 15:19:12 +00:00
_glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, 0, GLFW_RELEASE, mods);
2013-02-20 16:44:16 +00:00
}
2010-09-07 15:34:51 +00:00
2014-01-15 12:21:13 +00:00
// Did the cursor move in an focused window that has disabled the cursor
if (window->cursorMode == GLFW_CURSOR_DISABLED &&
!window->win32.cursorCentered)
{
int width, height;
_glfwPlatformGetWindowSize(window, &width, &height);
2013-04-16 18:50:59 +00:00
_glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0);
window->win32.cursorCentered = GL_TRUE;
}
2010-09-07 15:34:51 +00:00
}
}
2010-09-10 20:03:36 +00:00
void _glfwPlatformWaitEvents(void)
2010-09-07 15:34:51 +00:00
{
WaitMessage();
_glfwPlatformPollEvents();
}
2014-02-10 17:16:58 +00:00
void _glfwPlatformPostEmptyEvent(void)
{
_GLFWwindow* window = _glfw.windowListHead;
PostMessage(window->win32.handle, WM_NULL, 0, 0);
}
2013-04-04 14:16:21 +00:00
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos)
2010-09-07 15:34:51 +00:00
{
2013-04-04 14:16:21 +00:00
POINT pos = { (int) xpos, (int) ypos };
ClientToScreen(window->win32.handle, &pos);
2010-09-10 20:03:36 +00:00
SetCursorPos(pos.x, pos.y);
2013-04-16 18:50:59 +00:00
window->win32.oldCursorX = (int) xpos;
window->win32.oldCursorY = (int) ypos;
2010-09-07 15:34:51 +00:00
}
2014-01-15 12:21:13 +00:00
void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
{
2014-01-15 12:21:13 +00:00
switch (window->cursorMode)
{
case GLFW_CURSOR_NORMAL:
2014-01-15 12:21:13 +00:00
restoreCursor(window);
break;
case GLFW_CURSOR_HIDDEN:
hideCursor(window);
break;
case GLFW_CURSOR_DISABLED:
2014-01-15 12:21:13 +00:00
disableCursor(window);
break;
}
}
2014-02-23 15:43:17 +00:00
int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
const GLFWimage* image,
int xhot, int yhot)
{
2014-01-23 14:24:57 +00:00
HDC dc;
HBITMAP bitmap, mask;
BITMAPV5HEADER bi;
ICONINFO ii;
2014-01-23 14:24:57 +00:00
DWORD* target = 0;
2014-02-23 15:43:17 +00:00
BYTE* source = (BYTE*) image->pixels;
2014-01-23 14:24:57 +00:00
int i;
2014-01-23 14:24:57 +00:00
ZeroMemory(&bi, sizeof(bi));
bi.bV5Size = sizeof(BITMAPV5HEADER);
2014-02-23 15:43:17 +00:00
bi.bV5Width = image->width;
bi.bV5Height = -image->height;
bi.bV5Planes = 1;
bi.bV5BitCount = 32;
bi.bV5Compression = BI_BITFIELDS;
2014-01-23 14:24:57 +00:00
bi.bV5RedMask = 0x00ff0000;
bi.bV5GreenMask = 0x0000ff00;
bi.bV5BlueMask = 0x000000ff;
bi.bV5AlphaMask = 0xff000000;
2014-01-23 14:24:57 +00:00
dc = GetDC(NULL);
bitmap = CreateDIBSection(dc, (BITMAPINFO*) &bi, DIB_RGB_COLORS,
(void**) &target, NULL, (DWORD) 0);
ReleaseDC(NULL, dc);
2014-01-23 14:24:57 +00:00
if (!bitmap)
return GL_FALSE;
2014-02-23 15:43:17 +00:00
mask = CreateBitmap(image->width, image->height, 1, 1, NULL);
2014-01-23 14:24:57 +00:00
if (!mask)
{
2014-01-23 14:24:57 +00:00
DeleteObject(bitmap);
return GL_FALSE;
}
2014-02-23 15:43:17 +00:00
for (i = 0; i < image->width * image->height; i++, target++, source += 4)
2014-03-20 10:17:55 +00:00
{
*target = (source[3] << 24) |
(source[0] << 16) |
(source[1] << 8) |
source[2];
}
2014-01-23 14:24:57 +00:00
ZeroMemory(&ii, sizeof(ii));
ii.fIcon = FALSE;
ii.xHotspot = xhot;
ii.yHotspot = yhot;
ii.hbmMask = mask;
ii.hbmColor = bitmap;
cursor->win32.handle = (HCURSOR) CreateIconIndirect(&ii);
2014-01-23 14:24:57 +00:00
DeleteObject(bitmap);
DeleteObject(mask);
2014-01-23 14:24:57 +00:00
if (!cursor->win32.handle)
return GL_FALSE;
return GL_TRUE;
}
void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
{
2014-01-23 14:24:57 +00:00
if (cursor->win32.handle)
DestroyIcon((HICON) cursor->win32.handle);
}
void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
{
// It should be guaranteed that the cursor is not being used by this window if
// the following condition is not met. That way it should be safe to destroy the
// cursor after calling glfwSetCursor(window, NULL) on all windows using the cursor.
if (window->cursorMode == GLFW_CURSOR_NORMAL && _glfw.focusedWindow == window &&
window->win32.cursorInside)
{
if (cursor)
SetCursor(cursor->win32.handle);
else
2014-01-23 14:24:57 +00:00
SetCursor(LoadCursorW(NULL, IDC_ARROW));
}
}
2013-04-17 13:29:17 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW native API //////
//////////////////////////////////////////////////////////////////////////
GLFWAPI HWND glfwGetWin32Window(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
return window->win32.handle;
}