GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/textbox.h Lines: 4 6 66.7 %
Date: 2017-11-29 Branches: 0 0 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_TEXTBOX_H
67
#define GUI_WIDGETS_TEXTBOX_H
68
69
#include "gui/widgets/widget.h"
70
71
#include "enums/simpletypes/opaque.h"
72
73
#include "listeners/keylistener.h"
74
#include "listeners/mouselistener.h"
75
76
#include "localconsts.h"
77
78
/**
79
 * A text box, meant to be used inside a scroll area. Same as the Guichan text
80
 * box except this one doesn't have a background or border, instead completely
81
 * relying on the scroll area.
82
 *
83
 * \ingroup GUI
84
 */
85
class TextBox final : public Widget,
86
                      public MouseListener,
87
                      public KeyListener
88
{
89
    public:
90
        /**
91
         * Constructor.
92
         */
93
        explicit TextBox(const Widget2 *const widget);
94
95
        A_DELETE_COPY(TextBox)
96
97
        ~TextBox();
98
99
        /**
100
         * Sets the text after wrapping it to the current width of the widget.
101
         */
102
        void setTextWrapped(const std::string &text, const int minDimension);
103
104
        /**
105
         * Get the minimum text width for the text box.
106
         */
107
        int getMinWidth() const noexcept2 A_WARN_UNUSED
108
4
        { return mMinWidth; }
109
110
        void keyPressed(KeyEvent& event) override final;
111
112
        void draw(Graphics *const graphics) override final A_NONNULL(2);
113
114
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
115
116
        void setForegroundColor(const Color &color);
117
118
        void setForegroundColorAll(const Color &color1,
119
                                   const Color &color2);
120
121
        /**
122
         * Sets the text of the text box.
123
         *
124
         * @param text The text of the text box.
125
         * @see getText
126
         */
127
        void setText(const std::string& text);
128
129
        /**
130
         * Gets the text of the text box.
131
         *
132
         * @return The text of the text box.
133
         * @see setText
134
         */
135
        std::string getText() const;
136
137
        /**
138
         * Gets a certain row from the text.
139
         *
140
         * @param row The number of the row to get from the text.
141
         * @return A row from the text of the text box.
142
         * @see setTextRow
143
         */
144
        const std::string& getTextRow(const int row) const
145
        { return mTextRows[row]; }
146
147
        /**
148
         * Sets the text of a certain row of the text.
149
         *
150
         * @param row The number of the row to set in the text.
151
         * @param text The text to set in the given row number.
152
         * @see getTextRow
153
         */
154
        void setTextRow(const int row, const std::string& text);
155
156
        /**
157
         * Gets the number of rows in the text.
158
         *
159
         * @return The number of rows in the text.
160
         */
161
        unsigned int getNumberOfRows() const
162
2
        { return CAST_S32(mTextRows.size()); }
163
164
        /**
165
         * Gets the caret position in the text.
166
         *
167
         * @return The caret position in the text.
168
         * @see setCaretPosition
169
         */
170
        unsigned int getCaretPosition() const;
171
172
        /**
173
         * Sets the position of the caret in the text.
174
         *
175
         * @param position the positon of the caret.
176
         * @see getCaretPosition
177
         */
178
        void setCaretPosition(unsigned int position);
179
180
        /**
181
         * Gets the row number where the caret is currently located.
182
         *
183
         * @return The row number where the caret is currently located.
184
         * @see setCaretRow
185
         */
186
        unsigned int getCaretRow() const
187
        { return mCaretRow; }
188
189
        /**
190
         * Sets the row where the caret should be currently located.
191
         *
192
         * @param The row where the caret should be currently located.
193
         * @see getCaretRow
194
         */
195
        void setCaretRow(const int row);
196
197
        /**
198
         * Gets the column where the caret is currently located.
199
         *
200
         * @return The column where the caret is currently located.
201
         * @see setCaretColumn
202
         */
203
        unsigned int getCaretColumn() const;
204
205
        /**
206
         * Sets the column where the caret should be currently located.
207
         *
208
         * @param The column where the caret should be currently located.
209
         * @see getCaretColumn
210
         */
211
        void setCaretColumn(const int column);
212
213
        /**
214
         * Sets the row and the column where the caret should be curretly
215
         * located.
216
         *
217
         * @param row The row where the caret should be currently located.
218
         * @param column The column where the caret should be currently located.
219
         * @see getCaretRow, getCaretColumn
220
         */
221
        void setCaretRowColumn(const int row, const int column);
222
223
        /**
224
         * Scrolls the text to the caret if the text box is in a scroll area.
225
         *
226
         * @see ScrollArea
227
         */
228
        void scrollToCaret();
229
230
        /**
231
         * Checks if the text box is editable.
232
         *
233
         * @return True it the text box is editable, false otherwise.
234
         * @see setEditable
235
         */
236
        bool isEditable() const
237
        { return mEditable; }
238
239
        /**
240
         * Sets the text box to be editable or not.
241
         *
242
         * @param editable True if the text box should be editable, false otherwise.
243
         */
244
        void setEditable(const bool editable)
245
44
        { mEditable = editable; }
246
247
        /**
248
         * Adds a row of text to the end of the text.
249
         *
250
         * @param row The row to add.
251
         */
252
        void addRow(const std::string &row);
253
254
        /**
255
         * Checks if the text box is opaque. An opaque text box will draw
256
         * it's background and it's text. A non opaque text box only draw it's
257
         * text making it transparent.
258
         *
259
         * @return True if the text box is opaque, false otherwise.
260
         * @see setOpaque
261
         */
262
        bool isOpaque() const noexcept2
263
        { return mOpaque == Opaque_true; }
264
265
        /**
266
         * Sets the text box to be opaque or not. An opaque text box will draw
267
         * it's background and it's text. A non opaque text box only draw it's
268
         * text making it transparent.
269
         *
270
         * @param opaque True if the text box should be opaque, false otherwise.
271
         * @see isOpaque
272
         */
273
        void setOpaque(const Opaque opaque) noexcept2
274
52
        { mOpaque = opaque; }
275
276
        void fontChanged() override final
277
        { adjustSize(); }
278
279
        void mousePressed(MouseEvent& event) override final;
280
281
        void mouseDragged(MouseEvent& event) override final;
282
283
    private:
284
        /**
285
         * Draws the caret. Overloaded this method if you want to
286
         * change the style of the caret.
287
         *
288
         * @param graphics a Graphics object to draw with.
289
         * @param x the x position.
290
         * @param y the y position.
291
         */
292
        void drawCaret(Graphics *const graphics,
293
                       const int x,
294
                       const int y) const A_NONNULL(2);
295
296
        /**
297
         * Adjusts the text box's size to fit the text.
298
         */
299
        void adjustSize();
300
301
        /**
302
         * Holds all the rows of the text.
303
         */
304
        STD_VECTOR<std::string> mTextRows;
305
306
        /**
307
         * Holds the current column of the caret.
308
         */
309
        int mCaretColumn;
310
311
        /**
312
         * Holds the current row of the caret.
313
         */
314
        int mCaretRow;
315
316
        int mMinWidth;
317
318
        /**
319
         * True if the text box is editable, false otherwise.
320
         */
321
        bool mEditable;
322
323
        /**
324
         * True if the text box is editable, false otherwise.
325
         */
326
        Opaque mOpaque;
327
};
328
329
#endif  // GUI_WIDGETS_TEXTBOX_H