| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/wrench_menu.h" | 5 #include "chrome/browser/views/wrench_menu.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/app/chrome_dll_resource.h" | 12 #include "chrome/app/chrome_dll_resource.h" |
| 13 #include "chrome/browser/browser.h" | 13 #include "chrome/browser/browser.h" |
| 14 #include "chrome/browser/host_zoom_map.h" | 14 #include "chrome/browser/host_zoom_map.h" |
| 15 #include "chrome/browser/profile.h" | 15 #include "chrome/browser/profile.h" |
| 16 #include "chrome/browser/tab_contents/tab_contents.h" | 16 #include "chrome/browser/tab_contents/tab_contents.h" |
| 17 #include "chrome/common/notification_observer.h" | 17 #include "chrome/common/notification_observer.h" |
| 18 #include "chrome/common/notification_registrar.h" | 18 #include "chrome/common/notification_registrar.h" |
| 19 #include "chrome/common/notification_source.h" | 19 #include "chrome/common/notification_source.h" |
| 20 #include "chrome/common/notification_type.h" | 20 #include "chrome/common/notification_type.h" |
| 21 #include "gfx/canvas.h" | 21 #include "gfx/canvas.h" |
| 22 #include "gfx/canvas_skia.h" |
| 22 #include "gfx/skia_util.h" | 23 #include "gfx/skia_util.h" |
| 23 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 24 #include "grit/theme_resources.h" | 25 #include "grit/theme_resources.h" |
| 25 #include "third_party/skia/include/core/SkPaint.h" | 26 #include "third_party/skia/include/core/SkPaint.h" |
| 26 #include "views/background.h" | 27 #include "views/background.h" |
| 27 #include "views/controls/button/image_button.h" | 28 #include "views/controls/button/image_button.h" |
| 28 #include "views/controls/button/menu_button.h" | 29 #include "views/controls/button/menu_button.h" |
| 29 #include "views/controls/button/text_button.h" | 30 #include "views/controls/button/text_button.h" |
| 30 #include "views/controls/label.h" | 31 #include "views/controls/label.h" |
| 31 #include "views/controls/menu/menu_config.h" | 32 #include "views/controls/menu/menu_config.h" |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 | 361 |
| 361 zoom_label_ = new Label(l10n_util::GetStringF(IDS_ZOOM_PERCENT, L"100")); | 362 zoom_label_ = new Label(l10n_util::GetStringF(IDS_ZOOM_PERCENT, L"100")); |
| 362 zoom_label_->SetColor(MenuConfig::instance().text_color); | 363 zoom_label_->SetColor(MenuConfig::instance().text_color); |
| 363 zoom_label_->SetHorizontalAlignment(Label::ALIGN_RIGHT); | 364 zoom_label_->SetHorizontalAlignment(Label::ALIGN_RIGHT); |
| 364 MenuButtonBackground* center_bg = | 365 MenuButtonBackground* center_bg = |
| 365 new MenuButtonBackground(MenuButtonBackground::CENTER_BUTTON); | 366 new MenuButtonBackground(MenuButtonBackground::CENTER_BUTTON); |
| 366 zoom_label_->set_background(center_bg); | 367 zoom_label_->set_background(center_bg); |
| 367 zoom_label_->set_border(new MenuButtonBorder()); | 368 zoom_label_->set_border(new MenuButtonBorder()); |
| 368 zoom_label_->SetFont(MenuConfig::instance().font); | 369 zoom_label_->SetFont(MenuConfig::instance().font); |
| 369 AddChildView(zoom_label_); | 370 AddChildView(zoom_label_); |
| 370 // Get the preferred width given 100%, we'll use this in calculating our | 371 zoom_label_width_ = MaxWidthForZoomLabel(); |
| 371 // preferred size. | |
| 372 zoom_label_width_ = zoom_label_->GetPreferredSize().width(); | |
| 373 | 372 |
| 374 decrement_button_ = CreateAndConfigureButton( | 373 decrement_button_ = CreateAndConfigureButton( |
| 375 this, this, IDS_ZOOM_MINUS2, MenuButtonBackground::RIGHT_BUTTON, | 374 this, this, IDS_ZOOM_MINUS2, MenuButtonBackground::RIGHT_BUTTON, |
| 376 menu_model, decrement_index, NULL); | 375 menu_model, decrement_index, NULL); |
| 377 | 376 |
| 378 center_bg->SetOtherButtons(increment_button_, decrement_button_); | 377 center_bg->SetOtherButtons(increment_button_, decrement_button_); |
| 379 | 378 |
| 380 fullscreen_button_ = new FullscreenButton(this); | 379 fullscreen_button_ = new FullscreenButton(this); |
| 381 fullscreen_button_->SetImage( | 380 fullscreen_button_->SetImage( |
| 382 ImageButton::BS_NORMAL, | 381 ImageButton::BS_NORMAL, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 const NotificationSource& source, | 448 const NotificationSource& source, |
| 450 const NotificationDetails& details) { | 449 const NotificationDetails& details) { |
| 451 DCHECK_EQ(NotificationType::ZOOM_LEVEL_CHANGED, type.value); | 450 DCHECK_EQ(NotificationType::ZOOM_LEVEL_CHANGED, type.value); |
| 452 UpdateZoomControls(); | 451 UpdateZoomControls(); |
| 453 } | 452 } |
| 454 | 453 |
| 455 private: | 454 private: |
| 456 void UpdateZoomControls() { | 455 void UpdateZoomControls() { |
| 457 bool enable_increment, enable_decrement; | 456 bool enable_increment, enable_decrement; |
| 458 int zoom_percent = | 457 int zoom_percent = |
| 459 static_cast<int>(GetZoom(&enable_increment, &enable_decrement) * 100); | 458 static_cast<int>(GetZoom(&enable_increment, &enable_decrement)); |
| 460 zoom_label_->SetText(l10n_util::GetStringF( | 459 zoom_label_->SetText(l10n_util::GetStringF( |
| 461 IDS_ZOOM_PERCENT, IntToWString(zoom_percent))); | 460 IDS_ZOOM_PERCENT, IntToWString(zoom_percent))); |
| 462 increment_button_->SetEnabled(enable_increment); | 461 increment_button_->SetEnabled(enable_increment); |
| 463 decrement_button_->SetEnabled(enable_decrement); | 462 decrement_button_->SetEnabled(enable_decrement); |
| 464 } | 463 } |
| 465 | 464 |
| 466 double GetZoom(bool* enable_increment, bool* enable_decrement) { | 465 double GetZoom(bool* enable_increment, bool* enable_decrement) { |
| 467 // TODO: move this somewhere it can be shared. | 466 // TODO: move this somewhere it can be shared. |
| 468 TabContents* selected_tab = menu_->browser_->GetSelectedTabContents(); | 467 TabContents* selected_tab = menu_->browser_->GetSelectedTabContents(); |
| 469 *enable_decrement = *enable_increment = false; | 468 *enable_decrement = *enable_increment = false; |
| 470 if (!selected_tab) | 469 if (!selected_tab) |
| 471 return 1; | 470 return 1; |
| 472 | 471 |
| 473 HostZoomMap* zoom_map = selected_tab->profile()->GetHostZoomMap(); | 472 HostZoomMap* zoom_map = selected_tab->profile()->GetHostZoomMap(); |
| 474 if (!zoom_map) | 473 if (!zoom_map) |
| 475 return 1; | 474 return 1; |
| 476 | 475 |
| 477 int zoom_level = zoom_map->GetZoomLevel(selected_tab->GetURL()); | 476 int zoom_level = zoom_map->GetZoomLevel(selected_tab->GetURL()); |
| 478 double value = static_cast<double>( | 477 double value = ZoomPercentFromZoomLevel(zoom_level); |
| 479 std::max(std::min(std::pow(1.2, zoom_level), 3.0), .5)); | 478 *enable_decrement = (value != 50); |
| 480 *enable_decrement = (value != .5); | 479 *enable_increment = (value != 300); |
| 481 *enable_increment = (value != 3.0); | |
| 482 return value; | 480 return value; |
| 483 } | 481 } |
| 484 | 482 |
| 483 double ZoomPercentFromZoomLevel(int level) { |
| 484 return static_cast<double>( |
| 485 std::max(std::min(std::pow(1.2, level), 3.0), .5)) * 100; |
| 486 } |
| 487 |
| 488 // Calculates the max width the zoom string can be. |
| 489 int MaxWidthForZoomLabel() { |
| 490 gfx::Font font = zoom_label_->font(); |
| 491 gfx::Insets insets; |
| 492 if (zoom_label_->border()) |
| 493 zoom_label_->border()->GetInsets(&insets); |
| 494 int max_w = 0; |
| 495 for (int i = -4; i <= 7; ++i) { |
| 496 int zoom_percent = static_cast<int>(ZoomPercentFromZoomLevel(i)); |
| 497 int w = font.GetStringWidth( |
| 498 l10n_util::GetStringF(IDS_ZOOM_PERCENT, zoom_percent)); |
| 499 max_w = std::max(w, max_w); |
| 500 } |
| 501 return max_w + insets.width(); |
| 502 } |
| 503 |
| 485 // Hosting WrenchMenu. | 504 // Hosting WrenchMenu. |
| 486 WrenchMenu* menu_; | 505 WrenchMenu* menu_; |
| 487 | 506 |
| 488 // The menu model containing the increment/decrement/reset items. | 507 // The menu model containing the increment/decrement/reset items. |
| 489 MenuModel* menu_model_; | 508 MenuModel* menu_model_; |
| 490 | 509 |
| 491 // Index of the fullscreen menu item in the model. | 510 // Index of the fullscreen menu item in the model. |
| 492 const int fullscreen_index_; | 511 const int fullscreen_index_; |
| 493 | 512 |
| 494 NotificationRegistrar registrar_; | 513 NotificationRegistrar registrar_; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 } | 692 } |
| 674 | 693 |
| 675 return menu_item; | 694 return menu_item; |
| 676 } | 695 } |
| 677 | 696 |
| 678 void WrenchMenu::CancelAndEvaluate(MenuModel* model, int index) { | 697 void WrenchMenu::CancelAndEvaluate(MenuModel* model, int index) { |
| 679 selected_menu_model_ = model; | 698 selected_menu_model_ = model; |
| 680 selected_index_ = index; | 699 selected_index_ = index; |
| 681 root_->Cancel(); | 700 root_->Cancel(); |
| 682 } | 701 } |
| OLD | NEW |