TGUI  0.9.5
Loading...
Searching...
No Matches
TextArea.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
26#ifndef TGUI_TEXT_AREA_HPP
27#define TGUI_TEXT_AREA_HPP
28
29
30#include <TGUI/CopiedSharedPtr.hpp>
31#include <TGUI/Widgets/Scrollbar.hpp>
32#include <TGUI/Renderers/TextAreaRenderer.hpp>
33#include <TGUI/Text.hpp>
34
36
37namespace tgui
38{
45 class TGUI_API TextArea : public Widget
46 {
47 public:
48
49 typedef std::shared_ptr<TextArea> Ptr;
50 typedef std::shared_ptr<const TextArea> ConstPtr;
51
52
60 TextArea(const char* typeName = "TextArea", bool initRenderer = true);
61
62
70
71
80
81
87 const TextAreaRenderer* getSharedRenderer() const;
88
95 const TextAreaRenderer* getRenderer() const;
96
97
106 void setSize(const Layout2d& size) override;
107 using Widget::setSize;
108
109
116 void setText(const String& text);
117
118
125 void addText(const String& text);
126
127
135
136
142 void setDefaultText(const String& text);
143
144
150 const String& getDefaultText() const;
151
152
159 void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex);
160
161
169
170
181 std::size_t getSelectionStart() const;
182
183
194 std::size_t getSelectionEnd() const;
195
196
204 void setTextSize(unsigned int size) override;
205
206
216 void setMaximumCharacters(std::size_t maxChars = 0);
217
218
228 std::size_t getMaximumCharacters() const;
229
230
238 void setCaretPosition(std::size_t charactersBeforeCaret);
239
240
248 std::size_t getCaretPosition() const;
249
250
260 void setReadOnly(bool readOnly = true);
261
262
272 bool isReadOnly() const;
273
274
282
283
291
292
300
301
309
310
319 std::size_t getLinesCount() const;
320
321
330 void setFocused(bool focused) override;
331
332
344 void enableMonospacedFontOptimization(bool enable = true);
345
346
352 void setVerticalScrollbarValue(unsigned int value);
353
354
360 unsigned int getVerticalScrollbarValue() const;
361
362
368 void setHorizontalScrollbarValue(unsigned int value);
369
370
376 unsigned int getHorizontalScrollbarValue() const;
377
378
385 bool isMouseOnWidget(Vector2f pos) const override;
386
390 void leftMousePressed(Vector2f pos) override;
391
395 void leftMouseReleased(Vector2f pos) override;
396
400 void mouseMoved(Vector2f pos) override;
401
405 void keyPressed(const Event::KeyEvent& event) override;
406
410 void textEntered(char32_t key) override;
411
415 bool mouseWheelScrolled(float delta, Vector2f pos) override;
416
420 void mouseNoLongerOnWidget() override;
421
425 void leftMouseButtonNoLongerDown() override;
426
427
429 protected:
430
431
433 // This function will search after which character the caret should be placed. It will not change the caret position.
435 Vector2<std::size_t> findCaretPosition(Vector2f position) const;
436
437
439 // Gets the index of either m_selStart or m_selEnd
441 std::size_t getIndexOfSelectionPos(Vector2<std::size_t> selectionPos) const;
442
443
445 // This function is called when you are selecting text.
446 // It will find out which part of the text is selected.
448 void selectText(float posX, float posY);
449
450
452 // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
453 // some characters selected.
455 void deleteSelectedCharacters();
456
457
459 // Rearrange the text inside the text area (by using word wrap).
461 void rearrangeText(bool keepSelection);
462
464 // Updates the physical size of the scrollbars, as well as the viewport size.
466 void updateScrollbars();
467
469 // This function will split the text into five pieces so that the text can be easily drawn.
471 void updateSelectionTexts();
472
473
475 // Handles "Backspace" key press
477 void backspaceKeyPressed();
478
480 // Handles "Delete" key press
482 void deleteKeyPressed();
483
485 // Handles "Ctrl+C" key press (or equivalent on macOS)
487 void copySelectedTextToClipboard();
488
490 // Handles "Ctrl+X" key press (or equivalent on macOS)
492 void cutSelectedTextToClipboard();
493
495 // Handles "Ctrl+V" key press (or equivalent on macOS)
497 void pasteTextFromClipboard();
498
500 // Handles "Ctrl+A" key press (or equivalent on macOS)
502 void selectAllText();
503
505 // Handles "PageUp" key press
507 void moveCaretPageUp();
508
510 // Handles "PageDown" key press
512 void moveCaretPageDown();
513
515 // Handles "ArrowLeft" key press
517 void moveCaretLeft(bool shiftPressed);
518
520 // Handles "ArrowRight" key press
522 void moveCaretRight(bool shiftPressed);
523
525 // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
527 void moveCaretWordBegin();
528
530 // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
532 void moveCaretWordEnd();
533
534
542 void draw(BackendRenderTargetBase& target, RenderStates states) const override;
543
544
546 protected:
547
549 // Returns the size without the borders
551 Vector2f getInnerSize() const;
552
553
555 // This function is called every frame with the time passed since the last frame.
557 bool updateTime(Duration elapsedTime) override;
558
559
561 // Recalculates the positions of the contents of the text area.
563 void recalculatePositions();
564
565
567 // Recalculates which lines are currently visible.
569 void recalculateVisibleLines();
570
571
581 Signal& getSignal(String signalName) override;
582
583
590 void rendererChanged(const String& property) override;
591
592
596 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
597
598
602 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
603
604
606 // Makes a copy of the widget
608 Widget::Ptr clone() const override
609 {
610 return std::make_shared<TextArea>(*this);
611 }
612
613
615 public:
616
617 SignalString onTextChange = {"TextChanged"};
618 Signal onSelectionChange = {"SelectionChanged"};
619
620
622 protected:
623
624 String m_text;
625 unsigned int m_lineHeight = 24;
626
627 // The width of the largest line
628 float m_maxLineWidth;
629
630 std::vector<String> m_lines;
631
632 // The maximum characters (0 by default, which means no limit)
633 std::size_t m_maxChars = 0;
634
635 // What is known about the visible lines?
636 std::size_t m_topLine = 1;
637 std::size_t m_visibleLines = 1;
638
639 // Information about the selection
640 Vector2<std::size_t> m_selStart;
641 Vector2<std::size_t> m_selEnd;
642 std::pair<Vector2<std::size_t>, Vector2<std::size_t>> m_lastSelection;
643
644 // Information about the caret
645 Vector2f m_caretPosition;
646 bool m_caretVisible = true;
647
648 Text m_textBeforeSelection;
649 Text m_textSelection1;
650 Text m_textSelection2;
651 Text m_textAfterSelection1;
652 Text m_textAfterSelection2;
653 Text m_defaultText;
654
655 std::vector<FloatRect> m_selectionRects;
656
657 // The scrollbars
658 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
659 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
660 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
661 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Never;
662
663 // Is there a possibility that the user is going to double click?
664 bool m_possibleDoubleClick = false;
665
666 bool m_readOnly = false;
667
668 bool m_monospacedFontOptimizationEnabled = false;
669
670 Sprite m_spriteBackground;
671
672 // Cached renderer properties
673 Borders m_bordersCached;
674 Padding m_paddingCached;
675 Color m_borderColorCached;
676 Color m_backgroundColorCached;
677 Color m_caretColorCached;
678 Color m_selectedTextBackgroundColorCached;
679 float m_caretWidthCached = 1;
680
682 };
683
685}
686
688
689#endif // TGUI_TEXT_AREA_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Wrapper for colors.
Definition Color.hpp:63
Definition CopiedSharedPtr.hpp:40
Wrapper for durations.
Definition Duration.hpp:52
Class to store the position or size of a widget.
Definition Layout.hpp:262
Definition Outline.hpp:39
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:50
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:58
Definition Sprite.hpp:49
Wrapper class to store strings.
Definition String.hpp:79
Definition TextAreaRenderer.hpp:37
Text area widget.
Definition TextArea.hpp:46
std::shared_ptr< TextArea > Ptr
Shared widget pointer.
Definition TextArea.hpp:49
void setReadOnly(bool readOnly=true)
Makes the text area read-only or make it writable again.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void enableMonospacedFontOptimization(bool enable=true)
Changes whether an optimization is made that only works when using a monospaced font.
String getText() const
Returns the text of the text area.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void setFocused(bool focused) override
Focus or unfocus the widget.
const String & getDefaultText() const
Returns the default text of the text area. This is the text drawn when the text area is empty.
void setText(const String &text)
Changes the text of the text area.
void setHorizontalScrollbarValue(unsigned int value)
Changes the thumb position of the horizontal scrollbar.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
Scrollbar::Policy getHorizontalScrollbarPolicy() const
Returns when the horizontal scrollbar should be displayed.
std::size_t getCaretPosition() const
Returns after which character the blinking cursor is currently located.
TextAreaRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::size_t getSelectionEnd() const
Returns the index where the selection ends.
bool isReadOnly() const
Checks if the text area read-only or writable.
std::size_t getLinesCount() const
Returns the amount of lines that the text occupies in the TextArea.
unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
void setMaximumCharacters(std::size_t maxChars=0)
Changes the maximum character limit.
void setCaretPosition(std::size_t charactersBeforeCaret)
Sets the blinking caret to after a specific character.
String getSelectedText() const
Returns the text that you currently have selected.
void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex)
Changes which part of the text is selected.
void setHorizontalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the horizontal scrollbar should be displayed.
TextAreaRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setVerticalScrollbarValue(unsigned int value)
Changes the thumb position of the vertical scrollbar.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition TextArea.hpp:608
unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
std::size_t getSelectionStart() const
Returns the index where the selection starts.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
static TextArea::Ptr create()
Creates a new text area widget.
static TextArea::Ptr copy(TextArea::ConstPtr textArea)
Makes a copy of another text area.
std::shared_ptr< const TextArea > ConstPtr
Shared constant widget pointer.
Definition TextArea.hpp:50
void addText(const String &text)
Appends some text to the text that was already in the text area.
std::size_t getMaximumCharacters() const
Returns the maximum character limit.
void draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
void setDefaultText(const String &text)
Changes the default text of the text area. This is the text drawn when the text area is empty.
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 setTextSize(unsigned int size) override
Changes the character size of the text.
void setSize(const Layout2d &size) override
Changes the size of the text area.
Scrollbar::Policy getVerticalScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
void setVerticalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the vertical scrollbar should be displayed.
Definition Text.hpp:44
The parent class for every widget.
Definition Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
KeyPressed event parameters.
Definition Event.hpp:167
States used for drawing.
Definition RenderStates.hpp:39