TGUI  v0.6.10
TextBox.hpp
1 //
3 // TGUI - Texus's Graphical User Interface
4 // Copyright (C) 2012-2015 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 
39 
40  class TGUI_API TextBox : public Widget, public WidgetBorders
41  {
42  public:
43 
45 
46 
51  TextBox();
52 
53 
60  TextBox(const TextBox& copy);
61 
62 
67  virtual ~TextBox();
68 
69 
78  TextBox& operator= (const TextBox& right);
79 
80 
82  // Makes a copy of the widget by calling the copy constructor.
83  // This function calls new and if you use this function then you are responsible for calling delete.
85  virtual TextBox* clone();
86 
87 
97  bool load(const std::string& configFileFilename, const std::string& sectionName = "TextBox");
98 
99 
107  const std::string& getLoadedConfigFile() const;
108 
109 
119  virtual void setSize(float width, float height);
120 
121 
130  virtual sf::Vector2f getSize() const;
131 
132 
141  virtual sf::Vector2f getFullSize() const;
142 
143 
150  void setText(const sf::String& text);
151 
152 
159  void addText(const sf::String& text);
160 
161 
168  sf::String getText() const;
169 
170 
180  void setTextFont(const sf::Font& font);
181 
182 
189  const sf::Font* getTextFont() const;
190 
191 
199  void setTextSize(unsigned int size);
200 
201 
208  unsigned int getTextSize() const;
209 
210 
220  void setMaximumCharacters(unsigned int maxChars = 0);
221 
222 
232  unsigned int getMaximumCharacters() const;
233 
234 
244  virtual void setBorders(unsigned int leftBorder = 0,
245  unsigned int topBorder = 0,
246  unsigned int rightBorder = 0,
247  unsigned int bottomBorder = 0);
248 
249 
261  void changeColors(const sf::Color& backgroundColor = sf::Color( 50, 50, 50),
262  const sf::Color& textColor = sf::Color( 0, 0, 0),
263  const sf::Color& selectedTextColor = sf::Color(255, 255, 255),
264  const sf::Color& selectedTextBackgroundColor = sf::Color( 10, 110, 255),
265  const sf::Color& borderColor = sf::Color( 0, 0, 0),
266  const sf::Color& selectionPointColor = sf::Color(110, 110, 255));
267 
268 
275  void setBackgroundColor(const sf::Color& backgroundColor);
276 
277 
284  void setTextColor(const sf::Color& textColor);
285 
286 
293  void setSelectedTextColor(const sf::Color& selectedTextColor);
294 
295 
302  void setSelectedTextBackgroundColor(const sf::Color& selectedTextBackgroundColor);
303 
304 
311  void setBorderColor(const sf::Color& borderColor);
312 
313 
320  void setSelectionPointColor(const sf::Color& selectionPointColor);
321 
322 
329  const sf::Color& getBackgroundColor() const;
330 
331 
338  const sf::Color& getTextColor() const;
339 
340 
347  const sf::Color& getSelectedTextColor() const;
348 
349 
356  const sf::Color& getSelectedTextBackgroundColor() const;
357 
358 
365  const sf::Color& getBorderColor() const;
366 
367 
374  const sf::Color& getSelectionPointColor() const;
375 
376 
388  void setSelectionPointPosition(unsigned int charactersBeforeSelectionPoint);
389 
390 
402  bool setScrollbar(const std::string& scrollbarConfigFileFilename);
403 
404 
411  void removeScrollbar();
412 
413 
420  void setSelectionPointWidth(unsigned int width = 2);
421 
422 
429  unsigned int getSelectionPointWidth() const;
430 
431 
441  void setReadOnly(bool readOnly = true);
442 
443 
454  virtual void setTransparency(unsigned char transparency);
455 
456 
460  virtual bool mouseOnWidget(float x, float y);
461 
465  virtual void leftMousePressed(float x, float y);
466 
470  virtual void leftMouseReleased(float x, float y);
471 
475  virtual void mouseMoved(float x, float y);
476 
480  virtual void keyPressed(const sf::Event::KeyEvent& event);
481 
485  virtual void textEntered(sf::Uint32 Key);
486 
490  virtual void mouseWheelMoved(int delta, int x, int y);
491 
495  virtual void mouseNotOnWidget();
496 
500  virtual void mouseNoLongerDown();
501 
505  virtual void widgetUnfocused();
506 
507 
510  // This function is a (slow) way to set properties on the widget, no matter what type it is.
511  // When the requested property doesn't exist in the widget then the functions will return false.
513  virtual bool setProperty(std::string property, const std::string& value);
514 
517  // This function is a (slow) way to get properties of the widget, no matter what type it is.
518  // When the requested property doesn't exist in the widget then the functions will return false.
520  virtual bool getProperty(std::string property, std::string& value) const;
521 
522 
525  // Returns a list of all properties that can be used in setProperty and getProperty.
526  // The second value in the pair is the type of the property (e.g. int, uint, string, ...).
528  virtual std::list< std::pair<std::string, std::string> > getPropertyList() const;
529 
530 
532  protected:
533 
534 
536  // This function will search where the selection point should be. It will not change the selection point.
537  // It will return after which character the selection point should be.
539  unsigned int findSelectionPointPosition(float posX, float posY);
540 
541 
543  // This function is called when you are selecting text.
544  // It will find out which part of the text is selected.
546  void selectText(float posX, float posY);
547 
548 
550  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
551  // some characters selected.
553  void deleteSelectedCharacters();
554 
555 
557  // To keep the drawing as fast as possible, all the calculation are done in front by this function.
558  // It is called when the text changes, when scrolling, ...
560  void updateDisplayedText();
561 
562 
564  // This function is called by updateDisplayedText and will split the text into five pieces so that the text can
565  // be easily drawn.
567  void updateSelectionTexts(float maxLineWidth);
568 
569 
571  // This function is called when the widget is added to a container.
573  virtual void initialize(Container *const container);
574 
575 
577  // When AnimationManager changes the elapsed time then this function is called.
579  virtual void update();
580 
581 
583  // Draws the widget on the render target.
585  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
586 
587 
589  public:
590 
595  {
596  TextChanged = WidgetCallbacksCount * 1,
597  AllTextBoxCallbacks = WidgetCallbacksCount * 2 - 1,
598  TextBoxCallbacksCount = WidgetCallbacksCount * 2
599  };
600 
601 
603  protected:
604 
605  std::string m_LoadedConfigFile;
606 
607  // The size of the text box
608  sf::Vector2u m_Size;
609 
610  // Some information about the text
611  sf::String m_Text;
612  sf::String m_DisplayedText;
613  unsigned int m_TextSize;
614  unsigned int m_LineHeight;
615  unsigned int m_Lines;
616 
617  // The maximum characters (0 by default, which means no limit)
618  unsigned int m_MaxChars;
619 
620  // What is known about the visible lines?
621  unsigned int m_TopLine;
622  unsigned int m_VisibleLines;
623 
624  // Information about the selection
625  unsigned int m_SelChars;
626  unsigned int m_SelStart;
627  unsigned int m_SelEnd;
628 
629  // Information about the selection pointer
630  sf::Vector2u m_SelectionPointPosition;
631  bool m_SelectionPointVisible;
632 
633  // The color of the flickering selection pointer
634  sf::Color m_SelectionPointColor;
635 
636  // The width in pixels of the flickering selection pointer
637  unsigned int m_SelectionPointWidth;
638 
639  // Should the text be resplit into the five texts?
640  bool m_SelectionTextsNeedUpdate;
641 
642  // The colors that are used by the text box
643  sf::Color m_BackgroundColor;
644  sf::Color m_SelectedTextBgrColor;
645  sf::Color m_BorderColor;
646 
647  // The sfml Text widgets
648  sf::Text m_TextBeforeSelection;
649  sf::Text m_TextSelection1;
650  sf::Text m_TextSelection2;
651  sf::Text m_TextAfterSelection1;
652  sf::Text m_TextAfterSelection2;
653 
654  std::vector<float> m_MultilineSelectionRectWidth;
655 
656  // The scrollbar
657  Scrollbar* m_Scroll;
658 
659  // Is there a possibility that the user is going to double click?
660  bool m_PossibleDoubleClick;
661 
662  bool m_readOnly;
663 
665  };
666 
668 }
669 
671 
672 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AnimatedPicture.hpp:33
The parent class for every widget.
Definition: Widget.hpp:45
Definition: TextBox.hpp:40
Parent class for every widget that has borders.
Definition: Widget.hpp:480
Parent class for widgets that store multiple widgets.
Definition: Container.hpp:43
TextBoxCallbacks
Defines specific triggers to TextBox.
Definition: TextBox.hpp:594
Definition: Scrollbar.hpp:38
Definition: SharedWidgetPtr.hpp:44