Programming with the XForms Library
Since learning any programming library involves a substantial investment of time, it may be worth your while to consider a few alternatives before jumping headlong into XForms.
The following discussion is far from exhaustive and is based solely on the idiosyncratic experiences of the author.
Motif is a very common library which is used to build many commercial X-based programs. To create your own Motif applications, you need the Motif development library, which is fairly expensive (at least by Linux standards). The main advantage to Motif is that it is a mature system which can create visually appealing applications. The main drawback is its price. There is a free version of Motif in the works (called LessTif), but it is not yet 100% functional.
Another library with a licensing arrangement similar to XForms is called Qt. The Qt library is free for non-commercial use under X, but is also available for Microsoft Windows and other operating systems. This has the great advantage that if you write a program under Linux, people can also use it on completely different operating systems. It is also the core of the new KDE Desktop Environment, which should be available by the time this article appears in print. If KDE catches on, then Qt will become very popular in the Linux world. The Qt library is based around C++ (rather than plain old C, which XForms uses).
If you want an X development system that is truly free, then you may want to look at V. This is another C++-based library, which includes all the standard features, such as buttons, menus, etc. As the author of V admits, it is probably not suitable for trying to write a state-of-the-art interface, but it is a reasonably complete package, and it's difficult to argue with the price.
The main task in installing XForms is to get the library and include file into the right places on your system. The Web and ftp sites for XForms are listed at the end of this article. Most people will want to grab the ELF version, but if you are still running a.out, then make sure you get the relevant file. The distribution includes the library, more than 50 example applications, and the fdesign program. You should also get the reference manual, which is available in a separate file.
At the time of writing, the most recent version of XForms is 0.81, and the Linux package is in a compressed tar file of roughly 600KB (i.e., it has a .tgz extension). You should make sure you are logged on as root and unpack the distribution somewhere like /usr/local/. The archive installs into a directory called xforms. It's generally a good idea to check the Readme file included in the distribution for advice on installation.
If you have a more or less standard setup, then all you need to do is go to the xforms directory and type make install. This should copy the library and include files into the right places. If this doesn't work (or you want to do things by hand), just make sure forms.h ends up somewhere gcc can find it (i.e., in /usr/include/) and all libform files end up in the same place as libX11 (i.e., in /usr/X11R6/lib/).
If you want to verify things are installed correctly, change to the DEMOS subdirectory and try make demo. If this does not work, go back and ensure the library and includes are where they're supposed to be.
With the library and include file installed, it's time to write your first program. It is a C tradition that a first attempt should always say “hello world”, so we'll write one that does just that. Either type in the source code for xhello.c (see Listing 1) or copy it from the series web site.
As the source code suggests, you should be able to compile the program with the command:
gcc -lX11 -lforms -lm xhello.c -o xhello
The command xhello should start up the program (make sure you're running X). You may have to type ./xhello, depending on how your shell's PATH variable is set. The running program should look something like Figure 1. Note, you must include the linking options for this to work. In particular, -lX11 links your code to the standard X library, which XForms depends on (libX11 is included in all X distributions). The forms library is linked in with -lforms, and it uses some math routines which must be linked with -lm.
Although it doesn't do very much, the xhello program shows the basic steps involved in writing an XForms application. First, we include the forms header file, which gives us access to the XForms routines. Then, fl_initialize() is called to let XForms set itself up. We pass all command line options (stored in argv) to this function, so that XForms can pull out those it wants. For example, our xhello program already understands command options like -share (to share the colour map) and -display (to open itself on a specified X display).
With initialization taken care of, we can create as many graphic elements as we like. In this example, we have just one button located in a single window.5 We then show the window we've created with the fl_show_form() function. To get the program to wait for the button to be pushed before exiting, we invoke fl_do_forms() which waits until the state of our window changes before returning.
5. This brings up a note on terminology. Many people think of a rectangular collection of objects on a screen as a window. The XForms library refers to just such a collection as a form. For the rest of this series, it is best to consider the two terms interchangeable.
There are a couple of things to notice at this point. First, if you have never written an X application before, then congratulations are in order (give yourself a pat on the back). Second, although installing the library may have taken some time, the result was well worth it. With just two variables and well under a dozen function calls, we have a fully fledged running program. Not only that, our program is attractive: the colour scheme is easy on the eyes, the button is nicely shaded to appear three dimensional, etc. Finally, the program is quick and responsive, even on slow hardware. This is due to a combination of factors, the most important of which are that XForms is quite efficient, and everything has been done in pure C.
- Let's Go to Mars with Martian Lander
- Applied Expert Systems, Inc.'s CleverView for TCP/IP on Linux
- My Childhood in a Cigar Box
- Papa's Got a Brand New NAS
- Returning Values from Bash Functions
- Tech Tip: Really Simple HTTP Server with Python
- Rogue Wave Software's TotalView for HPC and CodeDynamics
- Simplenote, Simply Awesome!
- Panther MPC, Inc.'s Panther Alpha