LineEdit.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Copyright (c) 2008-2017 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. LineEdit(Context* context);
  35. /// Destruct.
  36. virtual ~LineEdit();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Apply attribute changes that can not be applied immediately.
  40. virtual void ApplyAttributes();
  41. /// Perform UI element update.
  42. virtual void Update(float timeStep);
  43. /// React to mouse click begin.
  44. virtual void OnClickBegin
  45. (const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor);
  46. /// React to mouse doubleclick.
  47. virtual void OnDoubleClick
  48. (const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor);
  49. /// React to mouse drag begin.
  50. virtual void
  51. OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
  52. /// React to mouse drag motion.
  53. virtual void OnDragMove
  54. (const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, int buttons, int qualifiers,
  55. Cursor* cursor);
  56. /// React to drag and drop test. Return true to signal that the drop is acceptable.
  57. virtual bool OnDragDropTest(UIElement* source);
  58. /// React to drag and drop finish. Return true to signal that the drop was accepted.
  59. virtual bool OnDragDropFinish(UIElement* source);
  60. /// React to a key press.
  61. virtual void OnKey(int key, int buttons, int qualifiers);
  62. /// React to text input event.
  63. virtual void OnTextInput(const String& text);
  64. /// Set text.
  65. void SetText(const String& text);
  66. /// Set cursor position.
  67. void SetCursorPosition(unsigned position);
  68. /// Set cursor blink rate. 0 disables blinking.
  69. void SetCursorBlinkRate(float rate);
  70. /// Set maximum text length. 0 for unlimited.
  71. void SetMaxLength(unsigned length);
  72. /// Set echo character for password entry and such. 0 (default) shows the actual text.
  73. void SetEchoCharacter(unsigned c);
  74. /// Set whether can move cursor with arrows or mouse, default true.
  75. void SetCursorMovable(bool enable);
  76. /// Set whether selections are allowed, default true.
  77. void SetTextSelectable(bool enable);
  78. /// Set whether copy-paste operations are allowed, default true.
  79. void SetTextCopyable(bool enable);
  80. /// Return text.
  81. const String& GetText() const { return line_; }
  82. /// Return cursor position.
  83. unsigned GetCursorPosition() const { return cursorPosition_; }
  84. /// Return cursor blink rate.
  85. float GetCursorBlinkRate() const { return cursorBlinkRate_; }
  86. /// Return maximum text length.
  87. unsigned GetMaxLength() const { return maxLength_; }
  88. /// Return echo character.
  89. unsigned GetEchoCharacter() const { return echoCharacter_; }
  90. /// Return whether can move cursor with arrows or mouse.
  91. bool IsCursorMovable() const { return cursorMovable_; }
  92. /// Return whether selections are allowed.
  93. bool IsTextSelectable() const { return textSelectable_; }
  94. /// Return whether copy-paste operations are allowed.
  95. bool IsTextCopyable() const { return textCopyable_; }
  96. /// Return text element.
  97. Text* GetTextElement() const { return text_; }
  98. /// Return cursor element.
  99. BorderImage* GetCursor() const { return cursor_; }
  100. protected:
  101. /// Filter implicit attributes in serialization process.
  102. virtual bool FilterImplicitAttributes(XMLElement& dest) const;
  103. /// Update displayed text.
  104. void UpdateText();
  105. /// Update cursor position and restart cursor blinking.
  106. void UpdateCursor();
  107. /// Return char index corresponding to position within element, or M_MAX_UNSIGNED if not found.
  108. unsigned GetCharIndex(const IntVector2& position);
  109. /// Text element.
  110. SharedPtr<Text> text_;
  111. /// Cursor element.
  112. SharedPtr<BorderImage> cursor_;
  113. /// Text line.
  114. String line_;
  115. /// Last used text font.
  116. Font* lastFont_;
  117. /// Last used text size.
  118. int lastFontSize_;
  119. /// Text edit cursor position.
  120. unsigned cursorPosition_;
  121. /// Drag begin cursor position.
  122. unsigned dragBeginCursor_;
  123. /// Cursor blink rate.
  124. float cursorBlinkRate_;
  125. /// Cursor blink timer.
  126. float cursorBlinkTimer_;
  127. /// Maximum text length.
  128. unsigned maxLength_;
  129. /// Echo character.
  130. unsigned echoCharacter_;
  131. /// Cursor movable flag.
  132. bool cursorMovable_;
  133. /// Text selectable flag.
  134. bool textSelectable_;
  135. /// Copy-paste enable flag.
  136. bool textCopyable_;
  137. private:
  138. /// Handle being focused.
  139. void HandleFocused(StringHash eventType, VariantMap& eventData);
  140. /// Handle being defocused.
  141. void HandleDefocused(StringHash eventType, VariantMap& eventData);
  142. /// Handle the element layout having been updated.
  143. void HandleLayoutUpdated(StringHash eventType, VariantMap& eventData);
  144. };
  145. }