diff options
author | ritsuka <[email protected]> | 2014-08-02 16:14:16 +0000 |
---|---|---|
committer | ritsuka <[email protected]> | 2014-08-02 16:14:16 +0000 |
commit | e51d2ad08ee494c46ef1d97260437f9462f69ee2 (patch) | |
tree | d306dc2f9fefa1e76aa625d5426ceedf50a72c7c /macosx/HBLanguagesSelection.m | |
parent | 969fe98fc90991e61f74720221f53bd746fbd5d4 (diff) |
MacGui: add drag & drop to rearrange languages in the subtitles default panel.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6258 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'macosx/HBLanguagesSelection.m')
-rw-r--r-- | macosx/HBLanguagesSelection.m | 140 |
1 files changed, 138 insertions, 2 deletions
diff --git a/macosx/HBLanguagesSelection.m b/macosx/HBLanguagesSelection.m index 0ffe3e52a..c57df8bbe 100644 --- a/macosx/HBLanguagesSelection.m +++ b/macosx/HBLanguagesSelection.m @@ -36,8 +36,8 @@ if (self) { NSMutableArray *internal = [[NSMutableArray alloc] init]; + NSMutableArray *selected = [[NSMutableArray alloc] init]; - int insertedItems = 0; const iso639_lang_t *lang = lang_get_next(NULL); for (lang = lang_get_next(lang); lang != NULL; lang = lang_get_next(lang)) { @@ -48,7 +48,7 @@ if ([languages containsObject:item.iso639_2]) { item.isSelected = YES; - [internal insertObject:item atIndex:insertedItems++]; + [selected addObject:item]; } else { @@ -65,6 +65,16 @@ } [internal insertObject:item atIndex:0]; + // Insert the selected items + // in the original order. + [selected sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { + return [languages indexOfObject:[obj1 iso639_2]] - [languages indexOfObject:[obj2 iso639_2]]; + }]; + + [internal insertObjects:selected + atIndexes:[NSIndexSet indexSetWithIndexesInRange:(NSMakeRange(1, selected.count))]]; + [selected release]; + _languagesArray = internal; } @@ -122,4 +132,130 @@ NSString *kHBLanguagesDragRowsType = @"kHBLanguagesDragRowsType"; return [super arrangeObjects:filteredObjects]; } +- (void) awakeFromNib +{ + [self.tableView registerForDraggedTypes:@[kHBLanguagesDragRowsType]]; + self.isDragginEnabled = YES; +} + +- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard +{ + if (self.isDragginEnabled) + { + NSData *data = nil; + if (self.showSelectedOnly) + { + // If the show selected only filter + // is enabled, we need to modify the rowIndexes + // to match the position of the elements in the unfiltered array + NSArray *filteredArray = [[self arrangedObjects] copy]; + self.showSelectedOnly = NO; + NSArray *unfilteredArray = [self arrangedObjects]; + + NSMutableIndexSet *unfilteredIndexes = [NSMutableIndexSet indexSet]; + NSUInteger currentIndex = [rowIndexes firstIndex]; + while (currentIndex != NSNotFound) + { + NSUInteger newIndex = [unfilteredArray indexOfObject:[filteredArray objectAtIndex:currentIndex]]; + [unfilteredIndexes addIndex:newIndex]; + currentIndex = [rowIndexes indexGreaterThanIndex:currentIndex]; + } + + data = [NSKeyedArchiver archivedDataWithRootObject:unfilteredIndexes]; + [filteredArray release]; + } + else + { + data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes]; + } + + [pboard declareTypes:[NSArray arrayWithObject:kHBLanguagesDragRowsType] owner:self]; + [pboard setData:data forType:kHBLanguagesDragRowsType]; + } + + return self.isDragginEnabled; +} + +- (NSDragOperation)tableView:(NSTableView *)view + validateDrop:(id <NSDraggingInfo>)info + proposedRow:(NSInteger)row + proposedDropOperation:(NSTableViewDropOperation)operation +{ + NSDragOperation dragOp = NSDragOperationNone; + + // if drag source is our own table view, it's a move or a copy + if ([info draggingSource] == view) + { + // At a minimum, allow move + dragOp = NSDragOperationMove; + } + + [view setDropRow:row dropOperation:NSTableViewDropAbove]; + + return dragOp; +} + +- (BOOL)tableView:(NSTableView *)view acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation +{ + if (([info draggingSourceOperationMask] == NSDragOperationCopy)) + { + return nil; + } + + if ([info draggingSource] == view) + { + // Get the index set from the pasteBoard. + NSData *rowData = [[info draggingPasteboard] dataForType:kHBLanguagesDragRowsType]; + NSIndexSet *indexSet = [NSKeyedUnarchiver unarchiveObjectWithData:rowData]; + + NSUInteger i = [indexSet countOfIndexesInRange:NSMakeRange(0, row)]; + + // Rearrage the objects. + [self moveObjectsInArrangedObjectsFromIndexes:indexSet toIndex:row]; + + // Update the selection. + row -= i; + NSIndexSet *selectionSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(row, [indexSet count])]; + [view selectRowIndexes:selectionSet byExtendingSelection:NO]; + + return YES; + } + + return NO; +} + +- (void)moveObjectsInArrangedObjectsFromIndexes:(NSIndexSet *)indexSet toIndex:(NSUInteger)insertIndex +{ + NSArray *objects = [self arrangedObjects]; + + NSInteger aboveInsertIndexCount = 0; + NSInteger removeIndex; + + NSUInteger thisIndex = [indexSet lastIndex]; + while (thisIndex != NSNotFound) + { + if (thisIndex >= insertIndex) + { + removeIndex = thisIndex + aboveInsertIndexCount; + aboveInsertIndexCount += 1; + } + else + { + removeIndex = thisIndex; + insertIndex -= 1; + } + + // Get the object we're moving + id object = objects[removeIndex]; + + // Move the object + [object retain]; + [self removeObjectAtArrangedObjectIndex:removeIndex]; + [self insertObject:object atArrangedObjectIndex:insertIndex]; + [object release]; + + thisIndex = [indexSet indexLessThanIndex:thisIndex]; + } +} + @end |