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