don't install test plugins don't eat turkey
[platform/upstream/gstreamer.git] / gst / gobject2gtk.c
1 /* Written by Erik Walthinsen 06-2001 */
2 /* Modified by Jamie Gennis 06-2001 */
3 #include <string.h>
4 #include <stdio.h>
5 #include "gstlog.h"
6 #include "gobject2gtk.h"
7
8
9 /* list functions not in glib 1.2 */
10 GList *
11 g_list_delete_link (GList *list, GList *llink)
12 {
13   GList *temp = g_list_remove_link(list, llink);
14   g_list_free(llink);
15   return temp;
16 }
17
18 GSList *
19 g_slist_delete_link (GSList *list, GSList *llink)
20 {
21   GSList *temp = g_slist_remove_link(list, llink);
22   g_slist_free(llink);
23   return temp;
24 }
25
26 /* string helper functions not in glib 1.2 */
27
28 gchar*
29 g_strcanon (gchar       *string,
30             const gchar *valid_chars,
31             gchar        substitutor)
32 {
33   register gchar *c;
34   
35   g_return_val_if_fail (string != NULL, NULL);
36   g_return_val_if_fail (valid_chars != NULL, NULL);
37     
38   for (c = string; *c; c++)
39     {
40       if (!strchr (valid_chars, *c))
41         *c = substitutor;
42     }
43
44   return string;
45 }
46
47 /* GObject dummy implementation */
48 static void
49 g_object_set_arg(GtkObject *object, GtkArg *arg, guint id)
50 {
51   ((GObjectClass *)object->klass)->set_property((GObject *)object,id,arg,NULL);
52 }
53
54 static void
55 g_object_get_arg(GtkObject *object, GtkArg *arg, guint id)
56 {
57   ((GObjectClass *)object->klass)->get_property((GObject *)object,id,arg,NULL);
58 }
59
60 static void
61 g_object_base_class_init (GObjectClass *klass)
62 {
63   GtkObjectClass *gtkobject_class;
64
65   gtkobject_class = (GtkObjectClass*) klass;
66  
67   gtkobject_class->set_arg = g_object_set_arg;
68   gtkobject_class->get_arg = g_object_get_arg;
69 }
70
71 void
72 g2g_object_run_dispose (GObject *object)
73 {
74   g_return_if_fail (G_IS_OBJECT (object));
75   g_return_if_fail (object->ref_count > 0);
76
77   g_object_ref (object);
78   G_OBJECT_GET_CLASS (object)->dispose (object);
79   g_object_unref (object);
80 }
81
82 GType
83 g2g_object_get_type (void)
84 {
85   static GType object_type = 0;
86
87   if (!object_type) {
88     static const GtkTypeInfo object_info = {
89       "GObject",
90       sizeof(GObject),
91       sizeof(GObjectClass),
92       (GtkClassInitFunc)NULL,
93       (GtkObjectInitFunc)NULL,
94       (GtkArgSetFunc)NULL,
95       (GtkArgGetFunc)NULL,
96       (GtkClassInitFunc)g_object_base_class_init,
97     };
98     object_type = gtk_type_unique(gtk_object_get_type(),&object_info);
99   }
100   return object_type;
101
102
103
104
105
106 guint
107 g2g_type_register_static (GtkType parent_type, gchar *type_name,
108                         const GTypeInfo *info, guint flags)
109 {
110   GtkTypeInfo gtkinfo = {
111     type_name,
112     info->instance_size,
113     info->class_size,
114     info->class_init,
115     info->instance_init,
116     NULL,
117     NULL,
118     info->base_init,
119   };
120   return gtk_type_unique(parent_type,&gtkinfo);
121 }
122
123
124 gpointer
125 g2g_object_new(GtkType type,gpointer blah_varargs_stuff) {
126   return gtk_type_new(type);
127 }
128
129
130 void
131 g2g_object_class_install_property(GObjectClass *oclass,guint property_id,GParamSpec *pspec)
132 {
133   gchar *arg_fullname;
134  
135   arg_fullname = g_strdup_printf("%s::%s",gtk_type_name(oclass->type),pspec->name);
136   /* fprintf(stderr,"installing arg \"%s\" into class \"%s\"\n",arg_fullname,""); */
137   gtk_object_add_arg_type(arg_fullname,pspec->value_type,pspec->flags,property_id);
138   g_free(pspec);
139 }
140
141 GParamSpec *
142 g2g_object_class_find_property(GObjectClass *class, const gchar *name)
143 {
144   GtkArgInfo *info;
145   GParamSpec *spec;
146
147   /* fprintf(stderr,"class name is %s\n",gtk_type_name(class->type)); */
148
149   /* the return value NULL if no error */
150   if (gtk_object_arg_get_info(class->type,name,&info) != NULL) {
151     return NULL;
152   }
153   
154   spec = g_new0(GParamSpec,1);
155
156   spec->name = (gchar *) name;
157   spec->value_type = info->type;
158   spec->flags = info->arg_flags;
159
160   return spec;
161 }
162
163 GParamSpec **
164 g2g_object_class_list_properties(GObjectClass *oclass,guint *n_properties) {
165   GType type = G_OBJECT_CLASS_TYPE (oclass);
166   guint32 *flags;
167   GtkArg *args;
168   gint num_args;
169   GParamSpec **params;
170   int i;
171
172   args = gtk_object_query_args (type, &flags, &num_args);
173   /* FIXME: args and flags need to be freed. */
174
175   params = g_new0(GParamSpec *,num_args);
176   for (i=0;i<num_args;i++) {
177     params[i] = g_new0(GParamSpec,1);
178     params[i]->name = args[i].name;
179     params[i]->value_type = args[i].type;
180     params[i]->flags = flags[i];
181   }
182
183   *n_properties = num_args;
184
185   return params;
186 }
187
188 GParamSpec *
189 g2g_param_spec_boolean(gchar *name,gchar *nick,gchar *blurb,gboolean def,gint flags) {
190   GParamSpec *spec = g_new(GParamSpec,1);
191
192   spec->name = name;
193   spec->value_type = GTK_TYPE_BOOL;
194   spec->flags = flags;
195
196   return spec;
197 }
198
199 GParamSpec *
200 g2g_param_spec_enum(gchar *name,gchar *nick,gchar *blurb,GtkType e,guint def,gint flags) {
201   GParamSpec *spec = g_new(GParamSpec,1);
202
203   spec->name = name;
204   spec->value_type = e;
205   spec->flags = flags;
206
207   return spec;
208 }
209
210 GParamSpec *
211 g2g_param_spec_int(gchar *name,gchar *nick,gchar *blurb,gint min,gint max,gint def,gint flags) {
212   GParamSpec *spec = g_new(GParamSpec,1);
213
214   spec->name = name;
215   spec->value_type = GTK_TYPE_INT;
216   spec->flags = flags;
217
218   return spec;
219 }
220
221 GParamSpec *
222 g2g_param_spec_uint(gchar *name,gchar *nick,gchar *blurb,guint min,guint max,guint def,gint flags) {
223   GParamSpec *spec = g_new(GParamSpec,1);
224
225   spec->name = name;
226   spec->value_type = GTK_TYPE_UINT;
227   spec->flags = flags;
228
229   return spec;
230 }
231
232 GParamSpec *
233 g2g_param_spec_long(gchar *name,gchar *nick,gchar *blurb,glong min,glong max,glong def,gint flags) {
234   GParamSpec *spec = g_new(GParamSpec,1);
235
236   spec->name = name;
237   spec->value_type = GTK_TYPE_LONG;
238   spec->flags = flags;
239
240   return spec;
241 }
242
243 GParamSpec *
244 g2g_param_spec_ulong(gchar *name,gchar *nick,gchar *blurb,gulong min,gulong max,gulong def,gint flags) {
245   GParamSpec *spec = g_new(GParamSpec,1);
246
247   spec->name = name;
248   spec->value_type = GTK_TYPE_ULONG;
249   spec->flags = flags;
250
251   return spec;
252 }
253
254 GParamSpec *
255 g2g_param_spec_float(gchar *name,gchar *nick,gchar *blurb,float min,float max,float def,gint flags) {
256   GParamSpec *spec = g_new(GParamSpec,1);
257
258   spec->name = name;
259   spec->value_type = GTK_TYPE_FLOAT;
260   spec->flags = flags;
261
262   return spec;
263 }
264
265 GParamSpec *
266 g2g_param_spec_double(gchar *name,gchar *nick,gchar *blurb,double min,double max,double def,gint flags) {
267   GParamSpec *spec = g_new(GParamSpec,1);
268
269   spec->name = name;
270   spec->value_type = GTK_TYPE_DOUBLE;
271   spec->flags = flags;
272
273   return spec;
274 }
275
276 GParamSpec *
277 g2g_param_spec_pointer(gchar *name,gchar *nick,gchar *blurb,gint flags) {
278   GParamSpec *spec = g_new(GParamSpec,1);
279
280   spec->name = name;
281   spec->value_type = GTK_TYPE_POINTER;
282   spec->flags = flags;
283
284   return spec;
285 }
286
287 GParamSpec *
288 g2g_param_spec_string(gchar *name,gchar *nick,gchar *blurb,gchar *def,gint flags) {
289   GParamSpec *spec = g_new(GParamSpec,1);
290
291   spec->name = name;
292   spec->value_type = GTK_TYPE_STRING;
293   spec->flags = flags;
294
295   return spec;
296 }
297
298
299
300 guint
301 g2g_signal_new (const gchar       *name,
302                 GtkType            object_type,
303                 GtkSignalRunType   signal_flags,
304                 guint              function_offset,
305                 gpointer           accumulator,  /* GSignalAccumulator */
306                 gpointer           accu_data,
307                 GtkSignalMarshaller  marshaller,
308                 GType              return_val,
309                 guint              nparams,
310                 ...)
311 {
312   GtkType *params;
313   guint i;
314   va_list args;
315   guint signal_id;
316
317   if (strcmp (name, "destroy") == 0)
318     name = "g2gdestroy";
319
320 #define MAX_SIGNAL_PARAMS               (31)            /* from gtksignal.c */
321   g_return_val_if_fail (nparams < MAX_SIGNAL_PARAMS, 0);
322      
323   if (nparams > 0) 
324     {
325       params = g_new (GtkType, nparams);
326
327       va_start (args, nparams);
328                    
329       for (i = 0; i < nparams; i++)
330         params[i] = va_arg (args, GtkType);
331   
332       va_end (args);
333     }           
334   else
335     params = NULL;
336  
337   signal_id = gtk_signal_newv (name,
338                                signal_flags,
339                                object_type,
340                                function_offset,
341                                marshaller,
342                                return_val,
343                                nparams,
344                                params);
345           
346   g_free (params);
347
348   /* now register it. */
349   gtk_object_class_add_signals(gtk_type_class(object_type), &signal_id, 1);
350     
351   return signal_id;
352 }
353
354 gint* g_signal_list_ids (GType type, guint *n_ids)
355 {
356   GtkObjectClass *class;
357
358   class = gtk_type_class (type);
359
360   *n_ids = class->nsignals;
361
362   return class->signals;
363 }