]> Pileus Git - ~andy/gtk/blob - tests/prop-editor.c
Plug leak.
[~andy/gtk] / tests / prop-editor.c
1 /* prop-editor.c
2  * Copyright (C) 2000  Red Hat, Inc.
3  *
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.
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  * Library General Public License for more details.
13  *
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.
18  */
19
20 #include <config.h>
21 #include <string.h>
22
23 #undef GTK_DISABLE_DEPRECATED
24 #include <gtk/gtk.h>
25
26 #include "prop-editor.h"
27
28
29 typedef struct
30 {
31   gpointer instance;
32   GObject *alive_object;
33   guint id;
34 } DisconnectData;
35
36 static void
37 disconnect_func (gpointer data)
38 {
39   DisconnectData *dd = data;
40   
41   g_signal_handler_disconnect (dd->instance, dd->id);
42 }
43
44 static void
45 signal_removed (gpointer  data,
46                 GClosure *closure)
47 {
48   DisconnectData *dd = data;
49
50   g_object_steal_data (dd->alive_object, "alive-object-data");
51   g_free (dd);
52 }
53
54 static void
55 g_object_connect_property (GObject *object,
56                            const gchar *prop_name,
57                            GCallback func,
58                            gpointer data,
59                            GObject *alive_object)
60 {
61   GClosure *closure;
62   gchar *with_detail = g_strconcat ("notify::", prop_name, NULL);
63   DisconnectData *dd;
64
65   dd = g_new (DisconnectData, 1);
66
67   closure = g_cclosure_new (func, data, NULL);
68
69   g_closure_add_invalidate_notifier (closure, dd, signal_removed);
70
71   dd->id = g_signal_connect_closure (object, with_detail,
72                                      closure, FALSE);
73
74   dd->instance = object;
75   dd->alive_object = alive_object;
76   
77   g_object_set_data_full (G_OBJECT (alive_object),
78                           "alive-object-data",
79                           dd,
80                           disconnect_func);
81   
82   g_free (with_detail);
83 }
84
85 typedef struct 
86 {
87   GObject *obj;
88   gchar *prop;
89   gint modified_id;
90 } ObjectProperty;
91
92 static void
93 free_object_property (ObjectProperty *p)
94 {
95   g_free (p->prop);
96   g_free (p);
97 }
98
99 static void
100 connect_controller (GObject *controller,
101                     const gchar *signal,
102                     GObject *model,
103                     const gchar *prop_name,
104                     GtkSignalFunc func)
105 {
106   ObjectProperty *p;
107
108   p = g_new (ObjectProperty, 1);
109   p->obj = model;
110   p->prop = g_strdup (prop_name);
111
112   p->modified_id = g_signal_connect_data (controller, signal, func, p,
113                                           (GClosureNotify)free_object_property,
114                                           0);
115   g_object_set_data (controller, "object-property", p);
116 }
117
118 static void
119 block_controller (GObject *controller)
120 {
121   ObjectProperty *p = g_object_get_data (controller, "object-property");
122
123   if (p)
124     g_signal_handler_block (controller, p->modified_id);
125 }
126
127 static void
128 unblock_controller (GObject *controller)
129 {
130   ObjectProperty *p = g_object_get_data (controller, "object-property");
131
132   if (p)
133     g_signal_handler_unblock (controller, p->modified_id);
134 }
135
136 static void
137 int_modified (GtkAdjustment *adj, gpointer data)
138 {
139   ObjectProperty *p = data;
140
141   g_object_set (p->obj, p->prop, (int) adj->value, NULL);
142 }
143
144 static void
145 int_changed (GObject *object, GParamSpec *pspec, gpointer data)
146 {
147   GtkAdjustment *adj = GTK_ADJUSTMENT (data);
148   GValue val = { 0, };  
149
150   g_value_init (&val, G_TYPE_INT);
151   g_object_get_property (object, pspec->name, &val);
152
153   if (g_value_get_int (&val) != (int)adj->value)
154     {
155       block_controller (G_OBJECT (adj));
156       gtk_adjustment_set_value (adj, g_value_get_int (&val));
157       unblock_controller (G_OBJECT (adj));
158     }
159
160   g_value_unset (&val);
161 }
162
163 static void
164 uint_modified (GtkAdjustment *adj, gpointer data)
165 {
166   ObjectProperty *p = data;
167
168   g_object_set (p->obj, p->prop, (guint) adj->value, NULL);
169 }
170
171 static void
172 uint_changed (GObject *object, GParamSpec *pspec, gpointer data)
173 {
174   GtkAdjustment *adj = GTK_ADJUSTMENT (data);
175   GValue val = { 0, };  
176
177   g_value_init (&val, G_TYPE_UINT);
178   g_object_get_property (object, pspec->name, &val);
179
180   if (g_value_get_uint (&val) != (guint)adj->value)
181     {
182       block_controller (G_OBJECT (adj));
183       gtk_adjustment_set_value (adj, g_value_get_uint (&val));
184       unblock_controller (G_OBJECT (adj));
185     }
186
187   g_value_unset (&val);
188 }
189
190 static void
191 float_modified (GtkAdjustment *adj, gpointer data)
192 {
193   ObjectProperty *p = data;
194
195   g_object_set (p->obj, p->prop, (float) adj->value, NULL);
196 }
197
198 static void
199 float_changed (GObject *object, GParamSpec *pspec, gpointer data)
200 {
201   GtkAdjustment *adj = GTK_ADJUSTMENT (data);
202   GValue val = { 0, };  
203
204   g_value_init (&val, G_TYPE_FLOAT);
205   g_object_get_property (object, pspec->name, &val);
206
207   if (g_value_get_float (&val) != (float) adj->value)
208     {
209       block_controller (G_OBJECT (adj));
210       gtk_adjustment_set_value (adj, g_value_get_float (&val));
211       unblock_controller (G_OBJECT (adj));
212     }
213
214   g_value_unset (&val);
215 }
216
217 static void
218 double_modified (GtkAdjustment *adj, gpointer data)
219 {
220   ObjectProperty *p = data;
221
222   g_object_set (p->obj, p->prop, (double) adj->value, NULL);
223 }
224
225 static void
226 double_changed (GObject *object, GParamSpec *pspec, gpointer data)
227 {
228   GtkAdjustment *adj = GTK_ADJUSTMENT (data);
229   GValue val = { 0, };  
230
231   g_value_init (&val, G_TYPE_DOUBLE);
232   g_object_get_property (object, pspec->name, &val);
233
234   if (g_value_get_double (&val) != adj->value)
235     {
236       block_controller (G_OBJECT (adj));
237       gtk_adjustment_set_value (adj, g_value_get_double (&val));
238       unblock_controller (G_OBJECT (adj));
239     }
240
241   g_value_unset (&val);
242 }
243
244 static void
245 string_modified (GtkEntry *entry, gpointer data)
246 {
247   ObjectProperty *p = data;
248   const gchar *text;
249
250   text = gtk_entry_get_text (entry);
251
252   g_object_set (p->obj, p->prop, text, NULL);
253 }
254
255 static void
256 string_changed (GObject *object, GParamSpec *pspec, gpointer data)
257 {
258   GtkEntry *entry = GTK_ENTRY (data);
259   GValue val = { 0, };  
260   const gchar *str;
261   const gchar *text;
262   
263   g_value_init (&val, G_TYPE_STRING);
264   g_object_get_property (object, pspec->name, &val);
265
266   str = g_value_get_string (&val);
267   if (str == NULL)
268     str = "";
269   text = gtk_entry_get_text (entry);
270
271   if (strcmp (str, text) != 0)
272     {
273       block_controller (G_OBJECT (entry));
274       gtk_entry_set_text (entry, str);
275       unblock_controller (G_OBJECT (entry));
276     }
277   
278   g_value_unset (&val);
279 }
280
281 static void
282 bool_modified (GtkToggleButton *tb, gpointer data)
283 {
284   ObjectProperty *p = data;
285
286   g_object_set (p->obj, p->prop, (int) tb->active, NULL);
287 }
288
289 static void
290 bool_changed (GObject *object, GParamSpec *pspec, gpointer data)
291 {
292   GtkToggleButton *tb = GTK_TOGGLE_BUTTON (data);
293   GValue val = { 0, };  
294   
295   g_value_init (&val, G_TYPE_BOOLEAN);
296   g_object_get_property (object, pspec->name, &val);
297
298   if (g_value_get_boolean (&val) != tb->active)
299     {
300       block_controller (G_OBJECT (tb));
301       gtk_toggle_button_set_active (tb, g_value_get_boolean (&val));
302       unblock_controller (G_OBJECT (tb));
303     }
304
305   gtk_label_set_text (GTK_LABEL (GTK_BIN (tb)->child), g_value_get_boolean (&val) ?
306                       "TRUE" : "FALSE");
307   
308   g_value_unset (&val);
309 }
310
311
312 static void
313 enum_modified (GtkOptionMenu *om, gpointer data)
314 {
315   ObjectProperty *p = data;
316   gint i;
317   GParamSpec *spec;
318   GEnumClass *eclass;
319   
320   spec = g_object_class_find_property (G_OBJECT_GET_CLASS (p->obj),
321                                        p->prop);
322
323   eclass = G_ENUM_CLASS (g_type_class_peek (spec->value_type));
324   
325   i = gtk_option_menu_get_history (om);
326
327   g_object_set (p->obj, p->prop, eclass->values[i].value, NULL);
328 }
329
330 static void
331 enum_changed (GObject *object, GParamSpec *pspec, gpointer data)
332 {
333   GtkOptionMenu *om = GTK_OPTION_MENU (data);
334   GValue val = { 0, };  
335   GEnumClass *eclass;
336   gint i;
337
338   eclass = G_ENUM_CLASS (g_type_class_peek (pspec->value_type));
339   
340   g_value_init (&val, pspec->value_type);
341   g_object_get_property (object, pspec->name, &val);
342
343   i = 0;
344   while (i < eclass->n_values)
345     {
346       if (eclass->values[i].value == g_value_get_enum (&val))
347         break;
348       ++i;
349     }
350   
351   if (gtk_option_menu_get_history (om) != i)
352     {
353       block_controller (G_OBJECT (om));
354       gtk_option_menu_set_history (om, i);
355       unblock_controller (G_OBJECT (om));
356     }
357   
358   g_value_unset (&val);
359
360 }
361
362 static gunichar
363 unichar_get_value (GtkEntry *entry)
364 {
365   const gchar *text = gtk_entry_get_text (entry);
366   
367   if (text[0])
368     return g_utf8_get_char (text);
369   else
370     return 0;
371 }
372
373 static void
374 unichar_modified (GtkEntry *entry, gpointer data)
375 {
376   ObjectProperty *p = data;
377   gunichar val = unichar_get_value (entry);
378
379   g_object_set (p->obj, p->prop, val, NULL);
380 }
381
382 static void
383 unichar_changed (GObject *object, GParamSpec *pspec, gpointer data)
384 {
385   GtkEntry *entry = GTK_ENTRY (data);
386   gunichar new_val;
387   gunichar old_val = unichar_get_value (entry);
388   gchar buf[7];
389   gint len;
390
391   g_object_get (object, pspec->name, &new_val, NULL);
392
393   if (new_val != old_val)
394     {
395       if (!new_val)
396         len = 0;
397       else
398         len = g_unichar_to_utf8 (new_val, buf);
399       
400       buf[len] = '\0';
401       
402       block_controller (G_OBJECT (entry));
403       gtk_entry_set_text (entry, buf);
404       unblock_controller (G_OBJECT (entry));
405     }
406 }
407
408 static void
409 pointer_changed (GObject *object, GParamSpec *pspec, gpointer data)
410 {
411   GtkLabel *label = GTK_LABEL (data);
412   gchar *str;
413   gpointer ptr;
414   
415   g_object_get (object, pspec->name, &ptr, NULL);
416
417   str = g_strdup_printf ("Pointer: %p", ptr);
418   gtk_label_set_text (label, str);
419   g_free (str);
420 }
421
422 static void
423 object_changed (GObject *object, GParamSpec *pspec, gpointer data)
424 {
425   GtkWidget *label, *button;
426   gchar *str;
427   GObject *obj;
428   const gchar *name;
429   
430   GList *children = gtk_container_get_children (GTK_CONTAINER (data)); 
431   label = GTK_WIDGET (children->data);
432   button = GTK_WIDGET (children->next->data);
433   g_object_get (object, pspec->name, &obj, NULL);
434   g_list_free (children);
435
436   if (obj)
437     name = g_type_name (G_TYPE_FROM_INSTANCE (obj));
438   else
439     name = "unknown";
440   str = g_strdup_printf ("Object: %p (%s)", obj, name);
441   
442   gtk_label_set_text (GTK_LABEL (label), str);
443   gtk_widget_set_sensitive (button, G_IS_OBJECT (obj));
444
445   if (obj)
446     g_object_unref (obj);
447
448   g_free (str);
449 }
450
451 static void
452 model_destroy (gpointer data)
453 {
454   g_object_steal_data (data, "model-object");
455   gtk_widget_destroy (data);
456 }
457
458 static void
459 window_destroy (gpointer data)
460 {
461   g_object_steal_data (data, "prop-editor-win");
462 }
463
464 static void
465 object_properties (GtkWidget *button, 
466                    GObject   *object)
467 {
468   gchar *name;
469   GObject *obj;
470
471   name = (gchar *) g_object_get_data (G_OBJECT (button), "property-name");
472   g_object_get (object, name, &obj, NULL);
473   if (G_IS_OBJECT (obj)) 
474     create_prop_editor (obj, 0);
475 }
476  
477 static GtkWidget *
478 property_widget (GObject *object, GParamSpec *spec, gboolean can_modify)
479 {
480   GtkWidget *prop_edit;
481   GtkAdjustment *adj;
482   gchar *msg;
483   GType type = G_PARAM_SPEC_TYPE (spec);
484
485   if (type == G_TYPE_PARAM_INT)
486     {
487       adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_INT (spec)->default_value,
488                                                 G_PARAM_SPEC_INT (spec)->minimum,
489                                                 G_PARAM_SPEC_INT (spec)->maximum,
490                                                 1,
491                                                 MAX ((G_PARAM_SPEC_INT (spec)->maximum -
492                                                       G_PARAM_SPEC_INT (spec)->minimum) / 10, 1),
493                                                 0.0));
494       
495       prop_edit = gtk_spin_button_new (adj, 1.0, 0);
496       
497       g_object_connect_property (object, spec->name,
498                                  G_CALLBACK (int_changed),
499                                  adj, G_OBJECT (adj));
500       
501       if (can_modify)
502         connect_controller (G_OBJECT (adj), "value_changed",
503                             object, spec->name, (GtkSignalFunc) int_modified);
504     }
505   else if (type == G_TYPE_PARAM_UINT)
506     {
507       adj = GTK_ADJUSTMENT (
508                             gtk_adjustment_new (G_PARAM_SPEC_UINT (spec)->default_value,
509                                                 G_PARAM_SPEC_UINT (spec)->minimum,
510                                                 G_PARAM_SPEC_UINT (spec)->maximum,
511                                                 1,
512                                                 MAX ((G_PARAM_SPEC_UINT (spec)->maximum -
513                                                       G_PARAM_SPEC_UINT (spec)->minimum) / 10, 1),
514                                                 0.0));
515       
516       prop_edit = gtk_spin_button_new (adj, 1.0, 0);
517       
518       g_object_connect_property (object, spec->name,
519                                  G_CALLBACK (uint_changed),
520                                  adj, G_OBJECT (adj));
521       
522       if (can_modify)
523         connect_controller (G_OBJECT (adj), "value_changed",
524                             object, spec->name, (GtkSignalFunc) uint_modified);
525     }
526   else if (type == G_TYPE_PARAM_FLOAT)
527     {
528
529       adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_FLOAT (spec)->default_value,
530                                                 G_PARAM_SPEC_FLOAT (spec)->minimum,
531                                                 G_PARAM_SPEC_FLOAT (spec)->maximum,
532                                                 0.1,
533                                                 MAX ((G_PARAM_SPEC_FLOAT (spec)->maximum -
534                                                       G_PARAM_SPEC_FLOAT (spec)->minimum) / 10, 0.1),
535                                                 0.0));
536       
537       prop_edit = gtk_spin_button_new (adj, 0.1, 2);
538       
539       g_object_connect_property (object, spec->name,
540                                  G_CALLBACK (float_changed),
541                                  adj, G_OBJECT (adj));
542       
543       if (can_modify)
544         connect_controller (G_OBJECT (adj), "value_changed",
545                             object, spec->name, (GtkSignalFunc) float_modified);
546     }
547   else if (type == G_TYPE_PARAM_DOUBLE)
548     {
549       adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_DOUBLE (spec)->default_value,
550                                                 G_PARAM_SPEC_DOUBLE (spec)->minimum,
551                                                 G_PARAM_SPEC_DOUBLE (spec)->maximum,
552                                                 0.1,
553                                                 MAX ((G_PARAM_SPEC_DOUBLE (spec)->maximum -
554                                                       G_PARAM_SPEC_DOUBLE (spec)->minimum) / 10, 0.1),
555                                                 0.0));
556       
557       prop_edit = gtk_spin_button_new (adj, 0.1, 2);
558       
559       g_object_connect_property (object, spec->name,
560                                  G_CALLBACK (double_changed),
561                                  adj, G_OBJECT (adj));
562       
563       if (can_modify)
564         connect_controller (G_OBJECT (adj), "value_changed",
565                             object, spec->name, (GtkSignalFunc) double_modified);
566     }
567   else if (type == G_TYPE_PARAM_STRING)
568     {
569       prop_edit = gtk_entry_new ();
570       
571       g_object_connect_property (object, spec->name,
572                                  G_CALLBACK (string_changed),
573                                  prop_edit, G_OBJECT (prop_edit));
574       
575       if (can_modify)
576         connect_controller (G_OBJECT (prop_edit), "changed",
577                             object, spec->name, (GtkSignalFunc) string_modified);
578     }
579   else if (type == G_TYPE_PARAM_BOOLEAN)
580     {
581       prop_edit = gtk_toggle_button_new_with_label ("");
582       
583       g_object_connect_property (object, spec->name,
584                                  G_CALLBACK (bool_changed),
585                                  prop_edit, G_OBJECT (prop_edit));
586       
587       if (can_modify)
588         connect_controller (G_OBJECT (prop_edit), "toggled",
589                             object, spec->name, (GtkSignalFunc) bool_modified);
590     }
591   else if (type == G_TYPE_PARAM_ENUM)
592     {
593       {
594         GtkWidget *menu;
595         GEnumClass *eclass;
596         gint j;
597         
598         prop_edit = gtk_option_menu_new ();
599         
600         menu = gtk_menu_new ();
601         
602         eclass = G_ENUM_CLASS (g_type_class_ref (spec->value_type));
603         
604         j = 0;
605         while (j < eclass->n_values)
606           {
607             GtkWidget *mi;
608             
609             mi = gtk_menu_item_new_with_label (eclass->values[j].value_name);
610             
611             gtk_widget_show (mi);
612             
613             gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
614             
615             ++j;
616           }
617         
618         g_type_class_unref (eclass);
619         
620         gtk_option_menu_set_menu (GTK_OPTION_MENU (prop_edit), menu);
621         
622         g_object_connect_property (object, spec->name,
623                                    G_CALLBACK (enum_changed),
624                                    prop_edit, G_OBJECT (prop_edit));
625         
626         if (can_modify)
627           connect_controller (G_OBJECT (prop_edit), "changed",
628                               object, spec->name, (GtkSignalFunc) enum_modified);
629       }
630     }
631   else if (type == G_TYPE_PARAM_UNICHAR)
632     {
633       prop_edit = gtk_entry_new ();
634       gtk_entry_set_max_length (GTK_ENTRY (prop_edit), 1);
635       
636       g_object_connect_property (object, spec->name,
637                                  G_CALLBACK (unichar_changed),
638                                  prop_edit, G_OBJECT (prop_edit));
639       
640       if (can_modify)
641         connect_controller (G_OBJECT (prop_edit), "changed",
642                             object, spec->name, (GtkSignalFunc) unichar_modified);
643     }
644   else if (type == G_TYPE_PARAM_POINTER)
645     {
646       prop_edit = gtk_label_new ("");
647       
648       g_object_connect_property (object, spec->name,
649                                  G_CALLBACK (pointer_changed),
650                                  prop_edit, G_OBJECT (prop_edit));
651     }
652   else if (type == G_TYPE_PARAM_OBJECT)
653     {
654       GtkWidget *label, *button;
655
656       prop_edit = gtk_hbox_new (FALSE, 5);
657
658       label = gtk_label_new ("");
659       button = gtk_button_new_with_label ("Properties");
660       g_object_set_data (G_OBJECT (button), "property-name", spec->name);
661       g_signal_connect (button, "clicked", 
662                         G_CALLBACK (object_properties), 
663                         object);
664
665       gtk_container_add (GTK_CONTAINER (prop_edit), label);
666       gtk_container_add (GTK_CONTAINER (prop_edit), button);
667       
668       g_object_connect_property (object, spec->name,
669                                  G_CALLBACK (object_changed),
670                                  prop_edit, G_OBJECT (label));
671     }
672   else
673     {  
674       msg = g_strdup_printf ("uneditable property type: %s",
675                              g_type_name (G_PARAM_SPEC_TYPE (spec)));
676       prop_edit = gtk_label_new (msg);            
677       g_free (msg);
678       gtk_misc_set_alignment (GTK_MISC (prop_edit), 0.0, 0.5);
679     }
680   
681   return prop_edit;
682 }
683
684 static GtkWidget *
685 properties_from_type (GObject     *object,
686                       GType        type,
687                       GtkTooltips *tips)
688 {
689   GtkWidget *prop_edit;
690   GtkWidget *label;
691   GtkWidget *sw;
692   GtkWidget *vbox;
693   GtkWidget *table;
694   GParamSpec **specs;
695   gint n_specs;
696   int i;
697
698   if (G_TYPE_IS_INTERFACE (type))
699     {
700       gpointer vtable = g_type_default_interface_peek (type);
701       specs = g_object_interface_list_properties (vtable, &n_specs);
702     }
703   else
704     {
705       GObjectClass *class = G_OBJECT_CLASS (g_type_class_peek (type));
706       specs = g_object_class_list_properties (class, &n_specs);
707     }
708         
709   if (n_specs == 0)
710     return NULL;
711   
712   table = gtk_table_new (n_specs, 2, FALSE);
713   gtk_table_set_col_spacing (GTK_TABLE (table), 0, 10);
714   gtk_table_set_row_spacings (GTK_TABLE (table), 3);
715
716   i = 0;
717   while (i < n_specs)
718     {
719       GParamSpec *spec = specs[i];
720       gboolean can_modify;
721       
722       prop_edit = NULL;
723
724       can_modify = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
725                     (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
726       
727       if ((spec->flags & G_PARAM_READABLE) == 0)
728         {
729           /* can't display unreadable properties */
730           ++i;
731           continue;
732         }
733       
734       if (spec->owner_type != type)
735         {
736           /* we're only interested in params of type */
737           ++i;
738           continue;
739         }
740
741       label = gtk_label_new (g_param_spec_get_nick (spec));
742       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
743       gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, i, i + 1);
744       
745       prop_edit = property_widget (object, spec, can_modify);
746       gtk_table_attach_defaults (GTK_TABLE (table), prop_edit, 1, 2, i, i + 1);
747
748       if (prop_edit)
749         {
750           if (!can_modify)
751             gtk_widget_set_sensitive (prop_edit, FALSE);
752
753           if (g_param_spec_get_blurb (spec))
754             gtk_tooltips_set_tip (tips, prop_edit, g_param_spec_get_blurb (spec), NULL);
755           
756           /* set initial value */
757           g_object_notify (object, spec->name);
758         }
759       
760       ++i;
761     }
762
763
764   vbox = gtk_vbox_new (FALSE, 0);
765   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
766
767   sw = gtk_scrolled_window_new (NULL, NULL);
768   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
769                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
770   
771   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), vbox);
772
773   g_free (specs);
774
775   return sw;
776 }
777
778 static void
779 kill_tips (GtkWindow *win, GtkObject *tips)
780 {
781   gtk_object_destroy (tips);
782   g_object_unref (tips);
783 }
784
785 /* Pass zero for type if you want all properties */
786 GtkWidget*
787 create_prop_editor (GObject   *object,
788                     GType      type)
789 {
790   GtkWidget *win;
791   GtkWidget *notebook;
792   GtkTooltips *tips;
793   GtkWidget *properties;
794   GtkWidget *label;
795   gchar *title;
796   GType *ifaces;
797   guint n_ifaces;
798   
799   if ((win = g_object_get_data (G_OBJECT (object), "prop-editor-win")))
800     {
801       gtk_window_present (GTK_WINDOW (win));
802       return win;
803     }
804
805   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
806   if (GTK_IS_WIDGET (object))
807     gtk_window_set_screen (GTK_WINDOW (win),
808                            gtk_widget_get_screen (GTK_WIDGET (object)));
809
810   tips = gtk_tooltips_new ();
811   g_object_ref (tips);
812   gtk_object_sink (GTK_OBJECT (tips));
813
814   /* Kill the tips when the widget goes away.  */
815   g_signal_connect (G_OBJECT (win), "destroy", G_CALLBACK (kill_tips), tips);
816
817   /* hold a weak ref to the object we're editing */
818   g_object_set_data_full (G_OBJECT (object), "prop-editor-win", win, model_destroy);
819   g_object_set_data_full (G_OBJECT (win), "model-object", object, window_destroy);
820
821   if (type == 0)
822     {
823       notebook = gtk_notebook_new ();
824       gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);
825       
826       gtk_container_add (GTK_CONTAINER (win), notebook);
827       
828       type = G_TYPE_FROM_INSTANCE (object);
829
830       title = g_strdup_printf ("Properties of %s widget", g_type_name (type));
831       gtk_window_set_title (GTK_WINDOW (win), title);
832       g_free (title);
833       
834       while (type)
835         {
836           properties = properties_from_type (object, type, tips);
837           if (properties)
838             {
839               label = gtk_label_new (g_type_name (type));
840               gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
841                                         properties, label);
842             }
843           
844           type = g_type_parent (type);
845         }
846
847       ifaces = g_type_interfaces (G_TYPE_FROM_INSTANCE (object), &n_ifaces);
848       while (n_ifaces--)
849         {
850           properties = properties_from_type (object, ifaces[n_ifaces], tips);
851           if (properties)
852             {
853               label = gtk_label_new (g_type_name (ifaces[n_ifaces]));
854               gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
855                                         properties, label);
856             }
857         }
858
859       g_free (ifaces);
860     }
861   else
862     {
863       properties = properties_from_type (object, type, tips);
864       gtk_container_add (GTK_CONTAINER (win), properties);
865       title = g_strdup_printf ("Properties of %s", g_type_name (type));
866       gtk_window_set_title (GTK_WINDOW (win), title);
867       g_free (title);
868     }
869   
870   gtk_window_set_default_size (GTK_WINDOW (win), -1, 400);
871   
872   gtk_widget_show_all (win);
873
874   return win;
875 }
876