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/command_line.h"
8 #include "base/i18n/rtl.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "content/public/browser/notification_service.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 bookmarks::BookmarkNode
;
21 using content::PageNavigator
;
25 // Returns true if |command_id| corresponds to a command that causes one or more
26 // bookmarks to be removed.
27 bool IsRemoveBookmarksCommand(int command_id
) {
28 return command_id
== IDC_CUT
|| command_id
== IDC_BOOKMARK_BAR_REMOVE
;
33 ////////////////////////////////////////////////////////////////////////////////
34 // BookmarkContextMenu, public:
36 BookmarkContextMenu::BookmarkContextMenu(
37 views::Widget
* parent_widget
,
40 PageNavigator
* page_navigator
,
41 const BookmarkNode
* parent
,
42 const std::vector
<const BookmarkNode
*>& selection
,
44 : controller_(new BookmarkContextMenuController(
45 parent_widget
? parent_widget
->GetNativeWindow() : NULL
, this,
46 browser
, profile
, page_navigator
, parent
, selection
)),
47 parent_widget_(parent_widget
),
48 menu_(new views::MenuItemView(this)),
49 menu_runner_(new views::MenuRunner(menu_
,
50 views::MenuRunner::HAS_MNEMONICS
|
51 views::MenuRunner::IS_NESTED
|
52 views::MenuRunner::CONTEXT_MENU
)),
54 close_on_remove_(close_on_remove
) {
55 ui::SimpleMenuModel
* menu_model
= controller_
->menu_model();
56 for (int i
= 0; i
< menu_model
->GetItemCount(); ++i
) {
57 views::MenuModelAdapter::AppendMenuItemFromModel(
58 menu_model
, i
, menu_
, menu_model
->GetCommandIdAt(i
));
62 BookmarkContextMenu::~BookmarkContextMenu() {
65 void BookmarkContextMenu::RunMenuAt(const gfx::Point
& point
,
66 ui::MenuSourceType source_type
) {
67 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode
))
70 content::NotificationService::current()->Notify(
71 chrome::NOTIFICATION_BOOKMARK_CONTEXT_MENU_SHOWN
,
72 content::Source
<BookmarkContextMenu
>(this),
73 content::NotificationService::NoDetails());
74 // width/height don't matter here.
75 if (menu_runner_
->RunMenuAt(parent_widget_
,
77 gfx::Rect(point
.x(), point
.y(), 0, 0),
78 views::MENU_ANCHOR_TOPLEFT
,
79 source_type
) == views::MenuRunner::MENU_DELETED
) {
84 void BookmarkContextMenu::SetPageNavigator(PageNavigator
* navigator
) {
85 controller_
->set_navigator(navigator
);
88 ////////////////////////////////////////////////////////////////////////////////
89 // BookmarkContextMenu, views::MenuDelegate implementation:
91 void BookmarkContextMenu::ExecuteCommand(int command_id
, int event_flags
) {
92 controller_
->ExecuteCommand(command_id
, event_flags
);
95 bool BookmarkContextMenu::IsItemChecked(int command_id
) const {
96 return controller_
->IsCommandIdChecked(command_id
);
99 bool BookmarkContextMenu::IsCommandEnabled(int command_id
) const {
100 return controller_
->IsCommandIdEnabled(command_id
);
103 bool BookmarkContextMenu::IsCommandVisible(int command_id
) const {
104 return controller_
->IsCommandIdVisible(command_id
);
107 bool BookmarkContextMenu::ShouldCloseAllMenusOnExecute(int id
) {
108 return (id
!= IDC_BOOKMARK_BAR_REMOVE
) || close_on_remove_
;
111 ////////////////////////////////////////////////////////////////////////////////
112 // BookmarkContextMenuControllerDelegate
115 void BookmarkContextMenu::CloseMenu() {
119 void BookmarkContextMenu::WillExecuteCommand(
121 const std::vector
<const BookmarkNode
*>& bookmarks
) {
122 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
123 observer_
->WillRemoveBookmarks(bookmarks
);
126 void BookmarkContextMenu::DidExecuteCommand(int command_id
) {
127 if (observer_
&& IsRemoveBookmarksCommand(command_id
))
128 observer_
->DidRemoveBookmarks();