Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: chrome/browser/views/location_bar_view.cc

Issue 7344: Convert GetPreferredSize from:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/location_bar_view.h" 5 #include "chrome/browser/views/location_bar_view.h"
6 6
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "chrome/app/chrome_dll_resource.h" 9 #include "chrome/app/chrome_dll_resource.h"
10 #include "chrome/app/theme/theme_resources.h" 10 #include "chrome/app/theme/theme_resources.h"
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 DCHECK(profile); 197 DCHECK(profile);
198 if (profile_ != profile) { 198 if (profile_ != profile) {
199 profile_ = profile; 199 profile_ = profile;
200 location_entry_->model()->SetProfile(profile); 200 location_entry_->model()->SetProfile(profile);
201 selected_keyword_view_.set_profile(profile); 201 selected_keyword_view_.set_profile(profile);
202 keyword_hint_view_.set_profile(profile); 202 keyword_hint_view_.set_profile(profile);
203 security_image_view_.set_profile(profile); 203 security_image_view_.set_profile(profile);
204 } 204 }
205 } 205 }
206 206
207 void LocationBarView::GetPreferredSize(CSize *out) { 207 gfx::Size LocationBarView::GetPreferredSize() {
208 CSize size; 208 return gfx::Size(
209 security_image_view_.GetPreferredSize(&size); 209 0,
210 out->cx = 0; 210 std::max(
211 211 (popup_window_mode_ ? kPopupBackgroundCenter
212 out->cy = std::max( 212 : kBackground)->height(),
213 (popup_window_mode_ ? kPopupBackgroundCenter : kBackground)->height(), 213 security_image_view_.GetPreferredSize().width()));
214 static_cast<int>(size.cy));
215 } 214 }
216 215
217 void LocationBarView::DidChangeBounds(const CRect& previous, 216 void LocationBarView::DidChangeBounds(const CRect& previous,
218 const CRect& current) { 217 const CRect& current) {
219 Layout(); 218 Layout();
220 } 219 }
221 220
222 void LocationBarView::Layout() { 221 void LocationBarView::Layout() {
223 DoLayout(true); 222 DoLayout(true);
224 } 223 }
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 void LocationBarView::DoLayout(const bool force_layout) { 367 void LocationBarView::DoLayout(const bool force_layout) {
369 if (!location_entry_.get()) 368 if (!location_entry_.get())
370 return; 369 return;
371 370
372 RECT formatting_rect; 371 RECT formatting_rect;
373 location_entry_->GetRect(&formatting_rect); 372 location_entry_->GetRect(&formatting_rect);
374 RECT edit_bounds; 373 RECT edit_bounds;
375 location_entry_->GetClientRect(&edit_bounds); 374 location_entry_->GetClientRect(&edit_bounds);
376 375
377 int entry_width = width() - kEntryPadding - kEntryPadding; 376 int entry_width = width() - kEntryPadding - kEntryPadding;
378 CSize security_image_size; 377 gfx::Size security_image_size;
379 if (security_image_view_.IsVisible()) { 378 if (security_image_view_.IsVisible()) {
380 security_image_view_.GetPreferredSize(&security_image_size); 379 security_image_size = security_image_view_.GetPreferredSize();
381 entry_width -= security_image_size.cx; 380 entry_width -= security_image_size.width();
382 } 381 }
383 CSize info_label_size; 382 gfx::Size info_label_size;
384 if (info_label_.IsVisible()) { 383 if (info_label_.IsVisible()) {
385 info_label_.GetPreferredSize(&info_label_size); 384 info_label_size = info_label_.GetPreferredSize();
386 entry_width -= (info_label_size.cx + kInnerPadding); 385 entry_width -= (info_label_size.width() + kInnerPadding);
387 } 386 }
388 387
389 const int max_edit_width = entry_width - formatting_rect.left - 388 const int max_edit_width = entry_width - formatting_rect.left -
390 (edit_bounds.right - formatting_rect.right); 389 (edit_bounds.right - formatting_rect.right);
391 if (max_edit_width < 0) 390 if (max_edit_width < 0)
392 return; 391 return;
393 const int text_width = TextDisplayWidth(); 392 const int text_width = TextDisplayWidth();
394 bool needs_layout = force_layout; 393 bool needs_layout = force_layout;
395 needs_layout |= AdjustHints(text_width, max_edit_width); 394 needs_layout |= AdjustHints(text_width, max_edit_width);
396 395
397 if (!needs_layout) 396 if (!needs_layout)
398 return; 397 return;
399 398
400 // TODO(sky): baseline layout. 399 // TODO(sky): baseline layout.
401 const SkBitmap* background = popup_window_mode_ ? kPopupBackgroundCenter 400 const SkBitmap* background = popup_window_mode_ ? kPopupBackgroundCenter
402 : kBackground; 401 : kBackground;
403 int bh = background->height(); 402 int bh = background->height();
404 int location_y = ((height() - bh) / 2) + kTextVertMargin; 403 int location_y = ((height() - bh) / 2) + kTextVertMargin;
405 int location_height = bh - (2 * kTextVertMargin); 404 int location_height = bh - (2 * kTextVertMargin);
406 if (info_label_.IsVisible()) { 405 if (info_label_.IsVisible()) {
407 info_label_.SetBounds(width() - kEntryPadding - info_label_size.cx, 406 info_label_.SetBounds(width() - kEntryPadding - info_label_size.width(),
408 location_y, 407 location_y,
409 info_label_size.cx, location_height); 408 info_label_size.width(), location_height);
410 } 409 }
411 if (security_image_view_.IsVisible()) { 410 if (security_image_view_.IsVisible()) {
412 const int info_label_width = info_label_size.cx ? 411 const int info_label_width = info_label_size.width() ?
413 info_label_size.cx + kInnerPadding : 0; 412 info_label_size.width() + kInnerPadding : 0;
414 security_image_view_.SetBounds(width() - kEntryPadding - 413 security_image_view_.SetBounds(width() - kEntryPadding -
415 info_label_width - 414 info_label_width -
416 security_image_size.cx, 415 security_image_size.width(),
417 location_y, 416 location_y,
418 security_image_size.cx, location_height); 417 security_image_size.width(), location_height) ;
419 } 418 }
420 gfx::Rect location_bounds(kEntryPadding, location_y, entry_width, 419 gfx::Rect location_bounds(kEntryPadding, location_y, entry_width,
421 location_height); 420 location_height);
422 if (selected_keyword_view_.IsVisible()) { 421 if (selected_keyword_view_.IsVisible()) {
423 LayoutView(true, &selected_keyword_view_, text_width, max_edit_width, 422 LayoutView(true, &selected_keyword_view_, text_width, max_edit_width,
424 &location_bounds); 423 &location_bounds);
425 } else if (keyword_hint_view_.IsVisible()) { 424 } else if (keyword_hint_view_.IsVisible()) {
426 LayoutView(false, &keyword_hint_view_, text_width, max_edit_width, 425 LayoutView(false, &keyword_hint_view_, text_width, max_edit_width,
427 &location_bounds); 426 &location_bounds);
428 } else if (type_to_search_view_.IsVisible()) { 427 } else if (type_to_search_view_.IsVisible()) {
(...skipping 19 matching lines...) Expand all
448 location_entry_->GetScrollPos(&scroll_position); 447 location_entry_->GetScrollPos(&scroll_position);
449 const int position_x = last_char_position.x + scroll_position.x; 448 const int position_x = last_char_position.x + scroll_position.x;
450 return UILayoutIsRightToLeft() ? width() - position_x : position_x; 449 return UILayoutIsRightToLeft() ? width() - position_x : position_x;
451 } 450 }
452 451
453 bool LocationBarView::UsePref(int pref_width, int text_width, int max_width) { 452 bool LocationBarView::UsePref(int pref_width, int text_width, int max_width) {
454 return (pref_width + kInnerPadding + text_width <= max_width); 453 return (pref_width + kInnerPadding + text_width <= max_width);
455 } 454 }
456 455
457 bool LocationBarView::NeedsResize(View* view, int text_width, int max_width) { 456 bool LocationBarView::NeedsResize(View* view, int text_width, int max_width) {
458 CSize size; 457 gfx::Size size = view->GetPreferredSize();
459 view->GetPreferredSize(&size); 458 if (!UsePref(size.width(), text_width, max_width))
460 if (!UsePref(size.cx, text_width, max_width)) 459 size = view->GetMinimumSize();
461 view->GetMinimumSize(&size); 460 return (view->width() != size.width());
462 return (view->width() != size.cx);
463 } 461 }
464 462
465 bool LocationBarView::AdjustHints(int text_width, int max_width) { 463 bool LocationBarView::AdjustHints(int text_width, int max_width) {
466 const std::wstring keyword(location_entry_->model()->keyword()); 464 const std::wstring keyword(location_entry_->model()->keyword());
467 const bool is_keyword_hint(location_entry_->model()->is_keyword_hint()); 465 const bool is_keyword_hint(location_entry_->model()->is_keyword_hint());
468 const bool show_selected_keyword = !keyword.empty() && !is_keyword_hint; 466 const bool show_selected_keyword = !keyword.empty() && !is_keyword_hint;
469 const bool show_keyword_hint = !keyword.empty() && is_keyword_hint; 467 const bool show_keyword_hint = !keyword.empty() && is_keyword_hint;
470 bool show_search_hint(location_entry_->model()->show_search_hint()); 468 bool show_search_hint(location_entry_->model()->show_search_hint());
471 DCHECK(keyword.empty() || !show_search_hint); 469 DCHECK(keyword.empty() || !show_search_hint);
472 470
473 if (show_search_hint) { 471 if (show_search_hint) {
474 // Only show type to search if all the text fits. 472 // Only show type to search if all the text fits.
475 CSize view_pref; 473 gfx::Size view_pref = type_to_search_view_.GetPreferredSize();
476 type_to_search_view_.GetPreferredSize(&view_pref); 474 show_search_hint = UsePref(view_pref.width(), text_width, max_width);
477 show_search_hint = UsePref(view_pref.cx, text_width, max_width);
478 } 475 }
479 476
480 // NOTE: This isn't just one big || statement as ToggleVisibility MUST be 477 // NOTE: This isn't just one big || statement as ToggleVisibility MUST be
481 // invoked for each view. 478 // invoked for each view.
482 bool needs_layout = false; 479 bool needs_layout = false;
483 needs_layout |= ToggleVisibility(show_selected_keyword, 480 needs_layout |= ToggleVisibility(show_selected_keyword,
484 &selected_keyword_view_); 481 &selected_keyword_view_);
485 needs_layout |= ToggleVisibility(show_keyword_hint, &keyword_hint_view_); 482 needs_layout |= ToggleVisibility(show_keyword_hint, &keyword_hint_view_);
486 needs_layout |= ToggleVisibility(show_search_hint, &type_to_search_view_); 483 needs_layout |= ToggleVisibility(show_search_hint, &type_to_search_view_);
487 if (show_selected_keyword) { 484 if (show_selected_keyword) {
(...skipping 10 matching lines...) Expand all
498 needs_layout |= NeedsResize(&keyword_hint_view_, text_width, max_width); 495 needs_layout |= NeedsResize(&keyword_hint_view_, text_width, max_width);
499 } 496 }
500 497
501 return needs_layout; 498 return needs_layout;
502 } 499 }
503 500
504 void LocationBarView::LayoutView(bool leading, ChromeViews::View* view, 501 void LocationBarView::LayoutView(bool leading, ChromeViews::View* view,
505 int text_width, int max_width, 502 int text_width, int max_width,
506 gfx::Rect* bounds) { 503 gfx::Rect* bounds) {
507 DCHECK(view && bounds); 504 DCHECK(view && bounds);
508 CSize view_size(0, 0); 505 gfx::Size view_size = view->GetPreferredSize();
509 view->GetPreferredSize(&view_size); 506 if (!UsePref(view_size.width(), text_width, max_width))
510 if (!UsePref(view_size.cx, text_width, max_width)) 507 view_size = view->GetMinimumSize();
511 view->GetMinimumSize(&view_size); 508 if (view_size.width() + kInnerPadding < bounds->width()) {
512 if (view_size.cx + kInnerPadding < bounds->width()) {
513 view->SetVisible(true); 509 view->SetVisible(true);
514 if (leading) { 510 if (leading) {
515 view->SetBounds(bounds->x(), bounds->y(), view_size.cx, bounds->height()); 511 view->SetBounds(bounds->x(), bounds->y(), view_size.width(),
516 bounds->Offset(view_size.cx + kInnerPadding, 0); 512 bounds->height());
513 bounds->Offset(view_size.width() + kInnerPadding, 0);
517 } else { 514 } else {
518 view->SetBounds(bounds->right() - view_size.cx, bounds->y(), 515 view->SetBounds(bounds->right() - view_size.width(), bounds->y(),
519 view_size.cx, bounds->height()); 516 view_size.width(), bounds->height());
520 } 517 }
521 bounds->set_width(bounds->width() - view_size.cx - kInnerPadding); 518 bounds->set_width(bounds->width() - view_size.width() - kInnerPadding);
522 } else { 519 } else {
523 view->SetVisible(false); 520 view->SetVisible(false);
524 } 521 }
525 } 522 }
526 523
527 void LocationBarView::SetSecurityIcon(ToolbarModel::Icon icon) { 524 void LocationBarView::SetSecurityIcon(ToolbarModel::Icon icon) {
528 switch (icon) { 525 switch (icon) {
529 case ToolbarModel::LOCK_ICON: 526 case ToolbarModel::LOCK_ICON:
530 security_image_view_.SetImageShown(SecurityImageView::LOCK); 527 security_image_view_.SetImageShown(SecurityImageView::LOCK);
531 security_image_view_.SetVisible(true); 528 security_image_view_.SetVisible(true);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 full_label_.SetFont(font); 631 full_label_.SetFont(font);
635 partial_label_.SetFont(font); 632 partial_label_.SetFont(font);
636 } 633 }
637 634
638 void LocationBarView::SelectedKeywordView::Paint(ChromeCanvas* canvas) { 635 void LocationBarView::SelectedKeywordView::Paint(ChromeCanvas* canvas) {
639 canvas->TranslateInt(0, kBackgroundYOffset); 636 canvas->TranslateInt(0, kBackgroundYOffset);
640 background_painter_.Paint(width(), height() - kTopInset, canvas); 637 background_painter_.Paint(width(), height() - kTopInset, canvas);
641 canvas->TranslateInt(0, -kBackgroundYOffset); 638 canvas->TranslateInt(0, -kBackgroundYOffset);
642 } 639 }
643 640
644 void LocationBarView::SelectedKeywordView::GetPreferredSize(CSize* size) { 641 gfx::Size LocationBarView::SelectedKeywordView::GetPreferredSize() {
645 full_label_.GetPreferredSize(size); 642 return full_label_.GetPreferredSize();
646 } 643 }
647 644
648 void LocationBarView::SelectedKeywordView::GetMinimumSize(CSize* size) { 645 gfx::Size LocationBarView::SelectedKeywordView::GetMinimumSize() {
649 partial_label_.GetMinimumSize(size); 646 return partial_label_.GetMinimumSize();
650 } 647 }
651 648
652 void LocationBarView::SelectedKeywordView::DidChangeBounds( 649 void LocationBarView::SelectedKeywordView::DidChangeBounds(
653 const CRect& previous, 650 const CRect& previous,
654 const CRect& current) { 651 const CRect& current) {
655 Layout(); 652 Layout();
656 } 653 }
657 654
658 void LocationBarView::SelectedKeywordView::Layout() { 655 void LocationBarView::SelectedKeywordView::Layout() {
659 CSize pref; 656 gfx::Size pref = GetPreferredSize();
660 GetPreferredSize(&pref); 657 bool at_pref = (width() == pref.width());
661 bool at_pref = (width() == pref.cx);
662 if (at_pref) 658 if (at_pref)
663 full_label_.SetBounds(0, 0, width(), height()); 659 full_label_.SetBounds(0, 0, width(), height());
664 else 660 else
665 partial_label_.SetBounds(0, 0, width(), height()); 661 partial_label_.SetBounds(0, 0, width(), height());
666 full_label_.SetVisible(at_pref); 662 full_label_.SetVisible(at_pref);
667 partial_label_.SetVisible(!at_pref); 663 partial_label_.SetVisible(!at_pref);
668 } 664 }
669 665
670 void LocationBarView::SelectedKeywordView::SetKeyword( 666 void LocationBarView::SelectedKeywordView::SetKeyword(
671 const std::wstring& keyword) { 667 const std::wstring& keyword) {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 gfx::Rect tab_button_bounds(image_x, 761 gfx::Rect tab_button_bounds(image_x,
766 kTabImageYOffset, 762 kTabImageYOffset,
767 kTabButtonBitmap->width(), 763 kTabButtonBitmap->width(),
768 kTabButtonBitmap->height()); 764 kTabButtonBitmap->height());
769 tab_button_bounds.set_x(MirroredLeftPointForRect(tab_button_bounds)); 765 tab_button_bounds.set_x(MirroredLeftPointForRect(tab_button_bounds));
770 canvas->DrawBitmapInt(*kTabButtonBitmap, 766 canvas->DrawBitmapInt(*kTabButtonBitmap,
771 tab_button_bounds.x(), 767 tab_button_bounds.x(),
772 tab_button_bounds.y()); 768 tab_button_bounds.y());
773 } 769 }
774 770
775 void LocationBarView::KeywordHintView::GetPreferredSize(CSize *out) { 771 gfx::Size LocationBarView::KeywordHintView::GetPreferredSize() {
776 // TODO(sky): currently height doesn't matter, once baseline support is 772 // TODO(sky): currently height doesn't matter, once baseline support is
777 // added this should check baselines. 773 // added this should check baselines.
778 leading_label_.GetPreferredSize(out); 774 gfx::Size prefsize = leading_label_.GetPreferredSize();
779 int width = out->cx; 775 int width = prefsize.width();
780 width += kTabButtonBitmap->width(); 776 width += kTabButtonBitmap->width();
781 trailing_label_.GetPreferredSize(out); 777 prefsize = trailing_label_.GetPreferredSize();
782 width += out->cx; 778 width += prefsize.width();
783 out->cx = width; 779 return gfx::Size(width, prefsize.height());
784 } 780 }
785 781
786 void LocationBarView::KeywordHintView::GetMinimumSize(CSize* out) { 782 gfx::Size LocationBarView::KeywordHintView::GetMinimumSize() {
787 // TODO(sky): currently height doesn't matter, once baseline support is 783 // TODO(sky): currently height doesn't matter, once baseline support is
788 // added this should check baselines. 784 // added this should check baselines.
789 out->cx = kTabButtonBitmap->width(); 785 return gfx::Size(kTabButtonBitmap->width(), 0);
790 } 786 }
791 787
792 void LocationBarView::KeywordHintView::Layout() { 788 void LocationBarView::KeywordHintView::Layout() {
793 // TODO(sky): baseline layout. 789 // TODO(sky): baseline layout.
794 bool show_labels = (width() != kTabButtonBitmap->width()); 790 bool show_labels = (width() != kTabButtonBitmap->width());
795 791
796 leading_label_.SetVisible(show_labels); 792 leading_label_.SetVisible(show_labels);
797 trailing_label_.SetVisible(show_labels); 793 trailing_label_.SetVisible(show_labels);
798 int x = 0; 794 int x = 0;
799 CSize pref; 795 gfx::Size pref;
800 796
801 if (show_labels) { 797 if (show_labels) {
802 leading_label_.GetPreferredSize(&pref); 798 pref = leading_label_.GetPreferredSize();
803 leading_label_.SetBounds(x, 0, pref.cx, height()); 799 leading_label_.SetBounds(x, 0, pref.width(), height());
804 800
805 x += pref.cx + kTabButtonBitmap->width(); 801 x += pref.width() + kTabButtonBitmap->width();
806 trailing_label_.GetPreferredSize(&pref); 802 pref = trailing_label_.GetPreferredSize();
807 trailing_label_.SetBounds(x, 0, pref.cx, height()); 803 trailing_label_.SetBounds(x, 0, pref.width(), height());
808 } 804 }
809 } 805 }
810 806
811 void LocationBarView::KeywordHintView::DidChangeBounds(const CRect& previous, 807 void LocationBarView::KeywordHintView::DidChangeBounds(const CRect& previous,
812 const CRect& current) { 808 const CRect& current) {
813 Layout(); 809 Layout();
814 } 810 }
815 811
816 // We don't translate accelerators for ALT + numpad digit, they are used for 812 // We don't translate accelerators for ALT + numpad digit, they are used for
817 // entering special characters. 813 // entering special characters.
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 void LocationBarView::SecurityImageView::InfoBubbleClosing( 1014 void LocationBarView::SecurityImageView::InfoBubbleClosing(
1019 InfoBubble* info_bubble) { 1015 InfoBubble* info_bubble) {
1020 info_bubble_ = NULL; 1016 info_bubble_ = NULL;
1021 } 1017 }
1022 1018
1023 bool LocationBarView::OverrideAccelerator( 1019 bool LocationBarView::OverrideAccelerator(
1024 const ChromeViews::Accelerator& accelerator) { 1020 const ChromeViews::Accelerator& accelerator) {
1025 return location_entry_->OverrideAccelerator(accelerator); 1021 return location_entry_->OverrideAccelerator(accelerator);
1026 } 1022 }
1027 1023
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698