-
Notifications
You must be signed in to change notification settings - Fork 47
/
PSMTabBarControl.m
2484 lines (2037 loc) · 81.2 KB
/
PSMTabBarControl.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PSMTabBarControl.m
// PSMTabBarControl
//
// Created by John Pannell on 10/13/05.
// Copyright 2005 Positive Spin Media. All rights reserved.
//
#import "PSMTabBarControl.h"
#import "PSMTabBarCell.h"
#import "PSMOverflowPopUpButton.h"
#import "PSMRolloverButton.h"
#import "PSMTabStyle.h"
#import "PSMMetalTabStyle.h"
#import "PSMAquaTabStyle.h"
#import "PSMUnifiedTabStyle.h"
#import "PSMAdiumTabStyle.h"
#import "PSMLiveChatTabStyle.h"
#import "PSMCardTabStyle.h"
#import "PSMTabDragAssistant.h"
#import "PSMTabBarController.h"
@interface PSMTabBarControl (/*Private*/)
- (NSTabViewItem *)_tabViewItemForEvent:(NSEvent *)event;
- (void)_delegateMouseEvent:(NSEvent *)event;
- (CGFloat)_heightOfTabCells;
- (CGFloat)_rightMargin;
- (CGFloat)_leftMargin;
- (CGFloat)_topMargin;
- (CGFloat)_bottomMargin;
- (NSSize)_addTabButtonSize;
- (NSRect)_addTabButtonRect;
- (NSSize)_overflowButtonSize;
- (NSRect)_overflowButtonRect;
- (void)_drawTabBarControlInRect:(NSRect)aRect;
- (void)_drawBezelInRect:(NSRect)rect;
- (void)_drawInteriorInRect:(NSRect)rect;
@end
@interface PSMTabBarControl (Private)
// constructor/destructor
- (void)initAddedProperties;
// accessors
- (NSEvent *)lastMouseDownEvent;
- (void)setLastMouseDownEvent:(NSEvent *)event;
// contents
- (void)addTabViewItem:(NSTabViewItem *)item;
- (void)addTabViewItem:(NSTabViewItem *)item atIndex:(NSUInteger)index;
- (void)removeTabForCell:(PSMTabBarCell *)cell;
// draw
- (void)update;
- (void)update:(BOOL)animate;
- (void)_positionOverflowMenu;
- (void)_checkWindowFrame;
// actions
- (void)overflowMenuAction:(id)sender;
- (void)closeTabClick:(id)sender;
- (void)tabClick:(id)sender;
- (void)tabNothing:(id)sender;
// notification handlers
- (void)frameDidChange:(NSNotification *)notification;
- (void)windowDidMove:(NSNotification *)aNotification;
- (void)windowDidUpdate:(NSNotification *)notification;
// NSTabView delegate
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)tabView;
// archiving
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
// convenience
- (void)_bindPropertiesForCell:(PSMTabBarCell *)cell andTabViewItem:(NSTabViewItem *)item;
- (id)cellForPoint:(NSPoint)point cellFrame:(NSRectPointer)outFrame;
- (void)_animateCells:(NSTimer *)timer;
@end
@implementation PSMTabBarControl
static NSMutableDictionary *registeredStyleClasses;
+(void)initialize {
if (registeredStyleClasses == nil) {
registeredStyleClasses = [[NSMutableDictionary dictionaryWithCapacity:10] retain];
[self registerDefaultTabStyleClasses];
}
}
#pragma mark -
#pragma mark Characteristics
+ (NSBundle *)bundle;
{
static NSBundle *bundle = nil;
if(!bundle) {
bundle = [NSBundle bundleForClass:[PSMTabBarControl class]];
}
return bundle;
}
/*!
@method availableCellWidth
@abstract The number of pixels available for cells
@discussion Calculates the number of pixels available for cells based on margins and the window resize badge.
@returns Returns the amount of space for cells.
*/
- (CGFloat)availableCellWidth {
CGFloat result = [self frame].size.width - [self leftMargin] - [self rightMargin];
result -= _resizeAreaCompensation;
//Don't let cells overlap the add tab button if it is visible
if ([self showAddTabButton]) {
result -= [self addTabButtonRect].size.width + 2*kPSMTabBarCellPadding;
}
return result;
}
/*!
@method availableCellHeight
@abstract The number of pixels available for cells
@discussion Calculates the number of pixels available for cells based on margins and the window resize badge.
@returns Returns the amount of space for cells.
*/
- (CGFloat)availableCellHeight {
CGFloat result = [self bounds].size.height - [self topMargin] - [self bottomMargin];
result -= _resizeAreaCompensation;
//Don't let cells overlap the add tab button if it is visible
if ([self showAddTabButton]) {
result -= [self addTabButtonRect].size.height;
}
//let room for overflow popup button
if ([self useOverflowMenu] && ![[self overflowPopUpButton] isHidden]) {
result -= [self overflowButtonRect].size.height;
}
return result;
}
/*!
@method genericCellRect
@abstract The basic rect for a tab cell.
@discussion Creates a generic frame for a tab cell based on the current control state.
@returns Returns a basic rect for a tab cell.
*/
- (NSRect)genericCellRect {
NSRect aRect = [self frame];
aRect.origin.x = [self leftMargin];
aRect.origin.y = 0.0;
aRect.size.width = [self availableCellWidth];
aRect.size.height = [self heightOfTabCells];
return aRect;
}
- (BOOL)isWindowActive {
NSWindow *window = [self window];
BOOL windowActive = NO;
if ([window isKeyWindow])
windowActive = YES;
else if ([window isKindOfClass:[NSPanel class]] && [NSApp isActive])
windowActive = YES;
return windowActive;
}
#pragma mark -
#pragma mark Constructor/destructor
- (void)initAddedProperties {
_cells = [[NSMutableArray alloc] initWithCapacity:10];
_controller = [[PSMTabBarController alloc] initWithTabBarControl:self];
_animationTimer = nil;
// default config
_currentStep = kPSMIsNotBeingResized;
_orientation = PSMTabBarHorizontalOrientation;
_canCloseOnlyTab = NO;
_disableTabClose = NO;
_showAddTabButton = NO;
_hideForSingleTab = NO;
_sizeCellsToFit = NO;
_isHidden = NO;
_awakenedFromNib = NO;
_automaticallyAnimates = NO;
_useOverflowMenu = YES;
_allowsBackgroundTabClosing = YES;
_allowsResizing = NO;
_selectsTabsOnMouseDown = NO;
_alwaysShowActiveTab = NO;
_allowsScrubbing = NO;
_cellMinWidth = 100;
_cellMaxWidth = 280;
_cellOptimumWidth = 130;
_tearOffStyle = PSMTabBarTearOffAlphaWindow;
style = [[PSMMetalTabStyle alloc] init];
// the overflow button/menu
NSRect overflowButtonRect = [self overflowButtonRect];
_overflowPopUpButton = [[PSMOverflowPopUpButton alloc] initWithFrame:overflowButtonRect pullsDown:YES];
[_overflowPopUpButton setAutoresizingMask:NSViewNotSizable | NSViewMinXMargin];
[_overflowPopUpButton setHidden:YES];
[[_overflowPopUpButton cell] accessibilitySetOverrideValue:NSLocalizedString(@"More tabs", nil) forAttribute:NSAccessibilityDescriptionAttribute];
[self addSubview:_overflowPopUpButton];
[self _positionOverflowMenu];
// new tab button
NSRect addTabButtonRect = [self addTabButtonRect];
_addTabButton = [[PSMRolloverButton alloc] initWithFrame:addTabButtonRect];
if(_addTabButton) {
NSImage *newButtonImage = [style addTabButtonImage];
if(newButtonImage) {
[_addTabButton setUsualImage:newButtonImage];
}
newButtonImage = [style addTabButtonPressedImage];
if(newButtonImage) {
[_addTabButton setAlternateImage:newButtonImage];
}
newButtonImage = [style addTabButtonRolloverImage];
if(newButtonImage) {
[_addTabButton setRolloverImage:newButtonImage];
}
[_addTabButton setTitle:@""];
[_addTabButton setImagePosition:NSImageOnly];
[_addTabButton setButtonType:NSMomentaryChangeButton];
[_addTabButton setBordered:NO];
[_addTabButton setBezelStyle:NSShadowlessSquareBezelStyle];
[[_addTabButton cell] accessibilitySetOverrideValue:NSLocalizedString(@"Add Tab", nil) forAttribute:NSAccessibilityDescriptionAttribute];
[self addSubview:_addTabButton];
if(_showAddTabButton) {
[_addTabButton setHidden:NO];
} else {
[_addTabButton setHidden:YES];
}
[_addTabButton setNeedsDisplay:YES];
}
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if(self) {
// Initialization
[self initAddedProperties];
[self registerForDraggedTypes:[NSArray arrayWithObjects:@"PSMTabBarControlItemPBType", nil]];
// resize
[self setPostsFrameChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(frameDidChange:) name:NSViewFrameDidChangeNotification object:self];
// add observing of cells
[self addObserver:self forKeyPath:@"cells" options:NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld | NSKeyValueObservingOptionInitial context:NULL];
}
// [self setTarget:self];
return self;
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"cells"];
[[NSNotificationCenter defaultCenter] removeObserver:self];
//stop any animations that may be running
[_animationTimer invalidate];
[_animationTimer release]; _animationTimer = nil;
[_showHideAnimationTimer invalidate];
[_showHideAnimationTimer release]; _showHideAnimationTimer = nil;
//Also unwind the spring, if it's wound.
[_springTimer invalidate];
[_springTimer release]; _springTimer = nil;
//unbind all the items to prevent crashing
//not sure if this is necessary or not
// http://code.google.com/p/maccode/issues/detail?id=35
NSArray *tmpCellArray = [_cells copy];
for (PSMTabBarCell *aCell in tmpCellArray) {
[self removeTabForCell:aCell];
}
[tmpCellArray release];
[_overflowPopUpButton release];
[_cells release];
[_controller release];
[tabView release];
[_addTabButton release];
[partnerView release];
[_lastMouseDownEvent release];
[style release];
[self unregisterDraggedTypes];
[super dealloc];
}
- (void)awakeFromNib {
// build cells from existing tab view items
for (NSTabViewItem *item in [tabView tabViewItems]) {
if(![[self representedTabViewItems] containsObject:item]) {
[self addTabViewItem:item];
}
}
}
- (void)viewWillMoveToWindow:(NSWindow *)aWindow {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:NSWindowDidBecomeKeyNotification object:nil];
[center removeObserver:self name:NSWindowDidResignKeyNotification object:nil];
[center removeObserver:self name:NSWindowDidUpdateNotification object:nil];
[center removeObserver:self name:NSWindowDidMoveNotification object:nil];
if(_showHideAnimationTimer) {
[_showHideAnimationTimer invalidate];
[_showHideAnimationTimer release]; _showHideAnimationTimer = nil;
}
if(aWindow) {
[center addObserver:self selector:@selector(windowStatusDidChange:) name:NSWindowDidBecomeKeyNotification object:aWindow];
[center addObserver:self selector:@selector(windowStatusDidChange:) name:NSWindowDidResignKeyNotification object:aWindow];
[center addObserver:self selector:@selector(windowDidUpdate:) name:NSWindowDidUpdateNotification object:aWindow];
[center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:aWindow];
}
}
- (void)windowStatusDidChange:(NSNotification *)notification {
[self setNeedsDisplay:YES];
}
#pragma mark -
#pragma mark Style Class Registry
+ (void)registerDefaultTabStyleClasses {
[self registerTabStyleClass:[PSMAquaTabStyle class]];
[self registerTabStyleClass:[PSMUnifiedTabStyle class]];
[self registerTabStyleClass:[PSMAdiumTabStyle class]];
[self registerTabStyleClass:[PSMMetalTabStyle class]];
[self registerTabStyleClass:[PSMCardTabStyle class]];
[self registerTabStyleClass:[PSMLiveChatTabStyle class]];
}
+ (void)registerTabStyleClass:(Class <PSMTabStyle>)aStyleClass {
[registeredStyleClasses setObject:aStyleClass forKey:[aStyleClass name]];
}
+ (void)unregisterTabStyleClass:(Class <PSMTabStyle>)aStyleClass {
[registeredStyleClasses removeObjectForKey:[aStyleClass name]];
}
+ (NSArray *)registeredTabStyleClasses {
return [registeredStyleClasses allValues];
}
+ (Class <PSMTabStyle>)registeredClassForStyleName:(NSString *)name {
return [registeredStyleClasses objectForKey:name];
}
#pragma mark -
#pragma mark Cell Management (KVC Compliant)
- (NSArray *)cells {
return [[_cells copy] autorelease];
}
// ---- KVC primitives ----
- (void)insertObject:(PSMTabBarCell *)aCell inCellsAtIndex:(NSUInteger)cellIndex {
[_cells insertObject:aCell atIndex:cellIndex];
}
- (void)insertCells:(NSArray *)aCellArray atIndexes:(NSIndexSet *)indexes {
[_cells insertObjects:aCellArray atIndexes:indexes];
}
-(void)removeObjectFromCellsAtIndex:(NSUInteger)anIndex {
[_cells removeObjectAtIndex:anIndex];
}
-(void)removeCellsAtIndexes:(NSIndexSet *)indexes {
[_cells removeObjectsAtIndexes:indexes];
}
-(void)replaceObjectInCellsAtIndex:(NSUInteger)anIndex withObject:(PSMTabBarCell *)aCell {
[_cells replaceObjectAtIndex:anIndex withObject:aCell];
}
-(void)replaceCellsAtIndexes:(NSIndexSet *)indexes withCells:(NSArray *)cellArray {
[_cells replaceObjectsAtIndexes:indexes withObjects:cellArray];
}
// ---- Highlevel methods using KVC compliant primitives ----
- (void)addCell:(PSMTabBarCell *)aCell {
[self insertObject:aCell inCellsAtIndex:[[self cells] count]];
}
- (void)insertCell:(PSMTabBarCell *)aCell atIndex:(NSUInteger)index {
[self insertObject:aCell inCellsAtIndex:index];
}
- (void)removeCellAtIndex:(NSUInteger)index {
[self removeObjectFromCellsAtIndex:index];
}
- (void)replaceCellAtIndex:(NSUInteger)index withCell:(PSMTabBarCell *)aCell {
[self replaceObjectInCellsAtIndex:index withObject:aCell];
}
#pragma mark -
#pragma mark Displaying a Cell
-(void)updateCell:(NSCell *)aCell {
if ([aCell isKindOfClass:[PSMTabBarCell class]])
{
[self setNeedsDisplayInRect:[(PSMTabBarCell *)aCell frame]];
}
else
[super updateCell:aCell];
}
#pragma mark -
#pragma mark Accessors
- (NSEvent *)lastMouseDownEvent {
return _lastMouseDownEvent;
}
- (void)setLastMouseDownEvent:(NSEvent *)event {
[event retain];
[_lastMouseDownEvent release];
_lastMouseDownEvent = event;
}
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)object {
delegate = object;
NSMutableArray *types = [NSMutableArray arrayWithObject:@"PSMTabBarControlItemPBType"];
//Update the allowed drag types
if([self delegate] && [[self delegate] respondsToSelector:@selector(allowedDraggedTypesForTabView:)]) {
[types addObjectsFromArray:[[self delegate] allowedDraggedTypesForTabView:tabView]];
}
[self unregisterDraggedTypes];
[self registerForDraggedTypes:types];
}
- (NSTabView *)tabView {
return tabView;
}
- (void)setTabView:(NSTabView *)view {
[view retain];
[tabView release];
tabView = view;
}
- (id<PSMTabStyle>)style {
return style;
}
- (NSString *)styleName {
return [style name];
}
- (void)setStyle:(id <PSMTabStyle>)newStyle {
if(style != newStyle) {
[style autorelease];
style = [newStyle retain];
// restyle add tab button
if(_addTabButton) {
NSImage *newButtonImage = [style addTabButtonImage];
if(newButtonImage) {
[_addTabButton setUsualImage:newButtonImage];
}
newButtonImage = [style addTabButtonPressedImage];
if(newButtonImage) {
[_addTabButton setAlternateImage:newButtonImage];
}
newButtonImage = [style addTabButtonRolloverImage];
if(newButtonImage) {
[_addTabButton setRolloverImage:newButtonImage];
}
}
[self update];
}
}
- (void)setStyleNamed:(NSString *)name {
Class <PSMTabStyle> styleClass = [[self class] registeredClassForStyleName:name];
if (styleClass == NULL)
return;
id <PSMTabStyle> newStyle = [[(Class)styleClass alloc] init];
[self setStyle:newStyle];
[newStyle release];
}
- (PSMTabBarOrientation)orientation {
return _orientation;
}
- (void)setOrientation:(PSMTabBarOrientation)value {
PSMTabBarOrientation lastOrientation = _orientation;
_orientation = value;
if(_tabBarWidth < 10) {
_tabBarWidth = 120;
}
if(lastOrientation != _orientation) {
[self _positionOverflowMenu]; //move the overflow popup button to the right place
[self update:NO];
}
}
- (BOOL)canCloseOnlyTab {
return _canCloseOnlyTab;
}
- (void)setCanCloseOnlyTab:(BOOL)value {
_canCloseOnlyTab = value;
if([_cells count] == 1) {
[self update];
}
}
- (BOOL)disableTabClose {
return _disableTabClose;
}
- (void)setDisableTabClose:(BOOL)value {
_disableTabClose = value;
[self update];
}
- (BOOL)hideForSingleTab {
return _hideForSingleTab;
}
- (void)setHideForSingleTab:(BOOL)value {
_hideForSingleTab = value;
[self update];
}
- (BOOL)showAddTabButton {
return _showAddTabButton;
}
- (void)setShowAddTabButton:(BOOL)value {
_showAddTabButton = value;
if(!NSIsEmptyRect([self addTabButtonRect])) {
[_addTabButton setFrame:[self addTabButtonRect]];
}
[_addTabButton setHidden:!_showAddTabButton];
[_addTabButton setNeedsDisplay:YES];
[self update];
}
- (NSInteger)cellMinWidth {
return _cellMinWidth;
}
- (void)setCellMinWidth:(NSInteger)value {
_cellMinWidth = value;
[self update];
}
- (NSInteger)cellMaxWidth {
return _cellMaxWidth;
}
- (void)setCellMaxWidth:(NSInteger)value {
_cellMaxWidth = value;
[self update];
}
- (NSInteger)cellOptimumWidth {
return _cellOptimumWidth;
}
- (void)setCellOptimumWidth:(NSInteger)value {
_cellOptimumWidth = value;
[self update];
}
- (BOOL)sizeCellsToFit {
return _sizeCellsToFit;
}
- (void)setSizeCellsToFit:(BOOL)value {
_sizeCellsToFit = value;
[self update];
}
- (BOOL)useOverflowMenu {
return _useOverflowMenu;
}
- (void)setUseOverflowMenu:(BOOL)value {
_useOverflowMenu = value;
[self update];
}
- (PSMRolloverButton *)addTabButton {
return _addTabButton;
}
- (PSMOverflowPopUpButton *)overflowPopUpButton {
return _overflowPopUpButton;
}
- (BOOL)allowsBackgroundTabClosing {
return _allowsBackgroundTabClosing;
}
- (void)setAllowsBackgroundTabClosing:(BOOL)value {
_allowsBackgroundTabClosing = value;
}
- (BOOL)allowsResizing {
return _allowsResizing;
}
- (void)setAllowsResizing:(BOOL)value {
_allowsResizing = value;
}
- (BOOL)selectsTabsOnMouseDown {
return _selectsTabsOnMouseDown;
}
- (void)setSelectsTabsOnMouseDown:(BOOL)value {
_selectsTabsOnMouseDown = value;
}
- (BOOL)automaticallyAnimates {
return _automaticallyAnimates;
}
- (void)setAutomaticallyAnimates:(BOOL)value {
_automaticallyAnimates = value;
}
- (BOOL)alwaysShowActiveTab {
return _alwaysShowActiveTab;
}
- (void)setAlwaysShowActiveTab:(BOOL)value {
_alwaysShowActiveTab = value;
}
- (BOOL)allowsScrubbing {
return _allowsScrubbing;
}
- (void)setAllowsScrubbing:(BOOL)value {
_allowsScrubbing = value;
}
- (PSMTabBarTearOffStyle)tearOffStyle {
return _tearOffStyle;
}
- (void)setTearOffStyle:(PSMTabBarTearOffStyle)tearOffStyle {
_tearOffStyle = tearOffStyle;
}
-(CGFloat)heightOfTabCells
{
if ([style respondsToSelector:@selector(heightOfTabCellsForTabBarControl:)])
return [style heightOfTabCellsForTabBarControl:self];
return [self _heightOfTabCells];
}
#pragma mark -
#pragma mark Functionality
- (void)addTabViewItem:(NSTabViewItem *)item atIndex:(NSUInteger)index {
// create cell
PSMTabBarCell *cell = [[PSMTabBarCell alloc] init];
[cell setControlView:self];
NSRect cellRect = NSZeroRect, lastCellFrame = NSZeroRect;
if([_cells lastObject] != nil) {
lastCellFrame = [[_cells lastObject] frame];
}
if([self orientation] == PSMTabBarHorizontalOrientation) {
cellRect = [self genericCellRect];
cellRect.size.width = 30;
cellRect.origin.x = lastCellFrame.origin.x + lastCellFrame.size.width;
} else {
cellRect = /*lastCellFrame*/ [self genericCellRect];
cellRect.size.width = lastCellFrame.size.width;
cellRect.size.height = 0;
cellRect.origin.y = lastCellFrame.origin.y + lastCellFrame.size.height;
}
[cell setRepresentedObject:item];
[cell setFrame:cellRect];
// bind it up
[self bindPropertiesForCell:cell andTabViewItem:item];
// add to collection
[self insertCell:cell atIndex:index];
[cell release];
if([_cells count] == [tabView numberOfTabViewItems]) {
[self update]; // don't update unless all are accounted for!
}
}
- (void)addTabViewItem:(NSTabViewItem *)item {
[self addTabViewItem:item atIndex:[_cells count]];
}
- (void)removeTabForCell:(PSMTabBarCell *)cell {
NSTabViewItem *item = [cell representedObject];
// unbind
[[cell indicator] unbind:@"animate"];
[[cell indicator] unbind:@"hidden"];
[cell unbind:@"hasIcon"];
[cell unbind:@"hasLargeImage"];
[cell unbind:@"title"];
[cell unbind:@"count"];
[cell unbind:@"countColor"];
[cell unbind:@"isEdited"];
if([item identifier] != nil) {
if([[item identifier] respondsToSelector:@selector(isProcessing)]) {
[[item identifier] removeObserver:cell forKeyPath:@"isProcessing"];
}
}
if([item identifier] != nil) {
if([[item identifier] respondsToSelector:@selector(icon)]) {
[[item identifier] removeObserver:cell forKeyPath:@"icon"];
}
}
if([item identifier] != nil) {
if([[item identifier] respondsToSelector:@selector(objectCount)]) {
[[item identifier] removeObserver:cell forKeyPath:@"objectCount"];
}
}
if([item identifier] != nil) {
if([[item identifier] respondsToSelector:@selector(countColor)]) {
[[item identifier] removeObserver:cell forKeyPath:@"countColor"];
}
}
if([item identifier] != nil) {
if([[item identifier] respondsToSelector:@selector(largeImage)]) {
[[item identifier] removeObserver:cell forKeyPath:@"largeImage"];
}
}
if([item identifier] != nil) {
if([[item identifier] respondsToSelector:@selector(isEdited)]) {
[[item identifier] removeObserver:cell forKeyPath:@"isEdited"];
}
}
// stop watching identifier
@try {
[item removeObserver:self forKeyPath:@"identifier"];
}
@catch (NSException *exception) {
}
// remove indicator
if([[self subviews] containsObject:[cell indicator]]) {
[[cell indicator] removeFromSuperview];
}
// remove tracking
[[NSNotificationCenter defaultCenter] removeObserver:cell];
// pull from collection
NSUInteger cellIndex = [_cells indexOfObjectIdenticalTo:cell];
if (cellIndex != NSNotFound)
[self removeCellAtIndex:cellIndex];
[self update];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
// did cell array change?
if ([keyPath isEqualToString:@"cells"]) {
[self updateTrackingAreas];
// did the tab's identifier change?
} else if([keyPath isEqualToString:@"identifier"]) {
id oldIdentifier = [change objectForKey: NSKeyValueChangeOldKey];
for (PSMTabBarCell *cell in _cells) {
if([cell representedObject] == object) {
// unbind the old value first
NSArray *selectors = [NSArray arrayWithObjects: @"isProcessing", @"icon", @"objectCount", @"countColor", @"largeImage", @"isEdited", nil];
for (NSString *selector in selectors) {
if([oldIdentifier respondsToSelector: NSSelectorFromString(selector)]) {
[oldIdentifier unbind: selector];
[oldIdentifier removeObserver:cell forKeyPath:selector];
}
}
[self _bindPropertiesForCell:cell andTabViewItem:object];
}
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark -
#pragma mark Hide/Show
- (void)hideTabBar:(BOOL)hide animate:(BOOL)animate {
if(!_awakenedFromNib || (_isHidden && hide) || (!_isHidden && !hide) || (_currentStep != kPSMIsNotBeingResized)) {
return;
}
[[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
_isHidden = hide;
_currentStep = 0;
if(!animate) {
_currentStep = (NSInteger)kPSMHideAnimationSteps;
}
if(hide) {
[_overflowPopUpButton removeFromSuperview];
[_addTabButton removeFromSuperview];
} else if(!animate) {
[self addSubview:_overflowPopUpButton];
[self addSubview:_addTabButton];
}
CGFloat partnerOriginalSize, partnerOriginalOrigin, myOriginalSize, myOriginalOrigin, partnerTargetSize, partnerTargetOrigin, myTargetSize, myTargetOrigin;
// target values for partner
if([self orientation] == PSMTabBarHorizontalOrientation) {
// current (original) values
myOriginalSize = [self frame].size.height;
myOriginalOrigin = [self frame].origin.y;
if(partnerView) {
partnerOriginalSize = [partnerView frame].size.height;
partnerOriginalOrigin = [partnerView frame].origin.y;
} else {
partnerOriginalSize = [[self window] frame].size.height;
partnerOriginalOrigin = [[self window] frame].origin.y;
}
if(partnerView) {
// above or below me?
if((myOriginalOrigin - 22) > partnerOriginalOrigin) {
// partner is below me
if(_isHidden) {
// I'm shrinking
myTargetOrigin = myOriginalOrigin + 21;
myTargetSize = myOriginalSize - 21;
partnerTargetOrigin = partnerOriginalOrigin;
partnerTargetSize = partnerOriginalSize + 21;
} else {
// I'm growing
myTargetOrigin = myOriginalOrigin - 21;
myTargetSize = myOriginalSize + 21;
partnerTargetOrigin = partnerOriginalOrigin;
partnerTargetSize = partnerOriginalSize - 21;
}
} else {
// partner is above me
if(_isHidden) {
// I'm shrinking
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize - 21;
partnerTargetOrigin = partnerOriginalOrigin - 21;
partnerTargetSize = partnerOriginalSize + 21;
} else {
// I'm growing
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize + 21;
partnerTargetOrigin = partnerOriginalOrigin + 21;
partnerTargetSize = partnerOriginalSize - 21;
}
}
} else {
// for window movement
if(_isHidden) {
// I'm shrinking
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize - 21;
partnerTargetOrigin = partnerOriginalOrigin + 21;
partnerTargetSize = partnerOriginalSize - 21;
} else {
// I'm growing
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize + 21;
partnerTargetOrigin = partnerOriginalOrigin - 21;
partnerTargetSize = partnerOriginalSize + 21;
}
}
} else { /* vertical */
// current (original) values
myOriginalSize = [self frame].size.width;
myOriginalOrigin = [self frame].origin.x;
if(partnerView) {
partnerOriginalSize = [partnerView frame].size.width;
partnerOriginalOrigin = [partnerView frame].origin.x;
} else {
partnerOriginalSize = [[self window] frame].size.width;
partnerOriginalOrigin = [[self window] frame].origin.x;
}
if(partnerView) {
//to the left or right?
if(myOriginalOrigin < partnerOriginalOrigin + partnerOriginalSize) {
// partner is to the left
if(_isHidden) {
// I'm shrinking
myTargetOrigin = myOriginalOrigin;
myTargetSize = 1;
partnerTargetOrigin = partnerOriginalOrigin - myOriginalSize + 1;
partnerTargetSize = partnerOriginalSize + myOriginalSize - 1;
_tabBarWidth = myOriginalSize;
} else {
// I'm growing
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize + _tabBarWidth;
partnerTargetOrigin = partnerOriginalOrigin + _tabBarWidth;
partnerTargetSize = partnerOriginalSize - _tabBarWidth;
}
} else {
// partner is to the right
if(_isHidden) {
// I'm shrinking
myTargetOrigin = myOriginalOrigin + myOriginalSize;
myTargetSize = 1;
partnerTargetOrigin = partnerOriginalOrigin;
partnerTargetSize = partnerOriginalSize + myOriginalSize;
_tabBarWidth = myOriginalSize;
} else {
// I'm growing
myTargetOrigin = myOriginalOrigin - _tabBarWidth;
myTargetSize = myOriginalSize + _tabBarWidth;
partnerTargetOrigin = partnerOriginalOrigin;
partnerTargetSize = partnerOriginalSize - _tabBarWidth;
}
}
} else {
// for window movement
if(_isHidden) {
// I'm shrinking
myTargetOrigin = myOriginalOrigin;
myTargetSize = 1;
partnerTargetOrigin = partnerOriginalOrigin + myOriginalSize - 1;
partnerTargetSize = partnerOriginalSize - myOriginalSize + 1;
_tabBarWidth = myOriginalSize;
} else {
// I'm growing
myTargetOrigin = myOriginalOrigin;