FifeGUI 0.2.0
A C++ GUI library designed for games.
textfield.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_TEXTFIELD_HPP_
6#define INCLUDE_FIFECHAN_WIDGETS_TEXTFIELD_HPP_
7
8#include <string>
9
10#include "fifechan/keylistener.hpp"
11#include "fifechan/mouselistener.hpp"
12#include "fifechan/platform.hpp"
13#include "fifechan/widget.hpp"
14
15namespace fcn
16{
17 class Text;
18 class UTF8StringEditor;
19
25 class FIFEGUI_API TextField : public Widget, public MouseListener, public KeyListener
26 {
27 public:
28 TextField();
29
36 explicit TextField(std::string const & text);
37
38 TextField(TextField const &) = delete;
39 TextField& operator=(TextField const &) = delete;
40 TextField(TextField&&) = delete;
41 TextField& operator=(TextField&&) = delete;
42
43 ~TextField() override;
44
51 virtual void setText(std::string const & text);
52
59 virtual std::string getText() const;
60
64 void adjustHeight();
65
72 bool isEditable() const;
73
81 void setEditable(bool editable);
82
90 void setCaretPosition(unsigned int position);
91
99 unsigned int getCaretPosition() const;
100
101 // Inherited from Widget
102
104
105 void resizeToContent(bool recursion) override;
106
110 void adjustSize() override;
111
112 void draw(Graphics* graphics) override;
113
114 // Inherited from MouseListener
115
116 void mousePressed(MouseEvent& mouseEvent) override;
117
118 void mouseDragged(MouseEvent& mouseEvent) override;
119
120 // Inherited from KeyListener
121
122 void keyPressed(KeyEvent& keyEvent) override;
123
124 protected:
134 void adjustSizeImpl();
135
143 virtual void drawCaret(Graphics* graphics, int x);
144
150 void fixScroll();
151
155 bool mEditable{true};
156
161
167 int mXScroll{0};
168
173 };
174} // namespace fcn
175
176#endif // INCLUDE_FIFECHAN_WIDGETS_TEXTFIELD_HPP_
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Represents a key event.
Definition keyevent.hpp:22
Represents a mouse event.
void adjustSizeImpl()
Adjusts the size of the button to fit the caption.
void resizeToContent(bool recursion) override
Resize this widget to fit its content.
void setCaretPosition(unsigned int position)
Sets the caret position.
bool mEditable
True if the text field is editable, false otherwise.
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed on the widget area.
virtual void drawCaret(Graphics *graphics, int x)
Draws the caret.
Definition textfield.cpp:97
UTF8StringEditor * mStringEditor
String editor for UTF8 support.
unsigned int getCaretPosition() const
Gets the caret position.
void fixScroll()
Scrolls the text horizontally so that the caret shows if needed.
int mXScroll
Holds the amount scrolled in x.
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
virtual void setText(std::string const &text)
Sets the text of the text field.
Definition textfield.cpp:45
virtual std::string getText() const
Gets the text of the text field.
void setEditable(bool editable)
Sets the text field to be editable or not.
void adjustSize() override
Adjusts the size of the text field to fit the text.
bool isEditable() const
Checks if the text field is editable.
void adjustHeight()
Adjusts the height of the text field to fit caption.
void draw(Graphics *graphics) override
Draws the widget.
Definition textfield.cpp:50
Text * mText
Holds the text of the text field.
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