- improved the CPOutlineView drag & drop support in cases where more than 1 parent is possible

- improved the outline view test project
This commit is contained in:
Klaas Pieter Annema committed 2010-02-04 16:56:48 +01:00
1 parent 117ec9970c
commit 7720200679
4 files changed
+138 -153

No files matched your search

+48 -80
View File
@@ -500,13 +500,27 @@ var CPOutlineViewDelegate_outlineView_dataViewForTableColumn_item_
return [super frameOfDataViewAtColumn:aColumn row:aRow];
}
- (id)_parentItemForRow:(int)theLowerRow andUpperRow:(int)theUpperRow atMouseOffset:(float)theXOffset
{
var level = [self levelForRow:theLowerRow],
upperLevel = [self levelForRow:theUpperRow];
if (upperLevel > level)
if (theXOffset > (level + 1) * [self indentationPerLevel])
return [self parentForItem:[self itemAtRow:theUpperRow]];
return [self parentForItem:[self itemAtRow:theLowerRow]];
}
- (CPRect)_rectForDropHighlightViewBetweenUpperRow:(int)theUpperRowIndex andLowerRow:(int)theLowerRowIndex offset:(float)theXOffset
{
// Just call super and update the x to reflect the current indentation level
var level = [self levelForRow:theUpperRowIndex],
rect = [super _rectForDropHighlightViewBetweenUpperRow:theUpperRowIndex andLowerRow:theLowerRowIndex offset:theXOffset];
rect.origin.x = level * [self indentationPerLevel];
// Call super and the x to reflect the current indentation level
var rect = [super _rectForDropHighlightViewBetweenUpperRow:theUpperRowIndex andLowerRow:theLowerRowIndex offset:theXOffset],
parentItem = [self _parentItemForRow:theLowerRowIndex andUpperRow:theUpperRowIndex atMouseOffset:theXOffset],
level = [self levelForItem:parentItem];
rect.origin.x = (level + 1) * [self indentationPerLevel];
return rect;
}
@@ -826,36 +840,31 @@ var _loadItemInfoForItem = function(/*CPOutlineView*/ anOutlineView, /*id*/ anIt
return [_outlineView._outlineViewDataSource outlineView:_outlineView writeItems:items toPasteboard:thePasteboard];
}
- (int)_childIndexForDropOperation:(CPTableViewDropOperation)theDropOperation item:(id)theItem
- (int)_childIndexForDropOperation:(CPTableViewDropOperation)theDropOperation row:(int)theRow offset:(CPPoint)theXOffset
{
var parentItem = [_outlineView parentForItem:theItem],
childIndex = CPNotFound;
if (theDropOperation === CPTableViewDropAbove)
{
var itemInfo = (parentItem !== nil) ? _outlineView._itemInfosForItems[[parentItem UID]] : _outlineView._rootItemInfo,
children = itemInfo.children;
childIndex = [children indexOfObject:theItem];
// When no child is found the index we want to add the item below all it's children
// Think about dragging an item below the collectionview
if (childIndex === -1)
childIndex = [children count];
}
else if (theOperation === CPTableViewDropOn)
childIndex = -1;
return childIndex;
var childIndex = CPNotFound;
if (theDropOperation === CPTableViewDropAbove)
{
var parentItem = [_outlineView _parentItemForRow:theRow andUpperRow:theRow - 1 atMouseOffset:theXOffset],
itemInfo = (parentItem !== nil) ? _outlineView._itemInfosForItems[[parentItem UID]] : _outlineView._rootItemInfo,
children = itemInfo.children;
childIndex = [children indexOfObject:[_outlineView itemAtRow:theRow]];
}
else if (theDropOperation === CPTableViewDropOn)
childIndex = -1;
return childIndex;
}
- (void)_parentItemForDropOperation:(CPTableViewDropOperation)theDropOperation item:(id)theItem
- (void)_parentItemForDropOperation:(CPTableViewDropOperation)theDropOperation row:(int)theRow offset:(CPPoint)theXOffset
{
if (theDropOperation === CPTableViewDropAbove)
return [_outlineView parentForItem:theItem];
else
return theItem;
if (theDropOperation === CPTableViewDropAbove)
return [_outlineView _parentItemForRow:theRow andUpperRow:theRow - 1 atMouseOffset:theXOffset]
return [_outlineView itemAtRow:theRow];
}
- (CPDragOperation)tableView:(CPTableView)aTableView validateDrop:(id < CPDraggingInfo >)theInfo
@@ -863,31 +872,10 @@ var _loadItemInfoForItem = function(/*CPOutlineView*/ anOutlineView, /*id*/ anIt
{
if (!(_outlineView._implementedOutlineViewDataSourceMethods & CPOutlineViewDataSource_outlineView_validateDrop_proposedItem_proposedChildIndex_))
return CPDragOperationNone;
var childIndex = [self _childIndexForDropOperation:theOperation item:[_outlineView itemAtRow:theRow]],
parentItem = [self _parentItemForDropOperation:theOperation item:[_outlineView itemAtRow:theRow]];
// var droppedItem = [_outlineView itemAtRow:theRow],
// parentItem = [_outlineView parentForItem:droppedItem];
// childIndex = CPNotFound;
//
// if (theOperation === CPTableViewDropAbove)
// {
// var itemInfo = (parentItem != nil) ? _outlineView._itemInfosForItems[[parentItem UID]] : _outlineView._rootItemInfo,
// children = itemInfo.children,
//
// childIndex = [children indexOfObject:droppedItem];
//
// // When no child is found the index we want to add the item below all it's children
// // Think about dragging an item below the collectionview
// if (childIndex === -1)
// childIndex = [children count];
// }
// else if (theOperation === CPTableViewDropOn)
// {
// parentItem = droppedItem;
// childIndex = -1;
// }
var location = [_outlineView convertPoint:[theInfo draggingLocation] fromView:nil],
childIndex = [self _childIndexForDropOperation:theOperation row:theRow offset:location.x],
parentItem = [self _parentItemForDropOperation:theOperation row:theRow offset:location.x];
return [_outlineView._outlineViewDataSource outlineView:_outlineView validateDrop:theInfo proposedItem:parentItem proposedChildIndex:childIndex];
}
@@ -896,30 +884,10 @@ var _loadItemInfoForItem = function(/*CPOutlineView*/ anOutlineView, /*id*/ anIt
{
if (!(_outlineView._implementedOutlineViewDataSourceMethods & CPOutlineViewDataSource_outlineView_acceptDrop_item_childIndex_))
return NO;
// var droppedItem = [_outlineView itemAtRow:theRow],
// parentItem = [_outlineView parentForItem:droppedItem];
// childIndex = CPNotFound;
//
// if (theOperation === CPTableViewDropAbove)
// {
// var itemInfo = (parentItem != nil) ? _outlineView._itemInfosForItems[[parentItem UID]] : _outlineView._rootItemInfo,
// children = itemInfo.children,
//
// childIndex = [children indexOfObject:droppedItem];
//
// // When no child is found the index we want to add the item below all it's children
// // Think about dragging an item below the collectionview
// if (childIndex === -1)
// childIndex = [children count];
// }
// else if (theOperation === CPTableViewDropOn)
// {
// parentItem = droppedItem;
// childIndex = -1;
// }
var childIndex = [self _childIndexForDropOperation:theOperation item:[_outlineView itemAtRow:theRow]],
parentItem = [self _parentItemForDropOperation:theOperation item:[_outlineView itemAtRow:theRow]];
var location = [_outlineView convertPoint:[theInfo draggingLocation] fromView:nil],
childIndex = [self _childIndexForDropOperation:theOperation row:theRow offset:location.x],
parentItem = [self _parentItemForDropOperation:theOperation row:theRow offset:location.x];
return [_outlineView._outlineViewDataSource outlineView:_outlineView acceptDrop:theInfo item:parentItem childIndex:childIndex];
}
+25 -22
View File
@@ -2498,7 +2498,7 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
- (CPDragOperation)draggingEntered:(id)sender
{
var location = [self convertPoint:[sender draggingLocation] fromView:nil],
dropOperation = [self _proposedDropOperation],
dropOperation = [self _proposedDropOperationAtPoint:location],
row = [self _proposedRowAtPoint:location];
if(_retargetedDropRow !== nil)
@@ -2538,7 +2538,7 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
_retargetedDropOperation = nil;
_retargetedDropRow = nil;
_draggedRowIndexes = [CPIndexSet indexSet];
[_dropOperationFeedbackView setHidden:YES];
[_dropOperationFeedbackView setHidden:YES];
}
/*
@ignore
@@ -2551,14 +2551,23 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
/*
@ignore
*/
- (CPTableViewDropOperation)_proposedDropOperation
- (CPTableViewDropOperation)_proposedDropOperationAtPoint:(CGPoint)theDragPoint
{
//check is something is forced...
// otherwise we use the above action by default
if(_retargetedDropOperation !== nil)
if(_retargetedDropOperation !== nil)
return _retargetedDropOperation;
else
return CPTableViewDropAbove;
var row = [self rowAtPoint:theDragPoint],
rowRect = [self rectOfRow:row];
// If there is no (the default) or to little inter cell spacing we create some room for the CPTableViewDropAbove indicator
// This probably doesn't work if the row height is smaller than or around 5.0
if ([self intercellSpacing].height < 5.0)
rowRect = CPRectInset(rowRect, 0.0, 5.0 - [self intercellSpacing].height);
if (CGRectContainsPoint(rowRect, theDragPoint))
return CPTableViewDropOn;
return CPTableViewDropAbove;
}
/*
@@ -2569,14 +2578,9 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
var numberOfRows = [self numberOfRows],
row = [self rowAtPoint:dragPoint];
// cocoa seems to jump to the next row when we approach the below row
// cocoa seems to jump to the next row when we approach the below row
dragPoint.y += FLOOR(CPRectGetHeight([self rectOfRow:row]) / 4.0);
row = [self rowAtPoint:dragPoint];
// Check if we are dragging outside the tableview
// because we want the drag highlight to be below the last row
if (row === -1)
row = numberOfRows + 1;
return row;
}
@@ -2591,13 +2595,13 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
- (CPRect)_rectForDropHighlightViewBetweenUpperRow:(int)theUpperRowIndex andLowerRow:(int)theLowerRowIndex offset:(float)theXOffset
{
return [self rectOfRow:theUpperRowIndex];
return [self rectOfRow:theLowerRowIndex];
}
- (CPDragOperation)draggingUpdated:(id)sender
{
var location = [self convertPoint:[sender draggingLocation] fromView:nil],
dropOperation = [self _proposedDropOperation],
dropOperation = [self _proposedDropOperationAtPoint:location],
numberOfRows = [self numberOfRows];
var row = [self _proposedRowAtPoint:location],
@@ -2620,7 +2624,7 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
[_dropOperationFeedbackView setDropOperation:dropOperation];
[_dropOperationFeedbackView setHidden:(dragOperation == CPDragOperationNone)];
[_dropOperationFeedbackView setFrame:[self _rectForDropHighlightViewBetweenUpperRow:row andLowerRow:row + 1 offset:location.x]];
[_dropOperationFeedbackView setFrame:[self _rectForDropHighlightViewBetweenUpperRow:row - 1 andLowerRow:row offset:location.x]];
[_dropOperationFeedbackView setCurrentRow:row];
[self addSubview:_dropOperationFeedbackView];
@@ -2651,12 +2655,11 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
- (BOOL)performDragOperation:(id)sender
{
var location = [self convertPoint:[sender draggingLocation] fromView:nil];
operation = [self _proposedDropOperation];
operation = [self _proposedDropOperationAtPoint:location],
row = _retargetedDropRow;
if(_retargetedDropRow !== nil)
var row = _retargetedDropRow;
else
var row = [self rowAtPoint:location];
if(row === nil)
var row = [self _proposedRowAtPoint:location];
return [_dataSource tableView:self acceptDrop:sender row:row dropOperation:operation];
}
+31 -17
View File
@@ -49,26 +49,36 @@ CustomOutlineViewDragType = @"CustomOutlineViewDragType";
- (CPString)description
{
return [self title];
return [self title];
}
- (void)insertSubmenu:(Menu)theItem atIndex:(int)theIndex
{
CPLog.debug(@"insert menu: %@ in menu: %@ at index: %i", theItem, self, theIndex);
// CPLog.debug(@"insert menu: %@ in menu: %@ at index: %i", theItem, self, theIndex);
if ([[self children] containsObject:theItem])
return;
if ([theItem menu])
[theItem removeFromMenu];
[theItem setMenu:self];
[[self children] insertObject:theItem atIndex:theIndex];
if (theIndex === -1)
{
[[self children] addObject:theItem];
}
else
{
[[self children] insertObject:theItem atIndex:theIndex];
}
// CPLog.debug(@"%@ children: %@", self, [self children]);
}
- (void)removeFromMenu
{
CPLog.debug(@"remove menu: %@ from menu: %@", self, [self menu]);
// CPLog.debug(@"remove menu: %@ from menu: %@", self, [self menu]);
[[[self menu] children] removeObject:self];
@@ -79,6 +89,9 @@ CustomOutlineViewDragType = @"CustomOutlineViewDragType";
- (void)setChildren:(CPArray)theChildren
{
if (theChildren === nil)
theChildren = [];
if (_children === theChildren)
return;
@@ -88,7 +101,7 @@ CustomOutlineViewDragType = @"CustomOutlineViewDragType";
var child = theChildren[childIndex];
[child setMenu:self];
}
_children = theChildren;
}
@@ -183,11 +196,11 @@ CustomOutlineViewDragType = @"CustomOutlineViewDragType";
[_outlineView setDataSource:self];
[_outlineView setAllowsMultipleSelection:YES];
[_outlineView expandItem:nil expandChildren:YES];
[_outlineView expandItem:nil expandChildren:YES];
// [_outlineView setIntercellSpacing:CPSizeMake(0.0, 0.0)]
// [scrollView setDocumentView:_outlineView];
[theWindow setContentView:_outlineView];
[theWindow setContentView:_outlineView];
[theWindow orderFront:self];
}
@@ -243,11 +256,11 @@ CustomOutlineViewDragType = @"CustomOutlineViewDragType";
- (CPDragOperation)outlineView:(CPOutlineView)anOutlineView validateDrop:(id < CPDraggingInfo >)theInfo proposedItem:(id)theItem proposedChildIndex:(int)theIndex
{
if (theItem === nil)
theItem = [self menu];
CPLog.debug(@"validate item: %@ at index: %i", theItem, theIndex);
if (theItem === nil)
theItem = [self menu];
CPLog.debug(@"validate item: %@ at index: %i", theItem, theIndex);
return CPDragOperationEvery;
}
@@ -255,8 +268,8 @@ CustomOutlineViewDragType = @"CustomOutlineViewDragType";
{
if (theItem === nil)
theItem = [self menu];
CPLog.debug(@"drop item: %@ at index: %i", theItem, theIndex);
CPLog.debug(@"drop item: %@ at index: %i", theItem, theIndex);
var menuIndex = [_draggedItems count];
while (menuIndex--)
@@ -265,13 +278,14 @@ CustomOutlineViewDragType = @"CustomOutlineViewDragType";
// CPLog.debug(@"move item: %@ to: %@ index: %@", menu, theItem, theIndex);
if (menu === theItem)
continue;
[menu removeFromMenu];
[theItem insertSubmenu:menu atIndex:theIndex];
theIndex += 1;
}
CPLog.debug([self menu]);
return YES;
}
+34 -34
View File
@@ -66,7 +66,7 @@ CPLogRegister(CPLogConsole);
// [textDataView setValue:CGInsetMake(0, 0, 0, 0) forThemeAttribute:@"focus-inset" inState:CPThemeStateBezeled|CPThemeStateEditing];
//[textDataView setValue:CGSizeMake(1,1) forThemeAttribute:@"text-shadow-offset"];
//[textDataView setValue:[CPColor blackColor] forThemeAttribute:@"text-shadow-color" inState:CPThemeStateHighlighted];
//[textDataView setValue:[CPColor blackColor] forThemeAttribute:@"text-shadow-color" inState:CPThemeStateHighlighted];
// [textDataView setBackgroundColor:[[CPColor redColor] colorWithAlphaComponent:0.5]];
@@ -149,7 +149,7 @@ CPLogRegister(CPLogConsole);
[textDataView setValue:[CPFont systemFontOfSize:12] forThemeAttribute:@"font" inState:CPThemeStateHighlighted];
//[textDataView setValue:CGSizeMake(1,1) forThemeAttribute:@"text-shadow-offset"];
//[textDataView setValue:[CPColor blackColor] forThemeAttribute:@"text-shadow-color" inState:CPThemeStateHighlighted];
//[textDataView setValue:[CPColor blackColor] forThemeAttribute:@"text-shadow-color" inState:CPThemeStateHighlighted];
// [textDataView setBackgroundColor:[[CPColor redColor] colorWithAlphaComponent:0.5]];
@@ -224,30 +224,30 @@ CPLogRegister(CPLogConsole);
//- (void)tableViewSelectionIsChanging:(CPNotification)aNotification
//{
// CPLog.debug(@"changing! %@", [aNotification description]);
// CPLog.debug(@"changing! %@", [aNotification description]);
//}
//
//- (void)tableViewSelectionDidChange:(CPNotification)aNotification
//{
// CPLog.debug(@"did change! %@", [aNotification description]);
// CPLog.debug(@"did change! %@", [aNotification description]);
//}
- (BOOL)tableView:(CPTableView)aTableView shouldSelectRow:(int)rowIndex
{
//CPLog.debug(@"shouldSelectRow %d", rowIndex);
//for (var i = 2, sqrt = SQRT(rowIndex+1); i <= sqrt; i++)
// if ((rowIndex+1) % i === 0)
//return false;
//CPLog.debug(@"shouldSelectRow %d", rowIndex);
//for (var i = 2, sqrt = SQRT(rowIndex+1); i <= sqrt; i++)
// if ((rowIndex+1) % i === 0)
//return false;
// if(rowIndex % 2 == 1)
// return true;
// return true;
// else
return true;
}
- (BOOL)selectionShouldChangeInTableView:(CPTableView)aTableView
{
//CPLog.debug(@"selectionShouldChangeInTableView");
return YES;
//CPLog.debug(@"selectionShouldChangeInTableView");
return YES;
}
- (void)tableViewSelectionDidChange:(id)notification
@@ -267,8 +267,8 @@ CPLogRegister(CPLogConsole);
//- (CPIndexSet)tableView:(CPTableView)tableView selectionIndexesForProposedSelection:(CPIndexSet)proposedSelectionIndexes
//{
// CPLog.debug(@"selectionIndexesForProposedSelection %@", [proposedSelectionIndexes description]);
// return proposedSelectionIndexes;
// CPLog.debug(@"selectionIndexesForProposedSelection %@", [proposedSelectionIndexes description]);
// return proposedSelectionIndexes;
//}
@@ -371,28 +371,28 @@ CPLogRegister(CPLogConsole);
var aboveCount = 0,
object,
removeIndex;
var index = [indexes lastIndex];
var index = [indexes lastIndex];
while (index != CPNotFound)
{
if (index >= insertIndex)
{
removeIndex = index + aboveCount;
aboveCount ++;
}
else
{
removeIndex = index;
insertIndex --;
}
object = [self objectAtIndex:removeIndex];
[self removeObjectAtIndex:removeIndex];
[self insertObject:object atIndex:insertIndex];
index = [indexes indexLessThanIndex:index];
}
{
if (index >= insertIndex)
{
removeIndex = index + aboveCount;
aboveCount ++;
}
else
{
removeIndex = index;
insertIndex --;
}
object = [self objectAtIndex:removeIndex];
[self removeObjectAtIndex:removeIndex];
[self insertObject:object atIndex:insertIndex];
index = [indexes indexLessThanIndex:index];
}
}
@end