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>
31 #endif /* __cplusplus */
35 /* The debugging versions of the casting macros make sure the cast is "ok"
36 * before proceeding, but they are definately slower than their less
37 * careful counterparts as they involve no less than 3 function calls.
39 #ifdef GTK_NO_CHECK_CASTS
41 #define GTK_CHECK_CAST(obj,cast_type,cast) ((cast*) (obj))
42 #define GTK_CHECK_CLASS_CAST(klass,cast_type,cast) ((cast*) (klass))
44 #else /* !GTK_NO_CHECK_CASTS */
46 #define GTK_CHECK_CAST(obj,cast_type,cast) \
47 ((cast*) gtk_object_check_cast ((GtkObject*) (obj), (cast_type)))
49 #define GTK_CHECK_CLASS_CAST(klass,cast_type,cast) \
50 ((cast*) gtk_object_check_class_cast ((GtkObjectClass*) (klass), (cast_type)))
52 #endif /* GTK_NO_CHECK_CASTS */
55 /* Determines whether `obj' and `klass' are a type of `otype'.
57 #define GTK_CHECK_TYPE(obj,otype) ( \
58 gtk_type_is_a (((GtkObject*) (obj))->klass->type, (otype)) \
60 #define GTK_CHECK_CLASS_TYPE(klass,otype) ( \
61 gtk_type_is_a (((GtkObjectClass*) (klass))->type, (otype)) \
64 /* Macro for casting a pointer to a GtkObject or GtkObjectClass pointer.
65 * The second portion of the ?: statments are just in place to offer
66 * descriptive warning message.
68 #define GTK_OBJECT(object) ( \
69 GTK_IS_OBJECT (object) ? \
70 (GtkObject*) (object) : \
71 (GtkObject*) gtk_object_check_cast ((GtkObject*) (object), GTK_TYPE_OBJECT) \
73 #define GTK_OBJECT_CLASS(klass) ( \
74 GTK_IS_OBJECT_CLASS (klass) ? \
75 (GtkObjectClass*) (klass) : \
76 (GtkObjectClass*) gtk_object_check_class_cast ((GtkObjectClass*) (klass), GTK_TYPE_OBJECT) \
79 /* Macro for testing whether `object' and `klass' are of type GTK_TYPE_OBJECT.
81 #define GTK_IS_OBJECT(object) ( \
83 GTK_IS_OBJECT_CLASS (((GtkObject*) (object))->klass) \
85 #define GTK_IS_OBJECT_CLASS(klass) ( \
87 GTK_FUNDAMENTAL_TYPE (((GtkObjectClass*) (klass))->type) == GTK_TYPE_OBJECT \
90 /* Macros for extracting various fields from GtkObject and GtkObjectClass.
92 #define GTK_OBJECT_TYPE(obj) (GTK_OBJECT (obj)->klass->type)
93 #define GTK_OBJECT_SIGNALS(obj) (GTK_OBJECT (obj)->klass->signals)
94 #define GTK_OBJECT_NSIGNALS(obj) (GTK_OBJECT (obj)->klass->nsignals)
96 /* GtkObject only uses the first 4 bits of the flags field.
97 * Derived objects may use the remaining bits. Though this
98 * is a kinda nasty break up, it does make the size of
99 * derived objects smaller.
103 G_NV (GTK_DESTROYED, destroyed, 1 << 0),
104 G_NV (GTK_FLOATING, floating, 1 << 1),
105 G_NV (GTK_CONNECTED, connected, 1 << 2),
107 G_NV (GTK_OBJECT_FLAG_LAST, object-flag-last, GTK_RESERVED_2)
108 } G_FLAGS (GtkObjectFlags);
110 /* Macros for extracting the object_flags from GtkObject.
112 #define GTK_OBJECT_FLAGS(obj) (GTK_OBJECT (obj)->flags)
113 #define GTK_OBJECT_DESTROYED(obj) (GTK_OBJECT_FLAGS (obj) & GTK_DESTROYED)
114 #define GTK_OBJECT_FLOATING(obj) (GTK_OBJECT_FLAGS (obj) & GTK_FLOATING)
115 #define GTK_OBJECT_CONNECTED(obj) (GTK_OBJECT_FLAGS (obj) & GTK_CONNECTED)
117 /* Macros for setting and clearing bits in the object_flags field of GtkObject.
119 #define GTK_OBJECT_SET_FLAGS(obj,flag) G_STMT_START{ (GTK_OBJECT_FLAGS (obj) |= (flag)); }G_STMT_END
120 #define GTK_OBJECT_UNSET_FLAGS(obj,flag) G_STMT_START{ (GTK_OBJECT_FLAGS (obj) &= ~(flag)); }G_STMT_END
122 /* GtkArg flag bits for gtk_object_add_arg_type
126 G_NV (GTK_ARG_READABLE, readable, 1 << 0),
127 G_NV (GTK_ARG_WRITABLE, writable, 1 << 1),
128 G_NV (GTK_ARG_CONSTRUCT, construct, 1 << 2),
129 G_NV (GTK_ARG_MASK, mask, 0x03),
132 G_NV (GTK_ARG_READWRITE, readwrite, GTK_ARG_READABLE | GTK_ARG_WRITABLE)
133 } G_FLAGS (GtkArgFlags);
136 typedef struct _GtkObjectClass GtkObjectClass;
139 /* GtkObject is the base of the object hierarchy. It defines
140 * the few basic items that all derived classes contain.
144 /* A pointer to the objects class. This will actually point to
145 * the derived objects class struct (which will be derived from
148 GtkObjectClass *klass;
150 /* 32 bits of flags. GtkObject only uses 4 of these bits and
151 * GtkWidget uses the rest. This is done because structs are
152 * aligned on 4 or 8 byte boundaries. If a new bitfield were
153 * used in GtkWidget much space would be wasted.
158 * refer to the file REFCOUNTING on this issue.
162 /* The list of signal handlers and other data
163 * fields for this object.
165 gpointer object_data;
168 /* GtkObjectClass is the base of the class hierarchy. It defines
169 * the basic necessities for the class mechanism to work. Namely,
170 * the "type", "signals" and "nsignals" fields.
172 struct _GtkObjectClass
174 /* The type identifier for the objects class. There is
175 * one unique identifier per class.
179 /* The signals this object class handles. "signals" is an
180 * array of signal ID's.
184 /* The number of signals listed in "signals".
188 /* The number of arguments per class.
192 /* The functions that will end an objects life time. In one way ore
193 * another all three of them are defined for all objects. If an
194 * object class overrides one of the methods in order to perform class
195 * specific destruction then it must still invoke its superclass'
196 * implementation of the method after it is finished with its
197 * own cleanup. (See the destroy function for GtkWidget for
198 * an example of how to do this).
200 void (* shutdown) (GtkObject *object);
201 void (* destroy) (GtkObject *object);
203 void (* finalize) (GtkObject *object);
207 /* For the purpose of user signals we need the signal function
208 * and signal marshaller signatures already in this place.
210 #define GTK_SIGNAL_FUNC(f) ((GtkSignalFunc) f)
212 typedef void (*GtkSignalFunc) (void);
213 typedef void (*GtkSignalMarshaller) (GtkObject *object,
219 /* Get the type identifier for GtkObject's.
221 GtkType gtk_object_get_type (void);
223 /* Append "signals" to those already defined in "class".
225 void gtk_object_class_add_signals (GtkObjectClass *klass,
229 /* Append a user defined signal without default handler to a class.
231 guint gtk_object_class_user_signal_new (GtkObjectClass *klass,
233 GtkSignalRunType signal_flags,
234 GtkSignalMarshaller marshaller,
238 guint gtk_object_class_user_signal_newv (GtkObjectClass *klass,
240 GtkSignalRunType signal_flags,
241 GtkSignalMarshaller marshaller,
246 guint gtk_object_class_add_user_signal (GtkObjectClass *klass,
248 GtkSignalMarshaller marshaller,
253 GtkObject* gtk_object_new (GtkType type,
256 GtkObject* gtk_object_newv (GtkType type,
259 void gtk_object_sink (GtkObject *object);
260 void gtk_object_ref (GtkObject *object);
261 void gtk_object_unref (GtkObject *object);
263 void gtk_object_weakref (GtkObject *object,
264 GtkDestroyNotify notify,
266 void gtk_object_weakunref (GtkObject *object,
267 GtkDestroyNotify notify,
270 void gtk_object_destroy (GtkObject *object);
272 /* gtk_object_getv() sets an arguments type and value, or just
273 * its type to GTK_TYPE_INVALID.
274 * if arg->type == GTK_TYPE_STRING, it's the callers response to
275 * do a g_free (GTK_VALUE_STRING (arg));
277 void gtk_object_getv (GtkObject *object,
281 /* gtk_object_set() takes a variable argument list of the form:
282 * (..., gchar *arg_name, ARG_VALUES, [repeatedly name/value pairs,] NULL)
283 * where ARG_VALUES type depend on the argument and can consist of
284 * more than one c-function argument.
286 void gtk_object_set (GtkObject *object,
289 void gtk_object_setv (GtkObject *object,
293 /* Allocate a GtkArg array of size nargs that hold the
294 * names and types of the args that can be used with
295 * gtk_object_set/gtk_object_get. if (arg_flags!=NULL),
296 * (*arg_flags) will be set to point to a newly allocated
297 * guint array that holds the flags of the args.
298 * It is the callers response to do a
299 * g_free (returned_args); g_free (*acess_masks).
301 GtkArg* gtk_object_query_args (GtkType class_type,
305 void gtk_object_add_arg_type (const gchar *arg_name,
310 GtkType gtk_object_get_arg_type (const gchar *arg_name);
312 /* Set 'data' to the "object_data" field of the object. The
313 * data is indexed by the "key". If there is already data
314 * associated with "key" then the new data will replace it.
315 * If 'data' is NULL then this call is equivalent to
316 * 'gtk_object_remove_data'.
318 void gtk_object_set_data (GtkObject *object,
322 /* Like gtk_object_set_data, but takes an additional argument
323 * which is a function to be called when the data is removed.
325 void gtk_object_set_data_full (GtkObject *object,
328 GtkDestroyNotify destroy);
330 /* Get the data associated with "key".
332 gpointer gtk_object_get_data (GtkObject *object,
335 /* Remove the data associated with "key". This call is
336 * equivalent to 'gtk_object_set_data' where 'data' is NULL.
338 void gtk_object_remove_data (GtkObject *object,
341 /* Object data functions that operate on key ids.
342 * These functions are meant for *internal* use only.
344 void gtk_object_set_data_by_id (GtkObject *object,
347 void gtk_object_set_data_by_id_full (GtkObject *object,
350 GtkDestroyNotify destroy);
351 gpointer gtk_object_get_data_by_id (GtkObject *object,
353 void gtk_object_remove_data_by_id (GtkObject *object,
355 #define gtk_object_data_try_key g_dataset_try_key
356 #define gtk_object_data_force_id g_dataset_force_id
358 /* Set the "user_data" object data field of "object". It should
359 * be noted that this is no different than calling 'gtk_object_set_data'
360 * with a key of "user_data". It is merely provided as a convenience.
362 void gtk_object_set_user_data (GtkObject *object,
365 /* Get the "user_data" object data field of "object". It should
366 * be noted that this is no different than calling 'gtk_object_get_data'
367 * with a key of "user_data". It is merely provided as a convenience.
369 gpointer gtk_object_get_user_data (GtkObject *object);
371 GtkObject* gtk_object_check_cast (GtkObject *obj,
374 GtkObjectClass* gtk_object_check_class_cast (GtkObjectClass *klass,
377 void gtk_trace_referencing (GtkObject *object,
384 # define gtk_object_ref(o) G_STMT_START{gtk_trace_referencing((o),G_GNUC_PRETTY_FUNCTION,0,__LINE__,1);}G_STMT_END
385 # define gtk_object_unref(o) G_STMT_START{gtk_trace_referencing((o),G_GNUC_PRETTY_FUNCTION,0,__LINE__,0);}G_STMT_END
386 #endif /* G_ENABLE_DEBUG && __GNUC__ */
392 #endif /* __cplusplus */
395 #endif /* __GTK_OBJECT_H__ */