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

Changing the height and width

All widgets have resources that control their height and width. For example, the shell widget that is the window for the program first.c that we have been working on has a height and a width. When it first starts up, the window (read shell widget) is sized so that it holds the label widget perfectly. What if you want the height and width of the shell widget to be different at startup though? You can do this within the program by setting the height and width resources of the shell widget. This process is shown in the code below:


        #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;

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

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

          ac=0;
          XtSetArg(al[ac],XmNheight,200); ac++;
          XtSetArg(al[ac],XmNwidth,200); ac++;
          XtSetValues(toplevel,al,ac);

          XtRealizeWidget(toplevel);
          XtMainLoop();
        }

In this code, four new lines have been added to first.c right above the call to realize the shell widget. An argument list is loaded with the new values for the height and width resources, and this argument list is then used to set the values of those resources in the toplevel widget. When you run this code, the window should open up with a size of 200x200 pixels.

Some bugs to watch for when setting resources: 1) be sure you init and increment ac correctly, 2) be sure you spell the resource names correctly (case matters), and 3) be sure you pass al and ac to XtSetValues in the correct order (you'll get a bus error otherwise, even though it will compile correctly).


morbe@enstb.enst-bretagne.fr