[gnumeric] Change cmd_resize_colrow to use GOUndo in prep to fix 622530



commit 1b8db9fa9bca13d82b72e4edc54f1ffe68112399
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Fri Jun 25 04:09:34 2010 -0600

    Change cmd_resize_colrow to use GOUndo in prep to fix 622530
    
    2010-06-25  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* src/Makefile.am (undo.[ch]): added
    	* src/commands.h: formatting
    	* src/commands.c (cmd_resize_colrow_*): deleted
    	(cmd_resize_colrow): reimplement using GOUndo.
    	* src/colrow.h (colrow_get_sizes): new
    	(colrow_index_list_copy): new
    	* src/colrow.c (colrow_restore_state_group): don't destroy the indexlist.
    	  Adjust all callers
    	(colrow_get_sizes): new
    	(colrow_index_list_copy): new

 ChangeLog                |   13 +++++
 po-functions/POTFILES.in |    1 +
 po/POTFILES.in           |    1 +
 src/Makefile.am          |    2 +
 src/colrow.c             |   51 +++++++++++++++++++--
 src/colrow.h             |    3 +
 src/commands.c           |  113 ++++++++++++----------------------------------
 src/commands.h           |    2 +-
 8 files changed, 96 insertions(+), 90 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 610e36d..6f91247 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2010-06-25  Andreas J. Guelzow <aguelzow pyrshep ca>
+
+	* src/Makefile.am (undo.[ch]): added
+	* src/commands.h: formatting
+	* src/commands.c (cmd_resize_colrow_*): deleted
+	(cmd_resize_colrow): reimplement using GOUndo.
+	* src/colrow.h (colrow_get_sizes): new
+	(colrow_index_list_copy): new
+	* src/colrow.c (colrow_restore_state_group): don't destroy the indexlist.
+	  Adjust all callers
+	(colrow_get_sizes): new
+	(colrow_index_list_copy): new
+
 2010-06-24  Andreas J. Guelzow <aguelzow pyrshep ca>
 
 	* src/commands.h (gnm_cmd_trunc_descriptor): new
diff --git a/po-functions/POTFILES.in b/po-functions/POTFILES.in
index a98dfcc..b3d09dc 100644
--- a/po-functions/POTFILES.in
+++ b/po-functions/POTFILES.in
@@ -217,6 +217,7 @@ src/tools/random-generator-cor.c
 src/tools/scenarios.c
 src/tools/simulation.c
 src/tools/tabulate.c
+src/undo.c
 src/validation.c
 src/value-sheet.c
 src/value.c
diff --git a/po/POTFILES.in b/po/POTFILES.in
index b8e62bb..87c4080 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -355,6 +355,7 @@ src/tools/random-generator-cor.c
 src/tools/scenarios.c
 src/tools/simulation.c
 src/tools/tabulate.c
+src/undo.c
 src/validation.c
 src/value-sheet.c
 src/value.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 028d81a..8de8af3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -175,6 +175,7 @@ libspreadsheet_la_SOURCES =			\
 	style-color.c				\
 	style-conditions.c			\
 	symbol.c				\
+	undo.c					\
 	validation.c				\
 	value.c					\
 	value-sheet.c				\
@@ -303,6 +304,7 @@ libspreadsheet_include_HEADERS = 	\
 	style-conditions.h			\
 	style-font.h				\
 	symbol.h				\
+	undo.h					\
 	validation.h				\
 	validation-combo.h			\
 	value.h					\
diff --git a/src/colrow.c b/src/colrow.c
index 57e124e..af31d16 100644
--- a/src/colrow.c
+++ b/src/colrow.c
@@ -309,6 +309,21 @@ colrow_get_index_list (int first, int last, ColRowIndexList *list)
 	return list;
 }
 
+ColRowIndexList *
+colrow_index_list_copy (ColRowIndexList *list)
+{
+	GList *copy = NULL, *ptr;
+
+	for (ptr = list ; ptr != NULL ; ptr = ptr->next) {
+		ColRowIndex *tmp = g_new (ColRowIndex, 1);
+		ColRowIndex *ex = ptr->data;
+		tmp->first = ex->first;
+		tmp->last = ex->last;
+		copy = g_list_prepend (copy, tmp);
+	}
+	return g_list_reverse (copy);
+}
+
 static void
 colrow_set_single_state (ColRowState *state,
 			 Sheet *sheet, int i, gboolean is_cols)
@@ -402,6 +417,37 @@ cb_clear_variable_width_content (GnmCellIter const *iter,
 }
 
 ColRowStateGroup *
+colrow_get_sizes (Sheet *sheet, gboolean is_cols,
+		  ColRowIndexList *src, int new_size)
+{
+	ColRowStateGroup *res = NULL;
+	ColRowIndexList *ptr;
+
+	for (ptr = src; ptr != NULL ; ptr = ptr->next) {
+		ColRowIndex const *index = ptr->data;
+		res = g_slist_prepend (res, colrow_get_states (sheet, is_cols,
+			index->first, index->last));
+
+		if (new_size > 0 && index->first == 0 &&
+		    (index->last+1) >= colrow_max (is_cols, sheet)) {
+			ColRowRLEState *rles = g_new0 (ColRowRLEState, 1);
+
+			rles->length = -1; /* Flag as changing the default */
+
+			if (is_cols)
+				rles->state.size_pts = sheet_col_get_default_size_pts (sheet);
+			else
+				rles->state.size_pts = sheet_row_get_default_size_pts (sheet);
+
+			/* Result is a magic 'default' record + >= 1 normal */
+			return g_slist_prepend (res, g_slist_append (NULL, rles));
+		}
+	}
+
+	return res;
+}
+
+ColRowStateGroup *
 colrow_set_sizes (Sheet *sheet, gboolean is_cols,
 		  ColRowIndexList *src, int new_size)
 {
@@ -595,7 +641,6 @@ colrow_restore_state_group (Sheet *sheet, gboolean is_cols,
 
 			/* we are guaranteed to have at least 1 more record */
 			ptr = ptr->next;
-			colrow_state_list_destroy (list);
 		}
 
 		colrow_set_states (sheet, is_cols, index->first, ptr->data);
@@ -604,12 +649,8 @@ colrow_restore_state_group (Sheet *sheet, gboolean is_cols,
 			sheet_foreach_cell_in_range (sheet, CELL_ITER_IGNORE_BLANK,
 				index->first, 0, index->last, gnm_sheet_get_last_row (sheet),
 				(CellIterFunc) &cb_clear_variable_width_content, NULL);
-		colrow_state_list_destroy (ptr->data);
 		selection = selection->prev;
 	}
-
-	/* we clear the list as we go, do not use colrow_state_group_destroy */
-	g_slist_free (state_groups);
 }
 
 /**
diff --git a/src/colrow.h b/src/colrow.h
index 6f085ad..6f15ccd 100644
--- a/src/colrow.h
+++ b/src/colrow.h
@@ -100,6 +100,7 @@ GString         *colrow_index_list_to_string (ColRowIndexList *list,
 					      gboolean *is_single);
 ColRowIndexList *colrow_get_index_list	     (int first, int last,
 					      ColRowIndexList *list);
+ColRowIndexList *colrow_index_list_copy	     (ColRowIndexList *list);
 
 #define colrow_state_list_destroy(l) (go_slist_free_custom ((l), g_free), NULL)
 
@@ -111,6 +112,8 @@ void		 colrow_set_states	     (Sheet *sheet, gboolean is_cols,
 ColRowStateGroup  *colrow_state_group_destroy	(ColRowStateGroup *set);
 ColRowStateGroup  *colrow_set_sizes		(Sheet *sheet, gboolean is_cols,
 						 ColRowIndexList *src, int new_size);
+ColRowStateGroup  *colrow_get_sizes		(Sheet *sheet, gboolean is_cols,
+						 ColRowIndexList *src, int new_size);
 void		   colrow_restore_state_group	(Sheet *sheet, gboolean is_cols,
 						 ColRowIndexList *selection,
 						 ColRowStateGroup *saved_state);
diff --git a/src/commands.c b/src/commands.c
index fe776b6..cea0233 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -75,6 +75,7 @@
 #include "data-shuffling.h"
 #include "tools/tabulate.h"
 #include "wbc-gtk.h"
+#include "undo.h"
 
 #include <goffice/goffice.h>
 #include <gsf/gsf-doc-meta-data.h>
@@ -777,6 +778,7 @@ cmd_set_text_undo (GnmCommand *cmd, WorkbookControl *wbc)
 		colrow_restore_state_group (me->cmd.sheet, TRUE,
 					    me->columns,
 					    me->old_widths);
+		colrow_state_group_destroy (me->old_widths);
 		me->old_widths = NULL;
 		colrow_index_list_destroy (me->columns);
 		me->columns = NULL;
@@ -786,6 +788,7 @@ cmd_set_text_undo (GnmCommand *cmd, WorkbookControl *wbc)
 		colrow_restore_state_group (me->cmd.sheet, FALSE,
 					    me->rows,
 					    me->old_heights);
+		colrow_state_group_destroy (me->old_heights);
 		me->old_heights = NULL;
 		colrow_index_list_destroy (me->rows);
 		me->rows = NULL;
@@ -1664,6 +1667,7 @@ cmd_format_undo (GnmCommand *cmd,
 				colrow_restore_state_group (me->cmd.sheet, FALSE,
 							    os->rows,
 							    os->old_heights);
+				colrow_state_group_destroy (os->old_heights);
 				os->old_heights = NULL;
 				colrow_index_list_destroy (os->rows);
 				os->rows = NULL;
@@ -1841,131 +1845,71 @@ cmd_selection_format (WorkbookControl *wbc,
 
 /******************************************************************/
 
-#define CMD_RESIZE_COLROW_TYPE        (cmd_resize_colrow_get_type ())
-#define CMD_RESIZE_COLROW(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), CMD_RESIZE_COLROW_TYPE, CmdResizeColRow))
-
-typedef struct {
-	GnmCommand cmd;
-
-	Sheet		*sheet;
-	gboolean	 is_cols;
-	ColRowIndexList *selection;
-	ColRowStateGroup*saved_sizes;
-	int		 new_size;
-} CmdResizeColRow;
-
-MAKE_GNM_COMMAND (CmdResizeColRow, cmd_resize_colrow, NULL)
-
-static gboolean
-cmd_resize_colrow_undo (GnmCommand *cmd,
-			G_GNUC_UNUSED WorkbookControl *wbc)
-{
-	CmdResizeColRow *me = CMD_RESIZE_COLROW (cmd);
-
-	g_return_val_if_fail (me != NULL, TRUE);
-	g_return_val_if_fail (me->selection != NULL, TRUE);
-	g_return_val_if_fail (me->saved_sizes != NULL, TRUE);
-
-	colrow_restore_state_group (me->sheet, me->is_cols,
-				    me->selection, me->saved_sizes);
-	me->saved_sizes = NULL;
-
-	return FALSE;
-}
-
-static gboolean
-cmd_resize_colrow_redo (GnmCommand *cmd,
-			G_GNUC_UNUSED WorkbookControl *wbc)
-{
-	CmdResizeColRow *me = CMD_RESIZE_COLROW (cmd);
-
-	g_return_val_if_fail (me != NULL, TRUE);
-	g_return_val_if_fail (me->selection != NULL, TRUE);
-	g_return_val_if_fail (me->saved_sizes == NULL, TRUE);
-
-	me->saved_sizes = colrow_set_sizes (me->sheet, me->is_cols,
-					    me->selection, me->new_size);
-	if (me->cmd.size == 1)
-		me->cmd.size += (g_slist_length (me->saved_sizes) +
-				 g_list_length (me->selection));
-
-	return FALSE;
-}
-static void
-cmd_resize_colrow_finalize (GObject *cmd)
-{
-	CmdResizeColRow *me = CMD_RESIZE_COLROW (cmd);
-
-	colrow_index_list_destroy (me->selection);
-	colrow_state_group_destroy (me->saved_sizes);
-
-	gnm_command_finalize (cmd);
-}
 
 gboolean
 cmd_resize_colrow (WorkbookControl *wbc, Sheet *sheet,
 		   gboolean is_cols, ColRowIndexList *selection,
 		   int new_size)
 {
-	CmdResizeColRow *me;
+	int size = 1;
+	char *text;
+	GOUndo *undo = NULL;
+	GOUndo *redo = NULL;
+	gboolean is_single, result;
 	GString *list;
-	gboolean is_single;
-
-	g_return_val_if_fail (IS_SHEET (sheet), TRUE);
-
-	me = g_object_new (CMD_RESIZE_COLROW_TYPE, NULL);
-
-	me->sheet = sheet;
-	me->is_cols = is_cols;
-	me->selection = selection;
-	me->saved_sizes = NULL;
-	me->new_size = new_size;
-
-	me->cmd.sheet = sheet;
-	me->cmd.size = 1;  /* Changed in initial redo.  */
+	ColRowStateGroup *saved_state;
 
 	list = colrow_index_list_to_string (selection, is_cols, &is_single);
-	/* Make sure the string doesn't get overly wide */
 	gnm_cmd_trunc_descriptor (list, NULL);
 
 	if (is_single) {
 		if (new_size < 0)
-			me->cmd.cmd_descriptor = is_cols
+			text = is_cols
 				? g_strdup_printf (_("Autofitting column %s"), list->str)
 				: g_strdup_printf (_("Autofitting row %s"), list->str);
 		else if (new_size >  0)
-			me->cmd.cmd_descriptor = is_cols
+			text = is_cols
 				? g_strdup_printf (_("Setting width of column %s to %d pixels"),
 						   list->str, new_size)
 				: g_strdup_printf (_("Setting height of row %s to %d pixels"),
 						   list->str, new_size);
-		else me->cmd.cmd_descriptor = is_cols
+		else text = is_cols
 			     ? g_strdup_printf (_("Setting width of column %s to default"),
 						list->str)
 			     : g_strdup_printf (
 				     _("Setting height of row %s to default"), list->str);
 	} else {
 		if (new_size < 0)
-			me->cmd.cmd_descriptor = is_cols
+			text = is_cols
 				? g_strdup_printf (_("Autofitting columns %s"), list->str)
 				: g_strdup_printf (_("Autofitting rows %s"), list->str);
 		else if (new_size >  0)
-			me->cmd.cmd_descriptor = is_cols
+			text = is_cols
 				? g_strdup_printf (_("Setting width of columns %s to %d pixels"),
 						   list->str, new_size)
 				: g_strdup_printf (_("Setting height of rows %s to %d pixels"),
 						   list->str, new_size);
-		else me->cmd.cmd_descriptor = is_cols
+		else text = is_cols
 			     ? g_strdup_printf (
 				     _("Setting width of columns %s to default"), list->str)
 			     : g_strdup_printf (
 				     _("Setting height of rows %s to default"), list->str);
 	}
-
 	g_string_free (list, TRUE);
-	return gnm_command_push_undo (wbc, G_OBJECT (me));
+
+	saved_state = colrow_get_sizes (sheet, is_cols, selection, new_size);;
+	undo = gnm_undo_colrow_restore_state_group_new 
+		(sheet, is_cols, colrow_index_list_copy (selection), saved_state);
+
+ 	redo = gnm_undo_colrow_set_sizes_new (sheet, is_cols, selection, new_size);
+	
+	result = cmd_generic_with_size (wbc, text, size, undo, redo);
+	g_free (text);
+
+	return result;
 }
 
+
 /******************************************************************/
 
 #define CMD_SORT_TYPE        (cmd_sort_get_type ())
@@ -3113,6 +3057,7 @@ cmd_autofill_undo (GnmCommand *cmd, WorkbookControl *wbc)
 		colrow_restore_state_group (me->cmd.sheet, TRUE,
 					    me->columns,
 					    me->old_widths);
+		colrow_state_group_destroy (me->old_widths);
 		me->old_widths = NULL;
 		colrow_index_list_destroy (me->columns);
 		me->columns = NULL;
diff --git a/src/commands.h b/src/commands.h
index 47eacee..f3371f2 100644
--- a/src/commands.h
+++ b/src/commands.h
@@ -1,6 +1,6 @@
 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 #ifndef _GNM_COMMANDS_H_
-# define _GNM_COMMANDS_H_
+#define _GNM_COMMANDS_H_
 
 #include "gnumeric.h"
 #include "tools/tools.h"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]