]> Pileus Git - ~andy/gtk/blob - gtk/gtkobject.h
Changed LGPL address for FSF in all .h and .c files
[~andy/gtk] / gtk / gtkobject.h
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #ifndef __GTK_OBJECT_H__
20 #define __GTK_OBJECT_H__
21
22
23 #include <gtk/gtkenums.h>
24 #include <gtk/gtktypeutils.h>
25 #include <gtk/gtkdebug.h>
26
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31
32
33
34 /* The debugging versions of the casting macros make sure the cast is "ok"
35  *  before proceeding, but they are definately slower than their less
36  *  careful counterparts as they involve no less than 3 function calls.
37  */
38 #ifdef GTK_NO_CHECK_CASTS
39
40 #define GTK_CHECK_CAST(obj,cast_type,cast)         ((cast*) (obj))
41 #define GTK_CHECK_CLASS_CAST(klass,cast_type,cast) ((cast*) (klass))
42
43 #else /* !GTK_NO_CHECK_CASTS */
44
45 #define GTK_CHECK_CAST(obj,cast_type,cast) \
46   ((cast*) gtk_object_check_cast ((GtkObject*) (obj), (cast_type)))
47
48 #define GTK_CHECK_CLASS_CAST(klass,cast_type,cast) \
49   ((cast*) gtk_object_check_class_cast ((GtkObjectClass*) (klass), (cast_type)))
50
51 #endif /* GTK_NO_CHECK_CASTS */
52
53
54 /* Determines whether 'obj' is a type of 'otype'.
55  */
56 #define GTK_CHECK_TYPE(obj,otype)  (gtk_type_is_a (((GtkObject*) (obj))->klass->type, (otype)))
57
58
59 /* Macro for casting a pointer to a GtkObject pointer.
60  */
61 #define GTK_OBJECT(obj)                   GTK_CHECK_CAST ((obj), gtk_object_get_type (), GtkObject)
62
63 /* Macros for extracting various fields from GtkObject and GtkObjectClass.
64  */
65 #define GTK_OBJECT_CLASS(klass)           (GTK_CHECK_CLASS_CAST ((klass), gtk_object_get_type (), GtkObjectClass))
66 #define GTK_OBJECT_TYPE(obj)              (GTK_OBJECT (obj)->klass->type)
67 #define GTK_OBJECT_SIGNALS(obj)           (GTK_OBJECT (obj)->klass->signals)
68 #define GTK_OBJECT_NSIGNALS(obj)          (GTK_OBJECT (obj)->klass->nsignals)
69     
70 /* GtkObject only uses the first 4 bits of the flags field.
71  * Derived objects may use the remaining bits. Though this
72  * is a kinda nasty break up, it does make the size of
73  * derived objects smaller.
74  */
75 enum
76 {
77   GTK_DESTROYED         = 1 << 0,
78   GTK_FLOATING          = 1 << 1,
79   GTK_RESERVED_1        = 1 << 2,
80   GTK_RESERVED_2        = 1 << 3,
81   GTK_OBJECT_FLAG_LAST  = GTK_RESERVED_2
82 };
83   
84 /* GtkArg flag bits for gtk_object_add_arg_type
85  */
86 enum
87 {
88   GTK_ARG_READABLE      = 1 << 0,
89   GTK_ARG_WRITABLE      = 1 << 1,
90   GTK_ARG_CONSTRUCT     = 1 << 2
91 };
92 #define GTK_ARG_READWRITE       (GTK_ARG_READABLE | GTK_ARG_WRITABLE)
93
94
95 /* Macros for extracting the object_flags from GtkObject.
96  */
97 #define GTK_OBJECT_FLAGS(obj)             (GTK_OBJECT (obj)->flags)
98 #define GTK_OBJECT_DESTROYED(obj)         (GTK_OBJECT_FLAGS (obj) & GTK_DESTROYED)
99 #define GTK_OBJECT_FLOATING(obj)          (GTK_OBJECT_FLAGS (obj) & GTK_FLOATING)
100
101 /* Macros for setting and clearing bits in the object_flags field of GtkObject.
102  */
103 #define GTK_OBJECT_SET_FLAGS(obj,flag)    G_STMT_START{ (GTK_OBJECT_FLAGS (obj) |= (flag)); }G_STMT_END
104 #define GTK_OBJECT_UNSET_FLAGS(obj,flag)  G_STMT_START{ (GTK_OBJECT_FLAGS (obj) &= ~(flag)); }G_STMT_END
105
106 /* Macro for testing whether "obj" is of type GtkObject.
107  */
108 #define GTK_IS_OBJECT(obj)                (GTK_CHECK_TYPE ((obj), gtk_object_get_type ()))
109
110
111 typedef struct _GtkObjectClass  GtkObjectClass;
112
113
114 /* GtkObject is the base of the object hierarchy. It defines
115  *  the few basic items that all derived classes contain.
116  */
117 struct _GtkObject
118 {
119   /* A pointer to the objects class. This will actually point to
120    *  the derived objects class struct (which will be derived from
121    *  GtkObjectClass).
122    */
123   GtkObjectClass *klass;
124
125   /* 32 bits of flags. GtkObject only uses 4 of these bits and
126    *  GtkWidget uses the rest. This is done because structs are
127    *  aligned on 4 or 8 byte boundaries. If a new bitfield were
128    *  used in GtkWidget much space would be wasted.
129    */
130   guint32 flags;
131
132   /* reference count.
133    * refer to the file REFCOUNTING on this issue.
134    */
135   guint ref_count;
136
137   /* The list of signal handlers and other data
138    *  fields for this object.
139    */
140   gpointer object_data;
141 };
142
143 /* GtkObjectClass is the base of the class hierarchy. It defines
144  *  the basic necessities for the class mechanism to work. Namely,
145  *  the "type", "signals" and "nsignals" fields.
146  */
147 struct _GtkObjectClass
148 {
149   /* The type identifier for the objects class. There is
150    *  one unique identifier per class.
151    */
152   GtkType type;
153
154   /* The signals this object class handles. "signals" is an
155    *  array of signal ID's.
156    */
157   guint *signals;
158
159   /* The number of signals listed in "signals".
160    */
161   guint nsignals;
162
163   /* The number of arguments per class.
164    */
165   guint n_args;
166
167   /* The functions that will end an objects life time. In one way ore
168    *  another all three of them are defined for all objects. If an
169    *  object class overrides one of the methods in order to perform class
170    *  specific destruction then it must still invoke its superclass'
171    *  implementation of the method after it is finished with its
172    *  own cleanup. (See the destroy function for GtkWidget for
173    *  an example of how to do this).
174    */
175   void (* shutdown) (GtkObject *object);
176   void (* destroy) (GtkObject *object);
177
178   void (* finalize) (GtkObject *object);
179 };
180
181
182 /* For the purpose of user signals we need the signal function
183  * and signal marshaller signatures already in this place.
184  */
185 #define GTK_SIGNAL_FUNC(f)  ((GtkSignalFunc) f)
186
187 typedef void (*GtkSignalFunc)       (void);
188 typedef void (*GtkSignalMarshaller) (GtkObject      *object,
189                                      GtkSignalFunc   func,
190                                      gpointer        func_data,
191                                      GtkArg         *args);
192
193
194 /* Get the type identifier for GtkObject's.
195  */
196 guint   gtk_object_get_type             (void);
197
198 /* Append "signals" to those already defined in "class".
199  */
200 void    gtk_object_class_add_signals    (GtkObjectClass *klass,
201                                          guint          *signals,
202                                          guint           nsignals);
203
204 /* Append a user defined signal without default handler to a class.
205  */
206 guint   gtk_object_class_add_user_signal (GtkObjectClass     *klass,
207                                           const gchar        *name,
208                                           GtkSignalMarshaller marshaller,
209                                           GtkType             return_val,
210                                           guint               nparams,
211                                           ...);
212
213 GtkObject*      gtk_object_new          (GtkType        type,
214                                          ...);
215
216 GtkObject*      gtk_object_newv         (GtkType        type,
217                                          guint          nargs,
218                                          GtkArg         *args);
219 void gtk_object_sink      (GtkObject        *object);
220 void gtk_object_ref       (GtkObject        *object);
221 void gtk_object_unref     (GtkObject        *object);
222
223 void gtk_object_weakref   (GtkObject        *object,
224                            GtkDestroyNotify  notify,
225                            gpointer          data);
226 void gtk_object_weakunref (GtkObject        *object,
227                            GtkDestroyNotify  notify,
228                            gpointer          data);
229
230 void gtk_object_destroy   (GtkObject *object);
231
232 /* gtk_object_getv() sets an arguments type and value, or just
233  * its type to GTK_TYPE_INVALID.
234  * if arg->type == GTK_TYPE_STRING, it's the callers response to
235  * do a g_free (GTK_VALUE_STRING (arg));
236  */
237 void    gtk_object_getv         (GtkObject      *object,
238                                  guint          nargs,
239                                  GtkArg         *args);
240
241 /* gtk_object_set() takes a variable argument list of the form:
242  * (..., gchar *arg_name, ARG_VALUES, [repeatedly name/value pairs,] NULL)
243  * where ARG_VALUES type depend on the argument and can consist of
244  * more than one c-function argument.
245  */
246 void    gtk_object_set          (GtkObject      *object,
247                                  ...);
248
249 void    gtk_object_setv         (GtkObject      *object,
250                                  guint          nargs,
251                                  GtkArg         *args);
252
253 /* Allocate a GtkArg array of size nargs that hold the
254  * names and types of the args that can be used with
255  * gtk_object_set/gtk_object_get. if (arg_flags!=NULL),
256  * (*arg_flags) will be set to point to a newly allocated
257  * guint array that holds the flags of the args.
258  * It is the callers response to do a
259  * g_free (returned_args); g_free (*acess_masks).
260  */
261 GtkArg* gtk_object_query_args   (GtkType        class_type,
262                                  guint32        **arg_flags,
263                                  guint          *nargs);
264
265 void    gtk_object_add_arg_type (const gchar    *arg_name,
266                                  GtkType        arg_type,
267                                  guint          arg_flags,
268                                  guint          arg_id);
269
270 GtkType gtk_object_get_arg_type (const gchar    *arg_name);
271
272 /* Set 'data' to the "object_data" field of the object. The
273  *  data is indexed by the "key". If there is already data
274  *  associated with "key" then the new data will replace it.
275  *  If 'data' is NULL then this call is equivalent to
276  *  'gtk_object_remove_data'.
277  */
278 void gtk_object_set_data       (GtkObject   *object,
279                                 const gchar *key,
280                                 gpointer     data);
281
282 /* Like gtk_object_set_data, but takes an additional argument
283  * which is a function to be called when the data is removed.
284  */
285 void gtk_object_set_data_full       (GtkObject   *object,
286                                      const gchar *key,
287                                      gpointer     data,
288                                      GtkDestroyNotify destroy);
289
290 /* Get the data associated with "key".
291  */
292 gpointer gtk_object_get_data       (GtkObject   *object,
293                                     const gchar *key);
294
295 /* Remove the data associated with "key". This call is
296  *  equivalent to 'gtk_object_set_data' where 'data' is NULL.
297  */
298 void gtk_object_remove_data       (GtkObject   *object,
299                                    const gchar *key);
300
301 /* Object data functions that operate on key ids.
302  *  These functions are meant for *internal* use only.
303  */
304 void gtk_object_set_data_by_id      (GtkObject       *object,
305                                      guint            data_id,
306                                      gpointer         data);
307 void gtk_object_set_data_by_id_full (GtkObject       *object,
308                                      guint            data_id,
309                                      gpointer         data,
310                                      GtkDestroyNotify destroy);
311 gpointer gtk_object_get_data_by_id  (GtkObject       *object,
312                                      guint            data_id);
313 void  gtk_object_remove_data_by_id  (GtkObject       *object,
314                                      guint            data_id);
315 guint gtk_object_data_try_key       (const gchar     *key);
316 guint gtk_object_data_force_id      (const gchar     *key);
317
318 /* Set the "user_data" object data field of "object". It should
319  *  be noted that this is no different than calling 'gtk_object_set_data'
320  *  with a key of "user_data". It is merely provided as a convenience.
321  */
322 void gtk_object_set_user_data (GtkObject *object,
323                                gpointer   data);
324
325 /* Get the "user_data" object data field of "object". It should
326  *  be noted that this is no different than calling 'gtk_object_get_data'
327  *  with a key of "user_data". It is merely provided as a convenience.
328  */
329 gpointer gtk_object_get_user_data (GtkObject *object);
330
331 GtkObject* gtk_object_check_cast (GtkObject *obj,
332                                   GtkType    cast_type);
333
334 GtkObjectClass* gtk_object_check_class_cast (GtkObjectClass *klass,
335                                              GtkType         cast_type);
336
337 void    gtk_trace_referencing   (gpointer    *object,
338                                  const gchar *func,
339                                  guint        local_frame,
340                                  guint        line,
341                                  gboolean     do_ref);
342      
343 #if G_ENABLE_DEBUG && defined (__GNUC__)
344 #  define gtk_object_ref(o)   G_STMT_START{static guint f=0;gtk_trace_referencing((gpointer)o,__PRETTY_FUNCTION__,++f,__LINE__, 1);f--;}G_STMT_END
345 #  define gtk_object_unref(o) G_STMT_START{static guint f=0;gtk_trace_referencing((gpointer)o,__PRETTY_FUNCTION__,++f,__LINE__, 0);f--;}G_STMT_END
346 #endif  /* G_ENABLE_DEBUG && __GNUC__ */
347
348
349
350 #ifdef __cplusplus
351 }
352 #endif /* __cplusplus */
353
354
355 #endif /* __GTK_OBJECT_H__ */