gtktypeutils.c 7.05 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6
7
8
9
10
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Tim Janik's avatar
Tim Janik committed
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
16
17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19
20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22
23
24
25
26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

Hans Breuer's avatar
updated    
Hans Breuer committed
27
28
#include <string.h> /* strcmp */

Elliot Lee's avatar
Elliot Lee committed
29
#include "gtktypeutils.h"
30
#include "gtkobject.h"
Elliot Lee's avatar
Elliot Lee committed
31
32


33
34
35
36
/* --- functions --- */
GtkType
gtk_type_unique (GtkType            parent_type,
		 const GtkTypeInfo *gtkinfo)
Elliot Lee's avatar
Elliot Lee committed
37
{
38
  GTypeInfo tinfo = { 0, };
Elliot Lee's avatar
Elliot Lee committed
39

40
41
42
43
  g_return_val_if_fail (GTK_TYPE_IS_OBJECT (parent_type), 0);
  g_return_val_if_fail (gtkinfo != NULL, 0);
  g_return_val_if_fail (gtkinfo->type_name != NULL, 0);
  g_return_val_if_fail (g_type_from_name (gtkinfo->type_name) == 0, 0);
Elliot Lee's avatar
Elliot Lee committed
44

45
46
47
48
49
50
51
52
53
  tinfo.class_size = gtkinfo->class_size;
  tinfo.base_init = gtkinfo->base_class_init_func;
  tinfo.base_finalize = NULL;
  tinfo.class_init = (GClassInitFunc) gtkinfo->class_init_func;
  tinfo.class_finalize = NULL;
  tinfo.class_data = NULL;
  tinfo.instance_size = gtkinfo->object_size;
  tinfo.n_preallocs = 0;
  tinfo.instance_init = gtkinfo->object_init_func;
Elliot Lee's avatar
Elliot Lee committed
54

55
  return g_type_register_static (parent_type, gtkinfo->type_name, &tinfo, 0);
Elliot Lee's avatar
Elliot Lee committed
56
57
}

58
59
gpointer
gtk_type_class (GtkType type)
Elliot Lee's avatar
Elliot Lee committed
60
{
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  static GQuark quark_static_class = 0;
  gpointer class;

  if (!G_TYPE_IS_ENUM (type) && !G_TYPE_IS_FLAGS (type))
    g_return_val_if_fail (GTK_TYPE_IS_OBJECT (type), NULL);

  /* ok, this is a bit ugly, GLib reference counts classes,
   * and gtk_type_class() used to always return static classes.
   * while we coud be faster with just peeking the glib class
   * for the normal code path, we can't be sure that that
   * class stays around (someone else might be holding the
   * reference count and is going to drop it later). so to
   * ensure that Gtk actually holds a static reference count
   * on the class, we use GType qdata to store referenced
   * classes, and only return those.
76
   */
Elliot Lee's avatar
Elliot Lee committed
77

78
79
  class = g_type_get_qdata (type, quark_static_class);
  if (!class)
80
    {
81
82
      if (!quark_static_class)
	quark_static_class = g_quark_from_static_string ("GtkStaticTypeClass");
83

84
85
86
      class = g_type_class_ref (type);
      g_assert (class != NULL);
      g_type_set_qdata (type, quark_static_class, class);
87
    }
Elliot Lee's avatar
Elliot Lee committed
88

89
  return class;
Elliot Lee's avatar
Elliot Lee committed
90
91
92
93
94
}

gpointer
gtk_type_new (GtkType type)
{
95
  gpointer object;
96

97
  g_return_val_if_fail (GTK_TYPE_IS_OBJECT (type), NULL);
Elliot Lee's avatar
Elliot Lee committed
98

99
  object = g_object_new (type, NULL);
100

101
  return object;
102
103
}

104
105
106
/* includes for various places
 * with enum definitions
 */
107
#define GTK_ENABLE_BROKEN
108
109
110
111
112
113
#include "makeenums.h"
/* type variable declarations
 */
#include "gtktypebuiltins_vars.c"
GType GTK_TYPE_IDENTIFIER = 0;
#include "gtktypebuiltins_evals.c"	  /* enum value definition arrays */
Elliot Lee's avatar
Elliot Lee committed
114

Owen Taylor's avatar
Owen Taylor committed
115
116
/* Hack to communicate with GLib object debugging for now
 */
Tor Lillqvist's avatar
Tor Lillqvist committed
117
118
119
120
121
#ifdef G_OS_WIN32
#define IMPORT __declspec(dllimport)
#else
#define IMPORT
#endif
Owen Taylor's avatar
Owen Taylor committed
122

123
124
125
#include <gtk.h>	/* for gtktypebuiltins_ids.c */
#include <gdk.h>	/* gtktypebuiltins_ids.c */

Elliot Lee's avatar
Elliot Lee committed
126
void
127
gtk_type_init (GTypeDebugFlags debug_flags)
Elliot Lee's avatar
Elliot Lee committed
128
{
129
130
131
132
133
134
135
136
  static gboolean initialized = FALSE;
  
  if (!initialized)
    {
      static struct {
	gchar              *type_name;
	GtkType            *type_id;
	GtkType             parent;
137
138
	gconstpointer	    pointer1;
	gpointer	    pointer2;
139
140
141
142
143
      } builtin_info[GTK_TYPE_N_BUILTINS + 1] = {
#include "gtktypebuiltins_ids.c"	/* type entries */
	{ NULL }
      };
      GTypeInfo tinfo = { 0, };
144
      guint i;
145

146
      initialized = TRUE;
147

148
149
      /* initialize GLib type system
       */
150
      g_type_init (debug_flags);
151
      
152
153
154
      /* GTK_TYPE_OBJECT
       */
      gtk_object_get_type ();
Elliot Lee's avatar
Elliot Lee committed
155

156
      /* GTK_TYPE_IDENTIFIER
157
       */
158
      GTK_TYPE_IDENTIFIER = g_type_register_static (G_TYPE_STRING, "GtkIdentifier", &tinfo, 0);
159
160
161
162

      /* enums and flags
       */
      for (i = 0; i < GTK_TYPE_N_BUILTINS; i++)
163
	{
164
165
166
	  GtkType type_id = 0;

	  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
	    {
Tim Janik's avatar
Tim Janik committed
172
	      static const gchar *copy_types[] = {
173
174
		"GtkSelectionData", "GdkEvent", "GdkColor", "GtkBorder", "GtkTextIter", "PangoTabArray",
		"PangoFontDescription", "GtkTreeIter", "GtkTreePath", "GtkRequisition"
Tim Janik's avatar
Tim Janik committed
175
176
177
178
179
180
181
182
183
184
	      };
	      gboolean ref_counted = TRUE;
	      guint j;

	      for (j = 0; j < sizeof (copy_types) / sizeof (copy_types[0]); j++)
		if (strcmp (copy_types[j], builtin_info[i].type_name) == 0)
		  {
		    ref_counted = FALSE;
		    break;
		  }
185
186
	      if (builtin_info[i].pointer1 && builtin_info[i].pointer2)
		type_id = g_boxed_type_register_static (builtin_info[i].type_name,
Tim Janik's avatar
Tim Janik committed
187
							NULL,
188
							builtin_info[i].pointer1,
Tim Janik's avatar
Tim Janik committed
189
190
							builtin_info[i].pointer2,
							ref_counted);
191
192
193
	      else
		type_id = g_type_register_static (GTK_TYPE_BOXED, builtin_info[i].type_name, &tinfo, 0);
	    }
194
195
196
197
	  else
	    g_assert_not_reached ();

	  *builtin_info[i].type_id = type_id;
198
	}
199
200
201
    }
}

202
GtkEnumValue*
203
gtk_type_enum_get_values (GtkType enum_type)
Elliot Lee's avatar
Elliot Lee committed
204
{
205
  GEnumClass *class;
Elliot Lee's avatar
Elliot Lee committed
206

207
  g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
208
  
209
  class = gtk_type_class (enum_type);
210
  
211
  return class->values;
212
213
214
}

GtkFlagValue*
215
gtk_type_flags_get_values (GtkType flags_type)
216
{
217
  GFlagsClass *class;
218

219
  g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL);
220

221
  class = gtk_type_class (flags_type);
222

223
  return class->values;
224
225
}

226
227
228
GtkEnumValue*
gtk_type_enum_find_value (GtkType      enum_type,
			  const gchar *value_name)
229
{
230
231
  GtkEnumValue *value;
  GEnumClass *class;
232

233
234
  g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
  g_return_val_if_fail (value_name != NULL, NULL);
235

236
237
238
239
  class = gtk_type_class (enum_type);
  value = g_enum_get_value_by_name (class, value_name);
  if (!value)
    value = g_enum_get_value_by_nick (class, value_name);
240

241
  return value;
242
243
}

244
245
246
GtkFlagValue*
gtk_type_flags_find_value (GtkType      flags_type,
			   const gchar *value_name)
247
{
248
249
  GtkFlagValue *value;
  GFlagsClass *class;
Elliot Lee's avatar
Elliot Lee committed
250

251
252
  g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL);
  g_return_val_if_fail (value_name != NULL, NULL);
Tim Janik's avatar
Tim Janik committed
253

254
255
256
257
  class = gtk_type_class (flags_type);
  value = g_flags_get_value_by_name (class, value_name);
  if (!value)
    value = g_flags_get_value_by_nick (class, value_name);
258

259
  return value;
260
}