OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_UI_GTK_BOOKMARK_SUB_MENU_MODEL_GTK_H_ |
| 6 #define CHROME_BROWSER_UI_GTK_BOOKMARK_SUB_MENU_MODEL_GTK_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" |
| 12 #include "ui/base/models/simple_menu_model.h" |
| 13 #include "webkit/glue/window_open_disposition.h" |
| 14 |
| 15 class Browser; |
| 16 class BookmarkModel; |
| 17 class BookmarkNode; |
| 18 class MenuGtk; // See below for why we need this. |
| 19 class PageNavigator; |
| 20 |
| 21 // BookmarkNodeMenuModel builds a SimpleMenuModel on demand when the menu is |
| 22 // shown, and automatically destroys child models when the menu is closed. |
| 23 class BookmarkNodeMenuModel : public ui::SimpleMenuModel { |
| 24 public: |
| 25 BookmarkNodeMenuModel(ui::SimpleMenuModel::Delegate* delegate, |
| 26 BookmarkModel* model, |
| 27 const BookmarkNode* node, |
| 28 PageNavigator* page_navigator); |
| 29 virtual ~BookmarkNodeMenuModel(); |
| 30 |
| 31 // From SimpleMenuModel. Takes care of deleting submenus. |
| 32 // Note that this is not virtual. That's OK for our use. |
| 33 void Clear(); |
| 34 |
| 35 // From MenuModel via SimpleMenuModel. |
| 36 virtual void MenuWillShow() OVERRIDE; |
| 37 virtual void MenuClosed() OVERRIDE; |
| 38 virtual void ActivatedAt(int index) OVERRIDE; |
| 39 virtual void ActivatedAt(int index, int event_flags) OVERRIDE; |
| 40 |
| 41 protected: |
| 42 // Adds all bookmark items to the model. Does not clear the model first. |
| 43 void PopulateMenu(); |
| 44 |
| 45 BookmarkModel* model() const { return model_; } |
| 46 void set_model(BookmarkModel* model) { model_ = model; } |
| 47 |
| 48 const BookmarkNode* node() const { return node_; } |
| 49 void set_node(const BookmarkNode* node) { node_ = node; } |
| 50 |
| 51 private: |
| 52 // Uses the page navigator to open the bookmark at the given index. |
| 53 void NavigateToMenuItem(int index, WindowOpenDisposition disposition); |
| 54 |
| 55 // The bookmark model whose bookmarks we will show. Note that in the top-level |
| 56 // bookmark menu, this may be null. (It is set only when the menu is shown.) |
| 57 BookmarkModel* model_; |
| 58 |
| 59 // The bookmark node for the folder that this model will show. Note that in |
| 60 // the top-level bookmark menu, this may be null, as above. |
| 61 const BookmarkNode* node_; |
| 62 |
| 63 // The page navigator used to open bookmarks in ActivatedAt(). |
| 64 PageNavigator* page_navigator_; |
| 65 |
| 66 // A list of the submenus we own and will need to delete. |
| 67 std::vector<BookmarkNodeMenuModel*> submenus_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(BookmarkNodeMenuModel); |
| 70 }; |
| 71 |
| 72 // This is the top-level bookmark menu model. It handles prepending a few fixed |
| 73 // items before the bookmarks. Child menus are all plain BookmarkNodeMenuModels. |
| 74 // This class also handles watching the bookmark model and forcing the menu to |
| 75 // close if it changes while the menu is open. |
| 76 class BookmarkSubMenuModel : public BookmarkNodeMenuModel, |
| 77 public BaseBookmarkModelObserver { |
| 78 public: |
| 79 BookmarkSubMenuModel(ui::SimpleMenuModel::Delegate* delegate, |
| 80 Browser* browser); |
| 81 |
| 82 virtual ~BookmarkSubMenuModel(); |
| 83 |
| 84 // See below; this is used to allow closing the menu when bookmarks change. |
| 85 void SetMenuGtk(MenuGtk* menu) { menu_ = menu; } |
| 86 |
| 87 // From BaseBookmarkModelObserver. |
| 88 virtual void BookmarkModelChanged() OVERRIDE; |
| 89 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; |
| 90 |
| 91 // From MenuModel via BookmarkNodeMenuModel, SimpleMenuModel. |
| 92 virtual void MenuWillShow() OVERRIDE; |
| 93 virtual void ActivatedAt(int index) OVERRIDE; |
| 94 virtual void ActivatedAt(int index, int event_flags) OVERRIDE; |
| 95 virtual bool IsEnabledAt(int index) const OVERRIDE; |
| 96 virtual bool IsVisibleAt(int index) const OVERRIDE; |
| 97 |
| 98 private: |
| 99 Browser* browser_; |
| 100 |
| 101 // The number of fixed items shown before the bookmarks. |
| 102 int fixed_items_; |
| 103 |
| 104 // We need to be able to call Cancel() on the menu when bookmarks change. This |
| 105 // is a bit of an abstraction violation but it could be converted to an |
| 106 // interface with just a Cancel() method if necessary. |
| 107 MenuGtk* menu_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(BookmarkSubMenuModel); |
| 110 }; |
| 111 |
| 112 #endif // CHROME_BROWSER_UI_GTK_BOOKMARK_SUB_MENU_MODEL_GTK_H_ |
OLD | NEW |