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/ui/gtk/gtk_util.h" | 12 #include "chrome/browser/ui/gtk/gtk_util.h" |
12 #include "content/browser/renderer_host/render_widget_host_view_gtk.h" | 13 #include "content/browser/renderer_host/render_widget_host_view_gtk.h" |
13 #include "content/browser/tab_contents/tab_contents.h" | 14 #include "content/browser/tab_contents/tab_contents.h" |
14 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
15 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
16 #include "ui/gfx/gtk_util.h" | 17 #include "ui/gfx/gtk_util.h" |
17 #include "webkit/glue/context_menu.h" | 18 #include "webkit/glue/context_menu.h" |
18 | 19 |
| 20 namespace { |
| 21 |
| 22 // A callback function for gtk_container_foreach(). This callback just checks |
| 23 // the menu ID and set the given user data if it is same as the specified ID. |
| 24 struct GtkWidgetAtParam { |
| 25 int index; |
| 26 GtkWidget* widget; |
| 27 }; |
| 28 |
| 29 void GtkWidgetAt(GtkWidget* widget, gpointer user_data) { |
| 30 GtkWidgetAtParam* param = reinterpret_cast<GtkWidgetAtParam*>(user_data); |
| 31 |
| 32 gpointer data = g_object_get_data(G_OBJECT(widget), "menu-id"); |
| 33 if (data && (GPOINTER_TO_INT(data) - 1) == param->index && |
| 34 GTK_IS_MENU_ITEM(widget)) { |
| 35 param->widget = widget; |
| 36 } |
| 37 } |
| 38 |
| 39 // Retrieves a GtkWidget which has the specified command_id. This function |
| 40 // traverses the given |model| in the depth-first order. When this function |
| 41 // finds an item whose command_id is the same as the given |command_id|, it |
| 42 // returns the GtkWidget associated with the item. This function emulates |
| 43 // views::MenuItemViews::GetMenuItemByID() for GTK. |
| 44 GtkWidget* GetMenuItemByID(ui::MenuModel* model, |
| 45 GtkWidget* menu, |
| 46 int command_id) { |
| 47 if (!menu) |
| 48 return NULL; |
| 49 |
| 50 for (int i = 0; i < model->GetItemCount(); ++i) { |
| 51 if (model->GetCommandIdAt(i) == command_id) { |
| 52 GtkWidgetAtParam param; |
| 53 param.index = i; |
| 54 param.widget = NULL; |
| 55 gtk_container_foreach(GTK_CONTAINER(menu), &GtkWidgetAt, ¶m); |
| 56 return param.widget; |
| 57 } |
| 58 |
| 59 ui::MenuModel* submenu = model->GetSubmenuModelAt(i); |
| 60 if (submenu) { |
| 61 GtkWidget* subitem = GetMenuItemByID( |
| 62 submenu, |
| 63 gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu)), |
| 64 command_id); |
| 65 if (subitem) |
| 66 return subitem; |
| 67 } |
| 68 } |
| 69 return NULL; |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
19 RenderViewContextMenuGtk::RenderViewContextMenuGtk( | 74 RenderViewContextMenuGtk::RenderViewContextMenuGtk( |
20 TabContents* web_contents, | 75 TabContents* web_contents, |
21 const ContextMenuParams& params, | 76 const ContextMenuParams& params, |
22 guint32 triggering_event_time) | 77 guint32 triggering_event_time) |
23 : RenderViewContextMenu(web_contents, params), | 78 : RenderViewContextMenu(web_contents, params), |
24 triggering_event_time_(triggering_event_time) { | 79 triggering_event_time_(triggering_event_time) { |
25 } | 80 } |
26 | 81 |
27 RenderViewContextMenuGtk::~RenderViewContextMenuGtk() { | 82 RenderViewContextMenuGtk::~RenderViewContextMenuGtk() { |
28 } | 83 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 } | 118 } |
64 | 119 |
65 void RenderViewContextMenuGtk::Popup(const gfx::Point& point) { | 120 void RenderViewContextMenuGtk::Popup(const gfx::Point& point) { |
66 menu_gtk_->PopupAsContext(point, triggering_event_time_); | 121 menu_gtk_->PopupAsContext(point, triggering_event_time_); |
67 } | 122 } |
68 | 123 |
69 bool RenderViewContextMenuGtk::AlwaysShowIconForCmd(int command_id) const { | 124 bool RenderViewContextMenuGtk::AlwaysShowIconForCmd(int command_id) const { |
70 return command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | 125 return command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && |
71 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; | 126 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; |
72 } | 127 } |
| 128 |
| 129 void RenderViewContextMenuGtk::UpdateMenuItem(int command_id, |
| 130 bool enabled, |
| 131 const string16& title) { |
| 132 GtkWidget* item = GetMenuItemByID(&menu_model_, menu_gtk_->widget(), |
| 133 command_id); |
| 134 if (!item || !GTK_IS_MENU_ITEM(item)) |
| 135 return; |
| 136 |
| 137 // Enable (or disable) the menu item and updates its text. |
| 138 gtk_widget_set_sensitive(item, enabled); |
| 139 gtk_menu_item_set_label(GTK_MENU_ITEM(item), UTF16ToUTF8(title).c_str()); |
| 140 } |
OLD | NEW |