LineEdit.h 5.8 KB

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