1421c997bSmrg<chapter id='Creating_New_Widgets_Subclassing'> 2994689c1Smrg<title>Creating New Widgets (Subclassing)</title> 3994689c1Smrg<para> 4994689c1SmrgAlthough the task of creating a new widget may at first appear a little 5994689c1Smrgdaunting, there is a basic simple pattern that all widgets follow. The 6994689c1SmrgAthena Widget library contains a special widget called the 7421c997bSmrg<emphasis remap='I'>Template</emphasis> widget that is intended to assist 8994689c1Smrgthe novice widget programmer in writing a custom widget. 9994689c1Smrg</para> 10994689c1Smrg<para> 11994689c1SmrgReasons for wishing to write a custom widget include: 12994689c1Smrg</para> 13994689c1Smrg<itemizedlist> 14994689c1Smrg <listitem> 15994689c1Smrg <para> 16994689c1SmrgProviding a graphical interface not currently supported by any existing 17994689c1Smrgwidget set. 18994689c1Smrg </para> 19994689c1Smrg </listitem> 20994689c1Smrg <listitem> 21994689c1Smrg <para> 22994689c1SmrgConvenient access to resource management procedures to obtain fonts, 23994689c1Smrgcolors, etc., even if user customization is not desired. 24994689c1Smrg </para> 25994689c1Smrg </listitem> 26994689c1Smrg <listitem> 27994689c1Smrg <para> 28994689c1SmrgConvenient access to user input dispatch and translation management procedures. 29994689c1Smrg </para> 30994689c1Smrg </listitem> 31994689c1Smrg <listitem> 32994689c1Smrg <para> 33994689c1SmrgAccess to callback mechanism for building higher-level application libraries. 34994689c1Smrg </para> 35994689c1Smrg </listitem> 36994689c1Smrg <listitem> 37994689c1Smrg <para> 38994689c1SmrgCustomizing the interface or behavior of an existing widget to suit a 39994689c1Smrgspecial application need. 40994689c1Smrg </para> 41994689c1Smrg </listitem> 42994689c1Smrg <listitem> 43994689c1Smrg <para> 44994689c1SmrgDesire to allow user customization of resources such as fonts, colors, 45994689c1Smrgetc., or to allow convenient re-binding of keys and buttons to internal 46994689c1Smrgfunctions. 47994689c1Smrg </para> 48994689c1Smrg </listitem> 49994689c1Smrg <listitem> 50994689c1Smrg <para> 51994689c1SmrgConverting a non-Toolkit application to use the Toolkit. 52994689c1Smrg </para> 53994689c1Smrg </listitem> 54994689c1Smrg</itemizedlist> 55994689c1Smrg 56994689c1Smrg<para> 57994689c1SmrgIn each of these cases, the operation needed to create a new widget is 58994689c1Smrgto "subclass" an existing one. If the desired semantics of the new 59994689c1Smrgwidget are similar to an existing one, then the implementation of the 60994689c1Smrgexisting widget should be examined to see how much work would be 61994689c1Smrgrequired to create a subclass that will then be 62994689c1Smrgable to share the existing class methods. Much time will be saved in 63994689c1Smrgwriting the new widget if an existing widget class Expose, Resize and/or 64994689c1SmrgGeometryManager method can be used by the subclass. 65994689c1Smrg</para> 66994689c1Smrg<para> 675ec34c4cSmrgNote that some trivial uses of a <quote>bare-bones</quote> widget may be achieved by 68994689c1Smrgsimply creating an instance of the Core 69994689c1Smrgwidget. The class variable to use when creating a Core widget is 70994689c1Smrg<function>widgetClass</function>. 71994689c1SmrgThe geometry of the Core widget is determined entirely by the parent 72994689c1Smrgwidget. 73994689c1Smrg</para> 74994689c1Smrg<para> 75994689c1SmrgIt is very often the case than an application will have a special need 76994689c1Smrgfor a certain set of functions and that many copies of these functions 77994689c1Smrgwill be needed. For example, when converting an older application to use 78994689c1Smrgthe Toolkit, it may be desirable to have a "Window Widget" class that 79994689c1Smrgmight have the following semantics: 80994689c1Smrg</para> 81994689c1Smrg<itemizedlist> 82994689c1Smrg <listitem> 83994689c1Smrg <para> 84994689c1SmrgAllocate 2 drawing colors in addition to a background color. 85994689c1Smrg </para> 86994689c1Smrg </listitem> 87994689c1Smrg <listitem> 88994689c1Smrg <para> 89994689c1SmrgAllocate a text font. 90994689c1Smrg </para> 91994689c1Smrg </listitem> 92994689c1Smrg <listitem> 93994689c1Smrg <para> 94994689c1SmrgExecute an application-supplied function to handle exposure events. 95994689c1Smrg </para> 96994689c1Smrg </listitem> 97994689c1Smrg <listitem> 98994689c1Smrg <para> 99994689c1SmrgExecute an application-supplied function to handle user input events. 100994689c1Smrg </para> 101994689c1Smrg </listitem> 102994689c1Smrg</itemizedlist> 103994689c1Smrg<para> 104994689c1SmrgIt is obvious that a completely general-purpose WindowWidgetClass could 105994689c1Smrgbe constructed that would export all class methods as callbacks lists, 106994689c1Smrgbut such a widget would be very large and would have to choose some 107994689c1Smrgarbitrary number of resources such as colors to allocate. An application 108994689c1Smrgthat used many instances of the general-purpose widget would therefore 109994689c1Smrgun-necessarily waste many resources. 110994689c1Smrg</para> 111994689c1Smrg<para> 112994689c1SmrgIn this section, an outline will be given of the procedure to follow to 113994689c1Smrgconstruct a special-purpose widget to address the items listed above. 114421c997bSmrgThe reader should refer to the appropriate sections of the 1155ec34c4cSmrg<olink targetdoc='intrinsics' targetptr='intrinsics' 1165ec34c4cSmrg><citetitle>X Toolkit Intrinsics - C Language Interface</citetitle></olink> 1175ec34c4cSmrgfor complete details of the material outlined here. 1185ec34c4cSmrg<olink targetdoc='intrinsics' targetptr='Widgets' 1195ec34c4cSmrg>Section 1.4 of the <citetitle>Intrinsics</citetitle></olink> 1205ec34c4cSmrgshould be read in 121994689c1Smrgconjunction with this section. 122994689c1Smrg</para> 123994689c1Smrg<para> 124994689c1SmrgAll Athena widgets have three separate files associated with them: 125994689c1Smrg</para> 126994689c1Smrg<itemizedlist> 127994689c1Smrg <listitem> 128994689c1Smrg <para> 129994689c1SmrgA "public" header file containing declarations needed by applications programmers 130994689c1Smrg </para> 131994689c1Smrg </listitem> 132994689c1Smrg <listitem> 133994689c1Smrg <para> 134994689c1SmrgA "private" header file containing additional declarations needed by the 135994689c1Smrgwidget and any subclasses 136994689c1Smrg </para> 137994689c1Smrg </listitem> 138994689c1Smrg <listitem> 139994689c1Smrg <para> 140994689c1SmrgA source code file containing the implementation of the widget 141994689c1Smrg </para> 142994689c1Smrg </listitem> 143994689c1Smrg</itemizedlist> 144994689c1Smrg 145994689c1Smrg<para> 146994689c1SmrgThis separation of functions into three files is suggested for all 147994689c1Smrgwidgets, but nothing in the Toolkit actually requires this format. In 148994689c1Smrgparticular, a private widget created for a single application may easily 149994689c1Smrgcombine the "public" and "private" header files into a single file, or 150994689c1Smrgmerge the contents into another application header file. Similarly, the 151994689c1Smrgwidget implementation can be merged into other application code. 152994689c1Smrg</para> 153994689c1Smrg 154994689c1Smrg<para> 155994689c1SmrgIn the following example, the public header file 1565ec34c4cSmrg<filename class="headerfile"><X11/Xaw/Template.h></filename>, 157994689c1Smrgthe private header file 1585ec34c4cSmrg<filename class="headerfile"><X11/Xaw/TemplateP.h></filename> 159994689c1Smrgand the source code file 1605ec34c4cSmrg<filename class="headerfile"><X11/Xaw/Template.c></filename> 161994689c1Smrgwill be modified to produce the "WindowWidget" described above. 162994689c1SmrgIn each case, the files have been designed so that a global string 163994689c1Smrgreplacement of "Template" and "template" 164994689c1Smrgwith the name of your new widget, using 165994689c1Smrgthe appropriate case, can be done. 166994689c1Smrg</para> 167994689c1Smrg<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="Template_public_header_file.xml"/> 168994689c1Smrg<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="Template_private_header_file.xml"/> 169994689c1Smrg<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="Template_widget_source_file.xml"/> 170994689c1Smrg</chapter> 171