TGUI  0.10-beta
ComboBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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
35namespace 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 typedef std::shared_ptr<ComboBox> Ptr;
55 typedef std::shared_ptr<const ComboBox> ConstPtr;
56
57
65 ComboBox(const char* typeName = "ComboBox", bool initRenderer = true);
66
67
69 // Copy constructor
71 ComboBox(const ComboBox& other);
72
74 // Move constructor
76 ComboBox(ComboBox&& other);
77
79 // Copy assignment operator
81 ComboBox& operator= (const ComboBox& other);
82
84 // Move assignment operator
86 ComboBox& operator= (ComboBox&& other);
87
88
96
97
107
108
114 const ComboBoxRenderer* getSharedRenderer() const;
115
122 const ComboBoxRenderer* getRenderer() const;
123
124
133 void setSize(const Layout2d& size) override;
134 using Widget::setSize;
135
136
144 void setEnabled(bool enabled) override;
145
146
158 void setItemsToDisplay(std::size_t nrOfItemsInListToDisplay);
159
160
172 std::size_t getItemsToDisplay() const;
173
174
185 std::size_t addItem(const String& itemName, const String& id = "");
186
187
206 bool setSelectedItem(const String& itemName);
207
208
228
229
243 bool setSelectedItemByIndex(std::size_t index);
244
245
253
254
267 bool removeItem(const String& itemName);
268
269
282 bool removeItemById(const String& id);
283
284
298 bool removeItemByIndex(std::size_t index);
299
300
306
307
318 String getItemById(const String& id) const;
319
320
329
330
339
340
348
349
363 bool changeItem(const String& originalValue, const String& newValue);
364
365
379 bool changeItemById(const String& id, const String& newValue);
380
381
393 bool changeItemByIndex(std::size_t index, const String& newValue);
394
395
402 std::size_t getItemCount() const;
403
404
411 std::vector<String> getItems() const;
412
413
422 std::vector<String> getItemIds() const;
423
424
434 void setMaximumItems(std::size_t maximumItems = 0);
435
436
444 std::size_t getMaximumItems() const;
445
446
452 void setDefaultText(const String& defaultText);
453
454
460 const String& getDefaultText() const;
461
462
468
469
475
476
481 bool contains(const String& item) const;
482
483
488 bool containsId(const String& id) const;
489
490
496 void setChangeItemOnScroll(bool changeOnScroll);
497
498
505
506
512 void setParent(Container* parent) override;
513
514
521 bool isMouseOnWidget(Vector2f pos) const override;
522
526 void leftMousePressed(Vector2f pos) override;
527
531 bool mouseWheelScrolled(float delta, Vector2f pos) override;
532
533
541 void draw(BackendRenderTarget& target, RenderStates states) const override;
542
543
545 protected:
546
556 Signal& getSignal(String signalName) override;
557
558
565 void rendererChanged(const String& property) override;
566
567
571 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
572
573
577 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
578
579
583 void updateTextSize() override;
584
585
587 // Returns the size without the borders
589 Vector2f getInnerSize() const;
590
591
593 // Update the height of the internal list box
595 void updateListBoxHeight();
596
597
599 // Shows the list of items.
601 void showListBox();
602
603
605 // Hides the list of items.
607 void hideListBox();
608
609
611 // Initialize the internal list box
613 void initListBox();
614
615
617 // Makes a copy of the widget
619 Widget::Ptr clone() const override;
620
621
623 public:
624
625 SignalItem onItemSelect = {"ItemSelected"};
626
627
629 protected:
630
631 // The number of items to display. If there is a scrollbar then you can scroll to see the other.
632 // If there is no scrollbar then this will be the maximum amount of items.
633 std::size_t m_nrOfItemsToDisplay = 0;
634
635 // Internally a list box is used to store all items
636 ListBox::Ptr m_listBox = ListBox::create();
637
638 Text m_text;
639 Text m_defaultText;
640
641 int m_previousSelectedItemIndex = -1;
642 bool m_changeItemOnScroll = false;
643
644 ExpandDirection m_expandDirection = ExpandDirection::Automatic;
645
646 Sprite m_spriteBackground;
647 Sprite m_spriteBackgroundDisabled;
648 Sprite m_spriteArrow;
649 Sprite m_spriteArrowHover;
650 Sprite m_spriteArrowDisabled;
651
652 // Cached renderer properties
653 Borders m_bordersCached;
654 Padding m_paddingCached;
655 Color m_borderColorCached;
656 Color m_backgroundColorCached;
657 Color m_backgroundColorDisabledCached;
658 Color m_arrowColorCached;
659 Color m_arrowColorHoverCached;
660 Color m_arrowColorDisabledCached;
661 Color m_arrowBackgroundColorCached;
662 Color m_arrowBackgroundColorHoverCached;
663 Color m_arrowBackgroundColorDisabledCached;
664 Color m_textColorCached;
665 Color m_textColorDisabledCached;
666
668 };
669
671}
672
674
675#endif // TGUI_COMBO_BOX_HPP
676
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Wrapper for colors.
Definition: Color.hpp:63
Definition: ComboBoxRenderer.hpp:37
Combo box widget.
Definition: ComboBox.hpp:41
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.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
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.
ComboBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
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.
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.
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:54
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.
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.
ComboBoxRenderer * getRenderer()
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.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
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.
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.
std::shared_ptr< const ComboBox > ConstPtr
Shared constant widget pointer.
Definition: ComboBox.hpp:55
ExpandDirection getExpandDirection() const
Returns the side where the list is displayed.
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.
Container widget.
Definition: Container.hpp:47
Class to store the position or size of a widget.
Definition: Layout.hpp:284
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:46
static ListBox::Ptr create()
Creates a new list box widget.
Definition: Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:556
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:79
Definition: Text.hpp:48
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39