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_factory.h" | 9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 10 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h" | 12 #include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h" |
12 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" | 13 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" |
13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" | 15 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" |
15 #include "chrome/browser/ui/views/bookmarks/bookmark_drag_drop_views.h" | 16 #include "chrome/browser/ui/views/bookmarks/bookmark_drag_drop_views.h" |
16 #include "chrome/browser/ui/views/event_utils.h" | 17 #include "chrome/browser/ui/views/event_utils.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
18 #include "components/bookmarks/browser/bookmark_model.h" | 19 #include "components/bookmarks/browser/bookmark_model.h" |
19 #include "content/public/browser/page_navigator.h" | 20 #include "content/public/browser/page_navigator.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 MenuItemView* parent, | 65 MenuItemView* parent, |
65 const BookmarkNode* node, | 66 const BookmarkNode* node, |
66 int start_child_index, | 67 int start_child_index, |
67 ShowOptions show_options, | 68 ShowOptions show_options, |
68 BookmarkLaunchLocation location) { | 69 BookmarkLaunchLocation location) { |
69 GetBookmarkModel()->AddObserver(this); | 70 GetBookmarkModel()->AddObserver(this); |
70 real_delegate_ = real_delegate; | 71 real_delegate_ = real_delegate; |
71 location_ = location; | 72 location_ = location; |
72 if (parent) { | 73 if (parent) { |
73 parent_menu_item_ = parent; | 74 parent_menu_item_ = parent; |
| 75 |
| 76 // Add a separator if there are existing items in the menu, and if the |
| 77 // current node has children. If |node| is the bookmark bar then the |
| 78 // managed node is shown as its first child, if it's not empty. |
| 79 BookmarkModel* model = GetBookmarkModel(); |
| 80 ChromeBookmarkClient* client = GetChromeBookmarkClient(); |
| 81 bool show_managed = show_options == SHOW_PERMANENT_FOLDERS && |
| 82 node == model->bookmark_bar_node() && |
| 83 !client->managed_node()->empty(); |
| 84 bool has_children = |
| 85 (start_child_index < node->child_count()) || show_managed; |
74 int initial_count = parent->GetSubmenu() ? | 86 int initial_count = parent->GetSubmenu() ? |
75 parent->GetSubmenu()->GetMenuItemCount() : 0; | 87 parent->GetSubmenu()->GetMenuItemCount() : 0; |
76 if ((start_child_index < node->child_count()) && | 88 if (has_children && initial_count > 0) |
77 (initial_count > 0)) { | |
78 parent->AppendSeparator(); | 89 parent->AppendSeparator(); |
79 } | 90 if (show_managed) |
| 91 BuildMenuForManagedNode(parent, &next_menu_id_); |
80 BuildMenu(node, start_child_index, parent, &next_menu_id_); | 92 BuildMenu(node, start_child_index, parent, &next_menu_id_); |
81 if (show_options == SHOW_PERMANENT_FOLDERS) | 93 if (show_options == SHOW_PERMANENT_FOLDERS) |
82 BuildMenusForPermanentNodes(parent, &next_menu_id_); | 94 BuildMenusForPermanentNodes(parent, &next_menu_id_); |
83 } else { | 95 } else { |
84 menu_ = CreateMenu(node, start_child_index, show_options); | 96 menu_ = CreateMenu(node, start_child_index, show_options); |
85 } | 97 } |
86 } | 98 } |
87 | 99 |
88 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { | 100 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { |
89 page_navigator_ = navigator; | 101 page_navigator_ = navigator; |
90 if (context_menu_.get()) | 102 if (context_menu_.get()) |
91 context_menu_->SetPageNavigator(navigator); | 103 context_menu_->SetPageNavigator(navigator); |
92 } | 104 } |
93 | 105 |
94 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() { | 106 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() { |
95 return BookmarkModelFactory::GetForProfile(profile_); | 107 return BookmarkModelFactory::GetForProfile(profile_); |
96 } | 108 } |
97 | 109 |
| 110 ChromeBookmarkClient* BookmarkMenuDelegate::GetChromeBookmarkClient() { |
| 111 return BookmarkModelFactory::GetChromeBookmarkClientForProfile(profile_); |
| 112 } |
| 113 |
98 void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node, | 114 void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node, |
99 int start_index) { | 115 int start_index) { |
100 DCHECK(!parent_menu_item_); | 116 DCHECK(!parent_menu_item_); |
101 if (!node_to_menu_map_[node]) | 117 if (!node_to_menu_map_[node]) |
102 CreateMenu(node, start_index, HIDE_PERMANENT_FOLDERS); | 118 CreateMenu(node, start_index, HIDE_PERMANENT_FOLDERS); |
103 menu_ = node_to_menu_map_[node]; | 119 menu_ = node_to_menu_map_[node]; |
104 } | 120 } |
105 | 121 |
106 base::string16 BookmarkMenuDelegate::GetTooltipText( | 122 base::string16 BookmarkMenuDelegate::GetTooltipText( |
107 int id, | 123 int id, |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 // This can happen with SHOW_PERMANENT_FOLDERS. | 279 // This can happen with SHOW_PERMANENT_FOLDERS. |
264 drop_parent = model->bookmark_bar_node(); | 280 drop_parent = model->bookmark_bar_node(); |
265 index_to_drop_at = drop_parent->child_count(); | 281 index_to_drop_at = drop_parent->child_count(); |
266 } | 282 } |
267 break; | 283 break; |
268 | 284 |
269 default: | 285 default: |
270 break; | 286 break; |
271 } | 287 } |
272 | 288 |
| 289 bool copy = event.source_operations() == ui::DragDropTypes::DRAG_COPY; |
273 return chrome::DropBookmarks(profile_, drop_data_, | 290 return chrome::DropBookmarks(profile_, drop_data_, |
274 drop_parent, index_to_drop_at); | 291 drop_parent, index_to_drop_at, copy); |
275 } | 292 } |
276 | 293 |
277 bool BookmarkMenuDelegate::ShowContextMenu(MenuItemView* source, | 294 bool BookmarkMenuDelegate::ShowContextMenu(MenuItemView* source, |
278 int id, | 295 int id, |
279 const gfx::Point& p, | 296 const gfx::Point& p, |
280 ui::MenuSourceType source_type) { | 297 ui::MenuSourceType source_type) { |
281 DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end()); | 298 DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end()); |
282 std::vector<const BookmarkNode*> nodes; | 299 std::vector<const BookmarkNode*> nodes; |
283 nodes.push_back(menu_id_to_node_map_[id]); | 300 nodes.push_back(menu_id_to_node_map_[id]); |
284 bool close_on_delete = !parent_menu_item_ && | 301 bool close_on_delete = !parent_menu_item_ && |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 is_mutating_model_ = false; | 419 is_mutating_model_ = false; |
403 } | 420 } |
404 | 421 |
405 MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent, | 422 MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent, |
406 int start_child_index, | 423 int start_child_index, |
407 ShowOptions show_options) { | 424 ShowOptions show_options) { |
408 MenuItemView* menu = new MenuItemView(real_delegate_); | 425 MenuItemView* menu = new MenuItemView(real_delegate_); |
409 menu->SetCommand(next_menu_id_++); | 426 menu->SetCommand(next_menu_id_++); |
410 menu_id_to_node_map_[menu->GetCommand()] = parent; | 427 menu_id_to_node_map_[menu->GetCommand()] = parent; |
411 menu->set_has_icons(true); | 428 menu->set_has_icons(true); |
| 429 bool show_permanent = show_options == SHOW_PERMANENT_FOLDERS; |
| 430 if (show_permanent && parent == GetBookmarkModel()->bookmark_bar_node()) |
| 431 BuildMenuForManagedNode(menu, &next_menu_id_); |
412 BuildMenu(parent, start_child_index, menu, &next_menu_id_); | 432 BuildMenu(parent, start_child_index, menu, &next_menu_id_); |
413 if (show_options == SHOW_PERMANENT_FOLDERS) | 433 if (show_permanent) |
414 BuildMenusForPermanentNodes(menu, &next_menu_id_); | 434 BuildMenusForPermanentNodes(menu, &next_menu_id_); |
415 return menu; | 435 return menu; |
416 } | 436 } |
417 | 437 |
418 void BookmarkMenuDelegate::BuildMenusForPermanentNodes( | 438 void BookmarkMenuDelegate::BuildMenusForPermanentNodes( |
419 views::MenuItemView* menu, | 439 views::MenuItemView* menu, |
420 int* next_menu_id) { | 440 int* next_menu_id) { |
421 BookmarkModel* model = GetBookmarkModel(); | 441 BookmarkModel* model = GetBookmarkModel(); |
422 bool added_separator = false; | 442 bool added_separator = false; |
423 BuildMenuForPermanentNode(model->other_node(), menu, next_menu_id, | 443 BuildMenuForPermanentNode(model->other_node(), menu, next_menu_id, |
(...skipping 22 matching lines...) Expand all Loading... |
446 } | 466 } |
447 | 467 |
448 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 468 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
449 gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); | 469 gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); |
450 MenuItemView* submenu = menu->AppendSubMenuWithIcon( | 470 MenuItemView* submenu = menu->AppendSubMenuWithIcon( |
451 id, node->GetTitle(), *folder_icon); | 471 id, node->GetTitle(), *folder_icon); |
452 BuildMenu(node, 0, submenu, next_menu_id); | 472 BuildMenu(node, 0, submenu, next_menu_id); |
453 menu_id_to_node_map_[id] = node; | 473 menu_id_to_node_map_[id] = node; |
454 } | 474 } |
455 | 475 |
| 476 void BookmarkMenuDelegate::BuildMenuForManagedNode( |
| 477 MenuItemView* menu, |
| 478 int* next_menu_id) { |
| 479 // Don't add a separator for this menu. |
| 480 bool added_separator = true; |
| 481 const BookmarkNode* node = GetChromeBookmarkClient()->managed_node(); |
| 482 // TODO(joaodasilva): use the "managed bookmark folder" icon here. |
| 483 // http://crbug.com/49598 |
| 484 BuildMenuForPermanentNode(node, menu, next_menu_id, &added_separator); |
| 485 } |
| 486 |
456 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, | 487 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, |
457 int start_child_index, | 488 int start_child_index, |
458 MenuItemView* menu, | 489 MenuItemView* menu, |
459 int* next_menu_id) { | 490 int* next_menu_id) { |
460 node_to_menu_map_[parent] = menu; | 491 node_to_menu_map_[parent] = menu; |
461 DCHECK(parent->empty() || start_child_index < parent->child_count()); | 492 DCHECK(parent->empty() || start_child_index < parent->child_count()); |
462 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 493 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
463 for (int i = start_child_index; i < parent->child_count(); ++i) { | 494 for (int i = start_child_index; i < parent->child_count(); ++i) { |
464 const BookmarkNode* node = parent->GetChild(i); | 495 const BookmarkNode* node = parent->GetChild(i); |
465 const int id = *next_menu_id; | 496 const int id = *next_menu_id; |
(...skipping 18 matching lines...) Expand all Loading... |
484 BuildMenu(node, 0, submenu, next_menu_id); | 515 BuildMenu(node, 0, submenu, next_menu_id); |
485 } else { | 516 } else { |
486 NOTREACHED(); | 517 NOTREACHED(); |
487 } | 518 } |
488 } | 519 } |
489 } | 520 } |
490 | 521 |
491 bool BookmarkMenuDelegate::IsOutsideMenuIdRange(int menu_id) const { | 522 bool BookmarkMenuDelegate::IsOutsideMenuIdRange(int menu_id) const { |
492 return menu_id < min_menu_id_ || menu_id > max_menu_id_; | 523 return menu_id < min_menu_id_ || menu_id > max_menu_id_; |
493 } | 524 } |
OLD | NEW |