GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/textfield.h Lines: 4 7 57.1 %
Date: 2017-11-29 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-2017  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
        explicit TextField(const Widget2 *restrict const widget,
98
                           const std::string &restrict text = "",
99
                           const LoseFocusOnTab loseFocusOnTab =
100
                           LoseFocusOnTab_true,
101
                           ActionListener *restrict
102
                           const listener = nullptr,
103
                           const std::string &restrict eventId = "",
104
                           const bool sendAlwaysEvents = false);
105
106
        A_DELETE_COPY(TextField)
107
108
        virtual ~TextField();
109
110
        /**
111
         * Draws the text field.
112
         */
113
        void draw(Graphics *const graphics) override A_NONNULL(2);
114
115
        void safeDraw(Graphics *const graphics) override A_NONNULL(2);
116
117
        /**
118
         * Update the alpha value to the graphic components.
119
         */
120
        void updateAlpha();
121
122
        /**
123
         * Draws the background and border.
124
         */
125
        void drawFrame(Graphics *const graphics) override final A_NONNULL(2);
126
127
        void safeDrawFrame(Graphics *const graphics) override final
128
                           A_NONNULL(2);
129
130
        /**
131
         * Determine whether the field should be numeric or not
132
         */
133
        void setNumeric(const bool numeric);
134
135
        /**
136
         * Set the range on the field if it is numeric
137
         */
138
        void setRange(const int min,
139
                      const int max)
140
        {
141
14
            mMinimum = min;
142
18
            mMaximum = max;
143
        }
144
145
        /**
146
         * Processes one keypress.
147
         */
148
        void keyPressed(KeyEvent &event) override;
149
150
        /**
151
         * Set the minimum value for a range
152
         */
153
        void setMinimum(const int min)
154
        { mMinimum = min; }
155
156
        /**
157
         * Set the maximum value for a range
158
         */
159
        void setMaximum(const int max)
160
        { mMaximum = max; }
161
162
        /**
163
         * Return the value for a numeric field
164
         */
165
        int getValue() const A_WARN_UNUSED;
166
167
        void setSendAlwaysEvents(const bool b) noexcept2
168
8
        { mSendAlwaysEvents = b; }
169
170
        void adjustSize();
171
172
        void adjustHeight();
173
174
        void setCaretPosition(unsigned int position);
175
176
        void mousePressed(MouseEvent &event) override final;
177
178
        void handlePaste();
179
180
        void handleCopy() const;
181
182
#ifdef ANDROID
183
        void focusGained(const Event &event) override final;
184
#else  // ANDROID
185
186
        void focusGained(const Event &event) override final A_CONST;
187
#endif  // ANDROID
188
189
        void focusLost(const Event &event) override A_CONST;
190
191
        void moveCaretBack();
192
193
        void moveCaretForward();
194
195
        void moveCaretWordBack();
196
197
        void moveCaretWordForward();
198
199
        void caretDelete();
200
201
        void caretDeleteToStart();
202
203
        void caretDeleteWord();
204
205
        void setAllowSpecialActions(const bool b)
206
4
        { mAllowSpecialActions = b; }
207
208
        std::string getTextBeforeCaret() const
209
        { return mText.substr(0, mCaretPosition); }
210
211
        /**
212
         * Sets the text of the text field.
213
         *
214
         * @param text The text of the text field.
215
         * @see getText
216
         */
217
        void setText(const std::string& text);
218
219
        /**
220
         * Gets the text of the text field.
221
         *
222
         * @return The text of the text field.
223
         * @see setText
224
         */
225
        const std::string& getText() const
226
        { return mText; }
227
228
        /**
229
         * Gets the caret position. As there is only one line of text
230
         * in a text field the position is the caret's x coordinate.
231
         *
232
         * @return The caret position.
233
         * @see setCaretPosition
234
         */
235
        unsigned int getCaretPosition() const
236
        { return mCaretPosition; }
237
238
        void mouseDragged(MouseEvent& event) override final;
239
240
        void widgetHidden(const Event &event) override final;
241
242
        void setParent(Widget *widget) override final;
243
244
        void setWindow(Widget *const widget) override final;
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