forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMCollectionView.m
862 lines (771 loc) · 23.3 KB
/
AMCollectionView.m
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//
// AMCollectionView.m
// AMCollectionViewTest
//
// Created by Andreas on 19.11.07.
// Copyright 2007 Andreas Mayer. All rights reserved.
//
#import "AMCollectionView.h"
#import "AMCollectionViewItem.h"
#import "AMCollectionViewItemProtocol.h"
@interface AMCollectionViewItem (Private)
- (void)setCollectionView:(AMCollectionView *)value;
@end
@interface AMCollectionView (Private)
- (BOOL)needsLayout;
- (void)setNeedsLayout:(BOOL)value;
- (NSDictionary *)itemsForObjects;
- (void)setItemsForObjects:(NSDictionary *)value;
- (NSData *)archivedItemPrototype;
- (void)setArchivedItemPrototype:(NSData *)value;
- (void)doLayout;
- (void)archiveItemPrototype:(AMCollectionViewItem *)item;
- (AMCollectionViewItem *)itemAtPoint:(NSPoint)point;
- (void)setNeedsDisplayForItems:(NSArray *)items;
//- (void)removeAllSubviews;
- (void)redrawAllSubviewsInRect:(NSRect)rect;
- (void)sendSelectionDidChangeNotification;
- (BOOL)deselectAllButItems:(NSArray *)items;
- (NSArray *)setNeedsDisplayForSelectedItems;
@end
@implementation AMCollectionView
- (id)initWithFrame:(NSRect)frame
{
am_initializing = YES;
self = [super initWithFrame:frame];
if (self) {
[self setPostsFrameChangedNotifications:YES];
[self setPostsBoundsChangedNotifications:YES];
[self setItemsForObjects:[[[NSMutableDictionary alloc] init] autorelease]];
}
return self;
}
- (void)awakeFromNib
{
am_initializing = NO;
if ([self itemPrototype]) {
[self archiveItemPrototype:[self itemPrototype]];
am_itemRespondsToSizeForViewWithProposedSize = [[self itemPrototype] respondsToSelector:@selector(sizeForViewWithProposedSize:)];
if ([[self itemPrototype] view]) {
[self setDrawsBackground:![[[self itemPrototype] view] isOpaque]];
[self setRowHeight:[[[self itemPrototype] view] frame].size.height];
}
}
[self setAutoresizesSubviews:NO] ;
}
- (void)dealloc
{
[itemPrototype release];
[backgroundColors release];
[content release];
[am_itemsForObjects release];
[am_archivedItemPrototype release];
[selectedRowColor release];
[secondarySelectedRowColor release];
[super dealloc];
}
//============================================================================
#pragma mark accessor methods
//============================================================================
- (AMCollectionViewItem *)itemPrototype
{
return [[itemPrototype retain] autorelease];
}
- (void)setItemPrototype:(AMCollectionViewItem *)value
{
if (am_initializing) {
// view outlet is not set yet, so we need to defer this until awakeFromNib
itemPrototype = [value retain];
} else {
if (itemPrototype != value) {
[itemPrototype release];
itemPrototype = [value retain];
[itemPrototype setCollectionView:self];
[self archiveItemPrototype:value];
am_itemRespondsToSizeForViewWithProposedSize = [itemPrototype respondsToSelector:@selector(sizeForViewWithProposedSize:)];
[self setRowHeight:[[itemPrototype view] frame].size.height];
}
}
}
- (NSArray *)backgroundColors
{
return [[backgroundColors retain] autorelease];
}
- (void)setBackgroundColors:(NSArray *)value
{
if (backgroundColors != value) {
[backgroundColors release];
backgroundColors = [value copy];
}
}
- (float)rowHeight
{
return rowHeight;
}
- (void)setRowHeight:(float)value
{
if (rowHeight != value) {
rowHeight = value;
}
}
- (NSArray *)content
{
return [[content retain] autorelease];
}
- (void)setContent:(NSArray *)value
itemClass:(Class)itemClass
{
if (content != value) {
[content release];
content = [value copy];
// reuse previous items
NSMutableArray *oldItems = [[am_itemsForObjects allValues] mutableCopy]; // release at end of method
NSMutableDictionary *newItems = [[[NSMutableDictionary alloc] init] autorelease];
NSEnumerator *enumerator = [content objectEnumerator];
id object;
AMCollectionViewItem *item;
while ((object = [enumerator nextObject])) {
item = [self itemForObject:object];
if (item) {
[oldItems removeObject:item];
[item setRepresentedObject:object] ; // JERRY
} else {
item = [[[itemClass alloc] initWithCollectionView:self representedObject:object] autorelease];
am_itemRespondsToSizeForViewWithProposedSize = (am_itemRespondsToSizeForViewWithProposedSize || [item respondsToSelector:@selector(sizeForViewWithProposedSize:)]);
}
[newItems setObject:item forKey:[NSValue valueWithNonretainedObject:object]];
}
[self setItemsForObjects:newItems];
// remove old items
enumerator = [oldItems objectEnumerator];
while ((item = [enumerator nextObject])) {
[item removeFromCollectionView];
}
[oldItems release];
[self doLayout];
[self setNeedsDisplay:YES];
}
}
- (NSIndexSet *)selectionIndexes
{
NSMutableIndexSet *result = [[[NSMutableIndexSet alloc] init] autorelease];
int index = 0;
NSEnumerator *enumerator = [content objectEnumerator];
id object;
AMCollectionViewItem *item;
while (object = [enumerator nextObject]) {
item = [am_itemsForObjects objectForKey:[NSValue valueWithNonretainedObject:object]];
if ([item isSelected]) {
[result addIndex:index];
}
index++;
}
return result;
}
- (void)setSelectionIndexes:(NSIndexSet *)indexSet
{
// change selection for items
AMCollectionViewItem *item = nil;
int index = 0;
NSEnumerator *enumerator = [content objectEnumerator];
id object;
while (object = [enumerator nextObject]) {
item = [am_itemsForObjects objectForKey:[NSValue valueWithNonretainedObject:object]];
[item setSelected:[indexSet containsIndex:index]];
index++;
}
[self sendSelectionDidChangeNotification];
}
- (BOOL)isSelectable
{
return selectable;
}
- (void)setSelectable:(BOOL)value
{
if (selectable != value) {
selectable = value;
}
}
- (BOOL)allowsMultipleSelection
{
return allowsMultipleSelection;
}
- (void)setAllowsMultipleSelection:(BOOL)value
{
if (allowsMultipleSelection != value) {
allowsMultipleSelection = value;
}
}
- (BOOL)drawsBackground
{
return am_drawsBackground;
}
- (void)setDrawsBackground:(BOOL)value
{
if (am_drawsBackground != value) {
am_drawsBackground = value;
}
}
- (BOOL)usesAlternatingRowBackgroundColors
{
return usesAlternatingRowBackgroundColors;
}
- (void)setUsesAlternatingRowBackgroundColors:(BOOL)value
{
if (usesAlternatingRowBackgroundColors != value) {
usesAlternatingRowBackgroundColors = value;
}
}
- (NSColor *)selectedRowColor
{
NSColor *result = [[selectedRowColor retain] autorelease];
if (!result) {
result = [NSColor selectedControlColor];
}
return result;
}
- (void)setSelectedRowColor:(NSColor *)value
{
if (selectedRowColor != value) {
[selectedRowColor release];
selectedRowColor = [value copy];
}
}
- (NSColor *)secondarySelectedRowColor
{
NSColor *result = [[secondarySelectedRowColor retain] autorelease];
if (!result) {
result = [NSColor secondarySelectedControlColor];
}
return result;
}
- (void)setSecondarySelectedRowColor:(NSColor *)value
{
if (secondarySelectedRowColor != value) {
[secondarySelectedRowColor release];
secondarySelectedRowColor = [value copy];
}
}
- (BOOL)isFirstResponder
{
NSResponder *firstResponder = [[self window] firstResponder];
return ([[self window] isKeyWindow] && ((firstResponder == self) || ([firstResponder isKindOfClass:[NSView class]] && [(NSView *)firstResponder isDescendantOf:self])));
}
- (NSDictionary *)itemsForObjects
{
return am_itemsForObjects;
}
- (void)setItemsForObjects:(NSDictionary *)value
{
if (am_itemsForObjects != value) {
[am_itemsForObjects release];
am_itemsForObjects = [value retain];
}
}
- (BOOL)needsLayout
{
return am_needsLayout;
}
- (void)setNeedsLayout:(BOOL)value
{
am_needsLayout = value;
}
- (NSData *)archivedItemPrototype
{
return am_archivedItemPrototype;
}
- (void)setArchivedItemPrototype:(NSData *)value
{
if (am_archivedItemPrototype != value) {
[am_archivedItemPrototype release];
am_archivedItemPrototype = [value retain];
}
}
//============================================================================
#pragma mark NSView methods
//============================================================================
- (void)drawRect:(NSRect)rect
{
// NSLog(@"drawRect:%f, %f, %f, %f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
if ([self needsLayout]) {
[self doLayout];
}
if ([self drawsBackground]) {
NSColor *currentSelectionColor = nil;
// BOOL isKeyWindow = [[self window] isKeyWindow];
// BOOL firstResponderIsView = [firstResponder isKindOfClass:[NSView class]];
// BOOL firstResponderIsDescendentOfSelf = [(NSView *)firstResponder isDescendantOf:self];
// NSLog(@"first responder: %@", firstResponder);
// NSLog(@"is key window: %@", (isKeyWindow ? @"YES" : @"NO"));
// NSLog(@"first responder is view: %@", (firstResponderIsView ? @"YES" : @"NO"));
// NSLog(@"first responder is descendent of self: %@", (firstResponderIsDescendentOfSelf ? @"YES" : @"NO"));
if ([self isFirstResponder]) {
// active
currentSelectionColor = [self selectedRowColor];
} else {
// inactive
currentSelectionColor = [self secondarySelectedRowColor];
}
NSArray *bgColors = [self backgroundColors];
if (!bgColors || ([bgColors count] == 0)) {
bgColors = [NSColor controlAlternatingRowBackgroundColors];
}
// in case we are content rect of a clip view, paint clip view in background color
NSScrollView *scrollView = [self enclosingScrollView];
if (scrollView) {
[scrollView setBackgroundColor:[bgColors objectAtIndex:0]];
[scrollView setDrawsBackground:YES];
}
// fill with first background color
[[bgColors objectAtIndex:0] set];
NSRectFill(rect);
int colorCount = [bgColors count];
int colorIndex;
int index = 0;
id object;
AMCollectionViewItem *item;
NSRect itemFrame;
NSEnumerator *enumerator = [[self content] objectEnumerator];
while ((object = [enumerator nextObject])) {
item = [self itemForObject:object];
if (item) {
itemFrame = [[item view] frame];
if (NSIntersectsRect(rect, itemFrame)) {
if ([item isSelected]) {
[currentSelectionColor set];
NSRectFill(itemFrame);
} else {
colorIndex = (index % colorCount);
if (colorIndex != 0) { // we already painted the whole thing in this color
[[bgColors objectAtIndex:colorIndex] set];
NSRectFill(itemFrame);
}
}
}
}
index++;
}
}
}
- (BOOL)isFlipped
{
return YES;
}
//- (void)setFrame:(NSRect)frameRect
//{
// NSLog(@"AMCollectionView -setFrame: (width: %f)", frameRect.size.width);
// BOOL didChangeWidth = (fabsf([self frame].size.width - frameRect.size.width) > 0.1);
// [super setFrame:frameRect];
// NSLog(@"am_itemRespondsToSizeForViewWithProposedSize = %@", (am_itemRespondsToSizeForViewWithProposedSize ? @"YES" : @"NO"));
// NSLog(@"is visible = %@", ([[self window] isVisible] ? @"YES" : @"NO"));
//
// if (am_itemRespondsToSizeForViewWithProposedSize && didChangeWidth) {
// NSLog(@"AMCollectionView - force layout");
// [self setNeedsLayout:YES];
// [self setNeedsDisplay:YES];
// }
//}
- (void)setFrameSize:(NSSize)newSize
{
BOOL didChangeWidth = (fabsf([self frame].size.width - newSize.width) > 0.1);
[super setFrameSize:newSize];
if (am_itemRespondsToSizeForViewWithProposedSize && didChangeWidth) {
[self setNeedsLayout:YES];
[self setNeedsDisplay:YES];
}
}
- (void)viewDidMoveToWindow
{
// we need to observe key window and first responder status
if ([self window]) {
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:[self window]];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidUpdate:) name:NSWindowDidUpdateNotification object:[self window]];
} else {
// [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:nil];
// [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidUpdateNotification object:nil];
}
}
//============================================================================
#pragma mark NSResponder methods
//============================================================================
- (void)moveUp:(id)sender
{
// find first selected row
NSArray *objects = [self content];
AMCollectionViewItem *item;
id object;
id previousObject = nil;
int row, selectedRow = NSNotFound;;
for (row = 0; row < [objects count]; row++) {
object = [objects objectAtIndex:row];
item = [self itemForObject:object];
if ([item isSelected]) {
selectedRow = row;
break;
}
previousObject = object;
}
if ((selectedRow != NSNotFound) && previousObject) {
// select previous object
[self selectItemsForObjects:[NSArray arrayWithObject:previousObject]];
[self performSelector:@selector(scrollObjectToVisible:) withObject:previousObject afterDelay:0.0];
}
}
- (void)moveDown:(id)sender
{
// find last selected row
NSArray *objects = [self content];
AMCollectionViewItem *item;
id object;
id previousObject = nil;
int row, selectedRow = NSNotFound;;
for (row = [objects count]-1; row >= 0 ; row--) {
object = [objects objectAtIndex:row];
item = [self itemForObject:object];
if ([item isSelected]) {
selectedRow = row;
break;
}
previousObject = object;
}
if ((selectedRow != NSNotFound) && previousObject) {
// select following object
[self selectItemsForObjects:[NSArray arrayWithObject:previousObject]];
[self performSelector:@selector(scrollObjectToVisible:) withObject:previousObject afterDelay:0.0];
}
}
- (void)mouseDown:(NSEvent *)theEvent
{
[[self window] makeFirstResponder:self];
// handle selection
BOOL selectionDidChange = NO;
// !!!: Shift-Select is missing
BOOL toggleSelection = (([theEvent modifierFlags] & NSCommandKeyMask) > 0);
NSPoint location = [theEvent locationInWindow];
NSPoint pointInView = [self convertPoint:location fromView:nil];
AMCollectionViewItem *item = [self itemAtPoint:pointInView];
if (item) {
if (toggleSelection) {
selectionDidChange = YES;
if ([item isSelected]) {
[item setSelected:NO];
} else {
if (![self allowsMultipleSelection]) {
[self deselectAll:self];
}
[item setSelected:YES];
}
} else {
if (![item isSelected]) {
selectionDidChange = YES;
[self deselectAll:self];
[item setSelected:YES];
}
}
[self setNeedsDisplayForItems:[NSArray arrayWithObject:item]];
if (selectionDidChange) {
[self sendSelectionDidChangeNotification];
}
}
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
//============================================================================
#pragma mark public methods
//============================================================================
- (AMCollectionViewItem *)newItemForRepresentedObject:(id)object
{
AMCollectionViewItem *result;
NSData *data = [self archivedItemPrototype];
result = [NSKeyedUnarchiver unarchiveObjectWithData:data];
// [result setView:view];
[result setRepresentedObject:object];
[result setCollectionView:self];
return result;
}
- (AMCollectionViewItem *)itemForObject:(id)object
{
id key = [NSValue valueWithNonretainedObject:object] ;
id item = [am_itemsForObjects objectForKey:key];
return item ;
}
- (id)objectForView:(NSView*)view
{
NSArray* objects = [self content] ;
NSEnumerator* enumerator = [objects objectEnumerator] ;
AMCollectionViewItem* item ;
id object ;
while ((object = [enumerator nextObject])) {
AMCollectionViewItem* item = [self itemForObject:object] ;
if ([item view] == view) {
return object ;
}
}
return nil ;
}
- (NSArray *)selectedObjects
{
NSMutableArray *result = [NSMutableArray array];
NSEnumerator *enumerator = [[am_itemsForObjects allKeys] objectEnumerator];
id key;
AMCollectionViewItem *item;
while ((key = [enumerator nextObject])) {
id object = [(NSValue *)key nonretainedObjectValue];
item = [self itemForObject:object];
if ([item isSelected]) {
[result addObject:object];
}
}
return result;
}
- (void)selectItemsForObjects:(NSArray *)objects
{
NSMutableArray *itemsToDeselect = [[am_itemsForObjects allValues] mutableCopy];
AMCollectionViewItem *item;
NSEnumerator *enumerator = [objects objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
item = [self itemForObject:object];
if (item) {
[item setSelected:YES];
[itemsToDeselect removeObject:item];
}
}
enumerator = [itemsToDeselect objectEnumerator];
while ((item = [enumerator nextObject])) {
[item setSelected:NO];
}
[self sendSelectionDidChangeNotification];
}
- (void)deselectAll:(id)sender
{
if ([self deselectAllButItems:nil]) {
[self sendSelectionDidChangeNotification];
}
}
- (void)scrollObjectToVisible:(id)object
{
AMCollectionViewItem *item = [self itemForObject:object];
NSView *view = [item view];
NSRect viewFrame = [view frame];
NSClipView *clipView = [[self enclosingScrollView] contentView];
if ([clipView respondsToSelector:@selector(scrollToPoint:)]) {
BOOL needsScroll = NO;
NSPoint newOrigin;
NSRect visibleRect = [clipView documentVisibleRect];
// NSLog(@"view: top %f bottom %f", viewFrame.origin.y, viewFrame.origin.y+viewFrame.size.height);
// NSLog(@"visible: top %f bottom %f", visibleRect.origin.y, visibleRect.origin.y+visibleRect.size.height);
if (NSMinY(viewFrame) < NSMinY(visibleRect)) {
// scroll to top
newOrigin = viewFrame.origin;
needsScroll = YES;
} else if (NSMaxY(viewFrame) > NSMaxY(visibleRect)) {
// scroll to bottom
newOrigin = NSMakePoint(0.0, viewFrame.origin.y-visibleRect.size.height+viewFrame.size.height);
needsScroll = YES;
}
if (needsScroll) {
newOrigin = [clipView constrainScrollPoint:newOrigin];
[clipView scrollToPoint:newOrigin];
[[self enclosingScrollView] reflectScrolledClipView:clipView];
}
}
}
//============================================================================
#pragma mark private methods
//============================================================================
- (void)kick
{
[self setNeedsLayout:NO];
// [self removeAllSubviews];
NSEnumerator *enumerator = [[self content] objectEnumerator];
id object;
float y = 0.0;
AMCollectionViewItem *item;
NSView *view;
NSSize viewSize;
NSRect viewFrame;
while ((object = [enumerator nextObject])) {
item = [self itemForObject:object];
view = [item view];
viewSize = [view frame].size;
viewSize.width = [self frame].size.width;
if ([item respondsToSelector:@selector(sizeForViewWithProposedSize:)]) {
viewSize = [item sizeForViewWithProposedSize:viewSize];
}
viewFrame.origin = NSMakePoint(0.0, y);
viewFrame.size = viewSize;
[view setFrame:viewFrame];
[self addSubview:view];
y += viewSize.height;
}
}
- (void)doLayout
{
[self setNeedsLayout:NO];
// [self removeAllSubviews];
NSEnumerator *enumerator = [[self content] objectEnumerator];
id object;
float y = 0.0;
AMCollectionViewItem *item;
NSView *view;
NSSize viewSize;
NSRect viewFrame;
while ((object = [enumerator nextObject])) {
item = [self itemForObject:object];
view = [item view];
viewSize = [view frame].size;
viewSize.width = [self frame].size.width;
if ([item respondsToSelector:@selector(sizeForViewWithProposedSize:)]) {
viewSize = [item sizeForViewWithProposedSize:viewSize];
}
viewFrame.origin = NSMakePoint(0.0, y);
viewFrame.size = viewSize;
[view setFrame:viewFrame];
[self addSubview:view];
y += viewSize.height;
}
viewSize.width = [self frame].size.width;
viewSize.height = y;
[self setFrameSize:viewSize];
}
//- (void)removeAllSubviews
//{
// NSArray *subviews = [[self subviews] copy];
// NSEnumerator *enumerator = [subviews objectEnumerator];
// id view;
// while ((view = [enumerator nextObject])) {
// [view removeFromSuperview];
// }
//}
- (void)redrawAllSubviewsInRect:(NSRect)rect
{
NSEnumerator *enumerator = [[self subviews] objectEnumerator];
id view;
while ((view = [enumerator nextObject])) {
if (NSIntersectsRect(rect, [view frame])) {
NSRect localRect = [self convertRect:rect toView:view];
[view drawRect:localRect];
}
}
}
- (void)archiveItemPrototype:(AMCollectionViewItem *)item
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:item];
[self setArchivedItemPrototype:data];
}
- (AMCollectionViewItem *)itemAtPoint:(NSPoint)point
{
AMCollectionViewItem *result = nil;
if (am_itemRespondsToSizeForViewWithProposedSize) {
// variable row height
// !!!: some optimization might be in order ...
NSEnumerator *enumerator = [[self content] objectEnumerator];
id object;
AMCollectionViewItem *item;
NSRect viewFrame;
while ((object = [enumerator nextObject])) {
item = [self itemForObject:object];
viewFrame = [[item view] frame];
if (NSPointInRect(point, viewFrame)) {
result = item;
break;
}
}
} else {
// fixed row height
int index = (point.y / [self rowHeight]);
if (index < [[self content] count]) {
result = [self itemForObject:[[self content] objectAtIndex:index]];
} else {
NSLog(@"registered click beyond content area");
}
}
return result;
}
- (BOOL)deselectAllButItems:(NSArray *)items
{
BOOL result = NO;
NSMutableArray *changedItems = [NSMutableArray array];
AMCollectionViewItem *item;
NSEnumerator *enumerator = [[self content] objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
item = [self itemForObject:object];
if (item && ![items containsObject:item]) {
if ([item isSelected]) {
[item setSelected:NO];
[changedItems addObject:item];
}
}
}
[self setNeedsDisplayForItems:changedItems];
result = ([changedItems count] > 0);
return result;
}
- (void)setNeedsDisplayForItems:(NSArray *)items
{
NSEnumerator *enumerator = [items objectEnumerator];
AMCollectionViewItem *item;
while ((item = [enumerator nextObject])) {
NSRect rect = [[item view] frame];
[self setNeedsDisplayInRect:rect];
}
}
- (NSArray *)setNeedsDisplayForSelectedItems
{
NSMutableArray *result = [NSMutableArray array];
NSEnumerator *enumerator = [[am_itemsForObjects allKeys] objectEnumerator];
id key;
AMCollectionViewItem *item;
while ((key = [enumerator nextObject])) {
id object = [(NSValue *)key nonretainedObjectValue];
item = [self itemForObject:object];
if ([item isSelected]) {
NSRect rect = [[item view] frame];
[self setNeedsDisplayInRect:rect];
}
}
return result;
}
//- (void)windowDidBecomeKey:(NSNotification *)aNotification
//{
// NSLog(@"windowDidBecomeKey:");
// [self setNeedsDisplay:YES];
//}
//
//- (void)windowDidResignKey:(NSNotification *)aNotification
//{
// NSLog(@"windowDidResignKey:");
// [self setNeedsDisplay:YES];
//}
//
- (void)windowDidUpdate:(NSNotification *)aNotification
{
// NSLog(@"windowDidUpdate:");
BOOL newFirstResponder = [self isFirstResponder];
if (newFirstResponder != am_isFirstResponder) {
// NSLog(@"redraw selection");
am_isFirstResponder = newFirstResponder;
[self setNeedsDisplayForSelectedItems];
}
}
- (void)sendSelectionDidChangeNotification
{
[[NSNotificationCenter defaultCenter] postNotificationName:AMCollectionViewSelectionDidChangeNotification object:self];
}
//============================================================================
#pragma mark collection view protocol
//============================================================================
- (void)noteSizeForItemsChanged:(NSArray *)items
{
// !!!: we should optimize this
[self setNeedsLayout:YES];
[self setNeedsDisplay:YES];
}
@end
NSString *const AMCollectionViewSelectionDidChangeNotification = @"AMCollectionViewSelectionDidChangeNotification";