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

27
28
29
30
31
32
33
34
/**
 * 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.
 */

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

/* 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).
 */
60
61
62
typedef struct {
	GladeWidget      *widget;
	GladeWidget      *parent;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
63
	GladeProject     *project;
64
	GladePlaceholder *placeholder;
65
	gboolean          props_recorded;
66
	GList            *pack_props;
67
	gchar            *special_type;
68
	gulong            handler_id;
69
} CommandData;
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
static GObjectClass *parent_class = NULL;

/* Group description used for the current group
 */
static gchar        *gc_group_description = NULL;

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

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

85
86

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

static void
glade_command_finalize (GObject *obj)
{
        GladeCommand *cmd = (GladeCommand *) obj;
        g_return_if_fail (cmd != NULL);

119
120
	if (cmd->description)
		g_free (cmd->description);
121
122
123
124
125
126

        /* Call the base class dtor */
	(* G_OBJECT_CLASS (parent_class)->finalize) (obj);
}

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

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

static void
139
glade_command_class_init (GladeCommandClass *klass)
140
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
	klass->undo            = NULL;
	klass->execute         = NULL;
	klass->unifies         = glade_command_unifies_impl;
	klass->collapse        = glade_command_collapse_impl;
152
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
165

#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								\
166
func ## _unifies (GladeCommand *this_cmd, GladeCommand *other_cmd);		\
167
static void								\
168
func ## _collapse (GladeCommand *this_cmd, GladeCommand *other_cmd);		\
169
170
171
172
173
static void								\
func ## _class_init (gpointer parent_tmp, gpointer notused)		\
{									\
	GladeCommandClass *parent = parent_tmp;				\
	GObjectClass* object_class;					\
174
	object_class = G_OBJECT_CLASS (parent);				\
175
176
	parent->undo =  func ## _undo;					\
	parent->execute =  func ## _execute;				\
177
178
	parent->unifies =  func ## _unifies;				\
	parent->collapse =  func ## _collapse;				\
179
180
181
182
183
	object_class->finalize = func ## _finalize;			\
}									\
typedef struct {							\
	GladeCommandClass cmd;						\
} type ## Class;							\
184
static MAKE_TYPE(func, type, GLADE_TYPE_COMMAND)
185

186

187
188
189
190
191
192
193
194
195
196
/**
 * glade_command_execute:
 * @command: A #GladeCommand
 *
 * Executes @command
 *
 * Returns: whether the command was successfully executed
 */
gboolean
glade_command_execute (GladeCommand *command)
197
{
198
	g_return_val_if_fail (GLADE_IS_COMMAND (command), FALSE);
199
	return GLADE_COMMAND_GET_CLASS (command)->execute (command);
200
201
202
}


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

218
/**
219
220
221
222
223
224
 * glade_command_unifies:
 * @command: A #GladeCommand
 * @other: another #GladeCommand
 *
 * Checks whether @command and @other can be unified
 * to make one single command.
225
 *
226
 * Returns: whether they can be unified.
227
 */
228
229
230
gboolean
glade_command_unifies (GladeCommand *command,
		       GladeCommand *other)
231
{
232
	g_return_val_if_fail (command, FALSE);
233
234
235
236
237
238
239

	/* 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;

240
	return GLADE_COMMAND_GET_CLASS (command)->unifies (command, other);
241
242
}

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

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

	g_return_if_fail (fmt);
274
275
276
277

	/* Only use the description for the first group.
	 */
	if (gc_group_depth++ == 0)
278
279
280
281
282
	{
		va_start (args, fmt);
		gc_group_description = g_strdup_vprintf (fmt, args);
		va_end (args);
	}
283
284
285
286
287
288
289
290
291
292
293
}

/**
 * glade_command_pop_group:
 *
 * Mark the end of a command group.
 */
void
glade_command_pop_group (void)
{
	if (gc_group_depth-- == 1)
294
	{
295
296
		gc_group_description = (g_free (gc_group_description), NULL);
		gc_group_id++;
297
298
	}

299
300
	if (gc_group_depth < 0)
		g_critical ("Unbalanced group stack detected in %s\n",
301
			    G_STRFUNC);
302
}
303

304
305
306
307
308
309
gint
glade_command_get_group_depth (void)
{
	return gc_group_depth;
}

310
311
312
313
314
315
316
317
318
319
static void
glade_command_check_group (GladeCommand *cmd)
{
	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;
	}
320
321
}

322
323
/**************************************************/
/*******     GLADE_COMMAND_SET_PROPERTY     *******/
324
325
/**************************************************/

326
327
/* create a new GladeCommandSetProperty class.  Objects of this class will
 * encapsulate a "set property" operation */
328
329

typedef struct {
330
331
332
333
	GladeCommand  parent;
	gboolean      set_once;
	gboolean      undo;
	GList        *sdata;
334
} GladeCommandSetProperty;
335

336
/* standard macros */
337
338
339
340
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))
341
342
#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))
343

344
/* Undo the last "set property command" */
345
static gboolean
346
glade_command_set_property_undo (GladeCommand *cmd)
347
{
348
	return glade_command_set_property_execute (cmd);
349
350
}

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

363
	g_return_val_if_fail (me != NULL, FALSE);
364

365
366
367
	if (me->set_once != FALSE)
		glade_property_push_superuser ();

368
369
370
	for (l = me->sdata; l; l = l->next)
	{
		GValue new_value = { 0, };
371
		GCSetPropData *sdata = l->data;
372

373
374
		g_value_init (&new_value, G_VALUE_TYPE (sdata->new_value));
		
375
376
377
378
379
		if (me->undo)
			g_value_copy (sdata->old_value, &new_value);
		else
			g_value_copy (sdata->new_value, &new_value);

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

			g_print ("Setting %s property of %s to %s (sumode: %d)\n",
388
				 sdata->property->klass->id,
389
390
391
392
393
394
395
396
397
398
				 sdata->property->widget->name,
				 str, glade_property_superuser ());

			g_free (str);
		}
#endif

		/* Packing properties need to be refreshed here since
		 * they are reset when they get added to containers.
		 */
399
		if (sdata->property->klass->packing)
400
401
402
403
		{
			GladeProperty *tmp_prop;

			tmp_prop = glade_widget_get_pack_property
404
				(sdata->property->widget, sdata->property->klass->id);
405
406
407
408
409
410
411
412
413

			if (sdata->property != tmp_prop)
			{
				g_object_unref (sdata->property);
				sdata->property = g_object_ref (tmp_prop);
				
			}
		}

414
415
		success = glade_property_set_value (sdata->property, &new_value);
		retval = retval || success;
416

417
		if (!me->set_once && success)
418
419
420
421
422
423
424
425
426
427
		{
			/* 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);
		}


428
		g_value_unset (&new_value);
429

430

431
	}
432
433
434
435
436
437
438

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

	me->set_once = TRUE;
	me->undo     = !me->undo;

439
	return retval;
440
441
442
}

static void
443
glade_command_set_property_finalize (GObject *obj)
444
{
445
	GladeCommandSetProperty *me;
446
	GList *l;
447
448
449

	me = GLADE_COMMAND_SET_PROPERTY (obj);

450
451
	for (l = me->sdata; l; l = l->next)
	{
452
		GCSetPropData *sdata = l->data;
453
454
455
456
457
458
459
460
461
462
463
464
465
		
		if (sdata->property)
			g_object_unref (G_OBJECT (sdata->property));

		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);
466

467
	}
468
	glade_command_finalize (obj);
469
470
471
}

static gboolean
472
glade_command_set_property_unifies (GladeCommand *this_cmd, GladeCommand *other_cmd)
473
{
474
475
476
       GladeCommandSetProperty *cmd1,   *cmd2;
       GCSetPropData           *pdata1, *pdata2;
       GList                   *list, *l;
477

478
479
480
481
482
483
484
485
	if (!other_cmd)
	{
		if (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd))
		{
			cmd1 = (GladeCommandSetProperty*) this_cmd;
			
			for (list = cmd1->sdata; list; list = list->next)
			{
486
487
				GladeProject *project;
				GladeProjectFormat fmt = GLADE_PROJECT_FORMAT_GTKBUILDER;
488
				pdata1 = list->data;
489
490
491
492
493
494
				
				if (pdata1->property->widget)
				{
					project = glade_widget_get_project (pdata1->property->widget);
					fmt     = glade_project_get_format (project);
				}
495
496
497

				if (glade_property_class_compare (pdata1->property->klass,
								  pdata1->old_value,
498
								  pdata1->new_value, fmt))
499
500
501
502
503
504
505
506
					return FALSE;
			}
			return TRUE;
				
		}
		return FALSE;
	}

507
508
       if (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd) && 
	   GLADE_IS_COMMAND_SET_PROPERTY (other_cmd))
509
       {
510
511
               cmd1 = (GladeCommandSetProperty*) this_cmd;
               cmd2 = (GladeCommandSetProperty*) other_cmd;
512
513
514
515
516
517
518
519
520
521
522
523

	       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;

524
			       if (pdata1->property->widget == pdata2->property->widget &&
525
526
				   glade_property_class_match (pdata1->property->klass,
							       pdata2->property->klass))
527
528
529
530
531
532
533
534
535
536
537
538
				       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;
539
540
       }
       return FALSE;
541
542
543
}

static void
544
glade_command_set_property_collapse (GladeCommand *this_cmd, GladeCommand *other_cmd)
545
{
546
547
548
549
	GladeCommandSetProperty *cmd1,   *cmd2;
	GCSetPropData           *pdata1, *pdata2;
	GList                   *list, *l;
	
550
551
	g_return_if_fail (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd) &&
			  GLADE_IS_COMMAND_SET_PROPERTY (other_cmd));
552
	
553
554
	cmd1 = (GladeCommandSetProperty*) this_cmd;
	cmd2 = (GladeCommandSetProperty*) other_cmd;
555
556
557
558
559
560
561
562
563


	for (list = cmd1->sdata; list; list = list->next)
	{
		pdata1 = list->data;
		for (l = cmd2->sdata; l; l = l->next)
		{
			pdata2 = l->data;
			
564
565
			if (glade_property_class_match (pdata1->property->klass,
							pdata2->property->klass))
566
567
568
569
570
571
572
573
			{
				/* Merge the GCSetPropData structs manually here
				 */
				g_value_copy (pdata2->new_value, pdata1->new_value);
				break;
			}
		}
	}
Paolo Borelli's avatar
Paolo Borelli committed
574

575
576
	/* Set the description
	 */
577
578
579
	g_free (this_cmd->description);
	this_cmd->description = other_cmd->description;
	other_cmd->description = NULL;
580

Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
581
	glade_app_update_ui ();
582
583
}

584
585

#define MAX_UNDO_MENU_ITEM_VALUE_LEN	10
586
static gchar *
587
588
glade_command_set_property_description (GladeCommandSetProperty *me,
					GladeProjectFormat       fmt)
589
{
590
	GCSetPropData   *sdata;
591
592
593
594
595
596
597
598
599
600
	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;
601
602
		value_name = glade_widget_adaptor_string_from_value
			(GLADE_WIDGET_ADAPTOR (sdata->property->klass->handle),
603
			 sdata->property->klass, sdata->new_value, fmt);
604

605
606
607
		if (!value_name || strlen (value_name) > MAX_UNDO_MENU_ITEM_VALUE_LEN
		    || strchr (value_name, '_')) {
			description = g_strdup_printf (_("Setting %s of %s"),
608
						       sdata->property->klass->name,
609
610
611
						       sdata->property->widget->name);
		} else {
			description = g_strdup_printf (_("Setting %s of %s to %s"),
612
						       sdata->property->klass->name,
613
614
615
616
617
618
619
620
						       sdata->property->widget->name, value_name);
		}
		g_free (value_name);
	}

	return description;
}

621

622
void
623
glade_command_set_properties_list (GladeProject *project, GList *props)
624
{
625
	GladeCommandSetProperty *me;
626
	GladeCommand  *cmd;
627
628
	GCSetPropData *sdata;
	GList         *list;
629
	gboolean       success;
630
	gboolean       multiple;
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645

	g_return_if_fail (GLADE_IS_PROJECT (project));
	g_return_if_fail (props);

	me = (GladeCommandSetProperty*) g_object_new (GLADE_COMMAND_SET_PROPERTY_TYPE, NULL);
	cmd = GLADE_COMMAND (me);

	/* Ref all props */
	for (list = props; list; list = list->next)
	{
		sdata = list->data;
		g_object_ref (G_OBJECT (sdata->property));
	}

	me->sdata        = props;
646
647
648
649
650
651
652
653
654
	cmd->description = 
		glade_command_set_property_description 
		(me, glade_project_get_format (project));


	multiple = g_list_length (me->sdata) > 1;
	if (multiple)
		glade_command_push_group (cmd->description);

655
656

	glade_command_check_group (GLADE_COMMAND (me));
657
658

	/* Push onto undo stack only if it executes successfully. */
659
660
661
	success = glade_command_set_property_execute (GLADE_COMMAND (me));
	
	if (success)
662
		glade_project_push_undo (GLADE_PROJECT (project),
663
664
665
666
					 GLADE_COMMAND (me));
	else
		/* No leaks on my shift! */
		g_object_unref (G_OBJECT (me));
667
668
669
	
	if (multiple)
		glade_command_pop_group ();
670
671
672
673
674
675
676
}


void
glade_command_set_properties (GladeProperty *property, const GValue *old_value, const GValue *new_value, ...)
{
	GCSetPropData *sdata;
677
678
	GladeProperty *prop;
	GValue        *ovalue, *nvalue;
679
	GList         *list = NULL;
680
	va_list        vl;
Paolo Borelli's avatar
Paolo Borelli committed
681
682

	g_return_if_fail (GLADE_IS_PROPERTY (property));
683

684
	/* Add first set */
685
686
	sdata = g_new (GCSetPropData, 1);
	sdata->property = property;
687
688
689
690
691
692
	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);
693
	list = g_list_prepend (list, sdata);
694
695
696
697
698
699
700
701
702
703
704

	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));

705
		sdata = g_new (GCSetPropData, 1);
706
707
708
709
710
711
712
		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);
713
		list = g_list_prepend (list, sdata);
714
715
716
	}	
	va_end (vl);

717
	glade_command_set_properties_list (property->widget->project, list);
718
719
}

720
void
721
glade_command_set_property_value (GladeProperty *property, const GValue* pvalue)
722
{
723
724
725
726
727
728

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

729
730
731
	glade_command_set_properties (property, property->value, pvalue, NULL);
}

732
733
734
735
736
737
738
739
740
void
glade_command_set_property (GladeProperty *property, ...)
{
	GValue *value;
	va_list args;
	
	g_return_if_fail (GLADE_IS_PROPERTY (property));

	va_start (args, property);
741
	value = glade_property_class_make_gvalue_from_vl (property->klass, args);
742
743
744
745
746
	va_end (args);
	
	glade_command_set_property_value (property, value);
}

747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/**************************************************/
/*******       GLADE_COMMAND_SET_NAME       *******/
/**************************************************/

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

	GladeWidget *widget;
	gchar *old_name;
	gchar *name;
} 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))
766
767
#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))
768
769
770
771
772
773
774
775

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

Tristan Van Berkom's avatar
Tristan Van Berkom committed
776
/*
777
778
779
780
781
782
 * Execute the set name command and revert it.  Ie, after the execution of this
 * function cmd will point to the undo action
 */
static gboolean
glade_command_set_name_execute (GladeCommand *cmd)
{
783
	GladeCommandSetName* me = GLADE_COMMAND_SET_NAME (cmd);
784
785
786
787
788
789
	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);

790
791
	glade_project_set_widget_name (me->widget->project, 
				       me->widget, me->name);
792
	
793
794
795
796
	tmp = me->old_name;
	me->old_name = me->name;
	me->name = tmp;

797
	return TRUE;
798
799
800
801
802
}

static void
glade_command_set_name_finalize (GObject *obj)
{
803
804
805
806
807
808
	GladeCommandSetName* me;

	g_return_if_fail (GLADE_IS_COMMAND_SET_NAME (obj));

	me = GLADE_COMMAND_SET_NAME (obj);

809
810
	g_free (me->old_name);
	g_free (me->name);
811

812
813
814
815
	glade_command_finalize (obj);
}

static gboolean
816
glade_command_set_name_unifies (GladeCommand *this_cmd, GladeCommand *other_cmd)
817
818
819
820
{
	GladeCommandSetName *cmd1;
	GladeCommandSetName *cmd2;

821
	if (GLADE_IS_COMMAND_SET_NAME (this_cmd) && GLADE_IS_COMMAND_SET_NAME (other_cmd))
822
	{
823
824
		cmd1 = GLADE_COMMAND_SET_NAME (this_cmd);
		cmd2 = GLADE_COMMAND_SET_NAME (other_cmd);
825
826
827
828
829
830
831
832

		return (cmd1->widget == cmd2->widget);
	}

	return FALSE;
}

static void
833
glade_command_set_name_collapse (GladeCommand *this_cmd, GladeCommand *other_cmd)
834
{
835
836
	GladeCommandSetName *nthis = GLADE_COMMAND_SET_NAME (this_cmd);
	GladeCommandSetName *nother = GLADE_COMMAND_SET_NAME (other_cmd);
837

838
	g_return_if_fail (GLADE_IS_COMMAND_SET_NAME (this_cmd) && GLADE_IS_COMMAND_SET_NAME (other_cmd));
839
840
841
842
843

	g_free (nthis->old_name);
	nthis->old_name = nother->old_name;
	nother->old_name = NULL;

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

Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
847
	glade_app_update_ui ();
848
849
850
851
852
853
854
855
}

/* this function takes the ownership of name */
void
glade_command_set_name (GladeWidget *widget, const gchar* name)
{
	GladeCommandSetName *me;
	GladeCommand *cmd;
856
857
858
859

	g_return_if_fail (GLADE_IS_WIDGET (widget));
	g_return_if_fail (name != NULL);

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

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

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

	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
	if (glade_command_set_name_execute (GLADE_COMMAND (me)))
877
		glade_project_push_undo (GLADE_PROJECT (widget->project), GLADE_COMMAND (me));
878
879
	else
		g_object_unref (G_OBJECT (me));
880
881
}

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

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

Tristan Van Berkom's avatar
Tristan Van Berkom committed
898
899
900
901
902
903
904

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))
905

906
907
908
909
910
911
912
913
914
915
static void 
glade_command_placeholder_destroyed (GtkObject *object, CommandData *cdata)
{
	if (GTK_OBJECT (cdata->placeholder) == object)
	{
		cdata->placeholder = NULL;
		cdata->handler_id = 0;
	}
}

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

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

Tristan Van Berkom's avatar
Tristan Van Berkom committed
926
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
935
/**
 * glade_command_add:
 * @widgets: a #Glist
 * @parent: a #GladeWidget
 * @placeholder: a #GladePlaceholder
936
 * @pasting: whether we are pasting an existing widget or creating a new one.
Tristan Van Berkom's avatar
Tristan Van Berkom committed
937
938
939
940
 *
 * 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).
941
942
943
 * 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
944
 */
945
static void
946
947
948
949
glade_command_add (GList            *widgets, 
		   GladeWidget      *parent, 
		   GladePlaceholder *placeholder,
		   gboolean          pasting)
950
{
Tristan Van Berkom's avatar
Tristan Van Berkom committed
951
952
953
954
955
	GladeCommandAddRemove	*me;
	CommandData				*cdata;
	GladeWidget				*widget = NULL;
	GList					*l, *list, *children, *placeholders = NULL;
	GtkWidget				*child;
956

Tristan Van Berkom's avatar
Tristan Van Berkom committed
957
958
	g_return_if_fail (widgets && widgets->data);
	g_return_if_fail (parent == NULL || GLADE_IS_WIDGET (parent));
959

Tristan Van Berkom's avatar
Tristan Van Berkom committed
960
961
	me = g_object_new (GLADE_COMMAND_ADD_REMOVE_TYPE, NULL);
	me->add = TRUE;
962
	me->from_clipboard = pasting;
963

Tristan Van Berkom's avatar
Tristan Van Berkom committed
964
965
966
967
968
969
	/* 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 && GTK_IS_WINDOW (widget->object) == FALSE)
970
	{
Tristan Van Berkom's avatar
Tristan Van Berkom committed
971
972
		GladeWidget *some_widget = glade_placeholder_get_parent (placeholder);
		me->project = glade_widget_get_project (some_widget);
973
	}
Tristan Van Berkom's avatar
Tristan Van Berkom committed
974
	else me->project = glade_app_get_project();
975

Tristan Van Berkom's avatar
Tristan Van Berkom committed
976
977
978
	GLADE_COMMAND (me)->description = 
		g_strdup_printf (_("Add %s"), g_list_length (widgets) == 1 ? 
				 widget->name : _("multiple"));
979

Tristan Van Berkom's avatar
Tristan Van Berkom committed
980
	for (list = widgets; list && list->data; list = list->next)
981
	{
Tristan Van Berkom's avatar
Tristan Van Berkom committed
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
		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));
		
		/* Parent */
		if (parent == NULL)
			cdata->parent = glade_widget_get_parent (widget);
		else if (placeholder && GTK_IS_WINDOW (widget->object) == FALSE)
			cdata->parent = glade_placeholder_get_parent (placeholder);
		else if (GTK_IS_WINDOW (widget->object) == FALSE)
			cdata->parent = parent;
		if (cdata->parent == NULL && GTK_IS_WINDOW (widget->object) == FALSE)
998
			g_message ("Parentless non GtkWindow widget in Add");
999

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1000
1001
		/* Placeholder */
		if (placeholder != NULL && g_list_length (widgets) == 1)
1002
		{
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1003
			glade_command_placeholder_connect (cdata, placeholder);
Paolo Borelli's avatar
Paolo Borelli committed
1004
		}
1005
1006
		else if (cdata->parent && 
			 glade_widget_placeholder_relation (cdata->parent, widget))
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
		{
			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;
Paolo Borelli's avatar
Paolo Borelli committed
1023

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
					/* 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);
			}
		}
1037

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1038
1039
1040
1041
1042
1043
		/* 
		 * 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;
1044

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1045
		me->widgets = g_list_prepend (me->widgets, cdata);
1046
1047
	}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1048
	glade_command_check_group (GLADE_COMMAND (me));
1049

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1050
1051
1052
1053
1054
	/*
	 * 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));
1055
	else
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1056
		g_object_unref (G_OBJECT (me));
1057

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1058
1059
	if (placeholders) 
		g_list_free (placeholders);
1060
		
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1061
} /* end of glade_command_add() */
1062

1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077


static void
glade_command_delete_prop_refs (GladeWidget *widget)
{
	GladeProperty *property;

	while (widget->prop_refs)
	{
		property = GLADE_PROPERTY (widget->prop_refs->data);
		glade_command_set_property (property, NULL);
	}
}


1078
/**
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1079
 * glade_command_remove:
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1080
 * @widgets: a #GList of #GladeWidgets
1081
 * @return_placeholders: whether or not to return a list of placehodlers
1082
 *
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1083
 * Performs a remove command on all widgets in @widgets from @parent.
1084
 */
1085
static void
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1086
glade_command_remove (GList *widgets)
1087
{
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1088
1089
1090
	GladeCommandAddRemove	*me;
	GladeWidget				*widget = NULL;
	CommandData				*cdata;
1091
	GtkWidget                               *placeholder;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1092
	GList					*list, *l;
1093

1094
1095
	g_return_if_fail (widgets != NULL);

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1096
1097
 	me = g_object_new (GLADE_COMMAND_ADD_REMOVE_TYPE, NULL);
	me->add = FALSE;
1098
	me->from_clipboard = FALSE;
1099

1100
	/* internal children cannot be deleted. Notify the user. */
1101
	for (list = widgets; list && list->data; list = list->next)
1102
	{
1103
1104
1105
		widget = list->data;
		if (widget->internal)
		{
1106
			glade_util_ui_message (glade_app_get_window(),	
1107
					       GLADE_UI_WARN, NULL,
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1108
					       _("You cannot remove a widget internal to a composite widget."));
1109
1110
1111
1112
			return;
		}
	}

1113
	me->project = glade_widget_get_project (widget);
1114
1115
1116
	GLADE_COMMAND (me)->description = g_strdup ("dummy");

	if (g_list_length (widgets) == 1)
1117
1118
		glade_command_push_group (_("Remove %s"), 
					  GLADE_WIDGET (widgets->data)->name);
1119
	else
1120
		glade_command_push_group (_("Remove multiple"));
1121

1122
1123
1124
1125
1126
1127
1128
	for (list = widgets; list && list->data; list = list->next)
	{
		widget         = list->data;

		cdata          = g_new0 (CommandData, 1);
		cdata->widget  = g_object_ref (G_OBJECT (widget));
		cdata->parent  = glade_widget_get_parent (widget);
1129
		cdata->project = glade_widget_get_project (widget);