From 490ecb0178c9fa1059b5cabf057f52b4faafae77 Mon Sep 17 00:00:00 2001 From: "pinkerton@chromium.org" Date: Wed, 15 Jul 2009 14:35:59 +0000 Subject: Implements the AppendRadioMenuItem and AppendCheckboxMenuItem functions for the mac platform. Patch from pwicks86@gmail.com (Paul Wicks) BUG=None TEST=None git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20728 0039d316-1c4b-4281-b951-d872f2087c98 --- .../tab_contents/render_view_context_menu_mac.h | 7 ++++ .../tab_contents/render_view_context_menu_mac.mm | 47 +++++++++++++--------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/chrome/browser/tab_contents/render_view_context_menu_mac.h b/chrome/browser/tab_contents/render_view_context_menu_mac.h index 0703a18..ee42bce 100644 --- a/chrome/browser/tab_contents/render_view_context_menu_mac.h +++ b/chrome/browser/tab_contents/render_view_context_menu_mac.h @@ -40,6 +40,13 @@ class RenderViewContextMenuMac : public RenderViewContextMenu { static NSString* PrepareLabelForDisplay(const string16& label); private: + // Private helper method used by the implementations of the above methods. + // All MenuItems are represented as NSMenuItems no matter what kind they + // are (Normal, Radio, Checkbox). All of the above call this method, providing + // a useful value for |state| based upon some specific lookup, usually based + // on the id. + void AppendMenuItemWithState(int id, const string16& label, + NSCellStateValue state); NSMenu* menu_; NSMenu* insert_menu_; // weak, where new items are inserted (usually // |menu_| unless there's a submenu in progress). diff --git a/chrome/browser/tab_contents/render_view_context_menu_mac.mm b/chrome/browser/tab_contents/render_view_context_menu_mac.mm index 34b49b3..bdb3dd7 100644 --- a/chrome/browser/tab_contents/render_view_context_menu_mac.mm +++ b/chrome/browser/tab_contents/render_view_context_menu_mac.mm @@ -9,6 +9,7 @@ #include "base/sys_string_conversions.h" #include "chrome/browser/profile.h" #include "grit/generated_resources.h" +#include "base/scoped_nsobject.h" // Obj-C bridge class that is the target of all items in the context menu. // Relies on the tag being set to the command id. Uses |context_| to @@ -84,26 +85,19 @@ void RenderViewContextMenuMac::AppendMenuItem(int command_id) { void RenderViewContextMenuMac::AppendMenuItem(int command_id, const string16& label) { - // Create the item and set its target/action to |target_| with the command - // as |command_id|. Then add it to the menu at the end. - NSMenuItem* item = - [[[NSMenuItem alloc] initWithTitle:PrepareLabelForDisplay(label) - action:@selector(itemSelected:) - keyEquivalent:@""] autorelease]; - [item setTag:command_id]; - [item setTarget:target_]; - [item setEnabled:IsItemCommandEnabled(command_id) ? YES : NO]; - [insert_menu_ addItem:item]; + AppendMenuItemWithState(command_id, label, NSOffState); } -void RenderViewContextMenuMac::AppendRadioMenuItem(int id, +void RenderViewContextMenuMac::AppendRadioMenuItem(int command_id, const string16& label) { - NOTIMPLEMENTED(); -} + AppendMenuItemWithState(command_id, label, + ItemIsChecked(command_id) ? NSOnState : NSOffState); -void RenderViewContextMenuMac::AppendCheckboxMenuItem(int id, +} +void RenderViewContextMenuMac::AppendCheckboxMenuItem(int command_id, const string16& label) { - NOTIMPLEMENTED(); + AppendMenuItemWithState(command_id, label, + ItemIsChecked(command_id) ? NSOnState : NSOffState); } void RenderViewContextMenuMac::AppendSeparator() { @@ -124,10 +118,10 @@ void RenderViewContextMenuMac::StartSubMenu(int command_id, // We don't need to retain the submenu as the context menu already does, but // we switch the "insert menu" so subsequent items are added to the submenu // and not the main menu. This happens until someone calls FinishSubMenu(). - NSMenuItem* submenu_item = - [[[NSMenuItem alloc] initWithTitle:PrepareLabelForDisplay(label) - action:nil - keyEquivalent:@""] autorelease]; + scoped_nsobject submenu_item([[NSMenuItem alloc] + initWithTitle:PrepareLabelForDisplay(label) + action:nil + keyEquivalent:@""]); insert_menu_ = [[[NSMenu alloc] init] autorelease]; [submenu_item setSubmenu:insert_menu_]; [menu_ addItem:submenu_item]; @@ -139,3 +133,18 @@ void RenderViewContextMenuMac::FinishSubMenu() { DCHECK(insert_menu_ != menu_); insert_menu_ = menu_; } + +void RenderViewContextMenuMac::AppendMenuItemWithState(int command_id, + const string16& label, + NSCellStateValue state) { + scoped_nsobject item([[NSMenuItem alloc] + initWithTitle:PrepareLabelForDisplay(label) + action:@selector(itemSelected:) + keyEquivalent:@""]); + [item setState:state]; + [item setTag:command_id]; + [item setTarget:target_]; + [item setEnabled:IsItemCommandEnabled(command_id) ? YES : NO]; + [insert_menu_ addItem:item]; +} + -- cgit v1.1