| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_CONTROLLER_VIEWS_H_ | 5 #ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_ |
| 6 #define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_CONTROLLER_VIEWS_H_ | 6 #define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 | 11 |
| 12 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" | 12 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_node_data.h" | 13 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
| 14 #include "chrome/browser/ui/views/bookmarks/bookmark_context_menu.h" | 14 #include "chrome/browser/ui/views/bookmarks/bookmark_context_menu.h" |
| 15 #include "ui/gfx/native_widget_types.h" | 15 #include "ui/gfx/native_widget_types.h" |
| 16 #include "views/controls/menu/menu_delegate.h" | 16 #include "views/controls/menu/menu_delegate.h" |
| 17 #include "views/controls/menu/menu_item_view.h" | |
| 18 | 17 |
| 19 class BookmarkBarView; | |
| 20 class BookmarkNode; | 18 class BookmarkNode; |
| 21 class Browser; | |
| 22 class PageNavigator; | 19 class PageNavigator; |
| 23 class Profile; | 20 class Profile; |
| 24 | 21 |
| 25 namespace gfx { | |
| 26 class Rect; | |
| 27 } // namespace gfx | |
| 28 | |
| 29 namespace ui { | 22 namespace ui { |
| 30 class OSExchangeData; | 23 class OSExchangeData; |
| 31 } // namespace ui | 24 } |
| 32 | 25 |
| 33 namespace views { | 26 namespace views { |
| 34 class MenuButton; | 27 class MenuItemView; |
| 35 } // namespace views | 28 } |
| 36 | 29 |
| 37 // BookmarkMenuController is responsible for showing a menu of bookmarks, | 30 // BookmarkMenuDelegate acts as the (informal) views::MenuDelegate for showing |
| 38 // each item in the menu represents a bookmark. | 31 // bookmarks in a MenuItemView. BookmarkMenuDelegate informally implements |
| 39 // BookmarkMenuController deletes itself as necessary, although the menu can | 32 // MenuDelegate as its assumed another class is going to forward the appropriate |
| 40 // be explicitly hidden by way of the Cancel method. | 33 // methods to this class. Doing so allows this class to be used for both menus |
| 41 class BookmarkMenuController : public BaseBookmarkModelObserver, | 34 // on the bookmark bar and the bookmarks in the wrench menu. |
| 42 public views::MenuDelegate, | 35 class BookmarkMenuDelegate : public BaseBookmarkModelObserver, |
| 43 public BookmarkContextMenuObserver { | 36 public BookmarkContextMenuObserver { |
| 44 public: | 37 public: |
| 45 // The observer is notified prior to the menu being deleted. | 38 enum ShowOptions { |
| 46 class Observer { | 39 // Indicates a menu should be added containing the 'other' bookmarks folder |
| 47 public: | 40 // and all its contents. This only makes sense when showing the contents |
| 48 virtual void BookmarkMenuDeleted(BookmarkMenuController* controller) = 0; | 41 // of the bookmark folder. |
| 42 SHOW_OTHER_FOLDER, |
| 49 | 43 |
| 50 protected: | 44 // Don't show the 'other' bookmarks folder. |
| 51 virtual ~Observer() {} | 45 HIDE_OTHER_FOLDER |
| 52 }; | 46 }; |
| 53 | 47 |
| 54 // Creates a BookmarkMenuController showing the children of |node| starting | 48 BookmarkMenuDelegate(Profile* profile, |
| 55 // at index |start_child_index|. | 49 PageNavigator* navigator, |
| 56 BookmarkMenuController(Browser* browser, | 50 gfx::NativeWindow parent, |
| 57 Profile* profile, | 51 int first_menu_id); |
| 58 PageNavigator* page_navigator, | 52 virtual ~BookmarkMenuDelegate(); |
| 59 gfx::NativeWindow parent, | |
| 60 const BookmarkNode* node, | |
| 61 int start_child_index); | |
| 62 | 53 |
| 63 void RunMenuAt(BookmarkBarView* bookmark_bar, bool for_drop); | 54 // Creates the menus from the model. |
| 55 void Init(views::MenuDelegate* real_delegate, |
| 56 views::MenuItemView* parent, |
| 57 const BookmarkNode* node, |
| 58 int start_child_index, |
| 59 ShowOptions show_options); |
| 64 | 60 |
| 65 // Shows the menu. | 61 // Returns the id given to the next menu. |
| 66 void RunMenuAt(views::MenuButton* button, | 62 int next_menu_id() const { return next_menu_id_; } |
| 67 views::MenuItemView::AnchorPosition position, | |
| 68 bool for_drop); | |
| 69 | 63 |
| 70 // Hides the menu. | 64 // Makes the menu for |node| the active menu. |start_index| is the index of |
| 71 void Cancel(); | 65 // the first child of |node| to show in the menu. |
| 72 | 66 void SetActiveMenu(const BookmarkNode* node, int start_index); |
| 73 // Returns the node the menu is showing for. | |
| 74 const BookmarkNode* node() const { return node_; } | |
| 75 | 67 |
| 76 // Returns the menu. | 68 // Returns the menu. |
| 77 views::MenuItemView* menu() const { return menu_; } | 69 views::MenuItemView* menu() const { return menu_; } |
| 78 | 70 |
| 79 // Returns the context menu, or NULL if the context menu isn't showing. | 71 // Returns the context menu, or NULL if the context menu isn't showing. |
| 80 views::MenuItemView* context_menu() const { | 72 views::MenuItemView* context_menu() const { |
| 81 return context_menu_.get() ? context_menu_->menu() : NULL; | 73 return context_menu_.get() ? context_menu_->menu() : NULL; |
| 82 } | 74 } |
| 83 | 75 |
| 84 void set_observer(Observer* observer) { observer_ = observer; } | 76 Profile* profile() { return profile_; } |
| 85 | 77 |
| 86 // MenuDelegate methods. | 78 gfx::NativeWindow parent() { return parent_; } |
| 87 virtual std::wstring GetTooltipText(int id, const gfx::Point& p); | 79 |
| 88 virtual bool IsTriggerableEvent(views::MenuItemView* view, | 80 // Returns true if we're in the process of mutating the model. This happens |
| 89 const views::MouseEvent& e); | 81 // when the user deletes menu items using the context menu. |
| 90 virtual void ExecuteCommand(int id, int mouse_event_flags); | 82 bool is_mutating_model() const { return is_mutating_model_; } |
| 91 virtual bool GetDropFormats( | 83 |
| 84 // MenuDelegate like methods (see class description for details). |
| 85 std::wstring GetTooltipText(int id, const gfx::Point& p); |
| 86 bool IsTriggerableEvent(views::MenuItemView* menu, |
| 87 const views::MouseEvent& e); |
| 88 void ExecuteCommand(int id, int mouse_event_flags); |
| 89 bool GetDropFormats( |
| 92 views::MenuItemView* menu, | 90 views::MenuItemView* menu, |
| 93 int* formats, | 91 int* formats, |
| 94 std::set<ui::OSExchangeData::CustomFormat>* custom_formats); | 92 std::set<ui::OSExchangeData::CustomFormat>* custom_formats); |
| 95 virtual bool AreDropTypesRequired(views::MenuItemView* menu); | 93 bool AreDropTypesRequired(views::MenuItemView* menu); |
| 96 virtual bool CanDrop(views::MenuItemView* menu, | 94 bool CanDrop(views::MenuItemView* menu, const ui::OSExchangeData& data); |
| 97 const ui::OSExchangeData& data); | 95 int GetDropOperation(views::MenuItemView* item, |
| 98 virtual int GetDropOperation(views::MenuItemView* item, | 96 const views::DropTargetEvent& event, |
| 99 const views::DropTargetEvent& event, | 97 views::MenuDelegate::DropPosition* position); |
| 100 DropPosition* position); | 98 int OnPerformDrop(views::MenuItemView* menu, |
| 101 virtual int OnPerformDrop(views::MenuItemView* menu, | 99 views::MenuDelegate::DropPosition position, |
| 102 DropPosition position, | 100 const views::DropTargetEvent& event); |
| 103 const views::DropTargetEvent& event); | 101 bool ShowContextMenu(views::MenuItemView* source, |
| 104 virtual bool ShowContextMenu(views::MenuItemView* source, | 102 int id, |
| 105 int id, | 103 const gfx::Point& p, |
| 106 const gfx::Point& p, | 104 bool is_mouse_gesture); |
| 107 bool is_mouse_gesture); | 105 bool CanDrag(views::MenuItemView* menu); |
| 108 virtual void DropMenuClosed(views::MenuItemView* menu); | 106 void WriteDragData(views::MenuItemView* sender, ui::OSExchangeData* data); |
| 109 virtual bool CanDrag(views::MenuItemView* menu); | 107 int GetDragOperations(views::MenuItemView* sender); |
| 110 virtual void WriteDragData(views::MenuItemView* sender, | 108 int GetMaxWidthForMenu(views::MenuItemView* menu); |
| 111 ui::OSExchangeData* data); | |
| 112 virtual int GetDragOperations(views::MenuItemView* sender); | |
| 113 virtual views::MenuItemView* GetSiblingMenu( | |
| 114 views::MenuItemView* menu, | |
| 115 const gfx::Point& screen_point, | |
| 116 views::MenuItemView::AnchorPosition* anchor, | |
| 117 bool* has_mnemonics, | |
| 118 views::MenuButton** button); | |
| 119 virtual int GetMaxWidthForMenu(views::MenuItemView* view); | |
| 120 | 109 |
| 121 // BookmarkModelObserver methods. | 110 // BookmarkModelObserver methods. |
| 122 virtual void BookmarkModelChanged(); | 111 virtual void BookmarkModelChanged(); |
| 123 virtual void BookmarkNodeFaviconLoaded(BookmarkModel* model, | 112 virtual void BookmarkNodeFaviconLoaded(BookmarkModel* model, |
| 124 const BookmarkNode* node); | 113 const BookmarkNode* node); |
| 125 | 114 |
| 126 // BookmarkContextMenu::Observer methods. | 115 // BookmarkContextMenu::Observer methods. |
| 127 virtual void WillRemoveBookmarks( | 116 virtual void WillRemoveBookmarks( |
| 128 const std::vector<const BookmarkNode*>& bookmarks); | 117 const std::vector<const BookmarkNode*>& bookmarks); |
| 129 virtual void DidRemoveBookmarks(); | 118 virtual void DidRemoveBookmarks(); |
| 130 | 119 |
| 131 private: | 120 private: |
| 132 typedef std::map<const BookmarkNode*, int> NodeToMenuIDMap; | 121 typedef std::map<const BookmarkNode*, int> NodeToMenuIDMap; |
| 133 | 122 typedef std::map<const BookmarkNode*, views::MenuItemView*> NodeToMenuMap; |
| 134 // BookmarkMenuController deletes itself as necessary. | |
| 135 virtual ~BookmarkMenuController(); | |
| 136 | 123 |
| 137 // Creates a menu and adds it to node_to_menu_id_map_. This uses | 124 // Creates a menu and adds it to node_to_menu_id_map_. This uses |
| 138 // BuildMenu to recursively populate the menu. | 125 // BuildMenu to recursively populate the menu. |
| 139 views::MenuItemView* CreateMenu(const BookmarkNode* parent, | 126 views::MenuItemView* CreateMenu(const BookmarkNode* parent, |
| 140 int start_child_index); | 127 int start_child_index, |
| 128 ShowOptions show_options); |
| 129 |
| 130 // Builds the menu for the other bookmarks folder. This is added as the last |
| 131 // item to menu_. |
| 132 void BuildOtherFolderMenu(views::MenuItemView* menu, int* next_menu_id); |
| 141 | 133 |
| 142 // Creates an entry in menu for each child node of |parent| starting at | 134 // Creates an entry in menu for each child node of |parent| starting at |
| 143 // |start_child_index|. | 135 // |start_child_index|. |
| 144 void BuildMenu(const BookmarkNode* parent, | 136 void BuildMenu(const BookmarkNode* parent, |
| 145 int start_child_index, | 137 int start_child_index, |
| 146 views::MenuItemView* menu, | 138 views::MenuItemView* menu, |
| 147 int* next_menu_id); | 139 int* next_menu_id); |
| 148 | 140 |
| 149 // Returns the menu whose id is |id|. | 141 // Returns the menu whose id is |id|. |
| 150 views::MenuItemView* GetMenuByID(int id); | 142 views::MenuItemView* GetMenuByID(int id); |
| 151 | 143 |
| 152 // Does the work of processing WillRemoveBookmarks. On exit the set of removed | 144 // Does the work of processing WillRemoveBookmarks. On exit the set of removed |
| 153 // menus is added to |removed_menus|. It's up to the caller to delete the | 145 // menus is added to |removed_menus|. It's up to the caller to delete the |
| 154 // the menus added to |removed_menus|. | 146 // the menus added to |removed_menus|. |
| 155 void WillRemoveBookmarksImpl( | 147 void WillRemoveBookmarksImpl( |
| 156 const std::vector<const BookmarkNode*>& bookmarks, | 148 const std::vector<const BookmarkNode*>& bookmarks, |
| 157 std::set<views::MenuItemView*>* removed_menus); | 149 std::set<views::MenuItemView*>* removed_menus); |
| 158 | 150 |
| 159 Browser* browser_; | |
| 160 | |
| 161 Profile* profile_; | 151 Profile* profile_; |
| 162 | 152 |
| 163 PageNavigator* page_navigator_; | 153 PageNavigator* page_navigator_; |
| 164 | 154 |
| 165 // Parent of menus. | 155 // Parent of menus. |
| 166 gfx::NativeWindow parent_; | 156 gfx::NativeWindow parent_; |
| 167 | 157 |
| 168 // The node we're showing the contents of. | |
| 169 const BookmarkNode* node_; | |
| 170 | |
| 171 // Maps from menu id to BookmarkNode. | 158 // Maps from menu id to BookmarkNode. |
| 172 std::map<int, const BookmarkNode*> menu_id_to_node_map_; | 159 std::map<int, const BookmarkNode*> menu_id_to_node_map_; |
| 173 | 160 |
| 174 // Mapping from node to menu id. This only contains entries for nodes of type | 161 // Mapping from node to menu id. This only contains entries for nodes of type |
| 175 // URL. | 162 // URL. |
| 176 NodeToMenuIDMap node_to_menu_id_map_; | 163 NodeToMenuIDMap node_to_menu_id_map_; |
| 177 | 164 |
| 178 // Current menu. | 165 // Current menu. |
| 179 views::MenuItemView* menu_; | 166 views::MenuItemView* menu_; |
| 180 | 167 |
| 181 // Data for the drop. | 168 // Data for the drop. |
| 182 BookmarkNodeData drop_data_; | 169 BookmarkNodeData drop_data_; |
| 183 | 170 |
| 184 // Used when a context menu is shown. | 171 // Used when a context menu is shown. |
| 185 scoped_ptr<BookmarkContextMenu> context_menu_; | 172 scoped_ptr<BookmarkContextMenu> context_menu_; |
| 186 | 173 |
| 187 // The observer, may be null. | |
| 188 Observer* observer_; | |
| 189 | |
| 190 // Is the menu being shown for a drop? | 174 // Is the menu being shown for a drop? |
| 191 bool for_drop_; | 175 bool for_drop_; |
| 192 | 176 |
| 193 // The bookmark bar. This is only non-null if we're showing a menu item | |
| 194 // for a folder on the bookmark bar and not for drop. | |
| 195 BookmarkBarView* bookmark_bar_; | |
| 196 | |
| 197 typedef std::map<const BookmarkNode*, views::MenuItemView*> NodeToMenuMap; | |
| 198 NodeToMenuMap node_to_menu_map_; | 177 NodeToMenuMap node_to_menu_map_; |
| 199 | 178 |
| 200 // ID of the next menu item. | 179 // ID of the next menu item. |
| 201 int next_menu_id_; | 180 int next_menu_id_; |
| 202 | 181 |
| 203 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuController); | 182 views::MenuDelegate* real_delegate_; |
| 183 |
| 184 // Is the model being changed? |
| 185 bool is_mutating_model_; |
| 186 |
| 187 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegate); |
| 204 }; | 188 }; |
| 205 | 189 |
| 206 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_CONTROLLER_VIEWS_H_ | 190 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_MENU_DELEGATE_H_ |
| OLD | NEW |