FifeGUI 0.3.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// Standard library includes
9#include <ctime>
10#include <string>
11#include <vector>
12
13// Platform config include
14#include "fifechan/platform.hpp"
15
16// Project headers (subdirs before local)
17#include "fifechan/listeners/keylistener.hpp"
18#include "fifechan/listeners/mouselistener.hpp"
19#include "fifechan/widget.hpp"
20
21namespace fcn
22{
23 class Text;
24 class UTF8StringEditor;
25
31 class FIFEGUI_API TextBox : public Widget, public MouseListener, public KeyListener
32 {
33 public:
39 explicit TextBox(std::string const & text = "");
40
41 TextBox(TextBox const &) = delete;
42 TextBox& operator=(TextBox const &) = delete;
43 TextBox(TextBox&&) = delete;
44 TextBox& operator=(TextBox&&) = delete;
45
46 ~TextBox() override;
47
54 void setText(std::string const & text);
55
62 std::string getText() const;
63
71 std::string getTextRow(int row) const;
72
80 void setTextRow(int row, std::string const & text);
81
87 unsigned int getNumberOfRows() const;
88
95 unsigned int getCaretPosition() const;
96
103 void setCaretPosition(unsigned int position);
104
111 unsigned int getCaretRow() const;
112
119 void setCaretRow(int row);
120
127 unsigned int getCaretColumn() const;
128
135 void setCaretColumn(int column);
136
145 void setCaretRowColumn(int row, int column);
146
152 virtual void scrollToCaret();
153
160 bool isEditable() const;
161
167 void setEditable(bool editable);
168
174 virtual void addRow(std::string const & row);
175
185 bool isOpaque() const;
186
196 void setOpaque(bool opaque);
197
198 // Inherited from Widget
199
200 void draw(Graphics* graphics) override;
201
202 void fontChanged() override;
203
205
206 void resizeToContent(bool recursion = true) override;
210 void adjustSize() override;
211
212 // Inherited from KeyListener
213
214 void keyPressed(KeyEvent& keyEvent) override;
215
216 void textInput(TextInputEvent& event) override;
217
218 // Inherited from MouseListener
219
220 void mousePressed(MouseEvent& mouseEvent) override;
221
222 void mouseDragged(MouseEvent& mouseEvent) override;
223
224 protected:
234 void adjustSizeImpl();
235
241 void setCaretColumnUTF8(int column);
242
248 void setCaretRowUTF8(int row);
249
256 void setCaretRowColumnUTF8(int row, int column);
257
267 virtual void drawCaret(Graphics* graphics, int x, int y);
268
273
277 bool mEditable{true};
278
282 bool mOpaque{true};
283
288 };
289} // namespace fcn
290
291#endif // INCLUDE_FIFECHAN_WIDGETS_TEXTBOX_HPP_
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
Represents a key event.
Definition keyevent.hpp:26
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:332
virtual void scrollToCaret()
Scrolls the text to the caret if the text box is in a scroll area.
Definition textbox.cpp:337
void setCaretColumnUTF8(int column)
Sets caret column (UTF-8 aware).
Definition textbox.cpp:372
virtual void addRow(std::string const &row)
Adds a row of text to the end of the text.
Definition textbox.cpp:354
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition textbox.cpp:98
std::string getTextRow(int row) const
Gets a certain row from the text.
Definition textbox.cpp:304
Text * mText
Holds the text of the text box.
Definition textbox.hpp:272
void setCaretRowColumn(int row, int column)
Sets the row and the column where the caret should be currently located.
Definition textbox.cpp:274
void draw(Graphics *graphics) override
Draws the widget.
Definition textbox.cpp:52
void textInput(TextInputEvent &event) override
Called when text input (IME, dead-key, paste) is received.
Definition textbox.cpp:213
void setTextRow(int row, std::string const &text)
Sets the text of a certain row of the text.
Definition textbox.cpp:312
bool mEditable
True if the text box is editable, false otherwise.
Definition textbox.hpp:277
void adjustSizeImpl()
Adjusts the size of the button to fit the caption.
Definition textbox.cpp:235
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition textbox.cpp:224
unsigned int getCaretPosition() const
Gets the caret position in the text.
Definition textbox.cpp:269
virtual void drawCaret(Graphics *graphics, int x, int y)
Draws the caret.
Definition textbox.cpp:75
bool mOpaque
True if the text box is opaque, false otherwise.
Definition textbox.hpp:282
std::string getText() const
Gets the text of the text box.
Definition textbox.cpp:327
void setCaretColumn(int column)
Sets the column where the caret should be currently located.
Definition textbox.cpp:293
TextBox(std::string const &text="")
Constructor.
Definition textbox.cpp:26
unsigned int getCaretColumn() const
Gets the column where the caret is currently located.
Definition textbox.cpp:299
void setCaretRow(int row)
Sets the row where the caret should be currently located.
Definition textbox.cpp:282
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed down on the widget area.
Definition textbox.cpp:83
void setEditable(bool editable)
Sets the text box to be editable or not.
Definition textbox.cpp:344
void adjustSize() override
Adjusts the text box's size to fit the text.
Definition textbox.cpp:230
void setCaretRowUTF8(int row)
Sets caret row (UTF-8 aware).
Definition textbox.cpp:380
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition textbox.cpp:93
unsigned int getCaretRow() const
Gets the row number where the caret is currently located.
Definition textbox.cpp:288
void setText(std::string const &text)
Sets the text of the text box.
Definition textbox.cpp:44
bool isOpaque() const
Checks if the text box is opaque.
Definition textbox.cpp:362
void setOpaque(bool opaque)
Sets the text box to be opaque or not.
Definition textbox.cpp:367
UTF8StringEditor * mStringEditor
UTF8StringEditor for UTF8 support.
Definition textbox.hpp:287
unsigned int getNumberOfRows() const
Gets the number of rows in the text.
Definition textbox.cpp:322
bool isEditable() const
Checks if the text box is editable.
Definition textbox.cpp:349
void setCaretPosition(unsigned int position)
Sets the position of the caret in the text.
Definition textbox.cpp:263
void setCaretRowColumnUTF8(int row, int column)
Sets the caret row and column (UTF-8 aware).
Definition textbox.cpp:392
Text input event for IME (input method editor) composition, dead keys, and pasted text.
Helper class for text manipulation within widgets.
Definition text.hpp:32
Utility for editing and handling UTF-8 encoded strings.
Widget()
Constructor.
Definition widget.cpp:52
virtual void resizeToContent(bool recursion=true)
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1580
Used replacement tokens by configure_file():