gfx/skia/trunk/src/views/mac/SkOptionsTableView.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial