--- --- TGUI: include/TGUI/Widgets/ComboBox.hpp Source File
TGUI  1.x-dev
Loading...
Searching...
No Matches
ComboBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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/Widgets/ListBox.hpp>
29#include <TGUI/Renderers/ComboBoxRenderer.hpp>
30
32
33TGUI_MODULE_EXPORT namespace tgui
34{
38 class TGUI_API ComboBox : public Widget
39 {
40 public:
41
43 enum class ExpandDirection
44 {
45 Down,
46 Up,
47 Automatic
48 };
49
50 public:
51
52 using Ptr = std::shared_ptr<ComboBox>;
53 using ConstPtr = std::shared_ptr<const ComboBox>;
54
55 static constexpr const char StaticWidgetType[] = "ComboBox";
56
64 ComboBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
65
67 // Copy constructor
69 ComboBox(const ComboBox& other);
70
72 // Move constructor
74 ComboBox(ComboBox&& other) noexcept;
75
77 // Copy assignment operator
79 ComboBox& operator= (const ComboBox& other);
80
82 // Move assignment operator
84 ComboBox& operator= (ComboBox&& other) noexcept;
85
92 TGUI_NODISCARD static ComboBox::Ptr create();
93
102 TGUI_NODISCARD static ComboBox::Ptr copy(const ComboBox::ConstPtr& comboBox);
103
108 TGUI_NODISCARD ComboBoxRenderer* getSharedRenderer() override;
109 TGUI_NODISCARD const ComboBoxRenderer* getSharedRenderer() const override;
110
116 TGUI_NODISCARD TGUI_NODISCARD ComboBoxRenderer* getRenderer() override;
117
126 void setSize(const Layout2d& size) override;
127 using Widget::setSize;
128
136 void setEnabled(bool enabled) override;
137
149 void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
150
162 TGUI_NODISCARD std::size_t getItemsToDisplay() const;
163
174 std::size_t addItem(const String& itemName, const String& id = "");
175
194 bool setSelectedItem(const String& itemName);
195
215
229 bool setSelectedItemByIndex(std::size_t index);
230
238
251 bool removeItem(const String& itemName);
252
265 bool removeItemById(const String& id);
266
280 bool removeItemByIndex(std::size_t index);
281
287
298 TGUI_NODISCARD String getItemById(const String& id) const;
299
307 TGUI_NODISCARD String getSelectedItem() const;
308
316 TGUI_NODISCARD String getSelectedItemId() const;
317
324 TGUI_NODISCARD int getSelectedItemIndex() const;
325
339 bool changeItem(const String& originalValue, const String& newValue);
340
354 bool changeItemById(const String& id, const String& newValue);
355
367 bool changeItemByIndex(std::size_t index, const String& newValue);
368
375 TGUI_NODISCARD std::size_t getItemCount() const;
376
383 TGUI_NODISCARD std::vector<String> getItems() const;
384
393 TGUI_NODISCARD std::vector<String> getItemIds() const;
394
407 void setItemData(std::size_t index, Any data);
408
415 template <typename DataType>
416 TGUI_NODISCARD DataType getItemData(std::size_t index) const
417 {
418 return m_listBox->getItemData<DataType>(index);
419 }
420
430 void setMaximumItems(std::size_t maximumItems = 0);
431
439 TGUI_NODISCARD std::size_t getMaximumItems() const;
440
446 void setDefaultText(const String& defaultText);
447
453 TGUI_NODISCARD const String& getDefaultText() const;
454
460
465 TGUI_NODISCARD ExpandDirection getExpandDirection() const;
466
471 TGUI_NODISCARD bool contains(const String& item) const;
472
477 TGUI_NODISCARD bool containsId(const String& id) const;
478
484 void setChangeItemOnScroll(bool changeOnScroll);
485
491 TGUI_NODISCARD bool getChangeItemOnScroll() const;
492
498 void setParent(Container* parent) override;
499
506 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
507
511 bool leftMousePressed(Vector2f pos) override;
512
516 bool scrolled(float delta, Vector2f pos, bool touch) override;
517
525 void draw(BackendRenderTarget& target, RenderStates states) const override;
526
528 protected:
529
539 TGUI_NODISCARD Signal& getSignal(String signalName) override;
540
547 void rendererChanged(const String& property) override;
548
552 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
553
557 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
558
562 void updateTextSize() override;
563
565 // Returns the size without the borders
567 TGUI_NODISCARD Vector2f getInnerSize() const;
568
570 // Update the height of the internal list box
572 void updateListBoxHeight();
573
575 // Shows the list of items.
577 void showListBox();
578
580 // Hides the list of items.
582 void hideListBox();
583
585 // Initialize the internal list box
587 void initListBox();
588
590 // Makes a copy of the widget
592 TGUI_NODISCARD Widget::Ptr clone() const override;
593
595 public:
596
597 SignalItem onItemSelect = {"ItemSelected"};
598
600 protected:
601
602 // The number of items to display. If there is a scrollbar then you can scroll to see the other.
603 // If there is no scrollbar then this will be the maximum amount of items.
604 std::size_t m_nrOfItemsToDisplay = 0;
605
606 // Internally a list box is used to store all items
607 ListBox::Ptr m_listBox = ListBox::create();
608
609 Text m_text;
610 Text m_defaultText;
611
612 int m_previousSelectedItemIndex = -1;
613 bool m_changeItemOnScroll = false;
614
615 ExpandDirection m_expandDirection = ExpandDirection::Automatic;
616
617 Sprite m_spriteBackground;
618 Sprite m_spriteBackgroundDisabled;
619 Sprite m_spriteArrow;
620 Sprite m_spriteArrowHover;
621 Sprite m_spriteArrowDisabled;
622
623 // Cached renderer properties
624 Borders m_bordersCached;
625 Padding m_paddingCached;
626 Color m_borderColorCached;
627 Color m_backgroundColorCached;
628 Color m_backgroundColorDisabledCached;
629 Color m_arrowColorCached;
630 Color m_arrowColorHoverCached;
631 Color m_arrowColorDisabledCached;
632 Color m_arrowBackgroundColorCached;
633 Color m_arrowBackgroundColorHoverCached;
634 Color m_arrowBackgroundColorDisabledCached;
635 Color m_textColorCached;
636 Color m_textColorDisabledCached;
637
639 };
640
642}
643
645
646#endif // TGUI_COMBO_BOX_HPP
647
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Wrapper for colors.
Definition Color.hpp:74
Combo box widget.
Definition ComboBox.hpp:39
TGUI_NODISCARD std::size_t getMaximumItems() const
Returns the maximum items that the combo box can contain.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
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.
TGUI_NODISCARD std::size_t getItemsToDisplay() const
Returns the number of items that are displayed in the list.
void setSize(const Layout2d &size) override
Changes the size of the combo box.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
TGUI_NODISCARD std::size_t getItemCount() const
Returns the amount of items in the combo box.
TGUI_NODISCARD bool contains(const String &item) const
Returns whether the combo box contains the given item.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list.
TGUI_NODISCARD ComboBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
TGUI_NODISCARD std::vector< String > getItems() const
Returns a copy of the items in the combo box.
TGUI_NODISCARD ExpandDirection getExpandDirection() const
Returns the side where the list is displayed.
TGUI_NODISCARD 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.
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.
ExpandDirection
The side where the list will be displayed.
Definition ComboBox.hpp:44
void setDefaultText(const String &defaultText)
Changes the default text of the combo box. This is the text drawn when no item is selected.
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.
TGUI_NODISCARD bool containsId(const String &id) const
Returns whether the combo box contains an item with the given id.
TGUI_NODISCARD DataType getItemData(std::size_t index) const
Returns user data stored in the item.
Definition ComboBox.hpp:416
bool removeItem(const String &itemName)
Removes the item from the list with the given name.
std::shared_ptr< const ComboBox > ConstPtr
Shared constant widget pointer.
Definition ComboBox.hpp:53
static TGUI_NODISCARD ComboBox::Ptr create()
Creates a new combo box widget.
TGUI_NODISCARD bool getChangeItemOnScroll() const
Returns whether the mouse wheel can be used to change the selected item while the list is closed.
void removeAllItems()
Removes all items from the list.
void setExpandDirection(ExpandDirection direction)
Changes the side where the list is displayed.
TGUI_NODISCARD String getItemById(const String &id) const
Returns the item name of the item with the given id.
void deselectItem()
Deselects the selected item.
void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay)
Changes the number of items that are displayed in the list.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
TGUI_NODISCARD TGUI_NODISCARD ComboBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the combo box can contain.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
TGUI_NODISCARD String getSelectedItemId() const
Gets the id of the selected item.
TGUI_NODISCARD String getSelectedItem() const
Returns the currently selected item.
bool setSelectedItemById(const String &id)
Selects an item from the list.
static TGUI_NODISCARD ComboBox::Ptr copy(const ComboBox::ConstPtr &comboBox)
Makes a copy of another combo box.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
TGUI_NODISCARD int getSelectedItemIndex() const
Gets the index of the selected item.
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...
TGUI_NODISCARD const String & getDefaultText() const
Returns the default text of the combo box. This is the text drawn when no item is selected.
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:52
TGUI_NODISCARD std::vector< String > getItemIds() const
Returns a copy of the item ids in the combo box.
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.
Definition ComboBoxRenderer.hpp:37
Container widget.
Definition Container.hpp:49
Class to store the position or size of a widget.
Definition Layout.hpp:328
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition ListBox.hpp:44
Definition Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:559
Definition Sprite.hpp:48
Wrapper class to store strings.
Definition String.hpp:101
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:50
The parent class for every widget.
Definition Widget.hpp:84
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
States used for drawing.
Definition RenderStates.hpp:39