/* An example of a multi-window application using GTK+ and GLADE. Written by Keith Sharp , Fri 04 Jun 2004 15:20:01 BST Note that a real preferences dialog would use GConf to store and retrieve values This code is Public Domain - do what you want with it. To compile: gcc -o app app.c `pkg-config --cflags --libs gtk+-2.0 libglade-2.0` */ #include #include /* Callback for the delete-event signal, do nothing but return FALSE so that the destroy signal is emitted, and do the work in that callback */ gboolean delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data) { return FALSE; } /* Callback for the destroy signal, all we do is quit. Other apps might do something more complex */ void destroy_event_cb (GtkWidget *widget, gpointer data) { gtk_main_quit (); } /* Callback for clicking on the prefs close button. All we do is hide the widget for later reuse */ void close_button_cb (GtkButton *button, gpointer data) { gtk_widget_hide (GTK_WIDGET(data)); } /* Callback for the activate signal on the entry widget. We connect to this so that we can do instant apply which is the recommendation for preferences in GNOME */ void entry_activate_cb (GtkEntry *entry, gpointer data) { const gchar *name; name = gtk_entry_get_text (entry); g_print ("Name: %s\n", name); } /* Callback for the clicked signal on the cancel button. All we do is hide the widget for later reuse */ void cancel_button_cb (GtkButton *button, gpointer data) { gtk_widget_hide (GTK_WIDGET(data)); } /* Callback for the clicked signal on the quit button. All we do is quit */ void quit_button_cb (GtkButton *button, gpointer data) { gtk_main_quit (); } /* Callback for the activate signal on the quit menu item. We show the should we quit window */ void quit_menu_cb (GtkMenuItem *menuitem, gpointer data) { gtk_widget_show_all (GTK_WIDGET(data)); } /* Callback for the activate signal on the prefs menu item. We show the preferences window */ void pref_menu_cb (GtkMenuItem *menuitem, gpointer data) { gtk_widget_show_all (GTK_WIDGET(data)); } /* The main function, initialise GTK, load the GLADE file, hook up the callbacks, and into the main loop */ gint main (gint argc, gchar *argv[]) { GladeXML *xml; GtkWidget *main, *quitmenu, *prefmenu; GtkWidget *prefs, *close, *entry; GtkWidget *quit, *cancelbutton, *quitbutton; /* All GTK apps need to initialise GTK */ gtk_init(&argc, &argv); /* Load the GLADE file, exit if this fails. */ xml = glade_xml_new ("app.glade", NULL, NULL); if (!xml) g_error ("Failed to load glade file\n"); /* First we setup the preferences window */ /* Find the widget for the prefs window */ prefs = glade_xml_get_widget (xml, "prefs-win"); if (!prefs) g_error ("Could not get prefs window widget\n"); /* Find the close button widget and connect up the clicked signal handler */ close = glade_xml_get_widget (xml, "close-button"); if (!close) g_error ("Could not get close button widget\n"); g_signal_connect (G_OBJECT(close), "clicked", G_CALLBACK(close_button_cb), prefs); /* Find the entry widget and connect up the activate signal handler */ entry = glade_xml_get_widget (xml, "name-entry"); if (!entry) g_error ("Could not get username entry widget\n"); g_signal_connect (G_OBJECT(entry), "activate", G_CALLBACK(entry_activate_cb), NULL); /* Now we setup the quit window */ /* Get the quit window widget */ quit = glade_xml_get_widget (xml, "quit-win"); if (!quit) g_error ("Could noy get quit window widget\n"); /* Find the quit button widget and connect the signal handler */ quitbutton = glade_xml_get_widget (xml, "quit-button"); if (!quitbutton) g_error ("Could not get quit button widget\n"); g_signal_connect (G_OBJECT(quitbutton), "clicked", G_CALLBACK(quit_button_cb), NULL); /* Find the cancel button widget and connect the signal handler */ cancelbutton = glade_xml_get_widget (xml, "cancel-button"); if (!cancelbutton) g_error ("Could not get cancel button widget\n"); g_signal_connect (G_OBJECT(cancelbutton), "clicked", G_CALLBACK(cancel_button_cb), quit); /* Finally we setup the main window */ /* Get the main window widget and connect destroy and delete signal handers */ main = glade_xml_get_widget (xml, "main-win"); if (!main) g_error ("Could not get main window widget\n"); g_signal_connect (G_OBJECT(main), "delete-event", G_CALLBACK(delete_event_cb), NULL); g_signal_connect (G_OBJECT(main), "destroy", G_CALLBACK(destroy_event_cb), NULL); /* Get the quit menu option and connect signal handler */ quitmenu = glade_xml_get_widget (xml, "quit-menu"); if (!quitmenu) g_error ("Could not get quit menu widget\n"); g_signal_connect (G_OBJECT(quitmenu), "activate", G_CALLBACK(quit_menu_cb), quit); /* Get the prefs menu option and connect signal handler */ prefmenu = glade_xml_get_widget (xml, "pref-menu"); if (!prefmenu) g_error ("Could not get pref menu widget\n"); g_signal_connect (G_OBJECT(prefmenu), "activate", G_CALLBACK(pref_menu_cb), prefs); /* Enter the main loop */ gtk_main (); /* We are finished, lets go home */ return 0; }