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_utils.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 content::PageNavigator
;
28 using content::UserMetricsAction
;
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)),
42 menu_delegate_
->Init(this, NULL
, node
, start_child_index
,
43 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS
,
44 bookmark_utils::LAUNCH_BAR_SUBFOLDER
);
45 menu_runner_
.reset(new views::MenuRunner(menu_delegate_
->menu()));
48 void BookmarkMenuController::RunMenuAt(BookmarkBarView
* bookmark_bar
,
50 bookmark_bar_
= bookmark_bar
;
51 views::MenuButton
* menu_button
= bookmark_bar_
->GetMenuButtonForNode(node_
);
53 MenuItemView::AnchorPosition anchor
;
54 bookmark_bar_
->GetAnchorPositionForButton(menu_button
, &anchor
);
55 gfx::Point screen_loc
;
56 views::View::ConvertPointToScreen(menu_button
, &screen_loc
);
57 // Subtract 1 from the height to make the popup flush with the button border.
58 gfx::Rect
bounds(screen_loc
.x(), screen_loc
.y(), menu_button
->width(),
59 menu_button
->height() - 1);
61 menu_delegate_
->GetBookmarkModel()->AddObserver(this);
62 // We only delete ourself after the menu completes, so we can safely ignore
64 ignore_result(menu_runner_
->RunMenuAt(menu_delegate_
->parent(), menu_button
,
65 bounds
, anchor
, ui::MENU_SOURCE_NONE
,
66 for_drop
? views::MenuRunner::FOR_DROP
: 0));
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 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::MenuItemView::AnchorPosition
* anchor
,
167 views::MenuButton
** button
) {
168 if (!bookmark_bar_
|| for_drop_
)
170 gfx::Point
bookmark_bar_loc(screen_point
);
171 views::View::ConvertPointToTarget(NULL
, 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);