TGUI  1.3-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
26#ifndef TGUI_COMBO_BOX_HPP
27#define TGUI_COMBO_BOX_HPP
28
29
30#include <TGUI/Widgets/ListBox.hpp>
31#include <TGUI/Renderers/ComboBoxRenderer.hpp>
32
34
35TGUI_MODULE_EXPORT namespace tgui
36{
40 class TGUI_API ComboBox : public Widget
41 {
42 public:
43
45 enum class ExpandDirection
46 {
47 Down,
48 Up,
49 Automatic
50 };
51
52 public:
53
54 using Ptr = std::shared_ptr<ComboBox>;
55 using ConstPtr = std::shared_ptr<const ComboBox>;
56
57 static constexpr const char StaticWidgetType[] = "ComboBox";
58
59
67 ComboBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
68
69
71 // Copy constructor
73 ComboBox(const ComboBox& other);
74
76 // Move constructor
78 ComboBox(ComboBox&& other) noexcept;
79
81 // Copy assignment operator
83 ComboBox& operator= (const ComboBox& other);
84
86 // Move assignment operator
88 ComboBox& operator= (ComboBox&& other) noexcept;
89
90
97 TGUI_NODISCARD static ComboBox::Ptr create();
98
99
108 TGUI_NODISCARD static ComboBox::Ptr copy(const ComboBox::ConstPtr& comboBox);
109
110
115 TGUI_NODISCARD ComboBoxRenderer* getSharedRenderer() override;
116 TGUI_NODISCARD const ComboBoxRenderer* getSharedRenderer() const override;
117
123 TGUI_NODISCARD TGUI_NODISCARD ComboBoxRenderer* getRenderer() override;
124
125
134 void setSize(const Layout2d& size) override;
135 using Widget::setSize;
136
137
145 void setEnabled(bool enabled) override;
146
147
159 void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
160
161
173 TGUI_NODISCARD std::size_t getItemsToDisplay() const;
174
175
186 std::size_t addItem(const String& itemName, const String& id = "");
187
188
207 bool setSelectedItem(const String& itemName);
208
209
229
230
244 bool setSelectedItemByIndex(std::size_t index);
245
246
254
255
268 bool removeItem(const String& itemName);
269
270
283 bool removeItemById(const String& id);
284
285
299 bool removeItemByIndex(std::size_t index);
300
301
307
308
319 TGUI_NODISCARD String getItemById(const String& id) const;
320
321
329 TGUI_NODISCARD String getSelectedItem() const;
330
331
339 TGUI_NODISCARD String getSelectedItemId() const;
340
341
348 TGUI_NODISCARD int getSelectedItemIndex() const;
349
350
364 bool changeItem(const String& originalValue, const String& newValue);
365
366
380 bool changeItemById(const String& id, const String& newValue);
381
382
394 bool changeItemByIndex(std::size_t index, const String& newValue);
395
396
403 TGUI_NODISCARD std::size_t getItemCount() const;
404
405
412 TGUI_NODISCARD std::vector<String> getItems() const;
413
414
423 TGUI_NODISCARD std::vector<String> getItemIds() const;
424
425
435 void setMaximumItems(std::size_t maximumItems = 0);
436
437
445 TGUI_NODISCARD std::size_t getMaximumItems() const;
446
447
453 void setDefaultText(const String& defaultText);
454
455
461 TGUI_NODISCARD const String& getDefaultText() const;
462
463
469
470
475 TGUI_NODISCARD ExpandDirection getExpandDirection() const;
476
477
482 TGUI_NODISCARD bool contains(const String& item) const;
483
484
489 TGUI_NODISCARD bool containsId(const String& id) const;
490
491
497 void setChangeItemOnScroll(bool changeOnScroll);
498
499
505 TGUI_NODISCARD bool getChangeItemOnScroll() const;
506
507
513 void setParent(Container* parent) override;
514
515
522 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
523
527 bool leftMousePressed(Vector2f pos) override;
528
532 bool scrolled(float delta, Vector2f pos, bool touch) override;
533
534
542 void draw(BackendRenderTarget& target, RenderStates states) const override;
543
544
546 protected:
547
557 TGUI_NODISCARD Signal& getSignal(String signalName) override;
558
559
566 void rendererChanged(const String& property) override;
567
568
572 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
573
574
578 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
579
580
584 void updateTextSize() override;
585
586
588 // Returns the size without the borders
590 TGUI_NODISCARD Vector2f getInnerSize() const;
591
592
594 // Update the height of the internal list box
596 void updateListBoxHeight();
597
598
600 // Shows the list of items.
602 void showListBox();
603
604
606 // Hides the list of items.
608 void hideListBox();
609
610
612 // Initialize the internal list box
614 void initListBox();
615
616
618 // Makes a copy of the widget
620 TGUI_NODISCARD Widget::Ptr clone() const override;
621
622
624 public:
625
626 SignalItem onItemSelect = {"ItemSelected"};
627
628
630 protected:
631
632 // The number of items to display. If there is a scrollbar then you can scroll to see the other.
633 // If there is no scrollbar then this will be the maximum amount of items.
634 std::size_t m_nrOfItemsToDisplay = 0;
635
636 // Internally a list box is used to store all items
637 ListBox::Ptr m_listBox = ListBox::create();
638
639 Text m_text;
640 Text m_defaultText;
641
642 int m_previousSelectedItemIndex = -1;
643 bool m_changeItemOnScroll = false;
644
645 ExpandDirection m_expandDirection = ExpandDirection::Automatic;
646
647 Sprite m_spriteBackground;
648 Sprite m_spriteBackgroundDisabled;
649 Sprite m_spriteArrow;
650 Sprite m_spriteArrowHover;
651 Sprite m_spriteArrowDisabled;
652
653 // Cached renderer properties
654 Borders m_bordersCached;
655 Padding m_paddingCached;
656 Color m_borderColorCached;
657 Color m_backgroundColorCached;
658 Color m_backgroundColorDisabledCached;
659 Color m_arrowColorCached;
660 Color m_arrowColorHoverCached;
661 Color m_arrowColorDisabledCached;
662 Color m_arrowBackgroundColorCached;
663 Color m_arrowBackgroundColorHoverCached;
664 Color m_arrowBackgroundColorDisabledCached;
665 Color m_textColorCached;
666 Color m_textColorDisabledCached;
667
669 };
670
672}
673
675
676#endif // TGUI_COMBO_BOX_HPP
677
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Wrapper for colors.
Definition Color.hpp:72
Definition ComboBoxRenderer.hpp:37
Combo box widget.
Definition ComboBox.hpp:41
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:46
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.
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:55
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.
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition ComboBox.hpp:54
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.
Container widget.
Definition Container.hpp:49
Class to store the position or size of a widget.
Definition Layout.hpp:305
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition ListBox.hpp:46
Definition Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:559
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
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