TGUI  0.7.8
ComboBox.hpp
1
2//
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
35namespace 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
92
93
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
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
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
485
486
496
497
511
512
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
790
791
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
Definition: Borders.hpp:38
Implicit converter for colors.
Definition: Color.hpp:40
Definition: ComboBox.hpp:694
void setArrowBackgroundColor(const Color &color)
Set the background color of the arrow that will be used inside the combo box.
void setArrowUpHoverTexture(const Texture &texture)
Changes the arrow up image when the mouse is on top of the arrow.
virtual ObjectConverter getProperty(std::string property) const override
Retrieve the value of a certain property.
void setBackgroundTexture(const Texture &texture)
Changes the background image.
virtual void setProperty(std::string property, const std::string &value) override
Change a property of the renderer.
void setBackgroundColor(const Color &backgroundColor)
Set the background color that will be used inside the combo box.
virtual void setBorders(const Borders &borders) override
Changes the size of the borders.
virtual void setPadding(const Padding &padding) override
Changes the padding of the list box.
void setTextColor(const Color &textColor)
Set the text color that will be used inside the combo box.
void setArrowDownTexture(const Texture &texture)
Changes the arrow down image.
void setArrowBackgroundColorHover(const Color &color)
Set the background color of the arrow when the mouse is standing on top of the combo box.
std::shared_ptr< ListBoxRenderer > getListBox() const
Returns the renderer of the list box.
void setArrowColor(const Color &color)
Set the color of the arrow that will be used inside the combo box.
void setArrowColorNormal(const Color &color)
Set the color of the arrow when the mouse is not on top of the combo box.
void setBorderColor(const Color &borderColor)
Set the border color text that will be used inside the combo box.
virtual void setProperty(std::string property, ObjectConverter &&value) override
Change a property of the renderer.
void setArrowColorHover(const Color &color)
Set the color of the arrow when the mouse is standing on top of the combo box.
void setArrowUpTexture(const Texture &texture)
Changes the arrow up image.
ComboBoxRenderer(ComboBox *comboBox)
Constructor.
Definition: ComboBox.hpp:703
virtual std::map< std::string, ObjectConverter > getPropertyValuePairs() const override
Get a map with all properties and their values.
void setArrowBackgroundColorNormal(const Color &color)
Set the background color of the arrow when the mouse is not on top of the combo box.
void setArrowDownHoverTexture(const Texture &texture)
Changes the arrow down image when the mouse is on top of the arrow.
Combo box widget.
Definition: ComboBox.hpp:52
static ComboBox::Ptr create()
Creates a new combo box widget.
bool removeItem(const sf::String &itemName)
Removes the item from the list with the given name.
virtual void reload(const std::string &primary="", const std::string &secondary="", bool force=false) override
Reload the widget.
void setSize(const Layout2d &size) override
Changes the size of the combo box.
void setTextSize(unsigned int textSize)
Changes the text size of the items.
const std::vector< sf::String > & getItemIds()
Return a copy of the item ids in the combo box.
ComboBox(const ComboBox &copy)
Copy constructor.
std::vector< sf::String > getItems()
Return a copy of the items in the combo box.
unsigned int getTextSize() const
Returns the text size of the items.
bool removeItemById(const sf::String &id)
Removes the item that were added with the given id.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list.
virtual sf::Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
void setListBox(ListBox::Ptr listBox)
Change the internal list box.
bool changeItemById(const sf::String &id, const sf::String &newValue)
Changes the name of an item with the given id to newValue.
virtual void setPosition(const Layout2d &position) override
Set the position of the widget.
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:55
sf::String getItemById(const sf::String &id) const
Returns the item name of the item with the given id.
Definition: ComboBox.hpp:347
int getSelectedItemIndex() const
Get the index of the selected item.
Definition: ComboBox.hpp:385
void removeAllItems()
Removes all items from the list.
bool changeItemByIndex(std::size_t index, const sf::String &newValue)
Changes the name of an item at the given index to newValue.
void deselectItem()
Deselects the selected item.
sf::String getSelectedItemId() const
Get the id of the selected item.
Definition: ComboBox.hpp:373
void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay)
Changes the number of items that are displayed in the list.
bool changeItem(const sf::String &originalValue, const sf::String &newValue)
Changes an item with name originalValue to newValue.
std::size_t getMaximumItems() const
Returns the maximum items that the combo box can contain.
virtual void setFont(const Font &font) override
Changes the font of the text in the widget.
Scrollbar::Ptr getScrollbar() const
Access the scrollbar.
bool addItem(const sf::String &itemName, const sf::String &id="")
Adds an item to the list, so that it can be selected later.
static ComboBox::Ptr copy(ComboBox::ConstPtr comboBox)
Makes a copy of another combo box.
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the combo box can contain.
std::size_t getItemsToDisplay() const
Returns the number of items that are displayed in the list.
Definition: ComboBox.hpp:192
virtual sf::Vector2f getFullSize() const override
Returns the full size of the combo box.
ListBox::Ptr getListBox() const
Returns the internal list box.
std::size_t getItemCount()
Returns the amount of items in the combo box.
Definition: ComboBox.hpp:443
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ComboBox.hpp:631
std::shared_ptr< const ComboBox > ConstPtr
Shared constant widget pointer.
Definition: ComboBox.hpp:56
sf::String getSelectedItem() const
Returns the currently selected item.
Definition: ComboBox.hpp:360
bool setSelectedItemById(const sf::String &id)
Selects an item from the list.
bool setSelectedItem(const sf::String &itemName)
Selects an item from the list.
void setScrollbar(Scrollbar::Ptr scrollbar)
Changes the scrollbar.
virtual void setOpacity(float opacity) override
Changes the opacity of the widget.
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
bool removeItemByIndex(std::size_t index)
Removes the item from the list.
Container widget.
Definition: Container.hpp:48
Definition: Font.hpp:38
Label widget.
Definition: Label.hpp:50
Class to store the position or size of a widget.
Definition: Layout.hpp:255
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:70
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:43
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:53
Definition: Texture.hpp:45
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:137
virtual void setBorders(const Borders &borders)
Changes the size of the borders.
Definition: Borders.hpp:149
Parent class for every widget that has padding.
Definition: Borders.hpp:211
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Base class for all renderer classes.
Definition: Widget.hpp:683
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34