[gnumeric] WorkbookView: fix tracking of single cell.



commit 44d958d66f681f39898cc20845d765544042bcdd
Author: Morten Welinder <terra gnome org>
Date:   Sun Jan 1 15:30:33 2012 -0500

    WorkbookView: fix tracking of single cell.
    
    Managed dependents are the tool of choice for this because we will
    automatically track changes due to insert/delete column/row, etc.

 ChangeLog           |    3 +
 NEWS                |    1 +
 src/wbc-gtk.c       |   34 +++++++---------
 src/workbook-view.c |  102 ++++++++++++++++++--------------------------------
 src/workbook-view.h |    6 +-
 5 files changed, 59 insertions(+), 87 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9796f34..ba7f858 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2012-01-01  Morten Welinder  <terra gnome org>
 
+	* src/workbook-view.h (WorkbookView): Rework to track a cell in a
+	managed dependent.
+
 	* src/dependent.c (dependent_managed_set_sheet): New function for
 	this mildly complicated operation.
 
diff --git a/NEWS b/NEWS
index 8e3433d..6f6bdb7 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,7 @@ Morten:
 	* Fix multihead problem with format dialog.
 	* Fix auto-expression problem with sheet deletion.
 	* Fix ellipsizing of auto-expression.
+	* Fix auto-expression of single cell when moved.
 
 --------------------------------------------------------------------------
 Gnumeric 1.11.1
diff --git a/src/wbc-gtk.c b/src/wbc-gtk.c
index 6e27811..8c33009 100644
--- a/src/wbc-gtk.c
+++ b/src/wbc-gtk.c
@@ -4511,23 +4511,20 @@ wbc_gtk_setup_icons (void)
 static void
 cb_auto_expr_cell_changed (GtkWidget *item, WBCGtk *wbcg)
 {
-	const Sheet *sheet;
 	const char *descr;
-	const GnmRange *r;
 	WorkbookView *wbv = wb_control_view (WORKBOOK_CONTROL (wbcg));
+	const GnmEvalPos *ep;
 
 	if (wbcg->updating_ui)
 		return;
 
 	descr = g_object_get_data (G_OBJECT (item), "descr");
-	sheet = g_object_get_data (G_OBJECT (item), "sheet");
-	r = g_object_get_data (G_OBJECT (item), "cell");
+	ep = g_object_get_data (G_OBJECT (item), "evalpos");
 
 	g_object_set (wbv,
-		      "auto-expr-func",  NULL,
+		      "auto-expr-func", NULL,
 		      "auto-expr-descr", descr,
-		      "auto-expr-cell",  &r->start,
-		      "auto-expr-sheet", sheet,
+		      "auto-expr-eval-pos", ep,
 		      NULL);
 
 }
@@ -4548,8 +4545,7 @@ cb_auto_expr_changed (GtkWidget *item, WBCGtk *wbcg)
 	g_object_set (wbv,
 		      "auto-expr-func", func,
 		      "auto-expr-descr", descr,
-		      "auto-expr-cell", NULL,
-		      "auto-expr-sheet", NULL,
+		      "auto-expr-eval-pos", NULL,
 		      NULL);
 }
 
@@ -4687,9 +4683,10 @@ cb_select_auto_expr (GtkWidget *widget, GdkEventButton *event, WBCGtk *wbcg)
 	Sheet *sheet = wb_view_cur_sheet (wbv);
 	GtkWidget *item, *menu;
 	int i;
-        GnmRange *r = g_new (GnmRange, 1);
-	char const *rname;
-	char * cell_item;
+	char *rname;
+	char *cell_item;
+	GnmCellPos const *pos;
+	GnmEvalPos ep;
 
 	if (event->button != 3)
 		return FALSE;
@@ -4734,19 +4731,18 @@ cb_select_auto_expr (GtkWidget *widget, GdkEventButton *event, WBCGtk *wbcg)
 	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
 	gtk_widget_show (item);
 
-	range_init_cellpos (r, &(scg_view (wbcg_cur_scg (wbcg)))->edit_pos);
-	rname = range_as_string (r);
-	cell_item = g_strdup_printf (_("Content of cell %s!%s"), sheet->name_unquoted, rname);
+	pos = &(scg_view (wbcg_cur_scg (wbcg)))->edit_pos;
+	eval_pos_init_pos (&ep, sheet, pos);
+	rname = g_strdup (cellpos_as_string (pos));
+	cell_item = g_strdup_printf (_("Content of %s"), rname);
 	item = gtk_menu_item_new_with_label(cell_item);
 	g_free (cell_item);
 	g_object_set_data_full (G_OBJECT (item),
-				"descr", (gpointer)g_strdup (rname),
+				"descr", rname,
 				(GDestroyNotify)g_free);
 	g_object_set_data_full (G_OBJECT (item),
-				"cell", (gpointer)r,
+				"evalpos", g_memdup (&ep, sizeof (ep)),
 				(GDestroyNotify)g_free);
-	g_object_set_data (G_OBJECT (item),
-			   "sheet", (gpointer)sheet);
 	g_signal_connect (G_OBJECT (item), "activate",
 		G_CALLBACK (cb_auto_expr_cell_changed), wbcg);
 	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
diff --git a/src/workbook-view.c b/src/workbook-view.c
index 4603e5b..33ce6b6 100644
--- a/src/workbook-view.c
+++ b/src/workbook-view.c
@@ -77,8 +77,7 @@ enum {
 	PROP_AUTO_EXPR_MAX_PRECISION,
 	PROP_AUTO_EXPR_TEXT,
 	PROP_AUTO_EXPR_ATTRS,
-	PROP_AUTO_EXPR_CELL,
-	PROP_AUTO_EXPR_SHEET,
+	PROP_AUTO_EXPR_EVAL_POS,
 	PROP_SHOW_HORIZONTAL_SCROLLBAR,
 	PROP_SHOW_VERTICAL_SCROLLBAR,
 	PROP_SHOW_NOTEBOOK_TABS,
@@ -530,7 +529,6 @@ wb_view_auto_expr_recalc (WorkbookView *wbv)
 	GnmValue	*v;
 	SheetView	*sv;
 	GnmExprTop const *texpr;
-	GnmRange        r;
 	GString *str;
 
 	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));
@@ -540,19 +538,17 @@ wb_view_auto_expr_recalc (WorkbookView *wbv)
 	    sv == NULL)
 		return;
 
-	if (wbv->auto_expr.sheet != NULL &&
-	    wbv->auto_expr.descr != NULL &&
-	    wbv->auto_expr.cell.row >= 0 &&
-	    wbv->auto_expr.cell.col >= 0) {
-		GnmValue *v;
-
-		str = g_string_new (wbv->auto_expr.sheet->name_unquoted);
-		g_string_append_c (str, '!');
-
-		range_init_cellpos (&r, &wbv->auto_expr.cell);
-		v = value_new_cellrange_r (wbv->auto_expr.sheet, &r);
-		texpr = gnm_expr_top_new_constant (v);
-		g_string_append (str, wbv->auto_expr.descr);
+	if (wbv->auto_expr.dep.sheet != NULL &&
+	    wbv->auto_expr.dep.texpr != NULL &&
+	    wbv->auto_expr.descr != NULL) {
+		GnmValue const *v;
+
+		texpr = wbv->auto_expr.dep.texpr;
+		gnm_expr_top_ref (texpr);
+		v = gnm_expr_top_get_constant (texpr);
+		str = g_string_new (v
+				    ? value_peek_string (v)
+				    : value_error_name (GNM_ERROR_REF, TRUE));
 	} else if (wbv->auto_expr.func != NULL &&
 		   wbv->auto_expr.descr != NULL) {
 		sv_selection_apply (sv, &accumulate_regions, FALSE, &selection);
@@ -748,54 +744,47 @@ wb_view_auto_expr_attrs (WorkbookView *wbv, PangoAttrList *attrs)
 }
 
 static void
-wb_view_auto_expr_cell (WorkbookView *wbv, gpointer *cell)
-{
-	if (cell == NULL) {
-		wbv->auto_expr.cell.col = -1;
-		wbv->auto_expr.cell.row = -1;
-	} else {
-		wbv->auto_expr.cell = *((GnmCellPos *)cell);
-		wb_view_auto_expr_recalc (wbv);
-	}
-}
-
-static void
 cb_clear_auto_expr_sheet (WorkbookView *wbv)
 {
 	g_object_set (G_OBJECT (wbv),
-		      "auto-expr-sheet", NULL,
-		      "auto-expr-cell", NULL,
+		      "auto-expr-eval-pos", NULL,
 		      NULL);
 }
 
 static void
-wb_view_auto_expr_sheet (WorkbookView *wbv, Sheet *sheet)
+wb_view_auto_expr_eval_pos (WorkbookView *wbv, GnmEvalPos const *ep)
 {
-	if (wbv->auto_expr.sheet == sheet)
-		return;
+	Sheet *sheet = ep ? ep->sheet : NULL;
 
 	if (wbv->auto_expr.sheet_detached_sig) {
-		g_signal_handler_disconnect (wbv->auto_expr.sheet,
+		g_signal_handler_disconnect (wbv->auto_expr.dep.sheet,
 					     wbv->auto_expr.sheet_detached_sig);
 		wbv->auto_expr.sheet_detached_sig = 0;
 	}
-	if (wbv->auto_expr.sheet)
-		g_object_unref (wbv->auto_expr.sheet);
 
-	wbv->auto_expr.sheet = sheet;
+	dependent_set_expr (&wbv->auto_expr.dep, NULL);
+	dependent_managed_set_sheet (&wbv->auto_expr.dep, sheet);
+
 	if (sheet) {
-		g_object_ref (sheet);
+		GnmRange r;
+		GnmValue *v;
+		GnmExprTop const *texpr;
 
 		wbv->auto_expr.sheet_detached_sig = g_signal_connect_swapped (
 			G_OBJECT (sheet),
 			"detached-from-workbook",
 			G_CALLBACK (cb_clear_auto_expr_sheet), wbv);
+
+		range_init_cellpos (&r, &ep->eval);
+		v = value_new_cellrange_r (sheet, &r);
+		texpr = gnm_expr_top_new_constant (v);
+		dependent_managed_set_expr (&wbv->auto_expr.dep, texpr);
+		gnm_expr_top_unref (texpr);
 	}
 
 	wb_view_auto_expr_recalc (wbv);
 }
 
-
 static void
 wb_view_set_property (GObject *object, guint property_id,
 		      const GValue *value, GParamSpec *pspec)
@@ -818,11 +807,8 @@ wb_view_set_property (GObject *object, guint property_id,
 	case PROP_AUTO_EXPR_ATTRS:
 		wb_view_auto_expr_attrs (wbv, g_value_peek_pointer (value));
 		break;
-	case PROP_AUTO_EXPR_CELL:
-		wb_view_auto_expr_cell (wbv, g_value_get_pointer (value));
-		break;
-	case PROP_AUTO_EXPR_SHEET:
-		wb_view_auto_expr_sheet (wbv, g_value_get_object (value));
+	case PROP_AUTO_EXPR_EVAL_POS:
+		wb_view_auto_expr_eval_pos (wbv, g_value_get_pointer (value));
 		break;
 	case PROP_SHOW_HORIZONTAL_SCROLLBAR:
 		wbv->show_horizontal_scrollbar = !!g_value_get_boolean (value);
@@ -883,12 +869,6 @@ wb_view_get_property (GObject *object, guint property_id,
 	case PROP_AUTO_EXPR_ATTRS:
 		g_value_set_boxed (value, wbv->auto_expr.attrs);
 		break;
-	case PROP_AUTO_EXPR_CELL:
-		g_value_set_pointer (value, &wbv->auto_expr.cell);
-		break;
-	case PROP_AUTO_EXPR_SHEET:
-		g_value_set_object (value, wbv->auto_expr.sheet);
-		break;
 	case PROP_SHOW_HORIZONTAL_SCROLLBAR:
 		g_value_set_boolean (value, wbv->show_horizontal_scrollbar);
 		break;
@@ -949,7 +929,7 @@ wb_view_dispose (GObject *object)
 			g_warning ("Unexpected left-over controls");
 	}
 
-	wb_view_auto_expr_sheet (wbv, NULL);
+	wb_view_auto_expr_eval_pos (wbv, NULL);
 	wb_view_auto_expr_func (wbv, NULL);
 	wb_view_auto_expr_attrs (wbv, NULL);
 	wb_view_auto_expr_text (wbv, NULL);
@@ -1026,21 +1006,12 @@ workbook_view_class_init (GObjectClass *gobject_class)
 				     GSF_PARAM_STATIC | G_PARAM_READWRITE));
         g_object_class_install_property
 		(gobject_class,
-		 PROP_AUTO_EXPR_CELL,
-		 g_param_spec_pointer ("auto-expr-cell",
-				       _("Auto-expression Cell Position"),
-				       _("The address of the cell to be shown."),
+		 PROP_AUTO_EXPR_EVAL_POS,
+		 g_param_spec_pointer ("auto-expr-eval-pos",
+				       _("Auto-expression position"),
+				       _("The cell position to track."),
 				       GSF_PARAM_STATIC |
-				       G_PARAM_READWRITE));
-        g_object_class_install_property
-		(gobject_class,
-		 PROP_AUTO_EXPR_SHEET,
-		 g_param_spec_object ("auto-expr-sheet",
-				      _("Auto-expression Sheet"),
-				      _("The sheet on which the cell resides."),
-				      GNM_SHEET_TYPE,
-				      GSF_PARAM_STATIC |
-				      G_PARAM_READWRITE));
+				       G_PARAM_WRITABLE));
         g_object_class_install_property
 		(gobject_class,
 		 PROP_SHOW_HORIZONTAL_SCROLLBAR,
@@ -1166,6 +1137,7 @@ workbook_view_new (Workbook *wb)
 	wbv->auto_expr.text = NULL;
 	wbv->auto_expr.attrs = NULL;
 	wbv->auto_expr.use_max_precision = FALSE;
+	dependent_managed_init (&wbv->auto_expr.dep, NULL);
 
 	for (i = 0 ; i < workbook_sheet_count (wb); i++)
 		wb_view_sheet_add (wbv, workbook_sheet_by_index (wb, i));
diff --git a/src/workbook-view.h b/src/workbook-view.h
index bff1689..0f5020d 100644
--- a/src/workbook-view.h
+++ b/src/workbook-view.h
@@ -3,6 +3,7 @@
 # define _GNM_WORKBOOK_VIEW_H_
 
 #include "gnumeric.h"
+#include "dependent.h"
 #include <glib-object.h>
 #include <gsf/gsf.h>
 
@@ -31,14 +32,13 @@ struct _WorkbookView {
 
 	/* The auto-expression */
 	struct {
-		GnmFunc  *func;
+		GnmFunc *func;
 		char *descr;
 		char *text;
 		PangoAttrList *attrs;
 		gboolean use_max_precision;
-		Sheet *sheet;
+		GnmDependent dep;
 		gulong sheet_detached_sig;
-		GnmCellPos cell;
 	} auto_expr;
 
 	/* selection */



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