TGUI  0.8.4
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 
462  void setExpandDirection(ExpandDirection direction);
463 
464 
469  ExpandDirection getExpandDirection() const;
470 
471 
476  bool contains(const sf::String& item) const;
477 
478 
483  bool containsId(const sf::String& id) const;
484 
485 
491  void setParent(Container* parent) override;
492 
493 
500  bool mouseOnWidget(Vector2f pos) const override;
501 
505  void leftMousePressed(Vector2f pos) override;
506 
510  bool mouseWheelScrolled(float delta, Vector2f pos) override;
511 
512 
520  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
521 
522 
524  protected:
525 
535  Signal& getSignal(std::string signalName) override;
536 
537 
544  void rendererChanged(const std::string& property) override;
545 
546 
550  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
551 
552 
556  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
557 
558 
560  // Returns the size without the borders
562  Vector2f getInnerSize() const;
563 
564 
566  // Update the height of the internal list box
568  void updateListBoxHeight();
569 
570 
572  // Shows the list of items.
574  void showListBox();
575 
576 
578  // Hides the list of items.
580  void hideListBox();
581 
582 
584  // Initialize the internal list box
586  void initListBox();
587 
588 
590  // Makes a copy of the widget
592  Widget::Ptr clone() const override
593  {
594  return std::make_shared<ComboBox>(*this);
595  }
596 
597 
599  public:
600 
601  SignalItem onItemSelect = {"ItemSelected"};
602 
603 
605  protected:
606 
607  // The number of items to display. If there is a scrollbar then you can scroll to see the other.
608  // If there is no scrollbar then this will be the maximum amount of items.
609  std::size_t m_nrOfItemsToDisplay = 0;
610 
611  // Internally a list box is used to store all items
612  ListBox::Ptr m_listBox = ListBox::create();
613 
614  Text m_text;
615 
616 #ifdef TGUI_NEXT
617  ExpandDirection m_expandDirection = ExpandDirection::Automatic;
618 #else
619  ExpandDirection m_expandDirection = ExpandDirection::Down;
620 #endif
621 
622  Sprite m_spriteBackground;
623  Sprite m_spriteArrow;
624  Sprite m_spriteArrowHover;
625 
626  // Cached renderer properties
627  Borders m_bordersCached;
628  Padding m_paddingCached;
629  Color m_borderColorCached;
630  Color m_backgroundColorCached;
631  Color m_arrowColorCached;
632  Color m_arrowColorHoverCached;
633  Color m_arrowBackgroundColorCached;
634  Color m_arrowBackgroundColorHoverCached;
635 
637  };
638 
640 }
641 
643 
644 #endif // TGUI_COMBO_BOX_HPP
645 
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:44
Wrapper for colors.
Definition: Color.hpp:47
Class to store the position or size of a widget.
Definition: Layout.hpp:247
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:438
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:592
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:54