11#import "ScreenSelectionController.h" 
   13#import "OrderedDictionary.h" 
   14#import "ConnectionParams.h" 
   16@interface ScreenSelectionController (Private)
 
   17- (NSString *)keyPathForKey:(NSString *)key;
 
   24  return [
self initWithConnectionParams:params keyPath:nil];
 
   27- (id)initWithConnectionParams:(
ConnectionParams *)params keyPath:(NSString *)keyPath
 
   29  self = [
super initWithStyle:UITableViewStyleGrouped];
 
   32    _params = [params retain];
 
   33    _keyPath = (keyPath != nil ? [keyPath retain] : nil);
 
   36    _resolution_modes = [ResolutionModes() retain];
 
   39    NSUInteger idx = [_color_options
 
   40        indexForValue:[NSNumber
 
   41                          numberWithInt:[_params
 
   42                                            intForKeyPath:[
self keyPathForKey:@"colors"]]]];
 
   43    _selection_color = (idx != NSNotFound) ? idx : 0;
 
   45    idx = [_resolution_modes
 
   46        indexOfObject:ScreenResolutionDescription(
 
   48                              intForKeyPath:[
self keyPathForKey:@"screen_resolution_type"]],
 
   49                          [_params intForKeyPath:[
self keyPathForKey:@"width"]],
 
   50                          [_params intForKeyPath:[
self keyPathForKey:@"height"]])];
 
   51    _selection_resolution = (idx != NSNotFound) ? idx : 0;
 
   59  [_params autorelease];
 
   60  [_keyPath autorelease];
 
   61  [_color_options autorelease];
 
   62  [_resolution_modes autorelease];
 
   65- (NSString *)keyPathForKey:(NSString *)key
 
   68    return [_keyPath stringByAppendingFormat:@".%@", key];
 
   72#pragma mark - View lifecycle 
   74- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 
   80#pragma mark - Table view data source 
   82- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 
   87- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 
   91    return [_color_options count];
 
   92  return [_resolution_modes count] + 2; 
 
   95- (UITableViewCell *)tableView:(UITableView *)tableView
 
   96         cellForRowAtIndexPath:(NSIndexPath *)indexPath
 
   98  UITableViewCell *cell = nil;
 
   99  switch ([indexPath section])
 
  102      cell = [
self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
 
  103      [[cell textLabel] setText:[_color_options keyAtIndex:[indexPath row]]];
 
  107      if ([indexPath row] < [_resolution_modes count])
 
  109        cell = [
self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
 
  110        [[cell textLabel] setText:[_resolution_modes objectAtIndex:[indexPath row]]];
 
  113        cell = [
self tableViewCellFromIdentifier:TableCellIdentifierText];
 
  120  if ([indexPath section] == 1)
 
  122    BOOL enabled = ([_params intForKeyPath:[
self keyPathForKey:@"screen_resolution_type"]] ==
 
  123                    TSXScreenOptionCustom);
 
  124    if ([indexPath row] == [_resolution_modes count])
 
  126      int value = [_params intForKeyPath:[
self keyPathForKey:@"width"]];
 
  128      [[textCell label] setText:NSLocalizedString(@"Width", @"Custom Screen Width")];
 
  129      [[textCell textfield] setText:[NSString stringWithFormat:@"%d", value ? value : 800]];
 
  130      [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
 
  131      [[textCell label] setEnabled:enabled];
 
  132      [[textCell textfield] setEnabled:enabled];
 
  133      [[textCell textfield] setTag:1];
 
  135    else if ([indexPath row] == ([_resolution_modes count] + 1))
 
  137      int value = [_params intForKeyPath:[
self keyPathForKey:@"height"]];
 
  139      [[textCell label] setText:NSLocalizedString(@"Height", @"Custom Screen Height")];
 
  140      [[textCell textfield] setText:[NSString stringWithFormat:@"%d", value ? value : 600]];
 
  141      [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
 
  142      [[textCell label] setEnabled:enabled];
 
  143      [[textCell textfield] setEnabled:enabled];
 
  144      [[textCell textfield] setTag:2];
 
  149  if ([indexPath row] == ([indexPath section] == 0 ? _selection_color : _selection_resolution))
 
  150    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
 
  152    [cell setAccessoryType:UITableViewCellAccessoryNone];
 
  157#pragma mark - Table view delegate 
  159- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 
  162  if ([indexPath section] == 1 && [indexPath row] >= [_resolution_modes count])
 
  166  int cur_selection = ([indexPath section] == 0 ? _selection_color : _selection_resolution);
 
  167  if ([indexPath row] != cur_selection)
 
  169    [tableView deselectRowAtIndexPath:indexPath animated:NO];
 
  171    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:cur_selection
 
  172                                                   inSection:[indexPath section]];
 
  175    UITableViewCell *old_sel_cell = [tableView cellForRowAtIndexPath:oldIndexPath];
 
  176    old_sel_cell.accessoryType = UITableViewCellAccessoryNone;
 
  179    UITableViewCell *new_sel_cell = [tableView cellForRowAtIndexPath:indexPath];
 
  180    new_sel_cell.accessoryType = UITableViewCellAccessoryCheckmark;
 
  182    if ([indexPath section] == 0)
 
  186          [[_color_options valueForKey:[_color_options keyAtIndex:[indexPath row]]] intValue];
 
  189      [_params setInt:sel_value forKeyPath:[
self keyPathForKey:@"colors"]];
 
  190      _selection_color = [indexPath row];
 
  196      TSXScreenOptions mode;
 
  197      ScanScreenResolution([_resolution_modes objectAtIndex:[indexPath row]], &width, &height,
 
  199      [_params setInt:mode forKeyPath:[
self keyPathForKey:@"screen_resolution_type"]];
 
  200      if (mode != TSXScreenOptionCustom)
 
  202        [_params setInt:width forKeyPath:[
self keyPathForKey:@"width"]];
 
  203        [_params setInt:height forKeyPath:[
self keyPathForKey:@"height"]];
 
  205      _selection_resolution = [indexPath row];
 
  208      NSArray *indexPaths = [NSArray
 
  209          arrayWithObjects:[NSIndexPath indexPathForRow:[_resolution_modes count]
 
  211                           [NSIndexPath indexPathForRow:([_resolution_modes count] + 1)
 
  214      [[
self tableView] reloadRowsAtIndexPaths:indexPaths
 
  215                              withRowAnimation:UITableViewRowAnimationNone];
 
  221#pragma mark Text Field delegate 
  223- (BOOL)textFieldShouldReturn:(UITextField *)textField
 
  225  [textField resignFirstResponder];
 
  229- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
 
  232  switch ([textField tag])
 
  236      if ([[textField text] intValue] < 640)
 
  237        [textField setText:@"640"];
 
  238      [_params setInt:[[textField text] intValue] forKeyPath:[
self keyPathForKey:@"width"]];
 
  242      if ([[textField text] intValue] < 480)
 
  243        [textField setText:@"480"];
 
  244      [_params setInt:[[textField text] intValue] forKeyPath:[
self keyPathForKey:@"height"]];