TGUI  0.8-dev
ComboBox.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_COMBO_BOX_HPP
27 #define TGUI_COMBO_BOX_HPP
28 
29 
30 #include <TGUI/Widgets/ListBox.hpp>
31 #include <TGUI/Renderers/ComboBoxRenderer.hpp>
32 
34 
35 namespace tgui
36 {
49  class TGUI_API ComboBox : public Widget
50  {
51  public:
52 
53  typedef std::shared_ptr<ComboBox> Ptr;
54  typedef std::shared_ptr<const ComboBox> ConstPtr;
55 
56 
58  // Default constructor
60  ComboBox();
61 
63  // Copy constructor
65  ComboBox(const ComboBox& other);
66 
68  // Move constructor
70  ComboBox(ComboBox&& other);
71 
73  // Copy assignment operator
75  ComboBox& operator= (const ComboBox& other);
76 
78  // Move assignment operator
80  ComboBox& operator= (ComboBox&& other);
81 
82 
89  static ComboBox::Ptr create();
90 
91 
100  static ComboBox::Ptr copy(ComboBox::ConstPtr comboBox);
101 
102 
110  {
111  return aurora::downcast<ComboBoxRenderer*>(m_renderer.get());
112  }
113 
114 
123  void setSize(const Layout2d& size) override;
125 
126 
138  void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
139 
140 
152  std::size_t getItemsToDisplay() const;
153 
154 
171  bool addItem(const sf::String& itemName, const sf::String& id = "");
172 
173 
192  bool setSelectedItem(const sf::String& itemName);
193 
194 
213  bool setSelectedItemById(const sf::String& id);
214 
215 
229  bool setSelectedItemByIndex(std::size_t index);
230 
231 
238  void deselectItem();
239 
240 
253  bool removeItem(const sf::String& itemName);
254 
255 
268  bool removeItemById(const sf::String& id);
269 
270 
284  bool removeItemByIndex(std::size_t index);
285 
286 
291  void removeAllItems();
292 
293 
304  sf::String getItemById(const sf::String& id) const;
305 
306 
314  sf::String getSelectedItem() const;
315 
316 
324  sf::String getSelectedItemId() const;
325 
326 
333  int getSelectedItemIndex() const;
334 
335 
349  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
350 
351 
365  bool changeItemById(const sf::String& id, const sf::String& newValue);
366 
367 
379  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
380 
381 
388  std::size_t getItemCount() const;
389 
390 
397  std::vector<sf::String> getItems();
398 
399 
408  const std::vector<sf::String>& getItemIds();
409 
410 
420  void setMaximumItems(std::size_t maximumItems = 0);
421 
422 
430  std::size_t getMaximumItems() const;
431 
432 
441  void setTextSize(unsigned int textSize);
442 
443 
450  unsigned int getTextSize() const;
451 
452 
458  virtual void setParent(Container* parent) override;
459 
460 
464  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
465 
469  virtual void leftMousePressed(sf::Vector2f pos) override;
470 
474  virtual void mouseWheelScrolled(float delta, int x, int y) override;
475 
476 
484  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
485 
486 
488  protected:
489 
496  virtual void rendererChanged(const std::string& property) override;
497 
498 
500  // Returns the size without the borders
502  sf::Vector2f getInnerSize() const;
503 
504 
506  // Update the height of the internal list box
508  void updateListBoxHeight();
509 
510 
512  // Shows the list of items.
514  void showListBox();
515 
516 
518  // Hides the list of items.
520  void hideListBox();
521 
522 
524  // Initialize the internal list box
526  void initListBox();
527 
528 
530  // Called by the internal ListBox when a different item is selected.
532  void newItemSelectedCallbackFunction();
533 
534 
536  // Called by the internal ListBox when it gets unfocused.
538  void listBoxUnfocusedCallbackFunction();
539 
540 
542  // Makes a copy of the widget
544  virtual Widget::Ptr clone() const override
545  {
546  return std::make_shared<ComboBox>(*this);
547  }
548 
549 
551  protected:
552 
553  // The number of items to display. If there is a scrollbar then you can scroll to see the other.
554  // If there is no scrollbar then this will be the maximum amount of items.
555  std::size_t m_nrOfItemsToDisplay = 0;
556 
557  // Internally a list box is used to store all items
558  ListBox::Ptr m_listBox = ListBox::create();
559 
560  Text m_text;
561 
562  Sprite m_spriteBackground;
563  Sprite m_spriteArrowUp;
564  Sprite m_spriteArrowDown;
565  Sprite m_spriteArrowUpHover;
566  Sprite m_spriteArrowDownHover;
567 
568  // Cached renderer properties
569  Borders m_bordersCached;
570  Padding m_paddingCached;
571  Color m_borderColorCached;
572  Color m_backgroundColorCached;
573  Color m_arrowColorCached;
574  Color m_arrowColorHoverCached;
575  Color m_arrowBackgroundColorCached;
576  Color m_arrowBackgroundColorHoverCached;
577 
579  };
580 
582 }
583 
585 
586 #endif // TGUI_COMBO_BOX_HPP
587 
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
ComboBoxRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: ComboBox.hpp:109
Class to store the position or size of a widget.
Definition: Layout.hpp:263
The parent class for every widget.
Definition: Widget.hpp:72
Combo box widget.
Definition: ComboBox.hpp:49
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:69
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: Text.hpp:41
static ListBox::Ptr create()
Creates a new list box widget.
std::shared_ptr< const ComboBox > ConstPtr
Shared constant widget pointer.
Definition: ComboBox.hpp:54
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
Definition: Outline.hpp:37
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: ComboBox.hpp:544
Definition: ComboBoxRenderer.hpp:36
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:53