MacOSX/VLC_app: Support drag and drop to the category view.
authorPierre d'Herbemont <pdherbemont@videolan.org>
Wed, 2 Jan 2008 17:18:21 +0000 (17:18 +0000)
committerPierre d'Herbemont <pdherbemont@videolan.org>
Wed, 2 Jan 2008 17:18:21 +0000 (17:18 +0000)
extras/MacOSX/VLC_app/Sources/VLCMainWindow.m
extras/MacOSX/VLC_app/Sources/VLCMediaArrayController.m

index 4476482..c285497 100644 (file)
@@ -31,7 +31,7 @@
 /******************************************************************************
  * VLCMainWindow (MasterViewDataSource)
  */
-@implementation VLCMainWindow (MasterViewDataSource)
+@implementation VLCMainWindow (MasterViewDelegate)
 - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
 {
     return [[item representedObject] isKindOfClass:[NSDictionary class]];
 }
 @end
 
+@implementation VLCMainWindow (MasterViewDataSource)
+/* Drag and drop */
+- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id < NSDraggingInfo >)info item:(id)item childIndex:(NSInteger)index
+{
+    int i;
+
+    if(![item respondsToSelector:@selector(representedObject)])
+        return NO;
+    
+    NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:@"VLCMediaURLType"];
+    if( !droppedItems )
+        droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
+    if( !droppedItems )
+        droppedItems = [[info draggingPasteboard] propertyListForType:NSURLPboardType];
+
+    NSAssert( droppedItems, @"Dropped an unsupported object type on the outline View" );
+
+    VLCMediaList * mediaList = [(VLCMedia *)[item representedObject] subitems];
+
+    for (i = 0; i < [droppedItems count]; i++)
+    {
+        NSString * filename = [droppedItems objectAtIndex:i];
+               VLCMedia *media = [VLCMedia mediaWithPath:filename];
+        [mediaList lock];
+               [mediaList insertMedia:media atIndex:index+1];
+        [mediaList unlock];
+    }
+    return YES;
+}
+
+- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
+{
+    NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:@"VLCMediaURLType"];
+    if( !droppedItems )
+        droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
+    if( !droppedItems )
+        droppedItems = [[info draggingPasteboard] propertyListForType:NSURLPboardType];
+
+    if(! droppedItems ||
+       ![item respondsToSelector:@selector(representedObject)] ||
+       ![[item representedObject] isKindOfClass:[VLCMedia class]] )
+    {
+        return NSDragOperationNone;
+    }
+
+    return NSDragOperationMove;
+}
+@end
+
 /******************************************************************************
  * VLCMainWindow
  */
 - (void)awakeFromNib;
 {
     NSTableColumn * tableColumn;
-    
-    /* This one will be removable, so we keep a reference of it so we can safely call removeFromSuperview */
-    [navigatorView retain];    
 
     /* Check ib outlets */
     NSAssert( mainSplitView, @"No split view or wrong split view");
     [categoryList setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];
     [categoryList setDelegate:self];
 
+    [categoryList registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, @"VLCMediaURLType", nil]];
+    [categoryList setDataSource: self];
 
     /***********************************
      * detailList setup
index 3c7e82e..bcd9923 100644 (file)
@@ -51,7 +51,7 @@
 - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info 
                                 proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
 {
-    return [contentMediaList isReadOnly] ? NSDragOperationNone : NSDragOperationGeneric;
+    return [contentMediaList isReadOnly] || op == NSTableViewDropOn ? NSDragOperationNone : NSDragOperationGeneric;
 }
 
 - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
     int i;
     row = 0;
     NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
-    
+    if( !droppedItems )
+        droppedItems = [[info draggingPasteboard] propertyListForType:NSURLPboardType];
     for (i = 0; i < [droppedItems count]; i++)
     {
         NSString * filename = [droppedItems objectAtIndex:i];
                VLCMedia *media = [VLCMedia mediaWithPath:filename];
         [contentMediaList lock];
-               [contentMediaList insertMedia:media atIndex:[contentMediaList count] > 0 ? [contentMediaList count]-1 : 0];
+               [contentMediaList insertMedia:media atIndex:row];
         [contentMediaList unlock];
     }
     return YES;
 }
+
+- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard
+{
+    NSMutableArray *array = [NSMutableArray arrayWithCapacity:[rowIndexes count]];
+    int i = [rowIndexes firstIndex];
+    do {
+        [array addObject:[[contentMediaList mediaAtIndex:i] url]];
+    } while ((i = [rowIndexes indexGreaterThanIndex:i]) != NSNotFound);
+
+    [pboard declareTypes:[NSArray arrayWithObject:@"VLCMediaURLType"] owner:self];
+    [pboard setPropertyList:array forType:@"VLCMediaURLType"];
+    return YES;
+}
 @end