TGUI  0.7.4
ListBox.hpp
1 //
3 // TGUI - Texus's 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 
33 
34 namespace tgui
35 {
36  class Label;
37  class ListBoxRenderer;
38 
66  class TGUI_API ListBox : public Widget
67  {
68  public:
69 
70  typedef std::shared_ptr<ListBox> Ptr;
71  typedef std::shared_ptr<const ListBox> ConstPtr;
72 
73 
75  // Default constructor
77  ListBox();
78 
79 
86  ListBox(const ListBox& copy);
87 
88 
97  ListBox& operator= (const ListBox& right);
98 
99 
106  static ListBox::Ptr create();
107 
108 
117  static ListBox::Ptr copy(ListBox::ConstPtr listBox);
118 
119 
126  std::shared_ptr<ListBoxRenderer> getRenderer() const
127  {
128  return std::static_pointer_cast<ListBoxRenderer>(m_renderer);
129  }
130 
131 
144  virtual void setPosition(const Layout2d& position) override;
146 
147 
154  void setSize(const Layout2d& size) override;
156 
157 
166  virtual sf::Vector2f getFullSize() const override;
167 
168 
177  virtual void setFont(const Font& font) override;
178 
179 
194  bool addItem(const sf::String& itemName, const sf::String& id = "");
195 
196 
211  bool setSelectedItem(const sf::String& itemName);
212 
213 
228  bool setSelectedItemById(const sf::String& id);
229 
230 
244  bool setSelectedItemByIndex(std::size_t index);
245 
246 
251  void deselectItem();
252 
253 
266  bool removeItem(const sf::String& itemName);
267 
268 
281  bool removeItemById(const sf::String& id);
282 
283 
297  bool removeItemByIndex(std::size_t index);
298 
299 
304  void removeAllItems();
305 
306 
317  sf::String getItemById(const sf::String& id) const;
318 
319 
327  sf::String getSelectedItem() const;
328 
329 
337  sf::String getSelectedItemId() const;
338 
339 
346  int getSelectedItemIndex() const;
347 
348 
362  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
363 
364 
378  bool changeItemById(const sf::String& id, const sf::String& newValue);
379 
380 
392  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
393 
394 
401  std::size_t getItemCount()
402  {
403  return m_items.size();
404  }
405 
406 
413  std::vector<sf::String> getItems();
414 
415 
424  const std::vector<sf::String>& getItemIds();
425 
426 
442  void setScrollbar(Scrollbar::Ptr scrollbar);
443 
444 
453  Scrollbar::Ptr getScrollbar() const;
454 
455 
464  void setItemHeight(unsigned int itemHeight);
465 
466 
473  unsigned int getItemHeight() const;
474 
475 
487  void setTextSize(unsigned int textSize);
488 
489 
496  unsigned int getTextSize() const;
497 
498 
508  void setMaximumItems(std::size_t maximumItems = 0);
509 
510 
518  std::size_t getMaximumItems() const
519  {
520  return m_maxItems;
521  }
522 
523 
532  void setAutoScroll(bool autoScroll);
533 
534 
542  bool getAutoScroll() const
543  {
544  return m_autoScroll;
545  }
546 
547 
554  virtual void setOpacity(float opacity) override;
555 
556 
565  virtual sf::Vector2f getWidgetOffset() const override;
566 
567 
571  virtual bool mouseOnWidget(float x, float y) const override;
572 
576  virtual void leftMousePressed(float x, float y) override;
577 
581  virtual void leftMouseReleased(float x, float y) override;
582 
586  virtual void mouseMoved(float x, float y) override;
587 
591  virtual void mouseWheelMoved(int delta, int x, int y) override;
592 
596  virtual void mouseNoLongerOnWidget() override;
597 
601  virtual void mouseNoLongerDown() override;
602 
603 
605  protected:
606 
608  // Update the color of the items
610  void updateItemColors();
611 
612 
625  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
626 
627 
629  // Makes a copy of the widget
631  virtual Widget::Ptr clone() const override
632  {
633  return std::make_shared<ListBox>(*this);
634  }
635 
636 
638  // This function is called every frame with the time passed since the last frame.
640  virtual void update(sf::Time elapsedTime) override;
641 
642 
644  // Draws the widget on the render target.
646  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
647 
648 
650  protected:
651 
652  // This contains the different items in the list box
653  std::vector<Label> m_items;
654  std::vector<sf::String> m_itemIds;
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 = 22;
664  unsigned int m_requestedTextSize = 0;
665  unsigned int m_textSize = 18;
666 
667  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
668  std::size_t m_maxItems = 0;
669 
670  // When there are too many items a scrollbar will be shown
671  Scrollbar::Ptr m_scroll = std::make_shared<Scrollbar>();
672 
673  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
674  bool m_possibleDoubleClick = false;
675 
676  bool m_autoScroll = true;
677 
678  // ComboBox contains a list box internally and it should be able to adjust it.
679  friend class ComboBox;
680  friend class ListBoxRenderer;
681 
683  };
684 
685 
687 
688  class TGUI_API ListBoxRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
689  {
690  public:
691 
698  ListBoxRenderer(ListBox* listBox) : m_listBox{listBox} {}
699 
700 
711  virtual void setProperty(std::string property, const std::string& value) override;
712 
713 
725  virtual void setProperty(std::string property, ObjectConverter&& value) override;
726 
727 
737  virtual ObjectConverter getProperty(std::string property) const override;
738 
739 
746  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
747 
748 
755  void setBackgroundColor(const Color& backgroundColor);
756 
757 
769  void setTextColor(const Color& textColor);
770 
771 
778  void setTextColorNormal(const Color& color);
779 
780 
787  void setTextColorHover(const Color& color);
788 
789 
796  void setHoverBackgroundColor(const Color& hoverBackgroundColor);
797 
798 
805  void setSelectedBackgroundColor(const Color& selectedBackgroundColor);
806 
807 
814  void setSelectedTextColor(const Color& selectedTextColor);
815 
816 
823  void setBorderColor(const Color& borderColor);
824 
825 
835  void setBackgroundTexture(const Texture& texture);
836 
837 
847  virtual void setPadding(const Padding& padding) override;
849 
850 
852  // Draws the widget on the render target.
854  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
855 
856 
858  private:
859 
861  // Returns the padding, which is possibly scaled with the background image.
863  Padding getScaledPadding() const;
864 
865 
867  // Makes a copy of the renderer
869  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
870 
871 
873  protected:
874 
875  ListBox* m_listBox;
876 
877  Texture m_backgroundTexture;
878 
879  sf::Color m_backgroundColor;
880  sf::Color m_textColor;
881  sf::Color m_hoverBackgroundColor;
882  sf::Color m_hoverTextColor;
883  sf::Color m_selectedBackgroundColor;
884  sf::Color m_selectedTextColor;
885  sf::Color m_borderColor;
886 
887  friend class ListBox;
888  friend class ComboBox;
889  friend class ComboBoxRenderer;
890 
892  };
893 
895 }
896 
898 
899 #endif // TGUI_LIST_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Implicit converter for colors.
Definition: Color.hpp:39
ListBoxRenderer(ListBox *listBox)
Constructor.
Definition: ListBox.hpp:698
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
The parent class for every widget.
Definition: Widget.hpp:71
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:53
Combo box widget.
Definition: ComboBox.hpp:51
std::shared_ptr< ListBoxRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: ListBox.hpp:126
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:70
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::size_t getItemCount()
Returns the amount of items in the list box.
Definition: ListBox.hpp:401
Parent class for every widget that has padding.
Definition: Borders.hpp:210
virtual void setPosition(const Layout2d &position)
set the position of the widget
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition: ListBox.hpp:71
Parent class for every widget that has borders.
Definition: Borders.hpp:136
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::size_t getMaximumItems() const
Returns the maximum items that the list box can contain.
Definition: ListBox.hpp:518
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: ListBox.hpp:631
List box widget.
Definition: ListBox.hpp:66
Definition: Texture.hpp:44
Definition: Borders.hpp:37
Definition: ListBox.hpp:688
Definition: ComboBox.hpp:693
bool getAutoScroll() const
Returns the maximum items that the list box can contain.
Definition: ListBox.hpp:542
Base class for all renderer classes.
Definition: Widget.hpp:682