Next: Attach positions - form2.c Up: Forms Previous: Forms

Simple Attachment -form1.c

Widgets are placed in a form by specifying the attachment of widgets to edges of other widgets. The edges of widgets can be on the form widget itself or other child widgets.

Edges are referred to by top, bottom, left and right attachments. A widget has resources such as XmNtopattachment to attach a widget to an appropriate edge.

To attach a widget to the parent form set the resource value XmATTACH_FORM.

To attach a widget to another widget set the resource value XmATTACH_WIDGET.

The widget that we seek to attach to must also be specified by setting the resource XmNtopWidget, for example, to the appropriate widget.

Let us look at how all this works in the form1.c program:

#include <Xm/Xm.h> 
#include <Xm/PushB.h> 
#include <Xm/Form.h> 
main (int argc, char **argv)  
{ XtAppContext app;
  Widget   top_wid, form,
	   button1, button2, button3, button4;
  int n=0;
  top_wid = XtVaAppInitialize(&app, "Form1",
        NULL, 0, &argc, argv, NULL, NULL);

  /* create form and child buttons */
  form = XtVaCreateManagedWidget("form", 
        xmFormWidgetClass, top_wid, NULL);
  button1 = XtVaCreateManagedWidget("Button 1",
      xmPushButtonWidgetClass, form,
      /* attach to top, left of form */
      XmNtopAttachment, XmATTACH_FORM,     
      XmNleftAttachment, XmATTACH_FORM,

  button2 = XtVaCreateManagedWidget("Button 2",
      xmPushButtonWidgetClass, form,
      XmNtopAttachment, XmATTACH_WIDGET,
      XmNtopWidget, button1, /* top to button 1 */
      XmNleftAttachment, XmATTACH_FORM, /* left, bottom to form */
      XmNbottomAttachment, XmATTACH_FORM,
  button3 = XtVaCreateManagedWidget("Button 3",
      xmPushButtonWidgetClass, form,
      XmNtopAttachment, XmATTACH_FORM, /* top, right to form */
      XmNrightAttachment, XmATTACH_FORM,
      XmNleftAttachment, XmATTACH_WIDGET, /* left to button 1 */
      XmNleftWidget, button1,

  button4 = XtVaCreateManagedWidget("Button 4",
      xmPushButtonWidgetClass, form,
      XmNbottomAttachment, XmATTACH_FORM, /* bottom right to form */
      XmNrightAttachment, XmATTACH_FORM,
      XmNtopAttachment, XmATTACH_WIDGET,
      XmNtopWidget, button3, /* top to button 3 */
      XmNleftAttachment, XmATTACH_WIDGET,
      XmNleftWidget, button2, /* left to button 2 */
  XtRealizeWidget (top_wid); 
  XtAppMainLoop (app); 
In this program, the form widget is created as a child of app and 4 buttons are the child of form.

Note: inclusion of <Xm/Form.h> file.

button1 is simply attached to the top left of the form.

button2 is attached to bottom left of the form and its top to button1

button3 is attached to the top right of the form and its right to button1

button4 is attached to the bottom right of the form and its top and left to button3 and button2 respectively.

Note: it is advisable to control the attachment as much as possible so that any resizing of the form will still preserve the desired order.
Tue May 24 16:52:56 BST 1994