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