]> Pileus Git - ~andy/gtk/blob - gtk/gtkobject.h
new user signal, and possibility to query signal information.
[~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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #ifndef __GTK_OBJECT_H__
19 #define __GTK_OBJECT_H__
20
21
22 #include <gtk/gtkenums.h>
23 #include <gtk/gtktypeutils.h>
24
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif /* __cplusplus */
29
30
31 /* GtkObject only uses the first 3 bits of the "flags" field.
32  *  They refer to the following flags.
33  * GtkWidget uses the remaining bits. Though this is a kinda nasty
34  *  break up, it does make the size of GtkWidget smaller.
35  */
36 enum
37 {
38   GTK_NEED_DESTROY      = 1 << 0,
39   GTK_BEING_DESTROYED   = 1 << 1,
40   GTK_IN_CALL           = 1 << 2
41 };
42
43
44 /* The debugging versions of the casting macros make sure the cast is "ok"
45  *  before proceeding, but they are definately slower than their less
46  *  careful counterparts as they involve no less than 3 function calls.
47  */
48 #ifdef NDEBUG
49
50 #define GTK_CHECK_CAST(obj,cast_type,cast)         ((cast*) obj)
51 #define GTK_CHECK_CLASS_CAST(klass,cast_type,cast) ((cast*) klass)
52
53 #else /* NDEBUG */
54
55 #define GTK_CHECK_CAST(obj,cast_type,cast) \
56   ((cast*) gtk_object_check_cast ((GtkObject*) obj, cast_type))
57
58 #define GTK_CHECK_CLASS_CAST(klass,cast_type,cast) \
59   ((cast*) gtk_object_check_class_cast ((GtkObjectClass*) klass, cast_type))
60
61 #endif /* NDEBUG */
62
63
64 /* Determines whether 'obj' is a type of 'otype'.
65  */
66 #define GTK_CHECK_TYPE(obj,otype)  (gtk_type_is_a (((GtkObject*) obj)->klass->type, otype))
67
68
69 /* Macro for casting a pointer to a GtkObject pointer.
70  */
71 #define GTK_OBJECT(obj)                   GTK_CHECK_CAST (obj, gtk_object_get_type (), GtkObject)
72
73 /* Macros for extracting various fields from GtkObject and
74  *  GtkObjectClass.
75  */
76 #define GTK_OBJECT_CLASS(klass)           GTK_CHECK_CLASS_CAST (klass, gtk_object_get_type (), GtkObjectClass)
77 #define GTK_OBJECT_FLAGS(obj)             (GTK_OBJECT (obj)->flags)
78 #define GTK_OBJECT_NEED_DESTROY(obj)      (GTK_OBJECT_FLAGS (obj) & GTK_NEED_DESTROY)
79 #define GTK_OBJECT_BEING_DESTROYED(obj)   (GTK_OBJECT_FLAGS (obj) & GTK_BEING_DESTROYED)
80 #define GTK_OBJECT_IN_CALL(obj)           (GTK_OBJECT_FLAGS (obj) & GTK_IN_CALL)
81 #define GTK_OBJECT_DESTROY(obj)           (GTK_OBJECT (obj)->klass->destroy)
82 #define GTK_OBJECT_TYPE(obj)              (GTK_OBJECT (obj)->klass->type)
83 #define GTK_OBJECT_SIGNALS(obj)           (GTK_OBJECT (obj)->klass->signals)
84 #define GTK_OBJECT_NSIGNALS(obj)          (GTK_OBJECT (obj)->klass->nsignals)
85
86 /* Macro for testing whether "obj" is of type GtkObject.
87  */
88 #define GTK_IS_OBJECT(obj)                GTK_CHECK_TYPE (obj, gtk_object_get_type ())
89
90 /* Macros for setting and clearing bits in the "flags" field of GtkObject.
91  */
92 #define GTK_OBJECT_SET_FLAGS(obj,flag)    (GTK_OBJECT_FLAGS (obj) |= (flag))
93 #define GTK_OBJECT_UNSET_FLAGS(obj,flag)  (GTK_OBJECT_FLAGS (obj) &= ~(flag))
94
95
96 typedef struct _GtkObjectClass  GtkObjectClass;
97
98
99 /* GtkObject is the base of the object hierarchy. It defines
100  *  the few basic items that all derived classes contain.
101  */
102 struct _GtkObject
103 {
104   /* 32 bits of flags. GtkObject only uses 3 of these bits and
105    *  GtkWidget uses the rest. This is done because structs are
106    *  aligned on 4 or 8 byte boundaries. If bitfields were used
107    *  both here and in GtkWidget much space would be wasted.
108    */
109   guint32 flags;
110
111   /* 16 bit reference count. "gtk_object_destroy" actually only
112    *  destroys an object when its ref count is 0. (Decrementing
113    *  a reference count of 0 is defined as a no-op).
114    */
115   guint16 ref_count;
116
117   /* A pointer to the objects class. This will actually point to
118    *  the derived objects class struct (which will be derived from
119    *  GtkObjectClass).
120    */
121   GtkObjectClass *klass;
122
123   /* The list of signal handlers and other data
124    *  fields for this object.
125    */
126   gpointer object_data;
127 };
128
129 /* GtkObjectClass is the base of the class hierarchy. It defines
130  *  the basic necessities for the class mechanism to work. Namely,
131  *  the "type", "signals" and "nsignals" fields.
132  */
133 struct _GtkObjectClass
134 {
135   /* The type identifier for the objects class. There is
136    *  one unique identifier per class.
137    */
138   GtkType type;
139
140   /* The signals this object class handles. "signals" is an
141    *  array of signal ID's.
142    */
143   gint *signals;
144
145   /* The number of signals listed in "signals".
146    */
147   gint nsignals;
148
149   /* The destroy function for objects. In one way ore another
150    *  this is defined for all objects. If an object class overrides
151    *  this method in order to perform class specific destruction
152    *  then it should still call it after it is finished with its
153    *  own cleanup. (See the destroy function for GtkWidget for
154    *  an example of how to do this).
155    */
156   void (* destroy) (GtkObject *object);
157
158   /* The number of arguments per class.
159    */
160   guint n_args;
161 };
162
163
164 /* For the purpose of user signals we need the signal function
165  * and signal marshaller signatures already in this place.
166  */
167 #define GTK_SIGNAL_FUNC(f)  ((GtkSignalFunc) f)
168
169 typedef void (*GtkSignalFunc)       (void);
170 typedef void (*GtkSignalMarshaller) (GtkObject      *object,
171                                      GtkSignalFunc   func,
172                                      gpointer        func_data,
173                                      GtkArg         *args);
174
175
176 /* Get the type identifier for GtkObject's.
177  */
178 guint   gtk_object_get_type             (void);
179
180 /* Append "signals" to those already defined in "class".
181  */
182 void    gtk_object_class_add_signals    (GtkObjectClass *klass,
183                                          gint           *signals,
184                                          gint            nsignals);
185
186 /* Append a user defined signal without default handler to a class.
187  */
188 gint    gtk_object_class_add_user_signal (GtkObjectClass     *klass,
189                                           const gchar        *name,
190                                           GtkSignalMarshaller marshaller,
191                                           GtkType             return_val,
192                                           gint                nparams,
193                                           ...);
194
195 GtkObject*      gtk_object_new          (guint          type,
196                                          ...);
197
198 GtkObject*      gtk_object_newv         (guint          type,
199                                          guint          nargs,
200                                          GtkArg         *args);
201
202 void    gtk_object_ref          (GtkObject      *object);
203
204 void    gtk_object_unref        (GtkObject      *object);
205
206 /* gtk_object_getv() sets an arguments type and value, or just
207  * its type to GTK_TYPE_INVALID.
208  * if arg->type == GTK_TYPE_STRING, it's the callers response to
209  * do a g_free (GTK_VALUE_STRING (arg));
210  */
211 void    gtk_object_getv         (GtkObject      *object,
212                                  guint          nargs,
213                                  GtkArg         *args);
214
215 /* gtk_object_set() takes a variable argument list of the form:
216  * (..., gchar *arg_name, ARG_VALUES, [repeatedly name/value pairs,] NULL)
217  * where ARG_VALUES type depend on the argument and can consist of
218  * more than one c-function argument.
219  */
220 void    gtk_object_set          (GtkObject      *object,
221                                  ...);
222
223 void    gtk_object_setv         (GtkObject      *object,
224                                  guint          nargs,
225                                  GtkArg         *args);
226
227 /* Allocate a GtkArg array of size nargs that hold the
228  * names and types of the args that can be used with
229  * gtk_object_set/gtk_object_get.
230  * It is the callers response to do a
231  * g_free (returned_args).
232  */
233 GtkArg* gtk_object_query_args   (GtkType        class_type,
234                                  guint          *nargs);
235
236 void    gtk_object_add_arg_type (const gchar    *arg_name,
237                                  GtkType        arg_type,
238                                  guint          arg_id);
239
240 GtkType gtk_object_get_arg_type (const gchar    *arg_name);
241
242 /* Emit the "destroy" signal for "object". Normally it is
243  *  permissible to emit a signal for an object instead of
244  *  calling the corresponding convenience routine, however
245  *  "gtk_object_destroy" should be called instead of emitting
246  *  the signal manually as it checks to see if the object is
247  *  currently handling another signal emittion (very likely)
248  *  and sets the GTK_NEED_DESTROY flag which tells the object
249  *  to be destroyed when it is done handling the signal emittion.
250  */
251 void gtk_object_destroy (GtkObject *object);
252
253 /* Set 'data' to the "object_data" field of the object. The
254  *  data is indexed by the "key". If there is already data
255  *  associated with "key" then the new data will replace it.
256  *  If 'data' is NULL then this call is equivalent to
257  *  'gtk_object_remove_data'.
258  */
259 void gtk_object_set_data (GtkObject   *object,
260                           const gchar *key,
261                           gpointer     data);
262
263 /* Get the data associated with "key".
264  */
265 gpointer gtk_object_get_data (GtkObject   *object,
266                               const gchar *key);
267
268 /* Remove the data associated with "key". This call is
269  *  equivalent to 'gtk_object_set_data' where 'data' is NULL.
270  */
271 void gtk_object_remove_data (GtkObject   *object,
272                              const gchar *key);
273
274 /* Set the "user_data" object data field of "object". It should
275  *  be noted that this is no different than calling 'gtk_object_data_add'
276  *  with a key of "user_data". It is merely provided as a convenience.
277  */
278 void gtk_object_set_user_data (GtkObject *object,
279                                gpointer   data);
280
281 /* Get the "user_data" object data field of "object". It should
282  *  be noted that this is no different than calling 'gtk_object_data_find'
283  *  with a key of "user_data". It is merely provided as a convenience.
284  */
285 gpointer gtk_object_get_user_data (GtkObject *object);
286
287 GtkObject* gtk_object_check_cast (GtkObject *obj,
288                                   GtkType    cast_type);
289
290 GtkObjectClass* gtk_object_check_class_cast (GtkObjectClass *klass,
291                                              GtkType         cast_type);
292
293
294 #ifdef __cplusplus
295 }
296 #endif /* __cplusplus */
297
298
299 #endif /* __GTK_OBJECT_H__ */