TGUI  0.8.1
TextBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 
243  void setVerticalScrollbarPresent(bool present);
244 
245 
254  bool isVerticalScrollbarPresent() const;
255 
256 
265  std::size_t getLinesCount() const;
266 
267 
276  void setFocused(bool focused) override;
277 
278 
285  bool mouseOnWidget(Vector2f pos) const override;
286 
290  void leftMousePressed(Vector2f pos) override;
291 
295  void leftMouseReleased(Vector2f pos) override;
296 
300  void mouseMoved(Vector2f pos) override;
301 
305  void keyPressed(const sf::Event::KeyEvent& event) override;
306 
310  void textEntered(std::uint32_t Key) override;
311 
315  bool mouseWheelScrolled(float delta, Vector2f pos) override;
316 
320  void mouseNoLongerOnWidget() override;
321 
325  void mouseNoLongerDown() override;
326 
327 
329  protected:
330 
331 
333  // This function will search after which character the caret should be placed. It will not change the caret position.
335  sf::Vector2<std::size_t> findCaretPosition(Vector2f position) const;
336 
337 
339  // Converts the two dimensional selection positions into one dimensional positions in the text.
340  // The first element of the pair is the selection start and the second one is the selection end.
342  std::pair<std::size_t, std::size_t> findTextSelectionPositions() const;
343 
344 
346  // This function is called when you are selecting text.
347  // It will find out which part of the text is selected.
349  void selectText(float posX, float posY);
350 
351 
353  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
354  // some characters selected.
356  void deleteSelectedCharacters();
357 
358 
360  // Rearrange the text inside the text box (by using word wrap).
362  void rearrangeText(bool keepSelection);
363 
364 
366  // This function will split the text into five pieces so that the text can be easily drawn.
368  void updateSelectionTexts();
369 
370 
378  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
379 
380 
382  protected:
383 
385  // Returns the size without the borders
387  Vector2f getInnerSize() const;
388 
389 
391  // This function is called every frame with the time passed since the last frame.
393  void update(sf::Time elapsedTime) override;
394 
395 
397  // Recalculates the positions of the contents of the text box.
399  void recalculatePositions();
400 
401 
403  // Recalculates which lines are currently visible.
405  void recalculateVisibleLines();
406 
407 
417  Signal& getSignal(std::string signalName) override;
418 
419 
426  void rendererChanged(const std::string& property) override;
427 
428 
432  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
433 
434 
438  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
439 
440 
442  // Makes a copy of the widget
444  Widget::Ptr clone() const override
445  {
446  return std::make_shared<TextBox>(*this);
447  }
448 
449 
451  public:
452 
453  SignalString onTextChange = {"TextChanged"};
454 
455 
457  protected:
458 
459  sf::String m_text;
460  unsigned int m_textSize = 18;
461  unsigned int m_lineHeight = 24;
462 
463  std::vector<sf::String> m_lines;
464 
465  // The maximum characters (0 by default, which means no limit)
466  std::size_t m_maxChars = 0;
467 
468  // What is known about the visible lines?
469  std::size_t m_topLine = 1;
470  std::size_t m_visibleLines = 1;
471 
472  // Information about the selection
473  sf::Vector2<std::size_t> m_selStart;
474  sf::Vector2<std::size_t> m_selEnd;
475 
476  // Information about the caret
477  Vector2f m_caretPosition;
478  bool m_caretVisible = true;
479 
480  Text m_textBeforeSelection;
481  Text m_textSelection1;
482  Text m_textSelection2;
483  Text m_textAfterSelection1;
484  Text m_textAfterSelection2;
485 
486  std::vector<FloatRect> m_selectionRects;
487 
488  // The scrollbar
489  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScroll;
490 
491  // Is there a possibility that the user is going to double click?
492  bool m_possibleDoubleClick = false;
493 
494  bool m_readOnly = false;
495 
496  Sprite m_spriteBackground;
497 
498  // Cached renderer properties
499  Borders m_bordersCached;
500  Padding m_paddingCached;
501  Color m_borderColorCached;
502  Color m_backgroundColorCached;
503  Color m_caretColorCached;
504  Color m_selectedTextBackgroundColorCached;
505  float m_caretWidthCached = 1;
506 
508  };
509 
511 }
512 
514 
515 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:43
Wrapper for colors.
Definition: Color.hpp:47
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.
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:444
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