bookmark_menu_delegate.h revision 3551c9c881056c480085172ff9840cab31610854
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.
4
5#ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_
6#define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_
7
8#include <map>
9#include <set>
10
11#include "base/compiler_specific.h"
12#include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
13#include "chrome/browser/bookmarks/bookmark_node_data.h"
14#include "chrome/browser/bookmarks/bookmark_utils.h"
15#include "chrome/browser/ui/views/bookmarks/bookmark_context_menu.h"
16#include "ui/views/controls/menu/menu_delegate.h"
17
18class BookmarkNode;
19class Browser;
20class Profile;
21
22namespace content {
23class PageNavigator;
24}
25
26namespace ui {
27class OSExchangeData;
28}
29
30namespace views {
31class MenuItemView;
32class Widget;
33}
34
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.
40class BookmarkMenuDelegate : public BaseBookmarkModelObserver,
41                             public BookmarkContextMenuObserver {
42 public:
43  enum ShowOptions {
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,
48
49    // Don't show any additional folders.
50    HIDE_PERMANENT_FOLDERS
51  };
52
53  BookmarkMenuDelegate(Browser* browser,
54                       content::PageNavigator* navigator,
55                       views::Widget* parent,
56                       int first_menu_id);
57  virtual ~BookmarkMenuDelegate();
58
59  // Creates the menus from the model.
60  void Init(views::MenuDelegate* real_delegate,
61            views::MenuItemView* parent,
62            const BookmarkNode* node,
63            int start_child_index,
64            ShowOptions show_options,
65            bookmark_utils::BookmarkLaunchLocation location);
66
67  // Sets the PageNavigator.
68  void SetPageNavigator(content::PageNavigator* navigator);
69
70  // Returns the id given to the next menu.
71  int next_menu_id() const { return next_menu_id_; }
72
73  // Makes the menu for |node| the active menu. |start_index| is the index of
74  // the first child of |node| to show in the menu.
75  void SetActiveMenu(const BookmarkNode* node, int start_index);
76
77  BookmarkModel* GetBookmarkModel();
78
79  // Returns the menu.
80  views::MenuItemView* menu() { return menu_; }
81
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;
85  }
86
87  views::Widget* parent() { return parent_; }
88  const views::Widget* parent() const { return parent_; }
89
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_; }
93
94  // MenuDelegate like methods (see class description for details).
95  string16 GetTooltipText(int id, const gfx::Point& p) const;
96  bool IsTriggerableEvent(views::MenuItemView* menu,
97                          const ui::Event& e);
98  void ExecuteCommand(int id, int mouse_event_flags);
99  bool ShouldExecuteCommandWithoutClosingMenu(int id, const ui::Event& e);
100  bool GetDropFormats(
101      views::MenuItemView* menu,
102      int* formats,
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,
113                       int id,
114                       const gfx::Point& p,
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
121  // BookmarkModelObserver methods.
122  virtual void BookmarkModelChanged() OVERRIDE;
123  virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
124                                          const BookmarkNode* node) OVERRIDE;
125
126  // BookmarkContextMenu::Observer methods.
127  virtual void WillRemoveBookmarks(
128      const std::vector<const BookmarkNode*>& bookmarks) OVERRIDE;
129  virtual void DidRemoveBookmarks() OVERRIDE;
130
131 private:
132  typedef std::map<int, const BookmarkNode*> MenuIDToNodeMap;
133  typedef std::map<const BookmarkNode*, views::MenuItemView*> NodeToMenuMap;
134
135  // Creates a menu. This uses BuildMenu() to recursively populate the menu.
136  views::MenuItemView* CreateMenu(const BookmarkNode* parent,
137                                  int start_child_index,
138                                  ShowOptions show_options);
139
140  // Invokes BuildMenuForPermanentNode() for the permanent nodes (excluding
141  // 'other bookmarks' folder).
142  void BuildMenusForPermanentNodes(views::MenuItemView* menu,
143                                   int* next_menu_id);
144
145  // If |node| has children a new menu is created and added to |menu| to
146  // represent it. If |node| is not empty and |added_separator| is false, a
147  // separator is added before the new menu items and |added_separator| is set
148  // to true.
149  void BuildMenuForPermanentNode(const BookmarkNode* node,
150                                 views::MenuItemView* menu,
151                                 int* next_menu_id,
152                                 bool* added_separator);
153
154  // Creates an entry in menu for each child node of |parent| starting at
155  // |start_child_index|.
156  void BuildMenu(const BookmarkNode* parent,
157                 int start_child_index,
158                 views::MenuItemView* menu,
159                 int* next_menu_id);
160
161  Browser* browser_;
162  Profile* profile_;
163
164  content::PageNavigator* page_navigator_;
165
166  // Parent of menus.
167  views::Widget* parent_;
168
169  // Maps from menu id to BookmarkNode.
170  MenuIDToNodeMap menu_id_to_node_map_;
171
172  // Current menu.
173  views::MenuItemView* menu_;
174
175  // Data for the drop.
176  BookmarkNodeData drop_data_;
177
178  // Used when a context menu is shown.
179  scoped_ptr<BookmarkContextMenu> context_menu_;
180
181  // Is the menu being shown for a drop?
182  bool for_drop_;
183
184  // If non-NULL this is the |parent| passed to Init and is NOT owned by us.
185  views::MenuItemView* parent_menu_item_;
186
187  // Maps from node to menu.
188  NodeToMenuMap node_to_menu_map_;
189
190  // ID of the next menu item.
191  int next_menu_id_;
192
193  views::MenuDelegate* real_delegate_;
194
195  // Is the model being changed?
196  bool is_mutating_model_;
197
198  // The location where this bookmark menu will be displayed (for UMA).
199  bookmark_utils::BookmarkLaunchLocation location_;
200
201  DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegate);
202};
203
204#endif  // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_
205