TGUI  0.7.8
TextBox.hpp
1
2//
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
34namespace 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
91
92
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
270
271
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
662 // Draws the widget on the render target.
664 void draw(sf::RenderTarget& target, sf::RenderStates states) const;
665
666
668 private:
669
671 // Returns the padding, which is possibly scaled with the background image.
673 Padding getScaledPadding() const;
674
675
677 // Makes a copy of the renderer
679 virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
680
681
683 protected:
684
685 TextBox* m_textBox;
686
687 float m_caretWidth = 1;
688
689 Texture m_backgroundTexture;
690
691 sf::Color m_textColor;
692 sf::Color m_selectedTextColor;
693 sf::Color m_caretColor;
694 sf::Color m_backgroundColor;
695 sf::Color m_selectedTextBgrColor;
696 sf::Color m_borderColor;
697
698 friend class TextBox;
699
701 };
702
704}
705
707
708#endif // TGUI_TEXT_BOX_HPP
Definition: Borders.hpp:38
Implicit converter for colors.
Definition: Color.hpp:40
Definition: Font.hpp:38
Class to store the position or size of a widget.
Definition: Layout.hpp:255
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:43
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:53
Definition: TextBox.hpp:513
void setBackgroundColor(const Color &backgroundColor)
Set the background color that will be used inside the text box.
void setBorderColor(const Color &borderColor)
Set the border color that will be used inside the text box.
virtual void setPadding(const Padding &padding) override
Changes the padding of the text box.
void setCaretColor(const Color &caretColor)
Set the color that will be used inside the text box for the blinking caret.
virtual std::map< std::string, ObjectConverter > getPropertyValuePairs() const override
Get a map with all properties and their values.
void setTextColor(const Color &textColor)
Set the text color that will be used inside the text box.
virtual void setProperty(std::string property, const std::string &value) override
Change a property of the renderer.
void setSelectedTextColor(const Color &selectedTextColor)
Set the text color of the selected text that will be used inside the text box.
void setBackgroundTexture(const Texture &texture)
Changes the background image.
virtual void setProperty(std::string property, ObjectConverter &&value) override
Change a property of the renderer.
virtual ObjectConverter getProperty(std::string property) const override
Retrieve the value of a certain property.
void setSelectedTextBackgroundColor(const Color &selectedTextBackgroundColor)
Set the background color of the selected text that will be used inside the text box.
TextBoxRenderer(TextBox *textBox)
Constructor.
Definition: TextBox.hpp:522
void setCaretWidth(float width)
This will change the width of the blinking caret.
Text box widget.
Definition: TextBox.hpp:51
void setScrollbar(Scrollbar::Ptr scrollbar)
Changes the scrollbar of the text box.
std::shared_ptr< TextBox > Ptr
Shared widget pointer.
Definition: TextBox.hpp:54
void setMaximumCharacters(std::size_t maxChars=0)
Changes the maximum character limit.
virtual void setFont(const Font &font) override
Changes the font of the text in the widget.
virtual sf::Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
bool isReadOnly() const
Check if the text box read-only or writable.
virtual void setPosition(const Layout2d &position) override
Set the position of the widget.
static TextBox::Ptr create()
Creates a new text box widget.
unsigned int getTextSize() const
Returns the character size of the text.
Definition: TextBox.hpp:221
void setText(const sf::String &text)
Changes the text of the text box.
virtual void setSize(const Layout2d &size) override
Changes the size of the text box.
virtual void setOpacity(float opacity) override
Changes the opacity of the widget.
TextBox(const TextBox &copy)
Copy constructor.
virtual sf::Vector2f getFullSize() const override
Returns the full size of the text box.
void setTextSize(unsigned int size)
Changes the character size of the text.
virtual void reload(const std::string &primary="", const std::string &secondary="", bool force=false) override
Reload the widget.
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: TextBox.hpp:452
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
sf::String getText() const
Returns the text of the text box.
Definition: TextBox.hpp:190
void setReadOnly(bool readOnly=true)
Make the text box read-only or make it writable again.
static TextBox::Ptr copy(TextBox::ConstPtr textBox)
Makes a copy of another text box.
Scrollbar::Ptr getScrollbar()
Access the scrollbar of the text box.
std::shared_ptr< const TextBox > ConstPtr
Shared constant widget pointer.
Definition: TextBox.hpp:55
void addText(const sf::String &text)
Appends some text to the text that was already in the text box.
std::size_t getMaximumCharacters() const
Returns the maximum character limit.
Definition: TextBox.hpp:248
sf::String getSelectedText() const
Returns the text that you currently have selected.
Definition: Texture.hpp:45
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:137
Parent class for every widget that has padding.
Definition: Borders.hpp:211
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Base class for all renderer classes.
Definition: Widget.hpp:683
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34