TGUI  0.8-alpha
ScrollablePanel.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_SCROLLABLE_PANEL_HPP
27 #define TGUI_SCROLLABLE_PANEL_HPP
28 
29 #include <TGUI/CopiedSharedPtr.hpp>
30 #include <TGUI/Widgets/Panel.hpp>
31 #include <TGUI/Widgets/Scrollbar.hpp>
32 #include <TGUI/Renderers/ScrollablePanelRenderer.hpp>
33 
35 
36 namespace tgui
37 {
41  class TGUI_API ScrollablePanel : public Panel
42  {
43  public:
44 
45  typedef std::shared_ptr<ScrollablePanel> Ptr;
46  typedef std::shared_ptr<const ScrollablePanel> ConstPtr;
47 
49  enum class ScrollbarPolicy
50  {
51  Automatic,
52  Always,
53  Never
54  };
55 
56 
65  ScrollablePanel(const Layout2d& size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
66 
67 
71  ScrollablePanel(const ScrollablePanel& copy);
72 
73 
77  ScrollablePanel(ScrollablePanel&& copy);
78 
79 
83  ScrollablePanel& operator= (const ScrollablePanel& other);
84 
85 
89  ScrollablePanel& operator= (ScrollablePanel&& other);
90 
91 
102  static ScrollablePanel::Ptr create(Layout2d size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
103 
104 
113 
114 
119  ScrollablePanelRenderer* getSharedRenderer();
120  const ScrollablePanelRenderer* getSharedRenderer() const;
121 
127  ScrollablePanelRenderer* getRenderer();
128  const ScrollablePanelRenderer* getRenderer() const;
129 
130 
136  void setSize(const Layout2d& size) override;
137  using Widget::setSize;
138 
139 
146  void add(const Widget::Ptr& widget, const sf::String& widgetName = "") override;
147 
148 
156  bool remove(const Widget::Ptr& widget) override;
157 
158 
162  void removeAllWidgets() override;
163 
164 
174  void setContentSize(Vector2f size);
175 
176 
184  Vector2f getContentSize() const;
185 
186 
192  Vector2f getContentOffset() const;
193 
194 
199  void setScrollbarWidth(float width);
200 
201 
206  float getScrollbarWidth() const;
207 
208 
213  void setVerticalScrollbarPolicy(ScrollbarPolicy policy);
214 
215 
220  ScrollbarPolicy getVerticalScrollbarPolicy() const;
221 
222 
227  void setHorizontalScrollbarPolicy(ScrollbarPolicy policy);
228 
229 
234  ScrollbarPolicy getHorizontalScrollbarPolicy() const;
235 
236 
240  void leftMousePressed(Vector2f pos) override;
241 
245  void leftMouseReleased(Vector2f pos) override;
246 
250  void mouseMoved(Vector2f pos) override;
251 
255  void mouseWheelScrolled(float delta, Vector2f pos) override;
256 
260  void mouseNoLongerOnWidget() override;
261 
265  void mouseNoLongerDown() override;
266 
267 
274  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
275 
276 
278  protected:
279 
286  void rendererChanged(const std::string& property) override;
287 
288 
292  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
293 
294 
298  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
299 
300 
302  // Makes a copy of the widget
304  Widget::Ptr clone() const override
305  {
306  return std::make_shared<ScrollablePanel>(*this);
307  }
308 
309 
311  private:
312 
314  // Update the position, size and value of the scrollbars
316  void updateScrollbars();
317 
318 
320  // Find out what the most right and bottom positions are that are in use by the child widgets
322  void recalculateMostBottomRightPosition();
323 
324 
326  // Connect the position and size events to recalculate the bottom right position when a widget is updated
328  void connectPositionAndSize(Widget::Ptr widget);
329 
330 
332  // Disconnect the position and size events that were connected to keep track of widget changes
334  void disconnectAllChildWidgets();
335 
336 
338  private:
339 
340  Vector2f m_contentSize;
341  Vector2f m_mostBottomRightPosition;
342  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
343  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
344 
345  ScrollbarPolicy m_verticalScrollbarPolicy = ScrollbarPolicy::Automatic;
346  ScrollbarPolicy m_horizontalScrollbarPolicy = ScrollbarPolicy::Automatic;
347 
348  std::map<Widget::Ptr, unsigned int> m_connectedCallbacks;
349 
351  };
352 
354 }
355 
357 
358 #endif // TGUI_SCROLLABLE_PANEL_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Group of widgets that has a background color and optional borders.
Definition: ScrollablePanel.hpp:41
Class to store the position or size of a widget.
Definition: Layout.hpp:243
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: ScrollablePanel.hpp:304
ScrollbarPolicy
Defines when the scrollbar shows up.
Definition: ScrollablePanel.hpp:49
Definition: Vector2f.hpp:37
std::shared_ptr< const ScrollablePanel > ConstPtr
Shared constant widget pointer.
Definition: ScrollablePanel.hpp:46
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
std::shared_ptr< ScrollablePanel > Ptr
Shared widget pointer.
Definition: ScrollablePanel.hpp:45
Group of widgets that has a background color and optional borders.
Definition: Panel.hpp:40
Definition: CopiedSharedPtr.hpp:39