mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-06-02 11:40:59 +00:00
Adding-on to our -in progress- work on Collection View.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/branches/gnustep_testplant_branch@36211 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
f62f56695d
commit
d1bb44a910
5 changed files with 380 additions and 58 deletions
|
@ -1,3 +1,12 @@
|
||||||
|
2013-02-28 Frank Le Grand <frank.legrand@testplant.com>
|
||||||
|
Continued work on Collection View: Applied more GNUstep coding standards (including
|
||||||
|
the use of macros), implemented selection.
|
||||||
|
This is a work in progress.
|
||||||
|
* Source\NSCollectionView.m
|
||||||
|
* Source\NSCollectionViewItem.m
|
||||||
|
* Headers\NSCollectionView.h
|
||||||
|
* Headers\NSCollectionViewItem.h
|
||||||
|
|
||||||
2013-01-31 Doug Simons <doug.simons@testplant.com>
|
2013-01-31 Doug Simons <doug.simons@testplant.com>
|
||||||
|
|
||||||
* Source/NSindow.m: Don't unregister delegate notifications if same delegate is set twice.
|
* Source/NSindow.m: Don't unregister delegate notifications if same delegate is set twice.
|
||||||
|
|
|
@ -44,7 +44,6 @@
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
@interface NSCollectionView : NSView
|
@interface NSCollectionView : NSView
|
||||||
{
|
{
|
||||||
NSArray *_content;
|
NSArray *_content;
|
||||||
|
@ -63,9 +62,11 @@
|
||||||
NSSize _minItemSize;
|
NSSize _minItemSize;
|
||||||
float _tileWidth;
|
float _tileWidth;
|
||||||
float _verticalMargin;
|
float _verticalMargin;
|
||||||
|
float _horizontalMargin;
|
||||||
|
|
||||||
NSUInteger _maxNumberOfColumns;
|
NSUInteger _maxNumberOfColumns;
|
||||||
NSUInteger _maxNumberOfRows;
|
NSUInteger _maxNumberOfRows;
|
||||||
|
long _numberOfColumns;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)allowsMultipleSelection;
|
- (BOOL)allowsMultipleSelection;
|
||||||
|
@ -116,7 +117,6 @@
|
||||||
withEvent:(NSEvent *)event
|
withEvent:(NSEvent *)event
|
||||||
offset:(NSPointPointer)dragImageOffset;
|
offset:(NSPointPointer)dragImageOffset;
|
||||||
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#endif /* _GNUstep_H_NSCollectionView */
|
#endif /* _GNUstep_H_NSCollectionView */
|
||||||
|
|
|
@ -35,8 +35,10 @@
|
||||||
#import <AppKit/NSImageView.h>
|
#import <AppKit/NSImageView.h>
|
||||||
#import <AppKit/NSView.h>
|
#import <AppKit/NSView.h>
|
||||||
#import <AppKit/NSViewController.h>
|
#import <AppKit/NSViewController.h>
|
||||||
|
#import <AppKit/NSCollectionView.h>
|
||||||
#import <AppKit/NSDragging.h>
|
#import <AppKit/NSDragging.h>
|
||||||
|
|
||||||
|
|
||||||
@interface NSCollectionViewItem : NSViewController
|
@interface NSCollectionViewItem : NSViewController
|
||||||
{
|
{
|
||||||
IBOutlet NSTextField *textField;
|
IBOutlet NSTextField *textField;
|
||||||
|
@ -44,9 +46,16 @@
|
||||||
BOOL _isSelected;
|
BOOL _isSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (NSCollectionView *)collectionView;
|
||||||
|
|
||||||
- (void)setSelected:(BOOL)shouldBeSelected;
|
- (void)setSelected:(BOOL)shouldBeSelected;
|
||||||
- (BOOL)isSelected;
|
- (BOOL)isSelected;
|
||||||
|
|
||||||
|
- (NSTextField *)textField;
|
||||||
|
- (NSImageView *)imageView;
|
||||||
|
- (void)setTextField:(NSTextField *)aTextField;
|
||||||
|
- (void)setImageView:(NSImageView *)anImageView;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#endif /* _GNUstep_H_NSCollectionView */
|
#endif /* _GNUstep_H_NSCollectionView */
|
||||||
|
|
|
@ -44,8 +44,10 @@
|
||||||
#import <Foundation/NSUserDefaults.h>
|
#import <Foundation/NSUserDefaults.h>
|
||||||
#import <Foundation/NSValue.h>
|
#import <Foundation/NSValue.h>
|
||||||
#import <Foundation/NSKeyedArchiver.h>
|
#import <Foundation/NSKeyedArchiver.h>
|
||||||
|
#import <Foundation/NSTimer.h>
|
||||||
|
|
||||||
#import "AppKit/NSView.h"
|
#import "AppKit/NSView.h"
|
||||||
|
#import "AppKit/NSAnimation.h"
|
||||||
#import "AppKit/NSNibLoading.h"
|
#import "AppKit/NSNibLoading.h"
|
||||||
#import "AppKit/NSTableView.h"
|
#import "AppKit/NSTableView.h"
|
||||||
#import "AppKit/NSApplication.h"
|
#import "AppKit/NSApplication.h"
|
||||||
|
@ -78,21 +80,35 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static NSString* NSCollectionViewMinItemSizeKey = @"NSCollectionViewMinItemSizeKey";
|
static NSString* NSCollectionViewMinItemSizeKey = @"NSMinGridSize";
|
||||||
static NSString* NSCollectionViewMaxItemSizeKey = @"NSCollectionViewMaxItemSizeKey";
|
static NSString* NSCollectionViewMaxItemSizeKey = @"NSMaxGridSize";
|
||||||
static NSString* NSCollectionViewVerticalMarginKey = @"NSCollectionViewVerticalMarginKey";
|
//static NSString* NSCollectionViewVerticalMarginKey = @"NSCollectionViewVerticalMarginKey";
|
||||||
static NSString* NSCollectionViewMaxNumberOfRowsKey = @"NSCollectionViewMaxNumberOfRowsKey";
|
static NSString* NSCollectionViewMaxNumberOfRowsKey = @"NSMaxNumberOfGridRows";
|
||||||
static NSString* NSCollectionViewMaxNumberOfColumnsKey = @"NSCollectionViewMaxNumberOfColumnsKey";
|
static NSString* NSCollectionViewMaxNumberOfColumnsKey = @"NSMaxNumberOfGridColumns";
|
||||||
static NSString* NSCollectionViewSelectableKey = @"NSCollectionViewSelectableKey";
|
static NSString* NSCollectionViewSelectableKey = @"NSSelectable";
|
||||||
static NSString* NSCollectionViewAllowsMultipleSelectionKey = @"NSCollectionViewAllowsMultipleSelectionKey";
|
static NSString* NSCollectionViewAllowsMultipleSelectionKey = @"NSAllowsMultipleSelection";
|
||||||
static NSString* NSCollectionViewBackgroundColorsKey = @"NSCollectionViewBackgroundColorsKey";
|
static NSString* NSCollectionViewBackgroundColorsKey = @"NSBackgroundColors";
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Class variables
|
* Class variables
|
||||||
*/
|
*/
|
||||||
static NSString *placeholderItem = nil;
|
static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
|
@interface NSCollectionView (CollectionViewInternalPrivate)
|
||||||
|
- (void)_resetItemSize;
|
||||||
|
- (void)_removeItemsViews;
|
||||||
|
- (int)_indexAtPoint:(NSPoint)point;
|
||||||
|
- (void)_modifySelectionWithNewIndex:(int)anIndex
|
||||||
|
direction:(int)aDireection
|
||||||
|
expand:(BOOL)shouldExpand;
|
||||||
|
|
||||||
|
- (void)_moveDownAndExpandSelection:(BOOL)shouldExpand;
|
||||||
|
- (void)_moveUpAndExpandSelection:(BOOL)shouldExpand;
|
||||||
|
- (void)_moveLeftAndExpandSelection:(BOOL)shouldExpand;
|
||||||
|
- (void)_moveRightAndExpandSelection:(BOOL)shouldExpand;
|
||||||
|
@end
|
||||||
|
|
||||||
|
|
||||||
@implementation NSCollectionView
|
@implementation NSCollectionView
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -105,11 +121,27 @@ static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
- (void)awakeFromNib
|
- (void)awakeFromNib
|
||||||
{
|
{
|
||||||
// FIXME: This is just preliminary stuff
|
[self _resetItemSize];
|
||||||
_minItemSize = NSMakeSize(120.0, 100.0);
|
|
||||||
_maxItemSize = NSMakeSize(120.0, 100.0);
|
|
||||||
_content = [[NSArray alloc] init];
|
_content = [[NSArray alloc] init];
|
||||||
_items = [[NSMutableArray alloc] init];
|
_items = [[NSMutableArray alloc] init];
|
||||||
|
_selectionIndexes = [[NSIndexSet alloc] init];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)_resetItemSize
|
||||||
|
{
|
||||||
|
if (itemPrototype)
|
||||||
|
{
|
||||||
|
_itemSize = [[itemPrototype view] frame].size;
|
||||||
|
_minItemSize = NSMakeSize (_itemSize.width, _itemSize.height);
|
||||||
|
_maxItemSize = NSMakeSize (_itemSize.width, _itemSize.height);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// FIXME: This is just arbitrary.
|
||||||
|
_itemSize = NSMakeSize(120.0, 100.0);
|
||||||
|
_minItemSize = NSMakeSize(120.0, 100.0);
|
||||||
|
_maxItemSize = NSMakeSize(120.0, 100.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)drawRect:(NSRect)dirtyRect
|
- (void)drawRect:(NSRect)dirtyRect
|
||||||
|
@ -120,10 +152,13 @@ static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
- (void)dealloc
|
- (void)dealloc
|
||||||
{
|
{
|
||||||
[_content release];
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||||
[itemPrototype release];
|
|
||||||
[_backgroundColors release];
|
DESTROY (_content);
|
||||||
[_selectionIndexes release];
|
//DESTROY (itemPrototype);
|
||||||
|
DESTROY (_backgroundColors);
|
||||||
|
DESTROY (_selectionIndexes);
|
||||||
|
DESTROY (_items);
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,10 +194,11 @@ static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
- (void)setContent:(NSArray *)content
|
- (void)setContent:(NSArray *)content
|
||||||
{
|
{
|
||||||
[_content release];
|
RELEASE (_content);
|
||||||
_content = [content retain];
|
_content = [content retain];
|
||||||
|
[self _removeItemsViews];
|
||||||
|
|
||||||
[_items release];
|
RELEASE (_items);
|
||||||
_items = [[NSMutableArray alloc] initWithCapacity:[_content count]];
|
_items = [[NSMutableArray alloc] initWithCapacity:[_content count]];
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
@ -190,22 +226,15 @@ static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
- (void)setItemPrototype:(NSCollectionViewItem *)prototype
|
- (void)setItemPrototype:(NSCollectionViewItem *)prototype
|
||||||
{
|
{
|
||||||
[itemPrototype release];
|
RELEASE (itemPrototype);
|
||||||
itemPrototype = [prototype retain];
|
itemPrototype = prototype;
|
||||||
|
RETAIN (itemPrototype);
|
||||||
//TODO: Re-enabled this
|
[self _resetItemSize];
|
||||||
// if (itemPrototype)
|
|
||||||
// _itemSize = [[itemPrototype view] frame].size;
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// // TODO: Figure out what to do if prototype==nil
|
|
||||||
// _itemSize = NSMakeSize (1,1);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)isFirstResponder
|
- (BOOL)isFirstResponder
|
||||||
{
|
{
|
||||||
// FIXME
|
// FIXME: This will be required for keyboard events
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,6 +308,14 @@ static NSString *placeholderItem = nil;
|
||||||
- (void)setSelectable:(BOOL)flag
|
- (void)setSelectable:(BOOL)flag
|
||||||
{
|
{
|
||||||
_isSelectable = flag;
|
_isSelectable = flag;
|
||||||
|
if (!_isSelectable)
|
||||||
|
{
|
||||||
|
int index = -1;
|
||||||
|
while ((index = [_selectionIndexes indexGreaterThanIndex:index]) != NSNotFound)
|
||||||
|
{
|
||||||
|
[[_items objectAtIndex:index] setSelected:NO];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSIndexSet *)selectionIndexes
|
- (NSIndexSet *)selectionIndexes
|
||||||
|
@ -288,8 +325,30 @@ static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
- (void)setSelectionIndexes:(NSIndexSet *)indexes
|
- (void)setSelectionIndexes:(NSIndexSet *)indexes
|
||||||
{
|
{
|
||||||
[_selectionIndexes release];
|
if (!_isSelectable || [_selectionIndexes isEqual:indexes])
|
||||||
_selectionIndexes = [indexes copy];
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Reset selectionIndexes in SetContent:
|
||||||
|
|
||||||
|
RELEASE(_selectionIndexes);
|
||||||
|
_selectionIndexes = indexes;
|
||||||
|
RETAIN(_selectionIndexes);
|
||||||
|
|
||||||
|
|
||||||
|
NSUInteger index = 0;
|
||||||
|
while (index < [_items count])
|
||||||
|
{
|
||||||
|
[[_items objectAtIndex:index] setSelected:NO];
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = -1;
|
||||||
|
while ((index = [_selectionIndexes indexGreaterThanIndex:index]) != NSNotFound)
|
||||||
|
{
|
||||||
|
[[_items objectAtIndex:index] setSelected:YES];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSRect)frameForItemAtIndex:(NSUInteger)index
|
- (NSRect)frameForItemAtIndex:(NSUInteger)index
|
||||||
|
@ -313,8 +372,7 @@ static NSString *placeholderItem = nil;
|
||||||
NSCollectionViewItem *collectionItem = nil;
|
NSCollectionViewItem *collectionItem = nil;
|
||||||
if (itemPrototype)
|
if (itemPrototype)
|
||||||
{
|
{
|
||||||
NSData *itemAsData = [NSKeyedArchiver archivedDataWithRootObject:itemPrototype];
|
ASSIGN(collectionItem, [itemPrototype copy]);
|
||||||
collectionItem = [NSKeyedUnarchiver unarchiveObjectWithData:itemAsData];
|
|
||||||
[collectionItem setRepresentedObject:object];
|
[collectionItem setRepresentedObject:object];
|
||||||
}
|
}
|
||||||
return collectionItem;
|
return collectionItem;
|
||||||
|
@ -332,26 +390,33 @@ static NSString *placeholderItem = nil;
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)reloadContent
|
- (void)_removeItemsViews
|
||||||
{
|
{
|
||||||
// First of all clean off the current item views:
|
if (!_items)
|
||||||
|
return;
|
||||||
|
|
||||||
long count = [_items count];
|
long count = [_items count];
|
||||||
|
|
||||||
while (count--)
|
while (count--)
|
||||||
{
|
{
|
||||||
if (![[_items objectAtIndex:count] isKindOfClass:[NSString class]])
|
if ([[_items objectAtIndex:count] respondsToSelector:@selector(view)])
|
||||||
{
|
{
|
||||||
[[[_items objectAtIndex:count] view] removeFromSuperview];
|
[[[_items objectAtIndex:count] view] removeFromSuperview];
|
||||||
[[_items objectAtIndex:count] setSelected:NO];
|
[[_items objectAtIndex:count] setSelected:NO];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)reloadContent
|
||||||
|
{
|
||||||
|
[self _removeItemsViews];
|
||||||
|
|
||||||
if (!itemPrototype)
|
if (!itemPrototype)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
long index = 0;
|
long index = 0;
|
||||||
|
|
||||||
count = [_content count];
|
long count = [_content count];
|
||||||
|
|
||||||
for (; index < count; ++index)
|
for (; index < count; ++index)
|
||||||
{
|
{
|
||||||
|
@ -368,6 +433,8 @@ static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
- (void)tile
|
- (void)tile
|
||||||
{
|
{
|
||||||
|
// TODO: - Animate items, Add Fade-in/Fade-out (as in Cocoa)
|
||||||
|
// - Put the tiling on a delay
|
||||||
if (!_items)
|
if (!_items)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -378,7 +445,7 @@ static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
NSSize itemSize = NSMakeSize(_minItemSize.width, _minItemSize.height);
|
NSSize itemSize = NSMakeSize(_minItemSize.width, _minItemSize.height);
|
||||||
|
|
||||||
long _numberOfColumns = MAX(1.0, floor(width / itemSize.width));
|
_numberOfColumns = MAX(1.0, floor(width / itemSize.width));
|
||||||
|
|
||||||
if (_maxNumberOfColumns > 0)
|
if (_maxNumberOfColumns > 0)
|
||||||
_numberOfColumns = MIN(_maxNumberOfColumns, _numberOfColumns);
|
_numberOfColumns = MIN(_maxNumberOfColumns, _numberOfColumns);
|
||||||
|
@ -404,7 +471,7 @@ static NSString *placeholderItem = nil;
|
||||||
if (_maxNumberOfColumns > 0 && _maxNumberOfRows > 0)
|
if (_maxNumberOfColumns > 0 && _maxNumberOfRows > 0)
|
||||||
count = MIN(count, _maxNumberOfColumns * _maxNumberOfRows);
|
count = MIN(count, _maxNumberOfColumns * _maxNumberOfRows);
|
||||||
|
|
||||||
float _horizontalMargin = floor((width - _numberOfColumns * itemSize.width) / (_numberOfColumns + 1));
|
_horizontalMargin = floor((width - _numberOfColumns * itemSize.width) / (_numberOfColumns + 1));
|
||||||
|
|
||||||
float x = _horizontalMargin;
|
float x = _horizontalMargin;
|
||||||
float y = -itemSize.height;
|
float y = -itemSize.height;
|
||||||
|
@ -418,7 +485,6 @@ static NSString *placeholderItem = nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSView *view = [[_items objectAtIndex:index] view];
|
NSView *view = [[_items objectAtIndex:index] view];
|
||||||
|
|
||||||
[view setFrameOrigin:NSMakePoint(x, y)];
|
[view setFrameOrigin:NSMakePoint(x, y)];
|
||||||
|
|
||||||
if (itemsNeedSizeUpdate)
|
if (itemsNeedSizeUpdate)
|
||||||
|
@ -431,13 +497,15 @@ static NSString *placeholderItem = nil;
|
||||||
|
|
||||||
_tileWidth = width;
|
_tileWidth = width;
|
||||||
[self setFrameSize:NSMakeSize(width, proposedHeight)];
|
[self setFrameSize:NSMakeSize(width, proposedHeight)];
|
||||||
_tileWidth = -1.0;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)resizeSubviewsWithOldSize:(NSSize)aSize
|
- (void)resizeSubviewsWithOldSize:(NSSize)aSize
|
||||||
{
|
{
|
||||||
|
NSSize currentSize = [self frame].size;
|
||||||
|
if (!NSEqualSizes(currentSize, aSize))
|
||||||
|
{
|
||||||
[self tile];
|
[self tile];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)initWithCoder:(NSCoder *)aCoder
|
- (id)initWithCoder:(NSCoder *)aCoder
|
||||||
|
@ -457,7 +525,7 @@ static NSString *placeholderItem = nil;
|
||||||
_maxNumberOfRows = [aCoder decodeInt64ForKey:NSCollectionViewMaxNumberOfRowsKey];
|
_maxNumberOfRows = [aCoder decodeInt64ForKey:NSCollectionViewMaxNumberOfRowsKey];
|
||||||
_maxNumberOfColumns = [aCoder decodeInt64ForKey:NSCollectionViewMaxNumberOfColumnsKey];
|
_maxNumberOfColumns = [aCoder decodeInt64ForKey:NSCollectionViewMaxNumberOfColumnsKey];
|
||||||
|
|
||||||
_verticalMargin = [aCoder decodeFloatForKey:NSCollectionViewVerticalMarginKey];
|
//_verticalMargin = [aCoder decodeFloatForKey:NSCollectionViewVerticalMarginKey];
|
||||||
|
|
||||||
_isSelectable = [aCoder decodeBoolForKey:NSCollectionViewSelectableKey];
|
_isSelectable = [aCoder decodeBoolForKey:NSCollectionViewSelectableKey];
|
||||||
_allowsMultipleSelection = [aCoder decodeBoolForKey:NSCollectionViewAllowsMultipleSelectionKey];
|
_allowsMultipleSelection = [aCoder decodeBoolForKey:NSCollectionViewAllowsMultipleSelectionKey];
|
||||||
|
@ -467,9 +535,6 @@ static NSString *placeholderItem = nil;
|
||||||
_tileWidth = -1.0;
|
_tileWidth = -1.0;
|
||||||
|
|
||||||
_selectionIndexes = [NSIndexSet indexSet];
|
_selectionIndexes = [NSIndexSet indexSet];
|
||||||
|
|
||||||
// FIXME
|
|
||||||
//_allowsEmptySelection = YES;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
|
@ -491,9 +556,205 @@ static NSString *placeholderItem = nil;
|
||||||
[aCoder encodeBool:_isSelectable forKey:NSCollectionViewSelectableKey];
|
[aCoder encodeBool:_isSelectable forKey:NSCollectionViewSelectableKey];
|
||||||
[aCoder encodeBool:_allowsMultipleSelection forKey:NSCollectionViewAllowsMultipleSelectionKey];
|
[aCoder encodeBool:_allowsMultipleSelection forKey:NSCollectionViewAllowsMultipleSelectionKey];
|
||||||
|
|
||||||
[aCoder encodeFloat:_verticalMargin forKey:NSCollectionViewVerticalMarginKey];
|
//[aCoder encodeFloat:_verticalMargin forKey:NSCollectionViewVerticalMarginKey];
|
||||||
|
|
||||||
[aCoder encodeObject:_backgroundColors forKey:NSCollectionViewBackgroundColorsKey];
|
[aCoder encodeObject:_backgroundColors forKey:NSCollectionViewBackgroundColorsKey];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)mouseDown:(NSEvent *)theEvent
|
||||||
|
{
|
||||||
|
// FIXME: This implements only a single mouse click.
|
||||||
|
// TODO: Handle double-click & mouse drag
|
||||||
|
NSPoint initialLocation = [theEvent locationInWindow];
|
||||||
|
NSPoint location = [self convertPoint: initialLocation fromView: nil];
|
||||||
|
int index = [self _indexAtPoint:location];
|
||||||
|
NSMutableIndexSet *currentIndexSet = [[NSMutableIndexSet alloc] initWithIndexSet:[self selectionIndexes]];
|
||||||
|
|
||||||
|
if (_isSelectable && (index >= 0 && index < [_items count]))
|
||||||
|
{
|
||||||
|
if (_allowsMultipleSelection
|
||||||
|
&& (([theEvent modifierFlags] & NSControlKeyMask) || ([theEvent modifierFlags] & NSShiftKeyMask)))
|
||||||
|
|
||||||
|
{
|
||||||
|
if ([theEvent modifierFlags] & NSControlKeyMask)
|
||||||
|
{
|
||||||
|
if ([currentIndexSet containsIndex:index])
|
||||||
|
{
|
||||||
|
[currentIndexSet removeIndex:index];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
[currentIndexSet addIndex:index];
|
||||||
|
}
|
||||||
|
[self setSelectionIndexes:currentIndexSet];
|
||||||
|
}
|
||||||
|
else if ([theEvent modifierFlags] & NSShiftKeyMask)
|
||||||
|
{
|
||||||
|
long firstSelectedIndex = [currentIndexSet firstIndex];
|
||||||
|
NSRange selectedRange;
|
||||||
|
|
||||||
|
if (firstSelectedIndex == NSNotFound)
|
||||||
|
{
|
||||||
|
selectedRange = NSMakeRange(index, index);
|
||||||
|
}
|
||||||
|
else if (index < firstSelectedIndex)
|
||||||
|
{
|
||||||
|
selectedRange = NSMakeRange(index, (firstSelectedIndex - index + 1));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
selectedRange = NSMakeRange(firstSelectedIndex, (index - firstSelectedIndex + 1));
|
||||||
|
}
|
||||||
|
[currentIndexSet addIndexesInRange:selectedRange];
|
||||||
|
[self setSelectionIndexes:currentIndexSet];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
[self setSelectionIndexes:[NSIndexSet indexSetWithIndex:index]];
|
||||||
|
}
|
||||||
|
[[self window] makeFirstResponder:self];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
[self setSelectionIndexes:[NSIndexSet indexSet]];
|
||||||
|
}
|
||||||
|
RELEASE (currentIndexSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (int)_indexAtPoint:(NSPoint)point
|
||||||
|
{
|
||||||
|
int row = floor(point.y / (_itemSize.height + _verticalMargin));
|
||||||
|
int column = floor(point.x / (_itemSize.width + _horizontalMargin));
|
||||||
|
return (column + (row * _numberOfColumns));
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)acceptsFirstResponder
|
||||||
|
{
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)keyDown:(NSEvent *)theEvent
|
||||||
|
{
|
||||||
|
[self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveUp:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveUpAndExpandSelection:NO];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveUpAndModifySelection:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveUpAndExpandSelection:YES];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)_moveUpAndExpandSelection:(BOOL)shouldExpand
|
||||||
|
{
|
||||||
|
int index = [[self selectionIndexes] firstIndex];
|
||||||
|
if (index != NSNotFound && (index - _numberOfColumns) >= 0)
|
||||||
|
{
|
||||||
|
[self _modifySelectionWithNewIndex:index - _numberOfColumns
|
||||||
|
direction:-1
|
||||||
|
expand:shouldExpand];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveDown:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveDownAndExpandSelection:NO];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveDownAndModifySelection:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveDownAndExpandSelection:YES];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(void)_moveDownAndExpandSelection:(BOOL)shouldExpand
|
||||||
|
{
|
||||||
|
int index = [[self selectionIndexes] lastIndex];
|
||||||
|
if (index != NSNotFound && (index + _numberOfColumns) < [_items count])
|
||||||
|
{
|
||||||
|
[self _modifySelectionWithNewIndex:index + _numberOfColumns
|
||||||
|
direction:1
|
||||||
|
expand:shouldExpand];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveLeft:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveLeftAndExpandSelection:NO];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveLeftAndModifySelection:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveLeftAndExpandSelection:YES];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveBackwardAndModifySelection:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveLeftAndExpandSelection:YES];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(void)_moveLeftAndExpandSelection:(BOOL)shouldExpand
|
||||||
|
{
|
||||||
|
int index = [[self selectionIndexes] firstIndex];
|
||||||
|
if (index != NSNotFound && index != 0)
|
||||||
|
{
|
||||||
|
[self _modifySelectionWithNewIndex:index-1 direction:-1 expand:shouldExpand];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveRight:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveRightAndExpandSelection:NO];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveRightAndModifySelection:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveRightAndExpandSelection:YES];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(IBAction)moveForwardAndModifySelection:(id)sender
|
||||||
|
{
|
||||||
|
[self _moveRightAndExpandSelection:YES];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(void)_moveRightAndExpandSelection:(BOOL)shouldExpand
|
||||||
|
{
|
||||||
|
int index = [[self selectionIndexes] lastIndex];
|
||||||
|
if (index != NSNotFound && index != ([_items count] - 1))
|
||||||
|
{
|
||||||
|
[self _modifySelectionWithNewIndex:index+1 direction:1 expand:shouldExpand];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)_modifySelectionWithNewIndex:(int)anIndex
|
||||||
|
direction:(int)aDirection
|
||||||
|
expand:(BOOL)shouldExpand
|
||||||
|
{
|
||||||
|
anIndex = MIN (MAX (anIndex, 0), [_items count] - 1);
|
||||||
|
|
||||||
|
if (_allowsMultipleSelection && shouldExpand)
|
||||||
|
{
|
||||||
|
NSMutableIndexSet *newIndexSet = [[NSMutableIndexSet alloc] initWithIndexSet:_selectionIndexes];
|
||||||
|
int firstIndex = [newIndexSet firstIndex];
|
||||||
|
int lastIndex = [newIndexSet lastIndex];
|
||||||
|
if (aDirection == -1)
|
||||||
|
{
|
||||||
|
[newIndexSet addIndexesInRange:NSMakeRange (anIndex, firstIndex - anIndex + 1)];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
[newIndexSet addIndexesInRange:NSMakeRange (lastIndex, anIndex - lastIndex + 1)];
|
||||||
|
}
|
||||||
|
[self setSelectionIndexes:newIndexSet];
|
||||||
|
RELEASE (newIndexSet);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
[self setSelectionIndexes:[NSIndexSet indexSetWithIndex:anIndex]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -83,12 +83,22 @@
|
||||||
|
|
||||||
- (void)dealloc
|
- (void)dealloc
|
||||||
{
|
{
|
||||||
|
DESTROY (textField);
|
||||||
|
DESTROY (imageView);
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setSelected:(BOOL)shouldBeSelected
|
- (NSCollectionView *)collectionView
|
||||||
{
|
{
|
||||||
_isSelected = shouldBeSelected;
|
return (NSCollectionView *)[[self view] superview];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setSelected:(BOOL)flag
|
||||||
|
{
|
||||||
|
if (_isSelected != flag)
|
||||||
|
{
|
||||||
|
_isSelected = flag;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)representedObject
|
- (id)representedObject
|
||||||
|
@ -102,6 +112,32 @@
|
||||||
[textField setStringValue:[self representedObject]];
|
[textField setStringValue:[self representedObject]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (NSTextField *)textField
|
||||||
|
{
|
||||||
|
return textField;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setTextField:(NSTextField *)aTextField
|
||||||
|
{
|
||||||
|
if (textField != aTextField)
|
||||||
|
{
|
||||||
|
textField = aTextField;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSImageView *)imageView
|
||||||
|
{
|
||||||
|
return imageView;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setImageView:(NSImageView *)anImageView
|
||||||
|
{
|
||||||
|
if (imageView != anImageView)
|
||||||
|
{
|
||||||
|
imageView = anImageView;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (id)initWithCoder:(NSCoder *)aCoder
|
- (id)initWithCoder:(NSCoder *)aCoder
|
||||||
{
|
{
|
||||||
self = [super initWithCoder:aCoder];
|
self = [super initWithCoder:aCoder];
|
||||||
|
@ -122,4 +158,11 @@
|
||||||
[aCoder encodeObject:imageView forKey:@"imageView"];
|
[aCoder encodeObject:imageView forKey:@"imageView"];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (id) copyWithZone:(NSZone *)zone
|
||||||
|
{
|
||||||
|
NSData *itemAsData = [NSKeyedArchiver archivedDataWithRootObject:self];
|
||||||
|
NSCollectionViewItem *newItem = [NSKeyedUnarchiver unarchiveObjectWithData:itemAsData];
|
||||||
|
return newItem;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue