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" |
11 #include "app/resource_bundle.h" | 11 #include "app/resource_bundle.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "chrome/browser/browser_theme_provider.h" | 13 #include "chrome/browser/browser_theme_provider.h" |
14 #include "chrome/browser/find_bar_controller.h" | 14 #include "chrome/browser/find_bar_controller.h" |
15 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
16 #include "chrome/browser/views/find_bar_win.h" | 16 #include "chrome/browser/views/find_bar_win.h" |
17 #include "chrome/browser/view_ids.h" | 17 #include "chrome/browser/view_ids.h" |
18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
19 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
20 #include "third_party/skia/include/effects/SkGradientShader.h" | 20 #include "third_party/skia/include/effects/SkGradientShader.h" |
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 = 1; |
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 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 find_text_->SetController(this); | |
94 AddChildView(find_text_); | 95 AddChildView(find_text_); |
95 | 96 |
96 match_count_text_ = new views::Label(); | 97 match_count_text_ = new views::Label(); |
97 match_count_text_->SetFont(rb.GetFont(ResourceBundle::BaseFont)); | 98 match_count_text_->SetFont(rb.GetFont(ResourceBundle::BaseFont)); |
98 match_count_text_->SetColor(kTextColorMatchCount); | 99 match_count_text_->SetColor(kTextColorMatchCount); |
99 match_count_text_->SetHorizontalAlignment(views::Label::ALIGN_CENTER); | 100 match_count_text_->SetHorizontalAlignment(views::Label::ALIGN_CENTER); |
100 AddChildView(match_count_text_); | 101 AddChildView(match_count_text_); |
101 | 102 |
102 // Create a focus forwarder view which sends focus to find_text_. | 103 // Create a focus forwarder view which sends focus to find_text_. |
103 focus_forwarder_view_ = new FocusForwarderView(find_text_); | 104 focus_forwarder_view_ = new FocusForwarderView(find_text_); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 // Otherwise we show an error background behind the match_count label. | 203 // Otherwise we show an error background behind the match_count label. |
203 match_count_text_->set_background( | 204 match_count_text_->set_background( |
204 views::Background::CreateSolidBackground(kBackgroundColorNoMatch)); | 205 views::Background::CreateSolidBackground(kBackgroundColorNoMatch)); |
205 match_count_text_->SetColor(kTextColorNoMatch); | 206 match_count_text_->SetColor(kTextColorNoMatch); |
206 } | 207 } |
207 | 208 |
208 // Make sure Find Next and Find Previous are enabled if we found any matches. | 209 // Make sure Find Next and Find Previous are enabled if we found any matches. |
209 find_previous_button_->SetEnabled(result.number_of_matches() > 0); | 210 find_previous_button_->SetEnabled(result.number_of_matches() > 0); |
210 find_next_button_->SetEnabled(result.number_of_matches() > 0); | 211 find_next_button_->SetEnabled(result.number_of_matches() > 0); |
211 | 212 |
212 Layout(); // The match_count label may have increased/decreased in size. | 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(); | |
213 } | 218 } |
214 | 219 |
215 void FindBarView::SetFocusAndSelection() { | 220 void FindBarView::SetFocusAndSelection() { |
216 find_text_->RequestFocus(); | 221 find_text_->RequestFocus(); |
217 if (!find_text_->text().empty()) { | 222 if (!find_text_->text().empty()) { |
218 find_text_->SelectAll(); | 223 find_text_->SelectAll(); |
219 | 224 |
220 find_previous_button_->SetEnabled(true); | 225 find_previous_button_->SetEnabled(true); |
221 find_next_button_->SetEnabled(true); | 226 find_next_button_->SetEnabled(true); |
222 } | 227 } |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 // Then, the FindPrevious button to the left the FindNext button. | 337 // Then, the FindPrevious button to the left the FindNext button. |
333 sz = find_previous_button_->GetPreferredSize(); | 338 sz = find_previous_button_->GetPreferredSize(); |
334 find_previous_button_->SetBounds(find_next_button_->x() - | 339 find_previous_button_->SetBounds(find_next_button_->x() - |
335 find_previous_button_->width(), | 340 find_previous_button_->width(), |
336 (height() - sz.height()) / 2, | 341 (height() - sz.height()) / 2, |
337 sz.width(), | 342 sz.width(), |
338 sz.height()); | 343 sz.height()); |
339 | 344 |
340 // Then the label showing the match count number. | 345 // Then the label showing the match count number. |
341 sz = match_count_text_->GetPreferredSize(); | 346 sz = match_count_text_->GetPreferredSize(); |
347 // We want to make sure the red "no-match" background almost completely fills | |
348 // up the amount of vertical space within the text box. We therefore fix the | |
349 // size relative to the button heights, which are fixed. We use the FindPrev | |
350 // button, which has a 1px outer margin, 1px border and we want to appear 1px | |
351 // below the border line so we subtract 3 for top and 3 for bottom. | |
352 sz.set_height(find_previous_button_->height() - 6); // Subtract 3px x 2. | |
353 | |
342 // We extend the label bounds a bit to give the background highlighting a bit | 354 // We extend the label bounds a bit to give the background highlighting a bit |
343 // of breathing room (margins around the text). | 355 // of breathing room (margins around the text). |
344 sz.Enlarge(kMatchCountExtraWidth, 0); | 356 sz.Enlarge(kMatchCountExtraWidth, 0); |
345 sz.set_width(std::max(kMatchCountMinWidth, static_cast<int>(sz.width()))); | 357 sz.set_width(std::max(kMatchCountMinWidth, static_cast<int>(sz.width()))); |
346 match_count_text_->SetBounds(find_previous_button_->x() - | 358 int match_count_x = find_previous_button_->x() - |
347 kWhiteSpaceAfterMatchCountLabel - | 359 kWhiteSpaceAfterMatchCountLabel - |
348 sz.width(), | 360 sz.width(); |
349 (height() - sz.height()) / 2 + 1, | 361 match_count_text_->SetBounds(match_count_x, |
362 (height() - sz.height()) / 2, | |
350 sz.width(), | 363 sz.width(), |
351 sz.height()); | 364 sz.height()); |
352 | 365 |
353 // And whatever space is left in between, gets filled up by the find edit box. | 366 // And whatever space is left in between, gets filled up by the find edit box. |
354 sz = find_text_->GetPreferredSize(); | 367 sz = find_text_->GetPreferredSize(); |
355 sz.set_width(match_count_text_->x() - kMarginLeftOfFindTextfield); | 368 sz.set_width(match_count_x - kMarginLeftOfFindTextfield); |
356 find_text_->SetBounds(match_count_text_->x() - sz.width(), | 369 find_text_->SetBounds(match_count_x - sz.width(), |
357 (height() - sz.height()) / 2 + 1, | 370 (height() - sz.height()) / 2 + 1, |
358 sz.width(), | 371 sz.width(), |
359 sz.height()); | 372 sz.height()); |
360 find_text_->SetController(this); | |
361 | 373 |
362 // The focus forwarder view is a hidden view that should cover the area | 374 // 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 | 375 // 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. | 376 // in that area we focus on the find text box. |
365 int find_text_edge = find_text_->x() + find_text_->width(); | 377 int find_text_edge = find_text_->x() + find_text_->width(); |
366 focus_forwarder_view_->SetBounds(find_text_edge, | 378 focus_forwarder_view_->SetBounds(find_text_edge, |
367 find_previous_button_->y(), | 379 find_previous_button_->y(), |
368 find_previous_button_->x() - | 380 find_previous_button_->x() - |
369 find_text_edge, | 381 find_text_edge, |
370 find_previous_button_->height()); | 382 find_previous_button_->height()); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
430 // in the Find box is changed right after the tab is destroyed. Otherwise, it | 442 // 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. | 443 // can lead to crashes, as exposed by automation testing in issue 8048. |
432 if (!controller->tab_contents()) | 444 if (!controller->tab_contents()) |
433 return; | 445 return; |
434 | 446 |
435 // When the user changes something in the text box we check the contents and | 447 // 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 | 448 // 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). | 449 // initiate search (even though old searches might be in progress). |
438 if (!new_contents.empty()) { | 450 if (!new_contents.empty()) { |
439 // The last two params here are forward (true) and case sensitive (false). | 451 // The last two params here are forward (true) and case sensitive (false). |
440 controller->tab_contents()->StartFinding(WideToUTF16(new_contents), true, fa lse); | 452 controller->tab_contents()->StartFinding(WideToUTF16(new_contents), |
Glen Murphy
2009/07/06 17:49:30
Double-check: this looks messed up (like the line
Finnur
2009/07/06 20:37:33
That's Rietveld for you. Because the line used to
| |
453 true, false); | |
441 } else { | 454 } else { |
442 // The textbox is empty so we reset. true = clear selection on page. | 455 // The textbox is empty so we reset. true = clear selection on page. |
443 controller->tab_contents()->StopFinding(true); | 456 controller->tab_contents()->StopFinding(true); |
444 UpdateForResult(controller->tab_contents()->find_result(), string16()); | 457 UpdateForResult(controller->tab_contents()->find_result(), string16()); |
445 } | 458 } |
446 } | 459 } |
447 | 460 |
448 bool FindBarView::HandleKeystroke(views::Textfield* sender, | 461 bool FindBarView::HandleKeystroke(views::Textfield* sender, |
449 const views::Textfield::Keystroke& key) { | 462 const views::Textfield::Keystroke& key) { |
450 // If the dialog is not visible, there is no reason to process keyboard input. | 463 // If the dialog is not visible, there is no reason to process keyboard input. |
(...skipping 29 matching lines...) Expand all Loading... | |
480 } | 493 } |
481 | 494 |
482 bool FindBarView::FocusForwarderView::OnMousePressed( | 495 bool FindBarView::FocusForwarderView::OnMousePressed( |
483 const views::MouseEvent& event) { | 496 const views::MouseEvent& event) { |
484 if (view_to_focus_on_mousedown_) { | 497 if (view_to_focus_on_mousedown_) { |
485 view_to_focus_on_mousedown_->ClearSelection(); | 498 view_to_focus_on_mousedown_->ClearSelection(); |
486 view_to_focus_on_mousedown_->RequestFocus(); | 499 view_to_focus_on_mousedown_->RequestFocus(); |
487 } | 500 } |
488 return true; | 501 return true; |
489 } | 502 } |
OLD | NEW |