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