--- --- TGUI: include/TGUI/Widgets/PanelListBox.hpp Source File
TGUI  1.x-dev
Loading...
Searching...
No Matches
PanelListBox.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_PANEL_LIST_BOX_HPP
26#define TGUI_PANEL_LIST_BOX_HPP
27
28#include <TGUI/Widgets/ScrollablePanel.hpp>
29#include <TGUI/Renderers/PanelListBoxRenderer.hpp>
30
32
33TGUI_MODULE_EXPORT namespace tgui
34{
35
39 class TGUI_API PanelListBox : public ScrollablePanel
40 {
41 public:
42 using Ptr = std::shared_ptr<PanelListBox>;
43 using ConstPtr = std::shared_ptr<const PanelListBox>;
44
45 static constexpr const char StaticWidgetType[] = "PanelListBox";
46
54 PanelListBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
55
60 static Ptr create();
61
67 static Ptr copy(const ConstPtr& panelListBox);
68
73 TGUI_NODISCARD PanelListBoxRenderer* getSharedRenderer() override;
74 TGUI_NODISCARD const PanelListBoxRenderer* getSharedRenderer() const override;
75
81 TGUI_NODISCARD PanelListBoxRenderer* getRenderer() override;
82
87 void setSize(const Layout2d& size) override;
88 using ScrollablePanel::setSize;
89
96 Panel::Ptr addItem(const String& id = {}, int index = -1);
97
103 TGUI_NODISCARD Panel::Ptr getPanelTemplate() const;
104
110 TGUI_NODISCARD Layout getItemsWidth() const;
111
117 void setItemsHeight(const Layout& height);
118
124 TGUI_NODISCARD Layout getItemsHeight() const;
125
139 bool setSelectedItem(const Panel::Ptr& panelPtr);
140
155
168 bool setSelectedItemByIndex(std::size_t index);
169
174
186 bool removeItem(const Panel::Ptr& panelPtr);
187
199 bool removeItemById(const String& id);
200
213 bool removeItemByIndex(std::size_t index);
214
219
229 TGUI_NODISCARD Panel::Ptr getItemById(const String& id) const;
230
238 TGUI_NODISCARD Panel::Ptr getItemByIndex(std::size_t index) const;
239
249 int getIndexById(const String& id) const;
250
258 int getIndexByItem(const Panel::Ptr& panelPtr) const;
259
267 String getIdByIndex(std::size_t index) const;
268
275 TGUI_NODISCARD Panel::Ptr getSelectedItem() const;
276
283 TGUI_NODISCARD String getSelectedItemId() const;
284
290 TGUI_NODISCARD int getSelectedItemIndex() const;
291
297 TGUI_NODISCARD std::size_t getItemCount() const;
298
304 TGUI_NODISCARD std::vector<Panel::Ptr> getItems() const;
305
311 TGUI_NODISCARD std::vector<String> getItemIds() const;
312
319 void setMaximumItems(std::size_t maximumItems = 0);
320
327 TGUI_NODISCARD std::size_t getMaximumItems() const;
328
336 bool contains(const Panel::Ptr& panelPtr) const;
337
345 bool containsId(const String& id) const;
346
350 void mouseMoved(Vector2f pos) override;
351
355 void mouseNoLongerOnWidget() override;
356
360 bool leftMousePressed(Vector2f pos) override;
361
363 protected:
364
374 TGUI_NODISCARD Signal& getSignal(String signalName) override;
375
381 void rendererChanged(const String& property) override;
382
386 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
387
391 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
392
394 // Updates the position of the items
396 void updateItemsPositions() const;
397
399 // Updates the size of the items
401 void updateItemsSize() const;
402
404 // Returns all items height
406 TGUI_NODISCARD float getAllItemsHeight() const;
407
409 // Returns height of the items
410 //
413 TGUI_NODISCARD float getItemsHeightUpToIndex(std::size_t index) const;
414
416 // Update which item is selected
418 void updateSelectedItem(int item);
419
421 // Update on which item the mouse is standing
423 void updateHoveringItem(int item);
424
426 // Update the colors and style of the selected and hovered items
428 void updateSelectedAndHoveringItemColorsAndStyle() const;
429
431 // Clear item style
432 //
435 void clearItemStyle(int item) const;
436
438 // Clear all items style
440 void clearAllItemsStyle() const;
441
443 // Makes a copy of the widget
445 TGUI_NODISCARD Widget::Ptr clone() const override;
446
448 public:
449
450 SignalPanelListBoxItem onItemSelect = {"ItemSelected"};
451
453 protected:
454 struct Item
455 {
456 Panel::Ptr panel;
457 String id;
458 };
459
460 std::vector<Item> m_items;
461 std::size_t m_maxItems;
462 Panel::Ptr m_panelTemplate;
463
464 int m_selectedItem;
465 int m_hoveringItem;
466
467 // Cached renderer properties
468 Color m_itemsBackgroundColorCached;
469 Color m_itemsBackgroundColorHoverCached;
470 Color m_selectedItemsBackgroundColorCached;
471 Color m_selectedItemsBackgroundColorHoverCached;
472
474 };
475
477}
478
480
481#endif // TGUI_PANEL_LIST_BOX_HPP
Wrapper for colors.
Definition Color.hpp:73
Class to store the position or size of a widget.
Definition Layout.hpp:313
Class to store the left, top, width or height of a widget.
Definition Layout.hpp:101
std::shared_ptr< Panel > Ptr
Shared widget pointer.
Definition Panel.hpp:42
List of panels, which can be flexible changed.
Definition PanelListBox.hpp:40
TGUI_NODISCARD PanelListBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD std::size_t getItemCount() const
Returns the amount of items in the panel list box.
TGUI_NODISCARD std::vector< Panel::Ptr > getItems() const
Returns a copy of the items in the list box.
bool removeItemById(const String &id)
Removes the item that were added with the given id.
bool setSelectedItemById(const String &id)
Selects an item in the panel list box.
TGUI_NODISCARD int getSelectedItemIndex() const
Gets the index of the selected item.
TGUI_NODISCARD Panel::Ptr getSelectedItem() const
Returns the currently selected item.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
std::shared_ptr< PanelListBox > Ptr
Shared widget pointer.
Definition PanelListBox.hpp:42
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
bool setSelectedItem(const Panel::Ptr &panelPtr)
Selects an item in the panel list box.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
TGUI_NODISCARD PanelListBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
String getIdByIndex(std::size_t index) const
Returns the id of the item at the given index.
TGUI_NODISCARD Panel::Ptr getPanelTemplate() const
Get panel template from which new elements are created.
Panel::Ptr addItem(const String &id={}, int index=-1)
Adds an item to the list.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
TGUI_NODISCARD String getSelectedItemId() const
Gets the id of the selected item.
bool containsId(const String &id) const
Returns whether the panel list box contains an item with the given id.
static Ptr copy(const ConstPtr &panelListBox)
Makes a copy of another panel list box.
TGUI_NODISCARD Panel::Ptr getItemByIndex(std::size_t index) const
Returns the item name 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.
void setItemsHeight(const Layout &height)
Changes the height of the items in the list box.
TGUI_NODISCARD std::size_t getMaximumItems() const
Returns the maximum items that the list box can contain.
void clearItemStyle(int item) const
TGUI_NODISCARD float getItemsHeightUpToIndex(std::size_t index) const
TGUI_NODISCARD std::vector< String > getItemIds() const
Returns a vector of stored items ids.
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the list box can contain.
std::shared_ptr< const PanelListBox > ConstPtr
Shared constant widget pointer.
Definition PanelListBox.hpp:43
TGUI_NODISCARD Layout getItemsWidth() const
Returns the width of the items in the list box.
static Ptr create()
Creates a new panel list box widget.
void deselectItem()
Deselects the selected item.
TGUI_NODISCARD Layout getItemsHeight() const
Returns the height of the items in the list box.
TGUI_NODISCARD Panel::Ptr getItemById(const String &id) const
Returns the item name of the item with the given id.
bool removeItem(const Panel::Ptr &panelPtr)
Removes the item from the panel list with the given pointer.
bool removeItemByIndex(std::size_t index)
Removes the item from the panel list box.
bool contains(const Panel::Ptr &panelPtr) const
Returns whether the panel list box contains the given item.
int getIndexByItem(const Panel::Ptr &panelPtr) const
Returns the index of the given item.
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.
void setSize(const Layout2d &size) override
Changes the size of the panel list box.
void removeAllItems()
Removes all items from the list.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the panel list box.
int getIndexById(const String &id) const
Returns the index of the item with the given id.
Definition PanelListBoxRenderer.hpp:36
Group of widgets that has a background color and optional borders.
Definition ScrollablePanel.hpp:45
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:630
Wrapper class to store strings.
Definition String.hpp:96
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Definition PanelListBox.hpp:455