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.
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/.
27 #include "gtktypeutils.h"
28 #include "gtkobject.h"
31 /* --- functions --- */
33 gtk_type_unique (GtkType parent_type,
34 const GtkTypeInfo *gtkinfo)
36 GTypeInfo tinfo = { 0, };
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);
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;
53 return g_type_register_static (parent_type, gtkinfo->type_name, &tinfo);
57 gtk_type_class (GtkType type)
59 static GQuark quark_static_class = 0;
62 if (!G_TYPE_IS_ENUM (type) && !G_TYPE_IS_FLAGS (type))
63 g_return_val_if_fail (GTK_TYPE_IS_OBJECT (type), NULL);
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.
76 class = g_type_get_qdata (type, quark_static_class);
79 if (!quark_static_class)
80 quark_static_class = g_quark_from_static_string ("GtkStaticTypeClass");
82 class = g_type_class_ref (type);
83 g_assert (class != NULL);
84 g_type_set_qdata (type, quark_static_class, class);
91 gtk_type_new (GtkType type)
95 g_return_val_if_fail (GTK_TYPE_IS_OBJECT (type), NULL);
97 object = g_type_create_instance (type);
102 /* includes for various places
103 * with enum definitions
105 #include "makeenums.h"
106 /* type variable declarations
108 #include "gtktypebuiltins_vars.c"
109 GType GTK_TYPE_IDENTIFIER = 0;
110 #include "gtktypebuiltins_evals.c" /* enum value definition arrays */
115 static gboolean initialized = FALSE;
119 static const struct {
122 } fundamental_info[] = {
123 { GTK_TYPE_POINTER, "gpointer" },
124 { GTK_TYPE_SIGNAL, "GtkSignal" },
130 const GtkEnumValue *values;
131 } builtin_info[GTK_TYPE_N_BUILTINS + 1] = {
132 #include "gtktypebuiltins_ids.c" /* type entries */
135 GTypeFundamentalInfo finfo = { 0, };
136 GTypeInfo tinfo = { 0, };
142 /* initialize GLib type system
148 gtk_object_get_type ();
150 /* compatibility fundamentals
152 for (i = 0; i < sizeof (fundamental_info) / sizeof (fundamental_info[0]); i++)
154 type_id = g_type_register_fundamental (fundamental_info[i].type_id,
155 fundamental_info[i].name,
158 g_assert (type_id == fundamental_info[i].type_id);
161 /* GTK_TYPE_IDENTIFIER
163 GTK_TYPE_IDENTIFIER = g_type_register_static (G_TYPE_STRING, "GtkIdentifier", &tinfo);
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);
173 for (i = 0; i < GTK_TYPE_N_BUILTINS; i++)
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);
184 g_assert_not_reached ();
186 *builtin_info[i].type_id = type_id;
192 gtk_type_enum_get_values (GtkType enum_type)
196 g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
198 class = gtk_type_class (enum_type);
200 return class->values;
204 gtk_type_flags_get_values (GtkType flags_type)
208 g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL);
210 class = gtk_type_class (flags_type);
212 return class->values;
216 gtk_type_enum_find_value (GtkType enum_type,
217 const gchar *value_name)
222 g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
223 g_return_val_if_fail (value_name != NULL, NULL);
225 class = gtk_type_class (enum_type);
226 value = g_enum_get_value_by_name (class, value_name);
228 value = g_enum_get_value_by_nick (class, value_name);
234 gtk_type_flags_find_value (GtkType flags_type,
235 const gchar *value_name)
240 g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL);
241 g_return_val_if_fail (value_name != NULL, NULL);
243 class = gtk_type_class (flags_type);
244 value = g_flags_get_value_by_name (class, value_name);
246 value = g_flags_get_value_by_nick (class, value_name);