WidgetTextInput.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCONTROLSWIDGETTEXTINPUT_H
  28. #define ROCKETCONTROLSWIDGETTEXTINPUT_H
  29. #include "../../Include/Rocket/Core/EventListener.h"
  30. #include "../../Include/Rocket/Core/Geometry.h"
  31. #include "../../Include/Rocket/Core/WString.h"
  32. #include "../../Include/Rocket/Core/Vertex.h"
  33. namespace Rocket {
  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. /// Returns the input element's underlying text element.
  66. Core::ElementText* GetTextElement();
  67. /// Returns the input element's maximum allowed text dimensions.
  68. const Rocket::Core::Vector2f& GetTextDimensions() const;
  69. protected:
  70. /// Processes the "keydown" and "textinput" event to write to the input field, and the "focus" and
  71. /// "blur" to set the state of the cursor.
  72. virtual void ProcessEvent(Core::Event& event);
  73. /// Adds a new character to the string at the cursor position.
  74. /// @param[in] character The character to add to the string.
  75. /// @return True if the character was successfully added, false otherwise.
  76. bool AddCharacter(Rocket::Core::word character);
  77. /// Deletes a character from the string.
  78. /// @param[in] backward True to delete a character behind the cursor, false for in front of the cursor.
  79. /// @return True if a character was deleted, false otherwise.
  80. bool DeleteCharacter(bool back);
  81. /// Returns true if the given character is permitted in the input field, false if not.
  82. /// @param[in] character The character to validate.
  83. /// @return True if the character is allowed, false if not.
  84. virtual bool IsCharacterValid(Rocket::Core::word character) = 0;
  85. /// Called when the user pressed enter.
  86. virtual void LineBreak() = 0;
  87. /// Returns the absolute index of the cursor.
  88. int GetCursorIndex() const;
  89. /// Gets the parent element containing the widget.
  90. Core::Element* GetElement();
  91. /// Dispatches a change event to the widget's element.
  92. void DispatchChangeEvent(bool linebreak = false);
  93. private:
  94. /// Moves the cursor along the current line.
  95. /// @param[in] x How far to move the cursor.
  96. /// @param[in] select True if the movement will also move the selection cursor, false if not.
  97. void MoveCursorHorizontal(int distance, bool select);
  98. /// Moves the cursor up and down the text field.
  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 MoveCursorVertical(int distance, bool select);
  102. /// Updates the absolute cursor index from the relative cursor indices.
  103. void UpdateAbsoluteCursor();
  104. /// Updates the relative cursor indices from the absolute cursor index.
  105. void UpdateRelativeCursor();
  106. /// Calculates the line index under a specific vertical position.
  107. /// @param[in] position The position to query.
  108. /// @return The index of the line under the mouse cursor.
  109. int CalculateLineIndex(float position);
  110. /// Calculates the character index along a line under a specific horizontal position.
  111. /// @param[in] line_index The line to query.
  112. /// @param[in] position The position to query.
  113. /// @return The index of the character under the mouse cursor.
  114. int CalculateCharacterIndex(int line_index, float position);
  115. /// Shows or hides the cursor.
  116. /// @param[in] show True to show the cursor, false to hide it.
  117. /// @param[in] move_to_cursor True to force the cursor to be visible, false to not scroll the widget.
  118. void ShowCursor(bool show, bool move_to_cursor = true);
  119. /// Formats the element, laying out the text and inserting scrollbars as appropriate.
  120. void FormatElement();
  121. /// Formats the input element's text field.
  122. /// @return The content area of the element.
  123. Rocket::Core::Vector2f FormatText();
  124. /// Generates the text cursor.
  125. void GenerateCursor();
  126. /// Updates the position to render the cursor.
  127. void UpdateCursorPosition();
  128. /// Expand or shrink the text selection to the position of the cursor.
  129. /// @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.
  130. void UpdateSelection(bool selecting);
  131. /// Removes the selection of text.
  132. void ClearSelection();
  133. /// Deletes all selected text and removes the selection.
  134. void DeleteSelection();
  135. /// Copies the selection (if any) to the clipboard.
  136. void CopySelection();
  137. /// Split one line of text into three parts, based on the current selection.
  138. /// @param[out] pre_selection The section of unselected text before any selected text on the line.
  139. /// @param[out] selection The section of selected text on the line.
  140. /// @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.
  141. /// @param[in] line The text making up the line.
  142. /// @param[in] line_begin The absolute index at the beginning of the line.
  143. void GetLineSelection(Core::WString& pre_selection, Core::WString& selection, Core::WString& post_selection, const Core::WString& line, int line_begin);
  144. struct Line
  145. {
  146. // The contents of the line (including the trailing endline, if that terminated the line).
  147. Core::WString content;
  148. // The length of the editable characters on the line (excluding any trailing endline).
  149. int content_length;
  150. // The number of extra characters at the end of the content that are not present in the actual value; in the
  151. // case of a soft return, this may be negative.
  152. int extra_characters;
  153. };
  154. ElementFormControl* parent;
  155. Core::ElementText* text_element;
  156. Core::ElementText* selected_text_element;
  157. Rocket::Core::Vector2f internal_dimensions;
  158. Rocket::Core::Vector2f scroll_offset;
  159. typedef std::vector< Line > LineList;
  160. LineList lines;
  161. int max_length;
  162. int edit_index;
  163. int absolute_cursor_index;
  164. int cursor_line_index;
  165. int cursor_character_index;
  166. // Selection. The start and end indices of the selection are in absolute coordinates.
  167. Core::Element* selection_element;
  168. int selection_anchor_index;
  169. int selection_begin_index;
  170. int selection_length;
  171. // The colour of the background of selected text.
  172. Rocket::Core::Colourb selection_colour;
  173. // The selection background.
  174. Core::Geometry selection_geometry;
  175. // Cursor visibility and timings.
  176. float cursor_timer;
  177. bool cursor_visible;
  178. bool keyboard_showed;
  179. /// Activate or deactivate keyboard (for touchscreen devices)
  180. /// @param[in] active True if need activate keyboard, false if need deactivate.
  181. void SetKeyboardActive(bool active);
  182. float last_update_time;
  183. // The cursor geometry.
  184. float ideal_cursor_position;
  185. Rocket::Core::Vector2f cursor_position;
  186. Rocket::Core::Vector2f cursor_size;
  187. Core::Geometry cursor_geometry;
  188. };
  189. }
  190. }
  191. #endif