Next: Line 1 Up: Motif Tutorial Previous: Your first program

Explanation of the first program

In Motif Tutorial 1 you typed in and executed the following code:


       #include <X11/Intrinsic.h>
        #include <Xm/Xm.h>
        #include <Xm/Label.h>

        main(int argc, char *argv[])
        {
          Widget toplevel, msg;
          Arg al[10];
          int ac;

1         toplevel=XtInitialize(argv[0],"",NULL,0,&argc,argv);
2         ac=0;
3         XtSetArg(al[ac],XmNlabelString,
                 XmStringCreate("hello",XmSTRING_DEFAULT_CHARSET)); ac++;
4         msg=XtCreateManagedWidget("msg",xmLabelWidgetClass,toplevel,al,ac);
5         XtRealizeWidget(toplevel);
6         XtMainLoop();
        }

But what does it all mean? This tutorial will go over what each line means and does, talk about some bugs, and generally explain what is going on.

[Note: this is going to seem extremely weird the first time through - even this tiny piece of code embodies a bunch of concepts. Just wade through once as best you can, and then go though it a second time and enlightenment will shine upon you (I hope). You might also consider reading mt2, then mt3, and then rereading the pair. mt3 contains examples and listings of resources and that might help you here.]

Notice first that this program includes three libraries. One is an Xlib library (X11/Intrinsic.h), and two are motif libraries. A big Motif program might include 20 or 30 libraries from X11, Xm, etc.

Any Motif program, like all C programs, has a main procedure. The main procedure accepts the command line arguments as its parameters. Even if the code you write doesn't use command line arguments, the main procedure needs to accept argc and argv. The X interface defines certain standard command line parameters that work the same way in all X applications (such as -iconic, -geometry, etc.) (you can read about them with "man X"). In practice you will pass argc and argv off to a routine that extracts all the standard X options and then returns to you what's left.

Before describing the rest of the program, you must understand the concept of a "widget". Motif tries to be an object oriented sort of programming envrionment, and under X the objects used to implement user interfaces are called widgets. Generally widgets are made out of other widgets in a hierarchical fashion. Each widget has associated with it a set of "resources", and a set of "callbacks" (both of these topics will get massive treatment as we go along). The resources are a set of variables that control the appearance and behavior of a widget, and the callbacks are functions that the widget calls so that your code can respond to user events. [Just for your own edification, the X intrinsics are an extension to X that makes widget sets possible. There are several different widget sets that you will hear about - Motif is one, but there is also the Athena widget set, the HP widget set, and probably several others.]




Next: Line 1 Up: Motif Tutorial Previous: Your first program


morbe@enstb.enst-bretagne.fr