API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
CPToolbarItem.j
Go to the documentation of this file.
1 /*
2  * CPToolbarItem.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 
30 
31 CPToolbarSeparatorItemIdentifier = @"CPToolbarSeparatorItem";
32 CPToolbarSpaceItemIdentifier = @"CPToolbarSpaceItem";
33 CPToolbarFlexibleSpaceItemIdentifier = @"CPToolbarFlexibleSpaceItem";
34 CPToolbarShowColorsItemIdentifier = @"CPToolbarShowColorsItem";
35 CPToolbarShowFontsItemIdentifier = @"CPToolbarShowFontsItem";
36 CPToolbarCustomizeToolbarItemIdentifier = @"CPToolbarCustomizeToolbarItem";
37 CPToolbarPrintItemIdentifier = @"CPToolbarPrintItem";
38 
45 @implementation CPToolbarItem : CPObject
46 {
47  CPString _itemIdentifier;
48 
49  CPToolbar _toolbar;
50 
51  CPString _label;
52  CPString _paletteLabel;
53  CPString _toolTip;
54  int _tag;
55  id _target;
56  SEL _action;
57  BOOL _isEnabled;
58  CPImage _image;
59  CPImage _alternateImage;
60 
61  CPView _view;
62 
63  CGSize _minSize;
64  CGSize _maxSize;
65 
66  int _visibilityPriority;
67 
68  BOOL _autovalidates;
69 }
70 
71 - (id)init
72 {
73  return [self initWithItemIdentifier:@""];
74 }
75 
76 // Creating a Toolbar Item
82 - (id)initWithItemIdentifier:(CPString)anItemIdentifier
83 {
84  self = [super init];
85 
86  if (self)
87  {
88  _itemIdentifier = anItemIdentifier;
89 
90  _tag = 0;
91  _isEnabled = YES;
92 
93  _minSize = CGSizeMakeZero();
94  _maxSize = CGSizeMakeZero();
95 
96  _visibilityPriority = CPToolbarItemVisibilityPriorityStandard;
97  _autovalidates = YES;
98  }
99 
100  return self;
101 }
102 
103 // Managing Attributes
107 - (CPString)itemIdentifier
108 {
109  return _itemIdentifier;
110 }
111 
115 - (CPToolbar)toolbar
116 {
117  return _toolbar;
118 }
119 
120 /* @ignore */
121 - (void)_setToolbar:(CPToolbar)aToolbar
122 {
123  _toolbar = aToolbar;
124 }
125 
130 {
131  return _label;
132 }
133 
138 - (void)setLabel:(CPString)aLabel
139 {
140  _label = aLabel;
141 }
142 
146 - (CPString)paletteLabel
147 {
148  return _paletteLabel;
149 }
150 
155 - (void)setPaletteLabel:(CPString)aPaletteLabel
156 {
157  _paletteLabel = aPaletteLabel;
158 }
159 
165 - (CPString)toolTip
166 {
167  if ([_view respondsToSelector:@selector(toolTip)])
168  return [_view toolTip];
169 
170  return _toolTip;
171 }
172 
177 - (void)setToolTip:(CPString)aToolTip
178 {
179  if ([_view respondsToSelector:@selector(setToolTip:)])
180  [_view setToolTip:aToolTip];
181 
182  _toolTip = aToolTip;
183 }
184 
188 - (int)tag
189 {
190  if ([_view respondsToSelector:@selector(tag)])
191  return [_view tag];
192 
193  return _tag;
194 }
195 
200 - (void)setTag:(int)aTag
201 {
202  if ([_view respondsToSelector:@selector(setTag:)])
203  [_view setTag:aTag];
204 
205  _tag = aTag;
206 }
207 
211 - (id)target
212 {
213  if (_view)
214  return [_view respondsToSelector:@selector(target)] ? [_view target] : nil;
215 
216  return _target;
217 }
218 
224 - (void)setTarget:(id)aTarget
225 {
226  if (!_view)
227  _target = aTarget;
228 
229  else if ([_view respondsToSelector:@selector(setTarget:)])
230  [_view setTarget:aTarget];
231 }
232 
236 - (SEL)action
237 {
238  if (_view)
239  return [_view respondsToSelector:@selector(action)] ? [_view action] : nil;
240 
241  return _action;
242 }
243 
248 - (void)setAction:(SEL)anAction
249 {
250  if (!_view)
251  _action = anAction;
252 
253  else if ([_view respondsToSelector:@selector(setAction:)])
254  [_view setAction:anAction];
255 }
256 
260 - (BOOL)isEnabled
261 {
262  if ([_view respondsToSelector:@selector(isEnabled)])
263  return [_view isEnabled];
264 
265  return _isEnabled;
266 }
267 
272 - (void)setEnabled:(BOOL)shouldBeEnabled
273 {
274  if (_isEnabled === shouldBeEnabled)
275  return;
276 
277  if ([_view respondsToSelector:@selector(setEnabled:)])
278  [_view setEnabled:shouldBeEnabled];
279 
280  _isEnabled = shouldBeEnabled;
281 }
282 
286 - (CPImage)image
287 {
288  if ([_view respondsToSelector:@selector(image)])
289  return [_view image];
290 
291  return _image;
292 }
293 
298 - (void)setImage:(CPImage)anImage
299 {
300  if ([_view respondsToSelector:@selector(setImage:)])
301  [_view setImage:anImage];
302 
303  _image = anImage;
304 
305  if (!_image)
306  return;
307 
308  if (_minSize.width === 0 && _minSize.height === 0 &&
309  _maxSize.width === 0 && _maxSize.height === 0)
310  {
311  var imageSize = [_image size];
312 
313  if (imageSize.width > 0 || imageSize.height > 0)
314  {
315  [self setMinSize:imageSize];
316  [self setMaxSize:imageSize];
317  }
318  }
319 }
320 
325 - (void)setAlternateImage:(CPImage)anImage
326 {
327  if ([_view respondsToSelector:@selector(setAlternateImage:)])
328  [_view setAlternateImage:anImage];
329 
330  _alternateImage = anImage;
331 }
332 
336 - (CPImage)alternateImage
337 {
338  if ([_view respondsToSelector:@selector(alternateIamge)])
339  return [_view alternateImage];
340 
341  return _alternateImage;
342 }
343 
347 - (CPView)view
348 {
349  return _view;
350 }
351 
356 - (void)setView:(CPView)aView
357 {
358  if (_view == aView)
359  return;
360 
361  _view = aView;
362 
363  if (_view)
364  {
365  // Tags get forwarded.
366  if (_tag !== 0 && [_view respondsToSelector:@selector(setTag:)])
367  [_view setTag:_tag];
368 
369  _target = nil;
370  _action = nil;
371  }
372 }
373 
377 - (CGSize)minSize
378 {
379  return _minSize;
380 }
381 
386 - (void)setMinSize:(CGSize)aMinSize
387 {
388  if (!aMinSize.height || !aMinSize.width)
389  return;
390 
391  _minSize = CGSizeMakeCopy(aMinSize);
392 
393  // Try to provide some sanity: Make maxSize >= minSize
394  _maxSize = CGSizeMake(MAX(_minSize.width, _maxSize.width), MAX(_minSize.height, _maxSize.height));
395 }
396 
400 - (CGSize)maxSize
401 {
402  return _maxSize;
403 }
404 
409 - (void)setMaxSize:(CGSize)aMaxSize
410 {
411  if (!aMaxSize.height || !aMaxSize.width)
412  return;
413 
414  _maxSize = CGSizeMakeCopy(aMaxSize);
415 
416  // Try to provide some sanity: Make minSize <= maxSize
417  _minSize = CGSizeMake(MIN(_minSize.width, _maxSize.width), MIN(_minSize.height, _maxSize.height));
418 }
419 
420 // Visibility Priority
430 - (int)visibilityPriority
431 {
432  return _visibilityPriority;
433 }
434 
445 - (void)setVisibilityPriority:(int)aVisibilityPriority
446 {
447  _visibilityPriority = aVisibilityPriority;
448 }
449 
450 - (void)validate
451 {
452  var action = [self action],
453  target = [self target];
454 
455  // View items do not do any target-action analysis.
456  if (_view)
457  {
458  if ([target respondsToSelector:@selector(validateToolbarItem:)])
459  {
460  var shouldBeEnabled = [target validateToolbarItem:self];
461  if (_isEnabled !== shouldBeEnabled)
462  [self setEnabled:shouldBeEnabled];
463  }
464 
465  return;
466  }
467 
468  if (!action)
469  {
470  if (_isEnabled)
471  return [self setEnabled:NO];
472  return;
473  }
474 
475  if (target && ![target respondsToSelector:action])
476  {
477  if (_isEnabled)
478  return [self setEnabled:NO];
479  return;
480  }
481 
482  target = [CPApp targetForAction:action to:target from:self];
483 
484  if (!target)
485  {
486  if (_isEnabled)
487  return [self setEnabled:NO];
488  return;
489  }
490 
491  if ([target respondsToSelector:@selector(validateToolbarItem:)])
492  {
493  var shouldBeEnabled = [target validateToolbarItem:self];
494  if (_isEnabled !== shouldBeEnabled)
495  [self setEnabled:shouldBeEnabled];
496  }
497  else
498  {
499  if (!_isEnabled)
500  [self setEnabled:YES];
501  }
502 }
503 
504 - (BOOL)autovalidates
505 {
506  return _autovalidates;
507 }
508 
509 - (void)setAutovalidates:(BOOL)shouldAutovalidate
510 {
511  _autovalidates = !!shouldAutovalidate;
512 }
513 
514 @end
515 
516 var CPToolbarItemItemIdentifierKey = @"CPToolbarItemItemIdentifierKey",
517  CPToolbarItemLabelKey = @"CPToolbarItemLabelKey",
518  CPToolbarItemPaletteLabelKey = @"CPToolbarItemPaletteLabelKey",
519  CPToolbarItemToolTipKey = @"CPToolbarItemToolTipKey",
520  CPToolbarItemTagKey = @"CPToolbarItemTagKey",
521  CPToolbarItemTargetKey = @"CPToolbarItemTargetKey",
522  CPToolbarItemActionKey = @"CPToolbarItemActionKey",
523  CPToolbarItemEnabledKey = @"CPToolbarItemEnabledKey",
524  CPToolbarItemImageKey = @"CPToolbarItemImageKey",
525  CPToolbarItemAlternateImageKey = @"CPToolbarItemAlternateImageKey",
526  CPToolbarItemViewKey = @"CPToolbarItemViewKey",
527  CPToolbarItemMinSizeKey = @"CPToolbarItemMinSizeKey",
528  CPToolbarItemMaxSizeKey = @"CPToolbarItemMaxSizeKey",
529  CPToolbarItemVisibilityPriorityKey = @"CPToolbarItemVisibilityPriorityKey",
530  CPToolbarItemAutovalidatesKey = @"CPToolbarItemAutovalidatesKey";
531 
533 
534 - (id)initWithCoder:(CPCoder)aCoder
535 {
536  self = [super init];
537 
538  if (self)
539  {
540  _itemIdentifier = [aCoder decodeObjectForKey:CPToolbarItemItemIdentifierKey];
541 
542  _minSize = [aCoder decodeSizeForKey:CPToolbarItemMinSizeKey];
543  _maxSize = [aCoder decodeSizeForKey:CPToolbarItemMaxSizeKey];
544 
545  [self setLabel:[aCoder decodeObjectForKey:CPToolbarItemLabelKey]];
546  [self setPaletteLabel:[aCoder decodeObjectForKey:CPToolbarItemPaletteLabelKey]];
547  [self setToolTip:[aCoder decodeObjectForKey:CPToolbarItemToolTipKey]];
548 
549  [self setTag:[aCoder decodeObjectForKey:CPToolbarItemTagKey]];
550  [self setTarget:[aCoder decodeObjectForKey:CPToolbarItemTargetKey]];
551  [self setAction:CPSelectorFromString([aCoder decodeObjectForKey:CPToolbarItemActionKey])];
552 
553  [self setEnabled:[aCoder decodeBoolForKey:CPToolbarItemEnabledKey]];
554 
555  [self setImage:[aCoder decodeObjectForKey:CPToolbarItemImageKey]];
556  [self setAlternateImage:[aCoder decodeObjectForKey:CPToolbarItemAlternateImageKey]];
557 
558  [self setView:[aCoder decodeObjectForKey:CPToolbarItemViewKey]];
559 
560  [self setVisibilityPriority:[aCoder decodeIntForKey:CPToolbarItemVisibilityPriorityKey]];
561  [self setAutovalidates:[aCoder decodeBoolForKey:CPToolbarItemAutovalidatesKey]];
562  }
563 
564  return self;
565 }
566 
567 - (void)encodeWithCoder:(CPCoder)aCoder
568 {
569  [aCoder encodeObject:_itemIdentifier forKey:CPToolbarItemItemIdentifierKey];
570 
571  [aCoder encodeObject:[self label] forKey:CPToolbarItemLabelKey];
572  [aCoder encodeObject:[self paletteLabel] forKey:CPToolbarItemPaletteLabelKey];
573 
574  [aCoder encodeObject:[self toolTip] forKey:CPToolbarItemToolTipKey];
575 
576  [aCoder encodeObject:[self tag] forKey:CPToolbarItemTagKey];
577  [aCoder encodeObject:[self target] forKey:CPToolbarItemTargetKey];
578  [aCoder encodeObject:[self action] forKey:CPToolbarItemActionKey];
579 
580  [aCoder encodeObject:[self isEnabled] forKey:CPToolbarItemEnabledKey];
581 
582  [aCoder encodeObject:[self image] forKey:CPToolbarItemImageKey];
583  [aCoder encodeObject:[self alternateImage] forKey:CPToolbarItemAlternateImageKey];
584 
585  [aCoder encodeObject:[self view] forKey:CPToolbarItemViewKey];
586 
587  [aCoder encodeSize:[self minSize] forKey:CPToolbarItemMinSizeKey];
588  [aCoder encodeSize:[self maxSize] forKey:CPToolbarItemMaxSizeKey];
589 
590  [aCoder encodeObject:[self visibilityPriority] forKey:CPToolbarItemVisibilityPriorityKey];
591  [aCoder encodeBool:[self autovalidates] forKey:CPToolbarItemAutovalidatesKey];
592 }
593 
594 @end
595 
597 
598 - (id)copy
599 {
600  var copy = [[[self class] alloc] initWithItemIdentifier:_itemIdentifier];
601 
602  if (_view)
604 
605  [copy _setToolbar:_toolbar];
606 
607  [copy setLabel:_label];
608  [copy setPaletteLabel:_paletteLabel];
609  [copy setToolTip:[self toolTip]];
610 
611  [copy setTag:[self tag]];
612  [copy setTarget:[self target]];
613  [copy setAction:[self action]];
614 
615  [copy setEnabled:[self isEnabled]];
616 
617  [copy setImage:[self image]];
618  [copy setAlternateImage:[self alternateImage]];
619 
620  [copy setMinSize:_minSize];
621  [copy setMaxSize:_maxSize];
622 
623  [copy setVisibilityPriority:[self visibilityPriority]];
624  [copy setAutovalidates:[self autovalidates]];
625 
626  return copy;
627 }
628 
629 @end
630 
631 // Standard toolbar identifiers
632 
634 
635 /* @ignore */
636 + (CPToolbarItem)_standardItemWithItemIdentifier:(CPString)anItemIdentifier
637 {
638  switch (anItemIdentifier)
639  {
640  case CPToolbarSeparatorItemIdentifier: return [_CPToolbarSeparatorItem new];
641  case CPToolbarSpaceItemIdentifier: return [_CPToolbarSpaceItem new];
642  case CPToolbarFlexibleSpaceItemIdentifier: return [_CPToolbarFlexibleSpaceItem new];
643  case CPToolbarShowColorsItemIdentifier: return [_CPToolbarShowColorsItem new];
644  case CPToolbarShowFontsItemIdentifier: return nil;
646  case CPToolbarPrintItemIdentifier: return nil;
647  }
648 
649  return nil;
650 }
651 
652 @end
653 
654