-
Notifications
You must be signed in to change notification settings - Fork 1
/
CollectionView.m
1032 lines (835 loc) · 26.8 KB
/
CollectionView.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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "CollectionView.h"
static const float DRAG_START_DISTANCE = 10;
static const float DRAG_IMAGE_ALPHA = 0.5;
static NSString * const DRAG_ITEM_TYPE = @"CollectionViewDragType";
typedef enum
{
DragTargetType_None,
DragTargetType_Top,
DragTargetType_Bottom,
} DragTargetType;
@interface CollectionView (Private)
// Override NSView
- (void)moveDown: (id)sender;
- (void)moveUp: (id)sender;
- (BOOL)isFlipped;
- (void)selectAll: (id)sender;
- (void)keyDown: (NSEvent *)event;
- (void)deleteBackward: (id)sender;
- (void)deleteForward: (id)sender;
- (NSDragOperation)draggingEntered: (id<NSDraggingInfo>)sender;
- (void)draggingExited: (id<NSDraggingInfo>)sender;
- (BOOL)prepareForDragOperation: (id<NSDraggingInfo>)sender;
- (BOOL)performDragOperation: (id<NSDraggingInfo>)sender;
- (NSDragOperation)draggingUpdated: (id<NSDraggingInfo>)sender;
- (BOOL)wantsPeriodicDraggingUpdates;
- (NSDragOperation)draggingSourceOperationMaskForLocal: (BOOL)isLocal;
- (void)resizeWithOldSuperviewSize: (NSSize)oldBoundsSize;
// Internal methods
- (void)setNeedsLayout: (BOOL)flag;
- (void)performLayout;
- (void)startDrag: (NSEvent *)event withItem: (CollectionViewItem *)item;
- (NSImage *)dragImageForIndexes: (NSArray *)indexes dragPoint: (NSPoint *)dragPoint;
- (void)itemClicked: (CollectionViewItem *)item event: (NSEvent *)event;
- (void)setAllItemsSelected: (BOOL)selected;
- (void)growSelectionToItem: (CollectionViewItem *)item;
- (void)moveSelection: (BOOL)moveUp byExtending: (BOOL)byExtending;
- (void)scrollToItem: (CollectionViewItem *)item;
- (void)maintainNonEmptySelection: (int)index;
- (void)removeItemsAtIndexes: (NSArray *)indexes;
- (int)indexFromDragTarget: (NSView *)targetView draggingInfo: (id<NSDraggingInfo>)draggingInfo;
- (void)setDragTarget: (NSView *)targetView draggingInfo: (id<NSDraggingInfo>)draggingInfo;
- (void)setIndex: (int)index isDragTarget: (BOOL)isDragTarget;
- (id<CollectionViewTarget>)target;
- (int)dragTargetIndex;
- (void)maximizeViewWidth: (id)sender;
- (void)onKeyWindowChanged: (NSNotification *)notification;
- (void)testSelectionChanged: (NSArray *)oldSelection;
- (NSArray *)filePathsForIndexes: (NSArray *)indexes;
@end
static float
PointDistance(NSPoint start,
NSPoint end)
{
float deltaX = end.x - start.x;
float deltaY = end.y - start.y;
return sqrt(deltaX * deltaX + deltaY * deltaY);
}
@interface CollectionViewItem (Private)
// Override NSView
- (NSMenu *)menuForEvent:(NSEvent *)event;
- (NSView *)hitTest: (NSPoint)point;
- (void)mouseDown: (NSEvent *)event;
- (void)mouseUp: (NSEvent *)event;
- (void)mouseDragged: (NSEvent *)event;
- (BOOL)acceptsFirstResponder;
// Internal methods
- (void)setIsSelected: (BOOL)flag;
- (BOOL)isSelected;
- (CollectionView *)collectionView;
- (BOOL)isLeftClickEvent: (NSEvent *)event;
- (void)setDragTargetType: (DragTargetType)type;
- (DragTargetType)dragTargetType;
- (void)updateHighlightState;
- (void)setHighlight: (BOOL)isHighlighted forTextField: (NSTextField *)textField;
- (BOOL)shouldDrawSecondaryHighlight;
@end
@implementation CollectionView
- (id)initWithFrame: (NSRect)frame
{
if ((self = [super initWithFrame:frame])) {
items = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[items release];
items = nil;
[super dealloc];
}
- (void)awakeFromNib
{
// ASSERT([[self target] conformsToProtocol:
// @protocol(CollectionViewTarget)]);
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onKeyWindowChanged:)
name:NSWindowDidBecomeKeyNotification
object:[self window]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onKeyWindowChanged:)
name:NSWindowDidResignKeyNotification
object:[self window]];
[[[self enclosingScrollView] contentView] setCopiesOnScroll:NO];
[self registerForDraggedTypes:
[NSArray arrayWithObjects:NSFilenamesPboardType, DRAG_ITEM_TYPE, nil]];
[self maximizeViewWidth:nil];
}
- (void)drawRect: (NSRect)rect
{
/*
* XXX: This is a hack to work around autohide scrollbars not working
* correctly. When removing an item causes the vertical scrollbar to
* be hidden our items don't automatically resize to fit the new width.
*
* Until that bug is fixed this hack disables the "needsLayout" optimization
* and forces a layout on every dray. The layout code is pretty fast so this
* isn't very expensive.
*/
needsLayout = YES;
if (needsLayout) {
[self performLayout];
[self maintainNonEmptySelection:0];
}
[super drawRect:rect];
[[NSColor colorWithCalibratedRed:214.0/255.0
green:221.0/255.0
blue:229.0/255.0
alpha:1.0] set];
NSRectFill([self bounds]);
[collectionViewDelegate drawComplete];
}
- (void)addItem: (CollectionViewItem *)item
atIndex: (int)index
{
// ASSERT(item);
[items insertObject:item atIndex:index];
[item setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin];
[self addSubview:item];
[self setNeedsLayout:YES];
}
- (void)addItem: (CollectionViewItem *)item
{
[items addObject:item];
[item setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin];
[self addSubview:item];
[self setNeedsLayout:YES];
}
- (void)removeItemAtIndex: (int)index
{
[self removeItemsAtIndexes:
[NSArray arrayWithObject:[NSNumber numberWithInt:index]]];
[self maintainNonEmptySelection:index];
}
- (void)removeAllItems
{
[items removeAllObjects];
[self setNeedsLayout:YES];
[self setNeedsDisplay:YES];
}
- (int)numberOfItems
{
return [items count];
}
- (NSArray *)selectedIndexes
{
NSMutableArray *selectedIndexes = [NSMutableArray array];
int count = [items count];
int i;
for (i = 0; i < count; i++) {
CollectionViewItem *item = [items objectAtIndex:i];
if ([item isSelected]) {
[selectedIndexes addObject:[NSNumber numberWithInt:i]];
}
}
return selectedIndexes;
}
@end // CollectionView
@implementation CollectionView (Private)
- (void)setNeedsLayout: (BOOL)flag
{
needsLayout = flag;
}
- (void)performLayout
{
// Calculate the total height.
float myHeight = 0;
int rowCount = [collectionViewDelegate numberOfRows];
int i;
CollectionViewItem *item;
NSMutableArray *collectionViewItems = [[[NSMutableArray alloc] init] autorelease];
for (i = 0; i < rowCount; i++) {
// item = [collectionViewDelegate collectionViewItemAt:i];
item = [items objectAtIndex:i];
[collectionViewItems addObject:item];
myHeight += [item frame].size.height;
}
// NSEnumerator *e = [items objectEnumerator];
// CollectionViewItem *item;
// while ((item = [e nextObject])) {
// myHeight += [item frame].size.height;
// }
// Resize the collection view to fit.
NSRect myFrame = [self frame];
[self setFrameSize:NSMakeSize(myFrame.size.width, myHeight)];
if (myFrame.size.height != myHeight) {
[self setNeedsDisplay:YES];
}
// Layout all the items.
float yPos = 0;
NSEnumerator *e = [collectionViewItems objectEnumerator];
while ((item = [e nextObject])) {
NSRect oldItemFrame = [item frame];
NSRect newItemFrame;
newItemFrame.origin.y = yPos;
newItemFrame.origin.x = 0;
newItemFrame.size.width = myFrame.size.width;
newItemFrame.size.height = oldItemFrame.size.height;
[item setFrame:newItemFrame];
yPos += newItemFrame.size.height;
if (!NSEqualRects(newItemFrame, oldItemFrame)) {
[item setNeedsDisplay:YES];
}
}
[self setNeedsLayout:NO];
}
- (BOOL)isFlipped
{
return YES;
}
- (void)startDrag: (NSEvent *)event
withItem: (CollectionViewItem *)item
{
// If the dragged item is selected then drag all selected items too.
NSArray *dragIndexes = nil;
if ([item isSelected]) {
dragIndexes = [self selectedIndexes];
} else {
dragIndexes = [NSArray arrayWithObject:
[NSNumber numberWithInt:[items indexOfObject:item]]];
}
// Write data to the paste board.
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObjects:DRAG_ITEM_TYPE,
NSFilenamesPboardType,
nil]
owner:self];
[pboard setPropertyList:dragIndexes forType:DRAG_ITEM_TYPE];
[pboard setPropertyList:[self filePathsForIndexes:dragIndexes] forType:NSFilenamesPboardType];
// Generate the drag image from the dragged items.
NSPoint dragPos;
NSImage *dragImage = [self dragImageForIndexes:dragIndexes dragPoint:&dragPos];
// Start the drag.
[self dragImage:dragImage
at:dragPos
offset:NSZeroSize
event:event
pasteboard:pboard
source:self
slideBack:YES];
}
- (NSImage *)dragImageForIndexes: (NSArray *)indexes
dragPoint: (NSPoint *)dragPoint
{
// Make an image as big as the visible rect.
NSRect dragRect = [self convertRect:[self visibleRect]
fromView:[self superview]];
NSImage *dragImage =
[[[NSImage alloc] initWithSize:dragRect.size] autorelease];
NSEnumerator *e = [indexes objectEnumerator];
NSNumber *indexNumber;
while ((indexNumber = [e nextObject])) {
CollectionViewItem *item =
[items objectAtIndex:[indexNumber intValue]];
NSRect itemRect = [item visibleRect];
if (NSEqualRects(itemRect, NSZeroRect)) {
continue;
}
// Get an image of the dragged view without the selection.
BOOL oldSelectedValue = [item isSelected];
[item setIsSelected:NO];
NSData *itemAsPDF = [item dataWithPDFInsideRect:itemRect];
[item setIsSelected:oldSelectedValue];
NSImage *itemImage = [[NSImage alloc] initWithData:itemAsPDF];
// Convert from our flipped axis into the image's non-flipped axis.
NSPoint pos = [item frame].origin;
pos.y = dragRect.origin.y + dragRect.size.height - pos.y;
pos.y -= itemRect.size.height;
// Drag the view's image into the drag image.
[dragImage lockFocus];
[itemImage drawAtPoint:pos
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:DRAG_IMAGE_ALPHA];
[dragImage unlockFocus];
[itemImage release];
}
// ASSERT(dragPoint);
*dragPoint = NSMakePoint(dragRect.origin.x,
dragRect.origin.y + dragRect.size.height);
return dragImage;
}
- (void)itemClicked: (CollectionViewItem *)item
event: (NSEvent *)event
{
NSArray *oldSelection = [self selectedIndexes];
if (([event modifierFlags] & NSCommandKeyMask) != 0) {
[item setIsSelected:![item isSelected]];
} else if (([event modifierFlags] & NSShiftKeyMask) != 0) {
[self growSelectionToItem:item];
} else {
[self setAllItemsSelected:NO];
[item setIsSelected:YES];
if ([event clickCount] == 2) {
[[self target]
performDoubleClickActionForIndex:[items indexOfObject:item]];
}
}
[self scrollToItem:item];
[self testSelectionChanged:oldSelection];
}
- (void)setAllItemsSelected: (BOOL)selected
{
NSEnumerator *e = [items objectEnumerator];
CollectionViewItem *item;
while ((item = [e nextObject])) {
[item setIsSelected:selected];
}
}
- (void)growSelectionToItem: (CollectionViewItem *)item
{
NSArray *oldSelection = [self selectedIndexes];
int itemIndex = [items indexOfObject:item];
int startIndex = [[oldSelection objectAtIndex:0] intValue];
int endIndex = [[oldSelection lastObject] intValue];
if (itemIndex < startIndex) {
startIndex = itemIndex;
} else if (itemIndex > endIndex) {
endIndex = itemIndex;
}
int i;
for (i = startIndex; i <= endIndex; i++) {
[[items objectAtIndex:i] setIsSelected:YES];
}
[self testSelectionChanged:oldSelection];
}
- (void)moveDown: (id)sender
{
BOOL shift = ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) != 0;
[self moveSelection:NO byExtending:shift];
}
- (void)moveUp: (id)sender
{
BOOL shift = ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) != 0;
[self moveSelection:YES byExtending:shift];
}
- (void)moveSelection: (BOOL)moveUp
byExtending: (BOOL)byExtending
{
if ([items count] == 0) {
return;
}
NSArray *oldSelection = [self selectedIndexes];
int index = NSNotFound;
if (moveUp) {
int firstIndex = [[oldSelection objectAtIndex:0] intValue];
firstIndex--;
if (firstIndex >= 0) {
index = firstIndex;
}
} else {
int lastIndex = [[oldSelection lastObject] intValue];
lastIndex++;
if (lastIndex < [items count]) {
index = lastIndex;
}
}
if (index != NSNotFound) {
if (!byExtending) {
[self setAllItemsSelected:NO];
}
CollectionViewItem *item = [items objectAtIndex:index];
[item setIsSelected:YES];
[self scrollToItem:item];
}
[self testSelectionChanged:oldSelection];
}
- (void)scrollToItem: (CollectionViewItem *)item
{
NSRect itemFrame = [item frame];
NSPoint top, bottom;
bottom.y = NSMaxY(itemFrame);
top.y = NSMinY(itemFrame);
top.x = 0;
bottom.x = 0;
NSRect visibleRect = [self visibleRect];
BOOL bottomVisible = NSPointInRect(bottom, visibleRect);
BOOL topVisible = NSPointInRect(top, visibleRect);
if (!topVisible || !bottomVisible) {
NSPoint scrollPos;
if (!bottomVisible) {
scrollPos.y = bottom.y - visibleRect.size.height;
} else {
scrollPos.y = top.y;
}
scrollPos.x = 0;
NSClipView *clipView = [[self enclosingScrollView] contentView];
[clipView scrollToPoint:[clipView constrainScrollPoint:scrollPos]];
[[self enclosingScrollView] reflectScrolledClipView:clipView];
}
}
- (void)selectAll: (id)sender
{
NSArray *oldSelection = [self selectedIndexes];
[self setAllItemsSelected:YES];
[self testSelectionChanged:oldSelection];
}
- (void)maintainNonEmptySelection: (int)index
{
NSArray *oldSelection = [self selectedIndexes];
if ([items count] > 0 && [oldSelection count] == 0) {
int selectionIndex = index;
if (selectionIndex < 0) {
selectionIndex = 0;
} else if (selectionIndex >= [items count]) {
selectionIndex = [items count] - 1;
}
[[items objectAtIndex:selectionIndex] setIsSelected:YES];
[self testSelectionChanged:oldSelection];
}
}
- (void)keyDown: (NSEvent *)event
{
unichar u = [[event charactersIgnoringModifiers] characterAtIndex: 0];
if (u == NSDeleteCharacter ||
u == NSDeleteFunctionKey) {
// Forward or backward delete.
[self interpretKeyEvents:[NSArray arrayWithObject:event]];
} else if (u == NSEnterCharacter ||
u == NSCarriageReturnCharacter) {
NSArray *indexes = [self selectedIndexes];
if ([indexes count] > 0) {
[[self target] performDoubleClickActionForIndex:
[[indexes objectAtIndex:0] intValue]];
}
} else {
[super keyDown:event];
}
}
- (void)deleteBackward: (id)sender
{
NSArray *indexes = [self selectedIndexes];
if ([indexes count] > 0 &&
[[self target] shouldRemoveItemsAtIndexes:indexes]) {
[self removeItemsAtIndexes:indexes];
[self maintainNonEmptySelection:[[indexes objectAtIndex:0] intValue] - 1];
[[self window] makeFirstResponder:self];
}
}
- (void)deleteForward: (id)sender
{
NSArray *indexes = [self selectedIndexes];
if ([indexes count] > 0 &&
[[self target] shouldRemoveItemsAtIndexes:indexes]) {
[self removeItemsAtIndexes:indexes];
[self maintainNonEmptySelection:[[indexes objectAtIndex:0] intValue]];
[[self window] makeFirstResponder:self];
}
}
- (void)removeItemsAtIndexes: (NSArray *)indexes
{
if ([indexes count] == 0) {
return;
}
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
NSEnumerator *e = [indexes objectEnumerator];
NSNumber *indexNumber;
while ((indexNumber = [e nextObject])) {
int index = [indexNumber intValue];
CollectionViewItem *item = [items objectAtIndex:index];
[item removeFromSuperview];
[indexSet addIndex:index];
}
[items removeObjectsAtIndexes:indexSet];
/*
* Need to force the layout to change right away so that scrolling and
* selection code will work.
*/
[self performLayout];
}
- (NSDragOperation)draggingEntered: (id<NSDraggingInfo>)sender
{
return [self draggingUpdated:sender];
}
- (void)draggingExited: (id<NSDraggingInfo>)sender
{
[self setDragTarget:nil draggingInfo:sender];
}
- (BOOL)prepareForDragOperation: (id<NSDraggingInfo>)sender
{
BOOL acceptDrop = NO;
NSArray *dragTypes = [[sender draggingPasteboard] types];
if ([dragTypes containsObject:DRAG_ITEM_TYPE]) {
acceptDrop = YES;
} else if ([dragTypes containsObject:NSFilenamesPboardType]) {
NSArray *filePaths = [[sender draggingPasteboard]
propertyListForType:NSFilenamesPboardType];
acceptDrop = [[self target] dragOperationForFiles:filePaths] !=
NSDragOperationNone;
}
if (!acceptDrop) {
[self setDragTarget:nil draggingInfo:sender];
}
return acceptDrop;
}
- (BOOL)performDragOperation: (id<NSDraggingInfo>)sender
{
int destIndex = [self dragTargetIndex];
// ASSERT(destIndex != NSNotFound);
[self setDragTarget:nil draggingInfo:sender];
NSArray *dragTypes = [[sender draggingPasteboard] types];
if ([dragTypes containsObject:DRAG_ITEM_TYPE]) {
NSArray *indexes = [[sender draggingPasteboard]
propertyListForType:DRAG_ITEM_TYPE];
[[self target] dragItemsAtIndexes:indexes toIndex:destIndex];
} else if ([dragTypes containsObject:NSFilenamesPboardType]) {
NSArray *filePaths = [[sender draggingPasteboard]
propertyListForType:NSFilenamesPboardType];
[[self target] dragFiles:filePaths toIndex:destIndex];
}
return YES;
}
- (NSDragOperation)draggingUpdated: (id<NSDraggingInfo>)sender
{
NSPoint dragPos = [[self superview] convertPoint:[sender draggingLocation]
fromView:nil];
NSView *targetView = [self hitTest:dragPos];
NSDragOperation dragOperation = NSDragOperationNone;
NSArray *dragTypes = [[sender draggingPasteboard] types];
if ([dragTypes containsObject:DRAG_ITEM_TYPE]) {
dragOperation = NSDragOperationMove;
} else if ([dragTypes containsObject:NSFilenamesPboardType]) {
NSArray *filePaths = [[sender draggingPasteboard]
propertyListForType:NSFilenamesPboardType];
dragOperation = [[self target] dragOperationForFiles:filePaths];
}
if (dragOperation == NSDragOperationNone) {
[self setDragTarget:nil draggingInfo:sender];
} else {
[self setDragTarget:targetView draggingInfo:sender];
}
return dragOperation;
}
- (BOOL)wantsPeriodicDraggingUpdates
{
return NO;
}
- (NSDragOperation)draggingSourceOperationMaskForLocal: (BOOL)isLocal
{
if (isLocal) {
return NSDragOperationMove;
} else {
return NSDragOperationLink;
}
}
- (int)indexFromDragTarget: (NSView *)targetView
draggingInfo: (id<NSDraggingInfo>)draggingInfo
{
int index = NSNotFound;
if (targetView &&
[targetView isKindOfClass:[CollectionViewItem class]]) {
index = [items indexOfObject:targetView];
}
if (index != NSNotFound) {
CollectionViewItem *item =
[items objectAtIndex:index];
NSPoint viewPos = [item convertPoint:[draggingInfo draggingLocation]
fromView:nil];
NSRect bounds = [item bounds];
if (viewPos.y < bounds.size.height / 2.0) {
index = fmin(index + 1, [items count]);
}
}
return index;
}
- (void)setDragTarget: (NSView *)targetView
draggingInfo: (id<NSDraggingInfo>)draggingInfo
{
int newDragTargetIndex = [self indexFromDragTarget:targetView
draggingInfo:draggingInfo];
int curDragTargetIndex = [self dragTargetIndex];
if (newDragTargetIndex != curDragTargetIndex) {
[self setIndex:curDragTargetIndex isDragTarget:NO];
[self setIndex:newDragTargetIndex isDragTarget:YES];
}
}
- (void)setIndex: (int)index
isDragTarget: (BOOL)isDragTarget
{
if (index != NSNotFound) {
DragTargetType dragTargetType = isDragTarget ? DragTargetType_Top :
DragTargetType_None;
int actualIndex = index;
if (actualIndex == [items count]) {
actualIndex = [items count] - 1;
if (isDragTarget) {
dragTargetType = DragTargetType_Bottom;
}
}
[[items objectAtIndex:actualIndex] setDragTargetType:dragTargetType];
}
}
- (id<CollectionViewTarget>)target
{
return target;
}
- (int)dragTargetIndex
{
int count = [items count];
int i;
for (i = 0; i < count; i++) {
CollectionViewItem *item = [items objectAtIndex:i];
if ([item dragTargetType] == DragTargetType_Top) {
return i;
} else if ([item dragTargetType] == DragTargetType_Bottom) {
return i + 1;
}
}
return NSNotFound;
}
- (void)resizeWithOldSuperviewSize: (NSSize)oldBoundsSize
{
[super resizeWithOldSuperviewSize:oldBoundsSize];
[self performSelector:@selector(maximizeViewWidth:) withObject:nil afterDelay:0.10];
//[self maximizeViewWidth:nil];
}
- (void)maximizeViewWidth: (id)sender
{
float width = [[[self enclosingScrollView] contentView] frame].size.width;
NSRect myOldFrame = [self frame];
if (myOldFrame.size.width != width) {
[self setFrameSize:NSMakeSize(width, myOldFrame.size.height)];
[self setNeedsDisplay:YES];
}
}
- (void)onKeyWindowChanged: (NSNotification *)notification
{
[self setNeedsDisplay:YES];
NSEnumerator *e = [items objectEnumerator];
CollectionViewItem *item;
while ((item = [e nextObject])) {
[item updateHighlightState];
}
}
- (void)testSelectionChanged: (NSArray *)oldSelection
{
BOOL didChange = NO;
if (!oldSelection) {
didChange = YES;
} else {
NSArray *newSelection = [self selectedIndexes];
didChange = ![oldSelection isEqualToArray:newSelection];
}
if (didChange) {
[[self target] onSelectionDidChange];
}
}
- (NSArray *)filePathsForIndexes: (NSArray *)indexes
{
NSMutableArray *filePaths = [NSMutableArray array];
NSEnumerator *e = [indexes objectEnumerator];
NSNumber *indexNumber;
while ((indexNumber = [e nextObject])) {
NSString *filePath = [[self target] filePathForIndex:[indexNumber intValue]];
if (filePath) {
[filePaths addObject:filePath];
}
}
return filePaths;
}
@end // CollectionView (Private)
@implementation CollectionViewItem
- (void)dealloc
{
[cachedTextColors release];
cachedTextColors = nil;
[super dealloc];
}
- (void)awakeFromNib
{
[self setNextResponder:[self superview]];
}
- (void)drawRect: (NSRect)rect
{
[super drawRect:rect];
if ([self isSelected]) {
if ([self shouldDrawSecondaryHighlight]) {
[[NSColor grayColor] set];
} else {
[[NSColor blueColor] set];
}
NSRectFill(rect);
}
if (dragTargetType != DragTargetType_None) {
NSRect dRect = [self bounds];
if (dragTargetType == DragTargetType_Top) {
dRect.origin.y = dRect.size.height - 2.0;
}
dRect.size.height = 2;
[[NSColor blackColor] set];
NSRectFill(dRect);
}
}
@end // CollectionViewItem
@implementation CollectionViewItem (Private)
- (NSMenu *)menuForEvent:(NSEvent *)event
{
if (![self isSelected]) {
[[self collectionView] itemClicked:self event:event];
}
return [[self superview] menuForEvent:event];
}
- (NSView *)hitTest: (NSPoint)point
{
NSView *result = [super hitTest:point];
if (result && ![result isKindOfClass:[NSButton class]]) {
return self;
} else {
return result;
}
}
- (void)mouseDown: (NSEvent *)event
{
if ([self isLeftClickEvent:event]) {
mouseDownPos = [event locationInWindow];
}
}
- (void)mouseUp: (NSEvent *)event
{
if ([self isLeftClickEvent:event]) {
[[self collectionView] itemClicked:self event:event];
}
}
- (void)mouseDragged: (NSEvent *)event
{
if ([self isLeftClickEvent:event]) {
NSPoint mouseDragPos = [event locationInWindow];
float distance = PointDistance(mouseDragPos, mouseDownPos);
if (distance > DRAG_START_DISTANCE) {
[[self collectionView] startDrag:event withItem:self];
}
}
}
- (BOOL)acceptsFirstResponder
{
return NO;
}
- (void)setIsSelected: (BOOL)flag
{
if (isSelected != flag) {
isSelected = flag;
[self updateHighlightState];
[self setNeedsDisplay:YES];
}
}
- (BOOL)isSelected
{
return isSelected;
}
- (CollectionView *)collectionView
{
// ASSERT([[self superview] isKindOfClass:[CollectionView class]]);
return (CollectionView *)[self superview];
}
- (BOOL)isLeftClickEvent: (NSEvent *)event
{
return [event buttonNumber] == 0 &&
([event modifierFlags] & NSControlKeyMask) == 0;
}
- (void)setDragTargetType: (DragTargetType)type
{
if (dragTargetType != type) {
dragTargetType = type;
[self setNeedsDisplay:YES];
}
}
- (DragTargetType)dragTargetType
{
return dragTargetType;
}
- (void)updateHighlightState
{
BOOL isHighlighted = [self isSelected] &&
![self shouldDrawSecondaryHighlight];
NSEnumerator *e = [[self subviews] objectEnumerator];
id subview;
while ((subview = [e nextObject])) {
if ([subview isKindOfClass:[NSTextField class]]) {
[self setHighlight:isHighlighted
forTextField:(NSTextField *)subview];
} else if ([subview respondsToSelector:@selector(setIsSelected:)]) {
[subview setIsSelected:isHighlighted];
}
}
}