TGUI 1.13
Loading...
Searching...
No Matches
ListBox.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_LIST_BOX_HPP
26#define TGUI_LIST_BOX_HPP
27
28#include <TGUI/Renderers/ListBoxRenderer.hpp>
29#include <TGUI/Text.hpp>
30#include <TGUI/Widgets/Scrollbar.hpp>
31
33
34namespace tgui
35{
39 class TGUI_API ListBox : public Widget, public ScrollbarChildInterface
40 {
41 public:
42 using Ptr = std::shared_ptr<ListBox>;
43 using ConstPtr = std::shared_ptr<const ListBox>;
44
45 static constexpr char StaticWidgetType[] = "ListBox";
46
50 using TextAlignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = HorizontalAlignment;
51
59 explicit ListBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
60
66 [[nodiscard]] static ListBox::Ptr create();
67
75 [[nodiscard]] static ListBox::Ptr copy(const ListBox::ConstPtr& listBox);
76
81 [[nodiscard]] ListBoxRenderer* getSharedRenderer() override;
82 [[nodiscard]] const ListBoxRenderer* getSharedRenderer() const override;
83
89 [[nodiscard]] ListBoxRenderer* getRenderer() override;
90
96 void setSize(const Layout2d& size) override;
97 using Widget::setSize;
98
109 std::size_t addItem(const String& itemName, const String& id = "");
110
120 void addMultipleItems(const std::vector<String>& itemNames);
121
135 bool setSelectedItem(const String& itemName);
136
151
164 bool setSelectedItemByIndex(std::size_t index);
165
170
182 bool removeItem(const String& itemName);
183
195 bool removeItemById(const String& id);
196
209 bool removeItemByIndex(std::size_t index);
210
215
225 [[nodiscard]] String getItemById(const String& id) const;
226
234 [[nodiscard]] String getItemByIndex(std::size_t index) const;
235
245 [[nodiscard]] int getIndexById(const String& id) const;
246
254 [[nodiscard]] String getIdByIndex(std::size_t index) const;
255
262 [[nodiscard]] String getSelectedItem() const;
263
270 [[nodiscard]] String getSelectedItemId() const;
271
277 [[nodiscard]] int getSelectedItemIndex() const;
278
284 [[nodiscard]] int getHoveredItemIndex() const;
285
298 bool changeItem(const String& originalValue, const String& newValue);
299
312 bool changeItemById(const String& id, const String& newValue);
313
324 bool changeItemByIndex(std::size_t index, const String& newValue);
325
331 [[nodiscard]] std::size_t getItemCount() const;
332
338 [[nodiscard]] std::vector<String> getItems() const;
339
347 [[nodiscard]] std::vector<String> getItemIds() const;
348
361 void setItemData(std::size_t index, Any data);
362
369 template <typename DataType>
370 [[nodiscard]] DataType getItemData(std::size_t index) const
371 {
372 if (index < m_items.size())
373 return std::any_cast<DataType>(m_items[index].data);
374 throw std::bad_cast();
375 }
376
384 void setItemHeight(unsigned int itemHeight);
385
391 [[nodiscard]] unsigned int getItemHeight() const;
392
401 void setMaximumItems(std::size_t maximumItems = 0);
402
409 [[nodiscard]] std::size_t getMaximumItems() const;
410
418 void setAutoScroll(bool autoScroll);
419
424 [[nodiscard]] bool getAutoScroll() const;
425
434
441
449 [[nodiscard]] bool contains(const String& item) const;
450
458 [[nodiscard]] bool containsId(const String& id) const;
459
465 TGUI_DEPRECATED("Use getScrollbar()->setValue(value) instead") void setScrollbarValue(unsigned int value);
466
472 TGUI_DEPRECATED("Use getScrollbar()->getValue() instead") [[nodiscard]] unsigned int getScrollbarValue() const;
473
481 TGUI_DEPRECATED("Use getScrollbar()->getMaxValue() instead") [[nodiscard]] unsigned int getScrollbarMaxValue() const;
482
488 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
489
493 bool leftMousePressed(Vector2f pos) override;
494
498 void leftMouseReleased(Vector2f pos) override;
499
503 void rightMousePressed(Vector2f pos) override;
504
508 void mouseMoved(Vector2f pos) override;
509
513 bool scrolled(float delta, Vector2f pos, bool touch) override;
514
518 void mouseNoLongerOnWidget() override;
519
523 void leftMouseButtonNoLongerDown() override;
524
528 void keyPressed(const Event::KeyEvent& event) override;
529
539 bool canHandleKeyPress(const Event::KeyEvent& event) override;
540
547 void draw(BackendRenderTarget& target, RenderStates states) const override;
548
550
551 protected:
561 [[nodiscard]] Signal& getSignal(String signalName) override;
562
568 void rendererChanged(const String& property) override;
569
573 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
574
578 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
579
583 void updateTextSize() override;
584
588 void scrollbarValueChanged() override;
589
591 // Returns the size without the borders
593 [[nodiscard]] Vector2f getInnerSize() const;
594
596 // Updates the position of the items and the scrollbar
598 void updateItemPositions();
599
601 // Update the colors and text style of the selected and hovered items
603 void updateSelectedAndHoveringItemColorsAndStyle();
604
606 // Update the color and text style of all the items
608 void updateItemColorsAndStyle();
609
611 // Update on which item the mouse is standing
613 void updateHoveringItem(int item);
614
616 // Update which item is selected
618 void updateSelectedItem(int item);
619
621 // Update the hovered item based on a mouse event. The innerPos is the mouse pos relative to the area inside the borders.
623 void setHoveredItemBasedOnMousePos(Vector2f innerPos);
624
626 // Update the selected (and hovered) item based on a mouse event. The innerPos is relative to the area inside the borders.
628 void setSelectedItemBasedOnMousePos(Vector2f innerPos);
629
631 // Checks whether the scrollbar value was changed and emit the onScroll event if it did
633 void triggerOnScroll();
634
636 // Adds an item to the list. Called from both addItem and addMultipleItems
638 void addItemImpl(const String& itemName, const String& id);
639
641 // This function is called every frame with the time passed since the last frame.
643 bool updateTime(Duration elapsedTime) override;
644
646 // Makes a copy of the widget
648 [[nodiscard]] Widget::Ptr clone() const override;
649
651
652 public:
653 SignalItem onItemSelect = {"ItemSelected"};
654 SignalItem onMousePress = {"MousePressed"};
656 "MouseReleased"};
657 SignalItem onDoubleClick = {"DoubleClicked"};
659 "RightClicked"};
660 SignalUInt onScroll = {"Scrolled"};
661
663
664 protected:
665 struct Item
666 {
667 Text text;
668 Any data;
669 String id;
670 };
671
672 std::vector<Item> m_items;
673
674 // What is the index of the selected item?
675 // This is also used by combo box, so it can't just be changed to a pointer!
676 int m_selectedItem = -1;
677
678 int m_hoveringItem = -1;
679
680 unsigned int m_itemHeight = 0;
681
682 // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
683 std::size_t m_maxItems = 0;
684
685 // When there are too many items a scrollbar will be shown
686 unsigned int m_lastScrollbarValue = 0;
687
688 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
689 bool m_possibleDoubleClick = false;
690
691 bool m_autoScroll = true;
693
694 Sprite m_spriteBackground;
695
696 // Cached renderer properties
697 Borders m_bordersCached;
698 Borders m_paddingCached;
699 Color m_borderColorCached;
700 Color m_backgroundColorCached;
701 Color m_backgroundColorHoverCached;
702 Color m_selectedBackgroundColorCached;
703 Color m_selectedBackgroundColorHoverCached;
704 Color m_textColorCached;
705 Color m_textColorHoverCached;
706 Color m_selectedTextColorCached;
707 Color m_selectedTextColorHoverCached;
708 TextStyles m_textStyleCached;
709 TextStyles m_selectedTextStyleCached;
710 };
711} // namespace tgui
712
713#endif // TGUI_LIST_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:63
Wrapper for durations.
Definition Duration.hpp:52
Class to store the position or size of a widget.
Definition Layout.hpp:320
Definition ListBoxRenderer.hpp:35
unsigned int getScrollbarMaxValue() const
Returns the maximum thumb position of the scrollbar.
std::vector< String > getItemIds() const
Returns a copy of the item ids in the list box.
bool changeItem(const String &originalValue, const String &newValue)
Changes an item with name originalValue to newValue.
DataType getItemData(std::size_t index) const
Returns user data stored in the item.
Definition ListBox.hpp:370
void setItemHeight(unsigned int itemHeight)
Changes the height of the items in the list box.
std::size_t getItemCount() const
Returns the amount of items in the list box.
bool changeItemByIndex(std::size_t index, const String &newValue)
Changes the name of an item at the given index to newValue.
bool changeItemById(const String &id, const String &newValue)
Changes the name of an item with the given id to newValue.
void setTextAlignment(HorizontalAlignment alignment)
Changes the horizontal text alignment.
unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
unsigned int getItemHeight() const
Returns the height of the items in the list box.
std::vector< String > getItems() const
Returns a copy of the items in the list box.
HorizontalAlignment getTextAlignment() const
Gets the current horizontal text alignment.
static ListBox::Ptr copy(const ListBox::ConstPtr &listBox)
Makes a copy of another list box.
void addMultipleItems(const std::vector< String > &itemNames)
Adds multiple items to the list.
ListBoxRenderer * getRenderer() 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.
String getItemById(const String &id) const
Returns the item name of the item with the given id.
void scrollbarValueChanged() override
Called when the value of the scrollbar has been changed via getScrollbar()->setValue(....
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
void deselectItem()
Deselects the selected item.
SignalItem onItemSelect
An item was selected in the list box. Optional parameter: selected item or its index.
Definition ListBox.hpp:653
ListBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition ListBox.hpp:45
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition ListBox.hpp:43
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the list box can contain.
SignalItem onDoubleClick
An item was double clicked. Optional parameter: selected item or its index.
Definition ListBox.hpp:657
int getHoveredItemIndex() const
Gets the index of the item below the mouse cursor.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
SignalItem onMousePress
The mouse went down on an item. Optional parameter: selected item or its index.
Definition ListBox.hpp:654
String getSelectedItemId() const
Gets the id of the selected item.
static ListBox::Ptr create()
Creates a new list box widget.
bool contains(const String &item) const
Returns whether the list box contains the given item.
void removeAllItems()
Removes all items from the list.
SignalItem onMouseRelease
The mouse was released on one of the items. Optional parameter: selected item or its index.
Definition ListBox.hpp:655
bool removeItemByIndex(std::size_t index)
Removes the item from the list box.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
bool removeItemById(const String &id)
Removes the item that were added with the given id.
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...
String getIdByIndex(std::size_t index) const
Returns the id of the item at the given index.
bool setSelectedItemById(const String &id)
Selects an item in the list box.
bool setSelectedItem(const String &itemName)
Selects an item in the list box.
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool removeItem(const String &itemName)
Removes the item from the list with the given name.
void setSize(const Layout2d &size) override
Changes the size of the list box.
bool containsId(const String &id) const
Returns whether the list box contains an item with the given id.
String getItemByIndex(std::size_t index) const
Returns the item name of the item at the given index.
SignalUInt onScroll
The list was scrolled. Optional parameter: new value of scrollbar.
Definition ListBox.hpp:660
std::size_t addItem(const String &itemName, const String &id="")
Adds an item to the list.
bool getAutoScroll() const
Returns whether the list box scrolls to the bottom when a new item is added.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
int getSelectedItemIndex() const
Gets the index of the selected item.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
HorizontalAlignment TextAlignment
The horizontal text alignment.
Definition ListBox.hpp:50
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.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition ListBox.hpp:42
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list 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.
int getIndexById(const String &id) const
Returns the index of the item with the given id.
SignalItem onRightClick
Right mouse pressed on list box (also fires if not on top of item). Optional parameter: selected item...
Definition ListBox.hpp:658
String getSelectedItem() const
Returns the currently selected item.
ScrollbarChildInterface()
Default constructor.
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
Wrapper for text styles.
Definition TextStyle.hpp:55
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:53
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:60
@ Left
Align to the left side.
Definition Layout.hpp:61
SignalTyped< unsigned int > SignalUInt
Signal with one "unsigned int" as optional unbound parameter.
Definition Signal.hpp:409
Definition Event.hpp:36
Definition ListBox.hpp:666
States used for drawing.
Definition RenderStates.hpp:38