LineEdit.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 char c, int buttons, int qualifiers);
  57. /// React to gaining focus.
  58. virtual void OnFocus();
  59. /// React to losing focus.
  60. virtual void OnDefocus();
  61. /// %Set text.
  62. void SetText(const String& text);
  63. /// %Set cursor position.
  64. void SetCursorPosition(unsigned position);
  65. /// %Set cursor blink rate. 0 disables blinking.
  66. void SetCursorBlinkRate(float rate);
  67. /// %Set maximum text length. 0 for unlimited.
  68. void SetMaxLength(unsigned length);
  69. /// %Set echo character for password entry and such. 0 (default) shows the actual text.
  70. void SetEchoCharacter(char c);
  71. /// %Set whether can move cursor with arrows or mouse, default true.
  72. void SetCursorMovable(bool enable);
  73. /// %Set whether selections are allowed, default true.
  74. void SetTextSelectable(bool enable);
  75. /// %Set whether copy-paste operations are allowed, default true.
  76. void SetTextCopyable(bool enable);
  77. /// Return text.
  78. const String& GetText() const { return line_; }
  79. /// Return cursor position.
  80. unsigned GetCursorPosition() const { return cursorPosition_; }
  81. /// Return cursor blink rate.
  82. float GetCursorBlinkRate() const { return cursorBlinkRate_; }
  83. /// Return maximum text length.
  84. unsigned GetMaxLength() const { return maxLength_; }
  85. /// Return echo character.
  86. char GetEchoCharacter() const { return echoCharacter_; }
  87. /// Return whether can move cursor with arrows or mouse.
  88. bool IsCursorMovable() const { return cursorMovable_; }
  89. /// Return whether selections are allowed.
  90. bool IsTextSelectable() const { return textSelectable_; }
  91. /// Return whether copy-paste operations are allowed.
  92. bool IsTextCopyable() const { return textCopyable_; }
  93. /// Return text element.
  94. Text* GetTextElement() const { return text_; }
  95. /// Return cursor element.
  96. BorderImage* GetCursor() const { return cursor_; }
  97. protected:
  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 start edit cursor position.
  117. unsigned dragStartCursor_;
  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. char echoCharacter_;
  126. /// Cursor movable flag.
  127. bool cursorMovable_;
  128. /// Text selectable flag.
  129. bool textSelectable_;
  130. /// Copy-paste enable flag.
  131. bool textCopyable_;
  132. };