Programming with the XForms Library
Where did Linux come from? That's a good question, and one that seems to confuse a lot of new users. The short answer, of course, is that Linux was written by Linus Torvalds (the original and central force behind the Linux kernel). But that cursory response misses much of the spirit of Linux. When you go into a computer store and buy a Linux CD-ROM set, you're getting a copy of the intellectual output of literally thousands of programmers. Most of these people have never met each other. Thanks to the Internet, however, this geographically dispersed group has managed to create a formidable array of tools and applications. Linux works so well that it creates the illusion that there is some kind of organization and control structure in place—some central authority directing every aspect of its development. This makes it easy to forget that every application you run—from an ASCII text editor to a Z-Modem download—exists because somebody, somewhere, sat down one day and said, “I can't find a way to do this under Linux, so maybe I should try to write my own program.”
This article is the first in a three-part series designed to introduce the reader to a programming tool called The XForms Library. As its name suggests, XForms is a set of tools and routines that can be used to easily create programs that run under the X Window System. The overall goal is to take a reader who has never written an X-based piece of software to the point where he or she can start writing their own applications and utilities. The series assumes the reader is familiar with the C programming language, since XForms is C-based. If you don't yet know anything about C programming, now may be a good time to take the plunge. Buy a book or two and start fiddling with it. With a few weeks of study, you'll probably have enough C under your belt to be able to follow the series by the time the next article appears. It is also assumed you have a working C compiler and the X Window System is already installed.
It's the author's hope that readers will eventually be able to contribute an XForms-based program to Linux. The next time you post a “where can I find a program to do x” message to comp.os.linux.misc and don't get an answer, think about whether it's something you might be able to write yourself. You probably won't earn a cent for your troubles, but the process is not without its rewards. One day, a few months after uploading your package to Sunsite, you may find yourself in a software shop that has just received a new shipment of Linux CD-ROMs. Up on a shelf, probably squeezed between the latest commercial software from Microsoft and Corel, will be a few kilobytes of code you wrote and which people all over the world are using.
The purpose of this first article is to explain how to get and install XForms. We also take a first glance at the process of creating an XForms application and actually write a couple of simple programs.
The next two articles, which will appear in the August and September issues of Linux Journal, will expand on this process. They will go through all the steps required to create useful software with XForms with the aid of an example application, a simple game theory simulator. Don't worry if you have no idea what game theory is, since all you need to know about it will be explained in the process. The choice of programming example is driven by the author's interests, and is irrelevant to the central purpose of the series: learning to use XForms.
Details on where to get all of the software mentioned are available in the Resources section at the end of this article. There is also a web page for this series which can be found at http://a42.com/~thor/xforms/. This site includes links to all relevant software, as well as listings of all the source code used in the series.
The XForms library was written by T. C. Zhao and Mark Overmars. It is free for non-commercial use, but if you're planning on selling your application, you will need to set up a monetary licensing arrangement with the XForms authors. If you're a free software “purist”, you may want to think about using V instead of XForms (see the next section).
The most important parts of the XForms package are the actual library (libforms) and the forms.h include file. The former contains functions for creating buttons, menus, and so on. The include file declares these library functions, which makes them available for use in your software. If you're a relative newcomer to all this, don't worry—we'll be going through every step required to get things working.
There is also a very thorough manual available in HTML format, which explains all of the available routines. A large set of example applications, which are referred to in the documentation, are included in the main distribution. These examples are very useful for getting good ideas on how to implement various procedures.
Finally, there is a program called fdesign included with XForms that can be used to design user interfaces. This is a real time saver, since you can use your mouse to control the placement of buttons, menus and other objects. Your design can then be saved to a file and easily brought into your source code. Complicated sets of overlapping windows with large numbers of graphical elements can thus be created in a relatively short period of time.
Since XForms is actually built around the X11 library, any program you write is highly portable to other X-capable systems. Although readers of this magazine will probably be developing for Linux, it's nice to know users of FreeBSD, Sun and other systems will be able to run your application if they want.