Next | Prev | Up | Top | Contents | Index

A Simple Example

The following example is in the /usr/share/src/sgitcl directory as progEG.tcl. Typically, a Motif program has a top-level object called a mainWindow. This holds a menu bar and a container such as a Form or rowColumn, which in turn holds the rest of the objects. Here is code to create a mainWindow with a list and some buttons in a form:

#! /usr/sgitcl/bin/moat 
xtAppInitialize -class Program
xmMainWindow .main managed
xmForm .main.form managed
xmList .main.form.list managed
xmPushButton .main.form.btn1 managed
xmPushButton .main.form.btn2 managed
The xmForm acts as what is called the "workWindow" of the mainWindow. This resource would be set as follows:

.main setValues -workWindow .main.form
Values would also be set into the list and buttons:

.main.form.list setValues \
        -itemCount 3 -items "one, two, three" \
        -selectionPolicy single_select
.main.form.btn1 setValues -labelString Quit
.main.form.btn2 setValues -labelString "Do nothing"
Callbacks are set up for the Quit button and the selection list:

.main.form.btn1 activateCallback {exit 0}
.main.form.list singleSelectionCallback {puts stdout "Selected %item"}
Geometry would be set for the form, placing all objects in correct relation to each other. This produces a list on the left, with the two buttons above and below on the right:

.main.form.list setValues \
        -topAttachment attach_form \
        -leftAttachment attach_form \
        -bottomAttachment attach_form
.main.form.btn1 setValues \
        -topAttachment attach_form \
        -leftAttachment attach_widget \
        -leftWidget .main.form.list
.main.form.btn2 setValues \
        -topAttachment attach_widget \
        -topWidget .main.form.btn1 \
        -leftAttachment attach_widget \
        -leftWidget .main.form.list
Since we initially created all the widgets as managed, it is not necessary to explicitly manage them before entering the main loop.

Finally, windows are created and the main event loop is entered:

. realizeWidget
. mainLoop
Once entered in the main event loop, the application is really running: widgets are created, displayed, and manipulated as user events that trigger associated callbacks.


Next | Prev | Up | Top | Contents | Index