-
Notifications
You must be signed in to change notification settings - Fork 10
/
GSAutoLayoutStandardManager.j
executable file
·355 lines (289 loc) · 8.56 KB
/
GSAutoLayoutStandardManager.j
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
/* -*-objc-*-
Author: Nicola Pero <[email protected]>
Date: January 2003
Author of Cappuccino port: Daniel Boehringer (2012)
This file is part of GNUstep Renaissance
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
@import "GSAutoLayoutManager.j"
@implementation GSAutoLayoutStandardManager : GSAutoLayoutManager
{
/* Cached number of line parts with expand set to YES. Computed
* during minimum layout. */
int _numberOfExpandingLineParts;
}
- (id) init
{
return [super init];
}
- (void) dealloc
{
[super dealloc];
}
- (BOOL) internalUpdateMinimumLayout
{
/* Identify the special segments with span != 1 and store
* them in this array.
*/
var specialSegments = [CPMutableArray new];
var e = [_lines objectEnumerator];
var line;
while ((line = [e nextObject]) != nil)
{
var i, count = [line._segments count];
for (i = 0; i < count; i++)
{
var segment;
segment = [line._segments objectAtIndex: i];
if (segment._span > 1)
{
[specialSegments addObject: segment];
}
}
}
[self internalUpdateLineParts];
/* We now work on the _minimumLayout of the line parts; please note
* that we only work on the length for now, and ignore the
* position. */
/* If there are any minimumLength set for line parts, set them as
* initial lengths for them. */
{
var i, count = [_lineParts count];
for (i = 0; i < count; i++)
{
var linePart;
var linePartInfo;
linePart = [_lineParts objectAtIndex: i];
linePartInfo = linePart._info;
if (linePartInfo != nil)
{
(linePart._minimumLayout).length = linePartInfo._minimumLength;
/* Since we're there, also set the expand flag. Just in
* case we never work on this line part again. */
if (linePartInfo._alwaysExpands == YES)
{
linePart._expands = YES;
}
}
}
}
/* Now determine the minimum length of each line part, ignoring
* special segments. */
e = [_lines objectEnumerator];
while ((line = [e nextObject]) != nil)
{
var i, count = [line._segments count];
for (i = 0; i < count; i++)
{
var segment;
segment = [line._segments objectAtIndex: i];
if (segment._span > 1)
{
/* ignore (for now - it will be dealt with in the
* second loop below). */
}
else
{
/* Update autolayout information on this line part:
* consider the existing _minimumLayout.length, the
* minimLength of this segment, and any _minimumLength
* set for the line part. Also update information on
* the expand flag.
*/
var linePart;
var minLinePartLength;
linePart = [_lineParts objectAtIndex: segment._linePart];
minLinePartLength = segment._minBorder
+ segment._minimumContentsLength
+ segment._maxBorder;
minLinePartLength = Math.max ((linePart._minimumLayout).length,
minLinePartLength);
if (segment._alignment == GSAutoLayoutExpand
|| segment._alignment == GSAutoLayoutWeakExpand)
{
linePart._expands = YES;
}
/* Use any special information that was set for this
* line part. */
{
var info;
info = linePart._info;
if (info != nil)
{
/* info._minimumLength and info._alwaysExpand
* have already been dealt with. */
if (info._neverExpands == YES)
{
linePart._expands = NO;
}
}
}
(linePart._minimumLayout).length = minLinePartLength;
}
}
}
/* Now work out what to do with the special segments. */
{
var i, count = [specialSegments count];
for (i = 0; i < count; i++)
{
var segment;
var j;
var length = 0;
var linePartsWhichExpand = 0;
var segmentMinimumLength;
segment = [specialSegments objectAtIndex: i];
segmentMinimumLength = segment._minBorder
+ segment._minimumContentsLength + segment._maxBorder;
/* Compute the (current) total length of the line parts
* spanned by this segment. */
for (j = 0; j < segment._span; j++)
{
var linePart;
linePart = [_lineParts objectAtIndex: segment._linePart + j];
length += (linePart._minimumLayout).length;
if (linePart._expands)
{
linePartsWhichExpand++;
}
}
/* If it's not enough to display the segment, expand the
* line parts. */
if (length < segmentMinimumLength)
{
/* If some line parts are marked as expanding, expand them
* rather than the line parts not marked as expanding. */
if (linePartsWhichExpand > 0)
{
var enlargeBy = (segmentMinimumLength - length)
/ linePartsWhichExpand;
for (j = 0; j < segment._span; j++)
{
var linePart;
linePart = [_lineParts objectAtIndex: segment._linePart + j];
if (linePart._expands)
{
(linePart._minimumLayout).length += enlargeBy;
}
}
}
else
{
/* Else expands all line parts of the same amount to
* distribute the ugliness on all line parts. */
var enlargeBy = (segmentMinimumLength - length)
/ segment._span;
for (j = 0; j < segment._span; j++)
{
var linePart;
linePart = [_lineParts objectAtIndex: segment._linePart + j];
(linePart._minimumLayout).length += enlargeBy;
}
}
}
/* If the segment might need to expand in the future, but no
* line parts expand, then mark them all as expanding to
* distribute the ugliness between all of them. */
if ((segment._alignment == GSAutoLayoutExpand
|| segment._alignment == GSAutoLayoutWeakExpand)
&& linePartsWhichExpand == 0)
{
for (j = 0; j < segment._span; j++)
{
var linePart;
linePart = [_lineParts objectAtIndex: segment._linePart + j];
linePart._expands = YES;
}
}
}
}
/* First, compute the _minimumLayout.position of all line parts. */
{
var position = 0;
var i, count = [_lineParts count];
for (i = 0; i < count; i++)
{
var linePart;
linePart = [_lineParts objectAtIndex: i];
(linePart._minimumLayout).position = position;
position += (linePart._minimumLayout).length;
}
_minimumLength = position;
}
/* Then, propagate the minimum layout to all segments. */
[self internalUpdateSegmentsMinimumLayoutFromLineParts];
/* Cache the number of expanding line parts. */
_numberOfExpandingLineParts = 0;
{
var i, count = [_lineParts count];
for (i = 0; i < count; i++)
{
var linePart = [_lineParts objectAtIndex: i];
if (linePart._expands)
{
_numberOfExpandingLineParts++;
}
}
}
/* TODO - really check if something changed or not and return NO if
* not. */
return YES;
}
- (BOOL) internalUpdateLayout
{
var enlargeBy;
if (_length < _minimumLength)
{
/* We are being constrained below our minimum size ... adopt the
* minimum layout for views. */
enlargeBy = 0;
}
else
{
if (_numberOfExpandingLineParts == 0)
{
/* OK - we have no line parts that want to expand. We just
* do nothing. The views will be displayed in their minimum
* layout, and the rest of the space will be empty. */
enlargeBy = 0;
}
else
{
enlargeBy = (_length - _minimumLength) / _numberOfExpandingLineParts;
}
}
{
var i, numberOfLineParts = [_lineParts count];
var positionDrift = 0;
for (i = 0; i < numberOfLineParts; i++)
{
var linePart = [_lineParts objectAtIndex: i];
(linePart._layout).position = (linePart._minimumLayout).position + positionDrift;
if (linePart._expands)
{
(linePart._layout).length = (linePart._minimumLayout).length + enlargeBy;
positionDrift += enlargeBy;
}
else
{
(linePart._layout).length = (linePart._minimumLayout).length;
}
}
}
[self internalUpdateSegmentsLayoutFromLineParts];
/* TODO - only return YES if something changed in the layout ! */
/* Idea - the superclass could check ? */
return YES;
}
@end