Chromium Code Reviews| 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/scoped_ptr.h" | 8 #include "base/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) { | |
|
Avi (use Gerrit)
2011/03/25 00:19:35
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 // Called by MenuDelegate when an icon changes. | |
| 65 - (void)onIconChanged:(int) modelIndex { | |
|
Avi (use Gerrit)
2011/03/25 00:19:35
No space after ).
| |
| 66 // modelIndex + 1 since 0-th item is the "title" | |
| 67 NSMenuItem* menuItem = | |
| 68 [backForwardMenu_ itemAtIndex:(NSInteger)modelIndex + 1]; | |
|
Avi (use Gerrit)
2011/03/25 00:19:35
Why the cast? Drop the "(NSInteger)".
| |
| 69 SkBitmap icon; | |
| 70 // Icon (if it has one). | |
|
Avi (use Gerrit)
2011/03/25 00:19:35
Comment is obvious; drop.
| |
| 71 if (model_->GetIconAt(modelIndex, &icon)) { | |
| 72 [menuItem setImage:SkBitmapToNSImage(icon)]; | |
| 73 } | |
| 74 } | |
| 75 | |
| 45 // Methods as delegate: | 76 // Methods as delegate: |
| 46 | 77 |
| 47 // Called by backForwardMenu_ just before tracking begins. | 78 // Called by backForwardMenu_ just before tracking begins. |
| 48 //TODO(viettrungluu): should we do anything for chapter stops (see model)? | 79 //TODO(viettrungluu): should we do anything for chapter stops (see model)? |
| 49 - (void)menuNeedsUpdate:(NSMenu*)menu { | 80 - (void)menuNeedsUpdate:(NSMenu*)menu { |
| 50 DCHECK(menu == backForwardMenu_); | 81 DCHECK(menu == backForwardMenu_); |
| 82 // Retrieving favicons from history requires nestable tasks. | |
| 83 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
| 51 | 84 |
| 52 // Remove old menu items (backwards order is as good as any). | 85 // Remove old menu items (backwards order is as good as any). |
| 53 for (NSInteger i = [menu numberOfItems]; i > 0; i--) | 86 for (NSInteger i = [menu numberOfItems]; i > 0; i--) |
| 54 [menu removeItemAtIndex:(i - 1)]; | 87 [menu removeItemAtIndex:(i - 1)]; |
| 55 | 88 |
| 56 // 0-th item must be blank. (This is because we use a pulldown list, for which | 89 // 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.) | 90 // Cocoa uses the 0-th item as "title" in the button.) |
| 58 [menu insertItemWithTitle:@"" | 91 [menu insertItemWithTitle:@"" |
| 59 action:nil | 92 action:nil |
| 60 keyEquivalent:@"" | 93 keyEquivalent:@"" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 | 126 |
| 94 - (void)executeMenuItem:(id)sender { | 127 - (void)executeMenuItem:(id)sender { |
| 95 DCHECK([sender isKindOfClass:[NSMenuItem class]]); | 128 DCHECK([sender isKindOfClass:[NSMenuItem class]]); |
| 96 int menuID = [sender tag]; | 129 int menuID = [sender tag]; |
| 97 model_->ActivatedAtWithDisposition( | 130 model_->ActivatedAtWithDisposition( |
| 98 menuID, | 131 menuID, |
| 99 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent])); | 132 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent])); |
| 100 } | 133 } |
| 101 | 134 |
| 102 @end // @implementation BackForwardMenuController | 135 @end // @implementation BackForwardMenuController |
| OLD | NEW |