157e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le// Copyright (c) 2012 The Chromium Authors. All rights reserved.
21194ec356a16f3c6dcf408289e36e42c149d6dc8Kevin Jin// Use of this source code is governed by a BSD-style license that can be
357e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le// found in the LICENSE file.
457e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le
557e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_
657e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_
757e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le
857e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#include <map>
957e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#include <set>
1057e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le
1157e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#include "base/compiler_specific.h"
1257e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#include "chrome/browser/bookmarks/bookmark_stats.h"
1357e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#include "chrome/browser/ui/views/bookmarks/bookmark_context_menu.h"
1457e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#include "components/bookmarks/browser/base_bookmark_model_observer.h"
1557e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#include "components/bookmarks/browser/bookmark_node_data.h"
1657e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le#include "ui/views/controls/menu/menu_delegate.h"
171194ec356a16f3c6dcf408289e36e42c149d6dc8Kevin Jin
1857e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Leclass BookmarkNode;
1957e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Leclass Browser;
2057e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Leclass ChromeBookmarkClient;
21cce3dd4492cf04a89154e36fda1909ab4e35213aThanh Leclass Profile;
2257e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le
2357e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Lenamespace content {
24f50519233078e65a056cff49d7b4989d57c3e750Kevin Jinclass PageNavigator;
25f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin}
26f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin
2757e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Lenamespace ui {
2857e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Leclass OSExchangeData;
2957e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le}
3057e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le
31544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Lenamespace views {
32544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Leclass MenuItemView;
33544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Leclass Widget;
3457e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le}
3531692a4134ef82a4144d25980c0c5f14bbf1bfadThanh Le
3631692a4134ef82a4144d25980c0c5f14bbf1bfadThanh Le// BookmarkMenuDelegate acts as the (informal) views::MenuDelegate for showing
3757e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le// bookmarks in a MenuItemView. BookmarkMenuDelegate informally implements
3857e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le// MenuDelegate as its assumed another class is going to forward the appropriate
3957e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le// methods to this class. Doing so allows this class to be used for both menus
4057e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le// on the bookmark bar and the bookmarks in the wrench menu.
41544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Leclass BookmarkMenuDelegate : public BaseBookmarkModelObserver,
42544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le                             public BookmarkContextMenuObserver {
43544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le public:
44544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le  enum ShowOptions {
45544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le    // Indicates a menu should be added containing the permanent folders (other
46544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le    // than then bookmark bar folder). This only makes sense when showing the
47544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le    // contents of the bookmark bar folder.
48544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le    SHOW_PERMANENT_FOLDERS,
49544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le
50544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le    // Don't show any additional folders.
51544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le    HIDE_PERMANENT_FOLDERS
52544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le  };
53544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le
54544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le  BookmarkMenuDelegate(Browser* browser,
55544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le                       content::PageNavigator* navigator,
56544e68a6af3ef73316b9994bb7a63790c19415b9Thanh Le                       views::Widget* parent,
5757e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le                       int first_menu_id,
5857e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le                       int max_menu_id);
5957e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le  virtual ~BookmarkMenuDelegate();
6057e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le
61cce3dd4492cf04a89154e36fda1909ab4e35213aThanh Le  // Creates the menus from the model.
62cce3dd4492cf04a89154e36fda1909ab4e35213aThanh Le  void Init(views::MenuDelegate* real_delegate,
63cce3dd4492cf04a89154e36fda1909ab4e35213aThanh Le            views::MenuItemView* parent,
64cce3dd4492cf04a89154e36fda1909ab4e35213aThanh Le            const BookmarkNode* node,
65cce3dd4492cf04a89154e36fda1909ab4e35213aThanh Le            int start_child_index,
66cce3dd4492cf04a89154e36fda1909ab4e35213aThanh Le            ShowOptions show_options,
67f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin            BookmarkLaunchLocation location);
68f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
69f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  // Sets the PageNavigator.
70f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  void SetPageNavigator(content::PageNavigator* navigator);
71f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
72f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  // Returns the id given to the next menu.
73f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  int next_menu_id() const { return next_menu_id_; }
74c1a957dbec671deb711d2f69cf326dc97f339f66Kevin Jin
75f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  // Makes the menu for |node| the active menu. |start_index| is the index of
76f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  // the first child of |node| to show in the menu.
77f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  void SetActiveMenu(const BookmarkNode* node, int start_index);
78f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
79f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  BookmarkModel* GetBookmarkModel();
80f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  ChromeBookmarkClient* GetChromeBookmarkClient();
81f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
82f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  // Returns the menu.
83f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  views::MenuItemView* menu() { return menu_; }
84f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
85f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  // Returns the context menu, or NULL if the context menu isn't showing.
86f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  views::MenuItemView* context_menu() {
87f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin    return context_menu_.get() ? context_menu_->menu() : NULL;
88f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  }
89f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
90f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  views::Widget* parent() { return parent_; }
91f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  const views::Widget* parent() const { return parent_; }
92f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
93f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  // Returns true if we're in the process of mutating the model. This happens
94f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  // when the user deletes menu items using the context menu.
95f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin  bool is_mutating_model() const { return is_mutating_model_; }
96f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
97f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  // MenuDelegate like methods (see class description for details).
98f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  base::string16 GetTooltipText(int id, const gfx::Point& p) const;
9957e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le  bool IsTriggerableEvent(views::MenuItemView* menu,
10057e46577852ffa1dde4662f6018f7fbcfacb6148Thanh Le                          const ui::Event& e);
101  void ExecuteCommand(int id, int mouse_event_flags);
102  bool ShouldExecuteCommandWithoutClosingMenu(int id, const ui::Event& e);
103  bool GetDropFormats(
104      views::MenuItemView* menu,
105      int* formats,
106      std::set<ui::OSExchangeData::CustomFormat>* custom_formats);
107  bool AreDropTypesRequired(views::MenuItemView* menu);
108  bool CanDrop(views::MenuItemView* menu, const ui::OSExchangeData& data);
109  int GetDropOperation(views::MenuItemView* item,
110                       const ui::DropTargetEvent& event,
111                       views::MenuDelegate::DropPosition* position);
112  int OnPerformDrop(views::MenuItemView* menu,
113                    views::MenuDelegate::DropPosition position,
114                    const ui::DropTargetEvent& event);
115  bool ShowContextMenu(views::MenuItemView* source,
116                       int id,
117                       const gfx::Point& p,
118                       ui::MenuSourceType source_type);
119  bool CanDrag(views::MenuItemView* menu);
120  void WriteDragData(views::MenuItemView* sender, ui::OSExchangeData* data);
121  int GetDragOperations(views::MenuItemView* sender);
122  int GetMaxWidthForMenu(views::MenuItemView* menu);
123
124  // BookmarkModelObserver methods.
125  virtual void BookmarkModelChanged() OVERRIDE;
126  virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
127                                          const BookmarkNode* node) OVERRIDE;
128
129  // BookmarkContextMenu::Observer methods.
130  virtual void WillRemoveBookmarks(
131      const std::vector<const BookmarkNode*>& bookmarks) OVERRIDE;
132  virtual void DidRemoveBookmarks() OVERRIDE;
133
134 private:
135  typedef std::map<int, const BookmarkNode*> MenuIDToNodeMap;
136  typedef std::map<const BookmarkNode*, views::MenuItemView*> NodeToMenuMap;
137
138  // Creates a menu. This uses BuildMenu() to recursively populate the menu.
139  views::MenuItemView* CreateMenu(const BookmarkNode* parent,
140                                  int start_child_index,
141                                  ShowOptions show_options);
142
143  // Invokes BuildMenuForPermanentNode() for the permanent nodes (excluding
144  // 'other bookmarks' folder).
145  void BuildMenusForPermanentNodes(views::MenuItemView* menu,
146                                   int* next_menu_id);
147
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
151  // to true.
152  void BuildMenuForPermanentNode(const BookmarkNode* node,
153                                 int icon_resource_id,
154                                 views::MenuItemView* menu,
155                                 int* next_menu_id,
156                                 bool* added_separator);
157
158  void BuildMenuForManagedNode(views::MenuItemView* menu,
159                               int* next_menu_id);
160
161  // Creates an entry in menu for each child node of |parent| starting at
162  // |start_child_index|.
163  void BuildMenu(const BookmarkNode* parent,
164                 int start_child_index,
165                 views::MenuItemView* menu,
166                 int* next_menu_id);
167
168  // Returns true if |menu_id_| is outside the range of minimum and maximum menu
169  // ID's allowed.
170  bool IsOutsideMenuIdRange(int menu_id) const;
171
172  Browser* browser_;
173  Profile* profile_;
174
175  content::PageNavigator* page_navigator_;
176
177  // Parent of menus.
178  views::Widget* parent_;
179
180  // Maps from menu id to BookmarkNode.
181  MenuIDToNodeMap menu_id_to_node_map_;
182
183  // Current menu.
184  views::MenuItemView* menu_;
185
186  // Data for the drop.
187  bookmarks::BookmarkNodeData drop_data_;
188
189  // Used when a context menu is shown.
190  scoped_ptr<BookmarkContextMenu> context_menu_;
191
192  // If non-NULL this is the |parent| passed to Init and is NOT owned by us.
193  views::MenuItemView* parent_menu_item_;
194
195  // Maps from node to menu.
196  NodeToMenuMap node_to_menu_map_;
197
198  // ID of the next menu item.
199  int next_menu_id_;
200
201  // Minimum and maximum ID's to use for menu items.
202  const int min_menu_id_;
203  const int max_menu_id_;
204
205  views::MenuDelegate* real_delegate_;
206
207  // Is the model being changed?
208  bool is_mutating_model_;
209
210  // The location where this bookmark menu will be displayed (for UMA).
211  BookmarkLaunchLocation location_;
212
213  DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegate);
214};
215
216#endif  // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_
217