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