| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/libgtk2ui/app_indicator_icon_menu.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "chrome/browser/ui/libgtk2ui/menu_util.h" | |
| 11 #include "ui/base/models/menu_model.h" | |
| 12 | |
| 13 namespace libgtk2ui { | |
| 14 | |
| 15 AppIndicatorIconMenu::AppIndicatorIconMenu(ui::MenuModel* model) | |
| 16 : menu_model_(model), | |
| 17 click_action_replacement_menu_item_added_(false), | |
| 18 gtk_menu_(gtk_menu_new()), | |
| 19 block_activation_(false) { | |
| 20 if (menu_model_) { | |
| 21 BuildSubmenuFromModel(menu_model_, | |
| 22 gtk_menu_, | |
| 23 G_CALLBACK(OnMenuItemActivatedThunk), | |
| 24 &block_activation_, | |
| 25 this); | |
| 26 Refresh(); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 AppIndicatorIconMenu::~AppIndicatorIconMenu() { | |
| 31 gtk_widget_destroy(gtk_menu_); | |
| 32 } | |
| 33 | |
| 34 void AppIndicatorIconMenu::UpdateClickActionReplacementMenuItem( | |
| 35 const char* label, | |
| 36 const base::Closure& callback) { | |
| 37 click_action_replacement_callback_ = callback; | |
| 38 | |
| 39 if (click_action_replacement_menu_item_added_) { | |
| 40 GList* children = gtk_container_get_children(GTK_CONTAINER(gtk_menu_)); | |
| 41 for (GList* child = children; child; child = g_list_next(child)) { | |
| 42 if (g_object_get_data(G_OBJECT(child->data), "click-action-item") != | |
| 43 NULL) { | |
| 44 gtk_menu_item_set_label(GTK_MENU_ITEM(child->data), label); | |
| 45 break; | |
| 46 } | |
| 47 } | |
| 48 g_list_free(children); | |
| 49 } else { | |
| 50 click_action_replacement_menu_item_added_ = true; | |
| 51 | |
| 52 // If |menu_model_| is non empty, add a separator to separate the | |
| 53 // "click action replacement menu item" from the other menu items. | |
| 54 if (menu_model_ && menu_model_->GetItemCount() > 0) { | |
| 55 GtkWidget* menu_item = gtk_separator_menu_item_new(); | |
| 56 gtk_widget_show(menu_item); | |
| 57 gtk_menu_shell_prepend(GTK_MENU_SHELL(gtk_menu_), menu_item); | |
| 58 } | |
| 59 | |
| 60 GtkWidget* menu_item = gtk_menu_item_new_with_mnemonic(label); | |
| 61 g_object_set_data( | |
| 62 G_OBJECT(menu_item), "click-action-item", GINT_TO_POINTER(1)); | |
| 63 g_signal_connect(menu_item, | |
| 64 "activate", | |
| 65 G_CALLBACK(OnClickActionReplacementMenuItemActivatedThunk), | |
| 66 this); | |
| 67 gtk_widget_show(menu_item); | |
| 68 gtk_menu_shell_prepend(GTK_MENU_SHELL(gtk_menu_), menu_item); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void AppIndicatorIconMenu::Refresh() { | |
| 73 gtk_container_foreach( | |
| 74 GTK_CONTAINER(gtk_menu_), SetMenuItemInfo, &block_activation_); | |
| 75 } | |
| 76 | |
| 77 GtkMenu* AppIndicatorIconMenu::GetGtkMenu() { | |
| 78 return GTK_MENU(gtk_menu_); | |
| 79 } | |
| 80 | |
| 81 | |
| 82 void AppIndicatorIconMenu::OnClickActionReplacementMenuItemActivated( | |
| 83 GtkWidget* menu_item) { | |
| 84 click_action_replacement_callback_.Run(); | |
| 85 } | |
| 86 | |
| 87 void AppIndicatorIconMenu::OnMenuItemActivated(GtkWidget* menu_item) { | |
| 88 if (block_activation_) | |
| 89 return; | |
| 90 | |
| 91 ui::MenuModel* model = ModelForMenuItem(GTK_MENU_ITEM(menu_item)); | |
| 92 if (!model) { | |
| 93 // There won't be a model for "native" submenus like the "Input Methods" | |
| 94 // context menu. We don't need to handle activation messages for submenus | |
| 95 // anyway, so we can just return here. | |
| 96 DCHECK(gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item))); | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 // The activate signal is sent to radio items as they get deselected; | |
| 101 // ignore it in this case. | |
| 102 if (GTK_IS_RADIO_MENU_ITEM(menu_item) && | |
| 103 !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_item))) { | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 int id; | |
| 108 if (!GetMenuItemID(menu_item, &id)) | |
| 109 return; | |
| 110 | |
| 111 // The menu item can still be activated by hotkeys even if it is disabled. | |
| 112 if (menu_model_->IsEnabledAt(id)) | |
| 113 ExecuteCommand(model, id); | |
| 114 } | |
| 115 | |
| 116 } // namespace libgtk2ui | |
| OLD | NEW |