blob: 38b6da6ce71f444eb470a9c3edcafd90f848eefc [file] [log] [blame]
yangsu@google.com12d177d2011-08-01 17:07:12 +00001#import "SkOptionsTableView.h"
2#import "SkTextFieldCell.h"
3@implementation SkOptionItem
4@synthesize fCell, fItem;
5- (void)dealloc {
6 [fCell release];
7 [super dealloc];
8}
9@end
10
11@implementation SkOptionsTableView
12@synthesize fItems;
13
14- (id)initWithCoder:(NSCoder*)coder {
15 if ((self = [super initWithCoder:coder])) {
16 self.dataSource = self;
17 self.delegate = self;
18 [self setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
19 self.fItems = [NSMutableArray array];
20 }
21 return self;
22}
23
24- (void)dealloc {
25 self.fItems = nil;
26 [super dealloc];
27}
28
29- (void) view:(SkNSView*)view didAddMenu:(const SkOSMenu*)menu {}
30- (void) view:(SkNSView*)view didUpdateMenu:(const SkOSMenu*)menu {
31 [self updateMenu:menu];
32}
33
34- (void)registerMenus:(const SkTDArray<SkOSMenu*>*)menus {
35 fMenus = menus;
36 for (NSUInteger i = 0; i < fMenus->count(); ++i) {
37 [self loadMenu:(*fMenus)[i]];
38 }
39}
40
41- (void)updateMenu:(SkOSMenu*)menu {
42 // the first menu is always assumed to be the static, the second is
43 // repopulated every time over and over again
44 int menuIndex = fMenus->find(menu);
45 if (menuIndex >= 0 && menuIndex < fMenus->count()) {
46 NSUInteger first = 0;
47 for (NSInteger i = 0; i < menuIndex; ++i) {
48 first += (*fMenus)[i]->countItems();
49 }
50 [fItems removeObjectsInRange:NSMakeRange(first, [fItems count] - first)];
51 [self loadMenu:menu];
52 }
53 [self reloadData];
54}
55
56- (NSCellStateValue)triStateToNSState:(SkOSMenu::TriState)state {
57 if (SkOSMenu::kOnState == state)
58 return NSOnState;
59 else if (SkOSMenu::kOffState == state)
60 return NSOffState;
61 else
62 return NSMixedState;
63}
64
65- (void)loadMenu:(const SkOSMenu*)menu {
66 for (int i = 0; i < menu->countItems(); ++i) {
67 const SkOSMenu::Item* item = menu->getItem(i);
68 NSString* str;
69 int index = 0;
70 NSArray* optionstrs = nil;
71
72 SkOptionItem* option = [[SkOptionItem alloc] init];
73 option.fItem = item;
74 bool state = false;
75 SkOSMenu::TriState tristate;
76 switch (item->getType()) {
77 case SkOSMenu::kAction_Type:
78 option.fCell = [self createAction];
79 break;
80 case SkOSMenu::kList_Type:
81 optionstrs = [[NSString stringWithUTF8String:item->getEvent()->findString(SkOSMenu::List_Items_Str)]
82 componentsSeparatedByString:[NSString stringWithUTF8String:SkOSMenu::Delimiter]];
83 item->getEvent()->findS32(item->getSlotName(), &index);
84 option.fCell = [self createList:optionstrs current:index];
85 break;
86 case SkOSMenu::kSlider_Type:
87 SkScalar min, max, value;
88 item->getEvent()->findScalar(SkOSMenu::Slider_Min_Scalar, &min);
89 item->getEvent()->findScalar(SkOSMenu::Slider_Max_Scalar, &max);
90 item->getEvent()->findScalar(item->getSlotName(), &value);
91 option.fCell = [self createSlider:value
92 min:min
93 max:max];
94 break;
95 case SkOSMenu::kSwitch_Type:
96 item->getEvent()->findBool(item->getSlotName(), &state);
97 option.fCell = [self createSwitch:(BOOL)state];
98 break;
99 case SkOSMenu::kTriState_Type:
100 item->getEvent()->findS32(item->getSlotName(), (int*)&tristate);
101 option.fCell = [self createTriState:[self triStateToNSState:tristate]];
102 break;
103 case SkOSMenu::kTextField_Type:
104 str = [NSString stringWithUTF8String:item->getEvent()->findString(item->getSlotName())];
105 option.fCell = [self createTextField:str];
106 break;
107 default:
108 break;
109 }
110 [fItems addObject:option];
111 [option release];
112 }
113}
114
115- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
116 return [self.fItems count];
117}
118
119- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
120 int columnIndex = [tableView columnWithIdentifier:[tableColumn identifier]];
121 if (columnIndex == 0)
122 return [NSString stringWithUTF8String:((SkOptionItem*)[fItems objectAtIndex:row]).fItem->getLabel()];
123 else
124 return nil;
125}
126
127- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
128 if (tableColumn) {
129 int columnIndex = [tableView columnWithIdentifier:[tableColumn identifier]];
130 if (columnIndex == 1)
131 return [((SkOptionItem*)[fItems objectAtIndex:row]).fCell copy];
132 else
133 return [[[SkTextFieldCell alloc] init] autorelease];
134 }
135 return nil;
136}
137
138- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
139 int columnIndex = [tableView columnWithIdentifier:[tableColumn identifier]];
140 if (columnIndex == 1) {
141 SkOptionItem* option = (SkOptionItem*)[self.fItems objectAtIndex:row];
142 NSCell* storedCell = option.fCell;
143 const SkOSMenu::Item* item = option.fItem;
144 switch (item->getType()) {
145 case SkOSMenu::kAction_Type:
146 break;
147 case SkOSMenu::kList_Type:
148 [cell selectItemAtIndex:[(NSPopUpButtonCell*)storedCell indexOfSelectedItem]];
149 break;
150 case SkOSMenu::kSlider_Type:
151 [cell setFloatValue:[storedCell floatValue]];
152 break;
153 case SkOSMenu::kSwitch_Type:
154 [cell setTitle:storedCell.title];
155 [cell setState:[(NSButtonCell*)storedCell state]];
156 break;
157 case SkOSMenu::kTextField_Type:
158 if ([[storedCell stringValue] length] > 0)
159 [cell setStringValue:[storedCell stringValue]];
160 break;
161 case SkOSMenu::kTriState_Type:
162 [cell setTitle:storedCell.title];
163 [cell setState:[(NSButtonCell*)storedCell state]];
164 break;
165 default:
166 break;
167 }
168 }
169 else {
170 [(SkTextFieldCell*)cell setEditable:NO];
171 }
172}
173
174- (void)tableView:(NSTableView *)tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
175 int columnIndex = [tableView columnWithIdentifier:[tableColumn identifier]];
176 if (columnIndex == 1) {
177 SkOptionItem* option = (SkOptionItem*)[self.fItems objectAtIndex:row];
178 NSCell* cell = option.fCell;
179 const SkOSMenu::Item* item = option.fItem;
180 switch (item->getType()) {
181 case SkOSMenu::kAction_Type:
182 item->postEvent();
183 break;
184 case SkOSMenu::kList_Type:
185 [(NSPopUpButtonCell*)cell selectItemAtIndex:[anObject intValue]];
186 item->postEventWithInt([anObject intValue]);
187 break;
188 case SkOSMenu::kSlider_Type:
189 [cell setFloatValue:[anObject floatValue]];
190 item->postEventWithScalar([anObject floatValue]);
191 break;
192 case SkOSMenu::kSwitch_Type:
193 [cell setState:[anObject boolValue]];
194 item->postEventWithBool([anObject boolValue]);
195 break;
196 case SkOSMenu::kTextField_Type:
197 if ([anObject length] > 0) {
198 [cell setStringValue:anObject];
199 item->postEventWithString([anObject UTF8String]);
200 }
201 break;
202 case SkOSMenu::kTriState_Type:
203 [cell setState:[anObject intValue]];
204 item->postEventWithInt([anObject intValue]);
205 break;
206 default:
207 break;
208 }
209 }
210}
211
212- (NSCell*)createAction{
213 NSButtonCell* cell = [[[NSButtonCell alloc] init] autorelease];
214 [cell setTitle:@""];
215 [cell setButtonType:NSMomentaryPushInButton];
216 [cell setBezelStyle:NSSmallSquareBezelStyle];
217 return cell;
218}
219
220- (NSCell*)createList:(NSArray*)items current:(int)index {
221 NSPopUpButtonCell* cell = [[[NSPopUpButtonCell alloc] init] autorelease];
222 [cell addItemsWithTitles:items];
223 [cell selectItemAtIndex:index];
224 [cell setArrowPosition:NSPopUpArrowAtBottom];
225 [cell setBezelStyle:NSSmallSquareBezelStyle];
226 return cell;
227}
228
229- (NSCell*)createSegmented:(NSArray*)items current:(int)index {
230 NSSegmentedCell* cell = [[[NSSegmentedCell alloc] init] autorelease];
231 [cell setSegmentStyle:NSSegmentStyleSmallSquare];
232 [cell setSegmentCount:[items count]];
233 NSUInteger i = 0;
234 for (NSString* label in items) {
235 [cell setLabel:label forSegment:i];
236 ++i;
237 }
238 [cell setSelectedSegment:index];
239 return cell;
240}
241
242- (NSCell*)createSlider:(float)value min:(float)min max:(float)max {
243 NSSliderCell* cell = [[[NSSliderCell alloc] init] autorelease];
244 [cell setFloatValue:value];
245 [cell setMinValue:min];
246 [cell setMaxValue:max];
247 return cell;
248}
249
250- (NSCell*)createSwitch:(BOOL)state {
251 NSButtonCell* cell = [[[NSButtonCell alloc] init] autorelease];
252 [cell setTitle:(state) ? @"On" : @"Off"];
253 [cell setState:state];
254 [cell setButtonType:NSSwitchButton];
255 return cell;
256}
257
258- (NSCell*)createTextField:(NSString*)placeHolder; {
259 SkTextFieldCell* cell = [[[SkTextFieldCell alloc] init] autorelease];
260 [cell setEditable:YES];
261 [cell setStringValue:@""];
262 [cell setPlaceholderString:placeHolder];
263 return cell;
264}
265
266- (NSCell*)createTriState:(NSCellStateValue)state {
267 NSButtonCell* cell = [[[NSButtonCell alloc] init] autorelease];
268 if (NSOnState == state)
269 [cell setTitle:@"On"];
270 else if (NSOffState == state)
271 [cell setTitle:@"Off"];
272 else
273 [cell setTitle:@"Mixed"];
274 [cell setAllowsMixedState:TRUE];
275 [cell setState:(NSInteger)state];
276 [cell setButtonType:NSSwitchButton];
277 return cell;
278}
279@end