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 #import <AppKit/AppKit.h> | 5 #import <AppKit/AppKit.h> |
6 | 6 |
7 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
9 #import "chrome/browser/app_controller_mac.h" | 9 #import "chrome/browser/app_controller_mac.h" |
10 #include "chrome/browser/bookmarks/bookmark_model.h" | 10 #include "chrome/browser/bookmarks/bookmark_model.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
13 #include "chrome/browser/ui/browser_list.h" | 13 #include "chrome/browser/ui/browser_list.h" |
14 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" | 14 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" |
15 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_cocoa_controller.h" | 15 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_cocoa_controller.h" |
16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
17 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
18 #include "skia/ext/skia_utils_mac.h" | 18 #include "skia/ext/skia_utils_mac.h" |
19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
22 #include "ui/gfx/mac/nsimage_cache.h" | 22 #include "ui/gfx/mac/nsimage_cache.h" |
23 | 23 |
24 BookmarkMenuBridge::BookmarkMenuBridge(Profile* profile, | 24 BookmarkMenuBridge::BookmarkMenuBridge(Profile* profile, NSMenu* menu) |
25 NSMenu *menu) | |
26 : menuIsValid_(false), | 25 : menuIsValid_(false), |
27 profile_(profile), | 26 profile_(profile), |
28 controller_([[BookmarkMenuCocoaController alloc] initWithBridge:this | 27 controller_([[BookmarkMenuCocoaController alloc] initWithBridge:this |
29 andMenu:menu]) { | 28 andMenu:menu]) { |
30 if (GetBookmarkModel()) | 29 if (GetBookmarkModel()) |
31 ObserveBookmarkModel(); | 30 ObserveBookmarkModel(); |
32 } | 31 } |
33 | 32 |
34 BookmarkMenuBridge::~BookmarkMenuBridge() { | 33 BookmarkMenuBridge::~BookmarkMenuBridge() { |
35 BookmarkModel *model = GetBookmarkModel(); | 34 BookmarkModel* model = GetBookmarkModel(); |
36 if (model) | 35 if (model) |
37 model->RemoveObserver(this); | 36 model->RemoveObserver(this); |
38 [controller_ release]; | 37 [controller_ release]; |
39 } | 38 } |
40 | 39 |
41 NSMenu* BookmarkMenuBridge::BookmarkMenu() { | 40 NSMenu* BookmarkMenuBridge::BookmarkMenu() { |
42 return [controller_ menu]; | 41 return [controller_ menu]; |
43 } | 42 } |
44 | 43 |
45 void BookmarkMenuBridge::Loaded(BookmarkModel* model, bool ids_reassigned) { | 44 void BookmarkMenuBridge::Loaded(BookmarkModel* model, bool ids_reassigned) { |
46 InvalidateMenu(); | 45 InvalidateMenu(); |
47 } | 46 } |
48 | 47 |
49 void BookmarkMenuBridge::UpdateMenu(NSMenu* bookmark_menu) { | 48 void BookmarkMenuBridge::UpdateMenu(NSMenu* bookmark_menu) { |
50 UpdateMenuInternal(bookmark_menu, false); | 49 UpdateMenuInternal(bookmark_menu, false); |
51 } | 50 } |
52 | 51 |
53 void BookmarkMenuBridge::UpdateSubMenu(NSMenu* bookmark_menu) { | 52 void BookmarkMenuBridge::UpdateSubMenu(NSMenu* bookmark_menu) { |
54 UpdateMenuInternal(bookmark_menu, true); | 53 UpdateMenuInternal(bookmark_menu, true); |
55 } | 54 } |
56 | 55 |
57 void BookmarkMenuBridge::UpdateMenuInternal(NSMenu* bookmark_menu, | 56 void BookmarkMenuBridge::UpdateMenuInternal(NSMenu* bookmark_menu, |
58 bool is_submenu) { | 57 bool is_submenu) { |
59 DCHECK(bookmark_menu); | 58 DCHECK(bookmark_menu); |
60 if (menuIsValid_) | 59 if (menuIsValid_) |
61 return; | 60 return; |
| 61 |
62 BookmarkModel* model = GetBookmarkModel(); | 62 BookmarkModel* model = GetBookmarkModel(); |
63 if (!model || !model->IsLoaded()) | 63 if (!model || !model->IsLoaded()) |
64 return; | 64 return; |
65 | 65 |
66 if (!folder_image_) { | 66 if (!folder_image_) { |
67 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 67 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
68 folder_image_.reset( | 68 folder_image_.reset( |
69 [rb.GetNativeImageNamed(IDR_BOOKMARK_BAR_FOLDER) retain]); | 69 [rb.GetNativeImageNamed(IDR_BOOKMARK_BAR_FOLDER) retain]); |
70 } | 70 } |
71 | 71 |
72 ClearBookmarkMenu(bookmark_menu); | 72 ClearBookmarkMenu(bookmark_menu); |
73 | 73 |
74 // Add bookmark bar items, if any. | 74 // Add bookmark bar items, if any. |
75 const BookmarkNode* barNode = model->bookmark_bar_node(); | 75 const BookmarkNode* barNode = model->bookmark_bar_node(); |
76 CHECK(barNode); | 76 CHECK(barNode); |
77 if (!barNode->empty()) { | 77 if (!barNode->empty()) { |
78 [bookmark_menu addItem:[NSMenuItem separatorItem]]; | 78 [bookmark_menu addItem:[NSMenuItem separatorItem]]; |
79 AddNodeToMenu(barNode, bookmark_menu, !is_submenu); | 79 AddNodeToMenu(barNode, bookmark_menu, !is_submenu); |
80 } | 80 } |
81 | 81 |
82 // If the "Other Bookmarks" folder has any content, make a submenu for it and | 82 // If the "Other Bookmarks" folder has any content, make a submenu for it and |
83 // fill it in. | 83 // fill it in. |
84 if (!model->other_node()->empty()) { | 84 if (!model->other_node()->empty()) { |
85 NSString* other_items_title = | |
86 l10n_util::GetNSString(IDS_BOOKMARK_BAR_OTHER_FOLDER_NAME); | |
87 [bookmark_menu addItem:[NSMenuItem separatorItem]]; | 85 [bookmark_menu addItem:[NSMenuItem separatorItem]]; |
88 AddNodeAsSubmenu(bookmark_menu, | 86 AddNodeAsSubmenu(bookmark_menu, |
89 model->other_node(), | 87 model->other_node(), |
90 other_items_title, | |
91 !is_submenu); | 88 !is_submenu); |
92 } | 89 } |
93 | 90 |
| 91 // If the "Mobile Bookmarks" folder has any content, make a submenu for it and |
| 92 // fill it in. |
| 93 if (!model->mobile_node()->empty()) { |
| 94 // Add a separator if we did not already add one due to a non-empty |
| 95 // "Other Bookmarks" folder. |
| 96 if (model->other_node()->empty()) |
| 97 [bookmark_menu addItem:[NSMenuItem separatorItem]]; |
| 98 |
| 99 AddNodeAsSubmenu(bookmark_menu, |
| 100 model->mobile_node(), |
| 101 !is_submenu); |
| 102 } |
| 103 |
94 menuIsValid_ = true; | 104 menuIsValid_ = true; |
95 } | 105 } |
96 | 106 |
97 void BookmarkMenuBridge::BookmarkModelBeingDeleted(BookmarkModel* model) { | 107 void BookmarkMenuBridge::BookmarkModelBeingDeleted(BookmarkModel* model) { |
98 NSMenu* bookmark_menu = BookmarkMenu(); | 108 NSMenu* bookmark_menu = BookmarkMenu(); |
99 if (bookmark_menu == nil) | 109 if (bookmark_menu == nil) |
100 return; | 110 return; |
101 | 111 |
102 ClearBookmarkMenu(bookmark_menu); | 112 ClearBookmarkMenu(bookmark_menu); |
103 } | 113 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 // any. | 198 // any. |
189 [menu removeItem:item]; | 199 [menu removeItem:item]; |
190 } else { | 200 } else { |
191 // Leave it alone. | 201 // Leave it alone. |
192 } | 202 } |
193 } | 203 } |
194 } | 204 } |
195 | 205 |
196 void BookmarkMenuBridge::AddNodeAsSubmenu(NSMenu* menu, | 206 void BookmarkMenuBridge::AddNodeAsSubmenu(NSMenu* menu, |
197 const BookmarkNode* node, | 207 const BookmarkNode* node, |
198 NSString* title, | |
199 bool add_extra_items) { | 208 bool add_extra_items) { |
| 209 NSString* title = SysUTF16ToNSString(node->GetTitle()); |
200 NSMenuItem* items = [[[NSMenuItem alloc] | 210 NSMenuItem* items = [[[NSMenuItem alloc] |
201 initWithTitle:title | 211 initWithTitle:title |
202 action:nil | 212 action:nil |
203 keyEquivalent:@""] autorelease]; | 213 keyEquivalent:@""] autorelease]; |
204 [items setImage:folder_image_]; | 214 [items setImage:folder_image_]; |
205 [menu addItem:items]; | 215 [menu addItem:items]; |
206 NSMenu* other_submenu = [[[NSMenu alloc] initWithTitle:title] | 216 NSMenu* submenu = [[[NSMenu alloc] initWithTitle:title] autorelease]; |
207 autorelease]; | 217 [menu setSubmenu:submenu forItem:items]; |
208 [menu setSubmenu:other_submenu forItem:items]; | 218 AddNodeToMenu(node, submenu, add_extra_items); |
209 AddNodeToMenu(node, other_submenu, add_extra_items); | |
210 } | 219 } |
211 | 220 |
212 // TODO(jrg): limit the number of bookmarks in the menubar? | 221 // TODO(jrg): limit the number of bookmarks in the menubar? |
213 void BookmarkMenuBridge::AddNodeToMenu(const BookmarkNode* node, NSMenu* menu, | 222 void BookmarkMenuBridge::AddNodeToMenu(const BookmarkNode* node, NSMenu* menu, |
214 bool add_extra_items) { | 223 bool add_extra_items) { |
215 int child_count = node->child_count(); | 224 int child_count = node->child_count(); |
216 if (!child_count) { | 225 if (!child_count) { |
217 NSString* empty_string = l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU); | 226 NSString* empty_string = l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU); |
218 NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:empty_string | 227 NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:empty_string |
219 action:nil | 228 action:nil |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 | 316 |
308 NSMenuItem* BookmarkMenuBridge::MenuItemForNode(const BookmarkNode* node) { | 317 NSMenuItem* BookmarkMenuBridge::MenuItemForNode(const BookmarkNode* node) { |
309 if (!node) | 318 if (!node) |
310 return nil; | 319 return nil; |
311 std::map<const BookmarkNode*, NSMenuItem*>::iterator it = | 320 std::map<const BookmarkNode*, NSMenuItem*>::iterator it = |
312 bookmark_nodes_.find(node); | 321 bookmark_nodes_.find(node); |
313 if (it == bookmark_nodes_.end()) | 322 if (it == bookmark_nodes_.end()) |
314 return nil; | 323 return nil; |
315 return it->second; | 324 return it->second; |
316 } | 325 } |
OLD | NEW |