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