WidgetTextInput.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /// Returns the current length (in characters) of this text field.
  58. int GetLength() const;
  59. /// Update the colours of the selected text.
  60. void UpdateSelectionColours();
  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. Core::ElementText* GetTextElement();
  71. /// Returns the input element's maximum allowed text dimensions.
  72. const Rml::Core::Vector2f& GetTextDimensions() const;
  73. protected:
  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(Core::Event& event) override;
  77. /// Adds a new character to the string at the cursor position.
  78. /// @param[in] character The character to add to the string.
  79. /// @return True if the character was successfully added, false otherwise.
  80. bool AddCharacter(Rml::Core::CodePoint character);
  81. /// Deletes a character from the string.
  82. /// @param[in] backward True to delete a character behind the cursor, false for in front of the cursor.
  83. /// @return True if a character was deleted, false otherwise.
  84. bool DeleteCharacter(bool back);
  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. Core::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] x How far to move the cursor.
  100. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  101. void MoveCursorHorizontal(int distance, 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. /// Updates the absolute cursor index from the relative cursor indices.
  107. void UpdateAbsoluteCursor();
  108. /// Updates the relative cursor indices from the absolute cursor index.
  109. void UpdateRelativeCursor();
  110. /// Calculates the line index under a specific vertical position.
  111. /// @param[in] position The position to query.
  112. /// @return The index of the line under the mouse cursor.
  113. int CalculateLineIndex(float position);
  114. /// Calculates the character index along a line under a specific horizontal position.
  115. /// @param[in] line_index The line to query.
  116. /// @param[in] position The position to query.
  117. /// @return The index of the character under the mouse cursor.
  118. int CalculateCharacterIndex(int line_index, float position);
  119. /// Shows or hides the cursor.
  120. /// @param[in] show True to show the cursor, false to hide it.
  121. /// @param[in] move_to_cursor True to force the cursor to be visible, false to not scroll the widget.
  122. void ShowCursor(bool show, bool move_to_cursor = true);
  123. /// Formats the element, laying out the text and inserting scrollbars as appropriate.
  124. void FormatElement();
  125. /// Formats the input element's text field.
  126. /// @return The content area of the element.
  127. Rml::Core::Vector2f FormatText();
  128. /// Generates the text cursor.
  129. void GenerateCursor();
  130. /// Updates the position to render the cursor.
  131. void UpdateCursorPosition();
  132. /// Expand or shrink the text selection to the position of the cursor.
  133. /// @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.
  134. void UpdateSelection(bool selecting);
  135. /// Removes the selection of text.
  136. void ClearSelection();
  137. /// Deletes all selected text and removes the selection.
  138. void DeleteSelection();
  139. /// Copies the selection (if any) to the clipboard.
  140. void CopySelection();
  141. /// Split one line of text into three parts, based on the current selection.
  142. /// @param[out] pre_selection The section of unselected text before any selected text on the line.
  143. /// @param[out] selection The section of selected text on the line.
  144. /// @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.
  145. /// @param[in] line The text making up the line.
  146. /// @param[in] line_begin The absolute index at the beginning of the line.
  147. void GetLineSelection(Core::String& pre_selection, Core::String& selection, Core::String& post_selection, const Core::String& line, int line_begin);
  148. struct Line
  149. {
  150. // The contents of the line (including the trailing endline, if that terminated the line).
  151. Core::String content;
  152. // The length of the editable characters on the line (excluding any trailing endline).
  153. int content_length;
  154. // The number of extra characters at the end of the content that are not present in the actual value; in the
  155. // case of a soft return, this may be negative.
  156. int extra_characters;
  157. };
  158. ElementFormControl* parent;
  159. Core::ElementText* text_element;
  160. Core::ElementText* selected_text_element;
  161. Rml::Core::Vector2f internal_dimensions;
  162. Rml::Core::Vector2f scroll_offset;
  163. typedef std::vector< Line > LineList;
  164. LineList lines;
  165. int max_length;
  166. int edit_index;
  167. int absolute_cursor_index;
  168. int cursor_line_index;
  169. int cursor_character_index;
  170. // Selection. The start and end indices of the selection are in absolute coordinates.
  171. Core::Element* selection_element;
  172. int selection_anchor_index;
  173. int selection_begin_index;
  174. int selection_length;
  175. // The colour of the background of selected text.
  176. Rml::Core::Colourb selection_colour;
  177. // The selection background.
  178. Core::Geometry selection_geometry;
  179. // Cursor visibility and timings.
  180. float cursor_timer;
  181. bool cursor_visible;
  182. bool keyboard_showed;
  183. /// Activate or deactivate keyboard (for touchscreen devices)
  184. /// @param[in] active True if need activate keyboard, false if need deactivate.
  185. void SetKeyboardActive(bool active);
  186. double last_update_time;
  187. // The cursor geometry.
  188. float ideal_cursor_position;
  189. Rml::Core::Vector2f cursor_position;
  190. Rml::Core::Vector2f cursor_size;
  191. Core::Geometry cursor_geometry;
  192. };
  193. }
  194. }
  195. #endif