Programming and Graphics

(Kiana) #1

8.5 GUI programming withGtk+ 285


Figure 8.5.1 A window produced by the graphics toolkit GTK+. When the button
is clicked, the window disappears.

gtkinit (&argc, &argv);
window = gtkwindownew (GTKWINDOWTOPLEVEL);
gtkcontainersetborderwidth (GTKCONTAINER (window), 100);
button = gtkbuttonnewwithlabel ("Two horses: Jeremy and Zebediah");
gtkcontaineradd (GTKCONTAINER (window), button);

gsignalconnectswapped (GOBJECT (button), "clicked",
GCALLBACK (gtkwidgetdestroy),
GOBJECT (window));
gtkwidgetshow (button);
gtkwidgetshow (window);
gtkmain ();

return 0;
}

GtkWidgetin the fifth and sixth lines is the class type for theGtk+widgets.
Running the code produces the window shown in Figure 8.5.1.


Note the similarities with theGlutdrawing code. Most of theGtk+
commands are self-explanatory. The first and last commands call the functions:


gtkinit(&argc, &argv)


This function parses the window-specific parameters transmitted to the
foundation graphics library or server.

gtkmain()


This function launches the graphics display in an infinite loop that can
be interrupted only by certain events.

InGtk+programming, we first register the callbacks (widgets) and then start

Free download pdf