forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter.cpp
345 lines (293 loc) · 8.72 KB
/
splitter.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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 "ui/splitter.h"
#include "ui/load_layout_event.h"
#include "ui/manager.h"
#include "ui/message.h"
#include "ui/size_hint_event.h"
#include "ui/resize_event.h"
#include "ui/save_layout_event.h"
#include "ui/system.h"
#include "ui/theme.h"
#include <sstream>
namespace ui {
using namespace gfx;
Splitter::Splitter(Type type, int align)
: Widget(kSplitterWidget)
, m_type(type)
, m_pos(50)
{
setAlign(align);
initTheme();
}
void Splitter::setPosition(double pos)
{
m_pos = pos;
limitPos();
invalidate();
}
bool Splitter::onProcessMessage(Message* msg)
{
switch (msg->type()) {
case kMouseDownMessage:
if (isEnabled()) {
Widget* c1, *c2;
int x1, y1, x2, y2;
int bar, click_bar;
gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();
bar = click_bar = 0;
UI_FOREACH_WIDGET_WITH_END(children(), it, end) {
if (it+1 != end) {
c1 = *it;
c2 = *(it+1);
++bar;
if (this->align() & HORIZONTAL) {
x1 = c1->bounds().x2();
y1 = bounds().y;
x2 = c2->bounds().x;
y2 = bounds().y2();
}
else {
x1 = bounds().x;
y1 = c1->bounds().y2();
x2 = bounds().x2();
y2 = c2->bounds().y;
}
if ((mousePos.x >= x1) && (mousePos.x < x2) &&
(mousePos.y >= y1) && (mousePos.y < y2))
click_bar = bar;
}
}
if (!click_bar)
break;
captureMouse();
// Continue with motion message...
}
else
break;
case kMouseMoveMessage:
if (hasCapture()) {
gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();
if (align() & HORIZONTAL) {
switch (m_type) {
case ByPercentage:
m_pos = 100.0 * (mousePos.x - bounds().x) / bounds().w;
break;
case ByPixel:
m_pos = mousePos.x - bounds().x;
break;
}
}
else {
switch (m_type) {
case ByPercentage:
m_pos = 100.0 * (mousePos.y - bounds().y) / bounds().h;
break;
case ByPixel:
m_pos = mousePos.y - bounds().y;
break;
}
}
limitPos();
layout();
return true;
}
break;
case kMouseUpMessage:
if (hasCapture()) {
releaseMouse();
return true;
}
break;
case kSetCursorMessage:
if (isEnabled() && (!manager()->getCapture() || hasCapture())) {
gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();
Widget* c1, *c2;
int x1, y1, x2, y2;
bool change_cursor = false;
UI_FOREACH_WIDGET_WITH_END(children(), it, end) {
if (it+1 != end) {
c1 = *it;
c2 = *(it+1);
if (this->align() & HORIZONTAL) {
x1 = c1->bounds().x2();
y1 = bounds().y;
x2 = c2->bounds().x;
y2 = bounds().y2();
}
else {
x1 = bounds().x;
y1 = c1->bounds().y2();
x2 = bounds().x2();
y2 = c2->bounds().y;
}
if ((mousePos.x >= x1) && (mousePos.x < x2) &&
(mousePos.y >= y1) && (mousePos.y < y2)) {
change_cursor = true;
break;
}
}
}
if (change_cursor) {
if (align() & HORIZONTAL)
set_mouse_cursor(kSizeWECursor);
else
set_mouse_cursor(kSizeNSCursor);
return true;
}
}
break;
}
return Widget::onProcessMessage(msg);
}
void Splitter::onResize(ResizeEvent& ev)
{
#define LAYOUT_TWO_CHILDREN(x, y, w, h, l, t, r, b) \
{ \
avail = rc.w - childSpacing(); \
\
pos.x = rc.x; \
pos.y = rc.y; \
switch (m_type) { \
case ByPercentage: \
pos.w = int(avail*m_pos/100); \
break; \
case ByPixel: \
pos.w = int(m_pos); \
break; \
} \
\
/* TODO uncomment this to make a restricted splitter */ \
/* pos.w = MID(reqSize1.w, pos.w, avail-reqSize2.w); */ \
pos.h = rc.h; \
\
child1->setBounds(pos); \
gfx::Rect child1Pos = child1->bounds(); \
\
pos.x = child1Pos.x + child1Pos.w + childSpacing(); \
pos.y = rc.y; \
pos.w = avail - child1Pos.w; \
pos.h = rc.h; \
\
child2->setBounds(pos); \
}
gfx::Rect rc(ev.bounds());
gfx::Rect pos(0, 0, 0, 0);
int avail;
setBoundsQuietly(rc);
limitPos();
Widget* child1 = panel1();
Widget* child2 = panel2();
if (child1 && child2) {
if (align() & HORIZONTAL) {
LAYOUT_TWO_CHILDREN(x, y, w, h, l, t, r, b);
}
else {
LAYOUT_TWO_CHILDREN(y, x, h, w, t, l, b, r);
}
}
else if (child1)
child1->setBounds(rc);
else if (child2)
child2->setBounds(rc);
}
void Splitter::onSizeHint(SizeHintEvent& ev)
{
#define GET_CHILD_SIZE(w, h) \
do { \
w = MAX(w, reqSize.w); \
h = MAX(h, reqSize.h); \
} while(0)
#define FINAL_SIZE(w) \
do { \
w *= visibleChildren; \
w += childSpacing() * (visibleChildren-1); \
} while(0)
int visibleChildren;
Size reqSize;
visibleChildren = 0;
for (auto child : children()) {
if (child->isVisible())
visibleChildren++;
}
int w, h;
w = h = 0;
for (auto child : children()) {
if (!child->isVisible())
continue;
reqSize = child->sizeHint();
if (this->align() & HORIZONTAL)
GET_CHILD_SIZE(w, h);
else
GET_CHILD_SIZE(h, w);
}
if (visibleChildren > 0) {
if (this->align() & HORIZONTAL)
FINAL_SIZE(w);
else
FINAL_SIZE(h);
}
w += border().width();
h += border().height();
ev.setSizeHint(Size(w, h));
}
void Splitter::onLoadLayout(LoadLayoutEvent& ev)
{
ev.stream() >> m_pos;
if (m_pos < 0) m_pos = 0;
if (m_type == ByPixel)
m_pos *= guiscale();
}
void Splitter::onSaveLayout(SaveLayoutEvent& ev)
{
double pos = (m_type == ByPixel ? m_pos / guiscale(): m_pos);
ev.stream() << pos;
}
Widget* Splitter::panel1() const
{
const WidgetsList& list = children();
if (list.size() >= 1 && list[0]->isVisible())
return list[0];
else
return nullptr;
}
Widget* Splitter::panel2() const
{
const WidgetsList& list = children();
if (list.size() >= 2 && list[1]->isVisible())
return list[1];
else
return nullptr;
}
void Splitter::limitPos()
{
if (align() & HORIZONTAL) {
switch (m_type) {
case ByPercentage:
m_pos = MID(0, m_pos, 100);
break;
case ByPixel:
if (isVisible())
m_pos = MID(0, m_pos, bounds().w);
break;
}
}
else {
switch (m_type) {
case ByPercentage:
m_pos = MID(0, m_pos, 100);
break;
case ByPixel:
if (isVisible())
m_pos = MID(0, m_pos, bounds().h);
break;
}
}
}
} // namespace ui