TGUI  1.0-alpha
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
96
105 void setSize(const Layout2d& size) override;
106 using Widget::setSize;
107
108
115 void setText(String text);
116
117
124 void addText(String text);
125
126
134
135
141 void setDefaultText(const String& text);
142
143
149 const String& getDefaultText() const;
150
151
158 void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex);
159
160
168
169
180 std::size_t getSelectionStart() const;
181
182
193 std::size_t getSelectionEnd() const;
194
195
205 void setMaximumCharacters(std::size_t maxChars = 0);
206
207
217 std::size_t getMaximumCharacters() const;
218
219
227 void setCaretPosition(std::size_t charactersBeforeCaret);
228
229
237 std::size_t getCaretPosition() const;
238
239
249 void setReadOnly(bool readOnly = true);
250
251
261 bool isReadOnly() const;
262
263
271
272
280
281
289
290
298
299
308 std::size_t getLinesCount() const;
309
310
319 void setFocused(bool focused) override;
320
321
333 void enableMonospacedFontOptimization(bool enable = true);
334
335
341 void setVerticalScrollbarValue(unsigned int value);
342
343
349 unsigned int getVerticalScrollbarValue() const;
350
351
357 void setHorizontalScrollbarValue(unsigned int value);
358
359
365 unsigned int getHorizontalScrollbarValue() const;
366
367
374 bool isMouseOnWidget(Vector2f pos) const override;
375
379 void leftMousePressed(Vector2f pos) override;
380
384 void leftMouseReleased(Vector2f pos) override;
385
389 void mouseMoved(Vector2f pos) override;
390
394 void keyPressed(const Event::KeyEvent& event) override;
395
399 void textEntered(char32_t key) override;
400
404 bool mouseWheelScrolled(float delta, Vector2f pos) override;
405
409 void mouseNoLongerOnWidget() override;
410
414 void leftMouseButtonNoLongerDown() override;
415
416
418 protected:
419
420
422 // This function will search after which character the caret should be placed. It will not change the caret position.
424 Vector2<std::size_t> findCaretPosition(Vector2f position) const;
425
426
428 // Gets the index of either m_selStart or m_selEnd
430 std::size_t getIndexOfSelectionPos(Vector2<std::size_t> selectionPos) const;
431
432
434 // This function is called when you are selecting text.
435 // It will find out which part of the text is selected.
437 void selectText(float posX, float posY);
438
439
441 // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
442 // some characters selected.
444 void deleteSelectedCharacters();
445
446
448 // Rearrange the text inside the text area (by using word wrap).
450 void rearrangeText(bool keepSelection);
451
453 // Updates the physical size of the scrollbars, as well as the viewport size.
455 void updateScrollbars();
456
458 // This function will split the text into five pieces so that the text can be easily drawn.
460 void updateSelectionTexts();
461
462
464 // Handles "Backspace" key press
466 void backspaceKeyPressed();
467
469 // Handles "Delete" key press
471 void deleteKeyPressed();
472
474 // Handles "Ctrl+C" key press (or equivalent on macOS)
476 void copySelectedTextToClipboard();
477
479 // Handles "Ctrl+X" key press (or equivalent on macOS)
481 void cutSelectedTextToClipboard();
482
484 // Handles "Ctrl+V" key press (or equivalent on macOS)
486 void pasteTextFromClipboard();
487
489 // Handles "Ctrl+A" key press (or equivalent on macOS)
491 void selectAllText();
492
494 // Handles "PageUp" key press
496 void moveCaretPageUp();
497
499 // Handles "PageDown" key press
501 void moveCaretPageDown();
502
504 // Handles "ArrowLeft" key press
506 void moveCaretLeft(bool shiftPressed);
507
509 // Handles "ArrowRight" key press
511 void moveCaretRight(bool shiftPressed);
512
514 // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
516 void moveCaretWordBegin();
517
519 // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
521 void moveCaretWordEnd();
522
523
531 void draw(BackendRenderTarget& target, RenderStates states) const override;
532
533
535 protected:
536
538 // Returns the size without the borders
540 Vector2f getInnerSize() const;
541
542
544 // This function is called every frame with the time passed since the last frame.
546 bool updateTime(Duration elapsedTime) override;
547
548
550 // Recalculates the positions of the contents of the text area.
552 void recalculatePositions();
553
554
556 // Recalculates which lines are currently visible.
558 void recalculateVisibleLines();
559
560
570 Signal& getSignal(String signalName) override;
571
572
579 void rendererChanged(const String& property) override;
580
581
585 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
586
587
591 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
592
593
597 void updateTextSize() override;
598
599
601 // Makes a copy of the widget
603 Widget::Ptr clone() const override;
604
605
607 public:
608
609 SignalString onTextChange = {"TextChanged"};
610 Signal onSelectionChange = {"SelectionChanged"};
611
612
614 protected:
615
616 String m_text;
617 unsigned int m_lineHeight = 24;
618
619 // The width of the largest line
620 float m_maxLineWidth;
621
622 std::vector<String> m_lines;
623
624 // The maximum characters (0 by default, which means no limit)
625 std::size_t m_maxChars = 0;
626
627 // What is known about the visible lines?
628 std::size_t m_topLine = 1;
629 std::size_t m_visibleLines = 1;
630
631 // Information about the selection
632 Vector2<std::size_t> m_selStart;
633 Vector2<std::size_t> m_selEnd;
634 std::pair<Vector2<std::size_t>, Vector2<std::size_t>> m_lastSelection;
635
636 // Information about the caret
637 Vector2f m_caretPosition;
638 bool m_caretVisible = true;
639
640 Text m_textBeforeSelection;
641 Text m_textSelection1;
642 Text m_textSelection2;
643 Text m_textAfterSelection1;
644 Text m_textAfterSelection2;
645 Text m_defaultText;
646
647 std::vector<FloatRect> m_selectionRects;
648
649 // The scrollbars
650 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
651 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
652 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
653 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Never;
654
655 // Is there a possibility that the user is going to double click?
656 bool m_possibleDoubleClick = false;
657
658 bool m_readOnly = false;
659
660 bool m_monospacedFontOptimizationEnabled = false;
661
662 Sprite m_spriteBackground;
663
664 // Cached renderer properties
665 Borders m_bordersCached;
666 Padding m_paddingCached;
667 Color m_borderColorCached;
668 Color m_backgroundColorCached;
669 Color m_caretColorCached;
670 Color m_selectedTextBackgroundColorCached;
671 float m_caretWidthCached = 1;
672
674 };
675
677}
678
680
681#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 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.
void addText(String text)
Appends some text to the text that was already in the text area.
std::size_t getSelectionEnd() const
Returns the index where the selection ends.
void setText(String text)
Changes the text of the text area.
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 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