Howto link GMenu and GtkMenu button in Gtk 3












0















I don’t understand what is the problem in a small example.
I would like implement Gaction with Gmenu not in GtkApplication



first please find small example with GtkApplication



I took here



#include <gtk/gtk.h>
//menubuttonexample.c
/* Compile with:
* gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample.c -o menubuttonexample `pkg-config --cflags --libs gtk+-3.0 `
*/

/* Callback function for the undo action */
static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
{
g_print ("You clicked "About"n");
// not used
(void)simple;
(void)parameter;
(void)user_data;
}
static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
{
g_print ("You clicked "New"n");
// not used
(void)simple;
(void)parameter;
(void)user_data;
}

static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
{
GApplication *application = user_data;
g_application_quit (application);
// not used
(void)simple;
(void)parameter;
(void)user_data;
}

static void activate (GtkApplication *app, gpointer user_data)
{
GMenu *submenu;
GtkWidget *grid;
GMenu *menumodel;
GtkWidget *window;
GtkWidget *menubutton;
GSimpleAction *about_action;

window = gtk_application_window_new (app);
grid = gtk_grid_new ();

gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

menubutton = gtk_menu_button_new ();
gtk_widget_set_size_request (menubutton, 80, 35);

gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
gtk_container_add (GTK_CONTAINER (window), grid);

menumodel = g_menu_new ();
g_menu_append (menumodel, "New", "app.new");
g_menu_append (menumodel, "About", "win.about");

submenu = g_menu_new ();
g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
g_menu_append (submenu, "Quit", "app.quit");
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

about_action = g_simple_action_new ("about", NULL);
g_signal_connect (about_action, "activate", G_CALLBACK (about_callback),
GTK_WINDOW (window));
g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (about_action));

gtk_widget_show_all (window);
// not used
(void)user_data;

}

static void startup (GApplication *app, gpointer user_data)
{
GSimpleAction *new_action;
GSimpleAction *quit_action;

new_action = g_simple_action_new ("new", NULL);
g_signal_connect (new_action, "activate", G_CALLBACK (new_callback), app);
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (new_action));

quit_action = g_simple_action_new ("quit", NULL);
g_signal_connect (quit_action, "activate", G_CALLBACK (quit_callback), app);
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit_action));
// not used
(void)user_data;
}

int main (int argc, char **argv)
{
GtkApplication *app;
int status;

app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}


first code run ok for me.



In the second step I'm try to make the same with a gtk_window and without GtkApplication
please find the code following



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

//menubuttonexample_ter.c
/* Compile with:
* gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
*/

/** le même que menubuttonexample mais en version non Gtkapplication */

/* Callback function for the undo action */
static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
{
g_print ("You clicked "About"n");
// not used
(void)simple;
(void)parameter;
(void)user_data;
}
static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
{
g_print ("You clicked "New"n");
// not used
(void)simple;
(void)parameter;
(void)user_data;
}

static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
{
GApplication *application = user_data;
g_application_quit (application);
// not used
(void)simple;
(void)parameter;
(void)user_data;
}

static GActionGroup * create_action_group (void)
{
const GActionEntry entries = {
{ "win.quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
{ "win.new", new_callback, NULL, NULL, NULL,{0,0,0} },
{ "win.about", about_callback ,NULL, NULL, NULL,{0,0,0} }
};
GSimpleActionGroup *group;

group = g_simple_action_group_new ();
g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);

return G_ACTION_GROUP (group);
}

static GtkWidget* create_main_window (void)
{
GMenu *submenu;
GtkWidget *grid;
GMenu *menumodel;
GtkWidget *window;
GtkWidget *menubutton;

//GSimpleAction *about_action;
//GSimpleAction *new_action;
//GSimpleAction *quit_action;

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);


#define G_MENU_ATTRIBUTE_ACTION "win"

GActionGroup * group = NULL;

group = create_action_group(); // I don't know to link Gmenu and group


/* Lorsque le shell affiche le menu de l'application, les actions de type "win"font référence à la copie
* de cette action sur la fenêtre actuellement active.
* Si une telle action n'existe pas, l'élément de menu sera grisé.
* ===> c'est exactement ce que je constate à date */

grid = gtk_grid_new ();

menubutton = gtk_menu_button_new ();
gtk_widget_set_size_request (menubutton, 80, 35);


/* essai */
gtk_widget_map(GTK_WIDGET(menubutton));

gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
gtk_container_add (GTK_CONTAINER (window), grid);

menumodel = g_menu_new ();
g_menu_append (menumodel, "New", "win.new");
g_menu_append (menumodel, "About", "win.about");

submenu = g_menu_new ();
g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
// from the menu, we refer to the action as "app.quit" since we installed it on the application
g_menu_append (submenu, "Quit", "win.quit");
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));
return window;
}

int main (int argc, char **argv)
{
GtkWidget *window = NULL;

/* Initialisation de GTK+ */
gtk_init (&argc, &argv);
g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);

/* Creation du conteneur principal */
window = create_main_window();

/* Affichage de la fenetre principale */
gtk_widget_show_all (window);
/* Lancement de la boucle principale */
gtk_main ();
return EXIT_SUCCESS;
}


The level of presentational information. Seems ok. Because it's the same that first example.
Problem is purely functional.



I don't fond howto link GAction ou GActiongroup instance group with the Gmenu as menumodel.



Please help because I google some day without solution










share|improve this question



























    0















    I don’t understand what is the problem in a small example.
    I would like implement Gaction with Gmenu not in GtkApplication



    first please find small example with GtkApplication



    I took here



    #include <gtk/gtk.h>
    //menubuttonexample.c
    /* Compile with:
    * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample.c -o menubuttonexample `pkg-config --cflags --libs gtk+-3.0 `
    */

    /* Callback function for the undo action */
    static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
    {
    g_print ("You clicked "About"n");
    // not used
    (void)simple;
    (void)parameter;
    (void)user_data;
    }
    static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
    {
    g_print ("You clicked "New"n");
    // not used
    (void)simple;
    (void)parameter;
    (void)user_data;
    }

    static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
    {
    GApplication *application = user_data;
    g_application_quit (application);
    // not used
    (void)simple;
    (void)parameter;
    (void)user_data;
    }

    static void activate (GtkApplication *app, gpointer user_data)
    {
    GMenu *submenu;
    GtkWidget *grid;
    GMenu *menumodel;
    GtkWidget *window;
    GtkWidget *menubutton;
    GSimpleAction *about_action;

    window = gtk_application_window_new (app);
    grid = gtk_grid_new ();

    gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
    gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

    menubutton = gtk_menu_button_new ();
    gtk_widget_set_size_request (menubutton, 80, 35);

    gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
    gtk_container_add (GTK_CONTAINER (window), grid);

    menumodel = g_menu_new ();
    g_menu_append (menumodel, "New", "app.new");
    g_menu_append (menumodel, "About", "win.about");

    submenu = g_menu_new ();
    g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
    g_menu_append (submenu, "Quit", "app.quit");
    gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

    about_action = g_simple_action_new ("about", NULL);
    g_signal_connect (about_action, "activate", G_CALLBACK (about_callback),
    GTK_WINDOW (window));
    g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (about_action));

    gtk_widget_show_all (window);
    // not used
    (void)user_data;

    }

    static void startup (GApplication *app, gpointer user_data)
    {
    GSimpleAction *new_action;
    GSimpleAction *quit_action;

    new_action = g_simple_action_new ("new", NULL);
    g_signal_connect (new_action, "activate", G_CALLBACK (new_callback), app);
    g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (new_action));

    quit_action = g_simple_action_new ("quit", NULL);
    g_signal_connect (quit_action, "activate", G_CALLBACK (quit_callback), app);
    g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit_action));
    // not used
    (void)user_data;
    }

    int main (int argc, char **argv)
    {
    GtkApplication *app;
    int status;

    app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);
    return status;
    }


    first code run ok for me.



    In the second step I'm try to make the same with a gtk_window and without GtkApplication
    please find the code following



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

    //menubuttonexample_ter.c
    /* Compile with:
    * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
    */

    /** le même que menubuttonexample mais en version non Gtkapplication */

    /* Callback function for the undo action */
    static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
    {
    g_print ("You clicked "About"n");
    // not used
    (void)simple;
    (void)parameter;
    (void)user_data;
    }
    static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
    {
    g_print ("You clicked "New"n");
    // not used
    (void)simple;
    (void)parameter;
    (void)user_data;
    }

    static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
    {
    GApplication *application = user_data;
    g_application_quit (application);
    // not used
    (void)simple;
    (void)parameter;
    (void)user_data;
    }

    static GActionGroup * create_action_group (void)
    {
    const GActionEntry entries = {
    { "win.quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
    { "win.new", new_callback, NULL, NULL, NULL,{0,0,0} },
    { "win.about", about_callback ,NULL, NULL, NULL,{0,0,0} }
    };
    GSimpleActionGroup *group;

    group = g_simple_action_group_new ();
    g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);

    return G_ACTION_GROUP (group);
    }

    static GtkWidget* create_main_window (void)
    {
    GMenu *submenu;
    GtkWidget *grid;
    GMenu *menumodel;
    GtkWidget *window;
    GtkWidget *menubutton;

    //GSimpleAction *about_action;
    //GSimpleAction *new_action;
    //GSimpleAction *quit_action;

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
    gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);


    #define G_MENU_ATTRIBUTE_ACTION "win"

    GActionGroup * group = NULL;

    group = create_action_group(); // I don't know to link Gmenu and group


    /* Lorsque le shell affiche le menu de l'application, les actions de type "win"font référence à la copie
    * de cette action sur la fenêtre actuellement active.
    * Si une telle action n'existe pas, l'élément de menu sera grisé.
    * ===> c'est exactement ce que je constate à date */

    grid = gtk_grid_new ();

    menubutton = gtk_menu_button_new ();
    gtk_widget_set_size_request (menubutton, 80, 35);


    /* essai */
    gtk_widget_map(GTK_WIDGET(menubutton));

    gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
    gtk_container_add (GTK_CONTAINER (window), grid);

    menumodel = g_menu_new ();
    g_menu_append (menumodel, "New", "win.new");
    g_menu_append (menumodel, "About", "win.about");

    submenu = g_menu_new ();
    g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
    // from the menu, we refer to the action as "app.quit" since we installed it on the application
    g_menu_append (submenu, "Quit", "win.quit");
    gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));
    return window;
    }

    int main (int argc, char **argv)
    {
    GtkWidget *window = NULL;

    /* Initialisation de GTK+ */
    gtk_init (&argc, &argv);
    g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);

    /* Creation du conteneur principal */
    window = create_main_window();

    /* Affichage de la fenetre principale */
    gtk_widget_show_all (window);
    /* Lancement de la boucle principale */
    gtk_main ();
    return EXIT_SUCCESS;
    }


    The level of presentational information. Seems ok. Because it's the same that first example.
    Problem is purely functional.



    I don't fond howto link GAction ou GActiongroup instance group with the Gmenu as menumodel.



    Please help because I google some day without solution










    share|improve this question

























      0












      0








      0








      I don’t understand what is the problem in a small example.
      I would like implement Gaction with Gmenu not in GtkApplication



      first please find small example with GtkApplication



      I took here



      #include <gtk/gtk.h>
      //menubuttonexample.c
      /* Compile with:
      * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample.c -o menubuttonexample `pkg-config --cflags --libs gtk+-3.0 `
      */

      /* Callback function for the undo action */
      static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      g_print ("You clicked "About"n");
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }
      static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      g_print ("You clicked "New"n");
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }

      static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      GApplication *application = user_data;
      g_application_quit (application);
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }

      static void activate (GtkApplication *app, gpointer user_data)
      {
      GMenu *submenu;
      GtkWidget *grid;
      GMenu *menumodel;
      GtkWidget *window;
      GtkWidget *menubutton;
      GSimpleAction *about_action;

      window = gtk_application_window_new (app);
      grid = gtk_grid_new ();

      gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
      gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

      menubutton = gtk_menu_button_new ();
      gtk_widget_set_size_request (menubutton, 80, 35);

      gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
      gtk_container_add (GTK_CONTAINER (window), grid);

      menumodel = g_menu_new ();
      g_menu_append (menumodel, "New", "app.new");
      g_menu_append (menumodel, "About", "win.about");

      submenu = g_menu_new ();
      g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
      g_menu_append (submenu, "Quit", "app.quit");
      gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

      about_action = g_simple_action_new ("about", NULL);
      g_signal_connect (about_action, "activate", G_CALLBACK (about_callback),
      GTK_WINDOW (window));
      g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (about_action));

      gtk_widget_show_all (window);
      // not used
      (void)user_data;

      }

      static void startup (GApplication *app, gpointer user_data)
      {
      GSimpleAction *new_action;
      GSimpleAction *quit_action;

      new_action = g_simple_action_new ("new", NULL);
      g_signal_connect (new_action, "activate", G_CALLBACK (new_callback), app);
      g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (new_action));

      quit_action = g_simple_action_new ("quit", NULL);
      g_signal_connect (quit_action, "activate", G_CALLBACK (quit_callback), app);
      g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit_action));
      // not used
      (void)user_data;
      }

      int main (int argc, char **argv)
      {
      GtkApplication *app;
      int status;

      app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
      g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
      g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
      status = g_application_run (G_APPLICATION (app), argc, argv);
      g_object_unref (app);
      return status;
      }


      first code run ok for me.



      In the second step I'm try to make the same with a gtk_window and without GtkApplication
      please find the code following



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

      //menubuttonexample_ter.c
      /* Compile with:
      * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
      */

      /** le même que menubuttonexample mais en version non Gtkapplication */

      /* Callback function for the undo action */
      static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      g_print ("You clicked "About"n");
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }
      static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      g_print ("You clicked "New"n");
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }

      static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      GApplication *application = user_data;
      g_application_quit (application);
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }

      static GActionGroup * create_action_group (void)
      {
      const GActionEntry entries = {
      { "win.quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
      { "win.new", new_callback, NULL, NULL, NULL,{0,0,0} },
      { "win.about", about_callback ,NULL, NULL, NULL,{0,0,0} }
      };
      GSimpleActionGroup *group;

      group = g_simple_action_group_new ();
      g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);

      return G_ACTION_GROUP (group);
      }

      static GtkWidget* create_main_window (void)
      {
      GMenu *submenu;
      GtkWidget *grid;
      GMenu *menumodel;
      GtkWidget *window;
      GtkWidget *menubutton;

      //GSimpleAction *about_action;
      //GSimpleAction *new_action;
      //GSimpleAction *quit_action;

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
      gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);


      #define G_MENU_ATTRIBUTE_ACTION "win"

      GActionGroup * group = NULL;

      group = create_action_group(); // I don't know to link Gmenu and group


      /* Lorsque le shell affiche le menu de l'application, les actions de type "win"font référence à la copie
      * de cette action sur la fenêtre actuellement active.
      * Si une telle action n'existe pas, l'élément de menu sera grisé.
      * ===> c'est exactement ce que je constate à date */

      grid = gtk_grid_new ();

      menubutton = gtk_menu_button_new ();
      gtk_widget_set_size_request (menubutton, 80, 35);


      /* essai */
      gtk_widget_map(GTK_WIDGET(menubutton));

      gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
      gtk_container_add (GTK_CONTAINER (window), grid);

      menumodel = g_menu_new ();
      g_menu_append (menumodel, "New", "win.new");
      g_menu_append (menumodel, "About", "win.about");

      submenu = g_menu_new ();
      g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
      // from the menu, we refer to the action as "app.quit" since we installed it on the application
      g_menu_append (submenu, "Quit", "win.quit");
      gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));
      return window;
      }

      int main (int argc, char **argv)
      {
      GtkWidget *window = NULL;

      /* Initialisation de GTK+ */
      gtk_init (&argc, &argv);
      g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);

      /* Creation du conteneur principal */
      window = create_main_window();

      /* Affichage de la fenetre principale */
      gtk_widget_show_all (window);
      /* Lancement de la boucle principale */
      gtk_main ();
      return EXIT_SUCCESS;
      }


      The level of presentational information. Seems ok. Because it's the same that first example.
      Problem is purely functional.



      I don't fond howto link GAction ou GActiongroup instance group with the Gmenu as menumodel.



      Please help because I google some day without solution










      share|improve this question














      I don’t understand what is the problem in a small example.
      I would like implement Gaction with Gmenu not in GtkApplication



      first please find small example with GtkApplication



      I took here



      #include <gtk/gtk.h>
      //menubuttonexample.c
      /* Compile with:
      * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample.c -o menubuttonexample `pkg-config --cflags --libs gtk+-3.0 `
      */

      /* Callback function for the undo action */
      static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      g_print ("You clicked "About"n");
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }
      static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      g_print ("You clicked "New"n");
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }

      static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      GApplication *application = user_data;
      g_application_quit (application);
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }

      static void activate (GtkApplication *app, gpointer user_data)
      {
      GMenu *submenu;
      GtkWidget *grid;
      GMenu *menumodel;
      GtkWidget *window;
      GtkWidget *menubutton;
      GSimpleAction *about_action;

      window = gtk_application_window_new (app);
      grid = gtk_grid_new ();

      gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
      gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

      menubutton = gtk_menu_button_new ();
      gtk_widget_set_size_request (menubutton, 80, 35);

      gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
      gtk_container_add (GTK_CONTAINER (window), grid);

      menumodel = g_menu_new ();
      g_menu_append (menumodel, "New", "app.new");
      g_menu_append (menumodel, "About", "win.about");

      submenu = g_menu_new ();
      g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
      g_menu_append (submenu, "Quit", "app.quit");
      gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

      about_action = g_simple_action_new ("about", NULL);
      g_signal_connect (about_action, "activate", G_CALLBACK (about_callback),
      GTK_WINDOW (window));
      g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (about_action));

      gtk_widget_show_all (window);
      // not used
      (void)user_data;

      }

      static void startup (GApplication *app, gpointer user_data)
      {
      GSimpleAction *new_action;
      GSimpleAction *quit_action;

      new_action = g_simple_action_new ("new", NULL);
      g_signal_connect (new_action, "activate", G_CALLBACK (new_callback), app);
      g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (new_action));

      quit_action = g_simple_action_new ("quit", NULL);
      g_signal_connect (quit_action, "activate", G_CALLBACK (quit_callback), app);
      g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit_action));
      // not used
      (void)user_data;
      }

      int main (int argc, char **argv)
      {
      GtkApplication *app;
      int status;

      app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
      g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
      g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
      status = g_application_run (G_APPLICATION (app), argc, argv);
      g_object_unref (app);
      return status;
      }


      first code run ok for me.



      In the second step I'm try to make the same with a gtk_window and without GtkApplication
      please find the code following



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

      //menubuttonexample_ter.c
      /* Compile with:
      * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
      */

      /** le même que menubuttonexample mais en version non Gtkapplication */

      /* Callback function for the undo action */
      static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      g_print ("You clicked "About"n");
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }
      static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      g_print ("You clicked "New"n");
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }

      static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
      {
      GApplication *application = user_data;
      g_application_quit (application);
      // not used
      (void)simple;
      (void)parameter;
      (void)user_data;
      }

      static GActionGroup * create_action_group (void)
      {
      const GActionEntry entries = {
      { "win.quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
      { "win.new", new_callback, NULL, NULL, NULL,{0,0,0} },
      { "win.about", about_callback ,NULL, NULL, NULL,{0,0,0} }
      };
      GSimpleActionGroup *group;

      group = g_simple_action_group_new ();
      g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);

      return G_ACTION_GROUP (group);
      }

      static GtkWidget* create_main_window (void)
      {
      GMenu *submenu;
      GtkWidget *grid;
      GMenu *menumodel;
      GtkWidget *window;
      GtkWidget *menubutton;

      //GSimpleAction *about_action;
      //GSimpleAction *new_action;
      //GSimpleAction *quit_action;

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
      gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);


      #define G_MENU_ATTRIBUTE_ACTION "win"

      GActionGroup * group = NULL;

      group = create_action_group(); // I don't know to link Gmenu and group


      /* Lorsque le shell affiche le menu de l'application, les actions de type "win"font référence à la copie
      * de cette action sur la fenêtre actuellement active.
      * Si une telle action n'existe pas, l'élément de menu sera grisé.
      * ===> c'est exactement ce que je constate à date */

      grid = gtk_grid_new ();

      menubutton = gtk_menu_button_new ();
      gtk_widget_set_size_request (menubutton, 80, 35);


      /* essai */
      gtk_widget_map(GTK_WIDGET(menubutton));

      gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
      gtk_container_add (GTK_CONTAINER (window), grid);

      menumodel = g_menu_new ();
      g_menu_append (menumodel, "New", "win.new");
      g_menu_append (menumodel, "About", "win.about");

      submenu = g_menu_new ();
      g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
      // from the menu, we refer to the action as "app.quit" since we installed it on the application
      g_menu_append (submenu, "Quit", "win.quit");
      gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));
      return window;
      }

      int main (int argc, char **argv)
      {
      GtkWidget *window = NULL;

      /* Initialisation de GTK+ */
      gtk_init (&argc, &argv);
      g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);

      /* Creation du conteneur principal */
      window = create_main_window();

      /* Affichage de la fenetre principale */
      gtk_widget_show_all (window);
      /* Lancement de la boucle principale */
      gtk_main ();
      return EXIT_SUCCESS;
      }


      The level of presentational information. Seems ok. Because it's the same that first example.
      Problem is purely functional.



      I don't fond howto link GAction ou GActiongroup instance group with the Gmenu as menumodel.



      Please help because I google some day without solution







      c gtk3






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 12:27









      ManmaxManmax

      5510




      5510
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I found solution only two command are necessary



          /* Implémentation of  GActionMap interface */
          GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
          /* namespace "win" created and linked with action*/
          gtk_widget_insert_action_group (window, "win", actions);

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

          //menubuttonexample_ter.c
          /* Compile with:
          * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
          */
          /** le même que menubuttonexample mais en version non Gtkapplication */

          /* Callback function for the undo action */
          static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
          {
          g_print ("You clicked "About"n");
          // not used
          (void)simple;
          (void)parameter;
          (void)user_data;
          }
          static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
          {
          g_print ("You clicked "New"n");
          // not used
          (void)simple;
          (void)parameter;
          (void)user_data;
          }

          static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
          {
          gtk_main_quit();
          // not used
          (void)simple;
          (void)parameter;
          (void)user_data;
          }

          static GtkWidget* create_main_window (void)
          {
          GMenu *submenu;
          GtkWidget *grid;
          GMenu *menumodel;
          GtkWidget *window;
          GtkWidget *menubutton;

          window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
          gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
          gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

          grid = gtk_grid_new ();

          menubutton = gtk_menu_button_new ();
          gtk_widget_set_size_request (menubutton, 80, 35);

          gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
          gtk_container_add (GTK_CONTAINER (window), grid);

          /* Implémentation de l'interface GActionMap, une interface exposant un mappage de noms d'action à des GActions*/
          GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
          /* création de l'espace de nom "win" qui va lier les actions et le widget principal window */
          gtk_widget_insert_action_group (window, "win", actions);

          const GActionEntry entries = {
          { "quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
          { "new", new_callback, NULL, NULL, NULL,{0,0,0} },
          { "about", about_callback ,NULL, NULL, NULL,{0,0,0} }
          };

          g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), window);

          /** partie couche de présentation **/
          /* création du premier menu */
          menumodel = g_menu_new ();

          g_menu_append (menumodel, "New", "win.new");
          g_menu_append (menumodel, "About","win.about");

          /** le sous-menu **/
          submenu = g_menu_new ();
          g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
          g_menu_append (submenu, "Quit", "win.quit");
          /** association du menu au GtkMenuButton **/
          gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

          return window;
          }

          int main (int argc, char **argv)
          {
          GtkWidget *window = NULL;
          /* Initialisation de GTK+ */
          gtk_init (&argc, &argv);
          g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);
          /* Creation du conteneur principal */
          window = create_main_window();
          /* Affichage de la fenetre principale */
          gtk_widget_show_all (window);
          /* Lancement de la boucle principale */
          gtk_main ();
          return EXIT_SUCCESS;
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360871%2fhowto-link-gmenu-and-gtkmenu-button-in-gtk-3%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I found solution only two command are necessary



            /* Implémentation of  GActionMap interface */
            GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
            /* namespace "win" created and linked with action*/
            gtk_widget_insert_action_group (window, "win", actions);

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

            //menubuttonexample_ter.c
            /* Compile with:
            * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
            */
            /** le même que menubuttonexample mais en version non Gtkapplication */

            /* Callback function for the undo action */
            static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
            {
            g_print ("You clicked "About"n");
            // not used
            (void)simple;
            (void)parameter;
            (void)user_data;
            }
            static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
            {
            g_print ("You clicked "New"n");
            // not used
            (void)simple;
            (void)parameter;
            (void)user_data;
            }

            static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
            {
            gtk_main_quit();
            // not used
            (void)simple;
            (void)parameter;
            (void)user_data;
            }

            static GtkWidget* create_main_window (void)
            {
            GMenu *submenu;
            GtkWidget *grid;
            GMenu *menumodel;
            GtkWidget *window;
            GtkWidget *menubutton;

            window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
            gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
            gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

            grid = gtk_grid_new ();

            menubutton = gtk_menu_button_new ();
            gtk_widget_set_size_request (menubutton, 80, 35);

            gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
            gtk_container_add (GTK_CONTAINER (window), grid);

            /* Implémentation de l'interface GActionMap, une interface exposant un mappage de noms d'action à des GActions*/
            GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
            /* création de l'espace de nom "win" qui va lier les actions et le widget principal window */
            gtk_widget_insert_action_group (window, "win", actions);

            const GActionEntry entries = {
            { "quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
            { "new", new_callback, NULL, NULL, NULL,{0,0,0} },
            { "about", about_callback ,NULL, NULL, NULL,{0,0,0} }
            };

            g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), window);

            /** partie couche de présentation **/
            /* création du premier menu */
            menumodel = g_menu_new ();

            g_menu_append (menumodel, "New", "win.new");
            g_menu_append (menumodel, "About","win.about");

            /** le sous-menu **/
            submenu = g_menu_new ();
            g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
            g_menu_append (submenu, "Quit", "win.quit");
            /** association du menu au GtkMenuButton **/
            gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

            return window;
            }

            int main (int argc, char **argv)
            {
            GtkWidget *window = NULL;
            /* Initialisation de GTK+ */
            gtk_init (&argc, &argv);
            g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);
            /* Creation du conteneur principal */
            window = create_main_window();
            /* Affichage de la fenetre principale */
            gtk_widget_show_all (window);
            /* Lancement de la boucle principale */
            gtk_main ();
            return EXIT_SUCCESS;
            }





            share|improve this answer




























              0














              I found solution only two command are necessary



              /* Implémentation of  GActionMap interface */
              GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
              /* namespace "win" created and linked with action*/
              gtk_widget_insert_action_group (window, "win", actions);

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

              //menubuttonexample_ter.c
              /* Compile with:
              * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
              */
              /** le même que menubuttonexample mais en version non Gtkapplication */

              /* Callback function for the undo action */
              static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
              {
              g_print ("You clicked "About"n");
              // not used
              (void)simple;
              (void)parameter;
              (void)user_data;
              }
              static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
              {
              g_print ("You clicked "New"n");
              // not used
              (void)simple;
              (void)parameter;
              (void)user_data;
              }

              static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
              {
              gtk_main_quit();
              // not used
              (void)simple;
              (void)parameter;
              (void)user_data;
              }

              static GtkWidget* create_main_window (void)
              {
              GMenu *submenu;
              GtkWidget *grid;
              GMenu *menumodel;
              GtkWidget *window;
              GtkWidget *menubutton;

              window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
              gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
              gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

              grid = gtk_grid_new ();

              menubutton = gtk_menu_button_new ();
              gtk_widget_set_size_request (menubutton, 80, 35);

              gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
              gtk_container_add (GTK_CONTAINER (window), grid);

              /* Implémentation de l'interface GActionMap, une interface exposant un mappage de noms d'action à des GActions*/
              GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
              /* création de l'espace de nom "win" qui va lier les actions et le widget principal window */
              gtk_widget_insert_action_group (window, "win", actions);

              const GActionEntry entries = {
              { "quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
              { "new", new_callback, NULL, NULL, NULL,{0,0,0} },
              { "about", about_callback ,NULL, NULL, NULL,{0,0,0} }
              };

              g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), window);

              /** partie couche de présentation **/
              /* création du premier menu */
              menumodel = g_menu_new ();

              g_menu_append (menumodel, "New", "win.new");
              g_menu_append (menumodel, "About","win.about");

              /** le sous-menu **/
              submenu = g_menu_new ();
              g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
              g_menu_append (submenu, "Quit", "win.quit");
              /** association du menu au GtkMenuButton **/
              gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

              return window;
              }

              int main (int argc, char **argv)
              {
              GtkWidget *window = NULL;
              /* Initialisation de GTK+ */
              gtk_init (&argc, &argv);
              g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);
              /* Creation du conteneur principal */
              window = create_main_window();
              /* Affichage de la fenetre principale */
              gtk_widget_show_all (window);
              /* Lancement de la boucle principale */
              gtk_main ();
              return EXIT_SUCCESS;
              }





              share|improve this answer


























                0












                0








                0







                I found solution only two command are necessary



                /* Implémentation of  GActionMap interface */
                GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
                /* namespace "win" created and linked with action*/
                gtk_widget_insert_action_group (window, "win", actions);

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

                //menubuttonexample_ter.c
                /* Compile with:
                * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
                */
                /** le même que menubuttonexample mais en version non Gtkapplication */

                /* Callback function for the undo action */
                static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
                {
                g_print ("You clicked "About"n");
                // not used
                (void)simple;
                (void)parameter;
                (void)user_data;
                }
                static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
                {
                g_print ("You clicked "New"n");
                // not used
                (void)simple;
                (void)parameter;
                (void)user_data;
                }

                static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
                {
                gtk_main_quit();
                // not used
                (void)simple;
                (void)parameter;
                (void)user_data;
                }

                static GtkWidget* create_main_window (void)
                {
                GMenu *submenu;
                GtkWidget *grid;
                GMenu *menumodel;
                GtkWidget *window;
                GtkWidget *menubutton;

                window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
                gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
                gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

                grid = gtk_grid_new ();

                menubutton = gtk_menu_button_new ();
                gtk_widget_set_size_request (menubutton, 80, 35);

                gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
                gtk_container_add (GTK_CONTAINER (window), grid);

                /* Implémentation de l'interface GActionMap, une interface exposant un mappage de noms d'action à des GActions*/
                GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
                /* création de l'espace de nom "win" qui va lier les actions et le widget principal window */
                gtk_widget_insert_action_group (window, "win", actions);

                const GActionEntry entries = {
                { "quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
                { "new", new_callback, NULL, NULL, NULL,{0,0,0} },
                { "about", about_callback ,NULL, NULL, NULL,{0,0,0} }
                };

                g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), window);

                /** partie couche de présentation **/
                /* création du premier menu */
                menumodel = g_menu_new ();

                g_menu_append (menumodel, "New", "win.new");
                g_menu_append (menumodel, "About","win.about");

                /** le sous-menu **/
                submenu = g_menu_new ();
                g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
                g_menu_append (submenu, "Quit", "win.quit");
                /** association du menu au GtkMenuButton **/
                gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

                return window;
                }

                int main (int argc, char **argv)
                {
                GtkWidget *window = NULL;
                /* Initialisation de GTK+ */
                gtk_init (&argc, &argv);
                g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);
                /* Creation du conteneur principal */
                window = create_main_window();
                /* Affichage de la fenetre principale */
                gtk_widget_show_all (window);
                /* Lancement de la boucle principale */
                gtk_main ();
                return EXIT_SUCCESS;
                }





                share|improve this answer













                I found solution only two command are necessary



                /* Implémentation of  GActionMap interface */
                GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
                /* namespace "win" created and linked with action*/
                gtk_widget_insert_action_group (window, "win", actions);

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

                //menubuttonexample_ter.c
                /* Compile with:
                * gcc -std=c11 -Wall -fmax-errors=10 -Wextra menubuttonexample_ter.c -o menubuttonexample_ter `pkg-config --cflags --libs gtk+-3.0 `
                */
                /** le même que menubuttonexample mais en version non Gtkapplication */

                /* Callback function for the undo action */
                static void about_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
                {
                g_print ("You clicked "About"n");
                // not used
                (void)simple;
                (void)parameter;
                (void)user_data;
                }
                static void new_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
                {
                g_print ("You clicked "New"n");
                // not used
                (void)simple;
                (void)parameter;
                (void)user_data;
                }

                static void quit_callback (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
                {
                gtk_main_quit();
                // not used
                (void)simple;
                (void)parameter;
                (void)user_data;
                }

                static GtkWidget* create_main_window (void)
                {
                GMenu *submenu;
                GtkWidget *grid;
                GMenu *menumodel;
                GtkWidget *window;
                GtkWidget *menubutton;

                window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
                gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example");
                gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

                grid = gtk_grid_new ();

                menubutton = gtk_menu_button_new ();
                gtk_widget_set_size_request (menubutton, 80, 35);

                gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1);
                gtk_container_add (GTK_CONTAINER (window), grid);

                /* Implémentation de l'interface GActionMap, une interface exposant un mappage de noms d'action à des GActions*/
                GActionGroup *actions =(GActionGroup*)g_simple_action_group_new ();
                /* création de l'espace de nom "win" qui va lier les actions et le widget principal window */
                gtk_widget_insert_action_group (window, "win", actions);

                const GActionEntry entries = {
                { "quit", quit_callback, NULL, NULL, NULL,{0,0,0} },
                { "new", new_callback, NULL, NULL, NULL,{0,0,0} },
                { "about", about_callback ,NULL, NULL, NULL,{0,0,0} }
                };

                g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), window);

                /** partie couche de présentation **/
                /* création du premier menu */
                menumodel = g_menu_new ();

                g_menu_append (menumodel, "New", "win.new");
                g_menu_append (menumodel, "About","win.about");

                /** le sous-menu **/
                submenu = g_menu_new ();
                g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu));
                g_menu_append (submenu, "Quit", "win.quit");
                /** association du menu au GtkMenuButton **/
                gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel));

                return window;
                }

                int main (int argc, char **argv)
                {
                GtkWidget *window = NULL;
                /* Initialisation de GTK+ */
                gtk_init (&argc, &argv);
                g_print("Version de gtk: %d.%d.%dn", gtk_major_version, gtk_minor_version, gtk_micro_version);
                /* Creation du conteneur principal */
                window = create_main_window();
                /* Affichage de la fenetre principale */
                gtk_widget_show_all (window);
                /* Lancement de la boucle principale */
                gtk_main ();
                return EXIT_SUCCESS;
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 21:39









                ManmaxManmax

                5510




                5510






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360871%2fhowto-link-gmenu-and-gtkmenu-button-in-gtk-3%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    How to fix TextFormField cause rebuild widget in Flutter