TGUI  0.8-dev
ListBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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/Widgets/Scrollbar.hpp>
31 #include <TGUI/Renderers/ListBoxRenderer.hpp>
32 #include <TGUI/Text.hpp>
33 
35 
36 namespace tgui
37 {
41  class TGUI_API ListBox : public Widget
42  {
43  public:
44 
45  typedef std::shared_ptr<ListBox> Ptr;
46  typedef std::shared_ptr<const ListBox> ConstPtr;
47 
48 
50  // Default constructor
52  ListBox();
53 
54 
61  static ListBox::Ptr create();
62 
63 
72  static ListBox::Ptr copy(ListBox::ConstPtr listBox);
73 
74 
79  ListBoxRenderer* getSharedRenderer();
80  const ListBoxRenderer* getSharedRenderer() const;
81 
87  ListBoxRenderer* getRenderer();
88  const ListBoxRenderer* getRenderer() const;
89 
90 
97  void setPosition(const Layout2d& position) override;
98  using Widget::setPosition;
99 
100 
107  void setSize(const Layout2d& size) override;
108  using Widget::setSize;
109 
110 
125  bool addItem(const sf::String& itemName, const sf::String& id = "");
126 
127 
142  bool setSelectedItem(const sf::String& itemName);
143 
144 
159  bool setSelectedItemById(const sf::String& id);
160 
161 
175  bool setSelectedItemByIndex(std::size_t index);
176 
177 
182  void deselectItem();
183 
184 
197  bool removeItem(const sf::String& itemName);
198 
199 
212  bool removeItemById(const sf::String& id);
213 
214 
228  bool removeItemByIndex(std::size_t index);
229 
230 
235  void removeAllItems();
236 
237 
248  sf::String getItemById(const sf::String& id) const;
249 
250 
258  sf::String getSelectedItem() const;
259 
260 
268  sf::String getSelectedItemId() const;
269 
270 
277  int getSelectedItemIndex() const;
278 
279 
293  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
294 
295 
309  bool changeItemById(const sf::String& id, const sf::String& newValue);
310 
311 
323  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
324 
325 
332  std::size_t getItemCount() const;
333 
334 
341  std::vector<sf::String> getItems() const;
342 
343 
352  const std::vector<sf::String>& getItemIds() const;
353 
354 
363  void setItemHeight(unsigned int itemHeight);
364 
365 
372  unsigned int getItemHeight() const;
373 
374 
386  void setTextSize(unsigned int textSize);
387 
388 
395  unsigned int getTextSize() const;
396 
397 
407  void setMaximumItems(std::size_t maximumItems = 0);
408 
409 
417  std::size_t getMaximumItems() const;
418 
419 
428  void setAutoScroll(bool autoScroll);
429 
430 
438  bool getAutoScroll() const;
439 
440 
445  bool contains(const sf::String& item) const;
446 
447 
452  bool containsId(const sf::String& id) const;
453 
454 
461  bool mouseOnWidget(Vector2f pos) const override;
462 
466  void leftMousePressed(Vector2f pos) override;
467 
471  void leftMouseReleased(Vector2f pos) override;
472 
476  void mouseMoved(Vector2f pos) override;
477 
481  void mouseWheelScrolled(float delta, Vector2f pos) override;
482 
486  void mouseNoLongerOnWidget() override;
487 
491  void mouseNoLongerDown() override;
492 
493 
501  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
502 
503 
505  protected:
506 
516  Signal& getSignal(std::string signalName) override;
517 
518 
525  void rendererChanged(const std::string& property) override;
526 
527 
531  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
532 
533 
537  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
538 
539 
541  // Returns the size without the borders
543  Vector2f getInnerSize() const;
544 
545 
547  // Update the colors and text style of the selected and hovered items
549  void updateSelectedAndHoveringItemColorsAndStyle();
550 
551 
553  // Update the color and text style of all the items
555  void updateItemColorsAndStyle();
556 
557 
559  // Update on which item the mouse is standing
561  void updateHoveringItem(int item);
562 
563 
565  // Update which item is selected
567  void updateSelectedItem(int item);
568 
569 
571  // This function is called every frame with the time passed since the last frame.
573  void update(sf::Time elapsedTime) override;
574 
575 
577  // Makes a copy of the widget
579  Widget::Ptr clone() const override
580  {
581  return std::make_shared<ListBox>(*this);
582  }
583 
584 
586  public:
587 
588  SignalItem onItemSelect = {"ItemSelected"};
589  SignalItem onMousePress = {"MousePressed"};
590  SignalItem onMouseRelease = {"MouseReleased"};
591  SignalItem onDoubleClick = {"DoubleClicked"};
592 
593 
595  protected:
596 
597  // This contains the different items in the list box
598  std::vector<Text> m_items;
599  std::vector<sf::String> m_itemIds;
600 
601  // What is the index of the selected item?
602  // This is also used by combo box, so it can't just be changed to a pointer!
603  int m_selectedItem = -1;
604 
605  int m_hoveringItem = -1;
606 
607  // The size must be stored
608  unsigned int m_itemHeight = 0;
609  unsigned int m_requestedTextSize = 0;
610  unsigned int m_textSize = 0;
611 
612  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
613  std::size_t m_maxItems = 0;
614 
615  // When there are too many items a scrollbar will be shown
616  ScrollbarChildWidget m_scroll;
617 
618  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
619  bool m_possibleDoubleClick = false;
620 
621  bool m_autoScroll = true;
622 
623  Sprite m_spriteBackground;
624 
625  // Cached renderer properties
626  Borders m_bordersCached;
627  Borders m_paddingCached;
628  Color m_borderColorCached;
629  Color m_backgroundColorCached;
630  Color m_backgroundColorHoverCached;
631  Color m_selectedBackgroundColorCached;
632  Color m_selectedBackgroundColorHoverCached;
633  Color m_textColorCached;
634  Color m_textColorHoverCached;
635  Color m_selectedTextColorCached;
636  Color m_selectedTextColorHoverCached;
637  TextStyle m_textStyleCached;
638  TextStyle m_selectedTextStyleCached;
639 
641  };
642 
644 }
645 
647 
648 #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:66
Definition: Vector2f.hpp:37
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:45
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:579
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:384
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:46
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:70
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
List box widget.
Definition: ListBox.hpp:41
Definition: ListBoxRenderer.hpp:36
Definition: Outline.hpp:38