LineEdit.h 5.8 KB

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