OLD | NEW |
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 Loading... |
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) | |
46 : browser_(browser), | 45 : browser_(browser), |
47 profile_(browser->profile()), | 46 profile_(browser->profile()), |
48 page_navigator_(navigator), | 47 page_navigator_(navigator), |
49 parent_(parent), | 48 parent_(parent), |
50 menu_(NULL), | 49 menu_(NULL), |
51 for_drop_(false), | 50 for_drop_(false), |
52 parent_menu_item_(NULL), | 51 parent_menu_item_(NULL), |
53 next_menu_id_(first_menu_id), | 52 next_menu_id_(first_menu_id), |
54 min_menu_id_(first_menu_id), | |
55 max_menu_id_(max_menu_id), | |
56 real_delegate_(NULL), | 53 real_delegate_(NULL), |
57 is_mutating_model_(false), | 54 is_mutating_model_(false), |
58 location_(BOOKMARK_LAUNCH_LOCATION_NONE) {} | 55 location_(BOOKMARK_LAUNCH_LOCATION_NONE) {} |
59 | 56 |
60 BookmarkMenuDelegate::~BookmarkMenuDelegate() { | 57 BookmarkMenuDelegate::~BookmarkMenuDelegate() { |
61 GetBookmarkModel()->RemoveObserver(this); | 58 GetBookmarkModel()->RemoveObserver(this); |
62 } | 59 } |
63 | 60 |
64 void BookmarkMenuDelegate::Init(views::MenuDelegate* real_delegate, | 61 void BookmarkMenuDelegate::Init(views::MenuDelegate* real_delegate, |
65 MenuItemView* parent, | 62 MenuItemView* parent, |
66 const BookmarkNode* node, | 63 const BookmarkNode* node, |
67 int start_child_index, | 64 int start_child_index, |
68 ShowOptions show_options, | 65 ShowOptions show_options, |
69 BookmarkLaunchLocation location) { | 66 BookmarkLaunchLocation location) { |
70 GetBookmarkModel()->AddObserver(this); | 67 GetBookmarkModel()->AddObserver(this); |
71 real_delegate_ = real_delegate; | 68 real_delegate_ = real_delegate; |
72 location_ = location; | |
73 if (parent) { | 69 if (parent) { |
74 parent_menu_item_ = parent; | 70 parent_menu_item_ = parent; |
75 int initial_count = parent->GetSubmenu() ? | 71 int initial_count = parent->GetSubmenu() ? |
76 parent->GetSubmenu()->GetMenuItemCount() : 0; | 72 parent->GetSubmenu()->GetMenuItemCount() : 0; |
77 if ((start_child_index < node->child_count()) && | 73 if ((start_child_index < node->child_count()) && |
78 (initial_count > 0)) { | 74 (initial_count > 0)) { |
79 parent->AppendSeparator(); | 75 parent->AppendSeparator(); |
80 } | 76 } |
81 BuildMenu(node, start_child_index, parent, &next_menu_id_); | 77 BuildMenu(node, start_child_index, parent, &next_menu_id_); |
82 if (show_options == SHOW_PERMANENT_FOLDERS) | 78 if (show_options == SHOW_PERMANENT_FOLDERS) |
83 BuildMenusForPermanentNodes(parent, &next_menu_id_); | 79 BuildMenusForPermanentNodes(parent, &next_menu_id_); |
84 } else { | 80 } else { |
85 menu_ = CreateMenu(node, start_child_index, show_options); | 81 menu_ = CreateMenu(node, start_child_index, show_options); |
86 } | 82 } |
| 83 |
| 84 location_ = location; |
87 } | 85 } |
88 | 86 |
89 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { | 87 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { |
90 page_navigator_ = navigator; | 88 page_navigator_ = navigator; |
91 if (context_menu_.get()) | 89 if (context_menu_.get()) |
92 context_menu_->SetPageNavigator(navigator); | 90 context_menu_->SetPageNavigator(navigator); |
93 } | 91 } |
94 | 92 |
95 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() { | 93 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() { |
96 return BookmarkModelFactory::GetForProfile(profile_); | 94 return BookmarkModelFactory::GetForProfile(profile_); |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 } | 425 } |
428 | 426 |
429 void BookmarkMenuDelegate::BuildMenuForPermanentNode( | 427 void BookmarkMenuDelegate::BuildMenuForPermanentNode( |
430 const BookmarkNode* node, | 428 const BookmarkNode* node, |
431 MenuItemView* menu, | 429 MenuItemView* menu, |
432 int* next_menu_id, | 430 int* next_menu_id, |
433 bool* added_separator) { | 431 bool* added_separator) { |
434 if (!node->IsVisible() || node->GetTotalNodeCount() == 1) | 432 if (!node->IsVisible() || node->GetTotalNodeCount() == 1) |
435 return; // No children, don't create a menu. | 433 return; // No children, don't create a menu. |
436 | 434 |
437 int id = *next_menu_id; | |
438 // Don't create the submenu if its menu ID will be outside the range allowed. | |
439 if (IsOutsideMenuIdRange(id)) | |
440 return; | |
441 (*next_menu_id)++; | |
442 | |
443 if (!*added_separator) { | 435 if (!*added_separator) { |
444 *added_separator = true; | 436 *added_separator = true; |
445 menu->AppendSeparator(); | 437 menu->AppendSeparator(); |
446 } | 438 } |
447 | 439 int id = *next_menu_id; |
| 440 (*next_menu_id)++; |
448 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 441 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
449 gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); | 442 gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); |
450 MenuItemView* submenu = menu->AppendSubMenuWithIcon( | 443 MenuItemView* submenu = menu->AppendSubMenuWithIcon( |
451 id, node->GetTitle(), *folder_icon); | 444 id, node->GetTitle(), *folder_icon); |
452 BuildMenu(node, 0, submenu, next_menu_id); | 445 BuildMenu(node, 0, submenu, next_menu_id); |
453 menu_id_to_node_map_[id] = node; | 446 menu_id_to_node_map_[id] = node; |
454 } | 447 } |
455 | 448 |
456 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, | 449 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, |
457 int start_child_index, | 450 int start_child_index, |
458 MenuItemView* menu, | 451 MenuItemView* menu, |
459 int* next_menu_id) { | 452 int* next_menu_id) { |
460 node_to_menu_map_[parent] = menu; | 453 node_to_menu_map_[parent] = menu; |
461 DCHECK(parent->empty() || start_child_index < parent->child_count()); | 454 DCHECK(parent->empty() || start_child_index < parent->child_count()); |
462 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 455 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
463 for (int i = start_child_index; i < parent->child_count(); ++i) { | 456 for (int i = start_child_index; i < parent->child_count(); ++i) { |
464 const BookmarkNode* node = parent->GetChild(i); | 457 const BookmarkNode* node = parent->GetChild(i); |
465 const int id = *next_menu_id; | 458 const int id = *next_menu_id; |
466 // Don't create the item if its menu ID will be outside the range allowed. | |
467 if (IsOutsideMenuIdRange(id)) | |
468 break; | |
469 | |
470 (*next_menu_id)++; | 459 (*next_menu_id)++; |
471 | 460 |
472 menu_id_to_node_map_[id] = node; | 461 menu_id_to_node_map_[id] = node; |
473 if (node->is_url()) { | 462 if (node->is_url()) { |
474 const gfx::Image& image = GetBookmarkModel()->GetFavicon(node); | 463 const gfx::Image& image = GetBookmarkModel()->GetFavicon(node); |
475 const gfx::ImageSkia* icon = image.IsEmpty() ? | 464 const gfx::ImageSkia* icon = image.IsEmpty() ? |
476 rb->GetImageSkiaNamed(IDR_DEFAULT_FAVICON) : image.ToImageSkia(); | 465 rb->GetImageSkiaNamed(IDR_DEFAULT_FAVICON) : image.ToImageSkia(); |
477 node_to_menu_map_[node] = | 466 node_to_menu_map_[node] = |
478 menu->AppendMenuItemWithIcon(id, node->GetTitle(), *icon); | 467 menu->AppendMenuItemWithIcon(id, node->GetTitle(), *icon); |
479 } else if (node->is_folder()) { | 468 } else if (node->is_folder()) { |
480 gfx::ImageSkia* folder_icon = | 469 gfx::ImageSkia* folder_icon = |
481 rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); | 470 rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); |
482 MenuItemView* submenu = menu->AppendSubMenuWithIcon( | 471 MenuItemView* submenu = menu->AppendSubMenuWithIcon( |
483 id, node->GetTitle(), *folder_icon); | 472 id, node->GetTitle(), *folder_icon); |
484 BuildMenu(node, 0, submenu, next_menu_id); | 473 BuildMenu(node, 0, submenu, next_menu_id); |
485 } else { | 474 } else { |
486 NOTREACHED(); | 475 NOTREACHED(); |
487 } | 476 } |
488 } | 477 } |
489 } | 478 } |
490 | |
491 bool BookmarkMenuDelegate::IsOutsideMenuIdRange(int menu_id) const { | |
492 return menu_id < min_menu_id_ || menu_id > max_menu_id_; | |
493 } | |
OLD | NEW |