TGUI  v0.6.10
EditBox.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 #ifndef TGUI_EDIT_BOX_HPP
26 #define TGUI_EDIT_BOX_HPP
27 
28 
29 #include <TGUI/ClickableWidget.hpp>
30 
32 
33 namespace tgui
34 {
36 
37  class TGUI_API EditBox : public ClickableWidget, public WidgetBorders
38  {
39  public:
40 
42 
43 
47  struct Alignment
48  {
51  {
54 
57 
59  Right
60  };
61  };
62 
63 
68  EditBox();
69 
70 
77  EditBox(const EditBox& copy);
78 
79 
84  virtual ~EditBox();
85 
86 
95  EditBox& operator= (const EditBox& right);
96 
97 
99  // Makes a copy of the widget by calling the copy constructor.
100  // This function calls new and if you use this function then you are responsible for calling delete.
102  virtual EditBox* clone();
103 
104 
114  bool load(const std::string& configFileFilename, const std::string& sectionName = "EditBox");
115 
116 
124  const std::string& getLoadedConfigFile() const;
125 
126 
140  virtual void setPosition(float x, float y);
142 
143 
151  void setSize(float width, float height);
152 
153 
167  void setText(const sf::String& text);
168 
169 
176  sf::String getText() const;
177 
178 
186  void setTextSize(unsigned int textSize);
187 
188 
195  unsigned int getTextSize() const;
196 
197 
207  void setTextFont(const sf::Font& font);
208 
209 
216  const sf::Font* getTextFont() const;
217 
218 
231  void setPasswordCharacter(char passwordChar = '\0');
232 
233 
241  char getPasswordCharacter() const;
242 
243 
252  void setMaximumCharacters(unsigned int maxChars = 0);
253 
254 
264  unsigned int getMaximumCharacters() const;
265 
266 
273  void setAlignment(Alignment::Alignments alignment);
274 
275 
282  Alignment::Alignments getAlignment();
283 
284 
298  virtual void setBorders(unsigned int leftBorder = 0,
299  unsigned int topBorder = 0,
300  unsigned int rightBorder = 0,
301  unsigned int bottomBorder = 0);
302 
303 
313  void changeColors(const sf::Color& textColor = sf::Color( 0, 0, 0),
314  const sf::Color& selectedTextColor = sf::Color(255, 255, 255),
315  const sf::Color& selectedTextBackgroundColor = sf::Color( 10, 110, 255),
316  const sf::Color& selectionPointColor = sf::Color(110, 110, 255));
317 
318 
325  void setTextColor(const sf::Color& textColor);
326 
327 
334  void setSelectedTextColor(const sf::Color& selectedTextColor);
335 
336 
343  void setSelectedTextBackgroundColor(const sf::Color& selectedTextBackgroundColor);
344 
345 
352  void setSelectionPointColor(const sf::Color& selectionPointColor);
353 
354 
361  const sf::Color& getTextColor() const;
362 
363 
370  const sf::Color& getSelectedTextColor() const;
371 
372 
379  const sf::Color& getSelectedTextBackgroundColor() const;
380 
381 
388  const sf::Color& getSelectionPointColor() const;
389 
390 
400  void limitTextWidth(bool limitWidth);
401 
402 
411  void setSelectionPointPosition(unsigned int charactersBeforeSelectionPoint);
412 
413 
420  void setSelectionPointWidth(unsigned int width = 2);
421 
422 
429  unsigned int getSelectionPointWidth() const;
430 
431 
440  void setNumbersOnly(bool numbersOnly = true);
441 
442 
453  virtual void setTransparency(unsigned char transparency);
454 
455 
459  virtual void leftMousePressed(float x, float y);
460 
464  virtual void mouseMoved(float x, float y);
465 
469  virtual void keyPressed(const sf::Event::KeyEvent& event);
470 
474  virtual void textEntered(sf::Uint32 Key);
475 
479  virtual void widgetUnfocused();
480 
481 
484  // This function is a (slow) way to set properties on the widget, no matter what type it is.
485  // When the requested property doesn't exist in the widget then the functions will return false.
487  virtual bool setProperty(std::string property, const std::string& value);
488 
491  // This function is a (slow) way to get properties of the widget, no matter what type it is.
492  // When the requested property doesn't exist in the widget then the functions will return false.
494  virtual bool getProperty(std::string property, std::string& value) const;
495 
496 
499  // Returns a list of all properties that can be used in setProperty and getProperty.
500  // The second value in the pair is the type of the property (e.g. int, uint, string, ...).
502  virtual std::list< std::pair<std::string, std::string> > getPropertyList() const;
503 
504 
506  protected:
507 
508 
510  // This function will search where the selection point should be. It will not change the selection point.
511  // It will return after which character the selection point should be.
513  unsigned int findSelectionPointPosition(float PosX);
514 
515 
517  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
518  // some characters selected.
520  void deleteSelectedCharacters();
521 
522 
524  // Recalculates the position of the texts.
526  void recalculateTextPositions();
527 
528 
530  // This function is called when the widget is added to a container.
532  virtual void initialize(Container *const container);
533 
534 
536  // When AnimationManager changes the elapsed time then this function is called.
538  virtual void update();
539 
540 
542  // Draws the widget on the render target.
544  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
545 
546 
548  public:
549 
554  {
555  TextChanged = ClickableWidgetCallbacksCount * 1,
556  ReturnKeyPressed = ClickableWidgetCallbacksCount * 2,
557  AllEditBoxCallbacks = ClickableWidgetCallbacksCount * 4 - 1,
558  EditBoxCallbacksCount = ClickableWidgetCallbacksCount * 4
559  };
560 
561 
563  protected:
564 
565  std::string m_LoadedConfigFile;
566 
567  // Is the selection point visible or not?
568  bool m_SelectionPointVisible;
569 
570  // When this boolean is true then you can no longer add text when the EditBox is full.
571  // Changing it to false will allow you to scroll the text (default).
572  // You can change the boolean with the limitTextWidth(bool) function.
573  bool m_LimitTextWidth;
574 
575  // The text inside the edit box
576  sf::String m_DisplayedText;
577  sf::String m_Text;
578 
579  // This will store the size of the text ( 0 to auto size )
580  unsigned int m_TextSize;
581 
582  // The text alignment
583  Alignment::Alignments m_TextAlignment;
584 
585  // The selection
586  unsigned int m_SelChars;
587  unsigned int m_SelStart;
588  unsigned int m_SelEnd;
589 
590  // The password character
591  char m_PasswordChar;
592 
593  // The maximum allowed characters.
594  // Zero by default, meaning no limit.
595  unsigned int m_MaxChars;
596 
597  // If true then the image is split in three parts
598  bool m_SplitImage;
599 
600  // When the text width is not limited, you can scroll the edit box and only a part will be visible.
601  unsigned int m_TextCropPosition;
602 
603  // The rectangle behind the selected text
604  sf::RectangleShape m_SelectedTextBackground;
605 
606  // The flickering selection point
607  sf::RectangleShape m_SelectionPoint;
608 
609  // We need three SFML texts to draw our text, and one more for calculations.
610  sf::Text m_TextBeforeSelection;
611  sf::Text m_TextSelection;
612  sf::Text m_TextAfterSelection;
613  sf::Text m_TextFull;
614 
615  Texture m_TextureNormal_L;
616  Texture m_TextureNormal_M;
617  Texture m_TextureNormal_R;
618 
619  Texture m_TextureHover_L;
620  Texture m_TextureHover_M;
621  Texture m_TextureHover_R;
622 
623  Texture m_TextureFocused_L;
624  Texture m_TextureFocused_M;
625  Texture m_TextureFocused_R;
626 
627  // Is there a possibility that the user is going to double click?
628  bool m_PossibleDoubleClick;
629 
630  bool m_NumbersOnly;
631  bool m_SeparateHoverImage;
632 
634 
635  };
637 
638 }
640 
641 #endif // TGUI_EDIT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AnimatedPicture.hpp:33
virtual void setPosition(float x, float y)
Set the position of the widget.
The text alignment.
Definition: EditBox.hpp:47
Definition: TextureManager.hpp:52
EditBoxCallbacks
Defines specific triggers to EditBox.
Definition: EditBox.hpp:553
Center the text.
Definition: EditBox.hpp:56
Alignments
The text alignment.
Definition: EditBox.hpp:50
Definition: ClickableWidget.hpp:38
Put the text on the left side (default)
Definition: EditBox.hpp:53
Parent class for every widget that has borders.
Definition: Widget.hpp:480
Parent class for widgets that store multiple widgets.
Definition: Container.hpp:43
Definition: EditBox.hpp:37
Definition: SharedWidgetPtr.hpp:44