Next: Conclusion Up: Drawingresource files, Previous: Drawing

Resource Files

In all of the programs presented so far, we have set all of the widget resources directly in the code, using XtSetArg and XtSetValues. Motif supports a second mechanism for setting resources however, called "resource files". Resource files allow you to set up resource values in a separate text file that is external to the program itself. The resource values in the file are read in when the program starts up, and change the behavior of the specified widgets. This is a very handy feature, because it lets you change the behavior of a program without recompiling, or lets the user customize an application by editing the resource file. As an example, all of the labelString resources in a program might be read from a resource file. Then several resource files can be created to supply those labels in different languages (english, spanish, french, etc.).

To try out a resource file, type in the following code:


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

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

          toplevel=XtInitialize(argv[0],"Sample",NULL,0,&argc,argv);

          ac=0;
          button=XtCreateManagedWidget("button",xmPushButtonWidgetClass,
                toplevel,al,ac);

          XtRealizeWidget(toplevel);
          XtMainLoop();
        }

This is code from tutorial four. It creates a button. Now create a second file that contains the following lines. Call the file "resource.test".


        Sample.height:  300
        Sample.width:   300
        Sample*button*labelString:      "blah blah blah"

At the unix command line, type "setenv XENVIRONMENT filename", where "filename" is replaced by the complete path to the resource file (eg - /usr/users/smith/motif/resource.test). Now compile and run the program. When it executes, the application will have a 300x300 window, and the label for the button will read "blah blah blah".

To make this happen, the XtInitialize line has been changed to include the string "Sample". This is a "class name", and is used to identify resources intended for this application. The system then goes and looks for a resource file of that name in the app-defaults directory, and also looks for any resources of that name in the ".Xdefaults" file in your home directory and in the XENVIRONMENT file if specified (along with several other places). In this case, it finds a width, height, and labelString resource for the Sample class, and uses them to set the appropriate values.

In the resource file itself, you can delimit resource names with a "." or a "*". The "." is used when you know the explicit "path" through the widgets to the resource. The "*" is used when you don't know or want to specify the exact path. The word "button" is the name we gave to the button widget when it was created (first parameter to the XtCreateManagedWidget call). The resource names are the names listed for the widget resources, minus the "XmN" prefix.

There's a whole little science involved with resource files, and Young devotes an entire chapter to it. Look there for more info, or just play around and see what you can discover.

[Note: you can place resources in the ".Xdefaults" file. However, this file is normally read once at login time and cached. To reload it after you've made a change, type "xrdb -load .Xdefaults".]



Next: Conclusion Up: Drawingresource files, Previous: Drawing


morbe@enstb.enst-bretagne.fr