ManaPlus
textbox.h
Go to the documentation of this file.
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 
73 
74 #include "listeners/keylistener.h"
76 
77 #include "localconsts.h"
78 
86 class TextBox final : public Widget,
87  public MouseListener,
88  public KeyListener
89 {
90  public:
94  explicit TextBox(const Widget2 *const widget);
95 
97 
98  ~TextBox() override final;
99 
103  void setTextWrapped(const std::string &text, const int minDimension);
104 
109  { 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 
128  void setText(const std::string& text);
129 
136  std::string getText() const;
137 
145  const std::string& getTextRow(const int row) const
146  { return mTextRows[row]; }
147 
155  void setTextRow(const int row, const std::string& text);
156 
162  unsigned int getNumberOfRows() const
163  { return CAST_S32(mTextRows.size()); }
164 
171  unsigned int getCaretPosition() const;
172 
179  void setCaretPosition(unsigned int position);
180 
187  unsigned int getCaretRow() const
188  { return mCaretRow; }
189 
196  void setCaretRow(const int row);
197 
204  unsigned int getCaretColumn() const;
205 
212  void setCaretColumn(const int column);
213 
222  void setCaretRowColumn(const int row, const int column);
223 
229  void scrollToCaret();
230 
237  bool isEditable() const
238  { return mEditable; }
239 
245  void setEditable(const bool editable);
246 
252  void addRow(const std::string &row);
253 
262  bool isOpaque() const noexcept2
263  { return mOpaque == Opaque_true; }
264 
273  void setOpaque(const Opaque opaque) noexcept2
274  { mOpaque = opaque; }
275 
277  { adjustSize(); }
278 
279  void mousePressed(MouseEvent& event) override final;
280 
281  void mouseDragged(MouseEvent& event) override final;
282 
283  private:
292  void drawCaret(Graphics *const graphics,
293  const int x,
294  const int y) const A_NONNULL(2);
295 
299  void adjustSize();
300 
304  STD_VECTOR<std::string> mTextRows;
305 
310 
315 
317 
321  bool mEditable;
322 
327 };
328 
329 #endif // GUI_WIDGETS_TEXTBOX_H
#define CAST_S32
Definition: cast.h:30
Definition: color.h:76
int mMinWidth
Definition: textbox.h:316
void safeDraw(Graphics *const graphics)
Definition: textbox.cpp:477
void mouseDragged(MouseEvent &event)
Definition: textbox.cpp:622
void fontChanged()
Definition: textbox.h:276
unsigned int getCaretRow() const
Definition: textbox.h:187
std::vector< std::string > mTextRows
Definition: textbox.h:304
bool isOpaque() const
Definition: textbox.h:262
void setCaretPosition(unsigned int position)
Definition: textbox.cpp:521
void setEditable(const bool editable)
Definition: textbox.cpp:650
std::string getText() const
Definition: textbox.cpp:495
void setCaretColumn(const int column)
Definition: textbox.cpp:566
void setCaretRowColumn(const int row, const int column)
Definition: textbox.cpp:578
void setOpaque(const Opaque opaque)
Definition: textbox.h:273
void setTextRow(const int row, const std::string &text)
Definition: textbox.cpp:511
unsigned int getCaretColumn() const
void addRow(const std::string &row)
Definition: textbox.cpp:596
void adjustSize()
Definition: textbox.cpp:635
void setCaretRow(const int row)
Definition: textbox.cpp:542
void keyPressed(KeyEvent &event)
Definition: textbox.cpp:258
void setForegroundColor(const Color &color)
Definition: textbox.cpp:482
void mousePressed(MouseEvent &event)
Definition: textbox.cpp:602
void scrollToCaret()
Definition: textbox.cpp:584
void drawCaret(Graphics *const graphics, const int x, const int y) const
Definition: textbox.cpp:627
unsigned int getNumberOfRows() const
Definition: textbox.h:162
void setForegroundColorAll(const Color &color1, const Color &color2)
Definition: textbox.cpp:488
void draw(Graphics *const graphics)
Definition: textbox.cpp:446
int mCaretRow
Definition: textbox.h:314
unsigned int getCaretPosition() const
Definition: textbox.cpp:556
void setText(const std::string &text)
Definition: textbox.cpp:227
Opaque mOpaque
Definition: textbox.h:326
const std::string & getTextRow(const int row) const
Definition: textbox.h:145
~TextBox()
Definition: textbox.cpp:103
int getMinWidth() const
Definition: textbox.h:108
void setTextWrapped(const std::string &text, const int minDimension)
Definition: textbox.cpp:109
bool mEditable
Definition: textbox.h:321
bool isEditable() const
Definition: textbox.h:237
TextBox(const Widget2 *const widget)
Definition: textbox.cpp:79
int mCaretColumn
Definition: textbox.h:309
Definition: widget.h:99
#define override
Definition: localconsts.h:47
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define A_NONNULL(...)
Definition: localconsts.h:168
#define noexcept2
Definition: localconsts.h:50
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
const bool Opaque_true
Definition: opaque.h:30
bool Opaque
Definition: opaque.h:30