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 | 5 // C++ controller for the bookmark menu; one per AppController (which |
6 // C++ class that connects a BookmarkNode (or the entire model) to a Cocoa class | 6 // means there is only one). When bookmarks are changed, this class |
7 // that manages an NSMenu. Commonly this is for the main menu and that instance | 7 // takes care of updating Cocoa bookmark menus. This is not named |
8 // is owned by the AppController. This is also used by the folder menus on the | 8 // BookmarkMenuController to help avoid confusion between languages. |
9 // bookmark bar. | 9 // This class needs to be C++, not ObjC, since it derives from |
| 10 // BookmarkModelObserver. |
10 // | 11 // |
11 // In the main menu case, most Chromium Cocoa menu items are static from a nib | 12 // Most Chromium Cocoa menu items are static from a nib (e.g. New |
12 // (e.g. New Tab), but may be enabled/disabled under certain circumstances | 13 // Tab), but may be enabled/disabled under certain circumstances |
13 // (e.g. Cut and Paste). In addition, most Cocoa menu items have | 14 // (e.g. Cut and Paste). In addition, most Cocoa menu items have |
14 // firstResponder: as a target. Unusually, bookmark menu items are | 15 // firstResponder: as a target. Unusually, bookmark menu items are |
15 // created dynamically. They also have a target of | 16 // created dynamically. They also have a target of |
16 // BookmarkMenuCocoaController instead of firstResponder. | 17 // BookmarkMenuCocoaController instead of firstResponder. |
17 // See BookmarkMenuBridge::AddNodeToMenu()). | 18 // See BookmarkMenuBridge::AddNodeToMenu()). |
18 | 19 |
19 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ | 20 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ |
20 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ | 21 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ |
21 #pragma once | 22 #pragma once |
22 | 23 |
23 #include <map> | 24 #include <map> |
24 | 25 |
25 #include "base/memory/scoped_nsobject.h" | 26 #include "base/memory/scoped_nsobject.h" |
26 #include "chrome/browser/bookmarks/bookmark_model_observer.h" | 27 #include "chrome/browser/bookmarks/bookmark_model_observer.h" |
27 #import "chrome/browser/ui/cocoa/main_menu_item.h" | 28 #import "chrome/browser/ui/cocoa/main_menu_item.h" |
28 | 29 |
29 class BookmarkNode; | 30 class BookmarkNode; |
30 class Profile; | 31 class Profile; |
31 @class NSImage; | 32 @class NSImage; |
32 @class NSMenu; | 33 @class NSMenu; |
33 @class NSMenuItem; | 34 @class NSMenuItem; |
34 @class BookmarkMenuCocoaController; | 35 @class BookmarkMenuCocoaController; |
35 | 36 |
36 class BookmarkMenuBridge : public BookmarkModelObserver, | 37 class BookmarkMenuBridge : public BookmarkModelObserver, |
37 public MainMenuItem { | 38 public MainMenuItem { |
38 public: | 39 public: |
39 // Constructor for the main menu which lists all bookmarks. | |
40 BookmarkMenuBridge(Profile* profile, NSMenu* menu); | 40 BookmarkMenuBridge(Profile* profile, NSMenu* menu); |
41 | |
42 // Constructor for a submenu. | |
43 BookmarkMenuBridge(const BookmarkNode* root_node, | |
44 Profile* profile, | |
45 NSMenu* menu); | |
46 | |
47 virtual ~BookmarkMenuBridge(); | 41 virtual ~BookmarkMenuBridge(); |
48 | 42 |
49 // BookmarkModelObserver: | 43 // BookmarkModelObserver: |
50 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE; | 44 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE; |
51 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; | 45 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; |
52 virtual void BookmarkNodeMoved(BookmarkModel* model, | 46 virtual void BookmarkNodeMoved(BookmarkModel* model, |
53 const BookmarkNode* old_parent, | 47 const BookmarkNode* old_parent, |
54 int old_index, | 48 int old_index, |
55 const BookmarkNode* new_parent, | 49 const BookmarkNode* new_parent, |
56 int new_index) OVERRIDE; | 50 int new_index) OVERRIDE; |
(...skipping 14 matching lines...) Expand all Loading... |
71 // MainMenuItem: | 65 // MainMenuItem: |
72 virtual void ResetMenu() OVERRIDE; | 66 virtual void ResetMenu() OVERRIDE; |
73 virtual void BuildMenu() OVERRIDE; | 67 virtual void BuildMenu() OVERRIDE; |
74 | 68 |
75 // Rebuilds the main bookmark menu, if it has been marked invalid. | 69 // Rebuilds the main bookmark menu, if it has been marked invalid. |
76 void UpdateMenu(NSMenu* bookmark_menu); | 70 void UpdateMenu(NSMenu* bookmark_menu); |
77 | 71 |
78 // Rebuilds a bookmark menu that's a submenu of another menu. | 72 // Rebuilds a bookmark menu that's a submenu of another menu. |
79 void UpdateSubMenu(NSMenu* bookmark_menu); | 73 void UpdateSubMenu(NSMenu* bookmark_menu); |
80 | 74 |
81 // If this bridge is managing a menu for the "Off the Side" chevron button, | |
82 // this sets the index in the menu of the first node to display. | |
83 void set_off_the_side_node_start_index(size_t index) { | |
84 off_the_side_node_start_index_ = index; | |
85 InvalidateMenu(); | |
86 } | |
87 | |
88 // I wish I had a "friend @class" construct. | 75 // I wish I had a "friend @class" construct. |
89 BookmarkModel* GetBookmarkModel(); | 76 BookmarkModel* GetBookmarkModel(); |
90 Profile* GetProfile(); | 77 Profile* GetProfile(); |
91 BookmarkMenuCocoaController* controller() { return controller_.get(); } | |
92 | 78 |
93 protected: | 79 protected: |
94 // Rebuilds the bookmark content of supplied menu. | 80 // Rebuilds the bookmark content of supplied menu. |
95 void UpdateMenuInternal(NSMenu* bookmark_menu, bool is_submenu); | 81 void UpdateMenuInternal(NSMenu* bookmark_menu, bool is_submenu); |
96 | 82 |
97 // Clear all bookmarks from the given bookmark menu. | 83 // Clear all bookmarks from the given bookmark menu. |
98 void ClearBookmarkMenu(NSMenu* menu); | 84 void ClearBookmarkMenu(NSMenu* menu); |
99 | 85 |
100 // Mark the bookmark menu as being invalid. | 86 // Mark the bookmark menu as being invalid. |
101 void InvalidateMenu() { menu_is_valid_ = false; } | 87 void InvalidateMenu() { menuIsValid_ = false; } |
102 | 88 |
103 // Helper for adding the node as a submenu to the menu with the | 89 // Helper for adding the node as a submenu to the menu with the |
104 // given title. | 90 // given title. |
105 // If |add_extra_items| is true, also adds extra menu items at bottom of | 91 // If |add_extra_items| is true, also adds extra menu items at bottom of |
106 // menu, such as "Open All Bookmarks". | 92 // menu, such as "Open All Bookmarks". |
107 void AddNodeAsSubmenu(NSMenu* menu, | 93 void AddNodeAsSubmenu(NSMenu* menu, |
108 const BookmarkNode* node, | 94 const BookmarkNode* node, |
109 NSString* title, | 95 NSString* title, |
110 bool add_extra_items); | 96 bool add_extra_items); |
111 | 97 |
112 // Helper for recursively adding items to our bookmark menu. | 98 // Helper for recursively adding items to our bookmark menu. |
113 // All children of |node| will be added to |menu|. | 99 // All children of |node| will be added to |menu|. |
114 // If |add_extra_items| is true, also adds extra menu items at bottom of | 100 // If |add_extra_items| is true, also adds extra menu items at bottom of |
115 // menu, such as "Open All Bookmarks". | 101 // menu, such as "Open All Bookmarks". |
116 // TODO(jrg): add a counter to enforce maximum nodes added | 102 // TODO(jrg): add a counter to enforce maximum nodes added |
117 void AddNodeToMenu(const BookmarkNode* node, | 103 void AddNodeToMenu(const BookmarkNode* node, NSMenu* menu, |
118 NSMenu* menu, | |
119 bool add_extra_items); | 104 bool add_extra_items); |
120 | 105 |
121 // Helper for adding an item to our bookmark menu. An item which has a | 106 // Helper for adding an item to our bookmark menu. An item which has a |
122 // localized title specified by |message_id| will be added to |menu|. | 107 // localized title specified by |message_id| will be added to |menu|. |
123 // The item is also bound to |node| by tag. |command_id| selects the action. | 108 // The item is also bound to |node| by tag. |command_id| selects the action. |
124 void AddItemToMenu(int command_id, | 109 void AddItemToMenu(int command_id, |
125 int message_id, | 110 int message_id, |
126 const BookmarkNode* node, | 111 const BookmarkNode* node, |
127 NSMenu* menu, | 112 NSMenu* menu, |
128 bool enabled); | 113 bool enabled); |
(...skipping 12 matching lines...) Expand all Loading... |
141 | 126 |
142 // Return the Bookmark menu. | 127 // Return the Bookmark menu. |
143 virtual NSMenu* BookmarkMenu(); | 128 virtual NSMenu* BookmarkMenu(); |
144 | 129 |
145 // Start watching the bookmarks for changes. | 130 // Start watching the bookmarks for changes. |
146 void ObserveBookmarkModel(); | 131 void ObserveBookmarkModel(); |
147 | 132 |
148 private: | 133 private: |
149 friend class BookmarkMenuBridgeTest; | 134 friend class BookmarkMenuBridgeTest; |
150 | 135 |
151 // Performs the actual work for AddNodeToMenu(), keeping count of the | 136 // True iff the menu is up-to-date with the actual BookmarkModel. |
152 // recursion depth. | 137 bool menuIsValid_; |
153 void AddNodeToMenuRecursive(const BookmarkNode* node, | |
154 NSMenu* menu, | |
155 bool add_extra_items, | |
156 int recursion_depth); | |
157 | 138 |
158 // True iff the menu is up-to-date with the actual BookmarkModel. | 139 Profile* profile_; // weak |
159 bool menu_is_valid_; | 140 BookmarkMenuCocoaController* controller_; // strong |
160 | |
161 // The root node of the menu. | |
162 const BookmarkNode* root_node_; | |
163 | |
164 // Index from which to start adding children from the model. | |
165 size_t off_the_side_node_start_index_; | |
166 | |
167 Profile* profile_; // Weak. | |
168 scoped_nsobject<BookmarkMenuCocoaController> controller_; | |
169 | 141 |
170 // The folder image so we can use one copy for all. | 142 // The folder image so we can use one copy for all. |
171 scoped_nsobject<NSImage> folder_image_; | 143 scoped_nsobject<NSImage> folder_image_; |
172 | 144 |
173 // In order to appropriately update items in the bookmark menu, without | 145 // In order to appropriately update items in the bookmark menu, without |
174 // forcing a rebuild, map the model's nodes to menu items. | 146 // forcing a rebuild, map the model's nodes to menu items. |
175 std::map<const BookmarkNode*, NSMenuItem*> bookmark_nodes_; | 147 std::map<const BookmarkNode*, NSMenuItem*> bookmark_nodes_; |
176 }; | 148 }; |
177 | 149 |
178 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ | 150 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ |
OLD | NEW |