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_menu_controller_views.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/stl_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/bookmarks/bookmark_stats.h"
11 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
12 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_controller_observer.h"
13 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/bookmarks/browser/bookmark_model.h"
16 #include "content/public/browser/page_navigator.h"
17 #include "content/public/browser/user_metrics.h"
18 #include "grit/generated_resources.h"
19 #include "grit/theme_resources.h"
20 #include "grit/ui_resources.h"
21 #include "ui/base/dragdrop/os_exchange_data.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/views/controls/button/menu_button.h"
24 #include "ui/views/controls/menu/menu_item_view.h"
25 #include "ui/views/controls/menu/menu_runner.h"
26 #include "ui/views/widget/widget.h"
28 using base::UserMetricsAction
;
29 using content::PageNavigator
;
30 using views::MenuItemView
;
32 BookmarkMenuController::BookmarkMenuController(Browser
* browser
,
33 PageNavigator
* page_navigator
,
34 views::Widget
* parent
,
35 const BookmarkNode
* node
,
36 int start_child_index
)
38 new BookmarkMenuDelegate(browser
, page_navigator
, parent
, 1,
44 menu_delegate_
->Init(this, NULL
, node
, start_child_index
,
45 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS
,
46 BOOKMARK_LAUNCH_LOCATION_BAR_SUBFOLDER
);
47 menu_runner_
.reset(new views::MenuRunner(menu_delegate_
->menu()));
50 void BookmarkMenuController::RunMenuAt(BookmarkBarView
* bookmark_bar
,
52 bookmark_bar_
= bookmark_bar
;
53 views::MenuButton
* menu_button
= bookmark_bar_
->GetMenuButtonForNode(node_
);
55 views::MenuAnchorPosition anchor
;
56 bookmark_bar_
->GetAnchorPositionForButton(menu_button
, &anchor
);
57 gfx::Point screen_loc
;
58 views::View::ConvertPointToScreen(menu_button
, &screen_loc
);
59 // Subtract 1 from the height to make the popup flush with the button border.
60 gfx::Rect
bounds(screen_loc
.x(), screen_loc
.y(), menu_button
->width(),
61 menu_button
->height() - 1);
63 menu_delegate_
->GetBookmarkModel()->AddObserver(this);
64 // We only delete ourself after the menu completes, so we can safely ignore
66 ignore_result(menu_runner_
->RunMenuAt(menu_delegate_
->parent(), menu_button
,
67 bounds
, anchor
, ui::MENU_SOURCE_NONE
,
68 for_drop
? views::MenuRunner::FOR_DROP
: 0));
73 void BookmarkMenuController::Cancel() {
74 menu_delegate_
->menu()->Cancel();
77 MenuItemView
* BookmarkMenuController::menu() const {
78 return menu_delegate_
->menu();
81 MenuItemView
* BookmarkMenuController::context_menu() const {
82 return menu_delegate_
->context_menu();
85 void BookmarkMenuController::SetPageNavigator(PageNavigator
* navigator
) {
86 menu_delegate_
->SetPageNavigator(navigator
);
89 base::string16
BookmarkMenuController::GetTooltipText(int id
,
90 const gfx::Point
& p
) const {
91 return menu_delegate_
->GetTooltipText(id
, p
);
94 bool BookmarkMenuController::IsTriggerableEvent(views::MenuItemView
* menu
,
96 return menu_delegate_
->IsTriggerableEvent(menu
, e
);
99 void BookmarkMenuController::ExecuteCommand(int id
, int mouse_event_flags
) {
100 menu_delegate_
->ExecuteCommand(id
, mouse_event_flags
);
103 bool BookmarkMenuController::ShouldExecuteCommandWithoutClosingMenu(
104 int id
, const ui::Event
& e
) {
105 return menu_delegate_
->ShouldExecuteCommandWithoutClosingMenu(id
, e
);
108 bool BookmarkMenuController::GetDropFormats(
111 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) {
112 return menu_delegate_
->GetDropFormats(menu
, formats
, custom_formats
);
115 bool BookmarkMenuController::AreDropTypesRequired(MenuItemView
* menu
) {
116 return menu_delegate_
->AreDropTypesRequired(menu
);
119 bool BookmarkMenuController::CanDrop(MenuItemView
* menu
,
120 const ui::OSExchangeData
& data
) {
121 return menu_delegate_
->CanDrop(menu
, data
);
124 int BookmarkMenuController::GetDropOperation(
126 const ui::DropTargetEvent
& event
,
127 DropPosition
* position
) {
128 return menu_delegate_
->GetDropOperation(item
, event
, position
);
131 int BookmarkMenuController::OnPerformDrop(MenuItemView
* menu
,
132 DropPosition position
,
133 const ui::DropTargetEvent
& event
) {
134 int result
= menu_delegate_
->OnPerformDrop(menu
, position
, event
);
140 bool BookmarkMenuController::ShowContextMenu(MenuItemView
* source
,
143 ui::MenuSourceType source_type
) {
144 return menu_delegate_
->ShowContextMenu(source
, id
, p
, source_type
);
147 void BookmarkMenuController::DropMenuClosed(MenuItemView
* menu
) {
151 bool BookmarkMenuController::CanDrag(MenuItemView
* menu
) {
152 return menu_delegate_
->CanDrag(menu
);
155 void BookmarkMenuController::WriteDragData(MenuItemView
* sender
,
156 ui::OSExchangeData
* data
) {
157 return menu_delegate_
->WriteDragData(sender
, data
);
160 int BookmarkMenuController::GetDragOperations(MenuItemView
* sender
) {
161 return menu_delegate_
->GetDragOperations(sender
);
164 views::MenuItemView
* BookmarkMenuController::GetSiblingMenu(
165 views::MenuItemView
* menu
,
166 const gfx::Point
& screen_point
,
167 views::MenuAnchorPosition
* anchor
,
169 views::MenuButton
** button
) {
170 if (!bookmark_bar_
|| for_drop_
)
172 gfx::Point
bookmark_bar_loc(screen_point
);
173 views::View::ConvertPointFromScreen(bookmark_bar_
, &bookmark_bar_loc
);
175 const BookmarkNode
* node
= bookmark_bar_
->GetNodeForButtonAtModelIndex(
176 bookmark_bar_loc
, &start_index
);
177 if (!node
|| !node
->is_folder())
180 menu_delegate_
->SetActiveMenu(node
, start_index
);
181 *button
= bookmark_bar_
->GetMenuButtonForNode(node
);
182 bookmark_bar_
->GetAnchorPositionForButton(*button
, anchor
);
183 *has_mnemonics
= false;
187 int BookmarkMenuController::GetMaxWidthForMenu(MenuItemView
* view
) {
188 return menu_delegate_
->GetMaxWidthForMenu(view
);
191 void BookmarkMenuController::BookmarkModelChanged() {
192 if (!menu_delegate_
->is_mutating_model())
196 BookmarkMenuController::~BookmarkMenuController() {
197 menu_delegate_
->GetBookmarkModel()->RemoveObserver(this);
199 observer_
->BookmarkMenuControllerDeleted(this);