OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 #include "chrome/browser/cocoa/bookmark_menu_bridge.h" |
| 6 #import <AppKit/AppKit.h> |
| 7 #include "base/sys_string_conversions.h" |
| 8 #include "chrome/app/chrome_dll_resource.h" // IDC_BOOKMARK_MENU |
| 9 #include "chrome/browser/browser.h" |
| 10 #include "chrome/browser/profile.h" |
| 11 |
| 12 |
| 13 BookmarkMenuBridge::BookmarkMenuBridge(Browser* browser) : browser_(browser) { |
| 14 browser_->profile()->GetBookmarkModel()->AddObserver(this); |
| 15 } |
| 16 |
| 17 BookmarkMenuBridge::~BookmarkMenuBridge() { |
| 18 browser_->profile()->GetBookmarkModel()->RemoveObserver(this); |
| 19 } |
| 20 |
| 21 void BookmarkMenuBridge::Loaded(BookmarkModel* model) { |
| 22 NSMenu *bookmark_menu = [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARK_MENU] |
| 23 submenu]; |
| 24 if (bookmark_menu == nil) |
| 25 return; |
| 26 |
| 27 this->ClearBookmarkMenu(bookmark_menu); |
| 28 |
| 29 // TODO(jrg): limit the number of bookmarks in the menubar to max_nodes |
| 30 // int max_nodes = IDC_BOOKMARK_MENUITEM_MAX - IDC_BOOKMARK_MENUITEM_BASE; |
| 31 this->AddNodeToMenu(model->GetBookmarkBarNode(), bookmark_menu); |
| 32 } |
| 33 |
| 34 void BookmarkMenuBridge::BookmarkNodeMoved(BookmarkModel* model, |
| 35 BookmarkNode* old_parent, |
| 36 int old_index, |
| 37 BookmarkNode* new_parent, |
| 38 int new_index) { |
| 39 // TODO(jrg): this is brute force; perhaps we should be nicer. |
| 40 this->Loaded(model); |
| 41 } |
| 42 |
| 43 void BookmarkMenuBridge::BookmarkNodeAdded(BookmarkModel* model, |
| 44 BookmarkNode* parent, |
| 45 int index) { |
| 46 |
| 47 // TODO(jrg): this is brute force; perhaps we should be nicer. |
| 48 this->Loaded(model); |
| 49 } |
| 50 |
| 51 void BookmarkMenuBridge::BookmarkNodeChanged(BookmarkModel* model, |
| 52 BookmarkNode* node) { |
| 53 |
| 54 // TODO(jrg): this is brute force; perhaps we should be nicer. |
| 55 this->Loaded(model); |
| 56 } |
| 57 |
| 58 void BookmarkMenuBridge::BookmarkNodeFavIconLoaded(BookmarkModel* model, |
| 59 BookmarkNode* node) { |
| 60 // Nothing to do here -- no icons in the menubar menus yet. |
| 61 // TODO(jrg): |
| 62 // Both Safari and FireFox have icons in their menubars for bookmarks. |
| 63 } |
| 64 |
| 65 void BookmarkMenuBridge::BookmarkNodeChildrenReordered(BookmarkModel* model, |
| 66 BookmarkNode* node) { |
| 67 // TODO(jrg): this is brute force; perhaps we should be nicer. |
| 68 this->Loaded(model); |
| 69 } |
| 70 |
| 71 void BookmarkMenuBridge::ClearBookmarkMenu(NSMenu* menu) { |
| 72 // Recursively delete all menus that look like a bookmark. Assume |
| 73 // all items with submenus contain only bookmarks. This typically |
| 74 // deletes everything except the first two items ("Add Bookmark..." |
| 75 // and separator) |
| 76 NSArray* items = [menu itemArray]; |
| 77 for (NSMenuItem* item in items) { |
| 78 NSInteger tag = [item tag]; |
| 79 if ((tag >= IDC_BOOKMARK_MENUITEM_BASE) && |
| 80 (tag < IDC_BOOKMARK_MENUITEM_MAX)) { |
| 81 [menu removeItem:item]; |
| 82 } else if ([item hasSubmenu]) { |
| 83 [menu removeItem:item]; // Will eventually [obj release] all its kids |
| 84 } else { |
| 85 // Not a bookmark or item with submenu, so leave it alone. |
| 86 } |
| 87 } |
| 88 } |
| 89 |
| 90 // TODO(jrg): add actions for these menu items |
| 91 void BookmarkMenuBridge::AddNodeToMenu(BookmarkNode* node, |
| 92 NSMenu* menu) { |
| 93 for (int i = 0; i < node->GetChildCount(); i++) { |
| 94 BookmarkNode* child = node->GetChild(i); |
| 95 // TODO(jrg): Should we limit the title length? |
| 96 // For the Bookmark Bar under windows, items appear trimmed to ~19 |
| 97 // chars (looks like a pixel width limit). |
| 98 NSString* title = base::SysWideToNSString(child->GetTitle()); |
| 99 NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title |
| 100 action:nil |
| 101 keyEquivalent:@""] autorelease]; |
| 102 [item setTag:IDC_BOOKMARK_MENUITEM_BASE]; |
| 103 [menu addItem:item]; |
| 104 if (child->is_folder()) { |
| 105 NSMenu* submenu = [[[NSMenu alloc] initWithTitle:title] autorelease]; |
| 106 [menu setSubmenu:submenu forItem:item]; |
| 107 this->AddNodeToMenu(child, submenu); // recursive call |
| 108 } |
| 109 } |
| 110 } |
OLD | NEW |