glade-command.c 75.2 KB
Newer Older
1
/*
2
 * Copyright (C) 2002 Joaquín Cuenca Abela
3
4
5
6
7
8
9
10
11
12
13
14
15
 *
 * 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
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18
 *
 * Authors:
19
 *   Joaquín Cuenca Abela <e98cuenc@yahoo.com>
20
 *   Archit Baweja <bighead@users.sourceforge.net>
21
 */
22
23
24
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
Paolo Borelli's avatar
Paolo Borelli committed
25

26
27
28
29
30
31
32
33
/**
 * SECTION:glade-command
 * @Short_Description: An event filter to implement the Undo/Redo stack.
 *
 * The Glade Command api allows us to view user actions as items and execute 
 * and undo those items; each #GladeProject has its own Undo/Redo stack.
 */

34
35
#include <gtk/gtk.h>
#include <string.h>
36
#include <glib/gi18n-lib.h>
37
#include "glade.h"
38
39
40
#include "glade-project.h"
#include "glade-xml-utils.h"
#include "glade-widget.h"
41
#include "glade-palette.h"
42
43
#include "glade-command.h"
#include "glade-property.h"
Paolo Borelli's avatar
Paolo Borelli committed
44
#include "glade-property-class.h"
45
46
#include "glade-debug.h"
#include "glade-placeholder.h"
47
#include "glade-clipboard.h"
48
#include "glade-signal.h"
49
#include "glade-app.h"
50
#include "glade-fixed.h"
51
#include "glade-name-context.h"
Tristan Van Berkom's avatar
Tristan Van Berkom committed
52
53
54
55
56
57
58

/* Concerning placeholders: we do not hold any reference to placeholders,
 * placeholders that are supplied by the backend are not reffed, placeholders
 * that are created by glade-command are temporarily owned by glade-command
 * untill they are added to a container; in which case it belongs to GTK+
 * and the backend (but I prefer to think of it as the backend).
 */
59
60
61
62
63
64
65
66
67
68
69
typedef struct
{
  GladeWidget *widget;
  GladeWidget *parent;
  GladeProject *project;
  GList *reffed;
  GladePlaceholder *placeholder;
  gboolean props_recorded;
  GList *pack_props;
  gchar *special_type;
  gulong handler_id;
70
} CommandData;
71

72
73
74
75
static GObjectClass *parent_class = NULL;

/* Group description used for the current group
 */
76
static gchar *gc_group_description = NULL;
77
78
79

/* Use an id to avoid grouping together consecutive groups
 */
80
static gint gc_group_id = 1;
81
82
83

/* Groups can be grouped together, push/pop must balance (duh!)
 */
84
static gint gc_group_depth = 0;
85

86
87

#define MAKE_TYPE(func, type, parent)			\
88
GType							\
89
90
func ## _get_type (void)				\
{							\
91
	static GType cmd_type = 0;			\
92
							\
93
	if (!cmd_type)					\
94
	{						\
95
		static const GTypeInfo info =		\
96
97
		{					\
			sizeof (type ## Class),		\
98
99
100
101
			(GBaseInitFunc) NULL,		\
			(GBaseFinalizeFunc) NULL,	\
			(GClassInitFunc) func ## _class_init,	\
			(GClassFinalizeFunc) NULL,	\
102
103
104
			NULL,				\
			sizeof (type),			\
			0,				\
105
			(GInstanceInitFunc) NULL	\
106
107
		};					\
							\
108
		cmd_type = g_type_register_static (parent, #type, &info, 0);	\
109
110
	}						\
							\
111
	return cmd_type;				\
112
113
114
}							\

static void
115
glade_command_finalize (GObject * obj)
116
{
117
118
  GladeCommand *cmd = (GladeCommand *) obj;
  g_return_if_fail (cmd != NULL);
119

120
121
  if (cmd->description)
    g_free (cmd->description);
122

123
124
  /* Call the base class dtor */
  (*G_OBJECT_CLASS (parent_class)->finalize) (obj);
125
126
127
}

static gboolean
128
glade_command_unifies_impl (GladeCommand * this_cmd, GladeCommand * other_cmd)
129
{
130
  return FALSE;
131
132
133
}

static void
134
glade_command_collapse_impl (GladeCommand * this_cmd, GladeCommand * other_cmd)
135
{
136
  g_return_if_reached ();
137
138
139
}

static void
140
glade_command_class_init (GladeCommandClass * klass)
141
{
142
  GObjectClass *object_class;
143

144
145
  object_class = G_OBJECT_CLASS (klass);
  parent_class = g_type_class_peek_parent (klass);
146

147
  object_class->finalize = glade_command_finalize;
148

149
150
151
152
  klass->undo = NULL;
  klass->execute = NULL;
  klass->unifies = glade_command_unifies_impl;
  klass->collapse = glade_command_collapse_impl;
153
154
}

155
156
/* compose the _get_type function for GladeCommand */
MAKE_TYPE (glade_command, GladeCommand, G_TYPE_OBJECT)
157
158
159
160
161
162
163
164
#define GLADE_MAKE_COMMAND(type, func)					\
static gboolean								\
func ## _undo (GladeCommand *me);					\
static gboolean								\
func ## _execute (GladeCommand *me);					\
static void								\
func ## _finalize (GObject *object);					\
static gboolean								\
165
func ## _unifies (GladeCommand *this_cmd, GladeCommand *other_cmd);		\
166
static void								\
167
func ## _collapse (GladeCommand *this_cmd, GladeCommand *other_cmd);		\
168
169
170
171
172
static void								\
func ## _class_init (gpointer parent_tmp, gpointer notused)		\
{									\
	GladeCommandClass *parent = parent_tmp;				\
	GObjectClass* object_class;					\
173
	object_class = G_OBJECT_CLASS (parent);				\
174
175
	parent->undo =  func ## _undo;					\
	parent->execute =  func ## _execute;				\
176
177
	parent->unifies =  func ## _unifies;				\
	parent->collapse =  func ## _collapse;				\
178
179
180
181
182
	object_class->finalize = func ## _finalize;			\
}									\
typedef struct {							\
	GladeCommandClass cmd;						\
} type ## Class;							\
183
static MAKE_TYPE(func, type, GLADE_TYPE_COMMAND)
184
185
186
187
188
189
190
191
/**
 * glade_command_execute:
 * @command: A #GladeCommand
 *
 * Executes @command
 *
 * Returns: whether the command was successfully executed
 */
192
193
    gboolean
glade_command_execute (GladeCommand * command)
194
{
195
196
  g_return_val_if_fail (GLADE_IS_COMMAND (command), FALSE);
  return GLADE_COMMAND_GET_CLASS (command)->execute (command);
197
198
199
}


200
201
202
203
204
205
/**
 * glade_command_undo:
 * @command: A #GladeCommand
 *
 * Undo the effects of @command
 *
206
 * Returns: whether the command was successfully reversed
207
208
 */
gboolean
209
glade_command_undo (GladeCommand * command)
210
{
211
212
  g_return_val_if_fail (GLADE_IS_COMMAND (command), FALSE);
  return GLADE_COMMAND_GET_CLASS (command)->undo (command);
213
214
}

215
/**
216
217
218
219
220
221
 * glade_command_unifies:
 * @command: A #GladeCommand
 * @other: another #GladeCommand
 *
 * Checks whether @command and @other can be unified
 * to make one single command.
222
 *
223
 * Returns: whether they can be unified.
224
 */
225
gboolean
226
glade_command_unifies (GladeCommand * command, GladeCommand * other)
227
{
228
  g_return_val_if_fail (command, FALSE);
229

230
231
232
233
234
  /* Cannot unify with a part of a command group.
   * Unify atomic commands only
   */
  if (command->group_id != 0 || (other && other->group_id != 0))
    return FALSE;
235

236
  return GLADE_COMMAND_GET_CLASS (command)->unifies (command, other);
237
238
}

239
/**
240
241
242
 * glade_command_collapse:
 * @command: A #GladeCommand
 * @other: another #GladeCommand
243
 *
244
245
 * Merges @other into @command, so that @command now
 * covers both commands and @other can be dispensed with.
246
 */
247
void
248
glade_command_collapse (GladeCommand * command, GladeCommand * other)
249
{
250
251
  g_return_if_fail (command);
  GLADE_COMMAND_GET_CLASS (command)->collapse (command, other);
252
253
254
255
}

/**
 * glade_command_push_group:
256
 * @fmt:         The collective desctiption of the command group.
257
258
 *               only the description of the first group on the 
 *               stack is used when embedding groups.
259
 * @...: args to the format string.
260
261
262
263
 *
 * Marks the begining of a group.
 */
void
264
glade_command_push_group (const gchar * fmt, ...)
265
{
266
  va_list args;
267

268
  g_return_if_fail (fmt);
269

270
271
272
273
274
275
276
277
  /* Only use the description for the first group.
   */
  if (gc_group_depth++ == 0)
    {
      va_start (args, fmt);
      gc_group_description = g_strdup_vprintf (fmt, args);
      va_end (args);
    }
278
279
280
281
282
283
284
285
286
287
}

/**
 * glade_command_pop_group:
 *
 * Mark the end of a command group.
 */
void
glade_command_pop_group (void)
{
288
289
290
291
292
  if (gc_group_depth-- == 1)
    {
      gc_group_description = (g_free (gc_group_description), NULL);
      gc_group_id++;
    }
293

294
295
  if (gc_group_depth < 0)
    g_critical ("Unbalanced group stack detected in %s\n", G_STRFUNC);
296
}
297

298
299
300
gint
glade_command_get_group_depth (void)
{
301
  return gc_group_depth;
302
303
}

304
static void
305
glade_command_check_group (GladeCommand * cmd)
306
{
307
308
309
310
311
312
313
  g_return_if_fail (GLADE_IS_COMMAND (cmd));
  if (gc_group_description)
    {
      cmd->description =
          (g_free (cmd->description), g_strdup (gc_group_description));
      cmd->group_id = gc_group_id;
    }
314
315
}

316
317
/**************************************************/
/*******     GLADE_COMMAND_SET_PROPERTY     *******/
318
319
/**************************************************/

320
321
/* create a new GladeCommandSetProperty class.  Objects of this class will
 * encapsulate a "set property" operation */
322

323
324
325
326
327
328
typedef struct
{
  GladeCommand parent;
  gboolean set_once;
  gboolean undo;
  GList *sdata;
329
} GladeCommandSetProperty;
330

331
/* standard macros */
332
333
334
335
GLADE_MAKE_COMMAND (GladeCommandSetProperty, glade_command_set_property);
#define GLADE_COMMAND_SET_PROPERTY_TYPE		(glade_command_set_property_get_type ())
#define GLADE_COMMAND_SET_PROPERTY(o)	  	(G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_SET_PROPERTY_TYPE, GladeCommandSetProperty))
#define GLADE_COMMAND_SET_PROPERTY_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_SET_PROPERTY_TYPE, GladeCommandSetPropertyClass))
336
337
#define GLADE_IS_COMMAND_SET_PROPERTY(o)	(G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_SET_PROPERTY_TYPE))
#define GLADE_IS_COMMAND_SET_PROPERTY_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_SET_PROPERTY_TYPE))
338

339
/* Undo the last "set property command" */
340
static gboolean
341
glade_command_set_property_undo (GladeCommand * cmd)
342
{
343
  return glade_command_set_property_execute (cmd);
344
345
}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
346
/*
347
348
 * Execute the set property command and revert it. IE, after the execution of 
 * this function cmd will point to the undo action
349
350
 */
static gboolean
351
glade_command_set_property_execute (GladeCommand * cmd)
352
{
353
354
355
356
357
358
  GladeCommandSetProperty *me = (GladeCommandSetProperty *) cmd;
  GList *l;
  gboolean success;
  gboolean retval = FALSE;

  g_return_val_if_fail (me != NULL, FALSE);
359

360
361
  if (me->set_once != FALSE)
    glade_property_push_superuser ();
362

363
364
365
366
  for (l = me->sdata; l; l = l->next)
    {
      GValue new_value = { 0, };
      GCSetPropData *sdata = l->data;
367

368
      g_value_init (&new_value, G_VALUE_TYPE (sdata->new_value));
369

370
371
372
373
      if (me->undo)
        g_value_copy (sdata->old_value, &new_value);
      else
        g_value_copy (sdata->new_value, &new_value);
374

375
#if 0
376
377
378
379
380
381
382
383
384
385
386
387
388
      {
        gchar *str =
            glade_widget_adaptor_string_from_value
            (GLADE_WIDGET_ADAPTOR (sdata->property->klass->handle),
             sdata->property->klass, &new_value);

        g_print ("Setting %s property of %s to %s (sumode: %d)\n",
                 sdata->property->klass->id,
                 sdata->property->widget->name,
                 str, glade_property_superuser ());

        g_free (str);
      }
389
390
#endif

391
392
393
394
395
396
      /* Packing properties need to be refreshed here since
       * they are reset when they get added to containers.
       */
      if (sdata->property->klass->packing)
        {
          GladeProperty *tmp_prop;
397

398
399
          tmp_prop = glade_widget_get_pack_property
              (sdata->property->widget, sdata->property->klass->id);
400

401
402
403
404
          if (sdata->property != tmp_prop)
            {
              g_object_unref (sdata->property);
              sdata->property = g_object_ref (tmp_prop);
405

406
407
            }
        }
408

409
410
      success = glade_property_set_value (sdata->property, &new_value);
      retval = retval || success;
411

412
413
414
415
416
417
418
419
      if (!me->set_once && success)
        {
          /* If some verify functions didnt pass on 
           * the first go.. we need to record the actual
           * properties here.
           */
          g_value_copy (sdata->property->value, sdata->new_value);
        }
420

421

422
      g_value_unset (&new_value);
423

424

425
    }
426

427
428
  if (me->set_once != FALSE)
    glade_property_pop_superuser ();
429

430
431
432
433
  me->set_once = TRUE;
  me->undo = !me->undo;

  return retval;
434
435
436
}

static void
437
glade_command_set_property_finalize (GObject * obj)
438
{
439
440
441
442
  GladeCommandSetProperty *me;
  GList *l;

  me = GLADE_COMMAND_SET_PROPERTY (obj);
443

444
445
446
  for (l = me->sdata; l; l = l->next)
    {
      GCSetPropData *sdata = l->data;
447

448
449
      if (sdata->property)
        g_object_unref (G_OBJECT (sdata->property));
450

451
452
453
454
455
456
457
458
459
      if (sdata->old_value)
        {
          if (G_VALUE_TYPE (sdata->old_value) != 0)
            g_value_unset (sdata->old_value);
          g_free (sdata->old_value);
        }
      if (G_VALUE_TYPE (sdata->new_value) != 0)
        g_value_unset (sdata->new_value);
      g_free (sdata->new_value);
460

461
462
    }
  glade_command_finalize (obj);
463
464
465
}

static gboolean
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
524
525
526
glade_command_set_property_unifies (GladeCommand * this_cmd,
                                    GladeCommand * other_cmd)
{
  GladeCommandSetProperty *cmd1, *cmd2;
  GCSetPropData *pdata1, *pdata2;
  GList *list, *l;

  if (!other_cmd)
    {
      if (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd))
        {
          cmd1 = (GladeCommandSetProperty *) this_cmd;

          for (list = cmd1->sdata; list; list = list->next)
            {
              pdata1 = list->data;

              if (glade_property_class_compare (pdata1->property->klass,
                                                pdata1->old_value,
                                                pdata1->new_value))
                return FALSE;
            }
          return TRUE;

        }
      return FALSE;
    }

  if (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd) &&
      GLADE_IS_COMMAND_SET_PROPERTY (other_cmd))
    {
      cmd1 = (GladeCommandSetProperty *) this_cmd;
      cmd2 = (GladeCommandSetProperty *) other_cmd;

      if (g_list_length (cmd1->sdata) != g_list_length (cmd2->sdata))
        return FALSE;

      for (list = cmd1->sdata; list; list = list->next)
        {
          pdata1 = list->data;
          for (l = cmd2->sdata; l; l = l->next)
            {
              pdata2 = l->data;

              if (pdata1->property->widget == pdata2->property->widget &&
                  glade_property_class_match (pdata1->property->klass,
                                              pdata2->property->klass))
                break;
            }

          /* If both lists are the same length, and one class type
           * is not found in the other list, then we cant unify.
           */
          if (l == NULL)
            return FALSE;

        }

      return TRUE;
    }
  return FALSE;
527
528
529
}

static void
530
531
532
533
534
535
glade_command_set_property_collapse (GladeCommand * this_cmd,
                                     GladeCommand * other_cmd)
{
  GladeCommandSetProperty *cmd1, *cmd2;
  GCSetPropData *pdata1, *pdata2;
  GList *list, *l;
536

537
538
  g_return_if_fail (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd) &&
                    GLADE_IS_COMMAND_SET_PROPERTY (other_cmd));
539

540
541
542
543
544
545
546
547
548
549
  cmd1 = (GladeCommandSetProperty *) this_cmd;
  cmd2 = (GladeCommandSetProperty *) other_cmd;


  for (list = cmd1->sdata; list; list = list->next)
    {
      pdata1 = list->data;
      for (l = cmd2->sdata; l; l = l->next)
        {
          pdata2 = l->data;
550

551
552
553
554
555
556
557
558
559
560
          if (glade_property_class_match (pdata1->property->klass,
                                          pdata2->property->klass))
            {
              /* Merge the GCSetPropData structs manually here
               */
              g_value_copy (pdata2->new_value, pdata1->new_value);
              break;
            }
        }
    }
561

562
563
564
565
566
  /* Set the description
   */
  g_free (this_cmd->description);
  this_cmd->description = other_cmd->description;
  other_cmd->description = NULL;
567

568
569
  glade_app_update_ui ();
}
570

571

572
573
574
575
576
577
578
579
580
581
582
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
#define MAX_UNDO_MENU_ITEM_VALUE_LEN	10
static gchar *
glade_command_set_property_description (GladeCommandSetProperty * me)
{
  GCSetPropData *sdata;
  gchar *description = NULL;
  gchar *value_name;

  g_assert (me->sdata);

  if (g_list_length (me->sdata) > 1)
    description = g_strdup_printf (_("Setting multiple properties"));
  else
    {
      sdata = me->sdata->data;
      value_name = glade_widget_adaptor_string_from_value
          (GLADE_WIDGET_ADAPTOR (sdata->property->klass->handle),
           sdata->property->klass, sdata->new_value);

      g_assert (sdata->property->klass->name);
      g_assert (sdata->property->widget->name);

      if (!value_name || strlen (value_name) > MAX_UNDO_MENU_ITEM_VALUE_LEN
          || strchr (value_name, '_'))
        {
          description = g_strdup_printf (_("Setting %s of %s"),
                                         sdata->property->klass->name,
                                         sdata->property->widget->name);
        }
      else
        {
          description = g_strdup_printf (_("Setting %s of %s to %s"),
                                         sdata->property->klass->name,
                                         sdata->property->widget->name,
                                         value_name);
        }
      g_free (value_name);
    }

  return description;
612
613
}

614

615
void
616
glade_command_set_properties_list (GladeProject * project, GList * props)
617
{
618
619
620
621
622
623
  GladeCommandSetProperty *me;
  GladeCommand *cmd;
  GCSetPropData *sdata;
  GList *list;
  gboolean success;
  gboolean multiple;
624

625
626
  g_return_if_fail (GLADE_IS_PROJECT (project));
  g_return_if_fail (props);
627

628
629
630
  me = (GladeCommandSetProperty *)
      g_object_new (GLADE_COMMAND_SET_PROPERTY_TYPE, NULL);
  cmd = GLADE_COMMAND (me);
631

632
633
634
635
636
637
  /* Ref all props */
  for (list = props; list; list = list->next)
    {
      sdata = list->data;
      g_object_ref (G_OBJECT (sdata->property));
    }
638

639
640
  me->sdata = props;
  cmd->description = glade_command_set_property_description (me);
641
642


643
644
645
  multiple = g_list_length (me->sdata) > 1;
  if (multiple)
    glade_command_push_group (cmd->description);
646

647

648
  glade_command_check_group (GLADE_COMMAND (me));
649

650
651
652
653
654
655
656
657
658
659
660
  /* Push onto undo stack only if it executes successfully. */
  success = glade_command_set_property_execute (GLADE_COMMAND (me));

  if (success)
    glade_project_push_undo (GLADE_PROJECT (project), GLADE_COMMAND (me));
  else
    /* No leaks on my shift! */
    g_object_unref (G_OBJECT (me));

  if (multiple)
    glade_command_pop_group ();
661
662
663
664
}


void
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
glade_command_set_properties (GladeProperty * property,
                              const GValue * old_value,
                              const GValue * new_value, ...)
{
  GCSetPropData *sdata;
  GladeProperty *prop;
  GValue *ovalue, *nvalue;
  GList *list = NULL;
  va_list vl;

  g_return_if_fail (GLADE_IS_PROPERTY (property));

  /* Add first set */
  sdata = g_new (GCSetPropData, 1);
  sdata->property = property;
  sdata->old_value = g_new0 (GValue, 1);
  sdata->new_value = g_new0 (GValue, 1);
  g_value_init (sdata->old_value, G_VALUE_TYPE (old_value));
  g_value_init (sdata->new_value, G_VALUE_TYPE (new_value));
  g_value_copy (old_value, sdata->old_value);
  g_value_copy (new_value, sdata->new_value);
  list = g_list_prepend (list, sdata);

  va_start (vl, new_value);
  while ((prop = va_arg (vl, GladeProperty *)) != NULL)
    {
      ovalue = va_arg (vl, GValue *);
      nvalue = va_arg (vl, GValue *);

      g_assert (GLADE_IS_PROPERTY (prop));
      g_assert (G_IS_VALUE (ovalue));
      g_assert (G_IS_VALUE (nvalue));

      sdata = g_new (GCSetPropData, 1);
      sdata->property = g_object_ref (G_OBJECT (prop));
      sdata->old_value = g_new0 (GValue, 1);
      sdata->new_value = g_new0 (GValue, 1);
      g_value_init (sdata->old_value, G_VALUE_TYPE (ovalue));
      g_value_init (sdata->new_value, G_VALUE_TYPE (nvalue));
      g_value_copy (ovalue, sdata->old_value);
      g_value_copy (nvalue, sdata->new_value);
      list = g_list_prepend (list, sdata);
    }
  va_end (vl);

  glade_command_set_properties_list (property->widget->project, list);
711
712
}

713
void
714
715
glade_command_set_property_value (GladeProperty * property,
                                  const GValue * pvalue)
716
{
717

718
719
720
721
  /* Dont generate undo/redo items for unchanging property values.
   */
  if (glade_property_equals_value (property, pvalue))
    return;
722

723
  glade_command_set_properties (property, property->value, pvalue, NULL);
724
725
}

726
void
727
glade_command_set_property (GladeProperty * property, ...)
728
{
729
730
731
732
  GValue *value;
  va_list args;

  g_return_if_fail (GLADE_IS_PROPERTY (property));
733

734
735
736
737
738
  va_start (args, property);
  value = glade_property_class_make_gvalue_from_vl (property->klass, args);
  va_end (args);

  glade_command_set_property_value (property, value);
739
740
}

741
742
743
744
745
746
/**************************************************/
/*******       GLADE_COMMAND_SET_NAME       *******/
/**************************************************/

/* create a new GladeCommandSetName class.  Objects of this class will
 * encapsulate a "set name" operation */
747
748
749
typedef struct
{
  GladeCommand parent;
750

751
752
753
  GladeWidget *widget;
  gchar *old_name;
  gchar *name;
754
755
756
757
758
759
760
} GladeCommandSetName;

/* standard macros */
GLADE_MAKE_COMMAND (GladeCommandSetName, glade_command_set_name);
#define GLADE_COMMAND_SET_NAME_TYPE		(glade_command_set_name_get_type ())
#define GLADE_COMMAND_SET_NAME(o)	  	(G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_SET_NAME_TYPE, GladeCommandSetName))
#define GLADE_COMMAND_SET_NAME_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_SET_NAME_TYPE, GladeCommandSetNameClass))
761
762
#define GLADE_IS_COMMAND_SET_NAME(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_SET_NAME_TYPE))
#define GLADE_IS_COMMAND_SET_NAME_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_SET_NAME_TYPE))
763
764
765

/* Undo the last "set name command" */
static gboolean
766
glade_command_set_name_undo (GladeCommand * cmd)
767
{
768
  return glade_command_set_name_execute (cmd);
769
770
}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
771
/*
772
773
774
775
 * Execute the set name command and revert it.  Ie, after the execution of this
 * function cmd will point to the undo action
 */
static gboolean
776
glade_command_set_name_execute (GladeCommand * cmd)
777
{
778
779
780
781
782
783
  GladeCommandSetName *me = GLADE_COMMAND_SET_NAME (cmd);
  char *tmp;

  g_return_val_if_fail (me != NULL, TRUE);
  g_return_val_if_fail (me->widget != NULL, TRUE);
  g_return_val_if_fail (me->name != NULL, TRUE);
784

785
  glade_project_set_widget_name (me->widget->project, me->widget, me->name);
786

787
788
789
  tmp = me->old_name;
  me->old_name = me->name;
  me->name = tmp;
790

791
  return TRUE;
792
793
794
}

static void
795
glade_command_set_name_finalize (GObject * obj)
796
{
797
  GladeCommandSetName *me;
798

799
  g_return_if_fail (GLADE_IS_COMMAND_SET_NAME (obj));
800

801
  me = GLADE_COMMAND_SET_NAME (obj);
802

803
804
  g_free (me->old_name);
  g_free (me->name);
805

806
  glade_command_finalize (obj);
807
808
809
}

static gboolean
810
811
glade_command_set_name_unifies (GladeCommand * this_cmd,
                                GladeCommand * other_cmd)
812
{
813
814
  GladeCommandSetName *cmd1;
  GladeCommandSetName *cmd2;
815

816
817
818
819
820
  if (GLADE_IS_COMMAND_SET_NAME (this_cmd) &&
      GLADE_IS_COMMAND_SET_NAME (other_cmd))
    {
      cmd1 = GLADE_COMMAND_SET_NAME (this_cmd);
      cmd2 = GLADE_COMMAND_SET_NAME (other_cmd);
821

822
823
      return (cmd1->widget == cmd2->widget);
    }
824

825
  return FALSE;
826
827
828
}

static void
829
830
glade_command_set_name_collapse (GladeCommand * this_cmd,
                                 GladeCommand * other_cmd)
831
{
832
833
  GladeCommandSetName *nthis = GLADE_COMMAND_SET_NAME (this_cmd);
  GladeCommandSetName *nother = GLADE_COMMAND_SET_NAME (other_cmd);
834

835
836
  g_return_if_fail (GLADE_IS_COMMAND_SET_NAME (this_cmd) &&
                    GLADE_IS_COMMAND_SET_NAME (other_cmd));
837

838
839
840
  g_free (nthis->old_name);
  nthis->old_name = nother->old_name;
  nother->old_name = NULL;
841

842
843
844
  g_free (this_cmd->description);
  this_cmd->description =
      g_strdup_printf (_("Renaming %s to %s"), nthis->name, nthis->old_name);
845

846
  glade_app_update_ui ();
847
848
849
850
}

/* this function takes the ownership of name */
void
851
glade_command_set_name (GladeWidget * widget, const gchar * name)
852
{
853
854
  GladeCommandSetName *me;
  GladeCommand *cmd;
855

856
  g_return_if_fail (GLADE_IS_WIDGET (widget));
857
  g_return_if_fail (name && name[0]);
858

859
860
861
862
  /* Dont spam the queue with false name changes.
   */
  if (!strcmp (widget->name, name))
    return;
863

864
865
  me = g_object_new (GLADE_COMMAND_SET_NAME_TYPE, NULL);
  cmd = GLADE_COMMAND (me);
866

867
868
869
  me->widget = widget;
  me->name = g_strdup (name);
  me->old_name = g_strdup (widget->name);
870

871
872
  cmd->description =
      g_strdup_printf (_("Renaming %s to %s"), me->old_name, me->name);
873

874
  glade_command_check_group (GLADE_COMMAND (me));
875

876
877
878
879
880
  if (glade_command_set_name_execute (GLADE_COMMAND (me)))
    glade_project_push_undo (GLADE_PROJECT (widget->project),
                             GLADE_COMMAND (me));
  else
    g_object_unref (G_OBJECT (me));
881
882
}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
883
884
885
886
887
888
889
/******************************************************************************
 * 
 * add/remove
 * 
 * These canonical commands add/remove a widget list to/from the project.
 * 
 *****************************************************************************/
890

891
892
893
894
895
896
897
typedef struct
{
  GladeCommand parent;
  GladeProject *project;
  GList *widgets;
  gboolean add;
  gboolean from_clipboard;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
898
} GladeCommandAddRemove;
899

Tristan Van Berkom's avatar
Tristan Van Berkom committed
900
901
902
903
904
905
906

GLADE_MAKE_COMMAND (GladeCommandAddRemove, glade_command_add_remove);
#define GLADE_COMMAND_ADD_REMOVE_TYPE			(glade_command_add_remove_get_type ())
#define GLADE_COMMAND_ADD_REMOVE(o)	  			(G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_ADD_REMOVE_TYPE, GladeCommandAddRemove))
#define GLADE_COMMAND_ADD_REMOVE_CLASS(k)		(G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_ADD_REMOVE_TYPE, GladeCommandAddRemoveClass))
#define GLADE_IS_COMMAND_ADD_REMOVE(o)			(G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_ADD_REMOVE_TYPE))
#define GLADE_IS_COMMAND_ADD_REMOVE_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_ADD_REMOVE_TYPE))
907

908
909
static void
glade_command_placeholder_destroyed (GtkWidget * object, CommandData * cdata)
910
{
911
912
913
914
915
  if (GTK_WIDGET (cdata->placeholder) == object)
    {
      cdata->placeholder = NULL;
      cdata->handler_id = 0;
    }
916
917
}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
918
static void
919
920
glade_command_placeholder_connect (CommandData * cdata,
                                   GladePlaceholder * placeholder)
Tristan Van Berkom's avatar
Tristan Van Berkom committed
921
{
922
  g_assert (cdata && cdata->placeholder == NULL);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
923

924
925
926
  /* Something like a no-op with no placeholder */
  if ((cdata->placeholder = placeholder) == NULL)
    return;
927

928
929
930
  cdata->handler_id = g_signal_connect
      (placeholder, "destroy",
       G_CALLBACK (glade_command_placeholder_destroyed), cdata);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
931
932
}

933
934

static GList *
935
get_all_parentless_reffed_widgets (GList * reffed, GladeWidget * widget)
936
{
937
938
  GList *children, *l, *list;
  GladeWidget *child;
939

940
941
  if ((list = glade_widget_get_parentless_reffed_widgets (widget)) != NULL)
    reffed = g_list_concat (reffed, list);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
942

943
944
  children = glade_widget_adaptor_get_children (widget->adaptor,
                                                widget->object);
945

946
947
948
949
950
951
952
  for (l = children; l; l = l->next)
    {
      if ((child = glade_widget_get_from_gobject (l->data)) != NULL)
        {
          reffed = get_all_parentless_reffed_widgets (reffed, child);
        }
    }
953

954
  g_list_free (children);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
955

956
  return reffed;
957
958
}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
959
960
961
962
963
/**
 * glade_command_add:
 * @widgets: a #Glist
 * @parent: a #GladeWidget
 * @placeholder: a #GladePlaceholder
964
 * @pasting: whether we are pasting an existing widget or creating a new one.
Tristan Van Berkom's avatar
Tristan Van Berkom committed
965
966
967
968
 *
 * Performs an add command on all widgets in @widgets to @parent, possibly
 * replacing @placeholder (note toplevels dont need a parent; the active project
 * will be used when pasting toplevel objects).
969
970
971
 * Pasted widgets will persist packing properties from thier cut/copy source
 * while newly added widgets will prefer packing defaults.
 *
Tristan Van Berkom's avatar
Tristan Van Berkom committed
972
 */
973
static void
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
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
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
glade_command_add (GList * widgets,
                   GladeWidget * parent,
                   GladePlaceholder * placeholder, gboolean pasting)
{
  GladeCommandAddRemove *me;
  CommandData *cdata;
  GladeWidget *widget = NULL;
  GList *l, *list, *children, *placeholders = NULL;
  GtkWidget *child;

  g_return_if_fail (widgets && widgets->data);
  g_return_if_fail (parent == NULL || GLADE_IS_WIDGET (parent));

  me = g_object_new (GLADE_COMMAND_ADD_REMOVE_TYPE, NULL);
  me->add = TRUE;
  me->from_clipboard = pasting;

  /* Things can go wrong in this function if the dataset is inacurate,
   * we make no real attempt here to recover, just g_critical() and
   * fix the bugs as they pop up.
   */
  widget = GLADE_WIDGET (widgets->data);
  if (placeholder && GWA_IS_TOPLEVEL (widget->adaptor) == FALSE)
    me->project = glade_placeholder_get_project (placeholder);
  else
    me->project = glade_app_get_project ();

  GLADE_COMMAND (me)->description =
      g_strdup_printf (_("Add %s"), g_list_length (widgets) == 1 ?
                       widget->name : _("multiple"));

  for (list = widgets; list && list->data; list = list->next)
    {
      widget = list->data;
      cdata = g_new0 (CommandData, 1);
      if (widget->internal)
        g_critical ("Internal widget in Add");

      /* Widget */
      cdata->widget = g_object_ref (G_OBJECT (widget));

      /* Parentless ref */
      if ((cdata->reffed =
           get_all_parentless_reffed_widgets (cdata->reffed, widget)) != NULL)
        g_list_foreach (cdata->reffed, (GFunc) g_object_ref, NULL);

      /* Parent */
      if (parent == NULL)
        cdata->parent = glade_widget_get_parent (widget);
      else if (placeholder && GWA_IS_TOPLEVEL (widget->adaptor) == FALSE)
        cdata->parent = glade_placeholder_get_parent (placeholder);
      else if (GWA_IS_TOPLEVEL (widget->adaptor) == FALSE)
        cdata->parent = parent;

      /* Placeholder */
      if (placeholder != NULL && g_list_length (widgets) == 1)
        {
          glade_command_placeholder_connect (cdata, placeholder);
        }
      else if (cdata->parent &&
               glade_widget_placeholder_relation (cdata->parent, widget))
        {
          GtkContainer *cont = GTK_CONTAINER (cdata->parent->object);

          child = glade_util_get_placeholder_from_pointer (cont);
          if (child && g_list_find (placeholders, child) == NULL)
            {
              placeholders = g_list_append (placeholders, child);
              glade_command_placeholder_connect
                  (cdata, GLADE_PLACEHOLDER (child));
            }
          else if ((children = glade_widget_adaptor_get_children
                    (cdata->parent->adaptor, cdata->parent->object)) != NULL)
            {
              for (l = children; l && l->data; l = l->next)
                {
                  child = l->data;

                  /* Find a placeholder for this child */
                  if (GLADE_IS_PLACEHOLDER (child) &&
                      g_list_find (placeholders, child) == NULL)
                    {
                      placeholders = g_list_append (placeholders, child);
                      glade_command_placeholder_connect
                          (cdata, GLADE_PLACEHOLDER (child));
                      break;
                    }
                }
              g_list_free (children);
            }
        }

      /* 
       * Save a copy of the original project so we can forward that to glade-project, 
       * who'll copy in any resource files needed by any properties that are getting 
       * cross-project pasted.
       */
      cdata->project = cdata->widget->project;

      me->widgets = g_list_prepend (me->widgets, cdata);
    }

  glade_command_check_group (GLADE_COMMAND (me));

  /*
   * Push it onto the undo stack only on success
   */
  if (glade_command_add_remove_execute (GLADE_COMMAND (me)))
    glade_project_push_undo (glade_app_get_project (), GLADE_COMMAND (me));
  else
    g_object_unref (G_OBJECT (me));

  if (placeholders)
    g_list_free (placeholders);

}                               /* end of glade_command_add() */
1090

1091
1092
1093


static void
1094
glade_command_delete_prop_refs (GladeWidget * widget)
1095
{
1096
  GladeProperty *property;
1097

1098
1099
1100
1101
1102
  while (widget->prop_refs)
    {
      property = GLADE_PROPERTY (widget->prop_refs->data);
      glade_command_set_property (property, NULL);
    }
1103
1104
}

1105
static void glade_command_remove (GList * widgets);
1106
1107

static void
1108
glade_command_remove_locked (GladeWidget * widget, GList * reffed)
1109
{
1110
1111
  GList list = { 0, }, *widgets, *l;
  GladeWidget *locked;
1112

1113
  widgets = g_list_copy (widget->locked_widgets);
1114

1115
1116
1117
1118
  for (l = widget->locked_widgets; l; l = l->next)
    {
      locked = l->data;
      list.data = locked;
1119

1120
1121
      if (g_list_find (reffed, locked))
        continue;
1122

1123
1124
1125
      glade_command_unlock_widget (locked);
      glade_command_remove (&list);
    }
1126

1127
  g_list_free (widgets);
1128
}
1129

1130
/**
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1131
 * glade_command_remove:
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1132
 * @widgets: a #GList of #GladeWidgets
1133
 * @return_placeholders: whether or not to return a list of placehodlers
1134
 *
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1135
 * Performs a remove command on all widgets in @widgets from @parent.
1136
 */
1137
static void
1138
1139
1140