summaryrefslogtreecommitdiff
path: root/frontends/cocoa/PSMTabBarControl/PSMTabBarControl.m
blob: cb0e0342cbd183ad489b1e9935046737152ae797 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
//
//  PSMTabBarControl.m
//  PSMTabBarControl
//
//  Created by John Pannell on 10/13/05.
//  Copyright 2005 Positive Spin Media. All rights reserved.
//

#import <objc/runtime.h>

#import "PSMTabBarControl.h"
#import "PSMTabBarCell.h"
#import "PSMOverflowPopUpButton.h"
#import "PSMRolloverButton.h"
#import "PSMTabStyle.h"
#import "PSMUnifiedTabStyle.h"
#import "PSMTabDragAssistant.h"
#import "PSMTabBarController.h"

@interface PSMTabBarControl (Private)

// constructor/destructor
- (void)initAddedProperties;

// accessors
- (NSEvent *)lastMouseDownEvent;
- (void)setLastMouseDownEvent:(NSEvent *)event;

// contents
- (void)addTabViewItem:(NSTabViewItem *)item;
- (void)removeTabForCell:(PSMTabBarCell *)cell;

// draw
- (void)update;
- (void)update:(BOOL)animate;
- (void)_setupTrackingRectsForCell:(PSMTabBarCell *)cell;
- (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

#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 {
	return [self frame].size.width - [style leftMarginForTabBarControl] - [style rightMarginForTabBarControl] - _resizeAreaCompensation;
}

/*!
    @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 = [style leftMarginForTabBarControl];
	aRect.origin.y = 0.0;
	aRect.size.width = [self availableCellWidth];
	aRect.size.height = [style tabCellHeight];
	return aRect;
}

#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 = [[[[self class] defaultStyleClass] alloc] init];

	// the overflow button/menu
	NSRect overflowButtonRect = NSMakeRect([self frame].size.width - [style rightMarginForTabBarControl] + 1, 0, [style rightMarginForTabBarControl] - 1, [self frame].size.height);
	_overflowPopUpButton = [[PSMOverflowPopUpButton alloc] initWithFrame:overflowButtonRect pullsDown:YES];
	[_overflowPopUpButton setAutoresizingMask:NSViewNotSizable | NSViewMinXMargin];
	[_overflowPopUpButton setHidden:YES];
	[self addSubview:_overflowPopUpButton];
	[self _positionOverflowMenu];

	// new tab button
	NSRect addTabButtonRect = NSMakeRect([self frame].size.width - [style rightMarginForTabBarControl] + 1, 3.0, 16.0, 16.0);
	_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];
		[self addSubview:_addTabButton];

		if(_showAddTabButton) {
			[_addTabButton setHidden:NO];
		} else {
			[_addTabButton setHidden:YES];
		}
		[_addTabButton setNeedsDisplay:YES];
	}
}

+ (Class) defaultStyleClass
{
	return [PSMUnifiedTabStyle class];
}

- (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];
	}
	[self setTarget:self];
	return self;
}

- (void)dealloc {
	[[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
	NSEnumerator *enumerator = [[[_cells copy] autorelease] objectEnumerator];
	PSMTabBarCell *nextCell;
	while((nextCell = [enumerator nextObject])) {
		[self removeTabForCell:nextCell];
	}

	[_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
	NSArray *existingItems = [tabView tabViewItems];
	NSEnumerator *e = [existingItems objectEnumerator];
	NSTabViewItem *item;
	while((item = [e nextObject])) {
		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 Accessors

- (NSMutableArray *)cells {
	return _cells;
}

- (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 styleClass = NSClassFromString( [NSString stringWithFormat: @"PSM%@TabStyle", [name capitalizedString]] );
	if (styleClass == Nil) {
		styleClass = object_getClass([PSMTabBarControl defaultStyleClass]);
	}

	id <PSMTabStyle> newStyle = [[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 style] setOrientation:_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([_controller addButtonRect])) {
		[_addTabButton setFrame:[_controller addButtonRect]];
	}

	[_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;
}

#pragma mark -
#pragma mark Functionality

- (void)addTabViewItem:(NSTabViewItem *)item {
	// create cell
	PSMTabBarCell *cell = [[PSMTabBarCell alloc] initWithControlView:self];
	NSRect cellRect, lastCellFrame;
	if([_cells lastObject] != nil) {
		cellRect = lastCellFrame = [[_cells lastObject] frame];
	} else {
		cellRect = lastCellFrame = NSZeroRect;
	}

	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
	[_cells addObject:cell];
	[cell release];
	if([_cells count] == (NSUInteger)[tabView numberOfTabViewItems]) {
		[self update]; // don't update unless all are accounted for!
	}
}

- (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
	[item removeObserver:self forKeyPath:@"identifier"];

	// remove indicator
	if([[self subviews] containsObject:[cell indicator]]) {
		[[cell indicator] removeFromSuperview];
	}
	// remove tracking
	[[NSNotificationCenter defaultCenter] removeObserver:cell];

	if([cell closeButtonTrackingTag] != 0) {
		[self removeTrackingRect:[cell closeButtonTrackingTag]];
		[cell setCloseButtonTrackingTag:0];
	}
	if([cell cellTrackingTag] != 0) {
		[self removeTrackingRect:[cell cellTrackingTag]];
		[cell setCellTrackingTag:0];
	}

	// pull from collection
	[_cells removeObject:cell];

	[self update];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	// did the tab's identifier change?
	if([keyPath isEqualToString:@"identifier"]) {
		NSEnumerator *e = [_cells objectEnumerator];
		PSMTabBarCell *cell;
		while((cell = [e nextObject])) {
			if([cell representedObject] == object) {
				[self _bindPropertiesForCell:cell andTabViewItem:object];
			}
		}
	}
}

#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;
				myTargetSize = _tabBarWidth;
				partnerTargetOrigin = partnerOriginalOrigin - _tabBarWidth + 1;
				partnerTargetSize = partnerOriginalSize + _tabBarWidth - 1;
			}
		}

		if(!_isHidden && [[self delegate] respondsToSelector:@selector(desiredWidthForVerticalTabBar:)]) {
			myTargetSize = [[self delegate] desiredWidthForVerticalTabBar:self];
		}
	}

	NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:myOriginalOrigin], @"myOriginalOrigin", [NSNumber numberWithDouble:partnerOriginalOrigin], @"partnerOriginalOrigin", [NSNumber numberWithDouble:myOriginalSize], @"myOriginalSize", [NSNumber numberWithDouble:partnerOriginalSize], @"partnerOriginalSize", [NSNumber numberWithDouble:myTargetOrigin], @"myTargetOrigin", [NSNumber numberWithDouble:partnerTargetOrigin], @"partnerTargetOrigin", [NSNumber numberWithDouble:myTargetSize], @"myTargetSize", [NSNumber numberWithDouble:partnerTargetSize], @"partnerTargetSize", nil];
	if(_showHideAnimationTimer) {
		[_showHideAnimationTimer invalidate];
		[_showHideAnimationTimer release];
	}
	_showHideAnimationTimer = [[NSTimer scheduledTimerWithTimeInterval:(1.0 / 30.0) target:self selector:@selector(animateShowHide:) userInfo:userInfo repeats:YES] retain];
}

- (void)animateShowHide:(NSTimer *)timer {
	// moves the frame of the tab bar and window (or partner view) linearly to hide or show the tab bar
	NSRect myFrame = [self frame];
	NSDictionary *userInfo = [timer userInfo];
	CGFloat myCurrentOrigin = ([[userInfo objectForKey:@"myOriginalOrigin"] doubleValue] + (([[userInfo objectForKey:@"myTargetOrigin"] doubleValue] - [[userInfo objectForKey:@"myOriginalOrigin"] doubleValue]) * (_currentStep / kPSMHideAnimationSteps)));
	CGFloat myCurrentSize = ([[userInfo objectForKey:@"myOriginalSize"] doubleValue] + (([[userInfo objectForKey:@"myTargetSize"] doubleValue] - [[userInfo objectForKey:@"myOriginalSize"] doubleValue]) * (_currentStep / kPSMHideAnimationSteps)));
	CGFloat partnerCurrentOrigin = ([[userInfo objectForKey:@"partnerOriginalOrigin"] doubleValue] + (([[userInfo objectForKey:@"partnerTargetOrigin"] doubleValue] - [[userInfo objectForKey:@"partnerOriginalOrigin"] doubleValue]) * (_currentStep / kPSMHideAnimationSteps)));
	CGFloat partnerCurrentSize = ([[userInfo objectForKey:@"partnerOriginalSize"] doubleValue] + (([[userInfo objectForKey:@"partnerTargetSize"] doubleValue] - [[userInfo objectForKey:@"partnerOriginalSize"] doubleValue]) * (_currentStep / kPSMHideAnimationSteps)));

	NSRect myNewFrame;
	if([self orientation] == PSMTabBarHorizontalOrientation) {
		myNewFrame = NSMakeRect(myFrame.origin.x, myCurrentOrigin, myFrame.size.width, myCurrentSize);
	} else {
		myNewFrame = NSMakeRect(myCurrentOrigin, myFrame.origin.y, myCurrentSize, myFrame.size.height);
	}

	if(partnerView) {
		// resize self and view
		NSRect resizeRect;
		if([self orientation] == PSMTabBarHorizontalOrientation) {
			resizeRect = NSMakeRect([partnerView frame].origin.x, partnerCurrentOrigin, [partnerView frame].size.width, partnerCurrentSize);
		} else {
			resizeRect = NSMakeRect(partnerCurrentOrigin, [partnerView frame].origin.y, partnerCurrentSize, [partnerView frame].size.height);
		}
		[partnerView setFrame:resizeRect];
		[partnerView setNeedsDisplay:YES];
		[self setFrame:myNewFrame];
	} else {
		// resize self and window
		NSRect resizeRect;
		if([self orientation] == PSMTabBarHorizontalOrientation) {
			resizeRect = NSMakeRect([[self window] frame].origin.x, partnerCurrentOrigin, [[self window] frame].size.width, partnerCurrentSize);
		} else {
			resizeRect = NSMakeRect(partnerCurrentOrigin, [[self window] frame].origin.y, partnerCurrentSize, [[self window] frame].size.height);
		}
		[[self window] setFrame:resizeRect display:YES];
		[self setFrame:myNewFrame];
	}

	// next
	_currentStep++;
	if(_currentStep == kPSMHideAnimationSteps + 1) {
		_currentStep = kPSMIsNotBeingResized;
		[self viewDidEndLiveResize];
		[self update:NO];

		//send the delegate messages
		if(_isHidden) {
			if([[self delegate] respondsToSelector:@selector(tabView:tabBarDidHide:)]) {
				[[self delegate] tabView:[self tabView] tabBarDidHide:self];
			}
		} else {
			[self addSubview:_overflowPopUpButton];
			[self addSubview:_addTabButton];

			if([[self delegate] respondsToSelector:@selector(tabView:tabBarDidUnhide:)]) {
				[[self delegate] tabView:[self tabView] tabBarDidUnhide:self];
			}
		}

		[_showHideAnimationTimer invalidate];
		[_showHideAnimationTimer release]; _showHideAnimationTimer = nil;
	}
	[[self window] display];
}

- (BOOL)isTabBarHidden {
	return _isHidden;
}

- (BOOL)isAnimating {
	return _animationTimer != nil;
}

- (id)partnerView {
	return partnerView;
}

- (void)setPartnerView:(id)view {
	[partnerView release];
	[view retain];
	partnerView = view;
}

#pragma mark -
#pragma mark Drawing

- (BOOL)isFlipped {
	return YES;
}

- (void)drawRect:(NSRect)rect {
	[style drawTabBar:self inRect:rect];
}

- (void)update {
	[self update:_automaticallyAnimates];
}

- (void)update:(BOOL)animate {
	// make sure all of our tabs are accounted for before updating
	if((NSUInteger)[[self tabView] numberOfTabViewItems] != [_cells count]) {
		return;
	}

	// hide/show? (these return if already in desired state)
	if((_hideForSingleTab) && ([_cells count] <= 1)) {
		[self hideTabBar:YES animate:YES];
		return;
	} else {
		[self hideTabBar:NO animate:YES];
	}

	[self removeAllToolTips];
	[_controller layoutCells]; //eventually we should only have to call this when we know something has changed

	PSMTabBarCell *currentCell;

	NSMenu *overflowMenu = [_controller overflowMenu];
	[_overflowPopUpButton setHidden:(overflowMenu == nil)];
	[_overflowPopUpButton setMenu:overflowMenu];

	if(_animationTimer) {
		[_animationTimer invalidate];
		[_animationTimer release]; _animationTimer = nil;
	}

	if(animate) {
		NSMutableArray *targetFrames = [NSMutableArray arrayWithCapacity:[_cells count]];

		for(NSUInteger i = 0; i < [_cells count]; i++) {
			currentCell = [_cells objectAtIndex:i];

			//we're going from NSRect -> NSValue -> NSRect -> NSValue here - oh well
			[targetFrames addObject:[NSValue valueWithRect:[_controller cellFrameAtIndex:i]]];
		}

		[_addTabButton setHidden:!_showAddTabButton];

		NSAnimation *animation = [[NSAnimation alloc] initWithDuration:0.50 animationCurve:NSAnimationEaseInOut];
		[animation setAnimationBlockingMode:NSAnimationNonblocking];
		[animation startAnimation];
		_animationTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0
							target:self
							selector:@selector(_animateCells:)
							userInfo:[NSArray arrayWithObjects:targetFrames, animation, nil]
							repeats:YES] retain];
		[animation release];
		[[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:NSEventTrackingRunLoopMode];
		[self _animateCells:_animationTimer];
	} else {
		for(NSUInteger i = 0; i < [_cells count]; i++) {
			currentCell = [_cells objectAtIndex:i];
			[currentCell setFrame:[_controller cellFrameAtIndex:i]];

			if(![currentCell isInOverflowMenu]) {
				[self _setupTrackingRectsForCell:currentCell];
			}
		}

		[_addTabButton setFrame:[_controller addButtonRect]];
		[_addTabButton setHidden:!_showAddTabButton];
		[self setNeedsDisplay:YES];
	}
}

- (void)_animateCells:(NSTimer *)timer {
	NSAnimation *animation = [[timer userInfo] objectAtIndex:1];
	NSArray *targetFrames = [[timer userInfo] objectAtIndex:0];
	PSMTabBarCell *currentCell;
	NSUInteger cellCount = [_cells count];

	if((cellCount > 0) && [animation isAnimating]) {
		//compare our target position with the current position and move towards the target
		for(NSUInteger i = 0; i < [targetFrames count] && i < cellCount; i++) {
			currentCell = [_cells objectAtIndex:i];
			NSRect cellFrame = [currentCell frame], targetFrame = [[targetFrames objectAtIndex:i] rectValue];
			CGFloat sizeChange;
			CGFloat originChange;

			if([self orientation] == PSMTabBarHorizontalOrientation) {
				sizeChange = (targetFrame.size.width - cellFrame.size.width) * [animation currentProgress];
				originChange = (targetFrame.origin.x - cellFrame.origin.x) * [animation currentProgress];
				cellFrame.size.width += sizeChange;
				cellFrame.origin.x += originChange;
			} else {
				sizeChange = (targetFrame.size.height - cellFrame.size.height) * [animation currentProgress];
				originChange = (targetFrame.origin.y - cellFrame.origin.y) * [animation currentProgress];
				cellFrame.size.height += sizeChange;
				cellFrame.origin.y += originChange;
			}

			[currentCell setFrame:cellFrame];

			//highlight the cell if the mouse is over it
			NSPoint mousePoint = [self convertPoint:[[self window] mouseLocationOutsideOfEventStream] fromView:nil];
			NSRect closeRect = [currentCell closeButtonRectForFrame:cellFrame];
			[currentCell setHighlighted:NSMouseInRect(mousePoint, cellFrame, [self isFlipped])];
			[currentCell setCloseButtonOver:NSMouseInRect(mousePoint, closeRect, [self isFlipped])];
		}

		if(_showAddTabButton) {
			//animate the add tab button
			NSRect target = [_controller addButtonRect], frame = [_addTabButton frame];
			frame.origin.x += (target.origin.x - frame.origin.x) * [animation currentProgress];
			[_addTabButton setFrame:frame];
		}
	} else {
		//put all the cells where they should be in their final position
		if(cellCount > 0) {
			for(NSUInteger i = 0; i < [targetFrames count] && i < cellCount; i++) {
				PSMTabBarCell *currentCell = [_cells objectAtIndex:i];
				NSRect cellFrame = [currentCell frame], targetFrame = [[targetFrames objectAtIndex:i] rectValue];

				if([self orientation] == PSMTabBarHorizontalOrientation) {
					cellFrame.size.width = targetFrame.size.width;
					cellFrame.origin.x = targetFrame.origin.x;
				} else {
					cellFrame.size.height = targetFrame.size.height;
					cellFrame.origin.y = targetFrame.origin.y;
				}

				[currentCell setFrame:cellFrame];

				//highlight the cell if the mouse is over it
				NSPoint mousePoint = [self convertPoint:[[self window] mouseLocationOutsideOfEventStream] fromView:nil];
				NSRect closeRect = [currentCell closeButtonRectForFrame:cellFrame];
				[currentCell setHighlighted:NSMouseInRect(mousePoint, cellFrame, [self isFlipped])];
				[currentCell setCloseButtonOver:NSMouseInRect(mousePoint, closeRect, [self isFlipped])];
			}
		}

		//set the frame for the add tab button
		if(_showAddTabButton) {
			NSRect frame = [_addTabButton frame];
			frame.origin.x = [_controller addButtonRect].origin.x;
			[_addTabButton setFrame:frame];
		}

		[_animationTimer invalidate];
		[_animationTimer release]; _animationTimer = nil;

		for(NSUInteger i = 0; i < cellCount; i++) {
			currentCell = [_cells objectAtIndex:i];

			//we've hit the cells that are in overflow, stop setting up tracking rects
			if([currentCell isInOverflowMenu]) {
				break;
			}

			[self _setupTrackingRectsForCell:currentCell];
		}
	}

	[self setNeedsDisplay:YES];
}

- (void)_setupTrackingRectsForCell:(PSMTabBarCell *)cell {
	NSInteger tag, index = [_cells indexOfObject:cell];
	NSRect cellTrackingRect = [_controller cellTrackingRectAtIndex:index];
	NSPoint mousePoint = [self convertPoint:[[self window] mouseLocationOutsideOfEventStream] fromView:nil];
	BOOL mouseInCell = NSMouseInRect(mousePoint, cellTrackingRect, [self isFlipped]);

	//set the cell tracking rect
	[self removeTrackingRect:[cell cellTrackingTag]];
	tag = [self addTrackingRect:cellTrackingRect owner:cell userData:nil assumeInside:mouseInCell];
	[cell setCellTrackingTag:tag];
	[cell setHighlighted:mouseInCell];

	if([cell hasCloseButton] && ![cell isCloseButtonSuppressed]) {
		NSRect closeRect = [_controller closeButtonTrackingRectAtIndex:index];
		BOOL mouseInCloseRect = NSMouseInRect(mousePoint, closeRect, [self isFlipped]);

		//set the close button tracking rect
		[self removeTrackingRect:[cell closeButtonTrackingTag]];
		tag = [self addTrackingRect:closeRect owner:cell userData:nil assumeInside:mouseInCloseRect];
		[cell setCloseButtonTrackingTag:tag];

		[cell setCloseButtonOver:mouseInCloseRect];
	}

	//set the tooltip tracking rect
	[self addToolTipRect:[cell frame] owner:self userData:nil];
}

- (void)_positionOverflowMenu {
	NSRect cellRect, frame = [self frame];
	cellRect.size.height = [style tabCellHeight];
	cellRect.size.width = [style rightMarginForTabBarControl];

	if([self orientation] == PSMTabBarHorizontalOrientation) {
		cellRect.origin.y = 0;
		cellRect.origin.x = frame.size.width - [style rightMarginForTabBarControl] + (_resizeAreaCompensation ? -(_resizeAreaCompensation - 1) : 1);
		[_overflowPopUpButton setAutoresizingMask:NSViewNotSizable | NSViewMinXMargin];
	} else {
		cellRect.origin.x = 0;
		cellRect.origin.y = frame.size.height - [style tabCellHeight];
		cellRect.size.width = frame.size.width;
		[_overflowPopUpButton setAutoresizingMask:NSViewNotSizable | NSViewMinXMargin | NSViewMinYMargin];
	}

	[_overflowPopUpButton setFrame:cellRect];
}

- (void)_checkWindowFrame {
	//figure out if the new frame puts the control in the way of the resize widget
	NSWindow *window = [self window];

	if(window) {
		NSRect resizeWidgetFrame = [[window contentView] frame];
		resizeWidgetFrame.origin.x += resizeWidgetFrame.size.width - 22;
		resizeWidgetFrame.size.width = 22;
		resizeWidgetFrame.size.height = 22;

		if([window showsResizeIndicator] && NSIntersectsRect([self frame], resizeWidgetFrame)) {
			//the resize widgets are larger on metal windows
			_resizeAreaCompensation = [window styleMask] & NSTexturedBackgroundWindowMask ? 20 : 8;
		} else {
			_resizeAreaCompensation = 0;
		}

		[self _positionOverflowMenu];
	}
}

#pragma mark -
#pragma mark Mouse Tracking

- (BOOL)mouseDownCanMoveWindow {
	return NO;
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
	return YES;
}

- (void)mouseDown:(NSEvent *)theEvent {
	_didDrag = NO;

	// keep for dragging
	[self setLastMouseDownEvent:theEvent];
	// what cell?
	NSPoint mousePt = [self convertPoint:[theEvent locationInWindow] fromView:nil];
	NSRect frame = [self frame];

	if([self orientation] == PSMTabBarVerticalOrientation && [self allowsResizing] && partnerView && (mousePt.x > frame.size.width - 3)) {
		_resizing = YES;
	}

	NSRect cellFrame;
	PSMTabBarCell *cell = [self cellForPoint:mousePt cellFrame:&cellFrame];
	if(cell) {
		BOOL overClose = NSMouseInRect(mousePt, [cell closeButtonRectForFrame:cellFrame], [self isFlipped]);
		if(overClose &&
		   ![self disableTabClose] &&
		   ![cell isCloseButtonSuppressed] &&
		   ([self allowsBackgroundTabClosing] || [[cell representedObject] isEqualTo:[tabView selectedTabViewItem]] || [theEvent modifierFlags] & NSCommandKeyMask)) {
			[cell setCloseButtonOver:NO];
			[cell setCloseButtonPressed:YES];
			_closeClicked = YES;
		} else {
			[cell setCloseButtonPressed:NO];
			if(_selectsTabsOnMouseDown) {
				[self performSelector:@selector(tabClick:) withObject:cell];
			}
		}
		[self setNeedsDisplay:YES];
	}
}

- (void)mouseDragged:(NSEvent *)theEvent {
	if([self lastMouseDownEvent] == nil) {
		return;
	}

	NSPoint currentPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];

	if(_resizing) {
		NSRect frame = [self frame];
		CGFloat resizeAmount = [theEvent deltaX];
		if((currentPoint.x > frame.size.width && resizeAmount > 0) || (currentPoint.x < frame.size.width && resizeAmount < 0)) {
			[[NSCursor resizeLeftRightCursor] push];

			NSRect partnerFrame = [partnerView frame];

			//do some bounds checking
			if((frame.size.width + resizeAmount > [self cellMinWidth]) && (frame.size.width + resizeAmount < [self cellMaxWidth])) {
				frame.size.width += resizeAmount;
				partnerFrame.size.width -= resizeAmount;
				partnerFrame.origin.x += resizeAmount;

				[self setFrame:frame];
				[partnerView setFrame:partnerFrame];
				[[self superview] setNeedsDisplay:YES];
			}
		}
		return;
	}

	NSRect cellFrame;
	NSPoint trackingStartPoint = [self convertPoint:[[self lastMouseDownEvent] locationInWindow] fromView:nil];
	PSMTabBarCell *cell = [self cellForPoint:trackingStartPoint cellFrame:&cellFrame];
	if(cell) {
		//check to see if the close button was the target in the clicked cell
		//highlight/unhighlight the close button as necessary
		NSRect iconRect = [cell closeButtonRectForFrame:cellFrame];

		if(_closeClicked && NSMouseInRect(trackingStartPoint, iconRect, [self isFlipped]) &&
		   ([self allowsBackgroundTabClosing] || [[cell representedObject] isEqualTo:[tabView selectedTabViewItem]])) {
			[cell setCloseButtonPressed:NSMouseInRect(currentPoint, iconRect, [self isFlipped])];
			[self setNeedsDisplay:YES];
			return;
		}

		CGFloat dx = fabs(currentPoint.x - trackingStartPoint.x);
		CGFloat dy = fabs(currentPoint.y - trackingStartPoint.y);
		CGFloat distance = sqrt(dx * dx + dy * dy);

		if(distance >= 10 && !_didDrag && ![[PSMTabDragAssistant sharedDragAssistant] isDragging] &&
		   [self delegate] && [[self delegate] respondsToSelector:@selector(tabView:shouldDragTabViewItem:fromTabBar:)] &&
		   [[self delegate] tabView:tabView shouldDragTabViewItem:[cell representedObject] fromTabBar:self]) {
			_didDrag = YES;
			[[PSMTabDragAssistant sharedDragAssistant] startDraggingCell:cell fromTabBar:self withMouseDownEvent:[self lastMouseDownEvent]];
		}
	}
}

- (void)mouseUp:(NSEvent *)theEvent {
	if(_resizing) {
		_resizing = NO;
		[[NSCursor arrowCursor] set];
	} else {
		// what cell?
		NSPoint mousePt = [self convertPoint:[theEvent locationInWindow] fromView:nil];
		NSRect cellFrame, mouseDownCellFrame;
		PSMTabBarCell *cell = [self cellForPoint:mousePt cellFrame:&cellFrame];
		PSMTabBarCell *mouseDownCell = [self cellForPoint:[self convertPoint:[[self lastMouseDownEvent] locationInWindow] fromView:nil] cellFrame:&mouseDownCellFrame];
		if(cell) {
			NSPoint trackingStartPoint = [self convertPoint:[[self lastMouseDownEvent] locationInWindow] fromView:nil];
			NSRect iconRect = [mouseDownCell closeButtonRectForFrame:mouseDownCellFrame];

			if((NSMouseInRect(mousePt, iconRect, [self isFlipped])) && ![self disableTabClose] && ![cell isCloseButtonSuppressed] && [mouseDownCell closeButtonPressed]) {
				if(([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0) {
					//If the user is holding Option, close all other tabs
					NSEnumerator    *enumerator = [[[[self cells] copy] autorelease] objectEnumerator];
					PSMTabBarCell   *otherCell;

					while((otherCell = [enumerator nextObject])) {
						if(otherCell != cell) {
							[self performSelector:@selector(closeTabClick:) withObject:otherCell];
						}
					}

					//Fix the close button for the clicked tab not to be pressed
					[cell setCloseButtonPressed:NO];
				} else {
					//Otherwise, close this tab
					[self performSelector:@selector(closeTabClick:) withObject:cell];
				}
			} else if(NSMouseInRect(mousePt, mouseDownCellFrame, [self isFlipped]) &&
					  (!NSMouseInRect(trackingStartPoint, [cell closeButtonRectForFrame:cellFrame], [self isFlipped]) || ![self allowsBackgroundTabClosing] || [self disableTabClose])) {
				[mouseDownCell setCloseButtonPressed:NO];
				// If -[self selectsTabsOnMouseDown] is TRUE, we already performed tabClick: on mouseDown.
				if(![self selectsTabsOnMouseDown]) {
					[self performSelector:@selector(tabClick:) withObject:cell];
				}
			} else {
				[mouseDownCell setCloseButtonPressed:NO];
				[self performSelector:@selector(tabNothing:) withObject:cell];
			}
		}

		_closeClicked = NO;
	}
}

- (NSMenu *)menuForEvent:(NSEvent *)event {
	NSMenu *menu = nil;
	NSTabViewItem *item = [[self cellForPoint:[self convertPoint:[event locationInWindow] fromView:nil] cellFrame:nil] representedObject];

	if(item && [[self delegate] respondsToSelector:@selector(tabView:menuForTabViewItem:)]) {
		menu = [[self delegate] tabView:tabView menuForTabViewItem:item];
	}
	return menu;
}

#pragma mark -
#pragma mark Drag and Drop

- (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent *)theEvent {
	return YES;
}

// NSDraggingSource
- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal {
	return(isLocal ? NSDragOperationMove : NSDragOperationNone);
}

- (BOOL)ignoreModifierKeysWhileDragging {
	return YES;
}

- (void)draggedImage:(NSImage *)anImage beganAt:(NSPoint)screenPoint {
	[[PSMTabDragAssistant sharedDragAssistant] draggingBeganAt:screenPoint];
}

- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint {
	[[PSMTabDragAssistant sharedDragAssistant] draggingMovedTo:screenPoint];
}

// NSDraggingDestination
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
	if([[[sender draggingPasteboard] types] indexOfObject:@"PSMTabBarControlItemPBType"] != NSNotFound) {
		if([self delegate] && [[self delegate] respondsToSelector:@selector(tabView:shouldDropTabViewItem:inTabBar:)] &&
		   ![[self delegate] tabView:[[sender draggingSource] tabView] shouldDropTabViewItem:[[[PSMTabDragAssistant sharedDragAssistant] draggedCell] representedObject] inTabBar:self]) {
			return NSDragOperationNone;
		}

		[[PSMTabDragAssistant sharedDragAssistant] draggingEnteredTabBar:self atPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
		return NSDragOperationMove;
	}

	return NSDragOperationNone;
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender {
	PSMTabBarCell *cell = [self cellForPoint:[self convertPoint:[sender draggingLocation] fromView:nil] cellFrame:nil];

	if([[[sender draggingPasteboard] types] indexOfObject:@"PSMTabBarControlItemPBType"] != NSNotFound) {
		if([self delegate] && [[self delegate] respondsToSelector:@selector(tabView:shouldDropTabViewItem:inTabBar:)] &&
		   ![[self delegate] tabView:[[sender draggingSource] tabView] shouldDropTabViewItem:[[[PSMTabDragAssistant sharedDragAssistant] draggedCell] representedObject] inTabBar:self]) {
			return NSDragOperationNone;
		}

		[[PSMTabDragAssistant sharedDragAssistant] draggingUpdatedInTabBar:self atPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
		return NSDragOperationMove;
	} else if(cell) {
		//something that was accepted by the delegate was dragged on

		//Test for the space bar (the skip-the-delay key).
		/*enum { virtualKeycodeForSpace = 49 }; //Source: IM:Tx (Fig. C-2)
		   union {
		        KeyMap keymap;
		        char bits[16];
		   } keymap;
		   GetKeys(keymap.keymap);
		   if ((GetCurrentEventKeyModifiers() == 0) && bit_test(keymap.bits, virtualKeycodeForSpace)) {
		        //The user pressed the space bar. This skips the delay; the user wants to pop the spring on this tab *now*.

		        //For some reason, it crashes if I call -fire here. I don't know why. It doesn't crash if I simply set the fire date to now.
		        [_springTimer setFireDate:[NSDate date]];
		   } else {*/
		//Wind the spring for a spring-loaded drop.
		//The delay time comes from Finder's defaults, which specifies it in milliseconds.
		//If the delegate can't handle our spring-loaded drop, we'll abort it when the timer fires. See fireSpring:. This is simpler than constantly (checking for spring-loaded awareness and tearing down/rebuilding the timer) at every delegate change.

		//If the user has dragged to a different tab, reset the timer.
		if(_tabViewItemWithSpring != [cell representedObject]) {
			[_springTimer invalidate];
			[_springTimer release]; _springTimer = nil;
			_tabViewItemWithSpring = [cell representedObject];
		}
		if(!_springTimer) {
			//Finder's default delay time, as of Tiger, is 668 ms. If the user has never changed it, there's no setting in its defaults, so we default to that amount.
			NSNumber *delayNumber = [(NSNumber *)CFPreferencesCopyAppValue((CFStringRef)@"SpringingDelayMilliseconds", (CFStringRef)@"com.apple.finder") autorelease];
			NSTimeInterval delaySeconds = delayNumber ?[delayNumber doubleValue] / 1000.0 : 0.668;
			_springTimer = [[NSTimer scheduledTimerWithTimeInterval:delaySeconds
							 target:self
							 selector:@selector(fireSpring:)
							 userInfo:sender
							 repeats:NO] retain];
		}
		return NSDragOperationCopy;
	}

	return NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender {
	[_springTimer invalidate];
	[_springTimer release]; _springTimer = nil;

	[[PSMTabDragAssistant sharedDragAssistant] draggingExitedTabBar:self];
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
	//validate the drag operation only if there's a valid tab bar to drop into
	return [[[sender draggingPasteboard] types] indexOfObject:@"PSMTabBarControlItemPBType"] == NSNotFound ||
		   [[PSMTabDragAssistant sharedDragAssistant] destinationTabBar] != nil;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
	if([[[sender draggingPasteboard] types] indexOfObject:@"PSMTabBarControlItemPBType"] != NSNotFound) {
		[[PSMTabDragAssistant sharedDragAssistant] performDragOperation];
	} else if([self delegate] && [[self delegate] respondsToSelector:@selector(tabView:acceptedDraggingInfo:onTabViewItem:)]) {
		//forward the drop to the delegate
		[[self delegate] tabView:tabView acceptedDraggingInfo:sender onTabViewItem:[[self cellForPoint:[self convertPoint:[sender draggingLocation] fromView:nil] cellFrame:nil] representedObject]];
	}
	return YES;
}

- (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation {
	[[PSMTabDragAssistant sharedDragAssistant] draggedImageEndedAt:aPoint operation:operation];
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender {
}

#pragma mark -
#pragma mark Spring-loading

- (void)fireSpring:(NSTimer *)timer {
	NSAssert1(timer == _springTimer, @"Spring fired by unrecognized timer %@", timer);

	id <NSDraggingInfo> sender = [timer userInfo];
	PSMTabBarCell *cell = [self cellForPoint:[self convertPoint:[sender draggingLocation] fromView:nil] cellFrame:nil];
	[tabView selectTabViewItem:[cell representedObject]];

	_tabViewItemWithSpring = nil;
	[_springTimer invalidate];
	[_springTimer release]; _springTimer = nil;
}

#pragma mark -
#pragma mark Actions

- (void)overflowMenuAction:(id)sender {
	NSTabViewItem *tabViewItem = (NSTabViewItem *)[sender representedObject];
	[tabView selectTabViewItem:tabViewItem];
}

- (void)closeTabClick:(id)sender {
	NSTabViewItem *item = [sender representedObject];
	[sender retain];
	if(([_cells count] == 1) && (![self canCloseOnlyTab])) {
		return;
	}

	if([[self delegate] respondsToSelector:@selector(tabView:shouldCloseTabViewItem:)]) {
		if(![[self delegate] tabView:tabView shouldCloseTabViewItem:item]) {
			// fix mouse downed close button
			[sender setCloseButtonPressed:NO];
			return;
		}
	}

	[item retain];

	[tabView removeTabViewItem:item];
	[item release];
	[sender release];
}

- (void)tabClick:(id)sender {
	[tabView selectTabViewItem:[sender representedObject]];
}

- (void)tabNothing:(id)sender {
	//[self update];  // takes care of highlighting based on state
}

- (void)frameDidChange:(NSNotification *)notification {
	[self _checkWindowFrame];

	// trying to address the drawing artifacts for the progress indicators - hackery follows
	// this one fixes the "blanking" effect when the control hides and shows itself
	NSEnumerator *e = [_cells objectEnumerator];
	PSMTabBarCell *cell;
	while((cell = [e nextObject])) {
		[[cell indicator] stopAnimation:self];

		[[cell indicator] performSelector:@selector(startAnimation:)
		 withObject:nil
		 afterDelay:0];
	}

	[self update:NO];
}

- (void)viewDidMoveToWindow {
	[self _checkWindowFrame];
}

- (void)viewWillStartLiveResize {
	NSEnumerator *e = [_cells objectEnumerator];
	PSMTabBarCell *cell;
	while((cell = [e nextObject])) {
		[[cell indicator] stopAnimation:self];
	}
	[self setNeedsDisplay:YES];
}

-(void)viewDidEndLiveResize {
	NSEnumerator *e = [_cells objectEnumerator];
	PSMTabBarCell *cell;
	while((cell = [e nextObject])) {
		[[cell indicator] startAnimation:self];
	}

	[self _checkWindowFrame];
	[self update:NO];
}

- (void)resetCursorRects {
	[super resetCursorRects];
	if([self orientation] == PSMTabBarVerticalOrientation) {
		NSRect frame = [self frame];
		[self addCursorRect:NSMakeRect(frame.size.width - 2, 0, 2, frame.size.height) cursor:[NSCursor resizeLeftRightCursor]];
	}
}

- (void)windowDidMove:(NSNotification *)aNotification {
	[self setNeedsDisplay:YES];
}

- (void)windowDidUpdate:(NSNotification *)notification {
	// hide? must readjust things if I'm not supposed to be showing
	// this block of code only runs when the app launches
	if([self hideForSingleTab] && ([_cells count] <= 1) && !_awakenedFromNib) {
		// must adjust frames now before display
		NSRect myFrame = [self frame];
		if([self orientation] == PSMTabBarHorizontalOrientation) {
			if(partnerView) {
				NSRect partnerFrame = [partnerView frame];
				// above or below me?
				if(myFrame.origin.y - 22 > [partnerView frame].origin.y) {
					// partner is below me
					[self setFrame:NSMakeRect(myFrame.origin.x, myFrame.origin.y + 21, myFrame.size.width, myFrame.size.height - 21)];
					[partnerView setFrame:NSMakeRect(partnerFrame.origin.x, partnerFrame.origin.y, partnerFrame.size.width, partnerFrame.size.height + 21)];
				} else {
					// partner is above me
					[self setFrame:NSMakeRect(myFrame.origin.x, myFrame.origin.y, myFrame.size.width, myFrame.size.height - 21)];
					[partnerView setFrame:NSMakeRect(partnerFrame.origin.x, partnerFrame.origin.y - 21, partnerFrame.size.width, partnerFrame.size.height + 21)];
				}
				[partnerView setNeedsDisplay:YES];
				[self setNeedsDisplay:YES];
			} else {
				// for window movement
				NSRect windowFrame = [[self window] frame];
				[[self window] setFrame:NSMakeRect(windowFrame.origin.x, windowFrame.origin.y + 21, windowFrame.size.width, windowFrame.size.height - 21) display:YES];
				[self setFrame:NSMakeRect(myFrame.origin.x, myFrame.origin.y, myFrame.size.width, myFrame.size.height - 21)];
			}
		} else {
			if(partnerView) {
				NSRect partnerFrame = [partnerView frame];
				//to the left or right?
				if(myFrame.origin.x < [partnerView frame].origin.x) {
					// partner is to the left
					[self setFrame:NSMakeRect(myFrame.origin.x, myFrame.origin.y, 1, myFrame.size.height)];
					[partnerView setFrame:NSMakeRect(partnerFrame.origin.x - myFrame.size.width + 1, partnerFrame.origin.y, partnerFrame.size.width + myFrame.size.width - 1, partnerFrame.size.height)];
				} else {
					// partner to the right
					[self setFrame:NSMakeRect(myFrame.origin.x + myFrame.size.width, myFrame.origin.y, 1, myFrame.size.height)];
					[partnerView setFrame:NSMakeRect(partnerFrame.origin.x, partnerFrame.origin.y, partnerFrame.size.width + myFrame.size.width, partnerFrame.size.height)];
				}
				_tabBarWidth = myFrame.size.width;
				[partnerView setNeedsDisplay:YES];
				[self setNeedsDisplay:YES];
			} else {
				// for window movement
				NSRect windowFrame = [[self window] frame];
				[[self window] setFrame:NSMakeRect(windowFrame.origin.x + myFrame.size.width - 1, windowFrame.origin.y, windowFrame.size.width - myFrame.size.width + 1, windowFrame.size.height) display:YES];
				[self setFrame:NSMakeRect(myFrame.origin.x, myFrame.origin.y, 1, myFrame.size.height)];
			}
		}

		_isHidden = YES;

		if([[self delegate] respondsToSelector:@selector(tabView:tabBarDidHide:)]) {
			[[self delegate] tabView:[self tabView] tabBarDidHide:self];
		}
	}

	_awakenedFromNib = YES;
	[self setNeedsDisplay:YES];

	//we only need to do this once
	[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidUpdateNotification object:nil];
}

#pragma mark -
#pragma mark Menu Validation

- (BOOL)validateMenuItem:(NSMenuItem *)sender {
	[sender setState:([[sender representedObject] isEqualTo:[tabView selectedTabViewItem]]) ? NSOnState : NSOffState];

	return [[self delegate] respondsToSelector:@selector(tabView:validateOverflowMenuItem:forTabViewItem:)] ?
		   [[self delegate] tabView:[self tabView] validateOverflowMenuItem:sender forTabViewItem:[sender representedObject]] : YES;
}

#pragma mark -
#pragma mark NSTabView Delegate

- (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem {
	// here's a weird one - this message is sent before the "tabViewDidChangeNumberOfTabViewItems"
	// message, thus I can end up updating when there are no cells, if no tabs were (yet) present
	NSUInteger tabIndex = [aTabView indexOfTabViewItem:tabViewItem];

	if([_cells count] > 0 && tabIndex < [_cells count]) {
		PSMTabBarCell *thisCell = [_cells objectAtIndex:tabIndex];
		if(_alwaysShowActiveTab && [thisCell isInOverflowMenu]) {
			//temporarily disable the delegate in order to move the tab to a different index
			id tempDelegate = [aTabView delegate];
			[aTabView setDelegate:nil];

			// move it all around first
			[tabViewItem retain];
			[thisCell retain];
			[aTabView removeTabViewItem:tabViewItem];
			[aTabView insertTabViewItem:tabViewItem atIndex:0];
			[_cells removeObjectAtIndex:tabIndex];
			[_cells insertObject:thisCell atIndex:0];
			[thisCell setIsInOverflowMenu:NO];                  //very important else we get a fun recursive loop going
			[[_cells objectAtIndex:[_cells count] - 1] setIsInOverflowMenu:YES];             //these 2 lines are pretty uncool and this logic needs to be updated
			[thisCell release];
			[tabViewItem release];

			[aTabView setDelegate:tempDelegate];

			//reset the selection since removing it changed the selection
			[aTabView selectTabViewItem:tabViewItem];

			[self update];
		} else {
			[_controller setSelectedCell:thisCell];
			[self setNeedsDisplay:YES];
		}
	}

	if([[self delegate] respondsToSelector:@selector(tabView:didSelectTabViewItem:)]) {
		[[self delegate] performSelector:@selector(tabView:didSelectTabViewItem:) withObject:aTabView withObject:tabViewItem];
	}
}

- (BOOL)tabView:(NSTabView *)aTabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem {
	if([[self delegate] respondsToSelector:@selector(tabView:shouldSelectTabViewItem:)]) {
		return [[self delegate] tabView:aTabView shouldSelectTabViewItem:tabViewItem];
	} else {
		return YES;
	}
}
- (void)tabView:(NSTabView *)aTabView willSelectTabViewItem:(NSTabViewItem *)tabViewItem {
	if([[self delegate] respondsToSelector:@selector(tabView:willSelectTabViewItem:)]) {
		[[self delegate] performSelector:@selector(tabView:willSelectTabViewItem:) withObject:aTabView withObject:tabViewItem];
	}
}

- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)aTabView {
	NSArray *tabItems = [tabView tabViewItems];
	// go through cells, remove any whose representedObjects are not in [tabView tabViewItems]
	NSEnumerator *e = [[[_cells copy] autorelease] objectEnumerator];
	PSMTabBarCell *cell;
	while((cell = [e nextObject])) {
		//remove the observer binding
		if([cell representedObject] && ![tabItems containsObject:[cell representedObject]]) {
			if([[self delegate] respondsToSelector:@selector(tabView:didCloseTabViewItem:)]) {
				[[self delegate] tabView:aTabView didCloseTabViewItem:[cell representedObject]];
			}

			[self removeTabForCell:cell];
		}
	}

	// go through tab view items, add cell for any not present
	NSMutableArray *cellItems = [self representedTabViewItems];
	NSEnumerator *ex = [tabItems objectEnumerator];
	NSTabViewItem *item;
	while((item = [ex nextObject])) {
		if(![cellItems containsObject:item]) {
			[self addTabViewItem:item];
		}
	}

	// pass along for other delegate responses
	if([[self delegate] respondsToSelector:@selector(tabViewDidChangeNumberOfTabViewItems:)]) {
		[[self delegate] performSelector:@selector(tabViewDidChangeNumberOfTabViewItems:) withObject:aTabView];
	}

	// reset cursor tracking for the add tab button if one exists
	if([self addTabButton]) {
		[[self addTabButton] resetCursorRects];
	}
}

#pragma mark -
#pragma mark Tooltips

- (NSString *)view:(NSView *)view stringForToolTip:(NSToolTipTag)tag point:(NSPoint)point userData:(void *)userData {
	if([[self delegate] respondsToSelector:@selector(tabView:toolTipForTabViewItem:)]) {
		return [[self delegate] tabView:[self tabView] toolTipForTabViewItem:[[self cellForPoint:point cellFrame:nil] representedObject]];
	}
	return nil;
}

#pragma mark -
#pragma mark Archiving

- (void)encodeWithCoder:(NSCoder *)aCoder 
{
	[super encodeWithCoder:aCoder];
	if ([aCoder allowsKeyedCoding]) {
		[aCoder encodeObject:_cells forKey:@"PSMcells"];
		[aCoder encodeObject:tabView forKey:@"PSMtabView"];
		[aCoder encodeObject:_overflowPopUpButton forKey:@"PSMoverflowPopUpButton"];
		[aCoder encodeObject:_addTabButton forKey:@"PSMaddTabButton"];
		[aCoder encodeObject:style forKey:@"PSMstyle"];
		[aCoder encodeInteger:_orientation forKey:@"PSMorientation"];
		[aCoder encodeBool:_canCloseOnlyTab forKey:@"PSMcanCloseOnlyTab"];
		[aCoder encodeBool:_disableTabClose forKey:@"PSMdisableTabClose"];
		[aCoder encodeBool:_hideForSingleTab forKey:@"PSMhideForSingleTab"];
		[aCoder encodeBool:_allowsBackgroundTabClosing forKey:@"PSMallowsBackgroundTabClosing"];
		[aCoder encodeBool:_allowsResizing forKey:@"PSMallowsResizing"];
		[aCoder encodeBool:_selectsTabsOnMouseDown forKey:@"PSMselectsTabsOnMouseDown"];
		[aCoder encodeBool:_showAddTabButton forKey:@"PSMshowAddTabButton"];
		[aCoder encodeBool:_sizeCellsToFit forKey:@"PSMsizeCellsToFit"];
		[aCoder encodeInteger:_cellMinWidth forKey:@"PSMcellMinWidth"];
		[aCoder encodeInteger:_cellMaxWidth forKey:@"PSMcellMaxWidth"];
		[aCoder encodeInteger:_cellOptimumWidth forKey:@"PSMcellOptimumWidth"];
		[aCoder encodeInteger:_currentStep forKey:@"PSMcurrentStep"];
		[aCoder encodeBool:_isHidden forKey:@"PSMisHidden"];
		[aCoder encodeObject:partnerView forKey:@"PSMpartnerView"];
		[aCoder encodeBool:_awakenedFromNib forKey:@"PSMawakenedFromNib"];
		[aCoder encodeObject:_lastMouseDownEvent forKey:@"PSMlastMouseDownEvent"];
		[aCoder encodeObject:delegate forKey:@"PSMdelegate"];
		[aCoder encodeBool:_useOverflowMenu forKey:@"PSMuseOverflowMenu"];
		[aCoder encodeBool:_automaticallyAnimates forKey:@"PSMautomaticallyAnimates"];
		[aCoder encodeBool:_alwaysShowActiveTab forKey:@"PSMalwaysShowActiveTab"];
	}
}

- (id)initWithCoder:(NSCoder *)aDecoder 
{
	self = [super initWithCoder:aDecoder];
	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];
		if ([aDecoder allowsKeyedCoding]) {
			_cells = [[aDecoder decodeObjectForKey:@"PSMcells"] retain];
			tabView = [[aDecoder decodeObjectForKey:@"PSMtabView"] retain];
			_overflowPopUpButton = [[aDecoder decodeObjectForKey:@"PSMoverflowPopUpButton"] retain];
			_addTabButton = [[aDecoder decodeObjectForKey:@"PSMaddTabButton"] retain];
			style = [[aDecoder decodeObjectForKey:@"PSMstyle"] retain];
			_orientation = (PSMTabBarOrientation)[aDecoder decodeIntegerForKey:@"PSMorientation"];
			_canCloseOnlyTab = [aDecoder decodeBoolForKey:@"PSMcanCloseOnlyTab"];
			_disableTabClose = [aDecoder decodeBoolForKey:@"PSMdisableTabClose"];
			_hideForSingleTab = [aDecoder decodeBoolForKey:@"PSMhideForSingleTab"];
			_allowsBackgroundTabClosing = [aDecoder decodeBoolForKey:@"PSMallowsBackgroundTabClosing"];
			_allowsResizing = [aDecoder decodeBoolForKey:@"PSMallowsResizing"];
			_selectsTabsOnMouseDown = [aDecoder decodeBoolForKey:@"PSMselectsTabsOnMouseDown"];
			_showAddTabButton = [aDecoder decodeBoolForKey:@"PSMshowAddTabButton"];
			_sizeCellsToFit = [aDecoder decodeBoolForKey:@"PSMsizeCellsToFit"];
			_cellMinWidth = [aDecoder decodeIntegerForKey:@"PSMcellMinWidth"];
			_cellMaxWidth = [aDecoder decodeIntegerForKey:@"PSMcellMaxWidth"];
			_cellOptimumWidth = [aDecoder decodeIntegerForKey:@"PSMcellOptimumWidth"];
			_currentStep = [aDecoder decodeIntegerForKey:@"PSMcurrentStep"];
			_isHidden = [aDecoder decodeBoolForKey:@"PSMisHidden"];
			partnerView = [[aDecoder decodeObjectForKey:@"PSMpartnerView"] retain];
			_awakenedFromNib = [aDecoder decodeBoolForKey:@"PSMawakenedFromNib"];
			_lastMouseDownEvent = [[aDecoder decodeObjectForKey:@"PSMlastMouseDownEvent"] retain];
			_useOverflowMenu = [aDecoder decodeBoolForKey:@"PSMuseOverflowMenu"];
			_automaticallyAnimates = [aDecoder decodeBoolForKey:@"PSMautomaticallyAnimates"];
			_alwaysShowActiveTab = [aDecoder decodeBoolForKey:@"PSMalwaysShowActiveTab"];
			delegate = [[aDecoder decodeObjectForKey:@"PSMdelegate"] retain];
		}
	}
	[self setTarget:self];
	return self;
}

#pragma mark -
#pragma mark IB Palette

- (NSSize)minimumFrameSizeFromKnobPosition:(NSInteger)position {
	return NSMakeSize(100.0, 22.0);
}

- (NSSize)maximumFrameSizeFromKnobPosition:(NSInteger)knobPosition {
	return NSMakeSize(10000.0, 22.0);
}

- (void)placeView:(NSRect)newFrame {
	// this is called any time the view is resized in IB
	[self setFrame:newFrame];
	[self update:NO];
}

#pragma mark -
#pragma mark Convenience

- (void)bindPropertiesForCell:(PSMTabBarCell *)cell andTabViewItem:(NSTabViewItem *)item {
	[self _bindPropertiesForCell:cell andTabViewItem:item];

	// watch for changes in the identifier
	[item addObserver:self forKeyPath:@"identifier" options:0 context:nil];
}

- (void)_bindPropertiesForCell:(PSMTabBarCell *)cell andTabViewItem:(NSTabViewItem *)item {
	// bind the indicator to the represented object's status (if it exists)
	[[cell indicator] setHidden:YES];
	if([item identifier] != nil) {
		if([[[cell representedObject] identifier] respondsToSelector:@selector(isProcessing)]) {
			NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
			[bindingOptions setObject:NSNegateBooleanTransformerName forKey:@"NSValueTransformerName"];
			[[cell indicator] bind:@"animate" toObject:[item identifier] withKeyPath:@"isProcessing" options:nil];
			[[cell indicator] bind:@"hidden" toObject:[item identifier] withKeyPath:@"isProcessing" options:bindingOptions];
			[[item identifier] addObserver:cell forKeyPath:@"isProcessing" options:0 context:nil];
		}
	}

	// bind for the existence of an icon
	[cell setHasIcon:NO];
	if([item identifier] != nil) {
		if([[[cell representedObject] identifier] respondsToSelector:@selector(icon)]) {
			NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
			[bindingOptions setObject:NSIsNotNilTransformerName forKey:@"NSValueTransformerName"];
			[cell bind:@"hasIcon" toObject:[item identifier] withKeyPath:@"icon" options:bindingOptions];
			[[item identifier] addObserver:cell forKeyPath:@"icon" options:0 context:nil];
		}
	}

	// bind for the existence of a counter
	[cell setCount:0];
	if([item identifier] != nil) {
		if([[[cell representedObject] identifier] respondsToSelector:@selector(objectCount)]) {
			[cell bind:@"count" toObject:[item identifier] withKeyPath:@"objectCount" options:nil];
			[[item identifier] addObserver:cell forKeyPath:@"objectCount" options:0 context:nil];
		}
	}

	// bind for the color of a counter
	[cell setCountColor:nil];
	if([item identifier] != nil) {
		if([[[cell representedObject] identifier] respondsToSelector:@selector(countColor)]) {
			[cell bind:@"countColor" toObject:[item identifier] withKeyPath:@"countColor" options:nil];
			[[item identifier] addObserver:cell forKeyPath:@"countColor" options:0 context:nil];
		}
	}

	// bind for a large image
	[cell setHasLargeImage:NO];
	if([item identifier] != nil) {
		if([[[cell representedObject] identifier] respondsToSelector:@selector(largeImage)]) {
			NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
			[bindingOptions setObject:NSIsNotNilTransformerName forKey:@"NSValueTransformerName"];
			[cell bind:@"hasLargeImage" toObject:[item identifier] withKeyPath:@"largeImage" options:bindingOptions];
			[[item identifier] addObserver:cell forKeyPath:@"largeImage" options:0 context:nil];
		}
	}

	[cell setIsEdited:NO];
	if([item identifier] != nil) {
		if([[[cell representedObject] identifier] respondsToSelector:@selector(isEdited)]) {
			[cell bind:@"isEdited" toObject:[item identifier] withKeyPath:@"isEdited" options:nil];
			[[item identifier] addObserver:cell forKeyPath:@"isEdited" options:0 context:nil];
		}
	}

	// bind my string value to the label on the represented tab
	[cell bind:@"title" toObject:item withKeyPath:@"label" options:nil];
}

- (NSMutableArray *)representedTabViewItems {
	NSMutableArray *temp = [NSMutableArray arrayWithCapacity:[_cells count]];
	NSEnumerator *e = [_cells objectEnumerator];
	PSMTabBarCell *cell;
	while((cell = [e nextObject])) {
		if([cell representedObject]) {
			[temp addObject:[cell representedObject]];
		}
	}
	return temp;
}

- (id)cellForPoint:(NSPoint)point cellFrame:(NSRectPointer)outFrame {
	if([self orientation] == PSMTabBarHorizontalOrientation && !NSPointInRect(point, [self genericCellRect])) {
		return nil;
	}

	NSInteger i, cnt = [_cells count];
	for(i = 0; i < cnt; i++) {
		PSMTabBarCell *cell = [_cells objectAtIndex:i];

		if(NSPointInRect(point, [cell frame])) {
			if(outFrame) {
				*outFrame = [cell frame];
			}
			return cell;
		}
	}
	return nil;
}

- (PSMTabBarCell *)lastVisibleTab {
	NSInteger i, cellCount = [_cells count];
	for(i = 0; i < cellCount; i++) {
		if([[_cells objectAtIndex:i] isInOverflowMenu]) {
			return [_cells objectAtIndex:(i - 1)];
		}
	}
	return [_cells objectAtIndex:(cellCount - 1)];
}

- (NSInteger)numberOfVisibleTabs {
	NSUInteger i, cellCount = 0;
	PSMTabBarCell *nextCell;

	for(i = 0; i < [_cells count]; i++) {
		nextCell = [_cells objectAtIndex:i];

		if([nextCell isInOverflowMenu]) {
			break;
		}

		if(![nextCell isPlaceholder]) {
			cellCount++;
		}
	}

	return cellCount;
}

#pragma mark -
#pragma mark Accessibility

-(BOOL)accessibilityIsIgnored {
	return NO;
}

- (id)accessibilityAttributeValue:(NSString *)attribute {
	id attributeValue = nil;
	if([attribute isEqualToString: NSAccessibilityRoleAttribute]) {
		attributeValue = NSAccessibilityGroupRole;
	} else if([attribute isEqualToString: NSAccessibilityChildrenAttribute]) {
		attributeValue = NSAccessibilityUnignoredChildren(_cells);
	} else {
		attributeValue = [super accessibilityAttributeValue:attribute];
	}
	return attributeValue;
}

- (id)accessibilityHitTest:(NSPoint)point {
	id hitTestResult = self;

	NSEnumerator *enumerator = [_cells objectEnumerator];
	PSMTabBarCell *cell = nil;
	PSMTabBarCell *highlightedCell = nil;

	while(!highlightedCell && (cell = [enumerator nextObject])) {
		if([cell isHighlighted]) {
			highlightedCell = cell;
		}
	}

	if(highlightedCell) {
		hitTestResult = [highlightedCell accessibilityHitTest:point];
	}

	return hitTestResult;
}

@end