LineEdit.h 6.3 KB

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