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_runner.h"
17 #include "ui/views/widget/widget.h"
19 using content::PageNavigator
;
23 // Returns true if |command_id| corresponds to a command that causes one or more
24 // bookmarks to be removed.
25 bool IsRemoveBookmarksCommand(int command_id
) {
26 return command_id
== IDC_CUT
|| command_id
== IDC_BOOKMARK_BAR_REMOVE
;
31 ////////////////////////////////////////////////////////////////////////////////
32 // BookmarkContextMenu, public:
34 BookmarkContextMenu::BookmarkContextMenu(
35 views::Widget
* parent_widget
,
38 PageNavigator
* page_navigator
,
39 const BookmarkNode
* parent
,
40 const std::vector
<const BookmarkNode
*>& selection
,
42 : controller_(new BookmarkContextMenuController(
43 parent_widget
? parent_widget
->GetNativeWindow() : NULL
, this,
44 browser
, profile
, page_navigator
, parent
, selection
)),
45 parent_widget_(parent_widget
),
46 menu_(new views::MenuItemView(this)),
47 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 menu_
->AppendMenuItemFromModel(
55 menu_model
, i
, 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(
70 parent_widget_
, NULL
, gfx::Rect(point
.x(), point
.y(), 0, 0),
71 views::MenuItemView::TOPLEFT
, source_type
,
72 (views::MenuRunner::HAS_MNEMONICS
| views::MenuRunner::IS_NESTED
|
73 views::MenuRunner::CONTEXT_MENU
)) ==
74 views::MenuRunner::MENU_DELETED
)
78 void BookmarkContextMenu::SetPageNavigator(PageNavigator
* navigator
) {
79 controller_
->set_navigator(navigator
);
82 ////////////////////////////////////////////////////////////////////////////////
83 // BookmarkContextMenu, views::MenuDelegate implementation:
85 void BookmarkContextMenu::ExecuteCommand(int command_id
, int event_flags
) {
86 controller_
->ExecuteCommand(command_id
, event_flags
);
89 bool BookmarkContextMenu::IsItemChecked(int command_id
) const {
90 return controller_
->IsCommandIdChecked(command_id
);
93 bool BookmarkContextMenu::IsCommandEnabled(int command_id
) const {
94 return controller_
->IsCommandIdEnabled(command_id
);
97 bool BookmarkContextMenu::ShouldCloseAllMenusOnExecute(int id
) {
98 return (id
!= IDC_BOOKMARK_BAR_REMOVE
) || close_on_remove_
;
101 ////////////////////////////////////////////////////////////////////////////////
102 // BookmarkContextMenuControllerDelegate
105 void BookmarkContextMenu::CloseMenu() {
109 void BookmarkContextMenu::WillExecuteCommand(
111 const std::vector
<const BookmarkNode
*>& bookmarks
) {
112 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
113 observer_
->WillRemoveBookmarks(bookmarks
);
116 void BookmarkContextMenu::DidExecuteCommand(int command_id
) {
117 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
118 observer_
->DidRemoveBookmarks();