17.7 MINIMALIST PROGRAMS IN GNOMEGTK


17.7 MINIMALIST PROGRAMS IN GNOME/GTK+

A GUI program in GNOME/GTK+ starts most commonly by invoking the function gnome_init for the initialization of both GNOME and GTK+. As with Qt, the top-level main is written as if we want to use it with command line arguments, that is with the parameters argc and argv as shown in line (A) of the program below. The compiler synthesizes these command line arguments and then passes them on to gnome_init in line (C). Regarding the syntax of the call to gnome_init in line (C), the first argument supplies a symbolic name you'd want GNOME to associate with the graphical application being created, and the second a version number, in the form of a character string. As mentioned already, the last two arguments acquire compiler-synthesized values from the parameters of main.

The initialization step is usually followed by the construction of a top-level window by invoking gnome_app_new().[13] In the following program, the top-level window is constructed in line (D). While the first argument here is the name of the application declared in line (C), the second a text string that will be displayed in the title bar of the top-level window,

 
//FirstWindow.c #include <gnome.h> int main( int argc, char* argv[] ) //(A) { GtkWidget* topLevelWindow; //(B) gnome_init( "gnomewin", "1.0", argc, argv ); //(C) topLevelWindow = gnome_app_new( "gnomewin", "Gnome Window" ); //(D) gtk_container_set_border_width( GTK_CONTAINER( topLevelWindow ), 200); //(E) gtk_widget_show( topLevelWindow ); //(F) gtk_main(); //(G) exit( 0 ); //(H) }

In line (E), we establish the size of the window by invoking the function gtk_container_set_border_width(). Note how the first argument to this function call has a C style cast from GtkWidget* to GTK_CONTAINER. The function called demands that its first argument be of type GTK_CONTAINER. The command in line (F) then displays the top-level window on a terminal.

As we will see later in greater detail, the statement in line (G) is very important to a GNOME/GTK+ program because it is the invocation of gtk_main() that starts the event processing loop. Even though for this first GNOME/GTK+ exercise we do not wish to provide for user interaction, we must still include this statement since otherwise the displayed window will get destroyed the moment it is created. Calling gtk_main() parks the flow of control in an infinite loop and the window stays visible. The event processing loop can only be terminated by invoking gtk_main_quit(). Since we have not included that call in our program, even if we close the displayed window the program will keep on running in the background and you'll have to kill the process to terminate the program.

The compilation of the above program-as of all GNOME/GTK+ programs-is best done with the help of a makefile. The developers of GNOME/GTK+ have provided a utility program called gnome-config that can automatically figure out the locations of the libraries needed for compilation and dynamic linking. To illustrate, the command line invocation

      gnome-config --cflags gnomeui 

returns the following string for the locations of the ‘include' files needed for the compilation of a GNOME program

      -I/usr/include -DNEED_GNOMESUPPORT_H -I/usr/lib/gnome-libs/include      -I/usr/lib/glib/include -I/usr/X11R6/include 

Shown below is a makefile that would work for the program FirstWindow.c:

 
#Makefile_GTK_FirstWindow CC=gcc LDLIBS='gnome-config --libs gnomeui' CFLAGS=-Wall -g 'gnome-config --cflags gnomeui' FirstWindow: FirstWindow.o Makefile_FirstWindow $(CC) $(LDLIBS) FirstWindow.o -o FirstWindow FirstWindow.o: FirstWindow.c $(CC) $(CFLAGS) -c FirstWindow.c clean: rm -f FirstWindow rm -f FirstWindow.o

assuming that you want to use the Gnu C compiler gcc. Figure 17.7 shows the window created by FirstWindow.c.

click to expand
Figure 17.7

The above example did not incorporate any human interaction. Paralleling our minimalist examples for Java and Qt, we now present a GNOME/GTK+ program that contains a button which when pushed will terminate the program.

 
//FirstWindowWithButton.c #include <gnome.h> gint eventDestroy( GtkWidget* widget, GdkEvent* event, gpointer data ); //(A) int main( int argc, char* argv[] ) //(B) { GtkWidget* topLevelWindow; //(C) GtkWidget* myButton; //(D) gnome_init( "buttonwin", "1.0", argc, argv ); //(E) topLevelWindow = gnome_app_new( "buttonwin", "My Window" ); //(F) gtk_container_set_border_width( GTK_CONTAINER( topLevelWindow ), 100 ); //(G) myButton = gtk_button_new_with_label( "Quit" ); //(H) gtk_signal_connect( GTK_OBJECT( myButton ), //(I) "clicked", GTK_SIGNAL_FUNC( eventDestroy ), NULL ); gnome_app_set_contents( GNOME_APP( topLevelWindow ), myButton ); //(J) gtk_widget_show( topLevelWindow ); //(K) gtk_main(); //(L) exit( 0 ); //(M) } gint eventDestroy( GtkWidget* widget, GdkEvent* event, gpointer data ) { //(N) gtk_main_quit(); //(O) return( 0 ); //(P) }

As in our first GNOME/GTK+ program, we first carry out the initialization in line (E), followed by the construction of a top-level main window in line (F) and then the setting of the size of the top-level window in line (G). In line (H), we construct a button object by invoking its constructor gtk_button_new_with_label and provide the constructor with a label to put on the button.

Line (I) is crucial to how this program will respond to an interaction with the button. This line registers what's known as a callback function or just callback-in this case this function is eventDestroy()-with the signal "clicked" that is emitted by a button. We will have more to say about signals and callbacks later in Section 17.14. At this point, all we want the reader to do is to look at the definition of the callback function in lines (N) through (P). This function invokes gtk_main_quit(). While the function gtk_main() invoked in line (L) starts the event processing loop, the invocation of gtk_mail_quit() in line (O) terminates it. Termination of the event processing loop causes the control to shift back to line (M), which brings the execution to an end.

To compile this program, you'd again need the makefile that was shown for the program FirstWindow.c after it is changed to reflect the name of the new source file, the executable, and so on. The window created by this program is shown in Figure 17.8.

click to expand
Figure 17.8

[13]This function actually calls gtk_window_new() using GTK_WINDOW_TOPLEVEL as its argument.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net