FreeRDP
Loading...
Searching...
No Matches
ScreenSelectionController.m
1/*
2 controller for screen settings selection
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 "ScreenSelectionController.h"
12#import "Utils.h"
13#import "OrderedDictionary.h"
14#import "ConnectionParams.h"
15
16@interface ScreenSelectionController (Private)
17- (NSString *)keyPathForKey:(NSString *)key;
18@end
19
20@implementation ScreenSelectionController
21
22- (id)initWithConnectionParams:(ConnectionParams *)params
23{
24 return [self initWithConnectionParams:params keyPath:nil];
25}
26
27- (id)initWithConnectionParams:(ConnectionParams *)params keyPath:(NSString *)keyPath
28{
29 self = [super initWithStyle:UITableViewStyleInsetGrouped];
30 if (self)
31 {
32 _params = [params retain];
33 _keyPath = (keyPath != nil ? [keyPath retain] : nil);
34
35 _color_options = (OrderedDictionary *)[SelectionForColorSetting() retain];
36 _resolution_modes = [ResolutionModes() retain];
37
38 // init current selections
39 NSUInteger idx = [_color_options
40 indexForValue:[NSNumber
41 numberWithInt:[_params
42 intForKeyPath:[self keyPathForKey:@"colors"]]]];
43 _selection_color = (idx != NSNotFound) ? idx : 0;
44
45 idx = [_resolution_modes
46 indexOfObject:ScreenResolutionDescription(
47 [_params
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;
52 }
53 return self;
54}
55
56- (void)dealloc
57{
58 [super dealloc];
59 [_params autorelease];
60 [_keyPath autorelease];
61 [_color_options autorelease];
62 [_resolution_modes autorelease];
63}
64
65- (NSString *)keyPathForKey:(NSString *)key
66{
67 if (_keyPath)
68 return [_keyPath stringByAppendingFormat:@".%@", key];
69 return key;
70}
71
72#pragma mark - View lifecycle
73
74- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
75{
76 // Return YES for supported orientations
77 return YES;
78}
79
80#pragma mark - Table view data source
81
82- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
83{
84 return 2;
85}
86
87- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
88{
89 // Return the number of rows in the section.
90 if (section == 0)
91 return [_color_options count];
92 return [_resolution_modes count] + 2; // +2 for custom width/height input fields
93}
94
95- (UITableViewCell *)tableView:(UITableView *)tableView
96 cellForRowAtIndexPath:(NSIndexPath *)indexPath
97{
98 UITableViewCell *cell = nil;
99 switch ([indexPath section])
100 {
101 case 0:
102 cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
103 [[cell textLabel] setText:[_color_options keyAtIndex:[indexPath row]]];
104 break;
105
106 case 1:
107 if ([indexPath row] < [_resolution_modes count])
108 {
109 cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
110 [[cell textLabel] setText:[_resolution_modes objectAtIndex:[indexPath row]]];
111 }
112 else
113 cell = [self tableViewCellFromIdentifier:TableCellIdentifierText];
114 break;
115
116 default:
117 break;
118 }
119
120 if ([indexPath section] == 1)
121 {
122 BOOL enabled = ([_params intForKeyPath:[self keyPathForKey:@"screen_resolution_type"]] ==
123 TSXScreenOptionCustom);
124 if ([indexPath row] == [_resolution_modes count])
125 {
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 setEnabled:enabled];
132 [[textCell textfield] setTag:1];
133 }
134 else if ([indexPath row] == ([_resolution_modes count] + 1))
135 {
136 int value = [_params intForKeyPath:[self keyPathForKey:@"height"]];
138 [[textCell label] setText:NSLocalizedString(@"Height", @"Custom Screen Height")];
139 [[textCell textfield] setText:[NSString stringWithFormat:@"%d", value ? value : 600]];
140 [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
141 [textCell setEnabled:enabled];
142 [[textCell textfield] setTag:2];
143 }
144 }
145
146 // set default checkmark
147 if ([indexPath row] == ([indexPath section] == 0 ? _selection_color : _selection_resolution))
148 [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
149 else
150 [cell setAccessoryType:UITableViewCellAccessoryNone];
151
152 return cell;
153}
154
155#pragma mark - Table view delegate
156
157- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
158{
159 // custom width/height cells are not selectable
160 if ([indexPath section] == 1 && [indexPath row] >= [_resolution_modes count])
161 return;
162
163 // has selection change?
164 NSInteger cur_selection = ([indexPath section] == 0 ? _selection_color : _selection_resolution);
165 if ([indexPath row] != cur_selection)
166 {
167 [tableView deselectRowAtIndexPath:indexPath animated:NO];
168
169 NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:cur_selection
170 inSection:[indexPath section]];
171
172 // clear old checkmark
173 UITableViewCell *old_sel_cell = [tableView cellForRowAtIndexPath:oldIndexPath];
174 old_sel_cell.accessoryType = UITableViewCellAccessoryNone;
175
176 // set new checkmark
177 UITableViewCell *new_sel_cell = [tableView cellForRowAtIndexPath:indexPath];
178 new_sel_cell.accessoryType = UITableViewCellAccessoryCheckmark;
179
180 if ([indexPath section] == 0)
181 {
182 // get value from color dictionary
183 int sel_value =
184 [[_color_options valueForKey:[_color_options keyAtIndex:[indexPath row]]] intValue];
185
186 // update selection index and params value
187 [_params setInt:sel_value forKeyPath:[self keyPathForKey:@"colors"]];
188 _selection_color = [indexPath row];
189 }
190 else
191 {
192 // update selection index and params value
193 int width, height;
194 TSXScreenOptions mode;
195 ScanScreenResolution([_resolution_modes objectAtIndex:[indexPath row]], &width, &height,
196 &mode);
197 [_params setInt:mode forKeyPath:[self keyPathForKey:@"screen_resolution_type"]];
198 if (mode != TSXScreenOptionCustom)
199 {
200 [_params setInt:width forKeyPath:[self keyPathForKey:@"width"]];
201 [_params setInt:height forKeyPath:[self keyPathForKey:@"height"]];
202 }
203 _selection_resolution = [indexPath row];
204
205 // refresh width/height edit fields if custom selection changed
206 NSArray *indexPaths = [NSArray
207 arrayWithObjects:[NSIndexPath indexPathForRow:[_resolution_modes count]
208 inSection:1],
209 [NSIndexPath indexPathForRow:([_resolution_modes count] + 1)
210 inSection:1],
211 nil];
212 [[self tableView] reloadRowsAtIndexPaths:indexPaths
213 withRowAnimation:UITableViewRowAnimationNone];
214 }
215 }
216}
217
218#pragma mark -
219#pragma mark Text Field delegate
220
221- (BOOL)textFieldShouldReturn:(UITextField *)textField
222{
223 [textField resignFirstResponder];
224 return NO;
225}
226
227- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
228{
229
230 switch ([textField tag])
231 {
232 // update resolution settings (and check for invalid input)
233 case 1:
234 if ([[textField text] intValue] < 640)
235 [textField setText:@"640"];
236 [_params setInt:[[textField text] intValue] forKeyPath:[self keyPathForKey:@"width"]];
237 break;
238
239 case 2:
240 if ([[textField text] intValue] < 480)
241 [textField setText:@"480"];
242 [_params setInt:[[textField text] intValue] forKeyPath:[self keyPathForKey:@"height"]];
243 break;
244
245 default:
246 break;
247 }
248 return YES;
249}
250
251@end