prop-editor.c 33.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* prop-editor.c
 * Copyright (C) 2000  Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <string.h>

22
#undef GTK_DISABLE_DEPRECATED
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <gtk/gtk.h>

#include "prop-editor.h"


typedef struct
{
  gpointer instance;
  GObject *alive_object;
  guint id;
} DisconnectData;

static void
disconnect_func (gpointer data)
{
  DisconnectData *dd = data;
  
  g_signal_handler_disconnect (dd->instance, dd->id);
}

static void
signal_removed (gpointer  data,
		GClosure *closure)
{
  DisconnectData *dd = data;

  g_object_steal_data (dd->alive_object, "alive-object-data");
  g_free (dd);
}

53 54 55 56 57 58
static gboolean
is_child_property (GParamSpec *pspec)
{
  return g_param_spec_get_qdata (pspec, g_quark_from_string ("is-child-prop")) != NULL;
}

59
static void
60 61 62 63 64 65 66 67 68 69 70 71
mark_child_property (GParamSpec *pspec)
{
  g_param_spec_set_qdata (pspec, g_quark_from_string ("is-child-prop"), 
			  GINT_TO_POINTER (TRUE));
}

static void
g_object_connect_property (GObject     *object,
			   GParamSpec  *spec,
                           GCallback    func,
                           gpointer     data,
                           GObject     *alive_object)
72 73
{
  GClosure *closure;
74
  gchar *with_detail;
75 76
  DisconnectData *dd;

77 78 79 80 81
  if (is_child_property (spec))
    with_detail = g_strconcat ("child-notify::", spec->name, NULL);
  else
    with_detail = g_strconcat ("notify::", spec->name, NULL);

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  dd = g_new (DisconnectData, 1);

  closure = g_cclosure_new (func, data, NULL);

  g_closure_add_invalidate_notifier (closure, dd, signal_removed);

  dd->id = g_signal_connect_closure (object, with_detail,
				     closure, FALSE);

  dd->instance = object;
  dd->alive_object = alive_object;
  
  g_object_set_data_full (G_OBJECT (alive_object),
                          "alive-object-data",
                          dd,
                          disconnect_func);
  
  g_free (with_detail);
}

typedef struct 
{
  GObject *obj;
105
  GParamSpec *spec;
106
  gint modified_id;
107 108 109 110 111 112 113 114 115
} ObjectProperty;

static void
free_object_property (ObjectProperty *p)
{
  g_free (p);
}

static void
116 117 118 119 120
connect_controller (GObject       *controller,
                    const gchar   *signal,
                    GObject       *model,
		    GParamSpec    *spec,
                    GtkSignalFunc  func)
121 122 123 124 125
{
  ObjectProperty *p;

  p = g_new (ObjectProperty, 1);
  p->obj = model;
126
  p->spec = spec;
127

128 129
  p->modified_id = g_signal_connect_data (controller, signal, func, p,
					  (GClosureNotify)free_object_property,
130
					  0);
131 132 133 134 135 136 137 138
  g_object_set_data (controller, "object-property", p);
}

static void
block_controller (GObject *controller)
{
  ObjectProperty *p = g_object_get_data (controller, "object-property");

139 140
  if (p)
    g_signal_handler_block (controller, p->modified_id);
141 142 143 144 145 146 147
}

static void
unblock_controller (GObject *controller)
{
  ObjectProperty *p = g_object_get_data (controller, "object-property");

148 149
  if (p)
    g_signal_handler_unblock (controller, p->modified_id);
150 151 152 153 154 155 156
}

static void
int_modified (GtkAdjustment *adj, gpointer data)
{
  ObjectProperty *p = data;

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, (int) adj->value, NULL);
    }
  else
    g_object_set (p->obj, p->spec->name, (int) adj->value, NULL);
}

static void
get_property_value (GObject *object, GParamSpec *pspec, GValue *value)
{
  if (is_child_property (pspec))
    {
      GtkWidget *widget = GTK_WIDGET (object);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_get_property (GTK_CONTAINER (parent),
					widget, pspec->name, value);
    }
  else
    g_object_get_property (object, pspec->name, value);
182 183 184 185 186 187 188 189 190
}

static void
int_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkAdjustment *adj = GTK_ADJUSTMENT (data);
  GValue val = { 0, };  

  g_value_init (&val, G_TYPE_INT);
191 192

  get_property_value (object, pspec, &val);
193 194

  if (g_value_get_int (&val) != (int)adj->value)
195 196 197 198 199
    {
      block_controller (G_OBJECT (adj));
      gtk_adjustment_set_value (adj, g_value_get_int (&val));
      unblock_controller (G_OBJECT (adj));
    }
200 201 202 203

  g_value_unset (&val);
}

204 205 206 207 208
static void
uint_modified (GtkAdjustment *adj, gpointer data)
{
  ObjectProperty *p = data;

209 210 211 212 213 214 215 216 217 218
  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, (guint) adj->value, NULL);
    }
  else
    g_object_set (p->obj, p->spec->name, (guint) adj->value, NULL);
219 220 221 222 223 224 225 226 227
}

static void
uint_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkAdjustment *adj = GTK_ADJUSTMENT (data);
  GValue val = { 0, };  

  g_value_init (&val, G_TYPE_UINT);
228
  get_property_value (object, pspec, &val);
229 230 231 232 233 234 235 236 237 238 239

  if (g_value_get_uint (&val) != (guint)adj->value)
    {
      block_controller (G_OBJECT (adj));
      gtk_adjustment_set_value (adj, g_value_get_uint (&val));
      unblock_controller (G_OBJECT (adj));
    }

  g_value_unset (&val);
}

240 241 242 243 244
static void
float_modified (GtkAdjustment *adj, gpointer data)
{
  ObjectProperty *p = data;

245 246 247 248 249 250 251 252 253 254
  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, (float) adj->value, NULL);
    }
  else
    g_object_set (p->obj, p->spec->name, (float) adj->value, NULL);
255 256 257 258 259 260 261 262 263
}

static void
float_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkAdjustment *adj = GTK_ADJUSTMENT (data);
  GValue val = { 0, };  

  g_value_init (&val, G_TYPE_FLOAT);
264
  get_property_value (object, pspec, &val);
265 266

  if (g_value_get_float (&val) != (float) adj->value)
267 268 269 270 271
    {
      block_controller (G_OBJECT (adj));
      gtk_adjustment_set_value (adj, g_value_get_float (&val));
      unblock_controller (G_OBJECT (adj));
    }
272 273 274 275

  g_value_unset (&val);
}

276 277 278 279 280
static void
double_modified (GtkAdjustment *adj, gpointer data)
{
  ObjectProperty *p = data;

281 282 283 284 285 286 287 288 289 290
  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, (double) adj->value, NULL);
    }
  else
    g_object_set (p->obj, p->spec->name, (double) adj->value, NULL);
291 292 293 294 295 296 297 298 299
}

static void
double_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkAdjustment *adj = GTK_ADJUSTMENT (data);
  GValue val = { 0, };  

  g_value_init (&val, G_TYPE_DOUBLE);
300
  get_property_value (object, pspec, &val);
301 302 303 304 305 306 307 308 309 310 311

  if (g_value_get_double (&val) != adj->value)
    {
      block_controller (G_OBJECT (adj));
      gtk_adjustment_set_value (adj, g_value_get_double (&val));
      unblock_controller (G_OBJECT (adj));
    }

  g_value_unset (&val);
}

312 313 314 315 316 317 318 319
static void
string_modified (GtkEntry *entry, gpointer data)
{
  ObjectProperty *p = data;
  const gchar *text;

  text = gtk_entry_get_text (entry);

320 321 322 323 324 325 326 327 328 329
  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, text, NULL);
    }
  else
    g_object_set (p->obj, p->spec->name, text, NULL);
330 331 332 333 334 335 336 337 338 339 340
}

static void
string_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkEntry *entry = GTK_ENTRY (data);
  GValue val = { 0, };  
  const gchar *str;
  const gchar *text;
  
  g_value_init (&val, G_TYPE_STRING);
341
  get_property_value (object, pspec, &val);
342 343 344 345 346 347 348

  str = g_value_get_string (&val);
  if (str == NULL)
    str = "";
  text = gtk_entry_get_text (entry);

  if (strcmp (str, text) != 0)
349 350 351 352 353
    {
      block_controller (G_OBJECT (entry));
      gtk_entry_set_text (entry, str);
      unblock_controller (G_OBJECT (entry));
    }
354 355 356 357 358 359 360 361 362
  
  g_value_unset (&val);
}

static void
bool_modified (GtkToggleButton *tb, gpointer data)
{
  ObjectProperty *p = data;

363 364 365 366 367 368 369 370 371 372
  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, (int) tb->active, NULL);
    }
  else
    g_object_set (p->obj, p->spec->name, (int) tb->active, NULL);
373 374 375 376 377 378 379 380 381
}

static void
bool_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkToggleButton *tb = GTK_TOGGLE_BUTTON (data);
  GValue val = { 0, };  
  
  g_value_init (&val, G_TYPE_BOOLEAN);
382
  get_property_value (object, pspec, &val);
383 384

  if (g_value_get_boolean (&val) != tb->active)
385 386 387 388 389
    {
      block_controller (G_OBJECT (tb));
      gtk_toggle_button_set_active (tb, g_value_get_boolean (&val));
      unblock_controller (G_OBJECT (tb));
    }
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

  gtk_label_set_text (GTK_LABEL (GTK_BIN (tb)->child), g_value_get_boolean (&val) ?
                      "TRUE" : "FALSE");
  
  g_value_unset (&val);
}


static void
enum_modified (GtkOptionMenu *om, gpointer data)
{
  ObjectProperty *p = data;
  gint i;
  GEnumClass *eclass;
  
405
  eclass = G_ENUM_CLASS (g_type_class_peek (p->spec->value_type));
406 407
  
  i = gtk_option_menu_get_history (om);
408 409 410 411 412
  
  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);
413

414 415 416 417 418
      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, eclass->values[i].value, NULL);
    }
  else
    g_object_set (p->obj, p->spec->name, eclass->values[i].value, NULL);
419 420 421 422 423 424 425 426 427 428 429 430 431
}

static void
enum_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkOptionMenu *om = GTK_OPTION_MENU (data);
  GValue val = { 0, };  
  GEnumClass *eclass;
  gint i;

  eclass = G_ENUM_CLASS (g_type_class_peek (pspec->value_type));
  
  g_value_init (&val, pspec->value_type);
432
  get_property_value (object, pspec, &val);
433 434 435 436 437 438 439 440 441 442

  i = 0;
  while (i < eclass->n_values)
    {
      if (eclass->values[i].value == g_value_get_enum (&val))
        break;
      ++i;
    }
  
  if (gtk_option_menu_get_history (om) != i)
443 444 445 446 447
    {
      block_controller (G_OBJECT (om));
      gtk_option_menu_set_history (om, i);
      unblock_controller (G_OBJECT (om));
    }
448 449 450 451 452
  
  g_value_unset (&val);

}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
static void
flags_modified (GtkCheckButton *button, gpointer data)
{
  ObjectProperty *p = data;
  gboolean active;
  GFlagsClass *fclass;
  guint flags;
  gint i;
  
  fclass = G_FLAGS_CLASS (g_type_class_peek (p->spec->value_type));
  
  active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
  i = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button), "index"));

  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_get (GTK_CONTAINER (parent), 
			       widget, p->spec->name, &flags, NULL);
      if (active)
        flags |= fclass->values[i].value;
      else
        flags &= ~fclass->values[i].value;

      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, flags, NULL);
    }
  else
    {
      g_object_get (p->obj, p->spec->name, &flags, NULL);

      if (active)
        flags |= fclass->values[i].value;
      else
        flags &= ~fclass->values[i].value;

      g_object_set (p->obj, p->spec->name, flags, NULL);
    }
}

static void
flags_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GList *children, *c;
  GValue val = { 0, };  
  GFlagsClass *fclass;
  guint flags;
  gint i;

  fclass = G_FLAGS_CLASS (g_type_class_peek (pspec->value_type));
  
  g_value_init (&val, pspec->value_type);
  get_property_value (object, pspec, &val);
  flags = g_value_get_flags (&val);
  g_value_unset (&val);

  children = gtk_container_get_children (GTK_CONTAINER (data));

  for (c = children, i = 0; c; c = c->next, i++)
    {
      block_controller (G_OBJECT (c->data));
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (c->data),
                                    (fclass->values[i].value & flags) != 0);
      unblock_controller (G_OBJECT (c->data));
    }

  g_list_free (children);
}

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
static gunichar
unichar_get_value (GtkEntry *entry)
{
  const gchar *text = gtk_entry_get_text (entry);
  
  if (text[0])
    return g_utf8_get_char (text);
  else
    return 0;
}

static void
unichar_modified (GtkEntry *entry, gpointer data)
{
  ObjectProperty *p = data;
  gunichar val = unichar_get_value (entry);

541 542 543 544 545 546 547 548 549 550
  if (is_child_property (p->spec))
    {
      GtkWidget *widget = GTK_WIDGET (p->obj);
      GtkWidget *parent = gtk_widget_get_parent (widget);

      gtk_container_child_set (GTK_CONTAINER (parent), 
			       widget, p->spec->name, val, NULL);
    }
  else
    g_object_set (p->obj, p->spec->name, val, NULL);
551 552 553 554 555 556 557 558
}

static void
unichar_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkEntry *entry = GTK_ENTRY (data);
  gunichar new_val;
  gunichar old_val = unichar_get_value (entry);
559
  GValue val = { 0, };
560 561
  gchar buf[7];
  gint len;
562 563 564 565
  
  g_value_init (&val, pspec->value_type);
  get_property_value (object, pspec, &val);
  new_val = (gunichar)g_value_get_uint (&val);
566 567 568 569 570 571 572 573 574 575

  if (new_val != old_val)
    {
      if (!new_val)
	len = 0;
      else
	len = g_unichar_to_utf8 (new_val, buf);
      
      buf[len] = '\0';
      
576
      block_controller (G_OBJECT (entry));
577
      gtk_entry_set_text (entry, buf);
578
      unblock_controller (G_OBJECT (entry));
579 580 581
    }
}

582 583 584 585 586 587 588 589 590 591 592 593 594 595
static void
pointer_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkLabel *label = GTK_LABEL (data);
  gchar *str;
  gpointer ptr;
  
  g_object_get (object, pspec->name, &ptr, NULL);

  str = g_strdup_printf ("Pointer: %p", ptr);
  gtk_label_set_text (label, str);
  g_free (str);
}

596 597 598 599 600 601 602 603 604 605 606 607
gchar *
object_label (GObject *obj)
{
  const gchar *name;

  if (obj)
    name = g_type_name (G_TYPE_FROM_INSTANCE (obj));
  else
    name = "unknown";
  return g_strdup_printf ("Object: %p (%s)", obj, name);
}

608 609 610
static void
object_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
611
  GtkWidget *label, *button;
612 613 614
  gchar *str;
  GObject *obj;
  
615 616 617
  GList *children = gtk_container_get_children (GTK_CONTAINER (data)); 
  label = GTK_WIDGET (children->data);
  button = GTK_WIDGET (children->next->data);
618
  g_object_get (object, pspec->name, &obj, NULL);
619
  g_list_free (children);
620

621
  str = object_label (obj);
622
  
623 624 625
  gtk_label_set_text (GTK_LABEL (label), str);
  gtk_widget_set_sensitive (button, G_IS_OBJECT (obj));

Morten Welinder's avatar
Morten Welinder committed
626 627 628
  if (obj)
    g_object_unref (obj);

629 630 631
  g_free (str);
}

632
static void
633 634 635 636 637 638
model_destroy (gpointer data)
{
  g_object_steal_data (data, "model-object");
  gtk_widget_destroy (data);
}

639
static void
640 641 642 643 644
window_destroy (gpointer data)
{
  g_object_steal_data (data, "prop-editor-win");
}

645 646 647 648 649 650 651 652 653 654 655 656 657
static void
object_properties (GtkWidget *button, 
		   GObject   *object)
{
  gchar *name;
  GObject *obj;

  name = (gchar *) g_object_get_data (G_OBJECT (button), "property-name");
  g_object_get (object, name, &obj, NULL);
  if (G_IS_OBJECT (obj)) 
    create_prop_editor (obj, 0);
}
 
658
static GtkWidget *
659 660 661
property_widget (GObject    *object, 
		 GParamSpec *spec, 
		 gboolean    can_modify)
662 663
{
  GtkWidget *prop_edit;
664 665
  GtkAdjustment *adj;
  gchar *msg;
666 667 668
  GType type = G_PARAM_SPEC_TYPE (spec);

  if (type == G_TYPE_PARAM_INT)
669 670 671 672 673 674 675 676 677 678 679
    {
      adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_INT (spec)->default_value,
						G_PARAM_SPEC_INT (spec)->minimum,
						G_PARAM_SPEC_INT (spec)->maximum,
						1,
						MAX ((G_PARAM_SPEC_INT (spec)->maximum -
						      G_PARAM_SPEC_INT (spec)->minimum) / 10, 1),
						0.0));
      
      prop_edit = gtk_spin_button_new (adj, 1.0, 0);
      
680 681
      g_object_connect_property (object, spec, 
				 G_CALLBACK (int_changed), 
682 683 684 685
				 adj, G_OBJECT (adj));
      
      if (can_modify)
	connect_controller (G_OBJECT (adj), "value_changed",
686
			    object, spec, (GtkSignalFunc) int_modified);
687 688 689
    }
  else if (type == G_TYPE_PARAM_UINT)
    {
690
      adj = GTK_ADJUSTMENT (
691 692 693 694 695 696 697
			    gtk_adjustment_new (G_PARAM_SPEC_UINT (spec)->default_value,
						G_PARAM_SPEC_UINT (spec)->minimum,
						G_PARAM_SPEC_UINT (spec)->maximum,
						1,
						MAX ((G_PARAM_SPEC_UINT (spec)->maximum -
						      G_PARAM_SPEC_UINT (spec)->minimum) / 10, 1),
						0.0));
698 699 700
      
      prop_edit = gtk_spin_button_new (adj, 1.0, 0);
      
701 702
      g_object_connect_property (object, spec, 
				 G_CALLBACK (uint_changed), 
703 704 705 706
				 adj, G_OBJECT (adj));
      
      if (can_modify)
	connect_controller (G_OBJECT (adj), "value_changed",
707
			    object, spec, (GtkSignalFunc) uint_modified);
708 709 710 711
    }
  else if (type == G_TYPE_PARAM_FLOAT)
    {

712 713 714 715 716 717 718 719 720 721
      adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_FLOAT (spec)->default_value,
						G_PARAM_SPEC_FLOAT (spec)->minimum,
						G_PARAM_SPEC_FLOAT (spec)->maximum,
						0.1,
						MAX ((G_PARAM_SPEC_FLOAT (spec)->maximum -
						      G_PARAM_SPEC_FLOAT (spec)->minimum) / 10, 0.1),
						0.0));
      
      prop_edit = gtk_spin_button_new (adj, 0.1, 2);
      
722 723
      g_object_connect_property (object, spec, 
				 G_CALLBACK (float_changed), 
724 725 726 727
				 adj, G_OBJECT (adj));
      
      if (can_modify)
	connect_controller (G_OBJECT (adj), "value_changed",
728
			    object, spec, (GtkSignalFunc) float_modified);
729 730 731
    }
  else if (type == G_TYPE_PARAM_DOUBLE)
    {
732 733 734 735 736 737 738 739 740 741
      adj = GTK_ADJUSTMENT (gtk_adjustment_new (G_PARAM_SPEC_DOUBLE (spec)->default_value,
						G_PARAM_SPEC_DOUBLE (spec)->minimum,
						G_PARAM_SPEC_DOUBLE (spec)->maximum,
						0.1,
						MAX ((G_PARAM_SPEC_DOUBLE (spec)->maximum -
						      G_PARAM_SPEC_DOUBLE (spec)->minimum) / 10, 0.1),
						0.0));
      
      prop_edit = gtk_spin_button_new (adj, 0.1, 2);
      
742 743
      g_object_connect_property (object, spec, 
				 G_CALLBACK (double_changed), 
744 745 746 747
				 adj, G_OBJECT (adj));
      
      if (can_modify)
	connect_controller (G_OBJECT (adj), "value_changed",
748
			    object, spec, (GtkSignalFunc) double_modified);
749 750 751
    }
  else if (type == G_TYPE_PARAM_STRING)
    {
752 753
      prop_edit = gtk_entry_new ();
      
754
      g_object_connect_property (object, spec, 
Manish Singh's avatar
Manish Singh committed
755
				 G_CALLBACK (string_changed),
756 757 758 759
				 prop_edit, G_OBJECT (prop_edit));
      
      if (can_modify)
	connect_controller (G_OBJECT (prop_edit), "changed",
760
			    object, spec, (GtkSignalFunc) string_modified);
761 762 763
    }
  else if (type == G_TYPE_PARAM_BOOLEAN)
    {
764 765
      prop_edit = gtk_toggle_button_new_with_label ("");
      
766
      g_object_connect_property (object, spec, 
Manish Singh's avatar
Manish Singh committed
767
				 G_CALLBACK (bool_changed),
768 769 770 771
				 prop_edit, G_OBJECT (prop_edit));
      
      if (can_modify)
	connect_controller (G_OBJECT (prop_edit), "toggled",
772
			    object, spec, (GtkSignalFunc) bool_modified);
773 774 775
    }
  else if (type == G_TYPE_PARAM_ENUM)
    {
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
      {
	GtkWidget *menu;
	GEnumClass *eclass;
	gint j;
	
	prop_edit = gtk_option_menu_new ();
	
	menu = gtk_menu_new ();
	
	eclass = G_ENUM_CLASS (g_type_class_ref (spec->value_type));
	
	j = 0;
	while (j < eclass->n_values)
	  {
	    GtkWidget *mi;
	    
	    mi = gtk_menu_item_new_with_label (eclass->values[j].value_name);
	    
	    gtk_widget_show (mi);
	    
	    gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
	    
	    ++j;
	  }
	
	g_type_class_unref (eclass);
	
	gtk_option_menu_set_menu (GTK_OPTION_MENU (prop_edit), menu);
	
805
	g_object_connect_property (object, spec,
Manish Singh's avatar
Manish Singh committed
806
				   G_CALLBACK (enum_changed),
807 808 809 810
				   prop_edit, G_OBJECT (prop_edit));
	
	if (can_modify)
	  connect_controller (G_OBJECT (prop_edit), "changed",
811
			      object, spec, (GtkSignalFunc) enum_modified);
812
      }
813
    }
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
  else if (type == G_TYPE_PARAM_FLAGS)
    {
      {
	GFlagsClass *fclass;
	gint j;
	
	prop_edit = gtk_vbox_new (FALSE, 0);
	
	fclass = G_FLAGS_CLASS (g_type_class_ref (spec->value_type));
	
	for (j = 0; j < fclass->n_values; j++)
	  {
	    GtkWidget *b;
	    
	    b = gtk_check_button_new_with_label (fclass->values[j].value_name);
            g_object_set_data (G_OBJECT (b), "index", GINT_TO_POINTER (j));
	    gtk_widget_show (b);
	    gtk_box_pack_start (GTK_BOX (prop_edit), b, FALSE, FALSE, 0);
	    if (can_modify) 
	      connect_controller (G_OBJECT (b), "toggled",
	     	                  object, spec, (GtkSignalFunc) flags_modified);
	  }
	
	g_type_class_unref (fclass);
	
	g_object_connect_property (object, spec,
				   G_CALLBACK (flags_changed),
				   prop_edit, G_OBJECT (prop_edit));
      }
    }
844 845
  else if (type == G_TYPE_PARAM_UNICHAR)
    {
846 847 848
      prop_edit = gtk_entry_new ();
      gtk_entry_set_max_length (GTK_ENTRY (prop_edit), 1);
      
849
      g_object_connect_property (object, spec,
Manish Singh's avatar
Manish Singh committed
850
				 G_CALLBACK (unichar_changed),
851 852 853 854
				 prop_edit, G_OBJECT (prop_edit));
      
      if (can_modify)
	connect_controller (G_OBJECT (prop_edit), "changed",
855
			    object, spec, (GtkSignalFunc) unichar_modified);
856 857 858
    }
  else if (type == G_TYPE_PARAM_POINTER)
    {
859 860
      prop_edit = gtk_label_new ("");
      
861
      g_object_connect_property (object, spec, 
Manish Singh's avatar
Manish Singh committed
862
				 G_CALLBACK (pointer_changed),
863
				 prop_edit, G_OBJECT (prop_edit));
864 865 866
    }
  else if (type == G_TYPE_PARAM_OBJECT)
    {
867 868 869 870 871 872 873 874 875 876 877 878 879
      GtkWidget *label, *button;

      prop_edit = gtk_hbox_new (FALSE, 5);

      label = gtk_label_new ("");
      button = gtk_button_new_with_label ("Properties");
      g_object_set_data (G_OBJECT (button), "property-name", spec->name);
      g_signal_connect (button, "clicked", 
			G_CALLBACK (object_properties), 
			object);

      gtk_container_add (GTK_CONTAINER (prop_edit), label);
      gtk_container_add (GTK_CONTAINER (prop_edit), button);
880
      
881
      g_object_connect_property (object, spec,
Manish Singh's avatar
Manish Singh committed
882
				 G_CALLBACK (object_changed),
883
				 prop_edit, G_OBJECT (label));
884 885 886
    }
  else
    {  
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
      msg = g_strdup_printf ("uneditable property type: %s",
			     g_type_name (G_PARAM_SPEC_TYPE (spec)));
      prop_edit = gtk_label_new (msg);            
      g_free (msg);
      gtk_misc_set_alignment (GTK_MISC (prop_edit), 0.0, 0.5);
    }
  
  return prop_edit;
}

static GtkWidget *
properties_from_type (GObject     *object,
		      GType        type,
		      GtkTooltips *tips)
{
  GtkWidget *prop_edit;
  GtkWidget *label;
904
  GtkWidget *sw;
905 906
  GtkWidget *vbox;
  GtkWidget *table;
907
  GParamSpec **specs;
908
  guint n_specs;
909
  int i;
910

911 912 913 914 915 916 917 918 919 920
  if (G_TYPE_IS_INTERFACE (type))
    {
      gpointer vtable = g_type_default_interface_peek (type);
      specs = g_object_interface_list_properties (vtable, &n_specs);
    }
  else
    {
      GObjectClass *class = G_OBJECT_CLASS (g_type_class_peek (type));
      specs = g_object_class_list_properties (class, &n_specs);
    }
921
        
922 923
  if (n_specs == 0)
    return NULL;
924
  
925 926 927
  table = gtk_table_new (n_specs, 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 10);
  gtk_table_set_row_spacings (GTK_TABLE (table), 3);
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

  i = 0;
  while (i < n_specs)
    {
      GParamSpec *spec = specs[i];
      gboolean can_modify;
      
      prop_edit = NULL;

      can_modify = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
                    (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
      
      if ((spec->flags & G_PARAM_READABLE) == 0)
        {
          /* can't display unreadable properties */
          ++i;
          continue;
        }
      
947 948 949 950 951 952 953
      if (spec->owner_type != type)
	{
	  /* we're only interested in params of type */
	  ++i;
	  continue;
	}

Tim Janik's avatar
Tim Janik committed
954
      label = gtk_label_new (g_param_spec_get_nick (spec));
955 956 957 958 959
      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
      gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, i, i + 1);
      
      prop_edit = property_widget (object, spec, can_modify);
      gtk_table_attach_defaults (GTK_TABLE (table), prop_edit, 1, 2, i, i + 1);
960 961 962 963 964 965

      if (prop_edit)
        {
          if (!can_modify)
            gtk_widget_set_sensitive (prop_edit, FALSE);

Tim Janik's avatar
Tim Janik committed
966 967
	  if (g_param_spec_get_blurb (spec))
	    gtk_tooltips_set_tip (tips, prop_edit, g_param_spec_get_blurb (spec), NULL);
968 969 970 971 972 973 974 975
	  
          /* set initial value */
          g_object_notify (object, spec->name);
        }
      
      ++i;
    }

976 977 978 979 980 981 982 983 984

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), vbox);
985 986 987

  g_free (specs);

988 989 990
  return sw;
}

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
static GtkWidget *
child_properties_from_object (GObject     *object,
			      GtkTooltips *tips)
{
  GtkWidget *prop_edit;
  GtkWidget *label;
  GtkWidget *sw;
  GtkWidget *vbox;
  GtkWidget *table;
  GtkWidget *parent;
  GParamSpec **specs;
  guint n_specs;
  gint i;

  if (!GTK_IS_WIDGET (object))
    return NULL;

  parent = gtk_widget_get_parent (GTK_WIDGET (object));

  if (!parent)
    return NULL;

  specs = gtk_container_class_list_child_properties (G_OBJECT_GET_CLASS (parent), &n_specs);

  table = gtk_table_new (n_specs, 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 10);
  gtk_table_set_row_spacings (GTK_TABLE (table), 3);

  i = 0;
  while (i < n_specs)
    {
      GParamSpec *spec = specs[i];
      gboolean can_modify;
      
      prop_edit = NULL;

      can_modify = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
                    (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
      
      if ((spec->flags & G_PARAM_READABLE) == 0)
        {
          /* can't display unreadable properties */
          ++i;
          continue;
        }
      
      label = gtk_label_new (g_param_spec_get_nick (spec));
      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
      gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, i, i + 1);
      
      mark_child_property (spec);
      prop_edit = property_widget (object, spec, can_modify);
      gtk_table_attach_defaults (GTK_TABLE (table), prop_edit, 1, 2, i, i + 1);

      if (prop_edit)
        {
          if (!can_modify)
            gtk_widget_set_sensitive (prop_edit, FALSE);

	  if (g_param_spec_get_blurb (spec))
	    gtk_tooltips_set_tip (tips, prop_edit, g_param_spec_get_blurb (spec), NULL);
	  
          /* set initial value */
          gtk_widget_child_notify (GTK_WIDGET (object), spec->name);
        }
      
      ++i;
    }

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), vbox);

  g_free (specs);

  return sw;
}

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
static void
child_properties (GtkWidget *button, 
		  GObject   *object)
{
  create_prop_editor (object, 0);
}

static GtkWidget *
children_from_object (GObject     *object,
		      GtkTooltips *tips)
{
  GList *children, *c;
  GtkWidget *table, *label, *prop_edit, *button, *vbox, *sw;
  gchar *str;
  gint i;

  if (!GTK_IS_CONTAINER (object))
    return NULL;

  children = gtk_container_get_children (GTK_CONTAINER (object));

  table = gtk_table_new (g_list_length (children), 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 10);
  gtk_table_set_row_spacings (GTK_TABLE (table), 3);
 
  for (c = children, i = 0; c; c = c->next, i++)
    {
      object = c->data;

      label = gtk_label_new ("Child");
      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
      gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, i, i + 1);

      prop_edit = gtk_hbox_new (FALSE, 5);

      str = object_label (object);
      label = gtk_label_new (str);
      g_free (str);
      button = gtk_button_new_with_label ("Properties");
      g_signal_connect (button, "clicked",
                        G_CALLBACK (child_properties),
                        object);

      gtk_container_add (GTK_CONTAINER (prop_edit), label);
      gtk_container_add (GTK_CONTAINER (prop_edit), button);

      gtk_table_attach_defaults (GTK_TABLE (table), prop_edit, 1, 2, i, i + 1);
    }

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), vbox);

  g_list_free (children);

  return sw;
}

static GtkWidget *
cells_from_object (GObject     *object,
                   GtkTooltips *tips)
{
  GList *cells, *c;
  GtkWidget *table, *label, *prop_edit, *button, *vbox, *sw;
  gchar *str;
  gint i;

  if (!GTK_IS_CELL_LAYOUT (object))
    return NULL;

  cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (object));

  table = gtk_table_new (g_list_length (cells), 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 10);
  gtk_table_set_row_spacings (GTK_TABLE (table), 3);
 
  for (c = cells, i = 0; c; c = c->next, i++)
    {
      object = c->data;

      label = gtk_label_new ("Cell");
      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
      gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, i, i + 1);

      prop_edit = gtk_hbox_new (FALSE, 5);

      str = object_label (object);
      label = gtk_label_new (str);
      g_free (str);
      button = gtk_button_new_with_label ("Properties");
      g_signal_connect (button, "clicked",
                        G_CALLBACK (child_properties),
                        object);

      gtk_container_add (GTK_CONTAINER (prop_edit), label);
      gtk_container_add (GTK_CONTAINER (prop_edit), button);

      gtk_table_attach_defaults (GTK_TABLE (table), prop_edit, 1, 2, i, i + 1);
    }

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), vbox);

  g_list_free (cells);

  return sw;
}
1192 1193 1194 1195 1196 1197
static void
kill_tips (GtkWindow *win, GtkObject *tips)
{
  gtk_object_destroy (tips);
  g_object_unref (tips);
}
1198 1199 1200

/* Pass zero for type if you want all properties */
GtkWidget*
1201 1202
create_prop_editor (GObject   *object,
		    GType      type)
1203 1204 1205 1206 1207 1208 1209
{
  GtkWidget *win;
  GtkWidget *notebook;
  GtkTooltips *tips;
  GtkWidget *properties;
  GtkWidget *label;
  gchar *title;
1210 1211 1212
  GType *ifaces;
  guint n_ifaces;
  
1213 1214 1215 1216 1217 1218 1219
  if ((win = g_object_get_data (G_OBJECT (object), "prop-editor-win")))
    {
      gtk_window_present (GTK_WINDOW (win));
      return win;
    }

  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1220 1221 1222
  if (GTK_IS_WIDGET (object))
    gtk_window_set_screen (GTK_WINDOW (win),
			   gtk_widget_get_screen (GTK_WIDGET (object)));
1223

1224
  tips = gtk_tooltips_new ();
1225 1226 1227 1228
  g_object_ref (tips);
  gtk_object_sink (GTK_OBJECT (tips));

  /* Kill the tips when the widget goes away.  */
1229
  g_signal_connect (win, "destroy", G_CALLBACK (kill_tips), tips);
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242

  /* hold a weak ref to the object we're editing */
  g_object_set_data_full (G_OBJECT (object), "prop-editor-win", win, model_destroy);
  g_object_set_data_full (G_OBJECT (win), "model-object", object, window_destroy);

  if (type == 0)
    {
      notebook = gtk_notebook_new ();
      gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);
      
      gtk_container_add (GTK_CONTAINER (win), notebook);
      
      type = G_TYPE_FROM_INSTANCE (object);
1243

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
      title = g_strdup_printf ("Properties of %s widget", g_type_name (type));
      gtk_window_set_title (GTK_WINDOW (win), title);
      g_free (title);
      
      while (type)
	{
	  properties = properties_from_type (object, type, tips);
	  if (properties)
	    {
	      label = gtk_label_new (g_type_name (type));
	      gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
					properties, label);
	    }
	  
	  type = g_type_parent (type);
	}
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

      ifaces = g_type_interfaces (G_TYPE_FROM_INSTANCE (object), &n_ifaces);
      while (n_ifaces--)
	{
	  properties = properties_from_type (object, ifaces[n_ifaces], tips);
	  if (properties)
	    {
	      label = gtk_label_new (g_type_name (ifaces[n_ifaces]));
	      gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
					properties, label);
	    }
	}

      g_free (ifaces);
1274 1275 1276 1277 1278 1279 1280 1281

      properties = child_properties_from_object (object, tips);
      if (properties)
	{
	  label = gtk_label_new ("Child properties");
	  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
				    properties, label);
	}
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297

      properties = children_from_object (object, tips);
      if (properties)
	{
	  label = gtk_label_new ("Children");
	  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
				    properties, label);
	}

      properties = cells_from_object (object, tips);
      if (properties)
	{
	  label = gtk_label_new ("Cell renderers");
	  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
				    properties, label);
	}
1298 1299 1300 1301 1302 1303 1304
    }
  else
    {
      properties = properties_from_type (object, type, tips);
      gtk_container_add (GTK_CONTAINER (win), properties);
      title = g_strdup_printf ("Properties of %s", g_type_name (type));
      gtk_window_set_title (GTK_WINDOW (win), title);
1305
      g_free (title);
1306 1307 1308
    }
  
  gtk_window_set_default_size (GTK_WINDOW (win), -1, 400);
1309 1310 1311 1312 1313 1314
  
  gtk_widget_show_all (win);

  return win;
}