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 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
15 | 15 |
| 16 namespace { |
| 17 |
| 18 // Retrieves an NSMenuItem which has the specified command_id. This function |
| 19 // traverses the given |model| in the depth-first order. When this function |
| 20 // finds an item whose command_id is the same as the given |command_id|, it |
| 21 // returns the NSMenuItem associated with the item. This function emulates |
| 22 // views::MenuItemViews::GetMenuItemByID() for Mac. |
| 23 NSMenuItem* GetMenuItemByID(ui::MenuModel* model, |
| 24 NSMenu* menu, |
| 25 int command_id) { |
| 26 for (int i = 0; i < model->GetItemCount(); ++i) { |
| 27 NSMenuItem* item = [menu itemAtIndex:i]; |
| 28 if (model->GetCommandIdAt(i) == command_id) |
| 29 return item; |
| 30 |
| 31 ui::MenuModel* submenu = model->GetSubmenuModelAt(i); |
| 32 if (submenu && [item hasSubmenu]) { |
| 33 NSMenuItem* subitem = GetMenuItemByID(submenu, |
| 34 [item submenu], |
| 35 command_id); |
| 36 if (subitem) |
| 37 return subitem; |
| 38 } |
| 39 } |
| 40 return nil; |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
16 // Obj-C bridge class that is the target of all items in the context menu. | 45 // Obj-C bridge class that is the target of all items in the context menu. |
17 // Relies on the tag being set to the command id. | 46 // Relies on the tag being set to the command id. |
18 | 47 |
19 RenderViewContextMenuMac::RenderViewContextMenuMac( | 48 RenderViewContextMenuMac::RenderViewContextMenuMac( |
20 TabContents* web_contents, | 49 TabContents* web_contents, |
21 const ContextMenuParams& params, | 50 const ContextMenuParams& params, |
22 NSView* parent_view) | 51 NSView* parent_view) |
23 : RenderViewContextMenu(web_contents, params), | 52 : RenderViewContextMenu(web_contents, params), |
24 parent_view_(parent_view) { | 53 parent_view_(parent_view) { |
25 } | 54 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 NSString* text = base::SysUTF16ToNSString(params_.selection_text); | 125 NSString* text = base::SysUTF16ToNSString(params_.selection_text); |
97 NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName]; | 126 NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName]; |
98 // 10.5 and earlier require declareTypes before setData. | 127 // 10.5 and earlier require declareTypes before setData. |
99 // See the documentation on [NSPasteboard declareTypes]. | 128 // See the documentation on [NSPasteboard declareTypes]. |
100 NSArray* toDeclare = [NSArray arrayWithObject:NSStringPboardType]; | 129 NSArray* toDeclare = [NSArray arrayWithObject:NSStringPboardType]; |
101 [pboard declareTypes:toDeclare owner:nil]; | 130 [pboard declareTypes:toDeclare owner:nil]; |
102 BOOL ok = [pboard setString:text forType:NSStringPboardType]; | 131 BOOL ok = [pboard setString:text forType:NSStringPboardType]; |
103 if (ok) | 132 if (ok) |
104 NSPerformService(@"Look Up in Dictionary", pboard); | 133 NSPerformService(@"Look Up in Dictionary", pboard); |
105 } | 134 } |
| 135 |
| 136 void RenderViewContextMenuMac::UpdateMenuItem(int command_id, |
| 137 bool enabled, |
| 138 const string16& title) { |
| 139 NSMenuItem* item = GetMenuItemByID(&menu_model_, [menuController_ menu], |
| 140 command_id); |
| 141 if (!item) |
| 142 return; |
| 143 |
| 144 // Update the returned NSMenuItem directly so we can update it immediately. |
| 145 [item setEnabled:enabled]; |
| 146 [item setTitle:SysUTF16ToNSString(title)]; |
| 147 [[item menu] itemChanged:item]; |
| 148 } |
OLD | NEW |