TGUI  0.7.8
ListBox.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_LIST_BOX_HPP
27#define TGUI_LIST_BOX_HPP
28
29
30#include <TGUI/Widgets/Scrollbar.hpp>
31
33
34namespace tgui
35{
36 class Label;
37 class ListBoxRenderer;
38
66 class TGUI_API ListBox : public Widget
67 {
68 public:
69
70 typedef std::shared_ptr<ListBox> Ptr;
71 typedef std::shared_ptr<const ListBox> ConstPtr;
72
73
75 // Default constructor
77 ListBox();
78
79
86 ListBox(const ListBox& copy);
87
88
97 ListBox& operator= (const ListBox& right);
98
99
107
108
118
119
126 std::shared_ptr<ListBoxRenderer> getRenderer() const
127 {
128 return std::static_pointer_cast<ListBoxRenderer>(m_renderer);
129 }
130
131
144 virtual void setPosition(const Layout2d& position) override;
146
147
154 void setSize(const Layout2d& size) override;
156
157
166 virtual sf::Vector2f getFullSize() const override;
167
168
177 virtual void setFont(const Font& font) override;
178
179
194 bool addItem(const sf::String& itemName, const sf::String& id = "");
195
196
211 bool setSelectedItem(const sf::String& itemName);
212
213
228 bool setSelectedItemById(const sf::String& id);
229
230
244 bool setSelectedItemByIndex(std::size_t index);
245
246
252
253
266 bool removeItem(const sf::String& itemName);
267
268
281 bool removeItemById(const sf::String& id);
282
283
297 bool removeItemByIndex(std::size_t index);
298
299
305
306
317 sf::String getItemById(const sf::String& id) const;
318
319
327 sf::String getSelectedItem() const;
328
329
337 sf::String getSelectedItemId() const;
338
339
347
348
362 bool changeItem(const sf::String& originalValue, const sf::String& newValue);
363
364
378 bool changeItemById(const sf::String& id, const sf::String& newValue);
379
380
392 bool changeItemByIndex(std::size_t index, const sf::String& newValue);
393
394
401 std::size_t getItemCount()
402 {
403 return m_items.size();
404 }
405
406
413 std::vector<sf::String> getItems();
414
415
424 const std::vector<sf::String>& getItemIds();
425
426
443
444
454
455
464 void setItemHeight(unsigned int itemHeight);
465
466
473 unsigned int getItemHeight() const;
474
475
487 void setTextSize(unsigned int textSize);
488
489
496 unsigned int getTextSize() const;
497
498
508 void setMaximumItems(std::size_t maximumItems = 0);
509
510
518 std::size_t getMaximumItems() const
519 {
520 return m_maxItems;
521 }
522
523
532 void setAutoScroll(bool autoScroll);
533
534
542 bool getAutoScroll() const
543 {
544 return m_autoScroll;
545 }
546
547
554 virtual void setOpacity(float opacity) override;
555
556
565 virtual sf::Vector2f getWidgetOffset() const override;
566
567
571 virtual bool mouseOnWidget(float x, float y) const override;
572
576 virtual void leftMousePressed(float x, float y) override;
577
581 virtual void leftMouseReleased(float x, float y) override;
582
586 virtual void mouseMoved(float x, float y) override;
587
591 virtual void mouseWheelMoved(int delta, int x, int y) override;
592
596 virtual void mouseNoLongerOnWidget() override;
597
601 virtual void mouseNoLongerDown() override;
602
603
605 protected:
606
608 // Update the color of the items
610 void updateItemColors();
611
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<ListBox>(*this);
634 }
635
636
638 // This function is called every frame with the time passed since the last frame.
640 virtual void update(sf::Time elapsedTime) override;
641
642
644 // Draws the widget on the render target.
646 virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
647
648
650 protected:
651
652 // This contains the different items in the list box
653 std::vector<Label> m_items;
654 std::vector<sf::String> m_itemIds;
655
656 // What is the index of the selected item?
657 // This is also used by combo box, so it can't just be changed to a pointer!
658 int m_selectedItem = -1;
659
660 int m_hoveringItem = -1;
661
662 // The size must be stored
663 unsigned int m_itemHeight = 22;
664 unsigned int m_requestedTextSize = 0;
665 unsigned int m_textSize = 18;
666
667 // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
668 std::size_t m_maxItems = 0;
669
670 // When there are too many items a scrollbar will be shown
671 Scrollbar::Ptr m_scroll = std::make_shared<Scrollbar>();
672
673 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
674 bool m_possibleDoubleClick = false;
675
676 bool m_autoScroll = true;
677
678 // ComboBox contains a list box internally and it should be able to adjust it.
679 friend class ComboBox;
680 friend class ListBoxRenderer;
681
683 };
684
685
687
688 class TGUI_API ListBoxRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
689 {
690 public:
691
698 ListBoxRenderer(ListBox* listBox) : m_listBox{listBox} {}
699
700
711 virtual void setProperty(std::string property, const std::string& value) override;
712
713
725 virtual void setProperty(std::string property, ObjectConverter&& value) override;
726
727
737 virtual ObjectConverter getProperty(std::string property) const override;
738
739
746 virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
747
748
755 void setBackgroundColor(const Color& backgroundColor);
756
757
769 void setTextColor(const Color& textColor);
770
771
778 void setTextColorNormal(const Color& color);
779
780
787 void setTextColorHover(const Color& color);
788
789
796 void setHoverBackgroundColor(const Color& hoverBackgroundColor);
797
798
805 void setSelectedBackgroundColor(const Color& selectedBackgroundColor);
806
807
814 void setSelectedTextColor(const Color& selectedTextColor);
815
816
823 void setBorderColor(const Color& borderColor);
824
825
835 void setBackgroundTexture(const Texture& texture);
836
837
847 virtual void setPadding(const Padding& padding) override;
849
850
852 // Draws the widget on the render target.
854 void draw(sf::RenderTarget& target, sf::RenderStates states) const;
855
856
858 private:
859
861 // Returns the padding, which is possibly scaled with the background image.
863 Padding getScaledPadding() const;
864
865
867 // Makes a copy of the renderer
869 virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
870
871
873 protected:
874
875 ListBox* m_listBox;
876
877 Texture m_backgroundTexture;
878
879 sf::Color m_backgroundColor;
880 sf::Color m_textColor;
881 sf::Color m_hoverBackgroundColor;
882 sf::Color m_hoverTextColor;
883 sf::Color m_selectedBackgroundColor;
884 sf::Color m_selectedTextColor;
885 sf::Color m_borderColor;
886
887 friend class ListBox;
888 friend class ComboBox;
889 friend class ComboBoxRenderer;
890
892 };
893
895}
896
898
899#endif // TGUI_LIST_BOX_HPP
Definition: Borders.hpp:38
Implicit converter for colors.
Definition: Color.hpp:40
Definition: ComboBox.hpp:694
Combo box widget.
Definition: ComboBox.hpp:52
Definition: Font.hpp:38
Class to store the position or size of a widget.
Definition: Layout.hpp:255
Definition: ListBox.hpp:689
void setTextColorNormal(const Color &color)
Changes the color of the text in the normal state (mouse not on top of the item).
void setSelectedBackgroundColor(const Color &selectedBackgroundColor)
Set the background color of the selected text that will be used inside the list box.
void setHoverBackgroundColor(const Color &hoverBackgroundColor)
Set the background color of the unselected item on which the mouse is standing.
virtual void setProperty(std::string property, const std::string &value) override
Change a property of the renderer.
void setTextColorHover(const Color &color)
Changes the color of the text in the hover state (mouse is standing on top of the item).
virtual void setProperty(std::string property, ObjectConverter &&value) override
Change a property of the renderer.
virtual ObjectConverter getProperty(std::string property) const override
Retrieve the value of a certain property.
ListBoxRenderer(ListBox *listBox)
Constructor.
Definition: ListBox.hpp:698
void setTextColor(const Color &textColor)
Set the text color that will be used inside the list box.
void setBackgroundTexture(const Texture &texture)
Changes the background image.
void setBackgroundColor(const Color &backgroundColor)
Set the background color that will be used inside the list box.
virtual std::map< std::string, ObjectConverter > getPropertyValuePairs() const override
Get a map with all properties and their values.
void setBorderColor(const Color &borderColor)
Set the border color text that will be used inside the list box.
virtual void setPadding(const Padding &padding) override
Changes the padding of the list box.
void setSelectedTextColor(const Color &selectedTextColor)
Set the text color of the selected text that will be used inside the list box.
List box widget.
Definition: ListBox.hpp:67
void setTextSize(unsigned int textSize)
Changes the text size of the items.
void setItemHeight(unsigned int itemHeight)
Changes the height of the items in the list box.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:70
bool changeItemById(const sf::String &id, const sf::String &newValue)
Changes the name of an item with the given id to newValue.
virtual sf::Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
unsigned int getItemHeight() const
Returns the height of the items in the list box.
Scrollbar::Ptr getScrollbar() const
Access the scrollbar of the list box.
virtual void setFont(const Font &font) override
Changes the font of the text in the widget.
void deselectItem()
Deselects the selected item.
bool addItem(const sf::String &itemName, const sf::String &id="")
Adds an item to the list.
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition: ListBox.hpp:71
bool removeItem(const sf::String &itemName)
Removes the item from the list with the given name.
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the list box can contain.
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ListBox.hpp:631
bool setSelectedItem(const sf::String &itemName)
Selects an item in the list box.
virtual sf::Vector2f getFullSize() const override
Returns the full size of the list box.
static ListBox::Ptr copy(ListBox::ConstPtr listBox)
Makes a copy of another list box.
static ListBox::Ptr create()
Creates a new list box widget.
virtual void reload(const std::string &primary="", const std::string &secondary="", bool force=false) override
Reload the widget.
bool removeItemById(const sf::String &id)
Removes the item that were added with the given id.
void removeAllItems()
Removes all items from the list.
const std::vector< sf::String > & getItemIds()
Return a copy of the item ids in the list box.
bool removeItemByIndex(std::size_t index)
Removes the item from the list box.
virtual void setPosition(const Layout2d &position) override
Set the position of the widget.
std::shared_ptr< ListBoxRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Definition: ListBox.hpp:126
void setSize(const Layout2d &size) override
Changes the size of the list box.
std::size_t getItemCount()
Returns the amount of items in the list box.
Definition: ListBox.hpp:401
ListBox(const ListBox &copy)
Copy constructor.
bool getAutoScroll() const
Returns the maximum items that the list box can contain.
Definition: ListBox.hpp:542
bool setSelectedItemById(const sf::String &id)
Selects an item in the list box.
sf::String getItemById(const sf::String &id) const
Returns the item name of the item with the given id.
int getSelectedItemIndex() const
Get the index of the selected item.
sf::String getSelectedItem() const
Returns the currently selected item.
void setAutoScroll(bool autoScroll)
Changes whether the list box scrolls to the bottom when a new item is added.
std::size_t getMaximumItems() const
Returns the maximum items that the list box can contain.
Definition: ListBox.hpp:518
virtual void setOpacity(float opacity) override
Changes the opacity of the widget.
bool changeItemByIndex(std::size_t index, const sf::String &newValue)
Changes the name of an item at the given index to newValue.
void setScrollbar(Scrollbar::Ptr scrollbar)
Changes the scrollbar of the list box.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list box.
sf::String getSelectedItemId() const
Get the id of the selected item.
unsigned int getTextSize() const
Returns the text size of the items.
std::vector< sf::String > getItems()
Return a copy of the items in the list box.
bool changeItem(const sf::String &originalValue, const sf::String &newValue)
Changes an item with name originalValue to newValue.
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
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