TGUI  0.8.8
ComboBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 {
40  class TGUI_API ComboBox : public Widget
41  {
42  public:
43 
45  enum class ExpandDirection
46  {
47  Down,
48  Up,
49  Automatic
50  };
51 
52  public:
53 
54  typedef std::shared_ptr<ComboBox> Ptr;
55  typedef std::shared_ptr<const ComboBox> ConstPtr;
56 
57 
59  // Default constructor
61  ComboBox();
62 
64  // Copy constructor
66  ComboBox(const ComboBox& other);
67 
69  // Move constructor
71  ComboBox(ComboBox&& other);
72 
74  // Copy assignment operator
76  ComboBox& operator= (const ComboBox& other);
77 
79  // Move assignment operator
81  ComboBox& operator= (ComboBox&& other);
82 
83 
91 
92 
102 
103 
109  const ComboBoxRenderer* getSharedRenderer() const;
110 
117  const ComboBoxRenderer* getRenderer() const;
118 
119 
128  void setSize(const Layout2d& size) override;
129  using Widget::setSize;
130 
131 
139  void setEnabled(bool enabled) override;
140 
141 
153  void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
154 
155 
167  std::size_t getItemsToDisplay() const;
168 
169 
186  bool addItem(const sf::String& itemName, const sf::String& id = "");
187 
188 
207  bool setSelectedItem(const sf::String& itemName);
208 
209 
228  bool setSelectedItemById(const sf::String& id);
229 
230 
244  bool setSelectedItemByIndex(std::size_t index);
245 
246 
253  void deselectItem();
254 
255 
268  bool removeItem(const sf::String& itemName);
269 
270 
283  bool removeItemById(const sf::String& id);
284 
285 
299  bool removeItemByIndex(std::size_t index);
300 
301 
307 
308 
319  sf::String getItemById(const sf::String& id) const;
320 
321 
329  sf::String getSelectedItem() const;
330 
331 
339  sf::String getSelectedItemId() const;
340 
341 
348  int getSelectedItemIndex() const;
349 
350 
364  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
365 
366 
380  bool changeItemById(const sf::String& id, const sf::String& newValue);
381 
382 
394  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
395 
396 
403  std::size_t getItemCount() const;
404 
405 
412  std::vector<sf::String> getItems() const;
413 
414 
423  const std::vector<sf::String>& getItemIds() const;
424 
425 
435  void setMaximumItems(std::size_t maximumItems = 0);
436 
437 
445  std::size_t getMaximumItems() const;
446 
447 
456  void setTextSize(unsigned int textSize) override;
457 
458 
465  unsigned int getTextSize() const override;
466 
467 
473  void setDefaultText(const sf::String& defaultText);
474 
475 
481  const sf::String& getDefaultText() const;
482 
483 
489 
490 
496 
497 
502  bool contains(const sf::String& item) const;
503 
504 
509  bool containsId(const sf::String& id) const;
510 
511 
517  void setChangeItemOnScroll(bool changeOnScroll);
518 
519 
525  bool getChangeItemOnScroll() const;
526 
527 
533  void setParent(Container* parent) override;
534 
535 
542  bool mouseOnWidget(Vector2f pos) const override;
543 
547  void leftMousePressed(Vector2f pos) override;
548 
552  bool mouseWheelScrolled(float delta, Vector2f pos) override;
553 
554 
562  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
563 
564 
566  protected:
567 
577  Signal& getSignal(std::string signalName) override;
578 
579 
586  void rendererChanged(const std::string& property) override;
587 
588 
592  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
593 
594 
598  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
599 
600 
602  // Returns the size without the borders
604  Vector2f getInnerSize() const;
605 
606 
608  // Update the height of the internal list box
610  void updateListBoxHeight();
611 
612 
614  // Shows the list of items.
616  void showListBox();
617 
618 
620  // Hides the list of items.
622  void hideListBox();
623 
624 
626  // Initialize the internal list box
628  void initListBox();
629 
630 
632  // Makes a copy of the widget
634  Widget::Ptr clone() const override
635  {
636  return std::make_shared<ComboBox>(*this);
637  }
638 
639 
641  public:
642 
643  SignalItem onItemSelect = {"ItemSelected"};
644 
645 
647  protected:
648 
649  // The number of items to display. If there is a scrollbar then you can scroll to see the other.
650  // If there is no scrollbar then this will be the maximum amount of items.
651  std::size_t m_nrOfItemsToDisplay = 0;
652 
653  // Internally a list box is used to store all items
654  ListBox::Ptr m_listBox = ListBox::create();
655 
656  Text m_text;
657  Text m_defaultText;
658 
659  int m_previousSelectedItemIndex = -1;
660 #ifdef TGUI_NEXT
661  bool m_changeItemOnScroll = false;
662 #else
663  bool m_changeItemOnScroll = true;
664 #endif
665 
666 #ifdef TGUI_NEXT
667  ExpandDirection m_expandDirection = ExpandDirection::Automatic;
668 #else
669  ExpandDirection m_expandDirection = ExpandDirection::Down;
670 #endif
671 
672  Sprite m_spriteBackground;
673  Sprite m_spriteBackgroundDisabled;
674  Sprite m_spriteArrow;
675  Sprite m_spriteArrowHover;
676  Sprite m_spriteArrowDisabled;
677 
678  // Cached renderer properties
679  Borders m_bordersCached;
680  Padding m_paddingCached;
681  Color m_borderColorCached;
682  Color m_backgroundColorCached;
683  Color m_backgroundColorDisabledCached;
684  Color m_arrowColorCached;
685  Color m_arrowColorHoverCached;
686  Color m_arrowColorDisabledCached;
687  Color m_arrowBackgroundColorCached;
688  Color m_arrowBackgroundColorHoverCached;
689  Color m_arrowBackgroundColorDisabledCached;
690  Color m_textColorCached;
691  Color m_textColorDisabledCached;
692 
694  };
695 
697 }
698 
700 
701 #endif // TGUI_COMBO_BOX_HPP
702 
const sf::String & getDefaultText() const
Returns the default text of the combo box. This is the text drawn when no item is selected.
void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay)
Changes the number of items that are displayed in the list.
void setDefaultText(const sf::String &defaultText)
Changes the default text of the combo box. This is the text drawn when no item is selected.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
std::vector< sf::String > getItems() const
Returns a copy of the items in the combo box.
void setChangeItemOnScroll(bool changeOnScroll)
Changes whether the mouse wheel can be used to change the selected item while the list is closed.
unsigned int getTextSize() const override
Returns the text size of the items.
Container widget.
Definition: Container.hpp:48
ComboBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static ComboBox::Ptr copy(ComboBox::ConstPtr comboBox)
Makes a copy of another combo box.
static ComboBox::Ptr create()
Creates a new combo box widget.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
bool removeItemByIndex(std::size_t index)
Removes the item from the list.
Definition: Text.hpp:43
bool removeItem(const sf::String &itemName)
Removes the item from the list with the given name.
bool changeItemById(const sf::String &id, const sf::String &newValue)
Changes the name of an item with the given id to newValue.
sf::String getSelectedItem() const
Returns the currently selected item.
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:54
void setEnabled(bool enabled) override
Enables or disables the widget.
std::size_t getItemCount() const
Returns the amount of items in the combo box.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
std::size_t getItemsToDisplay() const
Returns the number of items that are displayed in the list.
Wrapper for colors.
Definition: Color.hpp:49
ExpandDirection getExpandDirection() const
Returns the side where the list is displayed.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:567
ExpandDirection
The side where the list will be displayed.
Definition: ComboBox.hpp:46
void setSize(const Layout2d &size) override
Changes the size of the combo box.
int getSelectedItemIndex() const
Gets the index of the selected item.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
std::size_t getMaximumItems() const
Returns the maximum items that the combo box can contain.
const std::vector< sf::String > & getItemIds() const
Returns a copy of the item ids in the combo box.
bool setSelectedItem(const sf::String &itemName)
Selects an item from the list.
bool containsId(const sf::String &id) const
Returns whether the combo box contains an item with the given id.
bool contains(const sf::String &item) const
Returns whether the combo box contains the given item.
void setExpandDirection(ExpandDirection direction)
Changes the side where the list is displayed.
Definition: Sprite.hpp:46
bool changeItem(const sf::String &originalValue, const sf::String &newValue)
Changes an item with name originalValue to newValue.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
ComboBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setTextSize(unsigned int textSize) override
Changes the text size of the items.
void removeAllItems()
Removes all items from the list.
bool removeItemById(const sf::String &id)
Removes the item that were added with the given id.
Definition: Vector2f.hpp:39
bool getChangeItemOnScroll() const
Returns whether the mouse wheel can be used to change the selected item while the list is closed.
Definition: Outline.hpp:39
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ComboBox.hpp:634
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the combo box can contain.
The parent class for every widget.
Definition: Widget.hpp:74
bool setSelectedItemById(const sf::String &id)
Selects an item from the list.
bool changeItemByIndex(std::size_t index, const sf::String &newValue)
Changes the name of an item at the given index to newValue.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:46
std::shared_ptr< const ComboBox > ConstPtr
Shared constant widget pointer.
Definition: ComboBox.hpp:55
sf::String getItemById(const sf::String &id) const
Returns the item name of the item with the given id.
bool addItem(const sf::String &itemName, const sf::String &id="")
Adds an item to the list, so that it can be selected later.
static ListBox::Ptr create()
Creates a new list box widget.
void deselectItem()
Deselects the selected item.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
sf::String getSelectedItemId() const
Gets the id of the selected item.
Combo box widget.
Definition: ComboBox.hpp:41
Definition: ComboBoxRenderer.hpp:37