Next: Line 4 Up: Explanation of the Previous: Line 1

Lines 2 and 3

Lines 2 and 3 are used to set the labelString resource of the Label Widget. The labelString is being set to hold the word "hello". This is why the window said "hello" when the program executed. The actual widget is created in line 4.


         ac=0;
          XtSetArg(al[ac],XmNlabelString,
                 XmStringCreate("hello",XmSTRING_DEFAULT_CHARSET)); ac++;

Most normal people look at this whole thing with some puzzlement for several weeks, but here is what is going on. "Toplevel" (line 1) is the top-most widget. It is essentially the window that holds this application (also known as a "shell widget"). Toplevel was created for you by XtInitialize to hold the application. Toplevel is empty however - you place other widgets into this empty shell to create the user interface. In this case, we are creating one widget called a label widget. A label widget holds a static text label. In this label widget goes the static text "hello".

A label widget has 18 resources that you can change to customize it's appearance. A resource is simply a variable that controls the behavior of the widget. For example, in a label widget you can change the text it displays, the font used, the size, etc. [How do you know what the resources are, and what they are called? You get the Motif Programmer's Reference Manual which contains complete descriptions of all widgets and their resources, or you get Young's book, which summarizes them. At the bottom of mt3 I will also start placing resource lists for the widgets we use.]

In this case we want to change the labelString resource of the label widget we are about to create. We are going to leave all of the other resources of the label widget at their default values. We are going to set the labelString resource as the widget is created.

To change the resources of any widget from inside of a program, you create an argument array, fill that array with the resource values you wish to change, and then pass that argument array to the widget either at creation (as shown here) or after creation using the XtSetValues call. A counter is also used to keep track of the number of resource values stored in the array (ac is the counter in this program, and al is the argument list). Using the XtSetArg call, the code is placing the value "hello" into al[0], and specifying that the value should be used for the XmlabelString resource. The value of ac is incremented as well (EXTREMELY IMPORTANT - make sure ac accurately reflects the number of resources in al).

There is also the XmStringCreate call to contend with. Motif strings are different from normal strings (because they contain more information, such as which direction the characters are normally drawn (r-to-l or l-to-r), etc.), so a special creation procedure is used to create them. The first parameter is the value of the string, and the second is the character set that should be used for the string.

There are a number of errors that are easy to create with resource setting and argument lists. You can misspell the resource name, forget to initialize or increment ac, place too many values in al (since it's an array it can overflow), forget to create an actual XmString, etc. Be careful.

All of this is done to set up the argument list. Now we can create the actual widget itself.



Next: Line 4 Up: Explanation of the Previous: Line 1


morbe@enstb.enst-bretagne.fr