| 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 #import "chrome/browser/cocoa/back_forward_menu_controller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/scoped_ptr.h" |
| 9 #include "base/sys_string_conversions.h" |
| 10 #include "chrome/browser/back_forward_menu_model.h" |
| 11 #import "chrome/browser/cocoa/delayedmenu_button.h" |
| 12 #include "skia/ext/skia_utils_mac.h" |
| 13 |
| 14 using base::SysUTF16ToNSString; |
| 15 using gfx::SkBitmapToNSImage; |
| 16 |
| 17 @implementation BackForwardMenuController |
| 18 |
| 19 // Accessors and mutators: |
| 20 |
| 21 @synthesize type = type_; |
| 22 |
| 23 // Own methods: |
| 24 |
| 25 - (id)initWithBrowser:(Browser*)browser |
| 26 modelType:(BackForwardMenuType)type |
| 27 button:(DelayedMenuButton*)button { |
| 28 if ((self = [super init])) { |
| 29 type_ = type; |
| 30 button_ = button; |
| 31 model_.reset(new BackForwardMenuModel(browser, type_)); |
| 32 DCHECK(model_.get()); |
| 33 menu_.reset([[NSMenu alloc] initWithTitle:@""]); |
| 34 DCHECK(menu_.get()); |
| 35 [menu_ setDelegate:self]; |
| 36 |
| 37 [button_ setMenu:menu_]; |
| 38 [button_ setMenuEnabled:YES]; |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 // Methods as delegate: |
| 44 |
| 45 // Called by menu_ just before tracking begins. |
| 46 //TODO(viettrungluu@gmail.com): do anything for chapter stops (see model)? |
| 47 - (void)menuNeedsUpdate:(NSMenu*)menu { |
| 48 DCHECK(menu == menu_); |
| 49 |
| 50 // Remove old menu items (backwards order is as good as any). |
| 51 for (NSInteger i = [menu_ numberOfItems]; i > 0; i--) |
| 52 [menu_ removeItemAtIndex:(i-1)]; |
| 53 |
| 54 // 0-th item must be blank. (This is because we use a pulldown list, for which |
| 55 // Cocoa uses the 0-th item as "title" in the button.) |
| 56 [menu_ insertItemWithTitle:@"" |
| 57 action:nil |
| 58 keyEquivalent:@"" |
| 59 atIndex:0]; |
| 60 for (int menuID = 1; menuID <= model_->GetTotalItemCount(); menuID++) { |
| 61 if (model_->IsSeparator(menuID)) { |
| 62 [menu_ insertItem:[NSMenuItem separatorItem] |
| 63 atIndex:menuID]; |
| 64 } else { |
| 65 // Create a menu item with the right label. |
| 66 NSMenuItem* menuItem = [[NSMenuItem alloc] |
| 67 initWithTitle:SysUTF16ToNSString(model_->GetItemLabel(menuID)) |
| 68 action:nil |
| 69 keyEquivalent:@""]; |
| 70 [menuItem autorelease]; |
| 71 |
| 72 // Only enable it if it's supposed to do something. |
| 73 [menuItem setEnabled:(model_->ItemHasCommand(menuID) ? YES : NO)]; |
| 74 |
| 75 // Icon (if it has one). |
| 76 if (model_->ItemHasIcon(menuID)) |
| 77 [menuItem setImage:SkBitmapToNSImage(model_->GetItemIcon(menuID))]; |
| 78 |
| 79 // This will make it call our |-executeMenuItem:| method. We store the |
| 80 // |menuID| (or |menu_id|) in the tag. |
| 81 [menuItem setTag:menuID]; |
| 82 [menuItem setTarget:self]; |
| 83 [menuItem setAction:@selector(executeMenuItem:)]; |
| 84 |
| 85 // Put it in the menu! |
| 86 [menu_ insertItem:menuItem |
| 87 atIndex:menuID]; |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 // Action methods: |
| 93 |
| 94 - (void)executeMenuItem:(id)sender { |
| 95 DCHECK([sender isKindOfClass:[NSMenuItem class]]); |
| 96 int menuID = [sender tag]; |
| 97 model_->ExecuteCommandById(menuID); |
| 98 } |
| 99 |
| 100 @end // @implementation BackForwardMenuController |
| OLD | NEW |