TGUI  0.8.4
ScrollablePanel.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 
48 #ifndef TGUI_REMOVE_DEPRECATED_CODE
49  using ScrollbarPolicy TGUI_DEPRECATED("Use tgui::Scrollbar::Policy instead") = Scrollbar::Policy;
51 #endif
52 
61  ScrollablePanel(const Layout2d& size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
62 
63 
67  ScrollablePanel(const ScrollablePanel& copy);
68 
69 
73  ScrollablePanel(ScrollablePanel&& copy);
74 
75 
79  ScrollablePanel& operator= (const ScrollablePanel& other);
80 
81 
85  ScrollablePanel& operator= (ScrollablePanel&& other);
86 
87 
98  static ScrollablePanel::Ptr create(Layout2d size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
99 
100 
109 
110 
115  ScrollablePanelRenderer* getSharedRenderer();
116  const ScrollablePanelRenderer* getSharedRenderer() const;
117 
123  ScrollablePanelRenderer* getRenderer();
124  const ScrollablePanelRenderer* getRenderer() const;
125 
126 
132  void setSize(const Layout2d& size) override;
133  using Widget::setSize;
134 
135 
140  Vector2f getAbsolutePosition() const override;
141 
142 
149  void add(const Widget::Ptr& widget, const sf::String& widgetName = "") override;
150 
151 
159  bool remove(const Widget::Ptr& widget) override;
160 
161 
165  void removeAllWidgets() override;
166 
167 
177  void setContentSize(Vector2f size);
178 
179 
187  Vector2f getContentSize() const;
188 
189 
195  Vector2f getContentOffset() const;
196 
197 #ifndef TGUI_REMOVE_DEPRECATED_CODE
198  TGUI_DEPRECATED("Use the ScrollbarWidth renderer property instead") void setScrollbarWidth(float width);
203 #endif
204 
209  float getScrollbarWidth() const;
210 
211 
216  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
217 
218 
223  Scrollbar::Policy getVerticalScrollbarPolicy() const;
224 
225 
230  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
231 
232 
237  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
238 
239 
243  void leftMousePressed(Vector2f pos) override;
244 
248  void leftMouseReleased(Vector2f pos) override;
249 
253  void mouseMoved(Vector2f pos) override;
254 
258  bool mouseWheelScrolled(float delta, Vector2f pos) override;
259 
263  void mouseNoLongerOnWidget() override;
264 
268  void mouseNoLongerDown() override;
269 
270 
273  // Shows the tool tip when the widget is located below the mouse.
274  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
275  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
277  Widget::Ptr askToolTip(Vector2f mousePos) override;
278 
279 
286  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
287 
288 
290  protected:
291 
298  void rendererChanged(const std::string& property) override;
299 
300 
304  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
305 
306 
310  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
311 
312 
314  // Makes a copy of the widget
316  Widget::Ptr clone() const override
317  {
318  return std::make_shared<ScrollablePanel>(*this);
319  }
320 
321 
323  private:
324 
326  // Update the position, size and value of the scrollbars
328  void updateScrollbars();
329 
330 
332  // Find out what the most right and bottom positions are that are in use by the child widgets
334  void recalculateMostBottomRightPosition();
335 
336 
338  // Connect the position and size events to recalculate the bottom right position when a widget is updated
340  void connectPositionAndSize(Widget::Ptr widget);
341 
342 
344  // Disconnect the position and size events that were connected to keep track of widget changes
346  void disconnectAllChildWidgets();
347 
348 
350  private:
351 
352  Vector2f m_contentSize;
353  Vector2f m_mostBottomRightPosition;
354  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
355  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
356 
357  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
358  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
359 
360  std::map<Widget::Ptr, unsigned int> m_connectedCallbacks;
361 
363  };
364 
366 }
367 
369 
370 #endif // TGUI_SCROLLABLE_PANEL_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Show the scrollbar only when needed (default)
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:247
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ScrollablePanel.hpp:316
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.
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
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