TGUI  0.8.3
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 
141  sf::String getSelectedText() const;
142 
143 
151  void setTextSize(unsigned int size);
152 
153 
160  unsigned int getTextSize() const;
161 
162 
172  void setMaximumCharacters(std::size_t maxChars = 0);
173 
174 
184  std::size_t getMaximumCharacters() const;
185 
186 
195  void setCaretPosition(std::size_t charactersBeforeCaret);
196 
197 
206  std::size_t getCaretPosition() const;
207 
208 
218  void setReadOnly(bool readOnly = true);
219 
220 
230  bool isReadOnly() const;
231 
232 #ifndef TGUI_REMOVE_DEPRECATED_CODE
233  TGUI_DEPRECATED("Use setHorizontalScrollbarPolicy instead") void setHorizontalScrollbarPresent(bool present);
244 
245 
254  TGUI_DEPRECATED("Use getHorizontalScrollbarPolicy instead") bool isHorizontalScrollbarPresent() const;
255 
256 
267  TGUI_DEPRECATED("Use setVerticalScrollbarPolicy instead") void setVerticalScrollbarPresent(bool present);
268 
269 
278  TGUI_DEPRECATED("Use getVerticalScrollbarPolicy instead") bool isVerticalScrollbarPresent() const;
279 #endif
280 
287  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
288 
289 
296  Scrollbar::Policy getVerticalScrollbarPolicy() const;
297 
298 
305  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
306 
307 
314  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
315 
316 
325  std::size_t getLinesCount() const;
326 
327 
336  void setFocused(bool focused) override;
337 
338 
350  void enableMonospacedFontOptimization(bool enable = true);
351 
352 
359  bool mouseOnWidget(Vector2f pos) const override;
360 
364  void leftMousePressed(Vector2f pos) override;
365 
369  void leftMouseReleased(Vector2f pos) override;
370 
374  void mouseMoved(Vector2f pos) override;
375 
379  void keyPressed(const sf::Event::KeyEvent& event) override;
380 
384  void textEntered(std::uint32_t Key) override;
385 
389  bool mouseWheelScrolled(float delta, Vector2f pos) override;
390 
394  void mouseNoLongerOnWidget() override;
395 
399  void mouseNoLongerDown() override;
400 
401 
403  protected:
404 
405 
407  // This function will search after which character the caret should be placed. It will not change the caret position.
409  sf::Vector2<std::size_t> findCaretPosition(Vector2f position) const;
410 
411 
413  // Converts the two dimensional selection positions into one dimensional positions in the text.
414  // The first element of the pair is the selection start and the second one is the selection end.
416  std::pair<std::size_t, std::size_t> findTextSelectionPositions() const;
417 
418 
420  // This function is called when you are selecting text.
421  // It will find out which part of the text is selected.
423  void selectText(float posX, float posY);
424 
425 
427  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
428  // some characters selected.
430  void deleteSelectedCharacters();
431 
432 
434  // Rearrange the text inside the text box (by using word wrap).
436  void rearrangeText(bool keepSelection);
437 
439  // Updates the physical size of the scrollbars, as well as the viewport size.
441  void updateScrollbars();
442 
444  // This function will split the text into five pieces so that the text can be easily drawn.
446  void updateSelectionTexts();
447 
448 
456  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
457 
458 
460  protected:
461 
463  // Returns the size without the borders
465  Vector2f getInnerSize() const;
466 
467 
469  // This function is called every frame with the time passed since the last frame.
471  void update(sf::Time elapsedTime) override;
472 
473 
475  // Recalculates the positions of the contents of the text box.
477  void recalculatePositions();
478 
479 
481  // Recalculates which lines are currently visible.
483  void recalculateVisibleLines();
484 
485 
495  Signal& getSignal(std::string signalName) override;
496 
497 
504  void rendererChanged(const std::string& property) override;
505 
506 
510  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
511 
512 
516  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
517 
518 
520  // Makes a copy of the widget
522  Widget::Ptr clone() const override
523  {
524  return std::make_shared<TextBox>(*this);
525  }
526 
527 
529  public:
530 
531  SignalString onTextChange = {"TextChanged"};
532 
533 
535  protected:
536 
537  sf::String m_text;
538  unsigned int m_textSize = 18;
539  unsigned int m_lineHeight = 24;
540 
541  // The width of the largest line
542  float m_maxLineWidth;
543 
544  std::vector<sf::String> m_lines;
545 
546  // The maximum characters (0 by default, which means no limit)
547  std::size_t m_maxChars = 0;
548 
549  // What is known about the visible lines?
550  std::size_t m_topLine = 1;
551  std::size_t m_visibleLines = 1;
552 
553  // Information about the selection
554  sf::Vector2<std::size_t> m_selStart;
555  sf::Vector2<std::size_t> m_selEnd;
556 
557  // Information about the caret
558  Vector2f m_caretPosition;
559  bool m_caretVisible = true;
560 
561  Text m_textBeforeSelection;
562  Text m_textSelection1;
563  Text m_textSelection2;
564  Text m_textAfterSelection1;
565  Text m_textAfterSelection2;
566 
567  std::vector<FloatRect> m_selectionRects;
568 
569  // The scrollbars
570  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
571  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
572  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
573  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Never;
574 
575  // Is there a possibility that the user is going to double click?
576  bool m_possibleDoubleClick = false;
577 
578  bool m_readOnly = false;
579 
580  bool m_monospacedFontOptimizationEnabled = false;
581 
582  Sprite m_spriteBackground;
583 
584  // Cached renderer properties
585  Borders m_bordersCached;
586  Padding m_paddingCached;
587  Color m_borderColorCached;
588  Color m_backgroundColorCached;
589  Color m_caretColorCached;
590  Color m_selectedTextBackgroundColorCached;
591  float m_caretWidthCached = 1;
592 
594  };
595 
597 }
598 
600 
601 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Show the scrollbar only when needed (default)
Definition: Sprite.hpp:43
Wrapper for colors.
Definition: Color.hpp:47
Never show the scrollbar, even if the contents does not fit.
Class to store the position or size of a widget.
Definition: Layout.hpp:245
The parent class for every widget.
Definition: Widget.hpp:72
Definition: Vector2f.hpp:37
Text box widget.
Definition: TextBox.hpp:46
Definition: Text.hpp:42
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
std::shared_ptr< TextBox > Ptr
Shared widget pointer.
Definition: TextBox.hpp:50
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: TextBox.hpp:522
Definition: CopiedSharedPtr.hpp:39
Definition: Outline.hpp:38
std::shared_ptr< const TextBox > ConstPtr
Shared constant widget pointer.
Definition: TextBox.hpp:51
Definition: TextBoxRenderer.hpp:36