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 "ui/views/controls/menu/menu_item_view.h"
14 #include "ui/views/controls/menu/menu_model_adapter.h"
15 #include "ui/views/controls/menu/menu_runner.h"
16 #include "ui/views/widget/widget.h"
18 using bookmarks::BookmarkNode
;
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_
,
48 views::MenuRunner::HAS_MNEMONICS
|
49 views::MenuRunner::IS_NESTED
|
50 views::MenuRunner::CONTEXT_MENU
)),
52 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(parent_widget_
,
72 gfx::Rect(point
.x(), point
.y(), 0, 0),
73 views::MENU_ANCHOR_TOPLEFT
,
74 source_type
) == 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::IsCommandVisible(int command_id
) const {
99 return controller_
->IsCommandIdVisible(command_id
);
102 bool BookmarkContextMenu::ShouldCloseAllMenusOnExecute(int id
) {
103 return (id
!= IDC_BOOKMARK_BAR_REMOVE
) || close_on_remove_
;
106 ////////////////////////////////////////////////////////////////////////////////
107 // BookmarkContextMenuControllerDelegate
110 void BookmarkContextMenu::CloseMenu() {
114 void BookmarkContextMenu::WillExecuteCommand(
116 const std::vector
<const BookmarkNode
*>& bookmarks
) {
117 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
118 observer_
->WillRemoveBookmarks(bookmarks
);
121 void BookmarkContextMenu::DidExecuteCommand(int command_id
) {
122 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
123 observer_
->DidRemoveBookmarks();