V—A Free C++ GUI Framework for X
I love Linux as a development platform—it has all the Unix tools I've been using for years. Linux has been a superior development platform in every way except for development of graphical user interfaces (GUIs) with X. For some time, programmers using Microsoft Windows have had the option of using either Microsoft's MFC or Borland's OWL to develop C++ GUI applications. However, Linux has caught up in the graphics area as some very good freeware C++ GUI frameworks are now available for X and Linux. This article is about V, the GUI framework I've written.
I first developed V as a tutorial example for a C++ book I'm writing. My tutorial has turned into a very complete, easy-to-use C++ GUI framework suitable for developing almost any GUI application. It works with X and Linux, and uses the standard X Athena toolkit, so that everything remains free. V uses some customized versions of the free Athena 3D widget set, so V applications have a very handsome 3D look. V also works with Microsoft Windows, so applications you build on Linux with V are easily ported to MS-Windows, as well as other X platforms. V is distributed under the GNU Library General Public License, so you can freely use V to develop either other freeware or proprietary applications (as long as you make the source of V available to the end user).
Two other good frameworks are wxWindows and YACL. While these two programs have their strong points, I think V is much simpler to use and has applications that are more user friendly. For more details, a comparison of V and the other frameworks is posted on my web page (http://www.cs.unm.edu/~wampler/). All three programs provide much of the functionality of commercial frameworks such as MFC or OWL.
If you don't use a GUI library or framework, you are stuck using the native X toolkits to develop your GUI, either by using X directly (very hard) or using the Xt Athena or Motif widget sets (just a bit easier). While you can develop very nice interfaces using these toolkits directly, there is a steep learning curve. A good programmer needs at least a month of steady effort to be able to write programs using either Xt Athena or Motif, and the applications are limited to X platforms. One of the problems with the native X toolkits is that they are designed to be totally flexible, and to handle almost any interface design. This very generality makes them hard to learn and to use. For most applications, you simply don't need that much flexibility.
V, on the other hand, has been designed from the start with ease of programming in mind. I have received reports from new V users saying they were able to get their first V applications up and running in just four or five hours from downloading the V distribution to ending with a running application. Within a week, programmers are able to fully use almost every feature of V. Even the inexperienced programmers in my classes at the University of New Mexico have learned to program with V in a week.
The cost for this ease of programming is rather small. For one thing, you must use V's definition of the GUI elements. When you use V, you end up with interfaces that conform closely to standard Motif or Microsoft Windows applications. Also, you lose some of the low level access and flexibility you have when using the low-level toolkits. For the vast majority of applications, this loss is not significant. V does provides hooks for getting at some of the low-level details, but the only use I've heard made of them is to interface with OpenGL and Mesa.
Figure 1 shows the V Icon Editor included with the V distribution. The Icon Editor is representative of a typical V application. The main user interface element is the command window. The command window has a menu bar that is the standard drop-down menu found on most Motif and Windows applications. Below the menu bar are one or more tool bars that allow you to make the most commonly used commands readily available to the user. The current trend is to place buttons with small icons on the tool bar; with V you can use any standard control object on the tool bar. Under the tool bar is the drawing canvas where your application shows data, such as text information, graphical information, or, in this case, the icon to edit. The drawing canvas can have scroll bars, and the user interacts with the data displayed on the canvas using the mouse or keyboard. Below the drawing canvas is a status bar, used to show useful information, like the mouse position.
Dialogs are an important part of most GUI programs. Figure 2 shows the color selection modeless dialog used with the V Icon Editor. V supports both modal and modeless dialogs with a complete set of controls.
I hope you agree that the appearance of this V application is as good as any other X application you've seen on Linux. Icon Editor also compiles in Microsoft Windows without a single #ifdef and will look just like a standard Windows application.
V is a C++ framework, and therefore, consists of several C++ base classes you can use to derive your own classes. The core class is vApp, which is used as the main foundation of the rest of the application. A minimal application has a command window derived from the vCommandWindow class containing at least a menu and a drawing canvas. The command window can also contain tool and status bars. When the user selects menu items or activates command objects on the tool bar, V sends messages to your derived vCommandWindow object which your program then interprets. Your program usually shows its output on a canvas object, derived from a vCanvas class. Mouse input is handled by sending messages to your derived canvas class.
While it is possible to build an application using only the vApp, vCommandWindow, and vCanvas classes, most applications will also use dialogs. The dialog controls supported by V currently include:
standard push button
push button with icon and color
toggle buttons
labels
icons
check box
radio button
combo box
list
spinners
slider
progress bar
text in
multi-line text
frame (box)
toggle frames (similar to tabs)
The layout of controls in a dialog is determined by specifying a position for each control relative to other controls in the dialog. For example, you might specify one control is to the right and below another control. All dialog controls can also be used on the tool bar of a command window.
V also includes several standard dialogs that implement common operations. One of the most useful is the file open dialog, which lets the user select files and directories interactively. Since no standard X file open dialog exists, V implements its own. The MS-Windows version of V uses the standard Windows file dialog. Other standard dialogs include font selection, yes/no response and a message box. Using standard dialogs helps ensure that your application will match the look and feel of the native platform.
The first step to using V is obtaining the latest release. The primary source for V is my web page, http://www.cs.unm.edu/~wampler/. V is also available for anonymous ftp from ftp://ftp.cs.unm.edu/pub/wampler. Unpack the distribution, decide where you want to keep the source and libraries, modify the makefile to reflect this decision and execute make.
After you've built V, read the documentation. You can either print it, or use xdvi or ghostscript to view the provided dvi or ps files. You then typically start your own application by modifying one of the examples provided. Most new V users find it very easy to modify the example to get a good start on their own. The entire process of downloading, building, reading the manual and building a first custom V application has been done by several current V users (already familiar with the Internet, Linux and C++) in just four or five hours.
Since its release to the world in February 1996, V has been in use by my software engineering class and received considerable interest and positive response from the user community. If you've put off writing a GUI application because X programming is just too hard to learn, V may be the solution you need. If you've been putting off learning C++, again V may be just what you need. V is a very elegant, small and well-designed C++ class library. GUIs are one of the most naturally object-oriented applications around and are a good way to get started with object-oriented programming.
Bruce E. Wampler, PhD (wampler@cs.unm.edu) has been involved in the PC industry since its earliest days. In fact, he founded two successful software companies, Aspen Software and Reference Software International, and was the principal designer and author of the well-known grammar checker Grammatik. He is currently an Adjunct Professor of Computer Science at the University of New Mexico.