GtkMenu in Sub-menu level cannot raise 'active' signal in ubuntu 20.04

Viewed 49

Recently, I upgrade my code from ubuntu 14.04 to 20.04. The UI seems works fine but the submenu with the touch panel. I press the menu and select menuitem2 and the submenu was showed, When I tap the touch screen to open the sub-menuitem, it did not response.

I wrote a code to check and found the signal 'select' was sent instead of the 'active' when I tap the touch screen. (The code works fine when using mouse click) Is anyone could help me to solve this problem? Or have any other GtkWidget could use to replace the GtkMenu?

Any comments and suggestion will be appreciated.

Brlow is my code:

/* 
 * Build:
 * gcc $(pkg-config --cflags gtk+-3.0) popover_sample.c -o popover_sample $(pkg-config --libs gtk+-3.0)
 */

#include <stddef.h>
#include <gtk/gtk.h>


void on_ITEM2_activate(GtkMenuItem* button, gpointer user_data)
{
    printf("on_ITEM2_activate \n");
}

void on_SEC_2_activate(GtkMenuItem* button, gpointer user_data)
{
    printf("on_SEC_2_activate \n");
}


void on_SEC_1_activate(GtkMenuItem* button, gpointer user_data)
{
    printf("on_SEC_1_activate \n");
}

void on_submenuitem1_activate(GtkMenuItem* button, gpointer user_data)
{
    printf("on_submenuitem1_activate \n");
}

void on_submenuitem2_activate(GtkMenuItem* button, gpointer user_data)
{
    printf("on_submenuitem2_activate \n");
}


void on_submenuitem3_activate(GtkMenuItem* button, gpointer user_data)
{
    printf("on_submenuitem3_activate \n");
}

void on_submenuitem4_activate(GtkMenuItem* button, gpointer user_data)
{
    printf("on_submenuitem4_activate \n");
}


// called when window is closed
void on_QUIT_BTN_clicked()
{
    gtk_main_quit();
}


void on_btn1_clicked(GtkMenuItem* button, gpointer user_data)
{
    gtk_main_quit();
}

void on_ITEM1_activate(GtkMenuItem* button, gpointer user_data)
{
    GtkWidget *open_item;   // New menuitem
    GtkWidget *save_item;   // New menuitem
    GtkWidget *quit_item;   // New menuitem
    GtkWidget *sep;      // New sep
    GtkWidget *ITEM1_Menu;  // New menu

    ITEM1_Menu = gtk_menu_new();

    open_item = gtk_menu_item_new_with_label ("Open");
    save_item = gtk_menu_item_new_with_label ("Save");
    quit_item = gtk_menu_item_new_with_label ("Quit");
    sep = gtk_separator_menu_item_new();

    gtk_menu_item_set_submenu(GTK_MENU_ITEM(button), ITEM1_Menu);
    gtk_menu_shell_append(GTK_MENU_SHELL(ITEM1_Menu), open_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(ITEM1_Menu), save_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(ITEM1_Menu), sep);
    gtk_menu_shell_append(GTK_MENU_SHELL(ITEM1_Menu), quit_item);


    g_signal_connect_swapped (G_OBJECT (open_item), "activate",
                              G_CALLBACK (on_submenuitem1_activate),
                              NULL);
    g_signal_connect_swapped (G_OBJECT (save_item), "activate",
                              G_CALLBACK (on_submenuitem2_activate),
                              NULL);

    g_signal_connect_swapped (G_OBJECT (quit_item), "activate",
                              G_CALLBACK (on_QUIT_BTN_clicked),
                              NULL);

    gtk_widget_show (open_item);
    gtk_widget_show (save_item);
    gtk_widget_show (quit_item);
    gtk_widget_show (sep);

    printf("on_ITEM1_activate \n");
}



int main(int argc, char *argv[])
{
    GtkBuilder      *builder;
    GtkWidget       *window;
    GError *error = NULL;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();

    if (gtk_builder_add_from_file (builder, "Menu_Test.glade", &error) == 0)
    {
        printf("Unable to open file named \"Menu_Test.glade\" \n");
        return 1;
    }

    window = GTK_WIDGET(gtk_builder_get_object(builder, "test_win"));
    if (NULL == window)
    {
        printf("Unable to file object with id \"test_win\" \n");
    }

    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    window = GTK_WIDGET(gtk_builder_get_object(builder, "test_win"));
    

    gtk_builder_connect_signals(builder, NULL);
    g_object_unref(builder);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

and the glade as below:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkMenu" id="menu1">
    <property name="visible">True</property>
    <property name="can-focus">False</property>
    <child>
      <object class="GtkMenuItem" id="menuitem1">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="tooltip-text" translatable="yes">ITEM_1</property>
        <property name="label" translatable="yes">menuitem1</property>
        <property name="use-underline">True</property>
        <signal name="select" handler="on_ITEM1_activate" swapped="no"/>
      </object>
    </child>
    <child>
      <object class="GtkMenuItem" id="menuitem2">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="tooltip-text" translatable="yes">ITEM_2</property>
        <property name="label" translatable="yes">menuitem2</property>
        <property name="use-underline">True</property>
        <signal name="activate" handler="on_ITEM2_activate" swapped="no"/>
        <child type="submenu">
          <object class="GtkMenu" id="menu2">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <child>
              <object class="GtkMenuItem" id="submenuitem3">
                <property name="visible">True</property>
                <property name="can-focus">False</property>
                <property name="tooltip-text" translatable="yes">Item_2_1</property>
                <property name="label" translatable="yes">menuitem3</property>
                <property name="use-underline">True</property>
                <signal name="activate" handler="on_submenuitem3_activate" swapped="no"/>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="submenuitem4">
                <property name="name">submenuitem4</property>
                <property name="visible">True</property>
                <property name="can-focus">False</property>
                <property name="tooltip-text" translatable="yes">Item_2_2</property>
                <property name="label" translatable="yes">menuitem4</property>
                <property name="use-underline">True</property>
                <signal name="activate" handler="on_submenuitem4_activate" swapped="no"/>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="test_win">
    <property name="width-request">300</property>
    <property name="height-request">300</property>
    <property name="can-focus">False</property>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">76</property>
        <child>
          <object class="GtkMenuButton" id="menubutton1">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <property name="popup">menu1</property>
            <child>
              <placeholder/>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <property name="halign">center</property>
            <property name="valign">baseline</property>
            <signal name="clicked" handler="on_QUIT_BTN_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

1 Answers

After upgrade the gtk lib to 3.24.28 version. This problem was solved.

Related