Finished to implement the NSTable/OutlineView sort descriptor support.

More changes are needed to get proper sorting support (e.g. visual feedback).


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@29293 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
qmathe 2010-01-16 14:45:13 +00:00
parent fd57feed5b
commit 239eec74dc
6 changed files with 177 additions and 21 deletions

View file

@ -1,3 +1,26 @@
2010-01-16 Quentin Mathe <quentin.mathe@gmail.com>
Finished to implement the NSTable/OutlineView sort descriptor support.
More changes are needed to get proper sorting support (e.g. visual
feedback).
* Headers/NSTableView.h:
Added _sortDescriptors ivar.
(-tableView:sortDescriptorsDidChange:): Declared.
* Source/NSTableView.m
(-_clickTableColumn:, -_didChangeSortDescriptors:): Added.
(-_initDefaults, -dealloc): Modified to initialize/release
_sortDescriptors ivar.
(-initWithCoder:, -encodeWithCoder:): Modified to support archiving
the sort descriptors.
(-setSortSelectors:, -sortSelectors): Implemented.
* Source/NSTableHeaderView.m (-mouseDown:): Modified to now call
-_clickTableColumn which prepares the sort descriptors among other
things. -_didClickTableColumn: will be called by -_clickTableColumn:.
* Headers/NSOutlineView.h (-outlineView:sortDescriptorsDidChange:,
-outlineView:didClickTableColumn:): Declared.
* Source/NSOutlineView.m (-_didClickTableColumn:,
-_didChangeSortDescriptors:): Added overriden NSTableView methods.
2010-01-15 German Arias <german@xelalug.org> 2010-01-15 German Arias <german@xelalug.org>
* Source/NSImage.m: Changes to fix bug #28580. * Source/NSImage.m: Changes to fix bug #28580.

View file

@ -264,6 +264,14 @@ willDisplayOutlineCell: (id)cell
* the selection is allowed and NO, if not. * the selection is allowed and NO, if not.
*/ */
- (BOOL) selectionShouldChangeInOutlineView: (NSOutlineView *)outlineView; - (BOOL) selectionShouldChangeInOutlineView: (NSOutlineView *)outlineView;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_3, GS_API_LATEST)
- (void) outlineView: (NSOutlineView *)outlineView
sortDescriptorsDidChange: (NSArray *)oldSortDescriptors;
- (void) outlineView: (NSOutlineView *)outlineView
didClickTableColumn: (NSTableColumn *)aTableColumn;
#endif
@end @end
#endif /* _GNUstep_H_NSOutlineView */ #endif /* _GNUstep_H_NSOutlineView */

View file

@ -104,6 +104,7 @@ typedef enum _NSTableViewColumnAutoresizingStyle
BOOL _autosaveTableColumns; BOOL _autosaveTableColumns;
NSString *_autosaveName; NSString *_autosaveName;
BOOL _verticalMotionDrag; BOOL _verticalMotionDrag;
NSArray *_sortDescriptors;
/* /*
* Ivars Acting as Cache * Ivars Acting as Cache
@ -318,8 +319,10 @@ typedef enum _NSTableViewColumnAutoresizingStyle
#endif #endif
/* sorting */ /* sorting */
#if OS_API_VERSION(MAC_OS_X_VERSION_10_3, GS_API_LATEST)
- (void) setSortDescriptors: (NSArray *)array; - (void) setSortDescriptors: (NSArray *)array;
- (NSArray *) sortDescriptors; - (NSArray *) sortDescriptors;
#endif
@end /* interface of NSTableView */ @end /* interface of NSTableView */
@ -375,12 +378,16 @@ APPKIT_EXPORT NSString *NSTableViewSelectionIsChangingNotification;
@interface NSObject (NSTableViewDelegate) @interface NSObject (NSTableViewDelegate)
- (BOOL) selectionShouldChangeInTableView: (NSTableView *)aTableView; - (BOOL) selectionShouldChangeInTableView: (NSTableView *)aTableView;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_3, GS_API_LATEST)
- (void) tableView: (NSTableView*)tableView
sortDescriptorsDidChange: (NSArray *)oldSortDescriptors;
- (void) tableView: (NSTableView*)tableView - (void) tableView: (NSTableView*)tableView
didClickTableColumn: (NSTableColumn *)tableColumn; didClickTableColumn: (NSTableColumn *)tableColumn;
- (void) tableView: (NSTableView*)tableView - (void) tableView: (NSTableView*)tableView
didDragTableColumn: (NSTableColumn *)tableColumn; didDragTableColumn: (NSTableColumn *)tableColumn;
- (void) tableView: (NSTableView*)tableView - (void) tableView: (NSTableView*)tableView
mouseDownInHeaderOfTableColumn: (NSTableColumn *)tableColumn; mouseDownInHeaderOfTableColumn: (NSTableColumn *)tableColumn;
#endif
- (BOOL)tableView: (NSTableView *)aTableView - (BOOL)tableView: (NSTableView *)aTableView
shouldEditTableColumn: (NSTableColumn *)aTableColumn shouldEditTableColumn: (NSTableColumn *)aTableColumn
row: (int)rowIndex; row: (int)rowIndex;

View file

@ -1663,6 +1663,24 @@ Also returns the child index relative to this parent. */
return YES; return YES;
} }
- (void) _didChangeSortDescriptors: (NSArray *)oldSortDescriptors
{
if ([_delegate
respondsToSelector: @selector(outlineView:sortDescriptorsDidChange:)])
{
[_delegate outlineView: self sortDescriptorsDidChange: oldSortDescriptors];
}
}
- (void) _didClickTableColumn: (NSTableColumn *)tc
{
if ([_delegate
respondsToSelector: @selector(outlineView:didClickTableColumn:)])
{
[_delegate outlineView: self didClickTableColumn: tc];
}
}
- (BOOL) _shouldEditTableColumn: (NSTableColumn *)tableColumn - (BOOL) _shouldEditTableColumn: (NSTableColumn *)tableColumn
row: (int) rowIndex row: (int) rowIndex
{ {

View file

@ -55,7 +55,7 @@
width: (float)width; width: (float)width;
- (float *) _columnOrigins; - (float *) _columnOrigins;
- (void) _mouseDownInHeaderOfTableColumn: (NSTableColumn *)tc; - (void) _mouseDownInHeaderOfTableColumn: (NSTableColumn *)tc;
- (void) _didClickTableColumn: (NSTableColumn *)tc; - (void) _clickTableColumn: (NSTableColumn *)tc;
@end @end
@implementation NSTableHeaderView @implementation NSTableHeaderView
@ -753,11 +753,8 @@
if (mouseDragged == NO) if (mouseDragged == NO)
{ {
[_tableView _selectColumn: columnIndex [_tableView _selectColumn: columnIndex modifiers: modifiers];
modifiers: modifiers]; [_tableView _clickTableColumn: currentColumn];
[_tableView _didClickTableColumn:
currentColumn];
[self setNeedsDisplay: YES];; [self setNeedsDisplay: YES];;
} }
@ -842,10 +839,8 @@
} }
else else
{ {
[_tableView _selectColumn: columnIndex [_tableView _selectColumn: columnIndex modifiers: modifiers];
modifiers: modifiers]; [_tableView _clickTableColumn: currentColumn];
[_tableView _didClickTableColumn:
currentColumn];
[self setNeedsDisplay: YES]; [self setNeedsDisplay: YES];
/* /*

View file

@ -38,6 +38,7 @@
#include <Foundation/NSIndexSet.h> #include <Foundation/NSIndexSet.h>
#include <Foundation/NSNotification.h> #include <Foundation/NSNotification.h>
#include <Foundation/NSSet.h> #include <Foundation/NSSet.h>
#include <Foundation/NSSortDescriptor.h>
#include <Foundation/NSUserDefaults.h> #include <Foundation/NSUserDefaults.h>
#include <Foundation/NSValue.h> #include <Foundation/NSValue.h>
#include <Foundation/NSKeyedArchiver.h> #include <Foundation/NSKeyedArchiver.h>
@ -119,6 +120,8 @@ typedef struct _tableViewFlags
- (BOOL) _shouldSelectRow: (int)rowIndex; - (BOOL) _shouldSelectRow: (int)rowIndex;
- (BOOL) _shouldSelectionChange; - (BOOL) _shouldSelectionChange;
- (void) _didChangeSortDescriptors: (NSArray *)oldSortDescriptors;
- (void) _didClickTableColumn: (NSTableColumn *)tc;
- (BOOL) _shouldEditTableColumn: (NSTableColumn *)tableColumn - (BOOL) _shouldEditTableColumn: (NSTableColumn *)tableColumn
row: (int) rowIndex; row: (int) rowIndex;
- (void) _willDisplayCell: (NSCell*)cell - (void) _willDisplayCell: (NSCell*)cell
@ -1991,6 +1994,7 @@ static void computeNewSelection
_draggingSourceOperationMaskForLocal = NSDragOperationCopy _draggingSourceOperationMaskForLocal = NSDragOperationCopy
| NSDragOperationLink | NSDragOperationGeneric | NSDragOperationPrivate; | NSDragOperationLink | NSDragOperationGeneric | NSDragOperationPrivate;
_draggingSourceOperationMaskForRemote = NSDragOperationNone; _draggingSourceOperationMaskForRemote = NSDragOperationNone;
ASSIGN(_sortDescriptors, [NSArray array]);
} }
- (id) initWithFrame: (NSRect)frameRect - (id) initWithFrame: (NSRect)frameRect
@ -2021,6 +2025,7 @@ static void computeNewSelection
RELEASE (_tableColumns); RELEASE (_tableColumns);
RELEASE (_selectedColumns); RELEASE (_selectedColumns);
RELEASE (_selectedRows); RELEASE (_selectedRows);
RELEASE (_sortDescriptors);
TEST_RELEASE (_headerView); TEST_RELEASE (_headerView);
TEST_RELEASE (_cornerView); TEST_RELEASE (_cornerView);
if (_autosaveTableColumns == YES) if (_autosaveTableColumns == YES)
@ -5650,6 +5655,11 @@ This method is deprecated, use -columnIndexesInRect:. */
[aCoder encodeObject: _cornerView forKey: @"NSCornerView"]; [aCoder encodeObject: _cornerView forKey: @"NSCornerView"];
} }
if ([[self sortDescriptors] count] > 0)
{
[aCoder encodeObject: _sortDescriptors forKey: @"NSSortDescriptors"];
}
tableViewFlags.columnSelection = [self allowsColumnSelection]; tableViewFlags.columnSelection = [self allowsColumnSelection];
tableViewFlags.multipleSelection = [self allowsMultipleSelection]; tableViewFlags.multipleSelection = [self allowsMultipleSelection];
tableViewFlags.emptySelection = [self allowsEmptySelection]; tableViewFlags.emptySelection = [self allowsEmptySelection];
@ -5710,6 +5720,7 @@ This method is deprecated, use -columnIndexesInRect:. */
ASSIGN(_gridColor, [NSColor gridColor]); ASSIGN(_gridColor, [NSColor gridColor]);
ASSIGN(_backgroundColor, [NSColor controlBackgroundColor]); ASSIGN(_backgroundColor, [NSColor controlBackgroundColor]);
ASSIGN(_tableColumns, [NSMutableArray array]); ASSIGN(_tableColumns, [NSMutableArray array]);
ASSIGN(_sortDescriptors, [NSArray array]);
if ([aDecoder containsValueForKey: @"NSDataSource"]) if ([aDecoder containsValueForKey: @"NSDataSource"])
{ {
@ -5784,6 +5795,11 @@ This method is deprecated, use -columnIndexesInRect:. */
[self setHeaderView: [aDecoder decodeObjectForKey: @"NSHeaderView"]]; [self setHeaderView: [aDecoder decodeObjectForKey: @"NSHeaderView"]];
} }
if ([aDecoder containsValueForKey: @"NSSortDescriptors"])
{
ASSIGN(_sortDescriptors, [aDecoder decodeObjectForKey: @"NSSortDescriptors"]);
}
if ([aDecoder containsValueForKey: @"NSTvFlags"]) if ([aDecoder containsValueForKey: @"NSTvFlags"])
{ {
unsigned long flags = [aDecoder decodeIntForKey: @"NSTvFlags"]; unsigned long flags = [aDecoder decodeIntForKey: @"NSTvFlags"];
@ -5797,7 +5813,7 @@ This method is deprecated, use -columnIndexesInRect:. */
[self setAllowsColumnResizing: tableViewFlags.columnResizing]; [self setAllowsColumnResizing: tableViewFlags.columnResizing];
[self setAllowsColumnReordering: tableViewFlags.columnOrdering]; [self setAllowsColumnReordering: tableViewFlags.columnOrdering];
} }
// get the table columns... // get the table columns...
columns = [aDecoder decodeObjectForKey: @"NSTableColumns"]; columns = [aDecoder decodeObjectForKey: @"NSTableColumns"];
e = [columns objectEnumerator]; e = [columns objectEnumerator];
@ -5941,15 +5957,40 @@ This method is deprecated, use -columnIndexesInRect:. */
} }
} }
- (void) _didClickTableColumn: (NSTableColumn *)tc - (void) _clickTableColumn: (NSTableColumn *)tc
{ {
if ([_delegate NSSortDescriptor *oldMainSortDescriptor = nil;
respondsToSelector: NSSortDescriptor *newMainSortDescriptor = [tc sortDescriptorPrototype];
@selector(tableView:didClickTableColumn:)]) NSMutableArray *newSortDescriptors =
[NSMutableArray arrayWithArray: [self sortDescriptors]];
NSEnumerator *e = [newSortDescriptors objectEnumerator];
NSSortDescriptor *descriptor = nil;
NSMutableArray *outdatedDescriptors = [NSMutableArray array];
if ([[self sortDescriptors] count] > 0)
{ {
[_delegate tableView: self oldMainSortDescriptor = [[self sortDescriptors] objectAtIndex: 0];
didClickTableColumn: tc];
} }
/* Remove every main descriptor equivalents (normally only one) */
while ((descriptor = [e nextObject]) != nil)
{
if ([[descriptor key] isEqual: [newMainSortDescriptor key]])
[outdatedDescriptors addObject: descriptor];
}
/* Invert the sort direction when the same column header is clicked twice */
if ([[newMainSortDescriptor key] isEqual: [oldMainSortDescriptor key]])
{
newMainSortDescriptor = [newMainSortDescriptor reversedSortDescriptor];
}
[newSortDescriptors removeObjectsInArray: outdatedDescriptors];
[newSortDescriptors insertObject: newMainSortDescriptor atIndex: 0];
[self setSortDescriptors: newSortDescriptors];
[self _didClickTableColumn: tc];
} }
- (void) _editNextCellAfterRow: (int) row - (void) _editNextCellAfterRow: (int) row
@ -6513,15 +6554,59 @@ This method is deprecated, use -columnIndexesInRect:. */
/* /*
* sorting * sorting
*/ */
- (void) setSortDescriptors: (NSArray *)array
/** Sets the sort descriptors used to sort the rows and delegates the sorting
to -tableView:didChangeSortDescriptors or -outlineView:didChangeSortDescriptors:
in NSOutlineView.
The delegate methods can retrieve the new sort descriptors with
-sortDescriptors and override them with -setSortDescriptors:.<br />
The first object in the new sort descriptor array is the sort descriptor
prototype returned by the table column whose header was the last clicked.
See -[NSTableColumn sortDescriptorPrototype].
This method is called automatically when you click on a table column header,
so you shouldn't need to call it usually.
Take note the sort descriptors are encoded by the keyed archiving (rarely used
since neither IB or Gorm support to set these directly). */
- (void) setSortDescriptors: (NSArray *)sortDescriptors
{ {
// FIXME NSArray *oldSortDescriptors = [self sortDescriptors];
NSArray *newSortDescriptors = nil;
/* To replicate precisely the Cocoa behavior */
if (sortDescriptors == nil)
{
newSortDescriptors = [NSArray array];
}
else
{
/* _sortDescriptors must remain immutable since -sortDescriptors doesn't
return a defensive copy */
newSortDescriptors = [NSArray arrayWithArray: sortDescriptors];
}
if ([newSortDescriptors isEqual: oldSortDescriptors])
return;
RETAIN(oldSortDescriptors);
ASSIGN(_sortDescriptors, newSortDescriptors);
[self _didChangeSortDescriptors: oldSortDescriptors];
RELEASE(oldSortDescriptors);
} }
/** Returns the current sort descriptors, usually updated every time a click
happens on a table column header.
By default, returns an empty array.
For a more detailed explanation, -setSortDescriptors:. */
- (NSArray *)sortDescriptors - (NSArray *)sortDescriptors
{ {
// FIXME return _sortDescriptors;
return nil;
} }
/* /*
@ -6616,6 +6701,26 @@ This method is deprecated, use -columnIndexesInRect:. */
return YES; return YES;
} }
- (void) _didChangeSortDescriptors: (NSArray *)oldSortDescriptors
{
if ([_delegate
respondsToSelector: @selector(tableView:sortDescriptorsDidChange:)])
{
[_delegate tableView: self sortDescriptorsDidChange: oldSortDescriptors];
}
}
- (void) _didClickTableColumn: (NSTableColumn *)tc
{
if ([_delegate
respondsToSelector:
@selector(tableView:didClickTableColumn:)])
{
[_delegate tableView: self
didClickTableColumn: tc];
}
}
- (BOOL) _shouldEditTableColumn: (NSTableColumn *)tableColumn - (BOOL) _shouldEditTableColumn: (NSTableColumn *)tableColumn
row: (int) rowIndex row: (int) rowIndex
{ {