TGUI  0.9-dev
ListBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 
51  // Default constructor
53  ListBox();
54 
55 
62  static ListBox::Ptr create();
63 
64 
73  static ListBox::Ptr copy(ListBox::ConstPtr listBox);
74 
75 
80  ListBoxRenderer* getSharedRenderer();
81  const ListBoxRenderer* getSharedRenderer() const;
82 
88  ListBoxRenderer* getRenderer();
89  const ListBoxRenderer* getRenderer() const;
90 
91 
98  void setPosition(const Layout2d& position) override;
99  using Widget::setPosition;
100 
101 
108  void setSize(const Layout2d& size) override;
109  using Widget::setSize;
110 
111 
126  bool 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 
236  void removeAllItems();
237 
238 
249  String getItemById(const String& id) const;
250 
251 
259  String getItemByIndex(std::size_t index) const;
260 
261 
269  String getSelectedItem() const;
270 
271 
279  String getSelectedItemId() const;
280 
281 
288  int getSelectedItemIndex() const;
289 
290 
304  bool changeItem(const String& originalValue, const String& newValue);
305 
306 
320  bool changeItemById(const String& id, const String& newValue);
321 
322 
334  bool changeItemByIndex(std::size_t index, const String& newValue);
335 
336 
343  std::size_t getItemCount() const;
344 
345 
352  std::vector<String> getItems() const;
353 
354 
363  std::vector<String> getItemIds() const;
364 
365 
377  void setItemData(std::size_t index, Any data);
378 
379 
386  template <typename T>
387  T getItemData(std::size_t index) const
388  {
389  if (index < m_items.size())
390  return AnyCast<T>(m_items[index].data);
391  else
392  throw std::bad_cast();
393  }
394 
403  void setItemHeight(unsigned int itemHeight);
404 
405 
412  unsigned int getItemHeight() const;
413 
414 
426  void setTextSize(unsigned int textSize) override;
427 
428 
438  void setMaximumItems(std::size_t maximumItems = 0);
439 
440 
448  std::size_t getMaximumItems() const;
449 
450 
459  void setAutoScroll(bool autoScroll);
460 
461 
466  bool getAutoScroll() const;
467 
468 
476  bool contains(const String& item) const;
477 
478 
486  bool containsId(const String& id) const;
487 
488 
494  void setScrollbarValue(unsigned int value);
495 
496 
502  unsigned int getScrollbarValue() const;
503 
504 
511  bool mouseOnWidget(Vector2f pos) const override;
512 
516  void leftMousePressed(Vector2f pos) override;
517 
521  void leftMouseReleased(Vector2f pos) override;
522 
526  void mouseMoved(Vector2f pos) override;
527 
531  bool mouseWheelScrolled(float delta, Vector2f pos) override;
532 
536  void mouseNoLongerOnWidget() override;
537 
541  void leftMouseButtonNoLongerDown() override;
542 
543 
551  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
552 
553 
555  protected:
556 
566  Signal& getSignal(String signalName) override;
567 
568 
575  void rendererChanged(const String& property) override;
576 
577 
581  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
582 
583 
587  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
588 
589 
591  // Returns the size without the borders
593  Vector2f getInnerSize() const;
594 
595 
597  // Update the colors and text style of the selected and hovered items
599  void updateSelectedAndHoveringItemColorsAndStyle();
600 
601 
603  // Update the color and text style of all the items
605  void updateItemColorsAndStyle();
606 
607 
609  // Update on which item the mouse is standing
611  void updateHoveringItem(int item);
612 
613 
615  // Update which item is selected
617  void updateSelectedItem(int item);
618 
619 
621  // This function is called every frame with the time passed since the last frame.
623  void update(Duration elapsedTime) override;
624 
625 
627  // Makes a copy of the widget
629  Widget::Ptr clone() const override
630  {
631  return std::make_shared<ListBox>(*this);
632  }
633 
634 
636  public:
637 
638  SignalItem onItemSelect = {"ItemSelected"};
639  SignalItem onMousePress = {"MousePressed"};
640  SignalItem onMouseRelease = {"MouseReleased"};
641  SignalItem onDoubleClick = {"DoubleClicked"};
642 
643 
645  protected:
646 
647  struct Item
648  {
649  Text text;
650  Any data;
651  String id;
652  };
653 
654  std::vector<Item> m_items;
655 
656  // What is the index of the selected item?
657  // This is also used by combo box, so it can't just be changed to a pointer!
658  int m_selectedItem = -1;
659 
660  int m_hoveringItem = -1;
661 
662  // The size must be stored
663  unsigned int m_itemHeight = 0;
664  unsigned int m_requestedTextSize = 0;
665 
666  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
667  std::size_t m_maxItems = 0;
668 
669  // When there are too many items a scrollbar will be shown
671 
672  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
673  bool m_possibleDoubleClick = false;
674 
675  bool m_autoScroll = true;
676 
677  Sprite m_spriteBackground;
678 
679  // Cached renderer properties
680  Borders m_bordersCached;
681  Borders m_paddingCached;
682  Color m_borderColorCached;
683  Color m_backgroundColorCached;
684  Color m_backgroundColorHoverCached;
685  Color m_selectedBackgroundColorCached;
686  Color m_selectedBackgroundColorHoverCached;
687  Color m_textColorCached;
688  Color m_textColorHoverCached;
689  Color m_selectedTextColorCached;
690  Color m_selectedTextColorHoverCached;
691  TextStyle m_textStyleCached;
692  TextStyle m_selectedTextStyleCached;
693 
695  };
696 
698 }
699 
701 
702 #endif // TGUI_LIST_BOX_HPP
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:216
Definition: ListBoxRenderer.hpp:36
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
Definition: Text.hpp:42
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:47
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:710
List box widget.
Definition: ListBox.hpp:42
Wrapper for durations.
Definition: Duration.hpp:48
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:70
T getItemData(std::size_t index) const
Returns user data stored in the item.
Definition: ListBox.hpp:387
Definition: Outline.hpp:38
The parent class for every widget.
Definition: Widget.hpp:68
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:46
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition: ListBox.hpp:47
Definition: ListBox.hpp:647
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ListBox.hpp:629
Wrapper for text styles.
Definition: TextStyle.hpp:44