GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/textfield.h Lines: 4 7 57.1 %
Date: 2021-03-17 Branches: 0 4 0.0 %

Line Branch Exec Source
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
70
#include "listeners/focuslistener.h"
71
#include "listeners/keylistener.h"
72
#include "listeners/mouselistener.h"
73
#include "listeners/widgetlistener.h"
74
75
#include "enums/simpletypes/losefocusontab.h"
76
77
#include "gui/fonts/textchunk.h"
78
79
#include "gui/widgets/widget.h"
80
81
#include "localconsts.h"
82
83
/**
84
 * A text field.
85
 *
86
 * \ingroup GUI
87
 */
88
class TextField notfinal : public Widget,
89
                           public FocusListener,
90
                           public KeyListener,
91
                           public MouseListener,
92
                           public WidgetListener
93
{
94
    public:
95
        /**
96
         * Constructor, initializes the text field with the given string.
97
         */
98
        TextField(const Widget2 *restrict const widget,
99
                  const std::string &restrict text,
100
                  const LoseFocusOnTab loseFocusOnTab,
101
                  ActionListener *restrict const listener,
102
                  const std::string &restrict eventId,
103
                  const bool sendAlwaysEvents);
104
105
        A_DELETE_COPY(TextField)
106
107
        ~TextField() override;
108
109
        /**
110
         * Draws the text field.
111
         */
112
        void draw(Graphics *const graphics) override A_NONNULL(2);
113
114
        void safeDraw(Graphics *const graphics) override A_NONNULL(2);
115
116
        /**
117
         * Update the alpha value to the graphic components.
118
         */
119
        void updateAlpha();
120
121
        /**
122
         * Draws the background and border.
123
         */
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
129
        /**
130
         * Determine whether the field should be numeric or not
131
         */
132
        void setNumeric(const bool numeric);
133
134
        /**
135
         * Set the range on the field if it is numeric
136
         */
137
        void setRange(const int min,
138
                      const int max)
139
        {
140
9
            mMinimum = min;
141
9
            mMaximum = max;
142
        }
143
144
        /**
145
         * Processes one keypress.
146
         */
147
        void keyPressed(KeyEvent &event) override;
148
149
        /**
150
         * Set the minimum value for a range
151
         */
152
        void setMinimum(const int min)
153
        { mMinimum = min; }
154
155
        /**
156
         * Set the maximum value for a range
157
         */
158
        void setMaximum(const int max)
159
        { mMaximum = max; }
160
161
        /**
162
         * Return the value for a numeric field
163
         */
164
        int getValue() const A_WARN_UNUSED;
165
166
        void setSendAlwaysEvents(const bool b) noexcept2
167
4
        { 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
2
        { mAllowSpecialActions = b; }
206
207
        std::string getTextBeforeCaret() const
208
        { return mText.substr(0, mCaretPosition); }
209
210
        /**
211
         * Sets the text of the text field.
212
         *
213
         * @param text The text of the text field.
214
         * @see getText
215
         */
216
        void setText(const std::string& text);
217
218
        /**
219
         * Gets the text of the text field.
220
         *
221
         * @return The text of the text field.
222
         * @see setText
223
         */
224
        const std::string& getText() const
225
        { return mText; }
226
227
        /**
228
         * Gets the caret position. As there is only one line of text
229
         * in a text field the position is the caret's x coordinate.
230
         *
231
         * @return The caret position.
232
         * @see setCaretPosition
233
         */
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
260
        /**
261
         * Holds the text of the text box.
262
         */
263
        std::string mText;
264
265
        TextChunk mTextChunk;
266
267
        /**
268
         * Holds the caret position.
269
         */
270
        unsigned int mCaretPosition;
271
272
        /**
273
         * Holds the amount scrolled in x. If a user types more characters than
274
         * the text field can display, due to the text field being to small, the
275
         * text needs to scroll in order to show the last type character.
276
         */
277
        int mXScroll;
278
279
        const Color *mCaretColor;
280
        static int instances;
281
        static float mAlpha;
282
        static ImageRect skin;
283
        int mMinimum;
284
        int mMaximum;
285
        time_t mLastEventPaste;
286
        int mPadding;
287
        bool mNumeric;
288
        LoseFocusOnTab mLoseFocusOnTab;
289
        bool mAllowSpecialActions;
290
        bool mSendAlwaysEvents;
291
        bool mTextChanged;
292
};
293
294
#endif  // GUI_WIDGETS_TEXTFIELD_H