bsalomon@google.com | f75b84e | 2011-09-29 14:58:28 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 9 | #import "SkOptionsTableView.h" |
| 10 | #import "SkTextFieldCell.h" |
| 11 | @implementation SkOptionItem |
| 12 | @synthesize fCell, fItem; |
| 13 | - (void)dealloc { |
| 14 | [fCell release]; |
| 15 | [super dealloc]; |
| 16 | } |
| 17 | @end |
| 18 | |
| 19 | @implementation SkOptionsTableView |
| 20 | @synthesize fItems; |
| 21 | |
| 22 | - (id)initWithCoder:(NSCoder*)coder { |
| 23 | if ((self = [super initWithCoder:coder])) { |
| 24 | self.dataSource = self; |
| 25 | self.delegate = self; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 26 | fMenus = NULL; |
| 27 | fShowKeys = YES; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 28 | [self setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; |
| 29 | self.fItems = [NSMutableArray array]; |
| 30 | } |
| 31 | return self; |
| 32 | } |
| 33 | |
| 34 | - (void)dealloc { |
| 35 | self.fItems = nil; |
| 36 | [super dealloc]; |
| 37 | } |
| 38 | |
| 39 | - (void) view:(SkNSView*)view didAddMenu:(const SkOSMenu*)menu {} |
| 40 | - (void) view:(SkNSView*)view didUpdateMenu:(const SkOSMenu*)menu { |
| 41 | [self updateMenu:menu]; |
| 42 | } |
| 43 | |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 44 | - (IBAction)toggleKeyEquivalents:(id)sender { |
| 45 | fShowKeys = !fShowKeys; |
| 46 | NSMenuItem* item = (NSMenuItem*)sender; |
| 47 | [item setState:fShowKeys]; |
| 48 | [self reloadData]; |
| 49 | } |
| 50 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 51 | - (void)registerMenus:(const SkTDArray<SkOSMenu*>*)menus { |
| 52 | fMenus = menus; |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 53 | for (int i = 0; i < fMenus->count(); ++i) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 54 | [self loadMenu:(*fMenus)[i]]; |
| 55 | } |
| 56 | } |
| 57 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 58 | - (void)updateMenu:(const SkOSMenu*)menu { |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 59 | // the first menu is always assumed to be the static, the second is |
| 60 | // repopulated every time over and over again |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 61 | |
| 62 | // seems pretty weird that we have to get rid of the const'ness here, |
| 63 | // but trying to propagate the const'ness through all the way to the fMenus |
| 64 | // vector was a non-starter. |
| 65 | |
| 66 | int menuIndex = fMenus->find(const_cast<SkOSMenu *>(menu)); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 67 | if (menuIndex >= 0 && menuIndex < fMenus->count()) { |
| 68 | NSUInteger first = 0; |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 69 | for (int i = 0; i < menuIndex; ++i) { |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 70 | first += (*fMenus)[i]->getCount(); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 71 | } |
| 72 | [fItems removeObjectsInRange:NSMakeRange(first, [fItems count] - first)]; |
| 73 | [self loadMenu:menu]; |
| 74 | } |
| 75 | [self reloadData]; |
| 76 | } |
| 77 | |
| 78 | - (NSCellStateValue)triStateToNSState:(SkOSMenu::TriState)state { |
| 79 | if (SkOSMenu::kOnState == state) |
| 80 | return NSOnState; |
| 81 | else if (SkOSMenu::kOffState == state) |
| 82 | return NSOffState; |
| 83 | else |
| 84 | return NSMixedState; |
| 85 | } |
| 86 | |
| 87 | - (void)loadMenu:(const SkOSMenu*)menu { |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 88 | const SkOSMenu::Item* menuitems[menu->getCount()]; |
| 89 | menu->getItems(menuitems); |
| 90 | for (int i = 0; i < menu->getCount(); ++i) { |
| 91 | const SkOSMenu::Item* item = menuitems[i]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 92 | SkOptionItem* option = [[SkOptionItem alloc] init]; |
| 93 | option.fItem = item; |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 94 | |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 95 | if (SkOSMenu::kList_Type == item->getType()) { |
| 96 | int index = 0, count = 0; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 97 | SkOSMenu::FindListItemCount(*item->getEvent(), &count); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 98 | NSMutableArray* optionstrs = [[NSMutableArray alloc] initWithCapacity:count]; |
mike@reedtribe.org | 8c0f3d8 | 2011-10-30 16:25:09 +0000 | [diff] [blame] | 99 | SkAutoTDeleteArray<SkString> ada(new SkString[count]); |
| 100 | SkString* options = ada.get(); |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 101 | SkOSMenu::FindListItems(*item->getEvent(), options); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 102 | for (int i = 0; i < count; ++i) |
| 103 | [optionstrs addObject:[NSString stringWithUTF8String:options[i].c_str()]]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 104 | SkOSMenu::FindListIndex(*item->getEvent(), item->getSlotName(), &index); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 105 | option.fCell = [self createList:optionstrs current:index]; |
| 106 | [optionstrs release]; |
| 107 | } |
| 108 | else { |
| 109 | bool state = false; |
| 110 | SkString str; |
| 111 | SkOSMenu::TriState tristate; |
| 112 | switch (item->getType()) { |
| 113 | case SkOSMenu::kAction_Type: |
| 114 | option.fCell = [self createAction]; |
| 115 | break; |
| 116 | case SkOSMenu::kSlider_Type: |
| 117 | SkScalar min, max, value; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 118 | SkOSMenu::FindSliderValue(*item->getEvent(), item->getSlotName(), &value); |
| 119 | SkOSMenu::FindSliderMin(*item->getEvent(), &min); |
| 120 | SkOSMenu::FindSliderMax(*item->getEvent(), &max); |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 121 | option.fCell = [self createSlider:value |
| 122 | min:min |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 123 | max:max]; |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 124 | break; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 125 | case SkOSMenu::kSwitch_Type: |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 126 | SkOSMenu::FindSwitchState(*item->getEvent(), item->getSlotName(), &state); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 127 | option.fCell = [self createSwitch:(BOOL)state]; |
| 128 | break; |
| 129 | case SkOSMenu::kTriState_Type: |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 130 | SkOSMenu::FindTriState(*item->getEvent(), item->getSlotName(), &tristate); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 131 | option.fCell = [self createTriState:[self triStateToNSState:tristate]]; |
| 132 | break; |
| 133 | case SkOSMenu::kTextField_Type: |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 134 | SkOSMenu::FindText(*item->getEvent(),item->getSlotName(), &str); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 135 | option.fCell = [self createTextField:[NSString stringWithUTF8String:str.c_str()]]; |
| 136 | break; |
| 137 | default: |
| 138 | break; |
| 139 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 140 | } |
| 141 | [fItems addObject:option]; |
| 142 | [option release]; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { |
| 147 | return [self.fItems count]; |
| 148 | } |
| 149 | |
| 150 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 151 | NSInteger columnIndex = [tableView columnWithIdentifier:[tableColumn identifier]]; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 152 | if (columnIndex == 0) { |
| 153 | const SkOSMenu::Item* item = ((SkOptionItem*)[fItems objectAtIndex:row]).fItem; |
| 154 | NSString* label = [NSString stringWithUTF8String:item->getLabel()]; |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 155 | if (fShowKeys) |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 156 | return [NSString stringWithFormat:@"%@ (%c)", label, item->getKeyEquivalent()]; |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 157 | else |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 158 | return label; |
| 159 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 160 | else |
| 161 | return nil; |
| 162 | } |
| 163 | |
| 164 | - (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { |
| 165 | if (tableColumn) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 166 | NSInteger columnIndex = [tableView columnWithIdentifier:[tableColumn identifier]]; |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 167 | if (columnIndex == 1) |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 168 | return [((SkOptionItem*)[fItems objectAtIndex:row]).fCell copy]; |
| 169 | else |
| 170 | return [[[SkTextFieldCell alloc] init] autorelease]; |
| 171 | } |
| 172 | return nil; |
| 173 | } |
| 174 | |
| 175 | - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 176 | NSInteger columnIndex = [tableView columnWithIdentifier:[tableColumn identifier]]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 177 | if (columnIndex == 1) { |
| 178 | SkOptionItem* option = (SkOptionItem*)[self.fItems objectAtIndex:row]; |
| 179 | NSCell* storedCell = option.fCell; |
| 180 | const SkOSMenu::Item* item = option.fItem; |
| 181 | switch (item->getType()) { |
| 182 | case SkOSMenu::kAction_Type: |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 183 | break; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 184 | case SkOSMenu::kList_Type: |
| 185 | [cell selectItemAtIndex:[(NSPopUpButtonCell*)storedCell indexOfSelectedItem]]; |
| 186 | break; |
| 187 | case SkOSMenu::kSlider_Type: |
| 188 | [cell setFloatValue:[storedCell floatValue]]; |
| 189 | break; |
| 190 | case SkOSMenu::kSwitch_Type: |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 191 | [cell setState:[(NSButtonCell*)storedCell state]]; |
| 192 | break; |
| 193 | case SkOSMenu::kTextField_Type: |
| 194 | if ([[storedCell stringValue] length] > 0) |
| 195 | [cell setStringValue:[storedCell stringValue]]; |
| 196 | break; |
| 197 | case SkOSMenu::kTriState_Type: |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 198 | [cell setState:[(NSButtonCell*)storedCell state]]; |
| 199 | break; |
| 200 | default: |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | else { |
| 205 | [(SkTextFieldCell*)cell setEditable:NO]; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | - (void)tableView:(NSTableView *)tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 210 | NSInteger columnIndex = [tableView columnWithIdentifier:[tableColumn identifier]]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 211 | if (columnIndex == 1) { |
| 212 | SkOptionItem* option = (SkOptionItem*)[self.fItems objectAtIndex:row]; |
| 213 | NSCell* cell = option.fCell; |
| 214 | const SkOSMenu::Item* item = option.fItem; |
| 215 | switch (item->getType()) { |
| 216 | case SkOSMenu::kAction_Type: |
| 217 | item->postEvent(); |
| 218 | break; |
| 219 | case SkOSMenu::kList_Type: |
| 220 | [(NSPopUpButtonCell*)cell selectItemAtIndex:[anObject intValue]]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 221 | item->setInt([anObject intValue]); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 222 | break; |
| 223 | case SkOSMenu::kSlider_Type: |
| 224 | [cell setFloatValue:[anObject floatValue]]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 225 | item->setScalar([anObject floatValue]); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 226 | break; |
| 227 | case SkOSMenu::kSwitch_Type: |
| 228 | [cell setState:[anObject boolValue]]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 229 | item->setBool([anObject boolValue]); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 230 | break; |
| 231 | case SkOSMenu::kTextField_Type: |
| 232 | if ([anObject length] > 0) { |
| 233 | [cell setStringValue:anObject]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 234 | item->setString([anObject UTF8String]); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 235 | } |
| 236 | break; |
| 237 | case SkOSMenu::kTriState_Type: |
| 238 | [cell setState:[anObject intValue]]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 239 | item->setTriState((SkOSMenu::TriState)[anObject intValue]); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 240 | break; |
| 241 | default: |
| 242 | break; |
| 243 | } |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 244 | item->postEvent(); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | - (NSCell*)createAction{ |
| 249 | NSButtonCell* cell = [[[NSButtonCell alloc] init] autorelease]; |
| 250 | [cell setTitle:@""]; |
| 251 | [cell setButtonType:NSMomentaryPushInButton]; |
| 252 | [cell setBezelStyle:NSSmallSquareBezelStyle]; |
| 253 | return cell; |
| 254 | } |
| 255 | |
| 256 | - (NSCell*)createList:(NSArray*)items current:(int)index { |
| 257 | NSPopUpButtonCell* cell = [[[NSPopUpButtonCell alloc] init] autorelease]; |
| 258 | [cell addItemsWithTitles:items]; |
| 259 | [cell selectItemAtIndex:index]; |
| 260 | [cell setArrowPosition:NSPopUpArrowAtBottom]; |
| 261 | [cell setBezelStyle:NSSmallSquareBezelStyle]; |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 262 | return cell; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 263 | } |
| 264 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 265 | - (NSCell*)createSlider:(float)value min:(float)min max:(float)max { |
| 266 | NSSliderCell* cell = [[[NSSliderCell alloc] init] autorelease]; |
| 267 | [cell setFloatValue:value]; |
| 268 | [cell setMinValue:min]; |
| 269 | [cell setMaxValue:max]; |
| 270 | return cell; |
| 271 | } |
| 272 | |
| 273 | - (NSCell*)createSwitch:(BOOL)state { |
| 274 | NSButtonCell* cell = [[[NSButtonCell alloc] init] autorelease]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 275 | [cell setState:state]; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 276 | [cell setTitle:@""]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 277 | [cell setButtonType:NSSwitchButton]; |
| 278 | return cell; |
| 279 | } |
| 280 | |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 281 | - (NSCell*)createTextField:(NSString*)placeHolder { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 282 | SkTextFieldCell* cell = [[[SkTextFieldCell alloc] init] autorelease]; |
| 283 | [cell setEditable:YES]; |
| 284 | [cell setStringValue:@""]; |
| 285 | [cell setPlaceholderString:placeHolder]; |
| 286 | return cell; |
| 287 | } |
| 288 | |
| 289 | - (NSCell*)createTriState:(NSCellStateValue)state { |
| 290 | NSButtonCell* cell = [[[NSButtonCell alloc] init] autorelease]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 291 | [cell setAllowsMixedState:TRUE]; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 292 | [cell setTitle:@""]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 293 | [cell setState:(NSInteger)state]; |
| 294 | [cell setButtonType:NSSwitchButton]; |
| 295 | return cell; |
| 296 | } |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 297 | @end |