TGUI  0.8.5
ComboBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 
90  static ComboBox::Ptr create();
91 
92 
101  static ComboBox::Ptr copy(ComboBox::ConstPtr comboBox);
102 
103 
108  ComboBoxRenderer* getSharedRenderer();
109  const ComboBoxRenderer* getSharedRenderer() const;
110 
116  ComboBoxRenderer* getRenderer();
117  const ComboBoxRenderer* getRenderer() const;
118 
119 
128  void setSize(const Layout2d& size) override;
129  using Widget::setSize;
130 
131 
143  void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
144 
145 
157  std::size_t getItemsToDisplay() const;
158 
159 
176  bool addItem(const sf::String& itemName, const sf::String& id = "");
177 
178 
197  bool setSelectedItem(const sf::String& itemName);
198 
199 
218  bool setSelectedItemById(const sf::String& id);
219 
220 
234  bool setSelectedItemByIndex(std::size_t index);
235 
236 
243  void deselectItem();
244 
245 
258  bool removeItem(const sf::String& itemName);
259 
260 
273  bool removeItemById(const sf::String& id);
274 
275 
289  bool removeItemByIndex(std::size_t index);
290 
291 
296  void removeAllItems();
297 
298 
309  sf::String getItemById(const sf::String& id) const;
310 
311 
319  sf::String getSelectedItem() const;
320 
321 
329  sf::String getSelectedItemId() const;
330 
331 
338  int getSelectedItemIndex() const;
339 
340 
354  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
355 
356 
370  bool changeItemById(const sf::String& id, const sf::String& newValue);
371 
372 
384  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
385 
386 
393  std::size_t getItemCount() const;
394 
395 
402  std::vector<sf::String> getItems() const;
403 
404 
413  const std::vector<sf::String>& getItemIds() const;
414 
415 
425  void setMaximumItems(std::size_t maximumItems = 0);
426 
427 
435  std::size_t getMaximumItems() const;
436 
437 
446  void setTextSize(unsigned int textSize);
447 
448 
455  unsigned int getTextSize() const;
456 
457 
463  void setDefaultText(const sf::String& defaultText);
464 
465 
471  const sf::String& getDefaultText() const;
472 
473 
478  void setExpandDirection(ExpandDirection direction);
479 
480 
485  ExpandDirection getExpandDirection() const;
486 
487 
492  bool contains(const sf::String& item) const;
493 
494 
499  bool containsId(const sf::String& id) const;
500 
501 
507  void setChangeItemOnScroll(bool changeOnScroll);
508 
509 
515  bool getChangeItemOnScroll() const;
516 
517 
523  void setParent(Container* parent) override;
524 
525 
532  bool mouseOnWidget(Vector2f pos) const override;
533 
537  void leftMousePressed(Vector2f pos) override;
538 
542  bool mouseWheelScrolled(float delta, Vector2f pos) override;
543 
544 
552  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
553 
554 
556  protected:
557 
567  Signal& getSignal(std::string signalName) override;
568 
569 
576  void rendererChanged(const std::string& property) override;
577 
578 
582  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
583 
584 
588  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
589 
590 
592  // Returns the size without the borders
594  Vector2f getInnerSize() const;
595 
596 
598  // Update the height of the internal list box
600  void updateListBoxHeight();
601 
602 
604  // Shows the list of items.
606  void showListBox();
607 
608 
610  // Hides the list of items.
612  void hideListBox();
613 
614 
616  // Initialize the internal list box
618  void initListBox();
619 
620 
622  // Makes a copy of the widget
624  Widget::Ptr clone() const override
625  {
626  return std::make_shared<ComboBox>(*this);
627  }
628 
629 
631  public:
632 
633  SignalItem onItemSelect = {"ItemSelected"};
634 
635 
637  protected:
638 
639  // The number of items to display. If there is a scrollbar then you can scroll to see the other.
640  // If there is no scrollbar then this will be the maximum amount of items.
641  std::size_t m_nrOfItemsToDisplay = 0;
642 
643  // Internally a list box is used to store all items
644  ListBox::Ptr m_listBox = ListBox::create();
645 
646  Text m_text;
647  Text m_defaultText;
648 
649  int m_previousSelectedItemIndex = -1;
650 #ifdef TGUI_NEXT
651  bool m_changeItemOnScroll = false;
652 #else
653  bool m_changeItemOnScroll = true;
654 #endif
655 
656 #ifdef TGUI_NEXT
657  ExpandDirection m_expandDirection = ExpandDirection::Automatic;
658 #else
659  ExpandDirection m_expandDirection = ExpandDirection::Down;
660 #endif
661 
662  Sprite m_spriteBackground;
663  Sprite m_spriteArrow;
664  Sprite m_spriteArrowHover;
665 
666  // Cached renderer properties
667  Borders m_bordersCached;
668  Padding m_paddingCached;
669  Color m_borderColorCached;
670  Color m_backgroundColorCached;
671  Color m_arrowColorCached;
672  Color m_arrowColorHoverCached;
673  Color m_arrowBackgroundColorCached;
674  Color m_arrowBackgroundColorHoverCached;
675 
677  };
678 
680 }
681 
683 
684 #endif // TGUI_COMBO_BOX_HPP
685 
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:45
Wrapper for colors.
Definition: Color.hpp:48
Class to store the position or size of a widget.
Definition: Layout.hpp:258
The parent class for every widget.
Definition: Widget.hpp:72
Combo box widget.
Definition: ComboBox.hpp:40
Definition: Vector2f.hpp:37
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:46
Definition: Text.hpp:42
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
static ListBox::Ptr create()
Creates a new list box widget.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:467
std::shared_ptr< const ComboBox > ConstPtr
Shared constant widget pointer.
Definition: ComboBox.hpp:55
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
ExpandDirection
The side where the list will be displayed.
Definition: ComboBox.hpp:45
Definition: Outline.hpp:38
Definition: ComboBoxRenderer.hpp:36
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ComboBox.hpp:624
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:54