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_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_
10 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
11 #include "ui/base/models/simple_menu_model.h"
12 #include "ui/base/window_open_disposition.h"
17 class MenuGtk
; // See below for why we need this.
24 // BookmarkNodeMenuModel builds a SimpleMenuModel on demand when the menu is
25 // shown, and automatically destroys child models when the menu is closed.
26 class BookmarkNodeMenuModel
: public ui::SimpleMenuModel
{
28 BookmarkNodeMenuModel(ui::SimpleMenuModel::Delegate
* delegate
,
30 const BookmarkNode
* node
,
31 content::PageNavigator
* page_navigator
,
33 virtual ~BookmarkNodeMenuModel();
35 // From SimpleMenuModel. Takes care of deleting submenus.
36 // Note that this is not virtual. That's OK for our use.
39 // From MenuModel via SimpleMenuModel.
40 virtual void MenuWillShow() OVERRIDE
;
41 virtual void MenuClosed() OVERRIDE
;
42 virtual void ActivatedAt(int index
) OVERRIDE
;
43 virtual void ActivatedAt(int index
, int event_flags
) OVERRIDE
;
46 // Adds all bookmark items to the model. Does not clear the model first.
49 // Add a submenu for the given bookmark folder node.
50 void AddSubMenuForNode(const BookmarkNode
* node
);
52 BookmarkModel
* model() const { return model_
; }
53 void set_model(BookmarkModel
* model
) { model_
= model
; }
55 const BookmarkNode
* node() const { return node_
; }
56 void set_node(const BookmarkNode
* node
) { node_
= node
; }
59 // Uses the page navigator to open the bookmark at the given index.
60 void NavigateToMenuItem(int index
, WindowOpenDisposition disposition
);
62 // The bookmark model whose bookmarks we will show. Note that in the top-level
63 // bookmark menu, this may be null. (It is set only when the menu is shown.)
64 BookmarkModel
* model_
;
66 // The bookmark node for the folder that this model will show. Note that in
67 // the top-level bookmark menu, this may be null, as above.
68 const BookmarkNode
* node_
;
70 // The page navigator used to open bookmarks in ActivatedAt().
71 content::PageNavigator
* page_navigator_
;
75 // A list of the submenus we own and will need to delete.
76 std::vector
<BookmarkNodeMenuModel
*> submenus_
;
78 DISALLOW_COPY_AND_ASSIGN(BookmarkNodeMenuModel
);
81 // This is the top-level bookmark menu model. It handles prepending a few fixed
82 // items before the bookmarks. Child menus are all plain BookmarkNodeMenuModels.
83 // This class also handles watching the bookmark model and forcing the menu to
84 // close if it changes while the menu is open.
85 class BookmarkSubMenuModel
: public BookmarkNodeMenuModel
,
86 public BaseBookmarkModelObserver
{
88 BookmarkSubMenuModel(ui::SimpleMenuModel::Delegate
* delegate
,
91 virtual ~BookmarkSubMenuModel();
93 // See below; this is used to allow closing the menu when bookmarks change.
94 void SetMenuGtk(MenuGtk
* menu
) { menu_
= menu
; }
96 // BaseBookmarkModelObserver:
97 virtual void BookmarkModelLoaded(BookmarkModel
* model
,
98 bool ids_reassigned
) OVERRIDE
;
99 virtual void BookmarkModelChanged() OVERRIDE
;
100 virtual void BookmarkModelBeingDeleted(BookmarkModel
* model
) OVERRIDE
;
102 // BookmarkNodeMenuModel:
103 virtual void MenuWillShow() OVERRIDE
;
104 virtual void MenuClosed() OVERRIDE
;
105 virtual void ActivatedAt(int index
) OVERRIDE
;
106 virtual void ActivatedAt(int index
, int event_flags
) OVERRIDE
;
107 virtual bool IsEnabledAt(int index
) const OVERRIDE
;
108 virtual bool IsVisibleAt(int index
) const OVERRIDE
;
110 // Returns true if the command id is for a bookmark item.
111 static bool IsBookmarkItemCommandId(int command_id
);
116 // The number of fixed items shown before the bookmarks.
118 // The index of the first non-bookmark item after the bookmarks.
121 // We need to be able to call Cancel() on the wrench menu when bookmarks
122 // change. This is a bit of an abstraction violation but it could be converted
123 // to an interface with just a Cancel() method if necessary.
126 // We keep track of whether the bookmark submenu is currently showing. If it's
127 // not showing, then we don't need to forcibly close the entire wrench menu
128 // when the bookmark model loads.
131 DISALLOW_COPY_AND_ASSIGN(BookmarkSubMenuModel
);
134 #endif // CHROME_BROWSER_UI_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_