WidgetTextInput.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 RMLUICONTROLSWIDGETTEXTINPUT_H
  29. #define RMLUICONTROLSWIDGETTEXTINPUT_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. namespace Core {
  35. class ElementText;
  36. }
  37. namespace Controls {
  38. class ElementFormControl;
  39. /**
  40. An abstract widget for editing and navigating around a text field.
  41. @author Peter Curry
  42. */
  43. class WidgetTextInput : public Core::EventListener
  44. {
  45. public:
  46. WidgetTextInput(ElementFormControl* parent);
  47. virtual ~WidgetTextInput();
  48. /// Sets the value of the text field.
  49. /// @param[in] value The new value to set on the text field.
  50. virtual void SetValue(const Core::String& value);
  51. /// Sets the maximum length (in characters) of this text field.
  52. /// @param[in] max_length The new maximum length of the text field. A number lower than zero will mean infinite characters.
  53. void SetMaxLength(int max_length);
  54. /// Returns the maximum length (in characters) of this text field.
  55. /// @return The maximum number of characters allowed in this text field.
  56. int GetMaxLength() const;
  57. /// Update the colours of the selected text.
  58. void UpdateSelectionColours();
  59. /// Updates the cursor, if necessary.
  60. void OnUpdate();
  61. /// Renders the cursor, if it is visible.
  62. void OnRender();
  63. /// Formats the widget's internal content.
  64. void OnLayout();
  65. /// Called when the parent element's size changes.
  66. void OnResize();
  67. /// Returns the input element's underlying text element.
  68. Core::ElementText* GetTextElement();
  69. /// Returns the input element's maximum allowed text dimensions.
  70. const Rml::Core::Vector2f& GetTextDimensions() const;
  71. protected:
  72. /// Processes the "keydown" and "textinput" event to write to the input field, and the "focus" and
  73. /// "blur" to set the state of the cursor.
  74. void ProcessEvent(Core::Event& event) override;
  75. /// Adds a new character to the string at the cursor position.
  76. /// @param[in] character The character to add to the string.
  77. /// @return True if the character was successfully added, false otherwise.
  78. bool AddCharacter(Rml::Core::word character);
  79. /// Deletes a character from the string.
  80. /// @param[in] backward True to delete a character behind the cursor, false for in front of the cursor.
  81. /// @return True if a character was deleted, false otherwise.
  82. bool DeleteCharacter(bool back);
  83. /// Returns true if the given character is permitted in the input field, false if not.
  84. /// @param[in] character The character to validate.
  85. /// @return True if the character is allowed, false if not.
  86. virtual bool IsCharacterValid(Rml::Core::word character) = 0;
  87. /// Called when the user pressed enter.
  88. virtual void LineBreak() = 0;
  89. /// Returns the absolute index of the cursor.
  90. int GetCursorIndex() const;
  91. /// Gets the parent element containing the widget.
  92. Core::Element* GetElement();
  93. /// Dispatches a change event to the widget's element.
  94. void DispatchChangeEvent(bool linebreak = false);
  95. private:
  96. /// Moves the cursor along the current line.
  97. /// @param[in] x How far to move the cursor.
  98. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  99. void MoveCursorHorizontal(int distance, bool select);
  100. /// Moves the cursor up and down the text field.
  101. /// @param[in] x How far to move the cursor.
  102. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  103. void MoveCursorVertical(int distance, bool select);
  104. /// Updates the absolute cursor index from the relative cursor indices.
  105. void UpdateAbsoluteCursor();
  106. /// Updates the relative cursor indices from the absolute cursor index.
  107. void UpdateRelativeCursor();
  108. /// Calculates the line index under a specific vertical position.
  109. /// @param[in] position The position to query.
  110. /// @return The index of the line under the mouse cursor.
  111. int CalculateLineIndex(float position);
  112. /// Calculates the character index along a line under a specific horizontal position.
  113. /// @param[in] line_index The line to query.
  114. /// @param[in] position The position to query.
  115. /// @return The index of the character under the mouse cursor.
  116. int CalculateCharacterIndex(int line_index, float position);
  117. /// Shows or hides the cursor.
  118. /// @param[in] show True to show the cursor, false to hide it.
  119. /// @param[in] move_to_cursor True to force the cursor to be visible, false to not scroll the widget.
  120. void ShowCursor(bool show, bool move_to_cursor = true);
  121. /// Formats the element, laying out the text and inserting scrollbars as appropriate.
  122. void FormatElement();
  123. /// Formats the input element's text field.
  124. /// @return The content area of the element.
  125. Rml::Core::Vector2f FormatText();
  126. /// Generates the text cursor.
  127. void GenerateCursor();
  128. /// Updates the position to render the cursor.
  129. void UpdateCursorPosition();
  130. /// Expand or shrink the text selection to the position of the cursor.
  131. /// @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.
  132. void UpdateSelection(bool selecting);
  133. /// Removes the selection of text.
  134. void ClearSelection();
  135. /// Deletes all selected text and removes the selection.
  136. void DeleteSelection();
  137. /// Copies the selection (if any) to the clipboard.
  138. void CopySelection();
  139. /// Split one line of text into three parts, based on the current selection.
  140. /// @param[out] pre_selection The section of unselected text before any selected text on the line.
  141. /// @param[out] selection The section of selected text on the line.
  142. /// @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.
  143. /// @param[in] line The text making up the line.
  144. /// @param[in] line_begin The absolute index at the beginning of the line.
  145. void GetLineSelection(Core::String& pre_selection, Core::String& selection, Core::String& post_selection, const Core::String& line, int line_begin);
  146. struct Line
  147. {
  148. // The contents of the line (including the trailing endline, if that terminated the line).
  149. Core::String content;
  150. // The length of the editable characters on the line (excluding any trailing endline).
  151. int content_length;
  152. // The number of extra characters at the end of the content that are not present in the actual value; in the
  153. // case of a soft return, this may be negative.
  154. int extra_characters;
  155. };
  156. ElementFormControl* parent;
  157. Core::ElementText* text_element;
  158. Core::ElementText* selected_text_element;
  159. Rml::Core::Vector2f internal_dimensions;
  160. Rml::Core::Vector2f scroll_offset;
  161. typedef std::vector< Line > LineList;
  162. LineList lines;
  163. int max_length;
  164. int edit_index;
  165. int absolute_cursor_index;
  166. int cursor_line_index;
  167. int cursor_character_index;
  168. // Selection. The start and end indices of the selection are in absolute coordinates.
  169. Core::Element* selection_element;
  170. int selection_anchor_index;
  171. int selection_begin_index;
  172. int selection_length;
  173. // The colour of the background of selected text.
  174. Rml::Core::Colourb selection_colour;
  175. // The selection background.
  176. Core::Geometry selection_geometry;
  177. // Cursor visibility and timings.
  178. float cursor_timer;
  179. bool cursor_visible;
  180. bool keyboard_showed;
  181. /// Activate or deactivate keyboard (for touchscreen devices)
  182. /// @param[in] active True if need activate keyboard, false if need deactivate.
  183. void SetKeyboardActive(bool active);
  184. double last_update_time;
  185. // The cursor geometry.
  186. float ideal_cursor_position;
  187. Rml::Core::Vector2f cursor_position;
  188. Rml::Core::Vector2f cursor_size;
  189. Core::Geometry cursor_geometry;
  190. };
  191. }
  192. }
  193. #endif