OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // show the menu | 61 // show the menu |
62 [NSMenu popUpContextMenu:menu_ | 62 [NSMenu popUpContextMenu:menu_ |
63 withEvent:[NSApp currentEvent] | 63 withEvent:[NSApp currentEvent] |
64 forView:parent_view_]; | 64 forView:parent_view_]; |
65 } | 65 } |
66 | 66 |
67 // Do things like remove the windows accelerators. | 67 // Do things like remove the windows accelerators. |
68 // TODO(pinkerton): Do we want to do anything like make a maximum string width | 68 // TODO(pinkerton): Do we want to do anything like make a maximum string width |
69 // and middle-truncate? | 69 // and middle-truncate? |
70 NSString* RenderViewContextMenuMac::PrepareLabelForDisplay( | 70 NSString* RenderViewContextMenuMac::PrepareLabelForDisplay( |
71 const std::wstring& label) { | 71 const string16& label) { |
72 // Strip out any "&"'s that are windows accelerators and we don't use. | 72 // Strip out any "&"'s that are windows accelerators and we don't use. |
73 NSMutableString* title = | 73 NSMutableString* title = |
74 [NSMutableString stringWithString:base::SysWideToNSString(label)]; | 74 [NSMutableString stringWithString:base::SysUTF16ToNSString(label)]; |
75 DCHECK(title); | 75 DCHECK(title); |
76 NSRange range = NSMakeRange(0, [title length]); | 76 NSRange range = NSMakeRange(0, [title length]); |
77 [title replaceOccurrencesOfString:@"&" withString:@"" options:0 range:range]; | 77 [title replaceOccurrencesOfString:@"&" withString:@"" options:0 range:range]; |
78 return title ? title : @""; | 78 return title ? title : @""; |
79 } | 79 } |
80 | 80 |
81 void RenderViewContextMenuMac::AppendMenuItem(int command_id) { | 81 void RenderViewContextMenuMac::AppendMenuItem(int command_id) { |
82 AppendMenuItem(command_id, l10n_util::GetString(command_id)); | 82 AppendMenuItem(command_id, l10n_util::GetStringUTF16(command_id)); |
83 } | 83 } |
84 | 84 |
85 void RenderViewContextMenuMac::AppendMenuItem(int command_id, | 85 void RenderViewContextMenuMac::AppendMenuItem(int command_id, |
86 const std::wstring& label) { | 86 const string16& label) { |
87 // Create the item and set its target/action to |target_| with the command | 87 // Create the item and set its target/action to |target_| with the command |
88 // as |command_id|. Then add it to the menu at the end. | 88 // as |command_id|. Then add it to the menu at the end. |
89 NSMenuItem* item = | 89 NSMenuItem* item = |
90 [[[NSMenuItem alloc] initWithTitle:PrepareLabelForDisplay(label) | 90 [[[NSMenuItem alloc] initWithTitle:PrepareLabelForDisplay(label) |
91 action:@selector(itemSelected:) | 91 action:@selector(itemSelected:) |
92 keyEquivalent:@""] autorelease]; | 92 keyEquivalent:@""] autorelease]; |
93 [item setTag:command_id]; | 93 [item setTag:command_id]; |
94 [item setTarget:target_]; | 94 [item setTarget:target_]; |
95 [item setEnabled:IsItemCommandEnabled(command_id) ? YES : NO]; | 95 [item setEnabled:IsItemCommandEnabled(command_id) ? YES : NO]; |
96 [insert_menu_ addItem:item]; | 96 [insert_menu_ addItem:item]; |
97 } | 97 } |
98 | 98 |
99 void RenderViewContextMenuMac::AppendRadioMenuItem(int id, | 99 void RenderViewContextMenuMac::AppendRadioMenuItem(int id, |
100 const std::wstring& label) { | 100 const string16& label) { |
101 NOTIMPLEMENTED(); | 101 NOTIMPLEMENTED(); |
102 } | 102 } |
103 | 103 |
104 void RenderViewContextMenuMac::AppendCheckboxMenuItem(int id, | 104 void RenderViewContextMenuMac::AppendCheckboxMenuItem(int id, |
105 const std::wstring& label) { | 105 const string16& label) { |
106 NOTIMPLEMENTED(); | 106 NOTIMPLEMENTED(); |
107 } | 107 } |
108 | 108 |
109 void RenderViewContextMenuMac::AppendSeparator() { | 109 void RenderViewContextMenuMac::AppendSeparator() { |
110 NSMenuItem* separator = [NSMenuItem separatorItem]; | 110 NSMenuItem* separator = [NSMenuItem separatorItem]; |
111 [insert_menu_ addItem:separator]; | 111 [insert_menu_ addItem:separator]; |
112 } | 112 } |
113 | 113 |
114 void RenderViewContextMenuMac::StartSubMenu(int command_id, | 114 void RenderViewContextMenuMac::StartSubMenu(int command_id, |
115 const std::wstring& label) { | 115 const string16& label) { |
116 // I'm not a fan of this kind of API, but the other platforms have similar | 116 // I'm not a fan of this kind of API, but the other platforms have similar |
117 // guards so at least we know everyone will break together if someone | 117 // guards so at least we know everyone will break together if someone |
118 // tries to mis-use the API. | 118 // tries to mis-use the API. |
119 if (insert_menu_ != menu_) { | 119 if (insert_menu_ != menu_) { |
120 NOTREACHED(); | 120 NOTREACHED(); |
121 return; | 121 return; |
122 } | 122 } |
123 | 123 |
124 // We don't need to retain the submenu as the context menu already does, but | 124 // We don't need to retain the submenu as the context menu already does, but |
125 // we switch the "insert menu" so subsequent items are added to the submenu | 125 // we switch the "insert menu" so subsequent items are added to the submenu |
126 // and not the main menu. This happens until someone calls FinishSubMenu(). | 126 // and not the main menu. This happens until someone calls FinishSubMenu(). |
127 NSMenuItem* submenu_item = | 127 NSMenuItem* submenu_item = |
128 [[[NSMenuItem alloc] initWithTitle:PrepareLabelForDisplay(label) | 128 [[[NSMenuItem alloc] initWithTitle:PrepareLabelForDisplay(label) |
129 action:nil | 129 action:nil |
130 keyEquivalent:@""] autorelease]; | 130 keyEquivalent:@""] autorelease]; |
131 insert_menu_ = [[[NSMenu alloc] init] autorelease]; | 131 insert_menu_ = [[[NSMenu alloc] init] autorelease]; |
132 [submenu_item setSubmenu:insert_menu_]; | 132 [submenu_item setSubmenu:insert_menu_]; |
133 [menu_ addItem:submenu_item]; | 133 [menu_ addItem:submenu_item]; |
134 } | 134 } |
135 | 135 |
136 void RenderViewContextMenuMac::FinishSubMenu() { | 136 void RenderViewContextMenuMac::FinishSubMenu() { |
137 // Set the "insert menu" back to the main menu so that subsequently added | 137 // Set the "insert menu" back to the main menu so that subsequently added |
138 // items get added to the main context menu. | 138 // items get added to the main context menu. |
139 DCHECK(insert_menu_ != menu_); | 139 DCHECK(insert_menu_ != menu_); |
140 insert_menu_ = menu_; | 140 insert_menu_ = menu_; |
141 } | 141 } |
OLD | NEW |