OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/edit_search_engine_dialog.h" | 5 #include "chrome/browser/ui/views/edit_search_engine_dialog.h" |
6 | 6 |
| 7 #include "base/i18n/case_conversion.h" |
7 #include "base/i18n/rtl.h" | 8 #include "base/i18n/rtl.h" |
8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/search_engines/template_url.h" | 11 #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/search_engines/edit_search_engine_controller.h" |
12 #include "chrome/browser/ui/views/constrained_window_views.h" | 13 #include "chrome/browser/ui/views/constrained_window_views.h" |
13 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
14 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
15 #include "grit/ui_resources.h" | 16 #include "grit/ui_resources.h" |
16 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 96 |
96 bool EditSearchEngineDialog::Accept() { | 97 bool EditSearchEngineDialog::Accept() { |
97 controller_->AcceptAddOrEdit(title_tf_->text(), keyword_tf_->text(), | 98 controller_->AcceptAddOrEdit(title_tf_->text(), keyword_tf_->text(), |
98 base::UTF16ToUTF8(url_tf_->text())); | 99 base::UTF16ToUTF8(url_tf_->text())); |
99 return true; | 100 return true; |
100 } | 101 } |
101 | 102 |
102 void EditSearchEngineDialog::ContentsChanged( | 103 void EditSearchEngineDialog::ContentsChanged( |
103 Textfield* sender, | 104 Textfield* sender, |
104 const base::string16& new_contents) { | 105 const base::string16& new_contents) { |
| 106 // Force the keyword text to be lowercase, keep the caret or selection. |
| 107 if (sender == keyword_tf_ && !sender->HasCompositionText()) { |
| 108 // TODO(msw): Prevent textfield scrolling with selection model changes here. |
| 109 const gfx::SelectionModel selection_model = sender->GetSelectionModel(); |
| 110 sender->SetText(base::i18n::ToLower(new_contents)); |
| 111 sender->SelectSelectionModel(selection_model); |
| 112 } |
| 113 |
105 GetDialogClientView()->UpdateDialogButtons(); | 114 GetDialogClientView()->UpdateDialogButtons(); |
106 UpdateImageViews(); | 115 UpdateImageViews(); |
107 } | 116 } |
108 | 117 |
109 bool EditSearchEngineDialog::HandleKeyEvent( | 118 bool EditSearchEngineDialog::HandleKeyEvent( |
110 Textfield* sender, | 119 Textfield* sender, |
111 const ui::KeyEvent& key_event) { | 120 const ui::KeyEvent& key_event) { |
112 return false; | 121 return false; |
113 } | 122 } |
114 | 123 |
115 void EditSearchEngineDialog::Init() { | 124 void EditSearchEngineDialog::Init() { |
116 // Create the views we'll need. | 125 // Create the views we'll need. |
117 if (controller_->template_url()) { | 126 if (controller_->template_url()) { |
118 title_tf_ = CreateTextfield(controller_->template_url()->short_name(), | 127 title_tf_ = CreateTextfield(controller_->template_url()->short_name()); |
119 false); | 128 keyword_tf_ = CreateTextfield(controller_->template_url()->keyword()); |
120 keyword_tf_ = CreateTextfield(controller_->template_url()->keyword(), true); | |
121 url_tf_ = CreateTextfield( | 129 url_tf_ = CreateTextfield( |
122 controller_->template_url()->url_ref().DisplayURL(), false); | 130 controller_->template_url()->url_ref().DisplayURL()); |
123 // We don't allow users to edit prepopulate URLs. This is done as | 131 // We don't allow users to edit prepopulate URLs. This is done as |
124 // occasionally we need to update the URL of prepopulated TemplateURLs. | 132 // occasionally we need to update the URL of prepopulated TemplateURLs. |
125 url_tf_->SetReadOnly(controller_->template_url()->prepopulate_id() != 0); | 133 url_tf_->SetReadOnly(controller_->template_url()->prepopulate_id() != 0); |
126 } else { | 134 } else { |
127 title_tf_ = CreateTextfield(base::string16(), false); | 135 title_tf_ = CreateTextfield(base::string16()); |
128 keyword_tf_ = CreateTextfield(base::string16(), true); | 136 keyword_tf_ = CreateTextfield(base::string16()); |
129 url_tf_ = CreateTextfield(base::string16(), false); | 137 url_tf_ = CreateTextfield(base::string16()); |
130 } | 138 } |
131 title_iv_ = new views::ImageView(); | 139 title_iv_ = new views::ImageView(); |
132 keyword_iv_ = new views::ImageView(); | 140 keyword_iv_ = new views::ImageView(); |
133 url_iv_ = new views::ImageView(); | 141 url_iv_ = new views::ImageView(); |
134 | 142 |
135 UpdateImageViews(); | 143 UpdateImageViews(); |
136 | 144 |
137 const int related_x = views::kRelatedControlHorizontalSpacing; | 145 const int related_x = views::kRelatedControlHorizontalSpacing; |
138 const int related_y = views::kRelatedControlVerticalSpacing; | 146 const int related_y = views::kRelatedControlVerticalSpacing; |
139 const int unrelated_y = views::kUnrelatedControlVerticalSpacing; | 147 const int unrelated_y = views::kUnrelatedControlVerticalSpacing; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 layout->AddPaddingRow(0, related_y); | 224 layout->AddPaddingRow(0, related_y); |
217 } | 225 } |
218 | 226 |
219 views::Label* EditSearchEngineDialog::CreateLabel(int message_id) { | 227 views::Label* EditSearchEngineDialog::CreateLabel(int message_id) { |
220 views::Label* label = | 228 views::Label* label = |
221 new views::Label(l10n_util::GetStringUTF16(message_id)); | 229 new views::Label(l10n_util::GetStringUTF16(message_id)); |
222 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 230 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
223 return label; | 231 return label; |
224 } | 232 } |
225 | 233 |
226 Textfield* EditSearchEngineDialog::CreateTextfield(const base::string16& text, | 234 Textfield* EditSearchEngineDialog::CreateTextfield(const base::string16& text) { |
227 bool lowercase) { | 235 Textfield* text_field = new Textfield(); |
228 Textfield* text_field = new Textfield( | |
229 lowercase ? Textfield::STYLE_LOWERCASE : Textfield::STYLE_DEFAULT); | |
230 text_field->SetText(text); | 236 text_field->SetText(text); |
231 text_field->SetController(this); | 237 text_field->set_controller(this); |
232 return text_field; | 238 return text_field; |
233 } | 239 } |
234 | 240 |
235 void EditSearchEngineDialog::UpdateImageViews() { | 241 void EditSearchEngineDialog::UpdateImageViews() { |
236 UpdateImageView(keyword_iv_, controller_->IsKeywordValid(keyword_tf_->text()), | 242 UpdateImageView(keyword_iv_, controller_->IsKeywordValid(keyword_tf_->text()), |
237 IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT); | 243 IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT); |
238 UpdateImageView(url_iv_, | 244 UpdateImageView(url_iv_, |
239 controller_->IsURLValid(base::UTF16ToUTF8(url_tf_->text())), | 245 controller_->IsURLValid(base::UTF16ToUTF8(url_tf_->text())), |
240 IDS_SEARCH_ENGINES_INVALID_URL_TT); | 246 IDS_SEARCH_ENGINES_INVALID_URL_TT); |
241 UpdateImageView(title_iv_, controller_->IsTitleValid(title_tf_->text()), | 247 UpdateImageView(title_iv_, controller_->IsTitleValid(title_tf_->text()), |
242 IDS_SEARCH_ENGINES_INVALID_TITLE_TT); | 248 IDS_SEARCH_ENGINES_INVALID_TITLE_TT); |
243 } | 249 } |
244 | 250 |
245 void EditSearchEngineDialog::UpdateImageView(views::ImageView* image_view, | 251 void EditSearchEngineDialog::UpdateImageView(views::ImageView* image_view, |
246 bool is_valid, | 252 bool is_valid, |
247 int invalid_message_id) { | 253 int invalid_message_id) { |
248 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 254 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
249 if (is_valid) { | 255 if (is_valid) { |
250 image_view->SetTooltipText(base::string16()); | 256 image_view->SetTooltipText(base::string16()); |
251 image_view->SetImage(rb.GetImageSkiaNamed(IDR_INPUT_GOOD)); | 257 image_view->SetImage(rb.GetImageSkiaNamed(IDR_INPUT_GOOD)); |
252 } else { | 258 } else { |
253 image_view->SetTooltipText(l10n_util::GetStringUTF16(invalid_message_id)); | 259 image_view->SetTooltipText(l10n_util::GetStringUTF16(invalid_message_id)); |
254 image_view->SetImage(rb.GetImageSkiaNamed(IDR_INPUT_ALERT)); | 260 image_view->SetImage(rb.GetImageSkiaNamed(IDR_INPUT_ALERT)); |
255 } | 261 } |
256 } | 262 } |
OLD | NEW |