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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
The GTK+ Object and Type System
</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="Advanced GTK+/Gnome Techniques" href=
"advanced.html">
<link rel="PREVIOUS" title="Advanced GTK+/Gnome Techniques"
href="advanced.html">
<link rel="NEXT" title="Type Checking and New Types" href=
"z105.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="advanced.html"><font color="#0000ff" size="2">
<b><<< 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="z105.html"><font color="#0000ff" size="2"><b>
Next >>></b></font></a>
</td>
</tr>
</table>
</div>
<div class="CHAPTER">
<h1>
<a name="CHA-OBJECTS">The GTK+ Object and Type System</a>
</h1>
<div class="TOC">
<dl>
<dt>
<b>Table of Contents</b>
</dt>
<dt>
<a href="cha-objects.html#SEC-OBJECTSTRUCTS">Object and
Class Structures</a>
</dt>
<dt>
<a href="z105.html">Type Checking and New Types</a>
</dt>
<dt>
<a href="sec-classinit.html">Initializing a New
Class</a>
</dt>
<dt>
<a href="sec-gtkarg.html"><span class="STRUCTNAME">
GtkArg</span> and the Type System</a>
</dt>
<dt>
<a href="hc-objectargs.html">Object Arguments</a>
</dt>
<dt>
<a href="z109.html">Signals</a>
</dt>
<dt>
<a href="sec-finalization.html">Object Finalization</a>
</dt>
<dt>
<a href="sec-objectdata.html">Attaching Data to
Objects</a>
</dt>
</dl>
</div>
<p>
People often ask why GTK+ was written in C rather than an
object-oriented language. The answer is that C is more
portable and standardly available than any other language.
However, although C lacks syntactic sugar for
object-oriented programming, it in no way prohibits an
object-oriented approach.
</p>
<p>
GTK+ implements its own custom object system, which offers
standard object-oriented features such as inheritance and
virtual functions. In the tradition of languages such as
Lisp, Smalltalk, and Java, the GTK+ object system is more
runtime-centric than that of C++, allowing interpreted
language bindings and GUI builders to interact with it in
powerful ways.
</p>
<p>
You may recall from <a href="cha-gtk.html">the chapter
called <i>GTK+ Basics</i></a> that widgets are a special
type of <span class="STRUCTNAME">GtkObject</span>; any
object with <tt class="CLASSNAME">GtkWidget</tt> in its
ancestry is a widget. Widgets represent a region on the
screen---most of them are user interface elements, such as
buttons or menus. There's nothing GUI-specific about <span
class="STRUCTNAME">GtkObject</span>; the object system can
be used in non-graphical programs.
</p>
<p>
This chapter dives right into the details of GTK+'s object
system, giving you an idea what's happening "behind the
scenes" in any GTK+ program. Sooner or later you'll need
this information: to write your own objects, debug existing
objects, or just understand GTK+ code on a conceptual
level.
</p>
<div class="SECT1">
<h1 class="SECT1">
<a name="SEC-OBJECTSTRUCTS">Object and Class
Structures</a>
</h1>
<p>
Each <span class="STRUCTNAME">GtkObject</span> has two
essential components: a struct representing an <i class=
"EMPHASIS">instance</i> of the object, and a struct
representing the <i class="EMPHASIS">class</i>. In
general, the instance struct contains the data members
for each instance, and the class struct contains class
function pointers (which can be overridden by
subclasses). The class struct can also contain class data
members---however, it's more typical to use static
variables in the <span class="STRUCTNAME">.c</span> file
implementing the object. If you're familiar with C++, the
class struct is equivalent to a vtable, only the class
struct is written by hand. It stores virtual functions
for an object type.
</p>
<p>
Here are the structs used in <tt class="CLASSNAME">
GtkButton</tt>:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
typedef struct _GtkButton GtkButton;
typedef struct _GtkButtonClass GtkButtonClass;
struct _GtkButton
{
GtkBin bin;
GtkWidget *child;
guint in_button : 1;
guint button_down : 1;
guint relief : 2;
};
struct _GtkButtonClass
{
GtkBinClass parent_class;
void (* pressed) (GtkButton *button);
void (* released) (GtkButton *button);
void (* clicked) (GtkButton *button);
void (* enter) (GtkButton *button);
void (* leave) (GtkButton *button);
};
</pre>
</td>
</tr>
</table>
<p>
Notice that the first member of <span class="STRUCTNAME">
struct _GtkButton</span> is <tt class="CLASSNAME">
GtkBin</tt>---that's because <tt class="CLASSNAME">
GtkButton</tt> is a subclass of <tt class="CLASSNAME">
GtkBin</tt>. (<tt class="CLASSNAME">GtkBin</tt> is a <tt
class="CLASSNAME">GtkContainer</tt> that can hold one
child.) Since <tt class="CLASSNAME">GtkBin</tt> is the
first member, we can safely cast a <tt class="CLASSNAME">
GtkButton</tt> to <tt class="CLASSNAME">GtkBin</tt>. In
<span class="STRUCTNAME">struct _GtkButtonClass</span>,
the same principle applies, and <span class="STRUCTNAME">
GtkBinClass</span> is the first member.
</p>
</div>
</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="advanced.html"><font color="#0000ff" size="2">
<b><<< 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="z105.html"><font color="#0000ff" size="2"><b>
Next >>></b></font></a>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<font color="#000000" size="2"><b>Advanced GTK+/Gnome
Techniques</b></font>
</td>
<td colspan="2" align="right">
<font color="#000000" size="2"><b>Type Checking and New
Types</b></font>
</td>
</tr>
</table>
</div>
</body>
</html>
|