TGUI  v0.7.5
ComboBox.hpp
1 //
3 // TGUI - Texus's 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/Label.hpp>
31 #include <TGUI/Widgets/ListBox.hpp>
32 
34 
35 namespace tgui
36 {
37  class ComboBoxRenderer;
38 
51  class TGUI_API ComboBox : public Widget
52  {
53  public:
54 
55  typedef std::shared_ptr<ComboBox> Ptr;
56  typedef std::shared_ptr<const ComboBox> ConstPtr;
57 
58 
60  // Default constructor
62  ComboBox();
63 
64 
71  ComboBox(const ComboBox& copy);
72 
73 
82  ComboBox& operator= (const ComboBox& right);
83 
84 
91  static ComboBox::Ptr create();
92 
93 
102  static ComboBox::Ptr copy(ComboBox::ConstPtr comboBox);
103 
104 
111  std::shared_ptr<ComboBoxRenderer> getRenderer() const
112  {
113  return std::static_pointer_cast<ComboBoxRenderer>(m_renderer);
114  }
115 
116 
129  virtual void setPosition(const Layout2d& position) override;
131 
132 
141  void setSize(const Layout2d& size) override;
143 
144 
153  virtual sf::Vector2f getFullSize() const override;
154 
155 
164  virtual void setFont(const Font& font) override;
165 
166 
178  void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
179 
180 
192  std::size_t getItemsToDisplay() const
193  {
194  return m_nrOfItemsToDisplay;
195  }
196 
197 
214  bool addItem(const sf::String& itemName, const sf::String& id = "");
215 
216 
235  bool setSelectedItem(const sf::String& itemName);
236 
237 
256  bool setSelectedItemById(const sf::String& id);
257 
258 
272  bool setSelectedItemByIndex(std::size_t index);
273 
274 
281  void deselectItem();
282 
283 
296  bool removeItem(const sf::String& itemName);
297 
298 
311  bool removeItemById(const sf::String& id);
312 
313 
327  bool removeItemByIndex(std::size_t index);
328 
329 
334  void removeAllItems();
335 
336 
347  sf::String getItemById(const sf::String& id) const
348  {
349  return m_listBox->getItemById(id);
350  }
351 
352 
360  sf::String getSelectedItem() const
361  {
362  return m_listBox->getSelectedItem();
363  }
364 
365 
373  sf::String getSelectedItemId() const
374  {
375  return m_listBox->getSelectedItemId();
376  }
377 
378 
386  {
387  return m_listBox->getSelectedItemIndex();
388  }
389 
390 
404  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
405 
406 
420  bool changeItemById(const sf::String& id, const sf::String& newValue);
421 
422 
434  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
435 
436 
443  std::size_t getItemCount()
444  {
445  return m_listBox->getItemCount();
446  }
447 
448 
455  std::vector<sf::String> getItems();
456 
457 
466  const std::vector<sf::String>& getItemIds();
467 
468 
484  void setScrollbar(Scrollbar::Ptr scrollbar);
485 
486 
495  Scrollbar::Ptr getScrollbar() const;
496 
497 
510  void setListBox(ListBox::Ptr listBox);
511 
512 
521  ListBox::Ptr getListBox() const;
522 
523 
533  void setMaximumItems(std::size_t maximumItems = 0);
534 
535 
543  std::size_t getMaximumItems() const;
544 
545 
554  void setTextSize(unsigned int textSize);
555 
556 
563  unsigned int getTextSize() const;
564 
565 
572  virtual void setOpacity(float opacity) override;
573 
574 
583  virtual sf::Vector2f getWidgetOffset() const override;
584 
585 
591  virtual void setParent(Container* parent) override;
592 
593 
597  virtual bool mouseOnWidget(float x, float y) const override;
598 
602  virtual void leftMousePressed(float x, float y) override;
603 
607  virtual void mouseWheelMoved(int delta, int x, int y) override;
608 
609 
611  protected:
612 
625  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
626 
627 
629  // Makes a copy of the widget
631  virtual Widget::Ptr clone() const override
632  {
633  return std::make_shared<ComboBox>(*this);
634  }
635 
636 
638  // Shows the list of items.
640  void showListBox();
641 
642 
644  // Hides the list of items.
646  void hideListBox();
647 
648 
650  // Initialize the internal list box
652  void initListBox();
653 
654 
656  // Called by the internal ListBox when a different item is selected.
658  void newItemSelectedCallbackFunction();
659 
660 
662  // Called by the internal ListBox when it gets unfocused.
664  void listBoxUnfocusedCallbackFunction();
665 
666 
668  // Draws the widget on the render target.
670  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
671 
672 
674  protected:
675 
676  // The number of items to display. If there is a scrollbar then you can scroll to see the other.
677  // If there is no scrollbar then this will be the maximum amount of items.
678  std::size_t m_nrOfItemsToDisplay = 0;
679 
680  // Internally a list box is used to store all items
681  ListBox::Ptr m_listBox = std::make_shared<ListBox>();
682 
683  Label m_text;
684 
685  friend class ComboBoxRenderer;
686 
688  };
689 
690 
692 
693  class TGUI_API ComboBoxRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
694  {
695  public:
696 
703  ComboBoxRenderer(ComboBox* comboBox) : m_comboBox{comboBox} {}
704 
705 
716  virtual void setProperty(std::string property, const std::string& value) override;
717 
718 
730  virtual void setProperty(std::string property, ObjectConverter&& value) override;
731 
732 
742  virtual ObjectConverter getProperty(std::string property) const override;
743 
744 
751  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
752 
753 
762  void setBackgroundColor(const Color& backgroundColor);
763 
764 
778  void setArrowBackgroundColor(const Color& color);
779 
780 
789  void setArrowBackgroundColorNormal(const Color& color);
790 
791 
800  void setArrowBackgroundColorHover(const Color& color);
801 
802 
816  void setArrowColor(const Color& color);
817 
818 
827  void setArrowColorNormal(const Color& color);
828 
829 
838  void setArrowColorHover(const Color& color);
839 
840 
847  void setTextColor(const Color& textColor);
848 
849 
856  void setBorderColor(const Color& borderColor);
857 
858 
868  void setBackgroundTexture(const Texture& texture);
869 
870 
880  void setArrowUpTexture(const Texture& texture);
881 
882 
892  void setArrowDownTexture(const Texture& texture);
893 
894 
903  void setArrowUpHoverTexture(const Texture& texture);
904 
905 
914  void setArrowDownHoverTexture(const Texture& texture);
915 
916 
923  virtual void setBorders(const Borders& borders) override;
925 
926 
936  virtual void setPadding(const Padding& padding) override;
938 
939 
946  std::shared_ptr<ListBoxRenderer> getListBox() const;
947 
948 
950  // Draws the widget on the render target.
952  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
953 
954 
956  private:
957 
959  // Returns the padding, which is possibly scaled with the background image.
961  Padding getScaledPadding() const;
962 
963 
965  // Makes a copy of the renderer
967  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
968 
969 
971  protected:
972 
973  ComboBox* m_comboBox;
974 
975  Texture m_backgroundTexture;
976 
977  Texture m_textureArrowUpNormal;
978  Texture m_textureArrowUpHover;
979  Texture m_textureArrowDownNormal;
980  Texture m_textureArrowDownHover;
981 
982  sf::Color m_textColor;
983  sf::Color m_arrowBackgroundColorNormal;
984  sf::Color m_arrowBackgroundColorHover;
985  sf::Color m_arrowColorNormal;
986  sf::Color m_arrowColorHover;
987 
988  friend class ComboBox;
989 
991  };
992 
994 }
995 
997 
998 #endif // TGUI_COMBO_BOX_HPP
999 
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
virtual void setBorders(const Borders &borders)
Changes the size of the borders.
Definition: Borders.hpp:149
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Implicit converter for colors.
Definition: Color.hpp:39
sf::String getSelectedItem() const
Returns the currently selected item.
Definition: ComboBox.hpp:360
std::size_t getItemCount()
Returns the amount of items in the combo box.
Definition: ComboBox.hpp:443
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Label widget.
Definition: Label.hpp:49
The parent class for every widget.
Definition: Widget.hpp:71
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:53
Combo box widget.
Definition: ComboBox.hpp:51
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:70
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Parent class for every widget that has padding.
Definition: Borders.hpp:210
std::size_t getItemsToDisplay() const
Returns the number of items that are displayed in the list.
Definition: ComboBox.hpp:192
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:136
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::shared_ptr< const ComboBox > ConstPtr
Shared constant widget pointer.
Definition: ComboBox.hpp:56
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
sf::String getItemById(const sf::String &id) const
Returns the item name of the item with the given id.
Definition: ComboBox.hpp:347
Container widget.
Definition: Container.hpp:47
Definition: Texture.hpp:44
Definition: Borders.hpp:37
int getSelectedItemIndex() const
Get the index of the selected item.
Definition: ComboBox.hpp:385
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: ComboBox.hpp:631
sf::String getSelectedItemId() const
Get the id of the selected item.
Definition: ComboBox.hpp:373
Definition: ComboBox.hpp:693
ComboBoxRenderer(ComboBox *comboBox)
Constructor.
Definition: ComboBox.hpp:703
std::shared_ptr< ComboBoxRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: ComboBox.hpp:111
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:55
Base class for all renderer classes.
Definition: Widget.hpp:682