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 {
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();
402 
403 
412  const std::vector<sf::String>& getItemIds();
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 
462  void setExpandDirection(ExpandDirection direction);
463 
464 
470  ExpandDirection getExpandDirection() const;
471 
472 
478  void setParent(Container* parent) override;
479 
480 
487  bool mouseOnWidget(sf::Vector2f pos) const override;
488 
492  void leftMousePressed(sf::Vector2f pos) override;
493 
497  void mouseWheelScrolled(float delta, sf::Vector2f pos) override;
498 
499 
507  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
508 
509 
511  protected:
512 
522  Signal& getSignal(std::string signalName) override;
523 
524 
531  void rendererChanged(const std::string& property) override;
532 
533 
535  // Returns the size without the borders
537  sf::Vector2f getInnerSize() const;
538 
539 
541  // Update the height of the internal list box
543  void updateListBoxHeight();
544 
545 
547  // Shows the list of items.
549  void showListBox();
550 
551 
553  // Hides the list of items.
555  void hideListBox();
556 
557 
559  // Initialize the internal list box
561  void initListBox();
562 
563 
565  // Makes a copy of the widget
567  Widget::Ptr clone() const override
568  {
569  return std::make_shared<ComboBox>(*this);
570  }
571 
572 
574  public:
575 
576  SignalItem onItemSelect = {"ItemSelected"};
577 
578 
580  protected:
581 
582  // The number of items to display. If there is a scrollbar then you can scroll to see the other.
583  // If there is no scrollbar then this will be the maximum amount of items.
584  std::size_t m_nrOfItemsToDisplay = 0;
585 
586  // Internally a list box is used to store all items
587  ListBox::Ptr m_listBox = ListBox::create();
588 
589  Text m_text;
590 
591  ExpandDirection m_expandDirection = ExpandDirection::Down;
592 
593  Sprite m_spriteBackground;
594  Sprite m_spriteArrow;
595  Sprite m_spriteArrowHover;
596 
597  // Cached renderer properties
598  Borders m_bordersCached;
599  Padding m_paddingCached;
600  Color m_borderColorCached;
601  Color m_backgroundColorCached;
602  Color m_arrowColorCached;
603  Color m_arrowColorHoverCached;
604  Color m_arrowBackgroundColorCached;
605  Color m_arrowBackgroundColorHoverCached;
606 
608  };
609 
611 }
612 
614 
615 #endif // TGUI_COMBO_BOX_HPP
616 
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:243
The parent class for every widget.
Definition: Widget.hpp:62
Combo box widget.
Definition: ComboBox.hpp:40
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:45
Definition: Text.hpp:41
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:394
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:66
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&#39;t know its exact type.
Definition: ComboBox.hpp:567
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:53