[gnumeric] rewrite cmd_autofilter_add_remove using cmd_generic (prep for enhancement request #607086)



commit 20d90570deb46aef2b072b93c60416275d773b33
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Mon Jul 5 14:00:09 2010 -0600

    rewrite cmd_autofilter_add_remove using cmd_generic (prep for enhancement request #607086)
    
    2010-07-05  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* src/commands.c (CMD_AUTOFILTER_ADD_REMOVE): delete
    	(cmd_autofilter_add_remove_*): delete
    	(cmd_autofilter_add_remove): rewrite using cmd_generic (prep for
    	  enhancement request #607086)

 ChangeLog          |    7 +++
 src/commands.c     |  104 ++++++++++++++++------------------------------------
 src/sheet-object.c |    2 +-
 3 files changed, 40 insertions(+), 73 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d1d6acb..ac508fd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2010-07-05  Andreas J. Guelzow <aguelzow pyrshep ca>
 
+	* src/commands.c (CMD_AUTOFILTER_ADD_REMOVE): delete
+	(cmd_autofilter_add_remove_*): delete
+	(cmd_autofilter_add_remove): rewrite using cmd_generic (prep for
+	  enhancement request #607086)
+
+2010-07-05  Andreas J. Guelzow <aguelzow pyrshep ca>
+
 	* src/sheet-filter.h (gnm_sheet_filter_intersect_rows): new
 	* src/sheet-filter.c (gnm_sheet_filter_intersect_rows): new
 	* src/sheet-view.h (sv_selection_intersects_filter_rows): new
diff --git a/src/commands.c b/src/commands.c
index 7200698..ec87ac7 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -7520,63 +7520,17 @@ cmd_so_set_adjustment (WorkbookControl *wbc,
 
 /******************************************************************/
 
-#define CMD_AUTOFILTER_ADD_REMOVE_TYPE        (cmd_autofilter_add_remove_get_type ())
-#define CMD_AUTOFILTER_ADD_REMOVE(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), CMD_AUTOFILTER_ADD_REMOVE_TYPE, CmdAutofilterAddRemove))
-
-typedef struct {
-	GnmCommand cmd;
-	
-	GnmFilter *filter;
-	gboolean   add;
-} CmdAutofilterAddRemove;
-
-MAKE_GNM_COMMAND (CmdAutofilterAddRemove, cmd_autofilter_add_remove, NULL)
-
-static gboolean
-cmd_autofilter_add_remove_impl (CmdAutofilterAddRemove *me, gboolean add)
-{
-	if (add)
-		gnm_filter_attach (me->filter, me->cmd.sheet);
-	else
-		gnm_filter_remove (me->filter);	
-
-	return FALSE;
-}
-
-
-static gboolean
-cmd_autofilter_add_remove_undo (GnmCommand *cmd, WorkbookControl *wbc)
-{
-	CmdAutofilterAddRemove *me = CMD_AUTOFILTER_ADD_REMOVE (cmd);
-
-	return cmd_autofilter_add_remove_impl (me, !me->add);
-}
-
-static gboolean
-cmd_autofilter_add_remove_redo (GnmCommand *cmd, WorkbookControl *wbc)
-{
-	CmdAutofilterAddRemove *me = CMD_AUTOFILTER_ADD_REMOVE (cmd);
-
-	return cmd_autofilter_add_remove_impl (me, me->add);
-}
-
-static void
-cmd_autofilter_add_remove_finalize (GObject *cmd)
-{
-	CmdAutofilterAddRemove *me = CMD_AUTOFILTER_ADD_REMOVE (cmd);
-
-	gnm_filter_unref (me->filter);
-	gnm_command_finalize (cmd);
-}
-
 gboolean
 cmd_autofilter_add_remove (WorkbookControl *wbc)
 {
-	CmdAutofilterAddRemove *me;
 	SheetView *sv = wb_control_cur_sheet_view (wbc);
 	GnmFilter *f = sv_editpos_in_filter (sv);
 	gboolean add = (f == NULL);
-	char *descr;
+	char *descr = NULL, *name = NULL;
+	GOUndo *undo = NULL;
+	GOUndo *redo = NULL;
+	gboolean result;
+	
 
 	if (add) {
 		GnmRange region;
@@ -7608,32 +7562,38 @@ cmd_autofilter_add_remove (WorkbookControl *wbc)
 		}
 		gnm_filter_remove (f);
 
+		redo = go_undo_binary_new 
+			(gnm_filter_ref (f), sv->sheet, 
+			 (GOUndoBinaryFunc) gnm_filter_attach,
+			 (GFreeFunc) gnm_filter_unref,
+			 NULL);
+		undo = go_undo_unary_new
+			(f, 
+			 (GOUndoUnaryFunc) gnm_filter_remove,
+			 (GFreeFunc) gnm_filter_unref);
+		
+		name = undo_range_name (sv->sheet, &(f->r));
 		descr = g_strdup_printf (_("Add Autofilter to %s"),
-					 range_as_string (&(f->r)));
+					 name);
 	} else {
-		/*
-		 * Removing a filter.
-		 * This actual removal is in the redo handler.
-		 */
+		undo = go_undo_binary_new 
+			(gnm_filter_ref (f), sv->sheet, 
+			 (GOUndoBinaryFunc) gnm_filter_attach,
+			 (GFreeFunc) gnm_filter_unref,
+			 NULL);
+		redo = go_undo_unary_new
+			(gnm_filter_ref (f), 
+			 (GOUndoUnaryFunc) gnm_filter_remove,
+			 (GFreeFunc) gnm_filter_unref);
+		name = undo_range_name (sv->sheet, &(f->r));
 		descr = g_strdup_printf (_("Remove Autofilter from %s"),
-					 range_as_string (&(f->r)));
+					 name);
 	}
+	result = cmd_generic (wbc, descr, undo, redo);
+	g_free (name);
+	g_free (descr);
 
-	me = g_object_new (CMD_AUTOFILTER_ADD_REMOVE_TYPE, NULL);
-
-	me->cmd.sheet = sv->sheet;
-	me->cmd.size = 1;
-	me->cmd.cmd_descriptor = descr;
-
-	me->filter = f;
-	me->add = add;
-
-	sheet_redraw_all (sv->sheet, TRUE);
-
-	sheet_mark_dirty (sv->sheet);
-	sheet_update (sv->sheet);
-
-	return gnm_command_push_undo (wbc, G_OBJECT (me));
+	return result;
 }
 
 
diff --git a/src/sheet-object.c b/src/sheet-object.c
index 397ba79..7ed3757 100644
--- a/src/sheet-object.c
+++ b/src/sheet-object.c
@@ -826,7 +826,7 @@ clear_sheet (SheetObject *so, GOUndo **pundo)
 			(g_object_ref (so),
 			 so->sheet,
 			 (GOUndoBinaryFunc)sheet_object_set_sheet,
-			 g_object_unref,
+			 (GFreeFunc) g_object_unref,
 			 NULL);
 		*pundo = go_undo_combine (*pundo, u);
 	}



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