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 #include "chrome/browser/tab_contents/render_view_context_menu_mac.h" | 5 #include "chrome/browser/tab_contents/render_view_context_menu_mac.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/memory/scoped_nsobject.h" | 8 #include "base/memory/scoped_nsobject.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
11 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
12 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 12 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
13 #import "chrome/browser/ui/cocoa/menu_controller.h" | 13 #import "chrome/browser/ui/cocoa/menu_controller.h" |
14 #import "content/common/chrome_application_mac.h" | 14 #import "content/common/chrome_application_mac.h" |
15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
16 | 16 |
| 17 namespace { |
| 18 |
| 19 // Retrieves an NSMenuItem which has the specified command_id. This function |
| 20 // traverses the given |model| in the depth-first order. When this function |
| 21 // finds an item whose command_id is the same as the given |command_id|, it |
| 22 // returns the NSMenuItem associated with the item. This function emulates |
| 23 // views::MenuItemViews::GetMenuItemByID() for Mac. |
| 24 NSMenuItem* GetMenuItemByID(ui::MenuModel* model, |
| 25 NSMenu* menu, |
| 26 int command_id) { |
| 27 for (int i = 0; i < model->GetItemCount(); ++i) { |
| 28 NSMenuItem* item = [menu itemAtIndex:i]; |
| 29 if (model->GetCommandIdAt(i) == command_id) |
| 30 return item; |
| 31 |
| 32 ui::MenuModel* submenu = model->GetSubmenuModelAt(i); |
| 33 if (submenu && [item hasSubmenu]) { |
| 34 NSMenuItem* subitem = GetMenuItemByID(submenu, |
| 35 [item submenu], |
| 36 command_id); |
| 37 if (subitem) |
| 38 return subitem; |
| 39 } |
| 40 } |
| 41 return nil; |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
17 // Obj-C bridge class that is the target of all items in the context menu. | 46 // Obj-C bridge class that is the target of all items in the context menu. |
18 // Relies on the tag being set to the command id. | 47 // Relies on the tag being set to the command id. |
19 | 48 |
20 RenderViewContextMenuMac::RenderViewContextMenuMac( | 49 RenderViewContextMenuMac::RenderViewContextMenuMac( |
21 TabContents* web_contents, | 50 TabContents* web_contents, |
22 const ContextMenuParams& params, | 51 const ContextMenuParams& params, |
23 NSView* parent_view) | 52 NSView* parent_view) |
24 : RenderViewContextMenu(web_contents, params), | 53 : RenderViewContextMenu(web_contents, params), |
25 parent_view_(parent_view) { | 54 parent_view_(parent_view) { |
26 } | 55 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 NSString* text = base::SysUTF16ToNSString(params_.selection_text); | 133 NSString* text = base::SysUTF16ToNSString(params_.selection_text); |
105 NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName]; | 134 NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName]; |
106 // 10.5 and earlier require declareTypes before setData. | 135 // 10.5 and earlier require declareTypes before setData. |
107 // See the documentation on [NSPasteboard declareTypes]. | 136 // See the documentation on [NSPasteboard declareTypes]. |
108 NSArray* toDeclare = [NSArray arrayWithObject:NSStringPboardType]; | 137 NSArray* toDeclare = [NSArray arrayWithObject:NSStringPboardType]; |
109 [pboard declareTypes:toDeclare owner:nil]; | 138 [pboard declareTypes:toDeclare owner:nil]; |
110 BOOL ok = [pboard setString:text forType:NSStringPboardType]; | 139 BOOL ok = [pboard setString:text forType:NSStringPboardType]; |
111 if (ok) | 140 if (ok) |
112 NSPerformService(@"Look Up in Dictionary", pboard); | 141 NSPerformService(@"Look Up in Dictionary", pboard); |
113 } | 142 } |
| 143 |
| 144 void RenderViewContextMenuMac::UpdateMenuItem(int command_id, |
| 145 bool enabled, |
| 146 const string16& title) { |
| 147 NSMenuItem* item = GetMenuItemByID(&menu_model_, [menuController_ menu], |
| 148 command_id); |
| 149 if (!item) |
| 150 return; |
| 151 |
| 152 // Update the returned NSMenuItem directly so we can update it immediately. |
| 153 [item setEnabled:enabled]; |
| 154 [item setTitle:SysUTF16ToNSString(title)]; |
| 155 [[item menu] itemChanged:item]; |
| 156 } |
OLD | NEW |