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