TGUI  0.9.1
ListBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
37 namespace 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 
57  ListBox(const char* typeName = "ListBox", bool initRenderer = true);
58 
59 
66  static ListBox::Ptr create();
67 
68 
78 
79 
85  const ListBoxRenderer* getSharedRenderer() const;
86 
93  const ListBoxRenderer* getRenderer() const;
94 
95 
102  void setPosition(const Layout2d& position) override;
103  using Widget::setPosition;
104 
105 
112  void setSize(const Layout2d& size) override;
113  using Widget::setSize;
114 
115 
126  std::size_t addItem(const String& itemName, const String& id = "");
127 
128 
143  bool setSelectedItem(const String& itemName);
144 
145 
160  bool setSelectedItemById(const String& id);
161 
162 
176  bool setSelectedItemByIndex(std::size_t index);
177 
178 
183  void deselectItem();
184 
185 
198  bool removeItem(const String& itemName);
199 
200 
213  bool removeItemById(const String& id);
214 
215 
229  bool removeItemByIndex(std::size_t index);
230 
231 
237 
238 
249  String getItemById(const String& id) const;
250 
251 
259  String getItemByIndex(std::size_t index) const;
260 
261 
271  int getIndexById(const String& id) const;
272 
273 
281  String getIdByIndex(std::size_t index) const;
282 
283 
292 
293 
302 
303 
310  int getSelectedItemIndex() const;
311 
312 
326  bool changeItem(const String& originalValue, const String& newValue);
327 
328 
342  bool changeItemById(const String& id, const String& newValue);
343 
344 
356  bool changeItemByIndex(std::size_t index, const String& newValue);
357 
358 
365  std::size_t getItemCount() const;
366 
367 
374  std::vector<String> getItems() const;
375 
376 
385  std::vector<String> getItemIds() const;
386 
387 
400  void setItemData(std::size_t index, Any data);
401 
402 
409  template <typename T>
410  T getItemData(std::size_t index) const
411  {
412  if (index < m_items.size())
413  return AnyCast<T>(m_items[index].data);
414  else
415  throw std::bad_cast();
416  }
417 
426  void setItemHeight(unsigned int itemHeight);
427 
428 
435  unsigned int getItemHeight() const;
436 
437 
449  void setTextSize(unsigned int textSize) override;
450 
451 
461  void setMaximumItems(std::size_t maximumItems = 0);
462 
463 
471  std::size_t getMaximumItems() const;
472 
473 
482  void setAutoScroll(bool autoScroll);
483 
484 
489  bool getAutoScroll() const;
490 
491 
499  bool contains(const String& item) const;
500 
501 
509  bool containsId(const String& id) const;
510 
511 
517  void setScrollbarValue(unsigned int value);
518 
519 
525  unsigned int getScrollbarValue() const;
526 
527 
534  bool isMouseOnWidget(Vector2f pos) const override;
535 
539  void leftMousePressed(Vector2f pos) override;
540 
544  void leftMouseReleased(Vector2f pos) override;
545 
549  void mouseMoved(Vector2f pos) override;
550 
554  bool mouseWheelScrolled(float delta, Vector2f pos) override;
555 
559  void mouseNoLongerOnWidget() override;
560 
564  void leftMouseButtonNoLongerDown() override;
565 
569  void keyPressed(const Event::KeyEvent& event) override;
570 
571 
579  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
580 
581 
583  protected:
584 
594  Signal& getSignal(String signalName) override;
595 
596 
603  void rendererChanged(const String& property) override;
604 
605 
609  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
610 
611 
615  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
616 
617 
619  // Returns the size without the borders
621  Vector2f getInnerSize() const;
622 
623 
625  // Update the colors and text style of the selected and hovered items
627  void updateSelectedAndHoveringItemColorsAndStyle();
628 
629 
631  // Update the color and text style of all the items
633  void updateItemColorsAndStyle();
634 
635 
637  // Update on which item the mouse is standing
639  void updateHoveringItem(int item);
640 
641 
643  // Update which item is selected
645  void updateSelectedItem(int item);
646 
647 
649  // This function is called every frame with the time passed since the last frame.
651  bool updateTime(Duration elapsedTime) override;
652 
653 
655  // Makes a copy of the widget
657  Widget::Ptr clone() const override
658  {
659  return std::make_shared<ListBox>(*this);
660  }
661 
662 
664  public:
665 
666  SignalItem onItemSelect = {"ItemSelected"};
667  SignalItem onMousePress = {"MousePressed"};
668  SignalItem onMouseRelease = {"MouseReleased"};
669  SignalItem onDoubleClick = {"DoubleClicked"};
670 
671 
673  protected:
674 
675  struct Item
676  {
677  Text text;
678  Any data;
679  String id;
680  };
681 
682  std::vector<Item> m_items;
683 
684  // What is the index of the selected item?
685  // This is also used by combo box, so it can't just be changed to a pointer!
686  int m_selectedItem = -1;
687 
688  int m_hoveringItem = -1;
689 
690  // The size must be stored
691  unsigned int m_itemHeight = 0;
692  unsigned int m_requestedTextSize = 0;
693 
694  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
695  std::size_t m_maxItems = 0;
696 
697  // When there are too many items a scrollbar will be shown
699 
700  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
701  bool m_possibleDoubleClick = false;
702 
703  bool m_autoScroll = true;
704 
705  Sprite m_spriteBackground;
706 
707  // Cached renderer properties
708  Borders m_bordersCached;
709  Borders m_paddingCached;
710  Color m_borderColorCached;
711  Color m_backgroundColorCached;
712  Color m_backgroundColorHoverCached;
713  Color m_selectedBackgroundColorCached;
714  Color m_selectedBackgroundColorHoverCached;
715  Color m_textColorCached;
716  Color m_textColorHoverCached;
717  Color m_selectedTextColorCached;
718  Color m_selectedTextColorHoverCached;
719  TextStyles m_textStyleCached;
720  TextStyles m_selectedTextStyleCached;
721 
723  };
724 
726 }
727 
729 
730 #endif // TGUI_LIST_BOX_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Wrapper for colors.
Definition: Color.hpp:52
Definition: CopiedSharedPtr.hpp:40
Wrapper for durations.
Definition: Duration.hpp:52
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Definition: ListBoxRenderer.hpp:37
List box widget.
Definition: ListBox.hpp:43
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.
void setTextSize(unsigned int textSize) override
Changes the text size of the items.
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.
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 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.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
static ListBox::Ptr copy(ListBox::ConstPtr listBox)
Makes a copy of another list box.
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 draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
void removeAllItems()
Removes all items from the list.
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.
bool removeItemByIndex(std::size_t index)
Removes the item from the list box.
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:410
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.
Definition: ListBox.hpp:657
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.
std::vector< String > getItems() const
Returns a copy of the items in the list box.
ListBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
void setPosition(const Layout2d &position) override
Sets the position of the widget.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list box.
int getIndexById(const String &id) const
Returns the index of the item with the given id.
ListBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::vector< String > getItemIds() const
Returns a copy of the item ids in the list box.
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:49
Wrapper class to store strings.
Definition: String.hpp:74
Wrapper for text styles.
Definition: TextStyle.hpp:58
Definition: Text.hpp:44
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.
virtual void setPosition(const Layout2d &position)
sets the position 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:676
States used for drawing.
Definition: RenderStates.hpp:39