FifeGUI 0.3.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// Standard library includes
9#include <string>
10
11// Platform config include
12#include "fifechan/platform.hpp"
13
14// Project headers (subdirs before local)
15#include "fifechan/listeners/keylistener.hpp"
16#include "fifechan/listeners/mouselistener.hpp"
17#include "fifechan/widget.hpp"
18
19namespace fcn
20{
21 class Text;
22 class UTF8StringEditor;
23
29 class FIFEGUI_API TextField : public Widget, public MouseListener, public KeyListener
30 {
31 public:
32 TextField();
33
41 explicit TextField(std::string const & text);
42
43 TextField(TextField const &) = delete;
44 TextField& operator=(TextField const &) = delete;
45 TextField(TextField&&) = delete;
46 TextField& operator=(TextField&&) = delete;
47
48 ~TextField() override;
49
56 virtual void setText(std::string const & text);
57
64 virtual std::string getText() const;
65
69 void adjustHeight();
70
77 bool isEditable() const;
78
87 void setEditable(bool editable);
88
98 void setCaretPosition(unsigned int position);
99
109 unsigned int getCaretPosition() const;
110
111 // Inherited from Widget
112
114
115 void resizeToContent(bool recursion = true) override;
116
120 void adjustSize() override;
121
122 void draw(Graphics* graphics) override;
123
124 // Inherited from MouseListener
125
126 void mousePressed(MouseEvent& mouseEvent) override;
127
128 void mouseDragged(MouseEvent& mouseEvent) override;
129
130 // Inherited from KeyListener
131
132 void keyPressed(KeyEvent& keyEvent) override;
133
134 void textInput(TextInputEvent& event) override;
135
136 protected:
146 void adjustSizeImpl();
147
155 virtual void drawCaret(Graphics* graphics, int x);
156
163 void fixScroll();
164
168 bool mEditable{true};
169
174
181 int mXScroll{0};
182
187 };
188} // namespace fcn
189
190#endif // INCLUDE_FIFECHAN_WIDGETS_TEXTFIELD_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 adjustSizeImpl()
Adjusts the size of the button to fit the caption.
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 down on the widget area.
virtual void drawCaret(Graphics *graphics, int x)
Draws the caret.
UTF8StringEditor * mStringEditor
String editor for UTF8 support.
unsigned int getCaretPosition() const
Gets the caret position.
void fixScroll()
Ensures the caret remains visible by adjusting horizontal scroll.
int mXScroll
Horizontal scroll offset in pixels.
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
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:49
virtual std::string getText() const
Gets the text of the text field.
void textInput(TextInputEvent &event) override
Called when text input (IME, dead-key, paste) is received.
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:54
Text * mText
Holds the text of the text field.
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():