From 77d2ebbda0669698c90c822a6bb32932a361db53 Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Fri, 1 Mar 2013 11:57:24 +0100 Subject: [PATCH 1/8] adds shouldReorderColumn to CPTableView delegate methods --- AppKit/CPTableHeaderView.j | 2 +- AppKit/CPTableView.j | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/AppKit/CPTableHeaderView.j b/AppKit/CPTableHeaderView.j index 44c34e1ac..f828843df 100644 --- a/AppKit/CPTableHeaderView.j +++ b/AppKit/CPTableHeaderView.j @@ -455,7 +455,7 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal - (BOOL)_shouldDragTableColumn:(int)aColumnIndex at:(CGPoint)aPoint { - return [_tableView allowsColumnReordering] && ABS(aPoint.x - _mouseDownLocation.x) >= 10.0; + return [_tableView _shouldReorderColumn:aColumnIndex toColumn:-1] && ABS(aPoint.x - _mouseDownLocation.x) >= 10.0; } - (CGRect)_headerRectOfLastVisibleColumn diff --git a/AppKit/CPTableView.j b/AppKit/CPTableView.j index 413468be5..6bc52d8f6 100644 --- a/AppKit/CPTableView.j +++ b/AppKit/CPTableView.j @@ -82,6 +82,7 @@ var CPTableViewDelegate_selectionShouldChangeInTableView_ CPTableViewDelegate_tableViewSelectionDidChange_ = 1 << 18, CPTableViewDelegate_tableViewSelectionIsChanging_ = 1 << 19, CPTableViewDelegate_tableViewMenuForTableColumn_Row_ = 1 << 20; + CPTableViewDelegate_tableView_shouldReorderColumn_toColumn_ = 1 << 21; //CPTableViewDraggingDestinationFeedbackStyles CPTableViewDraggingDestinationFeedbackStyleNone = -1; @@ -998,6 +999,21 @@ NOT YET IMPLEMENTED [self reloadDataForRowIndexes:_exposedRows columnIndexes:[CPIndexSet indexSetWithIndex:[_tableColumns indexOfObject:previouslyDraggedColumn]]]; } +/* + @ignore + Returns YES if the column at columnIndex can be reordered. + It can be possible if column reordering is allowed and if the tableview + delegate also accept the reordering +*/ +- (BOOL)_shouldReorderColumn:(int)columnIndex toColumn:(int)newColumnIndex +{ + if ([self allowsColumnReordering] + && _implementedDelegateMethods & CPTableViewDelegate_tableView_shouldReorderColumn_toColumn_) + return [_delegate tableView:self shouldReorderColumn:columnIndex toColumn:newColumnIndex]; + + return [self allowsColumnReordering]; +} + /* @ignore Same as moveColumn:toColumn: but doesn't trigger an autosave @@ -1011,6 +1027,9 @@ NOT YET IMPLEMENTED if (fromIndex === toIndex) return; + if (![self _shouldReorderColumn:fromIndex toColumn:toIndex]) + return; + if (_dirtyTableColumnRangeIndex < 0) _dirtyTableColumnRangeIndex = MIN(fromIndex, toIndex); else @@ -2575,6 +2594,8 @@ Informs the delegate that the tableview selection has changed. @section movingandresizingcolumsn Moving and Resizing Columns: Return YES if the column at a given index should move to a new column index, otherwise NO. +When a column is initially dragged by the user, the delegate is first called with a newColumnIndex value of -1 + @code - (BOOL)tableView:(CPTableView)tableView shouldReorderColumn:(int)columnIndex toColumn:(int)newColumnIndex; @endcode @@ -2726,6 +2747,10 @@ Your delegate can implement this method to avoid subclassing the tableview to ad if ([_delegate respondsToSelector:@selector(tableView:menuForTableColumn:row:)]) _implementedDelegateMethods |= CPTableViewDelegate_tableViewMenuForTableColumn_Row_; + if ([_delegate respondsToSelector:@selector(tableView:shouldReorderColumn:toColumn:)]) + _implementedDelegateMethods |= CPTableViewDelegate_tableView_shouldReorderColumn_toColumn_; + + if ([_delegate respondsToSelector:@selector(tableViewColumnDidMove:)]) [defaultCenter addObserver:_delegate From 152fd66bbec946be056f4afd24466b89e9608f1e Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Fri, 8 Mar 2013 18:11:27 +0100 Subject: [PATCH 2/8] fixed reordering column with a restriction on a targeted column --- AppKit/CPTableHeaderView.j | 9 ++++++--- AppKit/CPTableView.j | 3 --- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/AppKit/CPTableHeaderView.j b/AppKit/CPTableHeaderView.j index f828843df..7a8e1c565 100644 --- a/AppKit/CPTableHeaderView.j +++ b/AppKit/CPTableHeaderView.j @@ -503,9 +503,12 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal - (void)_moveColumn:(int)aFromIndex toColumn:(int)aToIndex { - [_tableView moveColumn:aFromIndex toColumn:aToIndex]; - _activeColumn = aToIndex; - _pressedColumn = _activeColumn; + if ([_tableView _shouldReorderColumn:aFromIndex toColumn:aToIndex]) + { + [_tableView moveColumn:aFromIndex toColumn:aToIndex]; + _activeColumn = aToIndex; + _pressedColumn = _activeColumn; + } } - (void)draggedView:(CPView)aView beganAt:(CGPoint)aPoint diff --git a/AppKit/CPTableView.j b/AppKit/CPTableView.j index f0787208b..4a5c4dfd1 100644 --- a/AppKit/CPTableView.j +++ b/AppKit/CPTableView.j @@ -1027,9 +1027,6 @@ NOT YET IMPLEMENTED if (fromIndex === toIndex) return; - if (![self _shouldReorderColumn:fromIndex toColumn:toIndex]) - return; - if (_dirtyTableColumnRangeIndex < 0) _dirtyTableColumnRangeIndex = MIN(fromIndex, toIndex); else From 159941696d77c403660d42e57220455ebd85a69f Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Fri, 8 Mar 2013 19:35:10 +0100 Subject: [PATCH 3/8] changed condition order for optimization --- AppKit/CPTableHeaderView.j | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AppKit/CPTableHeaderView.j b/AppKit/CPTableHeaderView.j index 7a8e1c565..e2e13539d 100644 --- a/AppKit/CPTableHeaderView.j +++ b/AppKit/CPTableHeaderView.j @@ -455,7 +455,7 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal - (BOOL)_shouldDragTableColumn:(int)aColumnIndex at:(CGPoint)aPoint { - return [_tableView _shouldReorderColumn:aColumnIndex toColumn:-1] && ABS(aPoint.x - _mouseDownLocation.x) >= 10.0; + return ABS(aPoint.x - _mouseDownLocation.x) >= 10.0 && [_tableView _shouldReorderColumn:aColumnIndex toColumn:-1]; } - (CGRect)_headerRectOfLastVisibleColumn From fe260a85f55a890d3fdac61f7137eb38ae4b1da3 Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Sun, 10 Mar 2013 16:41:37 +0100 Subject: [PATCH 4/8] added _lastDragDestinationColumnIndex to call delegate method only once --- AppKit/CPTableHeaderView.j | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/AppKit/CPTableHeaderView.j b/AppKit/CPTableHeaderView.j index e2e13539d..f9840f946 100644 --- a/AppKit/CPTableHeaderView.j +++ b/AppKit/CPTableHeaderView.j @@ -229,6 +229,7 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal CGPoint _previousTrackingLocation; int _activeColumn; int _pressedColumn; + int _lastDragDestinationColumnIndex; BOOL _isResizing; BOOL _isDragging; @@ -417,6 +418,7 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal - (void)startTrackingTableColumn:(int)aColumnIndex at:(CGPoint)aPoint { + _lastDragDestinationColumnIndex = -1; [self _setPressedColumn:aColumnIndex]; } @@ -544,14 +546,22 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal var hoveredColumn = [self columnAtPoint:hoverPoint]; - if (hoveredColumn !== -1) + if (hoveredColumn !== _lastDragDestinationColumnIndex) { var columnRect = [self headerRectOfColumn:hoveredColumn], columnCenterPoint = [self convertPoint:CGPointMake(_CGRectGetMidX(columnRect), _CGRectGetMidY(columnRect)) fromView:self]; if (hoveredColumn < _activeColumn && hoverPoint.x < columnCenterPoint.x) + { [self _moveColumn:_activeColumn toColumn:hoveredColumn]; + _lastDragDestinationColumnIndex = hoveredColumn; + } else if (hoveredColumn > _activeColumn && hoverPoint.x > columnCenterPoint.x) + { [self _moveColumn:_activeColumn toColumn:hoveredColumn]; + _lastDragDestinationColumnIndex = hoveredColumn; + } + + } _previousTrackingLocation = aPoint; From 85cb1b7a76aa37493e3636696a667775a6b21262 Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Sun, 10 Mar 2013 17:04:45 +0100 Subject: [PATCH 5/8] removed an extra blank and changed _shouldReorderColumn format --- AppKit/CPTableView.j | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/AppKit/CPTableView.j b/AppKit/CPTableView.j index 4a5c4dfd1..0306ad670 100644 --- a/AppKit/CPTableView.j +++ b/AppKit/CPTableView.j @@ -1007,9 +1007,11 @@ NOT YET IMPLEMENTED */ - (BOOL)_shouldReorderColumn:(int)columnIndex toColumn:(int)newColumnIndex { - if ([self allowsColumnReordering] - && _implementedDelegateMethods & CPTableViewDelegate_tableView_shouldReorderColumn_toColumn_) - return [_delegate tableView:self shouldReorderColumn:columnIndex toColumn:newColumnIndex]; + if ([self allowsColumnReordering] && + _implementedDelegateMethods & CPTableViewDelegate_tableView_shouldReorderColumn_toColumn_) + { + return [_delegate tableView:self shouldReorderColumn:columnIndex toColumn:newColumnIndex]; + } return [self allowsColumnReordering]; } @@ -2767,7 +2769,6 @@ Your delegate can implement this method to avoid subclassing the tableview to ad if ([_delegate respondsToSelector:@selector(tableView:shouldReorderColumn:toColumn:)]) _implementedDelegateMethods |= CPTableViewDelegate_tableView_shouldReorderColumn_toColumn_; - if ([_delegate respondsToSelector:@selector(tableViewColumnDidMove:)]) [defaultCenter addObserver:_delegate From ef4ba78ab7405343143a385ea31a291a63e54584 Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Sun, 10 Mar 2013 17:05:53 +0100 Subject: [PATCH 6/8] removed extra blank and added a blank line above if condition --- AppKit/CPTableHeaderView.j | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/AppKit/CPTableHeaderView.j b/AppKit/CPTableHeaderView.j index f9840f946..a3c7dad06 100644 --- a/AppKit/CPTableHeaderView.j +++ b/AppKit/CPTableHeaderView.j @@ -550,6 +550,7 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal { var columnRect = [self headerRectOfColumn:hoveredColumn], columnCenterPoint = [self convertPoint:CGPointMake(_CGRectGetMidX(columnRect), _CGRectGetMidY(columnRect)) fromView:self]; + if (hoveredColumn < _activeColumn && hoverPoint.x < columnCenterPoint.x) { [self _moveColumn:_activeColumn toColumn:hoveredColumn]; @@ -560,8 +561,6 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal [self _moveColumn:_activeColumn toColumn:hoveredColumn]; _lastDragDestinationColumnIndex = hoveredColumn; } - - } _previousTrackingLocation = aPoint; From b7b7d33597354037b53a9c8bc1931dcd2d4566ff Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Mon, 25 Mar 2013 13:22:43 +0100 Subject: [PATCH 7/8] avoid to call the delegate when moving before the first column and after the last column --- AppKit/CPTableHeaderView.j | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/AppKit/CPTableHeaderView.j b/AppKit/CPTableHeaderView.j index a3c7dad06..c9a041ae9 100644 --- a/AppKit/CPTableHeaderView.j +++ b/AppKit/CPTableHeaderView.j @@ -505,6 +505,7 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal - (void)_moveColumn:(int)aFromIndex toColumn:(int)aToIndex { + console.log("_moveColumn"); if ([_tableView _shouldReorderColumn:aFromIndex toColumn:aToIndex]) { [_tableView moveColumn:aFromIndex toColumn:aToIndex]; @@ -546,18 +547,21 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal var hoveredColumn = [self columnAtPoint:hoverPoint]; - if (hoveredColumn !== _lastDragDestinationColumnIndex) + if (hoveredColumn !== _lastDragDestinationColumnIndex && hoveredColumn !== -1) { + var columnRect = [self headerRectOfColumn:hoveredColumn], columnCenterPoint = [self convertPoint:CGPointMake(_CGRectGetMidX(columnRect), _CGRectGetMidY(columnRect)) fromView:self]; if (hoveredColumn < _activeColumn && hoverPoint.x < columnCenterPoint.x) { + console.log(hoveredColumn + " " +_activeColumn); [self _moveColumn:_activeColumn toColumn:hoveredColumn]; _lastDragDestinationColumnIndex = hoveredColumn; } else if (hoveredColumn > _activeColumn && hoverPoint.x > columnCenterPoint.x) { + console.log(hoveredColumn + " " +_activeColumn); [self _moveColumn:_activeColumn toColumn:hoveredColumn]; _lastDragDestinationColumnIndex = hoveredColumn; } From c135905e64a3b077c0b7bb26e3343a479ba394a9 Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Mon, 25 Mar 2013 13:37:49 +0100 Subject: [PATCH 8/8] removed blank line and console messages --- AppKit/CPTableHeaderView.j | 4 ---- 1 file changed, 4 deletions(-) diff --git a/AppKit/CPTableHeaderView.j b/AppKit/CPTableHeaderView.j index c9a041ae9..cd5e43c9b 100644 --- a/AppKit/CPTableHeaderView.j +++ b/AppKit/CPTableHeaderView.j @@ -505,7 +505,6 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal - (void)_moveColumn:(int)aFromIndex toColumn:(int)aToIndex { - console.log("_moveColumn"); if ([_tableView _shouldReorderColumn:aFromIndex toColumn:aToIndex]) { [_tableView moveColumn:aFromIndex toColumn:aToIndex]; @@ -549,19 +548,16 @@ var _CPTableColumnHeaderViewStringValueKey = @"_CPTableColumnHeaderViewStringVal if (hoveredColumn !== _lastDragDestinationColumnIndex && hoveredColumn !== -1) { - var columnRect = [self headerRectOfColumn:hoveredColumn], columnCenterPoint = [self convertPoint:CGPointMake(_CGRectGetMidX(columnRect), _CGRectGetMidY(columnRect)) fromView:self]; if (hoveredColumn < _activeColumn && hoverPoint.x < columnCenterPoint.x) { - console.log(hoveredColumn + " " +_activeColumn); [self _moveColumn:_activeColumn toColumn:hoveredColumn]; _lastDragDestinationColumnIndex = hoveredColumn; } else if (hoveredColumn > _activeColumn && hoverPoint.x > columnCenterPoint.x) { - console.log(hoveredColumn + " " +_activeColumn); [self _moveColumn:_activeColumn toColumn:hoveredColumn]; _lastDragDestinationColumnIndex = hoveredColumn; }