Setting up the menu button name (GMENU)

Hello this is my first post here and I salute you all.

I am learning GTK3 and for this I need to create a Menu with the Help of GMenu.
Everything works fine, but I can not figure out how to name the menu button which appears, like in my case Application.
At this point I have :

Application:
            Open
            Quit

I have two Questions:

  • from were comes the Application menu name.

  • how do I change it in something else, for example in File.

    File:
          Open
          Quit
    

Here is a Picture:
GMenu-Menu

And here is the code I have so far:

#include <gtk/gtk.h>

static void open_app ( GSimpleAction *action, GVariant *parameter, gpointer user_data )
{
    g_print ( "Opening...\n" );
}

static void quit_app ( GSimpleAction *simple, GVariant *parameter, gpointer user_data )
{
    GApplication *application = user_data;
    g_application_quit ( application );

    g_print ( "GodBye.\n" );
}

static void startup ( GtkApplication *app )
{
    GActionMap  *action_map;
    const gchar *const open_accels[]  = { "<Ctrl>O", NULL };
    const gchar *const quit_accels[]  = { "<Ctrl>X", NULL };

    static const GActionEntry actions[] =
    {
        { "open", open_app, NULL, NULL, NULL, { 0, 0, 0 } },
        { "quit", quit_app, NULL, NULL, NULL, { 0, 0, 0 } },
    };

    action_map   = G_ACTION_MAP ( app );

    gtk_application_set_accels_for_action ( GTK_APPLICATION ( app ), "app.open", open_accels );
    gtk_application_set_accels_for_action ( GTK_APPLICATION ( app ), "app.quit", quit_accels );
    g_action_map_add_action_entries ( action_map, actions, G_N_ELEMENTS ( actions ), app );
}


static void activate ( GtkApplication *app )
{
    GtkWidget *window;
    GMenu     *menu;

    window = gtk_application_window_new ( app );
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 200 );

    gtk_window_set_application ( GTK_WINDOW ( window ), GTK_APPLICATION ( app ) );
    gtk_window_set_title ( GTK_WINDOW ( window ), "Example" );

    menu = g_menu_new ();

    g_menu_append ( menu, "Open", "app.open" );
    g_menu_append ( menu, "Quit", "app.quit" );

    gtk_application_set_app_menu ( app, G_MENU_MODEL ( menu ) );

    gtk_widget_show_all ( GTK_WIDGET ( window ) );
    g_object_unref ( menu );
}

int main ( void )
{
    GtkApplication *app;
    int status;

    app = gtk_application_new  ( "org.gtk.example", G_APPLICATION_FLAGS_NONE );
    g_signal_connect_swapped   ( app, "startup",  G_CALLBACK ( startup ),  app );
    g_signal_connect_swapped   ( app, "activate", G_CALLBACK ( activate ), app );
    status = g_application_run ( G_APPLICATION ( app ), 0, NULL );
    g_object_unref ( app );

    return status;
}
1 Like

g_set_application_name

However you probably want to use menubar not app-menu

1 Like

Indeed the menubar I was needed and not the app-menu
Now everything works fine.
Thank you

EDIT:
I was following This Tutorial

The application menu from the early 3.x days is getting retired.

If you don’t have a menu bar, you should look into using a primary menu button instead.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.