409b7e05b4a5f44aa8387c665eb98c5d1b5856c2
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / tab_contents / render_view_context_menu_gtk.cc
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.
4
5 #include "chrome/browser/ui/gtk/tab_contents/render_view_context_menu_gtk.h"
6
7 #include <gtk/gtk.h>
8
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/ui/gtk/gtk_util.h"
13 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/render_widget_host_view.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/context_menu_params.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/accelerators/menu_label_accelerator_util_linux.h"
19 #include "ui/base/l10n/l10n_util.h"
20
21 using content::WebContents;
22
23 namespace {
24
25 // A callback function for gtk_container_foreach(). This callback just checks
26 // the menu ID and set the given user data if it is same as the specified ID.
27 struct GtkWidgetAtParam {
28   int index;
29   GtkWidget* widget;
30 };
31
32 void GtkWidgetAt(GtkWidget* widget, gpointer user_data) {
33   GtkWidgetAtParam* param = reinterpret_cast<GtkWidgetAtParam*>(user_data);
34
35   gpointer data = g_object_get_data(G_OBJECT(widget), "menu-id");
36   if (data && (GPOINTER_TO_INT(data) - 1) == param->index &&
37       GTK_IS_MENU_ITEM(widget)) {
38     param->widget = widget;
39   }
40 }
41
42 // Retrieves a GtkWidget which has the specified command_id. This function
43 // traverses the given |model| in the depth-first order. When this function
44 // finds an item whose command_id is the same as the given |command_id|, it
45 // returns the GtkWidget associated with the item. This function emulates
46 // views::MenuItemViews::GetMenuItemByID() for GTK.
47 GtkWidget* GetMenuItemByID(ui::MenuModel* model,
48                            GtkWidget* menu,
49                            int command_id) {
50   if (!menu)
51     return NULL;
52
53   for (int i = 0; i < model->GetItemCount(); ++i) {
54     if (model->GetCommandIdAt(i) == command_id) {
55       GtkWidgetAtParam param;
56       param.index = i;
57       param.widget = NULL;
58       gtk_container_foreach(GTK_CONTAINER(menu), &GtkWidgetAt, &param);
59       return param.widget;
60     }
61
62     ui::MenuModel* submenu = model->GetSubmenuModelAt(i);
63     if (submenu) {
64       GtkWidget* subitem = GetMenuItemByID(
65           submenu,
66           gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu)),
67           command_id);
68       if (subitem)
69         return subitem;
70     }
71   }
72   return NULL;
73 }
74
75 }  // namespace
76
77 RenderViewContextMenuGtk::RenderViewContextMenuGtk(
78     content::RenderFrameHost* render_frame_host,
79     const content::ContextMenuParams& params,
80     content::RenderWidgetHostView* view)
81     : RenderViewContextMenu(render_frame_host, params),
82       bidi_submenu_model_(this) {
83   GdkEventButton* event = view->GetLastMouseDown();
84   triggering_event_time_ = event ? event->time : GDK_CURRENT_TIME;
85 }
86
87 RenderViewContextMenuGtk::~RenderViewContextMenuGtk() {
88 }
89
90 void RenderViewContextMenuGtk::PlatformInit() {
91   menu_gtk_.reset(new MenuGtk(this, &menu_model_));
92
93   if (params_.is_editable) {
94     content::RenderWidgetHostView* rwhv =
95         source_web_contents_->GetRenderWidgetHostView();
96     if (rwhv) {
97       MenuGtk* menu = menu_gtk_.get();
98       gboolean show_input_method_menu = TRUE;
99
100       g_object_get(
101           gtk_widget_get_settings(GTK_WIDGET(rwhv->GetNativeView())),
102           "gtk-show-input-method-menu", &show_input_method_menu, NULL);
103       if (!show_input_method_menu)
104         return;
105
106       std::string label = ui::ConvertAcceleratorsFromWindowsStyle(
107           l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_INPUT_METHODS_MENU));
108       GtkWidget* menuitem = gtk_menu_item_new_with_mnemonic(label.c_str());
109       GtkWidget* submenu = rwhv->BuildInputMethodsGtkMenu();
110       gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
111       int inspect_element_index =
112           menu_model_.GetIndexOfCommandId(IDC_CONTENT_CONTEXT_INSPECTELEMENT);
113       if (inspect_element_index != -1) {
114         menu->InsertSeparator(inspect_element_index - 1);
115         menu->InsertMenuItem(IDC_INPUT_METHODS_MENU, menuitem,
116             inspect_element_index);
117       } else {
118         menu->AppendSeparator();
119         menu->AppendMenuItem(IDC_INPUT_METHODS_MENU, menuitem);
120       }
121     }
122   }
123 }
124
125 void RenderViewContextMenuGtk::PlatformCancel() {
126   menu_gtk_->Cancel();
127 }
128
129 bool RenderViewContextMenuGtk::GetAcceleratorForCommandId(
130     int command_id,
131     ui::Accelerator* accelerator) {
132   return false;
133 }
134
135 void RenderViewContextMenuGtk::Popup(const gfx::Point& point) {
136   menu_gtk_->PopupAsContext(point, triggering_event_time_);
137 }
138
139 bool RenderViewContextMenuGtk::AlwaysShowIconForCmd(int command_id) const {
140   return command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
141       command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST;
142 }
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
225 void RenderViewContextMenuGtk::UpdateMenuItem(int command_id,
226                                               bool enabled,
227                                               bool hidden,
228                                               const base::string16& title) {
229   GtkWidget* item = GetMenuItemByID(&menu_model_, menu_gtk_->widget(),
230                                     command_id);
231   if (!item || !GTK_IS_MENU_ITEM(item))
232     return;
233
234   // Enable (or disable) the menu item and updates its text.
235   gtk_widget_set_sensitive(item, enabled);
236   if (hidden)
237     gtk_widget_hide(item);
238   else
239     gtk_widget_show(item);
240   gtk_menu_item_set_label(GTK_MENU_ITEM(item),
241                           base::UTF16ToUTF8(title).c_str());
242 }