commit 37b839a2d3be8bacf160df237e673d33b1689204
Author: Vibha Yadav <yvibha@novell.com>
Date:   Fri May 7 11:30:49 2010 +0530

    Bug #545462 - Printing of contacts is weird.
    
    Lot of improvements in contact printing. A few more to follow suit.

diff --git a/addressbook/gui/widgets/e-addressbook-view.c b/addressbook/gui/widgets/e-addressbook-view.c
index 01605f3..c4d9ad2 100644
--- a/addressbook/gui/widgets/e-addressbook-view.c
+++ b/addressbook/gui/widgets/e-addressbook-view.c
@@ -1152,11 +1152,12 @@ contact_print_button_draw_page (GtkPrintOperation *operation,
                                 EPrintable *printable)
 {
 	GtkPageSetup *setup;
-	gdouble top_margin;
+	gdouble top_margin, page_width;
 	cairo_t *cr;
 
 	setup = gtk_print_context_get_page_setup (context);
 	top_margin = gtk_page_setup_get_top_margin (setup, GTK_UNIT_POINTS);
+	page_width = gtk_page_setup_get_page_width (setup, GTK_UNIT_POINTS);
 
 	cr = gtk_print_context_get_cairo_context (context);
 
@@ -1164,8 +1165,9 @@ contact_print_button_draw_page (GtkPrintOperation *operation,
 
 	while (e_printable_data_left (printable)) {
 		cairo_save (cr);
+		contact_page_draw_footer(operation,context,page_nr++);
 		e_printable_print_page (
-			printable, context, 6.5 * 72, top_margin + 10, TRUE);
+			printable, context, page_width - 16, top_margin + 10, TRUE);
 		cairo_restore (cr);
 	}
 }
diff --git a/addressbook/printing/e-contact-print.c b/addressbook/printing/e-contact-print.c
index 93b76bf..96c9706 100644
--- a/addressbook/printing/e-contact-print.c
+++ b/addressbook/printing/e-contact-print.c
@@ -121,6 +121,7 @@ e_contact_output (GtkPrintContext *context,
 	pango_layout_set_text (layout, text, -1);
 	pango_layout_set_width (layout, pango_units_from_double (width));
 	pango_layout_set_indent (layout, pango_units_from_double (indent));
+	pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
 
 	cr = gtk_print_context_get_cairo_context (context);
 
@@ -217,12 +218,78 @@ e_contact_start_new_page (EContactPrintContext *ctxt)
 }
 
 static void
+e_contact_start_new_column (EContactPrintContext *ctxt)
+{
+	if (++ctxt->column >= ctxt->style->num_columns)
+		e_contact_start_new_page (ctxt);
+	else {
+		ctxt->x = ctxt->column *
+			(ctxt->column_width + ctxt->column_spacing);
+		ctxt->y = .0;
+	}
+}
+
+static gdouble
+e_contact_get_contact_height (EContact *contact, EContactPrintContext *ctxt)
+{
+	GtkPageSetup *setup;
+	gchar *file_as;
+	gdouble page_height;
+	gint field;
+	gdouble cntct_height = 0.0;
+
+	setup = gtk_print_context_get_page_setup (ctxt->context);
+	page_height = gtk_page_setup_get_page_height (setup, GTK_UNIT_POINTS);
+
+	cntct_height += get_font_height (ctxt->style->headings_font) * .2;
+
+	file_as = e_contact_get (contact, E_CONTACT_FILE_AS);
+
+	cntct_height += e_contact_text_height (
+		ctxt->context, ctxt->style->headings_font, file_as);
+
+	g_free (file_as);
+
+	cntct_height += get_font_height (ctxt->style->headings_font) * .2;
+
+	for (field = E_CONTACT_FILE_AS; field != E_CONTACT_LAST_SIMPLE_STRING; field++)
+	{
+		const gchar *value;
+		gchar *text;
+
+		value = e_contact_get_const (contact, field);
+		if (value == NULL || *value == '\0')
+			continue;
+
+		text = g_strdup_printf ("%s:  %s",
+			e_contact_pretty_name (field), value);
+
+		cntct_height += e_contact_text_height (
+			ctxt->context, ctxt->style->body_font, text);
+
+		cntct_height += .2 * get_font_height (ctxt->style->body_font);
+
+		g_free (text);
+	}
+
+	cntct_height += get_font_height (ctxt->style->headings_font) * .4 + 8;
+
+	return cntct_height;
+}
+
+
+static void
 e_contact_print_contact (EContact *contact, EContactPrintContext *ctxt)
 {
+	GtkPageSetup *setup;
 	gchar *file_as;
 	cairo_t *cr;
+	gdouble page_height;
 	gint field;
 
+	setup = gtk_print_context_get_page_setup (ctxt->context);
+	page_height = gtk_page_setup_get_page_height (setup, GTK_UNIT_POINTS);
+
 	cr = gtk_print_context_get_cairo_context (ctxt->context);
 	cairo_save(cr);
 	ctxt->y += get_font_height (ctxt->style->headings_font) * .2;
@@ -254,6 +321,10 @@ e_contact_print_contact (EContact *contact, EContactPrintContext *ctxt)
 	{
 		const gchar *value;
 		gchar *text;
+		gint wrapped_lines=0;
+
+		if (ctxt->y > page_height)
+			e_contact_start_new_column (ctxt);
 
 		value = e_contact_get_const (contact, field);
 		if (value == NULL || *value == '\0')
@@ -265,10 +336,12 @@ e_contact_print_contact (EContact *contact, EContactPrintContext *ctxt)
 		if (ctxt->pages == ctxt->page_nr)
 			e_contact_output (
 				ctxt->context, ctxt->style->body_font,
-				ctxt->x, ctxt->y, -1, text);
+				ctxt->x, ctxt->y, ctxt->column_width + 4, text);
 
-		ctxt->y += e_contact_text_height (
-			ctxt->context, ctxt->style->body_font, text);
+		if ( get_font_width (ctxt->context, ctxt->style->body_font, text) > ctxt->column_width)
+			wrapped_lines =  ( get_font_width (ctxt->context, ctxt->style->body_font, text) / (ctxt->column_width+4)) + 1;
+		ctxt->y = ctxt->y + ((wrapped_lines+1) *e_contact_text_height (
+			ctxt->context, ctxt->style->body_font, text));
 
 		ctxt->y += .2 * get_font_height (ctxt->style->body_font);
 
@@ -280,17 +353,7 @@ e_contact_print_contact (EContact *contact, EContactPrintContext *ctxt)
 	cairo_restore (cr);
 }
 
-static void
-e_contact_start_new_column (EContactPrintContext *ctxt)
-{
-	if (++ctxt->column >= ctxt->style->num_columns)
-		e_contact_start_new_page (ctxt);
-	else {
-		ctxt->x = ctxt->column *
-			(ctxt->column_width + ctxt->column_spacing);
-		ctxt->y = .0;
-	}
-}
+
 
 static gint
 contact_compare (EContact *contact1, EContact *contact2)
@@ -580,7 +643,7 @@ contact_draw (EContact *contact, EContactPrintContext *ctxt)
 		if (!ctxt->first_contact) {
 			if (ctxt->style->sections_start_new_page)
 				e_contact_start_new_page (ctxt);
-			else if (ctxt->y > page_height)
+			else if ((ctxt->y + e_contact_get_contact_height (contact, ctxt)) > page_height)
 				e_contact_start_new_column (ctxt);
 		}
 		if (ctxt->style->letter_headings)
@@ -588,7 +651,7 @@ contact_draw (EContact *contact, EContactPrintContext *ctxt)
 		ctxt->first_section = FALSE;
 	}
 
-	else if (!ctxt->first_contact && (ctxt->y > page_height)) {
+	else if (!ctxt->first_contact && (( ctxt->y + e_contact_get_contact_height (contact, ctxt)) > page_height)) {
 		e_contact_start_new_column (ctxt);
 		if (ctxt->style->letter_headings)
 			e_contact_print_letter_heading (ctxt, ctxt->section);
@@ -649,6 +712,55 @@ contact_begin_print (GtkPrintOperation *operation,
 	}
 }
 
+/* contact_page_draw_footer inserts the 
+ * page number at the end of each page 
+ * while printing*/
+void
+contact_page_draw_footer (GtkPrintOperation *operation,
+			  GtkPrintContext *context,
+			  gint page_nr)
+{
+	PangoFontDescription *desc;
+	PangoLayout *layout;
+	gdouble x, y, page_height, page_width, page_margin;
+	gint n_pages;
+	gchar *text;
+	cairo_t *cr;
+	GtkPageSetup *setup;
+	
+	/*Uncomment next if it is successful to get total number if pages in list view
+	 * g_object_get (operation, "n-pages", &n_pages, NULL)*/
+	text = g_strdup_printf (_("Page %d"), page_nr + 1);
+
+	setup = gtk_print_context_get_page_setup ( context);
+	page_height = gtk_page_setup_get_page_height (setup, GTK_UNIT_POINTS);
+	page_width = gtk_page_setup_get_page_width (setup, GTK_UNIT_POINTS);
+	page_margin = gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_POINTS);
+
+	desc = pango_font_description_from_string ("Sans Regular 8");
+	layout = gtk_print_context_create_pango_layout (context);
+	pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
+	pango_layout_set_font_description (layout, desc);
+	pango_layout_set_text (layout, text, -1);
+	pango_layout_set_width (layout, -1);
+
+	x = page_width/2.0 - page_margin;
+	y = page_height - page_margin/2.0;
+
+	cr = gtk_print_context_get_cairo_context (context);
+
+	cairo_save (cr);
+	cairo_set_source_rgb (cr, .0, .0, .0);
+	cairo_move_to (cr, x, y);
+	pango_cairo_show_layout (cr, layout);
+	cairo_restore (cr);
+
+	g_object_unref (layout);
+	pango_font_description_free (desc);
+
+	g_free (text);
+}
+
 static void
 contact_draw_page (GtkPrintOperation *operation,
                    GtkPrintContext *context,
@@ -666,6 +778,7 @@ contact_draw_page (GtkPrintOperation *operation,
 	ctxt->section = NULL;
 
 	g_list_foreach (ctxt->contact_list, (GFunc) contact_draw, ctxt);
+	contact_page_draw_footer (operation, context, page_nr);
 }
 
 static void
diff --git a/addressbook/printing/e-contact-print.h b/addressbook/printing/e-contact-print.h
index 1604693..e16abf2 100644
--- a/addressbook/printing/e-contact-print.h
+++ b/addressbook/printing/e-contact-print.h
@@ -31,5 +31,8 @@ void            e_contact_print               (EBook *book,
 					       EBookQuery *query,
 					       GList *contact_list,
 					       GtkPrintOperationAction action);
+void 		contact_page_draw_footer      (GtkPrintOperation *operation, 
+						GtkPrintContext *context, 
+						gint page_nr);
 
 #endif /* E_CONTACT_PRINT_H */
diff --git a/widgets/table/e-cell-text.c b/widgets/table/e-cell-text.c
index ba24631..06dde0e 100644
--- a/widgets/table/e-cell-text.c
+++ b/widgets/table/e-cell-text.c
@@ -613,7 +613,7 @@ build_layout (ECellTextView *text_view, gint row, const gchar *text, gint width)
 	}
 
 	pango_layout_set_width (layout, width * PANGO_SCALE);
-	pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
+	pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
 
 	pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
 	pango_layout_set_height (layout, 0);
@@ -1356,7 +1356,7 @@ ect_print (ECellView *ecell_view, GtkPrintContext *context,
 
 	cairo_save (cr);
 	layout = gtk_print_context_create_pango_layout (context);
-	font_des = pango_font_description_from_string ("sans 12"); /* fix me font hardcoded */
+	font_des = pango_font_description_from_string ("sans 10"); /* fix me font hardcoded */
 	pango_layout_set_font_description (layout, font_des);
 
 	pango_layout_set_text (layout, string, -1);
@@ -1415,6 +1415,7 @@ ect_print (ECellView *ecell_view, GtkPrintContext *context,
 
 	cairo_move_to(cr, 2, text_height- 5);
 	pango_layout_set_width (layout, (width - 4)*PANGO_SCALE);
+	pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
 	pango_cairo_show_layout(cr, layout);
 	cairo_restore (cr);
 
@@ -1435,7 +1436,28 @@ ect_print_height (ECellView *ecell_view, GtkPrintContext *context,
 	 * Height of some special font is much higher than others,
 	 * such	as Arabic. So leave some more margin for cell.
 	 */
-	return 16 + 8;
+	PangoFontDescription *font_des;
+	PangoLayout *layout;
+	ECellText *ect = E_CELL_TEXT(ecell_view->ecell);
+	ECellTextView *ectView = (ECellTextView *)ecell_view;
+	gchar *string;
+	gdouble text_width = 0.0, text_height = 0.0;
+	gint lines=1;
+
+	string = e_cell_text_get_text(ect, ecell_view->e_table_model, model_col, row);
+
+	layout = gtk_print_context_create_pango_layout (context);
+	font_des = pango_font_description_from_string ("sans 10"); /* fix me font hardcoded */
+	pango_layout_set_font_description (layout, font_des);
+
+	pango_layout_set_text (layout, string, -1);
+	get_font_size (layout, font_des, string, &text_width, &text_height);
+	/* Checking if the text width goes beyond the column width to increase the
+	 * number of lines.
+	 */
+	if ( text_width > width-4)
+		lines = (text_width / (width-4)) + 1;
+	return 16*lines + 8;
 }
 
 static gint
diff --git a/widgets/table/e-table-group-container.c b/widgets/table/e-table-group-container.c
index 72f2323..cf8aae1 100644
--- a/widgets/table/e-table-group-container.c
+++ b/widgets/table/e-table-group-container.c
@@ -1191,7 +1191,9 @@ e_table_group_container_print_page  (EPrintable *ep,
 				     ETGCPrintContext *groupcontext)
 {
 	cairo_t *cr;
+	GtkPageSetup *setup;
 	gdouble yd;
+	gdouble page_height, page_margin;
 	gdouble child_height;
 	ETableGroupContainerChildNode *child_node;
 	GList *child;
@@ -1202,7 +1204,10 @@ e_table_group_container_print_page  (EPrintable *ep,
 
 	child_printable = groupcontext->child_printable;
 	child = groupcontext->child;
-	yd = 6.5 * 72;
+	setup = gtk_print_context_get_page_setup (context);
+	page_height = gtk_page_setup_get_page_height (setup, GTK_UNIT_POINTS);
+	page_margin = gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_POINTS) + gtk_page_setup_get_top_margin (setup, GTK_UNIT_POINTS);
+	yd = page_height - page_margin;
 
 	if (child_printable) {
 		if (child)
@@ -1266,7 +1271,7 @@ e_table_group_container_print_page  (EPrintable *ep,
 		cairo_rectangle (cr, 0, 0, width - 2 * TEXT_AREA_HEIGHT,child_height);
 		cairo_clip(cr);
 
-		e_printable_print_page (child_printable, context, width-2 * TEXT_AREA_HEIGHT, 0, quantize);
+		e_printable_print_page (child_printable, context, width-2 * TEXT_AREA_HEIGHT, height , quantize);
 		yd += child_height + TEXT_AREA_HEIGHT;
 
 		if (e_printable_data_left(child_printable))
