TGUI  0.7.4
TextBox.hpp
1 //
3 // TGUI - Texus's 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/Widget.hpp>
31 
33 
34 namespace tgui
35 {
36  class Scrollbar;
37  class TextBoxRenderer;
38 
50  class TGUI_API TextBox : public Widget
51  {
52  public:
53 
54  typedef std::shared_ptr<TextBox> Ptr;
55  typedef std::shared_ptr<const TextBox> ConstPtr;
56 
57 
59  // Default constructor
61  TextBox();
62 
63 
70  TextBox(const TextBox& copy);
71 
72 
81  TextBox& operator= (const TextBox& right);
82 
83 
90  static TextBox::Ptr create();
91 
92 
101  static TextBox::Ptr copy(TextBox::ConstPtr textBox);
102 
103 
110  std::shared_ptr<TextBoxRenderer> getRenderer() const
111  {
112  return std::static_pointer_cast<TextBoxRenderer>(m_renderer);
113  }
114 
115 
128  virtual void setPosition(const Layout2d& position) override;
130 
131 
140  virtual void setSize(const Layout2d& size) override;
142 
143 
152  virtual sf::Vector2f getFullSize() const override;
153 
154 
163  virtual void setFont(const Font& font) override;
164 
165 
172  void setText(const sf::String& text);
173 
174 
181  void addText(const sf::String& text);
182 
183 
190  sf::String getText() const
191  {
192  return m_text;
193  }
194 
195 
202  sf::String getSelectedText() const;
203 
204 
212  void setTextSize(unsigned int size);
213 
214 
221  unsigned int getTextSize() const
222  {
223  return m_textSize;
224  }
225 
226 
236  void setMaximumCharacters(std::size_t maxChars = 0);
237 
238 
248  std::size_t getMaximumCharacters() const
249  {
250  return m_maxChars;
251  }
252 
253 
269  void setScrollbar(Scrollbar::Ptr scrollbar);
270 
271 
280  Scrollbar::Ptr getScrollbar();
281 
282 
292  void setReadOnly(bool readOnly = true);
293 
294 
304  bool isReadOnly() const;
305 
306 
313  virtual void setOpacity(float opacity) override;
314 
315 
324  virtual sf::Vector2f getWidgetOffset() const override;
325 
326 
330  virtual bool mouseOnWidget(float x, float y) const override;
331 
335  virtual void leftMousePressed(float x, float y) override;
336 
340  virtual void leftMouseReleased(float x, float y) override;
341 
345  virtual void mouseMoved(float x, float y) override;
346 
350  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
351 
355  virtual void textEntered(sf::Uint32 Key) override;
356 
360  virtual void mouseWheelMoved(int delta, int x, int y) override;
361 
365  virtual void mouseNoLongerOnWidget() override;
366 
370  virtual void mouseNoLongerDown() override;
371 
375  virtual void widgetFocused() override;
376 
380  virtual void widgetUnfocused() override;
381 
382 
384  protected:
385 
386 
388  // This function will search after which character the caret should be placed. It will not change the caret position.
390  sf::Vector2<std::size_t> findCaretPosition(sf::Vector2f position);
391 
392 
394  // Converts the two dimensional caret positions into a one dimensional position in the text.
396  std::pair<std::size_t, std::size_t> findTextCaretPosition();
397 
398 
400  // This function is called when you are selecting text.
401  // It will find out which part of the text is selected.
403  void selectText(float posX, float posY);
404 
405 
407  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
408  // some characters selected.
410  void deleteSelectedCharacters();
411 
412 
414  // Rearrange the text inside the text box (by using word wrap).
416  void rearrangeText(bool keepSelection);
417 
418 
420  // This function will split the text into five pieces so that the text can be easily drawn.
422  void updateSelectionTexts();
423 
424 
426  protected:
427 
429  // This function is called every frame with the time passed since the last frame.
431  virtual void update(sf::Time elapsedTime) override;
432 
433 
446  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
447 
448 
450  // Makes a copy of the widget
452  virtual Widget::Ptr clone() const override
453  {
454  return std::make_shared<TextBox>(*this);
455  }
456 
457 
459  // Draws the widget on the render target.
461  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
462 
463 
465  protected:
466 
467  sf::String m_text;
468  unsigned int m_textSize = 18;
469  unsigned int m_lineHeight = 24;
470 
471  std::vector<sf::String> m_lines = std::vector<sf::String>{""}; // Did not compile in VS2013 with just braces
472 
473  // The maximum characters (0 by default, which means no limit)
474  std::size_t m_maxChars = 0;
475 
476  // What is known about the visible lines?
477  std::size_t m_topLine = 1;
478  std::size_t m_visibleLines = 1;
479 
480  // Information about the selection
481  sf::Vector2<std::size_t> m_selStart;
482  sf::Vector2<std::size_t> m_selEnd;
483 
484  // Information about the caret
485  sf::Vector2f m_caretPosition;
486  bool m_caretVisible = true;
487 
488  sf::Text m_textBeforeSelection;
489  sf::Text m_textSelection1;
490  sf::Text m_textSelection2;
491  sf::Text m_textAfterSelection1;
492  sf::Text m_textAfterSelection2;
493 
494  std::vector<sf::FloatRect> m_selectionRects;
495 
496  // The scrollbar
497  Scrollbar::Ptr m_scroll = std::make_shared<Scrollbar>();
498 
499  // Is there a possibility that the user is going to double click?
500  bool m_possibleDoubleClick = false;
501 
502  bool m_readOnly = false;
503 
504  friend class TextBoxRenderer;
505 
507  };
508 
509 
511 
512  class TGUI_API TextBoxRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
513  {
514  public:
515 
522  TextBoxRenderer(TextBox* textBox) : m_textBox{textBox} {}
523 
524 
535  virtual void setProperty(std::string property, const std::string& value) override;
536 
537 
549  virtual void setProperty(std::string property, ObjectConverter&& value) override;
550 
551 
561  virtual ObjectConverter getProperty(std::string property) const override;
562 
563 
570  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
571 
572 
579  void setBackgroundColor(const Color& backgroundColor);
580 
581 
588  void setTextColor(const Color& textColor);
589 
590 
597  void setSelectedTextColor(const Color& selectedTextColor);
598 
599 
606  void setSelectedTextBackgroundColor(const Color& selectedTextBackgroundColor);
607 
608 
615  void setBorderColor(const Color& borderColor);
616 
617 
624  void setCaretColor(const Color& caretColor);
625 
626 
633  void setCaretWidth(float width);
634 
635 
645  void setBackgroundTexture(const Texture& texture);
646 
647 
657  virtual void setPadding(const Padding& padding) override;
659 
660 
674  void setScrollbar(const std::string& scrollbarThemeFileFilename = "", const std::string& section = "Scrollbar");
675 
676 
678  // Draws the widget on the render target.
680  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
681 
682 
684  private:
685 
687  // Returns the padding, which is possibly scaled with the background image.
689  Padding getScaledPadding() const;
690 
691 
693  // Makes a copy of the renderer
695  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
696 
697 
699  protected:
700 
701  TextBox* m_textBox;
702 
703  float m_caretWidth = 1;
704 
705  Texture m_backgroundTexture;
706 
707  sf::Color m_textColor;
708  sf::Color m_selectedTextColor;
709  sf::Color m_caretColor;
710  sf::Color m_backgroundColor;
711  sf::Color m_selectedTextBgrColor;
712  sf::Color m_borderColor;
713 
714  friend class TextBox;
715 
717  };
718 
720 }
721 
723 
724 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Implicit converter for colors.
Definition: Color.hpp:39
std::shared_ptr< TextBoxRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: TextBox.hpp:110
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
The parent class for every widget.
Definition: Widget.hpp:71
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:53
Text box widget.
Definition: TextBox.hpp:50
TextBoxRenderer(TextBox *textBox)
Constructor.
Definition: TextBox.hpp:522
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Parent class for every widget that has padding.
Definition: Borders.hpp:210
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: TextBox.hpp:452
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:136
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::size_t getMaximumCharacters() const
Returns the maximum character limit.
Definition: TextBox.hpp:248
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
std::shared_ptr< TextBox > Ptr
Shared widget pointer.
Definition: TextBox.hpp:54
unsigned int getTextSize() const
Returns the character size of the text.
Definition: TextBox.hpp:221
Definition: Texture.hpp:44
Definition: Borders.hpp:37
std::shared_ptr< const TextBox > ConstPtr
Shared constant widget pointer.
Definition: TextBox.hpp:55
sf::String getText() const
Returns the text of the text box.
Definition: TextBox.hpp:190
Definition: TextBox.hpp:512
Base class for all renderer classes.
Definition: Widget.hpp:682