![]() |
API 0.9.5
|
00001 /* 00002 * CPToolbarItem.j 00003 * AppKit 00004 * 00005 * Created by Francisco Tolmasky. 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00024 00025 00026 CPToolbarItemVisibilityPriorityStandard = 0; 00027 CPToolbarItemVisibilityPriorityLow = -1000; 00028 CPToolbarItemVisibilityPriorityHigh = 1000; 00029 CPToolbarItemVisibilityPriorityUser = 2000; 00030 00031 CPToolbarSeparatorItemIdentifier = @"CPToolbarSeparatorItem"; 00032 CPToolbarSpaceItemIdentifier = @"CPToolbarSpaceItem"; 00033 CPToolbarFlexibleSpaceItemIdentifier = @"CPToolbarFlexibleSpaceItem"; 00034 CPToolbarShowColorsItemIdentifier = @"CPToolbarShowColorsItem"; 00035 CPToolbarShowFontsItemIdentifier = @"CPToolbarShowFontsItem"; 00036 CPToolbarCustomizeToolbarItemIdentifier = @"CPToolbarCustomizeToolbarItem"; 00037 CPToolbarPrintItemIdentifier = @"CPToolbarPrintItem"; 00038 00045 @implementation CPToolbarItem : CPObject 00046 { 00047 CPString _itemIdentifier; 00048 00049 CPToolbar _toolbar; 00050 00051 CPString _label; 00052 CPString _paletteLabel; 00053 CPString _toolTip; 00054 int _tag; 00055 id _target; 00056 SEL _action; 00057 BOOL _isEnabled; 00058 CPImage _image; 00059 CPImage _alternateImage; 00060 00061 CPView _view; 00062 00063 CGSize _minSize; 00064 CGSize _maxSize; 00065 00066 int _visibilityPriority; 00067 00068 BOOL _autovalidates; 00069 } 00070 00071 - (id)init 00072 { 00073 return [self initWithItemIdentifier:@""]; 00074 } 00075 00076 // Creating a Toolbar Item 00082 - (id)initWithItemIdentifier:(CPString)anItemIdentifier 00083 { 00084 self = [super init]; 00085 00086 if (self) 00087 { 00088 _itemIdentifier = anItemIdentifier; 00089 00090 _tag = 0; 00091 _isEnabled = YES; 00092 00093 _minSize = CGSizeMakeZero(); 00094 _maxSize = CGSizeMakeZero(); 00095 00096 _visibilityPriority = CPToolbarItemVisibilityPriorityStandard; 00097 _autovalidates = YES; 00098 } 00099 00100 return self; 00101 } 00102 00103 // Managing Attributes 00107 - (CPString)itemIdentifier 00108 { 00109 return _itemIdentifier; 00110 } 00111 00115 - (CPToolbar)toolbar 00116 { 00117 return _toolbar; 00118 } 00119 00120 /* @ignore */ 00121 - (void)_setToolbar:(CPToolbar)aToolbar 00122 { 00123 _toolbar = aToolbar; 00124 } 00125 00129 - (CPString)label 00130 { 00131 return _label; 00132 } 00133 00138 - (void)setLabel:(CPString)aLabel 00139 { 00140 _label = aLabel; 00141 } 00142 00146 - (CPString)paletteLabel 00147 { 00148 return _paletteLabel; 00149 } 00150 00155 - (void)setPaletteLabel:(CPString)aPaletteLabel 00156 { 00157 _paletteLabel = aPaletteLabel; 00158 } 00159 00165 - (CPString)toolTip 00166 { 00167 if ([_view respondsToSelector:@selector(toolTip)]) 00168 return [_view toolTip]; 00169 00170 return _toolTip; 00171 } 00172 00177 - (void)setToolTip:(CPString)aToolTip 00178 { 00179 if ([_view respondsToSelector:@selector(setToolTip:)]) 00180 [_view setToolTip:aToolTip]; 00181 00182 _toolTip = aToolTip; 00183 } 00184 00188 - (int)tag 00189 { 00190 if ([_view respondsToSelector:@selector(tag)]) 00191 return [_view tag]; 00192 00193 return _tag; 00194 } 00195 00200 - (void)setTag:(int)aTag 00201 { 00202 if ([_view respondsToSelector:@selector(setTag:)]) 00203 [_view setTag:aTag]; 00204 00205 _tag = aTag; 00206 } 00207 00211 - (id)target 00212 { 00213 if (_view) 00214 return [_view respondsToSelector:@selector(target)] ? [_view target] : nil; 00215 00216 return _target; 00217 } 00218 00224 - (void)setTarget:(id)aTarget 00225 { 00226 if (!_view) 00227 _target = aTarget; 00228 00229 else if ([_view respondsToSelector:@selector(setTarget:)]) 00230 [_view setTarget:aTarget]; 00231 } 00232 00236 - (SEL)action 00237 { 00238 if (_view) 00239 return [_view respondsToSelector:@selector(action)] ? [_view action] : nil; 00240 00241 return _action; 00242 } 00243 00248 - (void)setAction:(SEL)anAction 00249 { 00250 if (!_view) 00251 _action = anAction; 00252 00253 else if ([_view respondsToSelector:@selector(setAction:)]) 00254 [_view setAction:anAction]; 00255 } 00256 00260 - (BOOL)isEnabled 00261 { 00262 if ([_view respondsToSelector:@selector(isEnabled)]) 00263 return [_view isEnabled]; 00264 00265 return _isEnabled; 00266 } 00267 00272 - (void)setEnabled:(BOOL)shouldBeEnabled 00273 { 00274 if ([_view respondsToSelector:@selector(setEnabled:)]) 00275 [_view setEnabled:shouldBeEnabled]; 00276 00277 _isEnabled = shouldBeEnabled; 00278 } 00279 00283 - (CPImage)image 00284 { 00285 if ([_view respondsToSelector:@selector(image)]) 00286 return [_view image]; 00287 00288 return _image; 00289 } 00290 00295 - (void)setImage:(CPImage)anImage 00296 { 00297 if ([_view respondsToSelector:@selector(setImage:)]) 00298 [_view setImage:anImage]; 00299 00300 _image = anImage; 00301 00302 if (!_image) 00303 return; 00304 00305 if (_minSize.width === 0 && _minSize.height === 0 && 00306 _maxSize.width === 0 && _maxSize.height === 0) 00307 { 00308 var imageSize = [_image size]; 00309 00310 if (imageSize.width > 0 || imageSize.height > 0) 00311 { 00312 [self setMinSize:imageSize]; 00313 [self setMaxSize:imageSize]; 00314 } 00315 } 00316 } 00317 00322 - (void)setAlternateImage:(CPImage)anImage 00323 { 00324 if ([_view respondsToSelector:@selector(setAlternateImage:)]) 00325 [_view setAlternateImage:anImage]; 00326 00327 _alternateImage = anImage; 00328 } 00329 00333 - (CPImage)alternateImage 00334 { 00335 if ([_view respondsToSelector:@selector(alternateIamge)]) 00336 return [_view alternateImage]; 00337 00338 return _alternateImage; 00339 } 00340 00344 - (CPView)view 00345 { 00346 return _view; 00347 } 00348 00353 - (void)setView:(CPView)aView 00354 { 00355 if (_view == aView) 00356 return; 00357 00358 _view = aView; 00359 00360 if (_view) 00361 { 00362 // Tags get forwarded. 00363 if (_tag !== 0 && [_view respondsToSelector:@selector(setTag:)]) 00364 [_view setTag:_tag]; 00365 00366 _target = nil; 00367 _action = nil; 00368 } 00369 } 00370 00374 - (CGSize)minSize 00375 { 00376 return _minSize; 00377 } 00378 00383 - (void)setMinSize:(CGSize)aMinSize 00384 { 00385 if (!aMinSize.height || !aMinSize.width) 00386 return; 00387 00388 _minSize = CGSizeMakeCopy(aMinSize); 00389 00390 // Try to provide some sanity: Make maxSize >= minSize 00391 _maxSize = CGSizeMake(MAX(_minSize.width, _maxSize.width), MAX(_minSize.height, _maxSize.height)); 00392 } 00393 00397 - (CGSize)maxSize 00398 { 00399 return _maxSize; 00400 } 00401 00406 - (void)setMaxSize:(CGSize)aMaxSize 00407 { 00408 if (!aMaxSize.height || !aMaxSize.width) 00409 return; 00410 00411 _maxSize = CGSizeMakeCopy(aMaxSize); 00412 00413 // Try to provide some sanity: Make minSize <= maxSize 00414 _minSize = CGSizeMake(MIN(_minSize.width, _maxSize.width), MIN(_minSize.height, _maxSize.height)); 00415 } 00416 00417 // Visibility Priority 00427 - (int)visibilityPriority 00428 { 00429 return _visibilityPriority; 00430 } 00431 00442 - (void)setVisibilityPriority:(int)aVisibilityPriority 00443 { 00444 _visibilityPriority = aVisibilityPriority; 00445 } 00446 00447 - (void)validate 00448 { 00449 var action = [self action], 00450 target = [self target]; 00451 00452 // View items do not do any target-action analysis. 00453 if (_view) 00454 { 00455 if ([target respondsToSelector:@selector(validateToolbarItem:)]) 00456 [self setEnabled:[target validateToolbarItem:self]]; 00457 00458 return; 00459 } 00460 00461 if (!action) 00462 return [self setEnabled:NO]; 00463 00464 if (target && ![target respondsToSelector:action]) 00465 return [self setEnabled:NO]; 00466 00467 target = [CPApp targetForAction:action to:target from:self]; 00468 00469 if (!target) 00470 return [self setEnabled:NO]; 00471 00472 if ([target respondsToSelector:@selector(validateToolbarItem:)]) 00473 [self setEnabled:[target validateToolbarItem:self]]; 00474 else 00475 [self setEnabled:YES]; 00476 } 00477 00478 - (BOOL)autovalidates 00479 { 00480 return _autovalidates; 00481 } 00482 00483 - (void)setAutovalidates:(BOOL)shouldAutovalidate 00484 { 00485 _autovalidates = !!shouldAutovalidate; 00486 } 00487 00488 @end 00489 00490 var CPToolbarItemItemIdentifierKey = @"CPToolbarItemItemIdentifierKey", 00491 CPToolbarItemLabelKey = @"CPToolbarItemLabelKey", 00492 CPToolbarItemPaletteLabelKey = @"CPToolbarItemPaletteLabelKey", 00493 CPToolbarItemToolTipKey = @"CPToolbarItemToolTipKey", 00494 CPToolbarItemTagKey = @"CPToolbarItemTagKey", 00495 CPToolbarItemTargetKey = @"CPToolbarItemTargetKey", 00496 CPToolbarItemActionKey = @"CPToolbarItemActionKey", 00497 CPToolbarItemEnabledKey = @"CPToolbarItemEnabledKey", 00498 CPToolbarItemImageKey = @"CPToolbarItemImageKey", 00499 CPToolbarItemAlternateImageKey = @"CPToolbarItemAlternateImageKey", 00500 CPToolbarItemViewKey = @"CPToolbarItemViewKey", 00501 CPToolbarItemMinSizeKey = @"CPToolbarItemMinSizeKey", 00502 CPToolbarItemMaxSizeKey = @"CPToolbarItemMaxSizeKey", 00503 CPToolbarItemVisibilityPriorityKey = @"CPToolbarItemVisibilityPriorityKey", 00504 CPToolbarItemAutovalidatesKey = @"CPToolbarItemAutovalidatesKey"; 00505 00506 @implementation CPToolbarItem (CPCoding) 00507 00508 - (id)initWithCoder:(CPCoder)aCoder 00509 { 00510 self = [super init]; 00511 00512 if (self) 00513 { 00514 _itemIdentifier = [aCoder decodeObjectForKey:CPToolbarItemItemIdentifierKey]; 00515 00516 _minSize = [aCoder decodeSizeForKey:CPToolbarItemMinSizeKey]; 00517 _maxSize = [aCoder decodeSizeForKey:CPToolbarItemMaxSizeKey]; 00518 00519 [self setLabel:[aCoder decodeObjectForKey:CPToolbarItemLabelKey]]; 00520 [self setPaletteLabel:[aCoder decodeObjectForKey:CPToolbarItemPaletteLabelKey]]; 00521 [self setToolTip:[aCoder decodeObjectForKey:CPToolbarItemToolTipKey]]; 00522 00523 [self setTag:[aCoder decodeObjectForKey:CPToolbarItemTagKey]]; 00524 [self setTarget:[aCoder decodeObjectForKey:CPToolbarItemTargetKey]]; 00525 [self setAction:CPSelectorFromString([aCoder decodeObjectForKey:CPToolbarItemActionKey])]; 00526 00527 [self setEnabled:[aCoder decodeBoolForKey:CPToolbarItemEnabledKey]]; 00528 00529 [self setImage:[aCoder decodeObjectForKey:CPToolbarItemImageKey]]; 00530 [self setAlternateImage:[aCoder decodeObjectForKey:CPToolbarItemAlternateImageKey]]; 00531 00532 [self setView:[aCoder decodeObjectForKey:CPToolbarItemViewKey]]; 00533 00534 [self setVisibilityPriority:[aCoder decodeIntForKey:CPToolbarItemVisibilityPriorityKey]]; 00535 [self setAutovalidates:[aCoder decodeBoolForKey:CPToolbarItemAutovalidatesKey]]; 00536 } 00537 00538 return self; 00539 } 00540 00541 - (void)encodeWithCoder:(CPCoder)aCoder 00542 { 00543 [aCoder encodeObject:_itemIdentifier forKey:CPToolbarItemItemIdentifierKey]; 00544 00545 [aCoder encodeObject:[self label] forKey:CPToolbarItemLabelKey]; 00546 [aCoder encodeObject:[self paletteLabel] forKey:CPToolbarItemPaletteLabelKey]; 00547 00548 [aCoder encodeObject:[self toolTip] forKey:CPToolbarItemToolTipKey]; 00549 00550 [aCoder encodeObject:[self tag] forKey:CPToolbarItemTagKey]; 00551 [aCoder encodeObject:[self target] forKey:CPToolbarItemTargetKey]; 00552 [aCoder encodeObject:[self action] forKey:CPToolbarItemActionKey]; 00553 00554 [aCoder encodeObject:[self isEnabled] forKey:CPToolbarItemEnabledKey]; 00555 00556 [aCoder encodeObject:[self image] forKey:CPToolbarItemImageKey]; 00557 [aCoder encodeObject:[self alternateImage] forKey:CPToolbarItemAlternateImageKey]; 00558 00559 [aCoder encodeObject:[self view] forKey:CPToolbarItemViewKey]; 00560 00561 [aCoder encodeSize:[self minSize] forKey:CPToolbarItemMinSizeKey]; 00562 [aCoder encodeSize:[self maxSize] forKey:CPToolbarItemMaxSizeKey]; 00563 00564 [aCoder encodeObject:[self visibilityPriority] forKey:CPToolbarItemVisibilityPriorityKey]; 00565 [aCoder encodeBool:[self autovalidates] forKey:CPToolbarItemAutovalidatesKey]; 00566 } 00567 00568 @end 00569 00570 @implementation CPToolbarItem (CPCopying) 00571 00572 - (id)copy 00573 { 00574 var copy = [[[self class] alloc] initWithItemIdentifier:_itemIdentifier]; 00575 00576 if (_view) 00577 [copy setView:[CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:_view]]]; 00578 00579 [copy _setToolbar:_toolbar]; 00580 00581 [copy setLabel:_label]; 00582 [copy setPaletteLabel:_paletteLabel]; 00583 [copy setToolTip:[self toolTip]]; 00584 00585 [copy setTag:[self tag]]; 00586 [copy setTarget:[self target]]; 00587 [copy setAction:[self action]]; 00588 00589 [copy setEnabled:[self isEnabled]]; 00590 00591 [copy setImage:[self image]]; 00592 [copy setAlternateImage:[self alternateImage]]; 00593 00594 [copy setMinSize:_minSize]; 00595 [copy setMaxSize:_maxSize]; 00596 00597 [copy setVisibilityPriority:[self visibilityPriority]]; 00598 [copy setAutovalidates:[self autovalidates]]; 00599 00600 return copy; 00601 } 00602 00603 @end 00604 00605 // Standard toolbar identifiers 00606 00607 @implementation CPToolbarItem (Standard) 00608 00609 /* @ignore */ 00610 + (CPToolbarItem)_standardItemWithItemIdentifier:(CPString)anItemIdentifier 00611 { 00612 switch (anItemIdentifier) 00613 { 00614 case CPToolbarSeparatorItemIdentifier: return [_CPToolbarSeparatorItem new]; 00615 case CPToolbarSpaceItemIdentifier: return [_CPToolbarSpaceItem new]; 00616 case CPToolbarFlexibleSpaceItemIdentifier: return [_CPToolbarFlexibleSpaceItem new]; 00617 case CPToolbarShowColorsItemIdentifier: return [_CPToolbarShowColorsItem new]; 00618 case CPToolbarShowFontsItemIdentifier: return nil; 00619 case CPToolbarCustomizeToolbarItemIdentifier: return nil; 00620 case CPToolbarPrintItemIdentifier: return nil; 00621 } 00622 00623 return nil; 00624 } 00625 00626 @end 00627 00628