[gnumeric] GUI: Move auto expression rendering to control.



commit 62e6ee462f18ab1353d5efdf67cef2f0b9f92b01
Author: Morten Welinder <terra gnome org>
Date:   Wed Jan 4 10:44:20 2012 -0500

    GUI: Move auto expression rendering to control.
    
    It's the right place for several reasons:
    
    1. The control knows how wide.  Controls might not agree on that.
    2. The control knows about colours.
    3. The control knows the right pango context to use.

 ChangeLog           |    6 ++
 NEWS                |    1 +
 src/wbc-gtk.c       |  133 +++++++++++++++++++++++++++++--------
 src/workbook-view.c |  186 +++++++-------------------------------------------
 src/workbook-view.h |    3 +-
 5 files changed, 140 insertions(+), 189 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6105d25..2e9e131 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2012-01-04  Morten Welinder  <terra gnome org>
+
+	* src/workbook-view.c: Change from "text" to "value" attribute and
+	move formatting from here...
+	* src/wbc-gtk.c: ...to here.
+
 2012-01-03  Morten Welinder  <terra gnome org>
 
 	* src/wbc-gtk.c (wbc_gtk_create_status_area): Add magic to ensure
diff --git a/NEWS b/NEWS
index 08a2f18..5f378b6 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,7 @@ Morten:
 	* Fix ellipsizing of auto-expression.
 	* Fix auto-expression of single cell when moved.
 	* Fix problems with underlines and rotated cells.  [#667152]
+	* Fix multihead problem with auto-expressions.
 
 --------------------------------------------------------------------------
 Gnumeric 1.11.1
diff --git a/src/wbc-gtk.c b/src/wbc-gtk.c
index bc45e67..ec1ed7d 100644
--- a/src/wbc-gtk.c
+++ b/src/wbc-gtk.c
@@ -4,7 +4,7 @@
  * wbc-gtk.c: A gtk based WorkbookControl
  *
  * Copyright (C) 2000-2007 Jody Goldberg (jody gnome org)
- * Copyright (C) 2006-2009 Morten Welinder (terra gnome org)
+ * Copyright (C) 2006-2012 Morten Welinder (terra gnome org)
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public
@@ -40,6 +40,8 @@
 #include "history.h"
 #include "func.h"
 #include "value.h"
+#include "style-font.h"
+#include "gnm-format.h"
 #include "expr.h"
 #include "expr-impl.h"
 #include "style-color.h"
@@ -1322,16 +1324,98 @@ wbcg_sheet_remove_all (WorkbookControl *wbc)
 	}
 }
 
+static gboolean
+cb_darken_foreground_attributes (PangoAttribute *attribute,
+				 G_GNUC_UNUSED gpointer data)
+{
+	if (attribute->klass->type == PANGO_ATTR_FOREGROUND) {
+		PangoAttrColor *cat = (PangoAttrColor *) attribute;
+		guint total = (guint)cat->color.red + (guint)cat->color.green + (guint)cat->color.blue;
+		if (total > 98302) {
+			float adj = 98302.5/total;
+			cat->color.red = cat->color.red * adj;
+			cat->color.green = cat->color.green * adj;
+			cat->color.blue = cat->color.blue * adj;
+		}
+	}
+	return FALSE;
+}
+
 static void
-wbcg_auto_expr_text_changed (WorkbookView *wbv,
-			     G_GNUC_UNUSED GParamSpec *pspec,
-			     WBCGtk *wbcg)
+darken_foreground_attributes (PangoAttrList *attrs)
+{
+	pango_attr_list_unref 
+		(pango_attr_list_filter 
+		 (attrs,
+		  cb_darken_foreground_attributes,
+		  NULL));
+}
+
+
+static void
+wbcg_auto_expr_value_changed (WorkbookView *wbv,
+			      G_GNUC_UNUSED GParamSpec *pspec,
+			      WBCGtk *wbcg)
 {
 	GtkLabel *lbl = GTK_LABEL (wbcg->auto_expr_label);
+	GnmValue const *v = wbv->auto_expr.value;
+
+	if (v) {
+		GOFormat const *format = VALUE_FMT (v);
+		GString *str = g_string_new (wbv->auto_expr.descr);
+		PangoAttrList *attrs = NULL;
+
+		g_string_append (str, " = ");
+
+		if (format) {
+			PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (wbcg->toplevel), NULL);
+			gsize old_len = str->len;
+			GODateConventions const *date_conv = workbook_date_conv (wb_view_get_workbook (wbv));
+			GOFormatNumberError err =
+				format_value_layout (layout, format, v,
+						     /* Note that we created a label large enough for */
+						     /* "Sumerage = -012345678901234" */
+						     27 - g_utf8_strlen (str->str, -1),
+						     date_conv);
+			switch (err) {
+			case GO_FORMAT_NUMBER_OK:
+			case GO_FORMAT_NUMBER_DATE_ERROR: {
+				PangoAttrList *atl;
+
+				go_pango_translate_layout (layout); /* translating custom attributes */
+				g_string_append (str, pango_layout_get_text (layout));
+				/* We need to shift the attribute list  */
+				atl = pango_attr_list_ref (pango_layout_get_attributes (layout));
+				if (atl != NULL) {
+					attrs = pango_attr_list_new ();
+					pango_attr_list_splice
+						(attrs, atl, old_len,
+						 str->len - old_len);
+					pango_attr_list_unref (atl);
+					/* The field background is white so we need to ensure that no */
+					/* foreground colour is set to white (or close to white)      */
+					darken_foreground_attributes (attrs);
+				}
+				break;
+			}
+			default:
+			case GO_FORMAT_NUMBER_INVALID_FORMAT:
+				g_string_append (str,  _("Invalid format"));
+				break;
+			}
+			g_object_unref (layout);
+		} else
+			g_string_append (str, value_peek_string (v));
+
+		gtk_label_set_text (lbl, str->str);
+		gtk_label_set_attributes (lbl, attrs);
 
-	gtk_label_set_text (lbl,
-			    wbv->auto_expr.text ? wbv->auto_expr.text : "");
-	gtk_label_set_attributes (lbl, wbv->auto_expr.attrs);
+		pango_attr_list_unref (attrs);
+		g_string_free (str, TRUE);
+	} else {
+		gtk_label_set_text (lbl, "");
+		gtk_label_set_attributes (lbl, NULL);
+	}
 }
 
 static void
@@ -2792,18 +2876,11 @@ wbcg_view_changed (WBCGtk *wbcg,
 		wbcg->sig_auto_expr_text =
 			g_signal_connect_object
 			(G_OBJECT (wbv),
-			 "notify::auto-expr-text",
-			 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),
+			 "notify::auto-expr-value",
+			 G_CALLBACK (wbcg_auto_expr_value_changed),
 			 wbcg,
 			 0);
-		wbcg_auto_expr_text_changed (wbv, NULL, wbcg);
+		wbcg_auto_expr_value_changed (wbv, NULL, wbcg);
 
 		wbcg->sig_show_horizontal_scrollbar =
 			g_signal_connect_object
@@ -4479,22 +4556,29 @@ wbc_gtk_setup_icons (void)
 static void
 cb_auto_expr_cell_changed (GtkWidget *item, WBCGtk *wbcg)
 {
-	const char *descr;
 	WorkbookView *wbv = wb_control_view (WORKBOOK_CONTROL (wbcg));
 	const GnmEvalPos *ep;
+	GnmExprTop const *texpr;
+	GnmValue const *v;
 
 	if (wbcg->updating_ui)
 		return;
 
-	descr = g_object_get_data (G_OBJECT (item), "descr");
 	ep = g_object_get_data (G_OBJECT (item), "evalpos");
 
 	g_object_set (wbv,
 		      "auto-expr-func", NULL,
-		      "auto-expr-descr", descr,
+		      "auto-expr-descr", NULL,
 		      "auto-expr-eval-pos", ep,
 		      NULL);
 
+	/* Now we have the expression set.  */
+	texpr = wbv->auto_expr.dep.texpr;
+	v = gnm_expr_top_get_constant (texpr);
+	if (v)
+		g_object_set (wbv,
+			      "auto-expr-descr", value_peek_string (v),
+			      NULL);
 }
 
 static void
@@ -4651,7 +4735,6 @@ cb_select_auto_expr (GtkWidget *widget, GdkEventButton *event, WBCGtk *wbcg)
 	Sheet *sheet = wb_view_cur_sheet (wbv);
 	GtkWidget *item, *menu;
 	int i;
-	char *rname;
 	char *cell_item;
 	GnmCellPos const *pos;
 	GnmEvalPos ep;
@@ -4701,14 +4784,10 @@ cb_select_auto_expr (GtkWidget *widget, GdkEventButton *event, WBCGtk *wbcg)
 
 	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);
+	cell_item = g_strdup_printf (_("Content of %s"), cellpos_as_string (pos));
+	item = gtk_menu_item_new_with_label (cell_item);
 	g_free (cell_item);
 	g_object_set_data_full (G_OBJECT (item),
-				"descr", rname,
-				(GDestroyNotify)g_free);
-	g_object_set_data_full (G_OBJECT (item),
 				"evalpos", g_memdup (&ep, sizeof (ep)),
 				(GDestroyNotify)g_free);
 	g_signal_connect (G_OBJECT (item), "activate",
diff --git a/src/workbook-view.c b/src/workbook-view.c
index 33ce6b6..b1b50b6 100644
--- a/src/workbook-view.c
+++ b/src/workbook-view.c
@@ -3,6 +3,7 @@
  * workbook-view.c: View functions for the workbook
  *
  * Copyright (C) 2000-2006 Jody Goldberg (jody gnome org)
+ * Copyright (C) 2012 Morten Welinder (terra gnome org)
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -30,8 +31,6 @@
 #include "sheet-view.h"
 #include "sheet-merge.h"
 #include "sheet-style.h"
-#include "style-font.h"
-#include "gnm-format.h"
 #include "func.h"
 #include "expr.h"
 #include "expr-name.h"
@@ -75,8 +74,7 @@ enum {
 	PROP_AUTO_EXPR_FUNC,
 	PROP_AUTO_EXPR_DESCR,
 	PROP_AUTO_EXPR_MAX_PRECISION,
-	PROP_AUTO_EXPR_TEXT,
-	PROP_AUTO_EXPR_ATTRS,
+	PROP_AUTO_EXPR_VALUE,
 	PROP_AUTO_EXPR_EVAL_POS,
 	PROP_SHOW_HORIZONTAL_SCROLLBAR,
 	PROP_SHOW_VERTICAL_SCROLLBAR,
@@ -494,42 +492,14 @@ accumulate_regions (SheetView *sv,  GnmRange const *r, gpointer closure)
 		gnm_expr_new_constant (value_new_cellrange_unsafe (&a, &b)));
 }
 
-static gboolean
-wb_view_darken_foreground_attributes_cb (PangoAttribute *attribute,
-					 G_GNUC_UNUSED gpointer data)
-{
-	if (attribute->klass->type == PANGO_ATTR_FOREGROUND) {
-		PangoAttrColor *cat = (PangoAttrColor *) attribute;
-		guint total = (guint)cat->color.red + (guint)cat->color.green + (guint)cat->color.blue;
-		if (total > 98302) {
-			float adj = 98302.5/total;
-			cat->color.red = cat->color.red * adj;
-			cat->color.green = cat->color.green * adj;
-			cat->color.blue = cat->color.blue * adj;
-		}
-	}
-	return FALSE;
-}
-
-static void
-wb_view_darken_foreground_attributes (PangoAttrList *attrs)
-{
-	pango_attr_list_unref 
-		(pango_attr_list_filter 
-		 (attrs,
-		  wb_view_darken_foreground_attributes_cb,
-		  NULL));
-}
-
 void
 wb_view_auto_expr_recalc (WorkbookView *wbv)
 {
-	GnmEvalPos      ep;
 	GnmExprList	*selection = NULL;
 	GnmValue	*v;
 	SheetView	*sv;
 	GnmExprTop const *texpr;
-	GString *str;
+	GnmEvalPos      ep;
 
 	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));
 
@@ -541,96 +511,27 @@ wb_view_auto_expr_recalc (WorkbookView *wbv)
 	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);
 		texpr = gnm_expr_top_new
 			(gnm_expr_new_funcall (wbv->auto_expr.func, selection));
-		str = g_string_new (wbv->auto_expr.descr);
 	} else {
-		str = g_string_new (NULL);
 		texpr = gnm_expr_top_new_constant (value_new_string (""));
 	}
 
 	eval_pos_init_sheet (&ep, wbv->current_sheet);
-	v = gnm_expr_top_eval (texpr, &ep, GNM_EXPR_EVAL_SCALAR_NON_EMPTY);
-
-	if (v) {
-		GOFormat const *format = NULL;
-		GOFormat const *tmp_format = NULL;
-		PangoAttrList *attrs = NULL;
-
-		g_string_append (str, " = ");
-		if (!wbv->auto_expr.use_max_precision) {
-			format = VALUE_FMT (v);
-			if (!format)
-				format = tmp_format =
-					auto_style_format_suggest (texpr, &ep);
-		}
 
-		if (format) {
-			PangoContext *context = gnm_pango_context_get ();
-			PangoLayout *layout = pango_layout_new (context);
-			gsize old_len = str->len;
-			GOFormatNumberError err =
-				format_value_layout (layout, format, v,
-						     /* Note that we created a label large enough for */
-						     /* "Sumerage = -012345678901234" */
-						     27 - g_utf8_strlen (str->str, -1),
-						     workbook_date_conv (wb_view_get_workbook (wbv)));
-			go_format_unref (tmp_format);
-			switch (err) {
-			case GO_FORMAT_NUMBER_OK:
-			case GO_FORMAT_NUMBER_DATE_ERROR: {
-				PangoAttrList *atl;
-
-				go_pango_translate_layout (layout); /* translating custom attributes */
-				g_string_append (str, pango_layout_get_text (layout));
-				/* We need to shift the attribute list  */
-				atl = pango_attr_list_ref (pango_layout_get_attributes (layout));
-				if (atl != NULL) {
-					attrs = pango_attr_list_new ();
-					pango_attr_list_splice
-						(attrs, atl, old_len,
-						 str->len - old_len);
-					pango_attr_list_unref (atl);
-					/* The field background is white so we need to ensure that no */
-					/* foreground colour is set to white (or close to white)      */
-					wb_view_darken_foreground_attributes (attrs);
-				}
-				break;
-			}
-			default:
-			case GO_FORMAT_NUMBER_INVALID_FORMAT:
-				g_string_append (str,  _("Invalid format"));
-				break;
-			}
-			g_object_unref (layout);
-			g_object_unref (context);
-		} else
-			g_string_append (str, value_peek_string (v));
-
-		g_object_set (wbv,
-			      "auto-expr-text", str->str,
-			      "auto-expr-attrs", attrs,
-			      NULL);
-		pango_attr_list_unref (attrs);
-		value_release (v);
-	} else {
-		g_object_set (wbv,
-			      "auto-expr-text", "Internal ERROR",
-			      "auto-expr-attrs", NULL,
-			      NULL);
+	v = gnm_expr_top_eval (texpr, &ep, GNM_EXPR_EVAL_SCALAR_NON_EMPTY);
+	if (v && !VALUE_FMT (v)) {
+		GOFormat const *fmt = auto_style_format_suggest (texpr, &ep);
+		value_set_fmt (v, fmt);
+		go_format_unref (fmt);
 	}
-	g_string_free (str, TRUE);
+	g_object_set (wbv, "auto-expr-value", v, NULL);
+	value_release (v);
 	gnm_expr_top_unref (texpr);
 }
 
@@ -718,29 +619,10 @@ wb_view_auto_expr_precision (WorkbookView *wbv, gboolean use_max_precision)
 }
 
 static void
-wb_view_auto_expr_text (WorkbookView *wbv, const char *text)
+wb_view_auto_expr_value (WorkbookView *wbv, const GnmValue *value)
 {
-	char *s;
-
-	if (go_str_compare (text, wbv->auto_expr.text) == 0)
-		return;
-
-	s = g_strdup (text);
-	g_free (wbv->auto_expr.text);
-	wbv->auto_expr.text = s;
-}
-
-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_unref (wbv->auto_expr.attrs);
-	wbv->auto_expr.attrs = attrs;
+	value_release (wbv->auto_expr.value);
+	wbv->auto_expr.value = value_dup (value);
 }
 
 static void
@@ -801,11 +683,8 @@ wb_view_set_property (GObject *object, guint property_id,
 	case PROP_AUTO_EXPR_MAX_PRECISION:
 		wb_view_auto_expr_precision (wbv, g_value_get_boolean (value));
 		break;
-	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));
+	case PROP_AUTO_EXPR_VALUE:
+		wb_view_auto_expr_value (wbv, g_value_get_boxed (value));
 		break;
 	case PROP_AUTO_EXPR_EVAL_POS:
 		wb_view_auto_expr_eval_pos (wbv, g_value_get_pointer (value));
@@ -863,11 +742,8 @@ wb_view_get_property (GObject *object, guint property_id,
 	case PROP_AUTO_EXPR_MAX_PRECISION:
 		g_value_set_boolean (value, wbv->auto_expr.use_max_precision);
 		break;
-	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);
+	case PROP_AUTO_EXPR_VALUE:
+		g_value_set_boxed (value, wbv->auto_expr.value);
 		break;
 	case PROP_SHOW_HORIZONTAL_SCROLLBAR:
 		g_value_set_boolean (value, wbv->show_horizontal_scrollbar);
@@ -931,8 +807,7 @@ wb_view_dispose (GObject *object)
 
 	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);
+	wb_view_auto_expr_value (wbv, NULL);
 	wb_view_auto_expr_descr (wbv, NULL);
 
 	wb_view_detach_from_workbook (wbv);
@@ -989,21 +864,13 @@ workbook_view_class_init (GObjectClass *gobject_class)
 				       G_PARAM_READWRITE));
         g_object_class_install_property
 		(gobject_class,
-		 PROP_AUTO_EXPR_TEXT,
-		 g_param_spec_string ("auto-expr-text",
-				      _("Auto-expression text"),
-				      _("Displayed text for the automatically computed sheet function."),
-				      NULL,
-				      GSF_PARAM_STATIC |
-				      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));
+		 PROP_AUTO_EXPR_VALUE,
+		 g_param_spec_boxed ("auto-expr-value",
+				     _("Auto-expression value"),
+				     _("The current value of the auto-exprssion."),
+				     gnm_value_get_type (),
+				     GSF_PARAM_STATIC |
+				     G_PARAM_READWRITE));
         g_object_class_install_property
 		(gobject_class,
 		 PROP_AUTO_EXPR_EVAL_POS,
@@ -1134,8 +1001,7 @@ workbook_view_new (Workbook *wb)
 	if (wbv->auto_expr.func)
 		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.value = NULL;
 	wbv->auto_expr.use_max_precision = FALSE;
 	dependent_managed_init (&wbv->auto_expr.dep, NULL);
 
diff --git a/src/workbook-view.h b/src/workbook-view.h
index 0f5020d..de3e940 100644
--- a/src/workbook-view.h
+++ b/src/workbook-view.h
@@ -34,8 +34,7 @@ struct _WorkbookView {
 	struct {
 		GnmFunc *func;
 		char *descr;
-		char *text;
-		PangoAttrList *attrs;
+		GnmValue *value;
 		gboolean use_max_precision;
 		GnmDependent dep;
 		gulong sheet_detached_sig;



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