3 The Motif Programming Model
This chapter teaches the fundamentals of Motif by example. It dissects a simple "Hello, World" program, showing the
program structure and style common to all Motif programs. Because much of this material is already covered in detail
in Volume Four, X Toolkit Intrinsics Programming Manual, Motif Edition, this chapter can be used as a refresher or a
light introduction for those who haven't read the earlier book. It makes reference to Volume One, Xlib Programming
Manual, and Volume Four to point out areas that the programmer needs to understand (windows, widgets, events,
callbacks, resources, translations) before progressing with Motif.
Though we expect most readers of this book to be familiar with the X Toolkit Intrinsics (Xt), this chapter briefly
reviews the foundations of Motif in Xt. This review serves a variety of purposes. First, for completeness, we define
our terms, so if you are unfamiliar with Xt, you will not be completely at sea if you forge ahead. Second, there are
many important aspects of the X Toolkit Intrinsics that we aren't going to cover in this book; this review gives us a
chance to direct you to other sources of information about these areas. Third, Motif diverges from Xt in some
important ways, and we point out these differences up front. Finally, we point out some of the particular choices you
can make when Xt or Motif provides more than one way to accomplish the same task.
If you are unfamiliar with any of the concepts introduced in this chapter, please read the first few chapters of
Volume Four, X Toolkit Intrinsics Programming Manual. Portions of Volume One, Xlib Programming Manual, and
Volume Three, X Window System User's Guide, Motif Edition, may also be appropriate.
3.1 Basic X Toolkit Terminology and Concepts
As discussed in Chapter 1, Introduction to Motif, the Motif user−interface specification is completely independent of
how it is implemented. In other words, you do not have to use the X Window System to implement a Motif−style
graphical user interface (GUI). However, to enhance portability and robustness, the Open Software Foundation (OSF)
chose to implement the Motif GUI using X as the window system and the X Toolkit Intrinsics as the platform for the
Application Programmer's Interface (API).
Xt provides an object−oriented framework for creating reusable, configurable user−interface components called
widgets. Motif provides widgets for such common user−interface elements as labels, buttons, menus, dialog boxes,
scrollbars, and text−entry or display areas. In addition, there are widgets called managers, whose only job is to control
the layout of other widgets, so the application doesn't have to worry about details of widget placement when the
application is moved or resized.
A widget operates independently of the application, except through prearranged interactions. For example, a button
widget knows how to draw itself, how to highlight itself when it is clicked on with the mouse, and how to respond to
that mouse click.
The general behavior of a widget, such as a PushButton, is defined as part of the Motif library. Xt defines certain base
classes of widgets, whose behavior can be inherited and augmented or modified by other widget classes (subclasses).
The base widget classes provide a common foundation for all Xt−based widget sets. A widget set, such as Motif's Xm
library, defines a complete set of widget classes, sufficient for most user−interface needs. Xt also supports
mechanisms for creating new widgets or for modifying existing ones.
Xt also supports lighter−weight objects called gadgets, which for the most part look and act just like widgets, but their
behavior is actually provided by the manager widget that contains them. For example, a pulldown menu pane can be
made up of button gadgets rather than button widgets, with the menu pane doing much of the work that would
normally be done by the button widgets.
Most widgets and gadgets inherit characteristics from objects above them in the class hierarchy. For example, the
Motif PushButton class inherits the ability to display a label from the Label widget class, which in turn inherits even
more basic widget behavior from its own superclasses. See Volume Four, X Toolkit Intrinsics Programming Manual,
for a complete discussion of Xt's classing mechanisms; see Chapter 3, Overview of the Motif Toolkit, for details about
the Motif widget class hierarchy.
The object−oriented approach of Xt completely insulates the application programmer from the code inside of widgets.
As a programmer, you only have access to functions that create, manage, and destroy widgets, plus certain public
widget variables known as resources. As a result, the internal implementation of a widget can change without
requiring changes to the API. A further benefit of the object−oriented approach is that it forces you to think about an
application in a more abstract and generalized fashion, which leads to fewer bugs in the short run and to a better
design in the long run.
Creating a widget is referred to as instantiating it. You ask the toolkit for an instance of a particular widget class,
which can be customized by setting its resources. All Motif PushButton widgets have the ability to display a label; an
instance of the PushButton widget class actually has a label that can be set with a resource.
Creating widgets is a lot like buying a car: first you choose the model (class) of car you want, then you choose the
options you want, and then you drive an actual car off the lot. There may exist many cars exactly like yours, others
that are similar, and still others that are completely different. You can create widgets, destroy them, and even change
their attributes just as you can buy, sell, or modify a car by painting it, adding a new stereo, and so on.
Widgets are designed so that many of their resources can be modified by the user at run−time. When an application is
run, Xt automatically loads data from a number of system and user−specific files. The data from these files is used to
build the resource database, which is used to configure the widgets in the application. If you want to keep the user
from modifying resources, you can set their values when you create the widget. This practice is commonly referred to
as hard−coding resources.
It is considered good practice to hard−code only those resource values that are essential to program operation and to
leave the rest of the resources configurable. Default values for configurable resources are typically specified in an
application defaults file, which is more colloquially referred to as the app−defaults file. By convention, this file is
stored in the directory /usr/lib/X11/app−defaults and it has the same name as the application with the first letter
capitalized. The app−defaults file is loaded into the resource database along with other files that may contain different
values set by the system administrator or the user. In the event of a conflict between different settings, a complex set
of precedence rules determines the value actually assigned to a resource. See Volume Four, X Toolkit Intrinsics
Programming Manual, for more information on how to set resources using the various resource files.
Motif widgets are prolific in their use of resources. For each widget class, there are many resources that neither the
application nor the user should ever need to change. Some of these resources provide fine control over the
three−dimensional appearance of Motif widgets; these resources should not be modified, since that would interfere
with the visual consistency of Motif applications. Other resources are used internally by Motif to make one large,
complex widget appear to the user in a variety of guises.
The callback resources for a widget are a particularly important class of resources that must be set in the application
code. A widget that expects to interact with an application provides a callback resource for each type of interaction it
supports. An application associates a function with the callback resources in which it is interested; the function is
invoked when the user performs certain actions in the widget. For example, a PushButton provides a callback for
when the user activates the button.
3 The Motif Programming Model 3 The Motif Programming Model