TGUI  0.8-dev
TextBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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/Widgets/Scrollbar.hpp>
31 #include <TGUI/Renderers/TextBoxRenderer.hpp>
32 #include <TGUI/Text.hpp>
33 
35 
36 namespace tgui
37 {
53  class TGUI_API TextBox : public Widget
54  {
55  public:
56 
57  typedef std::shared_ptr<TextBox> Ptr;
58  typedef std::shared_ptr<const TextBox> ConstPtr;
59 
60 
62  // Default constructor
64  TextBox();
65 
66 
73  static TextBox::Ptr create();
74 
75 
84  static TextBox::Ptr copy(TextBox::ConstPtr textBox);
85 
86 
94  {
95  return aurora::downcast<TextBoxRenderer*>(m_renderer.get());
96  }
97 
98 
107  virtual void setSize(const Layout2d& size) override;
109 
110 
117  void setText(const sf::String& text);
118 
119 
126  void addText(const sf::String& text);
127 
128 
135  const sf::String& getText() const;
136 
137 
144  sf::String getSelectedText() const;
145 
146 
154  void setTextSize(unsigned int size);
155 
156 
163  unsigned int getTextSize() const;
164 
165 
175  void setMaximumCharacters(std::size_t maxChars = 0);
176 
177 
187  std::size_t getMaximumCharacters() const;
188 
189 
198  void setCaretPosition(std::size_t charactersBeforeCaret);
199 
200 
209  std::size_t getCaretPosition() const;
210 
211 
221  void setReadOnly(bool readOnly = true);
222 
223 
233  bool isReadOnly() const;
234 
235 
246  void setVerticalScrollbarPresent(bool present);
247 
248 
257  bool isVerticalScrollbarPresent() const;
258 
259 
268  std::size_t getLinesCount() const;
269 
270 
274  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
275 
279  virtual void leftMousePressed(sf::Vector2f pos) override;
280 
284  virtual void leftMouseReleased(sf::Vector2f pos) override;
285 
289  virtual void mouseMoved(sf::Vector2f pos) override;
290 
294  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
295 
299  virtual void textEntered(sf::Uint32 Key) override;
300 
304  virtual void mouseWheelScrolled(float delta, int x, int y) override;
305 
309  virtual void mouseNoLongerOnWidget() override;
310 
314  virtual void mouseNoLongerDown() override;
315 
319  virtual void widgetFocused() override;
320 
324  virtual void widgetUnfocused() override;
325 
326 
328  protected:
329 
330 
332  // This function will search after which character the caret should be placed. It will not change the caret position.
334  sf::Vector2<std::size_t> findCaretPosition(sf::Vector2f position) const;
335 
336 
338  // Converts the two dimensional selection positions into one dimensional positions in the text.
339  // The first element of the pair is the selection start and the second one is the selection end.
341  std::pair<std::size_t, std::size_t> findTextSelectionPositions() const;
342 
343 
345  // This function is called when you are selecting text.
346  // It will find out which part of the text is selected.
348  void selectText(float posX, float posY);
349 
350 
352  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
353  // some characters selected.
355  void deleteSelectedCharacters();
356 
357 
359  // Rearrange the text inside the text box (by using word wrap).
361  void rearrangeText(bool keepSelection);
362 
363 
365  // This function will split the text into five pieces so that the text can be easily drawn.
367  void updateSelectionTexts();
368 
369 
377  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
378 
379 
381  protected:
382 
384  // Returns the size without the borders
386  sf::Vector2f getInnerSize() const;
387 
388 
390  // This function is called every frame with the time passed since the last frame.
392  virtual void update(sf::Time elapsedTime) override;
393 
394 
396  // Recalculates the positions of the contents of the text box.
398  void recalculatePositions();
399 
400 
402  // Recalculates which lines are currently visible.
404  void recalculateVisibleLines();
405 
406 
413  virtual void rendererChanged(const std::string& property) override;
414 
415 
417  // Makes a copy of the widget
419  virtual Widget::Ptr clone() const override
420  {
421  return std::make_shared<TextBox>(*this);
422  }
423 
424 
426  protected:
427 
428  sf::String m_text;
429  unsigned int m_textSize = 18;
430  unsigned int m_lineHeight = 24;
431 
432  std::vector<sf::String> m_lines;
433 
434  // The maximum characters (0 by default, which means no limit)
435  std::size_t m_maxChars = 0;
436 
437  // What is known about the visible lines?
438  std::size_t m_topLine = 1;
439  std::size_t m_visibleLines = 1;
440 
441  // Information about the selection
442  sf::Vector2<std::size_t> m_selStart;
443  sf::Vector2<std::size_t> m_selEnd;
444 
445  // Information about the caret
446  sf::Vector2f m_caretPosition;
447  bool m_caretVisible = true;
448 
449  Text m_textBeforeSelection;
450  Text m_textSelection1;
451  Text m_textSelection2;
452  Text m_textAfterSelection1;
453  Text m_textAfterSelection2;
454 
455  std::vector<sf::FloatRect> m_selectionRects;
456 
457  // The scrollbar
458  ScrollbarChildWidget m_verticalScroll;
459 
460  // Is there a possibility that the user is going to double click?
461  bool m_possibleDoubleClick = false;
462 
463  bool m_readOnly = false;
464 
465  Sprite m_spriteBackground;
466 
467  // Cached renderer properties
468  Borders m_bordersCached;
469  Padding m_paddingCached;
470  Color m_borderColorCached;
471  Color m_backgroundColorCached;
472  Color m_caretColorCached;
473  Color m_selectedTextBackgroundColorCached;
474  float m_caretWidthCached = 0;
475 
477  };
478 
480 }
481 
483 
484 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:263
The parent class for every widget.
Definition: Widget.hpp:72
Text box widget.
Definition: TextBox.hpp:53
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:355
Definition: Text.hpp:41
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: TextBox.hpp:419
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
std::shared_ptr< TextBox > Ptr
Shared widget pointer.
Definition: TextBox.hpp:57
Definition: Outline.hpp:37
TextBoxRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: TextBox.hpp:93
std::shared_ptr< const TextBox > ConstPtr
Shared constant widget pointer.
Definition: TextBox.hpp:58
Definition: TextBoxRenderer.hpp:36