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