RmlUi_Platform_Win32.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef RMLUI_BACKENDS_PLATFORM_WIN32_H
  29. #define RMLUI_BACKENDS_PLATFORM_WIN32_H
  30. #include "RmlUi_Include_Windows.h"
  31. #include <RmlUi/Core/Input.h>
  32. #include <RmlUi/Core/StringUtilities.h>
  33. #include <RmlUi/Core/SystemInterface.h>
  34. #include <RmlUi/Core/TextInputHandler.h>
  35. #include <RmlUi/Core/Types.h>
  36. #include <string>
  37. class SystemInterface_Win32 : public Rml::SystemInterface {
  38. public:
  39. SystemInterface_Win32();
  40. ~SystemInterface_Win32();
  41. // Optionally, provide or change the window to be used for setting the mouse cursor, clipboard text and IME position.
  42. void SetWindow(HWND window_handle);
  43. // -- Inherited from Rml::SystemInterface --
  44. double GetElapsedTime() override;
  45. void SetMouseCursor(const Rml::String& cursor_name) override;
  46. void SetClipboardText(const Rml::String& text) override;
  47. void GetClipboardText(Rml::String& text) override;
  48. void ActivateKeyboard(Rml::Vector2f caret_position, float line_height) override;
  49. private:
  50. HWND window_handle = nullptr;
  51. double time_frequency = {};
  52. LARGE_INTEGER time_startup = {};
  53. HCURSOR cursor_default = nullptr;
  54. HCURSOR cursor_move = nullptr;
  55. HCURSOR cursor_pointer = nullptr;
  56. HCURSOR cursor_resize = nullptr;
  57. HCURSOR cursor_cross = nullptr;
  58. HCURSOR cursor_text = nullptr;
  59. HCURSOR cursor_unavailable = nullptr;
  60. };
  61. class TextInputMethodEditor_Win32;
  62. /**
  63. Optional helper functions for the Win32 plaform.
  64. */
  65. namespace RmlWin32 {
  66. // Convenience helpers for converting between RmlUi strings (UTF-8) and Windows strings (UTF-16).
  67. Rml::String ConvertToUTF8(const std::wstring& wstr);
  68. std::wstring ConvertToUTF16(const Rml::String& str);
  69. // Window event handler to submit default input behavior to the context.
  70. // @return True if the event is still propagating, false if it was handled by the context.
  71. bool WindowProcedure(Rml::Context* context, TextInputMethodEditor_Win32& text_input_method_editor, HWND window_handle, UINT message, WPARAM w_param,
  72. LPARAM l_param);
  73. // Converts the key from Win32 key code to RmlUi key.
  74. Rml::Input::KeyIdentifier ConvertKey(int win32_key_code);
  75. // Returns the active RmlUi key modifier state.
  76. int GetKeyModifierState();
  77. } // namespace RmlWin32
  78. /**
  79. Custom backend implementation of TextInputHandler to handle the system's Input Method Editor (IME).
  80. This version supports only one active text input context.
  81. */
  82. class TextInputMethodEditor_Win32 final : public Rml::TextInputHandler {
  83. public:
  84. TextInputMethodEditor_Win32();
  85. void OnActivate(Rml::TextInputContext* input_context) override;
  86. void OnDeactivate(Rml::TextInputContext* input_context) override;
  87. void OnDestroy(Rml::TextInputContext* input_context) override;
  88. /// Check that a composition is currently active.
  89. /// @return True if we are composing, false otherwise.
  90. bool IsComposing() const;
  91. void StartComposition();
  92. void CancelComposition();
  93. /// Set the composition string.
  94. /// @param[in] composition A string to be set.
  95. void SetComposition(Rml::StringView composition);
  96. /// End the current composition by confirming the composition string.
  97. /// @param[in] composition A string to confirm.
  98. void ConfirmComposition(Rml::StringView composition);
  99. /// Set the cursor position within the composition.
  100. /// @param[in] cursor_pos A character position of the cursor within the composition string.
  101. /// @param[in] update Update the cursor position within active input contexts.
  102. void SetCursorPosition(int cursor_pos, bool update);
  103. private:
  104. void EndComposition();
  105. void SetCompositionString(Rml::StringView composition);
  106. void UpdateCursorPosition();
  107. private:
  108. // An actively used text input method context.
  109. Rml::TextInputContext* input_context;
  110. // A flag to mark a composition is currently active.
  111. bool composing;
  112. // Character position of the cursor in the composition string.
  113. int cursor_pos;
  114. // Composition range (character position) relative to the text input value.
  115. int composition_range_start;
  116. int composition_range_end;
  117. };
  118. #endif