00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <AppKit/CPTheme.j>
00024 @import <AppKit/CPView.j>
00025
00026
00027 var LEFT_PANEL_WIDTH = 176.0;
00028
00029 var BKLearnMoreToolbarItemIdentifier = @"BKLearnMoreToolbarItemIdentifier",
00030 BKStateToolbarItemIdentifier = @"BKStateToolbarItemIdentifier",
00031 BKBackgroundColorToolbarItemIdentifier = @"BKBackgroundColorToolbarItemIdentifier";
00032
00033 @implementation BKShowcaseController : CPObject
00034 {
00035 CPArray _themeDescriptorClasses;
00036
00037 CPCollectionView _themesCollectionView;
00038 CPCollectionView _themedObjectsCollectionView;
00039 }
00040
00041 - (void)applicationDidFinishLaunching:(CPNotification)aNotification
00042 {
00043 _themeDescriptorClasses = [BKThemeDescriptor allThemeDescriptorClasses];
00044
00045 var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
00046 toolbar = [[CPToolbar alloc] initWithIdentifier:@"Toolbar"];
00047
00048 [toolbar setDelegate:self];
00049 [theWindow setToolbar:toolbar];
00050
00051 var contentView = [theWindow contentView],
00052 bounds = [contentView bounds],
00053 splitView = [[CPSplitView alloc] initWithFrame:bounds];
00054
00055 [splitView setIsPaneSplitter:YES];
00056 [splitView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
00057
00058 [contentView addSubview:splitView];
00059
00060
00061 var label = [CPTextField labelWithTitle:@"THEMES"];
00062
00063 [label setFont:[CPFont boldSystemFontOfSize:11.0]];
00064 [label setTextColor:[CPColor colorWithCalibratedRed:92.0 / 255.0 green:110.0 / 255.0 blue:129.0 / 255.0 alpha:1.0]];
00065 [label setTextShadowColor:[CPColor colorWithCalibratedRed:225.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:0.7]];
00066 [label setTextShadowOffset:CGSizeMake(0.0, 1.0)];
00067 [label sizeToFit];
00068 [label setFrameOrigin:CGPointMake(5.0, 2.0)];
00069
00070 var themeDescriptorItem = [[CPCollectionViewItem alloc] init];
00071
00072 [themeDescriptorItem setView:[[BKThemeDescriptorCell alloc] init]];
00073
00074 _themesCollectionView = [[CPCollectionView alloc] initWithFrame:CGRectMake(0.0, 0.0, LEFT_PANEL_WIDTH, CGRectGetHeight(bounds))];
00075
00076 [_themesCollectionView setDelegate:self];
00077 [_themesCollectionView setItemPrototype:themeDescriptorItem];
00078 [_themesCollectionView setMinItemSize:CGSizeMake(20.0, 36.0)];
00079 [_themesCollectionView setMaxItemSize:CGSizeMake(10000000.0, 36.0)];
00080 [_themesCollectionView setMaxNumberOfColumns:1];
00081 [_themesCollectionView setContent:_themeDescriptorClasses];
00082 [_themesCollectionView setAutoresizingMask:CPViewWidthSizable];
00083 [_themesCollectionView setVerticalMargin:0.0];
00084 [_themesCollectionView setSelectable:YES];
00085 [_themesCollectionView setFrameOrigin:CGPointMake(0.0, 20.0)];
00086 [_themesCollectionView setAutoresizingMask:CPViewWidthSizable];
00087
00088 var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, LEFT_PANEL_WIDTH, CGRectGetHeight(bounds))],
00089 contentView = [scrollView contentView];
00090
00091 [scrollView setAutohidesScrollers:YES];
00092 [scrollView setDocumentView:_themesCollectionView];
00093
00094 [contentView setBackgroundColor:[CPColor colorWithRed:212.0 / 255.0 green:221.0 / 255.0 blue:230.0 / 255.0 alpha:1.0]];
00095 [contentView addSubview:label];
00096
00097 [splitView addSubview:scrollView];
00098
00099
00100 _themedObjectsCollectionView = [[CPCollectionView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(bounds) - LEFT_PANEL_WIDTH - 1.0, 10.0)];
00101
00102 var collectionViewItem = [[CPCollectionViewItem alloc] init];
00103
00104 [collectionViewItem setView:[[BKShowcaseCell alloc] init]];
00105
00106 [_themedObjectsCollectionView setItemPrototype:collectionViewItem];
00107 [_themedObjectsCollectionView setVerticalMargin:20.0];
00108 [_themedObjectsCollectionView setAutoresizingMask:CPViewWidthSizable];
00109
00110 var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(LEFT_PANEL_WIDTH + 1.0, 0.0, CGRectGetWidth(bounds) - LEFT_PANEL_WIDTH - 1.0, CGRectGetHeight(bounds))];
00111
00112 [scrollView setHasHorizontalScroller:NO];
00113 [scrollView setAutohidesScrollers:YES];
00114 [scrollView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
00115 [scrollView setDocumentView:_themedObjectsCollectionView];
00116
00117 [splitView addSubview:scrollView];
00118
00119 [_themesCollectionView setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]];
00120
00121 [theWindow setFullBridge:YES];
00122 [theWindow makeKeyAndOrderFront:self];
00123 }
00124
00125 - (void)collectionViewDidChangeSelection:(CPCollectionView)aCollectionView
00126 {
00127 var themeDescriptorClass = _themeDescriptorClasses[[[aCollectionView selectionIndexes] firstIndex]],
00128 itemSize = [themeDescriptorClass itemSize];
00129
00130
00131 itemSize.width = MAX(100.0, itemSize.width + 20.0);
00132 itemSize.height = MAX(100.0, itemSize.height + 30.0);
00133
00134 [_themedObjectsCollectionView setMinItemSize:itemSize];
00135 [_themedObjectsCollectionView setMaxItemSize:itemSize];
00136
00137 [_themedObjectsCollectionView setContent:[themeDescriptorClass themedObjectTemplates]];
00138 [BKShowcaseCell setBackgroundColor:[themeDescriptorClass showcaseBackgroundColor]];
00139 }
00140
00141 - (BOOL)hasLearnMoreURL
00142 {
00143 return [[CPBundle mainBundle] objectForInfoDictionaryKey:@"BKLearnMoreURL"];
00144 }
00145
00146 - (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
00147 {
00148 return [BKLearnMoreToolbarItemIdentifier, CPToolbarSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier, BKBackgroundColorToolbarItemIdentifier, BKStateToolbarItemIdentifier];
00149 }
00150
00151 - (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
00152 {
00153 var itemIdentifiers = [CPToolbarFlexibleSpaceItemIdentifier, BKBackgroundColorToolbarItemIdentifier, BKStateToolbarItemIdentifier];
00154
00155 if ([self hasLearnMoreURL])
00156 itemIdentifiers = [BKLearnMoreToolbarItemIdentifier].concat(itemIdentifiers);
00157
00158 return itemIdentifiers;
00159 }
00160
00161 - (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anItemIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
00162 {
00163 var toolbarItem = [[CPToolbarItem alloc] initWithItemIdentifier:anItemIdentifier];
00164
00165 [toolbarItem setTarget:self];
00166
00167 if (anItemIdentifier === BKStateToolbarItemIdentifier)
00168 {
00169 var popUpButton = [CPPopUpButton buttonWithTitle:@"Enabled"];
00170
00171 [popUpButton addItemWithTitle:@"Disabled"];
00172
00173 [toolbarItem setView:popUpButton];
00174 [toolbarItem setTarget:nil];
00175 [toolbarItem setAction:@selector(changeState:)];
00176 [toolbarItem setLabel:@"State"];
00177
00178 var width = CGRectGetWidth([popUpButton frame]);
00179
00180 [toolbarItem setMinSize:CGSizeMake(width + 20.0, 32.0)];
00181 [toolbarItem setMaxSize:CGSizeMake(width + 20.0, 32.0)];
00182 }
00183
00184 else if (anItemIdentifier === BKBackgroundColorToolbarItemIdentifier)
00185 {
00186 var popUpButton = [CPPopUpButton buttonWithTitle:@"Window Background"];
00187
00188 [popUpButton addItemWithTitle:@"Light Checkers"];
00189 [popUpButton addItemWithTitle:@"Dark Checkers"];
00190 [popUpButton addItemWithTitle:@"White"];
00191 [popUpButton addItemWithTitle:@"Black"];
00192 [popUpButton addItemWithTitle:@"More Choices..."];
00193
00194 var itemArray = [popUpButton itemArray];
00195
00196 [itemArray[0] setRepresentedObject:[BKThemeDescriptor windowBackgroundColor]];
00197 [itemArray[1] setRepresentedObject:[BKThemeDescriptor lightCheckersColor]];
00198 [itemArray[2] setRepresentedObject:[BKThemeDescriptor darkCheckersColor]];
00199 [itemArray[3] setRepresentedObject:[CPColor whiteColor]];
00200 [itemArray[4] setRepresentedObject:[CPColor blackColor]];
00201
00202 [toolbarItem setView:popUpButton];
00203 [toolbarItem setTarget:nil];
00204 [toolbarItem setAction:@selector(changeColor:)];
00205 [toolbarItem setLabel:@"Background Color"];
00206
00207 var width = CGRectGetWidth([popUpButton frame]);
00208
00209 [toolbarItem setMinSize:CGSizeMake(width, 32.0)];
00210 [toolbarItem setMaxSize:CGSizeMake(width, 32.0)];
00211 }
00212 else if (anItemIdentifier === BKLearnMoreToolbarItemIdentifier)
00213 {
00214 var title = [[CPBundle mainBundle] objectForInfoDictionaryKey:@"BKLearnMoreButtonTitle"];
00215
00216 if (!title)
00217 title = [[CPBundle mainBundle] objectForInfoDictionaryKey:@"CPBundleName"] || @"Home Page";
00218
00219 var button = [CPButton buttonWithTitle:title];
00220
00221 [button setDefaultButton:YES];
00222
00223 [toolbarItem setView:button];
00224 [toolbarItem setLabel:@"Learn More"];
00225 [toolbarItem setTarget:nil];
00226 [toolbarItem setAction:@selector(learnMore:)];
00227
00228 var width = CGRectGetWidth([button frame]);
00229
00230 [toolbarItem setMinSize:CGSizeMake(width, 32.0)];
00231 [toolbarItem setMaxSize:CGSizeMake(width, 32.0)];
00232 }
00233
00234 return toolbarItem;
00235 }
00236
00237 - (void)learnMore:(id)aSender
00238 {
00239 window.location.href = [[CPBundle mainBundle] objectForInfoDictionaryKey:@"BKLearnMoreURL"];
00240 }
00241
00242 - (BKThemeDescriptor)selectedThemeDescriptor
00243 {
00244 return _themeDescriptorClasses[[[_themesCollectionView selectionIndexes] firstIndex]];
00245 }
00246
00247 - (void)changeState:(id)aSender
00248 {
00249 var themedObjectTemplates = [[self selectedThemeDescriptor] themedObjectTemplates],
00250 count = [themedObjectTemplates count];
00251
00252 while (count--)
00253 {
00254 var themedObject = [themedObjectTemplates[count] valueForKey:@"themedObject"];
00255
00256 if ([themedObject respondsToSelector:@selector(setEnabled:)])
00257 [themedObject setEnabled:[aSender title] === @"Enabled" ? YES : NO];
00258 }
00259 }
00260
00261 - (void)changeColor:(id)aSender
00262 {
00263 var color = nil;
00264
00265 if ([aSender isKindOfClass:[CPColorPanel class]])
00266 color = [aSender color];
00267
00268 else
00269 {
00270 if ([aSender titleOfSelectedItem] === @"More Choices...")
00271 {
00272 [aSender addItemWithTitle:@"Other"];
00273 [aSender selectItemWithTitle:@"Other"];
00274
00275 [CPApp orderFrontColorPanel:self];
00276 }
00277 else
00278 {
00279 color = [[aSender selectedItem] representedObject];
00280
00281 [aSender removeItemWithTitle:@"Other"];
00282 }
00283 }
00284
00285 if (color)
00286 {
00287 [[self selectedThemeDescriptor] setShowcaseBackgroundColor:color];
00288 [BKShowcaseCell setBackgroundColor:color];
00289 }
00290 }
00291
00292 @end
00293
00294 var SelectionColor = nil;
00295
00296 @implementation BKThemeDescriptorCell : CPView
00297 {
00298 CPTextField _label;
00299 }
00300
00301 + (CPImage)selectionColor
00302 {
00303 if (!SelectionColor)
00304 SelectionColor = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[BKThemeDescriptorCell class]] pathForResource:@"selection.png"] size:CGSizeMake(1.0, 36.0)]];
00305
00306 return SelectionColor;
00307 }
00308
00309 - (void)setRepresentedObject:(id)aThemeDescriptor
00310 {
00311 if (!_label)
00312 {
00313 _label = [CPTextField labelWithTitle:@"hello"];
00314
00315 [_label setFont:[CPFont systemFontOfSize:11.0]];
00316 [_label setFrame:CGRectMake(10.0, 0.0, CGRectGetWidth([self bounds]) - 20.0, CGRectGetHeight([self bounds]))];
00317
00318 [_label setVerticalAlignment:CPCenterVerticalTextAlignment];
00319 [_label setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
00320
00321 [self addSubview:_label];
00322 }
00323
00324 [_label setStringValue:[aThemeDescriptor themeName] + " (" + [[aThemeDescriptor themedObjectTemplates] count] + ")"];
00325 }
00326
00327 - (void)setSelected:(BOOL)isSelected
00328 {
00329 [self setBackgroundColor:isSelected ? [[self class] selectionColor] : nil];
00330
00331 [_label setTextShadowOffset:isSelected ? CGSizeMake(0.0, 1.0) : CGSizeMakeZero()];
00332 [_label setTextShadowColor:isSelected ? [CPColor blackColor] : nil];
00333 [_label setFont:isSelected ? [CPFont boldSystemFontOfSize:11.0] : [CPFont systemFontOfSize:11.0]];
00334 [_label setTextColor:isSelected ? [CPColor whiteColor] : [CPColor blackColor]];
00335 }
00336
00337 @end
00338
00339
00340 var ShowcaseCellBackgroundColor = nil;
00341
00342 var BKShowcaseCellBackgroundColorDidChangeNotification = @"BKShowcaseCellBackgroundColorDidChangeNotification";
00343
00344 @implementation BKShowcaseCell : CPView
00345 {
00346 CPView _backgroundView;
00347
00348 CPView _view;
00349 CPTextField _label;
00350 }
00351
00352 + (void)setBackgroundColor:(CPColor)aColor
00353 {
00354 if (ShowcaseCellBackgroundColor === aColor)
00355 return;
00356
00357 ShowcaseCellBackgroundColor = aColor;
00358
00359 [[CPNotificationCenter defaultCenter]
00360 postNotificationName:BKShowcaseCellBackgroundColorDidChangeNotification
00361 object:nil];
00362 }
00363
00364 + (CPColor)backgroundColor
00365 {
00366 return ShowcaseCellBackgroundColor;
00367 }
00368
00369 - (id)init
00370 {
00371 self = [super init];
00372
00373 if (self)
00374 [[CPNotificationCenter defaultCenter]
00375 addObserver:self
00376 selector:@selector(showcaseBackgroundDidChange:)
00377 name:BKShowcaseCellBackgroundColorDidChangeNotification
00378 object:nil];
00379
00380 return self;
00381 }
00382
00383 - (id)initWithCoder:(CPCoder)aCoder
00384 {
00385 self = [super initWithCoder:aCoder];
00386
00387 if (self)
00388 [[CPNotificationCenter defaultCenter]
00389 addObserver:self
00390 selector:@selector(showcaseBackgroundDidChange:)
00391 name:BKShowcaseCellBackgroundColorDidChangeNotification
00392 object:nil];
00393
00394 return self;
00395 }
00396
00397 - (void)showcaseBackgroundDidChange:(CPNotification)aNotification
00398 {
00399 [_backgroundView setBackgroundColor:[BKShowcaseCell backgroundColor]];
00400 }
00401
00402 - (void)setSelected:(BOOL)isSelected
00403 {
00404 }
00405
00406 - (void)setRepresentedObject:(id)anObject
00407 {
00408 if (!_label)
00409 {
00410 _label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
00411
00412 [_label setAlignment:CPCenterTextAlignment];
00413 [_label setAutoresizingMask:CPViewMinYMargin | CPViewWidthSizable];
00414 [_label setFont:[CPFont boldSystemFontOfSize:11.0]];
00415
00416 [self addSubview:_label];
00417 }
00418
00419 [_label setStringValue:[anObject valueForKey:@"label"]];
00420 [_label sizeToFit];
00421
00422 [_label setFrame:CGRectMake(0.0, CGRectGetHeight([self bounds]) - CGRectGetHeight([_label frame]),
00423 CGRectGetWidth([self bounds]), CGRectGetHeight([_label frame]))];
00424
00425 if (!_backgroundView)
00426 {
00427 _backgroundView = [[CPView alloc] init];
00428
00429 [self addSubview:_backgroundView];
00430 }
00431
00432 [_backgroundView setFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([self bounds]), CGRectGetMinY([_label frame]))];
00433 [_backgroundView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
00434
00435 if (_view)
00436 [_view removeFromSuperview];
00437
00438 _view = [anObject valueForKey:@"themedObject"];
00439
00440 [_view setTheme:nil];
00441 [_view setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
00442 [_view setFrameOrigin:CGPointMake((CGRectGetWidth([_backgroundView bounds]) - CGRectGetWidth([_view frame])) / 2.0,
00443 (CGRectGetHeight([_backgroundView bounds]) - CGRectGetHeight([_view frame])) / 2.0)];
00444
00445 [_backgroundView addSubview:_view];
00446 [_backgroundView setBackgroundColor:[BKShowcaseCell backgroundColor]];
00447 }
00448
00449 @end