OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/views/find_bar_view.h" | 5 #include "chrome/browser/views/find_bar_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "app/gfx/canvas.h" | 9 #include "app/gfx/canvas.h" |
10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "views/background.h" | 21 #include "views/background.h" |
22 #include "views/controls/button/image_button.h" | 22 #include "views/controls/button/image_button.h" |
23 #include "views/controls/label.h" | 23 #include "views/controls/label.h" |
24 | 24 |
25 // The amount of whitespace to have before the find button. | 25 // The amount of whitespace to have before the find button. |
26 static const int kWhiteSpaceAfterMatchCountLabel = 3; | 26 static const int kWhiteSpaceAfterMatchCountLabel = 3; |
27 | 27 |
28 // The margins around the search field and the close button. | 28 // The margins around the search field and the close button. |
29 static const int kMarginLeftOfCloseButton = 3; | 29 static const int kMarginLeftOfCloseButton = 3; |
30 static const int kMarginRightOfCloseButton = 7; | 30 static const int kMarginRightOfCloseButton = 7; |
31 static const int kMarginLeftOfFindTextField = 12; | 31 static const int kMarginLeftOfFindTextfield = 12; |
32 | 32 |
33 // The margins around the match count label (We add extra space so that the | 33 // The margins around the match count label (We add extra space so that the |
34 // background highlight extends beyond just the text). | 34 // background highlight extends beyond just the text). |
35 static const int kMatchCountExtraWidth = 9; | 35 static const int kMatchCountExtraWidth = 9; |
36 | 36 |
37 // Minimum width for the match count label. | 37 // Minimum width for the match count label. |
38 static const int kMatchCountMinWidth = 30; | 38 static const int kMatchCountMinWidth = 30; |
39 | 39 |
40 // The text color for the match count label. | 40 // The text color for the match count label. |
41 static const SkColor kTextColorMatchCount = SkColorSetRGB(178, 178, 178); | 41 static const SkColor kTextColorMatchCount = SkColorSetRGB(178, 178, 178); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 : container_(container), | 80 : container_(container), |
81 find_text_(NULL), | 81 find_text_(NULL), |
82 match_count_text_(NULL), | 82 match_count_text_(NULL), |
83 focus_forwarder_view_(NULL), | 83 focus_forwarder_view_(NULL), |
84 find_previous_button_(NULL), | 84 find_previous_button_(NULL), |
85 find_next_button_(NULL), | 85 find_next_button_(NULL), |
86 close_button_(NULL), | 86 close_button_(NULL), |
87 animation_offset_(0) { | 87 animation_offset_(0) { |
88 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 88 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
89 | 89 |
90 find_text_ = new views::TextField(); | 90 find_text_ = new views::Textfield(); |
91 find_text_->SetID(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); | 91 find_text_->SetID(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); |
92 find_text_->SetFont(rb.GetFont(ResourceBundle::BaseFont)); | 92 find_text_->SetFont(rb.GetFont(ResourceBundle::BaseFont)); |
93 find_text_->set_default_width_in_chars(kDefaultCharWidth); | 93 find_text_->set_default_width_in_chars(kDefaultCharWidth); |
94 AddChildView(find_text_); | 94 AddChildView(find_text_); |
95 | 95 |
96 match_count_text_ = new views::Label(); | 96 match_count_text_ = new views::Label(); |
97 match_count_text_->SetFont(rb.GetFont(ResourceBundle::BaseFont)); | 97 match_count_text_->SetFont(rb.GetFont(ResourceBundle::BaseFont)); |
98 match_count_text_->SetColor(kTextColorMatchCount); | 98 match_count_text_->SetColor(kTextColorMatchCount); |
99 match_count_text_->SetHorizontalAlignment(views::Label::ALIGN_CENTER); | 99 match_count_text_->SetHorizontalAlignment(views::Label::ALIGN_CENTER); |
100 AddChildView(match_count_text_); | 100 AddChildView(match_count_text_); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 canvas->TileImageInt(*kDialog_middle, | 249 canvas->TileImageInt(*kDialog_middle, |
250 kDialog_left->width(), | 250 kDialog_left->width(), |
251 0, | 251 0, |
252 lb.width() - | 252 lb.width() - |
253 kDialog_left->width() - | 253 kDialog_left->width() - |
254 kDialog_right->width(), | 254 kDialog_right->width(), |
255 kDialog_middle->height()); | 255 kDialog_middle->height()); |
256 | 256 |
257 canvas->DrawBitmapInt(*kDialog_right, lb.right() - kDialog_right->width(), 0); | 257 canvas->DrawBitmapInt(*kDialog_right, lb.right() - kDialog_right->width(), 0); |
258 | 258 |
259 // Then we draw the background image for the Find TextField. We start by | 259 // Then we draw the background image for the Find Textfield. We start by |
260 // calculating the position of background images for the Find text box. | 260 // calculating the position of background images for the Find text box. |
261 gfx::Rect find_text_rect; | 261 gfx::Rect find_text_rect; |
262 gfx::Rect back_button_rect; | 262 gfx::Rect back_button_rect; |
263 int x = 0; // x coordinate of the curved edge background image. | 263 int x = 0; // x coordinate of the curved edge background image. |
264 int w = 0; // width of the background image for the text field. | 264 int w = 0; // width of the background image for the text field. |
265 if (UILayoutIsRightToLeft()) { | 265 if (UILayoutIsRightToLeft()) { |
266 find_text_rect = find_text_->GetBounds(APPLY_MIRRORING_TRANSFORMATION); | 266 find_text_rect = find_text_->GetBounds(APPLY_MIRRORING_TRANSFORMATION); |
267 back_button_rect = | 267 back_button_rect = |
268 find_previous_button_->GetBounds(APPLY_MIRRORING_TRANSFORMATION); | 268 find_previous_button_->GetBounds(APPLY_MIRRORING_TRANSFORMATION); |
269 x = find_text_rect.right(); | 269 x = find_text_rect.right(); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 sz.set_width(std::max(kMatchCountMinWidth, static_cast<int>(sz.width()))); | 345 sz.set_width(std::max(kMatchCountMinWidth, static_cast<int>(sz.width()))); |
346 match_count_text_->SetBounds(find_previous_button_->x() - | 346 match_count_text_->SetBounds(find_previous_button_->x() - |
347 kWhiteSpaceAfterMatchCountLabel - | 347 kWhiteSpaceAfterMatchCountLabel - |
348 sz.width(), | 348 sz.width(), |
349 (height() - sz.height()) / 2 + 1, | 349 (height() - sz.height()) / 2 + 1, |
350 sz.width(), | 350 sz.width(), |
351 sz.height()); | 351 sz.height()); |
352 | 352 |
353 // And whatever space is left in between, gets filled up by the find edit box. | 353 // And whatever space is left in between, gets filled up by the find edit box. |
354 sz = find_text_->GetPreferredSize(); | 354 sz = find_text_->GetPreferredSize(); |
355 sz.set_width(match_count_text_->x() - kMarginLeftOfFindTextField); | 355 sz.set_width(match_count_text_->x() - kMarginLeftOfFindTextfield); |
356 find_text_->SetBounds(match_count_text_->x() - sz.width(), | 356 find_text_->SetBounds(match_count_text_->x() - sz.width(), |
357 (height() - sz.height()) / 2 + 1, | 357 (height() - sz.height()) / 2 + 1, |
358 sz.width(), | 358 sz.width(), |
359 sz.height()); | 359 sz.height()); |
360 find_text_->SetController(this); | 360 find_text_->SetController(this); |
361 | 361 |
362 // The focus forwarder view is a hidden view that should cover the area | 362 // The focus forwarder view is a hidden view that should cover the area |
363 // between the find text box and the find button so that when the user clicks | 363 // between the find text box and the find button so that when the user clicks |
364 // in that area we focus on the find text box. | 364 // in that area we focus on the find text box. |
365 int find_text_edge = find_text_->x() + find_text_->width(); | 365 int find_text_edge = find_text_->x() + find_text_->width(); |
(...skipping 10 matching lines...) Expand all Loading... |
376 find_text_->RemoveBorder(); // We draw our own border (a background image). | 376 find_text_->RemoveBorder(); // We draw our own border (a background image). |
377 } | 377 } |
378 } | 378 } |
379 | 379 |
380 gfx::Size FindBarView::GetPreferredSize() { | 380 gfx::Size FindBarView::GetPreferredSize() { |
381 gfx::Size prefsize = find_text_->GetPreferredSize(); | 381 gfx::Size prefsize = find_text_->GetPreferredSize(); |
382 prefsize.set_height(kDialog_middle->height()); | 382 prefsize.set_height(kDialog_middle->height()); |
383 | 383 |
384 // Add up all the preferred sizes and margins of the rest of the controls. | 384 // Add up all the preferred sizes and margins of the rest of the controls. |
385 prefsize.Enlarge(kMarginLeftOfCloseButton + kMarginRightOfCloseButton + | 385 prefsize.Enlarge(kMarginLeftOfCloseButton + kMarginRightOfCloseButton + |
386 kMarginLeftOfFindTextField, | 386 kMarginLeftOfFindTextfield, |
387 0); | 387 0); |
388 prefsize.Enlarge(find_previous_button_->GetPreferredSize().width(), 0); | 388 prefsize.Enlarge(find_previous_button_->GetPreferredSize().width(), 0); |
389 prefsize.Enlarge(find_next_button_->GetPreferredSize().width(), 0); | 389 prefsize.Enlarge(find_next_button_->GetPreferredSize().width(), 0); |
390 prefsize.Enlarge(close_button_->GetPreferredSize().width(), 0); | 390 prefsize.Enlarge(close_button_->GetPreferredSize().width(), 0); |
391 return prefsize; | 391 return prefsize; |
392 } | 392 } |
393 | 393 |
394 //////////////////////////////////////////////////////////////////////////////// | 394 //////////////////////////////////////////////////////////////////////////////// |
395 // FindBarView, views::ButtonListener implementation: | 395 // FindBarView, views::ButtonListener implementation: |
396 | 396 |
(...skipping 16 matching lines...) Expand all Loading... |
413 case CLOSE_TAG: | 413 case CLOSE_TAG: |
414 container_->GetFindBarController()->EndFindSession(); | 414 container_->GetFindBarController()->EndFindSession(); |
415 break; | 415 break; |
416 default: | 416 default: |
417 NOTREACHED() << L"Unknown button"; | 417 NOTREACHED() << L"Unknown button"; |
418 break; | 418 break; |
419 } | 419 } |
420 } | 420 } |
421 | 421 |
422 //////////////////////////////////////////////////////////////////////////////// | 422 //////////////////////////////////////////////////////////////////////////////// |
423 // FindBarView, views::TextField::Controller implementation: | 423 // FindBarView, views::Textfield::Controller implementation: |
424 | 424 |
425 void FindBarView::ContentsChanged(views::TextField* sender, | 425 void FindBarView::ContentsChanged(views::Textfield* sender, |
426 const std::wstring& new_contents) { | 426 const std::wstring& new_contents) { |
427 FindBarController* controller = container_->GetFindBarController(); | 427 FindBarController* controller = container_->GetFindBarController(); |
428 DCHECK(controller); | 428 DCHECK(controller); |
429 // We must guard against a NULL tab_contents, which can happen if the text | 429 // We must guard against a NULL tab_contents, which can happen if the text |
430 // in the Find box is changed right after the tab is destroyed. Otherwise, it | 430 // in the Find box is changed right after the tab is destroyed. Otherwise, it |
431 // can lead to crashes, as exposed by automation testing in issue 8048. | 431 // can lead to crashes, as exposed by automation testing in issue 8048. |
432 if (!controller->tab_contents()) | 432 if (!controller->tab_contents()) |
433 return; | 433 return; |
434 | 434 |
435 // When the user changes something in the text box we check the contents and | 435 // When the user changes something in the text box we check the contents and |
436 // if the textbox contains something we set it as the new search string and | 436 // if the textbox contains something we set it as the new search string and |
437 // initiate search (even though old searches might be in progress). | 437 // initiate search (even though old searches might be in progress). |
438 if (!new_contents.empty()) { | 438 if (!new_contents.empty()) { |
439 // The last two params here are forward (true) and case sensitive (false). | 439 // The last two params here are forward (true) and case sensitive (false). |
440 controller->tab_contents()->StartFinding(WideToUTF16(new_contents), true, fa
lse); | 440 controller->tab_contents()->StartFinding(WideToUTF16(new_contents), true, fa
lse); |
441 } else { | 441 } else { |
442 // The textbox is empty so we reset. true = clear selection on page. | 442 // The textbox is empty so we reset. true = clear selection on page. |
443 controller->tab_contents()->StopFinding(true); | 443 controller->tab_contents()->StopFinding(true); |
444 UpdateForResult(controller->tab_contents()->find_result(), string16()); | 444 UpdateForResult(controller->tab_contents()->find_result(), string16()); |
445 } | 445 } |
446 } | 446 } |
447 | 447 |
448 bool FindBarView::HandleKeystroke(views::TextField* sender, | 448 bool FindBarView::HandleKeystroke(views::Textfield* sender, |
449 const views::TextField::Keystroke& key) { | 449 const views::Textfield::Keystroke& key) { |
450 // If the dialog is not visible, there is no reason to process keyboard input. | 450 // If the dialog is not visible, there is no reason to process keyboard input. |
451 if (!container_->IsVisible()) | 451 if (!container_->IsVisible()) |
452 return false; | 452 return false; |
453 | 453 |
454 // TODO(port): Handle this for other platforms. | 454 // TODO(port): Handle this for other platforms. |
455 #if defined(OS_WIN) | 455 #if defined(OS_WIN) |
456 if (container_->MaybeForwardKeystrokeToWebpage(key.message, key.key, | 456 if (container_->MaybeForwardKeystrokeToWebpage(key.message, key.key, |
457 key.flags)) | 457 key.flags)) |
458 return true; // Handled, we are done! | 458 return true; // Handled, we are done! |
459 | 459 |
460 if (views::TextField::IsKeystrokeEnter(key)) { | 460 if (views::Textfield::IsKeystrokeEnter(key)) { |
461 // Pressing Return/Enter starts the search (unless text box is empty). | 461 // Pressing Return/Enter starts the search (unless text box is empty). |
462 std::wstring find_string = find_text_->GetText(); | 462 std::wstring find_string = find_text_->GetText(); |
463 if (!find_string.empty()) { | 463 if (!find_string.empty()) { |
464 // Search forwards for enter, backwards for shift-enter. | 464 // Search forwards for enter, backwards for shift-enter. |
465 container_->GetFindBarController()->tab_contents()->StartFinding( | 465 container_->GetFindBarController()->tab_contents()->StartFinding( |
466 find_string, | 466 find_string, |
467 GetKeyState(VK_SHIFT) >= 0, | 467 GetKeyState(VK_SHIFT) >= 0, |
468 false); // Not case sensitive. | 468 false); // Not case sensitive. |
469 } | 469 } |
470 } | 470 } |
471 #endif | 471 #endif |
472 | 472 |
473 return false; | 473 return false; |
474 } | 474 } |
475 | 475 |
476 void FindBarView::ResetMatchCountBackground() { | 476 void FindBarView::ResetMatchCountBackground() { |
477 match_count_text_->set_background( | 477 match_count_text_->set_background( |
478 views::Background::CreateSolidBackground(kBackgroundColorMatch)); | 478 views::Background::CreateSolidBackground(kBackgroundColorMatch)); |
479 match_count_text_->SetColor(kTextColorMatchCount); | 479 match_count_text_->SetColor(kTextColorMatchCount); |
480 } | 480 } |
481 | 481 |
482 bool FindBarView::FocusForwarderView::OnMousePressed( | 482 bool FindBarView::FocusForwarderView::OnMousePressed( |
483 const views::MouseEvent& event) { | 483 const views::MouseEvent& event) { |
484 if (view_to_focus_on_mousedown_) { | 484 if (view_to_focus_on_mousedown_) { |
485 view_to_focus_on_mousedown_->ClearSelection(); | 485 view_to_focus_on_mousedown_->ClearSelection(); |
486 view_to_focus_on_mousedown_->RequestFocus(); | 486 view_to_focus_on_mousedown_->RequestFocus(); |
487 } | 487 } |
488 return true; | 488 return true; |
489 } | 489 } |
OLD | NEW |