Re: Howto attach a popup menu to a menubar (gtk2)



On 03/01/2017 08:17 PM, David C. Rankin wrote:
All,

  I have a menubar (working fine) and I want to attach a popup menu to that
widget to "show/hide" a toolbar below it. I cannot figure out how to attach
the popup to the 'menubar' itself generically.

    GtkWidget *evbox;           /* popup menu container */
    GtkWidget *pmenu;         /* the menu */
    GtkWidget *pshowtbMI;     /* the menu item */

  I've tried to attach the popup to the 'menubar' as a container with the
following code:

    evbox = gtk_event_box_new();
    gtk_container_add (GTK_CONTAINER(menubar), evbox);
    pmenu = gtk_menu_new();
    pshowtbMI = gtk_check_menu_item_new_with_label ("Show Toolbar");
    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(pshowtbMI), TRUE);
    gtk_menu_shell_append (GTK_MENU_SHELL(pmenu), pshowtbMI);
    g_signal_connect_swapped (G_OBJECT(evbox), "button-press-event",
        G_CALLBACK(show_popup), pmenu);
    g_signal_connect (G_OBJECT(pshowtbMI), "activate",
        G_CALLBACK(menu_showtb_activate), app);

  The show_popup is a generic event-handler that responds to the right-mouse
button (working fine).

  What can I do to provide a popup that will respond when I rt-click on a
blank part of the menubar?


Well,

  I have made progress. Rather than adding the eventbox container to the menu,
I added the menu to the eventbox container -- now the popup works fine when
right-clicking on the menubar... but clicking on the menus does not open them.
(the accelerators work fine and I can't open the menus with the menu
accelerators (e.g. Alt+F opens the file menu) I suspect this is a problem with
the event_box event-handler that catches the right-click but does not
propagate the left-click to the menu inside it.

  The current setup for the popup menu is:

    evbox = gtk_event_box_new();
    /* modified menu in container - popup works, but menu doesn't */
    gtk_container_add (GTK_CONTAINER(evbox), menubar);
    pmenu = gtk_menu_new();
    pshowtbMI = gtk_check_menu_item_new_with_label ("Show Toolbar");
    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(pshowtbMI), TRUE);
    gtk_menu_shell_append (GTK_MENU_SHELL(pmenu), pshowtbMI);
    /* modified menu in container - popup works, but menu doesn't */
    g_signal_connect_swapped (G_OBJECT(menubar), "button-press-event",
        G_CALLBACK(show_popup), pmenu);
    g_signal_connect (G_OBJECT(pshowtbMI), "activate",
        G_CALLBACK(menu_showtb_activate), app);

  The event handler I'm using 'show_popup' is:

    gboolean show_popup(GtkWidget *widget, GdkEvent *event)
    {
        const guint RIGHT_CLICK = 3;

        if (event->type == GDK_BUTTON_PRESS) {

            GdkEventButton *bevent = (GdkEventButton *) event;

            if (bevent->button == RIGHT_CLICK) {

                gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
                    bevent->button, bevent->time);
            }
            return TRUE;
        }
        return FALSE;
    }

  Do I need to somehow explicitly pass the button-press-event to the menubar
within the event_box somehow?

-- 
David C. Rankin, J.D.,P.E.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]