Cog/Playlist/DNDArrayController.m

121 lines
3.5 KiB
Mathematica
Raw Normal View History

2005-06-02 14:16:43 -04:00
#import "DNDArrayController.h"
#import "Logging.h"
2005-06-02 14:16:43 -04:00
@implementation DNDArrayController
2021-01-27 19:09:09 -03:00
NSString *CogPlaylistItemType = @"org.cogx.cog.playlist-item";
NSString *CogUrlsPboardType = @"COG_URLS_TYPE";
2005-06-02 14:16:43 -04:00
// @"CorePasteboardFlavorType 0x6974756E" is the "itun" type representing an iTunes plist
NSString *iTunesDropType = @"CorePasteboardFlavorType 0x6974756E";
2005-06-02 14:16:43 -04:00
- (void)awakeFromNib
{
// register for drag and drop
2021-01-27 19:09:09 -03:00
[self.tableView registerForDraggedTypes:@[CogPlaylistItemType, CogUrlsPboardType,
NSFilenamesPboardType, iTunesDropType]];
2005-06-02 14:16:43 -04:00
}
2021-01-27 19:09:09 -03:00
- (id <NSPasteboardWriting>)tableView:(NSTableView *)tableView
pasteboardWriterForRow:(NSInteger)row
2005-06-02 14:16:43 -04:00
{
2021-01-27 19:09:09 -03:00
NSPasteboardItem *item = [[NSPasteboardItem alloc] init];
[item setString:[@(row) stringValue] forType:CogPlaylistItemType];
return item;
2005-06-02 14:16:43 -04:00
}
2021-01-27 19:09:09 -03:00
- (void)tableView:(NSTableView *)tableView
draggingSession:(NSDraggingSession *)session
willBeginAtPoint:(NSPoint)screenPoint
forRowIndexes:(NSIndexSet *)rowIndexes
{
DLog(@"Drag session started with indexes: %@", rowIndexes);
}
2005-06-02 14:16:43 -04:00
2021-01-27 19:09:09 -03:00
- (NSDragOperation)tableView:(NSTableView*)tableView
validateDrop:(id <NSDraggingInfo>)info
proposedRow:(int)row
proposedDropOperation:(NSTableViewDropOperation)dropOperation
2005-06-02 14:16:43 -04:00
{
2021-01-27 19:09:09 -03:00
NSDragOperation dragOp = NSDragOperationCopy;
if ([info draggingSource] == tableView)
dragOp = NSDragOperationMove;
DLog(@"VALIDATING DROP!");
2005-06-02 14:16:43 -04:00
// we want to put the object at, not over,
2021-01-27 19:09:09 -03:00
// the current row (contrast NSTableViewDropOn)
[tableView setDropRow:row dropOperation:NSTableViewDropAbove];
2005-06-02 14:16:43 -04:00
return dragOp;
}
2021-01-27 19:09:09 -03:00
- (BOOL)tableView:(NSTableView*)tableView
acceptDrop:(id <NSDraggingInfo>)info
row:(int)row
dropOperation:(NSTableViewDropOperation)dropOperation
2005-06-02 14:16:43 -04:00
{
2021-01-27 19:09:09 -03:00
if (row < 0) {
row = 0;
}
NSArray<NSPasteboardItem *> *items = info.draggingPasteboard.pasteboardItems;
2005-06-02 14:16:43 -04:00
// if drag source is self, it's a move
2021-01-27 19:09:09 -03:00
if ([info draggingSource] == tableView || items == nil) {
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSPasteboardItem *item in items) {
[indexSet addIndex:[[item stringForType:CogPlaylistItemType] intValue]];
}
if ([indexSet count] > 0) {
DLog(@"INDEX SET ON DROP: %@", indexSet);
NSArray *selected = [[self arrangedObjects] objectsAtIndexes:indexSet];
[self moveObjectsInArrangedObjectsFromIndexes:indexSet toIndex:row];
[self setSelectedObjects:selected];
DLog(@"ACCEPTING DROP!");
return YES;
}
}
DLog(@"REJECTING DROP!");
return NO;
2005-06-02 14:16:43 -04:00
}
2021-01-27 19:09:09 -03:00
-(void) moveObjectsInArrangedObjectsFromIndexes:(NSIndexSet *)indexSet
toIndex:(unsigned int)insertIndex
{
2021-01-27 19:09:09 -03:00
NSArray *objects = [self arrangedObjects];
NSUInteger index = [indexSet lastIndex];
int aboveInsertIndexCount = 0;
id object;
int removeIndex;
while (NSNotFound != index) {
if (index >= insertIndex) {
removeIndex = (int)(index + aboveInsertIndexCount);
aboveInsertIndexCount += 1;
} else {
removeIndex = (int)index;
insertIndex -= 1;
}
object = [objects objectAtIndex:removeIndex];
[self removeObjectAtArrangedObjectIndex:removeIndex];
[self insertObject:object atArrangedObjectIndex:insertIndex];
index = [indexSet indexLessThanIndex:index];
2005-06-02 14:16:43 -04:00
}
}
@end