TGUI  0.8.7
TextBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 
77  static TextBox::Ptr copy(TextBox::ConstPtr textBox);
78 
79 
84  TextBoxRenderer* getSharedRenderer();
85  const TextBoxRenderer* getSharedRenderer() const;
86 
92  TextBoxRenderer* getRenderer();
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);
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
273  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 
327  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
328 
329 
336  Scrollbar::Policy getVerticalScrollbarPolicy() const;
337 
338 
345  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
346 
347 
354  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
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
491  // 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 
535  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
536 
537 
539  protected:
540 
542  // Returns the size without the borders
544  Vector2f getInnerSize() const;
545 
546 
548  // This function is called every frame with the time passed since the last frame.
550  void update(sf::Time elapsedTime) override;
551 
552 
554  // Recalculates the positions of the contents of the text box.
556  void recalculatePositions();
557 
558 
560  // Recalculates which lines are currently visible.
562  void recalculateVisibleLines();
563 
564 
574  Signal& getSignal(std::string signalName) override;
575 
576 
583  void rendererChanged(const std::string& property) override;
584 
585 
589  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
590 
591 
595  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
596 
597 
599  // Makes a copy of the widget
601  Widget::Ptr clone() const override
602  {
603  return std::make_shared<TextBox>(*this);
604  }
605 
606 
608  public:
609 
610  SignalString onTextChange = {"TextChanged"};
611  Signal onSelectionChange = {"SelectionChanged"};
612 
613 
615  protected:
616 
617  sf::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<sf::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  sf::Vector2<std::size_t> m_selStart;
634  sf::Vector2<std::size_t> m_selEnd;
635  std::pair<sf::Vector2<std::size_t>, sf::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_BOX_HPP
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:220
Text box widget.
Definition: TextBox.hpp:46
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Text.hpp:42
Definition: TextBoxRenderer.hpp:36
@ Automatic
Show the scrollbar only when needed (default)
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:48
std::shared_ptr< const TextBox > ConstPtr
Shared constant widget pointer.
Definition: TextBox.hpp:51
Definition: Sprite.hpp:45
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: TextBox.hpp:601
Definition: Vector2f.hpp:38
Definition: Outline.hpp:38
The parent class for every widget.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< TextBox > Ptr
Shared widget pointer.
Definition: TextBox.hpp:50
@ Never
Never show the scrollbar, even if the contents does not fit.