Otclient  14/8/2020
uitextedit.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
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 
23 #ifndef UITEXTEDIT_H
24 #define UITEXTEDIT_H
25 
26 #include "uiwidget.h"
27 
28 // @bindclass
29 class UITextEdit : public UIWidget
30 {
31 public:
32  UITextEdit();
33 
34  void drawSelf(Fw::DrawPane drawPane);
35 
36 private:
37  void update(bool focusCursor = false);
38 
39 public:
40  void setCursorPos(int pos);
41  void setSelection(int start, int end);
42  void setCursorVisible(bool enable) { m_cursorVisible = enable; }
43  void setChangeCursorImage(bool enable) { m_changeCursorImage = enable; }
44  void setTextHidden(bool hidden);
45  void setValidCharacters(const std::string validCharacters) { m_validCharacters = validCharacters; }
46  void setShiftNavigation(bool enable) { m_shiftNavigation = enable; }
47  void setMultiline(bool enable) { m_multiline = enable; }
48  void setMaxLength(uint maxLength) { m_maxLength = maxLength; }
49  void setTextVirtualOffset(const Point& offset);
50  void setEditable(bool editable) { m_editable = editable; }
51  void setSelectable(bool selectable) { m_selectable = selectable; }
52  void setSelectionColor(const Color& color) { m_selectionColor = color; }
53  void setSelectionBackgroundColor(const Color& color) { m_selectionBackgroundColor = color; }
54  void setAutoScroll(bool autoScroll) { m_autoScroll = autoScroll; }
55 
56  void moveCursorHorizontally(bool right);
57  void moveCursorVertically(bool up);
58  void appendText(std::string text);
59  void appendCharacter(char c);
60  void removeCharacter(bool right);
61  void blinkCursor();
62 
63  void del(bool right = false);
64  void paste(const std::string& text);
65  std::string copy();
66  std::string cut();
67  void selectAll() { setSelection(0, m_text.length()); }
68  void clearSelection() { setSelection(0, 0); }
69 
70  void wrapText();
71  std::string getDisplayedText();
72  std::string getSelection();
73  int getTextPos(Point pos);
74  int getCursorPos() { return m_cursorPos; }
75  Point getTextVirtualOffset() { return m_textVirtualOffset; }
76  Size getTextVirtualSize() { return m_textVirtualSize; }
77  Size getTextTotalSize() { return m_textTotalSize; }
78  uint getMaxLength() { return m_maxLength; }
79  int getSelectionStart() { return m_selectionStart; }
80  int getSelectionEnd() { return m_selectionEnd; }
81  Color getSelectionColor() { return m_selectionColor; }
82  Color getSelectionBackgroundColor() { return m_selectionBackgroundColor; }
83  bool hasSelection() { return m_selectionEnd - m_selectionStart > 0; }
84  bool isCursorVisible() { return m_cursorVisible; }
85  bool isChangingCursorImage() { return m_changeCursorImage; }
86  bool isTextHidden() { return m_textHidden; }
87  bool isShiftNavigation() { return m_shiftNavigation; }
88  bool isMultiline() { return m_multiline; }
89  bool isEditable() { return m_editable; }
90  bool isSelectable() { return m_selectable; }
91  bool isAutoScrolling() { return m_autoScroll; }
92 
93 protected:
94  void updateText();
95 
96  virtual void onHoverChange(bool hovered);
97  virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
98  virtual void onGeometryChange(const Rect& oldRect, const Rect& newRect);
99  virtual void onFocusChange(bool focused, Fw::FocusReason reason);
100  virtual bool onKeyText(const std::string& keyText);
101  virtual bool onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks);
102  virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
103  virtual bool onMouseRelease(const Point& mousePos, Fw::MouseButton button);
104  virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
105  virtual bool onDoubleClick(const Point& mousePos);
106  virtual void onTextAreaUpdate(const Point& vitualOffset, const Size& visibleSize, const Size& totalSize);
107 
108 private:
109  void disableUpdates() { m_updatesEnabled = false; }
110  void enableUpdates() { m_updatesEnabled = true; }
111  void recacheGlyphs() { m_glyphsMustRecache = true; }
112 
113  Rect m_drawArea;
114  int m_cursorPos;
115  Point m_textVirtualOffset;
116  Size m_textVirtualSize;
117  Size m_textTotalSize;
118  ticks_t m_cursorTicks;
119  bool m_textHidden;
120  bool m_shiftNavigation;
121  bool m_multiline;
122  bool m_cursorInRange;
123  bool m_cursorVisible;
124  bool m_editable;
125  bool m_changeCursorImage;
126  std::string m_validCharacters;
127  uint m_maxLength;
128  bool m_updatesEnabled;
129  bool m_autoScroll;
130 
131  bool m_selectable;
132  int m_selectionReference;
133  int m_selectionStart;
134  int m_selectionEnd;
135 
136  Color m_selectionColor;
137  Color m_selectionBackgroundColor;
138 
139  std::vector<Rect> m_glyphsCoords;
140  std::vector<Rect> m_glyphsTexCoords;
141 
142  CoordsBuffer m_glyphsTextCoordsBuffer;
143  CoordsBuffer m_glyphsSelectCoordsBuffer;
144  bool m_glyphsMustRecache;
145 };
146 
147 #endif
UITextEdit::setCursorPos
void setCursorPos(int pos)
Definition: uitextedit.cpp:338
UITextEdit::getDisplayedText
std::string getDisplayedText()
Definition: uitextedit.cpp:565
UITextEdit::isSelectable
bool isSelectable()
Definition: uitextedit.h:90
UIWidget
Definition: uiwidget.h:47
UITextEdit::getSelection
std::string getSelection()
Definition: uitextedit.cpp:579
Color
Definition: color.h:32
UITextEdit::setValidCharacters
void setValidCharacters(const std::string validCharacters)
Definition: uitextedit.h:45
UITextEdit::isMultiline
bool isMultiline()
Definition: uitextedit.h:88
UITextEdit::isAutoScrolling
bool isAutoScrolling()
Definition: uitextedit.h:91
UITextEdit::wrapText
void wrapText()
Definition: uitextedit.cpp:498
TRect< int >
UITextEdit::onKeyPress
virtual bool onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks)
Definition: uitextedit.cpp:667
UITextEdit::setShiftNavigation
void setShiftNavigation(bool enable)
Definition: uitextedit.h:46
UITextEdit::onStyleApply
virtual void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uitextedit.cpp:611
UITextEdit::selectAll
void selectAll()
Definition: uitextedit.h:67
UITextEdit::setTextHidden
void setTextHidden(bool hidden)
Definition: uitextedit.cpp:370
UITextEdit::getTextVirtualSize
Size getTextVirtualSize()
Definition: uitextedit.h:76
UITextEdit::onFocusChange
virtual void onFocusChange(bool focused, Fw::FocusReason reason)
Definition: uitextedit.cpp:654
UITextEdit::copy
std::string copy()
Definition: uitextedit.cpp:481
UITextEdit::getSelectionColor
Color getSelectionColor()
Definition: uitextedit.h:81
UITextEdit::onMouseMove
virtual bool onMouseMove(const Point &mousePos, const Point &mouseMoved)
Definition: uitextedit.cpp:812
UITextEdit::drawSelf
void drawSelf(Fw::DrawPane drawPane)
Definition: uitextedit.cpp:58
UITextEdit::getTextPos
int getTextPos(Point pos)
Definition: uitextedit.cpp:526
ticks_t
int64 ticks_t
Definition: types.h:43
UITextEdit::paste
void paste(const std::string &text)
Definition: uitextedit.cpp:474
UITextEdit::onGeometryChange
virtual void onGeometryChange(const Rect &oldRect, const Rect &newRect)
Definition: uitextedit.cpp:648
UITextEdit::appendText
void appendText(std::string text)
Definition: uitextedit.cpp:382
UITextEdit
Definition: uitextedit.h:29
uiwidget.h
UIWidget::m_text
std::string m_text
Definition: uiwidget.h:481
Fw::FocusReason
FocusReason
Definition: const.h:221
UITextEdit::cut
std::string cut()
Definition: uitextedit.cpp:491
UITextEdit::setSelectable
void setSelectable(bool selectable)
Definition: uitextedit.h:51
UITextEdit::getTextTotalSize
Size getTextTotalSize()
Definition: uitextedit.h:77
UITextEdit::setCursorVisible
void setCursorVisible(bool enable)
Definition: uitextedit.h:42
UITextEdit::onMousePress
virtual bool onMousePress(const Point &mousePos, Fw::MouseButton button)
Definition: uitextedit.cpp:787
UITextEdit::setSelection
void setSelection(int start, int end)
Definition: uitextedit.cpp:354
UITextEdit::clearSelection
void clearSelection()
Definition: uitextedit.h:68
UITextEdit::setMultiline
void setMultiline(bool enable)
Definition: uitextedit.h:47
UITextEdit::getTextVirtualOffset
Point getTextVirtualOffset()
Definition: uitextedit.h:75
uint
unsigned int uint
Definition: types.h:31
UITextEdit::isChangingCursorImage
bool isChangingCursorImage()
Definition: uitextedit.h:85
UITextEdit::isShiftNavigation
bool isShiftNavigation()
Definition: uitextedit.h:87
UITextEdit::moveCursorVertically
void moveCursorVertically(bool up)
Definition: uitextedit.cpp:521
UITextEdit::hasSelection
bool hasSelection()
Definition: uitextedit.h:83
UITextEdit::setSelectionColor
void setSelectionColor(const Color &color)
Definition: uitextedit.h:52
UITextEdit::getSelectionEnd
int getSelectionEnd()
Definition: uitextedit.h:80
UITextEdit::appendCharacter
void appendCharacter(char c)
Definition: uitextedit.cpp:415
UITextEdit::getSelectionStart
int getSelectionStart()
Definition: uitextedit.h:79
UITextEdit::removeCharacter
void removeCharacter(bool right)
Definition: uitextedit.cpp:439
UITextEdit::onDoubleClick
virtual bool onDoubleClick(const Point &mousePos)
Definition: uitextedit.cpp:828
UITextEdit::setMaxLength
void setMaxLength(uint maxLength)
Definition: uitextedit.h:48
UITextEdit::setTextVirtualOffset
void setTextVirtualOffset(const Point &offset)
Definition: uitextedit.cpp:376
uchar
unsigned char uchar
Definition: types.h:29
Fw::DrawPane
DrawPane
Definition: const.h:285
UITextEdit::setEditable
void setEditable(bool editable)
Definition: uitextedit.h:50
stdext::shared_object_ptr< OTMLNode >
CoordsBuffer
Definition: coordsbuffer.h:29
UITextEdit::isTextHidden
bool isTextHidden()
Definition: uitextedit.h:86
UITextEdit::setChangeCursorImage
void setChangeCursorImage(bool enable)
Definition: uitextedit.h:43
UITextEdit::updateText
void updateText()
Definition: uitextedit.cpp:586
UITextEdit::getSelectionBackgroundColor
Color getSelectionBackgroundColor()
Definition: uitextedit.h:82
UITextEdit::del
void del(bool right=false)
Definition: uitextedit.cpp:461
UITextEdit::setAutoScroll
void setAutoScroll(bool autoScroll)
Definition: uitextedit.h:54
UITextEdit::setSelectionBackgroundColor
void setSelectionBackgroundColor(const Color &color)
Definition: uitextedit.h:53
UITextEdit::moveCursorHorizontally
void moveCursorHorizontally(bool right)
Definition: uitextedit.cpp:503
UIWidget::enable
void enable()
Definition: uiwidget.h:223
UITextEdit::onTextAreaUpdate
virtual void onTextAreaUpdate(const Point &vitualOffset, const Size &visibleSize, const Size &totalSize)
Definition: uitextedit.cpp:839
UITextEdit::onHoverChange
virtual void onHoverChange(bool hovered)
Definition: uitextedit.cpp:601
TPoint< int >
UITextEdit::isEditable
bool isEditable()
Definition: uitextedit.h:89
UITextEdit::onMouseRelease
virtual bool onMouseRelease(const Point &mousePos, Fw::MouseButton button)
Definition: uitextedit.cpp:807
TSize< int >
UITextEdit::getCursorPos
int getCursorPos()
Definition: uitextedit.h:74
UITextEdit::getMaxLength
uint getMaxLength()
Definition: uitextedit.h:78
Fw::MouseButton
MouseButton
Definition: const.h:246
UITextEdit::blinkCursor
void blinkCursor()
Definition: uitextedit.cpp:455
UITextEdit::isCursorVisible
bool isCursorVisible()
Definition: uitextedit.h:84
UITextEdit::UITextEdit
UITextEdit()
Definition: uitextedit.cpp:32
UITextEdit::onKeyText
virtual bool onKeyText(const std::string &keyText)
Definition: uitextedit.cpp:778