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