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 #include "chrome/browser/ui/cocoa/tab_contents/render_view_context_menu_mac.h"
7 #include "base/compiler_specific.h"
8 #import "base/mac/scoped_sending_event.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "grit/generated_resources.h"
15 #import "ui/base/cocoa/menu_controller.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using content::WebContents;
22 // Retrieves an NSMenuItem which has the specified command_id. This function
23 // traverses the given |model| in the depth-first order. When this function
24 // finds an item whose command_id is the same as the given |command_id|, it
25 // returns the NSMenuItem associated with the item. This function emulates
26 // views::MenuItemViews::GetMenuItemByID() for Mac.
27 NSMenuItem* GetMenuItemByID(ui::MenuModel* model,
30 for (int i = 0; i < model->GetItemCount(); ++i) {
31 NSMenuItem* item = [menu itemAtIndex:i];
32 if (model->GetCommandIdAt(i) == command_id)
35 ui::MenuModel* submenu = model->GetSubmenuModelAt(i);
36 if (submenu && [item hasSubmenu]) {
37 NSMenuItem* subitem = GetMenuItemByID(submenu,
49 // Obj-C bridge class that is the target of all items in the context menu.
50 // Relies on the tag being set to the command id.
52 RenderViewContextMenuMac::RenderViewContextMenuMac(
53 WebContents* web_contents,
54 const content::ContextMenuParams& params,
56 : RenderViewContextMenu(web_contents, params),
57 speech_submenu_model_(this),
58 bidi_submenu_model_(this),
59 parent_view_(parent_view) {
62 RenderViewContextMenuMac::~RenderViewContextMenuMac() {
65 void RenderViewContextMenuMac::PlatformInit() {
67 menu_controller_.reset(
68 [[MenuController alloc] initWithModel:&menu_model_
69 useWithPopUpButtonCell:NO]);
71 // Synthesize an event for the click, as there is no certainty that
72 // [NSApp currentEvent] will return a valid event.
73 NSEvent* currentEvent = [NSApp currentEvent];
74 NSWindow* window = [parent_view_ window];
75 NSPoint position = [window mouseLocationOutsideOfEventStream];
76 NSTimeInterval eventTime = [currentEvent timestamp];
77 NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
79 modifierFlags:NSRightMouseDownMask
81 windowNumber:[window windowNumber]
88 // Make sure events can be pumped while the menu is up.
89 base::MessageLoop::ScopedNestableTaskAllower allow(
90 base::MessageLoop::current());
92 // One of the events that could be pumped is |window.close()|.
93 // User-initiated event-tracking loops protect against this by
94 // setting flags in -[CrApplication sendEvent:], but since
95 // web-content menus are initiated by IPC message the setup has to
97 base::mac::ScopedSendingEvent sendingEventScoper;
100 [NSMenu popUpContextMenu:[menu_controller_ menu]
102 forView:parent_view_];
106 void RenderViewContextMenuMac::PlatformCancel() {
107 [menu_controller_ cancel];
110 void RenderViewContextMenuMac::ExecuteCommand(int command_id, int event_flags) {
111 switch (command_id) {
112 case IDC_CONTENT_CONTEXT_LOOK_UP_IN_DICTIONARY:
113 LookUpInDictionary();
116 case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING:
120 case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING:
124 case IDC_WRITING_DIRECTION_DEFAULT:
125 // WebKit's current behavior is for this menu item to always be disabled.
129 case IDC_WRITING_DIRECTION_RTL:
130 case IDC_WRITING_DIRECTION_LTR: {
131 content::RenderViewHost* view_host = GetRenderViewHost();
132 blink::WebTextDirection dir = blink::WebTextDirectionLeftToRight;
133 if (command_id == IDC_WRITING_DIRECTION_RTL)
134 dir = blink::WebTextDirectionRightToLeft;
135 view_host->UpdateTextDirection(dir);
136 view_host->NotifyTextDirection();
141 RenderViewContextMenu::ExecuteCommand(command_id, event_flags);
146 bool RenderViewContextMenuMac::IsCommandIdChecked(int command_id) const {
147 switch (command_id) {
148 case IDC_WRITING_DIRECTION_DEFAULT:
149 return params_.writing_direction_default &
150 blink::WebContextMenuData::CheckableMenuItemChecked;
151 case IDC_WRITING_DIRECTION_RTL:
152 return params_.writing_direction_right_to_left &
153 blink::WebContextMenuData::CheckableMenuItemChecked;
154 case IDC_WRITING_DIRECTION_LTR:
155 return params_.writing_direction_left_to_right &
156 blink::WebContextMenuData::CheckableMenuItemChecked;
159 return RenderViewContextMenu::IsCommandIdChecked(command_id);
163 bool RenderViewContextMenuMac::IsCommandIdEnabled(int command_id) const {
164 switch (command_id) {
165 case IDC_CONTENT_CONTEXT_LOOK_UP_IN_DICTIONARY:
166 // This is OK because the menu is not shown when it isn't
170 case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING:
171 // This is OK because the menu is not shown when it isn't
175 case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: {
176 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
177 return view && view->IsSpeaking();
180 case IDC_WRITING_DIRECTION_DEFAULT: // Provided to match OS defaults.
181 return params_.writing_direction_default &
182 blink::WebContextMenuData::CheckableMenuItemEnabled;
183 case IDC_WRITING_DIRECTION_RTL:
184 return params_.writing_direction_right_to_left &
185 blink::WebContextMenuData::CheckableMenuItemEnabled;
186 case IDC_WRITING_DIRECTION_LTR:
187 return params_.writing_direction_left_to_right &
188 blink::WebContextMenuData::CheckableMenuItemEnabled;
191 return RenderViewContextMenu::IsCommandIdEnabled(command_id);
195 bool RenderViewContextMenuMac::GetAcceleratorForCommandId(
197 ui::Accelerator* accelerator) {
201 void RenderViewContextMenuMac::AppendPlatformEditableItems() {
202 // OS X provides a contextual menu to set writing direction for BiDi
204 // This functionality is exposed as a keyboard shortcut on Windows & Linux.
208 void RenderViewContextMenuMac::InitPlatformMenu() {
209 bool has_selection = !params_.selection_text.empty();
212 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
213 menu_model_.AddItemWithStringId(
214 IDC_CONTENT_CONTEXT_LOOK_UP_IN_DICTIONARY,
215 IDS_CONTENT_CONTEXT_LOOK_UP_IN_DICTIONARY);
217 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
218 if (view && view->SupportsSpeech()) {
219 speech_submenu_model_.AddItemWithStringId(
220 IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING,
221 IDS_SPEECH_START_SPEAKING_MAC);
222 speech_submenu_model_.AddItemWithStringId(
223 IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING,
224 IDS_SPEECH_STOP_SPEAKING_MAC);
225 menu_model_.AddSubMenu(
226 IDC_CONTENT_CONTEXT_SPEECH_MENU,
227 l10n_util::GetStringUTF16(IDS_SPEECH_MAC),
228 &speech_submenu_model_);
233 void RenderViewContextMenuMac::AppendBidiSubMenu() {
234 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_DEFAULT,
235 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT));
236 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_LTR,
237 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR));
238 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_RTL,
239 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL));
241 menu_model_.AddSubMenu(
242 IDC_WRITING_DIRECTION_MENU,
243 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU),
244 &bidi_submenu_model_);
247 void RenderViewContextMenuMac::LookUpInDictionary() {
248 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
250 view->ShowDefinitionForSelection();
253 void RenderViewContextMenuMac::StartSpeaking() {
254 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
256 view->SpeakSelection();
259 void RenderViewContextMenuMac::StopSpeaking() {
260 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
262 view->StopSpeaking();
265 void RenderViewContextMenuMac::UpdateMenuItem(int command_id,
268 const base::string16& title) {
269 NSMenuItem* item = GetMenuItemByID(&menu_model_, [menu_controller_ menu],
274 // Update the returned NSMenuItem directly so we can update it immediately.
275 [item setEnabled:enabled];
276 [item setTitle:SysUTF16ToNSString(title)];
277 [item setHidden:hidden];
278 [[item menu] itemChanged:item];