TGUI  0.8-alpha
ComboBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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  };
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 
107  ComboBoxRenderer* getSharedRenderer();
108  const ComboBoxRenderer* getSharedRenderer() const;
109 
115  ComboBoxRenderer* getRenderer();
116  const ComboBoxRenderer* getRenderer() const;
117 
118 
127  void setSize(const Layout2d& size) override;
128  using Widget::setSize;
129 
130 
142  void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
143 
144 
156  std::size_t getItemsToDisplay() const;
157 
158 
175  bool addItem(const sf::String& itemName, const sf::String& id = "");
176 
177 
196  bool setSelectedItem(const sf::String& itemName);
197 
198 
217  bool setSelectedItemById(const sf::String& id);
218 
219 
233  bool setSelectedItemByIndex(std::size_t index);
234 
235 
242  void deselectItem();
243 
244 
257  bool removeItem(const sf::String& itemName);
258 
259 
272  bool removeItemById(const sf::String& id);
273 
274 
288  bool removeItemByIndex(std::size_t index);
289 
290 
295  void removeAllItems();
296 
297 
308  sf::String getItemById(const sf::String& id) const;
309 
310 
318  sf::String getSelectedItem() const;
319 
320 
328  sf::String getSelectedItemId() const;
329 
330 
337  int getSelectedItemIndex() const;
338 
339 
353  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
354 
355 
369  bool changeItemById(const sf::String& id, const sf::String& newValue);
370 
371 
383  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
384 
385 
392  std::size_t getItemCount() const;
393 
394 
401  std::vector<sf::String> getItems() const;
402 
403 
412  const std::vector<sf::String>& getItemIds() const;
413 
414 
424  void setMaximumItems(std::size_t maximumItems = 0);
425 
426 
434  std::size_t getMaximumItems() const;
435 
436 
445  void setTextSize(unsigned int textSize);
446 
447 
454  unsigned int getTextSize() const;
455 
456 
461  void setExpandDirection(ExpandDirection direction);
462 
463 
468  ExpandDirection getExpandDirection() const;
469 
470 
475  bool contains(const sf::String& item) const;
476 
477 
482  bool containsId(const sf::String& id) const;
483 
484 
490  void setParent(Container* parent) override;
491 
492 
499  bool mouseOnWidget(Vector2f pos) const override;
500 
504  void leftMousePressed(Vector2f pos) override;
505 
509  void mouseWheelScrolled(float delta, Vector2f pos) override;
510 
511 
519  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
520 
521 
523  protected:
524 
534  Signal& getSignal(std::string signalName) override;
535 
536 
543  void rendererChanged(const std::string& property) override;
544 
545 
549  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
550 
551 
555  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
556 
557 
559  // Returns the size without the borders
561  Vector2f getInnerSize() const;
562 
563 
565  // Update the height of the internal list box
567  void updateListBoxHeight();
568 
569 
571  // Shows the list of items.
573  void showListBox();
574 
575 
577  // Hides the list of items.
579  void hideListBox();
580 
581 
583  // Initialize the internal list box
585  void initListBox();
586 
587 
589  // Makes a copy of the widget
591  Widget::Ptr clone() const override
592  {
593  return std::make_shared<ComboBox>(*this);
594  }
595 
596 
598  public:
599 
600  SignalItem onItemSelect = {"ItemSelected"};
601 
602 
604  protected:
605 
606  // The number of items to display. If there is a scrollbar then you can scroll to see the other.
607  // If there is no scrollbar then this will be the maximum amount of items.
608  std::size_t m_nrOfItemsToDisplay = 0;
609 
610  // Internally a list box is used to store all items
611  ListBox::Ptr m_listBox = ListBox::create();
612 
613  Text m_text;
614 
615  ExpandDirection m_expandDirection = ExpandDirection::Down;
616 
617  Sprite m_spriteBackground;
618  Sprite m_spriteArrow;
619  Sprite m_spriteArrowHover;
620 
621  // Cached renderer properties
622  Borders m_bordersCached;
623  Padding m_paddingCached;
624  Color m_borderColorCached;
625  Color m_backgroundColorCached;
626  Color m_arrowColorCached;
627  Color m_arrowColorHoverCached;
628  Color m_arrowBackgroundColorCached;
629  Color m_arrowBackgroundColorHoverCached;
630 
632  };
633 
635 }
636 
638 
639 #endif // TGUI_COMBO_BOX_HPP
640 
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:43
Wrapper for colors.
Definition: Color.hpp:47
Class to store the position or size of a widget.
Definition: Layout.hpp:243
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:395
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
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
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&#39;t know its exact type.
Definition: ComboBox.hpp:591
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:53