Index: src/commands.c
===================================================================
RCS file: /cvs/gnome/gnumeric/src/commands.c,v
retrieving revision 1.410
diff -u -w -r1.410 commands.c
--- src/commands.c	15 Nov 2004 04:52:44 -0000	1.410
+++ src/commands.c	25 Nov 2004 15:47:34 -0000
@@ -448,6 +448,38 @@
 	return text;
 }
 
+static void
+gnm_reloc_undo_release (GnmRelocUndo *undo)
+{
+	if (undo->exprs != NULL) {
+		dependents_unrelocate_free (undo->exprs);
+		undo->exprs = NULL;
+	}
+	if (undo->objs != NULL) {
+		g_slist_foreach (undo->objs, (GFunc) g_object_unref, NULL);
+		g_slist_free (undo->objs);
+		undo->objs = NULL;
+	}
+}
+
+static void
+gnm_reloc_undo_apply (GnmRelocUndo *undo, Sheet *sheet)
+{
+	GSList *ptr;
+
+	/* Restore the dropped objects */
+	for (ptr = undo->objs ; ptr != NULL ; ptr = ptr->next) {
+		sheet_object_set_sheet (ptr->data, sheet);
+		g_object_unref (ptr->data);
+	}
+	g_slist_free (undo->objs);
+	undo->objs = NULL;
+
+	/* Restore the changed expressions */
+	dependents_unrelocate (undo->exprs);
+	undo->exprs = NULL;
+}
+
 /**
  * command_undo : Undo the last command executed.
  * @wbc : The workbook control which issued the request.
@@ -1259,9 +1291,7 @@
 	/* Throw away the undo info for the expressions after the action*/
 	dependents_unrelocate_free (tmp.exprs);
 
-	/* Restore the changed expressions before the action */
-	dependents_unrelocate (me->reloc_storage.exprs);
-	me->reloc_storage.exprs = NULL;
+	gnm_reloc_undo_apply (&me->reloc_storage, me->sheet);
 
 	/* Ins/Del Row/Col re-ants things completely to account
 	 * for the shift of col/rows.
@@ -1395,10 +1425,7 @@
 		me->cutcopied = NULL;
 	}
 	sv_weak_unref (&(me->cut_copy_view));
-	if (me->reloc_storage.exprs) {
-		dependents_unrelocate_free (me->reloc_storage.exprs);
-		me->reloc_storage.exprs = NULL;
-	}
+	gnm_reloc_undo_release (&me->reloc_storage);
 	gnm_command_finalize (cmd);
 }
 
@@ -2557,9 +2584,7 @@
 	colrow_state_list_destroy (me->saved_sizes);
 	me->saved_sizes = NULL;
 
-	/* Restore the changed expressions */
-	dependents_unrelocate (me->reloc_storage.exprs);
-	me->reloc_storage.exprs = NULL;
+	gnm_reloc_undo_apply (&me->reloc_storage, me->info.target_sheet);
 
 	while (me->paste_content) {
 		PasteContent *pc = me->paste_content->data;
@@ -2600,6 +2625,7 @@
 	g_return_val_if_fail (me != NULL, TRUE);
 	g_return_val_if_fail (me->paste_content == NULL, TRUE);
 	g_return_val_if_fail (me->reloc_storage.exprs == NULL, TRUE);
+	g_return_val_if_fail (me->reloc_storage.objs == NULL, TRUE);
 
 	tmp = me->info.origin;
 	range_translate (&tmp, me->info.col_offset, me->info.row_offset);
@@ -2676,10 +2702,7 @@
 		cellregion_unref (pc->contents);
 		g_free (pc);
 	}
-	if (me->reloc_storage.exprs) {
-		dependents_unrelocate_free (me->reloc_storage.exprs);
-		me->reloc_storage.exprs = NULL;
-	}
+	gnm_reloc_undo_release (&me->reloc_storage);
 	if (me->deleted_sheet_contents) {
 		cellregion_unref (me->deleted_sheet_contents);
 		me->deleted_sheet_contents = NULL;
@@ -2736,6 +2759,7 @@
 	me->paste_content  = NULL;
 	me->deleted_sheet_contents = NULL;
 	me->reloc_storage.exprs  = NULL;
+	me->reloc_storage.objs  = NULL;
 	me->move_selection = move_selection;
 	me->saved_sizes    = NULL;
 
Index: src/dependent.c
===================================================================
RCS file: /cvs/gnome/gnumeric/src/dependent.c,v
retrieving revision 1.228
diff -u -w -r1.228 dependent.c
--- src/dependent.c	1 Nov 2004 16:42:48 -0000	1.228
+++ src/dependent.c	25 Nov 2004 15:47:35 -0000
@@ -121,11 +121,12 @@
  * @cell : the dependent that changed
  *
  * Links the dependent and queues a recalc.
- */
+ **/
 static void
 dependent_changed (GnmDependent *dep)
 {
-	if (dep->sheet->workbook->recursive_dirty_enabled)
+	if (dep->sheet != NULL &&
+	    dep->sheet->workbook->recursive_dirty_enabled)
 		dependent_queue_recalc (dep);
 	else
 		dependent_flag_recalc (dep);
@@ -1512,6 +1513,7 @@
 		} else {
 			dependent_set_expr (tmp->u.dep, tmp->oldtree);
 			dependent_flag_recalc (tmp->u.dep);
+			if (tmp->u.dep->sheet != NULL)
 			dependent_link (tmp->u.dep, NULL);
 		}
 		gnm_expr_unref (tmp->oldtree);
Index: src/gnumeric.h
===================================================================
RCS file: /cvs/gnome/gnumeric/src/gnumeric.h,v
retrieving revision 1.101
diff -u -w -r1.101 gnumeric.h
--- src/gnumeric.h	11 Nov 2004 05:09:22 -0000	1.101
+++ src/gnumeric.h	25 Nov 2004 15:47:36 -0000
@@ -175,5 +175,6 @@
 
 typedef struct _PrintInformation        PrintInformation;
 typedef struct _SolverParameters	SolverParameters;
+typedef struct _GnmRelocUndo		GnmRelocUndo;
 
 #endif /* GNUMERIC_H */
Index: src/ranges.c
===================================================================
RCS file: /cvs/gnome/gnumeric/src/ranges.c,v
retrieving revision 1.115
diff -u -w -r1.115 ranges.c
--- src/ranges.c	5 Nov 2004 03:29:59 -0000	1.115
+++ src/ranges.c	25 Nov 2004 15:47:36 -0000
@@ -1184,7 +1184,7 @@
 gboolean
 global_range_contained (Sheet const *sheet, GnmValue const *a, GnmValue const *b)
 {
-	Sheet *target;
+	Sheet const *target;
 
 	g_return_val_if_fail (a != NULL, FALSE);
 	g_return_val_if_fail (b != NULL, FALSE);
Index: src/sheet-object.c
===================================================================
RCS file: /cvs/gnome/gnumeric/src/sheet-object.c,v
retrieving revision 1.208
diff -u -w -r1.208 sheet-object.c
--- src/sheet-object.c	15 Nov 2004 04:52:44 -0000	1.208
+++ src/sheet-object.c	25 Nov 2004 15:47:36 -0000
@@ -546,12 +546,14 @@
  * @rinfo : details on what should be moved.
  * @update : Should we do the bound_update now, or leave it for later.
  * 		if FALSE honour the move_with_cells flag.
+ * @undo : if non-NULL add dropped objects to ::objects
  *
  * Uses the relocation info and the anchors to decide whether or not, and how
  * to relocate objects when the grid moves (eg ins/del col/row).
  **/
 void
-sheet_objects_relocate (GnmExprRelocateInfo const *rinfo, gboolean update)
+sheet_objects_relocate (GnmExprRelocateInfo const *rinfo, gboolean update,
+			GnmRelocUndo *undo)
 {
 	GList   *ptr, *next;
 	GnmRange	 dest;
@@ -572,7 +574,8 @@
 			SheetObject *so = SHEET_OBJECT (ptr->data);
 			GnmRange const *r  = &so->anchor.cell_bound;
 			if (range_contains (&dest, r->start.col, r->start.row)) {
-				/* lost_objs = g_slist_prepend (lost_objs, g_object_ref (so)); */
+				if (NULL != undo)
+					undo->objs = g_slist_prepend (undo->objs, g_object_ref (so));
 				sheet_object_clear_sheet (so);
 			}
 		}
@@ -592,7 +595,8 @@
 			/* FIXME : just moving the range is insufficent for all anchor types */
 			/* Toss any objects that would be clipped. */
 			if (range_translate (r, rinfo->col_offset, rinfo->row_offset)) {
-				/* lost_objs = g_slist_prepend (lost_objs, g_object_ref (so)); */
+				if (NULL != undo)
+					undo->objs = g_slist_prepend (undo->objs, g_object_ref (so));
 				sheet_object_clear_sheet (so);
 				continue;
 			}
@@ -605,7 +609,8 @@
 				sheet_object_update_bounds (so, NULL);
 		} else if (!change_sheets &&
 			   range_contains (&dest, r->start.col, r->start.row)) {
-			/* lost_objs = g_slist_prepend (lost_objs, g_object_ref (so)); */
+			if (NULL != undo)
+				undo->objs = g_slist_prepend (undo->objs, g_object_ref (so));
 			sheet_object_clear_sheet (so);
 			continue;
 		}
Index: src/sheet-object.h
===================================================================
RCS file: /cvs/gnome/gnumeric/src/sheet-object.h,v
retrieving revision 1.69
diff -u -w -r1.69 sheet-object.h
--- src/sheet-object.h	15 Nov 2004 04:52:44 -0000	1.69
+++ src/sheet-object.h	25 Nov 2004 15:47:36 -0000
@@ -69,7 +69,8 @@
 void sheet_object_position_pts_get (SheetObject const *so, double *coords);
 
 /* Object Management */
-void    sheet_objects_relocate   (GnmExprRelocateInfo const *rinfo, gboolean update);
+void	sheet_objects_relocate   (GnmExprRelocateInfo const *rinfo, gboolean update,
+				  GnmRelocUndo *undo);
 void    sheet_objects_clear      (Sheet const *sheet, GnmRange const *r, GType t);
 GSList *sheet_objects_get        (Sheet const *sheet, GnmRange const *r, GType t);
 void    sheet_object_clone_sheet (Sheet const *src, Sheet *dst, GnmRange *range);
Index: src/sheet.c
===================================================================
RCS file: /cvs/gnome/gnumeric/src/sheet.c,v
retrieving revision 1.723
diff -u -w -r1.723 sheet.c
--- src/sheet.c	22 Nov 2004 13:02:30 -0000	1.723
+++ src/sheet.c	25 Nov 2004 15:47:36 -0000
@@ -3070,11 +3070,12 @@
 
 static void
 sheet_colrow_insdel_finish (GnmExprRelocateInfo const *rinfo, gboolean is_cols,
-			    int pos, int state_start, ColRowStateList *states)
+			    int pos, int state_start, ColRowStateList *states,
+			    GnmRelocUndo *reloc_storage)
 {
 	Sheet *sheet = rinfo->origin_sheet;
 
-	sheet_objects_relocate (rinfo, FALSE);
+	sheet_objects_relocate (rinfo, FALSE, reloc_storage);
 	sheet_merge_relocate (rinfo);
 
 	/* Notify sheet of pending updates */
@@ -3118,10 +3119,11 @@
 
 static void
 sheet_colrow_insert_finish (GnmExprRelocateInfo const *rinfo, gboolean is_cols,
-			    int pos, int count, ColRowStateList *states)
+			    int pos, int count, ColRowStateList *states,
+			    GnmRelocUndo *reloc_storage)
 {
 	sheet_style_insert_colrow (rinfo);
-	sheet_colrow_insdel_finish (rinfo, is_cols, pos, pos, states);
+	sheet_colrow_insdel_finish (rinfo, is_cols, pos, pos, states, reloc_storage);
 	sheet_colrow_set_collapse (rinfo->origin_sheet, is_cols, pos);
 	sheet_colrow_set_collapse (rinfo->origin_sheet, is_cols, pos+count);
 	sheet_colrow_set_collapse (rinfo->origin_sheet, is_cols,
@@ -3142,11 +3144,12 @@
 
 static void
 sheet_colrow_delete_finish (GnmExprRelocateInfo const *rinfo, gboolean is_cols,
-			    int pos, int count, ColRowStateList *states)
+			    int pos, int count, ColRowStateList *states,
+			    GnmRelocUndo *reloc_storage)
 {
 	int end = colrow_max (is_cols) - count;
 	sheet_style_relocate (rinfo);
-	sheet_colrow_insdel_finish (rinfo, is_cols, pos, end, states);
+	sheet_colrow_insdel_finish (rinfo, is_cols, pos, end, states, reloc_storage);
 	sheet_colrow_set_collapse (rinfo->origin_sheet, is_cols, pos);
 	sheet_colrow_set_collapse (rinfo->origin_sheet, is_cols, end);
 	sheet_filter_insdel_colrow (rinfo->origin_sheet, is_cols, FALSE,
@@ -3164,10 +3167,13 @@
 
 /**
  * sheet_insert_cols:
- * @sheet   The sheet
- * @col     At which position we want to insert
- * @count   The number of columns to be inserted
- */
+ * @sheet  : #Sheet
+ * @col    : At which position we want to insert
+ * @count  : The number of columns to be inserted
+ * @states :
+ * @reloc_storage   :
+ * @cc     :
+ **/
 gboolean
 sheet_insert_cols (Sheet *sheet,
 		   int col, int count, ColRowStateList *states,
@@ -3213,7 +3219,8 @@
 
 	solver_insert_cols (sheet, col, count);
 	scenario_insert_cols (sheet->scenarios, col, count);
-	sheet_colrow_insert_finish (&reloc_info, TRUE, col, count, states);
+	sheet_colrow_insert_finish (&reloc_info, TRUE, col, count,
+		states, reloc_storage);
 	return FALSE;
 }
 
@@ -3278,7 +3285,8 @@
 
 	solver_delete_cols (sheet, col, count);
 	scenario_delete_cols (sheet->scenarios, col, count);
-	sheet_colrow_delete_finish (&reloc_info, TRUE, col, count, states);
+	sheet_colrow_delete_finish (&reloc_info, TRUE, col, count,
+		states, reloc_storage);
 	return FALSE;
 }
 
@@ -3333,7 +3341,8 @@
 
 	solver_insert_rows (sheet, row, count);
 	scenario_insert_rows (sheet->scenarios, row, count);
-	sheet_colrow_insert_finish (&reloc_info, FALSE, row, count, states);
+	sheet_colrow_insert_finish (&reloc_info, FALSE, row, count,
+		states, reloc_storage);
 	return FALSE;
 }
 
@@ -3398,7 +3407,8 @@
 
 	solver_delete_rows (sheet, row, count);
 	scenario_delete_rows (sheet->scenarios, row, count);
-	sheet_colrow_delete_finish (&reloc_info, FALSE, row, count, states);
+	sheet_colrow_delete_finish (&reloc_info, FALSE, row, count,
+		states, reloc_storage);
 	return FALSE;
 }
 
@@ -3531,7 +3541,7 @@
 	}
 
 	/* 7. Move objects in the range */
-	sheet_objects_relocate (rinfo, TRUE);
+	sheet_objects_relocate (rinfo, TRUE, reloc_storage);
 	sheet_merge_relocate (rinfo);
 
 	/* 8. Notify sheet of pending update */
Index: src/sheet.h
===================================================================
RCS file: /cvs/gnome/gnumeric/src/sheet.h,v
retrieving revision 1.344
diff -u -w -r1.344 sheet.h
--- src/sheet.h	17 Oct 2004 00:33:48 -0000	1.344
+++ src/sheet.h	25 Nov 2004 15:47:36 -0000
@@ -263,9 +263,10 @@
 void	     sheet_adjust_outline_dir (Sheet *sheet, gboolean is_cols);
 
 /* Implementation for commands, no undo */
-typedef struct {
+struct _GnmRelocUndo {
 	GSList *exprs;
-} GnmRelocUndo;
+	GSList *objs;
+};
 
 gboolean  sheet_insert_cols (Sheet *sheet,
 			     int col, int count, ColRowStateList *states,
