TGUI  0.9-beta
TextArea.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 
37 namespace 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 
54  // Default constructor
56  TextArea();
57 
58 
66 
67 
76 
77 
83  const TextAreaRenderer* getSharedRenderer() const;
84 
91  const TextAreaRenderer* getRenderer() const;
92 
93 
102  void setSize(const Layout2d& size) override;
103  using Widget::setSize;
104 
105 
112  void setText(const String& text);
113 
114 
121  void addText(const String& text);
122 
123 
130  String getText() const;
131 
132 
138  void setDefaultText(const String& text);
139 
140 
146  const String& getDefaultText() const;
147 
148 
155  void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex);
156 
157 
165 
166 
177  std::size_t getSelectionStart() const;
178 
179 
190  std::size_t getSelectionEnd() const;
191 
192 
200  void setTextSize(unsigned int size) override;
201 
202 
212  void setMaximumCharacters(std::size_t maxChars = 0);
213 
214 
224  std::size_t getMaximumCharacters() const;
225 
226 
234  void setCaretPosition(std::size_t charactersBeforeCaret);
235 
236 
244  std::size_t getCaretPosition() const;
245 
246 
256  void setReadOnly(bool readOnly = true);
257 
258 
268  bool isReadOnly() const;
269 
270 
278 
279 
287 
288 
296 
297 
305 
306 
315  std::size_t getLinesCount() const;
316 
317 
326  void setFocused(bool focused) override;
327 
328 
340  void enableMonospacedFontOptimization(bool enable = true);
341 
342 
348  void setVerticalScrollbarValue(unsigned int value);
349 
350 
356  unsigned int getVerticalScrollbarValue() const;
357 
358 
364  void setHorizontalScrollbarValue(unsigned int value);
365 
366 
372  unsigned int getHorizontalScrollbarValue() const;
373 
374 
381  bool isMouseOnWidget(Vector2f pos) const override;
382 
386  void leftMousePressed(Vector2f pos) override;
387 
391  void leftMouseReleased(Vector2f pos) override;
392 
396  void mouseMoved(Vector2f pos) override;
397 
401  void keyPressed(const Event::KeyEvent& event) override;
402 
406  void textEntered(char32_t key) override;
407 
411  bool mouseWheelScrolled(float delta, Vector2f pos) override;
412 
416  void mouseNoLongerOnWidget() override;
417 
421  void leftMouseButtonNoLongerDown() override;
422 
423 
425  protected:
426 
427 
429  // This function will search after which character the caret should be placed. It will not change the caret position.
431  Vector2<std::size_t> findCaretPosition(Vector2f position) const;
432 
433 
435  // Gets the index of either m_selStart or m_selEnd
437  std::size_t getIndexOfSelectionPos(Vector2<std::size_t> selectionPos) const;
438 
439 
441  // This function is called when you are selecting text.
442  // It will find out which part of the text is selected.
444  void selectText(float posX, float posY);
445 
446 
448  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
449  // some characters selected.
451  void deleteSelectedCharacters();
452 
453 
455  // Rearrange the text inside the text area (by using word wrap).
457  void rearrangeText(bool keepSelection);
458 
460  // Updates the physical size of the scrollbars, as well as the viewport size.
462  void updateScrollbars();
463 
465  // This function will split the text into five pieces so that the text can be easily drawn.
467  void updateSelectionTexts();
468 
469 
471  // Handles "Backspace" key press
473  void backspaceKeyPressed();
474 
476  // Handles "Delete" key press
478  void deleteKeyPressed();
479 
481  // Handles "Ctrl+C" key press (or equivalent on macOS)
483  void copySelectedTextToClipboard();
484 
486  // Handles "Ctrl+X" key press (or equivalent on macOS)
488  void cutSelectedTextToClipboard();
489 
491  // Handles "Ctrl+V" key press (or equivalent on macOS)
493  void pasteTextFromClipboard();
494 
496  // Handles "Ctrl+A" key press (or equivalent on macOS)
498  void selectAllText();
499 
501  // Handles "PageUp" key press
503  void moveCaretPageUp();
504 
506  // Handles "PageDown" key press
508  void moveCaretPageDown();
509 
511  // Handles "ArrowLeft" key press
513  void moveCaretLeft(bool shiftPressed);
514 
516  // Handles "ArrowRight" key press
518  void moveCaretRight(bool shiftPressed);
519 
521  // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
523  void moveCaretWordBegin();
524 
526  // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
528  void moveCaretWordEnd();
529 
530 
538  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
539 
540 
542  protected:
543 
545  // Returns the size without the borders
547  Vector2f getInnerSize() const;
548 
549 
551  // This function is called every frame with the time passed since the last frame.
553  bool updateTime(Duration elapsedTime) override;
554 
555 
557  // Recalculates the positions of the contents of the text area.
559  void recalculatePositions();
560 
561 
563  // Recalculates which lines are currently visible.
565  void recalculateVisibleLines();
566 
567 
577  Signal& getSignal(String signalName) override;
578 
579 
586  void rendererChanged(const String& property) override;
587 
588 
592  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
593 
594 
598  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
599 
600 
602  // Makes a copy of the widget
604  Widget::Ptr clone() const override
605  {
606  return std::make_shared<TextArea>(*this);
607  }
608 
609 
611  public:
612 
613  SignalString onTextChange = {"TextChanged"};
614  Signal onSelectionChange = {"SelectionChanged"};
615 
616 
618  protected:
619 
620  String m_text;
621  unsigned int m_lineHeight = 24;
622 
623  // The width of the largest line
624  float m_maxLineWidth;
625 
626  std::vector<String> m_lines;
627 
628  // The maximum characters (0 by default, which means no limit)
629  std::size_t m_maxChars = 0;
630 
631  // What is known about the visible lines?
632  std::size_t m_topLine = 1;
633  std::size_t m_visibleLines = 1;
634 
635  // Information about the selection
636  Vector2<std::size_t> m_selStart;
637  Vector2<std::size_t> m_selEnd;
638  std::pair<Vector2<std::size_t>, Vector2<std::size_t>> m_lastSelection;
639 
640  // Information about the caret
641  Vector2f m_caretPosition;
642  bool m_caretVisible = true;
643 
644  Text m_textBeforeSelection;
645  Text m_textSelection1;
646  Text m_textSelection2;
647  Text m_textAfterSelection1;
648  Text m_textAfterSelection2;
649  Text m_defaultText;
650 
651  std::vector<FloatRect> m_selectionRects;
652 
653  // The scrollbars
654  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
655  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
656  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
657  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Never;
658 
659  // Is there a possibility that the user is going to double click?
660  bool m_possibleDoubleClick = false;
661 
662  bool m_readOnly = false;
663 
664  bool m_monospacedFontOptimizationEnabled = false;
665 
666  Sprite m_spriteBackground;
667 
668  // Cached renderer properties
669  Borders m_bordersCached;
670  Padding m_paddingCached;
671  Color m_borderColorCached;
672  Color m_backgroundColorCached;
673  Color m_caretColorCached;
674  Color m_selectedTextBackgroundColorCached;
675  float m_caretWidthCached = 1;
676 
678  };
679 
681 }
682 
684 
685 #endif // TGUI_TEXT_AREA_HPP
Definition: CopiedSharedPtr.hpp:40
std::shared_ptr< TextArea > Ptr
Shared widget pointer.
Definition: TextArea.hpp:49
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:57
void setHorizontalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the horizontal scrollbar should be displayed.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void setMaximumCharacters(std::size_t maxChars=0)
Changes the maximum character limit.
String getText() const
Returns the text of the text area.
const String & getDefaultText() const
Returns the default text of the text area. This is the text drawn when the text area is empty.
std::size_t getLinesCount() const
Returns the amount of lines that the text occupies in the TextArea.
TextAreaRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setVerticalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the vertical scrollbar should be displayed.
Definition: TextAreaRenderer.hpp:37
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Text.hpp:44
bool isReadOnly() const
Checks if the text area read-only or writable.
TextAreaRenderer * getSharedRenderer()
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.
void addText(const String &text)
Appends some text to the text that was already in the text area.
@ Automatic
Show the scrollbar only when needed (default)
Class to store the position or size of a widget.
Definition: Layout.hpp:260
void setFocused(bool focused) override
Focus or unfocus the widget.
Wrapper for colors.
Definition: Color.hpp:52
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
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.
String getSelectedText() const
Returns the text that you currently have selected.
void setVerticalScrollbarValue(unsigned int value)
Changes the thumb position of the vertical scrollbar.
Wrapper for durations.
Definition: Duration.hpp:52
Definition: Sprite.hpp:49
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Wrapper class to store strings.
Definition: String.hpp:74
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:50
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
Scrollbar::Policy getHorizontalScrollbarPolicy() const
Returns when the horizontal scrollbar should be displayed.
KeyPressed event parameters.
Definition: Event.hpp:167
void enableMonospacedFontOptimization(bool enable=true)
Changes whether an optimization is made that only works when using a monospaced font.
void draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
Definition: Outline.hpp:39
unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
The parent class for every widget.
Definition: Widget.hpp:70
std::size_t getMaximumCharacters() const
Returns the maximum character limit.
std::shared_ptr< const TextArea > ConstPtr
Shared constant widget pointer.
Definition: TextArea.hpp:50
void setReadOnly(bool readOnly=true)
Makes the text area read-only or make it writable again.
void setTextSize(unsigned int size) override
Changes the character size of the text.
Scrollbar::Policy getVerticalScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
Text area widget.
Definition: TextArea.hpp:46
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: TextArea.hpp:604
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
States used for drawing.
Definition: RenderStates.hpp:39
void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex)
Changes which part of the text is selected.
static TextArea::Ptr copy(TextArea::ConstPtr textArea)
Makes a copy of another text area.
void setHorizontalScrollbarValue(unsigned int value)
Changes the thumb position of the horizontal scrollbar.
std::size_t getCaretPosition() const
Returns after which character the blinking cursor is currently located.
void setCaretPosition(std::size_t charactersBeforeCaret)
Sets the blinking caret to after a specific character.
std::size_t getSelectionStart() const
Returns the index where the selection starts.
static TextArea::Ptr create()
Creates a new text area widget.
void setText(const String &text)
Changes the text of the text area.
void setDefaultText(const String &text)
Changes the default text of the text area. This is the text drawn when the text area is empty.
@ Never
Never show the scrollbar, even if the contents does not fit.
void setSize(const Layout2d &size) override
Changes the size of the text area.