- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_menu_bridge.h
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 // C++ controller for the bookmark menu; one per AppController (which
6 // means there is only one).  When bookmarks are changed, this class
7 // takes care of updating Cocoa bookmark menus.  This is not named
8 // BookmarkMenuController to help avoid confusion between languages.
9 // This class needs to be C++, not ObjC, since it derives from
10 // BookmarkModelObserver.
11 //
12 // Most Chromium Cocoa menu items are static from a nib (e.g. New
13 // Tab), but may be enabled/disabled under certain circumstances
14 // (e.g. Cut and Paste).  In addition, most Cocoa menu items have
15 // firstResponder: as a target.  Unusually, bookmark menu items are
16 // created dynamically.  They also have a target of
17 // BookmarkMenuCocoaController instead of firstResponder.
18 // See BookmarkMenuBridge::AddNodeToMenu()).
19
20 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_
21 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_
22
23 #include <map>
24
25 #include "base/mac/scoped_nsobject.h"
26 #include "chrome/browser/bookmarks/bookmark_model_observer.h"
27 #import "chrome/browser/ui/cocoa/main_menu_item.h"
28
29 class BookmarkNode;
30 class Profile;
31 @class NSImage;
32 @class NSMenu;
33 @class NSMenuItem;
34 @class BookmarkMenuCocoaController;
35
36 class BookmarkMenuBridge : public BookmarkModelObserver,
37                            public MainMenuItem {
38  public:
39   BookmarkMenuBridge(Profile* profile, NSMenu* menu);
40   virtual ~BookmarkMenuBridge();
41
42   // BookmarkModelObserver:
43   virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE;
44   virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
45   virtual void BookmarkNodeMoved(BookmarkModel* model,
46                                  const BookmarkNode* old_parent,
47                                  int old_index,
48                                  const BookmarkNode* new_parent,
49                                  int new_index) OVERRIDE;
50   virtual void BookmarkNodeAdded(BookmarkModel* model,
51                                  const BookmarkNode* parent,
52                                  int index) OVERRIDE;
53   virtual void BookmarkNodeRemoved(BookmarkModel* model,
54                                    const BookmarkNode* parent,
55                                    int old_index,
56                                    const BookmarkNode* node) OVERRIDE;
57   virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE;
58   virtual void BookmarkNodeChanged(BookmarkModel* model,
59                                    const BookmarkNode* node) OVERRIDE;
60   virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
61                                           const BookmarkNode* node) OVERRIDE;
62   virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
63                                              const BookmarkNode* node) OVERRIDE;
64
65   // MainMenuItem:
66   virtual void ResetMenu() OVERRIDE;
67   virtual void BuildMenu() OVERRIDE;
68
69   // Rebuilds the main bookmark menu, if it has been marked invalid.
70   void UpdateMenu(NSMenu* bookmark_menu);
71
72   // Rebuilds a bookmark menu that's a submenu of another menu.
73   void UpdateSubMenu(NSMenu* bookmark_menu);
74
75   // I wish I had a "friend @class" construct.
76   BookmarkModel* GetBookmarkModel();
77   Profile* GetProfile();
78
79  protected:
80   // Rebuilds the bookmark content of supplied menu.
81   void UpdateMenuInternal(NSMenu* bookmark_menu, bool is_submenu);
82
83   // Clear all bookmarks from the given bookmark menu.
84   void ClearBookmarkMenu(NSMenu* menu);
85
86   // Mark the bookmark menu as being invalid.
87   void InvalidateMenu()  { menuIsValid_ = false; }
88
89   // Helper for adding the node as a submenu to the menu with the
90   // given title.
91   // If |add_extra_items| is true, also adds extra menu items at bottom of
92   // menu, such as "Open All Bookmarks".
93   void AddNodeAsSubmenu(NSMenu* menu,
94                         const BookmarkNode* node,
95                         bool add_extra_items);
96
97   // Helper for recursively adding items to our bookmark menu.
98   // All children of |node| will be added to |menu|.
99   // If |add_extra_items| is true, also adds extra menu items at bottom of
100   // menu, such as "Open All Bookmarks".
101   // TODO(jrg): add a counter to enforce maximum nodes added
102   void AddNodeToMenu(const BookmarkNode* node, NSMenu* menu,
103                      bool add_extra_items);
104
105   // Helper for adding an item to our bookmark menu. An item which has a
106   // localized title specified by |message_id| will be added to |menu|.
107   // The item is also bound to |node| by tag. |command_id| selects the action.
108   void AddItemToMenu(int command_id,
109                      int message_id,
110                      const BookmarkNode* node,
111                      NSMenu* menu,
112                      bool enabled);
113
114   // This configures an NSMenuItem with all the data from a BookmarkNode. This
115   // is used to update existing menu items, as well as to configure newly
116   // created ones, like in AddNodeToMenu().
117   // |set_title| is optional since it is only needed when we get a
118   // node changed notification.  On initial build of the menu we set
119   // the title as part of alloc/init.
120   void ConfigureMenuItem(const BookmarkNode* node, NSMenuItem* item,
121                          bool set_title);
122
123   // Returns the NSMenuItem for a given BookmarkNode.
124   NSMenuItem* MenuItemForNode(const BookmarkNode* node);
125
126   // Return the Bookmark menu.
127   virtual NSMenu* BookmarkMenu();
128
129   // Start watching the bookmarks for changes.
130   void ObserveBookmarkModel();
131
132  private:
133   friend class BookmarkMenuBridgeTest;
134
135   // True iff the menu is up-to-date with the actual BookmarkModel.
136   bool menuIsValid_;
137
138   Profile* profile_;  // weak
139   BookmarkMenuCocoaController* controller_;  // strong
140
141   // The folder image so we can use one copy for all.
142   base::scoped_nsobject<NSImage> folder_image_;
143
144   // In order to appropriately update items in the bookmark menu, without
145   // forcing a rebuild, map the model's nodes to menu items.
146   std::map<const BookmarkNode*, NSMenuItem*> bookmark_nodes_;
147 };
148
149 #endif  // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_