TGUI  1.5
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#ifndef TGUI_LIST_BOX_HPP
26#define TGUI_LIST_BOX_HPP
27
28#include <TGUI/Widgets/Scrollbar.hpp>
29#include <TGUI/Renderers/ListBoxRenderer.hpp>
30#include <TGUI/Text.hpp>
31
33
34TGUI_MODULE_EXPORT namespace tgui
35{
39 class TGUI_API ListBox : public Widget, public ScrollbarChildInterface
40 {
41 public:
42
43 using Ptr = std::shared_ptr<ListBox>;
44 using ConstPtr = std::shared_ptr<const ListBox>;
45
46 static constexpr const char StaticWidgetType[] = "ListBox";
47
51 using TextAlignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = HorizontalAlignment;
52
60 ListBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
61
67 TGUI_NODISCARD static ListBox::Ptr create();
68
76 TGUI_NODISCARD static ListBox::Ptr copy(const ListBox::ConstPtr& listBox);
77
82 TGUI_NODISCARD ListBoxRenderer* getSharedRenderer() override;
83 TGUI_NODISCARD const ListBoxRenderer* getSharedRenderer() const override;
84
90 TGUI_NODISCARD ListBoxRenderer* getRenderer() override;
91
97 void setSize(const Layout2d& size) override;
98 using Widget::setSize;
99
110 std::size_t addItem(const String& itemName, const String& id = "");
111
125 bool setSelectedItem(const String& itemName);
126
141
154 bool setSelectedItemByIndex(std::size_t index);
155
160
172 bool removeItem(const String& itemName);
173
185 bool removeItemById(const String& id);
186
199 bool removeItemByIndex(std::size_t index);
200
205
215 TGUI_NODISCARD String getItemById(const String& id) const;
216
224 TGUI_NODISCARD String getItemByIndex(std::size_t index) const;
225
235 TGUI_NODISCARD int getIndexById(const String& id) const;
236
244 TGUI_NODISCARD String getIdByIndex(std::size_t index) const;
245
252 TGUI_NODISCARD String getSelectedItem() const;
253
260 TGUI_NODISCARD String getSelectedItemId() const;
261
267 TGUI_NODISCARD int getSelectedItemIndex() const;
268
274 TGUI_NODISCARD int getHoveredItemIndex() const;
275
288 bool changeItem(const String& originalValue, const String& newValue);
289
302 bool changeItemById(const String& id, const String& newValue);
303
314 bool changeItemByIndex(std::size_t index, const String& newValue);
315
321 TGUI_NODISCARD std::size_t getItemCount() const;
322
328 TGUI_NODISCARD std::vector<String> getItems() const;
329
337 TGUI_NODISCARD std::vector<String> getItemIds() const;
338
351 void setItemData(std::size_t index, Any data);
352
359 template <typename DataType>
360 TGUI_NODISCARD DataType getItemData(std::size_t index) const
361 {
362 if (index < m_items.size())
363 return AnyCast<DataType>(m_items[index].data);
364 else
365 throw std::bad_cast();
366 }
367
375 void setItemHeight(unsigned int itemHeight);
376
382 TGUI_NODISCARD unsigned int getItemHeight() const;
383
392 void setMaximumItems(std::size_t maximumItems = 0);
393
400 TGUI_NODISCARD std::size_t getMaximumItems() const;
401
409 void setAutoScroll(bool autoScroll);
410
415 TGUI_NODISCARD bool getAutoScroll() const;
416
425
431 TGUI_NODISCARD HorizontalAlignment getTextAlignment() const;
432
440 TGUI_NODISCARD bool contains(const String& item) const;
441
449 TGUI_NODISCARD bool containsId(const String& id) const;
450
456 TGUI_DEPRECATED("Use getScrollbar()->setValue(value) instead") void setScrollbarValue(unsigned int value);
457
463 TGUI_DEPRECATED("Use getScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getScrollbarValue() const;
464
472 TGUI_DEPRECATED("Use getScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getScrollbarMaxValue() const;
473
479 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
480
484 bool leftMousePressed(Vector2f pos) override;
485
489 void leftMouseReleased(Vector2f pos) override;
490
494 void mouseMoved(Vector2f pos) override;
495
499 bool scrolled(float delta, Vector2f pos, bool touch) override;
500
504 void mouseNoLongerOnWidget() override;
505
509 void leftMouseButtonNoLongerDown() override;
510
514 void keyPressed(const Event::KeyEvent& event) override;
515
525 bool canHandleKeyPress(const Event::KeyEvent& event) override;
526
533 void draw(BackendRenderTarget& target, RenderStates states) const override;
534
536 protected:
537
547 TGUI_NODISCARD Signal& getSignal(String signalName) override;
548
554 void rendererChanged(const String& property) override;
555
559 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
560
564 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
565
569 void updateTextSize() override;
570
574 void scrollbarValueChanged() override;
575
577 // Returns the size without the borders
579 TGUI_NODISCARD Vector2f getInnerSize() const;
580
582 // Updates the position of the items and the scrollbar
584 void updateItemPositions();
585
587 // Update the colors and text style of the selected and hovered items
589 void updateSelectedAndHoveringItemColorsAndStyle();
590
592 // Update the color and text style of all the items
594 void updateItemColorsAndStyle();
595
597 // Update on which item the mouse is standing
599 void updateHoveringItem(int item);
600
602 // Update which item is selected
604 void updateSelectedItem(int item);
605
607 // Checks whether the scrollbar value was changed and emit the onScroll event if it did
609 void triggerOnScroll();
610
612 // This function is called every frame with the time passed since the last frame.
614 bool updateTime(Duration elapsedTime) override;
615
617 // Makes a copy of the widget
619 TGUI_NODISCARD Widget::Ptr clone() const override;
620
622 public:
623
624 SignalItem onItemSelect = {"ItemSelected"};
625 SignalItem onMousePress = {"MousePressed"};
626 SignalItem onMouseRelease = {"MouseReleased"};
627 SignalItem onDoubleClick = {"DoubleClicked"};
628 SignalUInt onScroll = {"Scrolled"};
629
631 protected:
632
633 struct Item
634 {
635 Text text;
636 Any data;
637 String id;
638 };
639
640 std::vector<Item> m_items;
641
642 // What is the index of the selected item?
643 // This is also used by combo box, so it can't just be changed to a pointer!
644 int m_selectedItem = -1;
645
646 int m_hoveringItem = -1;
647
648 unsigned int m_itemHeight = 0;
649
650 // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
651 std::size_t m_maxItems = 0;
652
653 // When there are too many items a scrollbar will be shown
654 unsigned int m_lastScrollbarValue = 0;
655
656 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
657 bool m_possibleDoubleClick = false;
658
659 bool m_autoScroll = true;
660 HorizontalAlignment m_textAlignment = HorizontalAlignment::Left;
661
662 Sprite m_spriteBackground;
663
664 // Cached renderer properties
665 Borders m_bordersCached;
666 Borders m_paddingCached;
667 Color m_borderColorCached;
668 Color m_backgroundColorCached;
669 Color m_backgroundColorHoverCached;
670 Color m_selectedBackgroundColorCached;
671 Color m_selectedBackgroundColorHoverCached;
672 Color m_textColorCached;
673 Color m_textColorHoverCached;
674 Color m_selectedTextColorCached;
675 Color m_selectedTextColorHoverCached;
676 TextStyles m_textStyleCached;
677 TextStyles m_selectedTextStyleCached;
678
680 };
681
683}
684
686
687#endif // TGUI_LIST_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:73
Wrapper for durations.
Definition Duration.hpp:55
Class to store the position or size of a widget.
Definition Layout.hpp:313
Definition ListBoxRenderer.hpp:35
List box widget.
Definition ListBox.hpp:40
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.
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.
TGUI_NODISCARD String getItemByIndex(std::size_t index) const
Returns the item name of the item at the given index.
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 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:44
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.
TGUI_NODISCARD String getSelectedItem() const
Returns the currently selected item.
void removeAllItems()
Removes all items from the list.
TGUI_NODISCARD HorizontalAlignment getTextAlignment() const
Gets the current horizontal text alignment.
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.
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.
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.
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 setAutoScroll(bool autoScroll)
Changes whether the list box scrolls to the bottom when a new item is added.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition ListBox.hpp:43
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list box.
TGUI_NODISCARD int getHoveredItemIndex() const
Gets the index of the item below the mouse cursor.
TGUI_NODISCARD int getSelectedItemIndex() const
Gets the index of the selected item.
TGUI_NODISCARD DataType getItemData(std::size_t index) const
Returns user data stored in the item.
Definition ListBox.hpp:360
Definition Outline.hpp:38
Base class for widgets with a single scrollbar.
Definition Scrollbar.hpp:585
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:522
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Wrapper for text styles.
Definition TextStyle.hpp:55
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:48
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:61
Definition Event.hpp:38
Definition ListBox.hpp:634
States used for drawing.
Definition RenderStates.hpp:38