TGUI 1.13
Loading...
Searching...
No Matches
ComboBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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#ifndef TGUI_COMBO_BOX_HPP
26#define TGUI_COMBO_BOX_HPP
27
28#include <TGUI/Renderers/ComboBoxRenderer.hpp>
29#include <TGUI/Widgets/ListBox.hpp>
30
32
33namespace tgui
34{
38 class TGUI_API ComboBox : public Widget
39 {
40 public:
42 enum class ExpandDirection : std::uint8_t
43 {
44 Down,
45 Up,
46 Automatic
47 };
48
49 public:
50 using Ptr = std::shared_ptr<ComboBox>;
51 using ConstPtr = std::shared_ptr<const ComboBox>;
52
53 static constexpr char StaticWidgetType[] = "ComboBox";
54
62 explicit ComboBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
63
65 // Copy constructor
67 ComboBox(const ComboBox& other);
68
70 // Move constructor
72 ComboBox(ComboBox&& other) noexcept;
73
75 // Copy assignment operator
77 ComboBox& operator=(const ComboBox& other);
78
80 // Move assignment operator
82 ComboBox& operator=(ComboBox&& other) noexcept;
83
89 [[nodiscard]] static ComboBox::Ptr create();
90
98 [[nodiscard]] static ComboBox::Ptr copy(const ComboBox::ConstPtr& comboBox);
99
104 [[nodiscard]] ComboBoxRenderer* getSharedRenderer() override;
105 [[nodiscard]] const ComboBoxRenderer* getSharedRenderer() const override;
106
112 [[nodiscard]] [[nodiscard]] ComboBoxRenderer* getRenderer() override;
113
121 void setSize(const Layout2d& size) override;
122 using Widget::setSize;
123
131 void setEnabled(bool enabled) override;
132
143 void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
144
155 [[nodiscard]] std::size_t getItemsToDisplay() const;
156
167 std::size_t addItem(const String& itemName, const String& id = "");
168
178 void addMultipleItems(const std::vector<String>& itemNames);
179
197 bool setSelectedItem(const String& itemName);
198
217
230 bool setSelectedItemByIndex(std::size_t index);
231
238
250 bool removeItem(const String& itemName);
251
263 bool removeItemById(const String& id);
264
277 bool removeItemByIndex(std::size_t index);
278
283
293 [[nodiscard]] String getItemById(const String& id) const;
294
304 [[nodiscard]] String getItemByIndex(std::size_t index) const;
305
317 [[nodiscard]] int getIndexById(const String& id) const;
318
328 [[nodiscard]] String getIdByIndex(std::size_t index) const;
329
336 [[nodiscard]] String getSelectedItem() const;
337
344 [[nodiscard]] String getSelectedItemId() const;
345
351 [[nodiscard]] int getSelectedItemIndex() const;
352
365 bool changeItem(const String& originalValue, const String& newValue);
366
379 bool changeItemById(const String& id, const String& newValue);
380
391 bool changeItemByIndex(std::size_t index, const String& newValue);
392
398 [[nodiscard]] std::size_t getItemCount() const;
399
405 [[nodiscard]] std::vector<String> getItems() const;
406
414 [[nodiscard]] std::vector<String> getItemIds() const;
415
430 void setItemData(std::size_t index, Any data);
431
440 template <typename DataType>
441 [[nodiscard]] DataType getItemData(std::size_t index) const
442 {
443 return m_listBox->getItemData<DataType>(index);
444 }
445
454 void setMaximumItems(std::size_t maximumItems = 0);
455
462 [[nodiscard]] std::size_t getMaximumItems() const;
463
469 void setDefaultText(const String& defaultText);
470
476 [[nodiscard]] const String& getDefaultText() const;
477
483
488 [[nodiscard]] ExpandDirection getExpandDirection() const;
489
494 [[nodiscard]] bool contains(const String& item) const;
495
500 [[nodiscard]] bool containsId(const String& id) const;
501
507 void setChangeItemOnScroll(bool changeOnScroll);
508
514 [[nodiscard]] bool getChangeItemOnScroll() const;
515
521 void setParent(Container* parent) override;
522
528 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
529
533 bool leftMousePressed(Vector2f pos) override;
534
538 bool scrolled(float delta, Vector2f pos, bool touch) override;
539
546 void draw(BackendRenderTarget& target, RenderStates states) const override;
547
549
550 protected:
560 [[nodiscard]] Signal& getSignal(String signalName) override;
561
567 void rendererChanged(const String& property) override;
568
572 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
573
577 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
578
582 void updateTextSize() override;
583
585 // Returns the size without the borders
587 [[nodiscard]] Vector2f getInnerSize() const;
588
590 // Update the height of the internal list box
592 void updateListBoxHeight();
593
595 // Shows the list of items.
597 void showListBox();
598
600 // Hides the list of items.
602 void hideListBox();
603
605 // Initialize the internal list box
607 void initListBox();
608
610 // Makes a copy of the widget
612 [[nodiscard]] Widget::Ptr clone() const override;
613
615
616 public:
617 SignalItem onItemSelect = {"ItemSelected"};
618
620
621 protected:
622 // The number of items to display. If there is a scrollbar then you can scroll to see the other.
623 // If there is no scrollbar then this will be the maximum amount of items.
624 std::size_t m_nrOfItemsToDisplay = 0;
625
626 // Internally a list box is used to store all items
627 ListBox::Ptr m_listBox = ListBox::create();
628
629 Text m_text;
630 Text m_defaultText;
631
632 int m_previousSelectedItemIndex = -1;
633 bool m_changeItemOnScroll = false;
634
635 ExpandDirection m_expandDirection = ExpandDirection::Automatic;
636
637 Sprite m_spriteBackground;
638 Sprite m_spriteBackgroundDisabled;
639 Sprite m_spriteArrow;
640 Sprite m_spriteArrowHover;
641 Sprite m_spriteArrowDisabled;
642
643 // Cached renderer properties
644 Borders m_bordersCached;
645 Padding m_paddingCached;
646 Color m_borderColorCached;
647 Color m_backgroundColorCached;
648 Color m_backgroundColorDisabledCached;
649 Color m_arrowColorCached;
650 Color m_arrowColorHoverCached;
651 Color m_arrowColorDisabledCached;
652 Color m_arrowBackgroundColorCached;
653 Color m_arrowBackgroundColorHoverCached;
654 Color m_arrowBackgroundColorDisabledCached;
655 Color m_textColorCached;
656 Color m_textColorDisabledCached;
657 float m_roundedBorderRadiusCached = 0;
658 };
659} // namespace tgui
660
661#endif // TGUI_COMBO_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:63
Definition ComboBoxRenderer.hpp:35
static ComboBox::Ptr create()
Creates a new combo box widget.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
bool removeItemById(const String &id)
Removes the item that were added with the given id.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
bool contains(const String &item) const
Returns whether the combo box contains the given item.
void setSize(const Layout2d &size) override
Changes the size of the combo box.
ComboBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
String getIdByIndex(std::size_t index) const
Returns the id of the item at the given index.
std::size_t getItemCount() const
Returns the amount of items in the combo box.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list.
String getSelectedItemId() const
Gets the id of the selected item.
std::vector< String > getItemIds() const
Returns a copy of the item ids in the combo box.
String getSelectedItem() const
Returns the currently selected item.
bool getChangeItemOnScroll() const
Returns whether the mouse wheel can be used to change the selected item while the list is closed.
int getIndexById(const String &id) const
Returns the index of the item with the given id.
bool setSelectedItem(const String &itemName)
Selects an item from the list.
bool changeItemByIndex(std::size_t index, const String &newValue)
Changes the name of an item at the given index to newValue.
void setDefaultText(const String &defaultText)
Changes the default text of the combo box. This is the text drawn when no item is selected.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition ComboBox.hpp:53
bool changeItem(const String &originalValue, const String &newValue)
Changes an item with name originalValue to newValue.
void setChangeItemOnScroll(bool changeOnScroll)
Changes whether the mouse wheel can be used to change the selected item while the list is closed.
bool removeItem(const String &itemName)
Removes the item from the list with the given name.
const String & getDefaultText() const
Returns the default text of the combo box. This is the text drawn when no item is selected.
ExpandDirection
The side where the list will be displayed.
Definition ComboBox.hpp:43
static ComboBox::Ptr copy(const ComboBox::ConstPtr &comboBox)
Makes a copy of another combo box.
std::shared_ptr< const ComboBox > ConstPtr
Shared constant widget pointer.
Definition ComboBox.hpp:51
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool containsId(const String &id) const
Returns whether the combo box contains an item with the given id.
int getSelectedItemIndex() const
Gets the index of the selected item.
void removeAllItems()
Removes all items from the list.
void setExpandDirection(ExpandDirection direction)
Changes the side where the list is displayed.
void deselectItem()
Deselects the selected item.
void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay)
Changes the number of items that are displayed in the list.
SignalItem onItemSelect
An item was selected in the combo box. Optional parameter: selected item or its index.
Definition ComboBox.hpp:617
std::size_t getMaximumItems() const
Returns the maximum items that the combo box can contain.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void setEnabled(bool enabled) override
Enables or disables the widget.
std::size_t addItem(const String &itemName, const String &id="")
Adds an item to the list, so that it can be selected later.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
DataType getItemData(std::size_t index) const
Returns user data stored in the item.
Definition ComboBox.hpp:441
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.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
bool setSelectedItemById(const String &id)
Selects an item from the list.
String getItemById(const String &id) const
Returns the item name of the item with the given id.
void addMultipleItems(const std::vector< String > &itemNames)
Adds multiple items to the list.
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
ExpandDirection getExpandDirection() const
Returns the side where the list is displayed.
ComboBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setItemData(std::size_t index, Any data)
Store some user data with the item.
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition ComboBox.hpp:50
String getItemByIndex(std::size_t index) const
Returns the item name of the item at the given index.
bool removeItemByIndex(std::size_t index)
Removes the item from the list.
bool changeItemById(const String &id, const String &newValue)
Changes the name of an item with the given id to newValue.
std::vector< String > getItems() const
Returns a copy of the items in the combo box.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
Class to store the position or size of a widget.
Definition Layout.hpp:320
static ListBox::Ptr create()
Creates a new list box widget.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition ListBox.hpp:42
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:511
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Definition Sprite.hpp:45
Wrapper class to store strings.
Definition String.hpp:94
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:53
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
States used for drawing.
Definition RenderStates.hpp:38