Chapter 1. What Is Involved in Writing a Motif Widget

This chapter helps answer the following questions:

Pros and Cons of Writing a Motif Widget

Why would you want to write your own Motif widget? After all, Motif Release 2.0 already provides dozens of widgets. Using the standard widget set has some big advantages. The biggest advantage is, of course, that you do not have to do the work. Someone has already gone through the painstaking process of developing the widgets and testing them. The existing Motif widget set is used by thousands of programmers and millions of computer users. This wide use ensures that software defects (bugs) will be ferreted out and reported, and that the standard Motif widgets will grow ever more mature and defect free. Best of all, when a bug is found in one of the Motif widgets, you do not have to fix it.

So, what possible advantage would there be in writing your own widget? Broadly speaking, the biggest advantage is customization. The standard Motif widget set is meant to provide general-purpose solutions. However, general-purpose solutions may not always be sufficient. For example, you may need to write widgets that

  • Provide flexible table layout

  • Display complex graphs

  • Display new kinds of scales, such as circular scales

  • Generate form layouts that are appropriate for certain applications

Custom-built widgets can answer different needs. The most obvious need is to provide a widget for a specific application. However, custom-built widgets can also benefit a related group of applications. For example, a geologic consortium could produce a set of widgets that are useful for geologic applications. Doing so would give geologists a more consistent interface across geologic applications and would give geologic programmers a common set of widgets to work from.

Another advantage of writing your own widgets is that you can easily subclass them. In fact, you can gradually build an entire widget tree to meet your needs.

Prerequisite Knowledge

Before attempting to write a Motif widget, you should know

  • A lot about the Intrinsics (Xt).

  • At least a little about Xlib. For example, you should know something about Graphics Contexts (GCs).

  • A lot about writing Motif applications so that you understand what it is that Motif applications programmers expect from Motif widgets.

  • A lot about Motif style as described in the Motif Style Guide. All the Motif widgets you write should be style-guide compliant.

The Software You Will Need

In order to write your Motif widget, you do not need the source files for the standard Motif widgets. In other words, you can develop Motif widgets even if you do not have a Motif source license. (Of course, if you do have a Motif source license, you will probably find it somewhat easier to develop widgets.)

You must have the following software in order to create a Motif widget:

  • A C or C++ compiler. If you are programming in the C language, an ANSI C compiler is required.

  • The Motif applications programmer's development software. (This comprises all the header files necessary for developing Motif applications, plus the libXm.a library.)

Although not a requirement, we strongly recommend that you have access to the Exm Motif demonstration widget set. The source code for this widget set is stored online in the demos/lib/Exm directory that accompanies Motif. This widget set illustrates how to code many commonly used Motif widget features. Figure 1-1 shows the class hierarchy of the Exm demonstration widget set.

Figure 1-1. Hierarchy of Exm Demonstration Widget Set

If you are writing a Motif widget that is to be accessible from a User Interface Language (UIL) application, then you will also need to have the following software:

  • A UIL compiler.

  • The wml/tools directory.

  • Motif Resource Manager (MRM) software. This comprises several header files plus a runtime library named libMrm.a.

You can develop Motif widgets from any operating system that supports Motif. Ideally, the widget source code you write should compile without modification on any operating system that supports Motif.

What Kinds of Widgets Can You Create?

This guide tells you how to subclass a Motif widget from XmPrimitive or XmManager. This guide does not explain how to write a gadget or a shell widget. Furthermore, this guide does not explain how to subclass directly from the Athena widget set or from any other widget set.

Subclassing from XmPrimitive or XmManager will allow your widget to call existing methods that provide much of the appropriate Motif look and feel.

How Much Work Is Involved?

If you have made it this far, then you presumably have the prerequisite knowledge and software. Now the question is whether you also have the determination to complete writing a Motif widget.

For very simple widgets, you should expect to write at least 800 to 1000 lines of source code. Complex widgets may easily require 10,000 to 20,000 lines of source code. The amount of code is related not only to the complexity of the widget but also to the availability of suitable code in superclasses. That is, if your widget's superclass already contains a suitable method, then your widget can simply inherit that method.

We do not recommend that you write all this code "from scratch." Rather, we suggest copying large chunks of your widget from existing widgets, such as the Exm demonstration widgets we provide.

Defining the Goal

The primary goal for Motif widget writers is to create a self-contained object that satisfies the Motif Style Guide conventions and interacts appropriately with other Motif widgets. Another important goal is to write widget code that conforms to the widget coding guidelines presented in Chapter 2.

By "self-contained object," we are suggesting that you follow good object-oriented design rules. For example, any widget that you create should itself be subclassable. Design for reuse.

Neither Motif nor the Intrinsics will be able to verify that you have followed the dictates of the Motif Style Guide. However, Motif users will know when you have not followed the conventions. If you are not yet familiar with the Motif Style Guide, you should become so. In order to give your new widgets that all-important Motif look and feel, your widgets should do the following:

  • Inherit (or envelop) methods from XmPrimitive and XmManager, whenever possible.

  • Use the internal Motif widget writer functions (the Xme functions), whenever possible. The standard Motif widget set uses the Xme functions extensively. For example, the XmeDrawShadows function draws a Motif style shadow inside a widget. Therefore, there is no good reason to invent your own shadow drawing routine when you can call the existing one.

  • Specify translations and actions consistent with the Motif Style Guide. In other words, respond to events as similar Motif widgets would.