\&
.sp 1
.ce 3
\s+1\fBChapter 9\fP\s-1

\s+1\fBResource Management\fP\s-1
.sp 2
.nr H1 9
.nr H2 0
.nr H3 0
.nr H4 0
.nr H5 0
.LP
.XS
Chapter 9 \- Resource Management
.XE
A resource is a field in the widget record with a corresponding
resource entry in the resource list of the widget or any of its
superclasses.
This means that the field is
settable by
.PN XtCreateWidget
(by naming the field in the argument list), by an
entry in the default resource files (by using either the name or class), and by
.PN XtSetValues .
In addition, it is readable by
.PN XtGetValues .
Not all fields in a widget record are resources.
Some are for bookkeeping use by the
generic routines (like managed and being_destroyed).
Others can be for local bookkeeping,
and still others are derived from resources 
(many graphics contexts and pixmaps).
.LP
Writers of widgets need to obtain a large set of resources at widget
creation time.
Some of the resources come from the argument list supplied in the call to
.PN XtCreateWidget ,
some from the resource database,
and some from the internal defaults specified for the widget.
Resources are obtained first from the argument list,
then from the resource database for all resources not specified
in the argument list,
and lastly from the internal default, if needed.
.NH 2
Resource Lists
.XS
\*(SN Resource Lists
.XE
.LP
.IN "Resource Management"
A resource entry specifies a field in the widget,
the textual name and class of the field that argument lists
and external resource files use to refer to the field
and a default value that the field should get if no value is specified.
The declaration for the
.PN XtResource
structure is:
.LP
.Ds 0
.TA .5i 3i
.ta .5i 3i
typedef struct {
	String resource_name;
	String resource_class;
	String resource_type;
	Cardinal resource_size;
	Cardinal resource_offset;
	String default_type;
	caddr_t default_address;
} XtResource, *XtResourceList;
.De
.IN "XtResourceList"
.LP
The resource_name field contains the name used by clients to access the field
in the widget.
By convention, it starts with a lowercase letter
and is spelled identically to the field name,
except all underscores (_) are deleted and the next letter is replaced by its 
uppercase counterpart.
For example, the resource name for background_pixel becomes backgroundPixel.
Widget header files typically contain a symbolic name for each resource name.
All resource names, classes, and types used by the \*(xI are named in
.Pn < X11/StringDefs.h >.
The \*(xI symbolic resource names begin with
.PN XtN
and are followed by the string name (for example, XtNbackgroundPixel
for backgroundPixel).
.LP
A resource class provides two functions:
.IP \(bu 5
It isolates an application from different representations that widgets 
can use for a similar resource.
.IP \(bu 5
It lets you specify values for several actual resources with a single name.
A resource class should be chosen to span a group of closely related fields.
.LP
For example,
a widget can have several pixel resources: background, foreground,
border, block cursor, pointer cursor, and so on.
Typically, the background defaults to white
and everything else to black.
The resource class for each of these resources in the resource list
should be chosen so that it takes the minimal number of entries
in the resource database to make background offwhite
and everything else darkblue.
.LP
In this case, the background pixel should have a resource class of
.PN Background
and all the other pixel entries a resource class of
.PN Foreground .
Then, the resource file needs only two lines to
change all pixels to offwhite or darkblue:
.LP
.Ds 0
.TA .5i 1.5i
.ta .5i 1.5i
*Background:	offwhite
*Foreground:	darkblue
.De
.LP
Similarly, a widget may have several resource fonts (such as normal and bold),
but all fonts should have the class Font.
Thus, changing all fonts simply requires only a single line in the 
default resource file:
.LP
.Ds 0
.TA .5i 3i
.ta .5i 3i
*Font:	6x13
.De
.LP
By convention,
resource classes are always spelled starting with a capital letter.
Their symbolic names are preceded with
.PN XtC
(for example, XtCBackground).
.LP
The resource_type field is the physical representation type of the resource.
By convention, it starts with an uppercase letter and is
spelled identically to the type name of the field.
The resource type is used when resources are fetched to
convert from the resource database format (usually String) or the default
resource format (almost anything, but often String) to the desired
physical representation (see Section 9.6).
The \*(xI define the following resource types:
.TS H
lw(2.75i) lw(2.75i).
_
.sp 6p
.TB
Resource Type	Structure or Field Type
.sp 6p
_
.sp 6p
.TH
.R
T{
.PN XtRAcceleratorTable
T}	T{
XtAccelerators
T}
T{
.PN XtRBoolean
T}	T{
Boolean
T}
T{
.PN XtRBool
T}	T{
Bool
T}
T{
.PN XtRCallback
T}	T{
XtCallbackList
T}
T{
.PN XtRColor
T}	T{
XColor
T}
T{
.PN XtRCursor
T}	T{
Cursor
T}
T{
.PN XtRDimension
T}	T{
Dimension
T}
T{
.PN XtRDisplay
T}	T{
Display*
T}
T{
.PN XtRFile
T}	T{
FILE*
T}
T{
.PN XtRFloat
T}	T{
float
T}
T{
.PN XtRFont
T}	T{
Font
T}
T{
.PN XtRFontStruct
T}	T{
XFontStruct *
T}
T{
.PN XtRFunction
T}	T{
(*)()
T}
T{
.PN XtRInt
T}	T{
int
T}
T{
.PN XtRPixel
T}	T{
Pixel
T}
T{
.PN XtRPixmap
T}	T{
Pixmap
T}
T{
.PN XtRPointer
T}	T{
caddr_t
T}
T{
.PN XtRPosition
T}	T{
Position
T}
T{
.PN XtRShort
T}	T{
short
T}
T{
.PN XtRString
T}	T{
char*
T}
T{
.PN XtRTranslationTable
T}	T{
XtTranslations
T}
T{
.PN XtRUnsignedChar
T}	T{
unsigned char
T}
T{
.PN XtRWidget
T}	T{
Widget
T}
T{
.PN XtRWindow
T}	T{
Window
T}
.TE
.LP
The resource_size field is the size of the physical representation in bytes;
you should specify it as ``sizeof(\fItype\fP)'' so that the
compiler fills in the value.
The resource_offset field is the offset in bytes of the field 
within the widget.
You should use the
.PN XtOffset
macro to retrieve this value.
The default_type field is the representation type of the default
resource value.
If default_type is different from resource_type and the default_type is needed,
the resource manager invokes a conversion procedure from default_type
to resource_type.
Whenever possible,
the default type should be identical to the resource type in order
to minimize widget creation time.
However, there are sometimes no values of the type that the program 
can easily specify.
In this case,
it should be a value that the converter is guaranteed to work for (for example, 
.PN XtDefaultForeground
for a pixel resource).
The default_address field is the address of the default resource value.
The default is used if a resource is not specified in the argument list
or in the resource database or if the conversion from the representation
type stored in the resource database fails,
which can happen for various reasons (for example, a misspelled entry in a
resource file).
.LP
Two special representation types 
.Pn ( XtRImmediate
and
.PN XtRCallProc )
are usable only as default resource types.
.PN XtRImmediate
indicates that the value in the default_address field is the actual value of
the resource rather than the address of the value. 
The value must be in correct representation type for the resource.
No conversion is possible since there is no source representation type.
.PN XtRCallProc
indicates that the value in the default_address field is a procedure
variable.
This procedure is automatically invoked with the widget,
resource_offset, and a pointer to the
.PN XrmValue
in which to store the result and is an
.PN XtResourceDefaultProc :
.FD 0
typedef void (*XtResourceDefaultProc)(Widget, int, XrmValue *)
.br
       Widget \fIw\fP\^;
.br
       int \fIoffset\fP\^;
.br
       XrmValue *\fIvalue\fP\^;
.FN
.IP \fIw\fP 1i
Specifies the widget whose resource is to be obtained.
.IP \fIoffset\fP 1i
Specifies the offset of the field in the widget record.
.IP \fIvalue\fP 1i
Specifies the resource value to fill in.
.LP
The
.PN XtResourceDefaultProc
procedure should fill in the addr field of the value with a pointer 
to the default data in its correct type.
.NT
The default_address field in the resource structure is declared as
a caddr_t.
On some machine architectures,
this may be insufficient to hold procedure variables.
.NE
.sp
.LP
To get the resource list structure for a particular class, use
.PN XtGetResourceList :
.IN "XtGetResourceList" "" "@DEF@"
.FD 0
void XtGetResourceList(\fIclass\fP, \fIresources_return\fP, \fInum_resources_return\fP);
.br
      WidgetClass \fIclass\fP;
.br
      XtResourceList *\fIresources_return\fP;
.br
      Cardinal *\fInum_resources_return\fP;
.FN
.ds Cl \ for which you want the list
.IP \fIwidget_class\fP 1i
Specifies the widget class\*(Wc.
.IP \fIresources_return\fP 1i
Specifies a pointer to where to store the returned resource list.
The caller must free this storage using
.PN XtFree
when done with it.
.IP \fInum_resources_return\fP 1i
Specifies a pointer to where to store the number of entries in the 
resource list.
.LP
If it is called before the widget class is initialized (that is,
before the first widget of that class has been created),
.PN XtGetResourceList
returns the resource list as specified in the widget class record.
If it is called after the widget class has been initialized,
.PN XtGetResourceList
returns a merged resource list that contains the resources 
for all superclasses.
.sp
.LP
The routines
.PN XtSetValues
and
.PN XtGetValues
also use the resource list to set and get widget state.
For further information, 
see Sections 9.7.1 and 9.7.2.
.LP
Here is an abbreviated version of the resource list in the Label widget:
.LP
.Ds
.TA .5i 1.5 3i
.ta .5i 1.5 3i
/* Resources specific to Label */
static XtResource resources[] = {
{XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
   XtOffset(LabelWidget, label.foreground), XtRString, XtDefaultForeground},
{XtNfont,  XtCFont, XtRFontStruct, sizeof(XFontStruct *),
   XtOffset(LabelWidget, label.font),XtRString, XtDefaultFont},
{XtNlabel,  XtCLabel, XtRString, sizeof(String),
   XtOffset(LabelWidget, label.label), XtRString, NULL},
		.
		.
		.
}
.De
.LP
The complete resource name for a field of a widget instance is the
concatenation of the application shell name (from 
.PN XtAppCreateShell ),
the instance names of all the widget's parents up to the 
.PN ApplicationShellWidget ,
the instance name of the widget itself,
and the resource name of the specified field of the widget.
Likewise,
the full resource class of a field of a widget instance is the
concatenation of the application class (from
.PN XtAppCreateShell ),
the widget class names of all the widget's parents up to the 
.PN ApplicationShellWidget
(not the superclasses), the widget class name of the widget itself,
and the resource name of the specified field of the widget.
.NH 2
Byte Offset Calculations
.XS
\*(SN Byte Offset Calculations
.XE
.LP
To determine the byte offset of a field within a structure, use
.PN XtOffset .
.IN "XtOffset" "" "@DEF@"
.FD 0
Cardinal XtOffset(\fIpointer_type\fP, \fIfield_name\fP)
.br
      Type \fIpointer_type\fP;
.br
      Field \fIfield_name\fP;
.FN
.IP \fIpointer_type\fP 1i
Specifies a type that is declared as a pointer to the structure.
.IP \fIfield_name\fP 1i
Specifies the name of the field for which to calculate the byte offset.
.LP
The
.PN XtOffset
macro is usually used to determine the offset of various resource fields 
from the beginning of a widget and can be used at compile time in static
initializations.
.NH 2
Superclass to Subclass Chaining of Resource Lists
.XS
\*(SN Superclass to Subclass Chaining of Resource Lists
.XE
.LP
.IN "Inheritance"
.IN "Superclass Chaining"
.IN "Chaining"
The 
.PN XtCreateWidget
function gets resources as a superclass-to-subclass operation.
That is, the resources specified in 
.PN Core
resource list are fetched,
then those in the subclass, and so on down to the resources specified
for this widget's class. Within a class, resources are fetched in the order
they are declared.
.LP
In general, if a widget resource field is declared in a superclass,
that field is included in the superclass's resource list and need not be
included in the subclass's resource list.
For example, the
.PN Core 
class contains a resource entry for background_pixel.
Consequently,
the implementation of Label need not also have a resource entry
for background_pixel.
However, a subclass,
by specifying a resource entry for that field in its own resource list,
can override the resource entry for any field declared in a superclass.
This is most often done to override the defaults provided in the
superclass with new ones.
At class initialization time, 
resource lists for that class are scanned from the superclass down 
to the class to look for resources with the same offset.
A matching resource in a subclass will be reordered to override
the superclass entry.
(A copy of the superclass resource list is made to
avoid affecting other subclasses of the superclass.)
.NH 2
Subresources
.XS
\*(SN Subresources
.XE
.LP
A widget does not do anything to get its own resources;
instead,
.PN XtCreateWidget
does this automatically before calling the class initialize procedure.
.LP
Some widgets have subparts that are not widgets but for which the widget
would like to fetch resources.
For example, the Text widget fetches resources for its source and sink.
Such widgets call
.PN XtGetSubresources
to accomplish this.
.IN "XtGetSubresources" "" "@DEF@"
.FD 0
void XtGetSubresources(\fIw\fP, \fIbase\fP, \fIname\fP, \fIclass\fP, \
\fIresources\fP, \fInum_resources\fP, \fIargs\fP, \fInum_args\fP)
.br
      Widget \fIw\fP;
.br
      caddr_t \fIbase\fP;
.br
      String \fIname\fP;
.br
      String \fIclass\fP;
.br
      XtResourceList \fIresources\fP;
.br
      Cardinal \fInum_resources\fP;
.br
      ArgList \fIargs\fP;
.br
      Cardinal \fInum_args\fP;
.FN
.ds Wi that wants resources for a subpart
.IP \fIw\fP 1i
Specifies the widget \*(Wi.
.ds Ba written
.IP \fIbase\fP 1i
Specifies the base address of the subpart data structure where the resources
should be \*(Ba.
.IP \fIname\fP 1i
Specifies the name of the subpart.
.IP \fIclass\fP 1i
Specifies the class of the subpart.
.IP \fIresources\fP 1i
Specifies the resource list for the subpart.
.IP \fInum_resources\fP 1i
Specifies the number of resources in the resource list.
.ds Al to override resources obtained from the resource database
.IP \fIargs\fP 1i
Specifies the argument list \*(Al.
.IP \fInum_args\fP 1i
Specifies the number of arguments in the argument list.
.LP
The
.PN XtGetSubresources
function constructs a name/class list from the application name/class,
the name/classes of all its ancestors, and the widget itself.
Then, it appends to this list the name/class pair passed in.
The resources are fetched from the argument list, the resource database,
or the default values in the resource list.
Then, they are copied into the subpart record.
If args is NULL,
num_args must be zero.
However, if num_args is zero,
the argument list is not referenced.
.NH 2
Obtaining Application Resources
.XS
\fB\*(SN Obtaining Application Resources\fP
.XE
.LP
To retrieve resources that are not specific to a widget
but apply to the overall application, use
.PN XtGetApplicationResources .
.IN "XtGetApplicationResources" "" "@DEF@"
.FD 0
void XtGetApplicationResources(\fIw\fP, \fIbase\fP, \fIresources\fP, \
\fInum_resources\fP, \fIargs\fP, \fInum_args\fP)
.br
      Widget \fIw\fP;
.br
      caddr_t \fIbase\fP;
.br
      XtResourceList \fIresources\fP;
.br
      Cardinal \fInum_resources\fP;
.br
      ArgList \fIargs\fP;
.br
      Cardinal \fInum_args\fP;
.FN
.ds Wi that identifies the resource database to search
.IP \fIw\fP 1i
Specifies the widget \*(Wi.
(The database is that associated with the display for this widget.)
.ds Ba written
.IP \fIbase\fP 1i
Specifies the base address of the subpart data structure where the resources
should be \*(Ba.
.IP \fIresources\fP 1i
Specifies the resource list for the subpart.
.IP \fInum_resources\fP 1i
Specifies the number of resources in the resource list.
.ds Al to override resources obtained from the resource database
.IP \fIargs\fP 1i
Specifies the argument list \*(Al.
.IP \fInum_args\fP 1i
Specifies the number of arguments in the argument list.
.LP
The
.PN XtGetApplicationResources
function first uses the passed widget,
which is usually an application shell,
to construct a resource name and class list,
Then, it retrieves the resources from the argument list,
the resource database, or the resource list default values.
After adding base to each address,
.PN XtGetApplicationResources
copies the resources into the address given in the resource list.
If args is NULL,
num_args must be zero.
However, if num_args is zero,
the argument list is not referenced.
The portable way to specify application resources is to declare them
as members of a structure and pass the address of the structure 
as the base argument.
.NH 2
Resource Conversions
.XS
\*(SN Resource Conversions
.XE
.LP
The \*(xI provide a mechanism for registering representation converters that
are automatically invoked by the resource fetching routines.
The \*(xI additionally provide and registers several commonly used converters.
This resource conversion mechanism serves several purposes:
.IP \(bu 5
It permits user and application resource files to contain ASCII
representations of nontextual values.
.IP \(bu 5
It allows textual or other representations of default resource values that
are dependent on the display, screen, or color map, and thus must be
computed at run time.
.IP \(bu 5
It caches all conversion source and result data.
Conversions that require much computation or space
(for example, string to translation table)
or that require round trips to the server
(for example, string to font or color) are performed only once.
.NH 3
Predefined Resource Converters
.XS
\*(SN Predefined Resource Converters
.XE
.LP
The \*(xI define all the representations used in the 
.PN Core , 
.PN Composite , 
.PN Constraint , 
and 
.PN Shell 
widgets.
It registers the following resource converters:
.LP
From
.PN XtRString 
to:
.IP
.PN XtRAcceleratorTable ,
.PN XtRBoolean ,
.PN XtRBool ,
.PN XtRCursor ,
.PN XtRDimension ,
.PN XtRDisplay ,
.PN XtRFile ,
.PN XtRFloat ,
.PN XtRFont ,
.PN XtRFontStruct ,
.PN XtRInt ,
.PN XtRPixel ,
.PN XtRPosition ,
.PN XtRShort ,
.PN XtRTranslationTable ,
and
.PN XtRUnsignedChar .
.LP
From
.PN XtRColor ,
to:
.PN XtRPixel .
.LP
From
.PN XRInt ,
to:
.IP
.PN XtRBoolean ,
.PN XtRBool ,
.PN XtRColor ,
.PN XtRDimension ,
.PN XtRFloat ,
.PN XtRFont ,
.PN XtRPixel ,
.PN XtRPixmap ,
.PN XtRPosition ,
.PN XtRShort ,
and
.PN XtRUnsignedChar .
.LP
From
.PN XtRPixel ,
to:
.PN XtRColor .
.LP
The string to pixel conversion has two predefined constants that are
guaranteed to work and contrast with each other
.Pn ( XtDefaultForeground
and
.PN XtDefaultBackground ).
They evaluate the black and white pixel values of the widget's screen, 
respectively.
For applications that run with reverse video, however,
they evaluate the white and black pixel values of the widget's screen,
respectively.
Similarly, the string to font and font structure converters recognize
the constant
.PN XtDefaultFont
and evaluate this to the font in the screen's default graphics context.
.NH 3
New Resource Converters
.XS
\*(SN New Resource Converters
.XE
.LP
Type converters use pointers to
.PN XrmValue
structures (defined in
.Pn < X11/Xresource.h >)
for input and output values.
.LP
.Ds 0
.TA .5i 3i
.ta .5i 3i
typedef struct {
	unsigned int \fIsize\fP;
	caddr_t \fIaddr\fP;
} XrmValue, *XrmValuePtr;
.De
.LP
A resource converter procedure pointer is of type
.PN XtConverter :
.IN "XtConverter" "" "@DEF@"
.FD 0
typedef void (*XtConverter)(XrmValue *, Cardinal *, XrmValue *, XrmValue *);
.br
      XrmValue *\fIargs\fP;
.br
      Cardinal *\fInum_args\fP;
.br
      XrmValue *\fIfrom\fP;
.br
      XrmValue *\fIto\fP;
.FN
.IP \fIargs\fP 1i
Specifies a list of additional
.PN XrmValue
arguments to the converter if additional context is needed
to perform the conversion or NULL.
For example, the string-to-font converter needs the widget's screen,
or the string to pixel converter needs the widget's screen and color map.
.IP \fInum_args\fP 1i
Specifies the number of additional 
.PN XrmValue 
arguments or zero.
.IP \fIfrom\fP 1i
Specifies the value to convert.
.IP \fIto\fP 1i
Specifies the descriptor to use to return the converted value.
.LP
Type converters should perform the following actions:
.IP \(bu 5
Check to see that the number of arguments passed is correct.
.IP \(bu 5
Attempt the type conversion.
.IP \(bu 5
If successful, return a pointer to the data in the to parameter;
otherwise, call
.PN XtWarningMsg
and return without modifying the to argument.
.LP
Most type converters just take the data described by the specified from
argument and return data by writing into the specified to argument.
A few need other information, which is available in the specified 
argument list.
A type converter can invoke another type converter,
which allows differing sources that may convert into a common intermediate
result to make maximum use of the type converter cache.
.LP
Note that the address written to->addr cannot be that of a local variable of 
the converter because this is not valid after the converter returns.
It should be a pointer to a static variable,
as in the following example where screenColor is returned.
.LP
The following is an example of a converter that takes a string
and converts it to a Pixel:
.LP
.Ds 0
.TA .3i .7i 1i 1.3i 1.7i 2i
.ta .3i .7i 1i 1.3i 1.7i 2i
static void CvtStringToPixel(args, num_args, fromVal, toVal)
	XrmValue	*args;
	Cardinal		*num_args;
	XrmValue	*fromVal;
	XrmValue	*toVal;
{
	static XColor	screenColor;
	XColor		exactColor;
	Screen		*screen;
	Colormap		colormap;
	Status			status;
	char			message[1000];
	XrmQuark		q;
	String			params[1];
	Cardinal		num_params = 1;

	if (*num_args != 2)
		XtErrorMsg("cvtStringToPixel","wrongParameters","XtToolkitError",
			"String to pixel conversion needs screen and colormap arguments",
			(String *)NULL, (Cardinal *)NULL);

	screen = *((Screen **) args[0].addr);
	colormap = *((Colormap *) args[1].addr);

	LowerCase((char *) fromVal->addr, message);
	q = XrmStringToQuark(message);

	if (q == XtQExtdefaultbackground) { done(&screen->white_pixel, Pixel); return; }
	if (q == XtQExtdefaultforeground) { done(&screen->black_pixel, Pixel); return; }

	if ((char) fromVal->addr[0] == '#') {  /* some color rgb definition */

		status = XParseColor(DisplayOfScreen(screen), colormap, (String) fromVal->addr, 
			&screenColor);
		if (status != 0) status = XAllocColor(DisplayOfScreen(screen), colormap, &screenColor);

	} else  /* some color name */

		status = XAllocNamedColor(DisplayOfScreen(screen), colormap, (String) fromVal->addr, 
			&screenColor, &exactColor);

	if (status == 0) {

		params[0]=(String)fromVal->addr;
		XtWarningMsg("cvtStringToPixel","noColormap","XtToolkitError",
			"Cannot allocate colormap entry for \\"%s\\"", params, &num_params);

	} else {

		toVal->addr = (caddr_t)&screenColor.pixel;
		toVal->size = sizeof(Pixel);

	}
};
.De
.LP
All type converters should define some set of conversion values that they
are guaranteed to succeed on so these can be used in the resource defaults.
This issue arises only with conversions, such as fonts and colors,
where there is no string representation that all server implementations
will necessarily recognize.
For resources like these,
the converter should define a symbolic constant (for example,
.PN XtDefaultForeground ,
.PN XtDefaultBackground ,
or
.PN XtDefaultFont ).
.NH 3
Issuing Conversion Warnings
.XS
\*(SN Issuing Conversion Warnings
.XE
.LP
The
.PN XtStringConversionWarning
function is a convenience routine for new resource converters
that convert from strings.
.FD 0
void XtStringConversionWarning(\fIsrc\fP, \fIdst_type\fP)
.br
	String \fIsrc\fP, \fIdst_type\fP;
.FN
.IP \fIsrc\fP 1i
Specifies the string that could not be converted.
.IP \fIdst_type\fP 1i
Specifies the name of the type to which the string could not be converted.
.LP
The
.PN XtStringConversionWarning
function issues a warning message with name ``conversionError'',
type ``string'', class ``XtToolkitError, and the default message string
``Cannot convert "\fIsrc\fP" to type \fIdst_type\fP''.
.NH 3
Registering a New Resource Converter
.XS
\fB\*(SN Registering a New Resource Converter\fP
.XE
.LP
To register a new converter, use 
.PN XtAppAddConverter .
.IN "XtAppAddConverter" "" "@DEF@"
.FD 0
void XtAppAddConverter(\fIapp_context\fP, \fIfrom_type\fP, \fIto_type\fP, \
\fIconverter\fP, \fIconvert_args\fP, \fInum_args\fP)
.br
    XtAppContext \fIapp_context\fP;
.br
    String \fIfrom_type\fP;
.br
    String \fIto_type\fP;
.br
    XtConverter \fIconverter\fP;
.br
    XtConvertArgList \fIconvert_args\fP;
.br
    Cardinal \fInum_args\fP;
.FN
.IP \fIapp_context\fP 1i
Specifies the application context.
.IP \fIfrom_type\fP 1i
Specifies the source type.
.IP \fIto_type\fP 1i
Specifies the destination type.
.IP \fIconverter\fP 1i
Specifies the type converter procedure.
.IP \fIconvert_args\fP 1i
Specifies how to compute the additional arguments to the converter or NULL.
.IP \fInum_args\fP 1i
Specifies the number of additional arguments to the converter or zero.
.LP
If the same from_type and to_type are specified in two calls to
.PN XtAppAddConverter ,
the second call overrides the first.
For the few type converters that need additional arguments,
the \*(xI conversion mechanism provides a method of specifying
how these arguments should be computed.
The enumerated type
.PN XtAddressMode
and the structure
.PN XtConvertArgRec
specify how each argument is derived.
These are defined in
.Pn < X11/Convert.h >.
.LP
.Ds 0
.TA .5i 2.5i
.ta .5i 2.5i
typedef enum {
	/* address mode	parameter representation */
	   XtAddress,	/* address */
	   XtBaseOffset,	/* offset */
	   XtImmediate,	/* constant */
	   XtResourceString,	/* resource name string */
	   XtResourceQuark	/* resource name quark */
} XtAddressMode;

typedef struct {
	XtAddressMode address_mode;
	caddr_t address_id;
	Cardinal size;
} XtConvertArgRec, *XtConvertArgList;
.De
The address_mode field specifies how the address_id field should be
interpreted.
.PN XtAddress
causes address_id to be interpreted as the address of the data.
.PN XtBaseOffset
causes address_id to be interpreted as the offset from the widget base.
.PN XtImmediate
causes address_id to be interpreted as a constant.
.PN XtResourceString
causes address_id to be interpreted as the name of a resource
that is to be converted into an offset from widget base.
.PN XtResourceQuark
is an internal compiled form of an
.PN XtResourceString .
The size field specifies the length of the data in bytes.
.LP
The following provides the code that was used to register the CvtStringToPixel 
routine shown earlier:
.LP
.Ds 0
.TA .5i 3i
.ta .5i 3i
static XtConvertArgRec colorConvertArgs[] = {
	{XtBaseOffset, (caddr_t) XtOffset(Widget, core.screen),  sizeof(Screen *)},
	{XtBaseOffset, (caddr_t) XtOffset(Widget, core.colormap),sizeof(Colormap)}
};

XtAddConverter(XtRString, XtRPixel, CvtStringToPixel,
    colorConvertArgs, XtNumber(colorConvertArgs));
.De
.LP
The conversion argument descriptors colorConvertArgs and screenConvertArg are
predefined.
The screenConvertArg descriptor puts the widget's screen field into args[0].
The colorConvertArgs descriptor puts the widget's screen field into args[0],
and the widget's colormap field into args[1].
.LP
Conversion routines should not just put a descriptor for the address of the
base of the widget into args[0], and use that in the routine.
They should pass in the actual values that the conversion depends on.
By keeping the dependencies of the conversion procedure specific,
it is more likely that subsequent conversions will find what they need
in the conversion cache.
This way the cache is smaller and has fewer and more widely applicable entries.
.NH 3
Resource Converter Invocation
.XS
\*(SN Resource Converter Invocation
.XE
.LP
All resource-fetching routines (for example,
.PN XtGetSubresources ,
.PN XtGetApplicationResources ,
and so on) call resource converters if the user specifies a resource
that is a different representation from the desired representation or if the
widget's default resource value representation is different from the desired
representation.
.sp
.LP
To invoke resource conversions, use
.PN XtConvert
or
.PN XtDirectConvert .
.LP
.sp
.IN "XtConvert" "" "@DEF@"
.FD 0
void XtConvert(\fIw\fP, \fIfrom_type\fP, \fIfrom\fP, \fIto_type\fP, \
\fIto_return\fP)
.br
    Widget \fIw\fP;
.br
    String \fIfrom_type\fP;
.br
    XrmValuePtr \fIfrom\fP;
.br
    String \fIto_type\fP;
.br
    XrmValuePtr \fIto_return\fP;
.FN
.ds Wi to use for additional arguments (if any are needed)
.IP \fIw\fP 1i
Specifies the widget \*(Wi.
.IP \fIfrom_type\fP 1i
Specifies the source type.
.IP \fIfrom\fP 1i
Specifies the value to be converted.
.IP \fIto_type\fP 1i
Specifies the destination type.
.IP \fIto_return\fP 1i
Returns the converted value.
.LP
.IN "XtDirectConvert" "" "@DEF@"
.FD 0
void XtDirectConvert(\fIconverter\fP, \fIargs\fP, \fInum_args\fP, \fIfrom\fP, \
\fIto_return\fP)
.br
    XtConverter \fIconverter\fP;
.br
    XrmValuePtr \fIargs\fP;
.br
    Cardinal \fInum_args\fP;
.br
    XrmValuePtr \fIfrom\fP;
.br
    XrmValuePtr \fIto_return\fP;
.FN
.IP \fIconverter\fP 1i
Specifies the conversion procedure that is to be called.
.ds Al that contains the additional arguments needed to perform \
the conversion (often NULL)
.IP \fIargs\fP 1i
Specifies the argument list \*(Al.
.IP \fInum_args\fP 1i
Specifies the number of additional arguments (often zero).
.IP \fIfrom\fP 1i
Specifies the value to be converted.
.IP \fIto_return\fP 1i
Returns the converted value.
.LP
The
.PN XtConvert
function looks up the type converter registered to convert from_type 
to to_type, computes any additional arguments needed, and then calls
.PN XtDirectConvert .
The
.PN XtDirectConvert
function looks in the converter cache to see if this conversion procedure
has been called with the specified arguments.
If so, it returns a descriptor for information stored in the cache;
otherwise, it calls the converter and enters the result in the cache.
.LP
Before calling the specified converter,
.PN XtDirectConvert
sets the return value size to zero and the return value address to NULL.
To determine if the conversion was successful,
the client should check to_return.address for non-NULL.
.NH 2
Reading and Writing Widget State
.XS
\fB\*(SN Reading and Writing Widget State\fP
.XE
.LP
Any resource field in a widget can be read or written by a client.
On a write operation,
the widget decides what changes it will actually allow and updates all
derived fields appropriately.
.NH 3
Obtaining Widget State
.XS
\fB\*(SN Obtaining Widget State\fP
.XE
.LP
To retrieve the current value of a resource associated with a
widget instance, use
.PN XtGetValues .
.IN "XtGetValues" "" "@DEF@"
.FD 0
void XtGetValues(\fIw\fP, \fIargs\fP, \fInum_args\fP)
.br
      Widget \fIw\fP;
.br
      ArgList \fIargs\fP;
.br
      Cardinal \fInum_args\fP;
.FN
.IP \fIw\fP 1i
Specifies the widget.
.ds Al of name/address pairs that contain the resource name \
and the address into which the resource value is to be stored
.IP \fIargs\fP 1i
Specifies the argument list \*(Al.
The resource names are widget-dependent.
.IP \fInum_args\fP 1i
Specifies the number of arguments in the argument list.
.LP
The
.PN XtGetValues
function starts with the resources specified for the core widget fields
and proceeds down the subclass chain to the widget.
The value field of a passed argument list should contain the
address into which to store the corresponding resource value.
It is the caller's responsibility
to allocate and deallocate this storage according to the size of the
resource representation type used within the widget.
.LP
If the widget's parent is a subclass of
.PN constraintWidgetClass ,
.PN XtGetValues
then fetches the values for any constraint resources requested.
It starts with the constraint resources specified for
.PN constraintWidgetClass
and proceeds down to the subclass chain to the parent's constraint resources.
If the argument list contains a resource name that is not found in any of the
resource lists searched, 
the value at the corresponding address is not modified.
.IN "hook"
Finally, if the get_values_hook procedures are non-NULL, 
they are called in superclass-to-subclass order after
all the resource values have been fetched by
.PN XtGetValues .
This permits a subclass to provide nonwidget resource data to
.PN XtGetValues .
.NH 4
Widget Subpart Resource Data: the get_values_hook Procedure
.XS
\*(SN Widget Subpart Resource Data: the get_values_hook Procedure
.XE
.LP
Widgets that have subparts can return resource values from them for
.PN XtGetValues
by supplying a get_values_hook procedure.
The get_values_hook procedure pointer is of type
.PN XtArgsProc :
.IN "get_values_hook procedure" "" "@DEF@"
.FD 0
typedef void (*XtArgsProc)(Widget, ArgList, Cardinal *);
.br
     Widget \fIw\fP;
.br
     ArgList \fIargs\fP;
.br
     Cardinal *\fInum_args\fP;
.FN
.IP \fIw\fP 1i
Specifies the widget whose nonwidget resource values are to be retrieved.
.IP \fIargs\fP 1i
Specifies the argument list that was passed to
.PN XtCreateWidget .
.IP \fInum_args\fP 1i
Specifies the number of arguments in the argument list.
.LP
The widget should call
.PN XtGetSubvalues
and pass in its subresource list and the arg and num_args parameters.
.NH 4
Widget Subpart State
.XS
\*(SN Widget Subpart State
.XE
.LP
To retrieve the current value of a nonwidget resource data associated with a
widget instance, use
.PN XtGetSubvalues .
For a discussion of nonwidget subclass resources,
see Section 9.4.
.IN "XtGetSubvalues" "" "@DEF@"
.FD 0
void XtGetSubvalues(\fIbase\fP, \fIresources\fP, \fInum_resources\fP, \
\fIargs\fP, \fInum_args\fP)
.br
      caddr_t \fIbase\fP;
.br
      XtResourceList \fIresources\fP;
.br
      Cardinal \fInum_resources\fP;
.br
      ArgList \fIargs\fP;
.br
      Cardinal \fInum_args\fP;
.FN
.ds Ba retrieved
.IP \fIbase\fP 1i
Specifies the base address of the subpart data structure where the resources
should be \*(Ba.
.IP \fIresources\fP 1i
Specifies the nonwidget resources list.
.IP \fInum_resources\fP 1i
Specifies the number of resources in the resource list.
.ds Al of name/address pairs that contain the resource name \
and the address into which the resource value is to be stored
.IP \fIargs\fP 1i
Specifies the argument list \*(Al.
The arguments and values passed in are dependent on the subpart.
The storage for argument values that are pointed to by the
argument list must be deallocated by the application when no longer needed.
.IP \fInum_args\fP 1i
Specifies the number of arguments in the argument list.
.LP
The
.PN XtGetSubvalues
function obtains resource values from the structure identified by base.
.NH 3
Setting Widget State
.XS
\fB\*(SN Setting Widget State\fP
.XE
.LP
To modify the current value of a resource associated with a widget
instance, use
.PN XtSetValues .
.IN "XtSetValues" "" "@DEF@"
.FD 0
void XtSetValues(\fIw\fP, \fIargs\fP, \fInum_args\fP)
.br
      Widget \fIw\fP;
.br
      ArgList \fIargs\fP;
.br
      Cardinal \fInum_args\fP;
.FN
.IP \fIw\fP 1i
Specifies the widget.
.ds Al of name/value pairs that contain the resources to be modified \
and their new values
.IP \fIargs\fP 1i
Specifies the argument list \*(Al.
The resources and values passed are dependent on
the widget being modified.
.IP \fInum_args\fP 1i
Specifies the number of arguments in the argument list.
.LP
The
.PN XtSetValues
function starts with the resources specified for the 
.PN Core 
widget fields and proceeds down the subclass chain to the widget.
At each stage,
it writes the new value (if specified by one of the arguments) or the existing
value (if no new value is specified) to a new widget data record.
.PN XtSetValues
then calls the set_values procedures for the widget in superclass-to-subclass 
order.
.IN "hook"
If the widget has any non-NULL set_values_hook fields,
these are called immediately after the
corresponding set_values procedure.
This procedure permits subclasses to set nonwidget data for
.PN XtSetValues .
.LP
If the widget's parent is a subclass of
.PN constraintWidgetClass ,
.PN XtSetValues
also updates the widget's constraints.
It starts with the constraint resources specified for
.PN constraintWidgetClass
and proceeds down the subclass chain to the parent's class.
At each stage,
it writes the new value or the existing value to a new constraint record.
It then calls the constraint set_values procedures from
.PN constraintWidgetClass
down to the parent's class.
The constraint set_values procedures are called with widget arguments,
as for all set_values procedures, not just the constraint record arguments,
so that they can make adjustments to the desired values based
on full information about the widget.
.LP
.PN XtSetValues
determines if a geometry request is needed by comparing the current widget to
the new widget.
If any geometry changes are required,
it makes the request, and the geometry manager returns
.PN XtGeometryYes ,
.PN XtGeometryAlmost ,
or
.PN XtGeometryNo .
If
.PN XtGeometryYes ,
.PN XtSetValues
calls the widget's resize procedure.
If
.PN XtGeometryNo ,
.PN XtSetValues
resets the geometry fields to their original values.
If
.PN XtGeometryAlmost ,
.PN XtSetValues
calls the set_values_almost procedure,
which determines what should be done and writes new values for the
geometry fields into the new widget.
.PN XtSetValues
then repeats this process,
deciding once more whether the geometry manager should be called.
.LP
Finally, if any of the set_values procedures returned 
.PN True ,
.PN XtSetValues
causes the widget's expose procedure to be invoked by calling the Xlib
.PN XClearArea
function on the widget's window.
.NH 4
Widget State: the set_values Procedure
.XS
\*(SN Widget State: the set_values Procedure
.XE
.LP
The set_values procedure pointer in a widget class is of type
.PN XtSetValuesFunc :
.IN "XtSetValuesFunc" "" "@DEF@"
.IN "set_values procedure" "" "@DEF@"
.FD 0
typedef Boolean (*XtSetValuesFunc)(Widget, Widget, Widget);
.br
      Widget \fIcurrent\fP;
.br
      Widget \fIrequest\fP;
.br
      Widget \fInew\fP;
.FN
.IP \fIcurrent\fP 1i
Specifies a copy of the widget as it was before the
.PN XtSetValues
call.
.IP \fIrequest\fP 1i
Specifies a copy of the widget with all values changed as asked for by the
.PN XtSetValues
call before any class set_values procedures have been called.
.IP \fInew\fP 1i
Specifies the widget with the new values that are actually allowed.
.LP
The set_values procedure should recompute any field
derived from resources that are changed
(for example, many GCs depend on foreground and background).
If no recomputation is necessary and if none of the resources specific to a
subclass require the window to be redisplayed when their values are changed,
you can specify NULL for the set_values field in the class record.
.LP
Like the initialize procedure,
set_values mostly deals only with the fields defined in the subclass,
but it has to resolve conflicts with its superclass,
especially conflicts over width and height.
.LP
Sometimes a subclass may want to overwrite values filled in by its
superclass.
In particular, size calculations of a superclass are often
incorrect for a subclass and in this case,
the subclass must modify or recalculate fields declared
and computed by its superclass.
.LP
As an example,
a subclass can visually surround its superclass display.
In this case, the width and height calculated by the superclass set_values
procedure are too small and need to be incremented by the size of the surround.
The subclass needs to know if its superclass's size was calculated by the
superclass or was specified explicitly.
All widgets must place themselves into whatever size is explicitly given,
but they should compute a reasonable size if no size is requested.
How does a subclass know the difference between a specified size
and a size computed by a superclass?
.LP
The request and new parameters provide the necessary information.
The request widget is the widget as originally requested.
The new widget starts with the values in the request,
but it has been updated by all superclass set_values procedures called
so far.
A subclass set_values procedure can compare these two to resolve
any potential conflicts.
.LP
In the above example,
the subclass with the visual surround can see
if the width and height in the request widget are zero.
If so,
it adds its surround size to the width and height fields in the new widget.
If not, it must make do with the size originally specified.
.LP
The new widget is the actual widget instance record.
Therefore,
the set_values procedure should do all its work on the new widget
(the request widget should never be modified),
and if it needs to call any routines that operate on a widget,
it should specify new as the widget instance.
.LP
The widget specified by new starts with the values of that specified by request
but has been modified by any superclass set_values procedures.
A widget need not refer to the request widget,
unless it must resolve conflicts between the current and new widgets.
Any changes that the widget needs to make, including geometry changes,
should be made in the new widget.
.LP
Finally, the set_values procedure must return a Boolean that indicates whether
the widget needs to be redisplayed.
Note that a change in the geometry fields alone does not require
the set_values procedure to return 
.PN True ;
the X server will eventually generate an
.PN Expose
event, if necessary.
After calling all the set_values procedures,
.PN XtSetValues
forces a redisplay by calling the Xlib
.PN XClearArea
function if any of the set_values procedures returned 
.PN True .
Therefore, a set_values procedure should not try to do its own redisplaying.
.LP
Set_values procedures should not do any work in response to changes in
geometry because
.PN XtSetValues
eventually will perform a geometry request, and that request might be denied.
If the widget actually changes size in response to a
.PN XtSetValues ,
its resize procedure are called.
Widgets should do any geometry-related work in their resize procedure.
.LP
Note that it is permissible to call
.PN XtSetValues
before a widget is realized.
Therefore, the set_values proc must not assume that the widget is realized.
.NH 4
Widget State: the set_values_almost Procedure
.XS
\*(SN Widget State: the set_values_almost Procedure
.XE
.LP
The set_values_almost procedure pointer in a widget class is of type
.PN XtAlmostProc :
.IN "set_values_almost procedure" "" "@DEF@"
.IN "XtAlmostProc" "" "@DEF@"
.FD 0
typedef void (*XtAlmostProc)(Widget, Widget, XtWidgetGeometry *, \
XtWidgetGeometry *);
.br
      Widget \fIw\fP;
.br
      Widget \fInew_widget_return\fP;
.br
      XtWidgetGeometry *\fIrequest\fP;
.br
      XtWidgetGeometry *\fIreply\fP;
.FN
.IP \fIw\fP 1i
Specifies the widget on which the geometry change is requested.
.IP \fInew_widget_return\fP 1i
Specifies the new widget into which the geometry changes are to be stored.
.IP \fIrequest\fP 1i
Specifies the original geometry request that was sent to the geometry
manager that returned
.PN XtGeometryAlmost .
.IP \fIreply\fP 1i
Specifies the compromise geometry that was returned by the geometry
manager that returned
.PN XtGeometryAlmost .
.LP
Most classes inherit this operation from their superclass by
specifying 
.PN XtInheritSetValuesAlmost 
in the class initialization.
The 
.PN Core 
set_values_almost procedure accepts the compromise suggested.
.LP
The set_values_almost procedure is called when a client tries to set a widget's
geometry by means of a call to
.PN XtSetValues ,
and the geometry manager cannot
satisfy the request but instead returns
.PN XtGeometryAlmost
and a compromise geometry.
The set_values_almost procedure takes the original geometry and the
compromise geometry and determines whether the compromise is acceptable or a
different compromise might work.
It returns its results in the new_widget parameter,
which is then sent back to the geometry manager for another try.
.NH 4
Widget State: the constraint set_values Procedure
.XS
\*(SN Widget State: the constraint set_values Procedure
.XE
.IN "set_values procedure"
.LP
The constraint set_values procedure pointer is of type
.PN XtSetValuesFunc .
The values passed to the parent's constraint set_values procedure
are the same as those passed to the child's class
set_values procedure.
A class can specify NULL for the set_values field of the
.PN ConstraintPart
if it need not compute anything.
.LP
The constraint set_values procedure should recompute any constraint fields
derived from constraint resource that are changed.
Further, it should modify the widget fields as appropriate.
For example, if a constraint for the maximum height of a widget is changed
to a value smaller than the widget's current height,
the constraint set_values procedure should reset the height field in the
widget.
.NH 4
Widget Subpart State
.XS
\*(SN Setting Widget Subpart State
.XE
.LP
To set the current value of a nonwidget resource associated with a
widget instance, use
.PN XtSetSubvalues .
For a discussion of nonwidget subclass resources,
see Section 9.4.
.IN "XtSetSubvalues" "" "@DEF@"
.FD 0
void XtSetSubvalues(\fIbase\fP, \fIresources\fP, \fInum_resources\fP, \
\fIargs\fP, \fInum_args\fP)
.br
      caddr_t \fIbase\fP;
.br
      XtResourceList \fIresources\fP;
.br
      Cardinal \fInum_resources\fP;
.br
      ArgList \fIargs\fP;
.br
      Cardinal \fInum_args\fP;
.FN
.ds Ba written
.IP \fIbase\fP 1i
Specifies the base address of the subpart data structure where the resources
should be \*(Ba.
.IP \fIresources\fP 1i
Specifies the current nonwidget resources values.
.IP \fInum_resources\fP 1i
Specifies the number of resources in the resource list.
.ds Al of name/value pairs that contain the resources to be modified \
and their new values
.IP \fIargs\fP 1i
Specifies the argument list \*(Al.
The resources and values passed are dependent on
the subpart of the widget being modified.
.IP \fInum_args\fP 1i
Specifies the number of arguments in the argument list.
.LP
The
.PN XtSetSubvalues
function stores resources into the structure identified by base.
.NH 4
Widget Subpart Resource Data: the set_values_hook Procedure
.XS
\*(SN Widget Subpart Resource Data: the set_values_hook Procedure
.XE
.IN "set_values_hook procedure"
.LP
Widgets that have a subpart can set the resource values by using
.PN XtSetValues
and supplying a set_values_hook procedure.
The set_values_hook procedure pointer in a widget class is of type
.PN XtArgsFunc :
.IN "set_values_hook procedure" "" "@DEF@"
.IN "XtArgsFunc" "" "@DEF@"
.FD 0
typedef Boolean (*XtArgsFunc)(Widget, Arglist, Cardinal *);
.br
     Widget \fIw\fP;
.br
     ArgList \fIargs\fP;
.br
     Cardinal *\fInum_args\fP;
.FN
.IP \fIw\fP 1i
Specifies the widget whose nonwidget resource values are to be changed.
.IP \fIargs\fP 1i
Specifies the argument list that was passed to
.PN XtCreateWidget .
.IP \fInum_args\fP 1i
Specifies the number of arguments in the argument list.
.bp
