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