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 "components/bookmarks/browser/bookmark_model.h"
15 #include "content/public/browser/page_navigator.h"
16 #include "content/public/browser/user_metrics.h"
17 #include "ui/base/dragdrop/os_exchange_data.h"
18 #include "ui/views/controls/button/menu_button.h"
19 #include "ui/views/controls/menu/menu_item_view.h"
20 #include "ui/views/controls/menu/menu_runner.h"
21 #include "ui/views/widget/widget.h"
23 using base::UserMetricsAction
;
24 using bookmarks::BookmarkNode
;
25 using content::PageNavigator
;
26 using views::MenuItemView
;
28 BookmarkMenuController::BookmarkMenuController(Browser
* browser
,
29 PageNavigator
* page_navigator
,
30 views::Widget
* parent
,
31 const BookmarkNode
* node
,
32 int start_child_index
,
34 : menu_delegate_(new BookmarkMenuDelegate(browser
, page_navigator
, parent
)),
39 menu_delegate_
->Init(this, NULL
, node
, start_child_index
,
40 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS
,
41 BOOKMARK_LAUNCH_LOCATION_BAR_SUBFOLDER
);
42 menu_runner_
.reset(new views::MenuRunner(
43 menu_delegate_
->menu(), for_drop
? views::MenuRunner::FOR_DROP
: 0));
46 void BookmarkMenuController::RunMenuAt(BookmarkBarView
* bookmark_bar
) {
47 bookmark_bar_
= bookmark_bar
;
48 views::MenuButton
* menu_button
= bookmark_bar_
->GetMenuButtonForNode(node_
);
50 views::MenuAnchorPosition anchor
;
51 bookmark_bar_
->GetAnchorPositionForButton(menu_button
, &anchor
);
52 gfx::Point screen_loc
;
53 views::View::ConvertPointToScreen(menu_button
, &screen_loc
);
54 // Subtract 1 from the height to make the popup flush with the button border.
55 gfx::Rect
bounds(screen_loc
.x(), screen_loc
.y(), menu_button
->width(),
56 menu_button
->height() - 1);
57 menu_delegate_
->GetBookmarkModel()->AddObserver(this);
58 // We only delete ourself after the menu completes, so we can safely ignore
60 ignore_result(menu_runner_
->RunMenuAt(menu_delegate_
->parent(),
64 ui::MENU_SOURCE_NONE
));
69 void BookmarkMenuController::Cancel() {
70 menu_delegate_
->menu()->Cancel();
73 MenuItemView
* BookmarkMenuController::menu() const {
74 return menu_delegate_
->menu();
77 MenuItemView
* BookmarkMenuController::context_menu() const {
78 return menu_delegate_
->context_menu();
81 void BookmarkMenuController::SetPageNavigator(PageNavigator
* navigator
) {
82 menu_delegate_
->SetPageNavigator(navigator
);
85 base::string16
BookmarkMenuController::GetTooltipText(int id
,
86 const gfx::Point
& p
) const {
87 return menu_delegate_
->GetTooltipText(id
, p
);
90 bool BookmarkMenuController::IsTriggerableEvent(views::MenuItemView
* menu
,
92 return menu_delegate_
->IsTriggerableEvent(menu
, e
);
95 void BookmarkMenuController::ExecuteCommand(int id
, int mouse_event_flags
) {
96 menu_delegate_
->ExecuteCommand(id
, mouse_event_flags
);
99 bool BookmarkMenuController::ShouldExecuteCommandWithoutClosingMenu(
100 int id
, const ui::Event
& e
) {
101 return menu_delegate_
->ShouldExecuteCommandWithoutClosingMenu(id
, e
);
104 bool BookmarkMenuController::GetDropFormats(
107 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) {
108 return menu_delegate_
->GetDropFormats(menu
, formats
, custom_formats
);
111 bool BookmarkMenuController::AreDropTypesRequired(MenuItemView
* menu
) {
112 return menu_delegate_
->AreDropTypesRequired(menu
);
115 bool BookmarkMenuController::CanDrop(MenuItemView
* menu
,
116 const ui::OSExchangeData
& data
) {
117 return menu_delegate_
->CanDrop(menu
, data
);
120 int BookmarkMenuController::GetDropOperation(
122 const ui::DropTargetEvent
& event
,
123 DropPosition
* position
) {
124 return menu_delegate_
->GetDropOperation(item
, event
, position
);
127 int BookmarkMenuController::OnPerformDrop(MenuItemView
* menu
,
128 DropPosition position
,
129 const ui::DropTargetEvent
& event
) {
130 int result
= menu_delegate_
->OnPerformDrop(menu
, position
, event
);
136 bool BookmarkMenuController::ShowContextMenu(MenuItemView
* source
,
139 ui::MenuSourceType source_type
) {
140 return menu_delegate_
->ShowContextMenu(source
, id
, p
, source_type
);
143 void BookmarkMenuController::DropMenuClosed(MenuItemView
* menu
) {
147 bool BookmarkMenuController::CanDrag(MenuItemView
* menu
) {
148 return menu_delegate_
->CanDrag(menu
);
151 void BookmarkMenuController::WriteDragData(MenuItemView
* sender
,
152 ui::OSExchangeData
* data
) {
153 return menu_delegate_
->WriteDragData(sender
, data
);
156 int BookmarkMenuController::GetDragOperations(MenuItemView
* sender
) {
157 return menu_delegate_
->GetDragOperations(sender
);
160 views::MenuItemView
* BookmarkMenuController::GetSiblingMenu(
161 views::MenuItemView
* menu
,
162 const gfx::Point
& screen_point
,
163 views::MenuAnchorPosition
* anchor
,
165 views::MenuButton
** button
) {
166 if (!bookmark_bar_
|| for_drop_
)
168 gfx::Point
bookmark_bar_loc(screen_point
);
169 views::View::ConvertPointFromScreen(bookmark_bar_
, &bookmark_bar_loc
);
171 const BookmarkNode
* node
= bookmark_bar_
->GetNodeForButtonAtModelIndex(
172 bookmark_bar_loc
, &start_index
);
173 if (!node
|| !node
->is_folder())
176 menu_delegate_
->SetActiveMenu(node
, start_index
);
177 *button
= bookmark_bar_
->GetMenuButtonForNode(node
);
178 bookmark_bar_
->GetAnchorPositionForButton(*button
, anchor
);
179 *has_mnemonics
= false;
183 int BookmarkMenuController::GetMaxWidthForMenu(MenuItemView
* view
) {
184 return menu_delegate_
->GetMaxWidthForMenu(view
);
187 void BookmarkMenuController::WillShowMenu(MenuItemView
* menu
) {
188 menu_delegate_
->WillShowMenu(menu
);
191 void BookmarkMenuController::BookmarkModelChanged() {
192 if (!menu_delegate_
->is_mutating_model())
196 BookmarkMenuController::~BookmarkMenuController() {
197 menu_delegate_
->GetBookmarkModel()->RemoveObserver(this);
199 observer_
->BookmarkMenuControllerDeleted(this);