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 #ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_
6 #define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_
11 #include "base/compiler_specific.h"
12 #include "chrome/browser/bookmarks/bookmark_stats.h"
13 #include "chrome/browser/ui/views/bookmarks/bookmark_context_menu.h"
14 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
15 #include "components/bookmarks/browser/bookmark_node_data.h"
16 #include "ui/views/controls/menu/menu_delegate.h"
19 class ChromeBookmarkClient
;
35 // BookmarkMenuDelegate acts as the (informal) views::MenuDelegate for showing
36 // bookmarks in a MenuItemView. BookmarkMenuDelegate informally implements
37 // MenuDelegate as its assumed another class is going to forward the appropriate
38 // methods to this class. Doing so allows this class to be used for both menus
39 // on the bookmark bar and the bookmarks in the wrench menu.
40 class BookmarkMenuDelegate
: public bookmarks::BaseBookmarkModelObserver
,
41 public BookmarkContextMenuObserver
{
44 // Indicates a menu should be added containing the permanent folders (other
45 // than then bookmark bar folder). This only makes sense when showing the
46 // contents of the bookmark bar folder.
47 SHOW_PERMANENT_FOLDERS
,
49 // Don't show any additional folders.
50 HIDE_PERMANENT_FOLDERS
53 BookmarkMenuDelegate(Browser
* browser
,
54 content::PageNavigator
* navigator
,
55 views::Widget
* parent
);
56 ~BookmarkMenuDelegate() override
;
58 // Creates the menus from the model.
59 void Init(views::MenuDelegate
* real_delegate
,
60 views::MenuItemView
* parent
,
61 const bookmarks::BookmarkNode
* node
,
62 int start_child_index
,
63 ShowOptions show_options
,
64 BookmarkLaunchLocation location
);
66 // Sets the PageNavigator.
67 void SetPageNavigator(content::PageNavigator
* navigator
);
69 // Returns the id given to the next menu.
70 int next_menu_id() const { return next_menu_id_
; }
72 // Makes the menu for |node| the active menu. |start_index| is the index of
73 // the first child of |node| to show in the menu.
74 void SetActiveMenu(const bookmarks::BookmarkNode
* node
, int start_index
);
76 bookmarks::BookmarkModel
* GetBookmarkModel();
77 ChromeBookmarkClient
* GetChromeBookmarkClient();
80 views::MenuItemView
* menu() { return menu_
; }
82 // Returns the context menu, or NULL if the context menu isn't showing.
83 views::MenuItemView
* context_menu() {
84 return context_menu_
.get() ? context_menu_
->menu() : NULL
;
87 views::Widget
* parent() { return parent_
; }
88 const views::Widget
* parent() const { return parent_
; }
90 // Returns true if we're in the process of mutating the model. This happens
91 // when the user deletes menu items using the context menu.
92 bool is_mutating_model() const { return is_mutating_model_
; }
94 // MenuDelegate like methods (see class description for details).
95 base::string16
GetTooltipText(int id
, const gfx::Point
& p
) const;
96 bool IsTriggerableEvent(views::MenuItemView
* menu
,
98 void ExecuteCommand(int id
, int mouse_event_flags
);
99 bool ShouldExecuteCommandWithoutClosingMenu(int id
, const ui::Event
& e
);
101 views::MenuItemView
* menu
,
103 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
);
104 bool AreDropTypesRequired(views::MenuItemView
* menu
);
105 bool CanDrop(views::MenuItemView
* menu
, const ui::OSExchangeData
& data
);
106 int GetDropOperation(views::MenuItemView
* item
,
107 const ui::DropTargetEvent
& event
,
108 views::MenuDelegate::DropPosition
* position
);
109 int OnPerformDrop(views::MenuItemView
* menu
,
110 views::MenuDelegate::DropPosition position
,
111 const ui::DropTargetEvent
& event
);
112 bool ShowContextMenu(views::MenuItemView
* source
,
115 ui::MenuSourceType source_type
);
116 bool CanDrag(views::MenuItemView
* menu
);
117 void WriteDragData(views::MenuItemView
* sender
, ui::OSExchangeData
* data
);
118 int GetDragOperations(views::MenuItemView
* sender
);
119 int GetMaxWidthForMenu(views::MenuItemView
* menu
);
120 void WillShowMenu(views::MenuItemView
* menu
);
122 // BookmarkModelObserver methods.
123 void BookmarkModelChanged() override
;
124 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel
* model
,
125 const bookmarks::BookmarkNode
* node
) override
;
127 // BookmarkContextMenu::Observer methods.
128 void WillRemoveBookmarks(
129 const std::vector
<const bookmarks::BookmarkNode
*>& bookmarks
) override
;
130 void DidRemoveBookmarks() override
;
133 friend class BookmarkMenuDelegateTest
;
135 typedef std::map
<int, const bookmarks::BookmarkNode
*> MenuIDToNodeMap
;
136 typedef std::map
<const bookmarks::BookmarkNode
*, views::MenuItemView
*>
139 // Creates a menu. This uses BuildMenu() to recursively populate the menu.
140 views::MenuItemView
* CreateMenu(const bookmarks::BookmarkNode
* parent
,
141 int start_child_index
,
142 ShowOptions show_options
);
144 // Invokes BuildMenuForPermanentNode() for the permanent nodes (excluding
145 // 'other bookmarks' folder).
146 void BuildMenusForPermanentNodes(views::MenuItemView
* menu
);
148 // If |node| has children a new menu is created and added to |menu| to
149 // represent it. If |node| is not empty and |added_separator| is false, a
150 // separator is added before the new menu items and |added_separator| is set
152 void BuildMenuForPermanentNode(const bookmarks::BookmarkNode
* node
,
153 int icon_resource_id
,
154 views::MenuItemView
* menu
,
155 bool* added_separator
);
157 void BuildMenuForManagedNode(views::MenuItemView
* menu
);
158 void BuildMenuForSupervisedNode(views::MenuItemView
* menu
);
160 // Creates an entry in menu for each child node of |parent| starting at
161 // |start_child_index|.
162 void BuildMenu(const bookmarks::BookmarkNode
* parent
,
163 int start_child_index
,
164 views::MenuItemView
* menu
);
166 // Registers the necessary mappings for |menu| and |node|.
167 void AddMenuToMaps(views::MenuItemView
* menu
,
168 const bookmarks::BookmarkNode
* node
);
173 content::PageNavigator
* page_navigator_
;
176 views::Widget
* parent_
;
178 // Maps from menu id to BookmarkNode.
179 MenuIDToNodeMap menu_id_to_node_map_
;
182 views::MenuItemView
* menu_
;
184 // Data for the drop.
185 bookmarks::BookmarkNodeData drop_data_
;
187 // Used when a context menu is shown.
188 scoped_ptr
<BookmarkContextMenu
> context_menu_
;
190 // If non-NULL this is the |parent| passed to Init and is NOT owned by us.
191 views::MenuItemView
* parent_menu_item_
;
193 // Maps from node to menu.
194 NodeToMenuMap node_to_menu_map_
;
196 // ID of the next menu item.
199 views::MenuDelegate
* real_delegate_
;
201 // Is the model being changed?
202 bool is_mutating_model_
;
204 // The location where this bookmark menu will be displayed (for UMA).
205 BookmarkLaunchLocation location_
;
207 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegate
);
210 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_