OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/ui/gtk/tab_contents/render_view_context_menu_gtk.h" | 5 #include "chrome/browser/ui/gtk/tab_contents/render_view_context_menu_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/ui/gtk/gtk_util.h" | 12 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 13 #include "content/public/browser/render_view_host.h" |
13 #include "content/public/browser/render_widget_host_view.h" | 14 #include "content/public/browser/render_widget_host_view.h" |
14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
15 #include "content/public/common/context_menu_params.h" | 16 #include "content/public/common/context_menu_params.h" |
16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
17 #include "ui/base/accelerators/menu_label_accelerator_util_linux.h" | 18 #include "ui/base/accelerators/menu_label_accelerator_util_linux.h" |
18 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
19 | 20 |
20 using content::WebContents; | 21 using content::WebContents; |
21 | 22 |
22 namespace { | 23 namespace { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 71 } |
71 return NULL; | 72 return NULL; |
72 } | 73 } |
73 | 74 |
74 } // namespace | 75 } // namespace |
75 | 76 |
76 RenderViewContextMenuGtk::RenderViewContextMenuGtk( | 77 RenderViewContextMenuGtk::RenderViewContextMenuGtk( |
77 WebContents* web_contents, | 78 WebContents* web_contents, |
78 const content::ContextMenuParams& params, | 79 const content::ContextMenuParams& params, |
79 content::RenderWidgetHostView* view) | 80 content::RenderWidgetHostView* view) |
80 : RenderViewContextMenu(web_contents, params) { | 81 : RenderViewContextMenu(web_contents, params), |
| 82 bidi_submenu_model_(this) { |
81 GdkEventButton* event = view->GetLastMouseDown(); | 83 GdkEventButton* event = view->GetLastMouseDown(); |
82 triggering_event_time_ = event ? event->time : GDK_CURRENT_TIME; | 84 triggering_event_time_ = event ? event->time : GDK_CURRENT_TIME; |
83 } | 85 } |
84 | 86 |
85 RenderViewContextMenuGtk::~RenderViewContextMenuGtk() { | 87 RenderViewContextMenuGtk::~RenderViewContextMenuGtk() { |
86 } | 88 } |
87 | 89 |
88 void RenderViewContextMenuGtk::PlatformInit() { | 90 void RenderViewContextMenuGtk::PlatformInit() { |
89 menu_gtk_.reset(new MenuGtk(this, &menu_model_)); | 91 menu_gtk_.reset(new MenuGtk(this, &menu_model_)); |
90 | 92 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 134 |
133 void RenderViewContextMenuGtk::Popup(const gfx::Point& point) { | 135 void RenderViewContextMenuGtk::Popup(const gfx::Point& point) { |
134 menu_gtk_->PopupAsContext(point, triggering_event_time_); | 136 menu_gtk_->PopupAsContext(point, triggering_event_time_); |
135 } | 137 } |
136 | 138 |
137 bool RenderViewContextMenuGtk::AlwaysShowIconForCmd(int command_id) const { | 139 bool RenderViewContextMenuGtk::AlwaysShowIconForCmd(int command_id) const { |
138 return command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | 140 return command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && |
139 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; | 141 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; |
140 } | 142 } |
141 | 143 |
| 144 void RenderViewContextMenuGtk::ExecuteCommand(int command_id, int event_flags) { |
| 145 switch (command_id) { |
| 146 case IDC_WRITING_DIRECTION_DEFAULT: |
| 147 // WebKit's current behavior is for this menu item to always be disabled. |
| 148 NOTREACHED(); |
| 149 break; |
| 150 |
| 151 case IDC_WRITING_DIRECTION_RTL: |
| 152 case IDC_WRITING_DIRECTION_LTR: { |
| 153 content::RenderViewHost* view_host = GetRenderViewHost(); |
| 154 blink::WebTextDirection dir = blink::WebTextDirectionLeftToRight; |
| 155 if (command_id == IDC_WRITING_DIRECTION_RTL) |
| 156 dir = blink::WebTextDirectionRightToLeft; |
| 157 view_host->UpdateTextDirection(dir); |
| 158 view_host->NotifyTextDirection(); |
| 159 break; |
| 160 } |
| 161 |
| 162 default: |
| 163 RenderViewContextMenu::ExecuteCommand(command_id, event_flags); |
| 164 break; |
| 165 } |
| 166 } |
| 167 |
| 168 bool RenderViewContextMenuGtk::IsCommandIdChecked(int command_id) const { |
| 169 switch (command_id) { |
| 170 case IDC_WRITING_DIRECTION_DEFAULT: |
| 171 return params_.writing_direction_default & |
| 172 blink::WebContextMenuData::CheckableMenuItemChecked; |
| 173 case IDC_WRITING_DIRECTION_RTL: |
| 174 return params_.writing_direction_right_to_left & |
| 175 blink::WebContextMenuData::CheckableMenuItemChecked; |
| 176 case IDC_WRITING_DIRECTION_LTR: |
| 177 return params_.writing_direction_left_to_right & |
| 178 blink::WebContextMenuData::CheckableMenuItemChecked; |
| 179 |
| 180 default: |
| 181 return RenderViewContextMenu::IsCommandIdChecked(command_id); |
| 182 } |
| 183 } |
| 184 |
| 185 bool RenderViewContextMenuGtk::IsCommandIdEnabled(int command_id) const { |
| 186 switch (command_id) { |
| 187 case IDC_WRITING_DIRECTION_MENU: |
| 188 return true; |
| 189 case IDC_WRITING_DIRECTION_DEFAULT: // Provided to match OS defaults. |
| 190 return params_.writing_direction_default & |
| 191 blink::WebContextMenuData::CheckableMenuItemEnabled; |
| 192 case IDC_WRITING_DIRECTION_RTL: |
| 193 return params_.writing_direction_right_to_left & |
| 194 blink::WebContextMenuData::CheckableMenuItemEnabled; |
| 195 case IDC_WRITING_DIRECTION_LTR: |
| 196 return params_.writing_direction_left_to_right & |
| 197 blink::WebContextMenuData::CheckableMenuItemEnabled; |
| 198 |
| 199 default: |
| 200 return RenderViewContextMenu::IsCommandIdEnabled(command_id); |
| 201 } |
| 202 } |
| 203 |
| 204 void RenderViewContextMenuGtk::AppendPlatformEditableItems() { |
| 205 // OS X and Linux provide a contextual menu to set writing direction for BiDi |
| 206 // languages. |
| 207 // This functionality is exposed as a keyboard shortcut on Windows. |
| 208 AppendBidiSubMenu(); |
| 209 } |
| 210 |
| 211 void RenderViewContextMenuGtk::AppendBidiSubMenu() { |
| 212 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_DEFAULT, |
| 213 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT)); |
| 214 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_LTR, |
| 215 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR)); |
| 216 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_RTL, |
| 217 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL)); |
| 218 |
| 219 menu_model_.AddSubMenu( |
| 220 IDC_WRITING_DIRECTION_MENU, |
| 221 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU), |
| 222 &bidi_submenu_model_); |
| 223 } |
| 224 |
142 void RenderViewContextMenuGtk::UpdateMenuItem(int command_id, | 225 void RenderViewContextMenuGtk::UpdateMenuItem(int command_id, |
143 bool enabled, | 226 bool enabled, |
144 bool hidden, | 227 bool hidden, |
145 const string16& title) { | 228 const string16& title) { |
146 GtkWidget* item = GetMenuItemByID(&menu_model_, menu_gtk_->widget(), | 229 GtkWidget* item = GetMenuItemByID(&menu_model_, menu_gtk_->widget(), |
147 command_id); | 230 command_id); |
148 if (!item || !GTK_IS_MENU_ITEM(item)) | 231 if (!item || !GTK_IS_MENU_ITEM(item)) |
149 return; | 232 return; |
150 | 233 |
151 // Enable (or disable) the menu item and updates its text. | 234 // Enable (or disable) the menu item and updates its text. |
152 gtk_widget_set_sensitive(item, enabled); | 235 gtk_widget_set_sensitive(item, enabled); |
153 if (hidden) | 236 if (hidden) |
154 gtk_widget_hide(item); | 237 gtk_widget_hide(item); |
155 else | 238 else |
156 gtk_widget_show(item); | 239 gtk_widget_show(item); |
157 gtk_menu_item_set_label(GTK_MENU_ITEM(item), UTF16ToUTF8(title).c_str()); | 240 gtk_menu_item_set_label(GTK_MENU_ITEM(item), UTF16ToUTF8(title).c_str()); |
158 } | 241 } |
OLD | NEW |