FifeGUI 0.2.0
A C++ GUI library designed for games.
textbox.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_WIDGETS_TEXTBOX_HPP_
6#define INCLUDE_FIFECHAN_WIDGETS_TEXTBOX_HPP_
7
8#include <ctime>
9#include <string>
10#include <vector>
11
12#include "fifechan/listeners/keylistener.hpp"
13#include "fifechan/listeners/mouselistener.hpp"
14#include "fifechan/platform.hpp"
15#include "fifechan/widget.hpp"
16
17namespace fcn
18{
19 class Text;
20 class UTF8StringEditor;
21
27 class FIFEGUI_API TextBox : public Widget, public MouseListener, public KeyListener
28 {
29 public:
35 explicit TextBox(std::string const & text = "");
36
37 TextBox(TextBox const &) = delete;
38 TextBox& operator=(TextBox const &) = delete;
39 TextBox(TextBox&&) = delete;
40 TextBox& operator=(TextBox&&) = delete;
41
42 ~TextBox() override;
43
50 void setText(std::string const & text);
51
58 std::string getText() const;
59
67 std::string getTextRow(int row) const;
68
76 void setTextRow(int row, std::string const & text);
77
83 unsigned int getNumberOfRows() const;
84
91 unsigned int getCaretPosition() const;
92
99 void setCaretPosition(unsigned int position);
100
107 unsigned int getCaretRow() const;
108
115 void setCaretRow(int row);
116
123 unsigned int getCaretColumn() const;
124
131 void setCaretColumn(int column);
132
141 void setCaretRowColumn(int row, int column);
142
148 virtual void scrollToCaret();
149
156 bool isEditable() const;
157
163 void setEditable(bool editable);
164
170 virtual void addRow(std::string const & row);
171
180 bool isOpaque() const;
181
190 void setOpaque(bool opaque);
191
192 // Inherited from Widget
193
194 void draw(Graphics* graphics) override;
195
196 void fontChanged() override;
197
199
200 void resizeToContent(bool recursion) override;
204 void adjustSize() override;
205
206 // Inherited from KeyListener
207
208 void keyPressed(KeyEvent& keyEvent) override;
209
210 // Inherited from MouseListener
211
212 void mousePressed(MouseEvent& mouseEvent) override;
213
214 void mouseDragged(MouseEvent& mouseEvent) override;
215
216 protected:
226 void adjustSizeImpl();
227
232 void setCaretColumnUTF8(int column);
233
238 void setCaretRowUTF8(int row);
239
246 void setCaretRowColumnUTF8(int row, int column);
247
256 virtual void drawCaret(Graphics* graphics, int x, int y);
257
262
266 bool mEditable{true};
267
271 bool mOpaque{true};
272
277 };
278} // namespace fcn
279
280#endif // INCLUDE_FIFECHAN_WIDGETS_TEXTBOX_HPP_
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Represents a key event.
Definition keyevent.hpp:22
KeyListener(KeyListener const &)=default
Copy constructor.
Represents a mouse event.
MouseListener(MouseListener const &)=default
Copy constructor.
void fontChanged() override
Called when the font has changed.
Definition textbox.cpp:262
virtual void scrollToCaret()
Scrolls the text to the caret if the text box is in a scroll area.
Definition textbox.cpp:267
void setCaretColumnUTF8(int column)
Sets caret column (UTF-8 aware).
Definition textbox.cpp:298
virtual void addRow(std::string const &row)
Adds a row of text to the end of the text.
Definition textbox.cpp:282
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition textbox.cpp:82
std::string getTextRow(int row) const
Gets a certain row from the text.
Definition textbox.cpp:241
Text * mText
Holds the text of the text box.
Definition textbox.hpp:261
void setCaretRowColumn(int row, int column)
Sets the row and the column where the caret should be currently located.
Definition textbox.cpp:215
void draw(Graphics *graphics) override
Draws the widget.
Definition textbox.cpp:42
void setTextRow(int row, std::string const &text)
Sets the text of a certain row of the text.
Definition textbox.cpp:246
bool mEditable
True if the text box is editable, false otherwise.
Definition textbox.hpp:266
void adjustSizeImpl()
Adjusts the size of the button to fit the caption.
Definition textbox.cpp:199
unsigned int getCaretPosition() const
Gets the caret position in the text.
Definition textbox.cpp:210
virtual void drawCaret(Graphics *graphics, int x, int y)
Draws the caret.
Definition textbox.cpp:63
bool mOpaque
True if the text box is opaque, false otherwise.
Definition textbox.hpp:271
std::string getText() const
Gets the text of the text box.
Definition textbox.cpp:257
void setCaretColumn(int column)
Sets the column where the caret should be currently located.
Definition textbox.cpp:231
TextBox(std::string const &text="")
Constructor.
Definition textbox.cpp:21
unsigned int getCaretColumn() const
Gets the column where the caret is currently located.
Definition textbox.cpp:236
void setCaretRow(int row)
Sets the row where the caret should be currently located.
Definition textbox.cpp:221
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed on the widget area.
Definition textbox.cpp:69
void setEditable(bool editable)
Sets the text box to be editable or not.
Definition textbox.cpp:272
void adjustSize() override
Adjusts the text box's size to fit the text.
Definition textbox.cpp:194
void setCaretRowUTF8(int row)
Sets caret row (UTF-8 aware).
Definition textbox.cpp:304
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition textbox.cpp:77
unsigned int getCaretRow() const
Gets the row number where the caret is currently located.
Definition textbox.cpp:226
void resizeToContent(bool recursion) override
Resize this widget to fit its content.
Definition textbox.cpp:188
void setText(std::string const &text)
Sets the text of the text box.
Definition textbox.cpp:36
bool isOpaque() const
Checks if the text box is opaque.
Definition textbox.cpp:288
void setOpaque(bool opaque)
Sets the text box to be opaque or not.
Definition textbox.cpp:293
UTF8StringEditor * mStringEditor
UTF8StringEditor for UTF8 support.
Definition textbox.hpp:276
unsigned int getNumberOfRows() const
Gets the number of rows in the text.
Definition textbox.cpp:252
bool isEditable() const
Checks if the text box is editable.
Definition textbox.cpp:277
void setCaretPosition(unsigned int position)
Sets the position of the caret in the text.
Definition textbox.cpp:205
void setCaretRowColumnUTF8(int row, int column)
Sets the caret row and column (UTF-8 aware).
Definition textbox.cpp:316
Helper class for text manipulation within widgets.
Definition text.hpp:28
Utility for editing and handling UTF-8 encoded strings.
Widget()
Constructor.
Definition widget.cpp:36
void resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417