29 #define WM_DPICHANGED 0x02E0
33 #define WM_TOUCH 0x0240
38 #define TOUCH_COORD_TO_PIXEL(l) ((l) / 100)
43 DECLARE_HANDLE(HTOUCHINPUT);
47 TypeHandle WinGraphicsWindow::WinWindowHandle::_type_handle;
49 WinGraphicsWindow::WindowHandles WinGraphicsWindow::_window_handles;
53 bool WinGraphicsWindow::_cursor_hidden =
false;
55 RECT WinGraphicsWindow::_mouse_unconfined_cliprect;
60 bool WinGraphicsWindow::_got_saved_params =
false;
61 int WinGraphicsWindow::_saved_mouse_trails;
62 BOOL WinGraphicsWindow::_saved_cursor_shadow;
63 BOOL WinGraphicsWindow::_saved_mouse_vanish;
69 int WinGraphicsWindow::_window_class_index = 0;
71 static const char *
const errorbox_title =
"Panda3D Error";
75 typedef BOOL (WINAPI *PFN_REGISTERTOUCHWINDOW)(IN HWND hWnd, IN ULONG ulFlags);
76 typedef BOOL (WINAPI *PFN_GETTOUCHINPUTINFO)(IN HTOUCHINPUT hTouchInput, IN UINT cInputs, OUT PTOUCHINPUT pInputs, IN
int cbSize);
77 typedef BOOL (WINAPI *PFN_CLOSETOUCHINPUTHANDLE)(IN HTOUCHINPUT hTouchInput);
79 static PFN_REGISTERTOUCHWINDOW pRegisterTouchWindow = 0;
80 static PFN_GETTOUCHINPUTINFO pGetTouchInputInfo = 0;
81 static PFN_CLOSETOUCHINPUTHANDLE pCloseTouchInputHandle = 0;
88 const std::string &name,
94 GraphicsWindow(engine, pipe, name, fb_prop, win_prop, flags, gsg, host)
96 initialize_input_devices();
100 _tracking_mouse_leaving =
false;
102 _lost_keypresses =
false;
103 _lshift_down =
false;
104 _rshift_down =
false;
105 _lcontrol_down =
false;
106 _rcontrol_down =
false;
117 ~WinGraphicsWindow() {
118 if (_window_handle !=
nullptr) {
119 DCAST(WinWindowHandle, _window_handle)->clear_window();
131 nassertr(device >= 0 && device < (
int)_input_devices.size(),
MouseData());
137 if (device == 0 && result._in_window && GetCursorPos(&cpos) && ScreenToClient(_hWnd, &cpos)) {
139 result._xpos = cpos.x;
140 result._ypos = cpos.y;
166 if (!_properties.get_foreground() )
177 SetCursorPos(view_rect.left + x, view_rect.top + y);
182 if ((device < 1)||(device >= (
int)_input_devices.size())) {
200 HIMC hIMC = ImmGetContext(_hWnd);
202 if (!ImmSetOpenStatus(hIMC,
false)) {
203 windisplay_cat.debug() <<
"ImmSetOpenStatus failed\n";
205 ImmReleaseContext(_hWnd, hIMC);
209 windisplay_cat.debug() <<
"success: closed ime window\n";
257 if (!disable_message_loop) {
260 while (PeekMessage(&msg,
nullptr, 0, 0, PM_NOREMOVE)) {
301 LPoint2i top_left = _properties.get_origin();
302 LPoint2i bottom_right = top_left + _properties.get_size();
304 DWORD window_style = make_style(_properties);
305 SetWindowLong(_hWnd, GWL_STYLE, window_style);
309 SetRect(&view_rect, top_left[0], top_left[1],
310 bottom_right[0], bottom_right[1]);
312 GetWindowInfo(_hWnd, &wi);
313 AdjustWindowRectEx(&view_rect, wi.dwStyle, FALSE, wi.dwExStyle);
316 SetWindowPos(_hWnd, HWND_NOTOPMOST, view_rect.left, view_rect.top,
317 view_rect.right - view_rect.left,
318 view_rect.bottom - view_rect.top,
319 SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED |
320 SWP_NOSENDCHANGING | SWP_SHOWWINDOW);
324 std::string title = properties.
get_title();
325 _properties.set_title(title);
328 SetWindowTextW(_hWnd, title_w.c_str());
335 ::SendMessage(_hWnd, WM_SETICON, ICON_SMALL, (LPARAM)icon);
336 ::SendMessage(_hWnd, WM_SETICON, ICON_BIG, (LPARAM)icon);
345 _properties.set_cursor_hidden(hide_cursor);
346 if (_cursor_window ==
this) {
347 hide_or_show_cursor(hide_cursor);
355 _properties.set_cursor_filename(filename);
357 _cursor = get_cursor(filename);
359 _cursor = LoadCursor(
nullptr, IDC_ARROW);
362 if (_cursor_window ==
this) {
370 WindowProperties::ZOrder last_z_order = _properties.get_z_order();
372 adjust_z_order(last_z_order, properties.
get_z_order());
378 if (!SetActiveWindow(_hWnd)) {
379 windisplay_cat.warning()
380 <<
"SetForegroundWindow() failed!\n";
382 _properties.set_foreground(
true);
389 if (_properties.get_minimized() != properties.
get_minimized()) {
391 ShowWindow(_hWnd, SW_MINIMIZE);
393 ShowWindow(_hWnd, SW_RESTORE);
403 if (do_fullscreen_switch()){
404 _properties.set_fullscreen(
true);
407 windisplay_cat.warning()
408 <<
"Switching to fullscreen mode failed!\n";
411 if (do_windowed_switch()){
412 _properties.set_fullscreen(
false);
415 windisplay_cat.warning()
416 <<
"Switching to windowed mode failed!\n";
421 if (properties.has_mouse_mode()) {
422 if (properties.
get_mouse_mode() != _properties.get_mouse_mode()) {
424 case WindowProperties::M_absolute:
425 case WindowProperties::M_relative:
427 if (_properties.get_mouse_mode() == WindowProperties::M_confined) {
429 windisplay_cat.info() <<
"Unconfining cursor from window\n";
431 _properties.set_mouse_mode(WindowProperties::M_absolute);
434 case WindowProperties::M_confined:
435 if (confine_cursor()) {
436 _properties.set_mouse_mode(WindowProperties::M_confined);
450 void WinGraphicsWindow::
452 GraphicsWindow::trigger_flip();
458 InvalidateRect(_hWnd,
nullptr, FALSE);
459 _got_expose_event =
false;
461 if (windisplay_cat.is_spam()) {
462 windisplay_cat.spam()
463 <<
"InvalidateRect: " <<
this <<
"\n";
471 void WinGraphicsWindow::
473 set_cursor_out_of_window();
474 DestroyWindow(_hWnd);
478 do_fullscreen_disable();
482 _window_handles.erase(_hWnd);
485 GraphicsWindow::close_window();
492 bool WinGraphicsWindow::
494 if (_properties.has_cursor_filename()) {
495 _cursor = get_cursor(_properties.get_cursor_filename());
498 _cursor = LoadCursor(
nullptr, IDC_ARROW);
500 bool want_foreground = (!_properties.has_foreground() || _properties.get_foreground());
501 bool want_minimized = (_properties.has_minimized() && _properties.get_minimized()) && !want_foreground;
503 HWND old_foreground_window = GetForegroundWindow();
508 _creating_window =
this;
509 bool opened = open_graphic_window();
510 _creating_window =
nullptr;
518 _window_handles.insert(WindowHandles::value_type(_hWnd,
this));
521 SetWindowPos(_hWnd, HWND_TOP, 0,0,0,0,
522 SWP_NOMOVE | SWP_NOSENDCHANGING | SWP_NOSIZE);
525 if (want_minimized) {
526 ShowWindow(_hWnd, SW_MINIMIZE);
527 ShowWindow(_hWnd, SW_MINIMIZE);
529 ShowWindow(_hWnd, SW_SHOWNORMAL);
530 ShowWindow(_hWnd, SW_SHOWNORMAL);
533 HWND new_foreground_window = _hWnd;
534 if (!want_foreground) {
537 new_foreground_window = old_foreground_window;
540 if (!SetActiveWindow(new_foreground_window)) {
541 windisplay_cat.warning()
542 <<
"SetActiveWindow() failed!\n";
548 if (!SetForegroundWindow(new_foreground_window)) {
549 windisplay_cat.warning()
550 <<
"SetForegroundWindow() failed!\n";
556 HIMC hIMC = ImmGetContext(_hWnd);
558 _ime_open = (ImmGetOpenStatus(hIMC) != 0);
559 ImmReleaseContext(_hWnd, hIMC);
563 if (_input_devices.size() > 1) {
565 Rid.usUsagePage = 0x01;
568 Rid.hwndTarget = _hWnd;
569 RegisterRawInputDevices(&Rid, 1,
sizeof (Rid));
573 _window_handle = NativeWindowHandle::make_win(_hWnd);
576 _window_handle =
new WinWindowHandle(
this, *_window_handle);
579 if (_parent_window_handle !=
nullptr) {
580 _parent_window_handle->attach_child(_window_handle);
587 static bool initialized =
false;
590 HMODULE user32 = GetModuleHandleA(
"user32.dll");
593 pRegisterTouchWindow = (PFN_REGISTERTOUCHWINDOW)GetProcAddress(user32,
"RegisterTouchWindow");
594 pGetTouchInputInfo = (PFN_GETTOUCHINPUTINFO)GetProcAddress(user32,
"GetTouchInputInfo");
595 pCloseTouchInputHandle = (PFN_CLOSETOUCHINPUTHANDLE)GetProcAddress(user32,
"CloseTouchInputHandle");
600 if (pRegisterTouchWindow !=
nullptr) {
601 pRegisterTouchWindow(_hWnd, 0);
613 void WinGraphicsWindow::
614 initialize_input_devices() {
616 PRAWINPUTDEVICELIST pRawInputDeviceList;
618 nassertv(_input_devices.size() == 0);
621 memset(_input_device_handle, 0,
sizeof(_input_device_handle));
623 GraphicsWindowInputDevice::pointer_and_keyboard(
this,
"keyboard_mouse");
624 add_input_device(device);
628 if (GetRawInputDeviceList(
nullptr, &nInputDevices,
sizeof(RAWINPUTDEVICELIST)) != 0) {
633 pRawInputDeviceList = (PRAWINPUTDEVICELIST)alloca(
sizeof(RAWINPUTDEVICELIST) * nInputDevices);
634 if (pRawInputDeviceList==0) {
639 if (GetRawInputDeviceList(pRawInputDeviceList, &nInputDevices,
sizeof(RAWINPUTDEVICELIST)) == -1) {
644 for (
int i = 0; i < (int)nInputDevices; i++) {
645 if (pRawInputDeviceList[i].dwType == RIM_TYPEMOUSE) {
648 if (GetRawInputDeviceInfoA(pRawInputDeviceList[i].hDevice, RIDI_DEVICENAME, (LPVOID)0, &nSize) != 0) {
651 char *psName = (
char*)alloca(
sizeof(TCHAR) * nSize);
652 if (psName == 0)
return;
653 if (GetRawInputDeviceInfoA(pRawInputDeviceList[i].hDevice, RIDI_DEVICENAME, (LPVOID)psName, &nSize) < 0) {
658 if (strncmp(psName,
"\\??\\Root#RDP_MOU#0000#",22)!=0) {
659 if (_input_devices.size() < 32) {
660 if (strncmp(psName,
"\\??\\",4)==0) psName += 4;
661 char *pound1 = strchr(psName,
'#');
662 char *pound2 = pound1 ? strchr(pound1+1,
'#') : 0;
663 char *pound3 = pound2 ? strchr(pound2+1,
'#') : 0;
664 if (pound3) *pound3 = 0;
665 for (
char *p = psName; *p; p++) {
670 if (pound2) *pound2 =
'.';
671 _input_device_handle[_input_devices.size()] = pRawInputDeviceList[i].hDevice;
674 device->set_pointer_in_window(0, 0);
675 add_input_device(device);
688 void WinGraphicsWindow::
698 void WinGraphicsWindow::
708 bool WinGraphicsWindow::
709 do_reshape_request(
int x_origin,
int y_origin,
bool has_origin,
710 int x_size,
int y_size) {
711 if (windisplay_cat.is_debug()) {
712 windisplay_cat.debug()
713 <<
"Got reshape request (" << x_origin <<
", " << y_origin
714 <<
", " << has_origin <<
", " << x_size <<
", " << y_size <<
")\n";
719 if (x_origin == -2) {
720 x_origin = 0.5 * (_pipe->get_display_width() - x_size);
722 if (y_origin == -2) {
723 y_origin = 0.5 * (_pipe->get_display_height() - y_size);
725 _properties.set_origin(x_origin, y_origin);
727 if (x_origin == -1 && y_origin == -1) {
737 SetRect(&view_rect, x_origin, y_origin,
738 x_origin + x_size, y_origin + y_size);
740 GetWindowInfo(_hWnd, &wi);
741 AdjustWindowRectEx(&view_rect, wi.dwStyle, FALSE, wi.dwExStyle);
743 UINT flags = SWP_NOZORDER | SWP_NOSENDCHANGING;
746 x_origin = view_rect.left;
747 y_origin = view_rect.top;
749 x_origin = CW_USEDEFAULT;
750 y_origin = CW_USEDEFAULT;
754 SetWindowPos(_hWnd,
nullptr, x_origin, y_origin,
755 view_rect.right - view_rect.left,
756 view_rect.bottom - view_rect.top,
760 if (_properties.has_mouse_mode() &&
761 _properties.get_mouse_mode() == WindowProperties::M_confined) {
770 return do_fullscreen_resize(x_size, y_size);
777 void WinGraphicsWindow::
780 if (!GetClientRect(_hWnd, &view_rect)) {
783 if (windisplay_cat.is_debug()) {
784 windisplay_cat.debug()
785 <<
"GetClientRect() failed in handle_reshape. Ignoring.\n";
792 if (view_rect.left == 0 && view_rect.right == 0 &&
793 view_rect.bottom == 0 && view_rect.top == 0) {
794 if (windisplay_cat.is_debug()) {
795 windisplay_cat.debug()
796 <<
"GetClientRect() returned all zeroes in handle_reshape. Ignoring.\n";
801 bool result = (FALSE != ClientToScreen(_hWnd, (POINT*)&view_rect.left));
803 result = (FALSE != ClientToScreen(_hWnd, (POINT*)&view_rect.right));
807 if (windisplay_cat.is_debug()) {
808 windisplay_cat.debug()
809 <<
"ClientToScreen() failed in handle_reshape. Ignoring.\n";
815 properties.
set_size((view_rect.right - view_rect.left),
816 (view_rect.bottom - view_rect.top));
819 properties.
set_origin(view_rect.left, view_rect.top);
821 if (windisplay_cat.is_debug()) {
822 windisplay_cat.debug()
823 <<
"reshape to origin: (" << properties.
get_x_origin() <<
","
829 system_changed_properties(properties);
835 bool WinGraphicsWindow::
836 do_fullscreen_resize(
int x_size,
int y_size) {
837 HWND hDesktopWindow = GetDesktopWindow();
838 HDC scrnDC = GetDC(hDesktopWindow);
839 DWORD dwFullScreenBitDepth = GetDeviceCaps(scrnDC, BITSPIXEL);
840 ReleaseDC(hDesktopWindow, scrnDC);
848 if (!find_acceptable_display_mode(x_size, y_size,
849 dwFullScreenBitDepth, dm)) {
850 windisplay_cat.error()
851 <<
"window resize(" << x_size <<
", " << y_size
852 <<
") failed, no compatible fullscreen display mode found!\n";
857 SetWindowPos(_hWnd,
nullptr, 0,0, x_size, y_size,
858 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSENDCHANGING);
859 int chg_result = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
861 if (chg_result != DISP_CHANGE_SUCCESSFUL) {
862 windisplay_cat.error()
863 <<
"resize ChangeDisplaySettings failed (error code: "
864 << chg_result <<
") for specified res: "
865 << dm.dmPelsWidth <<
" x " << dm.dmPelsHeight
866 <<
" x " << dm.dmBitsPerPel <<
", "
867 << dm.dmDisplayFrequency <<
" Hz\n";
871 _fullscreen_display_mode = dm;
873 windisplay_cat.info()
874 <<
"Resized fullscreen window to " << x_size <<
", " << y_size
875 <<
" bitdepth " << dwFullScreenBitDepth <<
", "
876 << dm.dmDisplayFrequency <<
"Hz\n";
878 _properties.set_size(x_size, y_size);
887 bool WinGraphicsWindow::
888 do_fullscreen_switch() {
889 if (!do_fullscreen_enable()) {
895 props.set_fullscreen(
true);
896 DWORD window_style = make_style(props);
897 SetWindowLong(_hWnd, GWL_STYLE, window_style);
899 WINDOW_METRICS metrics;
901 if (!calculate_metrics(
true, window_style, metrics, has_origin)){
905 SetWindowPos(_hWnd, HWND_NOTOPMOST, 0, 0, metrics.width, metrics.height,
906 SWP_FRAMECHANGED | SWP_SHOWWINDOW);
913 bool WinGraphicsWindow::
914 do_windowed_switch() {
915 do_fullscreen_disable();
918 props.set_fullscreen(
false);
919 DWORD window_style = make_style(props);
920 SetWindowLong(_hWnd, GWL_STYLE, window_style);
922 WINDOW_METRICS metrics;
925 if (!calculate_metrics(
false, window_style, metrics, has_origin)){
933 SetWindowPos(_hWnd, HWND_NOTOPMOST, 0, 0,
934 metrics.width, metrics.height,
935 SWP_FRAMECHANGED | SWP_SHOWWINDOW);
938 if (_properties.has_mouse_mode() &&
939 _properties.get_mouse_mode() == WindowProperties::M_confined) {
950 void WinGraphicsWindow::
951 reconsider_fullscreen_size(DWORD &, DWORD &, DWORD &) {
960 void WinGraphicsWindow::
961 support_overlay_window(
bool) {
967 DWORD WinGraphicsWindow::
976 DWORD window_style = WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
979 window_style |= WS_SYSMENU;
981 window_style |= (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX);
984 window_style |= (WS_SIZEBOX | WS_MAXIMIZEBOX);
986 window_style |= WS_BORDER;
996 bool WinGraphicsWindow::
997 calculate_metrics(
bool fullscreen, DWORD window_style, WINDOW_METRICS &metrics,
1001 has_origin = _properties.has_origin();
1002 if (!fullscreen && has_origin) {
1003 metrics.x = _properties.get_x_origin();
1004 metrics.y = _properties.get_y_origin();
1007 if (metrics.x == -2) {
1008 metrics.x = 0.5 * (_pipe->get_display_width() - _properties.get_x_size());
1010 if (metrics.y == -2) {
1011 metrics.y = 0.5 * (_pipe->get_display_height() - _properties.get_y_size());
1013 _properties.set_origin(metrics.x, metrics.y);
1015 if (metrics.x == -1 && metrics.y == -1) {
1022 metrics.width = _properties.get_x_size();
1023 metrics.height = _properties.get_y_size();
1027 SetRect(&win_rect, metrics.x, metrics.y,
1028 metrics.x + metrics.width, metrics.y + metrics.height);
1031 if (!AdjustWindowRect(&win_rect, window_style, FALSE)) {
1032 windisplay_cat.error()
1033 <<
"AdjustWindowRect failed!" << endl;
1038 metrics.x = win_rect.left;
1039 metrics.y = win_rect.top;
1041 metrics.x = CW_USEDEFAULT;
1042 metrics.y = CW_USEDEFAULT;
1044 metrics.width = win_rect.right - win_rect.left;
1045 metrics.height = win_rect.bottom - win_rect.top;
1054 bool WinGraphicsWindow::
1055 open_graphic_window() {
1056 DWORD window_style = make_style(_properties);
1059 if (_properties.has_title()) {
1061 title = encoder.
decode_text(_properties.get_title());
1064 if (!_properties.has_size()) {
1066 _properties.set_size(640, 480);
1069 WINDOW_METRICS metrics;
1071 if (!calculate_metrics(fullscreen, window_style, metrics, has_origin)){
1075 const WindowClass &wclass = register_window_class(_properties);
1076 HINSTANCE hinstance = GetModuleHandle(
nullptr);
1081 WindowHandle *window_handle = _properties.get_parent_window();
1082 if (window_handle !=
nullptr) {
1083 windisplay_cat.info()
1084 <<
"Got parent_window " << *window_handle <<
"\n";
1086 if (os_handle !=
nullptr) {
1087 windisplay_cat.info()
1088 <<
"os_handle type " << os_handle->get_type() <<
"\n";
1090 if (os_handle->
is_of_type(NativeWindowHandle::WinHandle::get_class_type())) {
1091 NativeWindowHandle::WinHandle *win_handle = DCAST(NativeWindowHandle::WinHandle, os_handle);
1092 _hparent = win_handle->get_handle();
1093 }
else if (os_handle->
is_of_type(NativeWindowHandle::IntHandle::get_class_type())) {
1095 _hparent = (HWND)int_handle->get_handle();
1099 _parent_window_handle = window_handle;
1101 _parent_window_handle =
nullptr;
1105 _hWnd = CreateWindowW(wclass._name.c_str(), title.c_str(), window_style,
1106 metrics.x, metrics.y,
1109 nullptr,
nullptr, hinstance, 0);
1114 if (!fullscreen && has_origin) {
1115 x_origin = _properties.get_x_origin();
1116 y_origin = _properties.get_y_origin();
1119 _hWnd = CreateWindowW(wclass._name.c_str(), title.c_str(),
1120 WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS ,
1122 _properties.get_x_size(), _properties.get_y_size(),
1123 _hparent,
nullptr, hinstance, 0);
1136 system_changed_properties(properties);
1141 windisplay_cat.error()
1142 <<
"CreateWindow() failed!" << endl;
1143 show_error_message();
1152 if (!do_fullscreen_enable()){
1164 bool WinGraphicsWindow::
1165 do_fullscreen_enable() {
1167 HWND hDesktopWindow = GetDesktopWindow();
1168 HDC scrnDC = GetDC(hDesktopWindow);
1169 DWORD cur_bitdepth = GetDeviceCaps(scrnDC, BITSPIXEL);
1173 ReleaseDC(hDesktopWindow, scrnDC);
1175 DWORD dwWidth = _properties.get_x_size();
1176 DWORD dwHeight = _properties.get_y_size();
1177 DWORD dwFullScreenBitDepth = cur_bitdepth;
1180 reconsider_fullscreen_size(dwWidth, dwHeight, dwFullScreenBitDepth);
1181 if (!find_acceptable_display_mode(dwWidth, dwHeight, dwFullScreenBitDepth, dm)) {
1182 windisplay_cat.error()
1183 <<
"Videocard has no supported display resolutions at specified res ("
1184 << dwWidth <<
" x " << dwHeight <<
" x " << dwFullScreenBitDepth <<
")\n";
1188 dm.dmPelsWidth = dwWidth;
1189 dm.dmPelsHeight = dwHeight;
1190 dm.dmBitsPerPel = dwFullScreenBitDepth;
1191 int chg_result = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
1193 if (chg_result != DISP_CHANGE_SUCCESSFUL) {
1194 windisplay_cat.error()
1195 <<
"ChangeDisplaySettings failed (error code: "
1196 << chg_result <<
") for specified res: "
1197 << dm.dmPelsWidth <<
" x " << dm.dmPelsHeight
1198 <<
" x " << dm.dmBitsPerPel <<
", "
1199 << dm.dmDisplayFrequency <<
" Hz\n";
1203 _fullscreen_display_mode = dm;
1205 _properties.set_origin(0, 0);
1206 _properties.set_size(dwWidth, dwHeight);
1216 bool WinGraphicsWindow::
1217 do_fullscreen_disable() {
1218 int chg_result = ChangeDisplaySettings(
nullptr, 0x0);
1219 if (chg_result != DISP_CHANGE_SUCCESSFUL) {
1220 windisplay_cat.warning()
1221 <<
"ChangeDisplaySettings failed to restore Windowed mode\n";
1230 void WinGraphicsWindow::
1232 WindowProperties::ZOrder z_order = _properties.get_z_order();
1233 adjust_z_order(z_order, z_order);
1239 void WinGraphicsWindow::
1240 adjust_z_order(WindowProperties::ZOrder last_z_order,
1241 WindowProperties::ZOrder this_z_order) {
1243 bool do_change =
false;
1245 switch (this_z_order) {
1246 case WindowProperties::Z_bottom:
1247 order = HWND_BOTTOM;
1251 case WindowProperties::Z_normal:
1252 if ((last_z_order != WindowProperties::Z_normal) &&
1254 (last_z_order != WindowProperties::Z_bottom ||
1255 _properties.get_foreground())
1260 order = HWND_NOTOPMOST;
1265 case WindowProperties::Z_top:
1266 order = HWND_TOPMOST;
1271 BOOL result = SetWindowPos(_hWnd, order, 0,0,0,0,
1272 SWP_NOMOVE | SWP_NOSENDCHANGING | SWP_NOSIZE);
1274 windisplay_cat.warning()
1275 <<
"SetWindowPos failed.\n";
1285 void WinGraphicsWindow::
1286 track_mouse_leaving(HWND hwnd) {
1288 DCAST_INTO_V(winpipe, _pipe);
1290 TRACKMOUSEEVENT tme = {
1291 sizeof(TRACKMOUSEEVENT),
1298 BOOL bSucceeded = TrackMouseEvent(&tme);
1300 if (!bSucceeded && windisplay_cat.is_debug()) {
1301 windisplay_cat.debug()
1302 <<
"TrackMouseEvent failed!, LastError=" << GetLastError() << endl;
1305 _tracking_mouse_leaving =
true;
1311 bool WinGraphicsWindow::
1314 if (!GetWindowRect(_hWnd, &clip)) {
1315 windisplay_cat.warning()
1316 <<
"GetWindowRect() failed, cannot confine cursor.\n";
1319 windisplay_cat.info()
1320 <<
"ClipCursor() to " << clip.left <<
"," << clip.top <<
" to "
1321 << clip.right <<
"," << clip.bottom << endl;
1323 if (!ClipCursor(&clip)) {
1324 windisplay_cat.warning()
1325 <<
"Failed to confine cursor to window.\n";
1337 void WinGraphicsWindow::
1339 if (SetFocus(_hWnd) ==
nullptr && GetLastError() != 0) {
1345 if (_parent_window_handle !=
nullptr && _window_handle !=
nullptr) {
1346 _parent_window_handle->request_keyboard_focus(_window_handle);
1349 windisplay_cat.error()
1350 <<
"SetFocus failed: " << GetLastError() <<
"\n";
1377 if (windisplay_cat.is_spam()) {
1378 windisplay_cat.spam()
1380 <<
" window_proc(" << (
void *)
this <<
", " << hwnd <<
", "
1381 << msg <<
", " << wparam <<
", " << lparam <<
")\n";
1387 if (!_tracking_mouse_leaving) {
1389 track_mouse_leaving(hwnd);
1391 set_cursor_in_window();
1392 if(handle_mouse_motion(translate_mouse(LOWORD(lparam)), translate_mouse(HIWORD(lparam))))
1397 handle_raw_input((HRAWINPUT)lparam);
1401 _tracking_mouse_leaving =
false;
1402 handle_mouse_exit();
1403 set_cursor_out_of_window();
1408 track_mouse_leaving(hwnd);
1409 ClearToBlack(hwnd, _properties);
1412 GetCursorPos(&cpos);
1413 ScreenToClient(hwnd, &cpos);
1415 GetClientRect(hwnd, &clientRect);
1416 if (PtInRect(&clientRect,cpos)) {
1417 set_cursor_in_window();
1419 set_cursor_out_of_window();
1452 if (!close_request_event.empty()) {
1455 throw_event(close_request_event);
1463 system_changed_properties(properties);
1470 case WM_CHILDACTIVATE:
1471 if (windisplay_cat.is_debug()) {
1472 windisplay_cat.debug()
1473 <<
"WM_CHILDACTIVATE: " << hwnd <<
"\n";
1478 if (windisplay_cat.is_debug()) {
1479 windisplay_cat.debug()
1480 <<
"WM_ACTIVATE: " << hwnd <<
", " << wparam <<
", " << lparam <<
"\n";
1483 if ((wparam & 0xffff) != WA_INACTIVE)
1491 ChangeDisplaySettings(&_fullscreen_display_mode, CDS_FULLSCREEN);
1492 if (chg_result != DISP_CHANGE_SUCCESSFUL) {
1493 const DEVMODE &dm = _fullscreen_display_mode;
1494 windisplay_cat.error()
1495 <<
"restore ChangeDisplaySettings failed (error code: "
1496 << chg_result <<
") for specified res: "
1497 << dm.dmPelsWidth <<
" x " << dm.dmPelsHeight
1498 <<
" x " << dm.dmBitsPerPel <<
", "
1499 << dm.dmDisplayFrequency <<
" Hz\n";
1503 SetWindowPos(_hWnd, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSENDCHANGING | SWP_NOSIZE | SWP_NOOWNERZORDER);
1504 fullscreen_restored(properties);
1508 if (_properties.has_mouse_mode() &&
1509 _properties.get_mouse_mode() == WindowProperties::M_confined) {
1510 if (!confine_cursor()) {
1527 ShowWindow(_hWnd, SW_MINIMIZE);
1529 do_fullscreen_disable();
1530 fullscreen_minimized(properties);
1535 system_changed_properties(properties);
1541 if (windisplay_cat.is_debug()) {
1542 windisplay_cat.debug()
1543 <<
"WM_SIZE: " << hwnd <<
", " << wparam <<
"\n";
1547 case WM_EXITSIZEMOVE:
1548 if (windisplay_cat.is_debug()) {
1549 windisplay_cat.debug()
1550 <<
"WM_EXITSIZEMOVE: " << hwnd <<
", " << wparam <<
"\n";
1554 if (_properties.has_mouse_mode() &&
1555 _properties.get_mouse_mode() == WindowProperties::M_confined) {
1560 case WM_WINDOWPOSCHANGED:
1561 if (windisplay_cat.is_debug()) {
1562 windisplay_cat.debug()
1563 <<
"WM_WINDOWPOSCHANGED: " << hwnd <<
", " << wparam <<
"\n";
1565 if (_hWnd !=
nullptr) {
1575 if (GetUpdateRect(_hWnd,
nullptr,
false)) {
1576 if (windisplay_cat.is_spam()) {
1577 windisplay_cat.spam()
1578 <<
"Got update regions: " <<
this <<
"\n";
1580 _got_expose_event =
true;
1584 case WM_LBUTTONDOWN:
1585 if (_lost_keypresses) {
1586 resend_lost_keypresses();
1596 case WM_MBUTTONDOWN:
1597 if (_lost_keypresses) {
1598 resend_lost_keypresses();
1607 case WM_RBUTTONDOWN:
1608 if (_lost_keypresses) {
1609 resend_lost_keypresses();
1618 case WM_XBUTTONDOWN:
1620 if (_lost_keypresses) {
1621 resend_lost_keypresses();
1624 int whichButton = GET_XBUTTON_WPARAM(wparam);
1626 if (whichButton == XBUTTON1) {
1628 }
else if (whichButton == XBUTTON2) {
1635 if (_lost_keypresses) {
1636 resend_lost_keypresses();
1643 if (_lost_keypresses) {
1644 resend_lost_keypresses();
1651 if (_lost_keypresses) {
1652 resend_lost_keypresses();
1660 if (_lost_keypresses) {
1661 resend_lost_keypresses();
1664 int whichButton = GET_XBUTTON_WPARAM(wparam);
1665 if (whichButton == XBUTTON1) {
1667 }
else if (whichButton == XBUTTON2) {
1675 int delta = GET_WHEEL_DELTA_WPARAM(wparam);
1678 GetCursorPos(&point);
1679 ScreenToClient(hwnd, &point);
1680 double time = get_message_time();
1686 delta -= WHEEL_DELTA;
1692 delta += WHEEL_DELTA;
1700 case WM_IME_SETCONTEXT:
1704 windisplay_cat.debug() <<
"hwnd = " << hwnd <<
" and GetFocus = " << GetFocus() << endl;
1705 _ime_hWnd = ImmGetDefaultIMEWnd(hwnd);
1706 if (::SendMessage(_ime_hWnd, WM_IME_CONTROL, IMC_CLOSESTATUSWINDOW, 0))
1708 windisplay_cat.debug() <<
"SendMessage failed for " << _ime_hWnd << endl;
1710 windisplay_cat.debug() <<
"SendMessage Succeeded for " << _ime_hWnd << endl;
1712 windisplay_cat.debug() <<
"wparam is " << wparam <<
", lparam is " << lparam << endl;
1713 lparam &= ~ISC_SHOWUIALL;
1714 if (ImmIsUIMessage(_ime_hWnd, msg, wparam, lparam))
1715 windisplay_cat.debug() <<
"wparam is " << wparam <<
", lparam is " << lparam << endl;
1721 if (wparam == IMN_SETOPENSTATUS) {
1722 HIMC hIMC = ImmGetContext(hwnd);
1723 nassertr(hIMC != 0, 0);
1724 _ime_open = (ImmGetOpenStatus(hIMC) != 0);
1726 _ime_active =
false;
1730 COMPOSITIONFORM comf;
1732 ImmGetCompositionWindow(hIMC, &comf);
1733 ImmGetCandidateWindow(hIMC, 0, &canf);
1734 windisplay_cat.debug() <<
1735 "comf style " << comf.dwStyle <<
1736 " comf point: x" << comf.ptCurrentPos.x <<
",y " << comf.ptCurrentPos.y <<
1737 " comf rect: l " << comf.rcArea.left <<
",t " << comf.rcArea.top <<
",r " <<
1738 comf.rcArea.right <<
",b " << comf.rcArea.bottom << endl;
1739 windisplay_cat.debug() <<
1740 "canf style " << canf.dwStyle <<
1741 " canf point: x" << canf.ptCurrentPos.x <<
",y " << canf.ptCurrentPos.y <<
1742 " canf rect: l " << canf.rcArea.left <<
",t " << canf.rcArea.top <<
",r " <<
1743 canf.rcArea.right <<
",b " << canf.rcArea.bottom << endl;
1744 comf.dwStyle = CFS_POINT;
1745 comf.ptCurrentPos.x = 2000;
1746 comf.ptCurrentPos.y = 2000;
1748 canf.dwStyle = CFS_EXCLUDE;
1750 canf.ptCurrentPos.x = 0;
1751 canf.ptCurrentPos.y = 0;
1752 canf.rcArea.left = 0;
1753 canf.rcArea.top = 0;
1754 canf.rcArea.right = 640;
1755 canf.rcArea.bottom = 480;
1758 comf.rcArea.left = 200;
1759 comf.rcArea.top = 200;
1760 comf.rcArea.right = 0;
1761 comf.rcArea.bottom = 0;
1764 if (ImmSetCompositionWindow(hIMC, &comf))
1765 windisplay_cat.debug() <<
"set composition form: success\n";
1766 for (
int i=0; i<3; ++i) {
1767 if (ImmSetCandidateWindow(hIMC, &canf))
1768 windisplay_cat.debug() <<
"set candidate form: success\n";
1773 ImmReleaseContext(hwnd, hIMC);
1777 case WM_IME_STARTCOMPOSITION:
1778 support_overlay_window(
true);
1782 case WM_IME_ENDCOMPOSITION:
1783 support_overlay_window(
false);
1784 _ime_active =
false;
1793 case WM_IME_COMPOSITION:
1805 HIMC hIMC = ImmGetContext(hwnd);
1806 nassertr(hIMC != 0, 0);
1808 DWORD result_size = 0;
1809 static const int ime_buffer_size = 256;
1810 static const int ime_buffer_size_bytes = ime_buffer_size /
sizeof(wchar_t);
1811 wchar_t ime_buffer[ime_buffer_size];
1812 size_t cursor_pos, delta_start;
1814 if (lparam & GCS_RESULTSTR) {
1815 result_size = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR,
1816 ime_buffer, ime_buffer_size_bytes);
1817 size_t num_chars = result_size /
sizeof(wchar_t);
1818 for (
size_t i = 0; i < num_chars; ++i) {
1823 if (lparam & GCS_COMPSTR) {
1824 result_size = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS,
nullptr, 0);
1825 cursor_pos = result_size & 0xffff;
1827 result_size = ImmGetCompositionStringW(hIMC, GCS_DELTASTART,
nullptr, 0);
1828 delta_start = result_size & 0xffff;
1829 result_size = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, ime_buffer, ime_buffer_size);
1830 size_t num_chars = result_size /
sizeof(wchar_t);
1832 _input->
candidate(wstring(ime_buffer, num_chars),
1833 std::min(cursor_pos, delta_start),
1834 std::max(cursor_pos, delta_start),
1837 ImmReleaseContext(hwnd, hIMC);
1858 if (_lost_keypresses) {
1859 resend_lost_keypresses();
1861 if (windisplay_cat.is_debug()) {
1862 windisplay_cat.debug()
1863 <<
"syskeydown: " << wparam <<
" (" << lookup_key(wparam) <<
")\n";
1869 GetCursorPos(&point);
1870 ScreenToClient(hwnd, &point);
1871 handle_keypress(lookup_key(wparam), point.x, point.y,
1872 get_message_time());
1874 if ((lparam & 0x40000000) == 0) {
1875 handle_raw_keypress(lookup_raw_key(lparam), get_message_time());
1886 if (wparam == VK_MENU) {
1887 if ((GetKeyState(VK_LMENU) & 0x8000) != 0 && ! _lalt_down) {
1888 handle_keypress(KeyboardButton::lalt(), point.x, point.y,
1889 get_message_time());
1892 if ((GetKeyState(VK_RMENU) & 0x8000) != 0 && ! _ralt_down) {
1893 handle_keypress(KeyboardButton::ralt(), point.x, point.y,
1894 get_message_time());
1898 if (wparam == VK_F10) {
1907 if (wparam == SC_KEYMENU) {
1919 if (_lost_keypresses) {
1920 resend_lost_keypresses();
1922 if (windisplay_cat.is_debug()) {
1923 windisplay_cat.debug()
1924 <<
"keydown: " << wparam <<
" (" << lookup_key(wparam) <<
")\n";
1929 if ((lparam & 0x40000000) == 0) {
1931 GetCursorPos(&point);
1932 ScreenToClient(hwnd, &point);
1933 handle_keypress(lookup_key(wparam), point.x, point.y,
1934 get_message_time());
1935 handle_raw_keypress(lookup_raw_key(lparam), get_message_time());
1941 if (wparam == VK_SHIFT) {
1942 if ((GetKeyState(VK_LSHIFT) & 0x8000) != 0 && ! _lshift_down) {
1943 handle_keypress(KeyboardButton::lshift(), point.x, point.y,
1944 get_message_time());
1945 _lshift_down =
true;
1947 if ((GetKeyState(VK_RSHIFT) & 0x8000) != 0 && ! _rshift_down) {
1948 handle_keypress(KeyboardButton::rshift(), point.x, point.y,
1949 get_message_time());
1950 _rshift_down =
true;
1952 }
else if(wparam == VK_CONTROL) {
1953 if ((GetKeyState(VK_LCONTROL) & 0x8000) != 0 && ! _lcontrol_down) {
1954 handle_keypress(KeyboardButton::lcontrol(), point.x, point.y,
1955 get_message_time());
1956 _lcontrol_down =
true;
1958 if ((GetKeyState(VK_RCONTROL) & 0x8000) != 0 && ! _rcontrol_down) {
1959 handle_keypress(KeyboardButton::rcontrol(), point.x, point.y,
1960 get_message_time());
1961 _rcontrol_down =
true;
1967 if ((wparam==
'V') && (GetKeyState(VK_CONTROL) < 0) &&
1968 !_input_devices.empty() && paste_emit_keystrokes) {
1972 if (IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(
nullptr)) {
1974 hglb = GetClipboardData(CF_TEXT);
1975 if (hglb!=
nullptr) {
1976 lptstr = (
char *) GlobalLock(hglb);
1977 if (lptstr !=
nullptr) {
1979 for (pChar = lptstr; *pChar; pChar++) {
1992 GetCursorPos(&point);
1993 ScreenToClient(hwnd, &point);
1994 handle_keypress(lookup_key(wparam), point.x, point.y,
1995 get_message_time());
2008 if (wparam == VK_SHIFT) {
2009 if (((GetKeyState(VK_LSHIFT) & 0x8000) != 0) && ! _lshift_down ) {
2010 handle_keypress(KeyboardButton::lshift(), point.x, point.y,
2011 get_message_time());
2012 _lshift_down =
true;
2013 }
else if (((GetKeyState(VK_RSHIFT) & 0x8000) != 0) && ! _rshift_down ) {
2014 handle_keypress(KeyboardButton::rshift(), point.x, point.y,
2015 get_message_time());
2016 _rshift_down =
true;
2018 if ((GetKeyState(VK_LSHIFT) & 0x8000) != 0) {
2019 handle_keypress(KeyboardButton::lshift(), point.x, point.y,
2020 get_message_time());
2022 if ((GetKeyState(VK_RSHIFT) & 0x8000) != 0) {
2023 handle_keypress(KeyboardButton::rshift(), point.x, point.y,
2024 get_message_time());
2027 }
else if(wparam == VK_CONTROL) {
2028 if (((GetKeyState(VK_LCONTROL) & 0x8000) != 0) && ! _lcontrol_down ) {
2029 handle_keypress(KeyboardButton::lcontrol(), point.x, point.y,
2030 get_message_time());
2031 _lcontrol_down =
true;
2032 }
else if (((GetKeyState(VK_RCONTROL) & 0x8000) != 0) && ! _rcontrol_down ) {
2033 handle_keypress(KeyboardButton::rcontrol(), point.x, point.y,
2034 get_message_time());
2035 _rcontrol_down =
true;
2037 if ((GetKeyState(VK_LCONTROL) & 0x8000) != 0) {
2038 handle_keypress(KeyboardButton::lcontrol(), point.x, point.y,
2039 get_message_time());
2041 if ((GetKeyState(VK_RCONTROL) & 0x8000) != 0) {
2042 handle_keypress(KeyboardButton::rcontrol(), point.x, point.y,
2043 get_message_time());
2052 if (_lost_keypresses) {
2053 resend_lost_keypresses();
2055 if (windisplay_cat.is_debug()) {
2056 windisplay_cat.debug()
2057 <<
"keyup: " << wparam <<
" (" << lookup_key(wparam) <<
")\n";
2059 handle_keyrelease(lookup_key(wparam), get_message_time());
2060 handle_raw_keyrelease(lookup_raw_key(lparam), get_message_time());
2065 if (wparam == VK_SHIFT) {
2066 if ((GetKeyState(VK_LSHIFT) & 0x8000) == 0 && _lshift_down) {
2067 handle_keyrelease(KeyboardButton::lshift(), get_message_time());
2068 _lshift_down =
false;
2070 if ((GetKeyState(VK_RSHIFT) & 0x8000) == 0 && _rshift_down) {
2071 handle_keyrelease(KeyboardButton::rshift(), get_message_time());
2072 _rshift_down =
false;
2074 }
else if(wparam == VK_CONTROL) {
2075 if ((GetKeyState(VK_LCONTROL) & 0x8000) == 0 && _lcontrol_down) {
2076 handle_keyrelease(KeyboardButton::lcontrol(), get_message_time());
2077 _lcontrol_down =
false;
2079 if ((GetKeyState(VK_RCONTROL) & 0x8000) == 0 && _rcontrol_down) {
2080 handle_keyrelease(KeyboardButton::rcontrol(), get_message_time());
2081 _rcontrol_down =
false;
2083 }
else if(wparam == VK_MENU) {
2084 if ((GetKeyState(VK_LMENU) & 0x8000) == 0 && _lalt_down) {
2085 handle_keyrelease(KeyboardButton::lalt(), get_message_time());
2088 if ((GetKeyState(VK_RMENU) & 0x8000) == 0 && _ralt_down) {
2089 handle_keyrelease(KeyboardButton::ralt(), get_message_time());
2096 if (windisplay_cat.is_debug()) {
2097 windisplay_cat.debug()
2103 system_changed_properties(properties);
2125 if (windisplay_cat.is_debug()) {
2126 windisplay_cat.debug()
2130 if (_lost_keypresses) {
2131 resend_lost_keypresses();
2135 system_changed_properties(properties);
2139 if (windisplay_cat.is_debug()) {
2140 windisplay_cat.debug()
2144 system_changed_properties(properties);
2148 if (windisplay_cat.is_debug()) {
2149 windisplay_cat.debug()
2153 system_changed_properties(properties);
2159 if (windisplay_cat.is_debug()) {
2160 windisplay_cat.debug() <<
"DPI changed to " << LOWORD(wparam);
2162 if (LOWORD(wparam) != HIWORD(wparam)) {
2163 windisplay_cat.debug(
false) <<
"x" << HIWORD(wparam) <<
"\n";
2165 windisplay_cat.debug(
false) <<
"\n";
2170 if (!_properties.get_fixed_size() && dpi_window_resize) {
2171 RECT &rect = *(LPRECT)lparam;
2172 SetWindowPos(_hWnd, HWND_TOP, rect.left, rect.top,
2173 rect.right - rect.left, rect.bottom - rect.top,
2174 SWP_NOZORDER | SWP_NOACTIVATE);
2179 _num_touches = LOWORD(wparam);
2180 if (_num_touches > MAX_TOUCHES) {
2181 _num_touches = MAX_TOUCHES;
2183 if (pGetTouchInputInfo != 0) {
2184 pGetTouchInputInfo((HTOUCHINPUT)lparam, _num_touches, _touches,
sizeof(TOUCHINPUT));
2185 pCloseTouchInputHandle((HTOUCHINPUT)lparam);
2191 for ( WinProcClasses::iterator it=_window_proc_classes.begin() ; it != _window_proc_classes.end(); it++ ){
2192 (*it)->wnd_proc(
this, hwnd, msg, wparam, lparam);
2195 return DefWindowProcW(hwnd, msg, wparam, lparam);
2206 WindowHandles::const_iterator wi;
2207 wi = _window_handles.find(hwnd);
2208 if (wi != _window_handles.end()) {
2210 return (*wi).second->window_proc(hwnd, msg, wparam, lparam);
2214 if (_creating_window !=
nullptr) {
2215 return _creating_window->
window_proc(hwnd, msg, wparam, lparam);
2220 return DefWindowProcW(hwnd, msg, wparam, lparam);
2226 void WinGraphicsWindow::
2230 if (!GetMessage(&msg,
nullptr, 0, 0)) {
2237 TranslateMessage(&msg);
2239 DispatchMessage(&msg);
2247 void WinGraphicsWindow::
2248 resend_lost_keypresses() {
2249 nassertv(_lost_keypresses);
2254 _lost_keypresses =
false;
2261 void WinGraphicsWindow::
2263 bool hide_cursor =
false;
2264 if (to_window ==
nullptr) {
2267 if (_got_saved_params) {
2268 SystemParametersInfo(SPI_SETMOUSETRAILS, _saved_mouse_trails,
2270 SystemParametersInfo(SPI_SETCURSORSHADOW, 0,
2271 _saved_cursor_shadow ? (PVOID)1 :
nullptr, 0);
2272 SystemParametersInfo(SPI_SETMOUSEVANISH, 0,
2273 _saved_mouse_vanish ? (PVOID)1 :
nullptr, 0);
2274 _got_saved_params =
false;
2286 if (!_got_saved_params) {
2287 SystemParametersInfo(SPI_GETMOUSETRAILS, 0,
2288 &_saved_mouse_trails, 0);
2289 SystemParametersInfo(SPI_GETCURSORSHADOW, 0,
2290 &_saved_cursor_shadow, 0);
2291 SystemParametersInfo(SPI_GETMOUSEVANISH, 0,
2292 &_saved_mouse_vanish, 0);
2293 _got_saved_params =
true;
2295 SystemParametersInfo(SPI_SETMOUSETRAILS, 0, (PVOID)0, 0);
2296 SystemParametersInfo(SPI_SETCURSORSHADOW, 0, (PVOID)
false, 0);
2297 SystemParametersInfo(SPI_SETMOUSEVANISH, 0, (PVOID)
false, 0);
2300 SetCursor(to_window->_cursor);
2303 hide_or_show_cursor(hide_cursor);
2305 _cursor_window = to_window;
2313 void WinGraphicsWindow::
2314 hide_or_show_cursor(
bool hide_cursor) {
2316 if (!_cursor_hidden) {
2318 _cursor_hidden =
true;
2321 if (_cursor_hidden) {
2323 _cursor_hidden =
false;
2329 #define MIN_REFRESH_RATE 60
2332 #define ACCEPTABLE_REFRESH_RATE(RATE) ((RATE >= MIN_REFRESH_RATE) || (RATE==0) || (RATE==1))
2338 bool WinGraphicsWindow::
2339 find_acceptable_display_mode(DWORD dwWidth, DWORD dwHeight, DWORD bpp,
2344 ZeroMemory(&cur_dm,
sizeof(cur_dm));
2345 cur_dm.dmSize =
sizeof(cur_dm);
2346 EnumDisplaySettings(
nullptr, ENUM_CURRENT_SETTINGS, &cur_dm);
2349 int saved_modenum = -1;
2352 ZeroMemory(&dm,
sizeof(dm));
2353 dm.dmSize =
sizeof(dm);
2355 if (!EnumDisplaySettings(
nullptr, modenum, &dm)) {
2359 if ((dm.dmPelsWidth == dwWidth) && (dm.dmPelsHeight == dwHeight) &&
2360 (dm.dmBitsPerPel == bpp)) {
2363 if (dm.dmDisplayFrequency == cur_dm.dmDisplayFrequency) {
2365 }
else if (saved_modenum == -1) {
2366 saved_modenum = modenum;
2374 if (saved_modenum != -1) {
2375 ZeroMemory(&dm,
sizeof(dm));
2376 dm.dmSize =
sizeof(dm);
2378 if (EnumDisplaySettings(
nullptr, saved_modenum, &dm)) {
2390 void WinGraphicsWindow::
2391 show_error_message(DWORD message_id) {
2392 LPTSTR message_buffer;
2394 if (message_id == 0) {
2395 message_id = GetLastError();
2398 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
2399 nullptr, message_id,
2400 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
2401 (LPTSTR)&message_buffer,
2403 MessageBox(GetDesktopWindow(), message_buffer, _T(errorbox_title), MB_OK);
2404 windisplay_cat.fatal() <<
"System error msg: " << message_buffer << endl;
2405 LocalFree(message_buffer);
2411 void WinGraphicsWindow::
2412 handle_keypress(
ButtonHandle key,
int x,
int y,
double time) {
2414 if (key != ButtonHandle::none()) {
2423 void WinGraphicsWindow::
2425 if (key != ButtonHandle::none()) {
2433 void WinGraphicsWindow::
2435 if (key != ButtonHandle::none()) {
2443 void WinGraphicsWindow::
2445 if (key != ButtonHandle::none()) {
2453 void WinGraphicsWindow::
2455 if (key != ButtonHandle::none()) {
2465 lookup_key(WPARAM wparam)
const {
2470 case VK_BACK:
return KeyboardButton::backspace();
2471 case VK_DELETE:
return KeyboardButton::del();
2472 case VK_ESCAPE:
return KeyboardButton::escape();
2473 case VK_SPACE:
return KeyboardButton::space();
2474 case VK_UP:
return KeyboardButton::up();
2475 case VK_DOWN:
return KeyboardButton::down();
2476 case VK_LEFT:
return KeyboardButton::left();
2477 case VK_RIGHT:
return KeyboardButton::right();
2484 case VK_TAB:
return KeyboardButton::tab();
2485 case VK_PRIOR:
return KeyboardButton::page_up();
2486 case VK_NEXT:
return KeyboardButton::page_down();
2487 case VK_HOME:
return KeyboardButton::home();
2488 case VK_END:
return KeyboardButton::end();
2489 case VK_F1:
return KeyboardButton::f1();
2490 case VK_F2:
return KeyboardButton::f2();
2491 case VK_F3:
return KeyboardButton::f3();
2492 case VK_F4:
return KeyboardButton::f4();
2493 case VK_F5:
return KeyboardButton::f5();
2494 case VK_F6:
return KeyboardButton::f6();
2495 case VK_F7:
return KeyboardButton::f7();
2496 case VK_F8:
return KeyboardButton::f8();
2497 case VK_F9:
return KeyboardButton::f9();
2498 case VK_F10:
return KeyboardButton::f10();
2499 case VK_F11:
return KeyboardButton::f11();
2500 case VK_F12:
return KeyboardButton::f12();
2501 case VK_INSERT:
return KeyboardButton::insert();
2502 case VK_CAPITAL:
return KeyboardButton::caps_lock();
2503 case VK_NUMLOCK:
return KeyboardButton::num_lock();
2504 case VK_SCROLL:
return KeyboardButton::scroll_lock();
2505 case VK_PAUSE:
return KeyboardButton::pause();
2506 case VK_SNAPSHOT:
return KeyboardButton::print_screen();
2508 case VK_SHIFT:
return KeyboardButton::shift();
2509 case VK_LSHIFT:
return KeyboardButton::lshift();
2510 case VK_RSHIFT:
return KeyboardButton::rshift();
2512 case VK_CONTROL:
return KeyboardButton::control();
2513 case VK_LCONTROL:
return KeyboardButton::lcontrol();
2514 case VK_RCONTROL:
return KeyboardButton::rcontrol();
2516 case VK_MENU:
return KeyboardButton::alt();
2517 case VK_LMENU:
return KeyboardButton::lalt();
2518 case VK_RMENU:
return KeyboardButton::ralt();
2521 int key = MapVirtualKey(wparam, 2);
2522 if (isascii(key) && key != 0) {
2537 return ButtonHandle::none();
2545 lookup_raw_key(LPARAM lparam)
const {
2546 unsigned char vsc = (lparam & 0xff0000) >> 16;
2548 if (lparam & 0x1000000) {
2551 case 28:
return KeyboardButton::enter();
2552 case 29:
return KeyboardButton::rcontrol();
2554 case 55:
return KeyboardButton::print_screen();
2555 case 56:
return KeyboardButton::ralt();
2556 case 69:
return KeyboardButton::num_lock();
2557 case 71:
return KeyboardButton::home();
2558 case 72:
return KeyboardButton::up();
2559 case 73:
return KeyboardButton::page_up();
2560 case 75:
return KeyboardButton::left();
2561 case 77:
return KeyboardButton::right();
2562 case 79:
return KeyboardButton::end();
2563 case 80:
return KeyboardButton::down();
2564 case 81:
return KeyboardButton::page_down();
2565 case 82:
return KeyboardButton::insert();
2566 case 83:
return KeyboardButton::del();
2567 case 91:
return KeyboardButton::lmeta();
2568 case 92:
return KeyboardButton::rmeta();
2569 case 93:
return KeyboardButton::menu();
2575 ButtonHandle::none(),
2576 KeyboardButton::escape(),
2589 KeyboardButton::backspace(),
2590 KeyboardButton::tab(),
2603 KeyboardButton::enter(),
2604 KeyboardButton::lcontrol(),
2617 KeyboardButton::lshift(),
2629 KeyboardButton::rshift(),
2631 KeyboardButton::lalt(),
2632 KeyboardButton::space(),
2633 KeyboardButton::caps_lock(),
2634 KeyboardButton::f1(),
2635 KeyboardButton::f2(),
2636 KeyboardButton::f3(),
2637 KeyboardButton::f4(),
2638 KeyboardButton::f5(),
2639 KeyboardButton::f6(),
2640 KeyboardButton::f7(),
2641 KeyboardButton::f8(),
2642 KeyboardButton::f9(),
2643 KeyboardButton::f10(),
2644 KeyboardButton::pause(),
2645 KeyboardButton::scroll_lock(),
2660 return raw_map[vsc];
2665 case 87:
return KeyboardButton::f11();
2666 case 88:
return KeyboardButton::f12();
2667 default:
return ButtonHandle::none();
2679 get_keyboard_map()
const {
2684 unsigned short ex_vsc[] = {0x57, 0x58,
2685 0x011c, 0x011d, 0x0135, 0x0137, 0x0138, 0x0145, 0x0147, 0x0148, 0x0149, 0x014b, 0x014d, 0x014f, 0x0150, 0x0151, 0x0152, 0x0153, 0x015b, 0x015c, 0x015d};
2687 for (
int k = 1; k < 84 + 17; ++k) {
2689 vsc = ex_vsc[k - 84];
2694 UINT lparam = vsc << 16;
2696 if (raw_button == ButtonHandle::none()) {
2702 button = KeyboardButton::pause();
2704 }
else if (vsc >= 0x47 && vsc <= 0x53) {
2706 button = raw_button;
2718 UINT vk = MapVirtualKeyA(vsc, MAPVK_VSC_TO_VK_EX);
2719 button = lookup_key(vk);
2725 int len = GetKeyNameTextW(lparam, text, 256);
2737 void WinGraphicsWindow::
2738 handle_raw_input(HRAWINPUT hraw) {
2745 if (GetRawInputData(hraw, RID_INPUT,
nullptr, &dwSize,
sizeof(RAWINPUTHEADER)) == -1) {
2749 lpb = (LPBYTE)alloca(
sizeof(LPBYTE) * dwSize);
2750 if (lpb ==
nullptr) {
2754 if (GetRawInputData(hraw, RID_INPUT, lpb, &dwSize,
sizeof(RAWINPUTHEADER)) != dwSize) {
2758 RAWINPUT *raw = (RAWINPUT *)lpb;
2759 if (raw->header.hDevice == 0) {
2763 for (
size_t i = 1; i < _input_devices.size(); ++i) {
2764 if (_input_device_handle[i] == raw->header.hDevice) {
2768 int adjx = raw->data.mouse.lLastX;
2769 int adjy = raw->data.mouse.lLastY;
2771 if (raw->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE) {
2772 input->set_pointer_in_window(adjx, adjy);
2774 input->pointer_moved(adjx, adjy);
2777 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_1_DOWN) {
2780 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_1_UP) {
2783 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_2_DOWN) {
2786 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_2_UP) {
2789 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_3_DOWN) {
2792 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_3_UP) {
2795 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_4_DOWN) {
2798 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_4_UP) {
2801 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_5_DOWN) {
2804 if (raw->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_5_UP) {
2814 bool WinGraphicsWindow::
2815 handle_mouse_motion(
int x,
int y) {
2823 void WinGraphicsWindow::
2824 handle_mouse_exit() {
2833 HICON WinGraphicsWindow::
2834 get_icon(
const Filename &filename) {
2836 IconFilenames::iterator fi = _icon_filenames.find(filename);
2837 if (fi != _icon_filenames.end()) {
2838 return (HICON)((*fi).second);
2852 windisplay_cat.warning()
2853 <<
"Could not find icon filename " << filename <<
"\n";
2857 fi = _icon_filenames.find(resolved);
2858 if (fi != _icon_filenames.end()) {
2859 _icon_filenames[filename] = (*fi).second;
2860 return (HICON)((*fi).second);
2865 HANDLE h = LoadImage(
nullptr, os.c_str(),
2866 IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
2868 windisplay_cat.warning()
2869 <<
"windows icon filename '" << os <<
"' could not be loaded!!\n";
2872 _icon_filenames[filename] = h;
2873 _icon_filenames[resolved] = h;
2881 HCURSOR WinGraphicsWindow::
2882 get_cursor(
const Filename &filename) {
2884 if (filename.empty()) {
2889 IconFilenames::iterator fi = _cursor_filenames.find(filename);
2890 if (fi != _cursor_filenames.end()) {
2891 return (HCURSOR)((*fi).second);
2901 windisplay_cat.warning()
2902 <<
"Could not find cursor filename " << filename <<
"\n";
2905 fi = _cursor_filenames.find(resolved);
2906 if (fi != _cursor_filenames.end()) {
2907 _cursor_filenames[filename] = (*fi).second;
2908 return (HCURSOR)((*fi).second);
2913 HANDLE h = LoadImage(
nullptr, os.c_str(),
2914 IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
2916 windisplay_cat.warning()
2917 <<
"windows cursor filename '" << os <<
"' could not be loaded!!\n";
2918 show_error_message();
2921 _cursor_filenames[filename] = h;
2922 _cursor_filenames[resolved] = h;
2926 static HCURSOR get_cursor(
const Filename &filename);
2932 const WinGraphicsWindow::WindowClass &WinGraphicsWindow::
2934 WindowClass wcreg(props);
2935 std::wostringstream wclass_name;
2936 wclass_name << L
"WinGraphicsWindow" << _window_class_index;
2937 wcreg._name = wclass_name.str();
2939 std::pair<WindowClasses::iterator, bool> found = _window_classes.insert(wcreg);
2940 const WindowClass &wclass = (*found.first);
2942 if (!found.second) {
2948 _window_class_index++;
2952 HINSTANCE instance = GetModuleHandle(
nullptr);
2955 ZeroMemory(&wc,
sizeof(wc));
2956 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
2958 wc.hInstance = instance;
2960 wc.hIcon = wclass._icon;
2962 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
2963 wc.lpszMenuName =
nullptr;
2964 wc.lpszClassName = wclass._name.c_str();
2966 if (!RegisterClassW(&wc)) {
2967 windisplay_cat.error()
2968 <<
"could not register window class " << wclass._name <<
"!" << endl;
2978 WinGraphicsWindow::WinWindowHandle::
2990 void WinGraphicsWindow::WinWindowHandle::
2999 void WinGraphicsWindow::WinWindowHandle::
3000 receive_windows_message(
unsigned int msg,
int wparam,
int lparam) {
3001 if (_window !=
nullptr) {
3008 void PrintErrorMessage(DWORD msgID) {
3009 LPTSTR pMessageBuffer;
3011 if (msgID==PRINT_LAST_ERROR)
3012 msgID=GetLastError();
3014 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
3016 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
3017 (LPTSTR) &pMessageBuffer,
3019 MessageBox(GetDesktopWindow(),pMessageBuffer,_T(errorbox_title),MB_OK);
3020 windisplay_cat.fatal() <<
"System error msg: " << pMessageBuffer << endl;
3021 LocalFree( pMessageBuffer );
3027 if (windisplay_cat.is_debug()) {
3028 windisplay_cat.debug()
3029 <<
"Skipping ClearToBlack, no origin specified yet.\n";
3034 if (windisplay_cat.is_debug()) {
3035 windisplay_cat.debug()
3036 <<
"ClearToBlack(" << hWnd <<
", " << props <<
")\n";
3039 HDC hDC=GetDC(hWnd);
3045 FillRect(hDC,&clrRect,(HBRUSH)GetStockObject(BLACK_BRUSH));
3046 ReleaseDC(hWnd,hDC);
3055 GetClientRect(hwnd, view_rect);
3058 ul.x = view_rect->left;
3059 ul.y = view_rect->top;
3060 lr.x = view_rect->right;
3061 lr.y = view_rect->bottom;
3063 ClientToScreen(hwnd, &ul);
3064 ClientToScreen(hwnd, &lr);
3066 view_rect->left = ul.x;
3067 view_rect->top = ul.y;
3068 view_rect->right = lr.x;
3069 view_rect->bottom = lr.y;
3078 nassertv(wnd_proc !=
nullptr);
3087 nassertv(wnd_proc !=
nullptr);
3096 _window_proc_classes.clear();
3113 return callbackData->get_msg() == WM_TOUCH;
3122 return _num_touches;
3131 nassertr(index >= 0 && index < MAX_TOUCHES,
TouchInfo());
3133 TOUCHINPUT ti = _touches[index];
3135 point.x = TOUCH_COORD_TO_PIXEL(ti.x);
3136 point.y = TOUCH_COORD_TO_PIXEL(ti.y);
3137 ScreenToClient(_hWnd, &point);
3142 ret.set_id(ti.dwID);
3143 ret.set_flags(ti.dwFlags);