OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/views/toolbar/wrench_menu.h" | 5 #include "chrome/browser/ui/views/toolbar/wrench_menu.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <set> | 9 #include <set> |
10 | 10 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 command_id <= WrenchMenuModel::kMaxRecentTabsCommandId; | 91 command_id <= WrenchMenuModel::kMaxRecentTabsCommandId; |
92 } | 92 } |
93 | 93 |
94 // Subclass of ImageButton whose preferred size includes the size of the border. | 94 // Subclass of ImageButton whose preferred size includes the size of the border. |
95 class FullscreenButton : public ImageButton { | 95 class FullscreenButton : public ImageButton { |
96 public: | 96 public: |
97 explicit FullscreenButton(views::ButtonListener* listener) | 97 explicit FullscreenButton(views::ButtonListener* listener) |
98 : ImageButton(listener) { } | 98 : ImageButton(listener) { } |
99 | 99 |
100 // Overridden from ImageButton. | 100 // Overridden from ImageButton. |
101 virtual gfx::Size GetPreferredSize() OVERRIDE { | 101 virtual gfx::Size GetPreferredSize() const OVERRIDE { |
102 gfx::Size pref = ImageButton::GetPreferredSize(); | 102 gfx::Size pref = ImageButton::GetPreferredSize(); |
103 if (border()) { | 103 if (border()) { |
104 gfx::Insets insets = border()->GetInsets(); | 104 gfx::Insets insets = border()->GetInsets(); |
105 pref.Enlarge(insets.width(), insets.height()); | 105 pref.Enlarge(insets.width(), insets.height()); |
106 } | 106 } |
107 return pref; | 107 return pref; |
108 } | 108 } |
109 | 109 |
110 private: | 110 private: |
111 DISALLOW_COPY_AND_ASSIGN(FullscreenButton); | 111 DISALLOW_COPY_AND_ASSIGN(FullscreenButton); |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 virtual void WrenchMenuDestroyed() OVERRIDE { | 428 virtual void WrenchMenuDestroyed() OVERRIDE { |
429 menu_->RemoveObserver(this); | 429 menu_->RemoveObserver(this); |
430 menu_ = NULL; | 430 menu_ = NULL; |
431 menu_model_ = NULL; | 431 menu_model_ = NULL; |
432 } | 432 } |
433 | 433 |
434 protected: | 434 protected: |
435 WrenchMenu* menu() { return menu_; } | 435 WrenchMenu* menu() { return menu_; } |
436 MenuModel* menu_model() { return menu_model_; } | 436 MenuModel* menu_model() { return menu_model_; } |
437 | 437 |
| 438 bool use_new_menu() const { return menu_->use_new_menu(); } |
| 439 |
438 private: | 440 private: |
439 // Hosting WrenchMenu. | 441 // Hosting WrenchMenu. |
440 // WARNING: this may be NULL during shutdown. | 442 // WARNING: this may be NULL during shutdown. |
441 WrenchMenu* menu_; | 443 WrenchMenu* menu_; |
442 | 444 |
443 // The menu model containing the increment/decrement/reset items. | 445 // The menu model containing the increment/decrement/reset items. |
444 // WARNING: this may be NULL during shutdown. | 446 // WARNING: this may be NULL during shutdown. |
445 MenuModel* menu_model_; | 447 MenuModel* menu_model_; |
446 | 448 |
447 DISALLOW_COPY_AND_ASSIGN(WrenchMenuView); | 449 DISALLOW_COPY_AND_ASSIGN(WrenchMenuView); |
448 }; | 450 }; |
449 | 451 |
450 class ButtonContainerMenuItemView : public MenuItemView { | 452 class ButtonContainerMenuItemView : public MenuItemView { |
451 public: | 453 public: |
452 // Constructor for use with button containing menu items which have a | 454 // Constructor for use with button containing menu items which have a |
453 // different height then normal items. | 455 // different height then normal items. |
454 ButtonContainerMenuItemView(MenuItemView* parent, int command_id, int height) | 456 ButtonContainerMenuItemView(MenuItemView* parent, int command_id, int height) |
455 : MenuItemView(parent, command_id, MenuItemView::NORMAL), | 457 : MenuItemView(parent, command_id, MenuItemView::NORMAL), |
456 height_(height) { | 458 height_(height) { |
457 }; | 459 }; |
458 | 460 |
459 // Overridden from MenuItemView. | 461 // Overridden from MenuItemView. |
460 virtual gfx::Size GetChildPreferredSize() OVERRIDE { | 462 virtual gfx::Size GetChildPreferredSize() const OVERRIDE { |
461 gfx::Size size = MenuItemView::GetChildPreferredSize(); | 463 gfx::Size size = MenuItemView::GetChildPreferredSize(); |
462 // When there is a height override given, we need to deduct our spacing | 464 // When there is a height override given, we need to deduct our spacing |
463 // above and below to get to the correct height to return here for the | 465 // above and below to get to the correct height to return here for the |
464 // child item. | 466 // child item. |
465 int height = height_ - GetTopMargin() - GetBottomMargin(); | 467 int height = height_ - GetTopMargin() - GetBottomMargin(); |
466 if (height > size.height()) | 468 if (height > size.height()) |
467 size.set_height(height); | 469 size.set_height(height); |
468 return size; | 470 return size; |
469 } | 471 } |
470 | 472 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 IDS_PASTE, | 539 IDS_PASTE, |
538 menu->use_new_menu() && menu->supports_new_separators_ ? | 540 menu->use_new_menu() && menu->supports_new_separators_ ? |
539 MenuButtonBackground::CENTER_BUTTON : | 541 MenuButtonBackground::CENTER_BUTTON : |
540 MenuButtonBackground::RIGHT_BUTTON, | 542 MenuButtonBackground::RIGHT_BUTTON, |
541 paste_index, | 543 paste_index, |
542 NULL); | 544 NULL); |
543 copy_background->SetOtherButtons(cut, paste); | 545 copy_background->SetOtherButtons(cut, paste); |
544 } | 546 } |
545 | 547 |
546 // Overridden from View. | 548 // Overridden from View. |
547 virtual gfx::Size GetPreferredSize() OVERRIDE { | 549 virtual gfx::Size GetPreferredSize() const OVERRIDE { |
548 // Returned height doesn't matter as MenuItemView forces everything to the | 550 // Returned height doesn't matter as MenuItemView forces everything to the |
549 // height of the menuitemview. | 551 // height of the menuitemview. |
550 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0); | 552 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0); |
551 } | 553 } |
552 | 554 |
553 virtual void Layout() OVERRIDE { | 555 virtual void Layout() OVERRIDE { |
554 // All buttons are given the same width. | 556 // All buttons are given the same width. |
555 int width = GetMaxChildViewPreferredWidth(); | 557 int width = GetMaxChildViewPreferredWidth(); |
556 for (int i = 0; i < child_count(); ++i) | 558 for (int i = 0; i < child_count(); ++i) |
557 child_at(i)->SetBounds(i * width, 0, width, height()); | 559 child_at(i)->SetBounds(i * width, 0, width, height()); |
558 } | 560 } |
559 | 561 |
560 // Overridden from ButtonListener. | 562 // Overridden from ButtonListener. |
561 virtual void ButtonPressed(views::Button* sender, | 563 virtual void ButtonPressed(views::Button* sender, |
562 const ui::Event& event) OVERRIDE { | 564 const ui::Event& event) OVERRIDE { |
563 menu()->CancelAndEvaluate(menu_model(), sender->tag()); | 565 menu()->CancelAndEvaluate(menu_model(), sender->tag()); |
564 } | 566 } |
565 | 567 |
566 private: | 568 private: |
567 // Returns the max preferred width of all the children. | 569 // Returns the max preferred width of all the children. |
568 int GetMaxChildViewPreferredWidth() { | 570 int GetMaxChildViewPreferredWidth() const { |
569 int width = 0; | 571 int width = 0; |
570 for (int i = 0; i < child_count(); ++i) | 572 for (int i = 0; i < child_count(); ++i) |
571 width = std::max(width, child_at(i)->GetPreferredSize().width()); | 573 width = std::max(width, child_at(i)->GetPreferredSize().width()); |
572 return width; | 574 return width; |
573 } | 575 } |
574 | 576 |
575 DISALLOW_COPY_AND_ASSIGN(CutCopyPasteView); | 577 DISALLOW_COPY_AND_ASSIGN(CutCopyPasteView); |
576 }; | 578 }; |
577 | 579 |
578 // ZoomView -------------------------------------------------------------------- | 580 // ZoomView -------------------------------------------------------------------- |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 GetAccessibleNameForWrenchMenuItem( | 682 GetAccessibleNameForWrenchMenuItem( |
681 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN)); | 683 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN)); |
682 AddChildView(fullscreen_button_); | 684 AddChildView(fullscreen_button_); |
683 | 685 |
684 UpdateZoomControls(); | 686 UpdateZoomControls(); |
685 } | 687 } |
686 | 688 |
687 virtual ~ZoomView() {} | 689 virtual ~ZoomView() {} |
688 | 690 |
689 // Overridden from View. | 691 // Overridden from View. |
690 virtual gfx::Size GetPreferredSize() OVERRIDE { | 692 virtual gfx::Size GetPreferredSize() const OVERRIDE { |
691 // The increment/decrement button are forced to the same width. | 693 // The increment/decrement button are forced to the same width. |
692 int button_width = std::max(increment_button_->GetPreferredSize().width(), | 694 int button_width = std::max(increment_button_->GetPreferredSize().width(), |
693 decrement_button_->GetPreferredSize().width()); | 695 decrement_button_->GetPreferredSize().width()); |
694 int zoom_padding = menu()->use_new_menu() ? | 696 int zoom_padding = use_new_menu() ? |
695 kTouchZoomPadding : kZoomPadding; | 697 kTouchZoomPadding : kZoomPadding; |
696 int fullscreen_width = fullscreen_button_->GetPreferredSize().width() + | 698 int fullscreen_width = fullscreen_button_->GetPreferredSize().width() + |
697 zoom_padding; | 699 zoom_padding; |
698 // Returned height doesn't matter as MenuItemView forces everything to the | 700 // Returned height doesn't matter as MenuItemView forces everything to the |
699 // height of the menuitemview. Note that we have overridden the height when | 701 // height of the menuitemview. Note that we have overridden the height when |
700 // constructing the menu. | 702 // constructing the menu. |
701 return gfx::Size(button_width + zoom_label_width_ + button_width + | 703 return gfx::Size(button_width + zoom_label_width_ + button_width + |
702 fullscreen_width, 0); | 704 fullscreen_width, 0); |
703 } | 705 } |
704 | 706 |
705 virtual void Layout() OVERRIDE { | 707 virtual void Layout() OVERRIDE { |
706 int x = 0; | 708 int x = 0; |
707 int button_width = std::max(increment_button_->GetPreferredSize().width(), | 709 int button_width = std::max(increment_button_->GetPreferredSize().width(), |
708 decrement_button_->GetPreferredSize().width()); | 710 decrement_button_->GetPreferredSize().width()); |
709 gfx::Rect bounds(0, 0, button_width, height()); | 711 gfx::Rect bounds(0, 0, button_width, height()); |
710 | 712 |
711 decrement_button_->SetBoundsRect(bounds); | 713 decrement_button_->SetBoundsRect(bounds); |
712 | 714 |
713 x += bounds.width(); | 715 x += bounds.width(); |
714 bounds.set_x(x); | 716 bounds.set_x(x); |
715 bounds.set_width(zoom_label_width_); | 717 bounds.set_width(zoom_label_width_); |
716 zoom_label_->SetBoundsRect(bounds); | 718 zoom_label_->SetBoundsRect(bounds); |
717 | 719 |
718 x += bounds.width(); | 720 x += bounds.width(); |
719 bounds.set_x(x); | 721 bounds.set_x(x); |
720 bounds.set_width(button_width); | 722 bounds.set_width(button_width); |
721 increment_button_->SetBoundsRect(bounds); | 723 increment_button_->SetBoundsRect(bounds); |
722 | 724 |
723 x += bounds.width() + (menu()->use_new_menu() ? 0 : kZoomPadding); | 725 x += bounds.width() + (use_new_menu() ? 0 : kZoomPadding); |
724 bounds.set_x(x); | 726 bounds.set_x(x); |
725 bounds.set_width(fullscreen_button_->GetPreferredSize().width() + | 727 bounds.set_width(fullscreen_button_->GetPreferredSize().width() + |
726 (menu()->use_new_menu() ? kTouchZoomPadding : 0)); | 728 (use_new_menu() ? kTouchZoomPadding : 0)); |
727 fullscreen_button_->SetBoundsRect(bounds); | 729 fullscreen_button_->SetBoundsRect(bounds); |
728 } | 730 } |
729 | 731 |
730 // Overridden from ButtonListener. | 732 // Overridden from ButtonListener. |
731 virtual void ButtonPressed(views::Button* sender, | 733 virtual void ButtonPressed(views::Button* sender, |
732 const ui::Event& event) OVERRIDE { | 734 const ui::Event& event) OVERRIDE { |
733 if (sender->tag() == fullscreen_index_) { | 735 if (sender->tag() == fullscreen_index_) { |
734 menu()->CancelAndEvaluate(menu_model(), sender->tag()); | 736 menu()->CancelAndEvaluate(menu_model(), sender->tag()); |
735 } else { | 737 } else { |
736 // Zoom buttons don't close the menu. | 738 // Zoom buttons don't close the menu. |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 // These items are represented by child views. If ExecuteCommand is invoked | 1143 // These items are represented by child views. If ExecuteCommand is invoked |
1142 // it means the user clicked on the area around the buttons and we should | 1144 // it means the user clicked on the area around the buttons and we should |
1143 // not do anyting. | 1145 // not do anyting. |
1144 return; | 1146 return; |
1145 } | 1147 } |
1146 | 1148 |
1147 const Entry& entry = command_id_to_entry_.find(command_id)->second; | 1149 const Entry& entry = command_id_to_entry_.find(command_id)->second; |
1148 return entry.first->ActivatedAt(entry.second, mouse_event_flags); | 1150 return entry.first->ActivatedAt(entry.second, mouse_event_flags); |
1149 } | 1151 } |
1150 | 1152 |
1151 bool WrenchMenu::GetAccelerator(int command_id, ui::Accelerator* accelerator) { | 1153 bool WrenchMenu::GetAccelerator(int command_id, |
| 1154 ui::Accelerator* accelerator) const { |
1152 if (IsBookmarkCommand(command_id)) | 1155 if (IsBookmarkCommand(command_id)) |
1153 return false; | 1156 return false; |
1154 | 1157 |
1155 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) { | 1158 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) { |
1156 // These have special child views; don't show the accelerator for them. | 1159 // These have special child views; don't show the accelerator for them. |
1157 return false; | 1160 return false; |
1158 } | 1161 } |
1159 | 1162 |
1160 CommandIDToEntry::iterator ix = command_id_to_entry_.find(command_id); | 1163 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); |
1161 const Entry& entry = ix->second; | 1164 const Entry& entry = ix->second; |
1162 ui::Accelerator menu_accelerator; | 1165 ui::Accelerator menu_accelerator; |
1163 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator)) | 1166 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator)) |
1164 return false; | 1167 return false; |
1165 | 1168 |
1166 *accelerator = ui::Accelerator(menu_accelerator.key_code(), | 1169 *accelerator = ui::Accelerator(menu_accelerator.key_code(), |
1167 menu_accelerator.modifiers()); | 1170 menu_accelerator.modifiers()); |
1168 return true; | 1171 return true; |
1169 } | 1172 } |
1170 | 1173 |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1359 0, | 1362 0, |
1360 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, | 1363 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, |
1361 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); | 1364 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); |
1362 } | 1365 } |
1363 | 1366 |
1364 int WrenchMenu::ModelIndexFromCommandId(int command_id) const { | 1367 int WrenchMenu::ModelIndexFromCommandId(int command_id) const { |
1365 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); | 1368 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); |
1366 DCHECK(ix != command_id_to_entry_.end()); | 1369 DCHECK(ix != command_id_to_entry_.end()); |
1367 return ix->second.second; | 1370 return ix->second.second; |
1368 } | 1371 } |
OLD | NEW |