1994689c1Smrg<sect2 id="Public_Header_File">
2994689c1Smrg<title>Public Header File</title>
3994689c1Smrg<para>
4994689c1SmrgThe public header file contains declarations that will be required by any
5994689c1Smrgapplication module that needs to refer to the widget; whether to create
6994689c1Smrgan instance of the class, to perform an
7421c997bSmrg<xref linkend='XtSetValues' xrefstyle='select: title'/>
8994689c1Smrgoperation, or to call a public routine implemented by the widget class.
9994689c1Smrg</para>
10994689c1Smrg<para>
11994689c1Smrg<!-- .LP -->
12994689c1SmrgThe contents of the Template public header file,
135ec34c4cSmrg<filename class="headerfile">&lt;X11/Xaw/Template.h&gt;</filename>,
14994689c1Smrgare:
15994689c1Smrg</para>
16994689c1Smrg<literallayout class="monospaced">
17994689c1Smrg..
18994689c1Smrg<!-- .CB -->
19994689c1Smrg<!-- .\".so ../../lib/Xaw/Template.h -->
20994689c1Smrg/* Copyright (c) X Consortium 1987, 1988 */
21994689c1Smrg
22994689c1Smrg#ifndef _Template_h
23994689c1Smrg#define _Template_h
24994689c1Smrg
25994689c1Smrg/****************************************************************
26994689c1Smrg *
27994689c1Smrg * Template widget
28994689c1Smrg *
29994689c1Smrg ****************************************************************/
30994689c1Smrg
31994689c1Smrg/* Resources:
32994689c1Smrg
33994689c1Smrg Name	Class		RepType	Default Value
34994689c1Smrg ----		-----		-------	-------------
35994689c1Smrg background	Background		Pixel	XtDefaultBackground
36994689c1Smrg border	BorderColor		Pixel	XtDefaultForeground
37994689c1Smrg borderWidth	BorderWidth		Dimension	1
38994689c1Smrg destroyCallback	Callback		Pointer	NULL
39994689c1Smrg height	Height		Dimension	0
40994689c1Smrg mappedWhenManaged	MappedWhenManaged	Boolean	True
41994689c1Smrg sensitive	Sensitive		Boolean	True
42994689c1Smrg width	Width		Dimension	0
43994689c1Smrg x		Position		Position	0
44994689c1Smrg y		Position		Position	0
45994689c1Smrg
46994689c1Smrg*/
47994689c1Smrg
48994689c1Smrg/* define any special resource names here that are not in &lt;X11/StringDefs.h&gt; */
49994689c1Smrg
50994689c1Smrg#define XtNtemplateResource "templateResource"
51994689c1Smrg
52994689c1Smrg#define XtCTemplateResource "TemplateResource"
53994689c1Smrg
54994689c1Smrg/* declare specific TemplateWidget class and instance datatypes */
55994689c1Smrg
56994689c1Smrgtypedef struct _TemplateClassRec*	TemplateWidgetClass;
57994689c1Smrgtypedef struct _TemplateRec*	TemplateWidget;
58994689c1Smrg
59994689c1Smrg/* declare the class constant */
60994689c1Smrg
61994689c1Smrgextern WidgetClass templateWidgetClass;
62994689c1Smrg
63994689c1Smrg#endif /* _Template_h */
64994689c1Smrg<!-- .CE -->
65994689c1Smrg</literallayout>
66994689c1Smrg<para>
67994689c1Smrg<!-- .LP -->
68994689c1Smrg<!-- .sp -->
69994689c1SmrgYou will notice that most of this file is documentation.  The crucial
70994689c1Smrgparts are the last 8 lines where macros for any private resource names
71994689c1Smrgand classes are defined and where the widget class datatypes and class
72994689c1Smrgrecord pointer are declared.
73994689c1Smrg</para>
74994689c1Smrg<para>
75994689c1Smrg<!-- .LP -->
76994689c1SmrgFor the "WindowWidget", we want 2 drawing colors, a callback list for
77994689c1Smrguser input and an
78994689c1Smrg<function>exposeCallback</function> callback list, and we will declare three
79994689c1Smrgconvenience procedures, so we need to add
80994689c1Smrg</para>
81994689c1Smrg<literallayout class="monospaced">
82994689c1Smrg<!-- .LP -->
83994689c1Smrg<!-- .sp -->
84994689c1Smrg<!-- .CB -->
85994689c1Smrg/* Resources:
86994689c1Smrg	...
87994689c1Smrg callback	Callback	Callback	NULL
88994689c1Smrg drawingColor1	Color	Pixel		XtDefaultForeground
89994689c1Smrg drawingColor2	Color	Pixel		XtDefaultForeground
90994689c1Smrg exposeCallback	Callback	Callback	NULL
91994689c1Smrg font		Font	XFontStruct*	XtDefaultFont
92994689c1Smrg	...
93994689c1Smrg */
94994689c1Smrg
95994689c1Smrg#define XtNdrawingColor1 "drawingColor1"
96994689c1Smrg#define XtNdrawingColor2 "drawingColor2"
97994689c1Smrg#define XtNexposeCallback "exposeCallback"
98994689c1Smrg
995ec34c4cSmrgextern Pixel WindowColor1(&#x2006;/* Widget */&#x2006;);
1005ec34c4cSmrgextern Pixel WindowColor2(&#x2006;/* Widget */&#x2006;);
1015ec34c4cSmrgextern Font  WindowFont(&#x2006;/* Widget */&#x2006;);
102994689c1Smrg<!-- .CE -->
103994689c1Smrg</literallayout>
104994689c1Smrg<para>
105994689c1Smrg<!-- .LP -->
106994689c1SmrgNote that we have chosen to call the input callback list by the generic
107994689c1Smrgname, <function>callback</function>, rather than a specific name.  If widgets that define
108994689c1Smrga single user-input action all choose the same resource name then there
109994689c1Smrgis greater possibility for an application to switch between widgets of
110994689c1Smrgdifferent types.
111994689c1Smrg</para>
112994689c1Smrg</sect2>
113994689c1Smrg<sect2 id="Private_Header_File">
114994689c1Smrg<title>Private Header File</title>
115994689c1Smrg<para>
116994689c1Smrg<!-- .LP -->
117994689c1SmrgThe private header file contains the complete declaration of the class
118994689c1Smrgand instance structures for the widget and any additional private data
119994689c1Smrgthat will be required by anticipated subclasses of the widget.
120994689c1SmrgInformation in the private header file is normally hidden from the
121994689c1Smrgapplication and is designed to be accessed only through other public
122994689c1Smrgprocedures; e.g.
1235ec34c4cSmrg<function>XtSetValues</function>.
124994689c1Smrg</para>
125994689c1Smrg<para>
126994689c1Smrg<!-- .LP -->
127994689c1SmrgThe contents of the Template private header file,
1285ec34c4cSmrg<filename class="headerfile">&lt;X11/Xaw/TemplateP.h&gt;</filename>,
129994689c1Smrgare:
130994689c1Smrg</para>
131994689c1Smrg<!-- .CB -->
132994689c1Smrg<!-- .\".so ../../lib/Xaw/TemplateP.h -->
133994689c1Smrg<literallayout class="monospaced">
134994689c1Smrg/* Copyright (c) X Consortium 1987, 1988
135994689c1Smrg */
136994689c1Smrg
137994689c1Smrg#ifndef _TemplateP_h
138994689c1Smrg#define _TemplateP_h
139994689c1Smrg
140994689c1Smrg#include &lt;X11/Xaw/Template.h&gt;
141994689c1Smrg/* include superclass private header file */
142994689c1Smrg#include &lt;X11/CoreP.h&gt;
143994689c1Smrg
144994689c1Smrg/* define unique representation types not found in &lt;X11/StringDefs.h&gt; */
145994689c1Smrg
146994689c1Smrg#define XtRTemplateResource "TemplateResource"
147994689c1Smrg
148994689c1Smrgtypedef struct {
149994689c1Smrg	int empty;
150994689c1Smrg} TemplateClassPart;
151994689c1Smrg
152994689c1Smrgtypedef struct _TemplateClassRec {
153994689c1Smrg	CoreClassPart	core_class;
154994689c1Smrg	TemplateClassPart	template_class;
155994689c1Smrg} TemplateClassRec;
156994689c1Smrg
157994689c1Smrgextern TemplateClassRec templateClassRec;
158994689c1Smrg
159994689c1Smrgtypedef struct {
160994689c1Smrg	/* resources */
161994689c1Smrg	char* resource;
162994689c1Smrg	/* private state */
163994689c1Smrg} TemplatePart;
164994689c1Smrg
165994689c1Smrgtypedef struct _TemplateRec {
166994689c1Smrg	CorePart	core;
167994689c1Smrg	TemplatePart	template;
168994689c1Smrg} TemplateRec;
169994689c1Smrg
170994689c1Smrg#endif /* _TemplateP_h */
171994689c1Smrg<!-- .CE -->
172994689c1Smrg</literallayout>
173994689c1Smrg<para>
174994689c1SmrgThe private header file includes the private header file of its
175994689c1Smrgsuperclass, thereby exposing the entire internal structure of the widget.
176994689c1SmrgIt may not always be advantageous to do this; your own project
177994689c1Smrgdevelopment style will dictate the appropriate level of detail to expose
178994689c1Smrgin each module.
179994689c1Smrg</para>
180994689c1Smrg<para>
181994689c1SmrgThe "WindowWidget" needs to declare two fields in its instance structure to
182994689c1Smrghold the drawing colors, a resource field for the font and a field for the
183994689c1Smrgexpose and user input callback lists:
184994689c1Smrg</para>
185994689c1Smrg
186994689c1Smrg<literallayout class="monospaced">
187994689c1Smrgtypedef struct {
188994689c1Smrg	/* resources */
189994689c1Smrg	Pixel color_1;
190994689c1Smrg	Pixel color_2;
191994689c1Smrg	XFontStruct* font;
192994689c1Smrg	XtCallbackList expose_callback;
193994689c1Smrg	XtCallbackList input_callback;
194994689c1Smrg	/* private state */
195994689c1Smrg	/* (none) */
196994689c1Smrg} WindowPart;
197994689c1Smrg</literallayout>
198994689c1Smrg
199994689c1Smrg</sect2>
200994689c1Smrg<sect2 id="Widget_Source_File">
201994689c1Smrg<title>Widget Source File</title>
202994689c1Smrg<para>
203994689c1Smrg<!-- .LP -->
204994689c1SmrgThe source code file implements the widget class itself.  The unique
205994689c1Smrgpart of this file is the declaration and initialization of the
206994689c1Smrgwidget class record structure and the declaration of all resources and
207994689c1Smrgaction routines added by the widget class.
208994689c1Smrg</para>
209994689c1Smrg<para>
210994689c1Smrg<!-- .LP -->
211994689c1SmrgThe contents of the Template implementation file,
2125ec34c4cSmrg<filename class="headerfile">&lt;X11/Xaw/Template.c&gt;</filename>,
213994689c1Smrgare:
214994689c1Smrg</para>
215994689c1Smrg<!-- .CB -->
216994689c1Smrg<!-- .\".so ../../lib/Xaw/Template.c -->
217994689c1Smrg<literallayout class="monospaced">
218994689c1Smrg/* Copyright (c) X Consortium 1987, 1988
219994689c1Smrg */
220994689c1Smrg
221994689c1Smrg#include &lt;X11/IntrinsicP.h&gt;
222994689c1Smrg#include &lt;X11/StringDefs.h&gt;
223994689c1Smrg#include "TemplateP.h"
224994689c1Smrg
225994689c1Smrgstatic XtResource resources[] = {
226994689c1Smrg#define offset(field) XtOffsetOf(TemplateRec, template.field)
227994689c1Smrg	/* {name, class, type, size, offset, default_type, default_addr}, */
228994689c1Smrg    { XtNtemplateResource, XtCTemplateResource, XtRTemplateResource,
229994689c1Smrg	  sizeof(char*), offset(resource), XtRString, (XtPointer) "default" },
230994689c1Smrg#undef offset
231994689c1Smrg};
232994689c1Smrg
233994689c1Smrgstatic void TemplateAction(/* Widget, XEvent*, String*, Cardinal* */);
234994689c1Smrg
235994689c1Smrgstatic XtActionsRec actions[] =
236994689c1Smrg{
237994689c1Smrg	/* {name,	procedure}, */
238994689c1Smrg	{"template",	TemplateAction},
239994689c1Smrg};
240994689c1Smrg
241994689c1Smrgstatic char translations[] =
2425ec34c4cSmrg"	&lt;Key&gt;:	template(&#x2006;) \\n\\
243994689c1Smrg";
244994689c1Smrg
245994689c1SmrgTemplateClassRec templateClassRec = {
246994689c1Smrg  { /* core fields */
2475ec34c4cSmrg        /* superclass           */      (WidgetClass) &amp;widgetClassRec,
2485ec34c4cSmrg        /* class_name           */      "Template",
2495ec34c4cSmrg        /* widget_size          */      sizeof(TemplateRec),
2505ec34c4cSmrg        /* class_initialize     */      NULL,
2515ec34c4cSmrg        /* class_part_initialize */     NULL,
2525ec34c4cSmrg        /* class_inited         */      FALSE,
2535ec34c4cSmrg        /* initialize           */      NULL,
2545ec34c4cSmrg        /* initialize_hook      */      NULL,
2555ec34c4cSmrg        /* realize              */      XtInheritRealize,
2565ec34c4cSmrg        /* actions              */      actions,
2575ec34c4cSmrg        /* num_actions          */      XtNumber(actions),
2585ec34c4cSmrg        /* resources            */      resources,
2595ec34c4cSmrg        /* num_resources        */      XtNumber(resources),
2605ec34c4cSmrg        /* xrm_class            */      NULLQUARK,
2615ec34c4cSmrg        /* compress_motion      */      TRUE,
2625ec34c4cSmrg        /* compress_exposure    */      TRUE,
2635ec34c4cSmrg        /* compress_enterleave  */      TRUE,
2645ec34c4cSmrg        /* visible_interest     */      FALSE,
2655ec34c4cSmrg        /* destroy              */      NULL,
2665ec34c4cSmrg        /* resize               */      NULL,
2675ec34c4cSmrg        /* expose               */      NULL,
2685ec34c4cSmrg        /* set_values           */      NULL,
2695ec34c4cSmrg        /* set_values_hook      */      NULL,
2705ec34c4cSmrg        /* set_values_almost    */      XtInheritSetValuesAlmost,
2715ec34c4cSmrg        /* get_values_hook      */      NULL,
2725ec34c4cSmrg        /* accept_focus         */      NULL,
2735ec34c4cSmrg        /* version              */      XtVersion,
2745ec34c4cSmrg        /* callback_private     */      NULL,
2755ec34c4cSmrg        /* tm_table             */      translations,
2765ec34c4cSmrg        /* query_geometry       */      XtInheritQueryGeometry,
2775ec34c4cSmrg        /* display_accelerator  */      XtInheritDisplayAccelerator,
2785ec34c4cSmrg        /* extension            */      NULL
279994689c1Smrg  },
280994689c1Smrg  { /* template fields */
2815ec34c4cSmrg        /* empty                */      0
282994689c1Smrg  }
283994689c1Smrg};
284994689c1Smrg
285994689c1SmrgWidgetClass templateWidgetClass = (WidgetClass)&amp;templateClassRec;
286994689c1Smrg</literallayout>
287994689c1Smrg<!-- .CE -->
288994689c1Smrg<para>
289994689c1SmrgThe resource list for the "WindowWidget" might look like the following:
290994689c1Smrg</para>
291994689c1Smrg<!-- .CB -->
292994689c1Smrg<literallayout class="monospaced">
293994689c1Smrgstatic XtResource resources[] = {
294994689c1Smrg#define offset(field) XtOffsetOf(WindowWidgetRec, window.field)
295994689c1Smrg	/* {name, class, type, size, offset, default_type, default_addr}, */
296994689c1Smrg	{ XtNdrawingColor1, XtCColor, XtRPixel, sizeof(Pixel),
297994689c1Smrg		  offset(color_1), XtRString, XtDefaultForeground },
298994689c1Smrg	{ XtNdrawingColor2, XtCColor, XtRPixel, sizeof(Pixel),
299994689c1Smrg		  offset(color_2), XtRString, XtDefaultForeground },
300994689c1Smrg	{ XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct*),
301994689c1Smrg		  offset(font), XtRString, XtDefaultFont },
302994689c1Smrg	{ XtNexposeCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
303994689c1Smrg		  offset(expose_callback), XtRCallback, NULL },
304994689c1Smrg	{ XtNcallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
305994689c1Smrg		  offset(input_callback), XtRCallback, NULL },
306994689c1Smrg#undef offset
307994689c1Smrg};
308994689c1Smrg</literallayout>
309994689c1Smrg<!-- .CE -->
310994689c1Smrg<para>
311994689c1Smrg<!-- .LP -->
312994689c1SmrgThe user input callback will be implemented by an action procedure which
313994689c1Smrgpasses the event pointer as call_data.  The action procedure
314994689c1Smrgis declared as:
315994689c1Smrg<!-- .CB -->
316994689c1Smrg</para>
317994689c1Smrg<literallayout class="monospaced">
318994689c1Smrg/* ARGSUSED */
319994689c1Smrgstatic void InputAction(w, event, params, num_params)
320994689c1Smrg	Widget w;
321994689c1Smrg	XEvent *event;
322994689c1Smrg	String *params;		/* unused */
323994689c1Smrg	Cardinal *num_params;	/* unused */
324994689c1Smrg{
325994689c1Smrg	XtCallCallbacks(w, XtNcallback, (XtPointer)event);
326994689c1Smrg}
327994689c1Smrg
328994689c1Smrgstatic XtActionsRec actions[] =
329994689c1Smrg{
330994689c1Smrg	/* {name,	procedure}, */
331994689c1Smrg	{"input",	InputAction},
332994689c1Smrg};
333994689c1Smrg</literallayout>
334994689c1Smrg<!-- .CE -->
335994689c1Smrg<para>
336994689c1Smrg<!-- .LP -->
337994689c1Smrgand the default input binding will be to execute the input callbacks on
338994689c1Smrg<function>KeyPress</function> and <function>ButtonPress : </function>
339994689c1Smrg</para>
340994689c1Smrg<literallayout class="monospaced">
341994689c1Smrgstatic char translations[] =
3425ec34c4cSmrg"	&lt;Key&gt;:	input(&#x2006;) \\n\\
3435ec34c4cSmrg 	&lt;BtnDown&gt;:	input(&#x2006;) \\
344994689c1Smrg";
345994689c1Smrg</literallayout>
346994689c1Smrg<!-- .CE -->
347994689c1Smrg<para>
348994689c1SmrgIn the class record declaration and initialization, the only field that
349994689c1Smrgis different from the Template is the expose procedure:
350994689c1Smrg</para>
351994689c1Smrg<!-- .CB -->
352994689c1Smrg<literallayout class="monospaced">
353994689c1Smrg/* ARGSUSED */
354994689c1Smrgstatic void Redisplay(w, event, region)
355994689c1Smrg	Widget w;
356994689c1Smrg	XEvent *event;	/* unused */
357994689c1Smrg	Region region;
358994689c1Smrg{
359994689c1Smrg	XtCallCallbacks(w, XtNexposeCallback, (XtPointer)region);
360994689c1Smrg}
361994689c1Smrg
362994689c1SmrgWindowClassRec windowClassRec = {
363994689c1Smrg
364994689c1Smrg	...
365994689c1Smrg
366994689c1Smrg	/* expose	*/	Redisplay,
367994689c1Smrg</literallayout>
368994689c1Smrg<!-- .CE -->
369994689c1Smrg<para>
370994689c1Smrg<!-- .LP -->
371994689c1Smrg<!-- .sp -->
372994689c1SmrgThe "WindowWidget" will also declare three public procedures to return the
373994689c1Smrgdrawing colors and the font id, saving the application the effort of
374994689c1Smrgconstructing an argument list for a call to
375994689c1Smrg<function>XtGetValues :</function>
376994689c1Smrg</para>
377994689c1Smrg<!-- .LP -->
378994689c1Smrg<!-- .CB -->
379994689c1Smrg<literallayout class="monospaced">
380994689c1SmrgPixel WindowColor1(w)
381994689c1Smrg        Widget w;
382994689c1Smrg{
383994689c1Smrg        return ((WindowWidget)w)->window.color_1;
384994689c1Smrg}
385994689c1Smrg
386994689c1SmrgPixel WindowColor2(w)
387994689c1Smrg        Widget w;
388994689c1Smrg{
389994689c1Smrg        return ((WindowWidget)w)->window.color_2;
390994689c1Smrg}
391994689c1Smrg
392994689c1SmrgFont WindowFont(w)
393994689c1Smrg        Widget w;
394994689c1Smrg{
395994689c1Smrg        return ((WindowWidget)w)->window.font->fid;
396994689c1Smrg}
397994689c1Smrg</literallayout>
398994689c1Smrg
399994689c1Smrg<para>
400994689c1SmrgThe "WindowWidget" is now complete.  The application can retrieve the two
401994689c1Smrgdrawing colors from the widget instance by calling either
4025ec34c4cSmrg<function>XtGetValues</function>,
403994689c1Smrgor the <function>WindowColor</function> functions.  The actual window created for the
404994689c1Smrg"WindowWidget" is available by calling the
405994689c1Smrg<function>XtWindow</function> function.
406994689c1Smrg</para>
407994689c1Smrg</sect2>
408