00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPObject.j>
00024 @import <Foundation/CPString.j>
00025
00026 @import <AppKit/CPImage.j>
00027 @import <AppKit/CPView.j>
00028
00029 CPToolbarItemVisibilityPriorityStandard = 0;
00030 CPToolbarItemVisibilityPriorityLow = -1000;
00031 CPToolbarItemVisibilityPriorityHigh = 1000;
00032 CPToolbarItemVisibilityPriorityUser = 2000;
00033
00034 CPToolbarSeparatorItemIdentifier = @"CPToolbarSeparatorItem";
00035 CPToolbarSpaceItemIdentifier = @"CPToolbarSpaceItem";
00036 CPToolbarFlexibleSpaceItemIdentifier = @"CPToolbarFlexibleSpaceItem";
00037 CPToolbarShowColorsItemIdentifier = @"CPToolbarShowColorsItem";
00038 CPToolbarShowFontsItemIdentifier = @"CPToolbarShowFontsItem";
00039 CPToolbarCustomizeToolbarItemIdentifier = @"CPToolbarCustomizeToolbarItem";
00040 CPToolbarPrintItemIdentifier = @"CPToolbarPrintItem";
00041
00048 @implementation CPToolbarItem : CPObject
00049 {
00050 CPString _itemIdentifier;
00051
00052 CPToolbar _toolbar;
00053
00054 CPString _label;
00055 CPString _paletteLabel;
00056 CPString _toolTip;
00057 int _tag;
00058 id _target;
00059 SEL _action;
00060 BOOL _isEnabled;
00061 CPImage _image;
00062 CPImage _alternateImage;
00063
00064 CPView _view;
00065
00066 CGSize _minSize;
00067 CGSize _maxSize;
00068
00069 int _visibilityPriority;
00070
00071 BOOL _autovalidates;
00072 }
00073
00074 - (id)init
00075 {
00076 return [self initWithItemIdentifier:@""];
00077 }
00078
00079
00085 - (id)initWithItemIdentifier:(CPString)anItemIdentifier
00086 {
00087 self = [super init];
00088
00089 if (self)
00090 {
00091 _itemIdentifier = anItemIdentifier;
00092
00093 _tag = 0;
00094 _isEnabled = YES;
00095
00096 _minSize = CGSizeMakeZero();
00097 _maxSize = CGSizeMakeZero();
00098
00099 _visibilityPriority = CPToolbarItemVisibilityPriorityStandard;
00100 _autovalidates = YES;
00101 }
00102
00103 return self;
00104 }
00105
00106
00110 - (CPString)itemIdentifier
00111 {
00112 return _itemIdentifier;
00113 }
00114
00118 - (CPToolbar)toolbar
00119 {
00120 return _toolbar;
00121 }
00122
00123
00124 - (void)_setToolbar:(CPToolbar)aToolbar
00125 {
00126 _toolbar = aToolbar;
00127 }
00128
00132 - (CPString)label
00133 {
00134 return _label;
00135 }
00136
00141 - (void)setLabel:(CPString)aLabel
00142 {
00143 _label = aLabel;
00144 }
00145
00149 - (CPString)paletteLabel
00150 {
00151 return _paletteLabel;
00152 }
00153
00158 - (void)setPaletteLabel:(CPString)aPaletteLabel
00159 {
00160 _paletteLabel = aPaletteLabel;
00161 }
00162
00168 - (CPString)toolTip
00169 {
00170 if ([_view respondsToSelector:@selector(toolTip)])
00171 return [_view toolTip];
00172
00173 return _toolTip;
00174 }
00175
00180 - (void)setToolTip:(CPString)aToolTip
00181 {
00182 if ([_view respondsToSelector:@selector(setToolTip:)])
00183 [_view setToolTip:aToolTip];
00184
00185 _toolTip = aToolTip;
00186 }
00187
00191 - (int)tag
00192 {
00193 if ([_view respondsToSelector:@selector(tag)])
00194 return [_view tag];
00195
00196 return _tag;
00197 }
00198
00203 - (void)setTag:(int)aTag
00204 {
00205 if ([_view respondsToSelector:@selector(setTag:)])
00206 [_view setTag:aTag];
00207
00208 _tag = aTag;
00209 }
00210
00214 - (id)target
00215 {
00216 if (_view)
00217 return [_view respondsToSelector:@selector(target)] ? [_view target] : nil;
00218
00219 return _target;
00220 }
00221
00227 - (void)setTarget:(id)aTarget
00228 {
00229 if (!_view)
00230 _target = aTarget;
00231
00232 else if ([_view respondsToSelector:@selector(setTarget:)])
00233 [_view setTarget:aTarget];
00234 }
00235
00239 - (SEL)action
00240 {
00241 if (_view)
00242 return [_view respondsToSelector:@selector(action)] ? [_view action] : nil;
00243
00244 return _action;
00245 }
00246
00251 - (void)setAction:(SEL)anAction
00252 {
00253 if (!_view)
00254 _action = anAction;
00255
00256 else if ([_view respondsToSelector:@selector(setAction:)])
00257 [_view setAction:anAction];
00258 }
00259
00263 - (BOOL)isEnabled
00264 {
00265 if ([_view respondsToSelector:@selector(isEnabled)])
00266 return [_view isEnabled];
00267
00268 return _isEnabled;
00269 }
00270
00275 - (void)setEnabled:(BOOL)shouldBeEnabled
00276 {
00277 if ([_view respondsToSelector:@selector(setEnabled:)])
00278 [_view setEnabled:shouldBeEnabled];
00279
00280 _isEnabled = shouldBeEnabled;
00281 }
00282
00286 - (CPImage)image
00287 {
00288 if ([_view respondsToSelector:@selector(image)])
00289 return [_view image];
00290
00291 return _image;
00292 }
00293
00298 - (void)setImage:(CPImage)anImage
00299 {
00300 if ([_view respondsToSelector:@selector(setImage:)])
00301 [_view setImage:anImage];
00302
00303 _image = anImage;
00304
00305 if (!_image)
00306 return;
00307
00308 if (_minSize.width === 0 && _minSize.height === 0 &&
00309 _maxSize.width === 0 && _maxSize.height === 0)
00310 {
00311 var imageSize = [_image size];
00312
00313 if (imageSize.width > 0 || imageSize.height > 0)
00314 {
00315 [self setMinSize:imageSize];
00316 [self setMaxSize:imageSize];
00317 }
00318 }
00319 }
00320
00325 - (void)setAlternateImage:(CPImage)anImage
00326 {
00327 if ([_view respondsToSelector:@selector(setAlternateImage:)])
00328 [_view setAlternateImage:anImage];
00329
00330 _alternateImage = anImage;
00331 }
00332
00336 - (CPImage)alternateImage
00337 {
00338 if ([_view respondsToSelector:@selector(alternateIamge)])
00339 return [_view alternateImage];
00340
00341 return _alternateImage;
00342 }
00343
00347 - (CPView)view
00348 {
00349 return _view;
00350 }
00351
00356 - (void)setView:(CPView)aView
00357 {
00358 if (_view == aView)
00359 return;
00360
00361 _view = aView;
00362
00363 if (_view)
00364 {
00365
00366 if (_tag !== 0 && [_view respondsToSelector:@selector(setTag:)])
00367 [_view setTag:_tag];
00368
00369 _target = nil;
00370 _action = nil;
00371 }
00372 }
00373
00377 - (CGSize)minSize
00378 {
00379 return _minSize;
00380 }
00381
00386 - (void)setMinSize:(CGSize)aMinSize
00387 {
00388 if(!aMinSize.height || !aMinSize.width)
00389 return;
00390
00391 _minSize = CGSizeMakeCopy(aMinSize);
00392
00393
00394 _maxSize = CGSizeMake(MAX(_minSize.width, _maxSize.width), MAX(_minSize.height, _maxSize.height));
00395 }
00396
00400 - (CGSize)maxSize
00401 {
00402 return _maxSize;
00403 }
00404
00409 - (void)setMaxSize:(CGSize)aMaxSize
00410 {
00411 if(!aMaxSize.height || !aMaxSize.width)
00412 return;
00413
00414 _maxSize = CGSizeMakeCopy(aMaxSize);
00415
00416
00417 _minSize = CGSizeMake(MIN(_minSize.width, _maxSize.width), MIN(_minSize.height, _maxSize.height));
00418 }
00419
00420
00430 - (int)visibilityPriority
00431 {
00432 return _visibilityPriority;
00433 }
00434
00445 - (void)setVisibilityPriority:(int)aVisibilityPriority
00446 {
00447 _visibilityPriority = aVisibilityPriority;
00448 }
00449
00450 - (void)validate
00451 {
00452
00453 if (_view)
00454 {
00455 if ([target respondsToSelector:@selector(validateToolbarItem:)])
00456 [self setEnabled:[target validateToolbarItem:self]];
00457
00458 return;
00459 }
00460
00461 var action = [self action];
00462
00463 if (!action)
00464 return [self setEnabled:NO];
00465
00466 var target = [self target];
00467
00468 if (target && ![target respondsToSelector:action])
00469 return [self setEnabled:NO];
00470
00471 target = [CPApp targetForAction:action to:target from:self];
00472
00473 if (!target)
00474 return [self setEnabled:NO];
00475
00476 if ([target respondsToSelector:@selector(validateToolbarItem:)])
00477 [self setEnabled:[target validateToolbarItem:self]];
00478 else
00479 [self setEnabled:YES];
00480 }
00481
00482 - (BOOL)autovalidates
00483 {
00484 return _autovalidates;
00485 }
00486
00487 - (void)setAutovalidates:(BOOL)shouldAutovalidate
00488 {
00489 _autovalidates = !!shouldAutovalidate;
00490 }
00491
00492 @end
00493
00494 var CPToolbarItemItemIdentifierKey = @"CPToolbarItemItemIdentifierKey",
00495 CPToolbarItemLabelKey = @"CPToolbarItemLabelKey",
00496 CPToolbarItemPaletteLabelKey = @"CPToolbarItemPaletteLabelKey",
00497 CPToolbarItemToolTipKey = @"CPToolbarItemToolTipKey",
00498 CPToolbarItemTagKey = @"CPToolbarItemTagKey",
00499 CPToolbarItemTargetKey = @"CPToolbarItemTargetKey",
00500 CPToolbarItemActionKey = @"CPToolbarItemActionKey",
00501 CPToolbarItemEnabledKey = @"CPToolbarItemEnabledKey",
00502 CPToolbarItemImageKey = @"CPToolbarItemImageKey",
00503 CPToolbarItemAlternateImageKey = @"CPToolbarItemAlternateImageKey",
00504 CPToolbarItemViewKey = @"CPToolbarItemViewKey",
00505 CPToolbarItemMinSizeKey = @"CPToolbarItemMinSizeKey",
00506 CPToolbarItemMaxSizeKey = @"CPToolbarItemMaxSizeKey",
00507 CPToolbarItemVisibilityPriorityKey = @"CPToolbarItemVisibilityPriorityKey",
00508 CPToolbarItemAutovalidatesKey = @"CPToolbarItemAutovalidatesKey";
00509
00510 @implementation CPToolbarItem (CPCoding)
00511
00512 - (id)initWithCoder:(CPCoder)aCoder
00513 {
00514 self = [super init];
00515
00516 if (self)
00517 {
00518 _itemIdentifier = [aCoder decodeObjectForKey:CPToolbarItemItemIdentifierKey];
00519
00520 _minSize = [aCoder decodeSizeForKey:CPToolbarItemMinSizeKey];
00521 _maxSize = [aCoder decodeSizeForKey:CPToolbarItemMaxSizeKey];
00522
00523 [self setLabel:[aCoder decodeObjectForKey:CPToolbarItemLabelKey]];
00524 [self setPaletteLabel:[aCoder decodeObjectForKey:CPToolbarItemPaletteLabelKey]];
00525 [self setToolTip:[aCoder decodeObjectForKey:CPToolbarItemToolTipKey]];
00526
00527 [self setTag:[aCoder decodeObjectForKey:CPToolbarItemTagKey]];
00528 [self setTarget:[aCoder decodeObjectForKey:CPToolbarItemTargetKey]];
00529 [self setAction:CPSelectorFromString([aCoder decodeObjectForKey:CPToolbarItemActionKey])];
00530
00531 [self setEnabled:[aCoder decodeBoolForKey:CPToolbarItemEnabledKey]];
00532
00533 [self setImage:[aCoder decodeObjectForKey:CPToolbarItemImageKey]];
00534 [self setAlternateImage:[aCoder decodeObjectForKey:CPToolbarItemAlternateImageKey]];
00535
00536 [self setView:[aCoder decodeObjectForKey:CPToolbarItemViewKey]];
00537
00538 [self setVisibilityPriority:[aCoder decodeIntForKey:CPToolbarItemVisibilityPriorityKey]];
00539 [self setAutovalidates:[aCoder decodeBoolForKey:CPToolbarItemAutovalidatesKey]];
00540 }
00541
00542 return self;
00543 }
00544
00545 - (void)encodeWithCoder:(CPCoder)aCoder
00546 {
00547 [aCoder encodeObject:_itemIdentifier forKey:CPToolbarItemItemIdentifierKey];
00548
00549 [aCoder encodeObject:[self label] forKey:CPToolbarItemLabelKey];
00550 [aCoder encodeObject:[self paletteLabel] forKey:CPToolbarItemPaletteLabelKey];
00551
00552 [aCoder encodeObject:[self toolTip] forKey:CPToolbarItemToolTipKey];
00553
00554 [aCoder encodeObject:[self tag] forKey:CPToolbarItemTagKey];
00555 [aCoder encodeObject:[self target] forKey:CPToolbarItemTargetKey];
00556 [aCoder encodeObject:[self action] forKey:CPToolbarItemActionKey];
00557
00558 [aCoder encodeObject:[self isEnabled] forKey:CPToolbarItemEnabledKey];
00559
00560 [aCoder encodeObject:[self image] forKey:CPToolbarItemImageKey];
00561 [aCoder encodeObject:[self alternateImage] forKey:CPToolbarItemAlternateImageKey];
00562
00563 [aCoder encodeObject:[self view] forKey:CPToolbarItemViewKey];
00564
00565 [aCoder encodeSize:[self minSize] forKey:CPToolbarItemMinSizeKey];
00566 [aCoder encodeSize:[self maxSize] forKey:CPToolbarItemMaxSizeKey];
00567
00568 [aCoder encodeObject:[self visibilityPriority] forKey:CPToolbarItemVisibilityPriorityKey];
00569 [aCoder encodeBool:[self autovalidates] forKey:CPToolbarItemAutovalidatesKey];
00570 }
00571
00572 @end
00573
00574 @implementation CPToolbarItem (CPCopying)
00575
00576 - (id)copy
00577 {
00578 var copy = [[[self class] alloc] initWithItemIdentifier:_itemIdentifier];
00579
00580 if (_view)
00581 [copy setView:[CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:_view]]];
00582
00583 [copy _setToolbar:_toolbar];
00584
00585 [copy setLabel:_label];
00586 [copy setPaletteLabel:_paletteLabel];
00587 [copy setToolTip:[self toolTip]];
00588
00589 [copy setTag:[self tag]];
00590 [copy setTarget:[self target]];
00591 [copy setAction:[self action]];
00592
00593 [copy setEnabled:[self isEnabled]];
00594
00595 [copy setImage:[self image]];
00596 [copy setAlternateImage:[self alternateImage]];
00597
00598 [copy setMinSize:_minSize];
00599 [copy setMaxSize:_maxSize];
00600
00601 [copy setVisibilityPriority:[self visibilityPriority]];
00602 [copy setAutovalidates:[self autovalidates]];
00603
00604 return copy;
00605 }
00606
00607 @end
00608
00609
00610
00611 @implementation CPToolbarItem (Standard)
00612
00613
00614 + (CPToolbarItem)_standardItemWithItemIdentifier:(CPString)anItemIdentifier
00615 {
00616 switch (anItemIdentifier)
00617 {
00618 case CPToolbarSeparatorItemIdentifier: return [_CPToolbarSeparatorItem new];
00619 case CPToolbarSpaceItemIdentifier: return [_CPToolbarSpaceItem new];
00620 case CPToolbarFlexibleSpaceItemIdentifier: return [_CPToolbarFlexibleSpaceItem new];
00621 case CPToolbarShowColorsItemIdentifier: return [_CPToolbarShowColorsItem new];
00622 case CPToolbarShowFontsItemIdentifier: return nil;
00623 case CPToolbarCustomizeToolbarItemIdentifier: return nil;
00624 case CPToolbarPrintItemIdentifier: return nil;
00625 }
00626
00627 return nil;
00628 }
00629
00630 @end
00631
00632 @import "_CPToolbarFlexibleSpaceItem.j"
00633 @import "_CPToolbarShowColorsItem.j"
00634 @import "_CPToolbarSeparatorItem.j"
00635 @import "_CPToolbarSpaceItem.j"
00636