[gnumeric] GUI: Handle red negatives for auto-expr.



commit f329ad4232e4695fa465adb1132fd41b9899f001
Author: Morten Welinder <terra gnome org>
Date:   Tue Apr 27 11:34:25 2010 -0400

    GUI: Handle red negatives for auto-expr.

 ChangeLog           |   13 +++++++++++
 NEWS                |    1 +
 src/commands.c      |   48 ----------------------------------------
 src/gutils.c        |   50 ++++++++++++++++++++++++++++++++++++++++++
 src/gutils.h        |    2 +
 src/wbc-gtk-impl.h  |    2 +-
 src/wbc-gtk.c       |   13 ++++++++++-
 src/workbook-view.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++---
 src/workbook-view.h |    1 +
 9 files changed, 136 insertions(+), 54 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 5679d70..8e6ec7f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2010-04-27  Morten Welinder  <terra gnome org>
+
+	* src/workbook-view.c (wb_view_set_property,
+	wb_view_get_property): Keep auto expression attributes too.
+	(wb_view_auto_expr_recalc): Set colour when needed.  Fixes
+	#575104.
+
+	* src/wbc-gtk.c (wbcg_auto_expr_text_changed): Set attributes too.
+	(wbcg_view_changed): Watch attribute changes too.
+
+	* src/gutils.c (gnm_pango_attr_list_equal): Import from commands.c
+	and make this public.
+
 2010-04-23  Jean Brefort  <jean brefort normalesup org>
 
 	* src/graph.c (gnm_go_data_unserialize),
diff --git a/NEWS b/NEWS
index 32c7287..4931292 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,7 @@ Morten:
 	* Fix problem loading cells with empty strings.  [#616139]
 	* Fix WEEKNUM problems.  [#616330]
 	* Fix hidden-sheet problem.  [#616549]
+	* Fix auto-expr problem.  [#575104]
 
 --------------------------------------------------------------------------
 Gnumeric 1.10.2
diff --git a/src/commands.c b/src/commands.c
index 7324bf1..5def518 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -855,54 +855,6 @@ cmd_set_text_finalize (GObject *cmd)
 	gnm_command_finalize (cmd);
 }
 
-
-static gboolean
-cb_gnm_pango_attr_list_equal (PangoAttribute *a, gpointer _sl)
-{
-	GSList **sl = _sl;
-	*sl = g_slist_prepend (*sl, a);
-	return FALSE;
-}
-
-/*
- * This is a bit of a hack.  It might claim a difference even when things
- * actually are equal.  But not the other way around.
- */
-static gboolean
-gnm_pango_attr_list_equal (PangoAttrList const *l1, PangoAttrList const *l2)
-{
-	if (l1 == l2)
-		return TRUE;
-	else if (l1 == NULL || l2 == NULL)
-		return FALSE;
-	else {
-		gboolean res;
-		GSList *sl1 = NULL, *sl2 = NULL;
-		(void)pango_attr_list_filter ((PangoAttrList *)l1,
-					      cb_gnm_pango_attr_list_equal,
-					      &sl1);
-		(void)pango_attr_list_filter ((PangoAttrList *)l2,
-					      cb_gnm_pango_attr_list_equal,
-					      &sl2);
-
-		while (sl1 && sl2) {
-			const PangoAttribute *a1 = sl1->data;
-			const PangoAttribute *a2 = sl2->data;
-			if (a1->start_index != a2->start_index ||
-			    a1->end_index != a2->end_index ||
-			    !pango_attribute_equal (a1, a2))
-				break;
-			sl1 = g_slist_delete_link (sl1, sl1);
-			sl2 = g_slist_delete_link (sl2, sl2);
-		}
-
-		res = (sl1 == sl2);
-		g_slist_free (sl1);
-		g_slist_free (sl2);
-		return res;
-	}
-}
-
 gboolean
 cmd_set_text (WorkbookControl *wbc,
 	      Sheet *sheet, GnmCellPos const *pos,
diff --git a/src/gutils.c b/src/gutils.c
index 0197fd8..6be2311 100644
--- a/src/gutils.c
+++ b/src/gutils.c
@@ -308,6 +308,54 @@ gnm_pango_attr_dump (PangoAttrList *list)
 }
 #endif
 
+
+static gboolean
+cb_gnm_pango_attr_list_equal (PangoAttribute *a, gpointer _sl)
+{
+	GSList **sl = _sl;
+	*sl = g_slist_prepend (*sl, a);
+	return FALSE;
+}
+
+/*
+ * This is a bit of a hack.  It might claim a difference even when things
+ * actually are equal.  But not the other way around.
+ */
+gboolean
+gnm_pango_attr_list_equal (PangoAttrList const *l1, PangoAttrList const *l2)
+{
+	if (l1 == l2)
+		return TRUE;
+	else if (l1 == NULL || l2 == NULL)
+		return FALSE;
+	else {
+		gboolean res;
+		GSList *sl1 = NULL, *sl2 = NULL;
+		(void)pango_attr_list_filter ((PangoAttrList *)l1,
+					      cb_gnm_pango_attr_list_equal,
+					      &sl1);
+		(void)pango_attr_list_filter ((PangoAttrList *)l2,
+					      cb_gnm_pango_attr_list_equal,
+					      &sl2);
+
+		while (sl1 && sl2) {
+			const PangoAttribute *a1 = sl1->data;
+			const PangoAttribute *a2 = sl2->data;
+			if (a1->start_index != a2->start_index ||
+			    a1->end_index != a2->end_index ||
+			    !pango_attribute_equal (a1, a2))
+				break;
+			sl1 = g_slist_delete_link (sl1, sl1);
+			sl2 = g_slist_delete_link (sl2, sl2);
+		}
+
+		res = (sl1 == sl2);
+		g_slist_free (sl1);
+		g_slist_free (sl2);
+		return res;
+	}
+}
+
 /* ------------------------------------------------------------------------- */
 
 struct _GnmLocale {
@@ -405,3 +453,5 @@ gnm_insert_meta_date (GODoc *doc, char const *name)
 				  g_strdup (name), 
 				  value);
 }
+
+/* ------------------------------------------------------------------------- */
diff --git a/src/gutils.h b/src/gutils.h
index 95ef71d..194eebf 100644
--- a/src/gutils.h
+++ b/src/gutils.h
@@ -23,6 +23,8 @@ char const *gnm_usr_dir	       (void);
 int gnm_regcomp_XL (GORegexp *preg, char const *pattern,
 		    int cflags, gboolean full);
 
+gboolean gnm_pango_attr_list_equal (PangoAttrList const *l1, PangoAttrList const *l2);
+
 /* Locale utilities */
 typedef struct _GnmLocale GnmLocale;
 GnmLocale *gnm_push_C_locale (void);
diff --git a/src/wbc-gtk-impl.h b/src/wbc-gtk-impl.h
index c745a60..989655e 100644
--- a/src/wbc-gtk-impl.h
+++ b/src/wbc-gtk-impl.h
@@ -98,7 +98,7 @@ struct _WBCGtk {
 
 	SheetControlGUI *active_scg;
 	gulong sig_view_changed;
-	gulong sig_auto_expr_text;
+	gulong sig_auto_expr_text, sig_auto_expr_attrs;
 	gulong sig_show_horizontal_scrollbar, sig_show_vertical_scrollbar;
 	gulong sig_show_notebook_tabs;
 	gulong sig_sheet_order, sig_notify_uri, sig_notify_dirty;
diff --git a/src/wbc-gtk.c b/src/wbc-gtk.c
index 57f5b28..f2995e9 100644
--- a/src/wbc-gtk.c
+++ b/src/wbc-gtk.c
@@ -1478,8 +1478,11 @@ wbcg_auto_expr_text_changed (WorkbookView *wbv,
 			     G_GNUC_UNUSED GParamSpec *pspec,
 			     WBCGtk *wbcg)
 {
-	gtk_label_set_text (GTK_LABEL (wbcg->auto_expr_label),
+	GtkLabel *lbl = GTK_LABEL (wbcg->auto_expr_label);
+
+	gtk_label_set_text (lbl,
 			    wbv->auto_expr_text ? wbv->auto_expr_text : "");
+	gtk_label_set_attributes (lbl, wbv->auto_expr_attrs);
 }
 
 static void
@@ -2480,6 +2483,7 @@ wbcg_view_changed (WBCGtk *wbcg,
 		 0);
 
 	DISCONNECT (wbcg->sig_wbv, sig_auto_expr_text);
+	DISCONNECT (wbcg->sig_wbv, sig_auto_expr_attrs);
 	DISCONNECT (wbcg->sig_wbv, sig_show_horizontal_scrollbar);
 	DISCONNECT (wbcg->sig_wbv, sig_show_vertical_scrollbar);
 	DISCONNECT (wbcg->sig_wbv, sig_show_notebook_tabs);
@@ -2497,6 +2501,13 @@ wbcg_view_changed (WBCGtk *wbcg,
 			 G_CALLBACK (wbcg_auto_expr_text_changed),
 			 wbcg,
 			 0);
+		wbcg->sig_auto_expr_attrs =
+			g_signal_connect_object
+			(G_OBJECT (wbv),
+			 "notify::auto-expr-attrs",
+			 G_CALLBACK (wbcg_auto_expr_text_changed),
+			 wbcg,
+			 0);
 		wbcg_auto_expr_text_changed (wbv, NULL, wbcg);
 
 		wbcg->sig_show_horizontal_scrollbar =
diff --git a/src/workbook-view.c b/src/workbook-view.c
index 8b62300..fbd78b0 100644
--- a/src/workbook-view.c
+++ b/src/workbook-view.c
@@ -75,6 +75,7 @@ enum {
 	PROP_AUTO_EXPR_DESCR,
 	PROP_AUTO_EXPR_MAX_PRECISION,
 	PROP_AUTO_EXPR_TEXT,
+	PROP_AUTO_EXPR_ATTRS,
 	PROP_SHOW_HORIZONTAL_SCROLLBAR,
 	PROP_SHOW_VERTICAL_SCROLLBAR,
 	PROP_SHOW_NOTEBOOK_TABS,
@@ -514,6 +515,7 @@ wb_view_auto_expr_recalc (WorkbookView *wbv)
 		GString *str = g_string_new (wbv->auto_expr_descr);
 		GOFormat const *format = NULL;
 		GOFormat *tmp_format = NULL;
+		PangoAttrList *attrs;
 
 		g_string_append_c (str, '=');
 		if (!wbv->auto_expr_use_max_precision) {
@@ -524,19 +526,36 @@ wb_view_auto_expr_recalc (WorkbookView *wbv)
 		}
 
 		if (format) {
-			format_value_gstring (str, format, v, NULL,
+			GOColor color;
+			PangoAttribute *attr;
+			gsize old_len = str->len;
+
+			format_value_gstring (str, format, v, &color,
 					      -1, workbook_date_conv (wb_view_get_workbook (wbv)));
 			go_format_unref (tmp_format);
+
+			attrs = pango_attr_list_new ();
+			attr = go_color_to_pango (color, TRUE);
+			attr->start_index = old_len;
+			attr->end_index = str->len;
+			pango_attr_list_insert (attrs, attr);
 		} else {
 			g_string_append (str, value_peek_string (v));
+			attrs = NULL;
 		}
 
-		g_object_set (wbv, "auto-expr-text", str->str, NULL);
-
+		g_object_set (wbv,
+			      "auto-expr-text", str->str,
+			      "auto-expr-attrs", attrs,
+			      NULL);
 		g_string_free (str, TRUE);
+		pango_attr_list_unref (attrs);
 		value_release (v);
 	} else {
-		g_object_set (wbv, "auto-expr-text", "Internal ERROR", NULL);
+		g_object_set (wbv,
+			      "auto-expr-text", "Internal ERROR",
+			      "auto-expr-attrs", NULL,
+			      NULL);
 	}
 
 	gnm_expr_top_unref (texpr);
@@ -639,6 +658,19 @@ wb_view_auto_expr_text (WorkbookView *wbv, const char *text)
 }
 
 static void
+wb_view_auto_expr_attrs (WorkbookView *wbv, PangoAttrList *attrs)
+{
+	if (gnm_pango_attr_list_equal (attrs, wbv->auto_expr_attrs))
+		return;
+
+	if (attrs)
+		pango_attr_list_ref (attrs);
+	if (wbv->auto_expr_attrs)
+		pango_attr_list_ref (wbv->auto_expr_attrs);
+	wbv->auto_expr_attrs = attrs;
+}
+
+static void
 wb_view_set_property (GObject *object, guint property_id,
 		      const GValue *value, GParamSpec *pspec)
 {
@@ -657,6 +689,9 @@ wb_view_set_property (GObject *object, guint property_id,
 	case PROP_AUTO_EXPR_TEXT:
 		wb_view_auto_expr_text (wbv, g_value_get_string (value));
 		break;
+	case PROP_AUTO_EXPR_ATTRS:
+		wb_view_auto_expr_attrs (wbv, g_value_peek_pointer (value));
+		break;
 	case PROP_SHOW_HORIZONTAL_SCROLLBAR:
 		wbv->show_horizontal_scrollbar = !!g_value_get_boolean (value);
 		break;
@@ -703,6 +738,9 @@ wb_view_get_property (GObject *object, guint property_id,
 	case PROP_AUTO_EXPR_TEXT:
 		g_value_set_string (value, wbv->auto_expr_text);
 		break;
+	case PROP_AUTO_EXPR_ATTRS:
+		g_value_set_boxed (value, wbv->auto_expr_attrs);
+		break;
 	case PROP_SHOW_HORIZONTAL_SCROLLBAR:
 		g_value_set_boolean (value, wbv->show_horizontal_scrollbar);
 		break;
@@ -779,6 +817,11 @@ wb_view_finalize (GObject *object)
 	g_free (wbv->auto_expr_text);
 	wbv->auto_expr_text = NULL;
 
+	if (wbv->auto_expr_attrs) {
+		pango_attr_list_unref (wbv->auto_expr_attrs);
+		wbv->auto_expr_attrs = NULL;
+	}
+
 	if (wbv->current_style != NULL) {
 		gnm_style_unref (wbv->current_style);
 		wbv->current_style = NULL;
@@ -840,6 +883,14 @@ workbook_view_class_init (GObjectClass *gobject_class)
 				      G_PARAM_READWRITE));
         g_object_class_install_property
 		(gobject_class,
+		 PROP_AUTO_EXPR_ATTRS,
+		 g_param_spec_boxed ("auto-expr-attrs",
+				     _("Auto-expression Attributes"),
+				     _("Text attributes for the automatically computed sheet function."),
+				     PANGO_TYPE_ATTR_LIST,
+				     GSF_PARAM_STATIC | G_PARAM_READWRITE));
+        g_object_class_install_property
+		(gobject_class,
 		 PROP_SHOW_HORIZONTAL_SCROLLBAR,
 		 g_param_spec_boolean ("show-horizontal-scrollbar",
 				       _("Show horizontal scrollbar"),
@@ -940,6 +991,7 @@ workbook_view_new (Workbook *wb)
 		gnm_func_ref (wbv->auto_expr_func);
 	wbv->auto_expr_descr = g_strdup (_("Sum"));
 	wbv->auto_expr_text = NULL;
+	wbv->auto_expr_attrs = NULL;
 	wbv->auto_expr_use_max_precision = FALSE;
 
 	for (i = 0 ; i < workbook_sheet_count (wb); i++)
diff --git a/src/workbook-view.h b/src/workbook-view.h
index 8f184a7..f85aac3 100644
--- a/src/workbook-view.h
+++ b/src/workbook-view.h
@@ -31,6 +31,7 @@ struct _WorkbookView {
 	GnmFunc   *auto_expr_func;
 	char	  *auto_expr_descr;
 	char	  *auto_expr_text;
+	PangoAttrList *auto_expr_attrs;
 	gboolean  auto_expr_use_max_precision;
 
 	/* selection */



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