TGUI  0.8-beta
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 
144  Vector2f getAbsolutePosition() const override;
145 
146 
153  void add(const Widget::Ptr& widget, const sf::String& widgetName = "") override;
154 
155 
163  bool remove(const Widget::Ptr& widget) override;
164 
165 
169  void removeAllWidgets() override;
170 
171 
181  void setContentSize(Vector2f size);
182 
183 
191  Vector2f getContentSize() const;
192 
193 
199  Vector2f getContentOffset() const;
200 
201 
206  void setScrollbarWidth(float width);
207 
208 
213  float getScrollbarWidth() const;
214 
215 
220  void setVerticalScrollbarPolicy(ScrollbarPolicy policy);
221 
222 
227  ScrollbarPolicy getVerticalScrollbarPolicy() const;
228 
229 
234  void setHorizontalScrollbarPolicy(ScrollbarPolicy policy);
235 
236 
241  ScrollbarPolicy getHorizontalScrollbarPolicy() const;
242 
243 
247  void leftMousePressed(Vector2f pos) override;
248 
252  void leftMouseReleased(Vector2f pos) override;
253 
257  void mouseMoved(Vector2f pos) override;
258 
262  void mouseWheelScrolled(float delta, Vector2f pos) override;
263 
267  void mouseNoLongerOnWidget() override;
268 
272  void mouseNoLongerDown() override;
273 
274 
281  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
282 
283 
285  protected:
286 
293  void rendererChanged(const std::string& property) override;
294 
295 
299  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
300 
301 
305  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
306 
307 
309  // Makes a copy of the widget
311  Widget::Ptr clone() const override
312  {
313  return std::make_shared<ScrollablePanel>(*this);
314  }
315 
316 
318  private:
319 
321  // Update the position, size and value of the scrollbars
323  void updateScrollbars();
324 
325 
327  // Find out what the most right and bottom positions are that are in use by the child widgets
329  void recalculateMostBottomRightPosition();
330 
331 
333  // Connect the position and size events to recalculate the bottom right position when a widget is updated
335  void connectPositionAndSize(Widget::Ptr widget);
336 
337 
339  // Disconnect the position and size events that were connected to keep track of widget changes
341  void disconnectAllChildWidgets();
342 
343 
345  private:
346 
347  Vector2f m_contentSize;
348  Vector2f m_mostBottomRightPosition;
349  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
350  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
351 
352  ScrollbarPolicy m_verticalScrollbarPolicy = ScrollbarPolicy::Automatic;
353  ScrollbarPolicy m_horizontalScrollbarPolicy = ScrollbarPolicy::Automatic;
354 
355  std::map<Widget::Ptr, unsigned int> m_connectedCallbacks;
356 
358  };
359 
361 }
362 
364 
365 #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:311
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