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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/bookmarks/bookmark_context_menu.h"
7 #include "base/i18n/rtl.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "components/bookmarks/browser/bookmark_model.h"
12 #include "content/public/browser/notification_service.h"
13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/views/controls/menu/menu_item_view.h"
16 #include "ui/views/controls/menu/menu_model_adapter.h"
17 #include "ui/views/controls/menu/menu_runner.h"
18 #include "ui/views/widget/widget.h"
20 using content::PageNavigator
;
24 // Returns true if |command_id| corresponds to a command that causes one or more
25 // bookmarks to be removed.
26 bool IsRemoveBookmarksCommand(int command_id
) {
27 return command_id
== IDC_CUT
|| command_id
== IDC_BOOKMARK_BAR_REMOVE
;
32 ////////////////////////////////////////////////////////////////////////////////
33 // BookmarkContextMenu, public:
35 BookmarkContextMenu::BookmarkContextMenu(
36 views::Widget
* parent_widget
,
39 PageNavigator
* page_navigator
,
40 const BookmarkNode
* parent
,
41 const std::vector
<const BookmarkNode
*>& selection
,
43 : controller_(new BookmarkContextMenuController(
44 parent_widget
? parent_widget
->GetNativeWindow() : NULL
, this,
45 browser
, profile
, page_navigator
, parent
, selection
)),
46 parent_widget_(parent_widget
),
47 menu_(new views::MenuItemView(this)),
48 menu_runner_(new views::MenuRunner(menu_
)),
50 close_on_remove_(close_on_remove
) {
52 ui::SimpleMenuModel
* menu_model
= controller_
->menu_model();
53 for (int i
= 0; i
< menu_model
->GetItemCount(); ++i
) {
54 views::MenuModelAdapter::AppendMenuItemFromModel(
55 menu_model
, i
, menu_
, menu_model
->GetCommandIdAt(i
));
59 BookmarkContextMenu::~BookmarkContextMenu() {
62 void BookmarkContextMenu::RunMenuAt(const gfx::Point
& point
,
63 ui::MenuSourceType source_type
) {
64 content::NotificationService::current()->Notify(
65 chrome::NOTIFICATION_BOOKMARK_CONTEXT_MENU_SHOWN
,
66 content::Source
<BookmarkContextMenu
>(this),
67 content::NotificationService::NoDetails());
68 // width/height don't matter here.
69 if (menu_runner_
->RunMenuAt(
72 gfx::Rect(point
.x(), point
.y(), 0, 0),
73 views::MENU_ANCHOR_TOPLEFT
,
75 (views::MenuRunner::HAS_MNEMONICS
| views::MenuRunner::IS_NESTED
|
76 views::MenuRunner::CONTEXT_MENU
)) ==
77 views::MenuRunner::MENU_DELETED
) {
82 void BookmarkContextMenu::SetPageNavigator(PageNavigator
* navigator
) {
83 controller_
->set_navigator(navigator
);
86 ////////////////////////////////////////////////////////////////////////////////
87 // BookmarkContextMenu, views::MenuDelegate implementation:
89 void BookmarkContextMenu::ExecuteCommand(int command_id
, int event_flags
) {
90 controller_
->ExecuteCommand(command_id
, event_flags
);
93 bool BookmarkContextMenu::IsItemChecked(int command_id
) const {
94 return controller_
->IsCommandIdChecked(command_id
);
97 bool BookmarkContextMenu::IsCommandEnabled(int command_id
) const {
98 return controller_
->IsCommandIdEnabled(command_id
);
101 bool BookmarkContextMenu::ShouldCloseAllMenusOnExecute(int id
) {
102 return (id
!= IDC_BOOKMARK_BAR_REMOVE
) || close_on_remove_
;
105 ////////////////////////////////////////////////////////////////////////////////
106 // BookmarkContextMenuControllerDelegate
109 void BookmarkContextMenu::CloseMenu() {
113 void BookmarkContextMenu::WillExecuteCommand(
115 const std::vector
<const BookmarkNode
*>& bookmarks
) {
116 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
117 observer_
->WillRemoveBookmarks(bookmarks
);
120 void BookmarkContextMenu::DidExecuteCommand(int command_id
) {
121 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
122 observer_
->DidRemoveBookmarks();