TGUI  1.0-beta
Loading...
Searching...
No Matches
EditBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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#include <regex>
34
36
37namespace tgui
38{
45 class TGUI_API EditBox : public ClickableWidget
46 {
47 public:
48
49 using Ptr = std::shared_ptr<EditBox>;
50 using ConstPtr = std::shared_ptr<const EditBox>;
51
52 static constexpr const char StaticWidgetType[] = "EditBox";
53
54
58 enum class Alignment
59 {
61 Left,
62
64 Center,
65
67 Right
68 };
69
70
74 struct Validator
75 {
76 static TGUI_API const char* All;
77 static TGUI_API const char* Int;
78 static TGUI_API const char* UInt;
79 static TGUI_API const char* Float;
80 };
81
82
90 EditBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
91
92
100
101
110 static EditBox::Ptr copy(const EditBox::ConstPtr& editBox);
111
112
118 const EditBoxRenderer* getSharedRenderer() const;
119
126
127
134 void setSize(const Layout2d& size) override;
135 using Widget::setSize;
136
137
145 void setEnabled(bool enabled) override;
146
147
161 void setText(const String& text);
162
163
170 const String& getText() const;
171
172
181 void setDefaultText(const String& text);
182
183
192 const String& getDefaultText() const;
193
194
204 void selectText(std::size_t start = 0, std::size_t length = String::npos);
205
206
214
215
228 void setPasswordCharacter(char32_t passwordChar);
229
230
238 char32_t getPasswordCharacter() const;
239
240
249 void setMaximumCharacters(unsigned int maxChars);
250
251
261 unsigned int getMaximumCharacters() const;
262
263
270 void setAlignment(Alignment alignment);
271
272
280
281
291 void limitTextWidth(bool limitWidth = true);
292
293
300 bool isTextWidthLimited() const;
301
302
312 void setReadOnly(bool readOnly = true);
313
314
324 bool isReadOnly() const;
325
326
333 void setCaretPosition(std::size_t charactersBeforeCaret);
334
335
342 std::size_t getCaretPosition() const;
343
344
361 bool setInputValidator(const String& regex = U".*");
362
363
370 const String& getInputValidator() const;
371
372
380 void setSuffix(const String& suffix);
381
382
388 const String& getSuffix() const;
389
390
399 void setFocused(bool focused) override;
400
401
406 bool isMouseOnWidget(Vector2f pos) const override;
407
408
412 void leftMousePressed(Vector2f pos) override;
413
417 void mouseMoved(Vector2f pos) override;
418
422 void keyPressed(const Event::KeyEvent& event) override;
423
427 void textEntered(char32_t key) override;
428
429
437 void draw(BackendRenderTarget& target, RenderStates states) const override;
438
439
441 protected:
442
452 Signal& getSignal(String signalName) override;
453
454
461 void rendererChanged(const String& property) override;
462
463
467 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
468
469
473 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
474
475
479 void updateTextSize() override;
480
481
483 // Returns the total width that the text is going to take
485 float getFullTextWidth() const;
486
487
489 // Returns the size without the borders
491 Vector2f getInnerSize() const;
492
493
495 // Returns the width of the edit box minus the padding.
497 float getVisibleEditBoxWidth() const;
498
499
501 // This function will search after which character the caret should be placed. It will not change the caret position.
503 std::size_t findCaretPosition(float posX);
504
505
507 // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
508 // some characters selected.
510 void deleteSelectedCharacters();
511
512
514 // Recalculates the position of the texts.
516 void recalculateTextPositions();
517
518
520 // Updates the internal texts after SelStart or SelEnd changed.
522 void updateSelection();
523
524
526 // Update the color of the Text objects
528 void updateTextColor();
529
530
532 // This function is called every frame with the time passed since the last frame.
534 bool updateTime(Duration elapsedTime) override;
535
536
538 // Makes a copy of the widget
540 Widget::Ptr clone() const override;
541
542
544 private:
545
547 // Handles "Backspace" key press
549 void backspaceKeyPressed();
550
552 // Handles "Delete" key press
554 void deleteKeyPressed();
555
557 // Handles "Ctrl+C" key press (or equivalent on macOS)
559 void copySelectedTextToClipboard();
560
562 // Handles "Ctrl+X" key press (or equivalent on macOS)
564 void cutSelectedTextToClipboard();
565
567 // Handles "Ctrl+V" key press (or equivalent on macOS)
569 void pasteTextFromClipboard();
570
572 // Handles "ArrowLeft" key press
574 void moveCaretLeft(bool shiftPressed);
575
577 // Handles "ArrowRight" key press
579 void moveCaretRight(bool shiftPressed);
580
582 // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
584 void moveCaretWordBegin();
585
587 // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
589 void moveCaretWordEnd();
590
591
593 public:
594
595 SignalString onTextChange = {"TextChanged"};
596 SignalString onReturnKeyPress = {"ReturnKeyPressed"};
597 SignalString onReturnOrUnfocus = {"ReturnOrUnfocused"};
598
599
601 protected:
602
603 // Is the caret visible or not?
604 bool m_caretVisible = true;
605
606 // When this boolean is true then you can no longer add text when the EditBox is full.
607 // Changing it to false will allow you to scroll the text (default).
608 // You can change the boolean with the limitTextWidth(bool) function.
609 bool m_limitTextWidth = false;
610
611 bool m_readOnly = false;
612
613 // The text inside the edit box
614 String m_text;
615 String m_displayedText; // Same as m_text unless a password char is set
616
617 String m_regexString = U".*";
618 std::wregex m_regex;
619
620 // The text alignment
621 Alignment m_textAlignment = Alignment::Left;
622
623 // The selection
624 std::size_t m_selChars = 0;
625 std::size_t m_selStart = 0;
626 std::size_t m_selEnd = 0;
627
628 // The password character
629 char32_t m_passwordChar = '\0';
630
631 // The maximum allowed characters.
632 // Zero by default, meaning no limit.
633 unsigned int m_maxChars = 0;
634
635 // When the text width is not limited, you can scroll the edit box and only a part will be visible.
636 unsigned int m_textCropPosition = 0;
637
638 // The rectangle behind the selected text
639 FloatRect m_selectedTextBackground;
640
641 // The blinking caret
642 FloatRect m_caret = {0, 0, 1, 0};
643
644 // Is there a possibility that the user is going to double click?
645 bool m_possibleDoubleClick = false;
646
647 // We need three texts for drawing + one for the default text + one more for calculations.
648 Text m_textBeforeSelection;
649 Text m_textSelection;
650 Text m_textAfterSelection;
651 Text m_defaultText;
652 Text m_textFull;
653 Text m_textSuffix;
654
655 Sprite m_sprite;
656 Sprite m_spriteHover;
657 Sprite m_spriteDisabled;
658 Sprite m_spriteFocused;
659
660 // Cached renderer properties
661 Borders m_bordersCached;
662 Padding m_paddingCached;
663 Color m_borderColorCached;
664 Color m_borderColorHoverCached;
665 Color m_borderColorDisabledCached;
666 Color m_borderColorFocusedCached;
667 Color m_backgroundColorCached;
668 Color m_backgroundColorHoverCached;
669 Color m_backgroundColorDisabledCached;
670 Color m_backgroundColorFocusedCached;
671 Color m_caretColorCached;
672 Color m_caretColorHoverCached;
673 Color m_caretColorFocusedCached;
674 Color m_selectedTextBackgroundColorCached;
675
676
678 };
679
681}
682
684
685#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:52
Definition: EditBoxRenderer.hpp:37
Edit box widget.
Definition: EditBox.hpp:46
void setFocused(bool focused) override
Focus or unfocus the widget.
static EditBox::Ptr create()
Creates a new edit box widget.
void setReadOnly(bool readOnly=true)
Makes the edit box read-only or make it writable again.
const String & getText() const
Returns the text inside the edit box. This text is not affected by the password character.
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.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
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:59
bool isTextWidthLimited() const
Checks if the text width is limited to the size of the edit box.
void setMaximumCharacters(unsigned int maxChars)
Changes the character limit.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
char32_t getPasswordCharacter() const
Returns the password character.
void setText(const String &text)
Changes the text of the editbox.
const String & getInputValidator() const
Returns the regex to which the text is matched.
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?
std::size_t getCaretPosition() const
Returns after which character the blinking cursor is currently located.
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.
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.
std::shared_ptr< const EditBox > ConstPtr
Shared constant widget pointer.
Definition: EditBox.hpp:50
void setAlignment(Alignment alignment)
Changes the text alignment.
unsigned int getMaximumCharacters() const
Returns the character limit.
void setSuffix(const String &suffix)
Places a suffix at the right side of the edit box.
String getSelectedText() const
Returns the text that you currently have selected. This text is not affected by the password characte...
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:49
Alignment getAlignment() const
Gets the current text alignment.
const String & getSuffix() const
Returns the suffix currently displayed on the right side of the edit box.
void setEnabled(bool enabled) override
Enables or disables the widget.
void setSize(const Layout2d &size) override
Changes the size of the edit box.
const String & getDefaultText() const
Returns the default text of the edit box. This is the text drawn when the edit box is empty.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
EditBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
EditBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static EditBox::Ptr copy(const EditBox::ConstPtr &editBox)
Makes a copy of another edit box.
void setPasswordCharacter(char32_t passwordChar)
Sets a password character.
bool isReadOnly() const
Checks if the edit box read-only or writable.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
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:284
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Wrapper class to store strings.
Definition: String.hpp:79
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Predefined input validators.
Definition: EditBox.hpp:75
static TGUI_API const char * All
Accept any input.
Definition: EditBox.hpp:76
static TGUI_API const char * Float
Accept decimal numbers.
Definition: EditBox.hpp:79
static TGUI_API const char * Int
Accept negative and positive integers.
Definition: EditBox.hpp:77
static TGUI_API const char * UInt
Accept only positive integers.
Definition: EditBox.hpp:78
KeyPressed event parameters.
Definition: Event.hpp:167
States used for drawing.
Definition: RenderStates.hpp:39