author | nemo |
Fri, 02 Sep 2011 19:06:05 -0400 | |
changeset 5744 | 746de1d303d2 |
parent 5700 | f0960a88ab0e |
child 6078 | 8c0cc07731e5 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 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.delegate = self; |
|
35 |
textField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
36 |
textField.clearsOnBeginEditing = NO; |
|
37 |
textField.returnKeyType = UIReturnKeyDone; |
|
38 |
textField.adjustsFontSizeToFitWidth = YES; |
|
5700
f0960a88ab0e
savedgamesviewcontroller refactor, added icons to supportviewcontroller
koda
parents:
4976
diff
changeset
|
39 |
textField.minimumFontSize = 9; |
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; |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4211
diff
changeset
|
41 |
textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
3659 | 42 |
[textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
3697 | 43 |
|
3659 | 44 |
[self.contentView addSubview:textField]; |
3667 | 45 |
//[textField release]; |
3697 | 46 |
|
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
|
47 |
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
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
[self.contentView addSubview:titleLabel]; |
3667 | 52 |
//[titleLabel release]; |
3697 | 53 |
|
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
|
54 |
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
|
55 |
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
|
56 |
respectEditing = NO; |
3660 | 57 |
oldValue = nil; |
3659 | 58 |
} |
59 |
return self; |
|
60 |
} |
|
61 |
||
62 |
-(void) layoutSubviews { |
|
63 |
[super layoutSubviews]; |
|
64 |
||
65 |
CGRect contentRect = self.contentView.bounds; |
|
66 |
CGFloat boundsX = contentRect.origin.x; |
|
3697 | 67 |
|
3660 | 68 |
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
|
69 |
int skew = 0; |
3660 | 70 |
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
|
71 |
offset += self.imageView.frame.size.width; |
3697 | 72 |
|
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
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
|
79 |
} |
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
|
80 |
|
3782 | 81 |
textField.frame = CGRectMake(boundsX+offset+10, skew+10, 300, [UIFont labelFontSize] + 4); |
3659 | 82 |
} |
83 |
||
84 |
-(void) setSelected:(BOOL)selected animated:(BOOL)animated { |
|
85 |
[super setSelected:selected animated:animated]; |
|
86 |
// Configure the view for the selected state |
|
87 |
} |
|
88 |
||
89 |
-(void) dealloc { |
|
3667 | 90 |
self.delegate = nil; |
91 |
releaseAndNil(oldValue); |
|
92 |
releaseAndNil(titleLabel); |
|
93 |
releaseAndNil(textField); |
|
3659 | 94 |
[super dealloc]; |
95 |
} |
|
96 |
||
97 |
#pragma mark - |
|
98 |
#pragma mark textField delegate |
|
99 |
// limit the size of the field to 64 characters like in original frontend |
|
100 |
-(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
|
101 |
return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length); |
3659 | 102 |
} |
103 |
||
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
|
104 |
// allow editing only if delegate is set and conformant to protocol, and if editableOnlyWhileEditing |
3659 | 105 |
-(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
|
106 |
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
|
107 |
[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
|
108 |
(respectEditing) ? ((UITableView*)[self superview]).editing : YES; |
3659 | 109 |
} |
110 |
||
111 |
// the textfield is being modified, update the navigation controller |
|
112 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
|
3660 | 113 |
// don't interact with table below |
3659 | 114 |
((UITableView*)[self superview]).scrollEnabled = NO; |
3697 | 115 |
|
3660 | 116 |
self.oldValue = self.textField.text; |
3697 | 117 |
|
3659 | 118 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
119 |
style:UIBarButtonItemStylePlain |
|
120 |
target:self |
|
121 |
action:@selector(cancel:)]; |
|
122 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton; |
|
123 |
[cancelButton release]; |
|
3697 | 124 |
|
3659 | 125 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"") |
126 |
style:UIBarButtonItemStyleDone |
|
127 |
target:self |
|
128 |
action:@selector(save:)]; |
|
129 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
|
130 |
[saveButton release]; |
|
131 |
} |
|
132 |
||
3660 | 133 |
/* with this a field might remain in editing status even if the view moved; |
134 |
use method below instead that allows some more interaction |
|
3659 | 135 |
// don't accept 0-length strings |
136 |
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
3697 | 137 |
return ([aTextField.text length] > 0); |
3659 | 138 |
} |
3660 | 139 |
*/ |
140 |
||
141 |
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField { |
|
3697 | 142 |
return ([aTextField.text length] >= self.minimumCharacters); |
3660 | 143 |
} |
3659 | 144 |
|
145 |
// 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
|
146 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField { |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
147 |
// 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
|
148 |
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
|
149 |
[self save:aTextField]; |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
150 |
|
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
151 |
// restores default behaviour on caller |
3659 | 152 |
((UITableView*)[self superview]).scrollEnabled = YES; |
153 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem; |
|
154 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil; |
|
155 |
} |
|
156 |
||
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
|
157 |
#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
|
158 |
#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
|
159 |
// the user wants to show the keyboard |
3659 | 160 |
-(void) replyKeyboard { |
161 |
[self.textField becomeFirstResponder]; |
|
162 |
} |
|
163 |
||
164 |
// the user pressed cancel so hide keyboard |
|
165 |
-(void) cancel:(id) sender { |
|
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
166 |
// reverts any changes and performs a fake save for removing the keyboard |
3660 | 167 |
self.textField.text = self.oldValue; |
168 |
[self save:sender]; |
|
3659 | 169 |
} |
170 |
||
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
171 |
// send the value to the delegate (called before textFieldDidEndEditing) |
3659 | 172 |
-(void) save:(id) sender { |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
173 |
if (delegate == nil || [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] == NO) |
3659 | 174 |
return; |
3697 | 175 |
|
3660 | 176 |
// 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
|
177 |
if ([self textFieldShouldReturn:textField] == NO) |
3660 | 178 |
return; |
3697 | 179 |
|
3660 | 180 |
[delegate saveTextFieldValue:self.textField.text withTag:self.tag]; |
3659 | 181 |
[self.textField resignFirstResponder]; |
3660 | 182 |
self.oldValue = nil; |
3659 | 183 |
} |
184 |
||
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
|
185 |
// 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
|
186 |
-(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
|
187 |
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
|
188 |
[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
|
189 |
|
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 |
[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
|
191 |
} |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
192 |
|
3659 | 193 |
@end |