1<sect1 id="Widget_Source_File">
2<title>Widget Source File</title>
3<para>
4<!-- .LP -->
5The source code file implements the widget class itself.  The unique
6part of this file is the declaration and initialization of the
7widget class record structure and the declaration of all resources and
8action routines added by the widget class.
9</para>
10<para>
11<!-- .LP -->
12The contents of the Template implementation file,
13<filename class="headerfile">&lt;X11/Xaw/Template.c&gt;</filename>,
14are:
15</para>
16<!-- .CB -->
17<!-- .\".so ../../lib/Xaw/Template.c -->
18<literallayout class="monospaced">
19/* Copyright (c) X Consortium 1987, 1988
20 */
21
22#include &lt;X11/IntrinsicP.h&gt;
23#include &lt;X11/StringDefs.h&gt;
24#include "TemplateP.h"
25
26static XtResource resources[] = {
27#define offset(field) XtOffsetOf(TemplateRec, template.field)
28	/* {name, class, type, size, offset, default_type, default_addr}, */
29    { XtNtemplateResource, XtCTemplateResource, XtRTemplateResource,
30	  sizeof(char*), offset(resource), XtRString, (XtPointer) "default" },
31#undef offset
32};
33
34static void TemplateAction(/* Widget, XEvent*, String*, Cardinal* */);
35
36static XtActionsRec actions[] =
37{
38	/* {name,	procedure}, */
39	{"template",	TemplateAction},
40};
41
42static char translations[] =
43"	&lt;Key&gt;:	template(&#x2006;) \\n\\
44";
45
46TemplateClassRec templateClassRec = {
47  { /* core fields */
48	/* superclass		*/	(WidgetClass) &amp;widgetClassRec,
49	/* class_name		*/	"Template",
50	/* widget_size		*/	sizeof(TemplateRec),
51	/* class_initialize	*/	NULL,
52	/* class_part_initialize */	NULL,
53	/* class_inited		*/	FALSE,
54	/* initialize		*/	NULL,
55	/* initialize_hook	*/	NULL,
56	/* realize		*/	XtInheritRealize,
57	/* actions		*/	actions,
58	/* num_actions		*/	XtNumber(actions),
59	/* resources		*/	resources,
60	/* num_resources	*/	XtNumber(resources),
61	/* xrm_class		*/	NULLQUARK,
62	/* compress_motion	*/	TRUE,
63	/* compress_exposure	*/	TRUE,
64	/* compress_enterleave	*/	TRUE,
65	/* visible_interest	*/	FALSE,
66	/* destroy		*/	NULL,
67	/* resize		*/	NULL,
68	/* expose		*/	NULL,
69	/* set_values		*/	NULL,
70	/* set_values_hook	*/	NULL,
71	/* set_values_almost	*/	XtInheritSetValuesAlmost,
72	/* get_values_hook	*/	NULL,
73	/* accept_focus		*/	NULL,
74	/* version		*/	XtVersion,
75	/* callback_private	*/	NULL,
76	/* tm_table		*/	translations,
77	/* query_geometry	*/	XtInheritQueryGeometry,
78	/* display_accelerator	*/	XtInheritDisplayAccelerator,
79	/* extension		*/	NULL
80  },
81  { /* template fields */
82	/* empty		*/	0
83  }
84};
85
86WidgetClass templateWidgetClass = (WidgetClass)&amp;templateClassRec;
87</literallayout>
88<!-- .CE -->
89<para>
90The resource list for the "WindowWidget" might look like the following:
91</para>
92<!-- .CB -->
93<literallayout class="monospaced">
94static XtResource resources[] = {
95#define offset(field) XtOffsetOf(WindowWidgetRec, window.field)
96	/* {name, class, type, size, offset, default_type, default_addr}, */
97	{ XtNdrawingColor1, XtCColor, XtRPixel, sizeof(Pixel),
98		  offset(color_1), XtRString, XtDefaultForeground },
99	{ XtNdrawingColor2, XtCColor, XtRPixel, sizeof(Pixel),
100		  offset(color_2), XtRString, XtDefaultForeground },
101	{ XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct*),
102		  offset(font), XtRString, XtDefaultFont },
103	{ XtNexposeCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
104		  offset(expose_callback), XtRCallback, NULL },
105	{ XtNcallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
106		  offset(input_callback), XtRCallback, NULL },
107#undef offset
108};
109</literallayout>
110<!-- .CE -->
111<para>
112<!-- .LP -->
113The user input callback will be implemented by an action procedure which
114passes the event pointer as call_data.  The action procedure
115is declared as:
116<!-- .CB -->
117</para>
118<literallayout class="monospaced">
119/* ARGSUSED */
120static void InputAction(w, event, params, num_params)
121	Widget w;
122	XEvent *event;
123	String *params;		/* unused */
124	Cardinal *num_params;	/* unused */
125{
126	XtCallCallbacks(w, XtNcallback, (XtPointer)event);
127}
128
129static XtActionsRec actions[] =
130{
131	/* {name,	procedure}, */
132	{"input",	InputAction},
133};
134</literallayout>
135<!-- .CE -->
136<para>
137<!-- .LP -->
138and the default input binding will be to execute the input callbacks on
139<function>KeyPress</function> and <function>ButtonPress : </function>
140</para>
141<literallayout class="monospaced">
142static char translations[] =
143"	&lt;Key&gt;:	input(&#x2006;) \\n\\
144 	&lt;BtnDown&gt;:	input(&#x2006;) \\
145";
146</literallayout>
147<!-- .CE -->
148<para>
149In the class record declaration and initialization, the only field that
150is different from the Template is the expose procedure:
151</para>
152<!-- .CB -->
153<literallayout class="monospaced">
154/* ARGSUSED */
155static void Redisplay(w, event, region)
156	Widget w;
157	XEvent *event;	/* unused */
158	Region region;
159{
160	XtCallCallbacks(w, XtNexposeCallback, (XtPointer)region);
161}
162
163WindowClassRec windowClassRec = {
164
165	...
166
167	/* expose	*/	Redisplay,
168</literallayout>
169<!-- .CE -->
170<para>
171<!-- .LP -->
172<!-- .sp -->
173The "WindowWidget" will also declare three public procedures to return the
174drawing colors and the font id, saving the application the effort of
175constructing an argument list for a call to
176<function>XtGetValues :</function>
177</para>
178<!-- .LP -->
179<!-- .CB -->
180<literallayout class="monospaced">
181Pixel WindowColor1(w)
182        Widget w;
183{
184        return ((WindowWidget)w)->window.color_1;
185}
186
187Pixel WindowColor2(w)
188        Widget w;
189{
190        return ((WindowWidget)w)->window.color_2;
191}
192
193Font WindowFont(w)
194        Widget w;
195{
196        return ((WindowWidget)w)->window.font->fid;
197}
198</literallayout>
199
200<para>
201The "WindowWidget" is now complete.  The application can retrieve the two
202drawing colors from the widget instance by calling either
203<function>XtGetValues</function>,
204or the <function>WindowColor</function> functions.  The actual window created for the
205"WindowWidget" is available by calling the
206<function>XtWindow</function> function.
207</para>
208</sect1>
209