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