Re: popup menu problem
- From: Olivier Abad <oabad noos fr>
- To: gtk-app-devel-list gnome org
- Subject: Re: popup menu problem
- Date: Tue, 26 Sep 2006 11:33:27 +0200
Le mardi 26 septembre 2006 Ã 10:47 +0200, Olivier Abad a Ãcrit :
Hi,
I'm trying to implement a popup menu, but the items in my menu don't
seem to receive the "activate" signal, so my handler is never called.
However, it works if I connect the handler to "button-release-event"
instead of "activate" for my menu items.
Can someone tell me what I'm doing wrong ?
I'm attaching the code I used (taken from gtk+ documentation).
It didn't work the first time. Here is the code :
#include <gtk/gtk.h>
static void
click(GtkWidget *menu, gpointer user_data)
{
printf("click %d\n", (int)user_data);
}
static void
do_popup_menu (GtkWidget *my_widget, GdkEventButton *event)
{
GtkWidget *menu, *item;
int button, event_time;
menu = gtk_menu_new ();
g_signal_connect (G_OBJECT(menu), "deactivate",
G_CALLBACK (gtk_widget_destroy), NULL);
/* ... add menu items ... */
item = gtk_menu_item_new_with_label("label1");
gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
g_signal_connect(item, "activate", G_CALLBACK(click), (gpointer)1);
item = gtk_menu_item_new_with_label("label2");
gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
g_signal_connect(item, "activate", G_CALLBACK(click), (gpointer)2);
if (event)
{
button = event->button;
event_time = event->time;
}
else
{
button = 0;
event_time = gtk_get_current_event_time ();
}
gtk_menu_attach_to_widget (GTK_MENU (menu), my_widget, NULL);
gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
button, event_time);
}
static gboolean
my_widget_button_press_event_handler (GtkWidget *widget, GdkEventButton
*event)
{
/* Ignore double-clicks and triple-clicks */
if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
{
do_popup_menu (widget, event);
return TRUE;
}
return FALSE;
}
static gboolean
my_widget_popup_menu_handler (GtkWidget *widget)
{
do_popup_menu (widget, NULL);
return TRUE;
}
int main(int argc, char *argv[])
{
GtkWidget *window, *bt;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete-event",
G_CALLBACK(gtk_main_quit), NULL);
bt = gtk_button_new_with_label("click me");
g_signal_connect(bt, "button-press-event",
G_CALLBACK(my_widget_button_press_event_handler),
NULL);
g_signal_connect(bt, "popup-menu",
G_CALLBACK(my_widget_popup_menu_handler), NULL);
gtk_container_add(GTK_CONTAINER(window), bt);
gtk_widget_show(bt);
gtk_widget_show(window);
gtk_main();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]