]> Pileus Git - ~andy/gtk/blob - gtk/gtkobject.h
Initial revision
[~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->signals)
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   guint 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
159
160 /* Get the type identifier for GtkObject's.
161  */
162 guint gtk_object_get_type (void);
163
164 /* Append "signals" to those already defined in "class".
165  */
166 void gtk_object_class_add_signals (GtkObjectClass *klass,
167                                    gint           *signals,
168                                    gint            nsignals);
169
170 void gtk_object_ref (GtkObject *object);
171
172 void gtk_object_unref (GtkObject *object);
173
174 GtkObject* gtk_object_new (guint type,
175                            ...);
176
177 GtkObject* gtk_object_newv (guint   type,
178                             gint    nargs,
179                             GtkArg *args);
180
181 void gtk_object_set (GtkObject *obj,
182                      ...);
183
184 void gtk_object_setv (GtkObject *obj,
185                       gint       nargs,
186                       GtkArg    *args);
187
188 void gtk_object_add_arg_type (const char *arg_name,
189                               GtkType     arg_type);
190
191 GtkType gtk_object_get_arg_type (const char *arg_name);
192
193 /* Emit the "destroy" signal for "object". Normally it is
194  *  permissible to emit a signal for an object instead of
195  *  calling the corresponding convenience routine, however
196  *  "gtk_object_destroy" should be called instead of emitting
197  *  the signal manually as it checks to see if the object is
198  *  currently handling another signal emittion (very likely)
199  *  and sets the GTK_NEED_DESTROY flag which tells the object
200  *  to be destroyed when it is done handling the signal emittion.
201  */
202 void gtk_object_destroy (GtkObject *object);
203
204 /* Set 'data' to the "object_data" field of the object. The
205  *  data is indexed by the "key". If there is already data
206  *  associated with "key" then the new data will replace it.
207  *  If 'data' is NULL then this call is equivalent to
208  *  'gtk_object_remove_data'.
209  */
210 void gtk_object_set_data (GtkObject   *object,
211                           const gchar *key,
212                           gpointer     data);
213
214 /* Get the data associated with "key".
215  */
216 gpointer gtk_object_get_data (GtkObject   *object,
217                               const gchar *key);
218
219 /* Remove the data associated with "key". This call is
220  *  equivalent to 'gtk_object_set_data' where 'data' is NULL.
221  */
222 void gtk_object_remove_data (GtkObject   *object,
223                              const gchar *key);
224
225 /* Set the "user_data" object data field of "object". It should
226  *  be noted that this is no different than calling 'gtk_object_data_add'
227  *  with a key of "user_data". It is merely provided as a convenience.
228  */
229 void gtk_object_set_user_data (GtkObject *object,
230                                gpointer   data);
231
232 /* Get the "user_data" object data field of "object". It should
233  *  be noted that this is no different than calling 'gtk_object_data_find'
234  *  with a key of "user_data". It is merely provided as a convenience.
235  */
236 gpointer gtk_object_get_user_data (GtkObject *object);
237
238 GtkObject* gtk_object_check_cast (GtkObject *obj,
239                                   GtkType    cast_type);
240
241 GtkObjectClass* gtk_object_check_class_cast (GtkObjectClass *klass,
242                                              GtkType         cast_type);
243
244
245 #ifdef __cplusplus
246 }
247 #endif /* __cplusplus */
248
249
250 #endif /* __GTK_OBJECT_H__ */