author | koda |
Sat, 25 Sep 2010 03:04:22 +0200 | |
changeset 3902 | 3aac7ca07b0e |
parent 3878 | 348f4104461d |
child 3904 | 22e4d74240e5 |
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 |
|
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
|
26 |
@synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, 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; |
|
40 |
[textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
|
3697 | 41 |
|
3659 | 42 |
[self.contentView addSubview:textField]; |
3667 | 43 |
//[textField release]; |
3697 | 44 |
|
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
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
[self.contentView addSubview:titleLabel]; |
3667 | 50 |
//[titleLabel release]; |
3697 | 51 |
|
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
|
52 |
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
|
53 |
maximumCharacters = 64; |
3660 | 54 |
oldValue = nil; |
3659 | 55 |
} |
56 |
return self; |
|
57 |
} |
|
58 |
||
59 |
-(void) layoutSubviews { |
|
60 |
[super layoutSubviews]; |
|
61 |
||
62 |
CGRect contentRect = self.contentView.bounds; |
|
63 |
CGFloat boundsX = contentRect.origin.x; |
|
3697 | 64 |
|
3660 | 65 |
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
|
66 |
int skew = 0; |
3660 | 67 |
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
|
68 |
offset += self.imageView.frame.size.width; |
3697 | 69 |
|
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 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
} |
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 |
|
3782 | 78 |
textField.frame = CGRectMake(boundsX+offset+10, skew+10, 300, [UIFont labelFontSize] + 4); |
3659 | 79 |
} |
80 |
||
81 |
-(void) setSelected:(BOOL)selected animated:(BOOL)animated { |
|
82 |
[super setSelected:selected animated:animated]; |
|
83 |
// Configure the view for the selected state |
|
84 |
} |
|
85 |
||
86 |
-(void) dealloc { |
|
3667 | 87 |
self.delegate = nil; |
88 |
releaseAndNil(oldValue); |
|
89 |
releaseAndNil(titleLabel); |
|
90 |
releaseAndNil(textField); |
|
3659 | 91 |
[super dealloc]; |
92 |
} |
|
93 |
||
94 |
#pragma mark - |
|
95 |
#pragma mark textField delegate |
|
96 |
// limit the size of the field to 64 characters like in original frontend |
|
97 |
-(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
|
98 |
return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length); |
3659 | 99 |
} |
100 |
||
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 |
// allow editing only if delegate is set and conformant to protocol |
3659 | 102 |
-(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField { |
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
|
103 |
return (delegate != nil) && [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)]; |
3659 | 104 |
} |
105 |
||
106 |
// the textfield is being modified, update the navigation controller |
|
107 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
|
3660 | 108 |
// don't interact with table below |
3659 | 109 |
((UITableView*)[self superview]).scrollEnabled = NO; |
3697 | 110 |
|
3660 | 111 |
self.oldValue = self.textField.text; |
3697 | 112 |
|
3659 | 113 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
114 |
style:UIBarButtonItemStylePlain |
|
115 |
target:self |
|
116 |
action:@selector(cancel:)]; |
|
117 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton; |
|
118 |
[cancelButton release]; |
|
3697 | 119 |
|
3659 | 120 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"") |
121 |
style:UIBarButtonItemStyleDone |
|
122 |
target:self |
|
123 |
action:@selector(save:)]; |
|
124 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
|
125 |
[saveButton release]; |
|
126 |
} |
|
127 |
||
3660 | 128 |
/* with this a field might remain in editing status even if the view moved; |
129 |
use method below instead that allows some more interaction |
|
3659 | 130 |
// don't accept 0-length strings |
131 |
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
3697 | 132 |
return ([aTextField.text length] > 0); |
3659 | 133 |
} |
3660 | 134 |
*/ |
135 |
||
136 |
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField { |
|
3697 | 137 |
return ([aTextField.text length] >= self.minimumCharacters); |
3660 | 138 |
} |
3659 | 139 |
|
140 |
// 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
|
141 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField { |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
142 |
// 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
|
143 |
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
|
144 |
[self save:aTextField]; |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
145 |
|
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
146 |
// restores default behaviour on caller |
3659 | 147 |
((UITableView*)[self superview]).scrollEnabled = YES; |
148 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem; |
|
149 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil; |
|
150 |
} |
|
151 |
||
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
|
152 |
#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
|
153 |
#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
|
154 |
// the user wants to show the keyboard |
3659 | 155 |
-(void) replyKeyboard { |
156 |
[self.textField becomeFirstResponder]; |
|
157 |
} |
|
158 |
||
159 |
// the user pressed cancel so hide keyboard |
|
160 |
-(void) cancel:(id) sender { |
|
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
161 |
// reverts any changes and performs a fake save for removing the keyboard |
3660 | 162 |
self.textField.text = self.oldValue; |
163 |
[self save:sender]; |
|
3659 | 164 |
} |
165 |
||
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
166 |
// send the value to the delegate (called before textFieldDidEndEditing) |
3659 | 167 |
-(void) save:(id) sender { |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
168 |
if (delegate == nil || [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] == NO) |
3659 | 169 |
return; |
3697 | 170 |
|
3660 | 171 |
// 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
|
172 |
if ([self textFieldShouldReturn:textField] == NO) |
3660 | 173 |
return; |
3697 | 174 |
|
3660 | 175 |
[delegate saveTextFieldValue:self.textField.text withTag:self.tag]; |
3659 | 176 |
[self.textField resignFirstResponder]; |
3660 | 177 |
self.oldValue = nil; |
3659 | 178 |
} |
179 |
||
180 |
@end |