XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
NULL);
two = XtVaCreateManagedWidget ("Two",
xmPushButtonWidgetClass, parent,
XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, one,
/* attach top of widget to same y coordinate as top of "one" */
XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
XmNtopWidget, one,
NULL);
three = XtVaCreateManagedWidget ("Three",
xmPushButtonWidgetClass, parent,
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, one,
/* attach left of widget to same x coordinate as left side of "one" */
XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
XmNleftWidget, one,
NULL);
XtRealizeWidget (toplevel);
XtAppMainLoop (app);
}
The example uses three PushButton gadgets inside of a Form widget. The output of the program is shown in the
figure.
Output of attach.c
You should notice that the widgets are packed together quite tightly, which might not be how you expected them to
appear. In order to space the widgets more reasonably, we need to specify some distance between them using
attachment offsets.
9.4.2 Attachment Offsets
Attachment offsets control the spacing between widgets and the objects to which they are attached. The following
resources represent the attachment offsets for the four sides of a widget:
XmNleftOffset
XmNrightOffset
XmNtopOffset
XmNbottomOffset
9 Manager Widgets 9.4.2 Attachment Offsets
213
the figure shows the graphic representation of attachment offsets.
By default, offsets are set to 0 (zero), which means that there is no offset, as shown in the output for the source code
To make the output more reasonable, we need only to set the left offset between widgets One and Two and the top
offset to between widgets One and Three. The resources values can be hard−coded in the application or set in a
resource file, using the following specification:
*form.One.leftOffset: 10
*form.One.topOffset: 10
*form.Two.leftOffset: 10
*form.Three.topOffset: 10
Attachment offsets
Our choice of the value 10 was arbitrary. The widgets are now spaced more appropriately, as shown in the figure.
Output of attach.c with offset resources set to 10
While the layout of the widgets can be improved by setting offset resources, it is also possible to disrupt the layout.
Consider the following resource specifications:
9 Manager Widgets 9.4.2 Attachment Offsets
214
*form*leftOffset: 10
*form*topOffset: 10
While it might seem that these resource values are simply a terser way to specify the offsets shown earlier, the figure
makes it clear that these specifications do not produce the desired effect.
Output of attach.c with inappropriate offset resources
An application should hard−code whatever resources may be necessary to prevent the user from setting values that
would make the application non−functional or aesthetically unappealing. Offset resource values can be tricky because
they apply individually to each side of each widget in a Form. The problem with the resource specifications used to
produce the figure is that the offsets are being applied to each side of every widget, when some of the alignments need
to be precise. In order to prevent this problem, we need to hard−code the offsets for particular attachments, as shown
in the following code fragment:
two = XtVaCreateManagedWidget ("Two",
xmPushButtonWidgetClass, parent,
XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, one,
XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
XmNtopWidget, one,
XmNtopOffset, 0,
NULL);
three = XtVaCreateManagedWidget ("Three",
xmPushButtonWidgetClass, parent,
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, one,
XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
XmNleftWidget, one,
XmNleftOffset, 0,
NULL);
The use of zero−length offsets guarantees that the widgets they are associated with are aligned exactly with the
widgets to which they are attached, regardless of any resource specifications made by the user. A general rule of
thumb is that whenever you use XmATTACH_OPPOSITE_WIDGET, you should also set the appropriate offset to zero
so that the alignment remains consistent.
In some situations it is necessary to use negative offsets to properly arrange widgets in a Form. The most common
example of this situation occurs when using the XmATTACH_OPPOSITE_FORM attachment. Unless you use a
negative offset, as shown in the figure, the widgets are placed off the edge of the Form and are not visible.
9 Manager Widgets 9.4.2 Attachment Offsets
215
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.