Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / bookmarks / bookmark_sub_menu_model_gtk.h
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_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_
7
8 #include <vector>
9
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"
13
14 class Browser;
15 class BookmarkModel;
16 class BookmarkNode;
17 class MenuGtk;  // See below for why we need this.
18 class Profile;
19
20 namespace content {
21 class PageNavigator;
22 }
23
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 {
27  public:
28   BookmarkNodeMenuModel(ui::SimpleMenuModel::Delegate* delegate,
29                         BookmarkModel* model,
30                         const BookmarkNode* node,
31                         content::PageNavigator* page_navigator,
32                         Profile* profile);
33   virtual ~BookmarkNodeMenuModel();
34
35   // From SimpleMenuModel. Takes care of deleting submenus.
36   // Note that this is not virtual. That's OK for our use.
37   void Clear();
38
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;
44
45  protected:
46   // Adds all bookmark items to the model. Does not clear the model first.
47   void PopulateMenu();
48
49   // Add a submenu for the given bookmark folder node.
50   void AddSubMenuForNode(const BookmarkNode* node);
51
52   BookmarkModel* model() const { return model_; }
53   void set_model(BookmarkModel* model) { model_ = model; }
54
55   const BookmarkNode* node() const { return node_; }
56   void set_node(const BookmarkNode* node) { node_ = node; }
57
58  private:
59   // Uses the page navigator to open the bookmark at the given index.
60   void NavigateToMenuItem(int index, WindowOpenDisposition disposition);
61
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_;
65
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_;
69
70   // The page navigator used to open bookmarks in ActivatedAt().
71   content::PageNavigator* page_navigator_;
72
73   Profile* profile_;
74
75   // A list of the submenus we own and will need to delete.
76   std::vector<BookmarkNodeMenuModel*> submenus_;
77
78   DISALLOW_COPY_AND_ASSIGN(BookmarkNodeMenuModel);
79 };
80
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 {
87  public:
88   BookmarkSubMenuModel(ui::SimpleMenuModel::Delegate* delegate,
89                        Browser* browser);
90
91   virtual ~BookmarkSubMenuModel();
92
93   // See below; this is used to allow closing the menu when bookmarks change.
94   void SetMenuGtk(MenuGtk* menu) { menu_ = menu; }
95
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;
101
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;
109
110   // Returns true if the command id is for a bookmark item.
111   static bool IsBookmarkItemCommandId(int command_id);
112
113  private:
114   Browser* browser_;
115
116   // The number of fixed items shown before the bookmarks.
117   int fixed_items_;
118   // The index of the first non-bookmark item after the bookmarks.
119   int bookmark_end_;
120
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.
124   MenuGtk* menu_;
125
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.
129   bool menu_showing_;
130
131   DISALLOW_COPY_AND_ASSIGN(BookmarkSubMenuModel);
132 };
133
134 #endif  // CHROME_BROWSER_UI_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_