OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_gtk.h" | 5 #include "chrome/browser/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/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
11 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" | 12 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" |
12 #include "content/browser/tab_contents/tab_contents.h" | 13 #include "content/browser/tab_contents/tab_contents.h" |
13 #include "webkit/glue/context_menu.h" | 14 #include "webkit/glue/context_menu.h" |
14 | 15 |
| 16 namespace { |
| 17 |
| 18 // A callback function for gtk_container_foreach(). This callback just checks |
| 19 // the menu ID and set the given user data if it is same as the specified ID. |
| 20 struct GtkWidgetAtParam { |
| 21 int index; |
| 22 GtkWidget* widget; |
| 23 }; |
| 24 |
| 25 void GtkWidgetAt(GtkWidget* widget, gpointer user_data) { |
| 26 GtkWidgetAtParam* param = reinterpret_cast<GtkWidgetAtParam*>(user_data); |
| 27 |
| 28 gpointer data = g_object_get_data(G_OBJECT(widget), "menu-id"); |
| 29 if (data && (GPOINTER_TO_INT(data) - 1) == param->index && |
| 30 GTK_IS_MENU_ITEM(widget)) { |
| 31 param->widget = widget; |
| 32 } |
| 33 } |
| 34 |
| 35 // Retrieves a GtkWidget which has the specified command_id. This function |
| 36 // traverses the given |model| in the depth-first order. When this function |
| 37 // finds an item whose command_id is the same as the given |command_id|, it |
| 38 // returns the GtkWidget associated with the item. This function emulates |
| 39 // views::MenuItemViews::GetMenuItemByID() for GTK. |
| 40 GtkWidget* GetMenuItemByID(ui::MenuModel* model, |
| 41 GtkWidget* menu, |
| 42 int command_id) { |
| 43 if (!menu) |
| 44 return NULL; |
| 45 |
| 46 for (int i = 0; i < model->GetItemCount(); ++i) { |
| 47 if (model->GetCommandIdAt(i) == command_id) { |
| 48 GtkWidgetAtParam param; |
| 49 param.index = i; |
| 50 param.widget = NULL; |
| 51 gtk_container_foreach(GTK_CONTAINER(menu), &GtkWidgetAt, ¶m); |
| 52 return param.widget; |
| 53 } |
| 54 |
| 55 ui::MenuModel* submenu = model->GetSubmenuModelAt(i); |
| 56 if (submenu) { |
| 57 GtkWidget* subitem = GetMenuItemByID( |
| 58 submenu, |
| 59 gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu)), |
| 60 command_id); |
| 61 if (subitem) |
| 62 return subitem; |
| 63 } |
| 64 } |
| 65 return NULL; |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
15 RenderViewContextMenuGtk::RenderViewContextMenuGtk( | 70 RenderViewContextMenuGtk::RenderViewContextMenuGtk( |
16 TabContents* web_contents, | 71 TabContents* web_contents, |
17 const ContextMenuParams& params, | 72 const ContextMenuParams& params, |
18 guint32 triggering_event_time) | 73 guint32 triggering_event_time) |
19 : RenderViewContextMenu(web_contents, params), | 74 : RenderViewContextMenu(web_contents, params), |
20 triggering_event_time_(triggering_event_time) { | 75 triggering_event_time_(triggering_event_time) { |
21 } | 76 } |
22 | 77 |
23 RenderViewContextMenuGtk::~RenderViewContextMenuGtk() { | 78 RenderViewContextMenuGtk::~RenderViewContextMenuGtk() { |
24 } | 79 } |
(...skipping 16 matching lines...) Expand all Loading... |
41 } | 96 } |
42 | 97 |
43 void RenderViewContextMenuGtk::Popup(const gfx::Point& point) { | 98 void RenderViewContextMenuGtk::Popup(const gfx::Point& point) { |
44 menu_gtk_->PopupAsContext(point, triggering_event_time_); | 99 menu_gtk_->PopupAsContext(point, triggering_event_time_); |
45 } | 100 } |
46 | 101 |
47 bool RenderViewContextMenuGtk::AlwaysShowIconForCmd(int command_id) const { | 102 bool RenderViewContextMenuGtk::AlwaysShowIconForCmd(int command_id) const { |
48 return command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | 103 return command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && |
49 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; | 104 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; |
50 } | 105 } |
| 106 |
| 107 void RenderViewContextMenuGtk::UpdateMenuItem(int command_id, |
| 108 bool enabled, |
| 109 const string16& title) { |
| 110 GtkWidget* item = GetMenuItemByID(&menu_model_, menu_gtk_->widget(), |
| 111 command_id); |
| 112 if (!item || !GTK_IS_MENU_ITEM(item)) |
| 113 return; |
| 114 |
| 115 // Enable (or disable) the menu item and updates its text. |
| 116 gtk_widget_set_sensitive(item, enabled); |
| 117 gtk_menu_item_set_label(GTK_MENU_ITEM(item), UTF16ToUTF8(title).c_str()); |
| 118 } |
OLD | NEW |