Introduction to Motif Application Development

Part 9: Resource files

by Marshall Brain , brain@iftech.com
Interface Technologies, Inc.
(800) 224-4965
© Copyright 1994 by Marshall Brain. All rights reserved.
Version 2.01, 6/26/95

These tutorials are excerpted from the book "Motif Programming: The Essentials and More" , by Marshall Brain.


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 <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".]