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 
447  bool mouseOnWidget(sf::Vector2f pos) const override;
448 
452  void leftMousePressed(sf::Vector2f pos) override;
453 
457  void leftMouseReleased(sf::Vector2f pos) override;
458 
462  void mouseMoved(sf::Vector2f pos) override;
463 
467  void mouseWheelScrolled(float delta, sf::Vector2f pos) override;
468 
472  void mouseNoLongerOnWidget() override;
473 
477  void mouseNoLongerDown() override;
478 
479 
487  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
488 
489 
491  protected:
492 
502  Signal& getSignal(std::string signalName) override;
503 
504 
511  void rendererChanged(const std::string& property) override;
512 
513 
515  // Returns the size without the borders
517  sf::Vector2f getInnerSize() const;
518 
519 
521  // Update the colors and text style of the selected and hovered items
523  void updateSelectedAndHoveringItemColorsAndStyle();
524 
525 
527  // Update the color and text style of all the items
529  void updateItemColorsAndStyle();
530 
531 
533  // Update on which item the mouse is standing
535  void updateHoveringItem(int item);
536 
537 
539  // Update which item is selected
541  void updateSelectedItem(int item);
542 
543 
545  // This function is called every frame with the time passed since the last frame.
547  void update(sf::Time elapsedTime) override;
548 
549 
551  // Makes a copy of the widget
553  Widget::Ptr clone() const override
554  {
555  return std::make_shared<ListBox>(*this);
556  }
557 
558 
560  public:
561 
562  SignalItem onItemSelect = {"ItemSelected"};
563  SignalItem onMousePress = {"MousePressed"};
564  SignalItem onMouseRelease = {"MouseReleased"};
565  SignalItem onDoubleClick = {"DoubleClicked"};
566 
567 
569  protected:
570 
571  // This contains the different items in the list box
572  std::vector<Text> m_items;
573  std::vector<sf::String> m_itemIds;
574 
575  // What is the index of the selected item?
576  // This is also used by combo box, so it can't just be changed to a pointer!
577  int m_selectedItem = -1;
578 
579  int m_hoveringItem = -1;
580 
581  // The size must be stored
582  unsigned int m_itemHeight = 22;
583  unsigned int m_requestedTextSize = 0;
584  unsigned int m_textSize = 18;
585 
586  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
587  std::size_t m_maxItems = 0;
588 
589  // When there are too many items a scrollbar will be shown
590  ScrollbarChildWidget m_scroll;
591 
592  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
593  bool m_possibleDoubleClick = false;
594 
595  bool m_autoScroll = true;
596 
597  Sprite m_spriteBackground;
598 
599  // Cached renderer properties
600  Borders m_bordersCached;
601  Borders m_paddingCached;
602  Color m_borderColorCached;
603  Color m_backgroundColorCached;
604  Color m_backgroundColorHoverCached;
605  Color m_selectedBackgroundColorCached;
606  Color m_selectedBackgroundColorHoverCached;
607  Color m_textColorCached;
608  Color m_textColorHoverCached;
609  Color m_selectedTextColorCached;
610  Color m_selectedTextColorHoverCached;
611  TextStyle m_textStyleCached;
612  TextStyle m_selectedTextStyleCached;
613 
615  };
616 
618 }
619 
621 
622 #endif // TGUI_LIST_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
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:243
The parent class for every widget.
Definition: Widget.hpp:62
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:553
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:372
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:394
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:66
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
List box widget.
Definition: ListBox.hpp:41
Definition: ListBoxRenderer.hpp:36
Definition: Outline.hpp:38