[ Previous | Next | Contents | Glossary | Home | Search ]
Motif 2.1 Programmer's Guide

Chapter 3. Structure of a Motif Program

Motif uses the same event-driven programming model as the X Toolkit Intrinsics. At its core, a Motif application waits for the user to provide input, usually by pressing a key, moving the mouse, or clicking a mouse button. Such an action by the user causes the X server to generate one or more X Window System events. Xt listens for these events and dispatches them to the appropriate Motif widget, usually the widget to which the user directed the input. The widget may take some action as a result of the user input. If the application has asked to be notified of that action, the widget "calls back" to the application--that is, it invokes an application callback procedure. When both Motif and the application have finished responding to the user input, the application waits for the user to provide more input. This cycle of user-initiated events and application response, called the event loop, continues until the user terminates the application.

For simple applications, the Intrinsics and Motif toolkits do everything necessary for dispatching user input to widgets. The application must take the following actions:

  1. Include the required header files

  2. Initialize the Intrinsics

  3. Create one or more widgets

  4. Define callback procedures and attach them to widgets

  5. Make the widgets visible

  6. Enter the event loop

    This chapter discusses each of these actions. The following table summarizes these steps and some of the procedures the application needs to call. Note that some of these steps are different when the application uses UIL and MRM. See Chapter 4 for more information.

    Table 2. Steps in Writing Widget Programs

    Step Description Related Functions
    1 Include required header files. #include <Xm/Xm.h> #include <Xm/ widget.h> or <Xm/XmAll.h>
    2 Initialize Xt Intrinsics. XtAppInitialize()

    Do steps 3 and 4 for each widget.
    3 Create widget. XtSetArg() XtCreateManagedWidget() or XtVaCreateManagedWidget() or XmCreate <WidgetName>() followed by XtManageChild( widget)
    4 Add callback routines. XtAddCallback()
    5 Realize widgets. XtRealizeWidget( parent)
    6 Enter event loop. XtAppMainLoop()

  7. [ Previous | Next | Contents | Glossary | Home | Search ]