LineEdit.h 6.2 KB

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