File: WrapPanel.cpp

package info (click to toggle)
mygui 3.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,224 kB
  • sloc: cpp: 118,031; ansic: 30,202; xml: 15,544; cs: 12,602; tcl: 776; python: 417; makefile: 34
file content (325 lines) | stat: -rw-r--r-- 7,997 bytes parent folder | download | duplicates (4)
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
/*!
	@file
	@author		Albert Semenov
	@date		02/2011
*/

#include "WrapPanel.h"

namespace MyGUI
{

	WrapPanel::WrapPanel() :
		mContentFloat(false)
	{
	}

	IntSize WrapPanel::overrideMeasure(const IntSize& _sizeAvailable)
	{
		if (mContentFloat)
			return floatMeasure(_sizeAvailable);
		return simpleMeasure(_sizeAvailable);
	}

	void WrapPanel::overrideArrange()
	{
		if (mContentFloat)
			floatArrange();
		else
			simpleArrange();
	}

	void WrapPanel::alignChildLine(size_t _startIndex, size_t _stopIndex, const IntCoord& _coordAvailable, int _lineWidth)
	{
		int left = _coordAvailable.left;
		if (mContentAlign.isHCenter())
			left = _coordAvailable.left + ((_coordAvailable.width - _lineWidth) / 2);
		else if (mContentAlign.isRight())
			left = _coordAvailable.left + (_coordAvailable.width - _lineWidth);

		for (size_t index = _startIndex; index < _stopIndex; ++ index)
		{
			Widget* child = getChildAt(index);
			IntSize size = Panel::getDesiredSize(child);

			int top = _coordAvailable.top;
			if (mContentAlign.isVCenter())
				top = _coordAvailable.top + ((_coordAvailable.height - size.height) / 2);
			else if (mContentAlign.isBottom())
				top = _coordAvailable.top + (_coordAvailable.height - size.height);

			Panel::updateArrange(child, IntCoord(left, top, size.width, size.height));
			left += size.width + mSpacer.width;
		}
	}

	Align WrapPanel::getContentAlign() const
	{
		return mContentAlign;
	}

	void WrapPanel::setContentAlign(Align _value)
	{
		mContentAlign = _value;
	}

	const IntSize& WrapPanel::getSpacer() const
	{
		return mSpacer;
	}

	void WrapPanel::setSpacer(const IntSize& _value)
	{
		mSpacer = _value;
	}

	bool WrapPanel::getContentFloat() const
	{
		return mContentFloat;
	}

	void WrapPanel::setContentFloat(bool _value)
	{
		mContentFloat = _value;
	}

	Align WrapPanel::getSnapFloat() const
	{
		return mSnapFloat;
	}

	void WrapPanel::setSnapFloat(Align _value)
	{
		mSnapFloat = _value;
	}

	IntSize WrapPanel::simpleMeasure(const IntSize& _sizeAvailable)
	{
		IntSize result;

		IntSize sizeAvailable = _sizeAvailable;
		IntPoint currentPosition;

		int maxLineHeight = 0;
		bool hasAnyWidget = false;

		size_t count = getChildCount();
		for (size_t index = 0; index < count; ++ index)
		{
			Widget* child = getChildAt(index);
			Panel::updateMeasure(child, sizeAvailable);
			IntSize size = Panel::getDesiredSize(child);

			if (((currentPosition.left + size.width) > sizeAvailable.width))
			{
				if (hasAnyWidget)
				{
					result.width = (std::max)(result.width, currentPosition.left - mSpacer.width);

					currentPosition.left = 0;
					currentPosition.top += maxLineHeight + mSpacer.height;
					maxLineHeight = 0;
				}
			}

			currentPosition.left += size.width + mSpacer.width;
			maxLineHeight = (std::max)(maxLineHeight, size.height);

			hasAnyWidget = true;
		}

		result.height = currentPosition.top + maxLineHeight;
		result.width = (std::max)(result.width, currentPosition.left);

		return result;
	}

	void WrapPanel::simpleArrange()
	{
		IntCoord coordAvailable(0, 0, getWidth(), getHeight());
		IntPoint currentPosition = coordAvailable.point();

		int maxLineHeight = 0;
		bool hasAnyWidget = false;
		size_t startLineIndex = 0;

		size_t count = getChildCount();
		for (size_t index = 0; index < count; ++ index)
		{
			Widget* child = getChildAt(index);
			IntSize size = Panel::getDesiredSize(child);

			if (((currentPosition.left + size.width) > coordAvailable.width))
			{
				if (hasAnyWidget)
				{
					alignChildLine(startLineIndex, index, IntCoord(coordAvailable.left, currentPosition.top, coordAvailable.width, maxLineHeight), currentPosition.left - mSpacer.width);

					currentPosition.left = 0;
					currentPosition.top += maxLineHeight + mSpacer.height;
					maxLineHeight = 0;

					startLineIndex = index;
				}
			}

			currentPosition.left += size.width + mSpacer.width;
			maxLineHeight = (std::max)(size.height, maxLineHeight);

			hasAnyWidget = true;
		}

		if (startLineIndex < count)
			alignChildLine(startLineIndex, count, IntCoord(coordAvailable.left, currentPosition.top, coordAvailable.width, maxLineHeight), currentPosition.left - mSpacer.width);
	}

	IntSize WrapPanel::floatMeasure(const IntSize& _sizeAvailable)
	{
		IntSize result;
		size_t count = getChildCount();

		if (count == 0)
			return result;

		Widget* child = getChildAt(0);
		Panel::updateMeasure(child, _sizeAvailable);
		IntSize firstSize = Panel::getDesiredSize(child);

		if (count == 1)
			return firstSize;

		IntSize sizeAvailable = _sizeAvailable;
		IntPoint currentPosition;

		int maxLineHeight = 0;
		bool hasAnyWidget = false;

		for (size_t index = 1; index < count; ++ index)
		{
			bool floatBehaivoir = ((firstSize.height + mSpacer.height) > currentPosition.top);

			if (floatBehaivoir)
			{
				sizeAvailable.width = _sizeAvailable.width - (firstSize.width + mSpacer.width);
			}
			else
			{
				sizeAvailable.width = _sizeAvailable.width;
			}

			Widget* child = getChildAt(index);
			Panel::updateMeasure(child, sizeAvailable);
			IntSize size = Panel::getDesiredSize(child);

			if ((currentPosition.left + size.width) > sizeAvailable.width)
			{
				if (hasAnyWidget || floatBehaivoir)
				{
					result.width = (std::max)(result.width, currentPosition.left - mSpacer.width);

					currentPosition.left = 0;
					currentPosition.top += maxLineHeight + mSpacer.height;
					maxLineHeight = 0;
				}
			}

			if (floatBehaivoir)
			{
				if (((currentPosition.left + size.width) > sizeAvailable.width))
				{
					-- index;
					continue;
				}
			}

			currentPosition.left += size.width + mSpacer.width;
			maxLineHeight = (std::max)(maxLineHeight, size.height);

			hasAnyWidget = true;
		}

		result.height = (std::max)(firstSize.height, currentPosition.top + maxLineHeight);
		result.width = (std::max)(result.width, currentPosition.left);

		return result;
	}

	void WrapPanel::floatArrange()
	{
		size_t count = getChildCount();

		if (count == 0)
			return;

		Widget* child = getChildAt(0);
		IntSize firstSize = Panel::getDesiredSize(child);
		if (mSnapFloat.isRight())
			Panel::updateArrange(child, IntCoord(getWidth() - firstSize.width, 0, firstSize.width, firstSize.height));
		else
			Panel::updateArrange(child, IntCoord(0, 0, firstSize.width, firstSize.height));

		if (count == 1)
			return;

		IntCoord coordAvailable(0, 0, getWidth(), getHeight());
		IntPoint currentPosition;

		int maxLineHeight = 0;
		bool hasAnyWidget = false;
		size_t startLineIndex = 1;

		for (size_t index = 1; index < count; ++ index)
		{
			bool floatBehaivoir = ((firstSize.height + mSpacer.height) > currentPosition.top);

			if (floatBehaivoir)
			{
				if (mSnapFloat.isRight())
					coordAvailable.left = 0;
				else
					coordAvailable.left = firstSize.width + mSpacer.width;
				coordAvailable.width = getWidth() - (firstSize.width + mSpacer.width);
			}
			else
			{
				coordAvailable.left = 0;
				coordAvailable.width = getWidth();
			}

			Widget* child = getChildAt(index);
			IntSize size = Panel::getDesiredSize(child);

			if ((currentPosition.left + size.width) > coordAvailable.width)
			{
				if (hasAnyWidget || floatBehaivoir)
				{
					alignChildLine(startLineIndex, index, IntCoord(coordAvailable.left, currentPosition.top, coordAvailable.width, maxLineHeight), currentPosition.left - mSpacer.width);

					currentPosition.left = 0;
					currentPosition.top += maxLineHeight + mSpacer.height;
					maxLineHeight = 0;

					startLineIndex = index;
				}
			}

			if (floatBehaivoir)
			{
				if (((currentPosition.left + size.width) > coordAvailable.width))
				{
					-- index;
					continue;
				}
			}

			currentPosition.left += size.width + mSpacer.width;
			maxLineHeight = (std::max)(size.height, maxLineHeight);

			hasAnyWidget = true;
		}

		if (startLineIndex < count)
			alignChildLine(startLineIndex, count, IntCoord(coordAvailable.left, currentPosition.top, coordAvailable.width, maxLineHeight), currentPosition.left - mSpacer.width);
	}

} // namespace MyGUI