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"
22 class ManagedBookmarkService
;
42 // BookmarkMenuDelegate acts as the (informal) views::MenuDelegate for showing
43 // bookmarks in a MenuItemView. BookmarkMenuDelegate informally implements
44 // MenuDelegate as its assumed another class is going to forward the appropriate
45 // methods to this class. Doing so allows this class to be used for both menus
46 // on the bookmark bar and the bookmarks in the wrench menu.
47 class BookmarkMenuDelegate
: public bookmarks::BaseBookmarkModelObserver
,
48 public BookmarkContextMenuObserver
{
51 // Indicates a menu should be added containing the permanent folders (other
52 // than then bookmark bar folder). This only makes sense when showing the
53 // contents of the bookmark bar folder.
54 SHOW_PERMANENT_FOLDERS
,
56 // Don't show any additional folders.
57 HIDE_PERMANENT_FOLDERS
60 BookmarkMenuDelegate(Browser
* browser
,
61 content::PageNavigator
* navigator
,
62 views::Widget
* parent
);
63 ~BookmarkMenuDelegate() override
;
65 // Creates the menus from the model.
66 void Init(views::MenuDelegate
* real_delegate
,
67 views::MenuItemView
* parent
,
68 const bookmarks::BookmarkNode
* node
,
69 int start_child_index
,
70 ShowOptions show_options
,
71 BookmarkLaunchLocation location
);
73 // Sets the PageNavigator.
74 void SetPageNavigator(content::PageNavigator
* navigator
);
76 // Returns the id given to the next menu.
77 int next_menu_id() const { return next_menu_id_
; }
79 // Makes the menu for |node| the active menu. |start_index| is the index of
80 // the first child of |node| to show in the menu.
81 void SetActiveMenu(const bookmarks::BookmarkNode
* node
, int start_index
);
83 bookmarks::BookmarkModel
* GetBookmarkModel();
84 bookmarks::ManagedBookmarkService
* GetManagedBookmarkService();
87 views::MenuItemView
* menu() { return menu_
; }
89 // Returns the context menu, or NULL if the context menu isn't showing.
90 views::MenuItemView
* context_menu() {
91 return context_menu_
.get() ? context_menu_
->menu() : NULL
;
94 views::Widget
* parent() { return parent_
; }
95 const views::Widget
* parent() const { return parent_
; }
97 // Returns true if we're in the process of mutating the model. This happens
98 // when the user deletes menu items using the context menu.
99 bool is_mutating_model() const { return is_mutating_model_
; }
101 // MenuDelegate like methods (see class description for details).
102 base::string16
GetTooltipText(int id
, const gfx::Point
& p
) const;
103 bool IsTriggerableEvent(views::MenuItemView
* menu
,
105 void ExecuteCommand(int id
, int mouse_event_flags
);
106 bool ShouldExecuteCommandWithoutClosingMenu(int id
, const ui::Event
& e
);
108 views::MenuItemView
* menu
,
110 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
);
111 bool AreDropTypesRequired(views::MenuItemView
* menu
);
112 bool CanDrop(views::MenuItemView
* menu
, const ui::OSExchangeData
& data
);
113 int GetDropOperation(views::MenuItemView
* item
,
114 const ui::DropTargetEvent
& event
,
115 views::MenuDelegate::DropPosition
* position
);
116 int OnPerformDrop(views::MenuItemView
* menu
,
117 views::MenuDelegate::DropPosition position
,
118 const ui::DropTargetEvent
& event
);
119 bool ShowContextMenu(views::MenuItemView
* source
,
122 ui::MenuSourceType source_type
);
123 bool CanDrag(views::MenuItemView
* menu
);
124 void WriteDragData(views::MenuItemView
* sender
, ui::OSExchangeData
* data
);
125 int GetDragOperations(views::MenuItemView
* sender
);
126 int GetMaxWidthForMenu(views::MenuItemView
* menu
);
127 void WillShowMenu(views::MenuItemView
* menu
);
129 // BookmarkModelObserver methods.
130 void BookmarkModelChanged() override
;
131 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel
* model
,
132 const bookmarks::BookmarkNode
* node
) override
;
134 // BookmarkContextMenu::Observer methods.
135 void WillRemoveBookmarks(
136 const std::vector
<const bookmarks::BookmarkNode
*>& bookmarks
) override
;
137 void DidRemoveBookmarks() override
;
140 friend class BookmarkMenuDelegateTest
;
142 typedef std::map
<int, const bookmarks::BookmarkNode
*> MenuIDToNodeMap
;
143 typedef std::map
<const bookmarks::BookmarkNode
*, views::MenuItemView
*>
146 // Creates a menu. This uses BuildMenu() to recursively populate the menu.
147 views::MenuItemView
* CreateMenu(const bookmarks::BookmarkNode
* parent
,
148 int start_child_index
,
149 ShowOptions show_options
);
151 // Invokes BuildMenuForPermanentNode() for the permanent nodes (excluding
152 // 'other bookmarks' folder).
153 void BuildMenusForPermanentNodes(views::MenuItemView
* menu
);
155 // If |node| has children a new menu is created and added to |menu| to
156 // represent it. If |node| is not empty and |added_separator| is false, a
157 // separator is added before the new menu items and |added_separator| is set
159 void BuildMenuForPermanentNode(const bookmarks::BookmarkNode
* node
,
160 const gfx::ImageSkia
& icon
,
161 views::MenuItemView
* menu
,
162 bool* added_separator
);
164 void BuildMenuForManagedNode(views::MenuItemView
* menu
);
165 void BuildMenuForSupervisedNode(views::MenuItemView
* menu
);
167 // Creates an entry in menu for each child node of |parent| starting at
168 // |start_child_index|.
169 void BuildMenu(const bookmarks::BookmarkNode
* parent
,
170 int start_child_index
,
171 views::MenuItemView
* menu
);
173 // Registers the necessary mappings for |menu| and |node|.
174 void AddMenuToMaps(views::MenuItemView
* menu
,
175 const bookmarks::BookmarkNode
* node
);
180 content::PageNavigator
* page_navigator_
;
183 views::Widget
* parent_
;
185 // Maps from menu id to BookmarkNode.
186 MenuIDToNodeMap menu_id_to_node_map_
;
189 views::MenuItemView
* menu_
;
191 // Data for the drop.
192 bookmarks::BookmarkNodeData drop_data_
;
194 // Used when a context menu is shown.
195 scoped_ptr
<BookmarkContextMenu
> context_menu_
;
197 // If non-NULL this is the |parent| passed to Init and is NOT owned by us.
198 views::MenuItemView
* parent_menu_item_
;
200 // Maps from node to menu.
201 NodeToMenuMap node_to_menu_map_
;
203 // ID of the next menu item.
206 views::MenuDelegate
* real_delegate_
;
208 // Is the model being changed?
209 bool is_mutating_model_
;
211 // The location where this bookmark menu will be displayed (for UMA).
212 BookmarkLaunchLocation location_
;
214 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegate
);
217 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_