TGUI  1.3-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
29#include <TGUI/Widgets/ClickableWidget.hpp>
30#include <TGUI/Renderers/EditBoxRenderer.hpp>
31#include <TGUI/Rect.hpp>
32#include <TGUI/Text.hpp>
33
34#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
35 #include <regex>
36#endif
37
39
40TGUI_MODULE_EXPORT namespace tgui
41{
48 class TGUI_API EditBox : public ClickableWidget
49 {
50 public:
51
52 using Ptr = std::shared_ptr<EditBox>;
53 using ConstPtr = std::shared_ptr<const EditBox>;
54
55 static constexpr const char StaticWidgetType[] = "EditBox";
56
57
61 enum class Alignment
62 {
64 Left,
65
67 Center,
68
70 Right
71 };
72
73
77 struct Validator
78 {
79 static TGUI_API const char32_t* All;
80 static TGUI_API const char32_t* Int;
81 static TGUI_API const char32_t* UInt;
82 static TGUI_API const char32_t* Float;
83 };
84
85
93 EditBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
94
95
102 TGUI_NODISCARD static EditBox::Ptr create();
103
104
113 TGUI_NODISCARD static EditBox::Ptr copy(const EditBox::ConstPtr& editBox);
114
115
120 TGUI_NODISCARD EditBoxRenderer* getSharedRenderer() override;
121 TGUI_NODISCARD const EditBoxRenderer* getSharedRenderer() const override;
122
128 TGUI_NODISCARD EditBoxRenderer* getRenderer() override;
129
130
137 void setSize(const Layout2d& size) override;
138 using Widget::setSize;
139
140
148 void setEnabled(bool enabled) override;
149
150
164 void setText(const String& text);
165
166
173 TGUI_NODISCARD const String& getText() const;
174
175
184 void setDefaultText(const String& text);
185
186
195 TGUI_NODISCARD const String& getDefaultText() const;
196
197
207 void selectText(std::size_t start = 0, std::size_t length = String::npos);
208
209
216 TGUI_NODISCARD String getSelectedText() const;
217
218
231 void setPasswordCharacter(char32_t passwordChar);
232
233
241 TGUI_NODISCARD char32_t getPasswordCharacter() const;
242
243
252 void setMaximumCharacters(unsigned int maxChars);
253
254
264 TGUI_NODISCARD unsigned int getMaximumCharacters() const;
265
266
273 void setAlignment(Alignment alignment);
274
275
282 TGUI_NODISCARD Alignment getAlignment() const;
283
284
294 void limitTextWidth(bool limitWidth = true);
295
296
303 TGUI_NODISCARD bool isTextWidthLimited() const;
304
305
315 void setReadOnly(bool readOnly = true);
316
317
327 TGUI_NODISCARD bool isReadOnly() const;
328
329
336 void setCaretPosition(std::size_t charactersBeforeCaret);
337
338
345 TGUI_NODISCARD std::size_t getCaretPosition() const;
346
347
364 bool setInputValidator(const String& regex = U".*");
365
366
373 TGUI_NODISCARD const String& getInputValidator() const;
374
375
383 void setSuffix(const String& suffix);
384
385
391 TGUI_NODISCARD const String& getSuffix() const;
392
393
402 void setFocused(bool focused) override;
403
404
409 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
410
411
415 bool leftMousePressed(Vector2f pos) override;
416
420 void mouseMoved(Vector2f pos) override;
421
425 void keyPressed(const Event::KeyEvent& event) override;
426
436 bool canHandleKeyPress(const Event::KeyEvent& event) override;
437
441 void textEntered(char32_t key) override;
442
443
451 void draw(BackendRenderTarget& target, RenderStates states) const override;
452
453
455 protected:
456
466 TGUI_NODISCARD Signal& getSignal(String signalName) override;
467
468
475 void rendererChanged(const String& property) override;
476
477
481 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
482
483
487 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
488
489
493 void updateTextSize() override;
494
495
497 // Returns the total width that the text is going to take
499 TGUI_NODISCARD float getFullTextWidth() const;
500
501
503 // Returns the size without the borders
505 TGUI_NODISCARD Vector2f getInnerSize() const;
506
507
509 // Returns the width of the edit box minus the padding.
511 TGUI_NODISCARD float getVisibleEditBoxWidth() const;
512
513
515 // This function will search after which character the caret should be placed. It will not change the caret position.
517 TGUI_NODISCARD std::size_t findCaretPosition(float posX);
518
519
521 // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
522 // some characters selected.
524 void deleteSelectedCharacters();
525
526
528 // Recalculates the position of the texts.
530 void recalculateTextPositions();
531
532
534 // Updates the internal texts after SelStart or SelEnd changed.
536 void updateSelection();
537
538
540 // Update the color of the Text objects
542 void updateTextColor();
543
544
546 // This function is called every frame with the time passed since the last frame.
548 bool updateTime(Duration elapsedTime) override;
549
550
552 // Makes a copy of the widget
554 TGUI_NODISCARD Widget::Ptr clone() const override;
555
556
558 // Updates m_selEnd with a new value and emits the onCaretPositionChange signal
559 // @param newValue the value to assign to m_selEnd.
561 void updateSelEnd(const std::size_t newValue);
562
563
569 void emitReturnOrUnfocus(const String& text);
570
571
573 private:
574
576 // Handles "Backspace" key press
578 void backspaceKeyPressed();
579
581 // Handles "Delete" key press
583 void deleteKeyPressed();
584
586 // Handles "Ctrl+C" key press (or equivalent on macOS)
588 void copySelectedTextToClipboard();
589
591 // Handles "Ctrl+X" key press (or equivalent on macOS)
593 void cutSelectedTextToClipboard();
594
596 // Handles "Ctrl+V" key press (or equivalent on macOS)
598 void pasteTextFromClipboard();
599
601 // Handles "ArrowLeft" key press
603 void moveCaretLeft(bool shiftPressed);
604
606 // Handles "ArrowRight" key press
608 void moveCaretRight(bool shiftPressed);
609
611 // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
613 void moveCaretWordBegin();
614
616 // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
618 void moveCaretWordEnd();
619
620
622 public:
623
624 SignalString onTextChange = {"TextChanged"};
625 SignalString onReturnKeyPress = {"ReturnKeyPressed"};
626 SignalString onReturnOrUnfocus = {"ReturnOrUnfocused"};
627 SignalTyped<std::size_t> onCaretPositionChange = {"CaretPositionChanged"};
628
629
631 protected:
632
633 // Is the caret visible or not?
634 bool m_caretVisible = true;
635
636 // When this boolean is true then you can no longer add text when the EditBox is full.
637 // Changing it to false will allow you to scroll the text (default).
638 // You can change the boolean with the limitTextWidth(bool) function.
639 bool m_limitTextWidth = false;
640
641 bool m_readOnly = false;
642
643 // The text inside the edit box
644 String m_text;
645 String m_displayedText; // Same as m_text unless a password char is set
646
647 String m_regexString = U".*";
648 std::wregex m_regex;
649
650 // The text alignment
651 Alignment m_textAlignment = Alignment::Left;
652
653 // The selection
654 std::size_t m_selChars = 0;
655 std::size_t m_selStart = 0;
656 std::size_t m_selEnd = 0;
657
658 // The password character
659 char32_t m_passwordChar = '\0';
660
661 // The maximum allowed characters.
662 // Zero by default, meaning no limit.
663 unsigned int m_maxChars = 0;
664
665 // When the text width is not limited, you can scroll the edit box and only a part will be visible.
666 unsigned int m_textCropPosition = 0;
667
668 // The rectangle behind the selected text
669 FloatRect m_selectedTextBackground;
670
671 // The blinking caret
672 FloatRect m_caret = {0, 0, 1, 0};
673
674 // Is there a possibility that the user is going to double click?
675 bool m_possibleDoubleClick = false;
676
677 // We need three texts for drawing + one for the default text + one more for calculations.
678 Text m_textBeforeSelection;
679 Text m_textSelection;
680 Text m_textAfterSelection;
681 Text m_defaultText;
682 Text m_textFull;
683 Text m_textSuffix;
684
685 Sprite m_sprite;
686 Sprite m_spriteHover;
687 Sprite m_spriteDisabled;
688 Sprite m_spriteFocused;
689
690 // Cached renderer properties
691 Borders m_bordersCached;
692 Padding m_paddingCached;
693 Color m_borderColorCached;
694 Color m_borderColorHoverCached;
695 Color m_borderColorDisabledCached;
696 Color m_borderColorFocusedCached;
697 Color m_backgroundColorCached;
698 Color m_backgroundColorHoverCached;
699 Color m_backgroundColorDisabledCached;
700 Color m_backgroundColorFocusedCached;
701 Color m_caretColorCached;
702 Color m_caretColorHoverCached;
703 Color m_caretColorFocusedCached;
704 Color m_selectedTextBackgroundColorCached;
705
706
708 private:
709
710 // Used to prevent emitting onReturnOrUnfocus twice when unfocusing the edit box inside the callback function.
711 bool m_onReturnOrUnfocusEmitted = false;
712
713
715 };
716
718}
719
721
722#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
Definition EditBoxRenderer.hpp:37
Edit box widget.
Definition EditBox.hpp:49
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.
Alignment
The text alignment.
Definition EditBox.hpp:62
TGUI_NODISCARD Alignment getAlignment() const
Gets the current text alignment.
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 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 limitTextWidth(bool limitWidth=true)
Should the text width be limited or should you be able to type even if the edit box is full?
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:53
TGUI_NODISCARD unsigned int getMaximumCharacters() const
Returns the character limit.
void setAlignment(Alignment alignment)
Changes the text alignment.
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:52
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 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 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.
Class to store the position or size of a widget.
Definition Layout.hpp:305
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
Predefined input validators.
Definition EditBox.hpp:78
static TGUI_API const char32_t * Int
Accept negative and positive integers.
Definition EditBox.hpp:80
static TGUI_API const char32_t * Float
Accept decimal numbers.
Definition EditBox.hpp:82
static TGUI_API const char32_t * UInt
Accept only positive integers.
Definition EditBox.hpp:81
static TGUI_API const char32_t * All
Accept any input.
Definition EditBox.hpp:79
KeyPressed event parameters.
Definition Event.hpp:169
States used for drawing.
Definition RenderStates.hpp:39