File: NSCollectionViewFlowLayout.m

package info (click to toggle)
gnustep-gui 0.32.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,348 kB
  • sloc: objc: 178,763; ansic: 24,089; cpp: 664; yacc: 464; sh: 90; makefile: 72
file content (518 lines) | stat: -rw-r--r-- 14,927 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/* Implementation of class NSCollectionViewFlowLayout
   Copyright (C) 2021 Free Software Foundation, Inc.
   
   By: Gregory John Casamento
   Date: 30-05-2021

   This file is part of the GNUstep Library.
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 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
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110 USA.
*/

#import <Foundation/NSIndexPath.h>

#import "AppKit/NSCollectionView.h"
#import "AppKit/NSCollectionViewFlowLayout.h"
#import "AppKit/NSCollectionViewItem.h"

#import "GSGuiPrivate.h"

@implementation NSCollectionViewFlowLayoutInvalidationContext

- (instancetype) init
{
  self = [super init];
  if (self != nil)
    {
      _invalidateFlowLayoutAttributes = YES;
      _invalidateFlowLayoutDelegateMetrics = YES;
    }
  return self;
}

- (void) setInvalidateFlowLayoutDelegateMetrics: (BOOL)flag
{
  _invalidateFlowLayoutDelegateMetrics = flag;
}

- (BOOL) invalidateFlowLayoutDelegateMetrics
{
  return _invalidateFlowLayoutDelegateMetrics;
}

- (void) setInvalidateFlowLayoutAttributes: (BOOL)flag
{
  _invalidateFlowLayoutAttributes = flag;
}

- (BOOL) invalidateFlowLayoutAttributes
{
  return _invalidateFlowLayoutAttributes;
}

@end

@implementation NSCollectionViewFlowLayout

- (instancetype) init
{
  self = [super init];
  if (self != nil)
    {
      _collapsedSections = [[NSMutableIndexSet alloc] init];
    }
  return self;
}

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

- (id) initWithCoder: (NSCoder *)coder
{
  self = [super initWithCoder: coder];
  if (self)
    {
      _collapsedSections = [[NSMutableIndexSet alloc] init];
      if ([coder allowsKeyedCoding])
        {
          if ([coder containsValueForKey: @"NSMinimumLineSpacing"])
            {
              _minimumLineSpacing = [coder decodeFloatForKey: @"NSMinimumLineSpacing"];
            }
          if ([coder containsValueForKey: @"NSMinimumInteritemSpacing"])
            {
              _minimumInteritemSpacing = [coder decodeFloatForKey: @"NSMinimumInteritemSpacing"];
            }
          if ([coder containsValueForKey: @"NSItemSize"])
            {
              _itemSize = [coder decodeSizeForKey: @"NSItemSize"];
            }
          if ([coder containsValueForKey: @"NSEstimatedItemSize"])
            {
              _estimatedItemSize = [coder decodeSizeForKey: @"NSEstimatedItemSize"];
            }
          if ([coder containsValueForKey: @"NSScrollDirection"])
            {
              _scrollDirection = [coder decodeIntForKey: @"NSScrollDirection"];
            }
          if ([coder containsValueForKey: @"NSHeaderReferenceSize"])
            {
              _headerReferenceSize = [coder decodeSizeForKey: @"NSHeaderReferneceSize"];
            }
          if ([coder containsValueForKey: @"NSFooterReferenceSize"])
            {
              _footerReferenceSize = [coder decodeSizeForKey: @"NSFooterReferenceSize"];
            }

          // decode inset...
          if ([coder containsValueForKey: @"NSSectionInset.bottom"])
            {
              _sectionInset.bottom = [coder decodeFloatForKey: @"NSSectionInset.bottom"];
            }
          if ([coder containsValueForKey: @"NSSectionInset.top"])
            {
              _sectionInset.top = [coder decodeFloatForKey: @"NSSectionInset.top"];
            }
          if ([coder containsValueForKey: @"NSSectionInset.left"])
            {
              _sectionInset.left = [coder decodeFloatForKey: @"NSSectionInset.left"];
            }
          if ([coder containsValueForKey: @"NSSectionInset.right"])
            {
              _sectionInset.right = [coder decodeFloatForKey: @"NSSectionInset.right"];
            }
          
          if ([coder containsValueForKey: @"NSSectionHeadersPinToVisibleBounds"])
            {
              _sectionHeadersPinToVisibleBounds = [coder decodeBoolForKey: @"NSSectionHeadersPinToVisibleBounds"];
            }
          if ([coder containsValueForKey: @"NSSectionFootersPinToVisibleBounds"])
            {
              _sectionFootersPinToVisibleBounds = [coder decodeBoolForKey: @"NSSectionFootersPinToVisibleBounds"];
            }
        }
      else
        {
          [coder decodeValueOfObjCType: @encode(CGFloat)
                                    at: &_minimumInteritemSpacing];
          _itemSize = [coder decodeSize];
          _estimatedItemSize = [coder decodeSize];
          decode_NSInteger(coder, &_scrollDirection);
          _headerReferenceSize = [coder decodeSize];
          _footerReferenceSize = [coder decodeSize];
          
          // decode inset...
          [coder decodeValueOfObjCType: @encode(CGFloat)
                                    at: &_sectionInset.bottom];
          [coder decodeValueOfObjCType: @encode(CGFloat)
                                    at: &_sectionInset.top];
          [coder decodeValueOfObjCType: @encode(CGFloat)
                                    at: &_sectionInset.left];
          [coder decodeValueOfObjCType: @encode(CGFloat)
                                    at: &_sectionInset.right];
          
          
          [coder decodeValueOfObjCType: @encode(BOOL)
                                    at: &_sectionHeadersPinToVisibleBounds];
          [coder decodeValueOfObjCType: @encode(BOOL)
                                    at: &_sectionFootersPinToVisibleBounds];
        }
    }
  return self;
}

- (void) encodeWithCoder: (NSCoder *)coder
{
  if ([coder allowsKeyedCoding])
    {
      [coder encodeFloat: _minimumLineSpacing
                  forKey: @"NSMinimumLineSpacing"];
      [coder encodeFloat: _minimumInteritemSpacing
                  forKey: @"NSMinimumInteritemSpacing"];
      [coder encodeSize: _itemSize
                 forKey: @"NSItemSize"];
      [coder encodeSize: _estimatedItemSize
                 forKey: @"NSEstimatedItemSize"];
      [coder encodeInt: _scrollDirection
                forKey: @"NSScrollDirection"];
      [coder encodeSize: _headerReferenceSize
                 forKey: @"NSHeaderReferneceSize"];
      [coder encodeSize: _footerReferenceSize
                 forKey: @"NSFooterReferenceSize"];
      
      // decode inset...
      [coder encodeFloat: _sectionInset.bottom
                  forKey: @"NSSectionInset.bottom"];
      [coder encodeFloat: _sectionInset.top
                  forKey: @"NSSectionInset.top"];
      [coder encodeFloat: _sectionInset.left
                  forKey: @"NSSectionInset.left"];
      [coder encodeFloat: _sectionInset.right
                  forKey: @"NSSectionInset.right"];

      [coder encodeBool: _sectionHeadersPinToVisibleBounds
                 forKey: @"NSSectionHeadersPinToVisibleBounds"];
      [coder encodeBool: _sectionFootersPinToVisibleBounds
                 forKey: @"NSSectionFootersPinToVisibleBounds"];
    }
  else
    {
      [coder encodeValueOfObjCType: @encode(CGFloat)
                                at: &_minimumInteritemSpacing];
      [coder encodeSize: _itemSize];
      [coder encodeSize: _estimatedItemSize];
      encode_NSInteger(coder, &_scrollDirection);
      [coder encodeSize: _headerReferenceSize];
      [coder encodeSize: _footerReferenceSize];
      
      // decode inset...
      [coder encodeValueOfObjCType: @encode(CGFloat)
                                at: &_sectionInset.bottom];
      [coder encodeValueOfObjCType: @encode(CGFloat)
                                at: &_sectionInset.top];
      [coder encodeValueOfObjCType: @encode(CGFloat)
                                at: &_sectionInset.left];
      [coder encodeValueOfObjCType: @encode(CGFloat)
                                at: &_sectionInset.right];


      [coder encodeValueOfObjCType: @encode(BOOL)
                                at: &_sectionHeadersPinToVisibleBounds];
      [coder encodeValueOfObjCType: @encode(BOOL)
                                at: &_sectionFootersPinToVisibleBounds];
    }
}

- (CGFloat) minimumLineSpacing
{
  return _minimumLineSpacing;
}

- (void) setMinimumLineSpacing: (CGFloat)spacing
{
  _minimumLineSpacing = spacing;
}

- (CGFloat) minimumInteritemSpacing
{
  return _minimumInteritemSpacing;
}

- (void) setMinimumInteritemSpacing: (CGFloat)spacing
{
  _minimumInteritemSpacing = spacing;
}
  
- (NSSize) itemSize
{  
  return _itemSize;
}

- (void) setItemSize: (NSSize)itemSize
{
  _itemSize = itemSize;
}
  
- (NSSize) estimatedItemSize
{
  return _estimatedItemSize;
}

- (void) setEstimatedItemSize: (NSSize)size
{
  _estimatedItemSize = size;
}
  
- (NSCollectionViewScrollDirection) scrollDirection
{
  return _scrollDirection;
}

- (void) setScrollDirection: (NSCollectionViewScrollDirection)direction
{
  _scrollDirection = direction;
}
  
- (NSSize) headerReferenceSize
{
  return _headerReferenceSize;
}

- (void) setHeaderReferenceSize: (NSSize)size
{
  _headerReferenceSize = size;
}
  
- (NSSize) footerReferenceSize
{
  return _footerReferenceSize;
}

- (void) setFooterReferenceSize: (NSSize)size
{
  _footerReferenceSize = size;
}
  
- (NSEdgeInsets) sectionInset
{
  return _sectionInset;
}

- (void) setSectionInset: (NSEdgeInsets)inset
{
  _sectionInset = inset;
}

- (BOOL) sectionHeadersPinToVisibleBounds
{
  return _sectionHeadersPinToVisibleBounds;
}

- (void) setSectionHeadersPinToVisibleBounds: (BOOL)f
{
  _sectionHeadersPinToVisibleBounds = f;
}

- (BOOL) sectionFootersPinToVisibleBounds
{
  return _sectionFootersPinToVisibleBounds;
}

- (void) setSectionFootersPinToVisibleBounds: (BOOL)f
{
  _sectionFootersPinToVisibleBounds = f;
}

- (BOOL) sectionAtIndexIsCollapsed: (NSUInteger)sectionIndex
{
  return [_collapsedSections containsIndex: sectionIndex];
}

- (void) collapseSectionAtIndex: (NSUInteger)sectionIndex
{
  [_collapsedSections addIndex: sectionIndex];
}

- (void) expandSectionAtIndex: (NSUInteger)sectionIndex
{
  [_collapsedSections removeIndex: sectionIndex];
}

// Methods to override for specific layouts...
- (void) prepareLayout
{
  [super prepareLayout];
  _ds = 0;
  _dr = 0;
}

- (NSCollectionViewLayoutAttributes *) layoutAttributesForItemAtIndexPath: (NSIndexPath *)indexPath
{
  NSCollectionViewLayoutAttributes *attrs = AUTORELEASE([[NSCollectionViewLayoutAttributes alloc] init]);
  NSSize sz = NSZeroSize;
  id <NSCollectionViewDelegateFlowLayout> delegate = (id <NSCollectionViewDelegateFlowLayout>)[_collectionView delegate];
  NSInteger section = [indexPath section];
  NSInteger row = [indexPath item];
  NSEdgeInsets insets;
  CGFloat mls = 0.0;
  CGFloat mis = 0.0;
  CGFloat h = 0.0, w = 0.0, x = 0.0, y = 0.0;
  NSRect f = NSZeroRect;
  NSRect vf = [_collectionView frame];  // needed for reflow...
  
  // Item size...
  if ([delegate respondsToSelector: @selector(collectionView:layout:sizeForItemAtIndexPath:)])
    {
      sz = [delegate collectionView: _collectionView
			     layout: self
	     sizeForItemAtIndexPath: indexPath];
    }
  else
    {
      sz = [self itemSize];
    }
  
  // Inset
  if ([delegate respondsToSelector: @selector(collectionView:layout:insetForSectionAtIndex:)])
    {
      insets = [delegate collectionView: _collectionView
				 layout: self
		 insetForSectionAtIndex: section];
    }
  else
    {
      insets = [self sectionInset];
    }
  
  // minimum line spacing
  if ([delegate respondsToSelector: @selector(collectionView:layout:minimimLineSpacingForSectionAtIndex:)])
    {
      mls = [delegate collectionView: _collectionView
			      layout: self
		      minimumLineSpacingForSectionAtIndex: section];
    }
  else
    {
      mls = [self minimumLineSpacing];
    }
  
  // minimum interitem spacing
  if ([delegate respondsToSelector: @selector(collectionView:layout:minimimInteritemSpacingForSectionAtIndex:)])
    {
      mis = [delegate collectionView: _collectionView
			      layout: self
		      minimumInteritemSpacingForSectionAtIndex: section];
    }
  else
    {
      mis = [self minimumInteritemSpacing];
    }

  // Calculations...
  h = sz.height;
  w = sz.width;
  x = (row * w) + insets.left + mis;
  y = (section * h) + insets.top + mls;
  f = NSMakeRect(x, y, w, h);

  // Determine if it is needed to reflow the given element...
  if ((x + w) > vf.size.width)
    {
      _ds += 1;
      x = insets.left + mis;
      y = ((section + _ds) * h) + insets.top + mls;
      f = NSMakeRect(x, y, w, h);
    }

  // Build attrs object...
  [attrs setFrame: f];
  [attrs setZIndex: 0];
  [attrs setSize: f.size];
  [attrs setHidden: NO];
  [attrs setAlpha: 1.0];

  return attrs;
}

- (NSCollectionViewLayoutAttributes *)
  layoutAttributesForSupplementaryViewOfKind: (NSCollectionViewSupplementaryElementKind)elementKind
                                 atIndexPath: (NSIndexPath *)indexPath
{
  return nil;
}

- (NSCollectionViewLayoutAttributes *)
  layoutAttributesForDecorationViewOfKind: (NSCollectionViewDecorationElementKind)elementKind
                              atIndexPath: (NSIndexPath *)indexPath
{
  return nil;
}

- (NSCollectionViewLayoutAttributes *)layoutAttributesForDropTargetAtPoint: (NSPoint)pointInCollectionView
{
  return nil;
}

- (NSCollectionViewLayoutAttributes *)layoutAttributesForInterItemGapBeforeIndexPath: (NSIndexPath *)indexPath
{
  return nil;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange: (NSRect)newBounds
{
  return NO;
}

- (NSCollectionViewLayoutInvalidationContext *)invalidationContextForBoundsChange: (NSRect)newBounds
{
  return nil;
}

- (BOOL)shouldInvalidateLayoutForPreferredLayoutAttributes: (NSCollectionViewLayoutAttributes *)preferredAttributes
                                    withOriginalAttributes: (NSCollectionViewLayoutAttributes *)originalAttributes
{
  return NO;
}

- (NSCollectionViewLayoutInvalidationContext *)
  invalidationContextForPreferredLayoutAttributes: (NSCollectionViewLayoutAttributes *)preferredAttributes
                           withOriginalAttributes: (NSCollectionViewLayoutAttributes *)originalAttributes
{
  return nil;
}

- (NSPoint) targetContentOffsetForProposedContentOffset: (NSPoint)proposedContentOffset
                                  withScrollingVelocity: (NSPoint)velocity
{
  return NSZeroPoint;
}

- (NSPoint) targetContentOffsetForProposedContentOffset: (NSPoint)proposedContentOffset
{
  return NSZeroPoint;  
}

- (NSSize) collectionViewContentSize
{
  return [_collectionView frame].size;
}
// end subclassing hooks...

@end