gnumeric r16498 - in branches/gnumeric-1-8: . src



Author: mortenw
Date: Thu Apr 10 15:13:18 2008
New Revision: 16498
URL: http://svn.gnome.org/viewvc/gnumeric?rev=16498&view=rev

Log:
2008-04-10  Morten Welinder  <terra gnome org>

	* src/clipboard.c (paste_cell): Translate dates if required.
	Fixes #490419.
	(cellregion_new): Setup new date_conv field from sheet.

	* src/xml-io.c (xml_cellregion_read): Set of date_conv field from
	xml.
	(xml_read_clipboard_cell): Use date_conv from cell region, not
	workbook.



Modified:
   branches/gnumeric-1-8/ChangeLog
   branches/gnumeric-1-8/NEWS
   branches/gnumeric-1-8/configure.in
   branches/gnumeric-1-8/src/clipboard.c
   branches/gnumeric-1-8/src/clipboard.h
   branches/gnumeric-1-8/src/commands.c
   branches/gnumeric-1-8/src/gutils.h
   branches/gnumeric-1-8/src/xml-io.c

Modified: branches/gnumeric-1-8/NEWS
==============================================================================
--- branches/gnumeric-1-8/NEWS	(original)
+++ branches/gnumeric-1-8/NEWS	Thu Apr 10 15:13:18 2008
@@ -18,6 +18,7 @@
 	* Improve performance of analysis tools for large ranges.
 	* Fix performance repeated VLOOKUP/HLOOKUP/MATCH with constant
 	  data range.  [#525875]
+	* Fix date pasting problems to/from 1904 workbooks.  [#490419]
 
 --------------------------------------------------------------------------
 Gnumeric 1.8.2

Modified: branches/gnumeric-1-8/configure.in
==============================================================================
--- branches/gnumeric-1-8/configure.in	(original)
+++ branches/gnumeric-1-8/configure.in	Thu Apr 10 15:13:18 2008
@@ -171,7 +171,7 @@
 
 dnl *****************************
 libspreadsheet_reqs="
-	$libgoffice		>= 0.5.5
+	$libgoffice		>= 0.6.3
 	libgsf-1		>= 1.14.6
 	libxml-2.0		>= 2.4.12
 "

Modified: branches/gnumeric-1-8/src/clipboard.c
==============================================================================
--- branches/gnumeric-1-8/src/clipboard.c	(original)
+++ branches/gnumeric-1-8/src/clipboard.c	Thu Apr 10 15:13:18 2008
@@ -45,6 +45,7 @@
 #include <locale.h>
 #include <string.h>
 #include <goffice/utils/go-glib-extras.h>
+#include <goffice/utils/datetime.h>
 
 #ifndef USE_CELL_COPY_POOLS
 #define USE_CELL_COPY_POOLS 1
@@ -189,6 +190,14 @@
 	}
 }
 
+struct paste_cell_data {
+	GnmPasteTarget const *pt;
+	GnmCellRegion const  *cr;
+	GnmCellPos	top_left;
+	GnmExprRelocateInfo rinfo;
+	gboolean translate_dates;
+};
+
 /**
  * paste_cell: Pastes a cell in the spreadsheet
  * @dst_sheet:   The sheet where the pasting will be done
@@ -199,19 +208,21 @@
  * @paste_flags: Bit mask that describes the paste options.
  */
 static void
-paste_cell (Sheet *dst_sheet,
-	    int target_col, int target_row,
-	    GnmExprRelocateInfo const *rinfo,
-	    GnmCellCopy const *src, int paste_flags)
+paste_cell (int target_col, int target_row,
+	    GnmCellCopy const *src,
+	    const struct paste_cell_data *dat)
 {
+	Sheet *dst_sheet = dat->pt->sheet;
+	int paste_flags = dat->pt->paste_flags;
+
 	if (paste_flags & PASTE_OPER_MASK)
 		paste_cell_with_operation (dst_sheet, target_col, target_row,
-					   rinfo, src, paste_flags);
+					   &dat->rinfo, src, paste_flags);
 	else {
 		GnmCell *dst = sheet_cell_fetch (dst_sheet, target_col, target_row);
 		if (NULL != src->texpr && (paste_flags & PASTE_CONTENTS)) {
 			GnmExprTop const *relo = gnm_expr_top_relocate (
-				src->texpr, rinfo, FALSE);
+				src->texpr, &dat->rinfo, FALSE);
 			if (paste_flags & PASTE_TRANSPOSE) {
 				GnmExprTop const *trelo =
 					gnm_expr_top_transpose (relo ? relo : src->texpr);
@@ -228,8 +239,28 @@
 						 value_dup (src->val), TRUE);
 			if (NULL != relo)
 				gnm_expr_top_unref (relo);
-		} else
-			gnm_cell_set_value (dst, value_dup (src->val));
+		} else {
+			GnmValue *newval = NULL;
+			GnmValue const *oldval = src->val;
+
+			if (dat->translate_dates && oldval && VALUE_IS_FLOAT (oldval)) {
+				GOFormat const *fmt = VALUE_FMT (oldval)
+					? VALUE_FMT (oldval)
+					: gnm_style_get_format (gnm_cell_get_style (dst));
+				if (go_format_is_date (fmt) == +1) {
+					gnm_float fnew = go_date_conv_translate
+						(value_get_as_float (oldval),
+						 dat->cr->date_conv,
+						 workbook_date_conv (dst_sheet->workbook));
+					newval = value_new_float (fnew);
+					value_set_fmt (newval, VALUE_FMT (oldval));
+				}
+			}
+
+			if (!newval)
+				newval = value_dup (src->val);
+			gnm_cell_set_value (dst, newval);
+		}
 	}
 }
 
@@ -264,13 +295,6 @@
 	g_object_unref (dst);
 }
 
-struct paste_cell_data {
-	GnmPasteTarget const *pt;
-	GnmCellRegion const  *cr;
-	GnmCellPos	top_left;
-	GnmExprRelocateInfo rinfo;
-};
-
 static void
 cb_paste_cell (GnmCellCopy const *src, gconstpointer ignore,
 	       struct paste_cell_data *dat)
@@ -295,8 +319,7 @@
 		dat->rinfo.pos.eval.row = target_row;
 	}
 
-	paste_cell (dat->pt->sheet, target_col, target_row,
-		    &dat->rinfo, src, dat->pt->paste_flags);
+	paste_cell (target_col, target_row, src, dat);
 }
 
 
@@ -324,6 +347,7 @@
 	GSList *ptr;
 	GnmRange const *r;
 	gboolean has_contents, adjust_merges = TRUE;
+	struct paste_cell_data dat;
 
 	g_return_val_if_fail (pt != NULL, TRUE);
 	g_return_val_if_fail (cr != NULL, TRUE);
@@ -432,11 +456,13 @@
 				    clearFlags, cc);
 	}
 
+	dat.translate_dates = cr->date_conv &&
+		!go_date_conv_equal (cr->date_conv, workbook_date_conv (pt->sheet->workbook));
+
 	for (i = 0; i < repeat_horizontal ; i++)
 		for (j = 0; j < repeat_vertical ; j++) {
 			int const left = i * src_cols + pt->range.start.col;
 			int const top = j * src_rows + pt->range.start.row;
-			struct paste_cell_data dat;
 
 			dat.top_left.col = left;
 			dat.top_left.row = top;
@@ -694,6 +720,9 @@
 {
 	GnmCellRegion *cr = g_new0 (GnmCellRegion, 1);
 	cr->origin_sheet	= origin_sheet;
+	cr->date_conv           = origin_sheet && origin_sheet->workbook
+		? workbook_date_conv (origin_sheet->workbook)
+		: NULL;
 	cr->cols = cr->rows	= -1;
 	cr->not_as_contents	= FALSE;
 	cr->cell_content	= NULL;

Modified: branches/gnumeric-1-8/src/clipboard.h
==============================================================================
--- branches/gnumeric-1-8/src/clipboard.h	(original)
+++ branches/gnumeric-1-8/src/clipboard.h	Thu Apr 10 15:13:18 2008
@@ -56,6 +56,7 @@
 
 struct _GnmCellRegion {
 	Sheet		*origin_sheet; /* can be NULL */
+	const GODateConventions *date_conv; /* can be NULL */
 	GnmCellPos	 base;
 	int		 cols, rows;
 	ColRowStateList *col_state, *row_state;

Modified: branches/gnumeric-1-8/src/commands.c
==============================================================================
--- branches/gnumeric-1-8/src/commands.c	(original)
+++ branches/gnumeric-1-8/src/commands.c	Thu Apr 10 15:13:18 2008
@@ -2760,6 +2760,30 @@
 
 /******************************************************************/
 
+static void
+warn_if_date_trouble (WorkbookControl *wbc, GnmCellRegion *cr)
+{
+	Workbook *wb = wb_control_get_workbook (wbc);
+	const GODateConventions *wb_date_conv = workbook_date_conv (wb);
+
+	if (cr->date_conv == NULL)
+		return;
+	if (go_date_conv_equal (cr->date_conv, wb_date_conv))
+		return;
+
+	/* We would like to show a warning, but it seems we cannot via a context.  */
+	{
+		GError *err;
+		err = g_error_new (go_error_invalid(), 0,
+				   _("Copying between files with different date conventions.\n"
+				     "It is possible that some dates could be be copied\n"
+				     "incorrectly."));
+		go_cmd_context_error (GO_CMD_CONTEXT (wbc), err);
+		g_error_free (err);
+	}
+}
+
+
 #define CMD_PASTE_COPY_TYPE        (cmd_paste_copy_get_type ())
 #define CMD_PASTE_COPY(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), CMD_PASTE_COPY_TYPE, CmdPasteCopy))
 
@@ -3033,6 +3057,8 @@
 		return TRUE;
 	}
 
+	warn_if_date_trouble (wbc, cr);
+
 	return command_push_undo (wbc, G_OBJECT (me));
 }
 

Modified: branches/gnumeric-1-8/src/gutils.h
==============================================================================
--- branches/gnumeric-1-8/src/gutils.h	(original)
+++ branches/gnumeric-1-8/src/gutils.h	Thu Apr 10 15:13:18 2008
@@ -2,6 +2,7 @@
 #ifndef _GNM_GUTILS_H_
 # define _GNM_GUTILS_H_
 
+#include "gnumeric.h"
 #include <goffice/utils/regutf8.h>
 
 G_BEGIN_DECLS

Modified: branches/gnumeric-1-8/src/xml-io.c
==============================================================================
--- branches/gnumeric-1-8/src/xml-io.c	(original)
+++ branches/gnumeric-1-8/src/xml-io.c	Thu Apr 10 15:13:18 2008
@@ -2078,10 +2078,8 @@
 			cc->val = value_new_from_string (value_type,
 				CXML2C (content), value_fmt, FALSE);
 		else {
-			GODateConventions const *date_conv =
-				ctxt->wb ? workbook_date_conv (ctxt->wb) : NULL;
 			parse_text_value_or_expr (&pp, CXML2C (content),
-				&cc->val, &cc->texpr, value_fmt, date_conv);
+				&cc->val, &cc->texpr, value_fmt, cr->date_conv);
 		}
 
 		if (shared_expr_index > 0) {
@@ -2143,6 +2141,7 @@
 	GnmCellRegion   *cr = NULL;
 	GnmLocale       *locale;
 	int dummy;
+	xmlChar *dateconvstr;
 
 	g_return_val_if_fail (buffer != NULL, NULL);
 
@@ -2174,6 +2173,15 @@
 	/* if it exists it is TRUE */
 	cr->not_as_contents = xml_node_get_int (clipboard, "NotAsContent", &dummy);
 
+	dateconvstr = xml_node_get_cstr (clipboard, "DateConvention");
+	if (!dateconvstr)
+		dateconvstr = xml_node_get_cstr (clipboard, "gnm:DateConvention");
+	cr->date_conv = go_date_conv_from_str (dateconvstr
+					       ? CXML2C (dateconvstr)
+					       : "Lotus:1900");
+	if (dateconvstr)
+		xmlFree (dateconvstr);
+
 	l = e_xml_get_child_by_name (clipboard, CC2XML ("Styles"));
 	if (l != NULL)
 		for (l = l->xmlChildrenNode; l != NULL ; l = l->next)



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