TGUI  0.9-dev
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 
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 String& text);
115 
116 
123  void addText(const String& text);
124 
125 
132  String getText() const;
133 
134 
140  void setDefaultText(const String& text);
141 
142 
148  const String& getDefaultText() const;
149 
150 
157  void setSelectedText(std::size_t selectionStartIndex, std::size_t selectionEndIndex);
158 
159 
166  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 
279  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
280 
281 
288  Scrollbar::Policy getVerticalScrollbarPolicy() const;
289 
290 
297  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
298 
299 
306  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
307 
308 
317  std::size_t getLinesCount() const;
318 
319 
328  void setFocused(bool focused) override;
329 
330 
342  void enableMonospacedFontOptimization(bool enable = true);
343 
344 
350  void setVerticalScrollbarValue(unsigned int value);
351 
352 
358  unsigned int getVerticalScrollbarValue() const;
359 
360 
366  void setHorizontalScrollbarValue(unsigned int value);
367 
368 
374  unsigned int getHorizontalScrollbarValue() const;
375 
376 
383  bool mouseOnWidget(Vector2f pos) const override;
384 
388  void leftMousePressed(Vector2f pos) override;
389 
393  void leftMouseReleased(Vector2f pos) override;
394 
398  void mouseMoved(Vector2f pos) override;
399 
403  void keyPressed(const sf::Event::KeyEvent& event) override;
404 
408  void textEntered(char32_t key) override;
409 
413  bool mouseWheelScrolled(float delta, Vector2f pos) override;
414 
418  void mouseNoLongerOnWidget() override;
419 
423  void leftMouseButtonNoLongerDown() override;
424 
425 
427  protected:
428 
429 
431  // This function will search after which character the caret should be placed. It will not change the caret position.
433  Vector2<std::size_t> findCaretPosition(Vector2f position) const;
434 
435 
437  // Gets the index of either m_selStart or m_selEnd
439  std::size_t getIndexOfSelectionPos(Vector2<std::size_t> selectionPos) const;
440 
441 
443  // This function is called when you are selecting text.
444  // It will find out which part of the text is selected.
446  void selectText(float posX, float posY);
447 
448 
450  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
451  // some characters selected.
453  void deleteSelectedCharacters();
454 
455 
457  // Rearrange the text inside the text box (by using word wrap).
459  void rearrangeText(bool keepSelection);
460 
462  // Updates the physical size of the scrollbars, as well as the viewport size.
464  void updateScrollbars();
465 
467  // This function will split the text into five pieces so that the text can be easily drawn.
469  void updateSelectionTexts();
470 
471 
479  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
480 
481 
483  protected:
484 
486  // Returns the size without the borders
488  Vector2f getInnerSize() const;
489 
490 
492  // This function is called every frame with the time passed since the last frame.
494  void update(Duration elapsedTime) override;
495 
496 
498  // Recalculates the positions of the contents of the text box.
500  void recalculatePositions();
501 
502 
504  // Recalculates which lines are currently visible.
506  void recalculateVisibleLines();
507 
508 
518  Signal& getSignal(String signalName) override;
519 
520 
527  void rendererChanged(const String& property) override;
528 
529 
533  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
534 
535 
539  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
540 
541 
543  // Makes a copy of the widget
545  Widget::Ptr clone() const override
546  {
547  return std::make_shared<TextBox>(*this);
548  }
549 
550 
552  public:
553 
554  SignalString onTextChange = {"TextChanged"};
555  Signal onSelectionChange = {"SelectionChanged"};
556 
557 
559  protected:
560 
561  String m_text;
562  unsigned int m_lineHeight = 24;
563 
564  // The width of the largest line
565  float m_maxLineWidth;
566 
567  std::vector<String> m_lines;
568 
569  // The maximum characters (0 by default, which means no limit)
570  std::size_t m_maxChars = 0;
571 
572  // What is known about the visible lines?
573  std::size_t m_topLine = 1;
574  std::size_t m_visibleLines = 1;
575 
576  // Information about the selection
577  Vector2<std::size_t> m_selStart;
578  Vector2<std::size_t> m_selEnd;
579  std::pair<Vector2<std::size_t>, Vector2<std::size_t>> m_lastSelection;
580 
581  // Information about the caret
582  Vector2f m_caretPosition;
583  bool m_caretVisible = true;
584 
585  Text m_textBeforeSelection;
586  Text m_textSelection1;
587  Text m_textSelection2;
588  Text m_textAfterSelection1;
589  Text m_textAfterSelection2;
590  Text m_defaultText;
591 
592  std::vector<FloatRect> m_selectionRects;
593 
594  // The scrollbars
595  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
596  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
597  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
598  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Never;
599 
600  // Is there a possibility that the user is going to double click?
601  bool m_possibleDoubleClick = false;
602 
603  bool m_readOnly = false;
604 
605  bool m_monospacedFontOptimizationEnabled = false;
606 
607  Sprite m_spriteBackground;
608 
609  // Cached renderer properties
610  Borders m_bordersCached;
611  Padding m_paddingCached;
612  Color m_borderColorCached;
613  Color m_backgroundColorCached;
614  Color m_caretColorCached;
615  Color m_selectedTextBackgroundColorCached;
616  float m_caretWidthCached = 1;
617 
619  };
620 
622 }
623 
625 
626 #endif // TGUI_TEXT_BOX_HPP
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:216
Text box widget.
Definition: TextBox.hpp:46
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
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:47
std::shared_ptr< const TextBox > ConstPtr
Shared constant widget pointer.
Definition: TextBox.hpp:51
Wrapper for durations.
Definition: Duration.hpp:48
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:70
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:545
Definition: Outline.hpp:38
The parent class for every widget.
Definition: Widget.hpp:68
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.