WidgetTextInput.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. namespace Rml {
  34. class ElementText;
  35. class ElementFormControl;
  36. /**
  37. An abstract widget for editing and navigating around a text field.
  38. @author Peter Curry
  39. */
  40. class WidgetTextInput : public EventListener
  41. {
  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. /// Update the colours of the selected text.
  58. void UpdateSelectionColours();
  59. /// Generates the text cursor.
  60. void GenerateCursor();
  61. /// Updates the cursor, if necessary.
  62. void OnUpdate();
  63. /// Renders the cursor, if it is visible.
  64. void OnRender();
  65. /// Formats the widget's internal content.
  66. void OnLayout();
  67. /// Called when the parent element's size changes.
  68. void OnResize();
  69. /// Returns the input element's underlying text element.
  70. ElementText* GetTextElement();
  71. /// Returns the input element's maximum allowed text dimensions.
  72. Vector2f GetTextDimensions() const;
  73. protected:
  74. enum class CursorMovement { Begin = -4, BeginLine = -3, PreviousWord = -2, Left = -1, Right = 1, NextWord = 2, EndLine = 3, End = 4 };
  75. /// Processes the "keydown" and "textinput" event to write to the input field, and the "focus" and
  76. /// "blur" to set the state of the cursor.
  77. void ProcessEvent(Event& event) override;
  78. /// Adds new characters to the string at the cursor position.
  79. /// @param[in] string The characters to add.
  80. /// @return True if at least one character was successfully added, false otherwise.
  81. bool AddCharacters(String string);
  82. /// Deletes characters from the string.
  83. /// @param[in] direction Movement of cursor for deletion.
  84. /// @return True if a character was deleted, false otherwise.
  85. bool DeleteCharacters(CursorMovement direction);
  86. /// Removes any invalid characters from the string.
  87. virtual void SanitizeValue(String& value) = 0;
  88. /// Transforms the displayed value of the text box, typically used for password fields.
  89. /// @note Only use this for transforming characters, do not modify the length of the string.
  90. virtual void TransformValue(String& value);
  91. /// Called when the user pressed enter.
  92. virtual void LineBreak() = 0;
  93. /// Gets the parent element containing the widget.
  94. Element* GetElement() const;
  95. /// Dispatches a change event to the widget's element.
  96. void DispatchChangeEvent(bool linebreak = false);
  97. private:
  98. /// Returns the displayed value of the text field.
  99. /// @note For password fields this would only return the displayed asterisks '****', while the attribute value below contains the underlying text.
  100. const String& GetValue() const;
  101. /// Returns the underlying text from the element's value attribute.
  102. String GetAttributeValue() const;
  103. /// Moves the cursor along the current line.
  104. /// @param[in] movement Cursor movement operation.
  105. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  106. void 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. void MoveCursorVertical(int distance, bool select);
  111. // Move the cursor to utf-8 boundaries, in case it was moved into the middle of a multibyte character.
  112. /// @param[in] forward True to seek forward, else back.
  113. void MoveCursorToCharacterBoundaries(bool forward);
  114. // Expands the cursor, selecting the current word or nearby whitespace.
  115. void ExpandSelection();
  116. /// Returns the current state of the relative cursor converted to the absolute index.
  117. /// @note The absolute index is the byte offset into the 'value' string.
  118. int GetAbsoluteCursorIndex() const;
  119. /// Sets the relative cursor indices based on an absolute index.
  120. /// @note The relative index for a given absolute index is not necessarily unique. For example, the very end of a word-wrapped line and the
  121. /// beginning of the next line will have the same absolute index.
  122. void SetCursorFromAbsoluteIndex(int absolute_index);
  123. /// Calculates the line index under a specific vertical position.
  124. /// @param[in] position The position to query.
  125. /// @return The index of the line under the mouse cursor.
  126. int CalculateLineIndex(float position) const;
  127. /// Calculates the character index along a line under a specific horizontal position.
  128. /// @param[in] line_index The line to query.
  129. /// @param[in] position The position to query.
  130. /// @param[out] on_right_side True if position is on the right side of the returned character, else left side.
  131. /// @return The index of the character under the mouse cursor.
  132. int CalculateCharacterIndex(int line_index, float position);
  133. /// Shows or hides the cursor.
  134. /// @param[in] show True to show the cursor, false to hide it.
  135. /// @param[in] move_to_cursor True to force the cursor to be visible, false to not scroll the widget.
  136. void ShowCursor(bool show, bool move_to_cursor = true);
  137. /// Formats the element, laying out the text and inserting scrollbars as appropriate.
  138. void FormatElement();
  139. /// Formats the input element's text field.
  140. /// @return The content area of the element.
  141. Vector2f FormatText();
  142. /// Updates the position to render the cursor.
  143. /// @param[in] update_ideal_cursor_position Generally should be true on horizontal movement and false on vertical movement.
  144. void UpdateCursorPosition(bool update_ideal_cursor_position);
  145. /// Expand or shrink the text selection to the position of the cursor.
  146. /// @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.
  147. void UpdateSelection(bool selecting);
  148. /// Removes the selection of text.
  149. void ClearSelection();
  150. /// Deletes all selected text and removes the selection.
  151. void DeleteSelection();
  152. /// Copies the selection (if any) to the clipboard.
  153. void CopySelection();
  154. /// Split one line of text into three parts, based on the current selection.
  155. /// @param[out] pre_selection The section of unselected text before any selected text on the line.
  156. /// @param[out] selection The section of selected text on the line.
  157. /// @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.
  158. /// @param[in] line The text making up the line.
  159. /// @param[in] line_begin The absolute index at the beginning of the line.
  160. void GetLineSelection(String& pre_selection, String& selection, String& post_selection, const String& line, int line_begin) const;
  161. struct Line
  162. {
  163. // The contents of the line (including the trailing endline, if that terminated the line).
  164. String content;
  165. // The length of the editable characters on the line (excluding any trailing endline).
  166. int content_length;
  167. // The number of extra characters at the end of the content that are not present in the actual value; in the
  168. // case of a soft return, this may be negative.
  169. int extra_characters;
  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. // Relative cursor indices.
  182. int cursor_line_index;
  183. int cursor_character_index;
  184. bool ideal_cursor_position_to_the_right_of_cursor;
  185. bool cancel_next_drag;
  186. // Selection. The start and end indices of the selection are in absolute coordinates.
  187. Element* selection_element;
  188. int selection_anchor_index;
  189. int selection_begin_index;
  190. int selection_length;
  191. // The colour of the background of selected text.
  192. Colourb selection_colour;
  193. // The selection background.
  194. Geometry selection_geometry;
  195. // Cursor visibility and timings.
  196. float cursor_timer;
  197. bool cursor_visible;
  198. bool keyboard_showed;
  199. /// Activate or deactivate keyboard (for touchscreen devices)
  200. /// @param[in] active True if need activate keyboard, false if need deactivate.
  201. void SetKeyboardActive(bool active);
  202. double last_update_time;
  203. // The cursor geometry.
  204. float ideal_cursor_position;
  205. Vector2f cursor_position;
  206. Vector2f cursor_size;
  207. Geometry cursor_geometry;
  208. };
  209. } // namespace Rml
  210. #endif