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/bookmarks/bookmark_model.h"
11 #include "chrome/browser/chrome_notification_types.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_
)),
51 close_on_remove_(close_on_remove
) {
53 ui::SimpleMenuModel
* menu_model
= controller_
->menu_model();
54 for (int i
= 0; i
< menu_model
->GetItemCount(); ++i
) {
55 views::MenuModelAdapter::AppendMenuItemFromModel(
56 menu_model
, i
, menu_
, menu_model
->GetCommandIdAt(i
));
60 BookmarkContextMenu::~BookmarkContextMenu() {
63 void BookmarkContextMenu::RunMenuAt(const gfx::Point
& point
,
64 ui::MenuSourceType source_type
) {
65 content::NotificationService::current()->Notify(
66 chrome::NOTIFICATION_BOOKMARK_CONTEXT_MENU_SHOWN
,
67 content::Source
<BookmarkContextMenu
>(this),
68 content::NotificationService::NoDetails());
69 // width/height don't matter here.
70 if (menu_runner_
->RunMenuAt(
71 parent_widget_
, NULL
, gfx::Rect(point
.x(), point
.y(), 0, 0),
72 views::MenuItemView::TOPLEFT
, source_type
,
73 (views::MenuRunner::HAS_MNEMONICS
| views::MenuRunner::IS_NESTED
|
74 views::MenuRunner::CONTEXT_MENU
)) ==
75 views::MenuRunner::MENU_DELETED
)
79 void BookmarkContextMenu::SetPageNavigator(PageNavigator
* navigator
) {
80 controller_
->set_navigator(navigator
);
83 ////////////////////////////////////////////////////////////////////////////////
84 // BookmarkContextMenu, views::MenuDelegate implementation:
86 void BookmarkContextMenu::ExecuteCommand(int command_id
, int event_flags
) {
87 controller_
->ExecuteCommand(command_id
, event_flags
);
90 bool BookmarkContextMenu::IsItemChecked(int command_id
) const {
91 return controller_
->IsCommandIdChecked(command_id
);
94 bool BookmarkContextMenu::IsCommandEnabled(int command_id
) const {
95 return controller_
->IsCommandIdEnabled(command_id
);
98 bool BookmarkContextMenu::ShouldCloseAllMenusOnExecute(int id
) {
99 return (id
!= IDC_BOOKMARK_BAR_REMOVE
) || close_on_remove_
;
102 ////////////////////////////////////////////////////////////////////////////////
103 // BookmarkContextMenuControllerDelegate
106 void BookmarkContextMenu::CloseMenu() {
110 void BookmarkContextMenu::WillExecuteCommand(
112 const std::vector
<const BookmarkNode
*>& bookmarks
) {
113 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
114 observer_
->WillRemoveBookmarks(bookmarks
);
117 void BookmarkContextMenu::DidExecuteCommand(int command_id
) {
118 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
119 observer_
->DidRemoveBookmarks();