GTK:当菜单上发生点击事件时,如何保持弹出菜单打开?

时间:2017-05-15 13:48:28

标签: c++ menu popup gtk contextmenu

我有一个弹出菜单,其子菜单包含可检查项目。我想在用户选中/取消选中某个项目时保持子菜单打开,以便用户可以执行新的检查/取消选中。

如果在菜单上发生点击事件,如何才能打开弹出菜单?

问题在于LibreOffice,弹出菜单的代码指针在这里:http://opengrok.libreoffice.org/xref/core/vcl/unx/gtk/gtksalmenu.cxx#408

提前致谢。

编辑:我在这里尝试了@ theGtknerd的方法弹出窗口没有重新打开:

#include <gtk/gtk.h>                                                            

int show_popup(GtkWidget *widget, GdkEvent *event) {

  const gint RIGHT_CLICK = 3;
  const gint LEFT_CLICK = 1;

  if (event->type == GDK_BUTTON_PRESS) {

      GdkEventButton *bevent = (GdkEventButton *) event;

      if (bevent->button == RIGHT_CLICK || bevent->button == LEFT_CLICK) {      

          gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
          }
      return TRUE;
  }
  return FALSE;
}

int main(int argc, char *argv[]) {

  GtkWidget *window;
  GtkWidget *ebox;
  GtkWidget *pmenu;
  GtkWidget *reOpenMi;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
  gtk_window_set_title(GTK_WINDOW(window), "Popup menu");

  ebox = gtk_event_box_new();
  gtk_container_add(GTK_CONTAINER(window), ebox);

  pmenu = gtk_menu_new();

  reOpenMi = gtk_menu_item_new_with_label("Minimize");
  gtk_widget_show(reOpenMi);
  gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), reOpenMi);

  g_signal_connect_swapped(G_OBJECT(reOpenMi), "activate", 
      G_CALLBACK(show_popup), GTK_MENU_SHELL(pmenu));    

  g_signal_connect(G_OBJECT(window), "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  g_signal_connect_swapped(G_OBJECT(ebox), "button-press-event", 
      G_CALLBACK(show_popup), pmenu);  

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

0 个答案:

没有答案