File: sec-classinit.html

package info (click to toggle)
ebook-dev-ggad 199908-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,264 kB
  • ctags: 1,163
  • sloc: sh: 44; makefile: 35
file content (321 lines) | stat: -rw-r--r-- 11,845 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>
      Initializing a New Class
    </title>
    <meta name="GENERATOR" content=
    "Modular DocBook HTML Stylesheet Version 1.45">
    <link rel="HOME" title="GTK+ / Gnome Application Development"
    href="ggad.html">
    <link rel="UP" title="The GTK+ Object and Type System" href= 
    "cha-objects.html">
    <link rel="PREVIOUS" title="Type Checking and New Types" href= 
    "z105.html">
    <link rel="NEXT" title="GtkArg and the Type System" href= 
    "sec-gtkarg.html">
  </head>
  <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= 
  "#840084" alink="#0000FF">
    <div class="NAVHEADER">
      <table width="100%" border="0" bgcolor="#ffffff" cellpadding= 
      "1" cellspacing="0">
        <tr>
          <th colspan="4" align="center">
            <font color="#000000" size="2">GTK+ / Gnome Application
            Development</font>
          </th>
        </tr>
        <tr>
          <td width="25%" bgcolor="#ffffff" align="left">
            <a href="z105.html"><font color="#0000ff" size="2"><b>
            &lt;&lt;&lt; Previous</b></font></a>
          </td>
          <td width="25%" colspan="2" bgcolor="#ffffff" align= 
          "center">
            <font color="#0000ff" size="2"><b><a href="ggad.html">
            <font color="#0000ff" size="2"><b>
            Home</b></font></a></b></font>
          </td>
          <td width="25%" bgcolor="#ffffff" align="right">
            <a href="sec-gtkarg.html"><font color="#0000ff" size=
            "2"><b>Next &gt;&gt;&gt;</b></font></a>
          </td>
        </tr>
      </table>
    </div>
    <div class="SECT1">
      <h1 class="SECT1">
        <a name="SEC-CLASSINIT">Initializing a New Class</a>
      </h1>
      <p>
        When a type is first used, GTK+ creates an instance of its
        class struct (using the information supplied to <tt class= 
        "FUNCTION">gtk_type_unique()</tt>). To initialize the class
        struct for a type, GTK+ first checks that all parent
        classes are initialized and initializes them if not. Then
        it fills the top portion of the class struct with a
        byte-for-byte copy of the parent's class struct. This means
        the subclass inherits any function pointers found in the
        parent class.
      </p>
      <p>
        Next, the base class initialization functions of each
        parent class and that of the class itself are called in
        order, starting with <span class="STRUCTNAME">
        GtkObject</span>. (The base class init function is the last
        argument to <tt class="FUNCTION">gtk_type_unique()</tt>). A
        base class initializer is optional; in the <tt class= 
        "CLASSNAME">GtkButton</tt> case, there is none. If present,
        the base class initializer supplements the byte-for-byte
        copy of the class struct; for example, some functions
        should not be inherited. To prevent class function
        inheritance, the base class initializer can zero certain
        function pointers. Normally you do not need a base class
        initializer.
      </p>
      <p>
        Finally, GTK+ calls the type's own class init function. The
        class init function can override functions from the parent
        class by replacing them in the class struct. It should also
        fill in any functions unique to the subclass, and register
        signals and object arguments (discussed later in the
        chapter).
      </p>
      <p>
        A concrete example should make the class creation process
        clear. The class hierarchy for <tt class="CLASSNAME">
        GtkButton</tt> is shown in <a href= 
        "sec-classinit.html#FIG-GTKBUTTON">Figure 1</a>. When the
        <tt class="CLASSNAME">GtkButton</tt> type is registered, an
        empty <tt class="CLASSNAME">GtkButtonClass</tt> is created.
        This class struct is initialized as follows:
      </p>
      <ol type="1">
        <li>
          <p>
            The class struct for <tt class="CLASSNAME">GtkBin</tt>,
            <tt class="CLASSNAME">GtkButton</tt>'s immediate
            parent, is copied into it. This means <tt class= 
            "CLASSNAME">GtkButton</tt> inherits class functions
            from <tt class="CLASSNAME">GtkBin</tt>.&#13;
          </p>
        </li>
        <li>
          <p>
            The base class initialization function for <span class= 
            "STRUCTNAME">GtkObject</span> is called on it. This
            zeroes some <span class="STRUCTNAME">GtkObject</span>
            class functions that should not be inherited.&#13;
          </p>
        </li>
        <li>
          <p>
            There is no base class initializer for <tt class= 
            "CLASSNAME">GtkWidget</tt>, or it would be called.
            &#13;
          </p>
        </li>
        <li>
          <p>
            The base class initializer for <tt class="CLASSNAME">
            GtkContainer</tt> is called. This zeroes some <span
            class="STRUCTNAME">GtkContainer</span> class functions
            that should not be inherited, and initializes a <span
            class="STRUCTNAME">GtkContainerClass</span> data
            member.&#13;
          </p>
        </li>
        <li>
          <p>
            There is no base class initializer for <tt class= 
            "CLASSNAME">GtkBin</tt>, or it would be called.&#13;
          </p>
        </li>
        <li>
          <p>
            There is no base class initializer for <tt class= 
            "CLASSNAME">GtkButton</tt>, or it would be called.&#13;
          </p>
        </li>
        <li>
          <p>
            The class initializer is called for <tt class=
            "CLASSNAME">GtkButton</tt>. This fills in the <span
            class="STRUCTNAME">GtkButtonClass</span> structure,
            registers signals, and registers object arguments.&#13;
          </p>
        </li>
      </ol>
      <p>
        When writing a new class, you only need to concern yourself
        with the final two steps---you should consider whether a
        base class initializer is needed, and supply it if so; you
        must supply a class initializer in all cases.
      </p>
      <div class="FIGURE">
        <a name="FIG-GTKBUTTON"></a>
        <p>
          <b>Figure 1. <tt class="CLASSNAME">GtkButton</tt>
          Ancestry</b>
        </p>
      </div>
      <table border="0" bgcolor="#E0E0E0" width="100%">
        <tr>
          <td>
<pre class="PROGRAMLISTING">
&#13;GtkObject
   |
GtkWidget
   | 
GtkContainer
   | 
GtkBin
   | 
GtkButton&#13;
</pre>
          </td>
        </tr>
      </table>
      <p>
        Here is the <tt class="CLASSNAME">GtkButton</tt> class
        initialization function, just to give you an initial sense
        of things; read on to learn what this code does.
      </p>
      <table border="0" bgcolor="#E0E0E0" width="100%">
        <tr>
          <td>
<pre class="PROGRAMLISTING">
&#13;static void
gtk_button_class_init (GtkButtonClass *klass)
{
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  object_class = (GtkObjectClass*) klass;
  widget_class = (GtkWidgetClass*) klass;
  container_class = (GtkContainerClass*) klass;

  parent_class = gtk_type_class (GTK_TYPE_BIN);

  gtk_object_add_arg_type ("GtkButton::label", 
                           GTK_TYPE_STRING, 
                           GTK_ARG_READWRITE, 
                           ARG_LABEL);
  gtk_object_add_arg_type ("GtkButton::relief", 
                           GTK_TYPE_RELIEF_STYLE, 
                           GTK_ARG_READWRITE, 
                           ARG_RELIEF);


  button_signals[PRESSED] =
    gtk_signal_new ("pressed",
                    GTK_RUN_FIRST,
                    object_class-&gt;type,
                    GTK_SIGNAL_OFFSET (GtkButtonClass, pressed),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE, 0);
  button_signals[RELEASED] =
    gtk_signal_new ("released",
                    GTK_RUN_FIRST,
                    object_class-&gt;type,
                    GTK_SIGNAL_OFFSET (GtkButtonClass, released),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE, 0);
  button_signals[CLICKED] =
    gtk_signal_new ("clicked",
                    GTK_RUN_FIRST | GTK_RUN_ACTION,
                    object_class-&gt;type,
                    GTK_SIGNAL_OFFSET (GtkButtonClass, clicked),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE, 0);
  button_signals[ENTER] =
    gtk_signal_new ("enter",
                    GTK_RUN_FIRST,
                    object_class-&gt;type,
                    GTK_SIGNAL_OFFSET (GtkButtonClass, enter),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE, 0);
  button_signals[LEAVE] =
    gtk_signal_new ("leave",
                    GTK_RUN_FIRST,
                    object_class-&gt;type,
                    GTK_SIGNAL_OFFSET (GtkButtonClass, leave),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE, 0);

  gtk_object_class_add_signals (object_class, button_signals, LAST_SIGNAL);


  object_class-&gt;set_arg = gtk_button_set_arg;
  object_class-&gt;get_arg = gtk_button_get_arg;

  widget_class-&gt;activate_signal = button_signals[CLICKED];
  widget_class-&gt;realize = gtk_button_realize;
  widget_class-&gt;draw = gtk_button_draw;
  widget_class-&gt;draw_focus = gtk_button_draw_focus;
  widget_class-&gt;draw_default = gtk_button_draw_default;
  widget_class-&gt;size_request = gtk_button_size_request;
  widget_class-&gt;size_allocate = gtk_button_size_allocate;
  widget_class-&gt;expose_event = gtk_button_expose;
  widget_class-&gt;button_press_event = gtk_button_button_press;
  widget_class-&gt;button_release_event = gtk_button_button_release;
  widget_class-&gt;enter_notify_event = gtk_button_enter_notify;
  widget_class-&gt;leave_notify_event = gtk_button_leave_notify;
  widget_class-&gt;focus_in_event = gtk_button_focus_in;
  widget_class-&gt;focus_out_event = gtk_button_focus_out;

  container_class-&gt;add = gtk_button_add;
  container_class-&gt;remove = gtk_button_remove;
  container_class-&gt;child_type = gtk_button_child_type;

  klass-&gt;pressed = gtk_real_button_pressed;
  klass-&gt;released = gtk_real_button_released;
  klass-&gt;clicked = NULL;
  klass-&gt;enter = gtk_real_button_enter;
  klass-&gt;leave = gtk_real_button_leave;
}
&#13;
</pre>
          </td>
        </tr>
      </table>
    </div>
    <div class="NAVFOOTER">
      <br>
      <br>
      <table width="100%" border="0" bgcolor="#ffffff" cellpadding= 
      "1" cellspacing="0">
        <tr>
          <td width="25%" bgcolor="#ffffff" align="left">
            <a href="z105.html"><font color="#0000ff" size="2"><b>
            &lt;&lt;&lt; Previous</b></font></a>
          </td>
          <td width="25%" colspan="2" bgcolor="#ffffff" align= 
          "center">
            <font color="#0000ff" size="2"><b><a href="ggad.html">
            <font color="#0000ff" size="2"><b>
            Home</b></font></a></b></font>
          </td>
          <td width="25%" bgcolor="#ffffff" align="right">
            <a href="sec-gtkarg.html"><font color="#0000ff" size=
            "2"><b>Next &gt;&gt;&gt;</b></font></a>
          </td>
        </tr>
        <tr>
          <td colspan="2" align="left">
            <font color="#000000" size="2"><b>Type Checking and New
            Types</b></font>
          </td>
          <td colspan="2" align="right">
            <font color="#000000" size="2"><b><span class= 
            "STRUCTNAME">GtkArg</span> and the Type
            System</b></font>
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>