File: GSAutoLayoutStandardManager.m

package info (click to toggle)
renaissance 0.8.0-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,484 kB
  • ctags: 389
  • sloc: objc: 10,524; makefile: 64; perl: 19; sh: 12
file content (431 lines) | stat: -rw-r--r-- 10,827 bytes parent folder | download | duplicates (2)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* -*-objc-*-
   GSAutoLayoutStandardManager.m

   Copyright (C) 2002 Free Software Foundation, Inc.

   Author: Nicola Pero <n.pero@mi.flashnet.it>
   Date: April 2002

   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.
*/

#include "GSAutoLayoutStandardManager.h"
#include "GSAutoLayoutManagerPrivate.h"

#ifndef GNUSTEP
# include <Foundation/Foundation.h>
#else
# include <Foundation/NSArray.h>
# include <Foundation/NSEnumerator.h>
# include <Foundation/NSSet.h>
# include <Foundation/NSString.h>
# include <Foundation/NSValue.h>
#endif

/* for ceil */
#include <math.h>

#define min(X, Y)  ((X) < (Y) ? (X) : (Y))
#define max(X, Y)  ((X) < (Y) ? (Y) : (X))

@interface GSAutoLayoutManagerColumn : NSObject
{
@public
  BOOL _expand;
  float _minimumLength;
  float _length;
}
@end

@implementation GSAutoLayoutManagerColumn
@end

@implementation GSAutoLayoutStandardManager

- (id) init
{
  _columns = [NSMutableArray new];
  return [super init];
}

- (void) dealloc
{
  RELEASE (_columns);
  [super dealloc];
}

- (BOOL) internalUpdateMinimumLayout
{
  /* Determine the number of columns.  */
  NSEnumerator *e = [_lines objectEnumerator];
  GSAutoLayoutManagerLine *line;
  int i, numberOfColumns = 0;
  /* Arrays holding the lines/segments with _span != 1, so that we can
   * later on manage them.  */
  NSMutableArray *specialSegments = [NSMutableArray new];
  NSMutableArray *specialSegmentIndexes = [NSMutableArray new];

  [_columns removeAllObjects];
  
  while ((line = [e nextObject]) != nil) 
    {
      int columns = 0;
      int count = [line->_segments count];

      for (i = 0; i < count; i++)
	{
	  GSAutoLayoutManagerSegment *segment;

	  segment = [line->_segments objectAtIndex: i];
	  if (segment->_span > 1)
	    {
	      [specialSegments addObject: segment];
	      [specialSegmentIndexes addObject: 
				       [NSNumber numberWithInt: columns]];
	    }
	  columns += segment->_span;
	}
      numberOfColumns = max(columns, numberOfColumns);
    }

  for (i = 0; i < numberOfColumns; i++)
    {
      GSAutoLayoutManagerColumn *c = [GSAutoLayoutManagerColumn new];
      [_columns addObject: c];
      RELEASE (c);
    }
  
  /* Now determine the minimum length of each column, ignoring
   * special segments.  */
  e = [_lines objectEnumerator];
  
  while ((line = [e nextObject]) != nil) 
    {
      int j, count = [line->_segments count];

      /* i holds the segment number, and j the column number.
       * If the segment always span a single column, i == j;
       * otherwise, j can be bigger than i.  */
      for (i = j = 0; i < count; i++)
	{
	  GSAutoLayoutManagerSegment *segment;

	  segment = [line->_segments objectAtIndex: i];

	  if (segment->_span > 1)
	    {
	      /* ignore.  */
	    }
	  else
	    {
	      GSAutoLayoutManagerColumn *column;
	      float minSegmentLength;
	      
	      minSegmentLength = segment->_minBorder 
		+ segment->_minimumContentsLength 
		+ segment->_maxBorder;

	      column = [_columns objectAtIndex: j];
	      column->_minimumLength = max(column->_minimumLength, 
					   minSegmentLength);
	      if (segment->_alignment == GSAutoLayoutExpand
		  || segment->_alignment == GSAutoLayoutWeakExpand)
		{
		  column->_expand = YES;
		}
	    }

	  j += segment->_span;
	}
    }

  /* Now take into account special segments.  */
  {
    int count = [specialSegments count];

    for (i = 0; i < count; i++)
      {
	GSAutoLayoutManagerSegment *segment;
	int j, index;
	float length = 0;
	int columnsWhichExpand = 0;
	float segmentMinimumLength;

	segment = [specialSegments objectAtIndex: i];
	index = [(NSNumber *)[specialSegmentIndexes objectAtIndex: i] 
			     intValue];

	segmentMinimumLength = segment->_minBorder 
	  + segment->_minimumContentsLength + segment->_maxBorder;
	
	/* Compute the total length of the columns spanned by this
	 * segment.  */
	for (j = 0; j < segment->_span; j++)
	  {
	    GSAutoLayoutManagerColumn *column;

	    column = [_columns objectAtIndex: index + j];
	    length += column->_minimumLength;
	    if (column->_expand)
	      {
		columnsWhichExpand++;
	      }
	  }

	/* If it's not enough to display the segment, expand the
	 * columns.  */
	if (length < segmentMinimumLength)
	  {
	    /* If some columns are marked as expanding, expand them rather
	     * than the columns not marked as expanding.  */
	    if (columnsWhichExpand > 0)
	      {
		float enlargeBy = (segmentMinimumLength - length) 
		  / columnsWhichExpand;

		for (j = 0; j < segment->_span; j++)
		  {
		    GSAutoLayoutManagerColumn *column;
		   
		    column = [_columns objectAtIndex: index + j];
		    if (column->_expand)
		      {
			column->_minimumLength += enlargeBy;
		      }
		  }
	      }
	    else
	      {
		/* Else expands all columns of the same amount to
		 * distribute the ugliness on all columns.  */
		float enlargeBy = (segmentMinimumLength - length) 
		  / segment->_span;

		for (j = 0; j < segment->_span; j++)
		  {
		    GSAutoLayoutManagerColumn *column;
		    
		    column = [_columns objectAtIndex: index + j];
		    column->_minimumLength += enlargeBy;
		  }
	      }
	  }

	/* If the segment needs to expand, but unfortunately no column
	 * expands ... */
	if ((segment->_alignment == GSAutoLayoutExpand
	     || segment->_alignment == GSAutoLayoutWeakExpand)
	    &&  columnsWhichExpand == 0)
	  {
	    /* Then mark all columns as expanding to distribute the
	     * ugliness between all the columns.  */
	    for (j = 0; j < segment->_span; j++)
	      {
		GSAutoLayoutManagerColumn *column;
		
		column = [_columns objectAtIndex: index + j];
		column->_expand = YES;
	      }
	  }
      }
  }
  
  /* Now do the minimum layout.  */
  _minimumLength = 0;

  e = [_lines objectEnumerator];
  
  while ((line = [e nextObject]) != nil) 
    {
      int j, count = [line->_segments count];
      float lineLength = 0;

      /* i holds the segment number, and j the column number.  */
      for (i = j = 0; i < count; i++)
	{
	  GSAutoLayoutManagerSegment *segment;
	  int startColumn = j;

	  segment = [line->_segments objectAtIndex: i];

	  (segment->_minimumLayout).position = lineLength;
	  (segment->_minimumLayout).length = 0;

	  for (; j < startColumn + segment->_span; j++)
	    {
	      GSAutoLayoutManagerColumn *column;
	      
	      column = [_columns objectAtIndex: j];
	      
	      (segment->_minimumLayout).length += column->_minimumLength;
	    }
	  
	  lineLength += (segment->_minimumLayout).length;

	  /* We do not need to layout segment contents inside the
	     segment in the minimum layout ... it's not needed.  */
	}
      _minimumLength = max(lineLength, _minimumLength);
    }

  /* Cache the number of expanding columns.  */
  _numberOfExpandingColumns = 0;

  for (i = 0; i < numberOfColumns; i++)
    {
      GSAutoLayoutManagerColumn *column = [_columns objectAtIndex: i];
      
      if (column->_expand)
	{
	  _numberOfExpandingColumns++;
	}
    }
  
  /* TODO - really check if something changed or not and return NO if
   * not.  */
  return YES;
}


- (BOOL) internalUpdateLayout
{
  NSEnumerator *e;
  GSAutoLayoutManagerLine *line;
  float enlargeBy;
  int i, numberOfColumns = [_columns count];

  if (_length < _minimumLength)
    {
      /* We are being constrained below our minimum size ... adopt the
       * minimum layout for views.  */
      enlargeBy = 0;
    }
  else
    {
      if (_numberOfExpandingColumns == 0)
	{
	  enlargeBy = 0;
	}
      else
	{
	  enlargeBy = (_length - _minimumLength) / _numberOfExpandingColumns;
	}
    }

  for (i = 0; i < numberOfColumns; i++)
    {
      GSAutoLayoutManagerColumn *column = [_columns objectAtIndex: i];
      
      if (column->_expand)
	{
	  column->_length = column->_minimumLength + enlargeBy;
	}
      else
	{
	  column->_length = column->_minimumLength;	  
	}
    }

  /* Now do the layout.  */
  e = [_lines objectEnumerator];
  
  while ((line = [e nextObject]) != nil) 
    {
      int j, count = [line->_segments count];
      float lineLength = 0;

      /* i holds the segment number, and j the column number.  */
      for (i = j = 0; i < count; i++)
	{
	  GSAutoLayoutManagerSegment *segment;
	  int startColumn = j;
	  GSAutoLayoutSegmentLayout s;

	  segment = [line->_segments objectAtIndex: i];

	  (segment->_layout).position = lineLength;
	  (segment->_layout).length = 0;

	  for (; j < startColumn + segment->_span; j++)
	    {
	      GSAutoLayoutManagerColumn *column;
	      
	      column = [_columns objectAtIndex: j];
	      
	      (segment->_layout).length += column->_length;
	    }
	  
	  lineLength += (segment->_layout).length;

	  /* Now place the segment contents inside the segment.  */
	  
	  /* First, start with the segment, then remove the fixed
	   * borders.  */
	  s = segment->_layout;
	  
	  s.position += segment->_minBorder;
	  s.length -= segment->_minBorder + segment->_maxBorder;

	  /* Now, align the segment contents in the resulting space.  */
	  switch (segment->_alignment)
	    {
	    case GSAutoLayoutExpand:
      	    case GSAutoLayoutWeakExpand:
	      break;
	    case GSAutoLayoutAlignMin:
	      s.length = segment->_minimumContentsLength;
	      break;
	    case GSAutoLayoutAlignMax:
	      s.position += s.length - segment->_minimumContentsLength;
	      s.length = segment->_minimumContentsLength;
	      break;
	    case GSAutoLayoutAlignCenter:
	    default:
	      s.position += ((s.length - segment->_minimumContentsLength) / 2);
	      s.length = segment->_minimumContentsLength;
	      break;
	    }
	  /* Save the results of our computations.  */
	  segment->_contentsLayout = s;
	}
    }

  /* TODO - only return YES if something changed in the layout ! */
  return YES;
}

- (void) setMinimumLength: (float)min
		alignment: (GSAutoLayoutAlignment)flag
		minBorder: (float)minBorder
		maxBorder: (float)maxBorder
		     span: (float)span
	 ofSegmentAtIndex: (int)segment
		   inLine: (id)line
{
  [super setMinimumLength: min
	 alignment: flag
	 minBorder: minBorder
	 maxBorder: maxBorder
	 span: ceilf (span)
	 ofSegmentAtIndex: segment
	 inLine: line];
}


@end