[gnumeric] StyleConditions: another step towards proper handling of expressions.



commit 1562e031606b0f66b923fb243f5ac9b91aa5af9b
Author: Morten Welinder <terra gnome org>
Date:   Wed May 23 15:40:26 2012 -0400

    StyleConditions: another step towards proper handling of expressions.

 ChangeLog              |    9 +++++++++
 src/style-conditions.c |   21 +++++++++++++++++----
 src/style-conditions.h |    5 +++--
 src/xml-sax-read.c     |   36 ++++++++++++++++++++++--------------
 4 files changed, 51 insertions(+), 20 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index a1a9e3b..a69596a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2012-05-23  Morten Welinder  <terra gnome org>
+
+	* src/style-conditions.c (gnm_style_cond_set_overlay): New
+	function.
+	(gnm_style_cond_new): Drop overlay argument.  All callers fixed.
+	(gnm_style_cond_dup): Use gnm_style_cond_set_overlay.
+
+	* src/xml-sax-read.c: Treat GnmStyleCond as (mostly) opaque.
+
 2012-05-21  Andreas J. Guelzow <aguelzow pyrshep ca>
 
 	* src/sheet-style.h (sheet_style_range_foreach): new
diff --git a/src/style-conditions.c b/src/style-conditions.c
index 07e9e7d..8e63025 100644
--- a/src/style-conditions.c
+++ b/src/style-conditions.c
@@ -69,11 +69,10 @@ gnm_style_cond_is_valid (GnmStyleCond const *cond)
 }
 
 GnmStyleCond *
-gnm_style_cond_new (GnmStyleCondOp op, GnmStyle *overlay)
+gnm_style_cond_new (GnmStyleCondOp op)
 {
 	GnmStyleCond *res = g_new0 (GnmStyleCond, 1);
 	res->op = op;
-	gnm_style_ref ((res->overlay = overlay));
 	return res;
 }
 
@@ -85,7 +84,8 @@ gnm_style_cond_dup (GnmStyleCond const *src)
 
 	g_return_val_if_fail (src != NULL, NULL);
 
-	dst = gnm_style_cond_new (src->op, src->overlay);
+	dst = gnm_style_cond_new (src->op);
+	gnm_style_cond_set_overlay (dst, src->overlay);
 	for (ui = 0; ui < 2; ui++)
 		gnm_style_cond_set_expr (dst, src->texpr[ui], ui);
 
@@ -122,6 +122,19 @@ gnm_style_cond_set_expr (GnmStyleCond *cond,
 	cond->texpr[idx] = texpr;
 }
 
+void
+gnm_style_cond_set_overlay (GnmStyleCond *cond, GnmStyle *overlay)
+{
+	g_return_if_fail (cond != NULL);
+
+	if (overlay)
+		gnm_style_ref (overlay);
+	if (cond->overlay)
+		gnm_style_unref (cond->overlay);
+	cond->overlay = overlay;
+}
+
+
 static void
 gnm_style_conditions_finalize (GObject *obj)
 {
@@ -162,7 +175,7 @@ gnm_style_conditions_new (void)
 }
 
 GnmStyleConditions *
-gnm_style_conditions_dup  (GnmStyleConditions const *cond)
+gnm_style_conditions_dup (GnmStyleConditions const *cond)
 {
 	GnmStyleConditions *dup;
 	GPtrArray const *ga;
diff --git a/src/style-conditions.h b/src/style-conditions.h
index aeb5c6e..029b097 100644
--- a/src/style-conditions.h
+++ b/src/style-conditions.h
@@ -42,14 +42,15 @@ typedef struct {
 	GnmStyleCondOp	  op;
 } GnmStyleCond;
 
-GnmStyleCond *gnm_style_cond_new (GnmStyleCondOp op,
-				  GnmStyle *overlay);
+GnmStyleCond *gnm_style_cond_new (GnmStyleCondOp op);
 void gnm_style_cond_free (GnmStyleCond *cond);
 GnmStyleCond *gnm_style_cond_dup (GnmStyleCond const *src);
 gboolean      gnm_style_cond_is_valid (GnmStyleCond const *cond);
 void          gnm_style_cond_set_expr (GnmStyleCond *cond,
 				       GnmExprTop const *texpr,
 				       unsigned idx);
+void          gnm_style_cond_set_overlay (GnmStyleCond *cond,
+					  GnmStyle *overlay);
 
 GnmStyleConditions *gnm_style_conditions_new  (void);
 GnmStyleConditions *gnm_style_conditions_dup  (GnmStyleConditions const *cond);
diff --git a/src/xml-sax-read.c b/src/xml-sax-read.c
index a7a3427..0946979 100644
--- a/src/xml-sax-read.c
+++ b/src/xml-sax-read.c
@@ -332,7 +332,8 @@ typedef struct {
 		gboolean	 allow_blank;
 		gboolean	 use_dropdown;
 	} validation;
-	GnmStyleCond	cond;
+
+	GnmStyleCond *cond;
 	GnmStyle *cond_save_style;
 
 	gboolean  style_range_init;
@@ -1670,11 +1671,9 @@ static void
 xml_sax_condition (GsfXMLIn *xin, xmlChar const **attrs)
 {
 	XMLSaxParseState *state = (XMLSaxParseState *)xin->user_state;
+	GnmStyleCondOp op = GNM_STYLE_COND_CUSTOM;
 
-	int dummy;
-
-	g_return_if_fail (state->cond.texpr[0] == NULL);
-	g_return_if_fail (state->cond.texpr[1] == NULL);
+	g_return_if_fail (state->cond == NULL);
 	g_return_if_fail (state->cond_save_style == NULL);
 
 	xml_sax_must_have_style (state);
@@ -1682,12 +1681,16 @@ xml_sax_condition (GsfXMLIn *xin, xmlChar const **attrs)
 	state->cond_save_style = state->style;
 	state->style = gnm_style_new ();
 
-	state->cond.op = GNM_STYLE_COND_CUSTOM;
-	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
+	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
+		int dummy;
+
 		if (gnm_xml_attr_int (attrs, "Operator", &dummy))
-			state->cond.op = dummy;
+			op = dummy;
 		else
 			unknown_attr (xin, attrs);
+	}
+
+	state->cond = gnm_style_cond_new (op);
 }
 
 static void
@@ -1698,8 +1701,10 @@ xml_sax_condition_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
 
 	xml_sax_must_have_style (state);
 	g_return_if_fail (state->cond_save_style != NULL);
+	g_return_if_fail (state->cond != NULL);
 
-	state->cond.overlay = state->style;
+	gnm_style_cond_set_overlay (state->cond, state->style);
+	gnm_style_unref (state->style);
 	state->style = state->cond_save_style;
 	state->cond_save_style = NULL;
 
@@ -1707,9 +1712,10 @@ xml_sax_condition_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
 	    NULL == (sc = gnm_style_get_conditions (state->style)))
 		gnm_style_set_conditions (state->style,
 			(sc = gnm_style_conditions_new ()));
-	gnm_style_conditions_insert (sc, &state->cond, -1);
+	gnm_style_conditions_insert (sc, state->cond, -1);
 
-	state->cond.texpr[0] = state->cond.texpr[1] = NULL;
+	gnm_style_cond_free (state->cond);
+	state->cond = NULL;
 }
 
 static void
@@ -1721,7 +1727,8 @@ xml_sax_condition_expr_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
 	GnmExprTop const *texpr;
 	GnmParsePos pos;
 
-	g_return_if_fail (state->cond.texpr[i] == NULL);
+	g_return_if_fail (state->cond == NULL);
+	g_return_if_fail (state->cond->texpr[i] == NULL);
 
 	texpr = gnm_expr_parse_str (xin->content->str,
 				    parse_pos_init_sheet (&pos, state->sheet),
@@ -1731,7 +1738,8 @@ xml_sax_condition_expr_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
 
 	g_return_if_fail (texpr != NULL);
 
-	state->cond.texpr[i] = texpr;
+	gnm_style_cond_set_expr (state->cond, texpr, i);
+	gnm_expr_top_unref (texpr);
 }
 
 static void
@@ -3214,7 +3222,7 @@ read_file_init_state (XMLSaxParseState *state,
 	state->filter = NULL;
 	state->validation.title = state->validation.msg = NULL;
 	state->validation.texpr[0] = state->validation.texpr[1] = NULL;
-	state->cond.texpr[0] = state->cond.texpr[1] = NULL;
+	state->cond = NULL;
 	state->cond_save_style = NULL;
 	state->expr_map = g_hash_table_new_full
 		(g_direct_hash, g_direct_equal,



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