[gnumeric] Fix editing of decimals. [#670558]



commit fae30dfddb6d31b466fa6bf6fe22b4347425cdcd
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Tue Feb 21 17:51:03 2012 -0700

    Fix editing of decimals. [#670558]
    
    2012-02-21  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* src/cell.h (gnm_cell_get_text_for_editing): new
    	* src/cell.c (gnm_cell_get_text_for_editing): new, extracted
    	from wbcg_edit_start in src/wbc-gtk-edit.c
    	(close_to_int): new, moved here from src/wbc-gtk-edit.c
    	(guess_time_format): ditto
    	* src/commands.c (cmd_set_text_full_check_text): use
    	gnm_cell_get_text_for_editing
    	* src/wbc-gtk-edit.c (close_to_int): move to src/cell.c
    	(guess_time_format): ditto
    	(wbcg_edit_start): use gnm_cell_get_text_for_editing

 ChangeLog          |   13 ++++
 NEWS               |    1 +
 src/cell.c         |  162 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/cell.h         |    2 +
 src/commands.c     |    9 ++-
 src/wbc-gtk-edit.c |  144 +---------------------------------------------
 6 files changed, 185 insertions(+), 146 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 620dfe2..6f525f8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2012-02-21  Andreas J. Guelzow <aguelzow pyrshep ca>
+
+	* src/cell.h (gnm_cell_get_text_for_editing): new
+	* src/cell.c (gnm_cell_get_text_for_editing): new, extracted
+	from wbcg_edit_start in src/wbc-gtk-edit.c
+	(close_to_int): new, moved here from src/wbc-gtk-edit.c
+	(guess_time_format): ditto
+	* src/commands.c (cmd_set_text_full_check_text): use
+	gnm_cell_get_text_for_editing
+	* src/wbc-gtk-edit.c (close_to_int): move to src/cell.c
+	(guess_time_format): ditto
+	(wbcg_edit_start): use gnm_cell_get_text_for_editing
+
 2012-02-20  Jean Brefort  <jean brefort normalesup org>
 
 	* src/undo.c: rename GNMUndo* to GnmUndo*.
diff --git a/NEWS b/NEWS
index 1bbb7fb..6b4aedd 100644
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,7 @@ Andreas:
 	* Fix data shuffle. [#669184]
 	* Clarify the skew-t and skew-normal descriptions.
 	* Fix EXACT. [#670232]
+	* Fix editing of decimals. [#670558]
 
 Jean:
 	* Fix formula interpretation in plot series (with Morten's help). [#662237]
diff --git a/src/cell.c b/src/cell.c
index 9a84b3e..27a5aab 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -677,6 +677,168 @@ gnm_cell_get_entered_text (GnmCell const *cell)
 	return g_strdup ("<ERROR>");
 }
 
+static gboolean
+close_to_int (gnm_float x, gnm_float eps)
+{
+	return gnm_abs (x - gnm_fake_round (x)) < eps;
+}
+
+static GOFormat *
+guess_time_format (const char *prefix, gnm_float f)
+{
+	int decs = 0;
+	gnm_float eps = 1e-6;
+	static int maxdecs = 6;
+	GString *str = g_string_new (prefix);
+	GOFormat *fmt;
+
+	if (f >= 0 && f < 1)
+		g_string_append (str, "hh:mm");
+	else
+		g_string_append (str, "[h]:mm");
+	f *= 24 * 60;
+	if (!close_to_int (f, eps / 60)) {
+		g_string_append (str, ":ss");
+		f *= 60;
+		if (!close_to_int (f, eps)) {
+			g_string_append_c (str, '.');
+			while (decs < maxdecs) {
+				decs++;
+				g_string_append_c (str, '0');
+				f *= 10;
+				if (close_to_int (f, eps))
+					break;
+			}
+		}
+	}
+
+	while (go_format_is_invalid ((fmt = go_format_new_from_XL (str->str))) && decs > 0) {
+		/* We don't know how many decimals GOFormat allows.  */
+		go_format_unref (fmt);
+		maxdecs = --decs;
+		g_string_truncate (str, str->len - 1);
+	}
+
+	g_string_free (str, TRUE);
+	return fmt;
+}
+
+/**
+ * gnm_cell_get_text_for_editing:
+ * @cell: the cell from which we want to pull the content from
+ *
+ * The returned value should be g_free'd
+ *
+ * Primary user of this function is the formula entry.
+ * This function should return the value most appropriate for
+ * editing
+ *
+ */
+
+char *
+gnm_cell_get_text_for_editing (GnmCell const * cell, Sheet *sheet, 
+			       gboolean *quoted, int *cursor_pos)
+{
+	GODateConventions const *date_conv;
+	gchar *text = NULL;
+	
+	g_return_val_if_fail (cell != NULL, NULL);
+	g_return_val_if_fail (sheet != NULL, NULL);
+
+	if (quoted)
+		*quoted = FALSE;
+	
+	date_conv = workbook_date_conv (sheet->workbook);
+
+	if (!gnm_cell_is_array (cell) &&
+	    !gnm_cell_has_expr (cell) && VALUE_IS_FLOAT (cell->value)) {
+		GOFormat const *fmt = gnm_cell_get_format (cell);
+		gnm_float f = value_get_as_float (cell->value);
+		
+		switch (go_format_get_family (fmt)) {
+		case GO_FORMAT_FRACTION:
+			text = gnm_cell_get_entered_text (cell);
+			g_strchug (text);
+			g_strchomp (text);
+			break;
+			
+		case GO_FORMAT_PERCENTAGE: {
+			GString *new_str = g_string_new (NULL);
+			gnm_render_general (NULL, new_str, go_format_measure_zero,
+					    go_font_metrics_unit, f * 100,
+					    -1, FALSE, 0, 0);
+			if (cursor_pos)
+				*cursor_pos = g_utf8_strlen (new_str->str, -1);
+			g_string_append_c (new_str, '%');
+			text = g_string_free (new_str, FALSE);
+			break;
+		}
+			
+		case GO_FORMAT_NUMBER:
+		case GO_FORMAT_SCIENTIFIC:
+		case GO_FORMAT_CURRENCY:
+		case GO_FORMAT_ACCOUNTING: {
+			GString *new_str = g_string_new (NULL);
+			gnm_render_general (NULL, new_str, go_format_measure_zero,
+					    go_font_metrics_unit, f,
+					    -1, FALSE, 0, 0);
+			text = g_string_free (new_str, FALSE);
+			break;
+		}
+			
+		case GO_FORMAT_DATE: {
+			GOFormat *new_fmt;
+			
+			new_fmt = gnm_format_for_date_editing (cell);
+			
+			if (!close_to_int (f, 1e-6 / (24 * 60 * 60))) {
+				GString *fstr = g_string_new (go_format_as_XL (new_fmt));
+				go_format_unref (new_fmt);
+				
+				g_string_append_c (fstr, ' ');
+				new_fmt = guess_time_format
+					(fstr->str,
+					 f - gnm_floor (f));
+				g_string_free (fstr, TRUE);
+			}
+			
+			text = format_value (new_fmt, cell->value,
+					     -1, date_conv);
+			if (!text || text[0] == 0) {
+				g_free (text);
+				text = format_value (go_format_general (),
+						     cell->value,
+						     -1,
+						     date_conv);
+			}
+			go_format_unref (new_fmt);
+			break;
+		}
+			
+		case GO_FORMAT_TIME: {
+			GOFormat *new_fmt = guess_time_format (NULL, f);
+			
+			text = format_value (new_fmt, cell->value, -1,
+					     date_conv);
+			go_format_unref (new_fmt);
+			break;
+		}
+			
+		default:
+			break;
+		}
+	}
+	
+	if (!text) {
+		text = gnm_cell_get_entered_text (cell);
+		if (quoted)
+			*quoted = (text[0] == '\'');
+	}
+	
+	return text;
+}
+
+
 
 /*
  * Return the height of the rendered layout after rotation.
diff --git a/src/cell.h b/src/cell.h
index a6f59e2..7fda4d4 100644
--- a/src/cell.h
+++ b/src/cell.h
@@ -98,6 +98,8 @@ int	gnm_cell_rendered_width		(GnmCell const * cell);	/* excludes offset */
 int	gnm_cell_rendered_offset	(GnmCell const * cell);
 GOColor gnm_cell_get_render_color	(GnmCell const * cell);
 char *	gnm_cell_get_entered_text	(GnmCell const * cell);
+char *	gnm_cell_get_text_for_editing	(GnmCell const * cell, Sheet *sheet, 
+					 gboolean *quoted, int *cursor_pos);
 char *  gnm_cell_get_rendered_text	(GnmCell *cell);
 
 G_END_DECLS
diff --git a/src/commands.c b/src/commands.c
index 9c47456..7df6ee7 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -751,6 +751,7 @@ cmd_set_text_full_check_text (GnmCellIter const *iter, char *text)
 {
 	char *old_text;
 	gboolean same;
+	gboolean quoted = FALSE;
 
 	if (gnm_cell_is_blank (iter->cell))
 		return ((text == NULL || text[0] == '\0') ? NULL : VALUE_TERMINATE);
@@ -758,12 +759,12 @@ cmd_set_text_full_check_text (GnmCellIter const *iter, char *text)
 	if (text == NULL || text[0] == '\0')
 		return VALUE_TERMINATE;
 
-	old_text = gnm_cell_get_entered_text (iter->cell);
-	same = strcmp (old_text, text) == 0;
+	old_text = gnm_cell_get_text_for_editing (iter->cell, iter->pp.sheet, NULL, &quoted);
+	same = g_strcmp0 (old_text, text) == 0;
 
-	if (!same && iter->cell->value && VALUE_IS_STRING (iter->cell->value)
+	if (!same && !quoted && iter->cell->value && VALUE_IS_STRING (iter->cell->value)
 	    && text[0] == '\'')
-		same = strcmp (old_text, text + 1) == 0;
+		same = g_strcmp0 (old_text, text + 1) == 0;
 
 	g_free (old_text);
 
diff --git a/src/wbc-gtk-edit.c b/src/wbc-gtk-edit.c
index a3dfccb..7aa9798 100644
--- a/src/wbc-gtk-edit.c
+++ b/src/wbc-gtk-edit.c
@@ -794,52 +794,6 @@ wbcg_edit_get_markup (WBCGtk *wbcg, gboolean full)
 	return full ? wbcg->edit_line.full_content : wbcg->edit_line.markup;
 }
 
-static gboolean
-close_to_int (gnm_float x, gnm_float eps)
-{
-	return gnm_abs (x - gnm_fake_round (x)) < eps;
-}
-
-
-static GOFormat *
-guess_time_format (const char *prefix, gnm_float f)
-{
-	int decs = 0;
-	gnm_float eps = 1e-6;
-	static int maxdecs = 6;
-	GString *str = g_string_new (prefix);
-	GOFormat *fmt;
-
-	if (f >= 0 && f < 1)
-		g_string_append (str, "hh:mm");
-	else
-		g_string_append (str, "[h]:mm");
-	f *= 24 * 60;
-	if (!close_to_int (f, eps / 60)) {
-		g_string_append (str, ":ss");
-		f *= 60;
-		if (!close_to_int (f, eps)) {
-			g_string_append_c (str, '.');
-			while (decs < maxdecs) {
-				decs++;
-				g_string_append_c (str, '0');
-				f *= 10;
-				if (close_to_int (f, eps))
-					break;
-			}
-		}
-	}
-
-	while (go_format_is_invalid ((fmt = go_format_new_from_XL (str->str))) && decs > 0) {
-		/* We don't know how many decimals GOFormat allows.  */
-		go_format_unref (fmt);
-		maxdecs = --decs;
-		g_string_truncate (str, str->len - 1);
-	}
-
-	g_string_free (str, TRUE);
-	return fmt;
-}
 
 static void
 cb_warn_toggled (GtkToggleButton *button, gboolean *b)
@@ -970,105 +924,11 @@ wbcg_edit_start (WBCGtk *wbcg,
 	if (blankp)
 		gtk_entry_set_text (wbcg_get_entry (wbcg), "");
 	else if (cell != NULL) {
-		gboolean set_text = FALSE;
 		gboolean quoted = FALSE;
-		GODateConventions const *date_conv =
-			workbook_date_conv (sv->sheet->workbook);
-
-		if (gnm_cell_is_array (cell)) {
-			/* If this is part of an array we need to remove the
-			 * '{' '}' and the size information from the display.
-			 * That is not actually part of the parsable expression.
-			 */
-			set_text = TRUE;
-		} else if (!gnm_cell_has_expr (cell) && VALUE_IS_FLOAT (cell->value)) {
-			GOFormat const *fmt = gnm_cell_get_format (cell);
-			gnm_float f = value_get_as_float (cell->value);
-
-			switch (go_format_get_family (fmt)) {
-			case GO_FORMAT_FRACTION:
-				text = gnm_cell_get_entered_text (cell);
-				g_strchug (text);
-				g_strchomp (text);
-				set_text = TRUE;
-				break;
-
-			case GO_FORMAT_PERCENTAGE: {
-				GString *new_str = g_string_new (NULL);
-				gnm_render_general (NULL, new_str, go_format_measure_zero,
-						    go_font_metrics_unit, f * 100,
-						    -1, FALSE, 0, 0);
-				cursor_pos = g_utf8_strlen (new_str->str, -1);
-				g_string_append_c (new_str, '%');
-				text = g_string_free (new_str, FALSE);
-				set_text = TRUE;
-				break;
-			}
-
-			case GO_FORMAT_NUMBER:
-			case GO_FORMAT_SCIENTIFIC:
-			case GO_FORMAT_CURRENCY:
-			case GO_FORMAT_ACCOUNTING: {
-				GString *new_str = g_string_new (NULL);
-				gnm_render_general (NULL, new_str, go_format_measure_zero,
-						    go_font_metrics_unit, f,
-						    -1, FALSE, 0, 0);
-				text = g_string_free (new_str, FALSE);
-				set_text = TRUE;
-				break;
-			}
-
-			case GO_FORMAT_DATE: {
-				GOFormat *new_fmt;
-
-				new_fmt = gnm_format_for_date_editing (cell);
-
-				if (!close_to_int (f, 1e-6 / (24 * 60 * 60))) {
-					GString *fstr = g_string_new (go_format_as_XL (new_fmt));
-					go_format_unref (new_fmt);
-
-					g_string_append_c (fstr, ' ');
-					new_fmt = guess_time_format
-						(fstr->str,
-						 f - gnm_floor (f));
-					g_string_free (fstr, TRUE);
-				}
-
-				text = format_value (new_fmt, cell->value,
-						     -1, date_conv);
-				if (!text || text[0] == 0) {
-					g_free (text);
-					text = format_value (go_format_general (),
-							     cell->value,
-							     -1,
-							     date_conv);
-				}
-				set_text = TRUE;
-				go_format_unref (new_fmt);
-				break;
-			}
-
-			case GO_FORMAT_TIME: {
-				GOFormat *new_fmt = guess_time_format (NULL, f);
-
-				text = format_value (new_fmt, cell->value, -1,
-						     workbook_date_conv (sv->sheet->workbook));
-				set_text = TRUE;
-				go_format_unref (new_fmt);
-				break;
-			}
 
-			default:
-				break;
-			}
-		}
-
-		if (!text) {
-			text = gnm_cell_get_entered_text (cell);
-			quoted = (text[0] == '\'');
-		}
+		text = gnm_cell_get_text_for_editing (cell, sv->sheet, &quoted, &cursor_pos);
 
-		if (set_text)
+		if (text)
 			gtk_entry_set_text (wbcg_get_entry (wbcg), text);
 
 		if (cell->value != NULL) {



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