1// Aseprite UI Library
2// Copyright (C) 2019 Igara Studio S.A.
3// Copyright (C) 2001-2015 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "ui/panel.h"
13
14#include "ui/resize_event.h"
15#include "ui/size_hint_event.h"
16
17#include <algorithm>
18
19namespace ui {
20
21Panel::Panel()
22 : m_multiple(false)
23{
24}
25
26void Panel::showChild(Widget* widget)
27{
28 m_multiple = false;
29 for (auto child : children()) {
30 if (!child->isDecorative())
31 child->setVisible(child == widget);
32 }
33 layout();
34}
35
36void Panel::showAllChildren()
37{
38 m_multiple = true;
39 for (auto child : children()) {
40 if (!child->isDecorative())
41 child->setVisible(true);
42 }
43 layout();
44}
45
46void Panel::onResize(ResizeEvent& ev)
47{
48 if (m_multiple) {
49 VBox::onResize(ev);
50 return;
51 }
52
53 // Copy the new position rectangle
54 setBoundsQuietly(ev.bounds());
55
56 // Set all the children to the same "cpos"
57 gfx::Rect cpos = childrenBounds();
58 for (auto child : children()) {
59 if (!child->isDecorative())
60 child->setBounds(cpos);
61 }
62}
63
64void Panel::onSizeHint(SizeHintEvent& ev)
65{
66 if (m_multiple) {
67 VBox::onSizeHint(ev);
68 return;
69 }
70
71 gfx::Size maxSize(0, 0);
72 gfx::Size reqSize;
73
74 for (auto child : children()) {
75 if (!child->isDecorative()) {
76 reqSize = child->sizeHint();
77
78 maxSize.w = std::max(maxSize.w, reqSize.w);
79 maxSize.h = std::max(maxSize.h, reqSize.h);
80 }
81 }
82
83 if (hasText())
84 maxSize.w = std::max(maxSize.w, textWidth());
85
86 ev.setSizeHint(
87 maxSize.w + border().width(),
88 maxSize.h + border().height());
89}
90
91} // namespace ui
92