TGUI  1.3-dev
Loading...
Searching...
No Matches
ListBox.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_LIST_BOX_HPP
27#define TGUI_LIST_BOX_HPP
28
29
30#include <TGUI/CopiedSharedPtr.hpp>
31#include <TGUI/Widgets/Scrollbar.hpp>
32#include <TGUI/Renderers/ListBoxRenderer.hpp>
33#include <TGUI/Text.hpp>
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
42 class TGUI_API ListBox : public Widget
43 {
44 public:
45
46 using Ptr = std::shared_ptr<ListBox>;
47 using ConstPtr = std::shared_ptr<const ListBox>;
48
49 static constexpr const char StaticWidgetType[] = "ListBox";
50
51
55 enum class TextAlignment
56 {
57 Left,
58 Center,
59 Right
60 };
61
62
70 ListBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
71
72
79 TGUI_NODISCARD static ListBox::Ptr create();
80
81
90 TGUI_NODISCARD static ListBox::Ptr copy(const ListBox::ConstPtr& listBox);
91
92
97 TGUI_NODISCARD ListBoxRenderer* getSharedRenderer() override;
98 TGUI_NODISCARD const ListBoxRenderer* getSharedRenderer() const override;
99
105 TGUI_NODISCARD ListBoxRenderer* getRenderer() override;
106
107
114 void setSize(const Layout2d& size) override;
115 using Widget::setSize;
116
117
128 std::size_t addItem(const String& itemName, const String& id = "");
129
130
145 bool setSelectedItem(const String& itemName);
146
147
163
164
178 bool setSelectedItemByIndex(std::size_t index);
179
180
186
187
200 bool removeItem(const String& itemName);
201
202
215 bool removeItemById(const String& id);
216
217
231 bool removeItemByIndex(std::size_t index);
232
233
239
240
251 TGUI_NODISCARD String getItemById(const String& id) const;
252
253
261 TGUI_NODISCARD String getItemByIndex(std::size_t index) const;
262
263
273 TGUI_NODISCARD int getIndexById(const String& id) const;
274
275
283 TGUI_NODISCARD String getIdByIndex(std::size_t index) const;
284
285
293 TGUI_NODISCARD String getSelectedItem() const;
294
295
303 TGUI_NODISCARD String getSelectedItemId() const;
304
305
312 TGUI_NODISCARD int getSelectedItemIndex() const;
313
314
328 bool changeItem(const String& originalValue, const String& newValue);
329
330
344 bool changeItemById(const String& id, const String& newValue);
345
346
358 bool changeItemByIndex(std::size_t index, const String& newValue);
359
360
367 TGUI_NODISCARD std::size_t getItemCount() const;
368
369
376 TGUI_NODISCARD std::vector<String> getItems() const;
377
378
387 TGUI_NODISCARD std::vector<String> getItemIds() const;
388
389
402 void setItemData(std::size_t index, Any data);
403
404
411 template <typename DataType>
412 TGUI_NODISCARD DataType getItemData(std::size_t index) const
413 {
414 if (index < m_items.size())
415 return AnyCast<DataType>(m_items[index].data);
416 else
417 throw std::bad_cast();
418 }
419
428 void setItemHeight(unsigned int itemHeight);
429
430
437 TGUI_NODISCARD unsigned int getItemHeight() const;
438
439
449 void setMaximumItems(std::size_t maximumItems = 0);
450
451
459 TGUI_NODISCARD std::size_t getMaximumItems() const;
460
461
470 void setAutoScroll(bool autoScroll);
471
472
477 TGUI_NODISCARD bool getAutoScroll() const;
478
479
488
489
495 TGUI_NODISCARD TextAlignment getTextAlignment() const;
496
497
505 TGUI_NODISCARD bool contains(const String& item) const;
506
507
515 TGUI_NODISCARD bool containsId(const String& id) const;
516
517
523 void setScrollbarValue(unsigned int value);
524
525
531 TGUI_NODISCARD unsigned int getScrollbarValue() const;
532
533
540 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
541
545 bool leftMousePressed(Vector2f pos) override;
546
550 void leftMouseReleased(Vector2f pos) override;
551
555 void mouseMoved(Vector2f pos) override;
556
560 bool scrolled(float delta, Vector2f pos, bool touch) override;
561
565 void mouseNoLongerOnWidget() override;
566
570 void leftMouseButtonNoLongerDown() override;
571
575 void keyPressed(const Event::KeyEvent& event) override;
576
586 bool canHandleKeyPress(const Event::KeyEvent& event) override;
587
588
596 void draw(BackendRenderTarget& target, RenderStates states) const override;
597
598
600 protected:
601
611 TGUI_NODISCARD Signal& getSignal(String signalName) override;
612
613
620 void rendererChanged(const String& property) override;
621
622
626 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
627
628
632 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
633
634
638 void updateTextSize() override;
639
640
642 // Returns the size without the borders
644 TGUI_NODISCARD Vector2f getInnerSize() const;
645
646
648 // Updates the position of the items and the scrollbar
650 void updateItemPositions();
651
652
654 // Update the colors and text style of the selected and hovered items
656 void updateSelectedAndHoveringItemColorsAndStyle();
657
658
660 // Update the color and text style of all the items
662 void updateItemColorsAndStyle();
663
664
666 // Update on which item the mouse is standing
668 void updateHoveringItem(int item);
669
670
672 // Update which item is selected
674 void updateSelectedItem(int item);
675
676
678 // Checks whether the scrollbar value was changed and emit the onScroll event if it did
680 void triggerOnScroll();
681
682
684 // This function is called every frame with the time passed since the last frame.
686 bool updateTime(Duration elapsedTime) override;
687
688
690 // Makes a copy of the widget
692 TGUI_NODISCARD Widget::Ptr clone() const override;
693
694
696 public:
697
698 SignalItem onItemSelect = {"ItemSelected"};
699 SignalItem onMousePress = {"MousePressed"};
700 SignalItem onMouseRelease = {"MouseReleased"};
701 SignalItem onDoubleClick = {"DoubleClicked"};
702 SignalUInt onScroll = {"Scrolled"};
703
704
706 protected:
707
708 struct Item
709 {
710 Text text;
711 Any data;
712 String id;
713 };
714
715 std::vector<Item> m_items;
716
717 // What is the index of the selected item?
718 // This is also used by combo box, so it can't just be changed to a pointer!
719 int m_selectedItem = -1;
720
721 int m_hoveringItem = -1;
722
723 unsigned int m_itemHeight = 0;
724
725 // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
726 std::size_t m_maxItems = 0;
727
728 // When there are too many items a scrollbar will be shown
730 unsigned int m_lastScrollbarValue = 0;
731
732 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
733 bool m_possibleDoubleClick = false;
734
735 bool m_autoScroll = true;
736 ListBox::TextAlignment m_textAlignment = ListBox::TextAlignment::Left;
737
738 Sprite m_spriteBackground;
739
740 // Cached renderer properties
741 Borders m_bordersCached;
742 Borders m_paddingCached;
743 Color m_borderColorCached;
744 Color m_backgroundColorCached;
745 Color m_backgroundColorHoverCached;
746 Color m_selectedBackgroundColorCached;
747 Color m_selectedBackgroundColorHoverCached;
748 Color m_textColorCached;
749 Color m_textColorHoverCached;
750 Color m_selectedTextColorCached;
751 Color m_selectedTextColorHoverCached;
752 TextStyles m_textStyleCached;
753 TextStyles m_selectedTextStyleCached;
754
756 };
757
759}
760
762
763#endif // TGUI_LIST_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Wrapper for colors.
Definition Color.hpp:72
Definition CopiedSharedPtr.hpp:45
Wrapper for durations.
Definition Duration.hpp:56
Class to store the position or size of a widget.
Definition Layout.hpp:305
Definition ListBoxRenderer.hpp:37
List box widget.
Definition ListBox.hpp:43
TGUI_NODISCARD String getItemById(const String &id) const
Returns the item name of the item with the given id.
bool changeItem(const String &originalValue, const String &newValue)
Changes an item with name originalValue to newValue.
void setItemHeight(unsigned int itemHeight)
Changes the height of the 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.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
bool changeItemById(const String &id, const String &newValue)
Changes the name of an item with the given id to newValue.
TGUI_NODISCARD String getItemByIndex(std::size_t index) const
Returns the item name of the item at the given index.
TextAlignment
The horizontal text alignment.
Definition ListBox.hpp:56
void setItemData(std::size_t index, Any data)
Store some user data with the item.
TGUI_NODISCARD std::vector< String > getItems() const
Returns a copy of the items in the list box.
TGUI_NODISCARD bool contains(const String &item) const
Returns whether the list box contains the given item.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
void deselectItem()
Deselects the selected item.
TGUI_NODISCARD std::vector< String > getItemIds() const
Returns a copy of the item ids in the list box.
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition ListBox.hpp:47
TGUI_NODISCARD bool getAutoScroll() const
Returns whether the list box scrolls to the bottom when a new item is added.
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the list box can contain.
TGUI_NODISCARD ListBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD bool containsId(const String &id) const
Returns whether the list box contains an item with the given id.
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.
TGUI_NODISCARD String getSelectedItem() const
Returns the currently selected item.
void setTextAlignment(TextAlignment alignment)
Changes the horizontal text alignment.
TGUI_NODISCARD TextAlignment getTextAlignment() const
Gets the current horizontal text alignment.
void removeAllItems()
Removes all items from the list.
bool removeItemByIndex(std::size_t index)
Removes the item from the list box.
TGUI_NODISCARD String getIdByIndex(std::size_t index) const
Returns the id of the item at the given index.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
static TGUI_NODISCARD ListBox::Ptr create()
Creates a new list box widget.
TGUI_NODISCARD unsigned int getItemHeight() const
Returns the height of the items in the list box.
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...
TGUI_NODISCARD int getIndexById(const String &id) const
Returns the index of the item with the given id.
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.
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.
static TGUI_NODISCARD ListBox::Ptr copy(const ListBox::ConstPtr &listBox)
Makes a copy of another list box.
TGUI_NODISCARD std::size_t getMaximumItems() const
Returns the maximum items that the list box can contain.
TGUI_NODISCARD std::size_t getItemCount() const
Returns the amount of items in the list box.
TGUI_NODISCARD ListBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD String getSelectedItemId() const
Gets the id of the selected item.
std::size_t addItem(const String &itemName, const String &id="")
Adds an item to the list.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void setAutoScroll(bool autoScroll)
Changes whether the list box scrolls to the bottom when a new item is added.
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 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.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition ListBox.hpp:46
TGUI_NODISCARD unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list box.
TGUI_NODISCARD int getSelectedItemIndex() const
Gets the index of the selected item.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
TGUI_NODISCARD DataType getItemData(std::size_t index) const
Returns user data stored in the item.
Definition ListBox.hpp:412
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
Wrapper for text styles.
Definition TextStyle.hpp:57
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
KeyPressed event parameters.
Definition Event.hpp:169
Definition ListBox.hpp:709
States used for drawing.
Definition RenderStates.hpp:39