File: sec-objectdata.html

package info (click to toggle)
ebook-dev-ggad 199908-5
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k, sarge
  • size: 2,264 kB
  • ctags: 1,163
  • sloc: sh: 44; makefile: 35
file content (240 lines) | stat: -rw-r--r-- 9,013 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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>
      Attaching Data to Objects
    </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="Object Finalization" href= 
    "sec-finalization.html">
    <link rel="NEXT" title="GDK Basics" href="cha-gdk.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="sec-finalization.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="cha-gdk.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-OBJECTDATA">Attaching Data to Objects</a>
      </h1>
      <p>
        You can "attach" arbitrary string-pointer pairs to a <span
        class="STRUCTNAME">GtkObject</span> instance, in effect
        adding a new data member. GTK+ uses this some internally,
        but it can also be a convenient way to pass data around in
        your application. In particular, it's a nice way to pass
        information to callbacks.
      </p>
      <p>
        Here's a simple example:
      </p>
      <table border="0" bgcolor="#E0E0E0" width="100%">
        <tr>
          <td>
<pre class="PROGRAMLISTING">
&#13;    GtkWidget* button = gtk_button_new();
    GtkWidget* label  = gtk_label_new(_("Foo"));
  
    gtk_object_set_data(GTK_OBJECT(button), "my_label_key", label); 
  
</pre>
          </td>
        </tr>
      </table>
      <p>
        Later, when you have a pointer to the button but not the
        label (perhaps in a callback connected to the button's
        <span class="SYMBOL">"clicked"</span> signal), you can do
        this:
      </p>
      <table border="0" bgcolor="#E0E0E0" width="100%">
        <tr>
          <td>
<pre class="PROGRAMLISTING">
&#13;    GtkWidget* label = gtk_object_get_data(GTK_OBJECT(button),
                                           "my_label_key");

    /* If no data is found for the key, NULL is returned. */

    if (label == NULL)
      {
        g_warning("No data was associated with 'my_label_key'!");
      }
  
</pre>
          </td>
        </tr>
      </table>
      <p>
        A pair of convenience functions use a predetermined key and
        thus save typing (and remembering) the object data key.
        These are <tt class="FUNCTION">
        gtk_object_set_user_data()</tt> and <tt class="FUNCTION">
        gtk_object_get_user_data()</tt>. You can also register a
        function to free the data when the data is removed or
        replaced, or the <span class="STRUCTNAME">GtkObject</span>
        is destroyed; This function should be of type <span class= 
        "STRUCTNAME">GtkDestroyNotify</span>:
      </p>
      <table border="0" bgcolor="#E0E0E0" width="100%">
        <tr>
          <td>
<pre class="PROGRAMLISTING">
&#13;  typedef void (*GtkDestroyNotify) (gpointer data);&#13;
</pre>
          </td>
        </tr>
      </table>
      <p>
        Conveniently, <tt class="FUNCTION">g_free()</tt> and <tt
        class="FUNCTION">gtk_object_unref()</tt> will work here.
        You register a "destroy notification" function when you set
        the data, using <tt class="FUNCTION">
        gtk_object_set_data_full()</tt>. You can remove data before
        the object is destroyed with <tt class="FUNCTION">
        gtk_object_remove_data()</tt>, or remove it without calling
        the destroy function with <tt class="FUNCTION">
        gtk_object_remove_no_notify()</tt>. Setting the data to
        <span class="STRUCTNAME">NULL</span> is equivalent to
        removing it with <tt class="FUNCTION">
        gtk_object_remove_data()</tt>, and will also call the
        destroy function if you registered one. <a href= 
        "sec-objectdata.html#FL-OBJECTDATA">Figure 6</a> summarizes
        the object data functions.
      </p>
      <p>
        It's worth pointing out that the object data system is a
        thin wrapper around the <span class="STRUCTNAME">
        GData</span> facility in glib, which can be used
        standalone.
      </p>
      <div class="FIGURE">
        <a name="FL-OBJECTDATA"></a>
        <div class="FUNCSYNOPSIS">
          <a name="FL-OBJECTDATA.SYNOPSIS"></a>
          <table border="0" bgcolor="#E0E0E0" width="100%">
            <tr>
              <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;gtk/gtkobject.h&gt;
</pre>
              </td>
            </tr>
          </table>
          <p>
            <code><code class="FUNCDEF">void <tt class="FUNCTION">
            gtk_object_set_data</tt></code>(GtkObject* <tt class= 
            "PARAMETER"><i>object</i></tt>, const gchar* <tt class= 
            "PARAMETER"><i>key</i></tt>, gpointer <tt class= 
            "PARAMETER"><i>data</i></tt>);</code>
          </p>
          <p>
            <code><code class="FUNCDEF">void <tt class="FUNCTION">
            gtk_object_set_data_full</tt></code>(GtkObject* <tt
            class="PARAMETER"><i>object</i></tt>, const gchar* <tt
            class="PARAMETER"><i>key</i></tt>, gpointer <tt class= 
            "PARAMETER"><i>data</i></tt>, GtkDestroyNotify <tt
            class="PARAMETER"><i>destroy</i></tt>);</code>
          </p>
          <p>
            <code><code class="FUNCDEF">void <tt class="FUNCTION">
            gtk_object_remove_data</tt></code>(GtkObject* <tt
            class="PARAMETER"><i>object</i></tt>, const gchar* <tt
            class="PARAMETER"><i>key</i></tt>);</code>
          </p>
          <p>
            <code><code class="FUNCDEF">gpointer <tt class=
            "FUNCTION">gtk_object_get_data</tt></code>(GtkObject*
            <tt class="PARAMETER"><i>object</i></tt>, const gchar*
            <tt class="PARAMETER"><i>key</i></tt>);</code>
          </p>
          <p>
            <code><code class="FUNCDEF">void <tt class="FUNCTION">
            gtk_object_remove_no_notify</tt></code>(GtkObject* <tt
            class="PARAMETER"><i>object</i></tt>, const gchar* <tt
            class="PARAMETER"><i>key</i></tt>);</code>
          </p>
          <p>
            <code><code class="FUNCDEF">void <tt class="FUNCTION">
            gtk_object_set_user_data</tt></code>(GtkObject* <tt
            class="PARAMETER"><i>object</i></tt>, gpointer <tt
            class="PARAMETER"><i>data</i></tt>);</code>
          </p>
          <p>
            <code><code class="FUNCDEF">gpointer <tt class=
            "FUNCTION">
            gtk_object_get_user_data</tt></code>(GtkObject* <tt
            class="PARAMETER"><i>object</i></tt>);</code>
          </p>
        </div>
        <p>
          <b>Figure 6. Attaching key-value pairs to a <span class= 
          "STRUCTNAME">GtkObject</span></b>
        </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="sec-finalization.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="cha-gdk.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>Object
            Finalization</b></font>
          </td>
          <td colspan="2" align="right">
            <font color="#000000" size="2"><b>GDK Basics</b></font>
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>