These tutorials are excerpted from the book "Motif Programming: The Essentials and More" , by Marshall Brain.
To try out a resource file, type in the following code:
#include <Xm/Xm.h> #include <Xm/Label.h> XtAppContext context; Widget toplevel, label; main(argc,argv) int argc; char *argv[]; { /* create the toplevel shell */ toplevel = XtAppInitialize(&context,"Sample",NULL,0, &argc,argv,NULL,NULL,0); /* create label widget */ label=XmCreateLabel(toplevel,"label",NULL, 0); XtManageChild(label); XtRealizeWidget(toplevel); XtAppMainLoop(context); } This is code from tutorial three. It creates a label. Now create a second file that contains the following lines. Call the file "resource.test".
Sample.height: 300 Sample.width: 300 Sample*label*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 XtAppInitialize 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. It also looks in the XENVIRONMENT file if specified. In this case it finds the file reosurce.test, and in 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 "label" is the name we gave to the label widget when it was created (second parameter to the XmCreateLabel 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. Look in the man page for X for more information, as well as in some of the files in your app-defaults directory.
[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 -merge .Xdefaults".]