FreeRDP
EditorBaseController.m
1 /*
2  Basic interface for settings editors
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9  */
10 
11 #import "EditorBaseController.h"
12 
13 @interface EditorBaseController ()
14 
15 @end
16 
17 NSString *TableCellIdentifierText = @"cellIdText";
18 NSString *TableCellIdentifierSecretText = @"cellIdSecretText";
19 NSString *TableCellIdentifierYesNo = @"cellIdYesNo";
20 NSString *TableCellIdentifierSelection = @"cellIdSelection";
21 NSString *TableCellIdentifierSubEditor = @"cellIdSubEditor";
22 NSString *TableCellIdentifierMultiChoice = @"cellIdMultiChoice";
23 NSString *TableCellIdentifierButton = @"cellIdButton";
24 
25 @implementation EditorBaseController
26 
27 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
28 {
29  return YES;
30 }
31 
32 #pragma mark - Create table view cells
33 - (UITableViewCell *)tableViewCellFromIdentifier:(NSString *)identifier
34 {
35  // try to reuse a cell
36  UITableViewCell *cell = [[self tableView] dequeueReusableCellWithIdentifier:identifier];
37  if (cell != nil)
38  return cell;
39 
40  // we have to create a new cell
41  if ([identifier isEqualToString:TableCellIdentifierText])
42  {
43  [[NSBundle mainBundle] loadNibNamed:@"EditTextTableViewCell" owner:self options:nil];
44  cell = _textTableViewCell;
45  _textTableViewCell = nil;
46  }
47  else if ([identifier isEqualToString:TableCellIdentifierSecretText])
48  {
49  [[NSBundle mainBundle] loadNibNamed:@"EditSecretTextTableViewCell" owner:self options:nil];
50  cell = _secretTextTableViewCell;
51  _secretTextTableViewCell = nil;
52  }
53  else if ([identifier isEqualToString:TableCellIdentifierYesNo])
54  {
55  [[NSBundle mainBundle] loadNibNamed:@"EditFlagTableViewCell" owner:self options:nil];
56  cell = _flagTableViewCell;
57  _flagTableViewCell = nil;
58  }
59  else if ([identifier isEqualToString:TableCellIdentifierSelection])
60  {
61  [[NSBundle mainBundle] loadNibNamed:@"EditSelectionTableViewCell" owner:self options:nil];
62  cell = _selectionTableViewCell;
63  _selectionTableViewCell = nil;
64  }
65  else if ([identifier isEqualToString:TableCellIdentifierSubEditor])
66  {
67  [[NSBundle mainBundle] loadNibNamed:@"EditSubEditTableViewCell" owner:self options:nil];
68  cell = _subEditTableViewCell;
69  _subEditTableViewCell = nil;
70  }
71  else if ([identifier isEqualToString:TableCellIdentifierButton])
72  {
73  [[NSBundle mainBundle] loadNibNamed:@"EditButtonTableViewCell" owner:self options:nil];
74  cell = _buttonTableViewCell;
75  _buttonTableViewCell = nil;
76  }
77  else if ([identifier isEqualToString:TableCellIdentifierMultiChoice])
78  {
79  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
80  reuseIdentifier:identifier] autorelease];
81  }
82  else
83  {
84  NSAssert(false, @"Unknown table cell identifier");
85  }
86 
87  return cell;
88 }
89 
90 #pragma mark - Utility functions
91 - (void)adjustEditTextTableViewCell:(EditTextTableViewCell *)cell
92 {
93  UILabel *label = [cell label];
94  UITextField *textField = [cell textfield];
95 
96  // adjust label
97  CGFloat width = [[label text] sizeWithFont:[label font]].width;
98  CGRect frame = [label frame];
99  CGFloat delta = width - frame.size.width;
100  frame.size.width = width;
101  [label setFrame:frame];
102 
103  // adjust text field
104  frame = [textField frame];
105  frame.origin.x += delta;
106  frame.size.width -= delta;
107  [textField setFrame:frame];
108 }
109 
110 @end