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-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_TEXTBOX_H
67 #define GUI_WIDGETS_TEXTBOX_H
68 
69 #include "gui/widgets/widget.h"
70 
72 
73 #include "listeners/keylistener.h"
75 
76 #include "localconsts.h"
77 
85 class TextBox final : public Widget,
86  public MouseListener,
87  public KeyListener
88 {
89  public:
93  explicit TextBox(const Widget2 *const widget);
94 
96 
97  ~TextBox() override final;
98 
102  void setTextWrapped(const std::string &text, const int minDimension);
103 
108  { 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 
127  void setText(const std::string& text);
128 
135  std::string getText() const;
136 
144  const std::string& getTextRow(const int row) const
145  { return mTextRows[row]; }
146 
154  void setTextRow(const int row, const std::string& text);
155 
161  unsigned int getNumberOfRows() const
162  { return CAST_S32(mTextRows.size()); }
163 
170  unsigned int getCaretPosition() const;
171 
178  void setCaretPosition(unsigned int position);
179 
186  unsigned int getCaretRow() const
187  { return mCaretRow; }
188 
195  void setCaretRow(const int row);
196 
203  unsigned int getCaretColumn() const;
204 
211  void setCaretColumn(const int column);
212 
221  void setCaretRowColumn(const int row, const int column);
222 
228  void scrollToCaret();
229 
236  bool isEditable() const
237  { return mEditable; }
238 
244  void setEditable(const bool editable);
245 
251  void addRow(const std::string &row);
252 
261  bool isOpaque() const noexcept2
262  { return mOpaque == Opaque_true; }
263 
272  void setOpaque(const Opaque opaque) noexcept2
273  { mOpaque = opaque; }
274 
276  { adjustSize(); }
277 
278  void mousePressed(MouseEvent& event) override final;
279 
280  void mouseDragged(MouseEvent& event) override final;
281 
282  private:
291  void drawCaret(Graphics *const graphics,
292  const int x,
293  const int y) const A_NONNULL(2);
294 
298  void adjustSize();
299 
303  STD_VECTOR<std::string> mTextRows;
304 
309 
314 
316 
320  bool mEditable;
321 
326 };
327 
328 #endif // GUI_WIDGETS_TEXTBOX_H
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
int mCaretColumn
Definition: textbox.h:308
void setTextRow(const int row, const std::string &text)
Definition: textbox.cpp:510
unsigned int getNumberOfRows() const
Definition: textbox.h:161
void setForegroundColor(const Color &color)
Definition: textbox.cpp:481
void setTextWrapped(const std::string &text, const int minDimension)
Definition: textbox.cpp:108
std::vector< std::string > mTextRows
Definition: textbox.h:303
int mMinWidth
Definition: textbox.h:315
void setCaretRowColumn(const int row, const int column)
Definition: textbox.cpp:577
#define A_WARN_UNUSED
Definition: localconsts.h:152
#define final
Definition: localconsts.h:45
void mousePressed(MouseEvent &event)
Definition: textbox.cpp:601
void drawCaret(Graphics *const graphics, const int x, const int y) const
Definition: textbox.cpp:626
const std::string & getTextRow(const int row) const
Definition: textbox.h:144
#define A_NONNULL(...)
Definition: localconsts.h:159
unsigned int getCaretPosition() const
Definition: textbox.cpp:555
bool isOpaque() const
Definition: textbox.h:261
void addRow(const std::string &row)
Definition: textbox.cpp:595
unsigned int getCaretColumn() const
void setCaretPosition(unsigned int position)
Definition: textbox.cpp:520
void setEditable(const bool editable)
Definition: textbox.cpp:649
#define CAST_S32
Definition: cast.h:29
TextBox(const Widget2 *const widget)
Definition: textbox.cpp:78
void draw(Graphics *const graphics)
Definition: textbox.cpp:445
unsigned int getCaretRow() const
Definition: textbox.h:186
int getMinWidth() const
Definition: textbox.h:107
void scrollToCaret()
Definition: textbox.cpp:583
std::string getText() const
Definition: textbox.cpp:494
void adjustSize()
Definition: textbox.cpp:634
bool mEditable
Definition: textbox.h:320
int mCaretRow
Definition: textbox.h:313
void setOpaque(const Opaque opaque)
Definition: textbox.h:272
void fontChanged()
Definition: textbox.h:275
void setForegroundColorAll(const Color &color1, const Color &color2)
Definition: textbox.cpp:487
Definition: widget.h:97
const bool Opaque_true
Definition: opaque.h:29
Opaque mOpaque
Definition: textbox.h:325
~TextBox()
Definition: textbox.cpp:102
void safeDraw(Graphics *const graphics)
Definition: textbox.cpp:476
void mouseDragged(MouseEvent &event)
Definition: textbox.cpp:621
Definition: color.h:74
void setText(const std::string &text)
Definition: textbox.cpp:226
#define override
Definition: localconsts.h:46
void setCaretRow(const int row)
Definition: textbox.cpp:541
void keyPressed(KeyEvent &event)
Definition: textbox.cpp:257
#define noexcept2
Definition: localconsts.h:49
void setCaretColumn(const int column)
Definition: textbox.cpp:565
bool isEditable() const
Definition: textbox.h:236