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