From 63160f896e887e1308e3b973b0e841ec3645c06f Mon Sep 17 00:00:00 2001
From: Andrea Zagli <azagli@libero.it>
Date: Sat, 16 Jul 2011 09:09:41 +0200
Subject: [PATCH] Added and managed struct RptMargin.

---
 src/rptcommon.c        |  83 ++++++++++++++++++++++++++++++++-
 src/rptcommon.h        |  16 ++++++-
 src/rptreport.c        | 102 +++++++++++++++++++++++------------------
 src/rptreport.h        |   5 +-
 tests/test_report.rpt  |  45 ++++--------------
 tests/test_rptreport.c |   6 +--
 6 files changed, 171 insertions(+), 86 deletions(-)

diff --git a/src/rptcommon.c b/src/rptcommon.c
index e9a764e..81a8e07 100644
--- a/src/rptcommon.c
+++ b/src/rptcommon.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2007-2011 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -292,6 +292,8 @@ RptPoint
 	RptPoint *point;
 
 	point = (RptPoint *)g_malloc0 (sizeof (RptPoint));
+	point->x = 0.0;
+	point->y = 0.0;
 
 	return point;
 }
@@ -350,6 +352,8 @@ RptSize
 	RptSize *size;
 
 	size = (RptSize *)g_malloc0 (sizeof (RptSize));
+	size->width = 0.0;
+	size->height = 0.0;
 
 	return size;
 }
@@ -407,6 +411,7 @@ RptRotation
 	RptRotation *rotation;
 
 	rotation = (RptRotation *)g_malloc0 (sizeof (RptRotation));
+	rotation->angle = 0.0;
 
 	return rotation;
 }
@@ -449,6 +454,82 @@ rpt_common_set_rotation (xmlNode *xnode, const RptRotation *rotation)
 		}
 }
 
+/**
+ * rpt_common_rptmargin_new:
+ *
+ * Returns: an new allocated #RptMargin struct.
+ */
+RptMargin
+*rpt_common_rptmargin_new (void)
+{
+	RptMargin *margin;
+
+	margin = (RptMargin *)g_malloc0 (sizeof (RptMargin));
+	margin->top = 0.0;
+	margin->right = 0.0;
+	margin->bottom = 0.0;
+	margin->left = 0.0;
+
+	return margin;
+}
+
+/**
+ * rpt_common_get_margin:
+ * @xnode: an #xmlNode.
+ *
+ * Returns: an #RptMargin struct that represent the page's margin specified
+ * on @xnode.
+ */
+RptMargin
+*rpt_common_get_margin (xmlNode *xnode)
+{
+	RptMargin *margin = NULL;
+	gchar *prop;
+
+	margin = rpt_common_rptmargin_new ();
+
+	prop = xmlGetProp (xnode, (const xmlChar *)"top");
+	if (prop != NULL)
+		{
+			margin->top = g_strtod (prop, NULL);
+		}
+	prop = xmlGetProp (xnode, (const xmlChar *)"right");
+	if (prop != NULL)
+		{
+			margin->right = g_strtod (prop, NULL);
+		}
+	prop = xmlGetProp (xnode, (const xmlChar *)"bottom");
+	if (prop != NULL)
+		{
+			margin->bottom = g_strtod (prop, NULL);
+		}
+	prop = xmlGetProp (xnode, (const xmlChar *)"left");
+	if (prop != NULL)
+		{
+			margin->left = g_strtod (prop, NULL);
+		}
+
+	return margin;
+}
+
+/**
+ * rpt_common_set_margin:
+ * @xnode: an #xmlNode.
+ * @margin:
+ *
+ */
+void
+rpt_common_set_margin (xmlNode *xnode, const RptMargin *margin)
+{
+	if (margin != NULL)
+		{
+			xmlSetProp (xnode, "top", g_strdup_printf ("%f", margin->top));
+			xmlSetProp (xnode, "right", g_strdup_printf ("%f", margin->right));
+			xmlSetProp (xnode, "bottom", g_strdup_printf ("%f", margin->bottom));
+			xmlSetProp (xnode, "left", g_strdup_printf ("%f", margin->left));
+		}
+}
+
 /**
  * rpt_common_rptfont_new:
  *
diff --git a/src/rptcommon.h b/src/rptcommon.h
index 3b27c69..ad1d813 100644
--- a/src/rptcommon.h
+++ b/src/rptcommon.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2007-2011 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -84,6 +84,15 @@ struct _RptRotation
 };
 typedef struct _RptRotation RptRotation;
 
+struct _RptMargin
+{
+	gdouble top;
+	gdouble right;
+	gdouble bottom;
+	gdouble left;
+};
+typedef struct _RptMargin RptMargin;
+
 /**
  * RptFont:
  * @name: the font's family name.
@@ -199,6 +208,11 @@ RptRotation *rpt_common_get_rotation (xmlNode *xnode);
 void rpt_common_set_rotation (xmlNode *xnode,
                               const RptRotation *rotation);
 
+RptMargin *rpt_common_rptmargin_new (void);
+RptMargin *rpt_common_get_margin (xmlNode *xnode);
+void rpt_common_set_margin (xmlNode *xnode,
+                            const RptMargin *margin);
+
 RptFont *rpt_common_rptfont_new (void);
 RptFont *rpt_common_get_font (xmlNode *xnode);
 void rpt_common_set_font (xmlNode *xnode,
diff --git a/src/rptreport.c b/src/rptreport.c
index 25a9cbb..9f64786 100644
--- a/src/rptreport.c
+++ b/src/rptreport.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2010 Andrea Zagli <azagli@inwind.it>
+ * Copyright (C) 2007-2011 Andrea Zagli <azagli@inwind.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -54,10 +54,7 @@ typedef struct
 typedef struct
 {
 	RptSize *size;
-	gdouble margin_top;
-	gdouble margin_right;
-	gdouble margin_bottom;
-	gdouble margin_left;
+	RptMargin *margin;
 } Page;
 
 typedef struct
@@ -243,9 +240,9 @@ rpt_report_init (RptReport *rpt_report)
 	priv->db = NULL;
 
 	priv->page = (Page *)g_malloc0 (sizeof (Page));
-	priv->page->size = (RptSize *)g_malloc0 (sizeof (RptSize));
-	priv->page->size->width = 0.0;
-	priv->page->size->height = 0.0;
+
+	priv->page->size = rpt_common_rptsize_new ();
+	priv->page->margin = rpt_common_rptmargin_new ();
 
 	priv->report_header = NULL;
 	priv->report_footer = NULL;
@@ -770,10 +767,10 @@ rpt_report_set_page_margins (RptReport *rpt_report,
 {
 	RptReportPrivate *priv = RPT_REPORT_GET_PRIVATE (rpt_report);
 
-	priv->page->margin_top = top;
-	priv->page->margin_right = right;
-	priv->page->margin_bottom = bottom;
-	priv->page->margin_left = left;
+	priv->page->margin->top = top;
+	priv->page->margin->right = right;
+	priv->page->margin->bottom = bottom;
+	priv->page->margin->left = left;
 }
 
 /**
@@ -794,10 +791,27 @@ rpt_report_get_page_margins (RptReport *rpt_report,
 {
 	RptReportPrivate *priv = RPT_REPORT_GET_PRIVATE (rpt_report);
 
-	*top = priv->page->margin_top;
-	*right = priv->page->margin_right;
-	*bottom = priv->page->margin_bottom;
-	*left = priv->page->margin_left;
+	*top = priv->page->margin->top;
+	*right = priv->page->margin->right;
+	*bottom = priv->page->margin->bottom;
+	*left = priv->page->margin->left;
+}
+
+RptMargin
+*rpt_report_get_page_margins_struct (RptReport *rpt_report)
+{
+	RptReportPrivate *priv = RPT_REPORT_GET_PRIVATE (rpt_report);
+
+	return g_memdup (priv->page->margin, sizeof (RptMargin));
+}
+
+void
+rpt_report_set_page_margins_struct (RptReport *rpt_report, RptMargin margin)
+{
+	RptReportPrivate *priv = RPT_REPORT_GET_PRIVATE (rpt_report);
+
+	g_free (priv->page->margin);
+	priv->page->margin = g_memdup (&margin, sizeof (RptMargin));
 }
 
 /**
@@ -1233,21 +1247,21 @@ xmlDoc
 
 	xnode = xmlNewNode (NULL, "page");
 	rpt_common_set_size (xnode, priv->page->size);
-	if (priv->page->margin_top != 0.0)
+	if (priv->page->margin->top != 0.0)
 		{
-			xmlSetProp (xnode, "margin-top", g_strdup_printf ("%f", priv->page->margin_top));
+			xmlSetProp (xnode, "margin-top", g_strdup_printf ("%f", priv->page->margin->top));
 		}
-	if (priv->page->margin_right != 0.0)
+	if (priv->page->margin->right != 0.0)
 		{
-			xmlSetProp (xnode, "margin-right", g_strdup_printf ("%f", priv->page->margin_right));
+			xmlSetProp (xnode, "margin-right", g_strdup_printf ("%f", priv->page->margin->right));
 		}
-	if (priv->page->margin_bottom != 0.0)
+	if (priv->page->margin->bottom != 0.0)
 		{
-			xmlSetProp (xnode, "margin-bottom", g_strdup_printf ("%f", priv->page->margin_bottom));
+			xmlSetProp (xnode, "margin-bottom", g_strdup_printf ("%f", priv->page->margin->bottom));
 		}
-	if (priv->page->margin_left != 0.0)
+	if (priv->page->margin->left != 0.0)
 		{
-			xmlSetProp (xnode, "margin-left", g_strdup_printf ("%f", priv->page->margin_left));
+			xmlSetProp (xnode, "margin-left", g_strdup_printf ("%f", priv->page->margin->left));
 		}
 	xmlAddChild (xroot, xnode);
 
@@ -1371,20 +1385,20 @@ xmlDoc
 				{
 					if (row == 0 ||
 					    priv->body->new_page_after ||
-					    (priv->page_footer != NULL && (cur_y + priv->body->height > priv->page->size->height - priv->page->margin_bottom - priv->page_footer->height)) ||
-					    cur_y > (priv->page->size->height - priv->page->margin_bottom))
+					    (priv->page_footer != NULL && (cur_y + priv->body->height > priv->page->size->height - priv->page->margin->bottom - priv->page_footer->height)) ||
+					    cur_y > (priv->page->size->height - priv->page->margin->bottom))
 						{
 							if (priv->cur_page > 0 && priv->page_footer != NULL)
 								{
 									if ((priv->cur_page == 1 && priv->page_footer->first_page) ||
 									    priv->cur_page > 1)
 										{
-											cur_y = priv->page->size->height - priv->page->margin_bottom - priv->page_footer->height;
+											cur_y = priv->page->size->height - priv->page->margin->bottom - priv->page_footer->height;
 											rpt_report_rptprint_section (rpt_report, xpage, &cur_y, RPTREPORT_SECTION_PAGE_FOOTER, row - 1);
 										}
 								}
 
-							cur_y = priv->page->margin_top;
+							cur_y = priv->page->margin->top;
 							xpage = rpt_report_rptprint_new_page (rpt_report, xroot);
 
 							if (priv->page_header != NULL)
@@ -1411,7 +1425,7 @@ xmlDoc
 
 			if (priv->cur_page > 0 && priv->report_footer != NULL)
 				{
-					if ((cur_y + priv->report_footer->height > priv->page->size->height - priv->page->margin_bottom - (priv->page_footer != NULL ? priv->page_footer->height : 0.0)) ||
+					if ((cur_y + priv->report_footer->height > priv->page->size->height - priv->page->margin->bottom - (priv->page_footer != NULL ? priv->page_footer->height : 0.0)) ||
 					    priv->report_footer->new_page_before)
 						{
 							if (priv->page_header != NULL)
@@ -1419,12 +1433,12 @@ xmlDoc
 									rpt_report_rptprint_section (rpt_report, xpage, &cur_y, RPTREPORT_SECTION_PAGE_HEADER, row - 1);
 								}
 
-							cur_y = priv->page->margin_top;
+							cur_y = priv->page->margin->top;
 							xpage = rpt_report_rptprint_new_page (rpt_report, xroot);
 
 							if (priv->cur_page > 0 && priv->page_footer != NULL)
 								{
-									cur_y = priv->page->size->height - priv->page->margin_bottom - priv->page_footer->height;
+									cur_y = priv->page->size->height - priv->page->margin->bottom - priv->page_footer->height;
 									rpt_report_rptprint_section (rpt_report, xpage, &cur_y, RPTREPORT_SECTION_PAGE_FOOTER, row - 1);
 								}
 						}
@@ -1432,7 +1446,7 @@ xmlDoc
 				}
 			if (priv->cur_page > 0 && priv->page_footer != NULL && priv->page_footer->last_page)
 				{
-					cur_y = priv->page->size->height - priv->page->margin_bottom - priv->page_footer->height;
+					cur_y = priv->page->size->height - priv->page->margin->bottom - priv->page_footer->height;
 					rpt_report_rptprint_section (rpt_report, xpage, &cur_y, RPTREPORT_SECTION_PAGE_FOOTER, row - 1);
 				}
 
@@ -1441,7 +1455,7 @@ xmlDoc
 		}
 	else
 		{
-			cur_y = priv->page->margin_top;
+			cur_y = priv->page->margin->top;
 			xpage = rpt_report_rptprint_new_page (rpt_report, xroot);
 
 			if (priv->page_header != NULL)
@@ -1461,7 +1475,7 @@ xmlDoc
 				}
 			if (priv->page_footer != NULL)
 				{
-					cur_y = priv->page->size->height - priv->page->margin_bottom - priv->page_footer->height;
+					cur_y = priv->page->size->height - priv->page->margin->bottom - priv->page_footer->height;
 					rpt_report_rptprint_section (rpt_report, xpage, &cur_y, RPTREPORT_SECTION_PAGE_FOOTER, -1);
 				}
 		}
@@ -2064,21 +2078,21 @@ static xmlNode
 	xmlAddChild (xroot, xnode);
 
 	rpt_common_set_size (xnode, priv->page->size);
-	if (priv->page->margin_top != 0.0)
+	if (priv->page->margin->top != 0.0)
 		{
-			xmlSetProp (xnode, "margin-top", g_strdup_printf ("%f", priv->page->margin_top));
+			xmlSetProp (xnode, "margin-top", g_strdup_printf ("%f", priv->page->margin->top));
 		}
-	if (priv->page->margin_right != 0.0)
+	if (priv->page->margin->right != 0.0)
 		{
-			xmlSetProp (xnode, "margin-right", g_strdup_printf ("%f", priv->page->margin_right));
+			xmlSetProp (xnode, "margin-right", g_strdup_printf ("%f", priv->page->margin->right));
 		}
-	if (priv->page->margin_bottom != 0.0)
+	if (priv->page->margin->bottom != 0.0)
 		{
-			xmlSetProp (xnode, "margin-bottom", g_strdup_printf ("%f", priv->page->margin_bottom));
+			xmlSetProp (xnode, "margin-bottom", g_strdup_printf ("%f", priv->page->margin->bottom));
 		}
-	if (priv->page->margin_left != 0.0)
+	if (priv->page->margin->left != 0.0)
 		{
-			xmlSetProp (xnode, "margin-left", g_strdup_printf ("%f", priv->page->margin_left));
+			xmlSetProp (xnode, "margin-left", g_strdup_printf ("%f", priv->page->margin->left));
 		}
 
 	priv->cur_page++;
@@ -2133,14 +2147,14 @@ rpt_report_rptprint_section (RptReport *rpt_report, xmlNode *xpage, gdouble *cur
 					xmlRemoveProp (attr);
 				}
 
-			if (priv->page->margin_left != 0.0)
+			if (priv->page->margin->left != 0.0)
 				{
 					prop = (gchar *)xmlGetProp (xnode, "x");
 					if (prop == NULL)
 						{
 							prop = g_strdup ("0.0");
 						}
-					xmlSetProp (xnode, "x", g_strdup_printf ("%f", strtod (prop, NULL) + priv->page->margin_left));
+					xmlSetProp (xnode, "x", g_strdup_printf ("%f", strtod (prop, NULL) + priv->page->margin->left));
 				}
 			
 			prop = (gchar *)xmlGetProp (xnode, "y");
diff --git a/src/rptreport.h b/src/rptreport.h
index 9a81511..f8bdca3 100644
--- a/src/rptreport.h
+++ b/src/rptreport.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2010 Andrea Zagli <azagli@inwind.it>
+ * Copyright (C) 2007-2011 Andrea Zagli <azagli@inwind.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -100,6 +100,9 @@ void rpt_report_set_page_margins (RptReport *rpt_report,
                                   gdouble bottom,
                                   gdouble left);
 
+RptMargin *rpt_report_get_page_margins_struct (RptReport *rpt_report);
+void rpt_report_set_page_margins_struct (RptReport *rpt_report, RptMargin margin);
+
 gdouble rpt_report_get_section_height (RptReport *rpt_report,
                                        RptReportSection section);
 void rpt_report_set_section_height (RptReport *rpt_report,
diff --git a/tests/test_report.rpt b/tests/test_report.rpt
index ce485ba..76ba242 100644
--- a/tests/test_report.rpt
+++ b/tests/test_report.rpt
@@ -1,42 +1,15 @@
 <?xml version="1.0"?>
 <reptool>
-  <database>
-    <provider>
-SQLite
-    </provider>
-    <connection-string>
-DB_DIR=.;DB_NAME=db_test.db
-    </connection-string>
-    <sql>
-SELECT * FROM articles ORDER BY name
-    </sql>
-  </database>
-  <page width="595.000000" height="842.000000"/>
+  <properties>
+    <unit-length>pt</unit-length>
+    <output-type>df</output-type>
+    <output-filename>rptreport.pdf</output-filename>
+    <copies>1</copies>
+  </properties>
+  <page width="595.000000" height="842.000000" margin-top="200.000000" margin-left="100.000000"/>
   <report>
-    <report-header height="80.000000">
-      <text name="rephead" x="10.000000" y="10.000000" width="300.000000" height="70.000000" font-name="Verdana" font-size="16.000000" font-bold="y" font-color="#0000FFFF" source="&quot;the report's header&quot;"/>
-    </report-header>
-    <page-header first-page="y" height="80.000000">
-      <text name="title" x="10.000000" y="10.000000" width="300.000000" height="50.000000" font-name="Courier New" font-size="10.000000" font-bold="y" source="&quot;the page's title&quot; &amp; &quot; - &quot; &amp; @Page"/>
-      <line name="line1" x="10.000000" y="65.000000" width="500.000000" height="0.000000" stroke-width="1.000000" stroke-color="#000000FF"/>
-    </page-header>
-    <body height="200.000000">
-      <text name="txt_id" x="50.000000" y="50.000000" width="100.000000" height="50.000000" border-top-width="1.000000" border-top-color="#FF0000FF" font-name="Sans" font-size="12.000000" source="[id]"/>
-      <text name="txt_name" x="200.000000" y="50.000000" width="100.000000" height="50.000000" font-name="Sans" font-size="12.000000" source="[name]"/>
-      <text name="txt_req" x="50.000000" y="100.000000" width="100.000000" height="50.000000" font-name="Sans" font-size="12.000000" source="[nonexistent]"/>
-      <rect name="rect1" x="400.000000" y="10.000000" width="20.000000" height="20.000000" stroke-width="1.000000" stroke-color="#000000FF" fill-color="#00FF00FF"/>
-      <image name="img1" x="450.000000" y="10.000000" width="60.000000" height="60.000000" border-bottom-width="1.000000" border-bottom-color="#FF0000FF" border-bottom-style="10.000000;10.000000;" source="tests/gnome-globe.png"/>
+    <body height="0.000000">
+      <text name="txt_id" x="50.000000" y="50.000000" width="100.000000" height="50.000000" border-top-width="1.000000" border-top-color="#FF0000FF" font-name="Sans" font-size="12.000000" source="&quot;text with&#10;new line&quot;"/>
     </body>
-    <report-footer height="50.000000">
-      <line name="line3" x="10.000000" y="10.000000" width="500.000000" height="0.000000" stroke-width="1.000000" stroke-color="#FFFF00FF" stroke-style="50.000000;10.000000;"/>
-      <text name="txt_report_footer" x="10.000000" y="20.000000" width="500.000000" height="30.000000" font-name="Sans" font-size="12.000000" horizontal-align="center" source="&quot;the report's footer&quot;"/>
-      <line name="line4" x="10.000000" y="50.000000" width="500.000000" height="0.000000" stroke-width="1.000000" stroke-color="#FFFF00FF" stroke-style="1.000000;5.000000;"/>
-    </report-footer>
-    <page-footer first-page="y" last-page="y" height="80.000000">
-      <line name="line2" x="10.000000" y="10.000000" width="500.000000" height="0.000000" stroke-width="1.000000" stroke-color="#000000FF"/>
-      <text name="footer" x="10.000000" y="20.000000" width="300.000000" height="50.000000" font-name="Sans" font-size="12.000000" source="&quot;the page's footer&quot;"/>
-      <ellipse name="ellipse1" x="400.000000" y="50.000000" width="20.000000" height="10.000000" stroke-width="1.000000" stroke-color="#FF0000FF" fill-color="#00FF00FF"/>
-      <text name="page_n" x="500.000000" y="20.000000" width="50.000000" height="50.000000" font-name="Sans" font-size="12.000000" source="@Page &amp; &quot;/&quot; &amp; @Pages"/>
-    </page-footer>
   </report>
 </reptool>
diff --git a/tests/test_rptreport.c b/tests/test_rptreport.c
index 23ccacf..c724906 100644
--- a/tests/test_rptreport.c
+++ b/tests/test_rptreport.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2010 Andrea Zagli <azagli@inwind.it>
+ * Copyright (C) 2007-2011 Andrea Zagli <azagli@inwind.it>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,11 +28,11 @@ gchar
 {
 	gchar *ret = NULL;
 
-	if (strcmp (field_name, "field_to_request") == 0)
+	if (g_strcmp0 (field_name, "field_to_request") == 0)
 		{
 			ret = g_strdup ("the field requested");
 		}
-	else if (strcmp (field_name, "nonexistent") == 0 &&
+	else if (g_strcmp0 (field_name, "nonexistent") == 0 &&
 	         data_model != NULL &&
 	         row > -1)
 		{
-- 
2.49.0