Like colors, the details of using font related objects and methods can best be seen by examining code for the example widget. Nevertheless, as for colors, the following code fragments demonstrate some of the issues involved in using fonts objects in a widget. Note that the font of a text object is normally not set to "10 Pt Bold Courier" (or some other literal font), but is instead set as specified interactively by the user of the widget.
// in the widget .h file ...
// Declare a string to hold the font tag and a buffer for the property
// dialog box
char fontTag[63+1], bufFontTag[63+1];
// Declare a font menu object, also for the property dialog box
fontMenuClass fm;
// Declare a pointer to an X Window font struct object
XFontStruct *fs;
// For font metrics
int fontAscent, fontDescent, fontHeight, stringWidth;
// in the widget .cc file ...
// ------------------------------------------------------------
// entry form callback code will be written to update the font tag;
// the code will look something like the following
strncpy( obj->fontTag, obj->fm.currentFontTag(), 63 );
// When a font tag is obtained, make the font available to the X Server
obj->actWin->fi->loadFontTag( obj->fontTag );
// Also, get the font struct for later use
obj->fs = obj->actWin->fi->getXFontStruct( obj->fontTag );
// ------------------------------------------------------------
// add the font menu object to the property dialog box
ef.addFontMenu( "Font", actWin->fi, &fm, fontTag );
fm.setFontAlignment( alignment );
// ------------------------------------------------------------
// When you are ready to display some text, get font metrics for
// string position calculations
updateFont( string, fontTag, &fs, &fontAscent, &fontDescent,
&fontHeight, &stringWidth );
// ------------------------------------------------------------
// Display the text
if ( strcmp( fontTag, "" ) != 0 ) {
actWin->drawGc.setFontTag( fontTag, actWin->fi );
}
actWin->drawGc.setFG( fgColor.pixelIndex(), &blink );
actWin->drawGc.setBG( actWin->ci->pix(bgColor) );
stringLength = strlen( string );
XDrawImageString( actWin->d, XtWindow(actWin->drawWidget),
actWin->drawGc.normGC(), stringX, stringY,
string, stringLength );
updateBlink( blink );