find_bar_view.cc revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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/find_bar_view.h" 6 7#include <algorithm> 8 9#include "base/string_util.h" 10#include "base/strings/string_number_conversions.h" 11#include "base/utf_string_conversions.h" 12#include "chrome/browser/profiles/profile.h" 13#include "chrome/browser/themes/theme_properties.h" 14#include "chrome/browser/ui/find_bar/find_bar_controller.h" 15#include "chrome/browser/ui/find_bar/find_bar_state.h" 16#include "chrome/browser/ui/find_bar/find_bar_state_factory.h" 17#include "chrome/browser/ui/find_bar/find_notification_details.h" 18#include "chrome/browser/ui/find_bar/find_tab_helper.h" 19#include "chrome/browser/ui/view_ids.h" 20#include "chrome/browser/ui/views/find_bar_host.h" 21#include "chrome/browser/ui/views/frame/browser_view.h" 22#include "grit/generated_resources.h" 23#include "grit/theme_resources.h" 24#include "grit/ui_resources.h" 25#include "third_party/skia/include/effects/SkGradientShader.h" 26#include "ui/base/clipboard/scoped_clipboard_writer.h" 27#include "ui/base/events/event.h" 28#include "ui/base/l10n/l10n_util.h" 29#include "ui/base/resource/resource_bundle.h" 30#include "ui/base/theme_provider.h" 31#include "ui/gfx/canvas.h" 32#include "ui/views/controls/button/image_button.h" 33#include "ui/views/controls/label.h" 34#include "ui/views/controls/textfield/textfield.h" 35#include "ui/views/widget/widget.h" 36 37// The amount of whitespace to have before the find button. 38static const int kWhiteSpaceAfterMatchCountLabel = 1; 39 40// The margins around the search field and the close button. 41static const int kMarginLeftOfCloseButton = 3; 42static const int kMarginRightOfCloseButton = 7; 43static const int kMarginLeftOfFindTextfield = 12; 44 45// The margins around the match count label (We add extra space so that the 46// background highlight extends beyond just the text). 47static const int kMatchCountExtraWidth = 9; 48 49// Minimum width for the match count label. 50static const int kMatchCountMinWidth = 30; 51 52// The text color for the match count label. 53static const SkColor kTextColorMatchCount = SkColorSetRGB(178, 178, 178); 54 55// The text color for the match count label when no matches are found. 56static const SkColor kTextColorNoMatch = SK_ColorBLACK; 57 58// The background color of the match count label when results are found. 59static const SkColor kBackgroundColorMatch = SkColorSetARGB(0, 255, 255, 255); 60 61// The background color of the match count label when no results are found. 62static const SkColor kBackgroundColorNoMatch = SkColorSetRGB(255, 102, 102); 63 64// The default number of average characters that the text box will be. This 65// number brings the width on a "regular fonts" system to about 300px. 66static const int kDefaultCharWidth = 43; 67 68//////////////////////////////////////////////////////////////////////////////// 69// FindBarView, public: 70 71FindBarView::FindBarView(FindBarHost* host) 72 : DropdownBarView(host), 73 find_text_(NULL), 74 match_count_text_(NULL), 75 focus_forwarder_view_(NULL), 76 find_previous_button_(NULL), 77 find_next_button_(NULL), 78 close_button_(NULL), 79 text_box_background_(NULL), 80 text_box_background_left_(NULL) { 81 set_id(VIEW_ID_FIND_IN_PAGE); 82 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 83 84 find_text_ = new SearchTextfieldView; 85 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); 86 find_text_->SetFont(rb.GetFont(ui::ResourceBundle::BaseFont)); 87 find_text_->set_default_width_in_chars(kDefaultCharWidth); 88 find_text_->SetController(this); 89 find_text_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FIND)); 90 91 AddChildView(find_text_); 92 93 match_count_text_ = new views::Label(); 94 match_count_text_->SetFont(rb.GetFont(ui::ResourceBundle::BaseFont)); 95 AddChildView(match_count_text_); 96 97 // Create a focus forwarder view which sends focus to find_text_. 98 focus_forwarder_view_ = new FocusForwarderView(find_text_); 99 AddChildView(focus_forwarder_view_); 100 101 find_previous_button_ = new views::ImageButton(this); 102 find_previous_button_->set_tag(FIND_PREVIOUS_TAG); 103 find_previous_button_->set_focusable(true); 104 find_previous_button_->SetImage(views::CustomButton::STATE_NORMAL, 105 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV)); 106 find_previous_button_->SetImage(views::CustomButton::STATE_HOVERED, 107 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_H)); 108 find_previous_button_->SetImage(views::CustomButton::STATE_PRESSED, 109 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_P)); 110 find_previous_button_->SetImage(views::CustomButton::STATE_DISABLED, 111 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_D)); 112 find_previous_button_->SetTooltipText( 113 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_PREVIOUS_TOOLTIP)); 114 find_previous_button_->SetAccessibleName( 115 l10n_util::GetStringUTF16(IDS_ACCNAME_PREVIOUS)); 116 AddChildView(find_previous_button_); 117 118 find_next_button_ = new views::ImageButton(this); 119 find_next_button_->set_tag(FIND_NEXT_TAG); 120 find_next_button_->set_focusable(true); 121 find_next_button_->SetImage(views::CustomButton::STATE_NORMAL, 122 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT)); 123 find_next_button_->SetImage(views::CustomButton::STATE_HOVERED, 124 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_H)); 125 find_next_button_->SetImage(views::CustomButton::STATE_PRESSED, 126 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_P)); 127 find_next_button_->SetImage(views::CustomButton::STATE_DISABLED, 128 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_D)); 129 find_next_button_->SetTooltipText( 130 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_NEXT_TOOLTIP)); 131 find_next_button_->SetAccessibleName( 132 l10n_util::GetStringUTF16(IDS_ACCNAME_NEXT)); 133 AddChildView(find_next_button_); 134 135 close_button_ = new views::ImageButton(this); 136 close_button_->set_tag(CLOSE_TAG); 137 close_button_->set_focusable(true); 138 close_button_->SetImage(views::CustomButton::STATE_NORMAL, 139 rb.GetImageSkiaNamed(IDR_CLOSE_1)); 140 close_button_->SetImage(views::CustomButton::STATE_HOVERED, 141 rb.GetImageSkiaNamed(IDR_CLOSE_1_H)); 142 close_button_->SetImage(views::CustomButton::STATE_PRESSED, 143 rb.GetImageSkiaNamed(IDR_CLOSE_1_P)); 144 close_button_->SetTooltipText( 145 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_CLOSE_TOOLTIP)); 146 close_button_->SetAccessibleName( 147 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); 148 close_button_->SetAnimationDuration(0); 149 AddChildView(close_button_); 150 151 SetBackground(rb.GetImageSkiaNamed(IDR_FIND_DLG_LEFT_BACKGROUND), 152 rb.GetImageSkiaNamed(IDR_FIND_DLG_RIGHT_BACKGROUND)); 153 154 SetBorder(IDR_FIND_DIALOG_LEFT, IDR_FIND_DIALOG_MIDDLE, 155 IDR_FIND_DIALOG_RIGHT); 156 157 preferred_height_ = rb.GetImageSkiaNamed(IDR_FIND_DIALOG_MIDDLE)->height(); 158 159 // Background images for the Find edit box. 160 text_box_background_ = rb.GetImageSkiaNamed(IDR_FIND_BOX_BACKGROUND); 161 text_box_background_left_ = 162 rb.GetImageSkiaNamed(IDR_FIND_BOX_BACKGROUND_LEFT); 163 164 EnableCanvasFlippingForRTLUI(true); 165} 166 167FindBarView::~FindBarView() { 168} 169 170void FindBarView::SetFindText(const string16& find_text) { 171 find_text_->SetText(find_text); 172} 173 174string16 FindBarView::GetFindText() const { 175 return find_text_->text(); 176} 177 178string16 FindBarView::GetFindSelectedText() const { 179 return find_text_->GetSelectedText(); 180} 181 182string16 FindBarView::GetMatchCountText() const { 183 return match_count_text_->text(); 184} 185 186void FindBarView::UpdateForResult(const FindNotificationDetails& result, 187 const string16& find_text) { 188 bool have_valid_range = 189 result.number_of_matches() != -1 && result.active_match_ordinal() != -1; 190 191 // http://crbug.com/34970: some IMEs get confused if we change the text 192 // composed by them. To avoid this problem, we should check the IME status and 193 // update the text only when the IME is not composing text. 194 if (find_text_->text() != find_text && !find_text_->IsIMEComposing()) { 195 SetFindText(find_text); 196 find_text_->SelectAll(true); 197 } 198 199 if (find_text.empty() || !have_valid_range) { 200 // If there was no text entered, we don't show anything in the result count 201 // area. 202 ClearMatchCount(); 203 return; 204 } 205 206 match_count_text_->SetText(l10n_util::GetStringFUTF16(IDS_FIND_IN_PAGE_COUNT, 207 base::IntToString16(result.active_match_ordinal()), 208 base::IntToString16(result.number_of_matches()))); 209 210 UpdateMatchCountAppearance(result.number_of_matches() == 0 && 211 result.final_update()); 212 213 // The match_count label may have increased/decreased in size so we need to 214 // do a layout and repaint the dialog so that the find text field doesn't 215 // partially overlap the match-count label when it increases on no matches. 216 Layout(); 217 SchedulePaint(); 218} 219 220void FindBarView::ClearMatchCount() { 221 match_count_text_->SetText(string16()); 222 UpdateMatchCountAppearance(false); 223 Layout(); 224 SchedulePaint(); 225} 226 227void FindBarView::SetFocusAndSelection(bool select_all) { 228 find_text_->RequestFocus(); 229 if (select_all && !find_text_->text().empty()) 230 find_text_->SelectAll(true); 231} 232 233/////////////////////////////////////////////////////////////////////////////// 234// FindBarView, views::View overrides: 235 236void FindBarView::OnPaint(gfx::Canvas* canvas) { 237 // Paint drop down bar border and background. 238 DropdownBarView::OnPaint(canvas); 239 240 // Then we draw the background image for the Find Textfield. We start by 241 // calculating the position of background images for the Find text box. 242 int find_text_x = find_text_->x(); 243 gfx::Point back_button_origin = find_previous_button_->bounds().origin(); 244 245 // Draw the image to the left that creates a curved left edge for the box. 246 canvas->TileImageInt(*text_box_background_left_, 247 find_text_x - text_box_background_left_->width(), 248 back_button_origin.y(), text_box_background_left_->width(), 249 text_box_background_left_->height()); 250 251 // Draw the top and bottom border for whole text box (encompasses both the 252 // find_text_ edit box and the match_count_text_ label). 253 canvas->TileImageInt(*text_box_background_, find_text_x, 254 back_button_origin.y(), 255 back_button_origin.x() - find_text_x, 256 text_box_background_->height()); 257 258 // Draw the background of the match text. We want to make sure the red 259 // "no-match" background almost completely fills up the amount of vertical 260 // space within the text box. We therefore fix the size relative to the button 261 // heights. We use the FindPrev button, which has a 1px outer whitespace 262 // margin, 1px border and we want to appear 1px below the border line so we 263 // subtract 3 for top and 3 for bottom. 264 gfx::Rect match_count_background_bounds(match_count_text_->bounds()); 265 match_count_background_bounds.set_height( 266 find_previous_button_->height() - 6); // Subtract 3px x 2. 267 match_count_background_bounds.set_y( 268 (height() - match_count_background_bounds.height()) / 2); 269 canvas->FillRect(match_count_background_bounds, 270 match_count_text_->background_color()); 271} 272 273void FindBarView::Layout() { 274 int panel_width = GetPreferredSize().width(); 275 276 // Stay within view bounds. 277 int view_width = width(); 278 if (view_width && view_width < panel_width) 279 panel_width = view_width; 280 281 // First we draw the close button on the far right. 282 gfx::Size sz = close_button_->GetPreferredSize(); 283 close_button_->SetBounds(panel_width - sz.width() - 284 kMarginRightOfCloseButton, 285 (height() - sz.height()) / 2, 286 sz.width(), 287 sz.height()); 288 // Set the color. 289 OnThemeChanged(); 290 291 // Next, the FindNext button to the left the close button. 292 sz = find_next_button_->GetPreferredSize(); 293 find_next_button_->SetBounds(close_button_->x() - 294 find_next_button_->width() - 295 kMarginLeftOfCloseButton, 296 (height() - sz.height()) / 2, 297 sz.width(), 298 sz.height()); 299 300 // Then, the FindPrevious button to the left the FindNext button. 301 sz = find_previous_button_->GetPreferredSize(); 302 find_previous_button_->SetBounds(find_next_button_->x() - 303 find_previous_button_->width(), 304 (height() - sz.height()) / 2, 305 sz.width(), 306 sz.height()); 307 308 sz = find_text_->GetPreferredSize(); 309 const int find_text_y = (height() - sz.height()) / 2 + 1; 310 311 // Then the label showing the match count number. 312 sz = match_count_text_->GetPreferredSize(); 313 // We extend the label bounds a bit to give the background highlighting a bit 314 // of breathing room (margins around the text). 315 sz.Enlarge(kMatchCountExtraWidth, 0); 316 sz.set_width(std::max(kMatchCountMinWidth, static_cast<int>(sz.width()))); 317 int match_count_x = find_previous_button_->x() - 318 kWhiteSpaceAfterMatchCountLabel - 319 sz.width(); 320 match_count_text_->SetBounds(match_count_x, 321 find_text_y + find_text_->GetBaseline() - 322 match_count_text_->GetBaseline(), 323 sz.width(), 324 sz.height()); 325 326 // And whatever space is left in between, gets filled up by the find edit box. 327 sz = find_text_->GetPreferredSize(); 328 sz.set_width(std::max(0, match_count_x - kMarginLeftOfFindTextfield)); 329 int find_text_x = std::max(0, match_count_x - sz.width()); 330 find_text_->SetBounds(find_text_x, 331 (height() - sz.height()) / 2 + 1, 332 sz.width(), 333 sz.height()); 334 335 // The focus forwarder view is a hidden view that should cover the area 336 // between the find text box and the find button so that when the user clicks 337 // in that area we focus on the find text box. 338 int find_text_edge = find_text_->x() + find_text_->width(); 339 focus_forwarder_view_->SetBounds(find_text_edge, 340 find_previous_button_->y(), 341 find_previous_button_->x() - 342 find_text_edge, 343 find_previous_button_->height()); 344} 345 346void FindBarView::ViewHierarchyChanged( 347 const ViewHierarchyChangedDetails& details) { 348 if (details.is_add && details.child == this) { 349 find_text_->SetHorizontalMargins(3, 3); // Left and Right margins. 350 find_text_->SetVerticalMargins(0, 0); // Top and bottom margins. 351 find_text_->RemoveBorder(); // We draw our own border (a background image). 352 } 353} 354 355gfx::Size FindBarView::GetPreferredSize() { 356 gfx::Size prefsize = find_text_->GetPreferredSize(); 357 prefsize.set_height(preferred_height_); 358 359 // Add up all the preferred sizes and margins of the rest of the controls. 360 prefsize.Enlarge(kMarginLeftOfCloseButton + kMarginRightOfCloseButton + 361 kMarginLeftOfFindTextfield, 362 0); 363 prefsize.Enlarge(find_previous_button_->GetPreferredSize().width(), 0); 364 prefsize.Enlarge(find_next_button_->GetPreferredSize().width(), 0); 365 prefsize.Enlarge(close_button_->GetPreferredSize().width(), 0); 366 return prefsize; 367} 368 369//////////////////////////////////////////////////////////////////////////////// 370// FindBarView, views::ButtonListener implementation: 371 372void FindBarView::ButtonPressed( 373 views::Button* sender, const ui::Event& event) { 374 switch (sender->tag()) { 375 case FIND_PREVIOUS_TAG: 376 case FIND_NEXT_TAG: 377 if (!find_text_->text().empty()) { 378 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents( 379 find_bar_host()->GetFindBarController()->web_contents()); 380 find_tab_helper->StartFinding(find_text_->text(), 381 sender->tag() == FIND_NEXT_TAG, 382 false); // Not case sensitive. 383 } 384 if (event.IsMouseEvent()) { 385 // If mouse event, we move the focus back to the text-field, so that the 386 // user doesn't have to click on the text field to change the search. We 387 // don't want to do this for keyboard clicks on the button, since the 388 // user is more likely to press FindNext again than change the search 389 // query. 390 find_text_->RequestFocus(); 391 } 392 break; 393 case CLOSE_TAG: 394 find_bar_host()->GetFindBarController()->EndFindSession( 395 FindBarController::kKeepSelectionOnPage, 396 FindBarController::kKeepResultsInFindBox); 397 break; 398 default: 399 NOTREACHED() << L"Unknown button"; 400 break; 401 } 402} 403 404//////////////////////////////////////////////////////////////////////////////// 405// FindBarView, views::TextfieldController implementation: 406 407void FindBarView::ContentsChanged(views::Textfield* sender, 408 const string16& new_contents) { 409 FindBarController* controller = find_bar_host()->GetFindBarController(); 410 DCHECK(controller); 411 content::WebContents* web_contents = controller->web_contents(); 412 // We must guard against a NULL web_contents, which can happen if the text 413 // in the Find box is changed right after the tab is destroyed. Otherwise, it 414 // can lead to crashes, as exposed by automation testing in issue 8048. 415 if (!web_contents) 416 return; 417 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); 418 419 // When the user changes something in the text box we check the contents and 420 // if the textbox contains something we set it as the new search string and 421 // initiate search (even though old searches might be in progress). 422 if (!new_contents.empty()) { 423 // The last two params here are forward (true) and case sensitive (false). 424 find_tab_helper->StartFinding(new_contents, true, false); 425 } else { 426 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); 427 UpdateForResult(find_tab_helper->find_result(), string16()); 428 find_bar_host()->MoveWindowIfNecessary(gfx::Rect(), false); 429 430 // Clearing the text box should clear the prepopulate state so that when 431 // we close and reopen the Find box it doesn't show the search we just 432 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged 433 // sent for a lot more things than just the user nulling out the search 434 // terms. See http://crbug.com/45372. 435 Profile* profile = 436 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 437 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); 438 find_bar_state->set_last_prepopulate_text(string16()); 439 } 440} 441 442bool FindBarView::HandleKeyEvent(views::Textfield* sender, 443 const ui::KeyEvent& key_event) { 444 // If the dialog is not visible, there is no reason to process keyboard input. 445 if (!host()->IsVisible()) 446 return false; 447 448 if (find_bar_host()->MaybeForwardKeyEventToWebpage(key_event)) 449 return true; // Handled, we are done! 450 451 if (key_event.key_code() == ui::VKEY_RETURN) { 452 // Pressing Return/Enter starts the search (unless text box is empty). 453 string16 find_string = find_text_->text(); 454 if (!find_string.empty()) { 455 FindBarController* controller = find_bar_host()->GetFindBarController(); 456 FindTabHelper* find_tab_helper = 457 FindTabHelper::FromWebContents(controller->web_contents()); 458 // Search forwards for enter, backwards for shift-enter. 459 find_tab_helper->StartFinding(find_string, 460 !key_event.IsShiftDown(), 461 false); // Not case sensitive. 462 } 463 return true; 464 } 465 466 return false; 467} 468 469void FindBarView::OnAfterCutOrCopy() { 470 Profile* profile = host()->browser_view()->browser()->profile(); 471 ui::SourceTag source_tag = 472 content::BrowserContext::GetMarkerForOffTheRecordContext(profile); 473 if (source_tag != ui::SourceTag()) { 474 // Overwrite the clipboard with the correct SourceTag 475 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); 476 string16 text; 477 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &text); 478 479 ui::ScopedClipboardWriter scw(clipboard, 480 ui::Clipboard::BUFFER_STANDARD, 481 source_tag); 482 scw.WriteText(text); 483 } 484} 485 486void FindBarView::UpdateMatchCountAppearance(bool no_match) { 487 if (no_match) { 488 match_count_text_->SetBackgroundColor(kBackgroundColorNoMatch); 489 match_count_text_->SetEnabledColor(kTextColorNoMatch); 490 } else { 491 match_count_text_->SetBackgroundColor(kBackgroundColorMatch); 492 match_count_text_->SetEnabledColor(kTextColorMatchCount); 493 } 494} 495 496bool FindBarView::FocusForwarderView::OnMousePressed( 497 const ui::MouseEvent& event) { 498 if (view_to_focus_on_mousedown_) 499 view_to_focus_on_mousedown_->RequestFocus(); 500 return true; 501} 502 503FindBarView::SearchTextfieldView::SearchTextfieldView() 504 : select_all_on_focus_(true) {} 505 506FindBarView::SearchTextfieldView::~SearchTextfieldView() {} 507 508bool FindBarView::SearchTextfieldView::OnMousePressed( 509 const ui::MouseEvent& event) { 510 // Avoid temporarily selecting all the text on focus from a mouse press; this 511 // prevents flickering before setting a cursor or dragging to select text. 512 select_all_on_focus_ = false; 513 return views::Textfield::OnMousePressed(event); 514} 515 516void FindBarView::SearchTextfieldView::OnMouseReleased( 517 const ui::MouseEvent& event) { 518 views::Textfield::OnMouseReleased(event); 519 select_all_on_focus_ = true; 520} 521 522void FindBarView::SearchTextfieldView::OnFocus() { 523 views::Textfield::OnFocus(); 524 if (select_all_on_focus_) 525 SelectAll(true); 526} 527 528FindBarHost* FindBarView::find_bar_host() const { 529 return static_cast<FindBarHost*>(host()); 530} 531 532void FindBarView::OnThemeChanged() { 533 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 534 if (GetThemeProvider()) { 535 close_button_->SetBackground( 536 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TAB_TEXT), 537 rb.GetImageSkiaNamed(IDR_CLOSE_1), 538 rb.GetImageSkiaNamed(IDR_CLOSE_1_MASK)); 539 } 540} 541