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_model.h"
11 #include "chrome/browser/bookmarks/bookmark_stats.h"
12 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
13 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_controller_observer.h"
14 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h"
15 #include "chrome/common/pref_names.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_runner.h"
25 #include "ui/views/widget/widget.h"
27 using base::UserMetricsAction
;
28 using content::PageNavigator
;
29 using views::MenuItemView
;
31 BookmarkMenuController::BookmarkMenuController(Browser
* browser
,
32 PageNavigator
* page_navigator
,
33 views::Widget
* parent
,
34 const BookmarkNode
* node
,
35 int start_child_index
)
37 new BookmarkMenuDelegate(browser
, page_navigator
, parent
, 1,
43 menu_delegate_
->Init(this, NULL
, node
, start_child_index
,
44 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS
,
45 BOOKMARK_LAUNCH_LOCATION_BAR_SUBFOLDER
);
46 menu_runner_
.reset(new views::MenuRunner(menu_delegate_
->menu()));
49 void BookmarkMenuController::RunMenuAt(BookmarkBarView
* bookmark_bar
,
51 bookmark_bar_
= bookmark_bar
;
52 views::MenuButton
* menu_button
= bookmark_bar_
->GetMenuButtonForNode(node_
);
54 MenuItemView::AnchorPosition anchor
;
55 bookmark_bar_
->GetAnchorPositionForButton(menu_button
, &anchor
);
56 gfx::Point screen_loc
;
57 views::View::ConvertPointToScreen(menu_button
, &screen_loc
);
58 // Subtract 1 from the height to make the popup flush with the button border.
59 gfx::Rect
bounds(screen_loc
.x(), screen_loc
.y(), menu_button
->width(),
60 menu_button
->height() - 1);
62 menu_delegate_
->GetBookmarkModel()->AddObserver(this);
63 // We only delete ourself after the menu completes, so we can safely ignore
65 ignore_result(menu_runner_
->RunMenuAt(menu_delegate_
->parent(), menu_button
,
66 bounds
, anchor
, ui::MENU_SOURCE_NONE
,
67 for_drop
? views::MenuRunner::FOR_DROP
: 0));
72 void BookmarkMenuController::Cancel() {
73 menu_delegate_
->menu()->Cancel();
76 MenuItemView
* BookmarkMenuController::menu() const {
77 return menu_delegate_
->menu();
80 MenuItemView
* BookmarkMenuController::context_menu() const {
81 return menu_delegate_
->context_menu();
84 void BookmarkMenuController::SetPageNavigator(PageNavigator
* navigator
) {
85 menu_delegate_
->SetPageNavigator(navigator
);
88 base::string16
BookmarkMenuController::GetTooltipText(int id
,
89 const gfx::Point
& p
) const {
90 return menu_delegate_
->GetTooltipText(id
, p
);
93 bool BookmarkMenuController::IsTriggerableEvent(views::MenuItemView
* menu
,
95 return menu_delegate_
->IsTriggerableEvent(menu
, e
);
98 void BookmarkMenuController::ExecuteCommand(int id
, int mouse_event_flags
) {
99 menu_delegate_
->ExecuteCommand(id
, mouse_event_flags
);
102 bool BookmarkMenuController::ShouldExecuteCommandWithoutClosingMenu(
103 int id
, const ui::Event
& e
) {
104 return menu_delegate_
->ShouldExecuteCommandWithoutClosingMenu(id
, e
);
107 bool BookmarkMenuController::GetDropFormats(
110 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) {
111 return menu_delegate_
->GetDropFormats(menu
, formats
, custom_formats
);
114 bool BookmarkMenuController::AreDropTypesRequired(MenuItemView
* menu
) {
115 return menu_delegate_
->AreDropTypesRequired(menu
);
118 bool BookmarkMenuController::CanDrop(MenuItemView
* menu
,
119 const ui::OSExchangeData
& data
) {
120 return menu_delegate_
->CanDrop(menu
, data
);
123 int BookmarkMenuController::GetDropOperation(
125 const ui::DropTargetEvent
& event
,
126 DropPosition
* position
) {
127 return menu_delegate_
->GetDropOperation(item
, event
, position
);
130 int BookmarkMenuController::OnPerformDrop(MenuItemView
* menu
,
131 DropPosition position
,
132 const ui::DropTargetEvent
& event
) {
133 int result
= menu_delegate_
->OnPerformDrop(menu
, position
, event
);
139 bool BookmarkMenuController::ShowContextMenu(MenuItemView
* source
,
142 ui::MenuSourceType source_type
) {
143 return menu_delegate_
->ShowContextMenu(source
, id
, p
, source_type
);
146 void BookmarkMenuController::DropMenuClosed(MenuItemView
* menu
) {
150 bool BookmarkMenuController::CanDrag(MenuItemView
* menu
) {
151 return menu_delegate_
->CanDrag(menu
);
154 void BookmarkMenuController::WriteDragData(MenuItemView
* sender
,
155 ui::OSExchangeData
* data
) {
156 return menu_delegate_
->WriteDragData(sender
, data
);
159 int BookmarkMenuController::GetDragOperations(MenuItemView
* sender
) {
160 return menu_delegate_
->GetDragOperations(sender
);
163 views::MenuItemView
* BookmarkMenuController::GetSiblingMenu(
164 views::MenuItemView
* menu
,
165 const gfx::Point
& screen_point
,
166 views::MenuItemView::AnchorPosition
* anchor
,
168 views::MenuButton
** button
) {
169 if (!bookmark_bar_
|| for_drop_
)
171 gfx::Point
bookmark_bar_loc(screen_point
);
172 views::View::ConvertPointFromScreen(bookmark_bar_
, &bookmark_bar_loc
);
174 const BookmarkNode
* node
= bookmark_bar_
->GetNodeForButtonAtModelIndex(
175 bookmark_bar_loc
, &start_index
);
176 if (!node
|| !node
->is_folder())
179 menu_delegate_
->SetActiveMenu(node
, start_index
);
180 *button
= bookmark_bar_
->GetMenuButtonForNode(node
);
181 bookmark_bar_
->GetAnchorPositionForButton(*button
, anchor
);
182 *has_mnemonics
= false;
186 int BookmarkMenuController::GetMaxWidthForMenu(MenuItemView
* view
) {
187 return menu_delegate_
->GetMaxWidthForMenu(view
);
190 void BookmarkMenuController::BookmarkModelChanged() {
191 if (!menu_delegate_
->is_mutating_model())
195 BookmarkMenuController::~BookmarkMenuController() {
196 menu_delegate_
->GetBookmarkModel()->RemoveObserver(this);
198 observer_
->BookmarkMenuControllerDeleted(this);