4

I created a simple C application using GTK4 that has a window, a menubar, a menu, and a menu item. I would like the menu to read "File" and the menu item underneath File to read "Connect". How can I code the menus and menubar so that I have a menu File->Connect?

#include <gtk/gtk.h>

static void connect(GApplication *app, gpointer *data) {
    puts("Connect menu item clicked.\n");
}

static void activate(GApplication *app, gpointer *data) {
    GtkWidget *win = gtk_application_window_new(GTK_APPLICATION(app));
    gtk_window_set_title(GTK_WINDOW(win), "IRC Client");
    gtk_window_set_default_size(GTK_WINDOW(win), 800, 800);

    GSimpleAction *act_connect = g_simple_action_new("Connect", NULL);
    g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(act_connect));
    g_signal_connect(act_connect, "activate", G_CALLBACK(connect), NULL);

    GMenu *menubar = g_menu_new();
    GMenuItem *menu_item_menu = g_menu_item_new("File", NULL);
    GMenu *menu = g_menu_new();
    GMenuItem *menu_item_connect = g_menu_item_new("Connect", "app.connect");
    g_menu_append_item(menu, menu_item_connect);
    g_object_unref(menu_item_connect);
    g_menu_item_set_submenu(menu_item_menu, G_MENU_MODEL(menu));
    g_menu_append_item(menubar, menu_item_menu);
    g_object_unref(menu_item_menu);

    gtk_application_set_menubar(GTK_APPLICATION(app), G_MENU_MODEL(menubar));
    gtk_application_window_set_show_menubar(GTK_APPLICATION_WINDOW(win), TRUE);
    gtk_window_present(GTK_WINDOW(win));
}

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

    app = gtk_application_new("com.ircclient", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    stat = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return stat;
}
underscore_d
  • 6,309
  • 3
  • 38
  • 64
ktm5124
  • 11,861
  • 21
  • 74
  • 119
  • Removal of the menu and toolbar widgets in Gtk+3 wasn't the stroke of genius it was supposed to be. Comparatively, it's frustratingly difficult to style the stand-in menu in Gtk4 with icons, accelerators, spacing and themes. At least that has been my experience. – David C. Rankin Aug 06 '23 at 02:38

1 Answers1

2

First let us start with your connect callback function, its prototype should look like this:

static void connect ( GSimpleAction *simple_action, GVariant *parameter, gpointer *data 

After that, you have one small problem, and you are good to go. Here on this line:

GSimpleAction *act_connect = g_simple_action_new("Connect", NULL);

This is your first parameter called "Connect".

And here on this line:

GMenuItem *menu_item_connect = g_menu_item_new("Connect", "app.connect");

The second parameter is "app.connect", now if you compare "Connect"with "app.connect" you will notice the mistake.

It is case-sensitive, just replace "Connect" with "connect" and you should be fine.

Now to your question, you need another menu for that, try to understand the following Demo:

#include <gtk/gtk.h>

static void action_clbk ( GSimpleAction *simple_action, G_GNUC_UNUSED GVariant *parameter, G_GNUC_UNUSED gpointer *data )
{
    g_print ( "The action %s was clicked.\n", g_action_get_name ( G_ACTION ( simple_action ) ) );
}

static void activate ( GApplication *app, G_GNUC_UNUSED gpointer *data )
{
    GtkWidget *win;
    GSimpleAction *act_connect;
    GSimpleAction *act_disconnect;

    /// ***
    GMenu *menu_bar;
    GMenu *network_menu;
    GMenu *server_menu;

    /// ***
    GMenuItem *menu_item_connect;
    GMenuItem *menu_item_disconnect;

    /// *** Menu Bar
    menu_bar = g_menu_new();

    /// *** Network_Menu
    network_menu = g_menu_new();
    g_menu_append_submenu ( menu_bar, "Network", G_MENU_MODEL ( network_menu ) );

    /// *** Server_Menu
    server_menu = g_menu_new();
    g_menu_append_submenu ( network_menu, "Server", G_MENU_MODEL ( server_menu ) );
    /// ***
    win = gtk_application_window_new ( GTK_APPLICATION ( app ) );
    gtk_window_set_title ( GTK_WINDOW ( win ), "IRC Client" );
    gtk_window_set_default_size ( GTK_WINDOW ( win ), 400, 400 );

    /// *** Create Connect and Disconnect Actions
    act_connect    = g_simple_action_new ( "connect", NULL );
    act_disconnect = g_simple_action_new ( "disconnect", NULL );

    /// *** Add them to the ActionMap
    g_action_map_add_action ( G_ACTION_MAP ( app ), G_ACTION ( act_connect ) );
    g_action_map_add_action ( G_ACTION_MAP ( app ), G_ACTION ( act_disconnect ) );

    /// *** Connect them to the activate Signal
    g_signal_connect ( act_connect,    "activate", G_CALLBACK ( action_clbk ), NULL );
    g_signal_connect ( act_disconnect, "activate", G_CALLBACK ( action_clbk ), NULL );

    /// *** Create the Connect Item
    menu_item_connect = g_menu_item_new ( "Connect", "app.connect" );
    g_menu_append_item ( server_menu, menu_item_connect );

    /// *** Create the Disconnect Item
    menu_item_disconnect = g_menu_item_new ( "Disconnect", "app.disconnect" );
    g_menu_append_item ( server_menu, menu_item_disconnect );

    /// ***
    gtk_application_set_menubar             ( GTK_APPLICATION ( app ), G_MENU_MODEL ( menu_bar ) );
    gtk_application_window_set_show_menubar ( GTK_APPLICATION_WINDOW ( win ), TRUE );

    /// ***
    gtk_window_present ( GTK_WINDOW ( win ) );

    /// *** Clean
    g_object_unref ( act_connect );
    g_object_unref ( act_disconnect );
    g_object_unref ( menu_item_connect );
    g_object_unref ( menu_item_disconnect );
    g_object_unref ( server_menu );
    g_object_unref ( network_menu );
    g_object_unref ( menu_bar );
}

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

    /// ***
    app = gtk_application_new ( "com.ircclient", G_APPLICATION_FLAGS_NONE );
    g_signal_connect ( app, "activate", G_CALLBACK ( activate ), NULL );

    /// ***
    stat = g_application_run ( G_APPLICATION ( app ), argc, argv );
    g_object_unref ( app );

    /// ***
    return stat;
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
Michi
  • 5,175
  • 7
  • 33
  • 58