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

Side by Side Diff: chrome/browser/ui/views/wrench_menu.cc

Issue 7720012: Moves ownership of MenuItemView to MenuRunner as well as responbility (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unit test Created 9 years, 4 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/wrench_menu.h" 5 #include "chrome/browser/ui/views/wrench_menu.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 18 matching lines...) Expand all
29 #include "ui/gfx/canvas.h" 29 #include "ui/gfx/canvas.h"
30 #include "ui/gfx/canvas_skia.h" 30 #include "ui/gfx/canvas_skia.h"
31 #include "ui/gfx/skia_util.h" 31 #include "ui/gfx/skia_util.h"
32 #include "views/background.h" 32 #include "views/background.h"
33 #include "views/controls/button/image_button.h" 33 #include "views/controls/button/image_button.h"
34 #include "views/controls/button/menu_button.h" 34 #include "views/controls/button/menu_button.h"
35 #include "views/controls/button/text_button.h" 35 #include "views/controls/button/text_button.h"
36 #include "views/controls/label.h" 36 #include "views/controls/label.h"
37 #include "views/controls/menu/menu_config.h" 37 #include "views/controls/menu/menu_config.h"
38 #include "views/controls/menu/menu_item_view.h" 38 #include "views/controls/menu/menu_item_view.h"
39 #include "views/controls/menu/menu_runner.h"
39 #include "views/controls/menu/menu_scroll_view_container.h" 40 #include "views/controls/menu/menu_scroll_view_container.h"
40 #include "views/controls/menu/submenu_view.h" 41 #include "views/controls/menu/submenu_view.h"
41 #include "views/widget/widget.h" 42 #include "views/widget/widget.h"
42 43
43 using ui::MenuModel; 44 using ui::MenuModel;
44 using views::CustomButton; 45 using views::CustomButton;
45 using views::ImageButton; 46 using views::ImageButton;
46 using views::Label; 47 using views::Label;
47 using views::MenuConfig; 48 using views::MenuConfig;
48 using views::MenuItemView; 49 using views::MenuItemView;
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 559
559 // Width given to |zoom_label_|. This is the width at 100%. 560 // Width given to |zoom_label_|. This is the width at 100%.
560 int zoom_label_width_; 561 int zoom_label_width_;
561 562
562 DISALLOW_COPY_AND_ASSIGN(ZoomView); 563 DISALLOW_COPY_AND_ASSIGN(ZoomView);
563 }; 564 };
564 565
565 // WrenchMenu ------------------------------------------------------------------ 566 // WrenchMenu ------------------------------------------------------------------
566 567
567 WrenchMenu::WrenchMenu(Browser* browser) 568 WrenchMenu::WrenchMenu(Browser* browser)
568 : browser_(browser), 569 : root_(NULL),
570 browser_(browser),
569 selected_menu_model_(NULL), 571 selected_menu_model_(NULL),
570 selected_index_(0), 572 selected_index_(0),
571 bookmark_menu_(NULL), 573 bookmark_menu_(NULL),
572 first_bookmark_command_id_(0) { 574 first_bookmark_command_id_(0) {
573 } 575 }
574 576
577 WrenchMenu::~WrenchMenu() {
578 if (bookmark_menu_delegate_.get()) {
579 BookmarkModel* model = browser_->profile()->GetBookmarkModel();
580 if (model)
581 model->RemoveObserver(this);
582 }
583 }
584
575 void WrenchMenu::Init(ui::MenuModel* model) { 585 void WrenchMenu::Init(ui::MenuModel* model) {
576 DCHECK(!root_.get()); 586 DCHECK(!root_);
577 root_.reset(new MenuItemView(this)); 587 root_ = new MenuItemView(this);
578 root_->set_has_icons(true); // We have checks, radios and icons, set this 588 root_->set_has_icons(true); // We have checks, radios and icons, set this
579 // so we get the taller menu style. 589 // so we get the taller menu style.
580 int next_id = 1; 590 int next_id = 1;
581 PopulateMenu(root_.get(), model, &next_id); 591 PopulateMenu(root_, model, &next_id);
582 first_bookmark_command_id_ = next_id + 1; 592 first_bookmark_command_id_ = next_id + 1;
593 menu_runner_.reset(new views::MenuRunner(root_));
583 } 594 }
584 595
585 void WrenchMenu::RunMenu(views::MenuButton* host) { 596 void WrenchMenu::RunMenu(views::MenuButton* host) {
586 // Up the ref count while the menu is displaying. This way if the window is
587 // deleted while we're running we won't prematurely delete the menu.
588 // TODO(sky): fix this, the menu should really take ownership of the menu
589 // (57890).
590 scoped_refptr<WrenchMenu> dont_delete_while_running(this);
591 gfx::Point screen_loc; 597 gfx::Point screen_loc;
592 views::View::ConvertPointToScreen(host, &screen_loc); 598 views::View::ConvertPointToScreen(host, &screen_loc);
593 gfx::Rect bounds(screen_loc, host->size()); 599 gfx::Rect bounds(screen_loc, host->size());
594 UserMetrics::RecordAction(UserMetricsAction("ShowAppMenu")); 600 UserMetrics::RecordAction(UserMetricsAction("ShowAppMenu"));
595 root_->RunMenuAt(host->GetWidget(), host, bounds, 601 if (menu_runner_->RunMenuAt(host->GetWidget(), host, bounds,
596 MenuItemView::TOPRIGHT, true); 602 MenuItemView::TOPRIGHT, views::MenuRunner::HAS_MNEMONICS) ==
603 views::MenuRunner::MENU_DELETED)
604 return;
597 if (bookmark_menu_delegate_.get()) { 605 if (bookmark_menu_delegate_.get()) {
598 BookmarkModel* model = browser_->profile()->GetBookmarkModel(); 606 BookmarkModel* model = browser_->profile()->GetBookmarkModel();
599 if (model) 607 if (model)
600 model->RemoveObserver(this); 608 model->RemoveObserver(this);
601 } 609 }
602 if (selected_menu_model_) 610 if (selected_menu_model_)
603 selected_menu_model_->ActivatedAt(selected_index_); 611 selected_menu_model_->ActivatedAt(selected_index_);
604 } 612 }
605 613
606 std::wstring WrenchMenu::GetTooltipText(int id, 614 std::wstring WrenchMenu::GetTooltipText(int id,
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 if (menu == bookmark_menu_) 766 if (menu == bookmark_menu_)
759 CreateBookmarkMenu(); 767 CreateBookmarkMenu();
760 } 768 }
761 769
762 void WrenchMenu::BookmarkModelChanged() { 770 void WrenchMenu::BookmarkModelChanged() {
763 DCHECK(bookmark_menu_delegate_.get()); 771 DCHECK(bookmark_menu_delegate_.get());
764 if (!bookmark_menu_delegate_->is_mutating_model()) 772 if (!bookmark_menu_delegate_->is_mutating_model())
765 root_->Cancel(); 773 root_->Cancel();
766 } 774 }
767 775
768 WrenchMenu::~WrenchMenu() {
769 }
770
771 void WrenchMenu::PopulateMenu(MenuItemView* parent, 776 void WrenchMenu::PopulateMenu(MenuItemView* parent,
772 MenuModel* model, 777 MenuModel* model,
773 int* next_id) { 778 int* next_id) {
774 int index_offset = model->GetFirstItemIndex(NULL); 779 int index_offset = model->GetFirstItemIndex(NULL);
775 for (int i = 0, max = model->GetItemCount(); i < max; ++i) { 780 for (int i = 0, max = model->GetItemCount(); i < max; ++i) {
776 int index = i + index_offset; 781 int index = i + index_offset;
777 782
778 MenuItemView* item = 783 MenuItemView* item =
779 AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id); 784 AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id);
780 785
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 browser_->window()->GetNativeHandle()); 864 browser_->window()->GetNativeHandle());
860 bookmark_menu_delegate_.reset( 865 bookmark_menu_delegate_.reset(
861 new BookmarkMenuDelegate(browser_->profile(), 866 new BookmarkMenuDelegate(browser_->profile(),
862 NULL, 867 NULL,
863 parent, 868 parent,
864 first_bookmark_command_id_)); 869 first_bookmark_command_id_));
865 bookmark_menu_delegate_->Init( 870 bookmark_menu_delegate_->Init(
866 this, bookmark_menu_, model->bookmark_bar_node(), 0, 871 this, bookmark_menu_, model->bookmark_bar_node(), 0,
867 BookmarkMenuDelegate::SHOW_OTHER_FOLDER); 872 BookmarkMenuDelegate::SHOW_OTHER_FOLDER);
868 } 873 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698