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