ShellWin32.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <windows.h>
  33. #include <stdio.h>
  34. #include <stdarg.h>
  35. static LRESULT CALLBACK WindowProcedure(HWND window_handle, UINT message, WPARAM w_param, LPARAM l_param);
  36. static bool activated = true;
  37. static bool running = false;
  38. static const char* instance_name = nullptr;
  39. static HWND window_handle = nullptr;
  40. static HINSTANCE instance_handle = nullptr;
  41. static double time_frequency;
  42. static LARGE_INTEGER time_startup;
  43. static std::unique_ptr<ShellFileInterface> file_interface;
  44. static HCURSOR cursor_default = nullptr;
  45. static HCURSOR cursor_move = nullptr;
  46. static HCURSOR cursor_cross = nullptr;
  47. static HCURSOR cursor_unavailable = nullptr;
  48. bool Shell::Initialise()
  49. {
  50. instance_handle = GetModuleHandle(nullptr);
  51. InputWin32::Initialise();
  52. LARGE_INTEGER time_ticks_per_second;
  53. QueryPerformanceFrequency(&time_ticks_per_second);
  54. QueryPerformanceCounter(&time_startup);
  55. time_frequency = 1.0 / (double) time_ticks_per_second.QuadPart;
  56. // Load cursors
  57. cursor_default = LoadCursorA(nullptr, IDC_ARROW);
  58. cursor_move = LoadCursorA(nullptr, IDC_SIZEALL);
  59. cursor_cross = LoadCursorA(nullptr, IDC_CROSS);
  60. cursor_unavailable = LoadCursorA(nullptr, IDC_NO);
  61. Rml::Core::String root = FindSamplesRoot();
  62. file_interface = std::make_unique<ShellFileInterface>(root);
  63. Rml::Core::SetFileInterface(file_interface.get());
  64. return true;
  65. }
  66. void Shell::Shutdown()
  67. {
  68. InputWin32::Shutdown();
  69. file_interface.reset();
  70. }
  71. Rml::Core::String Shell::FindSamplesRoot()
  72. {
  73. Rml::Core::String path = "../../Samples/";
  74. // Fetch the path of the executable, append the path onto that.
  75. char executable_file_name[MAX_PATH];
  76. if (GetModuleFileNameA(instance_handle, executable_file_name, MAX_PATH) >= MAX_PATH &&
  77. GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  78. {
  79. executable_file_name[0] = 0;
  80. }
  81. Rml::Core::String executable_path = Rml::Core::String(executable_file_name);
  82. executable_path = executable_path.substr(0, executable_path.rfind("\\") + 1);
  83. return executable_path + path;
  84. }
  85. static ShellRenderInterfaceExtensions *shell_renderer = nullptr;
  86. bool Shell::OpenWindow(const char* name, ShellRenderInterfaceExtensions *_shell_renderer, unsigned int width, unsigned int height, bool allow_resize)
  87. {
  88. WNDCLASS window_class;
  89. // Fill out the window class struct.
  90. window_class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  91. window_class.lpfnWndProc = WindowProcedure;
  92. window_class.cbClsExtra = 0;
  93. window_class.cbWndExtra = 0;
  94. window_class.hInstance = instance_handle;
  95. window_class.hIcon = LoadIcon(nullptr, IDI_WINLOGO);
  96. window_class.hCursor = cursor_default;
  97. window_class.hbrBackground = nullptr;
  98. window_class.lpszMenuName = nullptr;
  99. window_class.lpszClassName = name;
  100. if (!RegisterClass(&window_class))
  101. {
  102. DisplayError("Could not register window class.");
  103. CloseWindow();
  104. return false;
  105. }
  106. window_handle = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
  107. name, // Window class name.
  108. name,
  109. WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,
  110. 0, 0, // Window position.
  111. width, height,// Window size.
  112. nullptr,
  113. nullptr,
  114. instance_handle,
  115. nullptr);
  116. if (!window_handle)
  117. {
  118. DisplayError("Could not create window.");
  119. CloseWindow();
  120. return false;
  121. }
  122. instance_name = name;
  123. DWORD style = (allow_resize ? WS_OVERLAPPEDWINDOW : (WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX));
  124. DWORD extended_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  125. // Adjust the window size to take into account the edges
  126. RECT window_rect;
  127. window_rect.top = 0;
  128. window_rect.left = 0;
  129. window_rect.right = width;
  130. window_rect.bottom = height;
  131. AdjustWindowRectEx(&window_rect, style, FALSE, extended_style);
  132. SetWindowLong(window_handle, GWL_EXSTYLE, extended_style);
  133. SetWindowLong(window_handle, GWL_STYLE, style);
  134. if (_shell_renderer != nullptr)
  135. {
  136. shell_renderer = _shell_renderer;
  137. if(!shell_renderer->AttachToNative(window_handle))
  138. {
  139. CloseWindow();
  140. return false;
  141. }
  142. }
  143. // Resize the window.
  144. SetWindowPos(window_handle, HWND_TOP, 0, 0, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, SWP_NOACTIVATE);
  145. // Display the new window
  146. ShowWindow(window_handle, SW_SHOW);
  147. SetForegroundWindow(window_handle);
  148. SetFocus(window_handle);
  149. return true;
  150. }
  151. void Shell::CloseWindow()
  152. {
  153. if(shell_renderer) {
  154. shell_renderer->DetachFromNative();
  155. }
  156. DestroyWindow(window_handle);
  157. UnregisterClass(instance_name, instance_handle);
  158. }
  159. // Returns a platform-dependent handle to the window.
  160. void* Shell::GetWindowHandle()
  161. {
  162. return window_handle;
  163. }
  164. void Shell::EventLoop(ShellIdleFunction idle_function)
  165. {
  166. MSG message;
  167. running = true;
  168. // Loop on PeekMessage() / GetMessage() until exit has been requested.
  169. while (running)
  170. {
  171. if (PeekMessage(&message, nullptr, 0, 0, PM_NOREMOVE))
  172. {
  173. GetMessage(&message, nullptr, 0, 0);
  174. TranslateMessage(&message);
  175. DispatchMessage(&message);
  176. }
  177. idle_function();
  178. }
  179. }
  180. void Shell::RequestExit()
  181. {
  182. running = false;
  183. }
  184. void Shell::DisplayError(const char* fmt, ...)
  185. {
  186. const int buffer_size = 1024;
  187. char buffer[buffer_size];
  188. va_list argument_list;
  189. // Print the message to the buffer.
  190. va_start(argument_list, fmt);
  191. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  192. if ( len < 0 || len > buffer_size - 2 )
  193. {
  194. len = buffer_size - 2;
  195. }
  196. buffer[len] = '\n';
  197. buffer[len + 1] = '\0';
  198. va_end(argument_list);
  199. MessageBox(window_handle, buffer, "Shell Error", MB_OK);
  200. }
  201. void Shell::Log(const char* fmt, ...)
  202. {
  203. const int buffer_size = 1024;
  204. char buffer[buffer_size];
  205. va_list argument_list;
  206. // Print the message to the buffer.
  207. va_start(argument_list, fmt);
  208. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  209. if ( len < 0 || len > buffer_size - 2 )
  210. {
  211. len = buffer_size - 2;
  212. }
  213. buffer[len] = '\n';
  214. buffer[len + 1] = '\0';
  215. va_end(argument_list);
  216. OutputDebugString(buffer);
  217. }
  218. double Shell::GetElapsedTime()
  219. {
  220. LARGE_INTEGER counter;
  221. QueryPerformanceCounter(&counter);
  222. return double(counter.QuadPart - time_startup.QuadPart) * time_frequency;
  223. }
  224. void Shell::SetMouseCursor(const Rml::Core::String& cursor_name)
  225. {
  226. if (window_handle)
  227. {
  228. HCURSOR cursor_handle = nullptr;
  229. if (cursor_name.empty())
  230. cursor_handle = cursor_default;
  231. else if(cursor_name == "move")
  232. cursor_handle = cursor_move;
  233. else if (cursor_name == "cross")
  234. cursor_handle = cursor_cross;
  235. else if (cursor_name == "unavailable")
  236. cursor_handle = cursor_unavailable;
  237. if (cursor_handle)
  238. {
  239. SetCursor(cursor_handle);
  240. SetClassLongPtrA(window_handle, GCLP_HCURSOR, (LONG_PTR)cursor_handle);
  241. }
  242. }
  243. }
  244. void Shell::SetClipboardText(const Rml::Core::String& text_utf8)
  245. {
  246. if (window_handle)
  247. {
  248. if (!OpenClipboard(window_handle))
  249. return;
  250. EmptyClipboard();
  251. const Rml::Core::WString text = Rml::Core::StringUtilities::ToUTF16(text_utf8);
  252. size_t size = sizeof(wchar_t) * (text.size() + 1);
  253. HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, size);
  254. memcpy(clipboard_data, text.data(), size);
  255. if (SetClipboardData(CF_UNICODETEXT, clipboard_data) == nullptr)
  256. {
  257. CloseClipboard();
  258. GlobalFree(clipboard_data);
  259. }
  260. else
  261. CloseClipboard();
  262. }
  263. }
  264. void Shell::GetClipboardText(Rml::Core::String& text)
  265. {
  266. if (window_handle)
  267. {
  268. if (!OpenClipboard(window_handle))
  269. return;
  270. HANDLE clipboard_data = GetClipboardData(CF_UNICODETEXT);
  271. if (clipboard_data == nullptr)
  272. {
  273. CloseClipboard();
  274. return;
  275. }
  276. const wchar_t* clipboard_text = (const wchar_t*)GlobalLock(clipboard_data);
  277. if (clipboard_text)
  278. text = Rml::Core::StringUtilities::ToUTF8(clipboard_text);
  279. GlobalUnlock(clipboard_data);
  280. CloseClipboard();
  281. }
  282. }
  283. static LRESULT CALLBACK WindowProcedure(HWND window_handle, UINT message, WPARAM w_param, LPARAM l_param)
  284. {
  285. // See what kind of message we've got.
  286. switch (message)
  287. {
  288. case WM_ACTIVATE:
  289. {
  290. if (LOWORD(w_param) != WA_INACTIVE)
  291. {
  292. activated = true;
  293. }
  294. else
  295. {
  296. activated = false;
  297. }
  298. }
  299. break;
  300. // When the window closes, request exit
  301. case WM_CLOSE:
  302. {
  303. running = false;
  304. return 0;
  305. }
  306. break;
  307. case WM_SIZE:
  308. {
  309. int width = LOWORD(l_param);;
  310. int height = HIWORD(l_param);;
  311. shell_renderer->SetViewport(width, height);
  312. }
  313. break;
  314. default:
  315. {
  316. InputWin32::ProcessWindowsEvent(message, w_param, l_param);
  317. }
  318. break;
  319. }
  320. // All unhandled messages go to DefWindowProc.
  321. return DefWindowProc(window_handle, message, w_param, l_param);
  322. }