Lazyweb: GtkMenu help needed

Whenever I try to do any GUI coding, I seem to be continually tripped up by what appear to be incredibly basic problems. I don’t know whether it’s just that I don’t think the same way as the toolkit designers do, or if the toolkits are just designed in the most complicated way possible.

I’ve read over and over the GtkMenu and GtkMenuItem documentation, but I cannot see any way of getting the index number of a GtkMenuItem that is clicked on within a GtkMenu. It seems to me like this would be a fairly common thing to want to do, in order to create GtkMenus with dynamic items.

Can any GTK experts out there give me some suggestions as to how I can get the index number of the GtkMenuItem clicked on, in the handler() function below? (Aside from supplying menu_item[i] in g_signal_connect_swapped and then looping through all of menu_items[] and comparing them, that is). Surely the GTK designers can’t assume that every menu item is going to be connected to a different handler function?

    /* ...  */

    GtkWidget *menu;
    GtkWidget **menu_items;

    /* ... set menu item labels in LABEL[NUM_ITEMS] ...   */

    menu = gtk_menu_new();

    for(i=0;i<NUM_ITEMS;i++) {
        menu_item[i] = gtk_menu_item_new_with_label( LABEL[i] );


        g_signal_connect_swapped(menu, "button_press_event",
                G_CALLBACK(handler), ??? );

        /* Alternatively: 
        g_signal_connect_swapped(menu_item[i], "button_press_event",
                G_CALLBACK(handler), ??? );


    /* ...  */

static gint handler (GtkWidget *widget, GdkEvent *event) {

        /* ... want to find out which menu_item (n) was clicked on ...  */

        printf("You clicked item number %25d
", n );

Leave a Reply

Your email address will not be published. Required fields are marked *

Warning: Illegal string offset 'q' in /var/www/ on line 60

Warning: Illegal string offset 'a' in /var/www/ on line 61

Warning: Illegal string offset 'q' in /var/www/ on line 179

Anti-Spam Quiz: