• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #include "chrome/browser/ui/views/edit_search_engine_dialog.h"
6 
7 #include "base/i18n/rtl.h"
8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/search_engines/template_url.h"
11 #include "chrome/browser/ui/search_engines/edit_search_engine_controller.h"
12 #include "googleurl/src/gurl.h"
13 #include "grit/app_resources.h"
14 #include "grit/generated_resources.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "views/controls/image_view.h"
19 #include "views/controls/label.h"
20 #include "views/controls/table/table_view.h"
21 #include "views/controls/textfield/textfield.h"
22 #include "views/layout/grid_layout.h"
23 #include "views/layout/layout_constants.h"
24 #include "views/window/window.h"
25 
26 using views::GridLayout;
27 using views::ImageView;
28 using views::Textfield;
29 
30 
31 namespace {
32 // Converts a URL as understood by TemplateURL to one appropriate for display
33 // to the user.
GetDisplayURL(const TemplateURL & turl)34 std::wstring GetDisplayURL(const TemplateURL& turl) {
35   return turl.url() ? turl.url()->DisplayURL() : std::wstring();
36 }
37 }  // namespace
38 
39 namespace browser {
40 
EditSearchEngine(gfx::NativeWindow parent,const TemplateURL * template_url,EditSearchEngineControllerDelegate * delegate,Profile * profile)41 void EditSearchEngine(gfx::NativeWindow parent,
42                       const TemplateURL* template_url,
43                       EditSearchEngineControllerDelegate* delegate,
44                       Profile* profile) {
45   EditSearchEngineDialog::Show(parent, template_url, delegate, profile);
46 }
47 
48 }  // namespace browser
49 
EditSearchEngineDialog(const TemplateURL * template_url,EditSearchEngineControllerDelegate * delegate,Profile * profile)50 EditSearchEngineDialog::EditSearchEngineDialog(
51     const TemplateURL* template_url,
52     EditSearchEngineControllerDelegate* delegate,
53     Profile* profile)
54     : controller_(new EditSearchEngineController(template_url,
55                                                  delegate,
56                                                  profile)) {
57   Init();
58 }
59 
60 // static
Show(gfx::NativeWindow parent,const TemplateURL * template_url,EditSearchEngineControllerDelegate * delegate,Profile * profile)61 void EditSearchEngineDialog::Show(gfx::NativeWindow parent,
62                                   const TemplateURL* template_url,
63                                   EditSearchEngineControllerDelegate* delegate,
64                                   Profile* profile) {
65   EditSearchEngineDialog* contents =
66       new EditSearchEngineDialog(template_url, delegate, profile);
67   // Window interprets an empty rectangle as needing to query the content for
68   // the size as well as centering relative to the parent.
69   views::Window::CreateChromeWindow(parent, gfx::Rect(), contents);
70   contents->window()->Show();
71   contents->GetDialogClientView()->UpdateDialogButtons();
72   contents->title_tf_->SelectAll();
73   contents->title_tf_->RequestFocus();
74 }
75 
IsModal() const76 bool EditSearchEngineDialog::IsModal() const {
77   return true;
78 }
79 
GetWindowTitle() const80 std::wstring EditSearchEngineDialog::GetWindowTitle() const {
81   return UTF16ToWide(l10n_util::GetStringUTF16(controller_->template_url() ?
82       IDS_SEARCH_ENGINES_EDITOR_EDIT_WINDOW_TITLE :
83       IDS_SEARCH_ENGINES_EDITOR_NEW_WINDOW_TITLE));
84 }
85 
IsDialogButtonEnabled(MessageBoxFlags::DialogButton button) const86 bool EditSearchEngineDialog::IsDialogButtonEnabled(
87     MessageBoxFlags::DialogButton button) const {
88   if (button == MessageBoxFlags::DIALOGBUTTON_OK) {
89     return (controller_->IsKeywordValid(WideToUTF16(keyword_tf_->text())) &&
90             controller_->IsTitleValid(WideToUTF16(title_tf_->text())) &&
91             controller_->IsURLValid(WideToUTF8(url_tf_->text())));
92   }
93   return true;
94 }
95 
Cancel()96 bool EditSearchEngineDialog::Cancel() {
97   controller_->CleanUpCancelledAdd();
98   return true;
99 }
100 
Accept()101 bool EditSearchEngineDialog::Accept() {
102   controller_->AcceptAddOrEdit(WideToUTF16(title_tf_->text()),
103                                WideToUTF16(keyword_tf_->text()),
104                                WideToUTF8(url_tf_->text()));
105   return true;
106 }
107 
GetContentsView()108 views::View* EditSearchEngineDialog::GetContentsView() {
109   return this;
110 }
111 
ContentsChanged(Textfield * sender,const std::wstring & new_contents)112 void EditSearchEngineDialog::ContentsChanged(Textfield* sender,
113                                              const std::wstring& new_contents) {
114   GetDialogClientView()->UpdateDialogButtons();
115   UpdateImageViews();
116 }
117 
HandleKeyEvent(Textfield * sender,const views::KeyEvent & key_event)118 bool EditSearchEngineDialog::HandleKeyEvent(
119     Textfield* sender,
120     const views::KeyEvent& key_event) {
121   return false;
122 }
123 
Init()124 void EditSearchEngineDialog::Init() {
125   // Create the views we'll need.
126   if (controller_->template_url()) {
127     title_tf_ =
128         CreateTextfield(controller_->template_url()->short_name(), false);
129     keyword_tf_ = CreateTextfield(controller_->template_url()->keyword(), true);
130     url_tf_ =
131         CreateTextfield(GetDisplayURL(*controller_->template_url()), false);
132     // We don't allow users to edit prepopulate URLs. This is done as
133     // occasionally we need to update the URL of prepopulated TemplateURLs.
134     url_tf_->SetReadOnly(controller_->template_url()->prepopulate_id() != 0);
135   } else {
136     title_tf_ = CreateTextfield(std::wstring(), false);
137     keyword_tf_ = CreateTextfield(std::wstring(), true);
138     url_tf_ = CreateTextfield(std::wstring(), false);
139   }
140   title_iv_ = new ImageView();
141   keyword_iv_ = new ImageView();
142   url_iv_ = new ImageView();
143 
144   UpdateImageViews();
145 
146   const int related_x = views::kRelatedControlHorizontalSpacing;
147   const int related_y = views::kRelatedControlVerticalSpacing;
148   const int unrelated_y = views::kUnrelatedControlVerticalSpacing;
149 
150   // View and GridLayout take care of deleting GridLayout for us.
151   GridLayout* layout = GridLayout::CreatePanel(this);
152   SetLayoutManager(layout);
153 
154   // Define the structure of the layout.
155 
156   // For the buttons.
157   views::ColumnSet* column_set = layout->AddColumnSet(0);
158   column_set->AddPaddingColumn(1, 0);
159   column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
160                         GridLayout::USE_PREF, 0, 0);
161   column_set->AddPaddingColumn(0, related_x);
162   column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
163                         GridLayout::USE_PREF, 0, 0);
164   column_set->LinkColumnSizes(1, 3, -1);
165 
166   // For the Textfields.
167   column_set = layout->AddColumnSet(1);
168   column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
169                         GridLayout::USE_PREF, 0, 0);
170   column_set->AddPaddingColumn(0, related_x);
171   column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
172                         GridLayout::USE_PREF, 0, 0);
173   column_set->AddPaddingColumn(0, related_x);
174   column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
175                         GridLayout::USE_PREF, 0, 0);
176 
177   // For the description.
178   column_set = layout->AddColumnSet(2);
179   column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
180                         GridLayout::USE_PREF, 0, 0);
181 
182   // Add the contents.
183   layout->StartRow(0, 1);
184   layout->AddView(CreateLabel(IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_LABEL));
185   layout->AddView(title_tf_);
186   layout->AddView(title_iv_);
187 
188   layout->StartRowWithPadding(0, 1, 0, related_y);
189   layout->AddView(CreateLabel(IDS_SEARCH_ENGINES_EDITOR_KEYWORD_LABEL));
190   layout->AddView(keyword_tf_);
191   layout->AddView(keyword_iv_);
192 
193   layout->StartRowWithPadding(0, 1, 0, related_y);
194   layout->AddView(CreateLabel(IDS_SEARCH_ENGINES_EDITOR_URL_LABEL));
195   layout->AddView(url_tf_);
196   layout->AddView(url_iv_);
197 
198   // On RTL UIs (such as Arabic and Hebrew) the description text is not
199   // displayed correctly since it contains the substring "%s". This substring
200   // is not interpreted by the Unicode BiDi algorithm as an LTR string and
201   // therefore the end result is that the following right to left text is
202   // displayed: ".three two s% one" (where 'one', 'two', etc. are words in
203   // Hebrew).
204   //
205   // In order to fix this problem we transform the substring "%s" so that it
206   // is displayed correctly when rendered in an RTL context.
207   layout->StartRowWithPadding(0, 2, 0, unrelated_y);
208   std::wstring description = UTF16ToWide(l10n_util::GetStringUTF16(
209       IDS_SEARCH_ENGINES_EDITOR_URL_DESCRIPTION_LABEL));
210   if (base::i18n::IsRTL()) {
211     const std::wstring reversed_percent(L"s%");
212     std::wstring::size_type percent_index =
213         description.find(L"%s", static_cast<std::wstring::size_type>(0));
214     if (percent_index != std::wstring::npos)
215       description.replace(percent_index,
216                           reversed_percent.length(),
217                           reversed_percent);
218   }
219 
220   views::Label* description_label = new views::Label(description);
221   description_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
222   layout->AddView(description_label);
223 
224   layout->AddPaddingRow(0, related_y);
225 }
226 
CreateLabel(int message_id)227 views::Label* EditSearchEngineDialog::CreateLabel(int message_id) {
228   views::Label* label =
229       new views::Label(UTF16ToWide(l10n_util::GetStringUTF16(message_id)));
230   label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
231   return label;
232 }
233 
CreateTextfield(const std::wstring & text,bool lowercase)234 Textfield* EditSearchEngineDialog::CreateTextfield(const std::wstring& text,
235                                                    bool lowercase) {
236   Textfield* text_field = new Textfield(
237       lowercase ? Textfield::STYLE_LOWERCASE : Textfield::STYLE_DEFAULT);
238   text_field->SetText(text);
239   text_field->SetController(this);
240   return text_field;
241 }
242 
UpdateImageViews()243 void EditSearchEngineDialog::UpdateImageViews() {
244   UpdateImageView(keyword_iv_,
245                   controller_->IsKeywordValid(WideToUTF16(keyword_tf_->text())),
246                   IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT);
247   UpdateImageView(url_iv_, controller_->IsURLValid(WideToUTF8(url_tf_->text())),
248                   IDS_SEARCH_ENGINES_INVALID_URL_TT);
249   UpdateImageView(title_iv_,
250                   controller_->IsTitleValid(WideToUTF16(title_tf_->text())),
251                   IDS_SEARCH_ENGINES_INVALID_TITLE_TT);
252 }
253 
UpdateImageView(ImageView * image_view,bool is_valid,int invalid_message_id)254 void EditSearchEngineDialog::UpdateImageView(ImageView* image_view,
255                                              bool is_valid,
256                                              int invalid_message_id) {
257   if (is_valid) {
258     image_view->SetTooltipText(std::wstring());
259     image_view->SetImage(
260         ResourceBundle::GetSharedInstance().GetBitmapNamed(
261             IDR_INPUT_GOOD));
262   } else {
263     image_view->SetTooltipText(
264         UTF16ToWide(l10n_util::GetStringUTF16(invalid_message_id)));
265     image_view->SetImage(
266         ResourceBundle::GetSharedInstance().GetBitmapNamed(
267             IDR_INPUT_ALERT));
268   }
269 }
270