WidgetTextInput.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /**
  38. An abstract widget for editing and navigating around a text field.
  39. @author Peter Curry
  40. */
  41. class WidgetTextInput : public EventListener {
  42. public:
  43. WidgetTextInput(ElementFormControl* parent);
  44. virtual ~WidgetTextInput();
  45. /// Sets the value of the text field.
  46. /// @param[in] value The new value to set on the text field.
  47. /// @note The value will be sanitized and synchronized with the element's value attribute.
  48. void SetValue(String value);
  49. /// Sets the maximum length (in characters) of this text field.
  50. /// @param[in] max_length The new maximum length of the text field. A number lower than zero will mean infinite characters.
  51. void SetMaxLength(int max_length);
  52. /// Returns the maximum length (in characters) of this text field.
  53. /// @return The maximum number of characters allowed in this text field.
  54. int GetMaxLength() const;
  55. /// Returns the current length (in characters) of this text field.
  56. int GetLength() const;
  57. /// Selects all text.
  58. void Select();
  59. /// Selects the text in the given character range.
  60. /// @param[in] selection_start The first character to be selected.
  61. /// @param[in] selection_end The first character *after* the selection.
  62. void SetSelectionRange(int selection_start, int selection_end);
  63. /// Retrieves the selection range and text.
  64. /// @param[out] selection_start The first character selected.
  65. /// @param[out] selection_end The first character *after* the selection.
  66. /// @param[out] selected_text The selected text.
  67. void GetSelection(int* selection_start, int* selection_end, String* selected_text) const;
  68. /// Update the colours of the selected text.
  69. void UpdateSelectionColours();
  70. /// Generates the text cursor.
  71. void GenerateCursor();
  72. /// Force text formatting on the next layout update.
  73. void ForceFormattingOnNextLayout();
  74. /// Updates the cursor, if necessary.
  75. void OnUpdate();
  76. /// Renders the cursor, if it is visible.
  77. void OnRender();
  78. /// Formats the widget's internal content.
  79. void OnLayout();
  80. /// Called when the parent element's size changes.
  81. void OnResize();
  82. protected:
  83. enum class CursorMovement { Begin = -4, BeginLine = -3, PreviousWord = -2, Left = -1, Right = 1, NextWord = 2, EndLine = 3, End = 4 };
  84. float GetAlignmentSpecificTextOffset(const char* p_begin, int line_index) const;
  85. /// Processes the "keydown" and "textinput" event to write to the input field, and the "focus" and
  86. /// "blur" to set the state of the cursor.
  87. void ProcessEvent(Event& event) override;
  88. /// Adds new characters to the string at the cursor position.
  89. /// @param[in] string The characters to add.
  90. /// @return True if at least one character was successfully added, false otherwise.
  91. bool AddCharacters(String string);
  92. /// Deletes characters from the string.
  93. /// @param[in] direction Movement of cursor for deletion.
  94. /// @return True if a character was deleted, false otherwise.
  95. bool DeleteCharacters(CursorMovement direction);
  96. /// Removes any invalid characters from the string.
  97. virtual void SanitizeValue(String& value) = 0;
  98. /// Transforms the displayed value of the text box, typically used for password fields.
  99. /// @note Only use this for transforming characters, do not modify the length of the string.
  100. virtual void TransformValue(String& value);
  101. /// Called when the user pressed enter.
  102. virtual void LineBreak() = 0;
  103. /// Gets the parent element containing the widget.
  104. Element* GetElement() const;
  105. /// Dispatches a change event to the widget's element.
  106. void DispatchChangeEvent(bool linebreak = false);
  107. private:
  108. /// Returns the displayed value of the text field.
  109. /// @note For password fields this would only return the displayed asterisks '****', while the attribute value below contains the underlying text.
  110. const String& GetValue() const;
  111. /// Returns the underlying text from the element's value attribute.
  112. String GetAttributeValue() const;
  113. /// Moves the cursor along the current line.
  114. /// @param[in] movement Cursor movement operation.
  115. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  116. /// @return True if selection was changed.
  117. bool MoveCursorHorizontal(CursorMovement movement, bool select);
  118. /// Moves the cursor up and down the text field.
  119. /// @param[in] x How far to move the cursor.
  120. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  121. /// @return True if selection was changed.
  122. bool MoveCursorVertical(int distance, bool select);
  123. // Move the cursor to utf-8 boundaries, in case it was moved into the middle of a multibyte character.
  124. /// @param[in] forward True to seek forward, else back.
  125. void MoveCursorToCharacterBoundaries(bool forward);
  126. // Expands the cursor, selecting the current word or nearby whitespace.
  127. void ExpandSelection();
  128. /// Returns the relative indices from the current absolute index.
  129. void GetRelativeCursorIndices(int& out_cursor_line_index, int& out_cursor_character_index) const;
  130. /// Sets the absolute cursor index from the given relative indices.
  131. void SetCursorFromRelativeIndices(int line_index, int character_index);
  132. /// Calculates the line index under a specific vertical position.
  133. /// @param[in] position The position to query.
  134. /// @return The index of the line under the mouse cursor.
  135. int CalculateLineIndex(float position) const;
  136. /// Calculates the character index along a line under a specific horizontal position.
  137. /// @param[in] line_index The line to query.
  138. /// @param[in] position The position to query.
  139. /// @param[out] on_right_side True if position is on the right side of the returned character, else left side.
  140. /// @return The index of the character under the mouse cursor.
  141. int CalculateCharacterIndex(int line_index, float position);
  142. /// Shows or hides the cursor.
  143. /// @param[in] show True to show the cursor, false to hide it.
  144. /// @param[in] move_to_cursor True to force the cursor to be visible, false to not scroll the widget.
  145. void ShowCursor(bool show, bool move_to_cursor = true);
  146. /// Formats the element, laying out the text and inserting scrollbars as appropriate.
  147. void FormatElement();
  148. /// Formats the input element's text field.
  149. /// @param[in] height_constraint Abort formatting when the formatted size grows larger than this height.
  150. /// @return The content area of the element.
  151. Vector2f FormatText(float height_constraint = FLT_MAX);
  152. /// Updates the position to render the cursor.
  153. /// @param[in] update_ideal_cursor_position Generally should be true on horizontal movement and false on vertical movement.
  154. void UpdateCursorPosition(bool update_ideal_cursor_position);
  155. /// Expand or shrink the text selection to the position of the cursor.
  156. /// @param[in] selecting True if the new position of the cursor should expand / contract the selection area, false if it should only set the
  157. /// anchor for future selections.
  158. /// @return True if selection was changed.
  159. bool UpdateSelection(bool selecting);
  160. /// Removes the selection of text.
  161. /// @return True if selection was changed.
  162. bool ClearSelection();
  163. /// Deletes all selected text and removes the selection.
  164. void DeleteSelection();
  165. /// Copies the selection (if any) to the clipboard.
  166. void CopySelection();
  167. /// Split one line of text into three parts, based on the current selection.
  168. /// @param[out] pre_selection The section of unselected text before any selected text on the line.
  169. /// @param[out] selection The section of selected text on the line.
  170. /// @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
  171. /// will be empty.
  172. /// @param[in] line The text making up the line.
  173. /// @param[in] line_begin The absolute index at the beginning of the line.
  174. void GetLineSelection(String& pre_selection, String& selection, String& post_selection, const String& line, int line_begin) const;
  175. struct Line {
  176. // Offset into the text field's value.
  177. int value_offset;
  178. // The size of the contents of the line (including the trailing endline, if that terminated the line).
  179. int size;
  180. // The length of the editable characters on the line (excluding any trailing endline).
  181. int editable_length;
  182. };
  183. ElementFormControl* parent;
  184. ElementText* text_element;
  185. ElementText* selected_text_element;
  186. Vector2f internal_dimensions;
  187. Vector2f scroll_offset;
  188. using LineList = Vector<Line>;
  189. LineList lines;
  190. // Length in number of characters.
  191. int max_length;
  192. // -- All indices are in bytes: Should always be moved along UTF-8 start bytes. --
  193. // Absolute cursor index. Byte index into the text field's value.
  194. int absolute_cursor_index;
  195. // 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
  196. // line and at the beginning of the next line. This state determines which of these lines the cursor is placed on visually.
  197. bool cursor_wrap_down;
  198. bool ideal_cursor_position_to_the_right_of_cursor;
  199. bool cancel_next_drag;
  200. bool force_formatting_on_next_layout;
  201. // Selection. The start and end indices of the selection are in absolute coordinates.
  202. Element* selection_element;
  203. int selection_anchor_index;
  204. int selection_begin_index;
  205. int selection_length;
  206. // The colour of the background of selected text.
  207. ColourbPremultiplied selection_colour;
  208. // The selection background.
  209. Geometry selection_geometry;
  210. // Cursor visibility and timings.
  211. float cursor_timer;
  212. bool cursor_visible;
  213. bool keyboard_showed;
  214. /// Activate or deactivate keyboard (for touchscreen devices)
  215. /// @param[in] active True if need activate keyboard, false if need deactivate.
  216. void SetKeyboardActive(bool active);
  217. double last_update_time;
  218. // The cursor geometry.
  219. float ideal_cursor_position;
  220. Vector2f cursor_position;
  221. Vector2f cursor_size;
  222. Geometry cursor_geometry;
  223. };
  224. } // namespace Rml
  225. #endif