TGUI  0.8.5
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  void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex);
142 
143 
150  sf::String getSelectedText() const;
151 
152 
160  void setTextSize(unsigned int size);
161 
162 
169  unsigned int getTextSize() const;
170 
171 
181  void setMaximumCharacters(std::size_t maxChars = 0);
182 
183 
193  std::size_t getMaximumCharacters() const;
194 
195 
204  void setCaretPosition(std::size_t charactersBeforeCaret);
205 
206 
215  std::size_t getCaretPosition() const;
216 
217 
227  void setReadOnly(bool readOnly = true);
228 
229 
239  bool isReadOnly() const;
240 
241 #ifndef TGUI_REMOVE_DEPRECATED_CODE
242  TGUI_DEPRECATED("Use setHorizontalScrollbarPolicy instead") void setHorizontalScrollbarPresent(bool present);
253 
254 
263  TGUI_DEPRECATED("Use getHorizontalScrollbarPolicy instead") bool isHorizontalScrollbarPresent() const;
264 
265 
276  TGUI_DEPRECATED("Use setVerticalScrollbarPolicy instead") void setVerticalScrollbarPresent(bool present);
277 
278 
287  TGUI_DEPRECATED("Use getVerticalScrollbarPolicy instead") bool isVerticalScrollbarPresent() const;
288 #endif
289 
296  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
297 
298 
305  Scrollbar::Policy getVerticalScrollbarPolicy() const;
306 
307 
314  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
315 
316 
323  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
324 
325 
334  std::size_t getLinesCount() const;
335 
336 
345  void setFocused(bool focused) override;
346 
347 
359  void enableMonospacedFontOptimization(bool enable = true);
360 
361 
368  bool mouseOnWidget(Vector2f pos) const override;
369 
373  void leftMousePressed(Vector2f pos) override;
374 
378  void leftMouseReleased(Vector2f pos) override;
379 
383  void mouseMoved(Vector2f pos) override;
384 
388  void keyPressed(const sf::Event::KeyEvent& event) override;
389 
393  void textEntered(std::uint32_t Key) override;
394 
398  bool mouseWheelScrolled(float delta, Vector2f pos) override;
399 
403  void mouseNoLongerOnWidget() override;
404 
408  void mouseNoLongerDown() override;
409 
410 
412  protected:
413 
414 
416  // This function will search after which character the caret should be placed. It will not change the caret position.
418  sf::Vector2<std::size_t> findCaretPosition(Vector2f position) const;
419 
420 
422  // Converts the two dimensional selection positions into one dimensional positions in the text.
423  // The first element of the pair is the selection start and the second one is the selection end.
425  std::pair<std::size_t, std::size_t> findTextSelectionPositions() const;
426 
427 
429  // This function is called when you are selecting text.
430  // It will find out which part of the text is selected.
432  void selectText(float posX, float posY);
433 
434 
436  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
437  // some characters selected.
439  void deleteSelectedCharacters();
440 
441 
443  // Rearrange the text inside the text box (by using word wrap).
445  void rearrangeText(bool keepSelection);
446 
448  // Updates the physical size of the scrollbars, as well as the viewport size.
450  void updateScrollbars();
451 
453  // This function will split the text into five pieces so that the text can be easily drawn.
455  void updateSelectionTexts();
456 
457 
465  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
466 
467 
469  protected:
470 
472  // Returns the size without the borders
474  Vector2f getInnerSize() const;
475 
476 
478  // This function is called every frame with the time passed since the last frame.
480  void update(sf::Time elapsedTime) override;
481 
482 
484  // Recalculates the positions of the contents of the text box.
486  void recalculatePositions();
487 
488 
490  // Recalculates which lines are currently visible.
492  void recalculateVisibleLines();
493 
494 
504  Signal& getSignal(std::string signalName) override;
505 
506 
513  void rendererChanged(const std::string& property) override;
514 
515 
519  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
520 
521 
525  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
526 
527 
529  // Makes a copy of the widget
531  Widget::Ptr clone() const override
532  {
533  return std::make_shared<TextBox>(*this);
534  }
535 
536 
538  public:
539 
540  SignalString onTextChange = {"TextChanged"};
541 
542 
544  protected:
545 
546  sf::String m_text;
547  unsigned int m_textSize = 18;
548  unsigned int m_lineHeight = 24;
549 
550  // The width of the largest line
551  float m_maxLineWidth;
552 
553  std::vector<sf::String> m_lines;
554 
555  // The maximum characters (0 by default, which means no limit)
556  std::size_t m_maxChars = 0;
557 
558  // What is known about the visible lines?
559  std::size_t m_topLine = 1;
560  std::size_t m_visibleLines = 1;
561 
562  // Information about the selection
563  sf::Vector2<std::size_t> m_selStart;
564  sf::Vector2<std::size_t> m_selEnd;
565 
566  // Information about the caret
567  Vector2f m_caretPosition;
568  bool m_caretVisible = true;
569 
570  Text m_textBeforeSelection;
571  Text m_textSelection1;
572  Text m_textSelection2;
573  Text m_textAfterSelection1;
574  Text m_textAfterSelection2;
575 
576  std::vector<FloatRect> m_selectionRects;
577 
578  // The scrollbars
579  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
580  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
581  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
582  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Never;
583 
584  // Is there a possibility that the user is going to double click?
585  bool m_possibleDoubleClick = false;
586 
587  bool m_readOnly = false;
588 
589  bool m_monospacedFontOptimizationEnabled = false;
590 
591  Sprite m_spriteBackground;
592 
593  // Cached renderer properties
594  Borders m_bordersCached;
595  Padding m_paddingCached;
596  Color m_borderColorCached;
597  Color m_backgroundColorCached;
598  Color m_caretColorCached;
599  Color m_selectedTextBackgroundColorCached;
600  float m_caretWidthCached = 1;
601 
603  };
604 
606 }
607 
609 
610 #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:45
Wrapper for colors.
Definition: Color.hpp:48
Never show the scrollbar, even if the contents does not fit.
Class to store the position or size of a widget.
Definition: Layout.hpp:258
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't know its exact type.
Definition: TextBox.hpp:531
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