如何在菜单项上捕获gtk scroll-event

时间:2012-04-27 18:14:37

标签: c gtk glib gtk3 gdk

Gtk小部件发出信号scroll-event。我想在MenuItem上将函数连接到该信号。以下示例代码不起作用。

$ cat scroll-event-on-menu-item.c 
#include <gtk/gtk.h>

void scroll_cb(GtkStatusIcon* icon, GdkEvent* ev, gpointer userdata)
{
    g_message("scrolled!");
}

void quit_cb(GtkMenuItem* item, GdkEventButton* ev, gpointer userdata)
{
    gtk_main_quit();
}

void click_cb(GtkStatusIcon* icon, GdkEventButton* ev, gpointer userdata)
{
    GtkWidget* menu = gtk_menu_new();
    GtkWidget* item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
    gtk_widget_show(menu);
    gtk_widget_show(item);
    g_signal_connect(item, "button-press-event", G_CALLBACK(quit_cb), NULL);
    g_signal_connect(item, "scroll-event", G_CALLBACK(scroll_cb), NULL);
    gtk_menu_popup(GTK_MENU(menu), NULL, NULL, gtk_status_icon_position_menu,
            icon, ev->button, ev->time);
}

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

    GtkStatusIcon* icon = gtk_status_icon_new_from_stock(GTK_STOCK_INFO);
    g_signal_connect(icon, "button-press-event", G_CALLBACK(click_cb), NULL);

    gtk_main();

    return 0;
}

$ cat Makefile 
CFLAGS := $(shell pkg-config --cflags gtk+-3.0)
LDFLAGS := $(shell pkg-config --libs gtk+-3.0)
scroll-event-on-menu-item:

如何在菜单项上捕捉滚动事件?

1 个答案:

答案 0 :(得分:7)

您必须启用GDK_SCROLL_MASK掩码才能接收滚动事件。

gtk_widget_add_events(GTK_WIDGET(item), GDK_SCROLL_MASK);