1 // Copyright (c) 2012 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.
5 #import "chrome/browser/ui/cocoa/toolbar/back_forward_menu_controller.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/cocoa/menu_button.h"
11 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h"
12 #import "ui/events/event_utils.h"
13 #include "ui/gfx/image/image.h"
15 using base::SysUTF16ToNSString;
17 @implementation BackForwardMenuController
19 // Accessors and mutators:
21 @synthesize type = type_;
25 - (id)initWithBrowser:(Browser*)browser
26 modelType:(BackForwardMenuType)type
27 button:(MenuButton*)button {
28 if ((self = [super init])) {
31 model_.reset(new BackForwardMenuModel(browser, type_));
33 backForwardMenu_.reset([[NSMenu alloc] initWithTitle:@""]);
34 DCHECK(backForwardMenu_.get());
35 [backForwardMenu_ setDelegate:self];
37 [button_ setAttachedMenu:backForwardMenu_];
38 [button_ setOpenMenuOnClick:NO];
43 - (void)browserWillBeDestroyed {
44 [button_ setAttachedMenu:nil];
45 [backForwardMenu_ setDelegate:nil];
46 backForwardMenu_.reset();
50 // Methods as delegate:
52 // Called by backForwardMenu_ just before tracking begins.
53 //TODO(viettrungluu): should we do anything for chapter stops (see model)?
54 - (void)menuNeedsUpdate:(NSMenu*)menu {
55 DCHECK(menu == backForwardMenu_);
57 // Remove old menu items (backwards order is as good as any).
58 for (NSInteger i = [menu numberOfItems]; i > 0; i--)
59 [menu removeItemAtIndex:(i - 1)];
61 // 0-th item must be blank. (This is because we use a pulldown list, for which
62 // Cocoa uses the 0-th item as "title" in the button.)
63 [menu insertItemWithTitle:@""
67 for (int menuID = 0; menuID < model_->GetItemCount(); menuID++) {
68 if (model_->IsSeparator(menuID)) {
69 [menu insertItem:[NSMenuItem separatorItem]
70 atIndex:(menuID + 1)];
72 // Create a menu item with the right label.
73 NSMenuItem* menuItem = [[NSMenuItem alloc]
74 initWithTitle:SysUTF16ToNSString(model_->GetLabelAt(menuID))
77 [menuItem autorelease];
80 // Icon (if it has one).
81 if (model_->GetIconAt(menuID, &icon))
82 [menuItem setImage:icon.ToNSImage()];
84 // This will make it call our |-executeMenuItem:| method. We store the
85 // |menuID| (or |menu_id|) in the tag.
86 [menuItem setTag:menuID];
87 [menuItem setTarget:self];
88 [menuItem setAction:@selector(executeMenuItem:)];
90 // Put it in the menu!
91 [menu insertItem:menuItem
92 atIndex:(menuID + 1)];
99 - (void)executeMenuItem:(id)sender {
100 DCHECK([sender isKindOfClass:[NSMenuItem class]]);
101 int menuID = [sender tag];
102 int event_flags = ui::EventFlagsFromNative([NSApp currentEvent]);
103 model_->ActivatedAt(menuID, event_flags);
106 @end // @implementation BackForwardMenuController