CH7.xml revision 994689c1
1<chapter id="creating_new_widgets__subclassing_"> 2<title>Creating New Widgets (Subclassing)</title> 3<para> 4Although the task of creating a new widget may at first appear a little 5daunting, there is a basic simple pattern that all widgets follow. The 6Athena Widget library contains a special widget called the 7<emphasis remap='I'>Template</emphasis> widget that is intended to assist 8the novice widget programmer in writing a custom widget. 9</para> 10<para> 11Reasons for wishing to write a custom widget include: 12</para> 13<itemizedlist> 14 <listitem> 15 <para> 16Providing a graphical interface not currently supported by any existing 17widget set. 18 </para> 19 </listitem> 20 <listitem> 21 <para> 22Convenient access to resource management procedures to obtain fonts, 23colors, etc., even if user customization is not desired. 24 </para> 25 </listitem> 26 <listitem> 27 <para> 28Convenient access to user input dispatch and translation management procedures. 29 </para> 30 </listitem> 31 <listitem> 32 <para> 33Access to callback mechanism for building higher-level application libraries. 34 </para> 35 </listitem> 36 <listitem> 37 <para> 38Customizing the interface or behavior of an existing widget to suit a 39special application need. 40 </para> 41 </listitem> 42 <listitem> 43 <para> 44Desire to allow user customization of resources such as fonts, colors, 45etc., or to allow convenient re-binding of keys and buttons to internal 46functions. 47 </para> 48 </listitem> 49 <listitem> 50 <para> 51Converting a non-Toolkit application to use the Toolkit. 52 </para> 53 </listitem> 54</itemizedlist> 55 56<para> 57In each of these cases, the operation needed to create a new widget is 58to "subclass" an existing one. If the desired semantics of the new 59widget are similar to an existing one, then the implementation of the 60existing widget should be examined to see how much work would be 61required to create a subclass that will then be 62able to share the existing class methods. Much time will be saved in 63writing the new widget if an existing widget class Expose, Resize and/or 64GeometryManager method can be used by the subclass. 65</para> 66<para> 67Note that some trivial uses of a ``bare-bones'' widget may be achieved by 68simply creating an instance of the Core 69widget. The class variable to use when creating a Core widget is 70<function>widgetClass</function>. 71The geometry of the Core widget is determined entirely by the parent 72widget. 73</para> 74<para> 75It is very often the case than an application will have a special need 76for a certain set of functions and that many copies of these functions 77will be needed. For example, when converting an older application to use 78the Toolkit, it may be desirable to have a "Window Widget" class that 79might have the following semantics: 80</para> 81<itemizedlist> 82 <listitem> 83 <para> 84Allocate 2 drawing colors in addition to a background color. 85 </para> 86 </listitem> 87 <listitem> 88 <para> 89Allocate a text font. 90 </para> 91 </listitem> 92 <listitem> 93 <para> 94Execute an application-supplied function to handle exposure events. 95 </para> 96 </listitem> 97 <listitem> 98 <para> 99Execute an application-supplied function to handle user input events. 100 </para> 101 </listitem> 102</itemizedlist> 103<para> 104It is obvious that a completely general-purpose WindowWidgetClass could 105be constructed that would export all class methods as callbacks lists, 106but such a widget would be very large and would have to choose some 107arbitrary number of resources such as colors to allocate. An application 108that used many instances of the general-purpose widget would therefore 109un-necessarily waste many resources. 110</para> 111<para> 112In this section, an outline will be given of the procedure to follow to 113construct a special-purpose widget to address the items listed above. 114The reader should refer to the appropriate sections of the 115<emphasis remap='I'>X Toolkit Intrinsics - C Language Interface</emphasis> 116for complete details of the material outlined here. Section 1.4 of 117the <emphasis remap='I'>Intrinsics</emphasis> should be read in 118conjunction with this section. 119</para> 120<para> 121All Athena widgets have three separate files associated with them: 122</para> 123<itemizedlist> 124 <listitem> 125 <para> 126A "public" header file containing declarations needed by applications programmers 127 </para> 128 </listitem> 129 <listitem> 130 <para> 131A "private" header file containing additional declarations needed by the 132widget and any subclasses 133 </para> 134 </listitem> 135 <listitem> 136 <para> 137A source code file containing the implementation of the widget 138 </para> 139 </listitem> 140</itemizedlist> 141 142<para> 143This separation of functions into three files is suggested for all 144widgets, but nothing in the Toolkit actually requires this format. In 145particular, a private widget created for a single application may easily 146combine the "public" and "private" header files into a single file, or 147merge the contents into another application header file. Similarly, the 148widget implementation can be merged into other application code. 149</para> 150 151<para> 152In the following example, the public header file 153<function>< X11/Xaw/Template.h ></function>, 154the private header file 155<function>< X11/Xaw/TemplateP.h ></function> 156and the source code file 157<function>< X11/Xaw/Template.c ></function> 158will be modified to produce the "WindowWidget" described above. 159In each case, the files have been designed so that a global string 160replacement of "Template" and "template" 161with the name of your new widget, using 162the appropriate case, can be done. 163</para> 164<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="Template_public_header_file.xml"/> 165<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="Template_private_header_file.xml"/> 166<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="Template_widget_source_file.xml"/> 167</chapter> 168