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