2005-06-02 18:16:43 +00:00
|
|
|
|
|
|
|
#import "DNDArrayController.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
NSString *CogDNDIndexType = @"org.cogx.cog.dnd-index";
|
|
|
|
NSString *CogUrlsPboardType = @"org.cogx.cog.url";
|
|
|
|
NSString *iTunesDropType = @"com.apple.tv.metadata";
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
@implementation DNDArrayController
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
- (void)awakeFromNib {
|
|
|
|
[super awakeFromNib];
|
2005-06-02 18:16:43 +00:00
|
|
|
// register for drag and drop
|
2021-09-23 07:49:51 +00:00
|
|
|
NSPasteboardType fileType;
|
|
|
|
if (@available(macOS 10.13, *)) {
|
|
|
|
fileType = NSPasteboardTypeFileURL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fileType = NSFilenamesPboardType;
|
|
|
|
}
|
2021-01-30 23:14:08 +00:00
|
|
|
[self.tableView registerForDraggedTypes:@[CogDNDIndexType,
|
|
|
|
CogUrlsPboardType,
|
2021-09-23 07:49:51 +00:00
|
|
|
fileType,
|
2021-01-30 23:14:08 +00:00
|
|
|
iTunesDropType]];
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-27 22:09:09 +00:00
|
|
|
- (id <NSPasteboardWriting>)tableView:(NSTableView *)tableView
|
2021-01-30 23:14:08 +00:00
|
|
|
pasteboardWriterForRow:(NSInteger)row {
|
2021-01-27 22:09:09 +00:00
|
|
|
NSPasteboardItem *item = [[NSPasteboardItem alloc] init];
|
2021-01-30 23:14:08 +00:00
|
|
|
[item setString:[@(row) stringValue] forType:CogDNDIndexType];
|
|
|
|
|
2021-01-27 22:09:09 +00:00
|
|
|
return item;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
|
2021-01-27 22:09:09 +00:00
|
|
|
- (void)tableView:(NSTableView *)tableView
|
2021-01-30 23:14:08 +00:00
|
|
|
draggingSession:(NSDraggingSession *)session
|
|
|
|
willBeginAtPoint:(NSPoint)screenPoint
|
|
|
|
forRowIndexes:(NSIndexSet *)rowIndexes {
|
2021-01-27 22:09:09 +00:00
|
|
|
DLog(@"Drag session started with indexes: %@", rowIndexes);
|
|
|
|
}
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2021-01-27 22:09:09 +00:00
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
- (NSDragOperation)tableView:(NSTableView *)tableView
|
2021-01-27 22:09:09 +00:00
|
|
|
validateDrop:(id <NSDraggingInfo>)info
|
2021-01-30 23:14:08 +00:00
|
|
|
proposedRow:(NSInteger)row
|
|
|
|
proposedDropOperation:(NSTableViewDropOperation)dropOperation {
|
2021-01-27 22:09:09 +00:00
|
|
|
NSDragOperation dragOp = NSDragOperationCopy;
|
|
|
|
|
|
|
|
if ([info draggingSource] == tableView)
|
|
|
|
dragOp = NSDragOperationMove;
|
|
|
|
|
|
|
|
DLog(@"VALIDATING DROP!");
|
2005-06-02 18:16:43 +00:00
|
|
|
// we want to put the object at, not over,
|
2021-01-27 22:09:09 +00:00
|
|
|
// the current row (contrast NSTableViewDropOn)
|
|
|
|
[tableView setDropRow:row dropOperation:NSTableViewDropAbove];
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
return dragOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
- (BOOL)tableView:(NSTableView *)tableView
|
2021-01-27 22:09:09 +00:00
|
|
|
acceptDrop:(id <NSDraggingInfo>)info
|
2021-01-30 23:14:08 +00:00
|
|
|
row:(NSInteger)row
|
|
|
|
dropOperation:(NSTableViewDropOperation)dropOperation {
|
2021-01-27 22:09:09 +00:00
|
|
|
if (row < 0) {
|
|
|
|
row = 0;
|
|
|
|
}
|
2021-01-30 23:14:08 +00:00
|
|
|
|
2021-01-27 22:09:09 +00:00
|
|
|
NSArray<NSPasteboardItem *> *items = info.draggingPasteboard.pasteboardItems;
|
2005-06-02 18:16:43 +00:00
|
|
|
// if drag source is self, it's a move
|
2021-01-27 22:09:09 +00:00
|
|
|
if ([info draggingSource] == tableView || items == nil) {
|
|
|
|
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
|
|
|
|
for (NSPasteboardItem *item in items) {
|
2021-01-30 23:14:08 +00:00
|
|
|
[indexSet addIndex:(NSUInteger) [[item stringForType:CogDNDIndexType] intValue]];
|
2021-01-27 22:09:09 +00:00
|
|
|
}
|
|
|
|
if ([indexSet count] > 0) {
|
|
|
|
DLog(@"INDEX SET ON DROP: %@", indexSet);
|
|
|
|
NSArray *selected = [[self arrangedObjects] objectsAtIndexes:indexSet];
|
2021-01-30 23:14:08 +00:00
|
|
|
[self moveObjectsInArrangedObjectsFromIndexes:indexSet toIndex:(unsigned int) row];
|
|
|
|
|
2021-01-27 22:09:09 +00:00
|
|
|
[self setSelectedObjects:selected];
|
2021-01-30 23:14:08 +00:00
|
|
|
|
2021-01-27 22:09:09 +00:00
|
|
|
DLog(@"ACCEPTING DROP!");
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DLog(@"REJECTING DROP!");
|
|
|
|
return NO;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
- (void)moveObjectsInArrangedObjectsFromIndexes:(NSIndexSet *)indexSet
|
2021-04-30 01:16:24 +00:00
|
|
|
toIndex:(NSUInteger)insertIndex {
|
2022-01-22 00:11:24 +00:00
|
|
|
__block NSUInteger rangeCount = 0;
|
|
|
|
__block NSUInteger firstIndex = 0;
|
|
|
|
[indexSet enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
|
|
|
|
if (++rangeCount == 1)
|
|
|
|
firstIndex = range.location;
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (rangeCount == 1 &&
|
|
|
|
(insertIndex >= firstIndex &&
|
|
|
|
insertIndex < firstIndex + [indexSet count])) // Null operation
|
|
|
|
return;
|
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
NSArray *objects = [self arrangedObjects];
|
|
|
|
NSUInteger index = [indexSet lastIndex];
|
2021-01-27 22:09:09 +00:00
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
NSUInteger aboveInsertIndexCount = 0;
|
|
|
|
id object;
|
|
|
|
NSUInteger removeIndex;
|
2021-01-27 22:09:09 +00:00
|
|
|
|
|
|
|
while (NSNotFound != index) {
|
|
|
|
if (index >= insertIndex) {
|
2021-01-30 23:14:08 +00:00
|
|
|
removeIndex = index + aboveInsertIndexCount;
|
2021-01-27 22:09:09 +00:00
|
|
|
aboveInsertIndexCount += 1;
|
|
|
|
} else {
|
2021-01-30 23:14:08 +00:00
|
|
|
removeIndex = index;
|
2021-01-27 22:09:09 +00:00
|
|
|
insertIndex -= 1;
|
|
|
|
}
|
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
object = objects[removeIndex];
|
2021-01-27 22:09:09 +00:00
|
|
|
|
|
|
|
[self removeObjectAtArrangedObjectIndex:removeIndex];
|
|
|
|
[self insertObject:object atArrangedObjectIndex:insertIndex];
|
|
|
|
|
|
|
|
index = [indexSet indexLessThanIndex:index];
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 05:48:23 +00:00
|
|
|
- (void)moveObjectsFromIndex:(NSUInteger)fromIndex
|
|
|
|
toArrangedObjectIndexes:(NSIndexSet *)indexSet {
|
2022-01-22 00:11:24 +00:00
|
|
|
__block NSUInteger rangeCount = 0;
|
|
|
|
__block NSUInteger firstIndex = 0;
|
|
|
|
__block NSUInteger _fromIndex = fromIndex;
|
|
|
|
[indexSet enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
|
|
|
|
if (++rangeCount == 1)
|
|
|
|
firstIndex = range.location;
|
|
|
|
if (_fromIndex >= range.location) {
|
|
|
|
if (_fromIndex < range.location + range.length)
|
|
|
|
_fromIndex = range.location;
|
|
|
|
else
|
|
|
|
_fromIndex -= range.length;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (rangeCount == 1 &&
|
|
|
|
(fromIndex >= firstIndex &&
|
|
|
|
fromIndex < firstIndex + [indexSet count])) // Null operation
|
|
|
|
return;
|
|
|
|
|
|
|
|
fromIndex = _fromIndex;
|
|
|
|
|
2022-01-21 23:00:27 +00:00
|
|
|
NSArray *objects = [[self arrangedObjects] subarrayWithRange:NSMakeRange(fromIndex, [indexSet count])];
|
2022-01-21 05:48:23 +00:00
|
|
|
NSUInteger index = [indexSet firstIndex];
|
|
|
|
|
|
|
|
NSUInteger itemIndex = 0;
|
|
|
|
id object;
|
|
|
|
|
2022-01-21 23:00:27 +00:00
|
|
|
fromIndex += [objects count];
|
|
|
|
for (NSUInteger i = 0; i < [objects count]; i++) {
|
|
|
|
[self removeObjectAtArrangedObjectIndex:--fromIndex];
|
2022-01-21 05:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (NSNotFound != index) {
|
2022-01-21 23:00:27 +00:00
|
|
|
object = objects[itemIndex++];
|
2022-01-21 05:48:23 +00:00
|
|
|
|
|
|
|
[self insertObject:object atArrangedObjectIndex:index];
|
|
|
|
|
|
|
|
index = [indexSet indexGreaterThanIndex:index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
|
|
|
|
@end
|