TGUI  0.8.4
ListBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 sf::String& itemName, const sf::String& id = "");
127 
128 
143  bool setSelectedItem(const sf::String& itemName);
144 
145 
160  bool setSelectedItemById(const sf::String& id);
161 
162 
176  bool setSelectedItemByIndex(std::size_t index);
177 
178 
183  void deselectItem();
184 
185 
198  bool removeItem(const sf::String& itemName);
199 
200 
213  bool removeItemById(const sf::String& id);
214 
215 
229  bool removeItemByIndex(std::size_t index);
230 
231 
236  void removeAllItems();
237 
238 
249  sf::String getItemById(const sf::String& id) const;
250 
251 
259  sf::String getSelectedItem() const;
260 
261 
269  sf::String getSelectedItemId() const;
270 
271 
278  int getSelectedItemIndex() const;
279 
280 
294  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
295 
296 
310  bool changeItemById(const sf::String& id, const sf::String& newValue);
311 
312 
324  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
325 
326 
333  std::size_t getItemCount() const;
334 
335 
342  std::vector<sf::String> getItems() const;
343 
344 
353  const std::vector<sf::String>& getItemIds() const;
354 
355 
364  void setItemHeight(unsigned int itemHeight);
365 
366 
373  unsigned int getItemHeight() const;
374 
375 
387  void setTextSize(unsigned int textSize);
388 
389 
396  unsigned int getTextSize() const;
397 
398 
408  void setMaximumItems(std::size_t maximumItems = 0);
409 
410 
418  std::size_t getMaximumItems() const;
419 
420 
429  void setAutoScroll(bool autoScroll);
430 
431 
436  bool getAutoScroll() const;
437 
438 
446  bool contains(const sf::String& item) const;
447 
448 
456  bool containsId(const sf::String& id) const;
457 
458 
465  bool mouseOnWidget(Vector2f pos) const override;
466 
470  void leftMousePressed(Vector2f pos) override;
471 
475  void leftMouseReleased(Vector2f pos) override;
476 
480  void mouseMoved(Vector2f pos) override;
481 
485  bool mouseWheelScrolled(float delta, Vector2f pos) override;
486 
490  void mouseNoLongerOnWidget() override;
491 
495  void mouseNoLongerDown() override;
496 
497 
505  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
506 
507 
509  protected:
510 
520  Signal& getSignal(std::string signalName) override;
521 
522 
529  void rendererChanged(const std::string& property) override;
530 
531 
535  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
536 
537 
541  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
542 
543 
545  // Returns the size without the borders
547  Vector2f getInnerSize() const;
548 
549 
551  // Update the colors and text style of the selected and hovered items
553  void updateSelectedAndHoveringItemColorsAndStyle();
554 
555 
557  // Update the color and text style of all the items
559  void updateItemColorsAndStyle();
560 
561 
563  // Update on which item the mouse is standing
565  void updateHoveringItem(int item);
566 
567 
569  // Update which item is selected
571  void updateSelectedItem(int item);
572 
573 
575  // This function is called every frame with the time passed since the last frame.
577  void update(sf::Time elapsedTime) override;
578 
579 
581  // Makes a copy of the widget
583  Widget::Ptr clone() const override
584  {
585  return std::make_shared<ListBox>(*this);
586  }
587 
588 
590  public:
591 
592  SignalItem onItemSelect = {"ItemSelected"};
593  SignalItem onMousePress = {"MousePressed"};
594  SignalItem onMouseRelease = {"MouseReleased"};
595  SignalItem onDoubleClick = {"DoubleClicked"};
596 
597 
599  protected:
600 
601  // This contains the different items in the list box
602  std::vector<Text> m_items;
603  std::vector<sf::String> m_itemIds;
604 
605  // What is the index of the selected item?
606  // This is also used by combo box, so it can't just be changed to a pointer!
607  int m_selectedItem = -1;
608 
609  int m_hoveringItem = -1;
610 
611  // The size must be stored
612  unsigned int m_itemHeight = 0;
613  unsigned int m_requestedTextSize = 0;
614  unsigned int m_textSize = 0;
615 
616  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
617  std::size_t m_maxItems = 0;
618 
619  // When there are too many items a scrollbar will be shown
621 
622  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
623  bool m_possibleDoubleClick = false;
624 
625  bool m_autoScroll = true;
626 
627  Sprite m_spriteBackground;
628 
629  // Cached renderer properties
630  Borders m_bordersCached;
631  Borders m_paddingCached;
632  Color m_borderColorCached;
633  Color m_backgroundColorCached;
634  Color m_backgroundColorHoverCached;
635  Color m_selectedBackgroundColorCached;
636  Color m_selectedBackgroundColorHoverCached;
637  Color m_textColorCached;
638  Color m_textColorHoverCached;
639  Color m_selectedTextColorCached;
640  Color m_selectedTextColorHoverCached;
641  TextStyle m_textStyleCached;
642  TextStyle m_selectedTextStyleCached;
643 
645  };
646 
648 }
649 
651 
652 #endif // TGUI_LIST_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:44
Wrapper for colors.
Definition: Color.hpp:47
Wrapper for text styles.
Definition: TextStyle.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:247
The parent class for every widget.
Definition: Widget.hpp:72
Definition: Vector2f.hpp:37
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:46
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ListBox.hpp:583
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition: ListBox.hpp:47
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:438
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
List box widget.
Definition: ListBox.hpp:42
Definition: ListBoxRenderer.hpp:36
Definition: CopiedSharedPtr.hpp:39
Definition: Outline.hpp:38