WidgetTextInput.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. virtual void SetValue(const String& value);
  48. /// Sets the maximum length (in characters) of this text field.
  49. /// @param[in] max_length The new maximum length of the text field. A number lower than zero will mean infinite characters.
  50. void SetMaxLength(int max_length);
  51. /// Returns the maximum length (in characters) of this text field.
  52. /// @return The maximum number of characters allowed in this text field.
  53. int GetMaxLength() const;
  54. /// Returns the current length (in characters) of this text field.
  55. int GetLength() const;
  56. /// Update the colours of the selected text.
  57. void UpdateSelectionColours();
  58. /// Generates the text cursor.
  59. void GenerateCursor();
  60. /// Updates the cursor, if necessary.
  61. void OnUpdate();
  62. /// Renders the cursor, if it is visible.
  63. void OnRender();
  64. /// Formats the widget's internal content.
  65. void OnLayout();
  66. /// Called when the parent element's size changes.
  67. void OnResize();
  68. /// Returns the input element's underlying text element.
  69. ElementText* GetTextElement();
  70. /// Returns the input element's maximum allowed text dimensions.
  71. Vector2f GetTextDimensions() const;
  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. /// Returns true if the given character is permitted in the input field, false if not.
  86. /// @param[in] character The character to validate.
  87. /// @return True if the character is allowed, false if not.
  88. virtual bool IsCharacterValid(char character) = 0;
  89. /// Called when the user pressed enter.
  90. virtual void LineBreak() = 0;
  91. /// Returns the absolute index of the cursor.
  92. int GetCursorIndex() const;
  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. /// Moves the cursor along the current line.
  99. /// @param[in] movement Cursor movement operation.
  100. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  101. void MoveCursorHorizontal(CursorMovement movement, bool select);
  102. /// Moves the cursor up and down the text field.
  103. /// @param[in] x How far to move the cursor.
  104. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  105. void MoveCursorVertical(int distance, bool select);
  106. // Move the cursor to utf-8 boundaries, in case it was moved into the middle of a multibyte character.
  107. /// @param[in] forward True to seek forward, else back.
  108. void MoveCursorToCharacterBoundaries(bool forward);
  109. // Expands the cursor, selecting the current word or nearby whitespace.
  110. void ExpandSelection();
  111. /// Updates the absolute cursor index from the relative cursor indices.
  112. void UpdateAbsoluteCursor();
  113. /// Updates the relative cursor indices from the absolute cursor index.
  114. void UpdateRelativeCursor();
  115. /// Calculates the line index under a specific vertical position.
  116. /// @param[in] position The position to query.
  117. /// @return The index of the line under the mouse cursor.
  118. int CalculateLineIndex(float position);
  119. /// Calculates the character index along a line under a specific horizontal position.
  120. /// @param[in] line_index The line to query.
  121. /// @param[in] position The position to query.
  122. /// @param[out] on_right_side True if position is on the right side of the returned character, else left side.
  123. /// @return The index of the character under the mouse cursor.
  124. int CalculateCharacterIndex(int line_index, float position);
  125. /// Shows or hides the cursor.
  126. /// @param[in] show True to show the cursor, false to hide it.
  127. /// @param[in] move_to_cursor True to force the cursor to be visible, false to not scroll the widget.
  128. void ShowCursor(bool show, bool move_to_cursor = true);
  129. /// Formats the element, laying out the text and inserting scrollbars as appropriate.
  130. void FormatElement();
  131. /// Formats the input element's text field.
  132. /// @return The content area of the element.
  133. Vector2f FormatText();
  134. /// Updates the position to render the cursor.
  135. void UpdateCursorPosition();
  136. /// Expand or shrink the text selection to the position of the cursor.
  137. /// @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.
  138. void UpdateSelection(bool selecting);
  139. /// Removes the selection of text.
  140. void ClearSelection();
  141. /// Deletes all selected text and removes the selection.
  142. void DeleteSelection();
  143. /// Copies the selection (if any) to the clipboard.
  144. void CopySelection();
  145. /// Split one line of text into three parts, based on the current selection.
  146. /// @param[out] pre_selection The section of unselected text before any selected text on the line.
  147. /// @param[out] selection The section of selected text on the line.
  148. /// @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.
  149. /// @param[in] line The text making up the line.
  150. /// @param[in] line_begin The absolute index at the beginning of the line.
  151. void GetLineSelection(String& pre_selection, String& selection, String& post_selection, const String& line, int line_begin);
  152. struct Line
  153. {
  154. // The contents of the line (including the trailing endline, if that terminated the line).
  155. String content;
  156. // The length of the editable characters on the line (excluding any trailing endline).
  157. int content_length;
  158. // The number of extra characters at the end of the content that are not present in the actual value; in the
  159. // case of a soft return, this may be negative.
  160. int extra_characters;
  161. };
  162. ElementFormControl* parent;
  163. ElementText* text_element;
  164. ElementText* selected_text_element;
  165. Vector2f internal_dimensions;
  166. Vector2f scroll_offset;
  167. typedef Vector< Line > LineList;
  168. LineList lines;
  169. // Length in number of characters.
  170. int max_length;
  171. // Indices in bytes: Should always be moved along UTF-8 start bytes.
  172. int edit_index;
  173. int absolute_cursor_index;
  174. int cursor_line_index;
  175. int cursor_character_index;
  176. bool cursor_on_right_side_of_character;
  177. bool cancel_next_drag;
  178. // Selection. The start and end indices of the selection are in absolute coordinates.
  179. Element* selection_element;
  180. int selection_anchor_index;
  181. int selection_begin_index;
  182. int selection_length;
  183. // The colour of the background of selected text.
  184. Colourb selection_colour;
  185. // The selection background.
  186. Geometry selection_geometry;
  187. // Cursor visibility and timings.
  188. float cursor_timer;
  189. bool cursor_visible;
  190. bool keyboard_showed;
  191. /// Activate or deactivate keyboard (for touchscreen devices)
  192. /// @param[in] active True if need activate keyboard, false if need deactivate.
  193. void SetKeyboardActive(bool active);
  194. double last_update_time;
  195. // The cursor geometry.
  196. float ideal_cursor_position;
  197. Vector2f cursor_position;
  198. Vector2f cursor_size;
  199. Geometry cursor_geometry;
  200. };
  201. } // namespace Rml
  202. #endif