1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
19 #ifndef __GTK_OBJECT_H__
20 #define __GTK_OBJECT_H__
23 #include <gtk/gtkenums.h>
24 #include <gtk/gtktypeutils.h>
25 #include <gtk/gtkdebug.h>
30 #endif /* __cplusplus */
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.
38 #ifdef GTK_NO_CHECK_CASTS
40 #define GTK_CHECK_CAST(obj,cast_type,cast) ((cast*) (obj))
41 #define GTK_CHECK_CLASS_CAST(klass,cast_type,cast) ((cast*) (klass))
43 #else /* !GTK_NO_CHECK_CASTS */
45 #define GTK_CHECK_CAST(obj,cast_type,cast) \
46 ((cast*) gtk_object_check_cast ((GtkObject*) (obj), (cast_type)))
48 #define GTK_CHECK_CLASS_CAST(klass,cast_type,cast) \
49 ((cast*) gtk_object_check_class_cast ((GtkObjectClass*) (klass), (cast_type)))
51 #endif /* GTK_NO_CHECK_CASTS */
54 /* Determines whether 'obj' is a type of 'otype'.
56 #define GTK_CHECK_TYPE(obj,otype) (gtk_type_is_a (((GtkObject*) (obj))->klass->type, (otype)))
59 /* Macro for casting a pointer to a GtkObject pointer.
61 #define GTK_OBJECT(obj) GTK_CHECK_CAST ((obj), gtk_object_get_type (), GtkObject)
63 /* Macros for extracting various fields from GtkObject and GtkObjectClass.
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)
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.
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
84 /* GtkArg flag bits for gtk_object_add_arg_type
88 GTK_ARG_READABLE = 1 << 0,
89 GTK_ARG_WRITABLE = 1 << 1,
90 GTK_ARG_CONSTRUCT = 1 << 2
92 #define GTK_ARG_READWRITE (GTK_ARG_READABLE | GTK_ARG_WRITABLE)
95 /* Macros for extracting the object_flags from GtkObject.
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)
101 /* Macros for setting and clearing bits in the object_flags field of GtkObject.
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
106 /* Macro for testing whether "obj" is of type GtkObject.
108 #define GTK_IS_OBJECT(obj) (GTK_CHECK_TYPE ((obj), gtk_object_get_type ()))
111 typedef struct _GtkObjectClass GtkObjectClass;
114 /* GtkObject is the base of the object hierarchy. It defines
115 * the few basic items that all derived classes contain.
119 /* A pointer to the objects class. This will actually point to
120 * the derived objects class struct (which will be derived from
123 GtkObjectClass *klass;
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.
133 * refer to the file REFCOUNTING on this issue.
137 /* The list of signal handlers and other data
138 * fields for this object.
140 gpointer object_data;
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.
147 struct _GtkObjectClass
149 /* The type identifier for the objects class. There is
150 * one unique identifier per class.
154 /* The signals this object class handles. "signals" is an
155 * array of signal ID's.
159 /* The number of signals listed in "signals".
163 /* The number of arguments per class.
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).
175 void (* shutdown) (GtkObject *object);
176 void (* destroy) (GtkObject *object);
178 void (* finalize) (GtkObject *object);
182 /* For the purpose of user signals we need the signal function
183 * and signal marshaller signatures already in this place.
185 #define GTK_SIGNAL_FUNC(f) ((GtkSignalFunc) f)
187 typedef void (*GtkSignalFunc) (void);
188 typedef void (*GtkSignalMarshaller) (GtkObject *object,
194 /* Get the type identifier for GtkObject's.
196 guint gtk_object_get_type (void);
198 /* Append "signals" to those already defined in "class".
200 void gtk_object_class_add_signals (GtkObjectClass *klass,
204 /* Append a user defined signal without default handler to a class.
206 guint gtk_object_class_add_user_signal (GtkObjectClass *klass,
208 GtkSignalMarshaller marshaller,
213 GtkObject* gtk_object_new (GtkType type,
216 GtkObject* gtk_object_newv (GtkType type,
219 void gtk_object_sink (GtkObject *object);
220 void gtk_object_ref (GtkObject *object);
221 void gtk_object_unref (GtkObject *object);
223 void gtk_object_weakref (GtkObject *object,
224 GtkDestroyNotify notify,
226 void gtk_object_weakunref (GtkObject *object,
227 GtkDestroyNotify notify,
230 void gtk_object_destroy (GtkObject *object);
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));
237 void gtk_object_getv (GtkObject *object,
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.
246 void gtk_object_set (GtkObject *object,
249 void gtk_object_setv (GtkObject *object,
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).
261 GtkArg* gtk_object_query_args (GtkType class_type,
265 void gtk_object_add_arg_type (const gchar *arg_name,
270 GtkType gtk_object_get_arg_type (const gchar *arg_name);
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'.
278 void gtk_object_set_data (GtkObject *object,
282 /* Like gtk_object_set_data, but takes an additional argument
283 * which is a function to be called when the data is removed.
285 void gtk_object_set_data_full (GtkObject *object,
288 GtkDestroyNotify destroy);
290 /* Get the data associated with "key".
292 gpointer gtk_object_get_data (GtkObject *object,
295 /* Remove the data associated with "key". This call is
296 * equivalent to 'gtk_object_set_data' where 'data' is NULL.
298 void gtk_object_remove_data (GtkObject *object,
301 /* Object data functions that operate on key ids.
302 * These functions are meant for *internal* use only.
304 void gtk_object_set_data_by_id (GtkObject *object,
307 void gtk_object_set_data_by_id_full (GtkObject *object,
310 GtkDestroyNotify destroy);
311 gpointer gtk_object_get_data_by_id (GtkObject *object,
313 void gtk_object_remove_data_by_id (GtkObject *object,
315 guint gtk_object_data_try_key (const gchar *key);
316 guint gtk_object_data_force_id (const gchar *key);
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.
322 void gtk_object_set_user_data (GtkObject *object,
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.
329 gpointer gtk_object_get_user_data (GtkObject *object);
331 GtkObject* gtk_object_check_cast (GtkObject *obj,
334 GtkObjectClass* gtk_object_check_class_cast (GtkObjectClass *klass,
337 void gtk_trace_referencing (gpointer *object,
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__ */
352 #endif /* __cplusplus */
355 #endif /* __GTK_OBJECT_H__ */