OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import "chrome/browser/ui/cocoa/edit_search_engine_cocoa_controller.h" | |
6 | |
7 #include "app/l10n_util_mac.h" | |
8 #include "app/resource_bundle.h" | |
9 #include "base/logging.h" | |
10 #import "base/mac/mac_util.h" | |
11 #include "base/string16.h" | |
12 #include "base/sys_string_conversions.h" | |
13 #include "chrome/browser/search_engines/template_url.h" | |
14 #include "grit/app_resources.h" | |
15 #include "grit/generated_resources.h" | |
16 #include "grit/theme_resources.h" | |
17 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | |
18 | |
19 namespace { | |
20 | |
21 void ShiftOriginY(NSView* view, CGFloat amount) { | |
22 NSPoint origin = [view frame].origin; | |
23 origin.y += amount; | |
24 [view setFrameOrigin:origin]; | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 @implementation EditSearchEngineCocoaController | |
30 | |
31 - (id)initWithProfile:(Profile*)profile | |
32 delegate:(EditSearchEngineControllerDelegate*)delegate | |
33 templateURL:(const TemplateURL*)url { | |
34 DCHECK(profile); | |
35 NSString* nibpath = [base::mac::MainAppBundle() | |
36 pathForResource:@"EditSearchEngine" | |
37 ofType:@"nib"]; | |
38 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { | |
39 profile_ = profile; | |
40 templateURL_ = url; | |
41 controller_.reset( | |
42 new EditSearchEngineController(templateURL_, delegate, profile_)); | |
43 } | |
44 return self; | |
45 } | |
46 | |
47 - (void)awakeFromNib { | |
48 DCHECK([self window]); | |
49 DCHECK_EQ(self, [[self window] delegate]); | |
50 | |
51 // Make sure the url description field fits the text in it. | |
52 CGFloat descriptionShift = [GTMUILocalizerAndLayoutTweaker | |
53 sizeToFitFixedWidthTextField:urlDescriptionField_]; | |
54 | |
55 // Move the label container above the url description. | |
56 ShiftOriginY(labelContainer_, descriptionShift); | |
57 // There was no way via view containment to use a helper view to move all | |
58 // the textfields and images at once, most move them all on their own so | |
59 // they stay above the url description. | |
60 ShiftOriginY(nameField_, descriptionShift); | |
61 ShiftOriginY(keywordField_, descriptionShift); | |
62 ShiftOriginY(urlField_, descriptionShift); | |
63 ShiftOriginY(nameImage_, descriptionShift); | |
64 ShiftOriginY(keywordImage_, descriptionShift); | |
65 ShiftOriginY(urlImage_, descriptionShift); | |
66 | |
67 // Resize the containing box for the name/keyword/url fields/images since it | |
68 // also contains the url description (which just grew). | |
69 [[fieldAndImageContainer_ contentView] setAutoresizesSubviews:NO]; | |
70 NSRect rect = [fieldAndImageContainer_ frame]; | |
71 rect.size.height += descriptionShift; | |
72 [fieldAndImageContainer_ setFrame:rect]; | |
73 [[fieldAndImageContainer_ contentView] setAutoresizesSubviews:YES]; | |
74 | |
75 // Resize the window. | |
76 NSWindow* window = [self window]; | |
77 NSSize windowDelta = NSMakeSize(0, descriptionShift); | |
78 [GTMUILocalizerAndLayoutTweaker | |
79 resizeWindowWithoutAutoResizingSubViews:window | |
80 delta:windowDelta]; | |
81 | |
82 ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); | |
83 goodImage_.reset([bundle.GetNativeImageNamed(IDR_INPUT_GOOD) retain]); | |
84 badImage_.reset([bundle.GetNativeImageNamed(IDR_INPUT_ALERT) retain]); | |
85 if (templateURL_) { | |
86 // Defaults to |..._NEW_WINDOW_TITLE|. | |
87 [window setTitle:l10n_util::GetNSString( | |
88 IDS_SEARCH_ENGINES_EDITOR_EDIT_WINDOW_TITLE)]; | |
89 [nameField_ setStringValue: | |
90 base::SysWideToNSString(templateURL_->short_name())]; | |
91 [keywordField_ setStringValue: | |
92 base::SysWideToNSString(templateURL_->keyword())]; | |
93 [urlField_ setStringValue: | |
94 base::SysWideToNSString(templateURL_->url()->DisplayURL())]; | |
95 [urlField_ setEnabled:(templateURL_->prepopulate_id() == 0)]; | |
96 } | |
97 // When creating a new keyword, this will mark the fields as "invalid" and | |
98 // will not let the user save. If this is an edit, then this will set all | |
99 // the images to the "valid" state. | |
100 [self validateFields]; | |
101 } | |
102 | |
103 // When the window closes, clean ourselves up. | |
104 - (void)windowWillClose:(NSNotification*)notif { | |
105 [self autorelease]; | |
106 } | |
107 | |
108 // Performs the logic of closing the window. If we are a sheet, then it ends the | |
109 // modal session; otherwise, it closes the window. | |
110 - (void)doClose { | |
111 if ([[self window] isSheet]) { | |
112 [NSApp endSheet:[self window]]; | |
113 } else { | |
114 [[self window] close]; | |
115 } | |
116 } | |
117 | |
118 - (IBAction)cancel:(id)sender { | |
119 [self doClose]; | |
120 } | |
121 | |
122 - (IBAction)save:(id)sender { | |
123 DCHECK([self validateFields]); | |
124 string16 title = base::SysNSStringToUTF16([nameField_ stringValue]); | |
125 string16 keyword = base::SysNSStringToUTF16([keywordField_ stringValue]); | |
126 std::string url = base::SysNSStringToUTF8([urlField_ stringValue]); | |
127 controller_->AcceptAddOrEdit(title, keyword, url); | |
128 [self doClose]; | |
129 } | |
130 | |
131 // Delegate method for the text fields. | |
132 | |
133 - (void)controlTextDidChange:(NSNotification*)notif { | |
134 [self validateFields]; | |
135 } | |
136 | |
137 - (void)controlTextDidEndEditing:(NSNotification*)notif { | |
138 [self validateFields]; | |
139 } | |
140 | |
141 // Private -------------------------------------------------------------------- | |
142 | |
143 // Sets the appropriate image and tooltip based on a boolean |valid|. | |
144 - (void)setIsValid:(BOOL)valid | |
145 toolTip:(int)messageID | |
146 forImageView:(NSImageView*)imageView | |
147 textField:(NSTextField*)textField { | |
148 NSImage* image = (valid) ? goodImage_ : badImage_; | |
149 [imageView setImage:image]; | |
150 | |
151 NSString* toolTip = nil; | |
152 if (!valid) | |
153 toolTip = l10n_util::GetNSString(messageID); | |
154 [textField setToolTip:toolTip]; | |
155 [imageView setToolTip:toolTip]; | |
156 } | |
157 | |
158 // This sets the image state for all the controls and enables or disables the | |
159 // done button. Returns YES if all the fields are valid. | |
160 - (BOOL)validateFields { | |
161 string16 title = base::SysNSStringToUTF16([nameField_ stringValue]); | |
162 BOOL titleValid = controller_->IsTitleValid(title); | |
163 [self setIsValid:titleValid | |
164 toolTip:IDS_SEARCH_ENGINES_INVALID_TITLE_TT | |
165 forImageView:nameImage_ | |
166 textField:nameField_]; | |
167 | |
168 string16 keyword = base::SysNSStringToUTF16([keywordField_ stringValue]); | |
169 BOOL keywordValid = controller_->IsKeywordValid(keyword); | |
170 [self setIsValid:keywordValid | |
171 toolTip:IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT | |
172 forImageView:keywordImage_ | |
173 textField:keywordField_]; | |
174 | |
175 std::string url = base::SysNSStringToUTF8([urlField_ stringValue]); | |
176 BOOL urlValid = controller_->IsURLValid(url); | |
177 [self setIsValid:urlValid | |
178 toolTip:IDS_SEARCH_ENGINES_INVALID_URL_TT | |
179 forImageView:urlImage_ | |
180 textField:urlField_]; | |
181 | |
182 BOOL isValid = (titleValid && keywordValid && urlValid); | |
183 [doneButton_ setEnabled:isValid]; | |
184 return isValid; | |
185 } | |
186 | |
187 @end | |
OLD | NEW |