[gnumeric] Conditional formats: make gnm_style_conditions_insert sane.



commit 3be05dfe53f53893470233d47897e9af75af9fd1
Author: Morten Welinder <terra gnome org>
Date:   Tue May 29 10:59:21 2012 -0400

    Conditional formats: make gnm_style_conditions_insert sane.
    
    Taking ownership of parts of an argument is messy.

 ChangeLog                             |    3 +
 plugins/excel/ms-excel-read.c         |   98 +++++++++++++++++++--------------
 plugins/excel/xlsx-read.c             |   52 ++++++++++-------
 plugins/openoffice/openoffice-read.c  |   62 ++++++++++-----------
 src/dialogs/dialog-cell-format-cond.c |   68 ++++++++++++----------
 src/style-conditions.c                |    9 +--
 6 files changed, 158 insertions(+), 134 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 046cf6c..62faa37 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2012-05-25  Morten Welinder  <terra gnome org>
 
+	* src/style-conditions.c (gnm_style_conditions_insert): Change
+	memory ownership semantics to no longer taking partial ownership.
+
 	* src/xml-sax-read.c (xml_sax_condition_expr_end): Fixed reversed
 	condition.
 
diff --git a/plugins/excel/ms-excel-read.c b/plugins/excel/ms-excel-read.c
index 96aee43..516a825 100644
--- a/plugins/excel/ms-excel-read.c
+++ b/plugins/excel/ms-excel-read.c
@@ -5128,7 +5128,9 @@ excel_read_CF (BiffQuery *q, ExcelReadSheet *esheet, GnmStyleConditions *sc)
 	guint16 expr0_len,expr1_len;
 	guint32 flags;
 	unsigned offset;
-	GnmStyleCond cond;
+	GnmStyleCond *cond;
+	GnmStyleCondOp cop;
+	GnmStyle *overlay;
 
 	XL_CHECK_CONDITION (q->length >= 12);
 
@@ -5145,41 +5147,50 @@ excel_read_CF (BiffQuery *q, ExcelReadSheet *esheet, GnmStyleConditions *sc)
 	switch (type) {
 	case 1 :
 		switch (op) {
-		case 0x01 : cond.op = GNM_STYLE_COND_BETWEEN;	break;
-		case 0x02 : cond.op = GNM_STYLE_COND_NOT_BETWEEN;	break;
-		case 0x03 : cond.op = GNM_STYLE_COND_EQUAL;	break;
-		case 0x04 : cond.op = GNM_STYLE_COND_NOT_EQUAL;	break;
-		case 0x05 : cond.op = GNM_STYLE_COND_GT;		break;
-		case 0x06 : cond.op = GNM_STYLE_COND_LT;		break;
-		case 0x07 : cond.op = GNM_STYLE_COND_GTE;		break;
-		case 0x08 : cond.op = GNM_STYLE_COND_LTE;		break;
+		case 0x01: cop = GNM_STYLE_COND_BETWEEN; break;
+		case 0x02: cop = GNM_STYLE_COND_NOT_BETWEEN; break;
+		case 0x03: cop = GNM_STYLE_COND_EQUAL; break;
+		case 0x04: cop = GNM_STYLE_COND_NOT_EQUAL; break;
+		case 0x05: cop = GNM_STYLE_COND_GT; break;
+		case 0x06: cop = GNM_STYLE_COND_LT; break;
+		case 0x07: cop = GNM_STYLE_COND_GTE; break;
+		case 0x08: cop = GNM_STYLE_COND_LTE; break;
 		default:
 			g_warning ("EXCEL : Unknown condition (%d) for conditional format in sheet %s.",
 				   op, esheet->sheet->name_unquoted);
 			return;
 		}
 		break;
-	case 2 : cond.op = GNM_STYLE_COND_CUSTOM;
+	case 2:
+		cop = GNM_STYLE_COND_CUSTOM;
 		break;
 
-	default :
+	default:
 		g_warning ("EXCEL : Unknown condition type (%d) for format in sheet %s.",
 			   (int)type, esheet->sheet->name_unquoted);
 		return;
 	}
 
-	cond.texpr[0] = (expr0_len <= 0)
-		? NULL
-		: ms_sheet_parse_expr_internal
-		(esheet,
-		 q->data + q->length - expr0_len - expr1_len,
-		 expr0_len);
-	cond.texpr[1] = (expr1_len <= 0)
-		? NULL
-		: ms_sheet_parse_expr_internal
-		(esheet,
-		 q->data + q->length - expr1_len,
-		 expr1_len);
+	cond = gnm_style_cond_new (cop);
+
+	if (expr0_len > 0) {
+		GnmExprTop const *texpr = 
+			ms_sheet_parse_expr_internal
+			(esheet,
+			 q->data + q->length - expr0_len - expr1_len,
+			 expr0_len);
+		gnm_style_cond_set_expr (cond, texpr, 0);
+		gnm_expr_top_unref (texpr);
+	}
+	if (expr1_len > 0) {
+		GnmExprTop const *texpr = 
+			ms_sheet_parse_expr_internal
+			(esheet,
+			 q->data + q->length - expr1_len,
+			 expr1_len);
+		gnm_style_cond_set_expr (cond, texpr, 1);
+		gnm_expr_top_unref (texpr);
+	}
 
 	/* UNDOCUMENTED : the format of the conditional format
 	 * is unspecified.
@@ -5206,7 +5217,7 @@ excel_read_CF (BiffQuery *q, ExcelReadSheet *esheet, GnmStyleConditions *sc)
 	 *	Similar to XF from biff7
 	 */
 
-	cond.overlay = gnm_style_new ();
+	overlay = gnm_style_new ();
 
 	offset =  6  /* CF record header */ + 6; /* format header */
 	if (flags & 0x04000000) { /* font */
@@ -5217,29 +5228,29 @@ excel_read_CF (BiffQuery *q, ExcelReadSheet *esheet, GnmStyleConditions *sc)
 		XL_CHECK_CONDITION (q->length >= 64 + 54);
 
 		if (0xFFFFFFFF != (size = GSF_LE_GET_GUINT32 (data)))
-			gnm_style_set_font_size	(cond.overlay, size / 20.);
+			gnm_style_set_font_size	(overlay, size / 20.);
 		if (0xFFFFFFFF != (colour = GSF_LE_GET_GUINT32 (data + 16)))
-			gnm_style_set_font_color (cond.overlay,
+			gnm_style_set_font_color (overlay,
 						  excel_palette_get (esheet->container.importer,
 								     colour));
 
 		tmp8  = GSF_LE_GET_GUINT8  (data + 4);
 		font_flags = GSF_LE_GET_GUINT8  (data + 24);
 		if (0 == (font_flags & 2)) {
-			gnm_style_set_font_italic (cond.overlay, 0 != (tmp8 & 2));
-			gnm_style_set_font_bold   (cond.overlay,
+			gnm_style_set_font_italic (overlay, 0 != (tmp8 & 2));
+			gnm_style_set_font_bold   (overlay,
 						   GSF_LE_GET_GUINT16 (data + 8) >= 0x2bc);
 		}
 		if (0 == (font_flags & 0x80))
-			gnm_style_set_font_strike (cond.overlay, 0 != (tmp8 & 0x80));
+			gnm_style_set_font_strike (overlay, 0 != (tmp8 & 0x80));
 
 		if (0 == GSF_LE_GET_GUINT8  (data + 28)) {
 			switch (GSF_LE_GET_GUINT8  (data + 10)) {
 			default : g_printerr ("Unknown script %d\n", GSF_LE_GET_GUINT8 (data));
 				/* fall through */
-			case 0: gnm_style_set_font_script (cond.overlay, GO_FONT_SCRIPT_STANDARD); break;
-			case 1: gnm_style_set_font_script (cond.overlay, GO_FONT_SCRIPT_SUPER); break;
-			case 2: gnm_style_set_font_script (cond.overlay, GO_FONT_SCRIPT_SUB); break;
+			case 0: gnm_style_set_font_script (overlay, GO_FONT_SCRIPT_STANDARD); break;
+			case 1: gnm_style_set_font_script (overlay, GO_FONT_SCRIPT_SUPER); break;
+			case 2: gnm_style_set_font_script (overlay, GO_FONT_SCRIPT_SUB); break;
 			}
 		}
 		if (0 == GSF_LE_GET_GUINT8  (data + 32)) {
@@ -5263,7 +5274,7 @@ excel_read_CF (BiffQuery *q, ExcelReadSheet *esheet, GnmStyleConditions *sc)
 				break;
 			}
 			gnm_style_set_font_uline
-				(cond.overlay,
+				(overlay,
 				 xls_uline_to_gnm_underline (mul));
 		}
 
@@ -5279,19 +5290,19 @@ excel_read_CF (BiffQuery *q, ExcelReadSheet *esheet, GnmStyleConditions *sc)
 		guint16 patterns = GSF_LE_GET_GUINT16 (q->data + offset);
 		guint32 colours  = GSF_LE_GET_GUINT32 (q->data + offset + 2);
 		if (0 == (flags & 0x0400))
-			excel_read_CF_border (&cond, esheet, GNM_STYLE_BORDER_LEFT,
+			excel_read_CF_border (cond, esheet, GNM_STYLE_BORDER_LEFT,
 					      (patterns >>  0) & 0xf,
 					      (colours  >>  0) & 0x7f);
 		if (0 == (flags & 0x0800))
-			excel_read_CF_border (&cond, esheet, GNM_STYLE_BORDER_RIGHT,
+			excel_read_CF_border (cond, esheet, GNM_STYLE_BORDER_RIGHT,
 					      (patterns >>  4) & 0xf,
 					      (colours  >>  7) & 0x7f);
 		if (0 == (flags & 0x1000))
-			excel_read_CF_border (&cond, esheet, GNM_STYLE_BORDER_TOP,
+			excel_read_CF_border (cond, esheet, GNM_STYLE_BORDER_TOP,
 					      (patterns >>  8) & 0xf,
 					      (colours  >> 16) & 0x7f);
 		if (0 == (flags & 0x2000))
-			excel_read_CF_border (&cond, esheet, GNM_STYLE_BORDER_BOTTOM,
+			excel_read_CF_border (cond, esheet, GNM_STYLE_BORDER_BOTTOM,
 					      (patterns >> 12) & 0xf,
 					      (colours  >> 23) & 0x7f);
 
@@ -5304,15 +5315,15 @@ excel_read_CF (BiffQuery *q, ExcelReadSheet *esheet, GnmStyleConditions *sc)
 		int pattern = 0;
 
 		if (0 == (flags & 0x10000))
-			gnm_style_set_pattern (cond.overlay,
+			gnm_style_set_pattern (overlay,
 					       pattern = excel_map_pattern_index_from_excel (
 											     (background_flags >> 10) & 0x3F));
 		if (0 == (flags & 0x20000))
-			gnm_style_set_pattern_color (cond.overlay,
+			gnm_style_set_pattern_color (overlay,
 						     excel_palette_get (esheet->container.importer,
 									(background_flags >> 16) & 0x7F));
 		if (0 == (flags & 0x40000))
-			gnm_style_set_back_color (cond.overlay,
+			gnm_style_set_back_color (overlay,
 						  excel_palette_get (esheet->container.importer,
 								     (background_flags >> 23) & 0x7F));
 
@@ -5321,9 +5332,12 @@ excel_read_CF (BiffQuery *q, ExcelReadSheet *esheet, GnmStyleConditions *sc)
 
 	XL_CHECK_CONDITION (q->length == offset + expr0_len + expr1_len);
 
-	d (1, gnm_style_dump (cond.overlay););
+	d (1, gnm_style_dump (overlay););
 
-	gnm_style_conditions_insert (sc, &cond, -1);
+	gnm_style_cond_set_overlay (cond, overlay);
+	gnm_style_unref (overlay);
+	gnm_style_conditions_insert (sc, cond, -1);
+	gnm_style_cond_free (cond);
 }
 
 static void
diff --git a/plugins/excel/xlsx-read.c b/plugins/excel/xlsx-read.c
index 38b7ac3..873309b 100644
--- a/plugins/excel/xlsx-read.c
+++ b/plugins/excel/xlsx-read.c
@@ -161,7 +161,7 @@ typedef struct {
 
 	GnmStyleConditions *conditions;
 	GSList		   *cond_regions;
-	GnmStyleCond	    cond;
+	GnmStyleCond	   *cond;
 
 	GnmFilter	   *filter;
 	int		    filter_cur_field;
@@ -2180,8 +2180,9 @@ xlsx_cond_fmt_rule_begin (GsfXMLIn *xin, xmlChar const **attrs)
 	GnmStyleCondOp	op = GNM_STYLE_COND_CUSTOM;
 	XlsxCFTypes	type = XLSX_CF_TYPE_UNDEFINED;
 	char const	*type_str = _("Undefined");
+	GnmStyle        *overlay = NULL;
 
-	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
+	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
 		if (attr_bool (xin, attrs, "formatRow", &formatRow)) ;
 		else if (attr_bool (xin, attrs, "stopIfTrue", &stopIfTrue)) ;
 		else if (attr_bool (xin, attrs, "above", &above)) ;
@@ -2194,21 +2195,24 @@ xlsx_cond_fmt_rule_begin (GsfXMLIn *xin, xmlChar const **attrs)
 			type = tmp;
 			type_str = attrs[1];
 		}
+	}
 #if 0
+/*
 	"numFmtId"	="ST_NumFmtId" use="optional">
 	"priority"	="xs:int" use="required">
 	"text"		="xs:string" use="optional">
 	"timePeriod"	="ST_TimePeriod" use="optional">
 	"col1"		="xs:unsignedInt" use="optional">
 	"col2"		="xs:unsignedInt" use="optional">
+*/
 #endif
 
-	if (dxf >= 0 && NULL != (state->cond.overlay = xlsx_get_dxf (xin, dxf)))
-		gnm_style_ref (state->cond.overlay);
+	if (dxf >= 0)
+		overlay = xlsx_get_dxf (xin, dxf);
 
 	switch (type) {
 	case XLSX_CF_TYPE_CELL_IS :
-		state->cond.op = op;
+		/* Nothing */
 		break;
 	case XLSX_CF_TYPE_CONTAINS_STR :
 	case XLSX_CF_TYPE_NOT_CONTAINS_STR :
@@ -2218,12 +2222,16 @@ xlsx_cond_fmt_rule_begin (GsfXMLIn *xin, xmlChar const **attrs)
 	case XLSX_CF_TYPE_NOT_CONTAINS_BLANKS :
 	case XLSX_CF_TYPE_CONTAINS_ERRORS :
 	case XLSX_CF_TYPE_NOT_CONTAINS_ERRORS :
-		state->cond.op = type;
+		op = type;
 		break;
 
 	default :
 		xlsx_warning (xin, _("Ignoring unhandled conditional format of type '%s'"), type_str);
 	}
+
+	state->cond = gnm_style_cond_new (op);
+	gnm_style_cond_set_overlay (state->cond, overlay);
+
 	state->count = 0;
 }
 
@@ -2231,20 +2239,16 @@ static void
 xlsx_cond_fmt_rule_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
 {
 	XLSXReadState *state = (XLSXReadState *)xin->user_state;
-	if (gnm_style_cond_is_valid (&state->cond)) {
-		if (NULL == state->conditions)
-			state->conditions = gnm_style_conditions_new ();
-		gnm_style_conditions_insert (state->conditions, &state->cond, -1);
-	} else {
-		if (NULL != state->cond.texpr[0])
-			gnm_expr_top_unref (state->cond.texpr[0]);
-		if (NULL != state->cond.texpr[1])
-			gnm_expr_top_unref (state->cond.texpr[1]);
-		if (NULL != state->cond.overlay)
-			gnm_style_unref (state->cond.overlay);
+	if (state->cond) {
+		if (gnm_style_cond_is_valid (state->cond)) {
+			if (NULL == state->conditions)
+				state->conditions = gnm_style_conditions_new ();
+			gnm_style_conditions_insert (state->conditions,
+						     state->cond, -1);
+		}
+		gnm_style_cond_free (state->cond);
+		state->cond = NULL;
 	}
-	state->cond.texpr[0] = state->cond.texpr[1] = NULL;
-	state->cond.overlay = NULL;
 }
 
 static void
@@ -2252,11 +2256,15 @@ xlsx_cond_fmt_formula_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
 {
 	XLSXReadState *state = (XLSXReadState *)xin->user_state;
 	GnmParsePos pp;
-	if (state->count > 1)
+	if (!state->cond || state->count > 1)
 		return;
 
-	state->cond.texpr[state->count++] = xlsx_parse_expr (xin, xin->content->str,
-		parse_pos_init_sheet (&pp, state->sheet));
+	parse_pos_init_sheet (&pp, state->sheet);
+
+	gnm_style_cond_set_expr (state->cond, 
+				 xlsx_parse_expr (xin, xin->content->str, &pp),
+				 state->count);
+	state->count++;
 }
 
 static void
diff --git a/plugins/openoffice/openoffice-read.c b/plugins/openoffice/openoffice-read.c
index fa7c353..423c29b 100644
--- a/plugins/openoffice/openoffice-read.c
+++ b/plugins/openoffice/openoffice-read.c
@@ -3037,7 +3037,6 @@ odf_style_load_one_value (GsfXMLIn *xin, char *condition, GnmStyleCond *cond, gc
 	return (cond->texpr[0] != NULL);
 }
 
-/*odf_style_add_condition absorbs the cstyle reference */
 static void
 odf_style_add_condition (GsfXMLIn *xin, GnmStyle *style, GnmStyle *cstyle, 
 			 gchar const *condition, gchar const *base)
@@ -3045,7 +3044,7 @@ odf_style_add_condition (GsfXMLIn *xin, GnmStyle *style, GnmStyle *cstyle,
 	/* OOParseState *state = (OOParseState *)xin->user_state; */
 
 	gchar const *full_condition = condition;
-	GnmStyleCond cond;
+	GnmStyleCond *cond = NULL;
 	GnmStyleConditions *sc;
 	gboolean success = FALSE;
 	OOFormula f_type;
@@ -3054,9 +3053,6 @@ odf_style_add_condition (GsfXMLIn *xin, GnmStyle *style, GnmStyle *cstyle,
 	g_return_if_fail (cstyle != NULL);
 	g_return_if_fail (condition != NULL);
 	g_return_if_fail (base != NULL);
-	
-	cond.texpr[0] = NULL;
-	cond.texpr[1] = NULL;
 
 	f_type = odf_get_formula_type (xin, &condition);
 
@@ -3067,28 +3063,28 @@ odf_style_add_condition (GsfXMLIn *xin, GnmStyle *style, GnmStyle *cstyle,
 		case '<':
 			if (*condition == '=') {
 				condition++;
-				cond.op = GNM_STYLE_COND_LTE;
+				cond = gnm_style_cond_new (GNM_STYLE_COND_LTE);
 			} else
-				cond.op = GNM_STYLE_COND_LT;
+				cond = gnm_style_cond_new (GNM_STYLE_COND_LT);
 			success = TRUE;
 			break;
 		case '>':
 			if (*condition == '=') {
 				condition++;
-				cond.op = GNM_STYLE_COND_GTE;
+				cond = gnm_style_cond_new (GNM_STYLE_COND_GTE);
 			} else
-				cond.op = GNM_STYLE_COND_GT;
+				cond = gnm_style_cond_new (GNM_STYLE_COND_GT);
 			success = TRUE;
 			break;
 			break;
 		case '=':
-			cond.op = GNM_STYLE_COND_EQUAL;
+			cond = gnm_style_cond_new (GNM_STYLE_COND_EQUAL);
 			success = TRUE;
 			break;
 		case '!':
 			if (*condition == '=') {
 				condition++;
-				cond.op = GNM_STYLE_COND_NOT_EQUAL;
+				cond = gnm_style_cond_new (GNM_STYLE_COND_NOT_EQUAL);
 				success = TRUE;
 			}
 			break;
@@ -3097,73 +3093,72 @@ odf_style_add_condition (GsfXMLIn *xin, GnmStyle *style, GnmStyle *cstyle,
 		}
 		if (success) {
 			char *text = g_strdup (condition);
-			success = odf_style_load_one_value (xin, text, &cond, base, f_type);
+			success = odf_style_load_one_value (xin, text, cond, base, f_type);
 			g_free (text);
 		}
 
 	} else if (g_str_has_prefix (condition, "cell-content-is-between")) {
 		char *text;
-		cond.op = GNM_STYLE_COND_BETWEEN;
+		cond = gnm_style_cond_new (GNM_STYLE_COND_BETWEEN);
 		condition += strlen ("cell-content-is-between");
 		text = g_strdup (condition);
-		success = odf_style_load_two_values (xin, text, &cond, base, f_type);
+		success = odf_style_load_two_values (xin, text, cond, base, f_type);
 		g_free (text);
 	} else if (g_str_has_prefix (condition, "cell-content-is-not-between")) {
 		char *text;
-		cond.op = GNM_STYLE_COND_NOT_BETWEEN;
+		cond = gnm_style_cond_new (GNM_STYLE_COND_NOT_BETWEEN);
 		condition += strlen ("cell-content-is-not-between");
 		text = g_strdup (condition);
-		success = odf_style_load_two_values (xin, text, &cond, base, f_type);
+		success = odf_style_load_two_values (xin, text, cond, base, f_type);
 		g_free (text);
 	} else if (g_str_has_prefix (condition, "is-true-formula")) {
 		if (0 == strcmp (full_condition, "of:is-true-formula(ISERROR([.A1]))") &&
 		    g_str_has_suffix (base, ".$A$1")) {
-			cond.op = GNM_STYLE_COND_CONTAINS_ERR;
+			cond = gnm_style_cond_new (GNM_STYLE_COND_CONTAINS_ERR);
 			success = TRUE;
 		} else if (0 == strcmp (full_condition, "of:is-true-formula(NOT(ISERROR([.A1])))") &&
 			   g_str_has_suffix (base, ".$A$1")) {
-			cond.op = GNM_STYLE_COND_NOT_CONTAINS_ERR;
+			cond = gnm_style_cond_new (GNM_STYLE_COND_NOT_CONTAINS_ERR);
 			success = TRUE;
 		} else if (0 == strcmp (full_condition, "of:is-true-formula(NOT(ISERROR(FIND(\" \";[.A1]))))") &&
 			   g_str_has_suffix (base, ".$A$1")) {
-			cond.op = GNM_STYLE_COND_CONTAINS_BLANKS;
+			cond = gnm_style_cond_new (GNM_STYLE_COND_CONTAINS_BLANKS);
 			success = TRUE;
 		} else if (0 == strcmp (full_condition, "of:is-true-formula(ISERROR(FIND(\" \";[.A1])))") &&
 			   g_str_has_suffix (base, ".$A$1")) {
-			cond.op = GNM_STYLE_COND_NOT_CONTAINS_BLANKS;
+			cond = gnm_style_cond_new (GNM_STYLE_COND_NOT_CONTAINS_BLANKS);
 			success = TRUE;
 		} else {
 			char *text;
-			cond.op = GNM_STYLE_COND_CUSTOM;
+			cond = gnm_style_cond_new (GNM_STYLE_COND_CUSTOM);
 			condition += strlen ("is-true-formula");
 			text = g_strdup (condition);
-			success = odf_style_load_one_value (xin, text, &cond, base, f_type);
+			success = odf_style_load_one_value (xin, text, cond, base, f_type);
 			g_free (text);
 		}
 	}
 	
-	if (!success) {
-		if (cond.texpr[0] != NULL)
-			gnm_expr_top_unref (cond.texpr[0]);
-		if (cond.texpr[1] != NULL)
-			gnm_expr_top_unref (cond.texpr[1]);
+	if (!success || !cond) {
+		if (cond)
+			gnm_style_cond_free (cond);
 		oo_warning (xin,
 			    _("Unknown condition '%s' encountered, ignoring."),
 			    full_condition);
-		gnm_style_unref (cstyle);
 		return;
 	}
 
-	cond.overlay = cstyle;
+	gnm_style_cond_set_overlay (cond, cstyle);
 
 	if (gnm_style_is_element_set (style, MSTYLE_CONDITIONS) &&
 	    (sc = gnm_style_get_conditions (style)) != NULL)
-		gnm_style_conditions_insert (sc, &cond, -1);
+		gnm_style_conditions_insert (sc, cond, -1);
 	else {
 		sc = gnm_style_conditions_new ();
-		gnm_style_conditions_insert (sc, &cond, -1);
+		gnm_style_conditions_insert (sc, cond, -1);
 		gnm_style_set_conditions (style, sc);
 	}	
+
+	gnm_style_cond_free (cond);
 }
 
 static GnmStyle *
@@ -3176,9 +3171,10 @@ odf_style_from_oo_cell_style (GsfXMLIn *xin, OOCellStyle *oostyle)
 		GnmStyle *new_style = gnm_style_dup (oostyle->style);
 		GSList *styles = oostyle->styles, *conditions = oostyle->conditions, *bases = oostyle->bases;
 		while (styles && conditions && bases) {
-			odf_style_add_condition (xin, new_style,
-						 odf_style_from_oo_cell_style (xin, styles->data),
+			GnmStyle *cstyle = odf_style_from_oo_cell_style (xin, styles->data);
+			odf_style_add_condition (xin, new_style, cstyle,
 						 conditions->data, bases->data);
+			gnm_style_unref (cstyle);
 			styles = styles->next;
 			conditions = conditions->next;
 			bases = bases->next;
diff --git a/src/dialogs/dialog-cell-format-cond.c b/src/dialogs/dialog-cell-format-cond.c
index 51f83f9..3bbfd5e 100644
--- a/src/dialogs/dialog-cell-format-cond.c
+++ b/src/dialogs/dialog-cell-format-cond.c
@@ -285,78 +285,80 @@ cb_c_fmt_dialog_edit_style_button (G_GNUC_UNUSED GtkWidget *btn, CFormatState *s
 static GnmStyleCond *
 c_fmt_dialog_get_condition (CFormatState *state)
 {
-	GnmStyleCond *cond = g_new0(GnmStyleCond, 1);
+	GnmStyleCondOp op;
+	GnmStyleCond *cond;
 	GtkTreeIter iter;
 	gint n_expr = 0;
 	GnmParsePos pp;
+	GnmStyle *overlay;
 
 	parse_pos_init_editpos (&pp, state->sv);
 
-	cond->overlay = gnm_style_new ();
+	overlay = gnm_style_new ();
 	if (state->editor.style) {
 		if (cb_c_fmt_dialog_chooser_check_page
 		    (state, "check-background", FD_BACKGROUND)) {
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_COLOR_BACK);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_COLOR_PATTERN);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_PATTERN);
 		}
 		if (cb_c_fmt_dialog_chooser_check_page
 		    (state, "check-number", FD_NUMBER)) {
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FORMAT);
 		}
 		if (cb_c_fmt_dialog_chooser_check_page
 		    (state, "check-align", FD_ALIGNMENT)) {
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_ALIGN_V);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_ALIGN_H);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_INDENT);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_ROTATION);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_TEXT_DIR);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_WRAP_TEXT);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_SHRINK_TO_FIT);
 		}
 		if (cb_c_fmt_dialog_chooser_check_page
 		    (state, "check-font", FD_FONT)) {
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FONT_COLOR);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FONT_NAME);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FONT_BOLD);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FONT_ITALIC);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FONT_UNDERLINE);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FONT_STRIKETHROUGH);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FONT_SCRIPT);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_FONT_SIZE);
 		}
 		if (cb_c_fmt_dialog_chooser_check_page
 		    (state, "check-border", FD_BORDER)) {
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_BORDER_TOP);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_BORDER_BOTTOM);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_BORDER_LEFT);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_BORDER_RIGHT);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_BORDER_REV_DIAGONAL);
-			gnm_style_merge_element (cond->overlay, state->editor.style,
+			gnm_style_merge_element (overlay, state->editor.style,
 						 MSTYLE_BORDER_DIAGONAL);
 		}
 		if (cb_c_fmt_dialog_chooser_check_page
@@ -371,11 +373,15 @@ c_fmt_dialog_get_condition (CFormatState *state)
 	if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (state->editor.combo), &iter))
 		gtk_tree_model_get (GTK_TREE_MODEL (state->editor.typestore),
 				    &iter,
-				    1, &cond->op,
+				    1, &op,
 				    2, &n_expr,
 				    -1);
 	else
-		cond->op = GNM_STYLE_COND_CONTAINS_ERR;
+		op = GNM_STYLE_COND_CONTAINS_ERR;
+
+	cond = gnm_style_cond_new (op);
+	gnm_style_cond_set_overlay (cond, overlay);
+	gnm_style_unref (overlay);
 
 	if (n_expr > 0) {
 		GnmExprTop const *texpr = gnm_expr_entry_parse (GNM_EXPR_ENTRY (state->editor.expr_x), &pp,
@@ -397,7 +403,7 @@ cb_c_fmt_dialog_add_button (G_GNUC_UNUSED GtkWidget *btn, CFormatState *state)
 {
 	GnmStyleCond *cond = c_fmt_dialog_get_condition (state);
 	c_fmt_dialog_apply_add_choice (state, cond, TRUE);
-	g_free (cond);
+	gnm_style_cond_free (cond);
 }
 
 static void
@@ -405,7 +411,7 @@ cb_c_fmt_dialog_replace_button (G_GNUC_UNUSED GtkWidget *btn, CFormatState *stat
 {
 	GnmStyleCond *cond = c_fmt_dialog_get_condition (state);
 	c_fmt_dialog_apply_add_choice (state, cond, FALSE);
-	g_free (cond);
+	gnm_style_cond_free (cond);
 }
 
 static void
diff --git a/src/style-conditions.c b/src/style-conditions.c
index 8e63025..4035b02 100644
--- a/src/style-conditions.c
+++ b/src/style-conditions.c
@@ -227,15 +227,13 @@ gnm_style_conditions_insert (GnmStyleConditions *sc,
 
 	g_return_if_fail (cond_ != NULL);
 
-	cond = g_memdup (cond_, sizeof (*cond_));
-	if (sc == NULL || !gnm_style_cond_is_valid (cond)) {
-		gnm_style_cond_free (cond); /* be careful not to leak */
+	if (sc == NULL || !gnm_style_cond_is_valid (cond_))
 		return;
-	}
 
 	if (sc->conditions == NULL)
 		sc->conditions = g_ptr_array_new ();
 
+	cond = gnm_style_cond_dup (cond_);
 	g_ptr_array_add (sc->conditions, cond);
 	if (pos >= 0) {
 		int i;
@@ -250,8 +248,7 @@ gnm_style_conditions_insert (GnmStyleConditions *sc,
 }
 
 void
-gnm_style_conditions_delete  (GnmStyleConditions *sc,
-			      guint pos)
+gnm_style_conditions_delete (GnmStyleConditions *sc, guint pos)
 {
 	g_return_if_fail (sc != NULL);
 	g_return_if_fail (sc->conditions != NULL);



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