--- --- TGUI: include/TGUI/Widgets/EditBox.hpp Source File
TGUI  1.x-dev
Loading...
Searching...
No Matches
EditBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_EDIT_BOX_HPP
26#define TGUI_EDIT_BOX_HPP
27
28#include <TGUI/Widgets/ClickableWidget.hpp>
29#include <TGUI/Renderers/EditBoxRenderer.hpp>
30#include <TGUI/Rect.hpp>
31#include <TGUI/Text.hpp>
32
33#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <regex>
35#endif
36
38
39TGUI_MODULE_EXPORT namespace tgui
40{
47 class TGUI_API EditBox : public ClickableWidget
48 {
49 public:
50
51 using Ptr = std::shared_ptr<EditBox>;
52 using ConstPtr = std::shared_ptr<const EditBox>;
53
54 static constexpr const char StaticWidgetType[] = "EditBox";
55
59 using Alignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = HorizontalAlignment;
60
64 struct Validator
65 {
66 static TGUI_API const char32_t* All;
67 static TGUI_API const char32_t* Int;
68 static TGUI_API const char32_t* UInt;
69 static TGUI_API const char32_t* Float;
70 };
71
79 EditBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
80
87 TGUI_NODISCARD static EditBox::Ptr create();
88
97 TGUI_NODISCARD static EditBox::Ptr copy(const EditBox::ConstPtr& editBox);
98
103 TGUI_NODISCARD EditBoxRenderer* getSharedRenderer() override;
104 TGUI_NODISCARD const EditBoxRenderer* getSharedRenderer() const override;
105
111 TGUI_NODISCARD EditBoxRenderer* getRenderer() override;
112
119 void setSize(const Layout2d& size) override;
120 using Widget::setSize;
121
129 void setEnabled(bool enabled) override;
130
143 void setText(const String& text);
144
151 TGUI_NODISCARD const String& getText() const;
152
161 void setDefaultText(const String& text);
162
171 TGUI_NODISCARD const String& getDefaultText() const;
172
182 void selectText(std::size_t start = 0, std::size_t length = String::npos);
183
190 TGUI_NODISCARD String getSelectedText() const;
191
203 void setPasswordCharacter(char32_t passwordChar);
204
212 TGUI_NODISCARD char32_t getPasswordCharacter() const;
213
222 void setMaximumCharacters(unsigned int maxChars);
223
233 TGUI_NODISCARD unsigned int getMaximumCharacters() const;
234
242
249 TGUI_NODISCARD HorizontalAlignment getAlignment() const;
250
251#ifndef TGUI_REMOVE_DEPRECATED_CODE
261 TGUI_DEPRECATED("Use setTextWidthLimited instead") void limitTextWidth(bool limitWidth = true);
262#endif
263
274 void setTextWidthLimited(bool limitWidth);
275
282 TGUI_NODISCARD bool isTextWidthLimited() const;
283
293 void setReadOnly(bool readOnly = true);
294
304 TGUI_NODISCARD bool isReadOnly() const;
305
312 void setCaretPosition(std::size_t charactersBeforeCaret);
313
320 TGUI_NODISCARD std::size_t getCaretPosition() const;
321
338 bool setInputValidator(const String& regex = U".*");
339
346 TGUI_NODISCARD const String& getInputValidator() const;
347
355 void setSuffix(const String& suffix);
356
362 TGUI_NODISCARD const String& getSuffix() const;
363
372 void setFocused(bool focused) override;
373
378 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
379
383 bool leftMousePressed(Vector2f pos) override;
384
388 void mouseMoved(Vector2f pos) override;
389
393 void keyPressed(const Event::KeyEvent& event) override;
394
404 bool canHandleKeyPress(const Event::KeyEvent& event) override;
405
409 void textEntered(char32_t key) override;
410
418 void draw(BackendRenderTarget& target, RenderStates states) const override;
419
421 protected:
422
432 TGUI_NODISCARD Signal& getSignal(String signalName) override;
433
440 void rendererChanged(const String& property) override;
441
445 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
446
450 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
451
455 void updateTextSize() override;
456
458 // Returns the total width that the text is going to take
460 TGUI_NODISCARD float getFullTextWidth() const;
461
463 // Returns the size without the borders
465 TGUI_NODISCARD Vector2f getInnerSize() const;
466
468 // Returns the width of the edit box minus the padding.
470 TGUI_NODISCARD float getVisibleEditBoxWidth() const;
471
473 // This function will search after which character the caret should be placed. It will not change the caret position.
475 TGUI_NODISCARD std::size_t findCaretPosition(float posX);
476
478 // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
479 // some characters selected.
481 void deleteSelectedCharacters();
482
484 // Recalculates the position of the texts.
486 void recalculateTextPositions();
487
489 // Updates the internal texts after SelStart or SelEnd changed.
491 void updateSelection();
492
494 // Update the color of the Text objects
496 void updateTextColor();
497
499 // This function is called every frame with the time passed since the last frame.
501 bool updateTime(Duration elapsedTime) override;
502
504 // Makes a copy of the widget
506 TGUI_NODISCARD Widget::Ptr clone() const override;
507
509 // Updates m_selEnd with a new value and emits the onCaretPositionChange signal
510 // @param newValue the value to assign to m_selEnd.
512 void updateSelEnd(const std::size_t newValue);
513
519 void emitReturnOrUnfocus(const String& text);
520
522 private:
523
525 // Handles "Backspace" key press
527 void backspaceKeyPressed();
528
530 // Handles "Delete" key press
532 void deleteKeyPressed();
533
535 // Handles "Ctrl+C" key press (or equivalent on macOS)
537 void copySelectedTextToClipboard();
538
540 // Handles "Ctrl+X" key press (or equivalent on macOS)
542 void cutSelectedTextToClipboard();
543
545 // Handles "Ctrl+V" key press (or equivalent on macOS)
547 void pasteTextFromClipboard();
548
550 // Handles "ArrowLeft" key press
552 void moveCaretLeft(bool shiftPressed);
553
555 // Handles "ArrowRight" key press
557 void moveCaretRight(bool shiftPressed);
558
560 // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
562 void moveCaretWordBegin();
563
565 // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
567 void moveCaretWordEnd();
568
570 public:
571
572 SignalString onTextChange = {"TextChanged"};
573 SignalString onReturnKeyPress = {"ReturnKeyPressed"};
574 SignalString onReturnOrUnfocus = {"ReturnOrUnfocused"};
575 SignalTyped<std::size_t> onCaretPositionChange = {"CaretPositionChanged"};
576
578 protected:
579
580 // Is the caret visible or not?
581 bool m_caretVisible = true;
582
583 // When this boolean is true then you can no longer add text when the EditBox is full.
584 // Changing it to false will allow you to scroll the text (default).
585 // You can change the boolean with the setTextWidthLimited(bool) function.
586 bool m_limitTextWidth = false;
587
588 bool m_readOnly = false;
589
590 // The text inside the edit box
591 String m_text;
592 String m_displayedText; // Same as m_text unless a password char is set
593
594 String m_regexString = U".*";
595 std::wregex m_regex;
596
597 // The text alignment
598 HorizontalAlignment m_textAlignment = HorizontalAlignment::Left;
599
600 // The selection
601 std::size_t m_selChars = 0;
602 std::size_t m_selStart = 0;
603 std::size_t m_selEnd = 0;
604
605 // The password character
606 char32_t m_passwordChar = '\0';
607
608 // The maximum allowed characters.
609 // Zero by default, meaning no limit.
610 unsigned int m_maxChars = 0;
611
612 // When the text width is not limited, you can scroll the edit box and only a part will be visible.
613 unsigned int m_textCropPosition = 0;
614
615 // The rectangle behind the selected text
616 FloatRect m_selectedTextBackground;
617
618 // The blinking caret
619 FloatRect m_caret = {0, 0, 1, 0};
620
621 // Is there a possibility that the user is going to double click?
622 bool m_possibleDoubleClick = false;
623
624 // We need three texts for drawing + one for the default text + one more for calculations.
625 Text m_textBeforeSelection;
626 Text m_textSelection;
627 Text m_textAfterSelection;
628 Text m_defaultText;
629 Text m_textFull;
630 Text m_textSuffix;
631
632 Sprite m_sprite;
633 Sprite m_spriteHover;
634 Sprite m_spriteDisabled;
635 Sprite m_spriteFocused;
636
637 // Cached renderer properties
638 Borders m_bordersCached;
639 Padding m_paddingCached;
640 Color m_borderColorCached;
641 Color m_borderColorHoverCached;
642 Color m_borderColorDisabledCached;
643 Color m_borderColorFocusedCached;
644 Color m_backgroundColorCached;
645 Color m_backgroundColorHoverCached;
646 Color m_backgroundColorDisabledCached;
647 Color m_backgroundColorFocusedCached;
648 Color m_caretColorCached;
649 Color m_caretColorHoverCached;
650 Color m_caretColorFocusedCached;
651 Color m_selectedTextBackgroundColorCached;
652
654 private:
655
656 // Used to prevent emitting onReturnOrUnfocus twice when unfocusing the edit box inside the callback function.
657 bool m_onReturnOrUnfocusEmitted = false;
658
660 };
661
663}
664
666
667#endif // TGUI_EDIT_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Clickable widget.
Definition ClickableWidget.hpp:40
Wrapper for durations.
Definition Duration.hpp:56
Edit box widget.
Definition EditBox.hpp:48
void setFocused(bool focused) override
Focus or unfocus the widget.
TGUI_NODISCARD char32_t getPasswordCharacter() const
Returns the password character.
void emitReturnOrUnfocus(const String &text)
Emits the onReturnOrUnfocus signal.
void setReadOnly(bool readOnly=true)
Makes the edit box read-only or make it writable again.
void selectText(std::size_t start=0, std::size_t length=String::npos)
Selects text in the edit box.
void setCaretPosition(std::size_t charactersBeforeCaret)
Sets the blinking caret to after a specific character.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void setMaximumCharacters(unsigned int maxChars)
Changes the character limit.
static TGUI_NODISCARD EditBox::Ptr create()
Creates a new edit box widget.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
TGUI_NODISCARD 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.
TGUI_NODISCARD const String & getInputValidator() const
Returns the regex to which the text is matched.
void setAlignment(HorizontalAlignment alignment)
Changes the text alignment.
void setText(const String &text)
Changes the text of the editbox.
TGUI_NODISCARD EditBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool setInputValidator(const String &regex=U".*")
Defines how the text input should look like.
TGUI_NODISCARD EditBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD std::size_t getCaretPosition() const
Returns after which character the blinking cursor is currently located.
std::shared_ptr< const EditBox > ConstPtr
Shared constant widget pointer.
Definition EditBox.hpp:52
TGUI_NODISCARD unsigned int getMaximumCharacters() const
Returns the character limit.
TGUI_NODISCARD const String & getSuffix() const
Returns the suffix currently displayed on the right side of the edit box.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void setSuffix(const String &suffix)
Places a suffix at the right side of the edit box.
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition EditBox.hpp:51
TGUI_NODISCARD bool isReadOnly() const
Checks if the edit box read-only or writable.
TGUI_NODISCARD String getSelectedText() const
Returns the text that you currently have selected. This text is not affected by the password characte...
void setTextWidthLimited(bool limitWidth)
Should the text width be limited or should you be able to type even if the edit box is full?
void setEnabled(bool enabled) override
Enables or disables the widget.
void setSize(const Layout2d &size) override
Changes the size of the edit box.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
TGUI_NODISCARD const String & getDefaultText() const
Returns the default text of the edit box. This is the text drawn when the edit box is empty.
TGUI_NODISCARD bool isTextWidthLimited() const
Checks if the text width is limited to the size of the edit box.
void setPasswordCharacter(char32_t passwordChar)
Sets a password character.
static TGUI_NODISCARD EditBox::Ptr copy(const EditBox::ConstPtr &editBox)
Makes a copy of another edit box.
TGUI_NODISCARD HorizontalAlignment getAlignment() const
Gets the current text alignment.
TGUI_NODISCARD const String & getText() const
Returns the text inside the edit box. This text is not affected by the password character.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
void setDefaultText(const String &text)
Changes the default text of the editbox. This is the text drawn when the edit box is empty.
Definition EditBoxRenderer.hpp:37
Class to store the position or size of a widget.
Definition Layout.hpp:328
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Wrapper class to store strings.
Definition String.hpp:101
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:52
Predefined input validators.
Definition EditBox.hpp:65
static TGUI_API const char32_t * Int
Accept negative and positive integers.
Definition EditBox.hpp:67
static TGUI_API const char32_t * Float
Accept decimal numbers.
Definition EditBox.hpp:69
static TGUI_API const char32_t * UInt
Accept only positive integers.
Definition EditBox.hpp:68
static TGUI_API const char32_t * All
Accept any input.
Definition EditBox.hpp:66
KeyPressed event parameters.
Definition Event.hpp:169
States used for drawing.
Definition RenderStates.hpp:39