TGUI  0.10-beta
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
206 void setMaximumCharacters(std::size_t maxChars = 0);
207
208
218 std::size_t getMaximumCharacters() const;
219
220
228 void setCaretPosition(std::size_t charactersBeforeCaret);
229
230
238 std::size_t getCaretPosition() const;
239
240
250 void setReadOnly(bool readOnly = true);
251
252
262 bool isReadOnly() const;
263
264
272
273
281
282
290
291
299
300
309 std::size_t getLinesCount() const;
310
311
320 void setFocused(bool focused) override;
321
322
334 void enableMonospacedFontOptimization(bool enable = true);
335
336
342 void setVerticalScrollbarValue(unsigned int value);
343
344
350 unsigned int getVerticalScrollbarValue() const;
351
352
358 void setHorizontalScrollbarValue(unsigned int value);
359
360
366 unsigned int getHorizontalScrollbarValue() const;
367
368
375 bool isMouseOnWidget(Vector2f pos) const override;
376
380 void leftMousePressed(Vector2f pos) override;
381
385 void leftMouseReleased(Vector2f pos) override;
386
390 void mouseMoved(Vector2f pos) override;
391
395 void keyPressed(const Event::KeyEvent& event) override;
396
400 void textEntered(char32_t key) override;
401
405 bool mouseWheelScrolled(float delta, Vector2f pos) override;
406
410 void mouseNoLongerOnWidget() override;
411
415 void leftMouseButtonNoLongerDown() override;
416
417
419 protected:
420
421
423 // This function will search after which character the caret should be placed. It will not change the caret position.
425 Vector2<std::size_t> findCaretPosition(Vector2f position) const;
426
427
429 // Gets the index of either m_selStart or m_selEnd
431 std::size_t getIndexOfSelectionPos(Vector2<std::size_t> selectionPos) const;
432
433
435 // This function is called when you are selecting text.
436 // It will find out which part of the text is selected.
438 void selectText(float posX, float posY);
439
440
442 // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
443 // some characters selected.
445 void deleteSelectedCharacters();
446
447
449 // Rearrange the text inside the text area (by using word wrap).
451 void rearrangeText(bool keepSelection);
452
454 // Updates the physical size of the scrollbars, as well as the viewport size.
456 void updateScrollbars();
457
459 // This function will split the text into five pieces so that the text can be easily drawn.
461 void updateSelectionTexts();
462
463
465 // Handles "Backspace" key press
467 void backspaceKeyPressed();
468
470 // Handles "Delete" key press
472 void deleteKeyPressed();
473
475 // Handles "Ctrl+C" key press (or equivalent on macOS)
477 void copySelectedTextToClipboard();
478
480 // Handles "Ctrl+X" key press (or equivalent on macOS)
482 void cutSelectedTextToClipboard();
483
485 // Handles "Ctrl+V" key press (or equivalent on macOS)
487 void pasteTextFromClipboard();
488
490 // Handles "Ctrl+A" key press (or equivalent on macOS)
492 void selectAllText();
493
495 // Handles "PageUp" key press
497 void moveCaretPageUp();
498
500 // Handles "PageDown" key press
502 void moveCaretPageDown();
503
505 // Handles "ArrowLeft" key press
507 void moveCaretLeft(bool shiftPressed);
508
510 // Handles "ArrowRight" key press
512 void moveCaretRight(bool shiftPressed);
513
515 // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
517 void moveCaretWordBegin();
518
520 // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
522 void moveCaretWordEnd();
523
524
532 void draw(BackendRenderTarget& target, RenderStates states) const override;
533
534
536 protected:
537
539 // Returns the size without the borders
541 Vector2f getInnerSize() const;
542
543
545 // This function is called every frame with the time passed since the last frame.
547 bool updateTime(Duration elapsedTime) override;
548
549
551 // Recalculates the positions of the contents of the text area.
553 void recalculatePositions();
554
555
557 // Recalculates which lines are currently visible.
559 void recalculateVisibleLines();
560
561
571 Signal& getSignal(String signalName) override;
572
573
580 void rendererChanged(const String& property) override;
581
582
586 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
587
588
592 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
593
594
598 void updateTextSize() override;
599
600
602 // Makes a copy of the widget
604 Widget::Ptr clone() const override;
605
606
608 public:
609
610 SignalString onTextChange = {"TextChanged"};
611 Signal onSelectionChange = {"SelectionChanged"};
612
613
615 protected:
616
617 String m_text;
618 unsigned int m_lineHeight = 24;
619
620 // The width of the largest line
621 float m_maxLineWidth;
622
623 std::vector<String> m_lines;
624
625 // The maximum characters (0 by default, which means no limit)
626 std::size_t m_maxChars = 0;
627
628 // What is known about the visible lines?
629 std::size_t m_topLine = 1;
630 std::size_t m_visibleLines = 1;
631
632 // Information about the selection
633 Vector2<std::size_t> m_selStart;
634 Vector2<std::size_t> m_selEnd;
635 std::pair<Vector2<std::size_t>, Vector2<std::size_t>> m_lastSelection;
636
637 // Information about the caret
638 Vector2f m_caretPosition;
639 bool m_caretVisible = true;
640
641 Text m_textBeforeSelection;
642 Text m_textSelection1;
643 Text m_textSelection2;
644 Text m_textAfterSelection1;
645 Text m_textAfterSelection2;
646 Text m_defaultText;
647
648 std::vector<FloatRect> m_selectionRects;
649
650 // The scrollbars
651 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
652 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
653 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
654 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Never;
655
656 // Is there a possibility that the user is going to double click?
657 bool m_possibleDoubleClick = false;
658
659 bool m_readOnly = false;
660
661 bool m_monospacedFontOptimizationEnabled = false;
662
663 Sprite m_spriteBackground;
664
665 // Cached renderer properties
666 Borders m_bordersCached;
667 Padding m_paddingCached;
668 Color m_borderColorCached;
669 Color m_backgroundColorCached;
670 Color m_caretColorCached;
671 Color m_selectedTextBackgroundColorCached;
672 float m_caretWidthCached = 1;
673
675 };
676
678}
679
681
682#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:284
Definition: Outline.hpp:39
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:50
@ Automatic
Show the scrollbar only when needed (default)
@ Never
Never show the scrollbar, even if the contents does not fit.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Definition: Sprite.hpp:45
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.
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.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
std::size_t getMaximumCharacters() const
Returns the maximum character limit.
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 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.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
Definition: Text.hpp:48
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
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