Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / tab_contents / render_view_context_menu_mac.mm
blob47c4516c8103f92ae6086e2f46d2c7ae9878d1d3
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;
20 namespace {
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,
28                             NSMenu* menu,
29                             int command_id) {
30   for (int i = 0; i < model->GetItemCount(); ++i) {
31     NSMenuItem* item = [menu itemAtIndex:i];
32     if (model->GetCommandIdAt(i) == command_id)
33       return item;
35     ui::MenuModel* submenu = model->GetSubmenuModelAt(i);
36     if (submenu && [item hasSubmenu]) {
37       NSMenuItem* subitem = GetMenuItemByID(submenu,
38                                             [item submenu],
39                                             command_id);
40       if (subitem)
41         return subitem;
42     }
43   }
44   return nil;
47 }  // namespace
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,
55     NSView* parent_view)
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() {
66   InitPlatformMenu();
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
78                                            location:position
79                                       modifierFlags:NSRightMouseDownMask
80                                           timestamp:eventTime
81                                        windowNumber:[window windowNumber]
82                                             context:nil
83                                         eventNumber:0
84                                          clickCount:1
85                                            pressure:1.0];
87   {
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
96     // be done manually.
97     base::mac::ScopedSendingEvent sendingEventScoper;
99     // Show the menu.
100     [NSMenu popUpContextMenu:[menu_controller_ menu]
101                    withEvent:clickEvent
102                      forView:parent_view_];
103   }
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();
114       break;
116     case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING:
117       StartSpeaking();
118       break;
120     case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING:
121       StopSpeaking();
122       break;
124     case IDC_WRITING_DIRECTION_DEFAULT:
125       // WebKit's current behavior is for this menu item to always be disabled.
126       NOTREACHED();
127       break;
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();
137       break;
138     }
140     default:
141       RenderViewContextMenu::ExecuteCommand(command_id, event_flags);
142       break;
143   }
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;
158     default:
159       return RenderViewContextMenu::IsCommandIdChecked(command_id);
160   }
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
167       // appropriate.
168       return true;
170     case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING:
171       // This is OK because the menu is not shown when it isn't
172       // appropriate.
173       return true;
175     case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: {
176       content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
177       return view && view->IsSpeaking();
178     }
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;
190     default:
191       return RenderViewContextMenu::IsCommandIdEnabled(command_id);
192   }
195 bool RenderViewContextMenuMac::GetAcceleratorForCommandId(
196     int command_id,
197     ui::Accelerator* accelerator) {
198   return false;
201 void RenderViewContextMenuMac::AppendPlatformEditableItems() {
202   // OS X provides a contextual menu to set writing direction for BiDi
203   // languages.
204   // This functionality is exposed as a keyboard shortcut on Windows & Linux.
205   AppendBidiSubMenu();
208 void RenderViewContextMenuMac::InitPlatformMenu() {
209   bool has_selection = !params_.selection_text.empty();
211   if (has_selection) {
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_);
229     }
230   }
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();
249   if (view)
250     view->ShowDefinitionForSelection();
253 void RenderViewContextMenuMac::StartSpeaking() {
254   content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
255   if (view)
256     view->SpeakSelection();
259 void RenderViewContextMenuMac::StopSpeaking() {
260   content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
261   if (view)
262     view->StopSpeaking();
265 void RenderViewContextMenuMac::UpdateMenuItem(int command_id,
266                                               bool enabled,
267                                               bool hidden,
268                                               const base::string16& title) {
269   NSMenuItem* item = GetMenuItemByID(&menu_model_, [menu_controller_ menu],
270                                      command_id);
271   if (!item)
272     return;
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];