forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.cpp
155 lines (135 loc) · 5.34 KB
/
box.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Aseprite UI Library
// Copyright (C) 2001-2017 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 "gfx/size.h"
#include "ui/box.h"
#include "ui/message.h"
#include "ui/size_hint_event.h"
#include "ui/resize_event.h"
#include "ui/theme.h"
namespace ui {
using namespace gfx;
Box::Box(int align)
: Widget(kBoxWidget)
{
setAlign(align);
initTheme();
}
void Box::onSizeHint(SizeHintEvent& ev)
{
#define ADD_CHILD_SIZE(w, h) { \
if (align() & HOMOGENEOUS) \
prefSize.w = MAX(prefSize.w, childSize.w); \
else \
prefSize.w += childSize.w; \
prefSize.h = MAX(prefSize.h, childSize.h); \
}
#define FINAL_ADJUSTMENT(w) { \
if (align() & HOMOGENEOUS) \
prefSize.w *= visibleChildren; \
prefSize.w += childSpacing() * (visibleChildren-1); \
}
int visibleChildren = 0;
for (auto child : children()) {
if (!child->hasFlags(HIDDEN))
++visibleChildren;
}
Size prefSize(0, 0);
for (auto child : children()) {
if (child->hasFlags(HIDDEN))
continue;
Size childSize = child->sizeHint();
if (align() & HORIZONTAL) {
ADD_CHILD_SIZE(w, h);
}
else {
ADD_CHILD_SIZE(h, w);
}
}
if (visibleChildren > 0) {
if (align() & HORIZONTAL) {
FINAL_ADJUSTMENT(w);
}
else {
FINAL_ADJUSTMENT(h);
}
}
prefSize.w += border().width();
prefSize.h += border().height();
ev.setSizeHint(prefSize);
}
void Box::onResize(ResizeEvent& ev)
{
#define LAYOUT_CHILDREN(x, y, w, h) { \
availExtraSize = availSize.w - prefSize.w; \
availSize.w -= childSpacing() * (visibleChildren-1); \
if (align() & HOMOGENEOUS) \
homogeneousSize = availSize.w / visibleChildren; \
\
Rect defChildPos(childrenBounds()); \
int i = 0, j = 0; \
for (auto child : children()) { \
if (child->hasFlags(HIDDEN)) \
continue; \
\
int size = 0; \
\
if (align() & HOMOGENEOUS) { \
if (i < visibleChildren-1) \
size = homogeneousSize; \
else \
size = availSize.w; \
} \
else { \
size = child->sizeHint().w; \
\
if (child->isExpansive()) { \
int extraSize = (availExtraSize / (expansiveChildren-j)); \
size += extraSize; \
availExtraSize -= extraSize; \
if (++j == expansiveChildren) \
size += availExtraSize; \
} \
} \
\
Rect childPos = defChildPos; \
childPos.w = size = MID(child->minSize().w, size, child->maxSize().w); \
childPos.h = MID(child->minSize().h, childPos.h, child->maxSize().h); \
child->setBounds(childPos); \
\
defChildPos.x += size + childSpacing(); \
availSize.w -= size; \
++i; \
} \
}
setBoundsQuietly(ev.bounds());
int visibleChildren = 0;
int expansiveChildren = 0;
for (auto child : children()) {
if (!child->hasFlags(HIDDEN)) {
++visibleChildren;
if (child->isExpansive())
++expansiveChildren;
}
}
if (visibleChildren > 0) {
Size prefSize(sizeHint());
Size availSize(childrenBounds().size());
int homogeneousSize = 0;
int availExtraSize = 0;
prefSize.w -= border().width();
prefSize.h -= border().height();
if (align() & HORIZONTAL) {
LAYOUT_CHILDREN(x, y, w, h);
}
else {
LAYOUT_CHILDREN(y, x, h, w);
}
}
}
} // namespace ui