RmlUi_Platform_Win32.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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-2023 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 "RmlUi_Platform_Win32.h"
  29. #include "RmlUi_Include_Windows.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Core.h>
  32. #include <RmlUi/Core/Input.h>
  33. #include <RmlUi/Core/StringUtilities.h>
  34. #include <RmlUi/Core/SystemInterface.h>
  35. #include <RmlUi/Core/TextInputContext.h>
  36. #include <RmlUi/Core/TextInputHandler.h>
  37. #include <string.h>
  38. // Used to interact with the input method editor (IME). Users of MinGW should manually link to this.
  39. #ifdef _MSC_VER
  40. #pragma comment(lib, "imm32")
  41. #endif
  42. SystemInterface_Win32::SystemInterface_Win32()
  43. {
  44. LARGE_INTEGER time_ticks_per_second;
  45. QueryPerformanceFrequency(&time_ticks_per_second);
  46. QueryPerformanceCounter(&time_startup);
  47. time_frequency = 1.0 / (double)time_ticks_per_second.QuadPart;
  48. // Load cursors
  49. cursor_default = LoadCursor(nullptr, IDC_ARROW);
  50. cursor_move = LoadCursor(nullptr, IDC_SIZEALL);
  51. cursor_pointer = LoadCursor(nullptr, IDC_HAND);
  52. cursor_resize = LoadCursor(nullptr, IDC_SIZENWSE);
  53. cursor_cross = LoadCursor(nullptr, IDC_CROSS);
  54. cursor_text = LoadCursor(nullptr, IDC_IBEAM);
  55. cursor_unavailable = LoadCursor(nullptr, IDC_NO);
  56. }
  57. SystemInterface_Win32::~SystemInterface_Win32() = default;
  58. void SystemInterface_Win32::SetWindow(HWND in_window_handle)
  59. {
  60. window_handle = in_window_handle;
  61. }
  62. double SystemInterface_Win32::GetElapsedTime()
  63. {
  64. LARGE_INTEGER counter;
  65. QueryPerformanceCounter(&counter);
  66. return double(counter.QuadPart - time_startup.QuadPart) * time_frequency;
  67. }
  68. void SystemInterface_Win32::SetMouseCursor(const Rml::String& cursor_name)
  69. {
  70. if (window_handle)
  71. {
  72. HCURSOR cursor_handle = nullptr;
  73. if (cursor_name.empty() || cursor_name == "arrow")
  74. cursor_handle = cursor_default;
  75. else if (cursor_name == "move")
  76. cursor_handle = cursor_move;
  77. else if (cursor_name == "pointer")
  78. cursor_handle = cursor_pointer;
  79. else if (cursor_name == "resize")
  80. cursor_handle = cursor_resize;
  81. else if (cursor_name == "cross")
  82. cursor_handle = cursor_cross;
  83. else if (cursor_name == "text")
  84. cursor_handle = cursor_text;
  85. else if (cursor_name == "unavailable")
  86. cursor_handle = cursor_unavailable;
  87. else if (Rml::StringUtilities::StartsWith(cursor_name, "rmlui-scroll"))
  88. cursor_handle = cursor_move;
  89. if (cursor_handle)
  90. {
  91. SetCursor(cursor_handle);
  92. SetClassLongPtrA(window_handle, GCLP_HCURSOR, (LONG_PTR)cursor_handle);
  93. }
  94. }
  95. }
  96. void SystemInterface_Win32::SetClipboardText(const Rml::String& text_utf8)
  97. {
  98. if (window_handle)
  99. {
  100. if (!OpenClipboard(window_handle))
  101. return;
  102. EmptyClipboard();
  103. const std::wstring text = RmlWin32::ConvertToUTF16(text_utf8);
  104. const size_t size = sizeof(wchar_t) * (text.size() + 1);
  105. HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, size);
  106. memcpy(clipboard_data, text.data(), size);
  107. if (SetClipboardData(CF_UNICODETEXT, clipboard_data) == nullptr)
  108. {
  109. CloseClipboard();
  110. GlobalFree(clipboard_data);
  111. }
  112. else
  113. CloseClipboard();
  114. }
  115. }
  116. void SystemInterface_Win32::GetClipboardText(Rml::String& text)
  117. {
  118. if (window_handle)
  119. {
  120. if (!OpenClipboard(window_handle))
  121. return;
  122. HANDLE clipboard_data = GetClipboardData(CF_UNICODETEXT);
  123. if (clipboard_data == nullptr)
  124. {
  125. CloseClipboard();
  126. return;
  127. }
  128. const wchar_t* clipboard_text = (const wchar_t*)GlobalLock(clipboard_data);
  129. if (clipboard_text)
  130. text = RmlWin32::ConvertToUTF8(clipboard_text);
  131. GlobalUnlock(clipboard_data);
  132. CloseClipboard();
  133. }
  134. }
  135. void SystemInterface_Win32::ActivateKeyboard(Rml::Vector2f caret_position, float line_height)
  136. {
  137. HIMC himc = ImmGetContext(window_handle);
  138. if (himc == NULL)
  139. return;
  140. constexpr LONG BottomMargin = 2;
  141. // Adjust the position of the input method editor (IME) to the caret.
  142. const LONG x = static_cast<LONG>(caret_position.x);
  143. const LONG y = static_cast<LONG>(caret_position.y);
  144. const LONG w = 1;
  145. const LONG h = static_cast<LONG>(line_height) + BottomMargin;
  146. COMPOSITIONFORM comp = {};
  147. comp.dwStyle = CFS_FORCE_POSITION;
  148. comp.ptCurrentPos = {x, y};
  149. ImmSetCompositionWindow(himc, &comp);
  150. CANDIDATEFORM cand = {};
  151. cand.dwStyle = CFS_EXCLUDE;
  152. cand.ptCurrentPos = {x, y};
  153. cand.rcArea = {x, y, x + w, y + h};
  154. ImmSetCandidateWindow(himc, &cand);
  155. ImmReleaseContext(window_handle, himc);
  156. }
  157. Rml::String RmlWin32::ConvertToUTF8(const std::wstring& wstr)
  158. {
  159. const int count = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.length(), NULL, 0, NULL, NULL);
  160. Rml::String str(count, 0);
  161. WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL);
  162. return str;
  163. }
  164. std::wstring RmlWin32::ConvertToUTF16(const Rml::String& str)
  165. {
  166. const int count = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.length(), NULL, 0);
  167. std::wstring wstr(count, 0);
  168. MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.length(), &wstr[0], count);
  169. return wstr;
  170. }
  171. static int IMEGetCursorPosition(HIMC context)
  172. {
  173. return ImmGetCompositionString(context, GCS_CURSORPOS, nullptr, 0);
  174. }
  175. static std::wstring IMEGetCompositionString(HIMC context, bool finalize)
  176. {
  177. DWORD type = finalize ? GCS_RESULTSTR : GCS_COMPSTR;
  178. int len_bytes = ImmGetCompositionString(context, type, nullptr, 0);
  179. if (len_bytes <= 0)
  180. return {};
  181. int len_chars = len_bytes / sizeof(TCHAR);
  182. Rml::UniquePtr<TCHAR[]> buffer(new TCHAR[len_chars + 1]);
  183. ImmGetCompositionString(context, type, buffer.get(), len_bytes);
  184. #ifdef UNICODE
  185. return std::wstring(buffer.get(), len_chars);
  186. #else
  187. return RmlWin32::ConvertToUTF16(Rml::String(buffer.get(), len_chars));
  188. #endif
  189. }
  190. static void IMECompleteComposition(HWND window_handle)
  191. {
  192. if (HIMC context = ImmGetContext(window_handle))
  193. {
  194. ImmNotifyIME(context, NI_COMPOSITIONSTR, CPS_COMPLETE, NULL);
  195. ImmReleaseContext(window_handle, context);
  196. }
  197. }
  198. bool RmlWin32::WindowProcedure(Rml::Context* context, TextInputMethodEditor_Win32& text_input_method_editor, HWND window_handle, UINT message,
  199. WPARAM w_param, LPARAM l_param)
  200. {
  201. if (!context)
  202. return true;
  203. static bool tracking_mouse_leave = false;
  204. // If the user tries to interact with the window by using the mouse in any way, end the
  205. // composition by committing the current string. This behavior is identical to other
  206. // browsers and is expected, yet, Windows does not send any IME messages in such a case.
  207. if (text_input_method_editor.IsComposing() && message >= WM_LBUTTONDOWN && message <= WM_MBUTTONDBLCLK)
  208. IMECompleteComposition(window_handle);
  209. bool result = true;
  210. switch (message)
  211. {
  212. case WM_LBUTTONDOWN:
  213. result = context->ProcessMouseButtonDown(0, RmlWin32::GetKeyModifierState());
  214. SetCapture(window_handle);
  215. break;
  216. case WM_LBUTTONUP:
  217. ReleaseCapture();
  218. result = context->ProcessMouseButtonUp(0, RmlWin32::GetKeyModifierState());
  219. break;
  220. case WM_RBUTTONDOWN: result = context->ProcessMouseButtonDown(1, RmlWin32::GetKeyModifierState()); break;
  221. case WM_RBUTTONUP: result = context->ProcessMouseButtonUp(1, RmlWin32::GetKeyModifierState()); break;
  222. case WM_MBUTTONDOWN: result = context->ProcessMouseButtonDown(2, RmlWin32::GetKeyModifierState()); break;
  223. case WM_MBUTTONUP: result = context->ProcessMouseButtonUp(2, RmlWin32::GetKeyModifierState()); break;
  224. case WM_MOUSEMOVE:
  225. result = context->ProcessMouseMove(static_cast<int>((short)LOWORD(l_param)), static_cast<int>((short)HIWORD(l_param)),
  226. RmlWin32::GetKeyModifierState());
  227. if (!tracking_mouse_leave)
  228. {
  229. TRACKMOUSEEVENT tme = {};
  230. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  231. tme.dwFlags = TME_LEAVE;
  232. tme.hwndTrack = window_handle;
  233. tracking_mouse_leave = TrackMouseEvent(&tme);
  234. }
  235. break;
  236. case WM_MOUSEWHEEL:
  237. result = context->ProcessMouseWheel(static_cast<float>((short)HIWORD(w_param)) / static_cast<float>(-WHEEL_DELTA),
  238. RmlWin32::GetKeyModifierState());
  239. break;
  240. case WM_MOUSELEAVE:
  241. result = context->ProcessMouseLeave();
  242. tracking_mouse_leave = false;
  243. break;
  244. case WM_KEYDOWN: result = context->ProcessKeyDown(RmlWin32::ConvertKey((int)w_param), RmlWin32::GetKeyModifierState()); break;
  245. case WM_KEYUP: result = context->ProcessKeyUp(RmlWin32::ConvertKey((int)w_param), RmlWin32::GetKeyModifierState()); break;
  246. case WM_CHAR:
  247. {
  248. static wchar_t first_u16_code_unit = 0;
  249. const wchar_t c = (wchar_t)w_param;
  250. Rml::Character character = (Rml::Character)c;
  251. // Windows sends two-wide characters as two messages.
  252. if (c >= 0xD800 && c < 0xDC00)
  253. {
  254. // First 16-bit code unit of a two-wide character.
  255. first_u16_code_unit = c;
  256. }
  257. else
  258. {
  259. if (c >= 0xDC00 && c < 0xE000 && first_u16_code_unit != 0)
  260. {
  261. // Second 16-bit code unit of a two-wide character.
  262. Rml::String utf8 = ConvertToUTF8(std::wstring{first_u16_code_unit, c});
  263. character = Rml::StringUtilities::ToCharacter(utf8.data(), utf8.data() + utf8.size());
  264. }
  265. else if (c == '\r')
  266. {
  267. // Windows sends new-lines as carriage returns, convert to endlines.
  268. character = (Rml::Character)'\n';
  269. }
  270. first_u16_code_unit = 0;
  271. // Only send through printable characters.
  272. if (((char32_t)character >= 32 || character == (Rml::Character)'\n') && character != (Rml::Character)127)
  273. result = context->ProcessTextInput(character);
  274. }
  275. }
  276. break;
  277. case WM_IME_STARTCOMPOSITION:
  278. text_input_method_editor.StartComposition();
  279. // Prevent the native composition window from appearing by capturing the message.
  280. result = false;
  281. break;
  282. case WM_IME_ENDCOMPOSITION:
  283. if (text_input_method_editor.IsComposing())
  284. text_input_method_editor.ConfirmComposition(Rml::StringView());
  285. break;
  286. case WM_IME_COMPOSITION:
  287. {
  288. HIMC imm_context = ImmGetContext(window_handle);
  289. // Not every IME starts a composition.
  290. if (!text_input_method_editor.IsComposing())
  291. text_input_method_editor.StartComposition();
  292. if (!!(l_param & GCS_CURSORPOS))
  293. {
  294. // The cursor position is the wchar_t offset in the composition string. Because we
  295. // work with UTF-8 and not UTF-16, we will have to convert the character offset.
  296. int cursor_pos = IMEGetCursorPosition(imm_context);
  297. std::wstring composition = IMEGetCompositionString(imm_context, false);
  298. Rml::String converted = RmlWin32::ConvertToUTF8(composition.substr(0, cursor_pos));
  299. cursor_pos = (int)Rml::StringUtilities::LengthUTF8(converted);
  300. text_input_method_editor.SetCursorPosition(cursor_pos, true);
  301. }
  302. if (!!(l_param & CS_NOMOVECARET))
  303. {
  304. // Suppress the cursor position update. CS_NOMOVECARET is always a part of a more
  305. // complex message which means that the cursor is updated from a different event.
  306. text_input_method_editor.SetCursorPosition(-1, false);
  307. }
  308. if (!!(l_param & GCS_RESULTSTR))
  309. {
  310. std::wstring composition = IMEGetCompositionString(imm_context, true);
  311. text_input_method_editor.ConfirmComposition(RmlWin32::ConvertToUTF8(composition));
  312. }
  313. if (!!(l_param & GCS_COMPSTR))
  314. {
  315. std::wstring composition = IMEGetCompositionString(imm_context, false);
  316. text_input_method_editor.SetComposition(RmlWin32::ConvertToUTF8(composition));
  317. }
  318. // The composition has been canceled.
  319. if (!l_param)
  320. text_input_method_editor.CancelComposition();
  321. ImmReleaseContext(window_handle, imm_context);
  322. }
  323. break;
  324. case WM_IME_CHAR:
  325. case WM_IME_REQUEST:
  326. // Ignore WM_IME_CHAR and WM_IME_REQUEST to block the system from appending the composition string.
  327. result = false;
  328. break;
  329. default: break;
  330. }
  331. return result;
  332. }
  333. int RmlWin32::GetKeyModifierState()
  334. {
  335. int key_modifier_state = 0;
  336. if (GetKeyState(VK_CAPITAL) & 1)
  337. key_modifier_state |= Rml::Input::KM_CAPSLOCK;
  338. if (GetKeyState(VK_NUMLOCK) & 1)
  339. key_modifier_state |= Rml::Input::KM_NUMLOCK;
  340. if (HIWORD(GetKeyState(VK_SHIFT)) & 1)
  341. key_modifier_state |= Rml::Input::KM_SHIFT;
  342. if (HIWORD(GetKeyState(VK_CONTROL)) & 1)
  343. key_modifier_state |= Rml::Input::KM_CTRL;
  344. if (HIWORD(GetKeyState(VK_MENU)) & 1)
  345. key_modifier_state |= Rml::Input::KM_ALT;
  346. return key_modifier_state;
  347. }
  348. // These are defined in winuser.h of MinGW 64 but are missing from MinGW 32
  349. // Visual Studio has them by default
  350. #if defined(__MINGW32__) && !defined(__MINGW64__)
  351. #define VK_OEM_NEC_EQUAL 0x92
  352. #define VK_OEM_FJ_JISHO 0x92
  353. #define VK_OEM_FJ_MASSHOU 0x93
  354. #define VK_OEM_FJ_TOUROKU 0x94
  355. #define VK_OEM_FJ_LOYA 0x95
  356. #define VK_OEM_FJ_ROYA 0x96
  357. #define VK_OEM_AX 0xE1
  358. #define VK_ICO_HELP 0xE3
  359. #define VK_ICO_00 0xE4
  360. #define VK_ICO_CLEAR 0xE6
  361. #endif // !defined(__MINGW32__) || defined(__MINGW64__)
  362. Rml::Input::KeyIdentifier RmlWin32::ConvertKey(int win32_key_code)
  363. {
  364. // clang-format off
  365. switch (win32_key_code)
  366. {
  367. case 'A': return Rml::Input::KI_A;
  368. case 'B': return Rml::Input::KI_B;
  369. case 'C': return Rml::Input::KI_C;
  370. case 'D': return Rml::Input::KI_D;
  371. case 'E': return Rml::Input::KI_E;
  372. case 'F': return Rml::Input::KI_F;
  373. case 'G': return Rml::Input::KI_G;
  374. case 'H': return Rml::Input::KI_H;
  375. case 'I': return Rml::Input::KI_I;
  376. case 'J': return Rml::Input::KI_J;
  377. case 'K': return Rml::Input::KI_K;
  378. case 'L': return Rml::Input::KI_L;
  379. case 'M': return Rml::Input::KI_M;
  380. case 'N': return Rml::Input::KI_N;
  381. case 'O': return Rml::Input::KI_O;
  382. case 'P': return Rml::Input::KI_P;
  383. case 'Q': return Rml::Input::KI_Q;
  384. case 'R': return Rml::Input::KI_R;
  385. case 'S': return Rml::Input::KI_S;
  386. case 'T': return Rml::Input::KI_T;
  387. case 'U': return Rml::Input::KI_U;
  388. case 'V': return Rml::Input::KI_V;
  389. case 'W': return Rml::Input::KI_W;
  390. case 'X': return Rml::Input::KI_X;
  391. case 'Y': return Rml::Input::KI_Y;
  392. case 'Z': return Rml::Input::KI_Z;
  393. case '0': return Rml::Input::KI_0;
  394. case '1': return Rml::Input::KI_1;
  395. case '2': return Rml::Input::KI_2;
  396. case '3': return Rml::Input::KI_3;
  397. case '4': return Rml::Input::KI_4;
  398. case '5': return Rml::Input::KI_5;
  399. case '6': return Rml::Input::KI_6;
  400. case '7': return Rml::Input::KI_7;
  401. case '8': return Rml::Input::KI_8;
  402. case '9': return Rml::Input::KI_9;
  403. case VK_BACK: return Rml::Input::KI_BACK;
  404. case VK_TAB: return Rml::Input::KI_TAB;
  405. case VK_CLEAR: return Rml::Input::KI_CLEAR;
  406. case VK_RETURN: return Rml::Input::KI_RETURN;
  407. case VK_PAUSE: return Rml::Input::KI_PAUSE;
  408. case VK_CAPITAL: return Rml::Input::KI_CAPITAL;
  409. case VK_KANA: return Rml::Input::KI_KANA;
  410. //case VK_HANGUL: return Rml::Input::KI_HANGUL; /* overlaps with VK_KANA */
  411. case VK_JUNJA: return Rml::Input::KI_JUNJA;
  412. case VK_FINAL: return Rml::Input::KI_FINAL;
  413. case VK_HANJA: return Rml::Input::KI_HANJA;
  414. //case VK_KANJI: return Rml::Input::KI_KANJI; /* overlaps with VK_HANJA */
  415. case VK_ESCAPE: return Rml::Input::KI_ESCAPE;
  416. case VK_CONVERT: return Rml::Input::KI_CONVERT;
  417. case VK_NONCONVERT: return Rml::Input::KI_NONCONVERT;
  418. case VK_ACCEPT: return Rml::Input::KI_ACCEPT;
  419. case VK_MODECHANGE: return Rml::Input::KI_MODECHANGE;
  420. case VK_SPACE: return Rml::Input::KI_SPACE;
  421. case VK_PRIOR: return Rml::Input::KI_PRIOR;
  422. case VK_NEXT: return Rml::Input::KI_NEXT;
  423. case VK_END: return Rml::Input::KI_END;
  424. case VK_HOME: return Rml::Input::KI_HOME;
  425. case VK_LEFT: return Rml::Input::KI_LEFT;
  426. case VK_UP: return Rml::Input::KI_UP;
  427. case VK_RIGHT: return Rml::Input::KI_RIGHT;
  428. case VK_DOWN: return Rml::Input::KI_DOWN;
  429. case VK_SELECT: return Rml::Input::KI_SELECT;
  430. case VK_PRINT: return Rml::Input::KI_PRINT;
  431. case VK_EXECUTE: return Rml::Input::KI_EXECUTE;
  432. case VK_SNAPSHOT: return Rml::Input::KI_SNAPSHOT;
  433. case VK_INSERT: return Rml::Input::KI_INSERT;
  434. case VK_DELETE: return Rml::Input::KI_DELETE;
  435. case VK_HELP: return Rml::Input::KI_HELP;
  436. case VK_LWIN: return Rml::Input::KI_LWIN;
  437. case VK_RWIN: return Rml::Input::KI_RWIN;
  438. case VK_APPS: return Rml::Input::KI_APPS;
  439. case VK_SLEEP: return Rml::Input::KI_SLEEP;
  440. case VK_NUMPAD0: return Rml::Input::KI_NUMPAD0;
  441. case VK_NUMPAD1: return Rml::Input::KI_NUMPAD1;
  442. case VK_NUMPAD2: return Rml::Input::KI_NUMPAD2;
  443. case VK_NUMPAD3: return Rml::Input::KI_NUMPAD3;
  444. case VK_NUMPAD4: return Rml::Input::KI_NUMPAD4;
  445. case VK_NUMPAD5: return Rml::Input::KI_NUMPAD5;
  446. case VK_NUMPAD6: return Rml::Input::KI_NUMPAD6;
  447. case VK_NUMPAD7: return Rml::Input::KI_NUMPAD7;
  448. case VK_NUMPAD8: return Rml::Input::KI_NUMPAD8;
  449. case VK_NUMPAD9: return Rml::Input::KI_NUMPAD9;
  450. case VK_MULTIPLY: return Rml::Input::KI_MULTIPLY;
  451. case VK_ADD: return Rml::Input::KI_ADD;
  452. case VK_SEPARATOR: return Rml::Input::KI_SEPARATOR;
  453. case VK_SUBTRACT: return Rml::Input::KI_SUBTRACT;
  454. case VK_DECIMAL: return Rml::Input::KI_DECIMAL;
  455. case VK_DIVIDE: return Rml::Input::KI_DIVIDE;
  456. case VK_F1: return Rml::Input::KI_F1;
  457. case VK_F2: return Rml::Input::KI_F2;
  458. case VK_F3: return Rml::Input::KI_F3;
  459. case VK_F4: return Rml::Input::KI_F4;
  460. case VK_F5: return Rml::Input::KI_F5;
  461. case VK_F6: return Rml::Input::KI_F6;
  462. case VK_F7: return Rml::Input::KI_F7;
  463. case VK_F8: return Rml::Input::KI_F8;
  464. case VK_F9: return Rml::Input::KI_F9;
  465. case VK_F10: return Rml::Input::KI_F10;
  466. case VK_F11: return Rml::Input::KI_F11;
  467. case VK_F12: return Rml::Input::KI_F12;
  468. case VK_F13: return Rml::Input::KI_F13;
  469. case VK_F14: return Rml::Input::KI_F14;
  470. case VK_F15: return Rml::Input::KI_F15;
  471. case VK_F16: return Rml::Input::KI_F16;
  472. case VK_F17: return Rml::Input::KI_F17;
  473. case VK_F18: return Rml::Input::KI_F18;
  474. case VK_F19: return Rml::Input::KI_F19;
  475. case VK_F20: return Rml::Input::KI_F20;
  476. case VK_F21: return Rml::Input::KI_F21;
  477. case VK_F22: return Rml::Input::KI_F22;
  478. case VK_F23: return Rml::Input::KI_F23;
  479. case VK_F24: return Rml::Input::KI_F24;
  480. case VK_NUMLOCK: return Rml::Input::KI_NUMLOCK;
  481. case VK_SCROLL: return Rml::Input::KI_SCROLL;
  482. case VK_OEM_NEC_EQUAL: return Rml::Input::KI_OEM_NEC_EQUAL;
  483. //case VK_OEM_FJ_JISHO: return Rml::Input::KI_OEM_FJ_JISHO; /* overlaps with VK_OEM_NEC_EQUAL */
  484. case VK_OEM_FJ_MASSHOU: return Rml::Input::KI_OEM_FJ_MASSHOU;
  485. case VK_OEM_FJ_TOUROKU: return Rml::Input::KI_OEM_FJ_TOUROKU;
  486. case VK_OEM_FJ_LOYA: return Rml::Input::KI_OEM_FJ_LOYA;
  487. case VK_OEM_FJ_ROYA: return Rml::Input::KI_OEM_FJ_ROYA;
  488. case VK_SHIFT: return Rml::Input::KI_LSHIFT;
  489. case VK_CONTROL: return Rml::Input::KI_LCONTROL;
  490. case VK_MENU: return Rml::Input::KI_LMENU;
  491. case VK_BROWSER_BACK: return Rml::Input::KI_BROWSER_BACK;
  492. case VK_BROWSER_FORWARD: return Rml::Input::KI_BROWSER_FORWARD;
  493. case VK_BROWSER_REFRESH: return Rml::Input::KI_BROWSER_REFRESH;
  494. case VK_BROWSER_STOP: return Rml::Input::KI_BROWSER_STOP;
  495. case VK_BROWSER_SEARCH: return Rml::Input::KI_BROWSER_SEARCH;
  496. case VK_BROWSER_FAVORITES: return Rml::Input::KI_BROWSER_FAVORITES;
  497. case VK_BROWSER_HOME: return Rml::Input::KI_BROWSER_HOME;
  498. case VK_VOLUME_MUTE: return Rml::Input::KI_VOLUME_MUTE;
  499. case VK_VOLUME_DOWN: return Rml::Input::KI_VOLUME_DOWN;
  500. case VK_VOLUME_UP: return Rml::Input::KI_VOLUME_UP;
  501. case VK_MEDIA_NEXT_TRACK: return Rml::Input::KI_MEDIA_NEXT_TRACK;
  502. case VK_MEDIA_PREV_TRACK: return Rml::Input::KI_MEDIA_PREV_TRACK;
  503. case VK_MEDIA_STOP: return Rml::Input::KI_MEDIA_STOP;
  504. case VK_MEDIA_PLAY_PAUSE: return Rml::Input::KI_MEDIA_PLAY_PAUSE;
  505. case VK_LAUNCH_MAIL: return Rml::Input::KI_LAUNCH_MAIL;
  506. case VK_LAUNCH_MEDIA_SELECT: return Rml::Input::KI_LAUNCH_MEDIA_SELECT;
  507. case VK_LAUNCH_APP1: return Rml::Input::KI_LAUNCH_APP1;
  508. case VK_LAUNCH_APP2: return Rml::Input::KI_LAUNCH_APP2;
  509. case VK_OEM_1: return Rml::Input::KI_OEM_1;
  510. case VK_OEM_PLUS: return Rml::Input::KI_OEM_PLUS;
  511. case VK_OEM_COMMA: return Rml::Input::KI_OEM_COMMA;
  512. case VK_OEM_MINUS: return Rml::Input::KI_OEM_MINUS;
  513. case VK_OEM_PERIOD: return Rml::Input::KI_OEM_PERIOD;
  514. case VK_OEM_2: return Rml::Input::KI_OEM_2;
  515. case VK_OEM_3: return Rml::Input::KI_OEM_3;
  516. case VK_OEM_4: return Rml::Input::KI_OEM_4;
  517. case VK_OEM_5: return Rml::Input::KI_OEM_5;
  518. case VK_OEM_6: return Rml::Input::KI_OEM_6;
  519. case VK_OEM_7: return Rml::Input::KI_OEM_7;
  520. case VK_OEM_8: return Rml::Input::KI_OEM_8;
  521. case VK_OEM_AX: return Rml::Input::KI_OEM_AX;
  522. case VK_OEM_102: return Rml::Input::KI_OEM_102;
  523. case VK_ICO_HELP: return Rml::Input::KI_ICO_HELP;
  524. case VK_ICO_00: return Rml::Input::KI_ICO_00;
  525. case VK_PROCESSKEY: return Rml::Input::KI_PROCESSKEY;
  526. case VK_ICO_CLEAR: return Rml::Input::KI_ICO_CLEAR;
  527. case VK_ATTN: return Rml::Input::KI_ATTN;
  528. case VK_CRSEL: return Rml::Input::KI_CRSEL;
  529. case VK_EXSEL: return Rml::Input::KI_EXSEL;
  530. case VK_EREOF: return Rml::Input::KI_EREOF;
  531. case VK_PLAY: return Rml::Input::KI_PLAY;
  532. case VK_ZOOM: return Rml::Input::KI_ZOOM;
  533. case VK_PA1: return Rml::Input::KI_PA1;
  534. case VK_OEM_CLEAR: return Rml::Input::KI_OEM_CLEAR;
  535. }
  536. // clang-format on
  537. return Rml::Input::KI_UNKNOWN;
  538. }
  539. TextInputMethodEditor_Win32::TextInputMethodEditor_Win32() :
  540. input_context(nullptr), composing(false), cursor_pos(-1), composition_range_start(0), composition_range_end(0)
  541. {}
  542. void TextInputMethodEditor_Win32::OnActivate(Rml::TextInputContext* _input_context)
  543. {
  544. input_context = _input_context;
  545. }
  546. void TextInputMethodEditor_Win32::OnDeactivate(Rml::TextInputContext* _input_context)
  547. {
  548. if (input_context == _input_context)
  549. input_context = nullptr;
  550. }
  551. void TextInputMethodEditor_Win32::OnDestroy(Rml::TextInputContext* _input_context)
  552. {
  553. if (input_context == _input_context)
  554. input_context = nullptr;
  555. }
  556. bool TextInputMethodEditor_Win32::IsComposing() const
  557. {
  558. return composing;
  559. }
  560. void TextInputMethodEditor_Win32::StartComposition()
  561. {
  562. RMLUI_ASSERT(!composing);
  563. composing = true;
  564. }
  565. void TextInputMethodEditor_Win32::EndComposition()
  566. {
  567. if (input_context != nullptr)
  568. input_context->SetCompositionRange(0, 0);
  569. RMLUI_ASSERT(composing);
  570. composing = false;
  571. composition_range_start = 0;
  572. composition_range_end = 0;
  573. }
  574. void TextInputMethodEditor_Win32::CancelComposition()
  575. {
  576. RMLUI_ASSERT(IsComposing());
  577. if (input_context != nullptr)
  578. {
  579. // Purge the current composition string.
  580. input_context->SetText(Rml::StringView(), composition_range_start, composition_range_end);
  581. // Move the cursor back to where the composition began.
  582. input_context->SetCursorPosition(composition_range_start);
  583. }
  584. EndComposition();
  585. }
  586. void TextInputMethodEditor_Win32::SetComposition(Rml::StringView composition)
  587. {
  588. RMLUI_ASSERT(IsComposing());
  589. SetCompositionString(composition);
  590. UpdateCursorPosition();
  591. // Update the composition range only if the cursor can be moved around. Editors working with a single
  592. // character (e.g., Hangul IME) should have no visual feedback; they use a selection range instead.
  593. if (cursor_pos != -1 && input_context != nullptr)
  594. input_context->SetCompositionRange(composition_range_start, composition_range_end);
  595. }
  596. void TextInputMethodEditor_Win32::ConfirmComposition(Rml::StringView composition)
  597. {
  598. RMLUI_ASSERT(IsComposing());
  599. SetCompositionString(composition);
  600. if (input_context != nullptr)
  601. {
  602. input_context->SetCompositionRange(composition_range_start, composition_range_end);
  603. input_context->CommitComposition(composition);
  604. }
  605. // Move the cursor to the end of the string.
  606. SetCursorPosition(composition_range_end - composition_range_start, true);
  607. EndComposition();
  608. }
  609. void TextInputMethodEditor_Win32::SetCursorPosition(int _cursor_pos, bool update)
  610. {
  611. RMLUI_ASSERT(IsComposing());
  612. cursor_pos = _cursor_pos;
  613. if (update)
  614. UpdateCursorPosition();
  615. }
  616. void TextInputMethodEditor_Win32::SetCompositionString(Rml::StringView composition)
  617. {
  618. if (input_context == nullptr)
  619. return;
  620. // Retrieve the composition range if it is missing.
  621. if (composition_range_start == 0 && composition_range_end == 0)
  622. input_context->GetSelectionRange(composition_range_start, composition_range_end);
  623. input_context->SetText(composition, composition_range_start, composition_range_end);
  624. size_t length = Rml::StringUtilities::LengthUTF8(composition);
  625. composition_range_end = composition_range_start + (int)length;
  626. }
  627. void TextInputMethodEditor_Win32::UpdateCursorPosition()
  628. {
  629. // Cursor position update happens before a composition is set; ignore this event.
  630. if (input_context == nullptr || (composition_range_start == 0 && composition_range_end == 0))
  631. return;
  632. if (cursor_pos != -1)
  633. {
  634. int position = composition_range_start + cursor_pos;
  635. input_context->SetCursorPosition(position);
  636. }
  637. else
  638. {
  639. // If the API reports no cursor position, select the entire composition string for a better UX.
  640. input_context->SetSelectionRange(composition_range_start, composition_range_end);
  641. }
  642. }