HOW TO SPECIFY A FONT LIST FOR RENDERING
 
 
You surely want to let the user specify a font list within an application's resource file. This way the user doesn't get stuck with your set of fonts you've once selected for rendering a XmString. 

On the previous page I've showed how to obtain a default font list. But such a font list only contains one fixed font. If you want to get a font list from the application's resource file you have to follow these guidelines: 

  1. Query the resource data base for a font list entry on a particular widget. Note that this widget doesn't necessay need to have such a resoure defined in its class record. You just ask the database for an entry called "fontList". In the case of the DrawingArea widget no such resource exists for that widget class.
  2. char *ResourceItem;
    XmFontList MyFontList;
    Widget MyWidget;
    
    ...
    
    static char *LoadStringResource(Widget w,
                                    char *ResourceName,
                                    char *ResourceClass,
                                    char *Default)
    {
        XtResource Resources[] = {
            { NULL, NULL, XtRString, sizeof(String),
            0, XtRString, NULL }
        };
        String TheString;
        char *TempClassName;
    
        Resources[0].resource_name = ResourceName;
        if ( ResourceClass )
            Resources[0].resource_class = ResourceClass;
        else {
            TempClassName = XtNewString(ResourceName);
            TempClassName[0] = toupper(TempClassName[0]);
            Resources[0].resource_class = TempClassName;
        }
        XtGetApplicationResources(w,
                                  (XtPointer) &TheString,
                                  Resources, XtNumber(Resources),
                                  NULL, 0);
        if ( ResourceClass == NULL ) XtFree(TempClassName);
        return TheString ? TheString : Default;
    } /* LoadStringResource */
    
    ...
    
    MyWidget = ...
    
    ...
    
    ResourceFontList = LoadStringResource(MyWidget, "fontList",
                                          "FontList", NULL);
    If such an entry can be found in the resource database then the variable ResourceFontList contains the respective ASCII text. The utility function LoadStringResource() allows one to access the database easily. You'll also find it in my ButtonFace Library
  3. If ResourceFontList is NULL then no appropriate entry could be found and you should create your own default font list. Otherwise you must convert the offending entry to a XmFontList. This way Motif can use it when rendering a XmString.
  4. if ( ResourceFontList ) {
        XrmValue from, to;
    
        from.addr = (XtPointer) ResourceFontList;
        from.size = strlen(ResourceFontList) + 1;
        to.addr = NULL;
        XtConvert(MyWidget, XmRString, &from, XmRFontList, &to);
        if ( to.addr )
            MyFontList = *((XmFontList *) to.addr);
    }
    The MyWidget is needed not only for the database look-up in order to decide which item in the widget hierarchy should be queried but also for the conversion process. The String-to-XmFontList converter creates the font list on behalf of the widget specified. Thus the font list can be destroyed later together with that particular widget. 
  5. With the font list in MyFontList you can now create a graphical context as shown on the previous page.
That's it! Maybe it looks complicated at the first glance - but it's really easy. So I decided not to provide another source file... there are already enough of them lurking around our ftp server. Just save this article to disk and copy the pieces of source code to your own application. 


Previous page (How to render XmStrings) 
Back to the Tips page 

Contents: Harald Albrecht (albrecht@igpm.rwth-aachen.de) 
Layout: Harald Albrecht 
Last Change: 97/08/10 (ab)