menu_gtk.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "shell/browser/ui/gtk/menu_gtk.h"
  5. #include <gtk/gtk.h>
  6. #include "shell/browser/ui/gtk/menu_util.h"
  7. #include "ui/base/models/menu_model.h"
  8. namespace electron::gtkui {
  9. MenuGtk::MenuGtk(ui::MenuModel* model)
  10. : menu_model_(model), gtk_menu_(TakeGObject(gtk_menu_new())) {
  11. if (menu_model_) {
  12. BuildSubmenuFromModel(menu_model_, gtk_menu_,
  13. G_CALLBACK(OnMenuItemActivatedThunk),
  14. &block_activation_, this);
  15. Refresh();
  16. }
  17. }
  18. MenuGtk::~MenuGtk() {
  19. gtk_widget_destroy(gtk_menu_);
  20. }
  21. void MenuGtk::Refresh() {
  22. gtk_container_foreach(GTK_CONTAINER(gtk_menu_.get()), SetMenuItemInfo,
  23. &block_activation_);
  24. }
  25. GtkMenu* MenuGtk::GetGtkMenu() {
  26. return GTK_MENU(gtk_menu_.get());
  27. }
  28. void MenuGtk::OnMenuItemActivated(GtkWidget* menu_item) {
  29. if (block_activation_)
  30. return;
  31. ui::MenuModel* model = ModelForMenuItem(GTK_MENU_ITEM(menu_item));
  32. if (!model) {
  33. // There won't be a model for "native" submenus like the "Input Methods"
  34. // context menu. We don't need to handle activation messages for submenus
  35. // anyway, so we can just return here.
  36. DCHECK(gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item)));
  37. return;
  38. }
  39. // The activate signal is sent to radio items as they get deselected;
  40. // ignore it in this case.
  41. if (GTK_IS_RADIO_MENU_ITEM(menu_item) &&
  42. !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_item))) {
  43. return;
  44. }
  45. int id;
  46. if (!GetMenuItemID(menu_item, &id))
  47. return;
  48. // The menu item can still be activated by hotkeys even if it is disabled.
  49. if (model->IsEnabledAt(id))
  50. ExecuteCommand(model, id);
  51. }
  52. } // namespace electron::gtkui