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 {
45  class TGUI_API TextBox : public Widget
46  {
47  public:
48 
49  typedef std::shared_ptr<TextBox> Ptr;
50  typedef std::shared_ptr<const TextBox> ConstPtr;
51 
52 
54  // Default constructor
56  TextBox();
57 
58 
65  static TextBox::Ptr create();
66 
67 
76  static TextBox::Ptr copy(TextBox::ConstPtr textBox);
77 
78 
83  TextBoxRenderer* getSharedRenderer();
84  const TextBoxRenderer* getSharedRenderer() const;
85 
91  TextBoxRenderer* getRenderer();
92  const TextBoxRenderer* getRenderer() const;
93 
94 
103  void setSize(const Layout2d& size) override;
104  using Widget::setSize;
105 
106 
113  void setText(const sf::String& text);
114 
115 
122  void addText(const sf::String& text);
123 
124 
131  const sf::String& getText() const;
132 
133 
140  sf::String getSelectedText() const;
141 
142 
150  void setTextSize(unsigned int size);
151 
152 
159  unsigned int getTextSize() const;
160 
161 
171  void setMaximumCharacters(std::size_t maxChars = 0);
172 
173 
183  std::size_t getMaximumCharacters() const;
184 
185 
194  void setCaretPosition(std::size_t charactersBeforeCaret);
195 
196 
205  std::size_t getCaretPosition() const;
206 
207 
217  void setReadOnly(bool readOnly = true);
218 
219 
229  bool isReadOnly() const;
230 
231 
242  void setVerticalScrollbarPresent(bool present);
243 
244 
253  bool isVerticalScrollbarPresent() const;
254 
255 
264  std::size_t getLinesCount() const;
265 
266 
273  bool mouseOnWidget(sf::Vector2f pos) const override;
274 
278  void leftMousePressed(sf::Vector2f pos) override;
279 
283  void leftMouseReleased(sf::Vector2f pos) override;
284 
288  void mouseMoved(sf::Vector2f pos) override;
289 
293  void keyPressed(const sf::Event::KeyEvent& event) override;
294 
298  void textEntered(sf::Uint32 Key) override;
299 
303  void mouseWheelScrolled(float delta, sf::Vector2f pos) override;
304 
308  void mouseNoLongerOnWidget() override;
309 
313  void mouseNoLongerDown() override;
314 
318  void widgetFocused() override;
319 
323  void widgetUnfocused() override;
324 
325 
327  protected:
328 
329 
331  // This function will search after which character the caret should be placed. It will not change the caret position.
333  sf::Vector2<std::size_t> findCaretPosition(sf::Vector2f position) const;
334 
335 
337  // Converts the two dimensional selection positions into one dimensional positions in the text.
338  // The first element of the pair is the selection start and the second one is the selection end.
340  std::pair<std::size_t, std::size_t> findTextSelectionPositions() const;
341 
342 
344  // This function is called when you are selecting text.
345  // It will find out which part of the text is selected.
347  void selectText(float posX, float posY);
348 
349 
351  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
352  // some characters selected.
354  void deleteSelectedCharacters();
355 
356 
358  // Rearrange the text inside the text box (by using word wrap).
360  void rearrangeText(bool keepSelection);
361 
362 
364  // This function will split the text into five pieces so that the text can be easily drawn.
366  void updateSelectionTexts();
367 
368 
376  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
377 
378 
380  protected:
381 
383  // Returns the size without the borders
385  sf::Vector2f getInnerSize() const;
386 
387 
389  // This function is called every frame with the time passed since the last frame.
391  void update(sf::Time elapsedTime) override;
392 
393 
395  // Recalculates the positions of the contents of the text box.
397  void recalculatePositions();
398 
399 
401  // Recalculates which lines are currently visible.
403  void recalculateVisibleLines();
404 
405 
415  Signal& getSignal(std::string signalName) override;
416 
417 
424  void rendererChanged(const std::string& property) override;
425 
426 
428  // Makes a copy of the widget
430  Widget::Ptr clone() const override
431  {
432  return std::make_shared<TextBox>(*this);
433  }
434 
435 
437  public:
438 
439  SignalString onTextChange = {"TextChanged"};
440 
441 
443  protected:
444 
445  sf::String m_text;
446  unsigned int m_textSize = 18;
447  unsigned int m_lineHeight = 24;
448 
449  std::vector<sf::String> m_lines;
450 
451  // The maximum characters (0 by default, which means no limit)
452  std::size_t m_maxChars = 0;
453 
454  // What is known about the visible lines?
455  std::size_t m_topLine = 1;
456  std::size_t m_visibleLines = 1;
457 
458  // Information about the selection
459  sf::Vector2<std::size_t> m_selStart;
460  sf::Vector2<std::size_t> m_selEnd;
461 
462  // Information about the caret
463  sf::Vector2f m_caretPosition;
464  bool m_caretVisible = true;
465 
466  Text m_textBeforeSelection;
467  Text m_textSelection1;
468  Text m_textSelection2;
469  Text m_textAfterSelection1;
470  Text m_textAfterSelection2;
471 
472  std::vector<sf::FloatRect> m_selectionRects;
473 
474  // The scrollbar
475  ScrollbarChildWidget m_verticalScroll;
476 
477  // Is there a possibility that the user is going to double click?
478  bool m_possibleDoubleClick = false;
479 
480  bool m_readOnly = false;
481 
482  Sprite m_spriteBackground;
483 
484  // Cached renderer properties
485  Borders m_bordersCached;
486  Padding m_paddingCached;
487  Color m_borderColorCached;
488  Color m_backgroundColorCached;
489  Color m_caretColorCached;
490  Color m_selectedTextBackgroundColorCached;
491  float m_caretWidthCached = 0;
492 
494  };
495 
497 }
498 
500 
501 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:243
The parent class for every widget.
Definition: Widget.hpp:62
Text box widget.
Definition: TextBox.hpp:45
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:372
Definition: Text.hpp:41
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:66
std::shared_ptr< TextBox > Ptr
Shared widget pointer.
Definition: TextBox.hpp:49
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:430
Definition: Outline.hpp:38
std::shared_ptr< const TextBox > ConstPtr
Shared constant widget pointer.
Definition: TextBox.hpp:50
Definition: TextBoxRenderer.hpp:36