author | koda |
Sun, 10 Oct 2010 03:16:25 +0200 | |
changeset 3941 | 017b2b31e1c6 |
parent 3904 | 22e4d74240e5 |
child 4208 | dd54999c2822 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 03/07/2010. |
|
19 |
*/ |
|
20 |
||
3659 | 21 |
|
22 |
#import "EditableCellView.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
||
25 |
@implementation EditableCellView |
|
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
26 |
@synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, respectEditing, oldValue; |
3659 | 27 |
|
28 |
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { |
|
29 |
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { |
|
30 |
delegate = nil; |
|
3697 | 31 |
|
3659 | 32 |
textField = [[UITextField alloc] initWithFrame:CGRectZero]; |
33 |
textField.backgroundColor = [UIColor clearColor]; |
|
34 |
textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
|
35 |
textField.delegate = self; |
|
36 |
textField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
37 |
textField.clearsOnBeginEditing = NO; |
|
38 |
textField.returnKeyType = UIReturnKeyDone; |
|
39 |
textField.adjustsFontSizeToFitWidth = YES; |
|
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
40 |
textField.userInteractionEnabled = YES; |
3659 | 41 |
[textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
3697 | 42 |
|
3659 | 43 |
[self.contentView addSubview:textField]; |
3667 | 44 |
//[textField release]; |
3697 | 45 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
46 |
titleLabel = [[UILabel alloc] init]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
47 |
titleLabel.textAlignment = UITextAlignmentLeft; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
48 |
titleLabel.backgroundColor = [UIColor clearColor]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
49 |
titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
50 |
[self.contentView addSubview:titleLabel]; |
3667 | 51 |
//[titleLabel release]; |
3697 | 52 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
53 |
minimumCharacters = 1; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
54 |
maximumCharacters = 64; |
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
55 |
respectEditing = NO; |
3660 | 56 |
oldValue = nil; |
3659 | 57 |
} |
58 |
return self; |
|
59 |
} |
|
60 |
||
61 |
-(void) layoutSubviews { |
|
62 |
[super layoutSubviews]; |
|
63 |
||
64 |
CGRect contentRect = self.contentView.bounds; |
|
65 |
CGFloat boundsX = contentRect.origin.x; |
|
3697 | 66 |
|
3660 | 67 |
int offset = 0; |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
68 |
int skew = 0; |
3660 | 69 |
if (self.imageView != nil) |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
70 |
offset += self.imageView.frame.size.width; |
3697 | 71 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
72 |
if ([self.titleLabel.text length] == 0) |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
73 |
titleLabel.frame = CGRectZero; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
74 |
else { |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
75 |
titleLabel.frame = CGRectMake(boundsX+offset+10, 10, 100, [UIFont labelFontSize] + 4); |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
76 |
offset += 100; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
77 |
skew +=2; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
78 |
} |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
79 |
|
3782 | 80 |
textField.frame = CGRectMake(boundsX+offset+10, skew+10, 300, [UIFont labelFontSize] + 4); |
3659 | 81 |
} |
82 |
||
83 |
-(void) setSelected:(BOOL)selected animated:(BOOL)animated { |
|
84 |
[super setSelected:selected animated:animated]; |
|
85 |
// Configure the view for the selected state |
|
86 |
} |
|
87 |
||
88 |
-(void) dealloc { |
|
3667 | 89 |
self.delegate = nil; |
90 |
releaseAndNil(oldValue); |
|
91 |
releaseAndNil(titleLabel); |
|
92 |
releaseAndNil(textField); |
|
3659 | 93 |
[super dealloc]; |
94 |
} |
|
95 |
||
96 |
#pragma mark - |
|
97 |
#pragma mark textField delegate |
|
98 |
// limit the size of the field to 64 characters like in original frontend |
|
99 |
-(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
100 |
return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length); |
3659 | 101 |
} |
102 |
||
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
103 |
// allow editing only if delegate is set and conformant to protocol, and if editableOnlyWhileEditing |
3659 | 104 |
-(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField { |
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
105 |
return (delegate != nil) && |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
106 |
[delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] && |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
107 |
(respectEditing) ? ((UITableView*)[self superview]).editing : YES; |
3659 | 108 |
} |
109 |
||
110 |
// the textfield is being modified, update the navigation controller |
|
111 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
|
3660 | 112 |
// don't interact with table below |
3659 | 113 |
((UITableView*)[self superview]).scrollEnabled = NO; |
3697 | 114 |
|
3660 | 115 |
self.oldValue = self.textField.text; |
3697 | 116 |
|
3659 | 117 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
118 |
style:UIBarButtonItemStylePlain |
|
119 |
target:self |
|
120 |
action:@selector(cancel:)]; |
|
121 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton; |
|
122 |
[cancelButton release]; |
|
3697 | 123 |
|
3659 | 124 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"") |
125 |
style:UIBarButtonItemStyleDone |
|
126 |
target:self |
|
127 |
action:@selector(save:)]; |
|
128 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
|
129 |
[saveButton release]; |
|
130 |
} |
|
131 |
||
3660 | 132 |
/* with this a field might remain in editing status even if the view moved; |
133 |
use method below instead that allows some more interaction |
|
3659 | 134 |
// don't accept 0-length strings |
135 |
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
3697 | 136 |
return ([aTextField.text length] > 0); |
3659 | 137 |
} |
3660 | 138 |
*/ |
139 |
||
140 |
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField { |
|
3697 | 141 |
return ([aTextField.text length] >= self.minimumCharacters); |
3660 | 142 |
} |
3659 | 143 |
|
144 |
// the textfield has been modified, tell the delegate to do something |
|
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
145 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField { |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
146 |
// this forces a save when user selects a new field |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
147 |
if ([self.textField.text isEqualToString:self.oldValue] == NO) |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
148 |
[self save:aTextField]; |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
149 |
|
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
150 |
// restores default behaviour on caller |
3659 | 151 |
((UITableView*)[self superview]).scrollEnabled = YES; |
152 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem; |
|
153 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil; |
|
154 |
} |
|
155 |
||
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
156 |
#pragma mark - |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
157 |
#pragma mark instance methods |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
158 |
// the user wants to show the keyboard |
3659 | 159 |
-(void) replyKeyboard { |
160 |
[self.textField becomeFirstResponder]; |
|
161 |
} |
|
162 |
||
163 |
// the user pressed cancel so hide keyboard |
|
164 |
-(void) cancel:(id) sender { |
|
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
165 |
// reverts any changes and performs a fake save for removing the keyboard |
3660 | 166 |
self.textField.text = self.oldValue; |
167 |
[self save:sender]; |
|
3659 | 168 |
} |
169 |
||
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
170 |
// send the value to the delegate (called before textFieldDidEndEditing) |
3659 | 171 |
-(void) save:(id) sender { |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
172 |
if (delegate == nil || [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] == NO) |
3659 | 173 |
return; |
3697 | 174 |
|
3660 | 175 |
// don't save if the textfield is invalid |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
176 |
if ([self textFieldShouldReturn:textField] == NO) |
3660 | 177 |
return; |
3697 | 178 |
|
3660 | 179 |
[delegate saveTextFieldValue:self.textField.text withTag:self.tag]; |
3659 | 180 |
[self.textField resignFirstResponder]; |
3660 | 181 |
self.oldValue = nil; |
3659 | 182 |
} |
183 |
||
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
184 |
// when field is editable only when the tableview is editable, resign responder when exiting editing mode |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
185 |
-(void) willTransitionToState:(UITableViewCellStateMask)state { |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
186 |
if (respectEditing && state == UITableViewCellStateDefaultMask) |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
187 |
[self save:nil]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
188 |
|
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
189 |
[super willTransitionToState:state]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
190 |
} |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
191 |
|
3659 | 192 |
@end |