forked from nickjs/Cappuccino-Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPTrackingController.j
215 lines (165 loc) · 5.49 KB
/
CPTrackingController.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
/*
* CPTrackingController.j
* CappuccinoExtensions
*
* Created by Nicholas Small.
* Copyright 2009-2010, 280 North, Inc.
*
* 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 "CPViewController.j"
@import "CPRadioItem.j"
CPTrackingSegmentedMode = 0;
CPTrackingRadioMode = 1;
@implementation CPTrackingController : UIViewController
{
CPArray _viewControllers;
CPViewController _currentViewController;
CPControl _trackingControl;
}
- (id)initWithViewControllers:(CPArray)viewControllers
{
if (!viewControllers)
return nil;
self = [super init];
if (self)
{
_viewControllers = viewControllers;
}
return self;
}
- (void)loadView
{
var view = [[_CPLayoutView alloc] init];
[self setView:view];
}
- (CPControl)trackingControl
{
if (_trackingControl)
return _trackingControl;
var count = [_viewControllers count];
if (!count)
return nil;
_trackingControl = [[CPSegmentedControl alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 24.0)];
[_trackingControl setSegmentCount:count];
[_trackingControl setTrackingMode:CPSegmentSwitchTrackingSelectOne];
[_trackingControl setTarget:self];
[_trackingControl setAction:@selector(_trackingControlDidChange:)];
for (var i = 0; i < count; i++)
{
var controller = _viewControllers[i],
radioItem = [controller radioItem];
[_trackingControl setWidth:32.0 forSegment:i];
if ([radioItem image])
[_trackingControl setImage:[radioItem image] forSegment:i];
else
[_trackingControl setLabel:[[radioItem title] characterAtIndex:0] forSegment:i];
}
if (_currentViewController)
[_trackingControl setSelectedSegment:[_viewControllers indexOfObject:_currentViewController]];
return _trackingControl;
}
- (void)selectViewController:(CPViewController)aController
{
[_currentViewController viewWillDisappear:NO];
[[_currentViewController view] removeFromSuperview];
[_currentViewController viewDidDisappear:NO];
_currentViewController = aController;
if (_currentViewController)
{
var view = [_currentViewController view];
[_currentViewController viewWillAppear:NO];
[view setFrame:CGRectMakeCopy([_view bounds])];
[view setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
[_view addSubview:view];
[_currentViewController viewDidAppear:NO];
}
[_trackingControl setSelectedSegment:_currentViewController ? [_viewControllers indexOfObject:_currentViewController] : nil];
}
- (void)selectViewControllerAtIndex:(int)anIndex
{
var controller = [_viewControllers objectAtIndex:anIndex];
[self selectViewController:controller];
}
- (CPViewController)selectedViewController
{
return _currentViewController;
}
- (CPViewController)visibleViewController
{
return _currentViewController;
}
// Private API
- (CPArray)_radioItems
{
var array = [CPMutableArray array];
var count = [_viewControllers count];
for (var i = 0; i < count; i++)
{
var item = [_viewControllers[i] radioItem];
[array addObject:item];
}
return array;
}
- (@action)_trackingControlDidChange:(id)aSender
{
if (!aSender)
return;
[self selectViewControllerAtIndex:[aSender selectedSegment]];
}
- (@action)_selectToolbarItem:(id)aSender
{
if (!aSender)
return;
[self selectViewControllerAtIndex:[aSender tag]];
}
@end
@implementation CPTrackingController (CPToolbarDelegate)
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
var items = [self _radioItems],
count = [items count],
array = [CPMutableArray array];
for (var i = 0; i < count; i++)
[array addObject:[items[i] title]];
return array;
}
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
{
var items = [self _radioItems],
count = [items count],
item;
for (var i = 0; i < count; i++)
{
item = items[i];
if ([item title] === anIdentifier)
break;
else
item = nil;
}
if (!item)
return nil;
var toolbarItem = [[CPToolbarItem alloc] initWithItemIdentifier:anIdentifier];
[toolbarItem setMinSize:CGSizeMake(32.0, 32.0)];
[toolbarItem setMaxSize:CGSizeMake(32.0, 32.0)];
[toolbarItem setLabel:[item title]];
[toolbarItem setImage:[item image]];
[toolbarItem setAlternateImage:[item image]];
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(_selectToolbarItem:)];
[toolbarItem setTag:[items indexOfObject:item]];
return toolbarItem;
}
@end