WidgetTextInput.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_CORE_ELEMENTS_WIDGETTEXTINPUT_H
  29. #define RMLUI_CORE_ELEMENTS_WIDGETTEXTINPUT_H
  30. #include "../../../Include/RmlUi/Core/EventListener.h"
  31. #include "../../../Include/RmlUi/Core/Geometry.h"
  32. #include "../../../Include/RmlUi/Core/Vertex.h"
  33. #include <float.h>
  34. namespace Rml {
  35. class ElementText;
  36. class ElementFormControl;
  37. class TextInputHandler;
  38. class WidgetTextInputContext;
  39. /**
  40. An abstract widget for editing and navigating around a text field.
  41. @author Peter Curry
  42. */
  43. class WidgetTextInput : public EventListener {
  44. public:
  45. WidgetTextInput(ElementFormControl* parent);
  46. virtual ~WidgetTextInput();
  47. /// Sets the value of the text field.
  48. /// @param[in] value The new value to set on the text field.
  49. /// @note The value will be sanitized and synchronized with the element's value attribute.
  50. void SetValue(String value);
  51. /// Returns the underlying text from the element's value attribute.
  52. String GetAttributeValue() const;
  53. /// Sets the maximum length (in characters) of this text field.
  54. /// @param[in] max_length The new maximum length of the text field. A number lower than zero will mean infinite characters.
  55. void SetMaxLength(int max_length);
  56. /// Returns the maximum length (in characters) of this text field.
  57. /// @return The maximum number of characters allowed in this text field.
  58. int GetMaxLength() const;
  59. /// Returns the current length (in characters) of this text field.
  60. int GetLength() const;
  61. /// Selects all text.
  62. void Select();
  63. /// Selects the text in the given character range.
  64. /// @param[in] selection_start The first character to be selected.
  65. /// @param[in] selection_end The first character *after* the selection.
  66. void SetSelectionRange(int selection_start, int selection_end);
  67. /// Retrieves the selection range and text.
  68. /// @param[out] selection_start The first character selected.
  69. /// @param[out] selection_end The first character *after* the selection.
  70. /// @param[out] selected_text The selected text.
  71. void GetSelection(int* selection_start, int* selection_end, String* selected_text) const;
  72. /// Sets visual feedback used for the IME composition in the range.
  73. /// @param[in] range_start The first character to be selected.
  74. /// @param[in] range_end The first character *after* the selection.
  75. void SetCompositionRange(int range_start, int range_end);
  76. /// Obtains the IME composition byte range relative to the current value.
  77. void GetCompositionRange(int& range_start, int& range_end) const;
  78. /// Update the colours of the selected text.
  79. void UpdateSelectionColours();
  80. /// Generates the text cursor.
  81. void GenerateCursor();
  82. /// Force text formatting on the next layout update.
  83. void ForceFormattingOnNextLayout();
  84. /// Updates the cursor, if necessary.
  85. void OnUpdate();
  86. /// Renders the cursor, if it is visible.
  87. void OnRender();
  88. /// Formats the widget's internal content.
  89. void OnLayout();
  90. /// Called when the parent element's size changes.
  91. void OnResize();
  92. protected:
  93. enum class CursorMovement { Begin = -4, BeginLine = -3, PreviousWord = -2, Left = -1, Right = 1, NextWord = 2, EndLine = 3, End = 4 };
  94. /// Processes the "keydown" and "textinput" event to write to the input field, and the "focus" and
  95. /// "blur" to set the state of the cursor.
  96. void ProcessEvent(Event& event) override;
  97. /// Adds new characters to the string at the cursor position.
  98. /// @param[in] string The characters to add.
  99. /// @return True if at least one character was successfully added, false otherwise.
  100. bool AddCharacters(String string);
  101. /// Deletes characters from the string.
  102. /// @param[in] direction Movement of cursor for deletion.
  103. /// @return True if a character was deleted, false otherwise.
  104. bool DeleteCharacters(CursorMovement direction);
  105. /// Removes any invalid characters from the string.
  106. virtual void SanitizeValue(String& value) = 0;
  107. /// Transforms the displayed value of the text box, typically used for password fields.
  108. /// @note Only use this for transforming characters, do not modify the length of the string.
  109. virtual void TransformValue(String& value);
  110. /// Converts a display index to an attribute index.
  111. /// @param[in] display_index Absolute index into the displayed value (see GetValue).
  112. /// @param[in] attribute_value The attribute value to be considered.
  113. /// @return Absolute index into the attribute value (see GetAttributeValue).
  114. /// @note All indices stored in this class refer to the displayed value, unless otherwise specified. The display and
  115. /// attribute indices are typically identical, but may differ when the transformed value (see TransformValue) has
  116. /// modified the contents to be displayed.
  117. virtual int DisplayIndexToAttributeIndex(int display_index, const String& attribute_value);
  118. /// Converts an attribute index to a display index.
  119. /// @param[in] attribute_index Absolute index into the attribute value (see GetAttributeValue).
  120. /// @param[in] attribute_value The attribute value to be considered.
  121. /// @return Absolute index into the displayed value (see GetValue).
  122. virtual int AttributeIndexToDisplayIndex(int attribute_index, const String& attribute_value);
  123. /// Called when the user pressed enter.
  124. virtual void LineBreak() = 0;
  125. /// Gets the parent element containing the widget.
  126. Element* GetElement() const;
  127. /// Obtains the text input handler of the parent element's context.
  128. TextInputHandler* GetTextInputHandler() const;
  129. /// Returns true if the text input element is currently focused.
  130. bool IsFocused() const;
  131. /// Dispatches a change event to the widget's element.
  132. void DispatchChangeEvent(bool linebreak = false);
  133. private:
  134. struct Line {
  135. // Offset into the text field's value.
  136. int value_offset;
  137. // The size of the contents of the line (including the trailing endline, if that terminated the line).
  138. int size;
  139. // The length of the editable characters on the line (excluding any trailing endline).
  140. int editable_length;
  141. };
  142. /// Returns the displayed value of the text field.
  143. /// @note For password fields this would only return the displayed asterisks '****', while the attribute value below contains the underlying text.
  144. const String& GetValue() const;
  145. /// Moves the cursor along the current line.
  146. /// @param[in] movement Cursor movement operation.
  147. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  148. /// @param[out] out_of_bounds Set to true if the resulting line position is out of bounds, false if not.
  149. /// @return True if selection was changed.
  150. bool MoveCursorHorizontal(CursorMovement movement, bool select, bool& out_of_bounds);
  151. /// Moves the cursor up and down the text field.
  152. /// @param[in] distance How far to move the cursor.
  153. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  154. /// @param[out] out_of_bounds Set to true if the resulting line position is out of bounds, false if not.
  155. /// @return True if selection was changed.
  156. bool MoveCursorVertical(int distance, bool select, bool& out_of_bounds);
  157. // Move the cursor to utf-8 boundaries, in case it was moved into the middle of a multibyte character.
  158. /// @param[in] forward True to seek forward, else back.
  159. void MoveCursorToCharacterBoundaries(bool forward);
  160. // Expands the cursor, selecting the current word or nearby whitespace.
  161. void ExpandSelection();
  162. /// Returns the relative indices from the current absolute index.
  163. void GetRelativeCursorIndices(int& out_cursor_line_index, int& out_cursor_character_index) const;
  164. /// Sets the absolute cursor index from the given relative indices.
  165. void SetCursorFromRelativeIndices(int line_index, int character_index);
  166. /// Calculates the line index under a specific vertical position.
  167. /// @param[in] position The position to query.
  168. /// @return The index of the line under the mouse cursor.
  169. int CalculateLineIndex(float position) const;
  170. /// Calculates the character index along a line under a specific horizontal position.
  171. /// @param[in] line_index The line to query.
  172. /// @param[in] position The position to query.
  173. /// @return The index of the character under the mouse cursor.
  174. int CalculateCharacterIndex(int line_index, float position);
  175. /// Shows or hides the cursor.
  176. /// @param[in] show True to show the cursor, false to hide it.
  177. /// @param[in] move_to_cursor True to force the cursor to be visible, false to not scroll the widget.
  178. void ShowCursor(bool show, bool move_to_cursor = true);
  179. /// Formats the element, laying out the text and inserting scrollbars as appropriate.
  180. void FormatElement();
  181. /// Formats the input element's text field.
  182. /// @param[in] height_constraint Abort formatting when the formatted size grows larger than this height.
  183. /// @return The content area of the element.
  184. Vector2f FormatText(float height_constraint = FLT_MAX);
  185. /// Updates the position to render the cursor.
  186. /// @param[in] update_ideal_cursor_position Generally should be true on horizontal movement and false on vertical movement.
  187. void UpdateCursorPosition(bool update_ideal_cursor_position);
  188. /// Expand or shrink the text selection to the position of the cursor.
  189. /// @param[in] selecting True if the new position of the cursor should expand / contract the selection area, false if it should only set the
  190. /// anchor for future selections.
  191. /// @return True if selection was changed.
  192. bool UpdateSelection(bool selecting);
  193. /// Removes the selection of text.
  194. /// @return True if selection was changed.
  195. bool ClearSelection();
  196. /// Deletes all selected text and removes the selection.
  197. void DeleteSelection();
  198. /// Copies the selection (if any) to the clipboard.
  199. void CopySelection();
  200. /// Split one line of text into three parts, based on the current selection.
  201. /// @param[out] pre_selection The section of unselected text before any selected text on the line.
  202. /// @param[out] selection The section of selected text on the line.
  203. /// @param[out] post_selection The section of unselected text after any selected text on the line. If there is no selection on the line, then this
  204. /// will be empty.
  205. /// @param[in] line The text making up the line.
  206. /// @param[in] line_begin The absolute index at the beginning of the line.
  207. /// @lifetime The returned string views are tied to the lifetime of the line's data.
  208. void GetLineSelection(StringView& pre_selection, StringView& selection, StringView& post_selection, const String& line, int line_begin) const;
  209. /// Fetch the IME composition range on the line.
  210. /// @param[out] pre_composition The section of text before the IME composition string on the line.
  211. /// @param[out] ime_composition The IME composition string on the line.
  212. /// @param[in] line The text making up the line.
  213. /// @param[in] line_begin The absolute index at the beginning of the line.
  214. void GetLineIMEComposition(StringView& pre_composition, StringView& ime_composition, const String& line, int line_begin) const;
  215. /// Returns the offset that aligns the contents of the line according to the 'text-align' property.
  216. float GetAlignmentSpecificTextOffset(const Line& line) const;
  217. /// Returns the used line height.
  218. float GetLineHeight() const;
  219. /// Returns the width available for the text contents without overflowing, that is, the content area subtracted by any scrollbar.
  220. float GetAvailableWidth() const;
  221. /// Returns the height available for the text contents without overflowing, that is, the content area subtracted by any scrollbar.
  222. float GetAvailableHeight() const;
  223. ElementFormControl* parent;
  224. ElementText* text_element;
  225. ElementText* selected_text_element;
  226. Vector2f internal_dimensions;
  227. using LineList = Vector<Line>;
  228. LineList lines;
  229. // Length in number of characters.
  230. int max_length;
  231. // -- All indices are in bytes: Should always be moved along UTF-8 start bytes. --
  232. // Absolute cursor index. Byte index into the text field's value.
  233. int absolute_cursor_index;
  234. // When the cursor is located at the very end of a word-wrapped line there are two valid positions for the same absolute index: at the end of the
  235. // line and at the beginning of the next line. This state determines which of these lines the cursor is placed on visually.
  236. bool cursor_wrap_down;
  237. bool ideal_cursor_position_to_the_right_of_cursor;
  238. bool cancel_next_drag;
  239. bool force_formatting_on_next_layout;
  240. // Selection. The start and end indices of the selection are in absolute coordinates.
  241. Element* selection_element;
  242. int selection_anchor_index;
  243. int selection_begin_index;
  244. int selection_length;
  245. // The colour of the background of selected text.
  246. ColourbPremultiplied selection_colour;
  247. // The selection background.
  248. Geometry selection_composition_geometry;
  249. // IME composition range. The start and end indices are in absolute coordinates.
  250. int ime_composition_begin_index;
  251. int ime_composition_end_index;
  252. // The IME context for this widget.
  253. UniquePtr<WidgetTextInputContext> text_input_context;
  254. // Cursor visibility and timings.
  255. float cursor_timer;
  256. bool cursor_visible;
  257. bool keyboard_showed;
  258. /// Activate or deactivate keyboard (for touchscreen devices)
  259. /// @param[in] active True if need activate keyboard, false if need deactivate.
  260. void SetKeyboardActive(bool active);
  261. bool ink_overflow;
  262. double last_update_time;
  263. // The cursor geometry.
  264. float ideal_cursor_position;
  265. Vector2f cursor_position;
  266. Vector2f cursor_size;
  267. Geometry cursor_geometry;
  268. };
  269. } // namespace Rml
  270. #endif