Output of text_form.c
Notice that the Labels are centered vertically with respect to their corresponding Text widgets. This arrangement
happened because each Label was stretched vertically in order to attach it to the top and bottom of the respective
Form. Of course, if the Labels were higher than the Text widgets, the Text widgets would be stretched instead.
Later, we'll show another version of this program that gives better results. As you can imagine, there are many
different ways for a Form, or any other manager widget, to manage the geometry of its children to produce the same
layout. Later, when we discuss the RowColumn widget, we will show you another solution to the problem of
horizontal alignment. It is important to remember that there is no right or wrong way to create a layout, as long as it
works for your application. However, you should be very careful to experiment with resizing issues as well as with
resources that can be set by the user that might affect widget layout, such as fonts and strings.
9.4.6 Common Problems
With a Form widget, you can specify a virtually unlimited number of attachments for its children. The dependencies
inherent in these attachments can lead to various errors in the layout of the widgets. One common problem involves
circular dependencies. The following code fragment shows a very simple example of a circular dependency:
w1 = XtVaCreateManagedWidget ("w1", xmLabelGadgetClass, form, NULL);
w2 = XtVaCreateManagedWidget ("w2", xmLabelGadgetClass, form, NULL);
XtVaSetValues (w1,
XmNrightAttachment, XmATTACH_WIDGET,
XmNrightWidget, w2,
NULL);
XtVaSetValues (w2, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, w1, NULL); In this example,
the left widget is attached to the right widget and the right widget is attached to the left one. If you do mistakenly
specify a circular dependency, it is unlikely that it will be as obvious as this example. Fortunately, in most cases, the
Motif toolkit catches circular dependencies and displays an error message if one is found. When this situation occurs,
you need to reconsider your widget layout and try to arrange things such that the relationship between widgets is less
complex. One rule to remember is that adjacent widgets should only be attached in one direction.
9 Manager Widgets 9.4.6 Common Problems
222
When you attach the side of a widget to another widget in a Form, you need to be careful about how you specify the
attached widget. If you specify this widget in the application code, you need to make sure that the widget has been
created before you specify it as a resource value. With Motif 1.1, you cannot specify a widget ID in a resource file
unless you have installed your own widget−name−to−widget−ID converter. (See Volume Four, X Toolkit Intrinsics
Programming Manual, for information about resource converters.) In Motif 1.2, the toolkit provides a
name−to−widget converter, so you can specify widget IDs in a resource file.
Another common problem arises with certain Motif compound objects, such as ScrolledList and ScrolledText objects.
XmCreateScrolledText() and XmCreateScrolledList() return the corresponding Text or List widget,
but it is the parent of this widget that needs to be positioned within a Form. The following code fragment shows an
example of positioning a ScrolledList incorrectly:
form = XmCreateForm (parent, "form", NULL, 0);
list = XmCreateScrolledList (form, "scrolled_list", NULL, 0);
XtVaSetValues(list, /* <− WRONG */
XmNleftAttachment, XmATTACH_FORM,
XmNtopAttachment, XmATTACH_FORM,
NULL);
Since the List is a child of the ScrolledWindow, not the Form, specifying attachments for the List has no effect on the
position of the List in the Form. The attachments need to be specified on the ScrolledWindow, as shown in the
following code fragment:
XtVaSetValues (XtParent (list),
XmNleftAttachment, XmATTACH_FORM,
XmNtopAttachment, XmATTACH_FORM,
NULL);
If you specify attachments for two opposing sides of a widget, the Form resizes the widget as needed, so that the
default size of the widget is ignored. In most cases, the Form can resize the widget without a problem. However, one
particular case that can cause a problem is a List widget that has its XmNvisibleItemCount resource set. This
resource implies a specific size requirement, so that when the List is laid out in the Form widget, the negotiation
process between the Form and the List may not be resolved. See Chapter 12, The List Widget, for a complete
discussion of the List widget.
Attachments in Form widgets can be delicate specifications, which means that you must be specific and, above all,
complete in your descriptions of how widgets should be aligned and positioned. Since resources can be set from many
different places, the only way to guarantee that you get the layout you want is to hard−code these resource values
explicitly. Even though it is important to allow the user to specify as many resources as possible, you do not want to
compromise the integrity of your application. Attachments and attachment offsets are probably not in the set of
resources that should be user−definable.
Although attachments can be delicate, they are also provide a powerful, convenient, and flexible way to lay out
widgets within a Form, especially when the widgets are grouped together in some abstract way. Attachments make it
easy to chain widgets together, to bind them to the edge of a Form, and to allow them to be fixed on specific locations.
You do not need to use a single attachment type exclusively; it is perfectly reasonable, and in most cases necessary, to
use a variety of different types of attachments to achieve a particular layout. If you specify too few attachments, you
may end up with misplaced widgets or widgets that drift when the Form is resized, while too many attachments may
cause the Form to be too inflexible. In order to determine the best way to attach widgets to one another, you may find
it helpful to a draw picture first, with all of the hooks and offset values considered.
9 Manager Widgets 9.4.6 Common Problems
223
Get Volume 6A: Motif Programming Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.