Debian Codegraph @

phoenix/core/layout/horizontal-layout.cpp

raw
1 void HorizontalLayout::append(Sizable& sizable, Size size, unsigned spacing) {
2 for(auto& child : children) if(child.sizable == &sizable) return;
3 children.append({&sizable, size.width, size.height, spacing});
4 synchronizeLayout();
5 if(window()) window()->synchronizeLayout();
6 }
7
8 void HorizontalLayout::append(Sizable& sizable) {
9 for(auto& child : children) if(child.sizable == &sizable) return;
10 Layout::append(sizable);
11 if(window()) window()->synchronizeLayout();
12 }
13
14 Size HorizontalLayout::minimumSize() {
15 unsigned width = 0, height = 0;
16
17 for(auto& child : children) {
18 width += child.spacing;
19 if(child.width == Size::Minimum || child.width == Size::Maximum) {
20 width += child.sizable->minimumSize().width;
21 continue;
22 }
23 width += child.width;
24 }
25
26 for(auto& child : children) {
27 if(child.height == Size::Minimum || child.height == Size::Maximum) {
28 height = max(height, child.sizable->minimumSize().height);
29 continue;
30 }
31 height = max(height, child.height);
32 }
33
34 return {state.margin * 2 + width, state.margin * 2 + height};
35 }
36
37 void HorizontalLayout::remove(Sizable& sizable) {
38 for(unsigned n = 0; n < children.size(); n++) {
39 if(children[n].sizable == &sizable) {
40 if(dynamic_cast<Layout*>(children[n].sizable)) {
41 Layout* layout = (Layout*)children[n].sizable;
42 layout->reset();
43 }
44 children.remove(n);
45 Layout::remove(sizable);
46 if(window()) window()->synchronizeLayout();
47 break;
48 }
49 }
50 }
51
52 void HorizontalLayout::reset() {
53 for(auto& child : children) {
54 if(window() && dynamic_cast<Layout*>(child.sizable)) ((Layout*)child.sizable)->reset();
55 if(window() && dynamic_cast<Widget*>(child.sizable)) window()->remove((Widget&)*child.sizable);
56 }
57 }
58
59 void HorizontalLayout::setAlignment(double alignment) {
60 state.alignment = max(0.0, min(1.0, alignment));
61 }
62
63 void HorizontalLayout::setEnabled(bool enabled) {
64 Sizable::state.enabled = enabled;
65 for(auto& child : children) {
66 child.sizable->setEnabled(child.sizable->enabled());
67 }
68 }
69
70 void HorizontalLayout::setGeometry(Geometry containerGeometry) {
71 auto children = this->children;
72 for(auto& child : children) {
73 if(child.width == Size::Minimum) child.width = child.sizable->minimumSize().width;
74 if(child.height == Size::Minimum) child.height = child.sizable->minimumSize().height;
75 }
76
77 Geometry geometry = containerGeometry;
78 geometry.x += state.margin;
79 geometry.y += state.margin;
80 geometry.width -= state.margin * 2;
81 geometry.height -= state.margin * 2;
82
83 unsigned minimumWidth = 0, maximumWidthCounter = 0;
84 for(auto& child : children) {
85 if(child.width == Size::Maximum) maximumWidthCounter++;
86 if(child.width != Size::Maximum) minimumWidth += child.width;
87 minimumWidth += child.spacing;
88 }
89
90 for(auto& child : children) {
91 if(child.width == Size::Maximum) child.width = (geometry.width - minimumWidth) / maximumWidthCounter;
92 if(child.height == Size::Maximum) child.height = geometry.height;
93 }
94
95 unsigned maximumHeight = 0;
96 for(auto& child : children) maximumHeight = max(maximumHeight, child.height);
97
98 for(auto& child : children) {
99 unsigned pivot = (maximumHeight - child.height) * state.alignment;
100 Geometry childGeometry = {geometry.x, geometry.y + pivot, child.width, child.height};
101 if((signed)childGeometry.width < 1) childGeometry.width = 1;
102 if((signed)childGeometry.height < 1) childGeometry.height = 1;
103 child.sizable->setGeometry(childGeometry);
104
105 geometry.x += child.width + child.spacing;
106 geometry.width -= child.width + child.spacing;
107 }
108 }
109
110 void HorizontalLayout::setMargin(unsigned margin) {
111 state.margin = margin;
112 }
113
114 void HorizontalLayout::setVisible(bool visible) {
115 Sizable::state.visible = visible;
116 for(auto& child : children) {
117 child.sizable->setVisible(child.sizable->visible());
118 }
119 }
120
121 void HorizontalLayout::synchronizeLayout() {
122 for(auto& child : children) Layout::append(*child.sizable);
123 }
124
125 HorizontalLayout::~HorizontalLayout() {
126 while(children.size()) remove(*children[0].sizable);
127 }