Next: The MainWindow Widget and Menus Up: Forms Previous: Opposite attchment - form3.c

A more complete form program

Let us finish off this section by building a form widget that contains two different types of widget. It will also use callback functions thus making a more complete application program example.

The program is called arrows.c. It creates 4 ArrowButton widgets arranged in a north, south east west type arrangement. In the middle of the 4 ArrowButtons is a PushButton. This is labelled ``Quit'' and shows haw we can use callbacks to control our application. The arrows when pressed simply print to standard output.

The output of arrows.c looks like this:

Fig. 9 arrows.c output

The program listing is:


#include <Xm/Xm.h>
#include <Xm/PushB.h>
#include <Xm/ArrowB.h>
#include <Xm/Form.h>

void north(), south(), east(), west(), quitb(); /* callback fns */

main(int argc, char **argv)

{
    XtAppContext app;
    Widget top_wid, form,
           arrow1, arrow2, arrow3, arrow4,           
           quit;


    top_wid = XtVaAppInitialize(&app, "Multi Widgets",
        NULL, 0, &argc, argv, NULL, NULL);

  
    form = XtVaCreateWidget("form", xmFormWidgetClass, top_wid,
	XmNfractionBase,     3,
	NULL);


    arrow1 = XtVaCreateManagedWidget("arrow1",
	                xmArrowButtonWidgetClass, form,
	                XmNtopAttachment,    XmATTACH_POSITION,
	                XmNtopPosition,      0,
	                XmNbottomAttachment, XmATTACH_POSITION,
	                XmNbottomPosition,   1,
	                XmNleftAttachment,   XmATTACH_POSITION,
	                XmNleftPosition,     1,
	                XmNrightAttachment,   XmATTACH_POSITION,
	                XmNrightPosition,    2,
	                XmNarrowDirection,   XmARROW_UP,
	                NULL);


    arrow2 = XtVaCreateManagedWidget("arrow2",
	                xmArrowButtonWidgetClass, form,
	                XmNtopAttachment,    XmATTACH_POSITION,
	                XmNtopPosition,      1,
	                XmNbottomAttachment, XmATTACH_POSITION,
	                XmNbottomPosition,   2,
	                XmNleftAttachment,   XmATTACH_POSITION,
	                XmNleftPosition,     0,
	                XmNrightAttachment,   XmATTACH_POSITION,
	                XmNrightPosition,    1,
	                XmNarrowDirection,   XmARROW_LEFT,
	                NULL);


    arrow3 = XtVaCreateManagedWidget("arrow3",
	                xmArrowButtonWidgetClass, form,
	                XmNtopAttachment,    XmATTACH_POSITION,
	                XmNtopPosition,      1,
	                XmNbottomAttachment, XmATTACH_POSITION,
	                XmNbottomPosition,   2,
	                XmNleftAttachment,   XmATTACH_POSITION,
	                XmNleftPosition,     2,
	                XmNrightAttachment,   XmATTACH_POSITION,
	                XmNrightPosition,    3,
	                XmNarrowDirection,   XmARROW_RIGHT,
	                NULL);


   arrow4 = XtVaCreateManagedWidget("arrow4",
	                xmArrowButtonWidgetClass, form,
	                XmNtopAttachment,    XmATTACH_POSITION,
	                XmNtopPosition,      2,
	                XmNbottomAttachment, XmATTACH_POSITION,
	                XmNbottomPosition,   3,
	                XmNleftAttachment,   XmATTACH_POSITION,
	                XmNleftPosition,     1,
	                XmNrightAttachment,   XmATTACH_POSITION,
	                XmNrightPosition,    2,
	                XmNarrowDirection,   XmARROW_DOWN,
	                NULL);


   quit =XtVaCreateManagedWidget("Quit",
	                xmPushButtonWidgetClass, form,
	                XmNtopAttachment,    XmATTACH_POSITION,
	                XmNtopPosition,      1,
	                XmNbottomAttachment, XmATTACH_POSITION,
	                XmNbottomPosition,   2,
	                XmNleftAttachment,   XmATTACH_POSITION,
	                XmNleftPosition,     1,
	                XmNrightAttachment,   XmATTACH_POSITION,
	                XmNrightPosition,    2,
	                NULL);

	
    /* add callback functions */
    
    XtAddCallback(arrow1, XmNactivateCallback, north, NULL);
    XtAddCallback(arrow2, XmNactivateCallback, west, NULL);
    XtAddCallback(arrow3, XmNactivateCallback, east, NULL);
    XtAddCallback(arrow4, XmNactivateCallback, south, NULL);
    XtAddCallback(quit, XmNactivateCallback, quitb, NULL);

    XtManageChild(form);
    XtRealizeWidget(top_wid);
    XtAppMainLoop(app);
}


/* CALLBACKS */
void
north(Widget w, XtPointer client_data, 
	        XmPushButtonCallbackStruct *cbs)
{ printf("Going North\n");}

void
west(Widget w, XtPointer client_data, 
	        XmPushButtonCallbackStruct *cbs)
{ printf("Going West\n");}

void
east(Widget w, XtPointer client_data, 
	        XmPushButtonCallbackStruct *cbs)
{ printf("Going East\n");}


void
south(Widget w, XtPointer client_data, 
	        XmPushButtonCallbackStruct *cbs)

{ printf("Going South\n");}

void
quitb(Widget w, XtPointer client_data, 
	        XmPushButtonCallbackStruct *cbs)

{ printf("quit button pressed\n");
  exit(0);
}
The arrows.c program uses the fraction base positioning method of placing widgets within a form.

The XmNfractionBase is reset to 3 thus creating a 3 by 3 grid for attaching widget edges.

Each top, bottom, left and right edge of the 4 ArrowButtons and the ``Quit'' PushButton are attached to the appropriate position within the grid.

Callback functions for each widget are added in the usual way. The 4 arrow widgets callback functions simply print out their direction. The quit callback function exits the program.


Dave.Marshall@cm.cf.ac.uk
Tue May 24 16:52:56 BST 1994