1// Aseprite UI Library
2// Copyright (C) 2001-2013, 2015 David Capello
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "ui/scroll_bar.h"
12
13namespace ui {
14
15void setup_scrollbars(const gfx::Size& scrollableSize,
16 gfx::Rect& viewportArea,
17 Widget& parent,
18 ScrollBar& hbar,
19 ScrollBar& vbar)
20{
21#define NEED_BAR(w, h, width) \
22 ((scrollableSize.w > viewportArea.w) && \
23 (vbar.getBarWidth() < fullViewportArea.w) && \
24 (hbar.getBarWidth() < fullViewportArea.h))
25
26 const gfx::Rect fullViewportArea = viewportArea;
27
28 hbar.setSize(scrollableSize.w);
29 vbar.setSize(scrollableSize.h);
30
31 if (hbar.parent()) parent.removeChild(&hbar);
32 if (vbar.parent()) parent.removeChild(&vbar);
33
34 if (NEED_BAR(w, h, width)) {
35 viewportArea.h -= hbar.getBarWidth();
36 parent.addChild(&hbar);
37
38 if (NEED_BAR(h, w, height)) {
39 viewportArea.w -= vbar.getBarWidth();
40 if (NEED_BAR(w, h, width))
41 parent.addChild(&vbar);
42 else {
43 viewportArea.w += vbar.getBarWidth();
44 viewportArea.h += hbar.getBarWidth();
45 parent.removeChild(&hbar);
46 }
47 }
48 }
49 else if (NEED_BAR(h, w, height)) {
50 viewportArea.w -= vbar.getBarWidth();
51 parent.addChild(&vbar);
52
53 if (NEED_BAR(w, h, width)) {
54 viewportArea.h -= hbar.getBarWidth();
55 if (NEED_BAR(h, w, height))
56 parent.addChild(&hbar);
57 else {
58 viewportArea.w += vbar.getBarWidth();
59 viewportArea.h += hbar.getBarWidth();
60 parent.removeChild(&vbar);
61 }
62 }
63 }
64
65 if (parent.hasChild(&hbar)) {
66 hbar.setBounds(gfx::Rect(viewportArea.x, viewportArea.y2(),
67 viewportArea.w, hbar.getBarWidth()));
68 hbar.setVisible(true);
69 }
70 else
71 hbar.setVisible(false);
72
73 if (parent.hasChild(&vbar)) {
74 vbar.setBounds(gfx::Rect(viewportArea.x2(), viewportArea.y,
75 vbar.getBarWidth(), viewportArea.h));
76 vbar.setVisible(true);
77 }
78 else
79 vbar.setVisible(false);
80}
81
82} // namespace ui
83