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/options/edit_search_engine_cocoa_controller.h" | |
6 | |
7 #include "base/logging.h" | |
8 #import "base/mac/mac_util.h" | |
9 #include "base/string16.h" | |
10 #include "base/sys_string_conversions.h" | |
11 #include "chrome/browser/search_engines/template_url.h" | |
12 #include "grit/app_resources.h" | |
13 #include "grit/generated_resources.h" | |
14 #include "grit/theme_resources.h" | |
15 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | |
16 #include "ui/base/l10n/l10n_util_mac.h" | |
17 #include "ui/base/resource/resource_bundle.h" | |
18 #include "ui/gfx/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:(const TemplateURL*)url { | |
35 DCHECK(profile); | |
36 NSString* nibpath = [base::mac::MainAppBundle() | |
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 ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); | |
84 goodImage_.reset([bundle.GetNativeImageNamed(IDR_INPUT_GOOD) retain]); | |
85 badImage_.reset([bundle.GetNativeImageNamed(IDR_INPUT_ALERT) retain]); | |
86 if (templateURL_) { | |
87 // Defaults to |..._NEW_WINDOW_TITLE|. | |
88 [window setTitle:l10n_util::GetNSString( | |
89 IDS_SEARCH_ENGINES_EDITOR_EDIT_WINDOW_TITLE)]; | |
90 [nameField_ setStringValue: | |
91 base::SysUTF16ToNSString(templateURL_->short_name())]; | |
92 [keywordField_ setStringValue: | |
93 base::SysUTF16ToNSString(templateURL_->keyword())]; | |
94 [urlField_ setStringValue: | |
95 base::SysUTF16ToNSString(templateURL_->url()->DisplayURL())]; | |
96 [urlField_ setEnabled:(templateURL_->prepopulate_id() == 0)]; | |
97 } | |
98 // When creating a new keyword, this will mark the fields as "invalid" and | |
99 // will not let the user save. If this is an edit, then this will set all | |
100 // the images to the "valid" state. | |
101 [self validateFields]; | |
102 } | |
103 | |
104 // When the window closes, clean ourselves up. | |
105 - (void)windowWillClose:(NSNotification*)notif { | |
106 [self autorelease]; | |
107 } | |
108 | |
109 // Performs the logic of closing the window. If we are a sheet, then it ends the | |
110 // modal session; otherwise, it closes the window. | |
111 - (void)doClose { | |
112 if ([[self window] isSheet]) { | |
113 [NSApp endSheet:[self window]]; | |
114 } else { | |
115 [[self window] close]; | |
116 } | |
117 } | |
118 | |
119 - (IBAction)cancel:(id)sender { | |
120 [self doClose]; | |
121 } | |
122 | |
123 - (IBAction)save:(id)sender { | |
124 DCHECK([self validateFields]); | |
125 string16 title = base::SysNSStringToUTF16([nameField_ stringValue]); | |
126 string16 keyword = base::SysNSStringToUTF16([keywordField_ stringValue]); | |
127 std::string url = base::SysNSStringToUTF8([urlField_ stringValue]); | |
128 controller_->AcceptAddOrEdit(title, keyword, url); | |
129 [self doClose]; | |
130 } | |
131 | |
132 // Delegate method for the text fields. | |
133 | |
134 - (void)controlTextDidChange:(NSNotification*)notif { | |
135 [self validateFields]; | |
136 } | |
137 | |
138 - (void)controlTextDidEndEditing:(NSNotification*)notif { | |
139 [self validateFields]; | |
140 } | |
141 | |
142 // Private -------------------------------------------------------------------- | |
143 | |
144 // Sets the appropriate image and tooltip based on a boolean |valid|. | |
145 - (void)setIsValid:(BOOL)valid | |
146 toolTip:(int)messageID | |
147 forImageView:(NSImageView*)imageView | |
148 textField:(NSTextField*)textField { | |
149 NSImage* image = (valid) ? goodImage_ : badImage_; | |
150 [imageView setImage:image]; | |
151 | |
152 NSString* toolTip = nil; | |
153 if (!valid) | |
154 toolTip = l10n_util::GetNSString(messageID); | |
155 [textField setToolTip:toolTip]; | |
156 [imageView setToolTip:toolTip]; | |
157 } | |
158 | |
159 // This sets the image state for all the controls and enables or disables the | |
160 // done button. Returns YES if all the fields are valid. | |
161 - (BOOL)validateFields { | |
162 string16 title = base::SysNSStringToUTF16([nameField_ stringValue]); | |
163 BOOL titleValid = controller_->IsTitleValid(title); | |
164 [self setIsValid:titleValid | |
165 toolTip:IDS_SEARCH_ENGINES_INVALID_TITLE_TT | |
166 forImageView:nameImage_ | |
167 textField:nameField_]; | |
168 | |
169 string16 keyword = base::SysNSStringToUTF16([keywordField_ stringValue]); | |
170 BOOL keywordValid = controller_->IsKeywordValid(keyword); | |
171 [self setIsValid:keywordValid | |
172 toolTip:IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT | |
173 forImageView:keywordImage_ | |
174 textField:keywordField_]; | |
175 | |
176 std::string url = base::SysNSStringToUTF8([urlField_ stringValue]); | |
177 BOOL urlValid = controller_->IsURLValid(url); | |
178 [self setIsValid:urlValid | |
179 toolTip:IDS_SEARCH_ENGINES_INVALID_URL_TT | |
180 forImageView:urlImage_ | |
181 textField:urlField_]; | |
182 | |
183 BOOL isValid = (titleValid && keywordValid && urlValid); | |
184 [doneButton_ setEnabled:isValid]; | |
185 return isValid; | |
186 } | |
187 | |
188 @end | |
OLD | NEW |