only for RuBoard - do not distribute or recompile |
This section repeats the HelloWorld non-application you used in Chapter 2, GTK+ for VB Developers, but this time, it uses Glade.
One of the primary strengths ”some would say the most important strength ”of VB is that the IDE abstracted the user interface from the work code behind the scenes. It allowed the developer to click and drag the visual part of the application into place, without having to compile and run the application to see how it looks, only how it works. As you saw in Chapter 2, most of the code was concerned with setting up and configuring how the application would present itself visually when it first started. And that was a very simple screen.
The following steps take you through creating a new application using Glade.The result will be a new version of HelloWorld. Of course, if you normally run your Linux box from the command line, you will need to fire up your Xwindows system in order to use Glade.
First, create a directory. Go to the place you will want to keep your project and create a subdirectory for it. At the command line, enter this command:
% mkdir HelloWorld
From the command line, type
% glade
or find it in the Start menu if it was installed with the operating system. On my system, it is under Development. If you are using KDE, it may be under the Gnome subdirectory.
If you have Glade installed, three windows will pop up: Glade: <untitled>, Palette, and Properties: <none> (see Figure 5.1).
If you type glade and don t get the windows shown, see the section Where to Get Glade and How to Install.
In the Glade Project Window, select Options (see Figure 5.2).
With the Options window open , set the Project Directory to the directory you created in Step 1. This is very important because otherwise it will set to a default, probably in your home directory. Set the Project Name to HelloWorld (and the program name ”if it is different). See Figure 5.3 for an example.
You can also set Enable Gnome Support to FALSE if you choose. (By default, it is set to TRUE on my version of Glade.) However, because this book is not Gnome-specific, it won t be necessary.
In the Palette window, under GTK+ Basic, click the top left window. Window1 appears (see Figure 5.4).
In the Properties window, select the Widget tab and set the Name of the window to frm_main . For the title, enter HelloWorld (see Figure 5.5).
Returning to the Palette, select button (it s the third one down on the left on my version). Then position the mouse over the center of the window area and click. A button should fill the space; after all, the window is a container widget, and GTK+ defaults to filling the container space (see Figure 5.6).
Returning to the Properties window, make sure the properties displayed are for the command button you just created. Set the name to cmd_main and the label to HelloWorld<ret>Click to Close. (see Figure 5.7). (The <ret> is for the Return key. From Glade, you don t have to insert the \n newline character.)
Select the Signals tab (make sure the widget that s selected is cmd_main ). Then select the Signal box (see Figure 5.8). See Chapter 2 for a discussion of Signals and Callbacks.
In the Signals dialog box, select the clicked signal (see Figure 5.9).
Click OK, and then click Add. It is important for you to remember that the signal is not added to the widget as a callback until you click the Add button. After you click Add, the clicked signal appears listed in the Signals tab for the command button widget (see Figure 5.10).
Return to the Glade Project window. Click the Save button, and a Project Saved message should appear in the status bar. This saves your code in a *.glade file (unless you changed the settings from their default), which is an XML list of widgets, their properties, and the widget hierarchy tree. (This will be covered later in this chapter; see Listing 5.7.)
Next, click the Build button (on my version, it has interlocking gears for the icon). A message appears in the status bar saying Source Code Written (see Figure 5.11).
This writes code to four separate files and places them into a subdirectory of your project directory called src (for source ). The four files are named interface.c, support.c, main.c, and callbacks.c. A full listing of each of these files will be covered shortly, but each one is briefly described here:
File | Contents |
---|---|
interface.c | The windows and associated widgets, as functions to create and show |
support.c | Functions that allow easier interaction with the way Glade operates |
main.c | The program main |
callbacks.c | Blank functions for signals specified in the Signals tab of the Properties window |
Close Glade. At the Command line, move to the Hello World project directory and give the following command:
% ls -l -R
This produces a listing of the files from this point down in the directory tree. The -l flag tells the ls command to return information in the long format, and the -R tells it to recursively display information for all subdirectories. Listing 5.1 shows the results of the ls command.
01 .: 02 total 288 03 -rwxr-xr-x 1 root root 0 Sep 6 08:46 AUTHORS 04 -rwxr-xr-x 1 root root 0 Sep 6 08:46 ChangeLog 05 -rwxr-xr-x 1 root root 588 Sep 6 08:46 Makefile.am 06 -rwxr-xr-x 1 root root 0 Sep 6 08:46 NEWS 07 -rwxr-xr-x 1 root root 0 Sep 6 08:46 README 08 -rwxr-xr-x 1 root root 195 Sep 6 08:46 acconfig.h 09 -rwxr-xr-x 1 root root 4499 Sep 6 08:46 autogen.sh 10 -rwxr-xr-x 1 root root 1391 Sep 6 08:46 configure.in 11 -rwxr-xr-x 1 root root 1635 Sep 6 08:46 helloworld.glade 12 -rwxr-xr-x 1 root root 1635 Sep 6 08:43 helloworld.glade.bak 13 drwxr-xr-x 2 root root 32768 Sep 6 08:46 po 14 drwxr-xr-x 2 root root 32768 Sep 6 08:46 src 15 -rwxr-xr-x 1 root root 10 Sep 6 08:46 stamp-h.in 16 17 ./po: 18 total 32 19 -rwxr-xr-x 1 root root 0 Sep 6 08:46 ChangeLog 20 -rwxr-xr-x 1 root root 114 Sep 6 08:46 POTFILES.in 21 22 ./src: 23 total 256 24 -rwxr-xr-x 1 root root 290 Sep 6 08:46 Makefile.am 25 -rwxr-xr-x 1 root root 286 Sep 6 08:46 callbacks.c 26 -rwxr-xr-x 1 root root 162 Sep 6 08:46 callbacks.h 27 -rwxr-xr-x 1 root root 1086 Sep 6 08:46 interface.c 28 -rwxr-xr-x 1 root root 96 Sep 6 08:46 interface.h 29 -rwxr-xr-x 1 root root 874 Sep 6 08:46 main.c 30 -rwxr-xr-x 1 root root 4497 Sep 6 08:46 support.c 31 -rwxr-xr-x 1 root root 1558 Sep 6 08:46 support.h
Glade created all the files and directories named in the listing when you pushed either the Save button or the Build button. Notice that Glade has created two directories: po and src. The directory po/potfiles.in contains a list of files with translatable strings. The src subdirectory contains the source code for the project.
In particular, we are interested in the following files:
Line 11 | helloworld.glade |
Line 09 | Autogen.sh |
Line 27 | interface.c |
Line 29 | main.c |
Line 25 | callbacks.c |
Line 30 | support.c |
These files are covered in detail later in this chapter, in the section titled HelloWorld Files Dissected.
For now, you need to fill out a small portion of your application to make it work. You need to put some code behind the cmd_main clicked event. ( cmd_main is the one button in the middle of the HelloWorld application window.) Change to the src directory so you can open callbacks.c with the text editor of your choice and insert some code.
When you open callbacks.c , you ll see a function called on_cmd_main_clicked. Within the braces of that function, enter the following line:
gtk_main_quit();
That s it! Save and exit.When the user clicks on cmd_main, it calls the quit function for the application. You have done everything needed to run the HelloWorld application. (Note that only clicking cmd_main will properly close the application; if you click the window s Close button ”the one that normally has an X and sits in the top left corner of the window ”, the window will disappear, but the program will still be running.This is because you did not attach the quit call to the window.)
There are several ways to compile and run a Glade-created application. The first is to use the Autogen.sh file that Glade created for you when it wrote the source code for the application. That is not a foolproof way to compile though, and it can be a terrible hassle to sort out. The second way is to call it from the command line, or alternately, to put that command into a shell file of its own so you don t have to type the same command over and over.
autogen.sh is a shell script (a Linux batch file) that should compile a Glade application. In my experience, it does not always work well. It should be as simple as entering these command lines of your project main directory type:
%./autogen.sh % make
After that, go to the src subdirectory and type
% ./HelloWorld
or whatever your application is named, and off you go. As I said, it is supposed to be that simple, but very rarely is it. All I can tell you is if you have problems, try the following solutions:
In the Project Options dialog box, remove Gettext support. This is a very common fix on the Glade mailing list.
Verify that you have all the correct libraries installed with the most recent versions.
Peruse the Glade mailing list archives for autogen problems, and see if someone else has had a similar one.
You can, of course, compile this (or any) C program from the command line. I say this is a hack because you are going to have to comment out some code that is not absolutely necessary but that does have a use (internationalization, to be exact). To compile from the command line, cd to the src subdirectory and type this line:
% gcc -Wall -g callbacks.c interface.c support.c main.c `gtk-config --cflags` `gtk-config --libs`
The -Wall tells gcc to issue all warnings, and the -g option tells it to create debugging information for use with a debugger.
To run the program, type % ./a.out (or ./HelloWorld if you included a -o HelloWorld flag in the compile command).
Removing the add_pixmaps... CallsWhen you compile from the command line, you will probably have to comment out two lines in main.c (in Listing 5.3, they are lines 28 and 29). Those two lines will cause compile errors if you use this method to compile. These lines refer to directories that are not needed for a command line compile, but they are used if the autogen.sh shell script is used instead. |
If you put the compile command into a file, you can execute that command by calling the shell file. Listing 5.2 is a shell script that has two commands: The first clears the screen for readability, and the second starts the process of compiling the application. The backslash character (\) is the command line continuation character.
clear gcc -Wall -g callbacks.c interface.c \ support.c main.c \ `gtk-config --cflags --libs`
Notice that gtk-config has been abbreviated to have both options after the program command. Also, the clear command has been added. With the clear command, if the program encounters compile errors, they will be displayed on a clean screen for easier reading.
If you put the above commands in a file, call that file build.sh . Then you can just call ./build.sh to avoid all the retyping.
only for RuBoard - do not distribute or recompile |