TGUI  0.8-alpha
ListBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 
439  bool getAutoScroll() const;
440 
441 
446  bool contains(const sf::String& item) const;
447 
448 
453  bool containsId(const sf::String& id) const;
454 
455 
462  bool mouseOnWidget(Vector2f pos) const override;
463 
467  void leftMousePressed(Vector2f pos) override;
468 
472  void leftMouseReleased(Vector2f pos) override;
473 
477  void mouseMoved(Vector2f pos) override;
478 
482  void mouseWheelScrolled(float delta, Vector2f pos) override;
483 
487  void mouseNoLongerOnWidget() override;
488 
492  void mouseNoLongerDown() override;
493 
494 
502  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
503 
504 
506  protected:
507 
517  Signal& getSignal(std::string signalName) override;
518 
519 
526  void rendererChanged(const std::string& property) override;
527 
528 
532  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
533 
534 
538  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
539 
540 
542  // Returns the size without the borders
544  Vector2f getInnerSize() const;
545 
546 
548  // Update the colors and text style of the selected and hovered items
550  void updateSelectedAndHoveringItemColorsAndStyle();
551 
552 
554  // Update the color and text style of all the items
556  void updateItemColorsAndStyle();
557 
558 
560  // Update on which item the mouse is standing
562  void updateHoveringItem(int item);
563 
564 
566  // Update which item is selected
568  void updateSelectedItem(int item);
569 
570 
572  // This function is called every frame with the time passed since the last frame.
574  void update(sf::Time elapsedTime) override;
575 
576 
578  // Makes a copy of the widget
580  Widget::Ptr clone() const override
581  {
582  return std::make_shared<ListBox>(*this);
583  }
584 
585 
587  public:
588 
589  SignalItem onItemSelect = {"ItemSelected"};
590  SignalItem onMousePress = {"MousePressed"};
591  SignalItem onMouseRelease = {"MouseReleased"};
592  SignalItem onDoubleClick = {"DoubleClicked"};
593 
594 
596  protected:
597 
598  // This contains the different items in the list box
599  std::vector<Text> m_items;
600  std::vector<sf::String> m_itemIds;
601 
602  // What is the index of the selected item?
603  // This is also used by combo box, so it can't just be changed to a pointer!
604  int m_selectedItem = -1;
605 
606  int m_hoveringItem = -1;
607 
608  // The size must be stored
609  unsigned int m_itemHeight = 0;
610  unsigned int m_requestedTextSize = 0;
611  unsigned int m_textSize = 0;
612 
613  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
614  std::size_t m_maxItems = 0;
615 
616  // When there are too many items a scrollbar will be shown
618 
619  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
620  bool m_possibleDoubleClick = false;
621 
622  bool m_autoScroll = true;
623 
624  Sprite m_spriteBackground;
625 
626  // Cached renderer properties
627  Borders m_bordersCached;
628  Borders m_paddingCached;
629  Color m_borderColorCached;
630  Color m_backgroundColorCached;
631  Color m_backgroundColorHoverCached;
632  Color m_selectedBackgroundColorCached;
633  Color m_selectedBackgroundColorHoverCached;
634  Color m_textColorCached;
635  Color m_textColorHoverCached;
636  Color m_selectedTextColorCached;
637  Color m_selectedTextColorHoverCached;
638  TextStyle m_textStyleCached;
639  TextStyle m_selectedTextStyleCached;
640 
642  };
643 
645 }
646 
648 
649 #endif // TGUI_LIST_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:43
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:243
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&#39;t know its exact type.
Definition: ListBox.hpp:580
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:395
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:58
List box widget.
Definition: ListBox.hpp:42
Definition: ListBoxRenderer.hpp:36
Definition: CopiedSharedPtr.hpp:39
Definition: Outline.hpp:38