TGUI  0.8.9
TextBox.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_BOX_HPP
27 #define TGUI_TEXT_BOX_HPP
28 
29 
30 #include <TGUI/CopiedSharedPtr.hpp>
31 #include <TGUI/Widgets/Scrollbar.hpp>
32 #include <TGUI/Renderers/TextBoxRenderer.hpp>
33 #include <TGUI/Text.hpp>
34 
36 
37 namespace tgui
38 {
46  class TGUI_API TextBox : public Widget
47  {
48  public:
49 
50  typedef std::shared_ptr<TextBox> Ptr;
51  typedef std::shared_ptr<const TextBox> ConstPtr;
52 
53 
55  // Default constructor
57  TextBox();
58 
59 
66  static TextBox::Ptr create();
67 
68 
78 
79 
85  const TextBoxRenderer* getSharedRenderer() const;
86 
93  const TextBoxRenderer* getRenderer() const;
94 
95 
104  void setSize(const Layout2d& size) override;
105  using Widget::setSize;
106 
107 
114  void setText(const sf::String& text, bool triggerTextChangedSignal = false);
115 
116 
123  void addText(const sf::String& text);
124 
125 
132  const sf::String& getText() const;
133 
134 
140  void setDefaultText(const sf::String& text);
141 
142 
148  const sf::String& getDefaultText() const;
149 
150 
157  void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex);
158 
159 
166  sf::String getSelectedText() const;
167 
168 
179  std::size_t getSelectionStart() const;
180 
181 
192  std::size_t getSelectionEnd() const;
193 
194 
202  void setTextSize(unsigned int size) override;
203 
204 
214  void setMaximumCharacters(std::size_t maxChars = 0);
215 
216 
226  std::size_t getMaximumCharacters() const;
227 
228 
236  void setCaretPosition(std::size_t charactersBeforeCaret);
237 
238 
246  std::size_t getCaretPosition() const;
247 
248 
258  void setReadOnly(bool readOnly = true);
259 
260 
270  bool isReadOnly() const;
271 
272 #ifndef TGUI_REMOVE_DEPRECATED_CODE
283  TGUI_DEPRECATED("Use setHorizontalScrollbarPolicy instead") void setHorizontalScrollbarPresent(bool present);
284 
285 
294  TGUI_DEPRECATED("Use getHorizontalScrollbarPolicy instead") bool isHorizontalScrollbarPresent() const;
295 
296 
307  TGUI_DEPRECATED("Use setVerticalScrollbarPolicy instead") void setVerticalScrollbarPresent(bool present);
308 
309 
318  TGUI_DEPRECATED("Use getVerticalScrollbarPolicy instead") bool isVerticalScrollbarPresent() const;
319 #endif
320 
328 
329 
337 
338 
346 
347 
355 
356 
365  std::size_t getLinesCount() const;
366 
367 
376  void setFocused(bool focused) override;
377 
378 
390  void enableMonospacedFontOptimization(bool enable = true);
391 
392 
398  void setVerticalScrollbarValue(unsigned int value);
399 
400 
406  unsigned int getVerticalScrollbarValue() const;
407 
408 
414  void setHorizontalScrollbarValue(unsigned int value);
415 
416 
422  unsigned int getHorizontalScrollbarValue() const;
423 
424 
431  bool mouseOnWidget(Vector2f pos) const override;
432 
436  void leftMousePressed(Vector2f pos) override;
437 
441  void leftMouseReleased(Vector2f pos) override;
442 
446  void mouseMoved(Vector2f pos) override;
447 
451  void keyPressed(const sf::Event::KeyEvent& event) override;
452 
456  void textEntered(std::uint32_t Key) override;
457 
461  bool mouseWheelScrolled(float delta, Vector2f pos) override;
462 
466  void mouseNoLongerOnWidget() override;
467 
471  void leftMouseButtonNoLongerDown() override;
472 
473 
475  protected:
476 
477 
479  // This function will search after which character the caret should be placed. It will not change the caret position.
481  sf::Vector2<std::size_t> findCaretPosition(Vector2f position) const;
482 
483 
485  // Gets the index of either m_selStart or m_selEnd
487  std::size_t getIndexOfSelectionPos(sf::Vector2<std::size_t> selectionPos) const;
488 
489 
490 #ifndef TGUI_REMOVE_DEPRECATED_CODE
492  // Converts the two dimensional selection positions into one dimensional positions in the text.
493  // The first element of the pair is the selection start and the second one is the selection end.
495  TGUI_DEPRECATED("Use getSelectionStart and getSelectionEnd instead") std::pair<std::size_t, std::size_t> findTextSelectionPositions() const;
496 #endif
497 
499  // This function is called when you are selecting text.
500  // It will find out which part of the text is selected.
502  void selectText(float posX, float posY);
503 
504 
506  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
507  // some characters selected.
509  void deleteSelectedCharacters();
510 
511 
513  // Rearrange the text inside the text box (by using word wrap).
515  void rearrangeText(bool keepSelection);
516 
518  // Updates the physical size of the scrollbars, as well as the viewport size.
520  void updateScrollbars();
521 
523  // This function will split the text into five pieces so that the text can be easily drawn.
525  void updateSelectionTexts();
526 
527 
529  // Handles "Backspace" key press
531  void backspaceKeyPressed();
532 
534  // Handles "Delete" key press
536  void deleteKeyPressed();
537 
539  // Handles "Ctrl+C" key press (or equivalent on macOS)
541  void copySelectedTextToClipboard();
542 
544  // Handles "Ctrl+X" key press (or equivalent on macOS)
546  void cutSelectedTextToClipboard();
547 
549  // Handles "Ctrl+V" key press (or equivalent on macOS)
551  void pasteTextFromClipboard();
552 
554  // Handles "Ctrl+A" key press (or equivalent on macOS)
556  void selectAllText();
557 
559  // Handles "PageUp" key press
561  void moveCaretPageUp();
562 
564  // Handles "PageDown" key press
566  void moveCaretPageDown();
567 
569  // Handles "ArrowLeft" key press
571  void moveCaretLeft(bool shiftPressed);
572 
574  // Handles "ArrowRight" key press
576  void moveCaretRight(bool shiftPressed);
577 
579  // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
581  void moveCaretWordBegin();
582 
584  // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
586  void moveCaretWordEnd();
587 
588 
596  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
597 
598 
600  protected:
601 
603  // Returns the size without the borders
605  Vector2f getInnerSize() const;
606 
607 
609  // This function is called every frame with the time passed since the last frame.
611  bool update(sf::Time elapsedTime) override;
612 
613 
615  // Recalculates the positions of the contents of the text box.
617  void recalculatePositions();
618 
619 
621  // Recalculates which lines are currently visible.
623  void recalculateVisibleLines();
624 
625 
635  Signal& getSignal(std::string signalName) override;
636 
637 
644  void rendererChanged(const std::string& property) override;
645 
646 
650  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
651 
652 
656  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
657 
658 
660  // Makes a copy of the widget
662  Widget::Ptr clone() const override
663  {
664  return std::make_shared<TextBox>(*this);
665  }
666 
667 
669  public:
670 
671  SignalString onTextChange = {"TextChanged"};
672  Signal onSelectionChange = {"SelectionChanged"};
673 
674 
676  protected:
677 
678  sf::String m_text;
679  unsigned int m_lineHeight = 24;
680 
681  // The width of the largest line
682  float m_maxLineWidth;
683 
684  std::vector<sf::String> m_lines;
685 
686  // The maximum characters (0 by default, which means no limit)
687  std::size_t m_maxChars = 0;
688 
689  // What is known about the visible lines?
690  std::size_t m_topLine = 1;
691  std::size_t m_visibleLines = 1;
692 
693  // Information about the selection
694  sf::Vector2<std::size_t> m_selStart;
695  sf::Vector2<std::size_t> m_selEnd;
696  std::pair<sf::Vector2<std::size_t>, sf::Vector2<std::size_t>> m_lastSelection;
697 
698  // Information about the caret
699  Vector2f m_caretPosition;
700  bool m_caretVisible = true;
701 
702  Text m_textBeforeSelection;
703  Text m_textSelection1;
704  Text m_textSelection2;
705  Text m_textAfterSelection1;
706  Text m_textAfterSelection2;
707  Text m_defaultText;
708 
709  std::vector<FloatRect> m_selectionRects;
710 
711  // The scrollbars
712  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
713  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
714  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
715  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Never;
716 
717  // Is there a possibility that the user is going to double click?
718  bool m_possibleDoubleClick = false;
719 
720  bool m_readOnly = false;
721 
722  bool m_monospacedFontOptimizationEnabled = false;
723 
724  Sprite m_spriteBackground;
725 
726  // Cached renderer properties
727  Borders m_bordersCached;
728  Padding m_paddingCached;
729  Color m_borderColorCached;
730  Color m_backgroundColorCached;
731  Color m_caretColorCached;
732  Color m_selectedTextBackgroundColorCached;
733  float m_caretWidthCached = 1;
734 
736  };
737 
739 }
740 
742 
743 #endif // TGUI_TEXT_BOX_HPP
Wrapper for colors.
Definition: Color.hpp:49
Definition: CopiedSharedPtr.hpp:40
Class to store the position or size of a widget.
Definition: Layout.hpp:260
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:231
Definition: Sprite.hpp:46
Definition: TextBoxRenderer.hpp:37
Text box widget.
Definition: TextBox.hpp:47
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
void setVerticalScrollbarValue(unsigned int value)
Changes the thumb position of the vertical scrollbar.
const sf::String & getDefaultText() const
Returns the default text of the text box. This is the text drawn when the text box is empty.
void setHorizontalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the horizontal scrollbar should be displayed.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
std::shared_ptr< TextBox > Ptr
Shared widget pointer.
Definition: TextBox.hpp:50
void setDefaultText(const sf::String &text)
Changes the default text of the textbox. This is the text drawn when the text box is empty.
void setMaximumCharacters(std::size_t maxChars=0)
Changes the maximum character limit.
void setFocused(bool focused) override
Focus or unfocus the widget.
std::size_t getCaretPosition() const
Returns after which character the blinking cursor is currently located.
const sf::String & getText() const
Returns the text of the text box.
unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
void setSize(const Layout2d &size) override
Changes the size of the text box.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: TextBox.hpp:662
bool isReadOnly() const
Checks if the text box read-only or writable.
void setText(const sf::String &text, bool triggerTextChangedSignal=false)
Changes the text of the text box.
TextBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static TextBox::Ptr create()
Creates a new text box widget.
void setHorizontalScrollbarValue(unsigned int value)
Changes the thumb position of the horizontal scrollbar.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
void enableMonospacedFontOptimization(bool enable=true)
Changes whether an optimization is made that only works when using a monospaced font.
void setCaretPosition(std::size_t charactersBeforeCaret)
Sets the blinking caret to after a specific character.
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.
Scrollbar::Policy getHorizontalScrollbarPolicy() const
Returns when the horizontal scrollbar should be displayed.
std::size_t getSelectionEnd() const
Returns the index where the selection ends.
void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex)
Changes which part of the text is selected.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
TextBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setReadOnly(bool readOnly=true)
Makes the text box read-only or make it writable again.
void setTextSize(unsigned int size) override
Changes the character size of the text.
static TextBox::Ptr copy(TextBox::ConstPtr textBox)
Makes a copy of another text box.
std::size_t getSelectionStart() const
Returns the index where the selection starts.
std::shared_ptr< const TextBox > ConstPtr
Shared constant widget pointer.
Definition: TextBox.hpp:51
void addText(const sf::String &text)
Appends some text to the text that was already in the text box.
std::size_t getLinesCount() const
Returns the amount of lines that the text occupies in the TextBox.
std::size_t getMaximumCharacters() const
Returns the maximum character limit.
void setVerticalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the vertical scrollbar should be displayed.
Scrollbar::Policy getVerticalScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
sf::String getSelectedText() const
Returns the text that you currently have selected.
Definition: Text.hpp:43
Definition: Vector2f.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37