]> Pileus Git - ~andy/gtk/blob - gtk/gtktypeutils.c
make the current version number 1.3.1 (binary age 0, interface age 0).
[~andy/gtk] / gtk / gtktypeutils.c
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
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtktypeutils.h"
28 #include "gtkobject.h"
29
30
31 /* --- functions --- */
32 GtkType
33 gtk_type_unique (GtkType            parent_type,
34                  const GtkTypeInfo *gtkinfo)
35 {
36   GTypeInfo tinfo = { 0, };
37
38   g_return_val_if_fail (GTK_TYPE_IS_OBJECT (parent_type), 0);
39   g_return_val_if_fail (gtkinfo != NULL, 0);
40   g_return_val_if_fail (gtkinfo->type_name != NULL, 0);
41   g_return_val_if_fail (g_type_from_name (gtkinfo->type_name) == 0, 0);
42
43   tinfo.class_size = gtkinfo->class_size;
44   tinfo.base_init = gtkinfo->base_class_init_func;
45   tinfo.base_finalize = NULL;
46   tinfo.class_init = (GClassInitFunc) gtkinfo->class_init_func;
47   tinfo.class_finalize = NULL;
48   tinfo.class_data = NULL;
49   tinfo.instance_size = gtkinfo->object_size;
50   tinfo.n_preallocs = 0;
51   tinfo.instance_init = gtkinfo->object_init_func;
52
53   return g_type_register_static (parent_type, gtkinfo->type_name, &tinfo);
54 }
55
56 gpointer
57 gtk_type_class (GtkType type)
58 {
59   static GQuark quark_static_class = 0;
60   gpointer class;
61
62   if (!G_TYPE_IS_ENUM (type) && !G_TYPE_IS_FLAGS (type))
63     g_return_val_if_fail (GTK_TYPE_IS_OBJECT (type), NULL);
64
65   /* ok, this is a bit ugly, GLib reference counts classes,
66    * and gtk_type_class() used to always return static classes.
67    * while we coud be faster with just peeking the glib class
68    * for the normal code path, we can't be sure that that
69    * class stays around (someone else might be holding the
70    * reference count and is going to drop it later). so to
71    * ensure that Gtk actually holds a static reference count
72    * on the class, we use GType qdata to store referenced
73    * classes, and only return those.
74    */
75
76   class = g_type_get_qdata (type, quark_static_class);
77   if (!class)
78     {
79       if (!quark_static_class)
80         quark_static_class = g_quark_from_static_string ("GtkStaticTypeClass");
81
82       class = g_type_class_ref (type);
83       g_assert (class != NULL);
84       g_type_set_qdata (type, quark_static_class, class);
85     }
86
87   return class;
88 }
89
90 gpointer
91 gtk_type_new (GtkType type)
92 {
93   gpointer object;
94
95   g_return_val_if_fail (GTK_TYPE_IS_OBJECT (type), NULL);
96
97   object = g_type_create_instance (type);
98
99   return object;
100 }
101
102 /* includes for various places
103  * with enum definitions
104  */
105 #include "makeenums.h"
106 /* type variable declarations
107  */
108 #include "gtktypebuiltins_vars.c"
109 GType GTK_TYPE_IDENTIFIER = 0;
110 #include "gtktypebuiltins_evals.c"        /* enum value definition arrays */
111
112 void
113 gtk_type_init (void)
114 {
115   static gboolean initialized = FALSE;
116   
117   if (!initialized)
118     {
119       static const struct {
120         GtkType type_id;
121         gchar *name;
122       } fundamental_info[] = {
123         { GTK_TYPE_POINTER,     "gpointer" },
124         { GTK_TYPE_SIGNAL,      "GtkSignal" },
125       };
126       static struct {
127         gchar              *type_name;
128         GtkType            *type_id;
129         GtkType             parent;
130         const GtkEnumValue *values;
131       } builtin_info[GTK_TYPE_N_BUILTINS + 1] = {
132 #include "gtktypebuiltins_ids.c"        /* type entries */
133         { NULL }
134       };
135       GTypeFundamentalInfo finfo = { 0, };
136       GTypeInfo tinfo = { 0, };
137       GtkType type_id;
138       guint i;
139
140       initialized = TRUE;
141
142       /* initialize GLib type system
143        */
144       g_type_init ();
145       
146       /* GTK_TYPE_OBJECT
147        */
148       gtk_object_get_type ();
149
150       /* compatibility fundamentals
151        */
152       for (i = 0; i < sizeof (fundamental_info) / sizeof (fundamental_info[0]); i++)
153         {
154           type_id = g_type_register_fundamental (fundamental_info[i].type_id,
155                                                  fundamental_info[i].name,
156                                                  &tinfo,
157                                                  &finfo);
158           g_assert (type_id == fundamental_info[i].type_id);
159         }
160
161       /* GTK_TYPE_IDENTIFIER
162        */
163       GTK_TYPE_IDENTIFIER = g_type_register_static (G_TYPE_STRING, "GtkIdentifier", &tinfo);
164
165       /* GTK_TYPE_BOXED
166        */
167       finfo.type_flags = G_TYPE_FLAG_DERIVABLE;
168       type_id = g_type_register_fundamental (GTK_TYPE_BOXED, "GtkBoxed", &tinfo, &finfo);
169       g_assert (type_id == GTK_TYPE_BOXED);
170
171       /* enums and flags
172        */
173       for (i = 0; i < GTK_TYPE_N_BUILTINS; i++)
174         {
175           GtkType type_id = 0;
176
177           if (builtin_info[i].parent == G_TYPE_ENUM)
178             type_id = g_enum_register_static (builtin_info[i].type_name, builtin_info[i].values);
179           else if (builtin_info[i].parent == G_TYPE_FLAGS)
180             type_id = g_flags_register_static (builtin_info[i].type_name, builtin_info[i].values);
181           else if (builtin_info[i].parent == GTK_TYPE_BOXED)
182             type_id = g_type_register_static (GTK_TYPE_BOXED, builtin_info[i].type_name, &tinfo);
183           else
184             g_assert_not_reached ();
185
186           *builtin_info[i].type_id = type_id;
187         }
188     }
189 }
190
191 GtkEnumValue*
192 gtk_type_enum_get_values (GtkType enum_type)
193 {
194   GEnumClass *class;
195
196   g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
197   
198   class = gtk_type_class (enum_type);
199   
200   return class->values;
201 }
202
203 GtkFlagValue*
204 gtk_type_flags_get_values (GtkType flags_type)
205 {
206   GFlagsClass *class;
207
208   g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL);
209
210   class = gtk_type_class (flags_type);
211
212   return class->values;
213 }
214
215 GtkEnumValue*
216 gtk_type_enum_find_value (GtkType      enum_type,
217                           const gchar *value_name)
218 {
219   GtkEnumValue *value;
220   GEnumClass *class;
221
222   g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
223   g_return_val_if_fail (value_name != NULL, NULL);
224
225   class = gtk_type_class (enum_type);
226   value = g_enum_get_value_by_name (class, value_name);
227   if (!value)
228     value = g_enum_get_value_by_nick (class, value_name);
229
230   return value;
231 }
232
233 GtkFlagValue*
234 gtk_type_flags_find_value (GtkType      flags_type,
235                            const gchar *value_name)
236 {
237   GtkFlagValue *value;
238   GFlagsClass *class;
239
240   g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL);
241   g_return_val_if_fail (value_name != NULL, NULL);
242
243   class = gtk_type_class (flags_type);
244   value = g_flags_get_value_by_name (class, value_name);
245   if (!value)
246     value = g_flags_get_value_by_nick (class, value_name);
247
248   return value;
249 }