-
Notifications
You must be signed in to change notification settings - Fork 2
/
TNiTunesTabView.j
806 lines (641 loc) · 23 KB
/
TNiTunesTabView.j
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
/*
* TNiTunesTabView.j
* AppKit
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* Modified by Antoine Mercadal for the needs of Archipel Project
* Copyright 2010 Antoine Mercadal
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPColor.j>
@import <AppKit/CPImage.j>
@import <AppKit/CPTextField.j>
@import <AppKit/CPView.j>
@import "TNiTunesTabViewItem.j"
/*
Places tabs on top with a bezeled border.
@global
@group TNiTunesTabViewType
*/
TNTopTabsBezelBorder = 0;
/*
Displays no tabs and has a bezeled border.
@global
@group TNiTunesTabViewType
*/
TNNoTabsBezelBorder = 4;
/*
Has no tabs and displays a line border.
@global
@group TNiTunesTabViewType
*/
TNNoTabsLineBorder = 5;
/*
Displays no tabs and no border.
@global
@group TNiTunesTabViewType
*/
TNNoTabsNoBorder = 6;
var TNiTunesTabViewBezelBorderLeftImage = nil,
TNiTunesTabViewBackgroundCenterImage = nil,
TNiTunesTabViewBezelBorderRightImage = nil,
TNiTunesTabViewBezelBorderColor = nil,
TNiTunesTabViewBezelBorderBackgroundColor = nil;
var LEFT_INSET = 7.0,
RIGHT_INSET = 7.0,
TAB_WIDTH = 110.0;
var TNiTunesTabViewDidSelectTabViewItemSelector = 1,
TNiTunesTabViewShouldSelectTabViewItemSelector = 2,
TNiTunesTabViewWillSelectTabViewItemSelector = 4,
TNiTunesTabViewDidChangeNumberOfTabViewItemsSelector = 8;
/*!
@ingroup appkit
@class TNiTunesTabView
This class represents a view that has multiple subviews (TNiTunesTabViewItem) presented as individual tabs.
Only one TNiTunesTabViewItem is shown at a time, and other TNiTunesTabViewItems can be made visible
(one at a time) by clicking on the TNiTunesTabViewItem's tab at the top of the tab view.
THe currently selected TNiTunesTabViewItem is the view that is displayed.
*/
@implementation TNiTunesTabView : CPView
{
CPArray _tabViewItems;
CPView _auxiliaryView;
CPView _backgroundView;
CPView _contentView;
CPView _labelsView;
CPView _separatorView;
id _delegate;
TNiTunesTabViewItem _selectedTabViewItem;
TNiTunesTabViewType _tabViewType;
unsigned _delegateSelectors;
}
/*
@ignore
*/
+ (CPColor)bezelBorderColor
{
return TNiTunesTabViewBezelBorderColor;
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if (self)
{
_tabViewType = TNTopTabsBezelBorder;
_tabViewItems = [];
}
return self;
}
- (void)viewDidMoveToWindow
{
if (_tabViewType != TNTopTabsBezelBorder || _labelsView)
return;
[self _createBezelBorder];
[self layoutSubviews];
}
- (void)_createBezelBorder
{
if (_labelsView)
return;
var bounds = [self bounds];
bounds.size.width += LEFT_INSET;
bounds.origin.x -= RIGHT_INSET;
_labelsView = [[_TNiTunesTabLabelsView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(bounds), 0.0)];
[_labelsView setTabView:self];
[_labelsView setAutoresizingMask:CPViewWidthSizable];
[self addSubview:_labelsView];
}
/*
Lays out the subviews
@ignore
*/
- (void)layoutSubviews
{
if (_tabViewType == TNTopTabsBezelBorder)
{
var backgroundRect = [self bounds],
labelsViewHeight = [_TNiTunesTabLabelsView height];
backgroundRect.origin.y += labelsViewHeight;
backgroundRect.size.height -= labelsViewHeight;
[_backgroundView setFrame:backgroundRect];
var auxiliaryViewHeight = 5.0;
if (_auxiliaryView)
{
auxiliaryViewHeight = CGRectGetHeight([_auxiliaryView frame]);
[_auxiliaryView setFrame:CGRectMake(LEFT_INSET, labelsViewHeight, CGRectGetWidth(backgroundRect) - LEFT_INSET - RIGHT_INSET, auxiliaryViewHeight)];
}
[_separatorView setFrame:CGRectMake(LEFT_INSET, labelsViewHeight + auxiliaryViewHeight, CGRectGetWidth(backgroundRect) - LEFT_INSET - RIGHT_INSET, 1.0)];
}
// TNNoTabsNoBorder
[_contentView setFrame:[self contentRect]];
}
// Adding and Removing Tabs
/*!
Adds a TNiTunesTabViewItem to the tab view.
@param aTabViewItem the item to add
*/
- (void)addTabViewItem:(TNiTunesTabViewItem)aTabViewItem
{
[self insertTabViewItem:aTabViewItem atIndex:[_tabViewItems count]];
}
/*!
Inserts a TNiTunesTabViewItem into the tab view
at the specified index.
@param aTabViewItem the item to insert
@param anIndex the index for the item
*/
- (void)insertTabViewItem:(TNiTunesTabViewItem)aTabViewItem atIndex:(unsigned)anIndex
{
if (!_labelsView)
[self _createBezelBorder];
[_tabViewItems insertObject:aTabViewItem atIndex:anIndex];
[_labelsView tabView:self didAddTabViewItem:aTabViewItem];
[aTabViewItem setTabView:self];
if ([_tabViewItems count] == 1)
[self selectFirstTabViewItem:self];
if (_delegateSelectors & TNiTunesTabViewDidChangeNumberOfTabViewItemsSelector)
[_delegate tabViewDidChangeNumberOfTabViewItems:self];
}
/*!
Removes the specified tab view item from the tab view.
@param aTabViewItem the item to remove
*/
- (void)removeTabViewItem:(TNiTunesTabViewItem)aTabViewItem
{
var index = [self indexOfTabViewItem:aTabViewItem];
[_tabViewItems removeObject:aTabViewItem];
[_labelsView tabView:self didRemoveTabViewItemAtIndex:index];
[aTabViewItem setTabView:nil];
if (_delegateSelectors & TNiTunesTabViewDidChangeNumberOfTabViewItemsSelector)
[_delegate tabViewDidChangeNumberOfTabViewItems:self];
}
// Accessing Tabs
/*!
Returns the index of the specified item
@param aTabViewItem the item to find the index for
*/
- (int)indexOfTabViewItem:(TNiTunesTabViewItem)aTabViewItem
{
return [_tabViewItems indexOfObjectIdenticalTo:aTabViewItem];
}
/*!
Returns the index of the TNiTunesTabViewItem with the specified identifier.
@param anIdentifier the identifier of the item
*/
- (int)indexOfTabViewItemWithIdentifier:(CPString)anIdentifier
{
var index = 0,
count = [_tabViewItems count];
for (; index < count; ++index)
if ([[_tabViewItems[index] identifier] isEqual:anIdentifier])
return index;
return index;
}
/*!
Returns the number of items in the tab view.
*/
- (unsigned)numberOfTabViewItems
{
return [_tabViewItems count];
}
/*!
Returns the TNiTunesTabViewItem at the specified index.
*/
- (TNiTunesTabViewItem)tabViewItemAtIndex:(unsigned)anIndex
{
return _tabViewItems[anIndex];
}
/*!
Returns the array of items that backs this tab view.
*/
- (CPArray)tabViewItems
{
return _tabViewItems;
}
// Selecting a Tab
/*!
Sets the first tab view item in the array to be displayed to the user.
@param aSender the object making this request
*/
- (void)selectFirstTabViewItem:(id)aSender
{
var count = [_tabViewItems count];
if (count)
[self selectTabViewItemAtIndex:0];
}
/*!
Sets the last tab view item in the array to be displayed to the user.
@param aSender the object making this request
*/
- (void)selectLastTabViewItem:(id)aSender
{
var count = [_tabViewItems count];
if (count)
[self selectTabViewItemAtIndex:count - 1];
}
/*!
Sets the next tab item in the array to be displayed.
@param aSender the object making this request
*/
- (void)selectNextTabViewItem:(id)aSender
{
if (!_selectedTabViewItem)
return;
var index = [self indexOfTabViewItem:_selectedTabViewItem],
count = [_tabViewItems count];
[self selectTabViewItemAtIndex:index + 1 % count];
}
/*!
Selects the previous item in the array for display.
@param aSender the object making this request
*/
- (void)selectPreviousTabViewItem:(id)aSender
{
if (!_selectedTabViewItem)
return;
var index = [self indexOfTabViewItem:_selectedTabViewItem],
count = [_tabViewItems count];
[self selectTabViewItemAtIndex:index == 0 ? count : index - 1];
}
/*!
Displays the specified item in the tab view.
@param aTabViewItem the item to display
*/
- (void)selectTabViewItem:(TNiTunesTabViewItem)aTabViewItem
{
if ((_delegateSelectors & TNiTunesTabViewShouldSelectTabViewItemSelector) && ![_delegate tabView:self shouldSelectTabViewItem:aTabViewItem])
return;
if (_delegateSelectors & TNiTunesTabViewWillSelectTabViewItemSelector)
[_delegate tabView:self willSelectTabViewItem:aTabViewItem];
if (_selectedTabViewItem)
{
_selectedTabViewItem._tabState = TNBackgroundTab;
[_labelsView tabView:self didChangeStateOfTabViewItem:_selectedTabViewItem];
}
_selectedTabViewItem = aTabViewItem;
_selectedTabViewItem._tabState = TNSelectedTab;
var _previousContentView = _contentView;
_contentView = [_selectedTabViewItem view];
if (_previousContentView !== _contentView)
{
[_previousContentView removeFromSuperview];
[_contentView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
[self addSubview:_contentView];
}
var _previousAuxiliaryView = _auxiliaryView;
_auxiliaryView = [_selectedTabViewItem auxiliaryView];
if (_previousAuxiliaryView !== _auxiliaryView)
{
[_previousAuxiliaryView removeFromSuperview];
[_auxiliaryView setAutoresizingMask:CPViewWidthSizable];
[self addSubview:_auxiliaryView];
}
[_labelsView tabView:self didChangeStateOfTabViewItem:_selectedTabViewItem];
[self layoutSubviews];
if (_delegateSelectors & TNiTunesTabViewDidSelectTabViewItemSelector)
[_delegate tabView:self didSelectTabViewItem:aTabViewItem];
}
/*!
Selects the item at the specified index.
@param anIndex the index of the item to display.
*/
- (void)selectTabViewItemAtIndex:(unsigned)anIndex
{
[self selectTabViewItem:_tabViewItems[anIndex]];
}
/*!
Returns the current item being displayed.
*/
- (TNiTunesTabViewItem)selectedTabViewItem
{
return _selectedTabViewItem;
}
//
/*!
Sets the tab view type.
@param aTabViewType the view type
*/
- (void)setTabViewType:(TNiTunesTabViewType)aTabViewType
{
if (_tabViewType == aTabViewType)
return;
_tabViewType = aTabViewType;
if (_tabViewType == TNNoTabsBezelBorder || _tabViewType == TNNoTabsLineBorder || _tabViewType == TNNoTabsNoBorder)
[_labelsView removeFromSuperview];
else if (_labelsView && ![_labelsView superview])
[self addSubview:_labelsView];
if (_tabViewType == TNNoTabsLineBorder || _tabViewType == TNNoTabsNoBorder)
[_backgroundView removeFromSuperview];
else if (_backgroundView && ![_backgroundView superview])
[self addSubview:_backgroundView];
[self layoutSubviews];
}
/*!
Returns the tab view type.
*/
- (TNiTunesTabViewType)tabViewType
{
return _tabViewType;
}
// Determining the Size
/*!
Returns the content rectangle.
*/
- (CGRect)contentRect
{
var contentRect = CGRectMakeCopy([self bounds]);
if (_tabViewType == TNTopTabsBezelBorder)
{
var labelsViewHeight = 33.0,
auxiliaryViewHeight = _auxiliaryView ? CGRectGetHeight([_auxiliaryView frame]) : 0.0,
separatorViewHeight = 0.0;
contentRect.origin.y += labelsViewHeight + auxiliaryViewHeight + separatorViewHeight;
contentRect.size.height -= labelsViewHeight + auxiliaryViewHeight + separatorViewHeight * 2.0;
}
return contentRect;
}
/*!
Returns the receiver's delegate.
*/
- (id)delegate
{
return _delegate;
}
/*!
Sets the delegate for this tab view.
@param aDelegate the tab view's delegate
*/
- (void)setDelegate:(id)aDelegate
{
if (_delegate == aDelegate)
return;
_delegate = aDelegate;
_delegateSelectors = 0;
if ([_delegate respondsToSelector:@selector(tabView:shouldSelectTabViewItem:)])
_delegateSelectors |= TNiTunesTabViewShouldSelectTabViewItemSelector;
if ([_delegate respondsToSelector:@selector(tabView:willSelectTabViewItem:)])
_delegateSelectors |= TNiTunesTabViewWillSelectTabViewItemSelector;
if ([_delegate respondsToSelector:@selector(tabView:didSelectTabViewItem:)])
_delegateSelectors |= TNiTunesTabViewDidSelectTabViewItemSelector;
if ([_delegate respondsToSelector:@selector(tabViewDidChangeNumberOfTabViewItems:)])
_delegateSelectors |= TNiTunesTabViewDidChangeNumberOfTabViewItemsSelector;
}
- (void)mouseDown:(CPEvent)anEvent
{
var location = [_labelsView convertPoint:[anEvent locationInWindow] fromView:nil],
tabViewItem = [_labelsView representedTabViewItemAtPoint:location];
if (tabViewItem)
[self selectTabViewItem:tabViewItem];
}
@end
var TNiTunesTabViewItemsKey = "TNiTunesTabViewItemsKey",
TNiTunesTabViewSelectedItemKey = "TNiTunesTabViewSelectedItemKey",
TNiTunesTabViewTypeKey = "TNiTunesTabViewTypeKey",
TNiTunesTabViewDelegateKey = "TNiTunesTabViewDelegateKey";
@implementation TNiTunesTabView (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
if (self = [super initWithCoder:aCoder])
{
_tabViewType = [aCoder decodeIntForKey:TNiTunesTabViewTypeKey];
_tabViewItems = [];
// FIXME: this is somewhat hacky
[self _createBezelBorder];
var items = [aCoder decodeObjectForKey:TNiTunesTabViewItemsKey];
for (var i = 0; items && i < items.length; i++)
[self insertTabViewItem:items[i] atIndex:i];
var selected = [aCoder decodeObjectForKey:TNiTunesTabViewSelectedItemKey];
if (selected)
[self selectTabViewItem:selected];
[self setDelegate:[aCoder decodeObjectForKey:TNiTunesTabViewDelegateKey]];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
var actualSubviews = _subviews;
_subviews = [];
[super encodeWithCoder:aCoder];
_subviews = actualSubviews;
[aCoder encodeObject:_tabViewItems forKey:TNiTunesTabViewItemsKey];;
[aCoder encodeObject:_selectedTabViewItem forKey:TNiTunesTabViewSelectedItemKey];
[aCoder encodeInt:_tabViewType forKey:TNiTunesTabViewTypeKey];
[aCoder encodeConditionalObject:_delegate forKey:TNiTunesTabViewDelegateKey];
}
@end
var _TNiTunesTabLabelsViewBackgroundColor = nil,
_TNiTunesTabLabelsViewInsideMargin = 10.0,
_TNiTunesTabLabelsViewOutsideMargin = 15.0;
/* @ignore */
@implementation _TNiTunesTabLabelsView : CPScrollView
{
TNiTunesTabView _tabView @accessors(property=tabView);
CPArray _tabLabels;
CPView _documentView;
CPButton _buttonScrollLeft;
CPButton _buttonScrollRight;
}
+ (float)height
{
return 26.0;
}
- (id)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
_tabLabels = [];
var bundle = [CPBundle bundleForClass:[self class]],
imageButtonLeft = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"button-left.png"] size:CPSizeMake(14.0, 14.0)],
imageButtonLeftPressed = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"button-left-pressed.png"] size:CPSizeMake(14.0, 14.0)],
imageButtonRight = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"button-right.png"] size:CPSizeMake(14.0, 14.0)],
imageButtonRightPressed = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"button-right-pressed.png"] size:CPSizeMake(14.0, 14.0)];
[self setBackgroundColor:[CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"TNiTunesTabViewLabelBackground.png"]]]]
[self setFrameSize:CGSizeMake(CGRectGetWidth(aFrame), 33.0)];
_documentView = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, [self contentSize].height)];
[self setDocumentView:_documentView];
[self setHasHorizontalScroller:NO];
[self setHasVerticalScroller:NO];
[self setHorizontalLineScroll:50.0];
_buttonScrollRight = [[CPButton alloc] initWithFrame:CPRectMake(CGRectGetWidth(aFrame) - 21, 0, 14, 15)];
[_buttonScrollRight setBordered:NO];
[_buttonScrollRight setImage:imageButtonRight]
[_buttonScrollRight setValue:imageButtonRight forThemeAttribute:@"image"];
[_buttonScrollRight setValue:imageButtonRightPressed forThemeAttribute:@"image" inState:CPThemeStateHighlighted];
[_buttonScrollRight setTarget:self];
[_buttonScrollRight setAction:@selector(moveRight:)];
[_buttonScrollRight setContinuous:YES];
_buttonScrollLeft = [[CPButton alloc] initWithFrame:CPRectMake(1, 0, 14, 15)];
[_buttonScrollLeft setBordered:NO];
[_buttonScrollLeft setImage:imageButtonLeft]
[_buttonScrollLeft setTarget:self];
[_buttonScrollLeft setValue:imageButtonLeft forThemeAttribute:@"image"];
[_buttonScrollLeft setValue:imageButtonLeftPressed forThemeAttribute:@"image" inState:CPThemeStateHighlighted];
[_buttonScrollLeft setTarget:self];
[_buttonScrollLeft setAction:@selector(moveLeft:)];
[_buttonScrollLeft setContinuous:YES];
[self addSubview:_buttonScrollRight];
[self addSubview:_buttonScrollLeft];
}
return self;
}
- (void)reflectScrolledClipView:(CPClipView)aClipView
{
[super reflectScrolledClipView:aClipView];
if ([_horizontalScroller isEnabled])
{
[_buttonScrollLeft setHidden:NO];
[_buttonScrollRight setHidden:NO];
[_buttonScrollRight setFrame:CPRectMake(CGRectGetWidth([self frame]) - 21, 0, 14, 15)]; // why the CPViewMinXMargin doesn't work here ??
}
else
{
[_buttonScrollLeft setHidden:YES];
[_buttonScrollRight setHidden:YES];
}
}
- (void)tabView:(TNiTunesTabView)aTabView didAddTabViewItem:(TNiTunesTabViewItem)aTabViewItem
{
var label = [[_TNiTunesTabLabel alloc] initWithFrame:CGRectMakeZero()];
[label setTabViewItem:aTabViewItem];
_tabLabels.push(label);
[[self documentView] addSubview:label];
[self layoutSubviews];
}
- (void)tabView:(TNiTunesTabView)aTabView didRemoveTabViewItemAtIndex:(unsigned)index
{
var label = _tabLabels[index];
[_tabLabels removeObjectAtIndex:index];
[label setTabViewItem:nil];
[label removeFromSuperview];
[self layoutSubviews];
}
- (void)tabView:(TNiTunesTabView)aTabView didChangeStateOfTabViewItem:(TNiTunesTabViewItem)aTabViewItem
{
[_tabLabels[[aTabView indexOfTabViewItem:aTabViewItem]] setTabState:[aTabViewItem tabState]];
}
- (TNiTunesTabViewItem)representedTabViewItemAtPoint:(CGPoint)aPoint
{
var index = 0,
count = _tabLabels.length;
for (; index < count; ++index)
{
var label = _tabLabels[index];
if (CGRectContainsPoint([label frame], aPoint))
return [label tabViewItem];
}
return nil;
}
- (void)layoutSubviews
{
var count = _tabLabels.length,
width = TAB_WIDTH,
x = 15;
for (var i = 0; i < count; i++)
{
var label = _tabLabels[i];
width = MAX([[label stringValue] sizeWithFont:[label font]].width, width);
}
for (var i = 0; i < count; i++)
{
var label = _tabLabels[i],
frame = CGRectMake(x, 15.0, width, 20.0);
[label setFrame:frame];
x = CGRectGetMaxX(frame);
x += 1.0;
}
if (_documentView)
{
var currentFrame = [_documentView frame];
currentFrame.size.width = x + 14;
[_documentView setFrame:currentFrame];
}
}
- (void)setFrameSize:(CGSize)aSize
{
if (CGSizeEqualToSize([self frame].size, aSize))
return;
[super setFrameSize:aSize];
[self layoutSubviews];
}
@end
var _TNiTunesTabLabelBackgroundColor = nil,
_TNiTunesTabLabelSelectedBackgroundColor = nil;
/* @ignore */
@implementation _TNiTunesTabLabel : CPView
{
TNiTunesTabViewItem _tabViewItem;
CPTextField _labelField;
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if (self)
{
_labelField = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[_labelField setAlignment:CPCenterTextAlignment];
[_labelField setFrame:CGRectMake(5.0, 0.0, CGRectGetWidth(aFrame) - 10.0, 20.0)];
[_labelField setAutoresizingMask:CPViewWidthSizable];
[_labelField setFont:[CPFont boldSystemFontOfSize:11.0]];
[self addSubview:_labelField];
[self setTabState:TNBackgroundTab];
}
return self;
}
- (void)setTabState:(CPTabState)aTabState
{
var bundle = [CPBundle bundleForClass:[self class]];
if (!_TNiTunesTabLabelBackgroundColor)
_TNiTunesTabLabelBackgroundColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:
[
[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"TNiTunesTabLabelBackgroundLeft.png"] size:CGSizeMake(2.0, 18.0)],
[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"TNiTunesTabLabelBackgroundCenter.png"] size:CGSizeMake(1.0, 18.0)],
[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"TNiTunesTabLabelBackgroundRight.png"] size:CGSizeMake(2.0, 18.0)]
] isVertical:NO]];
if (!_TNiTunesTabLabelSelectedBackgroundColor)
_TNiTunesTabLabelSelectedBackgroundColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:
[
[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"TNiTunesTabLabelSelectedLeft.png"] size:CGSizeMake(2.0, 18.0)],
[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"TNiTunesTabLabelSelectedCenter.png"] size:CGSizeMake(1.0, 18.0)],
[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"TNiTunesTabLabelSelectedRight.png"] size:CGSizeMake(2.0, 18.0)]
] isVertical:NO]];
[self setBackgroundColor:aTabState == TNSelectedTab ? _TNiTunesTabLabelSelectedBackgroundColor :_TNiTunesTabLabelBackgroundColor];
[_labelField setTextColor:aTabState == TNSelectedTab ? [CPColor blackColor] : [CPColor whiteColor]];
}
- (void)setTabViewItem:(TNiTunesTabViewItem)aTabViewItem
{
_tabViewItem = aTabViewItem;
[self update];
}
- (TNiTunesTabViewItem)tabViewItem
{
return _tabViewItem;
}
- (void)update
{
[_labelField setStringValue:[_tabViewItem label]];
}
- (CPString)stringValue
{
return [_labelField stringValue];
}
- (CPString)font
{
return [_labelField font];
}
@end