Improved Hello World
Let's take a look at the slightly improved HelloWorld and better callback function examples. Here is the next topic, assemble the components.
#include
/ * Our new improvement callback function. Print the data to the standard output (stdout). * / void callback (gtkwidget * widget, gpointer data) {g_print ("Hello Again -% s Was Pressed / N", (Gchar *) DATA);
/ * Another callback function * / gint delete_event (gtkwidget * widget, gdkevent * Event, gpointer data) {gtk_main_quit (); returnaf false;}
INT Main (int Argc, char * argv []) {/ * gtkwidget is the storage type of the component * / gtkwidget * window; gtkwidget * button; gtkwidget * box1;
/ * This function is called in all GTK programs. The parameters are resolved from the command line and sent to the program. * / Gtk_init (& argc, & argv);
/ * Create a new window * / window = gtk_window_new (gtk_window_toplevel);
/ * This is a new call, the window title is "Hello Buttons!" * / Gtk_window_set_title (gtk_window (window), "Hello Buttons!");
/ * In this we set a handler for delete_event. * / G_signal_connect (g_object (window), "delete_event", g_callback (delete_event), null;
/ * Set the width of the window border * / gtk_container_set_border_width (GTK_CONTAINER (Window), 10);
/ * We created a assembly box. See the "Assembly" chapter for details. * We can't see the assembly box, which is only a tool for the arrangement of components. * / Box1 = gtk_hbox_new (false, 0);
/ * Put the assembly box into the main window. * / Gtk_container_add (gtk_container (window), Box1);
/ * Create a new button with a label as "Button 1". * / Button = gtk_button_new_with_label ("Button 1"); / * Set the callback function. * / G_signal_connect (g_object (button), "clicked", g_callback (callback), "Button 1");
/ * We put the button into an invisible assembly box that has been placed in the window. * / Gtk_box_pack_start (gtk_box (box1), button, true, true, 0);
/ * We have completed the preparations made for this button, and you can display it. * / GTK_WIDGET_SHOW (Button);
/ * The second button is also created. * / Button = gtk_button_new_with_label ("Button 2"); / * Touch the same callback function with different parameters, replaced with pointers to "Button 2". * / G_signal_connect (g_object (button), "clicked", g_callback (callback), "Button 2");
GTK_BOX_PACK_START (GTK_BOX (Box1), Button, True, True, 0);
/ * The order of display is not important, but I suggest the last display window. This will display it at the same time. * / GTK_WIDGET_SHOW (Button);
GTK_WIDGET_SHOW (BOX1);
GTK_WIDGET_SHOW (Window); / * Stop waiting for the event. * / Gtk_main ();
Return 0;}
Compile this program, you will find this program can't quit, you have to use the window manager or the command line to kill it. Insert a button for an exit program is a good practice to the reader.