Next: Reading Resources Up: Working with resources Previous: Changing the height

Changing the font used

As another example, the label widget also lets you specify the font used for the label. Setting a font is slightly more involved than setting the height of a widget, but follows the same plan, as shown 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);

          /* declare variables and hook in a different font. */
          {
            XFontStruct *font=NULL;
            XmFontList fontlist=NULL;
            char *namestring=NULL;

            namestring="*6x10*";
            font=XLoadQueryFont(XtDisplay(msg),namestring);
            fontlist=XmFontListCreate(font,XmSTRING_DEFAULT_CHARSET);
            ac=0;
            XtSetArg(al[ac],XmNfontList,fontlist); ac++;
            XtSetValues(msg,al,ac);
          }

          XtRealizeWidget(toplevel);
          XtMainLoop();
        }

The piece of code in braces near the middle sets up a font list and sets the fontList resource of the label widget. The first line of code takes the name of the font and points namestring at it. Next the font is loaded. Next a Motif fontlist is formed, and this fontlist is used to set the resource. When you run this code, You should see the label displayed with a different font than the font that originally appeared. [You can find info on XLoadQueryFont (and all other X and Xt procedures) by firing up Xman and looking in section 3 (system dependent).]

[How do you find out which font names are valid on your system? Generally,I type the command:


        xlsfonts -fn "*" > out,

and then look at "out". All the font names will be in the file. Some of them are short, like "6x10" used above, but some are monsters, as shown in the fragment of my "out" file copied below:


-adobe-times-bold-i-normal--24-240-75-75-p-128-iso8859-1
-adobe-times-bold-i-normal--25-180-100-100-p-128-iso8859-1
-adobe-times-bold-i-normal--34-240-100-100-p-170-iso8859-1

To use one of these fonts, I can say:


         namestring="*times*-25-*";

in the above code. This will get a 25 point times font. If I want a specific times font I can be more specific, like "*times*bold*-25-*". The "*" is a wildcard like it is in a file name.]


morbe@enstb.enst-bretagne.fr