From c4ee83e4c1b7a9a70fd2cb8500c97e696b6c8a42 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Fri, 14 Jan 2011 13:34:18 -0300 Subject: [PATCH] Basic CPBrowser CPCoding support. --- AppKit/CPBrowser.j | 111 +++++++++++++++++++++++++---------- Tests/AppKit/CPBrowserTest.j | 6 ++ 2 files changed, 86 insertions(+), 31 deletions(-) diff --git a/AppKit/CPBrowser.j b/AppKit/CPBrowser.j index bea4b7bd8..1fd211bc8 100644 --- a/AppKit/CPBrowser.j +++ b/AppKit/CPBrowser.j @@ -83,42 +83,47 @@ { if (self = [super initWithFrame:aFrame]) { - _rowHeight = 23.0; - _defaultColumnWidth = 140.0; - _minColumnWidth = 80.0; - _imageWidth = 23.0; - _leafWidth = 13.0; - _columnWidths = []; - - _pathSeparator = "/"; - _tableViews = []; - _tableDelegates = []; - _allowsMultipleSelection = YES; - _allowsEmptySelection = YES; - _tableViewClass = [_CPBrowserTableView class]; - - _prototypeView = [[CPTextField alloc] initWithFrame:CGRectMakeZero()]; - [_prototypeView setVerticalAlignment:CPCenterVerticalTextAlignment]; - [_prototypeView setValue:[CPColor whiteColor] forThemeAttribute:"text-color" inState:CPThemeStateSelectedDataView]; - [_prototypeView setLineBreakMode:CPLineBreakByTruncatingTail]; - - _horizontalScrollView = [[CPScrollView alloc] initWithFrame:[self bounds]]; - - [_horizontalScrollView setHasVerticalScroller:NO]; - [_horizontalScrollView setAutohidesScrollers:YES]; - [_horizontalScrollView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable]; - - _contentView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 0, CGRectGetHeight([self bounds]))]; - [_contentView setAutoresizingMask:CPViewHeightSizable]; - - [_horizontalScrollView setDocumentView:_contentView]; - - [self addSubview:_horizontalScrollView]; + [self _init]; } return self; } +- (void)_init +{ + _rowHeight = 23.0; + _defaultColumnWidth = 140.0; + _minColumnWidth = 80.0; + _imageWidth = 23.0; + _leafWidth = 13.0; + _columnWidths = []; + + _pathSeparator = "/"; + _tableViews = []; + _tableDelegates = []; + _allowsMultipleSelection = YES; + _allowsEmptySelection = YES; + _tableViewClass = [_CPBrowserTableView class]; + + _prototypeView = [[CPTextField alloc] initWithFrame:CGRectMakeZero()]; + [_prototypeView setVerticalAlignment:CPCenterVerticalTextAlignment]; + [_prototypeView setValue:[CPColor whiteColor] forThemeAttribute:"text-color" inState:CPThemeStateSelectedDataView]; + [_prototypeView setLineBreakMode:CPLineBreakByTruncatingTail]; + + _horizontalScrollView = [[CPScrollView alloc] initWithFrame:[self bounds]]; + + [_horizontalScrollView setHasVerticalScroller:NO]; + [_horizontalScrollView setAutohidesScrollers:YES]; + [_horizontalScrollView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable]; + + _contentView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 0, CGRectGetHeight([self bounds]))]; + [_contentView setAutoresizingMask:CPViewHeightSizable]; + + [_horizontalScrollView setDocumentView:_contentView]; + + [self addSubview:_horizontalScrollView]; +} + - (void)setPrototypeView:(CPView)aPrototypeView { _prototypeView = [CPKeyedUnarchiver unarchiveObjectWithData: @@ -650,6 +655,50 @@ @end +@implementation CPBrowser (CPCoding) + +- (id)initWithCoder:(CPCoder)aCoder +{ + self = [super initWithCoder:aCoder]; + + if (self) + { + [self _init]; + + [self setAutohidesScroller:[aCoder decodeBoolForKey:@"CPBrowserAutohidesScrollerKey"]]; + _allowsEmptySelection = [aCoder decodeBoolForKey:@"CPBrowserAllowsEmptySelectionKey"]; + _allowsMultipleSelection = [aCoder decodeBoolForKey:@"CPBrowserAllowsMultipleSelectionKey"]; + _prototypeView = [aCoder decodeObjectForKey:@"CPBrowserPrototypeViewKey"]; + _rowHeight = [aCoder decodeFloatForKey:@"CPBrowserRowHeightKey"]; + _imageWidth = [aCoder decodeFloatForKey:@"CPBrowserImageWidthKey"]; + _minColumnWidth = [aCoder decodeFloatForKey:@"CPBrowserMinColumnWidthKey"]; + _columnWidths = [aCoder decodeObjectForKey:@"CPBrowserColumnWidthsKey"]; + } + + return self; +} + +- (void)encodeWithCoder:(CPCoder)aCoder +{ + // Don't encode the subviews, they're transient and will be recreated from data. + var actualSubviews = _subviews; + _subviews = []; + [super encodeWithCoder:aCoder]; + _subviews = actualSubviews; + + [aCoder encodeBool:[self autohidesScroller] forKey:@"CPBrowserAutohidesScrollerKey"]; + [aCoder encodeBool:_allowsEmptySelection forKey:@"CPBrowserAllowsEmptySelectionKey"]; + [aCoder encodeBool:_allowsMultipleSelection forKey:@"CPBrowserAllowsMultipleSelectionKey"]; + [aCoder encodeObject:_prototypeView forKey:@"CPBrowserPrototypeViewKey"]; + [aCoder encodeFloat:_rowHeight forKey:@"CPBrowserRowHeightKey"]; + [aCoder encodeFloat:_imageWidth forKey:@"CPBrowserImageWidthKey"]; + [aCoder encodeFloat:_minColumnWidth forKey:@"CPBrowserMinColumnWidthKey"]; + [aCoder encodeObject:_columnWidths forKey:@"CPBrowserColumnWidthsKey"]; +} + +@end + + var _CPBrowserResizeControlBackgroundImage = nil; @implementation _CPBrowserResizeControl : CPView diff --git a/Tests/AppKit/CPBrowserTest.j b/Tests/AppKit/CPBrowserTest.j index 2f85c807e..ef763a510 100644 --- a/Tests/AppKit/CPBrowserTest.j +++ b/Tests/AppKit/CPBrowserTest.j @@ -33,6 +33,12 @@ [self assert:".1.2" equals:[browser itemAtRow:1 inColumn:1]]; } +- (void)testCoding +{ + // This should preferably not crash. + var decoded = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:browser]]; +} + @end @implementation CPBrowserDelegate : CPObject