ShellWin32.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <Shell.h>
  29. #include <RmlUi/Core.h>
  30. #include <win32/InputWin32.h>
  31. #include "ShellFileInterface.h"
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <stdarg.h>
  35. #include <shlwapi.h>
  36. static LRESULT CALLBACK WindowProcedure(HWND window_handle, UINT message, WPARAM w_param, LPARAM l_param);
  37. static Rml::Context* context = nullptr;
  38. static ShellRenderInterfaceExtensions* shell_renderer = nullptr;
  39. static bool activated = true;
  40. static bool running = false;
  41. static std::wstring instance_name;
  42. static HWND window_handle = nullptr;
  43. static HINSTANCE instance_handle = nullptr;
  44. static bool has_dpi_support = false;
  45. static UINT window_dpi = USER_DEFAULT_SCREEN_DPI;
  46. static int window_width = 0;
  47. static int window_height = 0;
  48. static double time_frequency;
  49. static LARGE_INTEGER time_startup;
  50. static Rml::UniquePtr<ShellFileInterface> file_interface;
  51. static HCURSOR cursor_default = nullptr;
  52. static HCURSOR cursor_move = nullptr;
  53. static HCURSOR cursor_pointer = nullptr;
  54. static HCURSOR cursor_resize= nullptr;
  55. static HCURSOR cursor_cross = nullptr;
  56. static HCURSOR cursor_text = nullptr;
  57. static HCURSOR cursor_unavailable = nullptr;
  58. #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
  59. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((HANDLE)-4)
  60. #endif
  61. #ifndef WM_DPICHANGED
  62. #define WM_DPICHANGED 0x02E0
  63. #endif
  64. // Declare pointers to the DPI aware Windows API functions.
  65. using ProcSetProcessDpiAwarenessContext = BOOL(WINAPI*)(HANDLE value);
  66. using ProcGetDpiForWindow = UINT(WINAPI*)(HWND hwnd);
  67. using ProcAdjustWindowRectExForDpi = BOOL(WINAPI*)(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
  68. static ProcSetProcessDpiAwarenessContext procSetProcessDpiAwarenessContext = NULL;
  69. static ProcGetDpiForWindow procGetDpiForWindow = NULL;
  70. static ProcAdjustWindowRectExForDpi procAdjustWindowRectExForDpi = NULL;
  71. static void UpdateDpi()
  72. {
  73. if (has_dpi_support)
  74. {
  75. UINT dpi = procGetDpiForWindow(window_handle);
  76. if (dpi != 0)
  77. {
  78. window_dpi = dpi;
  79. if (context)
  80. context->SetDensityIndependentPixelRatio(Shell::GetDensityIndependentPixelRatio());
  81. }
  82. }
  83. }
  84. static void UpdateWindowDimensions(int width = 0, int height = 0)
  85. {
  86. if (width > 0)
  87. window_width = width;
  88. if (height > 0)
  89. window_height = height;
  90. if (context)
  91. context->SetDimensions(Rml::Vector2i(window_width, window_height));
  92. if (shell_renderer)
  93. shell_renderer->SetViewport(window_width, window_height);
  94. }
  95. bool Shell::Initialise()
  96. {
  97. instance_handle = GetModuleHandle(nullptr);
  98. InputWin32::Initialise();
  99. LARGE_INTEGER time_ticks_per_second;
  100. QueryPerformanceFrequency(&time_ticks_per_second);
  101. QueryPerformanceCounter(&time_startup);
  102. time_frequency = 1.0 / (double) time_ticks_per_second.QuadPart;
  103. // Load cursors
  104. cursor_default = LoadCursor(nullptr, IDC_ARROW);
  105. cursor_move = LoadCursor(nullptr, IDC_SIZEALL);
  106. cursor_pointer = LoadCursor(nullptr, IDC_HAND);
  107. cursor_resize = LoadCursor(nullptr, IDC_SIZENWSE);
  108. cursor_cross = LoadCursor(nullptr, IDC_CROSS);
  109. cursor_text = LoadCursor(nullptr, IDC_IBEAM);
  110. cursor_unavailable = LoadCursor(nullptr, IDC_NO);
  111. Rml::String root = FindSamplesRoot();
  112. bool result = !root.empty();
  113. file_interface = Rml::MakeUnique<ShellFileInterface>(root);
  114. Rml::SetFileInterface(file_interface.get());
  115. // See if we have Per Monitor V2 DPI awareness. Requires Windows 10, version 1703.
  116. // Cast function pointers to void* first for MinGW not to emit errors.
  117. procSetProcessDpiAwarenessContext = (ProcSetProcessDpiAwarenessContext)(void*)GetProcAddress(
  118. GetModuleHandle(TEXT("User32.dll")),
  119. "SetProcessDpiAwarenessContext"
  120. );
  121. procGetDpiForWindow = (ProcGetDpiForWindow)(void*)GetProcAddress(
  122. GetModuleHandle(TEXT("User32.dll")),
  123. "GetDpiForWindow"
  124. );
  125. procAdjustWindowRectExForDpi = (ProcAdjustWindowRectExForDpi)(void*)GetProcAddress(
  126. GetModuleHandle(TEXT("User32.dll")),
  127. "AdjustWindowRectExForDpi"
  128. );
  129. has_dpi_support = (procSetProcessDpiAwarenessContext != NULL && procGetDpiForWindow != NULL && procAdjustWindowRectExForDpi != NULL);
  130. return result;
  131. }
  132. void Shell::Shutdown()
  133. {
  134. InputWin32::Shutdown();
  135. file_interface.reset();
  136. }
  137. Rml::String Shell::FindSamplesRoot()
  138. {
  139. const char* candidate_paths[] = { "", "..\\Samples\\", "..\\..\\Samples\\", "..\\..\\..\\Samples\\", "..\\..\\..\\..\\Samples\\" };
  140. // Fetch the path of the executable, test the candidate paths appended to that.
  141. char executable_file_name[MAX_PATH];
  142. if (GetModuleFileNameA(instance_handle, executable_file_name, MAX_PATH) >= MAX_PATH &&
  143. GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  144. {
  145. executable_file_name[0] = 0;
  146. }
  147. Rml::String executable_path(executable_file_name);
  148. executable_path = executable_path.substr(0, executable_path.rfind('\\') + 1);
  149. // We assume we have found the correct path if we can find the lookup file from it
  150. const char* lookup_file = "assets\\rml.rcss";
  151. for(const char* relative_path : candidate_paths)
  152. {
  153. Rml::String absolute_path = executable_path + relative_path;
  154. if (PathFileExistsA(Rml::String(absolute_path + lookup_file).c_str()))
  155. {
  156. char canonical_path[MAX_PATH];
  157. if (!PathCanonicalizeA(canonical_path, absolute_path.c_str()))
  158. canonical_path[0] = 0;
  159. return Rml::String(canonical_path);
  160. }
  161. }
  162. return Rml::String();
  163. }
  164. bool Shell::OpenWindow(const char* in_name, ShellRenderInterfaceExtensions *_shell_renderer, unsigned int width, unsigned int height, bool allow_resize)
  165. {
  166. // Activate Per Monitor V2.
  167. if (has_dpi_support && !procSetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
  168. {
  169. has_dpi_support = false;
  170. }
  171. WNDCLASSW window_class;
  172. const std::wstring name = ConvertToUTF16(Rml::String(in_name));
  173. // Fill out the window class struct.
  174. window_class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  175. window_class.lpfnWndProc = WindowProcedure;
  176. window_class.cbClsExtra = 0;
  177. window_class.cbWndExtra = 0;
  178. window_class.hInstance = instance_handle;
  179. window_class.hIcon = LoadIcon(nullptr, IDI_WINLOGO);
  180. window_class.hCursor = cursor_default;
  181. window_class.hbrBackground = nullptr;
  182. window_class.lpszMenuName = nullptr;
  183. window_class.lpszClassName = name.data();
  184. if (!RegisterClassW(&window_class))
  185. {
  186. DisplayError("Could not register window class.");
  187. CloseWindow();
  188. return false;
  189. }
  190. window_handle = CreateWindowExW(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
  191. name.data(), // Window class name.
  192. name.data(),
  193. WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,
  194. 0, 0, // Window position.
  195. 0, 0, // Window size.
  196. nullptr,
  197. nullptr,
  198. instance_handle,
  199. nullptr);
  200. if (!window_handle)
  201. {
  202. DisplayError("Could not create window.");
  203. CloseWindow();
  204. return false;
  205. }
  206. window_width = width;
  207. window_height = height;
  208. UpdateDpi();
  209. window_width = (width * window_dpi) / USER_DEFAULT_SCREEN_DPI;
  210. window_height = (height * window_dpi) / USER_DEFAULT_SCREEN_DPI;
  211. instance_name = name;
  212. DWORD style = (allow_resize ? WS_OVERLAPPEDWINDOW : (WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX));
  213. DWORD extended_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  214. // Adjust the window size to take into account the edges
  215. RECT window_rect;
  216. window_rect.top = 0;
  217. window_rect.left = 0;
  218. window_rect.right = window_width;
  219. window_rect.bottom = window_height;
  220. if (has_dpi_support)
  221. procAdjustWindowRectExForDpi(&window_rect, style, FALSE, extended_style, window_dpi);
  222. else
  223. AdjustWindowRectEx(&window_rect, style, FALSE, extended_style);
  224. SetWindowLong(window_handle, GWL_EXSTYLE, extended_style);
  225. SetWindowLong(window_handle, GWL_STYLE, style);
  226. if (_shell_renderer != nullptr)
  227. {
  228. shell_renderer = _shell_renderer;
  229. if(!shell_renderer->AttachToNative(window_handle))
  230. {
  231. CloseWindow();
  232. return false;
  233. }
  234. }
  235. UpdateWindowDimensions();
  236. // Resize the window.
  237. SetWindowPos(window_handle, HWND_TOP, 0, 0, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, SWP_NOACTIVATE);
  238. // Display the new window
  239. ShowWindow(window_handle, SW_SHOW);
  240. SetForegroundWindow(window_handle);
  241. SetFocus(window_handle);
  242. return true;
  243. }
  244. void Shell::CloseWindow()
  245. {
  246. if(shell_renderer) {
  247. shell_renderer->DetachFromNative();
  248. }
  249. DestroyWindow(window_handle);
  250. UnregisterClassW((LPCWSTR)instance_name.data(), instance_handle);
  251. }
  252. // Returns a platform-dependent handle to the window.
  253. void* Shell::GetWindowHandle()
  254. {
  255. return window_handle;
  256. }
  257. void Shell::EventLoop(ShellIdleFunction idle_function)
  258. {
  259. MSG message;
  260. running = true;
  261. // Loop on PeekMessage() / GetMessage() until exit has been requested.
  262. while (running)
  263. {
  264. if (PeekMessage(&message, nullptr, 0, 0, PM_NOREMOVE))
  265. {
  266. GetMessage(&message, nullptr, 0, 0);
  267. TranslateMessage(&message);
  268. DispatchMessage(&message);
  269. }
  270. idle_function();
  271. }
  272. }
  273. void Shell::RequestExit()
  274. {
  275. running = false;
  276. }
  277. void Shell::DisplayError(const char* fmt, ...)
  278. {
  279. const int buffer_size = 1024;
  280. char buffer[buffer_size];
  281. va_list argument_list;
  282. // Print the message to the buffer.
  283. va_start(argument_list, fmt);
  284. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  285. if ( len < 0 || len > buffer_size - 2 )
  286. {
  287. len = buffer_size - 2;
  288. }
  289. buffer[len] = '\n';
  290. buffer[len + 1] = '\0';
  291. va_end(argument_list);
  292. MessageBoxW(window_handle, ConvertToUTF16(buffer).c_str(), L"Shell Error", MB_OK);
  293. }
  294. void Shell::Log(const char* fmt, ...)
  295. {
  296. const int buffer_size = 1024;
  297. char buffer[buffer_size];
  298. va_list argument_list;
  299. // Print the message to the buffer.
  300. va_start(argument_list, fmt);
  301. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  302. if ( len < 0 || len > buffer_size - 2 )
  303. {
  304. len = buffer_size - 2;
  305. }
  306. buffer[len] = '\n';
  307. buffer[len + 1] = '\0';
  308. va_end(argument_list);
  309. OutputDebugStringW(ConvertToUTF16(buffer).c_str());
  310. }
  311. double Shell::GetElapsedTime()
  312. {
  313. LARGE_INTEGER counter;
  314. QueryPerformanceCounter(&counter);
  315. return double(counter.QuadPart - time_startup.QuadPart) * time_frequency;
  316. }
  317. void Shell::SetMouseCursor(const Rml::String& cursor_name)
  318. {
  319. if (window_handle)
  320. {
  321. HCURSOR cursor_handle = nullptr;
  322. if (cursor_name.empty() || cursor_name == "arrow")
  323. cursor_handle = cursor_default;
  324. else if(cursor_name == "move")
  325. cursor_handle = cursor_move;
  326. else if (cursor_name == "pointer")
  327. cursor_handle = cursor_pointer;
  328. else if (cursor_name == "resize")
  329. cursor_handle = cursor_resize;
  330. else if (cursor_name == "cross")
  331. cursor_handle = cursor_cross;
  332. else if (cursor_name == "text")
  333. cursor_handle = cursor_text;
  334. else if (cursor_name == "unavailable")
  335. cursor_handle = cursor_unavailable;
  336. if (cursor_handle)
  337. {
  338. SetCursor(cursor_handle);
  339. SetClassLongPtrA(window_handle, GCLP_HCURSOR, (LONG_PTR)cursor_handle);
  340. }
  341. }
  342. }
  343. void Shell::SetClipboardText(const Rml::String& text_utf8)
  344. {
  345. if (window_handle)
  346. {
  347. if (!OpenClipboard(window_handle))
  348. return;
  349. EmptyClipboard();
  350. const std::wstring text = ConvertToUTF16(text_utf8);
  351. const size_t size = sizeof(wchar_t) * (text.size() + 1);
  352. HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, size);
  353. memcpy(clipboard_data, text.data(), size);
  354. if (SetClipboardData(CF_UNICODETEXT, clipboard_data) == nullptr)
  355. {
  356. CloseClipboard();
  357. GlobalFree(clipboard_data);
  358. }
  359. else
  360. CloseClipboard();
  361. }
  362. }
  363. void Shell::GetClipboardText(Rml::String& text)
  364. {
  365. if (window_handle)
  366. {
  367. if (!OpenClipboard(window_handle))
  368. return;
  369. HANDLE clipboard_data = GetClipboardData(CF_UNICODETEXT);
  370. if (clipboard_data == nullptr)
  371. {
  372. CloseClipboard();
  373. return;
  374. }
  375. const wchar_t* clipboard_text = (const wchar_t*)GlobalLock(clipboard_data);
  376. if (clipboard_text)
  377. text = ConvertToUTF8(clipboard_text);
  378. GlobalUnlock(clipboard_data);
  379. CloseClipboard();
  380. }
  381. }
  382. void Shell::SetContext(Rml::Context* new_context)
  383. {
  384. context = new_context;
  385. UpdateDpi();
  386. UpdateWindowDimensions();
  387. }
  388. float Shell::GetDensityIndependentPixelRatio()
  389. {
  390. return float(window_dpi) / float(USER_DEFAULT_SCREEN_DPI);
  391. }
  392. static LRESULT CALLBACK WindowProcedure(HWND local_window_handle, UINT message, WPARAM w_param, LPARAM l_param)
  393. {
  394. // See what kind of message we've got.
  395. switch (message)
  396. {
  397. case WM_ACTIVATE:
  398. {
  399. if (LOWORD(w_param) != WA_INACTIVE)
  400. {
  401. activated = true;
  402. }
  403. else
  404. {
  405. activated = false;
  406. }
  407. }
  408. break;
  409. // When the window closes, request exit
  410. case WM_CLOSE:
  411. {
  412. running = false;
  413. return 0;
  414. }
  415. break;
  416. case WM_SIZE:
  417. {
  418. int width = LOWORD(l_param);
  419. int height = HIWORD(l_param);
  420. UpdateWindowDimensions(width, height);
  421. }
  422. break;
  423. case WM_DPICHANGED:
  424. {
  425. UpdateDpi();
  426. RECT* const new_pos = (RECT*)l_param;
  427. SetWindowPos(window_handle,
  428. NULL,
  429. new_pos->left,
  430. new_pos->top,
  431. new_pos->right - new_pos->left,
  432. new_pos->bottom - new_pos->top,
  433. SWP_NOZORDER | SWP_NOACTIVATE);
  434. }
  435. break;
  436. default:
  437. {
  438. InputWin32::ProcessWindowsEvent(local_window_handle, message, w_param, l_param);
  439. }
  440. break;
  441. }
  442. // All unhandled messages go to DefWindowProc.
  443. return DefWindowProc(local_window_handle, message, w_param, l_param);
  444. }