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
,
35 new BookmarkMenuDelegate(browser
, page_navigator
, parent
, 1,
41 menu_delegate_
->Init(this, NULL
, node
, start_child_index
,
42 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS
,
43 BOOKMARK_LAUNCH_LOCATION_BAR_SUBFOLDER
);
44 menu_runner_
.reset(new views::MenuRunner(
45 menu_delegate_
->menu(), for_drop
? views::MenuRunner::FOR_DROP
: 0));
48 void BookmarkMenuController::RunMenuAt(BookmarkBarView
* bookmark_bar
) {
49 bookmark_bar_
= bookmark_bar
;
50 views::MenuButton
* menu_button
= bookmark_bar_
->GetMenuButtonForNode(node_
);
52 views::MenuAnchorPosition anchor
;
53 bookmark_bar_
->GetAnchorPositionForButton(menu_button
, &anchor
);
54 gfx::Point screen_loc
;
55 views::View::ConvertPointToScreen(menu_button
, &screen_loc
);
56 // Subtract 1 from the height to make the popup flush with the button border.
57 gfx::Rect
bounds(screen_loc
.x(), screen_loc
.y(), menu_button
->width(),
58 menu_button
->height() - 1);
59 menu_delegate_
->GetBookmarkModel()->AddObserver(this);
60 // We only delete ourself after the menu completes, so we can safely ignore
62 ignore_result(menu_runner_
->RunMenuAt(menu_delegate_
->parent(),
66 ui::MENU_SOURCE_NONE
));
71 void BookmarkMenuController::Cancel() {
72 menu_delegate_
->menu()->Cancel();
75 MenuItemView
* BookmarkMenuController::menu() const {
76 return menu_delegate_
->menu();
79 MenuItemView
* BookmarkMenuController::context_menu() const {
80 return menu_delegate_
->context_menu();
83 void BookmarkMenuController::SetPageNavigator(PageNavigator
* navigator
) {
84 menu_delegate_
->SetPageNavigator(navigator
);
87 base::string16
BookmarkMenuController::GetTooltipText(int id
,
88 const gfx::Point
& p
) const {
89 return menu_delegate_
->GetTooltipText(id
, p
);
92 bool BookmarkMenuController::IsTriggerableEvent(views::MenuItemView
* menu
,
94 return menu_delegate_
->IsTriggerableEvent(menu
, e
);
97 void BookmarkMenuController::ExecuteCommand(int id
, int mouse_event_flags
) {
98 menu_delegate_
->ExecuteCommand(id
, mouse_event_flags
);
101 bool BookmarkMenuController::ShouldExecuteCommandWithoutClosingMenu(
102 int id
, const ui::Event
& e
) {
103 return menu_delegate_
->ShouldExecuteCommandWithoutClosingMenu(id
, e
);
106 bool BookmarkMenuController::GetDropFormats(
109 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) {
110 return menu_delegate_
->GetDropFormats(menu
, formats
, custom_formats
);
113 bool BookmarkMenuController::AreDropTypesRequired(MenuItemView
* menu
) {
114 return menu_delegate_
->AreDropTypesRequired(menu
);
117 bool BookmarkMenuController::CanDrop(MenuItemView
* menu
,
118 const ui::OSExchangeData
& data
) {
119 return menu_delegate_
->CanDrop(menu
, data
);
122 int BookmarkMenuController::GetDropOperation(
124 const ui::DropTargetEvent
& event
,
125 DropPosition
* position
) {
126 return menu_delegate_
->GetDropOperation(item
, event
, position
);
129 int BookmarkMenuController::OnPerformDrop(MenuItemView
* menu
,
130 DropPosition position
,
131 const ui::DropTargetEvent
& event
) {
132 int result
= menu_delegate_
->OnPerformDrop(menu
, position
, event
);
138 bool BookmarkMenuController::ShowContextMenu(MenuItemView
* source
,
141 ui::MenuSourceType source_type
) {
142 return menu_delegate_
->ShowContextMenu(source
, id
, p
, source_type
);
145 void BookmarkMenuController::DropMenuClosed(MenuItemView
* menu
) {
149 bool BookmarkMenuController::CanDrag(MenuItemView
* menu
) {
150 return menu_delegate_
->CanDrag(menu
);
153 void BookmarkMenuController::WriteDragData(MenuItemView
* sender
,
154 ui::OSExchangeData
* data
) {
155 return menu_delegate_
->WriteDragData(sender
, data
);
158 int BookmarkMenuController::GetDragOperations(MenuItemView
* sender
) {
159 return menu_delegate_
->GetDragOperations(sender
);
162 views::MenuItemView
* BookmarkMenuController::GetSiblingMenu(
163 views::MenuItemView
* menu
,
164 const gfx::Point
& screen_point
,
165 views::MenuAnchorPosition
* anchor
,
167 views::MenuButton
** button
) {
168 if (!bookmark_bar_
|| for_drop_
)
170 gfx::Point
bookmark_bar_loc(screen_point
);
171 views::View::ConvertPointFromScreen(bookmark_bar_
, &bookmark_bar_loc
);
173 const BookmarkNode
* node
= bookmark_bar_
->GetNodeForButtonAtModelIndex(
174 bookmark_bar_loc
, &start_index
);
175 if (!node
|| !node
->is_folder())
178 menu_delegate_
->SetActiveMenu(node
, start_index
);
179 *button
= bookmark_bar_
->GetMenuButtonForNode(node
);
180 bookmark_bar_
->GetAnchorPositionForButton(*button
, anchor
);
181 *has_mnemonics
= false;
185 int BookmarkMenuController::GetMaxWidthForMenu(MenuItemView
* view
) {
186 return menu_delegate_
->GetMaxWidthForMenu(view
);
189 void BookmarkMenuController::BookmarkModelChanged() {
190 if (!menu_delegate_
->is_mutating_model())
194 BookmarkMenuController::~BookmarkMenuController() {
195 menu_delegate_
->GetBookmarkModel()->RemoveObserver(this);
197 observer_
->BookmarkMenuControllerDeleted(this);