Next: Label Widget Up: Working with resources Previous: Changing the font

Reading Resources

It is also possible to read the value of a resource, and this is often useful when responding to a callback or when trying to see what the user has done to a user-modifiable widget (for example, a scroll bar). The code below simply asks for the height and width of the shell widget (toplevel) once the program has set them up:


       #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;
          int w,h;

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

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

          ac=0;
          XtSetArg(al[ac],XmNheight,&h); ac++;
          XtSetArg(al[ac],XmNwidth,&w); ac++;
          XtGetValues(msg,al,ac);
          printf("%d %d \n",w,h);

          XtRealizeWidget(toplevel);
          XtMainLoop();
        }

Simply use XtSetArg as you have in the past, but pass it the ADDRESS of the variable you want the value to be placed in. Then call XtGetValues to get the resource values requested.

Resources are the key to understanding the widgets. You probably have to buy either Young's book or the Motif PRM to get a good list of the available resources for each widget. It is also possible to set resources through resource files, and to create your own resources - more on that in a later tutorial.

In case you cannot afford or lay your hands on Young's book or the Motif PRM, here are the resource lists for the widgets we've used so far, along with their ancestors. This is summarized from Young's book - for more info go to the actual book, but this should be enough to get you going. If nothing else you could just try setting some of them randomly and see what happens. For totally complete descriptions of the widgets, you need to get the Motif PRM.

The "superclass" of a widget is the thing that this widget is made out of. For example, the superclass of a label widget is XmPrimitive. This means that a label widget is built from (or on top of) a primitive widget, and therefore inherits all of a primitve widget's resources as well. This idea of inheritance is an object oriented programming concept. The primitive widget in turn is built from a core widget. Core widgets have an XmNheight and XmNwidth resource, and therefore ALL widgets have a height and width resource.

The include file name is also important. If you use a widget, you must reference the include file for it in a #include statement. If you forget to do this the program will not compile.



Next: Label Widget Up: Working with resources Previous: Changing the font


morbe@enstb.enst-bretagne.fr