[gnumeric] Handle PrintInfo problems with variable-size sheets.



commit 862af3f70a3ffb43b1d2c8f8985fde94dd031a70
Author: Morten Welinder <terra gnome org>
Date:   Fri Apr 17 13:17:44 2009 -0400

    Handle PrintInfo problems with variable-size sheets.
    
    * src/print-info.c (load_range): Take Sheet argument too and
    supply it to the parser.  All callers changed.
    (print_info_new): Take Sheet argument too.  All callers changed.
    (print_info_dup): Constify.  Take extra Sheet argument; all
    callers changed.  Copy field-by-field.  Free clobbered page
    breaks.
    (gnm_page_breaks_free): Handle NULL.
    (gnm_page_breaks_new): Drop optional argument len.  All callers
    changed.
---
 ChangeLog                            |   10 ++++
 plugins/excel/ms-excel-read.c        |    2 +-
 plugins/excel/xlsx-read.c            |    3 +-
 plugins/openoffice/openoffice-read.c |    4 +-
 src/commands.c                       |   17 +++---
 src/dialogs/dialog-printer-setup.c   |    2 +-
 src/print-info.c                     |   90 +++++++++++++++++++++-------------
 src/print-info.h                     |    7 ++-
 src/sheet.c                          |    4 +-
 src/xml-io.c                         |    2 +-
 src/xml-sax-read.c                   |    9 +---
 11 files changed, 88 insertions(+), 62 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b90eb9c..1dfd2db 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2009-04-17  Morten Welinder  <terra gnome org>
 
+	* src/print-info.c (load_range): Take Sheet argument too and
+	supply it to the parser.  All callers changed.
+	(print_info_new): Take Sheet argument too.  All callers changed.
+	(print_info_dup): Constify.  Take extra Sheet argument; all
+	callers changed.  Copy field-by-field.  Free clobbered page
+	breaks.
+	(gnm_page_breaks_free): Handle NULL.
+	(gnm_page_breaks_new): Drop optional argument len.  All callers
+	changed.
+
 	* src/position.c (gnm_cellpos_hash, gnm_cellref_hash,
 	gnm_rangeref_hash): Improve these in light on variable-size
 	sheets.
diff --git a/plugins/excel/ms-excel-read.c b/plugins/excel/ms-excel-read.c
index 2963d62..8d74f8a 100644
--- a/plugins/excel/ms-excel-read.c
+++ b/plugins/excel/ms-excel-read.c
@@ -5963,7 +5963,7 @@ excel_read_PAGE_BREAK (BiffQuery *q, ExcelReadSheet *esheet, gboolean is_vert)
 	XL_CHECK_CONDITION (q->length >= 2);
 	count = GSF_LE_GET_GUINT16 (q->data);
 	XL_CHECK_CONDITION (q->length >= 2 + count * step);
-	breaks = gnm_page_breaks_new (count, is_vert);
+	breaks = gnm_page_breaks_new (is_vert);
 
 	/* 1) Ignore the first/last info for >= biff8
 	 * 2) Assume breaks are manual in the absence of any information  */
diff --git a/plugins/excel/xlsx-read.c b/plugins/excel/xlsx-read.c
index 499f4de..228c0f0 100644
--- a/plugins/excel/xlsx-read.c
+++ b/plugins/excel/xlsx-read.c
@@ -2781,8 +2781,7 @@ xlsx_CT_PageBreaks_begin (GsfXMLIn *xin, xmlChar const **attrs)
 		else if (attr_int  (xin, attrs, "manualBreakCount", &manual_count)) ;
 #endif
 
-	state->page_breaks = gnm_page_breaks_new (count,
-		xin->node->user_data.v_int);
+	state->page_breaks = gnm_page_breaks_new (xin->node->user_data.v_int);
 }
 
 static void
diff --git a/plugins/openoffice/openoffice-read.c b/plugins/openoffice/openoffice-read.c
index 5674521..d05f255 100644
--- a/plugins/openoffice/openoffice-read.c
+++ b/plugins/openoffice/openoffice-read.c
@@ -626,10 +626,10 @@ oo_append_page_break (OOParseState *state, int pos, gboolean is_vert)
 
 	if (is_vert) {
 		if (NULL == (breaks = state->page_breaks.v))
-			breaks = state->page_breaks.v = gnm_page_breaks_new (0, TRUE);
+			breaks = state->page_breaks.v = gnm_page_breaks_new (TRUE);
 	} else {
 		if (NULL == (breaks = state->page_breaks.h))
-			breaks = state->page_breaks.h = gnm_page_breaks_new (0, FALSE);
+			breaks = state->page_breaks.h = gnm_page_breaks_new (FALSE);
 	}
 
 	gnm_page_breaks_append_break (breaks, pos, GNM_PAGE_BREAK_MANUAL);
diff --git a/src/commands.c b/src/commands.c
index 1c6e90b..10b6e26 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -5522,9 +5522,10 @@ cmd_print_setup_undo (GnmCommand *cmd, WorkbookControl *wbc)
 	g_return_val_if_fail (me->old_pi != NULL, TRUE);
 
 	if (me->cmd.sheet) {
+		PrintInformation *pi = me->old_pi->data;
 		print_info_free (me->cmd.sheet->print_info);
-		me->cmd.sheet->print_info = print_info_dup (
-			(PrintInformation *) me->old_pi->data);
+		me->cmd.sheet->print_info =
+			print_info_dup (pi, me->cmd.sheet);
 	} else {
 		book = wb_control_get_workbook(wbc);
 		n = workbook_sheet_count (book);
@@ -5532,13 +5533,13 @@ cmd_print_setup_undo (GnmCommand *cmd, WorkbookControl *wbc)
 		g_return_val_if_fail (g_slist_length (infos) == n, TRUE);
 
 		for (i = 0 ; i < n ; i++) {
+			PrintInformation *pi = infos->data;
 			Sheet *sheet = workbook_sheet_by_index (book, i);
 
 			g_return_val_if_fail (infos != NULL, TRUE);
 
 			print_info_free (sheet->print_info);
-			sheet->print_info = print_info_dup (
-				(PrintInformation *) infos->data);
+			sheet->print_info = print_info_dup (pi, sheet);
 			infos = infos->next;
 		}
 	}
@@ -5558,18 +5559,18 @@ cmd_print_setup_redo (GnmCommand *cmd, WorkbookControl *wbc)
 			me->old_pi = g_slist_append (me->old_pi, me->cmd.sheet->print_info);
 		else
 			print_info_free (me->cmd.sheet->print_info);
-		me->cmd.sheet->print_info = print_info_dup (me->new_pi);
+		me->cmd.sheet->print_info = print_info_dup (me->new_pi, me->cmd.sheet);
 	} else {
 		book = wb_control_get_workbook(wbc);
 		n = workbook_sheet_count (book);
 		for (i = 0 ; i < n ; i++) {
-			Sheet * sheet = workbook_sheet_by_index (book, i);
+			Sheet *sheet = workbook_sheet_by_index (book, i);
 			sheet_mark_dirty (sheet);
 			if (save_pis)
 				me->old_pi = g_slist_prepend (me->old_pi, sheet->print_info);
 			else
 				print_info_free (sheet->print_info);
-			sheet->print_info = print_info_dup (me->new_pi);
+			sheet->print_info = print_info_dup (me->new_pi, sheet);
 		}
 		me->old_pi = g_slist_reverse (me->old_pi);
 	}
@@ -5605,7 +5606,7 @@ cmd_print_setup (WorkbookControl *wbc, Sheet *sheet, PrintInformation const *pi)
 	else
 		me->cmd.cmd_descriptor = g_strdup (_("Page Setup For All Sheets"));
 	me->old_pi = NULL;
-	me->new_pi = print_info_dup ((PrintInformation *) pi);
+	me->new_pi = print_info_dup (pi, sheet);
 
 	return command_push_undo (wbc, G_OBJECT (me));
 }
diff --git a/src/dialogs/dialog-printer-setup.c b/src/dialogs/dialog-printer-setup.c
index 594a028..902ad66 100644
--- a/src/dialogs/dialog-printer-setup.c
+++ b/src/dialogs/dialog-printer-setup.c
@@ -2609,7 +2609,7 @@ printer_setup_state_new (WBCGtk *wbcg, Sheet *sheet)
 	state->wbcg  = wbcg;
 	state->sheet = sheet;
 	state->gui   = gui;
-	state->pi    = print_info_dup (sheet->print_info);
+	state->pi    = print_info_dup (sheet->print_info, sheet);
 	state->display_unit = state->pi->desired_display.top;
 	state->customize_header = NULL; 
 	state->customize_footer = NULL; 
diff --git a/src/print-info.c b/src/print-info.c
index 21c7e73..940b0ab 100644
--- a/src/print-info.c
+++ b/src/print-info.c
@@ -148,10 +148,9 @@ print_info_free (PrintInformation *pi)
 }
 
 static gboolean
-load_range (char const *str, GnmRange *r)
+load_range (char const *str, GnmRange *r, Sheet *sheet)
 {
-#warning "We cannot use NULL here"
-	return str && range_parse (r, str, NULL);
+	return str && range_parse (r, str, sheet);
 }
 
 static void
@@ -258,9 +257,11 @@ print_info_load_defaults (PrintInformation *res)
 	res->desired_display.header = gnm_app_prefs->desired_display;
 
 	res->repeat_top.use   = load_range (gnm_app_prefs->print_repeat_top,
-					    &res->repeat_top.range);
+					    &res->repeat_top.range,
+					    res->sheet);
 	res->repeat_left.use  = load_range (gnm_app_prefs->print_repeat_left,
-					    &res->repeat_left.range);
+					    &res->repeat_left.range,
+					    res->sheet);
 
 	res->center_vertically     = gnm_app_prefs->print_center_vertically;
 	res->center_horizontally   = gnm_app_prefs->print_center_horizontally;
@@ -295,10 +296,11 @@ print_info_load_defaults (PrintInformation *res)
  *
  */
 PrintInformation *
-print_info_new (gboolean load_defaults)
+print_info_new (Sheet *sheet, gboolean load_defaults)
 {
 	PrintInformation *res = g_new0 (PrintInformation, 1);
 
+	res->sheet = sheet;
 	res->print_as_draft	   = FALSE;
 	res->comment_placement = PRINT_COMMENTS_IN_PLACE;
 	res->error_display     = PRINT_ERRORS_AS_DISPLAYED;
@@ -817,32 +819,57 @@ print_shutdown (void)
 	destroy_formats ();
 }
 
+#define COPY(field) dst->field = src->field
+
 PrintInformation *
-print_info_dup (PrintInformation *src)
+print_info_dup (PrintInformation const *src, Sheet *new_sheet)
 {
-	PrintInformation *dst = print_info_new (TRUE);
+	PrintInformation *dst = print_info_new (new_sheet, TRUE);
+
+	print_info_load_defaults ((PrintInformation *)src);
+
+	/* Don't copy sheet */
+	COPY(scaling);
+	COPY(edge_to_below_header);
+	COPY(edge_to_above_footer);
+        COPY(desired_display);
+	COPY(repeat_left);
+	COPY(print_across_then_down);
+	COPY(center_vertically);
+	COPY(center_horizontally);
+	COPY(print_grid_lines);
+	COPY(print_titles);
+	COPY(print_black_and_white);
+	COPY(print_as_draft);
+	COPY(print_even_if_only_styles);
+	COPY(do_not_print);
+	COPY(comment_placement);
+	COPY(error_display);
+
+	gnm_page_breaks_free (dst->page_breaks.h);
+	dst->page_breaks.h = gnm_page_breaks_dup (src->page_breaks.h);
 
-	print_info_load_defaults (src);
+	gnm_page_breaks_free (dst->page_breaks.v);
+	dst->page_breaks.v = gnm_page_breaks_dup (src->page_breaks.v);
 
-	/* clear the refs in the new obj */
 	print_hf_free (dst->header);
+	dst->header = print_hf_copy (src->header);
+
 	print_hf_free (dst->footer);
-	if (dst->page_setup)
-		g_object_unref (dst->page_setup);
+	dst->footer = print_hf_copy (src->footer);
 
-	*dst = *src; /* bit bash */
+	COPY(start_page);
+        COPY(n_copies);
 
-	dst->page_breaks.v = gnm_page_breaks_dup (src->page_breaks.v);
-	dst->page_breaks.h = gnm_page_breaks_dup (src->page_breaks.h);
-
-	/* setup the refs for new content */
-	dst->header	   = print_hf_copy (src->header);
-	dst->footer	   = print_hf_copy (src->footer);
-	dst->page_setup    = gtk_page_setup_copy (src->page_setup);
+	if (dst->page_setup)
+		g_object_unref (dst->page_setup);
+	dst->page_setup = gtk_page_setup_copy (src->page_setup);
 
 	return dst;
 }
 
+#undef COPY
+
 void
 print_info_get_margins (PrintInformation *pi,
 			double *top, double *bottom,
@@ -1189,8 +1216,7 @@ print_info_set_breaks (PrintInformation *pi,
 	if (*target == breaks) /* just in case something silly happens */
 		return;
 
-	if (NULL != *target)
-		gnm_page_breaks_free (*target);
+	gnm_page_breaks_free (*target);
 	*target = breaks;
 }
 
@@ -1200,23 +1226,17 @@ print_info_set_breaks (PrintInformation *pi,
 
 /**
  * gnm_page_breaks_new :
- * @len : optional estimate of number of breaks that will be in the collection
  * @is_vert :
  *
- * Allocate a collection of page breaks and provide some sanity checking for
- * @len.
+ * Allocate a collection of page breaks.
  **/
 GnmPageBreaks *
-gnm_page_breaks_new (int len, gboolean is_vert)
+gnm_page_breaks_new (gboolean is_vert)
 {
 	GnmPageBreaks *res = g_new (GnmPageBreaks, 1);
 
-#warning "We cannot use NULL here."
-	if (len < 0 || len > colrow_max (is_vert, NULL))
-		len = 0;
 	res->is_vert = is_vert;
-	res->details = g_array_sized_new (FALSE, FALSE,
-		sizeof (GnmPageBreak), len);
+	res->details = g_array_new (FALSE, FALSE, sizeof (GnmPageBreak));
 
 	return res;
 }
@@ -1225,7 +1245,7 @@ GnmPageBreaks *
 gnm_page_breaks_dup (GnmPageBreaks const *src)
 {
 	if (src != NULL) {
-		GnmPageBreaks *dst = gnm_page_breaks_new (src->details->len, src->is_vert);
+		GnmPageBreaks *dst = gnm_page_breaks_new (src->is_vert);
 		GArray       *d_details = dst->details;
 		GArray const *s_details = src->details;
 		unsigned i;
@@ -1243,8 +1263,10 @@ gnm_page_breaks_dup (GnmPageBreaks const *src)
 void
 gnm_page_breaks_free (GnmPageBreaks *breaks)
 {
-	g_array_free (breaks->details, TRUE);
-	g_free (breaks);
+	if (breaks) {
+		g_array_free (breaks->details, TRUE);
+		g_free (breaks);
+	}
 }
 
 gboolean
diff --git a/src/print-info.h b/src/print-info.h
index 95220ab..a72ba16 100644
--- a/src/print-info.h
+++ b/src/print-info.h
@@ -41,6 +41,7 @@ typedef struct {
 } GnmPageBreaks;
 
 struct _PrintInformation {
+	Sheet *sheet;
 	struct {
 		enum {
 			PRINT_SCALE_PERCENTAGE,
@@ -116,9 +117,9 @@ typedef struct {
 	GnmCellPos top_repeating;
 } HFRenderInfo;
 
-PrintInformation *print_info_new         (gboolean load_defaults);
+PrintInformation *print_info_new         (Sheet *sheet, gboolean load_defaults);
 PrintInformation *print_info_load_defaults (PrintInformation *pi);
-PrintInformation *print_info_dup	 (PrintInformation *pi);
+PrintInformation *print_info_dup	 (PrintInformation const *pi, Sheet *new_sheet);
 void              print_info_free        (PrintInformation *pi);
 void              print_info_save        (PrintInformation *pi);
 
@@ -177,7 +178,7 @@ void        print_info_set_edge_to_below_header (PrintInformation *pi,
 						 double e_h);
 void        print_info_set_breaks (PrintInformation *pi, GnmPageBreaks *breaks);
 
-GnmPageBreaks	*gnm_page_breaks_new		(int len, gboolean is_vert);
+GnmPageBreaks	*gnm_page_breaks_new		(gboolean is_vert);
 GnmPageBreaks	*gnm_page_breaks_dup		(GnmPageBreaks const *src);
 void		 gnm_page_breaks_free		(GnmPageBreaks *breaks);
 gboolean	 gnm_page_breaks_append_break	(GnmPageBreaks *breaks,
diff --git a/src/sheet.c b/src/sheet.c
index 0f2aa54..af99333 100644
--- a/src/sheet.c
+++ b/src/sheet.c
@@ -748,7 +748,7 @@ gnm_sheet_init (Sheet *sheet)
 	sheet->rows.info = g_ptr_array_new ();
 	sheet_row_set_default_size_pts (sheet, 12.75);
 
-	sheet->print_info = print_info_new (FALSE);
+	sheet->print_info = print_info_new (sheet, FALSE);
 
 	sheet->filters = NULL;
 	sheet->scenarios = NULL;
@@ -5248,7 +5248,7 @@ sheet_dup (Sheet const *src)
 		NULL);
 
 	print_info_free (dst->print_info);
-	dst->print_info = print_info_dup (src->print_info);
+	dst->print_info = print_info_dup (src->print_info, dst);
 
 	sheet_dup_styles         (src, dst);
 	sheet_dup_merged_regions (src, dst);
diff --git a/src/xml-io.c b/src/xml-io.c
index 5f54bb2..3742268 100644
--- a/src/xml-io.c
+++ b/src/xml-io.c
@@ -580,7 +580,7 @@ xml_read_breaks (XmlParseContext *ctxt, xmlNodePtr tree, gboolean is_vert)
 	GnmPageBreakType  type;
 
 	xml_node_get_int (tree, "count", &count);
-	page_breaks = gnm_page_breaks_new (count, is_vert);
+	page_breaks = gnm_page_breaks_new (is_vert);
 
 	for (tree = tree->xmlChildrenNode; tree != NULL ; tree = tree->next)
 		if (!xmlIsBlankNode (tree) &&
diff --git a/src/xml-sax-read.c b/src/xml-sax-read.c
index b365304..76ee36e 100644
--- a/src/xml-sax-read.c
+++ b/src/xml-sax-read.c
@@ -851,15 +851,8 @@ static void
 xml_sax_page_breaks_begin (GsfXMLIn *xin, xmlChar const **attrs)
 {
 	XMLSaxParseState *state = (XMLSaxParseState *)xin->user_state;
-	int count = 0;
-
 	g_return_if_fail (state->page_breaks == NULL);
-
-	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
-		if (gnm_xml_attr_int  (attrs, "count", &count)) ;
-
-	state->page_breaks = gnm_page_breaks_new (count,
-		xin->node->user_data.v_int);
+	state->page_breaks = gnm_page_breaks_new (xin->node->user_data.v_int);
 }
 
 static void



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