]> Pileus Git - ~andy/gtk/blob - tests/prop-editor.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~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   g_free (str);
446 }
447
448 static void
449 model_destroy (gpointer data)
450 {
451   g_object_steal_data (data, "model-object");
452   gtk_widget_destroy (data);
453 }
454
455 static void
456 window_destroy (gpointer data)
457 {
458   g_object_steal_data (data, "prop-editor-win");
459 }
460
461 static void
462 object_properties (GtkWidget *button, 
463                    GObject   *object)
464 {
465   gchar *name;
466   GObject *obj;
467
468   name = (gchar *) g_object_get_data (G_OBJECT (button), "property-name");
469   g_object_get (object, name, &obj, NULL);
470   if (G_IS_OBJECT (obj)) 
471     create_prop_editor (obj, 0);
472 }
473  
474 static GtkWidget *
475 property_widget (GObject *object, GParamSpec *spec, gboolean can_modify)
476 {
477   GtkWidget *prop_edit;
478   GtkAdjustment *adj;
479   gchar *msg;
480   GType type = G_PARAM_SPEC_TYPE (spec);
481
482   if (type == G_TYPE_PARAM_INT)
483     {
484       adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_INT (spec)->default_value,
485                                                 G_PARAM_SPEC_INT (spec)->minimum,
486                                                 G_PARAM_SPEC_INT (spec)->maximum,
487                                                 1,
488                                                 MAX ((G_PARAM_SPEC_INT (spec)->maximum -
489                                                       G_PARAM_SPEC_INT (spec)->minimum) / 10, 1),
490                                                 0.0));
491       
492       prop_edit = gtk_spin_button_new (adj, 1.0, 0);
493       
494       g_object_connect_property (object, spec->name,
495                                  G_CALLBACK (int_changed),
496                                  adj, G_OBJECT (adj));
497       
498       if (can_modify)
499         connect_controller (G_OBJECT (adj), "value_changed",
500                             object, spec->name, (GtkSignalFunc) int_modified);
501     }
502   else if (type == G_TYPE_PARAM_UINT)
503     {
504       adj = GTK_ADJUSTMENT (
505                             gtk_adjustment_new (G_PARAM_SPEC_UINT (spec)->default_value,
506                                                 G_PARAM_SPEC_UINT (spec)->minimum,
507                                                 G_PARAM_SPEC_UINT (spec)->maximum,
508                                                 1,
509                                                 MAX ((G_PARAM_SPEC_UINT (spec)->maximum -
510                                                       G_PARAM_SPEC_UINT (spec)->minimum) / 10, 1),
511                                                 0.0));
512       
513       prop_edit = gtk_spin_button_new (adj, 1.0, 0);
514       
515       g_object_connect_property (object, spec->name,
516                                  G_CALLBACK (uint_changed),
517                                  adj, G_OBJECT (adj));
518       
519       if (can_modify)
520         connect_controller (G_OBJECT (adj), "value_changed",
521                             object, spec->name, (GtkSignalFunc) uint_modified);
522     }
523   else if (type == G_TYPE_PARAM_FLOAT)
524     {
525
526       adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_FLOAT (spec)->default_value,
527                                                 G_PARAM_SPEC_FLOAT (spec)->minimum,
528                                                 G_PARAM_SPEC_FLOAT (spec)->maximum,
529                                                 0.1,
530                                                 MAX ((G_PARAM_SPEC_FLOAT (spec)->maximum -
531                                                       G_PARAM_SPEC_FLOAT (spec)->minimum) / 10, 0.1),
532                                                 0.0));
533       
534       prop_edit = gtk_spin_button_new (adj, 0.1, 2);
535       
536       g_object_connect_property (object, spec->name,
537                                  G_CALLBACK (float_changed),
538                                  adj, G_OBJECT (adj));
539       
540       if (can_modify)
541         connect_controller (G_OBJECT (adj), "value_changed",
542                             object, spec->name, (GtkSignalFunc) float_modified);
543     }
544   else if (type == G_TYPE_PARAM_DOUBLE)
545     {
546       adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_DOUBLE (spec)->default_value,
547                                                 G_PARAM_SPEC_DOUBLE (spec)->minimum,
548                                                 G_PARAM_SPEC_DOUBLE (spec)->maximum,
549                                                 0.1,
550                                                 MAX ((G_PARAM_SPEC_DOUBLE (spec)->maximum -
551                                                       G_PARAM_SPEC_DOUBLE (spec)->minimum) / 10, 0.1),
552                                                 0.0));
553       
554       prop_edit = gtk_spin_button_new (adj, 0.1, 2);
555       
556       g_object_connect_property (object, spec->name,
557                                  G_CALLBACK (double_changed),
558                                  adj, G_OBJECT (adj));
559       
560       if (can_modify)
561         connect_controller (G_OBJECT (adj), "value_changed",
562                             object, spec->name, (GtkSignalFunc) double_modified);
563     }
564   else if (type == G_TYPE_PARAM_STRING)
565     {
566       prop_edit = gtk_entry_new ();
567       
568       g_object_connect_property (object, spec->name,
569                                  G_CALLBACK (string_changed),
570                                  prop_edit, G_OBJECT (prop_edit));
571       
572       if (can_modify)
573         connect_controller (G_OBJECT (prop_edit), "changed",
574                             object, spec->name, (GtkSignalFunc) string_modified);
575     }
576   else if (type == G_TYPE_PARAM_BOOLEAN)
577     {
578       prop_edit = gtk_toggle_button_new_with_label ("");
579       
580       g_object_connect_property (object, spec->name,
581                                  G_CALLBACK (bool_changed),
582                                  prop_edit, G_OBJECT (prop_edit));
583       
584       if (can_modify)
585         connect_controller (G_OBJECT (prop_edit), "toggled",
586                             object, spec->name, (GtkSignalFunc) bool_modified);
587     }
588   else if (type == G_TYPE_PARAM_ENUM)
589     {
590       {
591         GtkWidget *menu;
592         GEnumClass *eclass;
593         gint j;
594         
595         prop_edit = gtk_option_menu_new ();
596         
597         menu = gtk_menu_new ();
598         
599         eclass = G_ENUM_CLASS (g_type_class_ref (spec->value_type));
600         
601         j = 0;
602         while (j < eclass->n_values)
603           {
604             GtkWidget *mi;
605             
606             mi = gtk_menu_item_new_with_label (eclass->values[j].value_name);
607             
608             gtk_widget_show (mi);
609             
610             gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
611             
612             ++j;
613           }
614         
615         g_type_class_unref (eclass);
616         
617         gtk_option_menu_set_menu (GTK_OPTION_MENU (prop_edit), menu);
618         
619         g_object_connect_property (object, spec->name,
620                                    G_CALLBACK (enum_changed),
621                                    prop_edit, G_OBJECT (prop_edit));
622         
623         if (can_modify)
624           connect_controller (G_OBJECT (prop_edit), "changed",
625                               object, spec->name, (GtkSignalFunc) enum_modified);
626       }
627     }
628   else if (type == G_TYPE_PARAM_UNICHAR)
629     {
630       prop_edit = gtk_entry_new ();
631       gtk_entry_set_max_length (GTK_ENTRY (prop_edit), 1);
632       
633       g_object_connect_property (object, spec->name,
634                                  G_CALLBACK (unichar_changed),
635                                  prop_edit, G_OBJECT (prop_edit));
636       
637       if (can_modify)
638         connect_controller (G_OBJECT (prop_edit), "changed",
639                             object, spec->name, (GtkSignalFunc) unichar_modified);
640     }
641   else if (type == G_TYPE_PARAM_POINTER)
642     {
643       prop_edit = gtk_label_new ("");
644       
645       g_object_connect_property (object, spec->name,
646                                  G_CALLBACK (pointer_changed),
647                                  prop_edit, G_OBJECT (prop_edit));
648     }
649   else if (type == G_TYPE_PARAM_OBJECT)
650     {
651       GtkWidget *label, *button;
652
653       prop_edit = gtk_hbox_new (FALSE, 5);
654
655       label = gtk_label_new ("");
656       button = gtk_button_new_with_label ("Properties");
657       g_object_set_data (G_OBJECT (button), "property-name", spec->name);
658       g_signal_connect (button, "clicked", 
659                         G_CALLBACK (object_properties), 
660                         object);
661
662       gtk_container_add (GTK_CONTAINER (prop_edit), label);
663       gtk_container_add (GTK_CONTAINER (prop_edit), button);
664       
665       g_object_connect_property (object, spec->name,
666                                  G_CALLBACK (object_changed),
667                                  prop_edit, G_OBJECT (label));
668     }
669   else
670     {  
671       msg = g_strdup_printf ("uneditable property type: %s",
672                              g_type_name (G_PARAM_SPEC_TYPE (spec)));
673       prop_edit = gtk_label_new (msg);            
674       g_free (msg);
675       gtk_misc_set_alignment (GTK_MISC (prop_edit), 0.0, 0.5);
676     }
677   
678   return prop_edit;
679 }
680
681 static GtkWidget *
682 properties_from_type (GObject     *object,
683                       GType        type,
684                       GtkTooltips *tips)
685 {
686   GtkWidget *prop_edit;
687   GtkWidget *label;
688   GtkWidget *sw;
689   GtkWidget *vbox;
690   GtkWidget *table;
691   GParamSpec **specs;
692   gint n_specs;
693   int i;
694
695   if (G_TYPE_IS_INTERFACE (type))
696     {
697       gpointer vtable = g_type_default_interface_peek (type);
698       specs = g_object_interface_list_properties (vtable, &n_specs);
699     }
700   else
701     {
702       GObjectClass *class = G_OBJECT_CLASS (g_type_class_peek (type));
703       specs = g_object_class_list_properties (class, &n_specs);
704     }
705         
706   if (n_specs == 0)
707     return NULL;
708   
709   table = gtk_table_new (n_specs, 2, FALSE);
710   gtk_table_set_col_spacing (GTK_TABLE (table), 0, 10);
711   gtk_table_set_row_spacings (GTK_TABLE (table), 3);
712
713   i = 0;
714   while (i < n_specs)
715     {
716       GParamSpec *spec = specs[i];
717       gboolean can_modify;
718       
719       prop_edit = NULL;
720
721       can_modify = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
722                     (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
723       
724       if ((spec->flags & G_PARAM_READABLE) == 0)
725         {
726           /* can't display unreadable properties */
727           ++i;
728           continue;
729         }
730       
731       if (spec->owner_type != type)
732         {
733           /* we're only interested in params of type */
734           ++i;
735           continue;
736         }
737
738       label = gtk_label_new (g_param_spec_get_nick (spec));
739       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
740       gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, i, i + 1);
741       
742       prop_edit = property_widget (object, spec, can_modify);
743       gtk_table_attach_defaults (GTK_TABLE (table), prop_edit, 1, 2, i, i + 1);
744
745       if (prop_edit)
746         {
747           if (!can_modify)
748             gtk_widget_set_sensitive (prop_edit, FALSE);
749
750           if (g_param_spec_get_blurb (spec))
751             gtk_tooltips_set_tip (tips, prop_edit, g_param_spec_get_blurb (spec), NULL);
752           
753           /* set initial value */
754           g_object_notify (object, spec->name);
755         }
756       
757       ++i;
758     }
759
760
761   vbox = gtk_vbox_new (FALSE, 0);
762   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
763
764   sw = gtk_scrolled_window_new (NULL, NULL);
765   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
766                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
767   
768   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), vbox);
769
770   g_free (specs);
771
772   return sw;
773 }
774
775
776 /* Pass zero for type if you want all properties */
777 GtkWidget*
778 create_prop_editor (GObject   *object,
779                     GType      type)
780 {
781   GtkWidget *win;
782   GtkWidget *notebook;
783   GtkTooltips *tips;
784   GtkWidget *properties;
785   GtkWidget *label;
786   gchar *title;
787   GType *ifaces;
788   guint n_ifaces;
789   
790   if ((win = g_object_get_data (G_OBJECT (object), "prop-editor-win")))
791     {
792       gtk_window_present (GTK_WINDOW (win));
793       return win;
794     }
795
796   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
797   if (GTK_IS_WIDGET (object))
798     gtk_window_set_screen (GTK_WINDOW (win),
799                            gtk_widget_get_screen (GTK_WIDGET (object)));
800   
801   tips = gtk_tooltips_new ();
802   g_signal_connect_swapped (win, "destroy",
803                             G_CALLBACK (gtk_object_destroy), tips);
804
805   /* hold a weak ref to the object we're editing */
806   g_object_set_data_full (G_OBJECT (object), "prop-editor-win", win, model_destroy);
807   g_object_set_data_full (G_OBJECT (win), "model-object", object, window_destroy);
808
809   if (type == 0)
810     {
811       notebook = gtk_notebook_new ();
812       gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);
813       
814       gtk_container_add (GTK_CONTAINER (win), notebook);
815       
816       type = G_TYPE_FROM_INSTANCE (object);
817
818       title = g_strdup_printf ("Properties of %s widget", g_type_name (type));
819       gtk_window_set_title (GTK_WINDOW (win), title);
820       g_free (title);
821       
822       while (type)
823         {
824           properties = properties_from_type (object, type, tips);
825           if (properties)
826             {
827               label = gtk_label_new (g_type_name (type));
828               gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
829                                         properties, label);
830             }
831           
832           type = g_type_parent (type);
833         }
834
835       ifaces = g_type_interfaces (G_TYPE_FROM_INSTANCE (object), &n_ifaces);
836       while (n_ifaces--)
837         {
838           properties = properties_from_type (object, ifaces[n_ifaces], tips);
839           if (properties)
840             {
841               label = gtk_label_new (g_type_name (ifaces[n_ifaces]));
842               gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
843                                         properties, label);
844             }
845         }
846
847       g_free (ifaces);
848     }
849   else
850     {
851       properties = properties_from_type (object, type, tips);
852       gtk_container_add (GTK_CONTAINER (win), properties);
853       title = g_strdup_printf ("Properties of %s", g_type_name (type));
854       gtk_window_set_title (GTK_WINDOW (win), title);
855       g_free (title);
856     }
857   
858   gtk_window_set_default_size (GTK_WINDOW (win), -1, 400);
859   
860   gtk_widget_show_all (win);
861
862   return win;
863 }
864