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 {
65  class TGUI_API ListBox : public Widget
66  {
67  public:
68 
69  typedef std::shared_ptr<ListBox> Ptr;
70  typedef std::shared_ptr<const ListBox> ConstPtr;
71 
72 
74  // Default constructor
76  ListBox();
77 
78 
85  static ListBox::Ptr create();
86 
87 
96  static ListBox::Ptr copy(ListBox::ConstPtr listBox);
97 
98 
106  {
107  return aurora::downcast<ListBoxRenderer*>(m_renderer.get());
108  }
109 
110 
117  virtual void setPosition(const Layout2d& position) override;
119 
120 
127  void setSize(const Layout2d& size) override;
129 
130 
145  bool addItem(const sf::String& itemName, const sf::String& id = "");
146 
147 
162  bool setSelectedItem(const sf::String& itemName);
163 
164 
179  bool setSelectedItemById(const sf::String& id);
180 
181 
195  bool setSelectedItemByIndex(std::size_t index);
196 
197 
202  void deselectItem();
203 
204 
217  bool removeItem(const sf::String& itemName);
218 
219 
232  bool removeItemById(const sf::String& id);
233 
234 
248  bool removeItemByIndex(std::size_t index);
249 
250 
255  void removeAllItems();
256 
257 
268  sf::String getItemById(const sf::String& id) const;
269 
270 
278  sf::String getSelectedItem() const;
279 
280 
288  sf::String getSelectedItemId() const;
289 
290 
297  int getSelectedItemIndex() const;
298 
299 
313  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
314 
315 
329  bool changeItemById(const sf::String& id, const sf::String& newValue);
330 
331 
343  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
344 
345 
352  std::size_t getItemCount() const;
353 
354 
361  std::vector<sf::String> getItems() const;
362 
363 
372  const std::vector<sf::String>& getItemIds() const;
373 
374 
383  void setItemHeight(unsigned int itemHeight);
384 
385 
392  unsigned int getItemHeight() const;
393 
394 
406  void setTextSize(unsigned int textSize);
407 
408 
415  unsigned int getTextSize() const;
416 
417 
427  void setMaximumItems(std::size_t maximumItems = 0);
428 
429 
437  std::size_t getMaximumItems() const;
438 
439 
448  void setAutoScroll(bool autoScroll);
449 
450 
458  bool getAutoScroll() const;
459 
460 
464  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
465 
469  virtual void leftMousePressed(sf::Vector2f pos) override;
470 
474  virtual void leftMouseReleased(sf::Vector2f pos) override;
475 
479  virtual void mouseMoved(sf::Vector2f pos) override;
480 
484  virtual void mouseWheelScrolled(float delta, int x, int y) override;
485 
489  virtual void mouseNoLongerOnWidget() override;
490 
494  virtual void mouseNoLongerDown() override;
495 
496 
504  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
505 
506 
508  protected:
509 
516  virtual void rendererChanged(const std::string& property) override;
517 
518 
520  // Returns the size without the borders
522  sf::Vector2f getInnerSize() const;
523 
524 
526  // Update the colors and text style of the selected and hovered items
528  void updateSelectedAndHoveringItemColorsAndStyle();
529 
530 
532  // Update the color and text style of all the items
534  void updateItemColorsAndStyle();
535 
536 
538  // Update on which item the mouse is standing
540  void updateHoveringItem(int item);
541 
542 
544  // Update which item is selected
546  void updateSelectedItem(int item);
547 
548 
550  // This function is called every frame with the time passed since the last frame.
552  virtual void update(sf::Time elapsedTime) override;
553 
554 
556  // Makes a copy of the widget
558  virtual Widget::Ptr clone() const override
559  {
560  return std::make_shared<ListBox>(*this);
561  }
562 
563 
565  protected:
566 
567  // This contains the different items in the list box
568  std::vector<Text> m_items;
569  std::vector<sf::String> m_itemIds;
570 
571  // What is the index of the selected item?
572  // This is also used by combo box, so it can't just be changed to a pointer!
573  int m_selectedItem = -1;
574 
575  int m_hoveringItem = -1;
576 
577  // The size must be stored
578  unsigned int m_itemHeight = 22;
579  unsigned int m_requestedTextSize = 0;
580  unsigned int m_textSize = 18;
581 
582  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
583  std::size_t m_maxItems = 0;
584 
585  // When there are too many items a scrollbar will be shown
586  ScrollbarChildWidget m_scroll;
587 
588  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
589  bool m_possibleDoubleClick = false;
590 
591  bool m_autoScroll = true;
592 
593  Sprite m_spriteBackground;
594 
595  // Cached renderer properties
596  Borders m_bordersCached;
597  Borders m_paddingCached;
598  Color m_borderColorCached;
599  Color m_backgroundColorCached;
600  Color m_backgroundColorHoverCached;
601  Color m_selectedBackgroundColorCached;
602  Color m_selectedBackgroundColorHoverCached;
603  Color m_textColorCached;
604  Color m_textColorHoverCached;
605  Color m_selectedTextColorCached;
606  Color m_selectedTextColorHoverCached;
607  TextStyle m_textStyleCached;
608  TextStyle m_selectedTextStyleCached;
609 
611  };
612 
614 }
615 
617 
618 #endif // TGUI_LIST_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Wrapper for text styles.
Definition: TextStyle.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:263
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:69
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:355
virtual void setPosition(const Layout2d &position)
Sets the position of the widget.
ListBoxRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: ListBox.hpp:105
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition: ListBox.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: ListBox.hpp:558
List box widget.
Definition: ListBox.hpp:65
Definition: ListBoxRenderer.hpp:36
Definition: Outline.hpp:37