LineEdit.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../UI/BorderImage.h"
  24. namespace Urho3D
  25. {
  26. class Font;
  27. class Text;
  28. /// Single-line text editor %UI element.
  29. class URHO3D_API LineEdit : public BorderImage
  30. {
  31. URHO3D_OBJECT(LineEdit, BorderImage);
  32. public:
  33. /// Construct.
  34. explicit LineEdit(Context* context);
  35. /// Destruct.
  36. ~LineEdit() override;
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Apply attribute changes that can not be applied immediately.
  40. void ApplyAttributes() override;
  41. /// Perform UI element update.
  42. void Update(float timeStep) override;
  43. /// React to mouse click begin.
  44. void OnClickBegin
  45. (const IntVector2& position, const IntVector2& screenPosition, MouseButton button, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor) override;
  46. /// React to mouse doubleclick.
  47. void OnDoubleClick
  48. (const IntVector2& position, const IntVector2& screenPosition, MouseButton button, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor) override;
  49. /// React to mouse drag begin.
  50. void
  51. OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor) override;
  52. /// React to mouse drag motion.
  53. void OnDragMove
  54. (const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, MouseButtonFlags buttons, QualifierFlags qualifiers,
  55. Cursor* cursor) override;
  56. /// React to drag and drop test. Return true to signal that the drop is acceptable.
  57. bool OnDragDropTest(UIElement* source) override;
  58. /// React to drag and drop finish. Return true to signal that the drop was accepted.
  59. bool OnDragDropFinish(UIElement* source) override;
  60. /// React to a key press.
  61. void OnKey(Key key, MouseButtonFlags buttons, QualifierFlags qualifiers) override;
  62. /// React to text input event.
  63. void OnTextInput(const String& text) override;
  64. /// Set text.
  65. /// @property
  66. void SetText(const String& text);
  67. /// Set cursor position.
  68. /// @property
  69. void SetCursorPosition(unsigned position);
  70. /// Set cursor blink rate. 0 disables blinking.
  71. /// @property
  72. void SetCursorBlinkRate(float rate);
  73. /// Set maximum text length. 0 for unlimited.
  74. /// @property
  75. void SetMaxLength(unsigned length);
  76. /// Set echo character for password entry and such. 0 (default) shows the actual text.
  77. /// @property
  78. void SetEchoCharacter(unsigned c);
  79. /// Set whether can move cursor with arrows or mouse, default true.
  80. /// @property
  81. void SetCursorMovable(bool enable);
  82. /// Set whether selections are allowed, default true.
  83. /// @property
  84. void SetTextSelectable(bool enable);
  85. /// Set whether copy-paste operations are allowed, default true.
  86. /// @property
  87. void SetTextCopyable(bool enable);
  88. /// Return text.
  89. /// @property
  90. const String& GetText() const { return line_; }
  91. /// Return cursor position.
  92. /// @property
  93. unsigned GetCursorPosition() const { return cursorPosition_; }
  94. /// Return cursor blink rate.
  95. /// @property
  96. float GetCursorBlinkRate() const { return cursorBlinkRate_; }
  97. /// Return maximum text length.
  98. /// @property
  99. unsigned GetMaxLength() const { return maxLength_; }
  100. /// Return echo character.
  101. /// @property
  102. unsigned GetEchoCharacter() const { return echoCharacter_; }
  103. /// Return whether can move cursor with arrows or mouse.
  104. /// @property
  105. bool IsCursorMovable() const { return cursorMovable_; }
  106. /// Return whether selections are allowed.
  107. /// @property
  108. bool IsTextSelectable() const { return textSelectable_; }
  109. /// Return whether copy-paste operations are allowed.
  110. /// @property
  111. bool IsTextCopyable() const { return textCopyable_; }
  112. /// Return text element.
  113. /// @property
  114. Text* GetTextElement() const { return text_; }
  115. /// Return cursor element.
  116. /// @property
  117. BorderImage* GetCursor() const { return cursor_; }
  118. protected:
  119. /// Filter implicit attributes in serialization process.
  120. bool FilterImplicitAttributes(XMLElement& dest) const override;
  121. /// Update displayed text.
  122. void UpdateText();
  123. /// Update cursor position and restart cursor blinking.
  124. void UpdateCursor();
  125. /// Return char index corresponding to position within element, or M_MAX_UNSIGNED if not found.
  126. unsigned GetCharIndex(const IntVector2& position);
  127. /// Text element.
  128. SharedPtr<Text> text_;
  129. /// Cursor element.
  130. SharedPtr<BorderImage> cursor_;
  131. /// Text line.
  132. String line_;
  133. /// Last used text font.
  134. Font* lastFont_;
  135. /// Last used text size.
  136. int lastFontSize_;
  137. /// Text edit cursor position.
  138. unsigned cursorPosition_;
  139. /// Drag begin cursor position.
  140. unsigned dragBeginCursor_;
  141. /// Cursor blink rate.
  142. float cursorBlinkRate_;
  143. /// Cursor blink timer.
  144. float cursorBlinkTimer_;
  145. /// Maximum text length.
  146. unsigned maxLength_;
  147. /// Echo character.
  148. unsigned echoCharacter_;
  149. /// Cursor movable flag.
  150. bool cursorMovable_;
  151. /// Text selectable flag.
  152. bool textSelectable_;
  153. /// Copy-paste enable flag.
  154. bool textCopyable_;
  155. private:
  156. /// Handle being focused.
  157. void HandleFocused(StringHash eventType, VariantMap& eventData);
  158. /// Handle being defocused.
  159. void HandleDefocused(StringHash eventType, VariantMap& eventData);
  160. /// Handle the element layout having been updated.
  161. void HandleLayoutUpdated(StringHash eventType, VariantMap& eventData);
  162. };
  163. }