LineEdit.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. namespace Urho3D
  26. {
  27. class Font;
  28. class Text;
  29. /// Single-line text editor %UI element.
  30. class LineEdit : public BorderImage
  31. {
  32. OBJECT(LineEdit);
  33. using UIElement::SetStyle;
  34. public:
  35. /// Construct.
  36. LineEdit(Context* context);
  37. /// Destruct.
  38. virtual ~LineEdit();
  39. /// Register object factory.
  40. static void RegisterObject(Context* context);
  41. /// Set UI element style from XML data.
  42. virtual void SetStyle(const XMLElement& element);
  43. /// Perform UI element update.
  44. virtual void Update(float timeStep);
  45. /// React to mouse click.
  46. virtual void OnClick(const IntVector2& position, const IntVector2& screenPosition, 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, 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 a key press translated to a character.
  58. virtual void OnChar(unsigned c, 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(char 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. char 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. /// Update displayed text.
  97. void UpdateText();
  98. /// Update cursor position and restart cursor blinking.
  99. void UpdateCursor();
  100. /// Return char index corresponding to position within element, or M_MAX_UNSIGNED if not found.
  101. unsigned GetCharIndex(const IntVector2& position);
  102. /// Text element.
  103. SharedPtr<Text> text_;
  104. /// Cursor element.
  105. SharedPtr<BorderImage> cursor_;
  106. /// Text line.
  107. String line_;
  108. /// Last used text font.
  109. Font* lastFont_;
  110. /// Last used text size.
  111. int lastFontSize_;
  112. /// Text edit cursor position.
  113. unsigned cursorPosition_;
  114. /// Drag begin cursor position.
  115. unsigned dragBeginCursor_;
  116. /// Cursor blink rate.
  117. float cursorBlinkRate_;
  118. /// Cursor blink timer.
  119. float cursorBlinkTimer_;
  120. /// Maximum text length.
  121. unsigned maxLength_;
  122. /// Echo character.
  123. char echoCharacter_;
  124. /// Cursor movable flag.
  125. bool cursorMovable_;
  126. /// Text selectable flag.
  127. bool textSelectable_;
  128. /// Copy-paste enable flag.
  129. bool textCopyable_;
  130. private:
  131. /// Handle being focused.
  132. void HandleFocused(StringHash eventType, VariantMap& eventData);
  133. /// Handle being defocused.
  134. void HandleDefocused(StringHash eventType, VariantMap& eventData);
  135. };
  136. }