Programming with the XForms Library

Part 3 shows us the means to give our game simulator a more professional appearance and to add a few goodies.
Using Goodies

With the inclusion of a menu system, resizing parameters and some decorative pixmaps, XForms-based applications like xgtsim2 can be easily polished into user-friendly, attractive software. XForms also provides a slew of easy-to-add program elements called “goodies”.

An example of a goodie occurs in the code for help_menu_routines(). If the user selects Use from the help menu, he gets a window that displays information about how to use xgtsim2. Since the program is just an example, we haven't actually written any help files for it—we just want to display a window explaining that help is not (yet) implemented. We could create this window manually, adding some text objects, an “OK” button and so on; however, this is a lot of work just to say there is no help available. Instead, we use a goodie called fl_show_alert(). This function accepts three lines of text as parameters, as well as an integer value to determine the placement of the ensuing announcement (the value 1 just tells XForms to place the window in the center of the display). With one line of code, we have an easy way to display text messages to the user without having to design a new window ourselves.

An even more powerful example of a goodie is the XForms-supplied file requester. Writing one of these from scratch can take a good deal of time, since we would need to create a window with some kind of browser, open and close buttons, implement a filtering mechanism, etc. The fl_show_fselector does all of this for us and allows the load_config() and save_config functions in xgtsim2 to be very compact. The full form of the function is as follows:

fl_show_fselector(const char *message,
        const char *directory,
        const char *pattern,
        const char *default)

The four string parameters allow us to set the selector's message, a specific directory to start from, a filtering pattern, and even a default file name. All of this occurs with a single function call. A somewhat subtle feature of the file selector is the existence of six such selectors, each of which remembers the last directory if the *directory string is passed as length 0. In xgtsim2, we use two of them, one for loading and one for saving. In each case, we declare which selector appears by making a call to fl_use_selector() before calling fl_show_selector(). That way, if users are loading data from one directory and saving it in another, they will not need to keep clicking back and forth between directories each time they want to access files.

There are also mechanisms for adding configurable buttons to the selector, setting the window title, and so on. Anyone who has designed a method for letting users load and save files will appreciate the amount of thought and planning that has gone into this widget.

There are many other goodies provided by XForms, including routines to get input from the user (fl_show_input), other message display routines (fl_show_question) and even a quick and easy method for getting color selections (fl_show_colormap()).

On Your Own

There is still much about XForms that we haven't touched on in this series, but the documentation included with XForms is excellent at explaining all of the resources available. With a little effort on the programmer's part, the library provides for fast program development and a professional look. There's even a form designer included in the XForms package which enables you to design an interface using a mouse. This makes creating complex windows a breeze, and the software produces output which can easily be incorporated into your source code.

Even if you never create a “killer app” with XForms, the basic lessons of placing GUI elements, assigning callbacks and showing windows are reasonably transportable to other programming environments and libraries. These articles should give you the basic knowledge required to create X programs. To paraphrase Donald Knuth, go forth and create great software.

Resources

Thor Sigvaldason is the author of the statistics program xldlas which uses the XForms library (see Linux Journal, February 1997). He is trying to finish a PhD in economics and can be reached at thor@netcom.ca.

______________________

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix