Specifications
Athena Widget Set libXaw 1.0.7
Chapter 7
Creating New Widgets (Subclassing)
Written By: Ralph Swick
Although the task of creating a newwidget may at first appear a little daunting, there is a basic
simple pattern that all widgets follow. The Athena Widget library contains a special widget called
the Template widget that is intended to assist the novice widget programmer in writing a custom
widget.
Reasons for wishing to write a custom widget include:
•Providing a graphical interface not currently supported by anyexisting widget set.
•Convenient access to resource management procedures to obtain fonts, colors, etc., evenif
user customization is not desired.
•Convenient access to user input dispatch and translation management procedures.
•Access to callback mechanism for building higher-levelapplication libraries.
•Customizing the interface or behavior of an existing widget to suit a special application need.
•Desire to allowuser customization of resources such as fonts, colors, etc., or to allowcon-
venient re-binding of keysand buttons to internal functions.
•Converting a non-Toolkit application to use the Toolkit.
In each of these cases, the operation needed to create a newwidget is to "subclass" an existing
one. If the desired semantics of the newwidget are similar to an existing one, then the implemen-
tation of the existing widget should be examined to see howmuch work would be required to cre-
ate a subclass that will then be able to share the existing class methods. Much time will be saved
in writing the newwidget if an existing widget class Expose, Resize and/or GeometryManager
method can be used by the subclass.
Note that some trivial uses of a ‘‘bare-bones’’widget may be achievedbysimply creating an
instance of the Core widget. The class variable to use when creating a Core widget is widget-
Class.The geometry of the Core widget is determined entirely by the parent widget.
It is very often the case than an application will have a special need for a certain set of functions
and that manycopies of these functions will be needed. Forexample, when converting an older
application to use the Toolkit, it may be desirable to have a "WindowWidget" class that might
have the following semantics:
•Allocate 2 drawing colors in addition to a background color.
•Allocate a text font.
•Execute an application-supplied function to handle exposure events.
•Execute an application-supplied function to handle user input events.
It is obvious that a completely general-purpose WindowWidgetClass could be constructed that
would export all class methods as callbacks lists, but such a widget would be very large and
would have tochoose some arbitrary number of resources such as colors to allocate. An applica-
tion that used manyinstances of the general-purpose widget would therefore un-necessarily waste
manyresources.
In this section, an outline will be givenofthe procedure to followtoconstruct a special-purpose
widget to address the items listed above.The reader should refer to the appropriate sections of
the XToolkit Intrinsics — C Language Interface for complete details of the material outlined
132