| 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 "chrome/browser/ui/cocoa/toolbar/back_forward_menu_controller.h" | 5 #import "chrome/browser/ui/cocoa/toolbar/back_forward_menu_controller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 10 #import "chrome/browser/ui/cocoa/event_utils.h" | 10 #import "chrome/browser/ui/cocoa/event_utils.h" |
| 11 #import "chrome/browser/ui/cocoa/menu_button.h" | 11 #import "chrome/browser/ui/cocoa/menu_button.h" |
| 12 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h" | 12 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h" |
| 13 #include "skia/ext/skia_utils_mac.h" | 13 #include "skia/ext/skia_utils_mac.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 | 15 |
| 16 using base::SysUTF16ToNSString; | 16 using base::SysUTF16ToNSString; |
| 17 using gfx::SkBitmapToNSImage; | 17 using gfx::SkBitmapToNSImage; |
| 18 | 18 |
| 19 @implementation BackForwardMenuController | 19 @implementation BackForwardMenuController |
| 20 | 20 |
| 21 // Accessors and mutators: | 21 // Accessors and mutators: |
| 22 | 22 |
| 23 @synthesize type = type_; | 23 @synthesize type = type_; |
| 24 | 24 |
| 25 namespace BackForwardMenuControllerInternal { |
| 26 |
| 27 class MenuDelegate : public ui::MenuModelDelegate { |
| 28 public: |
| 29 explicit MenuDelegate(BackForwardMenuController* owner) |
| 30 : owner_(owner) {} |
| 31 // Called by ui::MenuModelDelegate when an icon changes. |
| 32 virtual void OnIconChanged(int modelIndex) OVERRIDE { |
| 33 [owner_ onIconChanged:modelIndex]; |
| 34 } |
| 35 |
| 36 private: |
| 37 BackForwardMenuController* owner_; |
| 38 }; |
| 39 |
| 40 } // BackForwardMenuControllerInternal namespace |
| 41 |
| 25 // Own methods: | 42 // Own methods: |
| 26 | 43 |
| 27 - (id)initWithBrowser:(Browser*)browser | 44 - (id)initWithBrowser:(Browser*)browser |
| 28 modelType:(BackForwardMenuType)type | 45 modelType:(BackForwardMenuType)type |
| 29 button:(MenuButton*)button { | 46 button:(MenuButton*)button { |
| 30 if ((self = [super init])) { | 47 if ((self = [super init])) { |
| 31 type_ = type; | 48 type_ = type; |
| 32 button_ = button; | 49 button_ = button; |
| 33 model_.reset(new BackForwardMenuModel(browser, type_)); | 50 model_.reset(new BackForwardMenuModel(browser, type_)); |
| 34 DCHECK(model_.get()); | 51 DCHECK(model_.get()); |
| 35 backForwardMenu_.reset([[NSMenu alloc] initWithTitle:@""]); | 52 backForwardMenu_.reset([[NSMenu alloc] initWithTitle:@""]); |
| 36 DCHECK(backForwardMenu_.get()); | 53 DCHECK(backForwardMenu_.get()); |
| 37 [backForwardMenu_ setDelegate:self]; | 54 [backForwardMenu_ setDelegate:self]; |
| 38 | 55 contextMenuDelegate_.reset( |
| 56 new BackForwardMenuControllerInternal::MenuDelegate(self)); |
| 57 model_->SetMenuModelDelegate(contextMenuDelegate_.get()); |
| 39 [button_ setAttachedMenu:backForwardMenu_]; | 58 [button_ setAttachedMenu:backForwardMenu_]; |
| 40 [button_ setOpenMenuOnClick:NO]; | 59 [button_ setOpenMenuOnClick:NO]; |
| 41 } | 60 } |
| 42 return self; | 61 return self; |
| 43 } | 62 } |
| 44 | 63 |
| 64 - (void)dealloc { |
| 65 model_->SetMenuModelDelegate(NULL); |
| 66 [super dealloc]; |
| 67 } |
| 68 |
| 69 // Called by MenuDelegate when an icon changes. |
| 70 - (void)onIconChanged:(int)modelIndex { |
| 71 // modelIndex + 1 since 0-th item is the "title" |
| 72 NSMenuItem* menuItem = |
| 73 [backForwardMenu_ itemAtIndex:modelIndex + 1]; |
| 74 SkBitmap icon; |
| 75 if (model_->GetIconAt(modelIndex, &icon)) { |
| 76 [menuItem setImage:SkBitmapToNSImage(icon)]; |
| 77 } |
| 78 } |
| 79 |
| 45 // Methods as delegate: | 80 // Methods as delegate: |
| 46 | 81 |
| 47 // Called by backForwardMenu_ just before tracking begins. | 82 // Called by backForwardMenu_ just before tracking begins. |
| 48 //TODO(viettrungluu): should we do anything for chapter stops (see model)? | 83 //TODO(viettrungluu): should we do anything for chapter stops (see model)? |
| 49 - (void)menuNeedsUpdate:(NSMenu*)menu { | 84 - (void)menuNeedsUpdate:(NSMenu*)menu { |
| 50 DCHECK(menu == backForwardMenu_); | 85 DCHECK(menu == backForwardMenu_); |
| 86 // Retrieving favicons from history requires nestable tasks. |
| 87 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 51 | 88 |
| 52 // Remove old menu items (backwards order is as good as any). | 89 // Remove old menu items (backwards order is as good as any). |
| 53 for (NSInteger i = [menu numberOfItems]; i > 0; i--) | 90 for (NSInteger i = [menu numberOfItems]; i > 0; i--) |
| 54 [menu removeItemAtIndex:(i - 1)]; | 91 [menu removeItemAtIndex:(i - 1)]; |
| 55 | 92 |
| 56 // 0-th item must be blank. (This is because we use a pulldown list, for which | 93 // 0-th item must be blank. (This is because we use a pulldown list, for which |
| 57 // Cocoa uses the 0-th item as "title" in the button.) | 94 // Cocoa uses the 0-th item as "title" in the button.) |
| 58 [menu insertItemWithTitle:@"" | 95 [menu insertItemWithTitle:@"" |
| 59 action:nil | 96 action:nil |
| 60 keyEquivalent:@"" | 97 keyEquivalent:@"" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 130 |
| 94 - (void)executeMenuItem:(id)sender { | 131 - (void)executeMenuItem:(id)sender { |
| 95 DCHECK([sender isKindOfClass:[NSMenuItem class]]); | 132 DCHECK([sender isKindOfClass:[NSMenuItem class]]); |
| 96 int menuID = [sender tag]; | 133 int menuID = [sender tag]; |
| 97 model_->ActivatedAtWithDisposition( | 134 model_->ActivatedAtWithDisposition( |
| 98 menuID, | 135 menuID, |
| 99 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent])); | 136 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent])); |
| 100 } | 137 } |
| 101 | 138 |
| 102 @end // @implementation BackForwardMenuController | 139 @end // @implementation BackForwardMenuController |
| OLD | NEW |