TGUI  0.10-beta
ListBox.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_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
37namespace tgui
38{
42 class TGUI_API ListBox : public Widget
43 {
44 public:
45
46 typedef std::shared_ptr<ListBox> Ptr;
47 typedef std::shared_ptr<const ListBox> ConstPtr;
48
49
53 enum class TextAlignment
54 {
55 Left,
56 Center,
57 Right
58 };
59
60
68 ListBox(const char* typeName = "ListBox", bool initRenderer = true);
69
70
78
79
89
90
96 const ListBoxRenderer* getSharedRenderer() const;
97
104 const ListBoxRenderer* getRenderer() const;
105
106
113 void setSize(const Layout2d& size) override;
114 using Widget::setSize;
115
116
127 std::size_t addItem(const String& itemName, const String& id = "");
128
129
144 bool setSelectedItem(const String& itemName);
145
146
162
163
177 bool setSelectedItemByIndex(std::size_t index);
178
179
185
186
199 bool removeItem(const String& itemName);
200
201
214 bool removeItemById(const String& id);
215
216
230 bool removeItemByIndex(std::size_t index);
231
232
238
239
250 String getItemById(const String& id) const;
251
252
260 String getItemByIndex(std::size_t index) const;
261
262
272 int getIndexById(const String& id) const;
273
274
282 String getIdByIndex(std::size_t index) const;
283
284
293
294
303
304
312
313
327 bool changeItem(const String& originalValue, const String& newValue);
328
329
343 bool changeItemById(const String& id, const String& newValue);
344
345
357 bool changeItemByIndex(std::size_t index, const String& newValue);
358
359
366 std::size_t getItemCount() const;
367
368
375 std::vector<String> getItems() const;
376
377
386 std::vector<String> getItemIds() const;
387
388
401 void setItemData(std::size_t index, Any data);
402
403
410 template <typename T>
411 T getItemData(std::size_t index) const
412 {
413 if (index < m_items.size())
414 return AnyCast<T>(m_items[index].data);
415 else
416 throw std::bad_cast();
417 }
418
427 void setItemHeight(unsigned int itemHeight);
428
429
436 unsigned int getItemHeight() const;
437
438
448 void setMaximumItems(std::size_t maximumItems = 0);
449
450
458 std::size_t getMaximumItems() const;
459
460
469 void setAutoScroll(bool autoScroll);
470
471
476 bool getAutoScroll() const;
477
478
487
488
495
496
504 bool contains(const String& item) const;
505
506
514 bool containsId(const String& id) const;
515
516
522 void setScrollbarValue(unsigned int value);
523
524
530 unsigned int getScrollbarValue() const;
531
532
539 bool isMouseOnWidget(Vector2f pos) const override;
540
544 void leftMousePressed(Vector2f pos) override;
545
549 void leftMouseReleased(Vector2f pos) override;
550
554 void mouseMoved(Vector2f pos) override;
555
559 bool mouseWheelScrolled(float delta, Vector2f pos) override;
560
564 void mouseNoLongerOnWidget() override;
565
569 void leftMouseButtonNoLongerDown() override;
570
574 void keyPressed(const Event::KeyEvent& event) override;
575
576
584 void draw(BackendRenderTarget& target, RenderStates states) const override;
585
586
588 protected:
589
599 Signal& getSignal(String signalName) override;
600
601
608 void rendererChanged(const String& property) override;
609
610
614 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
615
616
620 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
621
622
626 void updateTextSize() override;
627
628
630 // Returns the size without the borders
632 Vector2f getInnerSize() const;
633
634
636 // Updates the position of the items and the scrollbar
638 void updateItemPositions();
639
640
642 // Update the colors and text style of the selected and hovered items
644 void updateSelectedAndHoveringItemColorsAndStyle();
645
646
648 // Update the color and text style of all the items
650 void updateItemColorsAndStyle();
651
652
654 // Update on which item the mouse is standing
656 void updateHoveringItem(int item);
657
658
660 // Update which item is selected
662 void updateSelectedItem(int item);
663
664
666 // Checks whether the scrollbar value was changed and emit the onScroll event if it did
668 void triggerOnScroll();
669
670
672 // This function is called every frame with the time passed since the last frame.
674 bool updateTime(Duration elapsedTime) override;
675
676
678 // Makes a copy of the widget
680 Widget::Ptr clone() const override;
681
682
684 public:
685
686 SignalItem onItemSelect = {"ItemSelected"};
687 SignalItem onMousePress = {"MousePressed"};
688 SignalItem onMouseRelease = {"MouseReleased"};
689 SignalItem onDoubleClick = {"DoubleClicked"};
690 SignalUInt onScroll = {"Scrolled"};
691
692
694 protected:
695
696 struct Item
697 {
698 Text text;
699 Any data;
700 String id;
701 };
702
703 std::vector<Item> m_items;
704
705 // What is the index of the selected item?
706 // This is also used by combo box, so it can't just be changed to a pointer!
707 int m_selectedItem = -1;
708
709 int m_hoveringItem = -1;
710
711 unsigned int m_itemHeight = 0;
712
713 // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
714 std::size_t m_maxItems = 0;
715
716 // When there are too many items a scrollbar will be shown
718 unsigned int m_lastScrollbarValue = 0;
719
720 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
721 bool m_possibleDoubleClick = false;
722
723 bool m_autoScroll = true;
725
726 Sprite m_spriteBackground;
727
728 // Cached renderer properties
729 Borders m_bordersCached;
730 Borders m_paddingCached;
731 Color m_borderColorCached;
732 Color m_backgroundColorCached;
733 Color m_backgroundColorHoverCached;
734 Color m_selectedBackgroundColorCached;
735 Color m_selectedBackgroundColorHoverCached;
736 Color m_textColorCached;
737 Color m_textColorHoverCached;
738 Color m_selectedTextColorCached;
739 Color m_selectedTextColorHoverCached;
740 TextStyles m_textStyleCached;
741 TextStyles m_selectedTextStyleCached;
742
744 };
745
747}
748
750
751#endif // TGUI_LIST_BOX_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Wrapper for colors.
Definition: Color.hpp:63
Definition: CopiedSharedPtr.hpp:40
Wrapper for durations.
Definition: Duration.hpp:52
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Definition: ListBoxRenderer.hpp:37
List box widget.
Definition: ListBox.hpp:43
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.
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.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:46
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.
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.
TextAlignment
The horizontal text alignment.
Definition: ListBox.hpp:54
@ Left
Put the text on the left side (default)
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 updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
void deselectItem()
Deselects the selected item.
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition: ListBox.hpp:47
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the list box can contain.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
static ListBox::Ptr copy(ListBox::ConstPtr listBox)
Makes a copy of another list box.
void setTextAlignment(TextAlignment alignment)
Changes the horizontal text alignment.
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.
TextAlignment getTextAlignment() const
Gets the current horizontal text alignment.
bool removeItemByIndex(std::size_t index)
Removes the item from the list box.
ListBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
T getItemData(std::size_t index) const
Returns user data stored in the item.
Definition: ListBox.hpp:411
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.
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.
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.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list box.
ListBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
String getSelectedItem() const
Returns the currently selected item.
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
Wrapper for text styles.
Definition: TextStyle.hpp:58
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
KeyPressed event parameters.
Definition: Event.hpp:167
Definition: ListBox.hpp:697
States used for drawing.
Definition: RenderStates.hpp:39