/
crowcolumnview.cpp
546 lines (499 loc) · 14.6 KB
/
crowcolumnview.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// This file is part of VSTGUI. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
#include "crowcolumnview.h"
#include "animation/animations.h"
#include "animation/timingfunctions.h"
#include <vector>
namespace VSTGUI {
namespace Layouting {
//--------------------------------------------------------------------------------
using CRects = std::vector<CRect>;
enum class Alignment
{
kTopLeft,
kTopCenter,
kTopRight,
kMiddleLeft,
kMiddleCenter,
kMiddleRight,
kBottomLeft,
kBottomCenter,
kBottomRight
};
enum class Style
{
kRow,
kColumn
};
//--------------------------------------------------------------------------------
Alignment translate (const CRowColumnView::LayoutStyle layoutStyle)
{
switch (layoutStyle)
{
case CRowColumnView::LayoutStyle::kTopLeft:
return Alignment::kTopLeft;
case CRowColumnView::LayoutStyle::kTopCenter:
return Alignment::kTopCenter;
case CRowColumnView::LayoutStyle::kTopRight:
return Alignment::kTopRight;
case CRowColumnView::LayoutStyle::kMiddleLeft:
return Alignment::kMiddleLeft;
case CRowColumnView::LayoutStyle::kMiddleCenter:
return Alignment::kMiddleCenter;
case CRowColumnView::LayoutStyle::kMiddleRight:
return Alignment::kMiddleRight;
case CRowColumnView::LayoutStyle::kBottomLeft:
return Alignment::kBottomLeft;
case CRowColumnView::LayoutStyle::kBottomCenter:
return Alignment::kBottomCenter;
case CRowColumnView::LayoutStyle::kBottomRight:
return Alignment::kBottomRight;
default:
return Alignment::kTopLeft;
}
}
//--------------------------------------------------------------------------------
Style translate (const CRowColumnView::Style style)
{
switch (style)
{
case CRowColumnView::Style::kRowStyle:
return Style::kRow;
case CRowColumnView::Style::kColumnStyle:
return Style::kColumn;
default:
return Style::kRow;
}
}
//--------------------------------------------------------------------------------
CPoint computeRectOffset (const CPoint& parent, const CPoint& rect, const Alignment alignment)
{
CPoint offset (0., 0.);
switch (alignment)
{
case Alignment::kTopLeft:
{
offset = CPoint (0., 0.);
break;
}
case Alignment::kTopCenter:
{
offset = CPoint ((parent.x - rect.x) / 2., 0.);
break;
}
case Alignment::kTopRight:
{
offset = CPoint (parent.x - rect.x, 0.);
break;
}
case Alignment::kMiddleLeft:
{
offset = CPoint (0., (parent.y - rect.y) / 2.);
break;
}
case Alignment::kMiddleCenter:
{
offset = CPoint ((parent.x - rect.x) / 2., (parent.y - rect.y) / 2.);
break;
}
case Alignment::kMiddleRight:
{
offset = CPoint (parent.x - rect.x, (parent.y - rect.y) / 2.);
break;
}
case Alignment::kBottomLeft:
{
offset = CPoint (0., parent.y - rect.y);
break;
}
case Alignment::kBottomCenter:
{
offset = CPoint ((parent.x - rect.x) / 2., parent.y - rect.y);
break;
}
case Alignment::kBottomRight:
{
offset = CPoint (parent.x - rect.x, parent.y - rect.y);
break;
}
default:
break;
}
return offset;
}
//--------------------------------------------------------------------------------
CRect computeGroupRect (const CRect& parent, const CRects& children, const Alignment alignment,
const Style style, double spacing)
{
CPoint maxSize;
if (style == Style::kRow)
{
for (const auto& rect : children)
{
if (maxSize.x < rect.getWidth ())
maxSize.x = rect.getWidth ();
maxSize.y = maxSize.y + rect.getHeight () + spacing;
}
// Remove the last spacing again
if (!children.empty())
maxSize.y -= spacing;
}
else
{
for (const auto& rect : children)
{
if (maxSize.y < rect.getHeight ())
maxSize.y = rect.getHeight ();
maxSize.x = maxSize.x + rect.getWidth () + spacing;
}
// Remove the last spacing again
if (!children.empty())
maxSize.x -= spacing;
}
const auto offset = computeRectOffset (parent.getSize (), maxSize, alignment);
return CRect().setSize(maxSize).offset(offset);
}
//--------------------------------------------------------------------------------
CRect computeGroupRect (const CViewContainer& parent, const Alignment alignment, const Style style,
const double spacing)
{
CRects childrenViewSizes;
parent.forEachChild (
[&] (const CView* child) { childrenViewSizes.push_back (child->getViewSize ()); });
return computeGroupRect (parent.getViewSize (), childrenViewSizes, alignment, style, spacing);
}
//--------------------------------------------------------------------------------
// AutoLayout Declaration
/// @brief An auto layout feature for the CRowColumnView
///
/// The AutoLayout flexibly layouts the children of a CRowColumnView. The children are grouped and
/// aligned among themselves depending on whether the style is a row (left to right) or a
/// column (top to bottom). The group is moved inside the parent to either of the nine
/// positions (top-left, top-center, top-right, middle-left, middle-center, middle-right,
/// bottom-left, bottom-center or bottom-right). If the size of the parent is changed, the layout
/// and alignment of the group is always retained.
//--------------------------------------------------------------------------------
class AutoLayout
{
public:
//--------------------------------------------------------------------------------
AutoLayout (const CViewContainer& parent, const Alignment alignment, const Style style,
const double spacing)
: alignment (alignment), style (style)
{
groupRect = Layouting::computeGroupRect (parent, alignment, style, spacing);
}
/** moves the child rect to the calculated position (inside the group and inside the parent) */
auto moveRect (CRect& viewSize) -> CRect&
{
// Offset the viewSize inside the groupRect...
const CPoint offset =
Layouting::computeRectOffset (groupRect.getSize (), viewSize.getSize (), alignment);
if (style == Style::kRow)
viewSize.offset (offset.x, 0.);
else
viewSize.offset (0., offset.y);
//...and offset by topLeft of the groupRect afterwards, in order to align with the
// 'real' parent.
return viewSize.offset (groupRect.getTopLeft ());
}
//--------------------------------------------------------------------------------
private:
const Alignment alignment = Alignment::kTopLeft;
const Style style = Style::kRow;
CRect groupRect;
};
//--------------------------------------------------------------------------------
}
//--------------------------------------------------------------------------------
CRowColumnView::CRowColumnView (const CRect& size, Style style, LayoutStyle layoutStyle, CCoord spacing, const CRect& margin)
: CAutoLayoutContainerView (size)
, style (style)
, layoutStyle (layoutStyle)
, spacing (spacing)
, margin (margin)
, flags (0)
, layoutGuard (false)
, viewResizeAnimationTime (200)
{
}
//--------------------------------------------------------------------------------
void CRowColumnView::setStyle (Style newStyle)
{
if (newStyle != style)
{
style = newStyle;
if (isAttached ())
layoutViews ();
}
}
//--------------------------------------------------------------------------------
void CRowColumnView::setSpacing (CCoord newSpacing)
{
if (newSpacing != spacing)
{
spacing = newSpacing;
if (isAttached ())
layoutViews ();
}
}
//--------------------------------------------------------------------------------
void CRowColumnView::setMargin (const CRect& newMargin)
{
if (newMargin != margin)
{
margin = newMargin;
if (isAttached ())
layoutViews ();
}
}
//--------------------------------------------------------------------------------
void CRowColumnView::setLayoutStyle (LayoutStyle inLayoutStyle)
{
if (inLayoutStyle != layoutStyle)
{
layoutStyle = inLayoutStyle;
if (isAttached ())
layoutViews ();
}
}
//--------------------------------------------------------------------------------
bool CRowColumnView::isAnimateViewResizing () const
{
return hasBit (flags, kAnimateViewResizing);
}
//--------------------------------------------------------------------------------
void CRowColumnView::setAnimateViewResizing (bool state)
{
setBit (flags, kAnimateViewResizing, state);
}
//--------------------------------------------------------------------------------
bool CRowColumnView::hideClippedSubviews () const
{
return hasBit (flags, kHideClippedSubViews);
}
//--------------------------------------------------------------------------------
void CRowColumnView::setHideClippedSubviews (bool state)
{
setBit (flags, kHideClippedSubViews, state);
}
//--------------------------------------------------------------------------------
void CRowColumnView::resizeSubView (CView* view, const CRect& newSize)
{
if (view->getViewSize () != newSize)
{
if (isAttached () && isAnimateViewResizing () && viewResizeAnimationTime > 0)
{
view->addAnimation ("CRowColumnResizing", new Animation::ViewSizeAnimation (newSize, false), new Animation::LinearTimingFunction (viewResizeAnimationTime));
}
else
{
view->setViewSize (newSize);
view->setMouseableArea (newSize);
}
}
}
//--------------------------------------------------------------------------------
void CRowColumnView::getMaxChildViewSize (CPoint& maxSize)
{
forEachChild ([&] (CView* view) {
const CRect& viewSize = view->getViewSize ();
if (viewSize.getWidth () > maxSize.x)
maxSize.x = viewSize.getWidth ();
if (viewSize.getHeight () > maxSize.y)
maxSize.y = viewSize.getHeight ();
});
}
//--------------------------------------------------------------------------------
void CRowColumnView::layoutViewsEqualSize ()
{
CPoint maxSize;
getMaxChildViewSize (maxSize);
if (style == kRowStyle)
maxSize.x = getViewSize ().getWidth () - (margin.right + margin.left);
else
maxSize.y = getViewSize ().getHeight () - (margin.top + margin.bottom);
Layouting::AutoLayout layout (*this, Layouting::translate (layoutStyle),
Layouting::translate (style), spacing);
CPoint location = margin.getTopLeft ();
forEachChild ([&] (CView* view) {
CRect viewSize = view->getViewSize ();
viewSize.originize ();
viewSize.offset (location.x, location.y);
switch (layoutStyle)
{
case kStretchEqualy:
{
viewSize.setWidth (maxSize.x);
viewSize.setHeight (maxSize.y);
break;
}
case kCenterEqualy:
{
CCoord diffX = (maxSize.x - viewSize.getWidth ()) / 2.;
CCoord diffY = (maxSize.y - viewSize.getHeight ()) / 2.;
viewSize.offset (diffX, diffY);
break;
}
case kRightBottomEqualy:
{
CCoord diffX = maxSize.x - viewSize.getWidth ();
CCoord diffY = maxSize.y - viewSize.getHeight ();
viewSize.offset (diffX, diffY);
break;
}
case kTopLeft:
case kTopCenter:
case kTopRight:
case kMiddleLeft:
case kMiddleCenter:
case kMiddleRight:
case kBottomLeft:
case kBottomCenter:
case kBottomRight:
{
layout.moveRect (viewSize);
break;
}
default:
break;
}
resizeSubView (view, viewSize);
if (style == kRowStyle)
{
location.y += spacing;
location.y += viewSize.getHeight ();
}
else
{
location.x += spacing;
location.x += viewSize.getWidth ();
}
});
}
//--------------------------------------------------------------------------------
void CRowColumnView::layoutViews ()
{
if (layoutGuard == false)
{
layoutGuard = true;
layoutViewsEqualSize ();
if (hideClippedSubviews ())
{
forEachChild ([&] (CView* view) {
if (view->getVisibleViewSize () != view->getViewSize ())
view->setVisible (false);
else
view->setVisible (true);
});
}
layoutGuard = false;
}
}
//--------------------------------------------------------------------------------
bool CRowColumnView::sizeToFit ()
{
if (!getChildren ().empty ())
{
CRect viewSize = getViewSize ();
CPoint maxSize;
if (style == kRowStyle)
{
forEachChild ([&] (CView* view) {
const CRect& size = view->getViewSize ();
if (size.getWidth () > maxSize.x)
maxSize.x = size.getWidth ();
maxSize.y += size.getHeight () + spacing;
});
}
else
{
forEachChild ([&] (CView* view) {
CRect size = view->getViewSize ();
maxSize.x += size.getWidth () + spacing;
if (size.bottom > maxSize.y)
maxSize.y = size.getHeight ();
});
}
viewSize.setWidth (maxSize.x + margin.left + margin.right);
viewSize.setHeight (maxSize.y + margin.top + margin.bottom);
if (viewSize != getViewSize ())
{
invalid ();
CViewContainer::setViewSize (viewSize);
CViewContainer::setMouseableArea (viewSize);
invalid ();
}
return true;
}
return false;
}
//--------------------------------------------------------------------------------
CMessageResult CRowColumnView::notify (CBaseObject* sender, IdStringPtr message)
{
if (message == kMsgViewSizeChanged)
{
if (isAttached ())
layoutViews ();
}
return CViewContainer::notify (sender, message);
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
CAutoLayoutContainerView::CAutoLayoutContainerView (const CRect& size)
: CViewContainer (size)
{
}
//--------------------------------------------------------------------------------
bool CAutoLayoutContainerView::attached (CView* parent)
{
if (!isAttached ())
{
layoutViews ();
return CViewContainer::attached (parent);
}
return false;
}
//--------------------------------------------------------------------------------
void CAutoLayoutContainerView::setViewSize (const CRect& rect, bool invalid)
{
CViewContainer::setViewSize (rect, invalid);
if (isAttached ())
layoutViews ();
}
//--------------------------------------------------------------------------------
bool CAutoLayoutContainerView::addView (CView* pView, CView* pBefore)
{
if (CViewContainer::addView (pView, pBefore))
{
if (isAttached ())
layoutViews ();
return true;
}
return false;
}
//--------------------------------------------------------------------------------
bool CAutoLayoutContainerView::removeView (CView* pView, bool withForget)
{
if (CViewContainer::removeView (pView, withForget))
{
if (isAttached ())
layoutViews ();
return true;
}
return false;
}
//--------------------------------------------------------------------------------
bool CAutoLayoutContainerView::changeViewZOrder (CView* view, uint32_t newIndex)
{
if (CViewContainer::changeViewZOrder (view, newIndex))
{
if (isAttached ())
layoutViews ();
return true;
}
return false;
}
} // VSTGUI