glade-base-editor.c 68.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2006 Juan Pablo Ugarte.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Juan Pablo Ugarte <juanpablougarte@gmail.com>
 */

Christian Persch's avatar
Christian Persch committed
22 23
#include "config.h"

24 25 26 27 28 29 30
/**
 * SECTION:glade-base-editor
 * @Short_Description: A customisable editor
 *
 * Convenience object to edit containers where placeholders do not make sense, like GtkMenubar.
 */

31
#include "glade.h"
32
#include "glade-marshallers.h"
33 34
#include "glade-editor-property.h"
#include "glade-base-editor.h"
35
#include "glade-app.h"
36
#include "glade-popup.h"
37 38 39 40 41 42 43 44
#include "glade-accumulators.h"

#include <string.h>
#include <glib/gi18n-lib.h>
#include <gdk/gdkkeysyms.h>

typedef enum
{
45 46 47 48
  GLADE_BASE_EDITOR_GTYPE,
  GLADE_BASE_EDITOR_CLASS_NAME,
  GLADE_BASE_EDITOR_TYPES_N_COLUMNS
} GladeBaseEditorChildEnum;
49 50 51

typedef enum
{
52 53 54 55 56 57 58 59 60 61 62 63
  GLADE_BASE_EDITOR_GWIDGET,
  GLADE_BASE_EDITOR_OBJECT,
  GLADE_BASE_EDITOR_TYPE_NAME,
  GLADE_BASE_EDITOR_NAME,
  GLADE_BASE_EDITOR_CHILD_TYPES,
  GLADE_BASE_EDITOR_N_COLUMNS
} GladeBaseEditorEnum;

typedef struct
{
  GType parent_type;
  GtkTreeModel *children;
64 65
} ChildTypeTab;

66 67
struct _GladeBaseEditorPrivate
{
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  GladeWidget *gcontainer;      /* The container we are editing */

  /* Editor UI */
  GtkWidget *paned, *table, *treeview, *main_scroll, *notebook;
  GtkWidget *remove_button;
  GladeSignalEditor *signal_editor;

  GList *child_types;

  GtkTreeModel *model;
  GladeProject *project;

  /* Add button data */
  GType add_type;
  gboolean add_as_child;

  /* Temporal variables */
  GtkTreeIter iter;             /* used in idle functions */
  gint row;

  gboolean updating_treeview;

  guint properties_idle;
91 92
};

93 94 95 96 97 98 99 100 101
enum
{
  SIGNAL_CHILD_SELECTED,
  SIGNAL_CHANGE_TYPE,
  SIGNAL_GET_DISPLAY_NAME,
  SIGNAL_BUILD_CHILD,
  SIGNAL_DELETE_CHILD,
  SIGNAL_MOVE_CHILD,
  LAST_SIGNAL
102
};
103

104 105 106 107
enum
{
  PROP_0,
  PROP_CONTAINER,
108 109
};

110

111 112
static guint glade_base_editor_signals[LAST_SIGNAL] = { 0 };

113 114
static GtkVBoxClass *parent_class = NULL;

115 116 117 118
static void glade_base_editor_set_container (GladeBaseEditor * editor,
                                             GObject * container);
static void glade_base_editor_block_callbacks (GladeBaseEditor * editor,
                                               gboolean block);
119

120 121

static void
122
reset_child_types (GladeBaseEditor * editor)
123
{
124 125
  GList *l;
  ChildTypeTab *tab;
126

127 128 129 130 131 132 133 134
  for (l = editor->priv->child_types; l; l = l->next)
    {
      tab = l->data;
      g_object_unref (tab->children);
      g_free (tab);
    }
  g_list_free (editor->priv->child_types);
  editor->priv->child_types = NULL;
135 136 137
}


138
static gint
139
sort_type_by_hierarchy (ChildTypeTab * a, ChildTypeTab * b)
140
{
141 142 143 144
  if (g_type_is_a (a->parent_type, b->parent_type))
    return -1;

  return 1;
145 146
}

147
static GtkTreeModel *
148
get_children_model_for_type (GladeBaseEditor * editor, GType type)
149
{
150 151 152 153 154 155 156 157
  GList *l;
  for (l = editor->priv->child_types; l; l = l->next)
    {
      ChildTypeTab *tab = l->data;
      if (g_type_is_a (type, tab->parent_type))
        return tab->children;
    }
  return NULL;
158 159
}

160
static GtkTreeModel *
161
get_children_model_for_child_type (GladeBaseEditor * editor, GType type)
162
{
163 164
  GList *l;
  GtkTreeModel *model = NULL;
165

166 167 168 169 170 171 172
  /* Get deep derived classes first and work up the sorted heirarchy */
  for (l = g_list_last (editor->priv->child_types); model == NULL && l;
       l = l->prev)
    {
      ChildTypeTab *tab = l->data;
      GtkTreeIter iter;
      GType iter_type;
173

174 175
      if (!gtk_tree_model_get_iter_first (tab->children, &iter))
        continue;
176

177 178 179 180
      do
        {
          gtk_tree_model_get (tab->children, &iter,
                              GLADE_BASE_EDITOR_GTYPE, &iter_type, -1);
181

182 183 184
          /* Find exact match types in this case */
          if (iter_type == type)
            model = tab->children;
185

186 187 188
        }
      while (model == NULL && gtk_tree_model_iter_next (tab->children, &iter));
    }
189

190
  return model;
191 192 193
}

static gboolean
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
glade_base_editor_get_type_info (GladeBaseEditor * e,
                                 GtkTreeIter * retiter, GType child_type, ...)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  GType type;

  model = get_children_model_for_child_type (e, child_type);

  if (!model || gtk_tree_model_get_iter_first (model, &iter) == FALSE)
    return FALSE;

  do
    {
      gtk_tree_model_get (model, &iter, GLADE_BASE_EDITOR_GTYPE, &type, -1);

      if (child_type == type)
        {
          va_list args;
          va_start (args, child_type);
          gtk_tree_model_get_valist (model, &iter, args);
          va_end (args);
          if (retiter)
            *retiter = iter;
          return TRUE;
        }
    }
  while (gtk_tree_model_iter_next (model, &iter));

  return FALSE;
224 225
}

226

227
static gchar *
228 229
glade_base_editor_get_display_name (GladeBaseEditor * editor,
                                    GladeWidget * gchild)
230
{
231 232 233 234 235
  gchar *retval;
  g_signal_emit (editor,
                 glade_base_editor_signals[SIGNAL_GET_DISPLAY_NAME],
                 0, gchild, &retval);
  return retval;
236 237 238
}

static void
239 240 241 242 243 244 245 246 247 248 249 250
glade_base_editor_fill_store_real (GladeBaseEditor * e,
                                   GladeWidget * gwidget, GtkTreeIter * parent)
{
  GList *children, *l;
  GladeWidget *gparent = NULL;
  GtkTreeIter iter;

  if (parent)
    gtk_tree_model_get (e->priv->model, parent,
                        GLADE_BASE_EDITOR_GWIDGET, &gparent, -1);


251
  children = glade_widget_get_children (gwidget);
252 253 254 255

  for (l = children; l; l = l->next)
    {
      GladeWidget *gchild;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
      GObject     *child = (GObject *) l->data;
      gchar       *type_name = NULL, *name;

      gchild = glade_widget_get_from_gobject (child);

      /* Have to check parents here for compatibility (could be the parenting menuitem of this menu
       * supports a menuitem...) */
      if (glade_base_editor_get_type_info (e, NULL,
					   G_OBJECT_TYPE (child),
					   GLADE_BASE_EDITOR_CLASS_NAME,
					   &type_name, -1))
	{
	  gtk_tree_store_append (GTK_TREE_STORE (e->priv->model), &iter, parent);

	  name = glade_base_editor_get_display_name (e, gchild);

	  gtk_tree_store_set (GTK_TREE_STORE (e->priv->model), &iter,
			      GLADE_BASE_EDITOR_GWIDGET, gchild,
			      GLADE_BASE_EDITOR_OBJECT, child,
			      GLADE_BASE_EDITOR_TYPE_NAME, type_name,
			      GLADE_BASE_EDITOR_NAME, name,
			      GLADE_BASE_EDITOR_CHILD_TYPES,
			      get_children_model_for_child_type (e,
								 G_OBJECT_TYPE
								 (child)),
			      -1);

	  glade_base_editor_fill_store_real (e, gchild, &iter);

	  g_free (name);
	  g_free (type_name);
	}
      else
	glade_base_editor_fill_store_real (e, gchild, parent);
290 291 292
    }

  g_list_free (children);
293 294 295
}

static void
296
glade_base_editor_fill_store (GladeBaseEditor * e)
297
{
298 299 300 301
  gtk_tree_store_clear (GTK_TREE_STORE (e->priv->model));
  gtk_tree_view_set_model (GTK_TREE_VIEW (e->priv->treeview), NULL);
  glade_base_editor_fill_store_real (e, e->priv->gcontainer, NULL);
  gtk_tree_view_set_model (GTK_TREE_VIEW (e->priv->treeview), e->priv->model);
302

303
  gtk_tree_view_expand_all (GTK_TREE_VIEW (e->priv->treeview));
304

305 306 307
}

static gboolean
308
glade_base_editor_get_child_selected (GladeBaseEditor * e, GtkTreeIter * iter)
309
{
310 311 312
  GtkTreeSelection *sel =
      gtk_tree_view_get_selection (GTK_TREE_VIEW (e->priv->treeview));
  return gtk_tree_selection_get_selected (sel, NULL, iter);
313 314
}

315 316
/* Forward declaration for glade_base_editor_project_widget_name_changed */
static void
317 318 319
glade_base_editor_project_widget_name_changed (GladeProject * project,
                                               GladeWidget * widget,
                                               GladeBaseEditor * editor);
320

321 322

static GladeWidget *
323 324
glade_base_editor_delegate_build_child (GladeBaseEditor * editor,
                                        GladeWidget * parent, GType type)
325
{
326 327 328 329
  GladeWidget *child = NULL;
  g_signal_emit (editor, glade_base_editor_signals[SIGNAL_BUILD_CHILD],
                 0, parent, type, &child);
  return child;
330 331 332
}

static gboolean
333 334 335
glade_base_editor_delegate_delete_child (GladeBaseEditor * editor,
                                         GladeWidget * parent,
                                         GladeWidget * child)
336
{
337
  gboolean retval;
338

339 340
  g_signal_emit (editor, glade_base_editor_signals[SIGNAL_DELETE_CHILD],
                 0, parent, child, &retval);
341

342
  return retval;
343 344
}

345
static void
346
glade_base_editor_name_activate (GtkEntry * entry, GladeWidget * gchild)
347
{
348 349
  const gchar *text = gtk_entry_get_text (GTK_ENTRY (entry));
  GladeBaseEditor *editor = g_object_get_data (G_OBJECT (entry), "editor");
350

351
  if (text && text[0] && strcmp (glade_widget_get_name (gchild), text))
352
    {
353 354
      g_signal_handlers_block_by_func (glade_widget_get_project (gchild),
                                       glade_base_editor_project_widget_name_changed, editor);
355
      glade_command_set_name (gchild, text);
356 357
      g_signal_handlers_unblock_by_func (glade_widget_get_project (gchild),
                                         glade_base_editor_project_widget_name_changed, editor);
358
    }
359 360 361
}

static void
362
glade_base_editor_remove_widget (GtkWidget * widget, gpointer container)
363
{
364
  gtk_container_remove (GTK_CONTAINER (container), widget);
365 366 367
}

static void
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
glade_base_editor_table_attach (GladeBaseEditor * e,
                                GtkWidget * child1, GtkWidget * child2)
{
  GtkGrid *table = GTK_GRID (e->priv->table);
  gint row = e->priv->row;

  if (child1)
    {
      gtk_grid_attach (table, child1, 0, row, 1, 1);
      gtk_widget_set_hexpand (child1, TRUE);
      gtk_widget_show (child1);
    }

  if (child2)
    {
      gtk_grid_attach (table, child2, 1, row, 1, 1);
      gtk_widget_show (child2);
    }

  e->priv->row++;
388 389 390
}

static void
391
glade_base_editor_clear (GladeBaseEditor * editor)
392
{
393 394 395 396 397 398
  GladeBaseEditorPrivate *e = editor->priv;
  gtk_container_foreach (GTK_CONTAINER (e->table),
                         glade_base_editor_remove_widget, e->table);
  e->row = 0;
  gtk_widget_set_sensitive (e->remove_button, FALSE);
  glade_signal_editor_load_widget (e->signal_editor, NULL);
399 400 401
}

static void
402 403
glade_base_editor_treeview_cursor_changed (GtkTreeView * treeview,
                                           GladeBaseEditor * editor)
404
{
405 406 407 408
  GladeBaseEditorPrivate *e = editor->priv;
  GtkTreeIter iter;
  GObject *child;
  GladeWidget *gchild;
409

410
  g_return_if_fail (GTK_IS_TREE_VIEW (treeview));
411

412 413
  if (!glade_base_editor_get_child_selected (editor, &iter))
    return;
414

415 416
  glade_base_editor_clear (editor);
  gtk_widget_set_sensitive (e->remove_button, TRUE);
417

418 419 420
  gtk_tree_model_get (e->model, &iter,
                      GLADE_BASE_EDITOR_GWIDGET, &gchild,
                      GLADE_BASE_EDITOR_OBJECT, &child, -1);
421

422 423 424 425 426 427
  /* Emit child-selected signal and let the user add the properties */
  g_signal_emit (editor, glade_base_editor_signals[SIGNAL_CHILD_SELECTED],
                 0, gchild);

  /* Update Signal Editor */
  glade_signal_editor_load_widget (e->signal_editor, gchild);
428 429 430 431 432
}

static gboolean
glade_base_editor_update_properties_idle (gpointer data)
{
433 434 435 436 437
  GladeBaseEditor *editor = (GladeBaseEditor *) data;
  glade_base_editor_treeview_cursor_changed
      (GTK_TREE_VIEW (editor->priv->treeview), editor);
  editor->priv->properties_idle = 0;
  return FALSE;
438 439
}

440
/* XXX Can we make it crisper by removing this idle ?? */
441 442
static void
glade_base_editor_update_properties (GladeBaseEditor * editor)
443
{
444
  g_return_if_fail (GLADE_IS_BASE_EDITOR (editor));
445

446 447 448
  if (!editor->priv->properties_idle)
    editor->priv->properties_idle =
        g_idle_add (glade_base_editor_update_properties_idle, editor);
449 450 451
}

static void
452 453 454 455 456 457 458 459 460 461 462 463 464 465
glade_base_editor_set_cursor (GladeBaseEditor * e, GtkTreeIter * iter)
{
  GtkTreePath *path;
  GtkTreeIter real_iter;

  if (iter == NULL && glade_base_editor_get_child_selected (e, &real_iter))
    iter = &real_iter;

  if (iter && (path = gtk_tree_model_get_path (e->priv->model, iter)))
    {
      gtk_tree_view_set_cursor (GTK_TREE_VIEW (e->priv->treeview), path, NULL,
                                FALSE);
      gtk_tree_path_free (path);
    }
466 467 468
}

static gboolean
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
glade_base_editor_find_child_real (GladeBaseEditor * e,
                                   GladeWidget * gchild, GtkTreeIter * iter)
{
  GtkTreeModel *model = e->priv->model;
  GtkTreeIter child_iter;
  GladeWidget *child;

  do
    {
      gtk_tree_model_get (model, iter, GLADE_BASE_EDITOR_GWIDGET, &child, -1);

      if (child == gchild)
        return TRUE;

      if (gtk_tree_model_iter_children (model, &child_iter, iter))
        if (glade_base_editor_find_child_real (e, gchild, &child_iter))
          {
            *iter = child_iter;
            return TRUE;
          }
    }
  while (gtk_tree_model_iter_next (model, iter));

  return FALSE;
493 494 495
}

static gboolean
496 497
glade_base_editor_find_child (GladeBaseEditor * e,
                              GladeWidget * child, GtkTreeIter * iter)
498
{
499 500 501 502
  if (gtk_tree_model_get_iter_first (e->priv->model, iter))
    return glade_base_editor_find_child_real (e, child, iter);

  return FALSE;
503 504 505
}

static void
506
glade_base_editor_select_child (GladeBaseEditor * e, GladeWidget * child)
507
{
508 509 510 511
  GtkTreeIter iter;

  if (glade_base_editor_find_child (e, child, &iter))
    glade_base_editor_set_cursor (e, &iter);
512 513 514
}

static void
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
glade_base_editor_child_change_type (GladeBaseEditor * editor,
                                     GtkTreeIter * iter, GType type)
{
  GladeWidget *gchild, *gparent;
  GObject *child;
  gchar *class_name;
  gboolean retval;

  glade_base_editor_block_callbacks (editor, TRUE);

  /* Get old widget data */
  gtk_tree_model_get (editor->priv->model, iter,
                      GLADE_BASE_EDITOR_GWIDGET, &gchild,
                      GLADE_BASE_EDITOR_OBJECT, &child, -1);

  if (type == G_OBJECT_TYPE (child))
    return;

533
  if (!gchild || !glade_widget_get_parent (gchild))
534 535
    return;

536
  gparent = glade_widget_get_parent (gchild);
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

  /* Start of glade-command */

  if (glade_base_editor_get_type_info (editor, NULL,
                                       type,
                                       GLADE_BASE_EDITOR_CLASS_NAME,
                                       &class_name, -1))
    {
      glade_command_push_group (_("Setting object type on %s to %s"),
                                glade_widget_get_name (gchild), class_name);
      g_free (class_name);
    }
  else
    return;

  g_signal_emit (editor,
                 glade_base_editor_signals[SIGNAL_CHANGE_TYPE],
                 0, gchild, type, &retval);

  /* End of glade-command */
  glade_command_pop_group ();

  /* Update properties */
  glade_base_editor_update_properties (editor);

  glade_base_editor_block_callbacks (editor, FALSE);
563 564 565
}

static void
566
glade_base_editor_type_changed (GtkComboBox * widget, GladeBaseEditor * e)
567
{
568 569 570 571 572 573 574
  GtkTreeIter iter, combo_iter;
  GType type;

  if (!glade_base_editor_get_child_selected (e, &iter))
    return;

  gtk_combo_box_get_active_iter (widget, &combo_iter);
575

576 577 578 579
  gtk_tree_model_get (gtk_combo_box_get_model (widget), &combo_iter,
                      GLADE_BASE_EDITOR_GTYPE, &type, -1);

  glade_base_editor_child_change_type (e, &iter, type);
580 581 582
}

static void
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
glade_base_editor_child_type_edited (GtkCellRendererText * cell,
                                     const gchar * path_string,
                                     const gchar * new_text,
                                     GladeBaseEditor * editor)
{
  GladeBaseEditorPrivate *e = editor->priv;
  GtkTreeModel *child_class;
  GtkTreePath *path;
  GtkTreeIter iter, combo_iter;
  GType type;
  gchar *type_name;

  path = gtk_tree_path_new_from_string (path_string);
  gtk_tree_model_get_iter (e->model, &iter, path);
  gtk_tree_model_get (e->model, &iter,
                      GLADE_BASE_EDITOR_CLASS_NAME, &type_name,
                      GLADE_BASE_EDITOR_CHILD_TYPES, &child_class, -1);

  if (strcmp (type_name, new_text) == 0)
    {
      g_free (type_name);
      return;
    }

  /* Lookup GType */
  if (!gtk_tree_model_get_iter_first (child_class, &combo_iter))
    return;

  do
    {
      gtk_tree_model_get (child_class, &combo_iter,
                          GLADE_BASE_EDITOR_GTYPE, &type,
                          GLADE_BASE_EDITOR_CLASS_NAME, &type_name, -1);

      if (strcmp (type_name, new_text) == 0)
        break;

      g_free (type_name);
    }
  while (gtk_tree_model_iter_next (child_class, &combo_iter));

  glade_base_editor_child_change_type (editor, &iter, type);
625 626 627
}

static void
628 629
glade_base_editor_reorder_children (GladeBaseEditor * editor,
                                    GtkTreeIter * child)
630
{
631 632 633 634 635 636 637 638 639 640
  GtkTreeModel *model = editor->priv->model;
  GladeWidget *gchild;
  GladeProperty *property;
  GtkTreeIter parent, iter;
  gint position = 0;

  if (gtk_tree_model_iter_parent (model, &parent, child))
    gtk_tree_model_iter_children (model, &iter, &parent);
  else
    gtk_tree_model_get_iter_first (model, &iter);
641

642 643 644
  do
    {
      gtk_tree_model_get (model, &iter, GLADE_BASE_EDITOR_GWIDGET, &gchild, -1);
645

646 647 648 649 650
      if ((property = glade_widget_get_property (gchild, "position")) != NULL)
        glade_command_set_property (property, position);
      position++;
    }
  while (gtk_tree_model_iter_next (model, &iter));
651 652 653
}

static void
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
glade_base_editor_add_child (GladeBaseEditor * editor,
                             GType type, gboolean as_child)
{
  GladeBaseEditorPrivate *e = editor->priv;
  GtkTreeIter iter, new_iter;
  GladeWidget *gparent, *gchild_new;
  gchar *name, *class_name;
  gboolean selected_iter;

  glade_base_editor_block_callbacks (editor, TRUE);

  gparent = e->gcontainer;

  if ((selected_iter = glade_base_editor_get_child_selected (editor, &iter)))
    {

      if (as_child)
        gtk_tree_model_get (e->model, &iter,
                            GLADE_BASE_EDITOR_GWIDGET, &gparent, -1);
      else if (gtk_tree_model_iter_parent (e->model, &new_iter, &iter))
        {
          gtk_tree_model_get (e->model, &new_iter,
                              GLADE_BASE_EDITOR_GWIDGET, &gparent, -1);
        }
    }

  if (!glade_base_editor_get_type_info (editor, NULL, type,
                                        GLADE_BASE_EDITOR_CLASS_NAME,
                                        &class_name, -1))
    return;

  glade_command_push_group (_("Add a %s to %s"), class_name,
                            glade_widget_get_name (gparent));

  /* Build Child */
  gchild_new = glade_base_editor_delegate_build_child (editor, gparent, type);

  if (gchild_new == NULL)
    {
      glade_command_pop_group ();
      return;
    }

  if (selected_iter)
    {
      if (as_child)
        gtk_tree_store_append (GTK_TREE_STORE (editor->priv->model), &new_iter,
                               &iter);
      else
        gtk_tree_store_insert_after (GTK_TREE_STORE (editor->priv->model),
                                     &new_iter, NULL, &iter);
    }
  else
    gtk_tree_store_append (GTK_TREE_STORE (editor->priv->model), &new_iter,
                           NULL);

  name = glade_base_editor_get_display_name (editor, gchild_new);

  gtk_tree_store_set (GTK_TREE_STORE (editor->priv->model), &new_iter,
                      GLADE_BASE_EDITOR_GWIDGET, gchild_new,
                      GLADE_BASE_EDITOR_OBJECT,
                      glade_widget_get_object (gchild_new),
                      GLADE_BASE_EDITOR_TYPE_NAME, class_name,
                      GLADE_BASE_EDITOR_NAME, name,
                      GLADE_BASE_EDITOR_CHILD_TYPES,
                      get_children_model_for_type (editor,
720 721
                                                   G_OBJECT_TYPE (glade_widget_get_object (gparent))),
		      -1);
722 723 724 725 726 727 728 729 730 731 732 733

  glade_base_editor_reorder_children (editor, &new_iter);

  gtk_tree_view_expand_all (GTK_TREE_VIEW (e->treeview));
  glade_base_editor_set_cursor (editor, &new_iter);

  glade_command_pop_group ();

  glade_base_editor_block_callbacks (editor, FALSE);

  g_free (name);
  g_free (class_name);
734 735
}

736

737
static void
738 739
glade_base_editor_add_item_activate (GtkMenuItem * menuitem,
                                     GladeBaseEditor * e)
740
{
741 742 743 744 745 746
  GObject *item = G_OBJECT (menuitem);
  GType type = GPOINTER_TO_INT (g_object_get_data (item, "object_type"));
  gboolean as_child =
      GPOINTER_TO_INT (g_object_get_data (item, "object_as_child"));

  glade_base_editor_add_child (e, type, as_child);
747 748
}

749
static GtkWidget *
750 751 752 753 754 755 756 757 758 759 760
glade_base_editor_popup (GladeBaseEditor * editor, GladeWidget * widget)
{
  GtkWidget *popup, *item;
  GtkTreeModel *model;
  GtkTreeIter iter;
  GType iter_type;
  gchar *label;
  gchar *class_name;

  if ((model =
       get_children_model_for_child_type (editor,
761
                                          G_OBJECT_TYPE (glade_widget_get_object (widget)))) == NULL)
762
    model =
763 764
      get_children_model_for_type (editor,
				   G_OBJECT_TYPE (glade_widget_get_object (editor->priv->gcontainer)));
765 766 767 768 769 770 771 772 773 774 775 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

  g_assert (model);

  popup = gtk_menu_new ();

  if (gtk_tree_model_get_iter_first (model, &iter))
    do
      {
        gtk_tree_model_get (model, &iter,
                            GLADE_BASE_EDITOR_GTYPE, &iter_type,
                            GLADE_BASE_EDITOR_CLASS_NAME, &class_name, -1);

        label = g_strdup_printf (_("Add %s item"), class_name);

        item = gtk_menu_item_new_with_label (label);
        gtk_widget_show (item);

        g_object_set_data (G_OBJECT (item), "object_type",
                           GINT_TO_POINTER (iter_type));

        g_object_set_data (G_OBJECT (item), "object_as_child",
                           GINT_TO_POINTER (FALSE));

        g_signal_connect (item, "activate",
                          G_CALLBACK (glade_base_editor_add_item_activate),
                          editor);
        gtk_menu_shell_append (GTK_MENU_SHELL (popup), item);

        g_free (label);
        g_free (class_name);

      }
    while (gtk_tree_model_iter_next (model, &iter));


  if ((model =
801
       get_children_model_for_type (editor, G_OBJECT_TYPE (glade_widget_get_object (widget)))) &&
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
      gtk_tree_model_get_iter_first (model, &iter))
    do
      {
        gtk_tree_model_get (model, &iter,
                            GLADE_BASE_EDITOR_GTYPE, &iter_type,
                            GLADE_BASE_EDITOR_CLASS_NAME, &class_name, -1);

        label = g_strdup_printf (_("Add child %s item"), class_name);

        item = gtk_menu_item_new_with_label (label);
        gtk_widget_show (item);

        g_object_set_data (G_OBJECT (item), "object_type",
                           GINT_TO_POINTER (iter_type));

        g_object_set_data (G_OBJECT (item), "object_as_child",
                           GINT_TO_POINTER (TRUE));

        g_signal_connect (item, "activate",
                          G_CALLBACK (glade_base_editor_add_item_activate),
                          editor);
        gtk_menu_shell_append (GTK_MENU_SHELL (popup), item);

        g_free (label);
        g_free (class_name);

      }
    while (gtk_tree_model_iter_next (model, &iter));

  return popup;
832 833 834
}

static gint
835 836
glade_base_editor_popup_handler (GtkWidget * treeview,
                                 GdkEventButton * event, GladeBaseEditor * e)
837
{
838 839
  GtkTreePath *path;
  GtkWidget *popup;
840

841 842 843 844 845 846 847 848
  if (glade_popup_is_popup_event (event))
    {
      if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (treeview),
                                         (gint) event->x, (gint) event->y,
                                         &path, NULL, NULL, NULL))
        {
          GtkTreeIter iter;
          GladeWidget *gwidget;
849

850 851
          gtk_tree_view_set_cursor (GTK_TREE_VIEW (treeview), path, NULL,
                                    FALSE);
852

853 854 855
          gtk_tree_model_get_iter (e->priv->model, &iter, path);
          gtk_tree_model_get (e->priv->model, &iter,
                              GLADE_BASE_EDITOR_GWIDGET, &gwidget, -1);
856 857


858
          popup = glade_base_editor_popup (e, gwidget);
859

860
          gtk_tree_path_free (path);
861

862 863 864 865 866 867 868
          gtk_menu_popup (GTK_MENU (popup), NULL, NULL, NULL, NULL,
                          event->button, event->time);
        }
      return TRUE;
    }

  return FALSE;
869 870 871
}


872
static void
873
glade_base_editor_add_activate (GtkButton * button, GladeBaseEditor * e)
874
{
875 876
  if (e->priv->add_type)
    glade_base_editor_add_child (e, e->priv->add_type, e->priv->add_as_child);
877 878 879
}

static void
880
glade_base_editor_delete_child (GladeBaseEditor * e)
881
{
882 883 884 885 886 887 888 889
  GladeWidget *child, *gparent;
  GtkTreeIter iter, parent;

  if (!glade_base_editor_get_child_selected (e, &iter))
    return;

  gtk_tree_model_get (e->priv->model, &iter,
                      GLADE_BASE_EDITOR_GWIDGET, &child, -1);
890

891 892 893 894 895
  if (gtk_tree_model_iter_parent (e->priv->model, &parent, &iter))
    gtk_tree_model_get (e->priv->model, &parent,
                        GLADE_BASE_EDITOR_GWIDGET, &gparent, -1);
  else
    gparent = e->priv->gcontainer;
896

897 898 899
  glade_command_push_group (_("Delete %s child from %s"),
                            glade_widget_get_name (child),
                            glade_widget_get_name (gparent));
900

901 902 903 904
  /* Emit delete-child signal */
  glade_base_editor_delegate_delete_child (e, gparent, child);

  glade_command_pop_group ();
905 906 907 908
}


static gboolean
909 910 911
glade_base_editor_treeview_key_press_event (GtkWidget * widget,
                                            GdkEventKey * event,
                                            GladeBaseEditor * e)
912
{
913 914 915 916
  if (event->keyval == GDK_KEY_Delete)
    glade_base_editor_delete_child (e);

  return FALSE;
917 918 919
}

static void
920
glade_base_editor_delete_activate (GtkButton * button, GladeBaseEditor * e)
921
{
922
  glade_base_editor_delete_child (e);
923 924 925
}

static gboolean
926 927 928
glade_base_editor_is_child (GladeBaseEditor * e,
                            GladeWidget * gchild, gboolean valid_type)
{
929
  GladeWidget *gcontainer = glade_widget_get_parent (gchild);
930 931 932 933 934 935 936 937

  if (!gcontainer)
    return FALSE;

  if (valid_type)
    {
      GObject *child = glade_widget_get_object (gchild);

938
      if (glade_widget_get_internal (gchild) ||
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
          glade_base_editor_get_type_info (e, NULL,
                                           G_OBJECT_TYPE (child), -1) == FALSE)
        return FALSE;

      gcontainer = e->priv->gcontainer;
    }
  else
    {
      GtkTreeIter iter;
      if (glade_base_editor_get_child_selected (e, &iter))
        gtk_tree_model_get (e->priv->model, &iter,
                            GLADE_BASE_EDITOR_GWIDGET, &gcontainer, -1);
      else
        return FALSE;
    }

  while ((gchild = glade_widget_get_parent (gchild)))
    if (gchild == gcontainer)
      return TRUE;

  return FALSE;
960 961 962 963 964
}

static gboolean
glade_base_editor_update_treeview_idle (gpointer data)
{
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
  GladeBaseEditor *e = ((GladeBaseEditor *) data);
  GList *selection = glade_project_selection_get (e->priv->project);

  glade_base_editor_block_callbacks (e, TRUE);

  glade_base_editor_fill_store (e);
  glade_base_editor_clear (e);

  gtk_tree_view_expand_all (GTK_TREE_VIEW (e->priv->treeview));

  if (selection)
    {
      GladeWidget *widget =
          glade_widget_get_from_gobject (G_OBJECT (selection->data));
      if (glade_base_editor_is_child (e, widget, TRUE))
        glade_base_editor_select_child (e, widget);
    }

  e->priv->updating_treeview = FALSE;
  glade_base_editor_block_callbacks (e, FALSE);

  return FALSE;
987 988 989
}

static void
990 991 992
glade_base_editor_project_widget_name_changed (GladeProject * project,
                                               GladeWidget * widget,
                                               GladeBaseEditor * editor)
993
{
994 995
  GladeWidget *selected_child;
  GtkTreeIter iter;
996

997 998 999 1000 1001 1002
  if (glade_base_editor_get_child_selected (editor, &iter))
    {
      gtk_tree_model_get (editor->priv->model, &iter,
                          GLADE_BASE_EDITOR_GWIDGET, &selected_child, -1);
      if (widget == selected_child)
        glade_base_editor_update_properties (editor);
1003

1004 1005
      g_object_unref (G_OBJECT (selected_child));
    }
1006 1007 1008
}

static void
1009
glade_base_editor_project_closed (GladeProject * project, GladeBaseEditor * e)
1010
{
1011
  glade_base_editor_set_container (e, NULL);
1012 1013 1014
}

static void
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
glade_base_editor_reorder (GladeBaseEditor * editor, GtkTreeIter * iter)
{
  GladeBaseEditorPrivate *e = editor->priv;
  GladeWidget *gchild, *gparent;
  GtkTreeIter parent_iter;
  gboolean retval;

  glade_command_push_group (_("Reorder %s's children"),
                            glade_widget_get_name (e->gcontainer));

  gtk_tree_model_get (e->model, iter, GLADE_BASE_EDITOR_GWIDGET, &gchild, -1);
  g_object_unref (G_OBJECT (gchild));

  if (gtk_tree_model_iter_parent (e->model, &parent_iter, iter))
    {
      gtk_tree_model_get (e->model, &parent_iter,
                          GLADE_BASE_EDITOR_GWIDGET, &gparent, -1);
      g_object_unref (G_OBJECT (gparent));
    }
  else
    gparent = e->gcontainer;

  g_signal_emit (editor, glade_base_editor_signals[SIGNAL_MOVE_CHILD],
                 0, gparent, gchild, &retval);

  if (retval)
    glade_base_editor_reorder_children (editor, iter);
  else
    {
      glade_base_editor_clear (editor);
      glade_base_editor_fill_store (editor);
      glade_base_editor_find_child (editor, gchild, &editor->priv->iter);
    }

  glade_command_pop_group ();
}

static gboolean
1053 1054
glade_base_editor_drag_and_drop_idle (gpointer data)
{
1055 1056 1057 1058 1059 1060 1061 1062
  GladeBaseEditor *e = (GladeBaseEditor *) data;

  glade_base_editor_reorder (e, &e->priv->iter);
  gtk_tree_view_expand_all (GTK_TREE_VIEW (e->priv->treeview));
  glade_base_editor_set_cursor (e, &e->priv->iter);
  glade_base_editor_block_callbacks (e, FALSE);

  return FALSE;
1063 1064 1065
}

static void
1066 1067 1068
glade_base_editor_row_inserted (GtkTreeModel * model,
                                GtkTreePath * path,
                                GtkTreeIter * iter, GladeBaseEditor * e)
1069
{
1070 1071 1072
  e->priv->iter = *iter;
  glade_base_editor_block_callbacks (e, TRUE);
  g_idle_add (glade_base_editor_drag_and_drop_idle, e);
1073 1074 1075
}

static void
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
glade_base_editor_project_remove_widget (GladeProject * project,
                                         GladeWidget * widget,
                                         GladeBaseEditor * e)
{
  if (widget == e->priv->gcontainer)
    {
      glade_base_editor_set_container (e, NULL);
      return;
    }

  if (glade_base_editor_is_child (e, widget, TRUE))
    {
      GtkTreeIter iter;
      if (glade_base_editor_find_child (e, widget, &iter))
        {
          gtk_tree_store_remove (GTK_TREE_STORE (e->priv->model), &iter);
          glade_base_editor_clear (e);
        }
    }

1096 1097
  if (glade_widget_get_internal (widget) && 
      glade_base_editor_is_child (e, widget, FALSE))
1098
    glade_base_editor_update_properties (e);
1099 1100 1101
}

static void
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
glade_base_editor_project_add_widget (GladeProject * project,
                                      GladeWidget * widget, GladeBaseEditor * e)
{
  if (e->priv->updating_treeview)
    return;

  if (glade_base_editor_is_child (e, widget, TRUE))
    {
      e->priv->updating_treeview = TRUE;
      g_idle_add (glade_base_editor_update_treeview_idle, e);
    }

1114 1115
  if (glade_widget_get_internal (widget) && 
      glade_base_editor_is_child (e, widget, FALSE))
1116
    glade_base_editor_update_properties (e);
1117 1118 1119
}

static gboolean
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
glade_base_editor_update_display_name (GtkTreeModel * model,
                                       GtkTreePath * path,
                                       GtkTreeIter * iter, gpointer data)
{
  GladeBaseEditor *editor = (GladeBaseEditor *) data;
  GladeWidget *gchild;
  gchar *name;

  gtk_tree_model_get (model, iter, GLADE_BASE_EDITOR_GWIDGET, &gchild, -1);

  name = glade_base_editor_get_display_name (editor, gchild);

  gtk_tree_store_set (GTK_TREE_STORE (editor->priv->model), iter,
                      GLADE_BASE_EDITOR_NAME, name, -1);
  g_free (name);
  g_object_unref (G_OBJECT (gchild));

  return FALSE;
1138 1139 1140
}

static void
1141 1142 1143
glade_base_editor_project_changed (GladeProject * project,
                                   GladeCommand * command,
                                   gboolean forward, GladeBaseEditor * editor)
1144
{
1145 1146
  gtk_tree_model_foreach (editor->priv->model,
                          glade_base_editor_update_display_name, editor);
1147 1148 1149 1150 1151
}



static void
1152
glade_base_editor_project_disconnect (GladeBaseEditor * editor)
1153
{
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
  GladeBaseEditorPrivate *e = editor->priv;

  if (e->project == NULL)
    return;

  g_signal_handlers_disconnect_by_func (e->project,
                                        glade_base_editor_project_closed,
                                        editor);

  g_signal_handlers_disconnect_by_func (e->project,
                                        glade_base_editor_project_remove_widget,
                                        editor);
1166

1167 1168 1169
  g_signal_handlers_disconnect_by_func (e->project,
                                        glade_base_editor_project_add_widget,
                                        editor);
1170

1171 1172 1173
  g_signal_handlers_disconnect_by_func (e->project,
                                        glade_base_editor_project_widget_name_changed,
                                        editor);
1174

1175 1176 1177 1178 1179 1180 1181 1182
  g_signal_handlers_disconnect_by_func (e->project,
                                        glade_base_editor_project_changed,
                                        editor);


  if (e->properties_idle)
    g_source_remove (e->properties_idle);
  e->properties_idle = 0;
1183 1184 1185
}

static void
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
glade_base_editor_set_container (GladeBaseEditor * editor, GObject * container)
{
  GladeBaseEditorPrivate *e = editor->priv;

  glade_base_editor_project_disconnect (editor);

  if (container == NULL)
    {
      reset_child_types (editor);

      e->gcontainer = NULL;
      e->project = NULL;
      glade_base_editor_block_callbacks (editor, TRUE);
      glade_base_editor_clear (editor);

      gtk_tree_view_set_model (GTK_TREE_VIEW (editor->priv->treeview), NULL);
      gtk_tree_store_clear (GTK_TREE_STORE (editor->priv->model));
      gtk_tree_view_set_model (GTK_TREE_VIEW (editor->priv->treeview),
                               editor->priv->model);

      gtk_widget_set_sensitive (e->paned, FALSE);
      glade_base_editor_block_callbacks (editor, FALSE);

      glade_signal_editor_load_widget (e->signal_editor, NULL);

      g_object_notify (G_OBJECT (editor), "container");
      return;
    }

  gtk_widget_set_sensitive (e->paned, TRUE);

  e->gcontainer = glade_widget_get_from_gobject (container);

  e->project = glade_widget_get_project (e->gcontainer);

  g_signal_connect (e->project, "close",
                    G_CALLBACK (glade_base_editor_project_closed), editor);

  g_signal_connect (e->project, "remove-widget",
                    G_CALLBACK (glade_base_editor_project_remove_widget),
                    editor);

  g_signal_connect (e->project, "add-widget",
                    G_CALLBACK (glade_base_editor_project_add_widget), editor);

  g_signal_connect (e->project, "widget-name-changed",
                    G_CALLBACK (glade_base_editor_project_widget_name_changed),
                    editor);

  g_signal_connect (e->project, "changed",
                    G_CALLBACK (glade_base_editor_project_changed), editor);

  g_object_notify (G_OBJECT (editor), "container");
1239 1240 1241 1242
}

/*************************** GladeBaseEditor Class ****************************/
static void
1243
glade_base_editor_finalize (GObject * object)
1244
{
1245
  GladeBaseEditor *cobj = GLADE_BASE_EDITOR (object);
1246

1247
  g_free (cobj->priv);
1248

1249
  G_OBJECT_CLASS (parent_class)->finalize (object);
1250 1251 1252
}

static void
1253
glade_base_editor_dispose (GObject * object)
1254
{
1255
  GladeBaseEditor *cobj = GLADE_BASE_EDITOR (object);
1256

1257
  reset_child_types (cobj);
1258

1259 1260
  glade_base_editor_project_disconnect (cobj);
  cobj->priv->project = NULL;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1261

1262
  G_OBJECT_CLASS (parent_class)->dispose (object);
1263 1264
}

1265
static void
1266 1267 1268
glade_base_editor_set_property (GObject * object,
                                guint prop_id,
                                const GValue * value, GParamSpec * pspec)
1269
{
1270
  GladeBaseEditor *editor = GLADE_BASE_EDITOR (object);
1271

1272 1273 1274 1275 1276 1277 1278 1279 1280
  switch (prop_id)
    {
      case PROP_CONTAINER:
        glade_base_editor_set_container (editor, g_value_get_object (value));
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
1281 1282 1283
}

static void
1284 1285 1286
glade_base_editor_get_property (GObject * object,
                                guint prop_id,
                                GValue * value, GParamSpec * pspec)
1287
{
1288
  GladeBaseEditor *editor = GLADE_BASE_EDITOR (object);
1289

1290 1291 1292
  switch (prop_id)
    {
      case PROP_CONTAINER:
1293
        g_value_set_object (value, glade_widget_get_object (editor->priv->gcontainer));
1294 1295 1296 1297 1298
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
1299 1300 1301
}


1302 1303
/* Default handlers */
static gboolean
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
glade_base_editor_change_type (GladeBaseEditor * editor,
                               GladeWidget * gchild, GType type)
{
  GladeWidget *parent, *gchild_new;
  GList *children, *l;
  GObject *child, *child_new;
  GtkTreeIter iter;
  gchar *name, *class_name;

  parent = glade_widget_get_parent (gchild);

  if (glade_base_editor_get_type_info (editor, NULL, type,
                                       GLADE_BASE_EDITOR_CLASS_NAME,
                                       &class_name, -1) == FALSE)
    return TRUE;

  child = glade_widget_get_object (gchild);
  name = g_strdup (glade_widget_get_name (gchild));
  glade_base_editor_find_child (editor, gchild, &iter);

  /* Create new widget */
  gchild_new = glade_base_editor_delegate_build_child (editor, parent, type);

  child_new = glade_widget_get_object (gchild_new);

  /* Cut and Paste childrens */
  if ((children = glade_widget_get_children (gchild)) != NULL)
    {
      GList *gchildren = NULL;

      l = children;
      while (l)
        {
          GladeWidget *w = glade_widget_get_from_gobject (l->data);

1339
          if (w && !glade_widget_get_internal (w))
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
            gchildren = g_list_prepend (gchildren, w);

          l = g_list_next (l);
        }

      if (gchildren)
        {
          glade_command_dnd (gchildren, gchild_new, NULL);

          g_list_free (children);
          g_list_free (gchildren);
        }
    }

  /* Copy properties */
  glade_widget_copy_properties (gchild_new, gchild, TRUE, TRUE);

  /* Delete old widget */
  glade_base_editor_delegate_delete_child (editor, parent, gchild);

  /* Apply packing properties to the new object 
   * 
   * No need to use GladeCommand here on the newly created widget,
   * they just become the initial state for this object.
   */
1365
  l = glade_widget_get_packing_properties (gchild);
1366 1367
  while (l)
    {
1368 1369
      GladeProperty      *orig_prop = (GladeProperty *) l->data;
      GladePropertyClass *pclass = glade_property_get_class (orig_prop);
1370
      GladeProperty      *dup_prop = glade_widget_get_property (gchild_new, glade_property_class_id (pclass));
1371
      glade_property_set_value (dup_prop, glade_property_inline_value (orig_prop));
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
      l = g_list_next (l);
    }

  /* Set the name */
  glade_command_set_name (gchild_new, name);

  if (GTK_IS_WIDGET (child_new))
    gtk_widget_show_all (GTK_WIDGET (child_new));

  gtk_tree_store_set (GTK_TREE_STORE (editor->priv->model), &iter,
                      GLADE_BASE_EDITOR_GWIDGET, gchild_new,
                      GLADE_BASE_EDITOR_OBJECT, child_new,
                      GLADE_BASE_EDITOR_TYPE_NAME, class_name, -1);
  g_free (class_name);
  g_free (name);

  return TRUE;
1389 1390 1391
}

static gchar *
1392 1393
glade_base_editor_get_display_name_impl (GladeBaseEditor * editor,
                                         GladeWidget * gchild)
1394
{
1395
  return g_strdup (glade_widget_get_name (gchild));
1396 1397 1398
}

static GladeWidget *
1399 1400
glade_base_editor_build_child (GladeBaseEditor * editor,
                               GladeWidget * gparent, GType type)
1401
{
1402 1403 1404
  return glade_command_create (glade_widget_adaptor_get_by_type (type),
                               gparent, NULL,
                               glade_widget_get_project (gparent));
1405 1406 1407
}

static gboolean
1408 1409
glade_base_editor_move_child (GladeBaseEditor * editor,
                              GladeWidget * gparent, GladeWidget * gchild)
1410
{
1411
  GList list = { 0, };
1412

1413 1414 1415 1416 1417
  if (gparent != glade_widget_get_parent (gchild))
    {
      list.data = gchild;
      glade_command_dnd (&list, gparent, NULL);
    }
1418

1419
  return TRUE;
1420 1421 1422
}

static gboolean
1423 1424 1425
glade_base_editor_delete_child_impl (GladeBaseEditor * editor,
                                     GladeWidget * gparent,
                                     GladeWidget * gchild)
1426
{
1427
  GList list = { 0, };
1428

1429 1430 1431 1432
  list.data = gchild;
  glade_command_delete (&list);

  return TRUE;
1433 1434 1435
}

static void
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
glade_base_editor_class_init (GladeBaseEditorClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = glade_base_editor_finalize;
  object_class->dispose = glade_base_editor_dispose;
  object_class->set_property = glade_base_editor_set_property;
  object_class->get_property = glade_base_editor_get_property;

  klass->change_type = glade_base_editor_change_type;
  klass->get_display_name = glade_base_editor_get_display_name_impl;
  klass->build_child = glade_base_editor_build_child;
  klass->delete_child = glade_base_editor_delete_child_impl;
  klass->move_child = glade_base_editor_move_child;

  g_object_class_install_property (object_class, PROP_CONTAINER,
                                   g_param_spec_object
                                   ("container", _("Container"),
                                    _
                                    ("The container object this editor is currently editing"),
                                    G_TYPE_OBJECT, G_PARAM_READWRITE));

1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
  /**
   * GladeBaseEditor::child-selected:
   * @gladebaseeditor: the #GladeBaseEditor which received the signal.
   * @gchild: the selected #GladeWidget.
   *
   * Emited when the user selects a child in the editor's treeview.
   * You can add the relevant child properties here using 
   * glade_base_editor_add_default_properties() and glade_base_editor_add_properties() 
   * You can also add labels with glade_base_editor_add_label to make the
   * editor look pretty.
   */
1471 1472 1473 1474 1475 1476 1477 1478
  glade_base_editor_signals[SIGNAL_CHILD_SELECTED] =
      g_signal_new ("child-selected",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeBaseEditorClass, child_selected),
                    NULL, NULL,
                    glade_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);

1479 1480 1481 1482 1483 1484 1485 1486
  /**
   * GladeBaseEditor::child-change-type:
   * @gladebaseeditor: the #GladeBaseEditor which received the signal.
   * @child: the #GObject being changed.
   * @type: the new type for @child.
   *
   * Returns: TRUE to stop signal emision.
   */
1487 1488 1489 1490 1491 1492 1493 1494 1495
  glade_base_editor_signals[SIGNAL_CHANGE_TYPE] =
      g_signal_new ("change-type",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeBaseEditorClass, change_type),
                    glade_boolean_handled_accumulator, NULL,
                    glade_marshal_BOOLEAN__OBJECT_UINT,
                    G_TYPE_BOOLEAN, 2, G_TYPE_OBJECT, G_TYPE_UINT);

1496 1497 1498 1499 1500 1501 1502 1503
  /**
   * GladeBaseEditor::get-display-name:
   * @gladebaseeditor: the #GladeBaseEditor which received the signal.
   * @gchild: the child to get display name string to show in @gladebaseeditor
   * treeview.
   *
   * Returns: a newly allocated string.
   */
1504 1505 1506 1507 1508 1509 1510 1511 1512
  glade_base_editor_signals[SIGNAL_GET_DISPLAY_NAME] =
      g_signal_new ("get-display-name",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeBaseEditorClass, get_display_name),
                    glade_string_accumulator, NULL,
                    glade_marshal_STRING__OBJECT,
                    G_TYPE_STRING, 1, G_TYPE_OBJECT);

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
  /**
   * GladeBaseEditor::build-child:
   * @gladebaseeditor: the #GladeBaseEditor which received the signal.
   * @gparent: the parent of the new child
   * @type: the #GType of the child
   *
   * Create a child widget here if something else must be done other than
   * calling glade_command_create() such as creating an intermediate parent.
   *
   * Returns: the newly created #GladeWidget or NULL if child cant be created
   */
1524 1525 1526 1527 1528 1529 1530 1531 1532
  glade_base_editor_signals[SIGNAL_BUILD_CHILD] =
      g_signal_new ("build-child",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeBaseEditorClass, build_child),
                    glade_stop_emission_accumulator, NULL,
                    glade_marshal_OBJECT__OBJECT_UINT,
                    G_TYPE_OBJECT, 2, G_TYPE_OBJECT, G_TYPE_UINT);

1533 1534 1535 1536 1537 1538
  /**
   * GladeBaseEditor::delete-child:
   * @gladebaseeditor: the #GladeBaseEditor which received the signal.
   * @gparent: the parent
   * @gchild: the child to delete
   */
1539 1540 1541 1542 1543 1544 1545 1546 1547
  glade_base_editor_signals[SIGNAL_DELETE_CHILD] =
      g_signal_new ("delete-child",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeBaseEditorClass, delete_child),
                    glade_boolean_handled_accumulator, NULL,
                    glade_marshal_BOOLEAN__OBJECT_OBJECT,
                    G_TYPE_BOOLEAN, 2, G_TYPE_OBJECT, G_TYPE_OBJECT);

1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
  /**
   * GladeBaseEditor::move-child:
   * @gladebaseeditor: the #GladeBaseEditor which received the signal.
   * @gparent: the new parent of @gchild
   * @gchild: the #GladeWidget to move
   *
   * Move child here if something else must be done other than cut & paste.
   *
   * Returns: wheater child has been sucessfully moved or not.
   */
1558 1559 1560 1561 1562 1563 1564 1565
  glade_base_editor_signals[SIGNAL_MOVE_CHILD] =
      g_signal_new ("move-child",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeBaseEditorClass, move_child),
                    glade_stop_emission_accumulator, NULL,
                    glade_marshal_BOOLEAN__OBJECT_OBJECT,
                    G_TYPE_BOOLEAN, 2, G_TYPE_OBJECT, G_TYPE_OBJECT);
1566 1567 1568
}

static void
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
glade_base_editor_block_callbacks (GladeBaseEditor * editor, gboolean block)
{
  GladeBaseEditorPrivate *e = editor->priv;
  if (block)
    {
      g_signal_handlers_block_by_func (e->model, glade_base_editor_row_inserted,
                                       editor);
      if (e->project)
        {
          g_signal_handlers_block_by_func (e->project,
                                           glade_base_editor_project_remove_widget,
                                           editor);
          g_signal_handlers_block_by_func (e->project,
                                           glade_base_editor_project_add_widget,
                                           editor);
          g_signal_handlers_block_by_func (e->project,
                                           glade_base_editor_project_changed,
                                           editor);
        }
    }
  else
    {
      g_signal_handlers_unblock_by_func (e->model,
                                         glade_base_editor_row_inserted,
                                         editor);
      if (e->project)
        {
          g_signal_handlers_unblock_by_func (e->project,
                                             glade_base_editor_project_remove_widget,
                                             editor);
          g_signal_handlers_unblock_by_func (e->project,
                                             glade_base_editor_project_add_widget,
                                             editor);
          g_signal_handlers_unblock_by_func (e->project,
                                             glade_base_editor_project_changed,
                                             editor);
        }
    }
1607 1608 1609
}

static void
1610
glade_base_editor_realize_callback (GtkWidget * widget, gpointer user_data)
1611
{
1612
  GladeBaseEditor *editor = GLADE_BASE_EDITOR (widget);
1613

1614 1615 1616 1617 1618 1619
  glade_base_editor_block_callbacks (editor, TRUE);

  glade_base_editor_fill_store (editor);
  gtk_tree_view_expand_all (GTK_TREE_VIEW (editor->priv->treeview));

  glade_base_editor_block_callbacks (editor, FALSE);
1620 1621
}

1622

1623 1624 1625 1626
static void
glade_base_editor_switch_page (GtkNotebook * notebook,
                               GtkWidget * page,
                               guint page_num, GladeBaseEditor * editor)
1627
{
1628
  GladeBaseEditorPrivate *e = editor->priv;
1629

1630 1631 1632 1633 1634 1635
  if (page_num == 0)
    glade_signal_editor_load_widget (e->signal_editor, e->gcontainer);
  else
    {
      GtkTreeIter iter;
      GladeWidget *gchild = NULL;
1636

1637 1638 1639 1640 1641 1642
      if (glade_base_editor_get_child_selected (editor, &iter))
        {
          gtk_tree_model_get (e->model, &iter,
                              GLADE_BASE_EDITOR_GWIDGET, &gchild, -1);
          g_object_unref (G_OBJECT (gchild));
        }
1643

1644 1645 1646 1647 1648
      if (gchild)
        glade_signal_editor_load_widget (e->signal_editor, gchild);
      else
        glade_signal_editor_load_widget (e->signal_editor, NULL);
    }
1649 1650 1651
}


1652
static void
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
glade_base_editor_init (GladeBaseEditor * editor)
{
  GladeBaseEditorPrivate *e;
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkWidget *paned, *hbox, *vbox, *tree_vbox, *scroll, *button_table, *button,
      *label;

  gtk_box_set_spacing (GTK_BOX (editor), 8);

  e = editor->priv = g_new0 (GladeBaseEditorPrivate, 1);

  /* Paned */
  e->paned = paned = gtk_vpaned_new ();
  gtk_widget_show (paned);
  gtk_box_pack_start (GTK_BOX (editor), e->paned, TRUE, TRUE, 0);

  /* Notebook */
  e->notebook = gtk_notebook_new ();
  gtk_widget_show (e->notebook);
  gtk_paned_pack1 (GTK_PANED (paned), e->notebook, TRUE, FALSE);

  /* ScrolledWindow */
  e->main_scroll = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (e->main_scroll);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (e->main_scroll),
                                       GTK_SHADOW_NONE);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (e->main_scroll),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  label = gtk_label_new (_("General"));
  gtk_widget_show (label);
  gtk_notebook_append_page (GTK_NOTEBOOK (e->notebook), e->main_scroll, label);

  /* Hbox */
  hbox = gtk_hbox_new (FALSE, 8);
  gtk_widget_show (hbox);

  label = gtk_label_new (_("Hierarchy"));
  gtk_widget_show (label);
  gtk_notebook_append_page (GTK_NOTEBOOK (e->notebook), hbox, label);

  /* TreeView Vbox */
  tree_vbox = gtk_vbox_new (FALSE, 8);
  gtk_widget_show (tree_vbox);
  gtk_box_pack_start (GTK_BOX (hbox), tree_vbox, FALSE, TRUE, 0);

  /* ScrolledWindow */
  scroll = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (scroll);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll),
                                       GTK_SHADOW_NONE);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
                                  GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  gtk_box_pack_start (GTK_BOX (tree_vbox), scroll, TRUE, TRUE, 0);

  /* TreeView */
  e->treeview = gtk_tree_view_new ();
  gtk_widget_show (e->treeview);
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (e->treeview), TRUE);
  gtk_tree_view_set_reorderable (GTK_TREE_VIEW (