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 "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" |
| 8 |
7 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
8 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
9 #import "chrome/browser/app_controller_mac.h" | 11 #import "chrome/browser/app_controller_mac.h" |
10 #include "chrome/browser/bookmarks/bookmark_model.h" | 12 #include "chrome/browser/bookmarks/bookmark_model.h" |
11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
13 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
14 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" | |
15 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_cocoa_controller.h" | 16 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_cocoa_controller.h" |
16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
17 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
18 #include "skia/ext/skia_utils_mac.h" | 19 #include "skia/ext/skia_utils_mac.h" |
19 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
20 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
22 #include "ui/gfx/mac/nsimage_cache.h" | 23 #include "ui/gfx/mac/nsimage_cache.h" |
23 | 24 |
24 BookmarkMenuBridge::BookmarkMenuBridge(Profile* profile, | 25 BookmarkMenuBridge::BookmarkMenuBridge(Profile* profile, |
25 NSMenu *menu) | 26 NSMenu *menu) |
26 : menuIsValid_(false), | 27 : menu_is_valid_(false), |
| 28 root_node_(NULL), |
27 profile_(profile), | 29 profile_(profile), |
28 controller_([[BookmarkMenuCocoaController alloc] initWithBridge:this | 30 controller_([[BookmarkMenuCocoaController alloc] initWithBridge:this |
29 andMenu:menu]) { | 31 andMenu:menu]) { |
| 32 if (GetBookmarkModel()) |
| 33 ObserveBookmarkModel(); |
| 34 } |
| 35 |
| 36 BookmarkMenuBridge::BookmarkMenuBridge(const BookmarkNode* root_node, |
| 37 Profile* profile, |
| 38 NSMenu* menu) |
| 39 : menu_is_valid_(false), |
| 40 root_node_(root_node), |
| 41 profile_(profile), |
| 42 controller_([[BookmarkMenuCocoaController alloc] initWithBridge:this |
| 43 andMenu:menu]) { |
30 if (GetBookmarkModel()) | 44 if (GetBookmarkModel()) |
31 ObserveBookmarkModel(); | 45 ObserveBookmarkModel(); |
32 } | 46 } |
33 | 47 |
34 BookmarkMenuBridge::~BookmarkMenuBridge() { | 48 BookmarkMenuBridge::~BookmarkMenuBridge() { |
35 BookmarkModel *model = GetBookmarkModel(); | 49 BookmarkModel *model = GetBookmarkModel(); |
36 if (model) | 50 if (model) |
37 model->RemoveObserver(this); | 51 model->RemoveObserver(this); |
38 [controller_ release]; | |
39 } | 52 } |
40 | 53 |
41 NSMenu* BookmarkMenuBridge::BookmarkMenu() { | 54 NSMenu* BookmarkMenuBridge::BookmarkMenu() { |
42 return [controller_ menu]; | 55 return [controller_ menu]; |
43 } | 56 } |
44 | 57 |
45 void BookmarkMenuBridge::Loaded(BookmarkModel* model, bool ids_reassigned) { | 58 void BookmarkMenuBridge::Loaded(BookmarkModel* model, bool ids_reassigned) { |
46 InvalidateMenu(); | 59 InvalidateMenu(); |
47 } | 60 } |
48 | 61 |
49 void BookmarkMenuBridge::UpdateMenu(NSMenu* bookmark_menu) { | 62 void BookmarkMenuBridge::UpdateMenu(NSMenu* bookmark_menu) { |
50 UpdateMenuInternal(bookmark_menu, false); | 63 UpdateMenuInternal(bookmark_menu, false); |
51 } | 64 } |
52 | 65 |
53 void BookmarkMenuBridge::UpdateSubMenu(NSMenu* bookmark_menu) { | 66 void BookmarkMenuBridge::UpdateSubMenu(NSMenu* bookmark_menu) { |
54 UpdateMenuInternal(bookmark_menu, true); | 67 UpdateMenuInternal(bookmark_menu, true); |
55 } | 68 } |
56 | 69 |
57 void BookmarkMenuBridge::UpdateMenuInternal(NSMenu* bookmark_menu, | 70 void BookmarkMenuBridge::UpdateMenuInternal(NSMenu* bookmark_menu, |
58 bool is_submenu) { | 71 bool is_submenu) { |
59 DCHECK(bookmark_menu); | 72 DCHECK(bookmark_menu); |
60 if (menuIsValid_) | 73 if (menu_is_valid_) |
61 return; | 74 return; |
62 BookmarkModel* model = GetBookmarkModel(); | 75 BookmarkModel* model = GetBookmarkModel(); |
63 if (!model || !model->IsLoaded()) | 76 if (!model || !model->IsLoaded()) |
64 return; | 77 return; |
65 | 78 |
66 if (!folder_image_) { | 79 if (!folder_image_) { |
67 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 80 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
68 folder_image_.reset( | 81 folder_image_.reset( |
69 [rb.GetNativeImageNamed(IDR_BOOKMARK_BAR_FOLDER) retain]); | 82 [rb.GetNativeImageNamed(IDR_BOOKMARK_BAR_FOLDER) retain]); |
70 } | 83 } |
71 | 84 |
72 ClearBookmarkMenu(bookmark_menu); | 85 ClearBookmarkMenu(bookmark_menu); |
73 | 86 |
74 // Add bookmark bar items, if any. | 87 if (!root_node_) { |
75 const BookmarkNode* barNode = model->bookmark_bar_node(); | 88 // Add bookmark bar items, if any. |
76 CHECK(barNode); | 89 const BookmarkNode* bar_node = model->bookmark_bar_node(); |
77 if (!barNode->empty()) { | 90 CHECK(bar_node); |
78 [bookmark_menu addItem:[NSMenuItem separatorItem]]; | 91 if (!bar_node->empty()) { |
79 AddNodeToMenu(barNode, bookmark_menu, !is_submenu); | 92 [bookmark_menu addItem:[NSMenuItem separatorItem]]; |
| 93 AddNodeToMenu(bar_node, bookmark_menu, !is_submenu); |
| 94 } |
| 95 |
| 96 // If the "Other Bookmarks" folder has any content, make a submenu for it |
| 97 // and fill it in. |
| 98 if (!model->other_node()->empty()) { |
| 99 NSString* other_items_title = |
| 100 l10n_util::GetNSString(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME); |
| 101 [bookmark_menu addItem:[NSMenuItem separatorItem]]; |
| 102 AddNodeAsSubmenu(bookmark_menu, |
| 103 model->other_node(), |
| 104 other_items_title, |
| 105 !is_submenu); |
| 106 } |
| 107 } else { |
| 108 AddNodeToMenu(root_node_, bookmark_menu, true); |
80 } | 109 } |
81 | 110 |
82 // If the "Other Bookmarks" folder has any content, make a submenu for it and | 111 menu_is_valid_ = true; |
83 // fill it in. | |
84 if (!model->other_node()->empty()) { | |
85 NSString* other_items_title = | |
86 l10n_util::GetNSString(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME); | |
87 [bookmark_menu addItem:[NSMenuItem separatorItem]]; | |
88 AddNodeAsSubmenu(bookmark_menu, | |
89 model->other_node(), | |
90 other_items_title, | |
91 !is_submenu); | |
92 } | |
93 | |
94 menuIsValid_ = true; | |
95 } | 112 } |
96 | 113 |
97 void BookmarkMenuBridge::BookmarkModelBeingDeleted(BookmarkModel* model) { | 114 void BookmarkMenuBridge::BookmarkModelBeingDeleted(BookmarkModel* model) { |
98 NSMenu* bookmark_menu = BookmarkMenu(); | 115 NSMenu* bookmark_menu = BookmarkMenu(); |
99 if (bookmark_menu == nil) | 116 if (bookmark_menu == nil) |
100 return; | 117 return; |
101 | 118 |
102 ClearBookmarkMenu(bookmark_menu); | 119 ClearBookmarkMenu(bookmark_menu); |
103 } | 120 } |
104 | 121 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 | 324 |
308 NSMenuItem* BookmarkMenuBridge::MenuItemForNode(const BookmarkNode* node) { | 325 NSMenuItem* BookmarkMenuBridge::MenuItemForNode(const BookmarkNode* node) { |
309 if (!node) | 326 if (!node) |
310 return nil; | 327 return nil; |
311 std::map<const BookmarkNode*, NSMenuItem*>::iterator it = | 328 std::map<const BookmarkNode*, NSMenuItem*>::iterator it = |
312 bookmark_nodes_.find(node); | 329 bookmark_nodes_.find(node); |
313 if (it == bookmark_nodes_.end()) | 330 if (it == bookmark_nodes_.end()) |
314 return nil; | 331 return nil; |
315 return it->second; | 332 return it->second; |
316 } | 333 } |
OLD | NEW |