comparison DPF-Prymula-audioplugins/dpf/dgl/src/Layout.cpp @ 3:84e66ea83026

DPF-Prymula-audioplugins-0.231015-2
author prymula <prymula76@outlook.com>
date Mon, 16 Oct 2023 21:53:34 +0200
parents
children
comparison
equal deleted inserted replaced
2:cf2cb71d31dd 3:84e66ea83026
1 /*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any purpose with
6 * or without fee is hereby granted, provided that the above copyright notice and this
7 * permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include "../Layout.hpp"
18 #include "../SubWidget.hpp"
19
20 START_NAMESPACE_DGL
21
22 typedef std::list<SubWidgetWithSizeHint>::iterator SubWidgetWithSizeHintIterator;
23 typedef std::list<HorizontalLayout*>::iterator HorizontalLayoutIterator;
24 typedef std::list<VerticalLayout*>::iterator VerticalLayoutIterator;
25
26 // --------------------------------------------------------------------------------------------------------------------
27
28 template<> // horizontal
29 uint Layout<true>::setAbsolutePos(int x, const int y, const uint padding)
30 {
31 uint maxHeight = 0;
32
33 for (SubWidgetWithSizeHintIterator it=widgets.begin(), end=widgets.end(); it != end; ++it)
34 {
35 SubWidgetWithSizeHint& s(*it);
36 maxHeight = std::max(maxHeight, s.widget->getHeight());
37 s.widget->setAbsolutePos(x, y);
38 x += s.widget->getWidth();
39 x += padding;
40 }
41
42 return maxHeight;
43 }
44
45 template<> // vertical
46 uint Layout<false>::setAbsolutePos(const int x, int y, const uint padding)
47 {
48 uint maxWidth = 0;
49
50 for (SubWidgetWithSizeHintIterator it=widgets.begin(), end=widgets.end(); it != end; ++it)
51 {
52 SubWidgetWithSizeHint& s(*it);
53 maxWidth = std::max(maxWidth, s.widget->getWidth());
54 s.widget->setAbsolutePos(x, y);
55 y += s.widget->getHeight();
56 y += padding;
57 }
58
59 return maxWidth;
60 }
61
62 template<> // horizontal
63 void Layout<true>::setSize(const uint width, const uint padding)
64 {
65 uint maxHeight = 0;
66 uint nonFixedWidth = width;
67 uint numDynamiclySizedWidgets = 0;
68
69 for (SubWidgetWithSizeHintIterator it=widgets.begin(), end=widgets.end(); it != end; ++it)
70 {
71 SubWidgetWithSizeHint& s(*it);
72 maxHeight = std::max(maxHeight, s.widget->getHeight());
73
74 if (s.sizeHint == Fixed)
75 nonFixedWidth -= s.widget->getWidth();
76 else
77 ++numDynamiclySizedWidgets;
78 }
79
80 if (const size_t numWidgets = widgets.size())
81 nonFixedWidth -= padding * (numWidgets - 1);
82
83 const uint widthPerWidget = numDynamiclySizedWidgets != 0 ? nonFixedWidth / numDynamiclySizedWidgets : 0;
84
85 for (SubWidgetWithSizeHintIterator it=widgets.begin(), end=widgets.end(); it != end; ++it)
86 {
87 SubWidgetWithSizeHint& s(*it);
88 if (s.sizeHint != Fixed)
89 s.widget->setSize(widthPerWidget, maxHeight);
90 else
91 s.widget->setHeight(maxHeight);
92 }
93 }
94
95 template<> // vertical
96 void Layout<false>::setSize(const uint height, const uint padding)
97 {
98 uint biggestWidth = 0;
99 uint nonFixedHeight = height;
100 uint numDynamiclySizedWidgets = 0;
101
102 for (SubWidgetWithSizeHintIterator it=widgets.begin(), end=widgets.end(); it != end; ++it)
103 {
104 SubWidgetWithSizeHint& s(*it);
105 biggestWidth = std::max(biggestWidth, s.widget->getWidth());
106
107 if (s.sizeHint == Fixed)
108 nonFixedHeight -= s.widget->getHeight();
109 else
110 ++numDynamiclySizedWidgets;
111 }
112
113 if (const size_t numWidgets = widgets.size())
114 nonFixedHeight -= padding * (numWidgets - 1);
115
116 const uint heightPerWidget = numDynamiclySizedWidgets != 0 ? nonFixedHeight / numDynamiclySizedWidgets : 0;
117
118 for (SubWidgetWithSizeHintIterator it=widgets.begin(), end=widgets.end(); it != end; ++it)
119 {
120 SubWidgetWithSizeHint& s(*it);
121 if (s.sizeHint != Fixed)
122 s.widget->setSize(biggestWidth, heightPerWidget);
123 else
124 s.widget->setWidth(biggestWidth);
125 }
126 }
127
128 // --------------------------------------------------------------------------------------------------------------------
129
130 /* TODO
131 void HorizontallyStackedVerticalLayout::adjustSize(const uint padding)
132 {
133 }
134 */
135
136 void HorizontallyStackedVerticalLayout::setAbsolutePos(int x, const int y, const uint padding)
137 {
138 for (VerticalLayoutIterator it=items.begin(), end=items.end(); it != end; ++it)
139 {
140 VerticalLayout* l(*it);
141 x += l->setAbsolutePos(x, y, padding);
142 x += padding;
143 }
144 }
145
146 // --------------------------------------------------------------------------------------------------------------------
147
148 Size<uint> VerticallyStackedHorizontalLayout::adjustSize(const uint padding)
149 {
150 uint biggestWidth = 0;
151 uint totalHeight = 0;
152
153 // iterate all widgets to find which one is the biggest (horizontally)
154 for (HorizontalLayoutIterator it=items.begin(), end=items.end(); it != end; ++it)
155 {
156 HorizontalLayout* const l(*it);
157 uint width = 0;
158 uint height = 0;
159
160 for (SubWidgetWithSizeHintIterator it=l->widgets.begin(), end=l->widgets.end(); it != end; ++it)
161 {
162 SubWidgetWithSizeHint& s(*it);
163
164 if (width != 0)
165 width += padding;
166
167 width += s.widget->getWidth();
168 height = std::max(height, s.widget->getHeight());
169 }
170
171 biggestWidth = std::max(biggestWidth, width);
172
173 if (totalHeight != 0)
174 totalHeight += padding;
175
176 totalHeight += height;
177 }
178
179 // now make all horizontal lines the same width
180 for (HorizontalLayoutIterator it=items.begin(), end=items.end(); it != end; ++it)
181 {
182 HorizontalLayout* const l(*it);
183 l->setSize(biggestWidth, padding);
184 }
185
186 return Size<uint>(biggestWidth, totalHeight);
187 }
188
189 void VerticallyStackedHorizontalLayout::setAbsolutePos(const int x, int y, const uint padding)
190 {
191 for (HorizontalLayoutIterator it=items.begin(), end=items.end(); it != end; ++it)
192 {
193 HorizontalLayout* l(*it);
194 y += l->setAbsolutePos(x, y, padding);
195 y += padding;
196 }
197 }
198
199 // --------------------------------------------------------------------------------------------------------------------
200
201 END_NAMESPACE_DGL