WidgetTextInput.h 11 KB

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