ManaPlus
textfield.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 /* _______ __ __ __ ______ __ __ _______ __ __
25  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
26  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
27  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
28  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
29  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
30  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
31  *
32  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
33  *
34  *
35  * Per Larsson a.k.a finalman
36  * Olof Naessén a.k.a jansem/yakslem
37  *
38  * Visit: http://guichan.sourceforge.net
39  *
40  * License: (BSD)
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  * notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  * notice, this list of conditions and the following disclaimer in
48  * the documentation and/or other materials provided with the
49  * distribution.
50  * 3. Neither the name of Guichan nor the names of its contributors may
51  * be used to endorse or promote products derived from this software
52  * without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
60  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 #ifndef GUI_WIDGETS_TEXTFIELD_H
68 #define GUI_WIDGETS_TEXTFIELD_H
69 
71 #include "listeners/keylistener.h"
74 
76 
77 #include "gui/fonts/textchunk.h"
78 
79 #include "gui/widgets/widget.h"
80 
81 #include "localconsts.h"
82 
88 class TextField notfinal : public Widget,
89  public FocusListener,
90  public KeyListener,
91  public MouseListener,
92  public WidgetListener
93 {
94  public:
98  TextField(const Widget2 *restrict const widget,
99  const std::string &restrict text,
100  const LoseFocusOnTab loseFocusOnTab,
102  const std::string &restrict eventId,
103  const bool sendAlwaysEvents);
104 
106 
107  ~TextField() override;
108 
112  void draw(Graphics *const graphics) override A_NONNULL(2);
113 
114  void safeDraw(Graphics *const graphics) override A_NONNULL(2);
115 
119  void updateAlpha();
120 
124  void drawFrame(Graphics *const graphics) override final A_NONNULL(2);
125 
126  void safeDrawFrame(Graphics *const graphics) override final
127  A_NONNULL(2);
128 
132  void setNumeric(const bool numeric);
133 
137  void setRange(const int min,
138  const int max)
139  {
140  mMinimum = min;
141  mMaximum = max;
142  }
143 
147  void keyPressed(KeyEvent &event) override;
148 
152  void setMinimum(const int min)
153  { mMinimum = min; }
154 
158  void setMaximum(const int max)
159  { mMaximum = max; }
160 
164  int getValue() const A_WARN_UNUSED;
165 
166  void setSendAlwaysEvents(const bool b) noexcept2
167  { mSendAlwaysEvents = b; }
168 
169  void adjustSize();
170 
171  void adjustHeight();
172 
173  void setCaretPosition(unsigned int position);
174 
175  void mousePressed(MouseEvent &event) override final;
176 
177  void handlePaste();
178 
179  void handleCopy() const;
180 
181 #ifdef ANDROID
182  void focusGained(const Event &event) override final;
183 #else // ANDROID
184 
185  void focusGained(const Event &event) override final;
186 #endif // ANDROID
187 
188  void focusLost(const Event &event) override;
189 
190  void moveCaretBack();
191 
192  void moveCaretForward();
193 
194  void moveCaretWordBack();
195 
196  void moveCaretWordForward();
197 
198  void caretDelete();
199 
200  void caretDeleteToStart();
201 
202  void caretDeleteWord();
203 
204  void setAllowSpecialActions(const bool b)
205  { mAllowSpecialActions = b; }
206 
207  std::string getTextBeforeCaret() const
208  { return mText.substr(0, mCaretPosition); }
209 
216  void setText(const std::string& text);
217 
224  const std::string& getText() const
225  { return mText; }
226 
234  unsigned int getCaretPosition() const
235  { return mCaretPosition; }
236 
237  void mouseDragged(MouseEvent& event) override final;
238 
239  void widgetHidden(const Event &event) override final;
240 
241  void setParent(Widget *widget) override final;
242 
243  void setWindow(Widget *const widget) override final;
244 
245  void signalEvent();
246 
247  protected:
248  void drawCaret(Graphics* graphics, int x) A_NONNULL(2);
249 
250  void fixScroll();
251 
252  void fontChanged() override;
253 
254  bool handleNormalKeys(const InputActionT action, bool &consumed);
255 
256  void handleCtrlKeys(const InputActionT action, bool &consumed);
257 
258  static Skin *mSkin;
259 
263  std::string mText;
264 
266 
270  unsigned int mCaretPosition;
271 
277  int mXScroll;
278 
280  static int instances;
281  static float mAlpha;
282  static ImageRect skin;
283  int mMinimum;
284  int mMaximum;
286  int mPadding;
287  bool mNumeric;
292 };
293 
294 #endif // GUI_WIDGETS_TEXTFIELD_H
Definition: color.h:76
Definition: event.h:79
Definition: skin.h:37
void safeDraw(Graphics *const graphics)
Definition: textfield.cpp:240
int mMinimum
Definition: textfield.h:283
void adjustHeight()
Definition: textfield.cpp:724
static ImageRect skin
Definition: textfield.h:282
void drawFrame(Graphics *const graphics)
Definition: textfield.cpp:245
time_t mLastEventPaste
Definition: textfield.h:285
void moveCaretForward()
Definition: textfield.cpp:605
void mousePressed(MouseEvent &event)
Definition: textfield.cpp:768
int mPadding
Definition: textfield.h:286
void setNumeric(const bool numeric)
Definition: textfield.cpp:269
void setMaximum(const int max)
Definition: textfield.h:158
void handleCopy() const
Definition: textfield.cpp:701
void handleCtrlKeys(const InputActionT action, bool &consumed)
Definition: textfield.cpp:496
bool mNumeric
Definition: textfield.h:287
const Color * mCaretColor
Definition: textfield.h:279
TextChunk mTextChunk
Definition: textfield.h:265
void keyPressed(KeyEvent &event)
Definition: textfield.cpp:301
void widgetHidden(const Event &event)
Definition: textfield.cpp:817
void drawCaret(Graphics *graphics, int x)
Definition: textfield.cpp:707
void setAllowSpecialActions(const bool b)
Definition: textfield.h:204
static Skin * mSkin
Definition: textfield.h:258
const std::string & getText() const
Definition: textfield.h:224
bool handleNormalKeys(const InputActionT action, bool &consumed)
Definition: textfield.cpp:409
LoseFocusOnTab mLoseFocusOnTab
Definition: textfield.h:288
TextField(const Widget2 *const widget, const std::string &text, const LoseFocusOnTab loseFocusOnTab, ActionListener *const listener, const std::string &eventId, const bool sendAlwaysEvents)
Definition: textfield.cpp:109
void draw(Graphics *const graphics)
Definition: textfield.cpp:210
bool mSendAlwaysEvents
Definition: textfield.h:290
void safeDrawFrame(Graphics *const graphics)
Definition: textfield.cpp:257
void setText(const std::string &text)
Definition: textfield.cpp:803
static float mAlpha
Definition: textfield.h:281
unsigned int mCaretPosition
Definition: textfield.h:270
void handlePaste()
Definition: textfield.cpp:629
void setParent(Widget *widget)
Definition: textfield.cpp:823
int mXScroll
Definition: textfield.h:277
void setMinimum(const int min)
Definition: textfield.h:152
void setWindow(Widget *const widget)
Definition: textfield.cpp:830
void fontChanged()
Definition: textfield.cpp:763
void caretDelete()
Definition: textfield.cpp:616
unsigned int getCaretPosition() const
Definition: textfield.h:234
void adjustSize()
Definition: textfield.cpp:716
void setRange(const int min, const int max)
Definition: textfield.h:137
std::string getTextBeforeCaret() const
Definition: textfield.h:207
int getValue() const
Definition: textfield.cpp:286
bool mAllowSpecialActions
Definition: textfield.h:289
void setCaretPosition(unsigned int position)
Definition: textfield.cpp:752
void moveCaretWordForward()
Definition: textfield.cpp:670
void caretDeleteWord()
Definition: textfield.cpp:690
void setSendAlwaysEvents(const bool b)
Definition: textfield.h:166
void fixScroll()
Definition: textfield.cpp:729
bool mTextChanged
Definition: textfield.h:291
void moveCaretBack()
Definition: textfield.cpp:595
void focusGained(const Event &event)
Definition: textfield.cpp:791
void mouseDragged(MouseEvent &event)
Definition: textfield.cpp:812
void updateAlpha()
Definition: textfield.cpp:194
void moveCaretWordBack()
Definition: textfield.cpp:651
void caretDeleteToStart()
Definition: textfield.cpp:641
std::string mText
Definition: textfield.h:263
void signalEvent()
Definition: textfield.cpp:843
void focusLost(const Event &event)
Definition: textfield.cpp:799
int mMaximum
Definition: textfield.h:284
static int instances
Definition: textfield.h:280
Definition: widget.h:99
InputAction ::T InputActionT
Definition: inputaction.h:717
#define restrict
Definition: localconsts.h:165
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define notfinal
Definition: localconsts.h:261
#define A_NONNULL(...)
Definition: localconsts.h:168
#define noexcept2
Definition: localconsts.h:50
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
bool LoseFocusOnTab