TGUI  0.8.9

Text box widget. More...

#include <TGUI/Widgets/TextBox.hpp>

Inheritance diagram for tgui::TextBox:
tgui::Widget tgui::SignalWidgetBase

Public Types

typedef std::shared_ptr< TextBoxPtr
 Shared widget pointer.
 
typedef std::shared_ptr< const TextBoxConstPtr
 Shared constant widget pointer.
 
- Public Types inherited from tgui::Widget
typedef std::shared_ptr< WidgetPtr
 Shared widget pointer.
 
typedef std::shared_ptr< const WidgetConstPtr
 Shared constant widget pointer.
 

Public Member Functions

TextBoxRenderergetSharedRenderer ()
 Returns the renderer, which gives access to functions that determine how the widget is displayed. More...
 
TextBoxRenderergetRenderer ()
 Returns the renderer, which gives access to functions that determine how the widget is displayed. More...
 
void setSize (const Layout2d &size) override
 Changes the size of the text box. More...
 
void setText (const sf::String &text, bool triggerTextChangedSignal=false)
 Changes the text of the text box. More...
 
void addText (const sf::String &text)
 Appends some text to the text that was already in the text box. More...
 
const sf::String & getText () const
 Returns the text of the text box. More...
 
void setDefaultText (const sf::String &text)
 Changes the default text of the textbox. This is the text drawn when the text box is empty. More...
 
const sf::String & getDefaultText () const
 Returns the default text of the text box. This is the text drawn when the text box is empty. More...
 
void setSelectedText (std::size_t selectionStartIndex, std::size_t selectionEndIndex)
 Changes which part of the text is selected. More...
 
sf::String getSelectedText () const
 Returns the text that you currently have selected. More...
 
std::size_t getSelectionStart () const
 Returns the index where the selection starts. More...
 
std::size_t getSelectionEnd () const
 Returns the index where the selection ends. More...
 
void setTextSize (unsigned int size) override
 Changes the character size of the text. More...
 
void setMaximumCharacters (std::size_t maxChars=0)
 Changes the maximum character limit. More...
 
std::size_t getMaximumCharacters () const
 Returns the maximum character limit. More...
 
void setCaretPosition (std::size_t charactersBeforeCaret)
 Sets the blinking caret to after a specific character. More...
 
std::size_t getCaretPosition () const
 Returns after which character the blinking cursor is currently located. More...
 
void setReadOnly (bool readOnly=true)
 Makes the text box read-only or make it writable again. More...
 
bool isReadOnly () const
 Checks if the text box read-only or writable. More...
 
void setHorizontalScrollbarPresent (bool present)
 Sets whether the horizontal scrollbar is present. More...
 
bool isHorizontalScrollbarPresent () const
 Returns whether the horizontal scrollbar is present. More...
 
void setVerticalScrollbarPresent (bool present)
 Sets whether the vertical scrollbar is present. More...
 
bool isVerticalScrollbarPresent () const
 Returns whether the vertical scrollbar is present. More...
 
void setVerticalScrollbarPolicy (Scrollbar::Policy policy)
 Changes when the vertical scrollbar should be displayed. More...
 
Scrollbar::Policy getVerticalScrollbarPolicy () const
 Returns when the vertical scrollbar should be displayed. More...
 
void setHorizontalScrollbarPolicy (Scrollbar::Policy policy)
 Changes when the horizontal scrollbar should be displayed. More...
 
Scrollbar::Policy getHorizontalScrollbarPolicy () const
 Returns when the horizontal scrollbar should be displayed. More...
 
std::size_t getLinesCount () const
 Returns the amount of lines that the text occupies in the TextBox. More...
 
void setFocused (bool focused) override
 Focus or unfocus the widget. More...
 
void enableMonospacedFontOptimization (bool enable=true)
 Changes whether an optimization is made that only works when using a monospaced font. More...
 
void setVerticalScrollbarValue (unsigned int value)
 Changes the thumb position of the vertical scrollbar. More...
 
unsigned int getVerticalScrollbarValue () const
 Returns the thumb position of the vertical scrollbar. More...
 
void setHorizontalScrollbarValue (unsigned int value)
 Changes the thumb position of the horizontal scrollbar. More...
 
unsigned int getHorizontalScrollbarValue () const
 Returns the thumb position of the horizontal scrollbar. More...
 
bool mouseOnWidget (Vector2f pos) const override
 Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget. More...
 
virtual void setSize (const Layout2d &size)
 Changes the size of the widget. More...
 
void setSize (Layout width, Layout height)
 Changes the size of the widget. More...
 
- Public Member Functions inherited from tgui::Widget
 Widget ()
 Default constructor.
 
 Widget (const Widget &)
 Copy constructor.
 
 Widget (Widget &&)
 Move constructor.
 
 ~Widget ()
 Destructor.
 
Widgetoperator= (const Widget &)
 Overload of copy assignment operator.
 
Widgetoperator= (Widget &&)
 Move assignment.
 
void setRenderer (std::shared_ptr< RendererData > rendererData)
 Sets a new renderer for the widget. The renderer determines how the widget looks. More...
 
WidgetRenderergetSharedRenderer ()
 Returns the renderer, which gives access to functions that determine how the widget is displayed. More...
 
WidgetRenderergetRenderer ()
 Returns the renderer, which gives access to functions that determine how the widget is displayed. More...
 
virtual void setPosition (const Layout2d &position)
 sets the position of the widget More...
 
void setPosition (Layout x, Layout y)
 Sets the position of the widget. More...
 
Vector2f getPosition () const
 Gets the position of the widget. More...
 
void setSize (Layout width, Layout height)
 Changes the size of the widget. More...
 
Vector2f getSize () const
 Returns the size of the widget. More...
 
virtual Vector2f getFullSize () const
 Returns the entire size that the widget is using. More...
 
virtual Vector2f getAbsolutePosition () const
 Get the absolute position of the widget instead of the relative position to its parent. More...
 
virtual Vector2f getWidgetOffset () const
 Returns the distance between the position where the widget is drawn and where the widget is placed. More...
 
virtual void showWithEffect (ShowAnimationType type, sf::Time duration)
 Shows the widget by introducing it with an animation. More...
 
virtual void hideWithEffect (ShowAnimationType type, sf::Time duration)
 Hides the widget by making it leave with an animation. More...
 
virtual void setVisible (bool visible)
 Shows or hides a widget. More...
 
bool isVisible () const
 Returns true when the widget is visible. More...
 
virtual void setEnabled (bool enabled)
 Enables or disables the widget. More...
 
bool isEnabled () const
 Returns true when the widget is enabled. More...
 
bool isFocused () const
 Returns true when the widget is focused and false otherwise. More...
 
const std::string & getWidgetType () const
 Returns the type of the widget. More...
 
ContainergetParent () const
 Returns a pointer to the parent widget. More...
 
bool isAnimationPlaying () const
 Returns whether there is an active animation (started with showWithEffect or hideWithEffect) More...
 
virtual void moveToFront ()
 Places the widget before all other widgets. More...
 
virtual void moveToBack ()
 Places the widget behind all other widgets. More...
 
void setUserData (std::any userData)
 Stores some data into the widget. More...
 
template<typename T >
getUserData () const
 Returns data stored in the widget. More...
 
void setInheritedFont (const Font &font)
 Sets the font of the widget that is used when no font is set in the renderer. More...
 
const FontgetInheritedFont () const
 Returns the font of the widget that is used when no font is set in the renderer. More...
 
void setInheritedOpacity (float opacity)
 Sets the opacity of the widget that will be multiplied with the opacity set in the renderer. More...
 
float getInheritedOpacity () const
 Returns the opacity of the widget that is multiplied with the opacity set in the renderer. More...
 
virtual unsigned int getTextSize () const
 Returns the character size of text in this widget. More...
 
void setToolTip (Widget::Ptr toolTip)
 Sets the tool tip that should be displayed when hovering over the widget. More...
 
Widget::Ptr getToolTip () const
 Returns the tool tip that is displayed when hovering over the widget. More...
 
void setWidgetName (const sf::String &name)
 Changes the name of a widget. More...
 
sf::String getWidgetName () const
 Returns the name of a widget. More...
 
void setFocusable (bool focusable)
 Changes whether a widget could be focused. More...
 
bool isFocusable () const
 Returns whether a widget could be focused. More...
 
virtual bool canGainFocus () const
 Returns whether the widget can currently gain focus. More...
 
bool isContainer () const
 Returns whether the widget is a container widget or not. More...
 
template<typename WidgetType >
std::shared_ptr< const WidgetType > cast () const
 Downcast const widget.
 
template<typename WidgetType >
std::shared_ptr< WidgetType > cast ()
 Downcast widget.
 
- Public Member Functions inherited from tgui::SignalWidgetBase
template<typename Func , typename... Args, typename std::enable_if< std::is_convertible< Func, std::function< void(const Args &...)> >::value >::type * = nullptr>
unsigned int connect (std::string signalName, Func &&handler, const Args &... args)
 Connects a signal handler that will be called when this signal is emitted. More...
 
template<typename Func , typename... BoundArgs, typename std::enable_if<!std::is_convertible< Func, std::function< void(const BoundArgs &...)> >::value &&std::is_convertible< Func, std::function< void(const BoundArgs &..., std::shared_ptr< Widget >, const std::string &)> >::value , ::type * = nullptr>
unsigned int connect (std::string signalName, Func &&handler, BoundArgs &&... args)
 Connects a signal handler that will be called when this signal is emitted. More...
 
template<typename Func , typename... BoundArgs, typename std::enable_if<!std::is_convertible< Func, std::function< void(const BoundArgs &...)> >::value &&!std::is_convertible< Func, std::function< void(const BoundArgs &..., std::shared_ptr< Widget >, const std::string &)> >::value , ::type * = nullptr>
unsigned int connect (std::string signalName, Func &&handler, BoundArgs &&... args)
 Connects a signal handler that will be called when this signal is emitted. More...
 
template<typename Func , typename... BoundArgs>
unsigned int connect (std::initializer_list< std::string > signalNames, Func &&handler, BoundArgs &&... args)
 Connect a signal handler to multiple signals. More...
 
bool disconnect (unsigned int id)
 Disconnects a signal handler. More...
 
void disconnectAll (std::string signalName)
 Disconnects all signal handler from a certain signal. More...
 
void disconnectAll ()
 Disconnects all signal handlers from signals.
 

Static Public Member Functions

static TextBox::Ptr create ()
 Creates a new text box widget. More...
 
static TextBox::Ptr copy (TextBox::ConstPtr textBox)
 Makes a copy of another text box. More...
 

Public Attributes

SignalString onTextChange = {"TextChanged"}
 The text was changed. Optional parameter: new text.
 
Signal onSelectionChange = {"SelectionChanged"}
 Selected text changed.
 
- Public Attributes inherited from tgui::Widget
SignalVector2f onPositionChange = {"PositionChanged"}
 The position of the widget changed. Optional parameter: new position.
 
SignalVector2f onSizeChange = {"SizeChanged"}
 The size of the widget changed. Optional parameter: new size.
 
Signal onFocus = {"Focused"}
 The widget was focused.
 
Signal onUnfocus = {"Unfocused"}
 The widget was unfocused.
 
Signal onMouseEnter = {"MouseEntered"}
 The mouse entered the widget.
 
Signal onMouseLeave = {"MouseLeft"}
 The mouse left the widget.
 
SignalAnimation onAnimationFinished = {"AnimationFinished"}
 A show or hide animation finished. Optional parameters: animation type, new widget visibility or both.
 

Protected Member Functions

void draw (sf::RenderTarget &target, sf::RenderStates states) const override
 Draw the widget to a render target. More...
 
SignalgetSignal (std::string signalName) override
 Retrieves a signal based on its name. More...
 
void rendererChanged (const std::string &property) override
 Function called when one of the properties of the renderer is changed. More...
 
std::unique_ptr< DataIO::Node > save (SavingRenderersMap &renderers) const override
 Saves the widget as a tree node in order to save it to a file.
 
void load (const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
 Loads the widget from a tree of nodes.
 
Widget::Ptr clone () const override
 Makes a copy of the widget if you don't know its exact type. More...
 
- Protected Member Functions inherited from tgui::Widget
virtual void mouseEnteredWidget ()
 This function is called when the mouse enters the widget.
 
virtual void mouseLeftWidget ()
 This function is called when the mouse leaves the widget.
 

Detailed Description

Text box widget.

A text box is a multi-line input box which supports word-wrap and a vertical scrollbar. If you are looking for a single line input field (with options like setting a password character or displaying a default text when it is empty) then check out the EditBox class.

Member Function Documentation

◆ addText()

void tgui::TextBox::addText ( const sf::String &  text)

Appends some text to the text that was already in the text box.

Parameters
textText that will be added to the text that is already in the text box

◆ clone()

Widget::Ptr tgui::TextBox::clone ( ) const
inlineoverrideprotectedvirtual

Makes a copy of the widget if you don't know its exact type.

This function should only be used when you don't know the type of the widget. If you know what kind of widget you are copying, you should use the copy function.

Returns
Copy of the widget

Implements tgui::Widget.

◆ copy()

static TextBox::Ptr tgui::TextBox::copy ( TextBox::ConstPtr  textBox)
static

Makes a copy of another text box.

Parameters
textBoxThe other text box
Returns
The new text box

◆ create()

static TextBox::Ptr tgui::TextBox::create ( )
static

Creates a new text box widget.

Returns
The new text box

◆ draw()

void tgui::TextBox::draw ( sf::RenderTarget &  target,
sf::RenderStates  states 
) const
overrideprotectedvirtual

Draw the widget to a render target.

Parameters
targetRender target to draw to
statesCurrent render states

Implements tgui::Widget.

◆ enableMonospacedFontOptimization()

void tgui::TextBox::enableMonospacedFontOptimization ( bool  enable = true)

Changes whether an optimization is made that only works when using a monospaced font.

Parameters
enableWhether the optimization should be enabled

Rearranging text when a horizontal scrollbar is enabled can be almost as expensive as the word-wrap which is performed when there is no horizontal scrollbar. When using a monospaced font there is no need for such expensive calculation. Call this function when using a monospaced font to make changing the text faster.

This function has no effect when the horizontal scrollbar is disabled (default).

◆ getCaretPosition()

std::size_t tgui::TextBox::getCaretPosition ( ) const

Returns after which character the blinking cursor is currently located.

Returns
Characters before the caret

This function is an alias for getSelectionEnd.

◆ getDefaultText()

const sf::String & tgui::TextBox::getDefaultText ( ) const

Returns the default text of the text box. This is the text drawn when the text box is empty.

Returns
The default text of the text box

◆ getHorizontalScrollbarPolicy()

Scrollbar::Policy tgui::TextBox::getHorizontalScrollbarPolicy ( ) const

Returns when the horizontal scrollbar should be displayed.

Returns
The policy for displaying the horizontal scrollbar

The default policy is Never, which means word-wrap will be used to keep the text within the TextBox.

◆ getHorizontalScrollbarValue()

unsigned int tgui::TextBox::getHorizontalScrollbarValue ( ) const

Returns the thumb position of the horizontal scrollbar.

Returns
Value of the horizontal scrollbar

◆ getLinesCount()

std::size_t tgui::TextBox::getLinesCount ( ) const

Returns the amount of lines that the text occupies in the TextBox.

Returns
Lines of text

Note that this is the amount of lines after word-wrap is applied.

◆ getMaximumCharacters()

std::size_t tgui::TextBox::getMaximumCharacters ( ) const

Returns the maximum character limit.

Returns
The character limit The function will return 0 when there is no limit.

There is no character limit by default.

◆ getRenderer()

TextBoxRenderer * tgui::TextBox::getRenderer ( )

Returns the renderer, which gives access to functions that determine how the widget is displayed.

Returns
Temporary pointer to the renderer
Warning
After calling this function, the widget has its own copy of the renderer and it will no longer be shared.

◆ getSelectedText()

sf::String tgui::TextBox::getSelectedText ( ) const

Returns the text that you currently have selected.

Returns
The selected text of the text box

◆ getSelectionEnd()

std::size_t tgui::TextBox::getSelectionEnd ( ) const

Returns the index where the selection ends.

The selection end always equals the caret position.

Note that the start of the selection may be behind the end of the selection when the user selected the text from right to left or from bottom to top.

Returns
Amount of characters before the end of the selection

◆ getSelectionStart()

std::size_t tgui::TextBox::getSelectionStart ( ) const

Returns the index where the selection starts.

When no text is selected, this function returns the same getSelectionEnd.

Note that the start of the selection may be behind the end of the selection when the user selected the text from right to left or from bottom to top.

Returns
Amount of characters before the start of the selection

◆ getSharedRenderer()

TextBoxRenderer * tgui::TextBox::getSharedRenderer ( )

Returns the renderer, which gives access to functions that determine how the widget is displayed.

Returns
Temporary pointer to the renderer that may be shared with other widgets using the same renderer

◆ getSignal()

Signal & tgui::TextBox::getSignal ( std::string  signalName)
overrideprotectedvirtual

Retrieves a signal based on its name.

Parameters
signalNameName of the signal
Returns
Signal that corresponds to the name
Exceptions
Exceptionwhen the name does not match any signal

Reimplemented from tgui::Widget.

◆ getText()

const sf::String & tgui::TextBox::getText ( ) const

Returns the text of the text box.

Returns
The text that is currently inside the text box

◆ getVerticalScrollbarPolicy()

Scrollbar::Policy tgui::TextBox::getVerticalScrollbarPolicy ( ) const

Returns when the vertical scrollbar should be displayed.

Returns
The policy for displaying the vertical scrollbar

The default policy is Automatic, which means it only shows when the text doesn't fit inside the TextBox.

◆ getVerticalScrollbarValue()

unsigned int tgui::TextBox::getVerticalScrollbarValue ( ) const

Returns the thumb position of the vertical scrollbar.

Returns
Value of the vertical scrollbar

◆ isHorizontalScrollbarPresent()

bool tgui::TextBox::isHorizontalScrollbarPresent ( ) const

Returns whether the horizontal scrollbar is present.

Returns
Is the horizontal scrollbar present?
See also
setHorizontalScrollbarPresent

◆ isReadOnly()

bool tgui::TextBox::isReadOnly ( ) const

Checks if the text box read-only or writable.

Returns
Is the text box read-only?

When the text box is read-only, you can no longer delete characters and type text. Selecting text, copying text and even calling the setText function will still work.

◆ isVerticalScrollbarPresent()

bool tgui::TextBox::isVerticalScrollbarPresent ( ) const

Returns whether the vertical scrollbar is present.

Returns
Is the vertical scrollbar present?
See also
setVerticalScrollbarPresent

◆ mouseOnWidget()

bool tgui::TextBox::mouseOnWidget ( Vector2f  pos) const
overridevirtual

Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget.

Returns
Is the mouse on top of the widget?

Implements tgui::Widget.

◆ rendererChanged()

void tgui::TextBox::rendererChanged ( const std::string &  property)
overrideprotectedvirtual

Function called when one of the properties of the renderer is changed.

Parameters
propertyLowercase name of the property that was changed

Reimplemented from tgui::Widget.

◆ setCaretPosition()

void tgui::TextBox::setCaretPosition ( std::size_t  charactersBeforeCaret)

Sets the blinking caret to after a specific character.

Parameters
charactersBeforeCaretThe new position

This function will set both the selection start and selection end to the requested value.

◆ setDefaultText()

void tgui::TextBox::setDefaultText ( const sf::String &  text)

Changes the default text of the textbox. This is the text drawn when the text box is empty.

Parameters
textThe new default text

◆ setFocused()

void tgui::TextBox::setFocused ( bool  focused)
overridevirtual

Focus or unfocus the widget.

Parameters
focusedIs the widget focused?

When a widget is focused, the previously focused widget will be unfocused.

Warning
This function only works properly when the widget was already added to its parent (e.g. the Gui).

Reimplemented from tgui::Widget.

◆ setHorizontalScrollbarPolicy()

void tgui::TextBox::setHorizontalScrollbarPolicy ( Scrollbar::Policy  policy)

Changes when the horizontal scrollbar should be displayed.

Parameters
policyThe policy for displaying the horizontal scrollbar

The default policy is Never, which means word-wrap will be used to keep the text within the TextBox.

◆ setHorizontalScrollbarPresent()

void tgui::TextBox::setHorizontalScrollbarPresent ( bool  present)

Sets whether the horizontal scrollbar is present.

When present, the scrollbar will appear once the text takes more space than the text box. When not present, the text will attempt to word wrap once the text line is full. The horizontal scroll is not present by default.

Parameters
presentShould the horizontal scrollbar be present?

◆ setHorizontalScrollbarValue()

void tgui::TextBox::setHorizontalScrollbarValue ( unsigned int  value)

Changes the thumb position of the horizontal scrollbar.

Parameters
valueNew value of the horizontal scrollbar

◆ setMaximumCharacters()

void tgui::TextBox::setMaximumCharacters ( std::size_t  maxChars = 0)

Changes the maximum character limit.

Parameters
maxCharsThe new character limit. Set it to 0 to disable the limit

This character limit is disabled by default.

◆ setReadOnly()

void tgui::TextBox::setReadOnly ( bool  readOnly = true)

Makes the text box read-only or make it writable again.

Parameters
readOnlyShould the text box be read-only?

When the text box is read-only, you can no longer delete characters and type text. Selecting text, copying text and even calling the setText function will still work.

◆ setSelectedText()

void tgui::TextBox::setSelectedText ( std::size_t  selectionStartIndex,
std::size_t  selectionEndIndex 
)

Changes which part of the text is selected.

Parameters
selectionStartIndexAmount of characters before the start of the selection
selectionEndIndexAmount of characters before the end of the selection

◆ setSize() [1/3]

virtual void tgui::Widget::setSize ( const Layout2d size)
virtual

Changes the size of the widget.

Parameters
sizeSize of the widget

Usage examples:

// Give the widget an exact size
widget->setSize({40, 30});
// Give the widget a size relative to the size of its parent
widget->setSize({"20%", "5%"});
// Make the widget 50 pixels higher than some other widget and automatically resize it when the other widget resizes
widget->setSize({200, tgui::bindHeight(otherWidget) + 50});
TGUI_API Layout bindHeight(std::shared_ptr< Widget > widget)
Bind to the height of the widget.

Reimplemented from tgui::Widget.

◆ setSize() [2/3]

void tgui::TextBox::setSize ( const Layout2d size)
overridevirtual

Changes the size of the text box.

This size does not include the borders.

Parameters
sizeThe new size of the text box

Reimplemented from tgui::Widget.

◆ setSize() [3/3]

void tgui::Widget::setSize ( Layout  width,
Layout  height 
)
inline

Changes the size of the widget.

Parameters
widthWidth of the widget
heightHeight of the widget
Warning
This setSize overload must never be used from internal TGUI code or by custom widget implementations

◆ setText()

void tgui::TextBox::setText ( const sf::String &  text,
bool  triggerTextChangedSignal = false 
)

Changes the text of the text box.

Parameters
textNew text
triggerTextChangedSignalShould the "TextChanged" callback be send? False by default for compatibility.

◆ setTextSize()

void tgui::TextBox::setTextSize ( unsigned int  size)
overridevirtual

Changes the character size of the text.

Parameters
sizeThe new text size. The minimum text size is 8

Reimplemented from tgui::Widget.

◆ setVerticalScrollbarPolicy()

void tgui::TextBox::setVerticalScrollbarPolicy ( Scrollbar::Policy  policy)

Changes when the vertical scrollbar should be displayed.

Parameters
policyThe policy for displaying the vertical scrollbar

The default policy is Automatic, which means it only shows when the text doesn't fit inside the TextBox.

◆ setVerticalScrollbarPresent()

void tgui::TextBox::setVerticalScrollbarPresent ( bool  present)

Sets whether the vertical scrollbar is present.

When present, the scrollbar will appear once the text takes more space than the text box. When not present, you will not be able to add text once the text box is full. The vertical scroll is present by default.

Parameters
presentShould the vertical scrollbar be present?

◆ setVerticalScrollbarValue()

void tgui::TextBox::setVerticalScrollbarValue ( unsigned int  value)

Changes the thumb position of the vertical scrollbar.

Parameters
valueNew value of the vertical scrollbar

The documentation for this class was generated from the following file: