I'm trying to create a basic menubar. I've tried some sample code found on the internet and nothing seems to work. It all compiles but the menuBar simply doesn't appear when I run the program. The following two pieces of code only produce a blank window. What could be wrong?
Any help is greatly appreciated.
#include <gtk/gtk.h>
static void
quit_activated(GSimpleAction *action, GVariant *parameter, gpointer user_data) {
GApplication *app = G_APPLICATION (user_data);
g_application_quit (app);
}
static void
app_activate (GApplication *app, gpointer user_data) {
GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
gtk_window_set_title (GTK_WINDOW (win), "menu1");
gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
GSimpleAction *act_quit = g_simple_action_new ("quit", NULL);
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_quit));
g_signal_connect (act_quit, "activate", G_CALLBACK (quit_activated), app);
GMenu *menubar = g_menu_new ();
GMenuItem *menu_item_menu = g_menu_item_new ("Menu", NULL);
GMenu *menu = g_menu_new ();
GMenuItem *menu_item_quit = g_menu_item_new ("Quit", "app.quit");
g_menu_append_item (menu, menu_item_quit);
g_object_unref (menu_item_quit);
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));
/* gtk_widget_show (win); is also OKay instead of gtk_window_present. */
}
#define APPLICATION_ID "com.github.ToshioCP.menu1"
int
main (int argc, char **argv) {
GtkApplication *app;
int stat;
app = gtk_application_new (APPLICATION_ID, G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL);
stat =g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return stat;
}
#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;
}