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_TEXTBOX_H |
68 |
|
|
#define GUI_WIDGETS_TEXTBOX_H |
69 |
|
|
|
70 |
|
|
#include "gui/widgets/widget.h" |
71 |
|
|
|
72 |
|
|
#include "enums/simpletypes/opaque.h" |
73 |
|
|
|
74 |
|
|
#include "listeners/keylistener.h" |
75 |
|
|
#include "listeners/mouselistener.h" |
76 |
|
|
|
77 |
|
|
#include "localconsts.h" |
78 |
|
|
|
79 |
|
|
/** |
80 |
|
|
* A text box, meant to be used inside a scroll area. Same as the Guichan text |
81 |
|
|
* box except this one doesn't have a background or border, instead completely |
82 |
|
|
* relying on the scroll area. |
83 |
|
|
* |
84 |
|
|
* \ingroup GUI |
85 |
|
|
*/ |
86 |
|
|
class TextBox final : public Widget, |
87 |
|
|
public MouseListener, |
88 |
|
|
public KeyListener |
89 |
|
|
{ |
90 |
|
|
public: |
91 |
|
|
/** |
92 |
|
|
* Constructor. |
93 |
|
|
*/ |
94 |
|
|
explicit TextBox(const Widget2 *const widget); |
95 |
|
|
|
96 |
|
|
A_DELETE_COPY(TextBox) |
97 |
|
|
|
98 |
|
|
~TextBox() override final; |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* Sets the text after wrapping it to the current width of the widget. |
102 |
|
|
*/ |
103 |
|
|
void setTextWrapped(const std::string &text, const int minDimension); |
104 |
|
|
|
105 |
|
|
/** |
106 |
|
|
* Get the minimum text width for the text box. |
107 |
|
|
*/ |
108 |
|
|
int getMinWidth() const noexcept2 A_WARN_UNUSED |
109 |
|
2 |
{ return mMinWidth; } |
110 |
|
|
|
111 |
|
|
void keyPressed(KeyEvent& event) override final; |
112 |
|
|
|
113 |
|
|
void draw(Graphics *const graphics) override final A_NONNULL(2); |
114 |
|
|
|
115 |
|
|
void safeDraw(Graphics *const graphics) override final A_NONNULL(2); |
116 |
|
|
|
117 |
|
|
void setForegroundColor(const Color &color); |
118 |
|
|
|
119 |
|
|
void setForegroundColorAll(const Color &color1, |
120 |
|
|
const Color &color2); |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Sets the text of the text box. |
124 |
|
|
* |
125 |
|
|
* @param text The text of the text box. |
126 |
|
|
* @see getText |
127 |
|
|
*/ |
128 |
|
|
void setText(const std::string& text); |
129 |
|
|
|
130 |
|
|
/** |
131 |
|
|
* Gets the text of the text box. |
132 |
|
|
* |
133 |
|
|
* @return The text of the text box. |
134 |
|
|
* @see setText |
135 |
|
|
*/ |
136 |
|
|
std::string getText() const; |
137 |
|
|
|
138 |
|
|
/** |
139 |
|
|
* Gets a certain row from the text. |
140 |
|
|
* |
141 |
|
|
* @param row The number of the row to get from the text. |
142 |
|
|
* @return A row from the text of the text box. |
143 |
|
|
* @see setTextRow |
144 |
|
|
*/ |
145 |
|
|
const std::string& getTextRow(const int row) const |
146 |
|
|
{ return mTextRows[row]; } |
147 |
|
|
|
148 |
|
|
/** |
149 |
|
|
* Sets the text of a certain row of the text. |
150 |
|
|
* |
151 |
|
|
* @param row The number of the row to set in the text. |
152 |
|
|
* @param text The text to set in the given row number. |
153 |
|
|
* @see getTextRow |
154 |
|
|
*/ |
155 |
|
|
void setTextRow(const int row, const std::string& text); |
156 |
|
|
|
157 |
|
|
/** |
158 |
|
|
* Gets the number of rows in the text. |
159 |
|
|
* |
160 |
|
|
* @return The number of rows in the text. |
161 |
|
|
*/ |
162 |
|
|
unsigned int getNumberOfRows() const |
163 |
|
1 |
{ return CAST_S32(mTextRows.size()); } |
164 |
|
|
|
165 |
|
|
/** |
166 |
|
|
* Gets the caret position in the text. |
167 |
|
|
* |
168 |
|
|
* @return The caret position in the text. |
169 |
|
|
* @see setCaretPosition |
170 |
|
|
*/ |
171 |
|
|
unsigned int getCaretPosition() const; |
172 |
|
|
|
173 |
|
|
/** |
174 |
|
|
* Sets the position of the caret in the text. |
175 |
|
|
* |
176 |
|
|
* @param position the positon of the caret. |
177 |
|
|
* @see getCaretPosition |
178 |
|
|
*/ |
179 |
|
|
void setCaretPosition(unsigned int position); |
180 |
|
|
|
181 |
|
|
/** |
182 |
|
|
* Gets the row number where the caret is currently located. |
183 |
|
|
* |
184 |
|
|
* @return The row number where the caret is currently located. |
185 |
|
|
* @see setCaretRow |
186 |
|
|
*/ |
187 |
|
|
unsigned int getCaretRow() const |
188 |
|
|
{ return mCaretRow; } |
189 |
|
|
|
190 |
|
|
/** |
191 |
|
|
* Sets the row where the caret should be currently located. |
192 |
|
|
* |
193 |
|
|
* @param The row where the caret should be currently located. |
194 |
|
|
* @see getCaretRow |
195 |
|
|
*/ |
196 |
|
|
void setCaretRow(const int row); |
197 |
|
|
|
198 |
|
|
/** |
199 |
|
|
* Gets the column where the caret is currently located. |
200 |
|
|
* |
201 |
|
|
* @return The column where the caret is currently located. |
202 |
|
|
* @see setCaretColumn |
203 |
|
|
*/ |
204 |
|
|
unsigned int getCaretColumn() const; |
205 |
|
|
|
206 |
|
|
/** |
207 |
|
|
* Sets the column where the caret should be currently located. |
208 |
|
|
* |
209 |
|
|
* @param The column where the caret should be currently located. |
210 |
|
|
* @see getCaretColumn |
211 |
|
|
*/ |
212 |
|
|
void setCaretColumn(const int column); |
213 |
|
|
|
214 |
|
|
/** |
215 |
|
|
* Sets the row and the column where the caret should be curretly |
216 |
|
|
* located. |
217 |
|
|
* |
218 |
|
|
* @param row The row where the caret should be currently located. |
219 |
|
|
* @param column The column where the caret should be currently located. |
220 |
|
|
* @see getCaretRow, getCaretColumn |
221 |
|
|
*/ |
222 |
|
|
void setCaretRowColumn(const int row, const int column); |
223 |
|
|
|
224 |
|
|
/** |
225 |
|
|
* Scrolls the text to the caret if the text box is in a scroll area. |
226 |
|
|
* |
227 |
|
|
* @see ScrollArea |
228 |
|
|
*/ |
229 |
|
|
void scrollToCaret(); |
230 |
|
|
|
231 |
|
|
/** |
232 |
|
|
* Checks if the text box is editable. |
233 |
|
|
* |
234 |
|
|
* @return True it the text box is editable, false otherwise. |
235 |
|
|
* @see setEditable |
236 |
|
|
*/ |
237 |
|
|
bool isEditable() const |
238 |
|
|
{ return mEditable; } |
239 |
|
|
|
240 |
|
|
/** |
241 |
|
|
* Sets the text box to be editable or not. |
242 |
|
|
* |
243 |
|
|
* @param editable True if the text box should be editable, false otherwise. |
244 |
|
|
*/ |
245 |
|
|
void setEditable(const bool 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 |
|
26 |
{ 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 |