TGUI  0.9-dev
ScrollablePanel.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 
56  ScrollablePanel(const Layout2d& size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
57 
58 
62  ScrollablePanel(const ScrollablePanel& copy);
63 
64 
68  ScrollablePanel(ScrollablePanel&& copy);
69 
70 
74  ScrollablePanel& operator= (const ScrollablePanel& other);
75 
76 
80  ScrollablePanel& operator= (ScrollablePanel&& other);
81 
82 
93  static ScrollablePanel::Ptr create(Layout2d size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
94 
95 
104 
105 
110  ScrollablePanelRenderer* getSharedRenderer();
111  const ScrollablePanelRenderer* getSharedRenderer() const;
112 
118  ScrollablePanelRenderer* getRenderer();
119  const ScrollablePanelRenderer* getRenderer() const;
120 
121 
127  void setSize(const Layout2d& size) override;
128  using Widget::setSize;
129 
130 
135  Vector2f getAbsolutePosition() const override;
136 
137 
144  void add(const Widget::Ptr& widget, const String& widgetName = "") override;
145 
146 
154  bool remove(const Widget::Ptr& widget) override;
155 
156 
160  void removeAllWidgets() override;
161 
162 
172  void setContentSize(Vector2f size);
173 
174 
182  Vector2f getContentSize() const;
183 
184 
190  Vector2f getContentOffset() const;
191 
192 
197  float getScrollbarWidth() const;
198 
199 
204  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
205 
206 
211  Scrollbar::Policy getVerticalScrollbarPolicy() const;
212 
213 
218  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
219 
220 
225  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
226 
227 
233  void setVerticalScrollAmount(unsigned int scrollAmount);
234 
235 
241  unsigned int getVerticalScrollAmount() const;
242 
243 
249  void setHorizontalScrollAmount(unsigned int scrollAmount);
250 
251 
257  unsigned int getHorizontalScrollAmount() const;
258 
259 
265  void setVerticalScrollbarValue(unsigned int value);
266 
267 
273  unsigned int getVerticalScrollbarValue() const;
274 
275 
281  void setHorizontalScrollbarValue(unsigned int value);
282 
283 
289  unsigned int getHorizontalScrollbarValue() const;
290 
291 
295  void leftMousePressed(Vector2f pos) override;
296 
300  void leftMouseReleased(Vector2f pos) override;
301 
305  void mouseMoved(Vector2f pos) override;
306 
310  bool mouseWheelScrolled(float delta, Vector2f pos) override;
311 
315  void mouseNoLongerOnWidget() override;
316 
320  void leftMouseButtonNoLongerDown() override;
321 
322 
325  // Shows the tool tip when the widget is located below the mouse.
326  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
327  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
329  Widget::Ptr askToolTip(Vector2f mousePos) override;
330 
331 
338  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
339 
340 
342  protected:
343 
350  void rendererChanged(const String& property) override;
351 
352 
356  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
357 
358 
362  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
363 
364 
366  // Makes a copy of the widget
368  Widget::Ptr clone() const override
369  {
370  return std::make_shared<ScrollablePanel>(*this);
371  }
372 
373 
375  private:
376 
378  // Update the position, size and value of the scrollbars
380  void updateScrollbars();
381 
382 
384  // Find out what the most right and bottom positions are that are in use by the child widgets
386  void recalculateMostBottomRightPosition();
387 
388 
390  // Connect the position and size events to recalculate the bottom right position when a widget is updated
392  void connectPositionAndSize(Widget::Ptr widget);
393 
394 
396  // Disconnect the position and size events that were connected to keep track of widget changes
398  void disconnectAllChildWidgets();
399 
400 
402  private:
403 
404  Vector2f m_contentSize;
405  Vector2f m_mostBottomRightPosition;
406  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
407  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
408 
409  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
410  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
411 
412  unsigned int m_verticalScrollAmount = 0;
413  unsigned int m_horizontalScrollAmount = 0;
414 
415  std::map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
416  std::map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
417 
419  };
420 
422 }
423 
425 
426 #endif // TGUI_SCROLLABLE_PANEL_HPP
Definition: CopiedSharedPtr.hpp:39
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ScrollablePanel.hpp:368
std::shared_ptr< const ScrollablePanel > ConstPtr
Shared constant widget pointer.
Definition: ScrollablePanel.hpp:46
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
@ Automatic
Show the scrollbar only when needed (default)
Class to store the position or size of a widget.
Definition: Layout.hpp:258
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
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Group of widgets that has a background color and optional borders.
Definition: ScrollablePanel.hpp:41