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 #include "gtkobject.h"
22 #include "gtkargcollector.c"
25 #define MAX_ARG_LENGTH (256)
28 /* --- typedefs --- */
29 typedef struct _GtkArgQueryData GtkArgQueryData;
32 /* --- structures --- */
33 struct _GtkArgQueryData
41 /* --- functions --- */
43 gtk_arg_type_new_static (GtkType base_class_type,
44 const gchar *arg_name,
45 guint class_n_args_offset,
46 GHashTable *arg_info_hash_table,
52 gchar class_part[MAX_ARG_LENGTH];
59 g_return_val_if_fail (arg_name != NULL, NULL);
60 g_return_val_if_fail (GTK_FUNDAMENTAL_TYPE (base_class_type) == GTK_TYPE_OBJECT, NULL);
61 g_return_val_if_fail (class_n_args_offset != 0, NULL);
62 g_return_val_if_fail (arg_info_hash_table != NULL, NULL);
63 g_return_val_if_fail (arg_type > GTK_TYPE_NONE, NULL);
64 g_return_val_if_fail (arg_id > 0, NULL);
65 g_return_val_if_fail ((arg_flags & GTK_ARG_READWRITE) != 0, NULL);
66 /* g_return_val_if_fail ((arg_flags & GTK_ARG_CHILD_ARG) == 0, NULL); */
68 arg_flags &= GTK_ARG_MASK;
70 arg_part = strchr (arg_name, ':');
71 if (!arg_part || (arg_part[0] != ':') || (arg_part[1] != ':'))
73 g_warning ("gtk_arg_type_new(): invalid arg name: \"%s\"\n", arg_name);
77 class_offset = (guint) (arg_part - arg_name);
78 strncpy (class_part, arg_name, class_offset);
79 class_part[class_offset] = 0;
81 class_type = gtk_type_from_name (class_part);
82 if (!gtk_type_is_a (class_type, base_class_type))
84 g_warning ("gtk_arg_type_new(): argument class in \"%s\" is not in the `%s' ancestry",
86 gtk_type_name (base_class_type));
90 p = gtk_type_class (class_type);
91 p += class_n_args_offset;
92 n_args_p = (guint*) p;
95 info = g_new (GtkArgInfo, 1);
96 info->class_type = class_type;
97 info->full_name = (gchar*) arg_name; /* _static */
98 info->name = info->full_name + class_offset + 2;
99 info->type = arg_type;
100 info->arg_flags = arg_flags;
101 info->arg_id = arg_id;
102 info->seq_id = *n_args_p;
104 g_hash_table_insert (arg_info_hash_table, info, info);
110 gtk_arg_name_strip_type (const gchar *arg_name)
112 gchar buffer[MAX_ARG_LENGTH];
117 if (!arg_name || strlen (arg_name) > MAX_ARG_LENGTH - 8)
120 p = strchr (arg_name, ':');
125 if ((p[0] != ':') || (p[1] != ':') || (p[2] == 0))
127 len = (guint) (p - arg_name);
128 strncpy (buffer, arg_name, len);
131 if (gtk_type_from_name (buffer) != GTK_TYPE_INVALID)
135 return (gchar*) arg_name;
139 gtk_arg_get_info (GtkType object_type,
140 GHashTable *arg_info_hash_table,
141 const gchar *arg_name,
145 gchar buffer[MAX_ARG_LENGTH];
153 if (!arg_name || strlen (arg_name) > MAX_ARG_LENGTH - 8)
154 return g_strdup ("argument name exceeds maximum size.");
156 /* split off the object-type part
158 p = strchr (arg_name, ':');
161 if ((p[0] != ':') || (p[1] != ':'))
162 return g_strconcat ("invalid argument syntax: \"",
166 len = (guint) (p - arg_name);
167 strncpy (buffer, arg_name, len);
170 otype = gtk_type_from_name (buffer);
171 if (otype != GTK_TYPE_INVALID)
175 otype = GTK_TYPE_INVALID;
177 /* split off the argument name
179 p = strchr (arg_name, ':');
182 if ((p[0] != ':') || (p[1] != ':'))
183 return g_strconcat ("invalid argument syntax: \"",
187 len = (guint) (p - arg_name);
188 strncpy (buffer, arg_name, len);
193 /* lookup the argument
195 if (otype != GTK_TYPE_INVALID)
199 info.class_type = otype;
200 info.name = (gchar*) arg_name;
202 *info_p = g_hash_table_lookup (arg_info_hash_table, &info);
203 if (*info_p && !gtk_type_is_a (object_type, (*info_p)->class_type))
209 while (!*info_p && GTK_FUNDAMENTAL_TYPE (otype) == GTK_TYPE_OBJECT)
213 info.class_type = otype;
214 info.name = (gchar*) arg_name;
216 *info_p = g_hash_table_lookup (arg_info_hash_table, &info);
218 otype = gtk_type_parent (otype);
223 return g_strconcat ("could not find argument \"",
226 gtk_type_name (object_type),
234 gtk_args_collect (GtkType object_type,
235 GHashTable *arg_info_hash_table,
237 GSList **info_list_p,
238 const gchar *first_arg_name,
243 const gchar *arg_name;
245 g_return_val_if_fail (arg_list_p != NULL, NULL);
247 g_return_val_if_fail (info_list_p != NULL, NULL);
249 g_return_val_if_fail (arg_info_hash_table != NULL, NULL);
253 arg_name = first_arg_name;
256 GtkArgInfo *info = NULL;
259 error = gtk_arg_get_info (object_type, arg_info_hash_table, arg_name, &info);
264 info_list = g_slist_prepend (info_list, info);
266 arg = gtk_arg_new (info->type);
267 arg->name = (gchar*) arg_name;
268 GTK_ARG_COLLECT_VALUE (arg, var_args, error);
269 arg_list = g_slist_prepend (arg_list, arg);
273 gtk_args_collect_cleanup (arg_list, info_list);
278 arg_name = va_arg (var_args, gchar*);
281 *arg_list_p = g_slist_reverse (arg_list);
282 *info_list_p = g_slist_reverse (info_list);
288 gtk_args_collect_cleanup (GSList *arg_list,
293 g_slist_free (info_list);
295 for (slist = arg_list; slist; slist = slist->next)
296 gtk_arg_free (slist->data, FALSE);
297 g_slist_free (arg_list);
301 gtk_args_query_foreach (gpointer key,
305 register GtkArgInfo *info;
306 register GtkArgQueryData *data;
308 g_assert (key == value); /* paranoid */
313 if (info->class_type == data->class_type)
314 data->arg_list = g_list_prepend (data->arg_list, info);
318 gtk_args_query (GtkType class_type,
319 GHashTable *arg_info_hash_table,
324 GtkArgQueryData query_data;
328 g_return_val_if_fail (n_args_p != NULL, NULL);
330 g_return_val_if_fail (arg_info_hash_table != NULL, NULL);
332 /* make sure the types class has been initialized, because
333 * the argument setup happens in the gtk_*_class_init() functions.
335 gtk_type_class (class_type);
337 query_data.arg_list = NULL;
338 query_data.class_type = class_type;
339 g_hash_table_foreach (arg_info_hash_table, gtk_args_query_foreach, &query_data);
341 if (query_data.arg_list)
343 register GList *list;
346 list = query_data.arg_list;
354 args = g_new0 (GtkArg, len);
357 *arg_flags = g_new (guint32, len);
366 g_assert (info->seq_id > 0 && info->seq_id <= len); /* paranoid */
368 args[info->seq_id - 1].type = info->type;
369 args[info->seq_id - 1].name = info->full_name;
371 (*arg_flags)[info->seq_id - 1] = info->arg_flags;
375 g_list_free (query_data.arg_list);
384 gtk_arg_new (GtkType arg_type)
388 arg = g_new0 (GtkArg, 1);
389 arg->type = arg_type;
396 gtk_arg_copy (GtkArg *src_arg,
399 g_return_val_if_fail (src_arg != NULL, NULL);
403 dest_arg = g_new0 (GtkArg, 1);
404 dest_arg->name = src_arg->name;
407 dest_arg->type = src_arg->type;
408 dest_arg->d = src_arg->d;
410 if (src_arg->type == GTK_TYPE_STRING)
411 GTK_VALUE_STRING (*dest_arg) = g_strdup (GTK_VALUE_STRING (*src_arg));
417 gtk_arg_free (GtkArg *arg,
418 gboolean free_contents)
420 g_return_if_fail (arg != NULL);
423 GTK_FUNDAMENTAL_TYPE (arg->type) == GTK_TYPE_STRING)
424 g_free (GTK_VALUE_STRING (*arg));
429 gtk_arg_info_equal (gconstpointer arg_info_1,
430 gconstpointer arg_info_2)
432 register const GtkArgInfo *info1 = arg_info_1;
433 register const GtkArgInfo *info2 = arg_info_2;
435 return ((info1->class_type == info2->class_type) &&
436 strcmp (info1->name, info2->name) == 0);
440 gtk_arg_info_hash (gconstpointer arg_info)
442 register const GtkArgInfo *info = arg_info;
443 register const gchar *p;
444 register guint h = info->class_type >> 8;
446 for (p = info->name; *p; p++)