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

Side by Side Diff: chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.cc

Issue 26350003: OLD: reland "views: change WrenchMenu to use each model's command ID's when creating MenuItemView's" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bookmarks/bookmark_menu_delegate.h" 5 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h"
6 6
7 #include "base/prefs/pref_service.h" 7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h" 9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
(...skipping 23 matching lines...) Expand all
34 using content::UserMetricsAction; 34 using content::UserMetricsAction;
35 using views::MenuItemView; 35 using views::MenuItemView;
36 36
37 // Max width of a menu. There does not appear to be an OS value for this, yet 37 // Max width of a menu. There does not appear to be an OS value for this, yet
38 // both IE and FF restrict the max width of a menu. 38 // both IE and FF restrict the max width of a menu.
39 static const int kMaxMenuWidth = 400; 39 static const int kMaxMenuWidth = 400;
40 40
41 BookmarkMenuDelegate::BookmarkMenuDelegate(Browser* browser, 41 BookmarkMenuDelegate::BookmarkMenuDelegate(Browser* browser,
42 PageNavigator* navigator, 42 PageNavigator* navigator,
43 views::Widget* parent, 43 views::Widget* parent,
44 int first_menu_id) 44 int first_menu_id,
45 int max_menu_id)
45 : browser_(browser), 46 : browser_(browser),
46 profile_(browser->profile()), 47 profile_(browser->profile()),
47 page_navigator_(navigator), 48 page_navigator_(navigator),
48 parent_(parent), 49 parent_(parent),
49 menu_(NULL), 50 menu_(NULL),
50 for_drop_(false), 51 for_drop_(false),
51 parent_menu_item_(NULL), 52 parent_menu_item_(NULL),
52 next_menu_id_(first_menu_id), 53 next_menu_id_(first_menu_id),
54 max_menu_id_(max_menu_id),
53 real_delegate_(NULL), 55 real_delegate_(NULL),
54 is_mutating_model_(false), 56 is_mutating_model_(false),
55 location_(BOOKMARK_LAUNCH_LOCATION_NONE) {} 57 location_(BOOKMARK_LAUNCH_LOCATION_NONE) {}
56 58
57 BookmarkMenuDelegate::~BookmarkMenuDelegate() { 59 BookmarkMenuDelegate::~BookmarkMenuDelegate() {
58 GetBookmarkModel()->RemoveObserver(this); 60 GetBookmarkModel()->RemoveObserver(this);
59 } 61 }
60 62
61 void BookmarkMenuDelegate::Init(views::MenuDelegate* real_delegate, 63 void BookmarkMenuDelegate::Init(views::MenuDelegate* real_delegate,
62 MenuItemView* parent, 64 MenuItemView* parent,
63 const BookmarkNode* node, 65 const BookmarkNode* node,
64 int start_child_index, 66 int start_child_index,
65 ShowOptions show_options, 67 ShowOptions show_options,
66 BookmarkLaunchLocation location) { 68 BookmarkLaunchLocation location) {
67 GetBookmarkModel()->AddObserver(this); 69 GetBookmarkModel()->AddObserver(this);
68 real_delegate_ = real_delegate; 70 real_delegate_ = real_delegate;
71 location_ = location;
69 if (parent) { 72 if (parent) {
70 parent_menu_item_ = parent; 73 parent_menu_item_ = parent;
71 int initial_count = parent->GetSubmenu() ? 74 int initial_count = parent->GetSubmenu() ?
72 parent->GetSubmenu()->GetMenuItemCount() : 0; 75 parent->GetSubmenu()->GetMenuItemCount() : 0;
73 if ((start_child_index < node->child_count()) && 76 if ((start_child_index < node->child_count()) &&
74 (initial_count > 0)) { 77 (initial_count > 0)) {
75 parent->AppendSeparator(); 78 parent->AppendSeparator();
76 } 79 }
77 BuildMenu(node, start_child_index, parent, &next_menu_id_); 80 BuildMenu(node, start_child_index, parent, &next_menu_id_);
78 if (show_options == SHOW_PERMANENT_FOLDERS) 81 if (show_options == SHOW_PERMANENT_FOLDERS)
79 BuildMenusForPermanentNodes(parent, &next_menu_id_); 82 BuildMenusForPermanentNodes(parent, &next_menu_id_);
80 } else { 83 } else {
81 menu_ = CreateMenu(node, start_child_index, show_options); 84 menu_ = CreateMenu(node, start_child_index, show_options);
82 } 85 }
83
84 location_ = location;
85 } 86 }
86 87
87 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { 88 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) {
88 page_navigator_ = navigator; 89 page_navigator_ = navigator;
89 if (context_menu_.get()) 90 if (context_menu_.get())
90 context_menu_->SetPageNavigator(navigator); 91 context_menu_->SetPageNavigator(navigator);
91 } 92 }
92 93
93 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() { 94 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() {
94 return BookmarkModelFactory::GetForProfile(profile_); 95 return BookmarkModelFactory::GetForProfile(profile_);
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 } 426 }
426 427
427 void BookmarkMenuDelegate::BuildMenuForPermanentNode( 428 void BookmarkMenuDelegate::BuildMenuForPermanentNode(
428 const BookmarkNode* node, 429 const BookmarkNode* node,
429 MenuItemView* menu, 430 MenuItemView* menu,
430 int* next_menu_id, 431 int* next_menu_id,
431 bool* added_separator) { 432 bool* added_separator) {
432 if (!node->IsVisible() || node->GetTotalNodeCount() == 1) 433 if (!node->IsVisible() || node->GetTotalNodeCount() == 1)
433 return; // No children, don't create a menu. 434 return; // No children, don't create a menu.
434 435
436 int id = *next_menu_id;
437 // Don't create the submenu if it will exceed the maximum menu Id allowed.
438 if (ExceedsMaximumMenuId(id))
439 return;
440 (*next_menu_id)++;
441
435 if (!*added_separator) { 442 if (!*added_separator) {
436 *added_separator = true; 443 *added_separator = true;
437 menu->AppendSeparator(); 444 menu->AppendSeparator();
438 } 445 }
439 int id = *next_menu_id; 446
440 (*next_menu_id)++;
441 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); 447 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
442 gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); 448 gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER);
443 MenuItemView* submenu = menu->AppendSubMenuWithIcon( 449 MenuItemView* submenu = menu->AppendSubMenuWithIcon(
444 id, node->GetTitle(), *folder_icon); 450 id, node->GetTitle(), *folder_icon);
445 BuildMenu(node, 0, submenu, next_menu_id); 451 BuildMenu(node, 0, submenu, next_menu_id);
446 menu_id_to_node_map_[id] = node; 452 menu_id_to_node_map_[id] = node;
447 } 453 }
448 454
449 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, 455 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent,
450 int start_child_index, 456 int start_child_index,
451 MenuItemView* menu, 457 MenuItemView* menu,
452 int* next_menu_id) { 458 int* next_menu_id) {
453 node_to_menu_map_[parent] = menu; 459 node_to_menu_map_[parent] = menu;
454 DCHECK(parent->empty() || start_child_index < parent->child_count()); 460 DCHECK(parent->empty() || start_child_index < parent->child_count());
455 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); 461 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
456 for (int i = start_child_index; i < parent->child_count(); ++i) { 462 for (int i = start_child_index; i < parent->child_count(); ++i) {
457 const BookmarkNode* node = parent->GetChild(i); 463 const BookmarkNode* node = parent->GetChild(i);
458 const int id = *next_menu_id; 464 const int id = *next_menu_id;
465 // Don't create the menu item if it will exceed the maximum menu Id allowed.
466 if (ExceedsMaximumMenuId(id))
467 break;
468
459 (*next_menu_id)++; 469 (*next_menu_id)++;
460 470
461 menu_id_to_node_map_[id] = node; 471 menu_id_to_node_map_[id] = node;
462 if (node->is_url()) { 472 if (node->is_url()) {
463 const gfx::Image& image = GetBookmarkModel()->GetFavicon(node); 473 const gfx::Image& image = GetBookmarkModel()->GetFavicon(node);
464 const gfx::ImageSkia* icon = image.IsEmpty() ? 474 const gfx::ImageSkia* icon = image.IsEmpty() ?
465 rb->GetImageSkiaNamed(IDR_DEFAULT_FAVICON) : image.ToImageSkia(); 475 rb->GetImageSkiaNamed(IDR_DEFAULT_FAVICON) : image.ToImageSkia();
466 node_to_menu_map_[node] = 476 node_to_menu_map_[node] =
467 menu->AppendMenuItemWithIcon(id, node->GetTitle(), *icon); 477 menu->AppendMenuItemWithIcon(id, node->GetTitle(), *icon);
468 } else if (node->is_folder()) { 478 } else if (node->is_folder()) {
469 gfx::ImageSkia* folder_icon = 479 gfx::ImageSkia* folder_icon =
470 rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); 480 rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER);
471 MenuItemView* submenu = menu->AppendSubMenuWithIcon( 481 MenuItemView* submenu = menu->AppendSubMenuWithIcon(
472 id, node->GetTitle(), *folder_icon); 482 id, node->GetTitle(), *folder_icon);
473 BuildMenu(node, 0, submenu, next_menu_id); 483 BuildMenu(node, 0, submenu, next_menu_id);
474 } else { 484 } else {
475 NOTREACHED(); 485 NOTREACHED();
476 } 486 }
477 } 487 }
478 } 488 }
489
490 bool BookmarkMenuDelegate::ExceedsMaximumMenuId(int menu_id) const {
491 return menu_id > max_menu_id_;
492 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698