anjuta r4619 - in trunk: . plugins/editor



Author: sgranjoux
Date: Tue Jan 20 21:26:42 2009
New Revision: 4619
URL: http://svn.gnome.org/viewvc/anjuta?rev=4619&view=rev

Log:
	* plugins/editor/print.h,
	plugins/editor/aneditor.cxx,
	plugins/editor/print.c,
	configure.in:
	Fix #564659 â Port anjuta to use Gtk+ print instead of deprecated
	gnomeprint

	* plugins/editor/text_editor.c:
	Fix one debugging error message

	* plugins/editor/Makefile.am:
	Add log domain


Modified:
   trunk/ChangeLog
   trunk/configure.in
   trunk/plugins/editor/Makefile.am
   trunk/plugins/editor/aneditor.cxx
   trunk/plugins/editor/print.c
   trunk/plugins/editor/print.h
   trunk/plugins/editor/text_editor.c

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Tue Jan 20 21:26:42 2009
@@ -158,7 +158,7 @@
 
 PKG_CHECK_MODULES(GLADE, libglade-2.0 >= $LIBGLADE_REQUIRED)
 
-PKG_CHECK_MODULES(GNOME, libgnome-2.0 >= $GNOME_REQUIRED libgnomeui-2.0 >= $GNOME_REQUIRED libgnomecanvas-2.0 >= $GNOME_REQUIRED libgnomeprint-2.2 >= $GNOME_REQUIRED libgnomeprintui-2.2 >= $GNOME_REQUIRED)
+PKG_CHECK_MODULES(GNOME, libgnome-2.0 >= $GNOME_REQUIRED libgnomeui-2.0 >= $GNOME_REQUIRED)
 
 PKG_CHECK_MODULES(GCONF, gconf-2.0 >= $GCONF_REQUIRED)
 

Modified: trunk/plugins/editor/Makefile.am
==============================================================================
--- trunk/plugins/editor/Makefile.am	(original)
+++ trunk/plugins/editor/Makefile.am	Tue Jan 20 21:26:42 2009
@@ -27,6 +27,7 @@
 	$(GTK_CFLAGS) \
 	$(GNOME_CFLAGS) \
 	-I$(srcdir)/scintilla/include
+	-DG_LOG_DOMAIN=\"libanjuta-editor\"
 
 libanjuta_editor_la_LDFLAGS = $(ANJUTA_PLUGIN_LDFLAGS)
 

Modified: trunk/plugins/editor/aneditor.cxx
==============================================================================
--- trunk/plugins/editor/aneditor.cxx	(original)
+++ trunk/plugins/editor/aneditor.cxx	Tue Jan 20 21:26:42 2009
@@ -1781,9 +1781,12 @@
 			if(wParam == lParam) return 0;
 			start = (guint) MINIMUM(wParam, lParam);
 			end = (guint) MAXIMUM(wParam, lParam);
+			/* Allocate a bit more space to allow reading multi
+			 * byte characters more easily */
 			gchar *buff = (gchar*) g_malloc((end-start+10)*2);
 			if(!buff) return 0;
 			GetRange(start, end, buff, true);
+			memset (buff + (end-start) * 2, 0, 20);
 			return (long) buff;
 		}
 		break;

Modified: trunk/plugins/editor/print.c
==============================================================================
--- trunk/plugins/editor/print.c	(original)
+++ trunk/plugins/editor/print.c	Tue Jan 20 21:26:42 2009
@@ -1,8 +1,9 @@
 /*
  * print.c
- * Copyright (C) 2002
- *     Biswapesh Chattopadhyay <biswapesh_chatterjee tcscal co in>
- *     Naba Kumar <kh_naba users sourceforge net>
+ * 
+ * Copyright (C) 2002 Biswapesh Chattopadhyay <biswapesh_chatterjee tcscal co in>
+ * Copyright (C) 2002 Naba Kumar <kh_naba users sourceforge net>
+ * Copyright (C) 2008 Sebastien Granjoux <seb sfo free fr>
  * 
  * 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
@@ -26,11 +27,6 @@
 #include <ctype.h>
 #include <string.h>
 
-#include <gnome.h>
-#include <libgnomeprint/gnome-print.h>
-#include <libgnomeprint/gnome-print-job.h>
-#include <libgnomeprintui/gnome-print-dialog.h>
-#include <libgnomeprintui/gnome-print-job-preview.h>
 #include <libanjuta/anjuta-utils.h>
 #include <libanjuta/anjuta-preferences.h>
 #include <libanjuta/anjuta-debug.h>
@@ -47,100 +43,113 @@
 #include "properties.h"
 
 #define AN_PRINT_FONT_BODY_DEFAULT   "courier"
-#define AN_PRINT_FONT_HEADER_DEFAULT "helvetica"
 #define AN_PRINT_FONT_SIZE_BODY_DEFAULT   10
-#define AN_PRINT_FONT_SIZE_HEADER_DEFALT 10
-#define AN_PRINT_FONT_SIZE_NUMBERS_DEFAULT 6
 #define AN_PRINT_MAX_STYLES 256
 #define AN_PRINT_LINENUMBER_STYLE 33
 #define AN_PRINT_DEFAULT_TEXT_STYLE 32
-#define AN_PRINT_LINENUM_PADDING '0'
-
-/* Boiler plate */
-#define STYLE_AT(pji, index) (pji)->styles[(index)]
+#define AN_PRINT_HEADER_SIZE_FACTOR 2.2
+#define AN_PRINT_LINE_NUMBER_SEPARATION 12
+#define AN_PRINT_PAGINATION_CHUNK_SIZE 3
 
 typedef struct _PrintJobInfoStyle
 {
-	GnomeFont      *font;
+	PangoFontDescription      *font;
+	GList								*attrs;	
 
-	gchar          *font_name;
-	gboolean        italics;
-	gboolean        bold;
-	gint            size;
+	gchar *font_name;
+	gboolean italics;
+	gboolean bold;
+	gint size;
+	
+	GdkColor fore_color;
+	GdkColor back_color;	
 	
-	GdkColor        fore_color;
-	GdkColor        back_color;
-
 } PrintJobInfoStyle;
 
-typedef struct _PrintJobInfo
+typedef struct _PrintPageInfo
 {
-	GnomePrintJob     *print_job;
-	GnomePrintConfig  *config;
-	GnomePrintContext *pc;
-
-	// const GnomePrintPaper *paper;
-	
-	PrintJobInfoStyle* styles_pool[AN_PRINT_MAX_STYLES];
+	guint pos;
+	guint line;
+} PrintPageInfo;
 
+typedef struct _PrintJobInfo
+{
 	TextEditor *te;
 
 	/* Print Buffer */
 	gchar  *buffer;
-	gchar  *styles;
 	guint   buffer_size;
+
+	/* Page offset */
+	GArray *pages;
+
+	/* Style pool */
+	PrintJobInfoStyle* styles_pool[AN_PRINT_MAX_STYLES];
 	
-	/* Document zoom factor */
+	/* Preferences */
+	gboolean print_header;
+	gboolean print_color;
+	gboolean print_line_numbers;
+	gboolean wrapping;
+	gint     tab_width;
 	gint zoom_factor;
 	
-	/* Preferences */
+	/* Margin in points */
 	gdouble page_width;
 	gdouble page_height;
 	gdouble margin_top;
 	gdouble margin_bottom;
 	gdouble margin_left;
 	gdouble margin_right;
-	gdouble margin_header;
-	gdouble margin_numbers;
-	
-	gboolean print_header;
-	gboolean print_color;
-	gint     print_line_numbers;
-	gboolean preview;
-	gboolean wrapping;
-	gint     tab_size;
+	gdouble header_height;
+	gdouble numbers_width;
+	gdouble numbers_height;
 	
 	/* GC state */
-	gfloat cursor_x;
-	gfloat cursor_y;
-	gfloat current_font_height;
-	guint  current_style_num;
-	PrintJobInfoStyle* current_style;
-	guint  current_page;
-	
-	/* Printing range */
-	gint range_type;
-	gint range_start_line;
-	gint range_end_line;
+	guint  current_style;
+	guint current_page;
+	guint current_pos;
+	guint current_line;
+	gdouble current_height;
+	
+	/* layout objects */
+	PangoLayout *layout;
+	PangoLayout *line_numbers_layout;
+	PangoLayout *header_layout;
 	
 	/* Progress */
-	GtkWidget *progress_bar;
-	GtkWidget *progress_dialog;
-	gboolean canceled;
+	AnjutaStatus* status;
 	
 } PrintJobInfo;
 
-static GnomePrintConfig *print_config;
-
-static PrintJobInfoStyle* anjuta_print_get_style (PrintJobInfo* pji, gint style);
-static void anjuta_print_show_header (PrintJobInfo * pji);
+/* Helper functions
+ *---------------------------------------------------------------------------*/
 
 static void
-anjuta_print_job_info_style_destroy (PrintJobInfoStyle *pis) {
-	if (pis) {
-		if (pis->font) gnome_font_unref(pis->font);
-		if (pis->font_name) g_free(pis->font_name);
-		g_free(pis);
+set_layout_tab_width (PangoLayout *layout, gint width)
+{
+	gchar *str;
+	gint tab_width = 0;
+
+	str = g_strnfill (width, ' ');
+	pango_layout_set_text (layout, str, -1);
+	g_free (str);
+
+	pango_layout_get_size (layout, &tab_width, NULL);
+
+	if (tab_width > 0)
+	{
+		PangoTabArray *tab_array;
+	
+		tab_array = pango_tab_array_new (1, FALSE);
+
+		pango_tab_array_set_tab (tab_array,
+								 0,
+								 PANGO_TAB_LEFT,
+								 tab_width);
+		pango_layout_set_tabs (layout, tab_array);
+
+		pango_tab_array_free (tab_array);
 	}
 }
 
@@ -160,34 +169,24 @@
 string_to_color (const char *val, GdkColor* color)
 {
 	/* g_print ("Color = %s\n", val); */
-	color->red   = IntFromHexDigit(val[1]) * 16 + IntFromHexDigit(val[2]);
-	color->green = IntFromHexDigit(val[3]) * 16 + IntFromHexDigit(val[4]);
-	color->blue  = IntFromHexDigit(val[5]) * 16 + IntFromHexDigit(val[6]);
+	color->red   = (IntFromHexDigit(val[1]) * 16 + IntFromHexDigit(val[2])) * 256;
+	color->green = (IntFromHexDigit(val[3]) * 16 + IntFromHexDigit(val[4])) * 256;
+	color->blue  = (IntFromHexDigit(val[5]) * 16 + IntFromHexDigit(val[6])) * 256;
 }
 
+/* Style info object
+ *---------------------------------------------------------------------------*/
+
 static void
 anjuta_print_job_info_style_load_font (PrintJobInfoStyle *pis)
 {
 	gchar *font_desc, *tmp;
-	PangoFontDescription *desc;
-	GnomeFontFace *font_face;
 	gint size = 12;
 	
 	g_return_if_fail (pis->font_name);
 	
+	/* Setup font */
 	font_desc = g_strdup (pis->font_name);
-	if (pis->bold)
-	{
-		tmp = font_desc;
-		font_desc = g_strconcat (tmp, " Bold", NULL);
-		g_free (tmp);
-	}
-	if (pis->italics)
-	{
-		tmp = font_desc;
-		font_desc = g_strconcat (tmp, " Italic", NULL);
-		g_free (tmp);
-	}
 	if (pis->size > 0)
 	{
 		size = pis->size;
@@ -196,22 +195,55 @@
 		g_free (tmp);
 	}
 	if (pis->font)
-		gnome_font_unref (pis->font);
+		pango_font_description_free (pis->font);
 	
-	// pis->font = gnome_font_find_closest_from_full_name (font_desc);
 	DEBUG_PRINT ("Print font loading: %s", font_desc);
-	desc = pango_font_description_from_string (font_desc);
-	font_face = gnome_font_face_find_closest_from_pango_description (desc);
-	pis->font = gnome_font_face_get_font_default (font_face, size);
-	g_assert (pis->font);
-	DEBUG_PRINT ("Full font name: %s", gnome_font_get_full_name (pis->font));
-	
-	g_object_unref (font_face);
-	pango_font_description_free (desc);
+	pis->font = pango_font_description_from_string (font_desc);
 	g_free (font_desc);
 }
 
 static void
+anjuta_print_job_info_style_clear_attributes (PrintJobInfoStyle *pis)
+{
+	if (pis->attrs != NULL)
+			g_list_foreach (pis->attrs, (GFunc)pango_attribute_destroy, NULL);
+			g_list_free (pis->attrs);
+	pis->attrs = NULL;
+}
+
+static void
+anjuta_print_job_info_style_set_attributes (PrintJobInfoStyle *pis)
+{
+	PangoAttribute *attr;
+	
+	/* Use attribute for font shape */
+	if (pis->bold)
+	{
+		attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
+		pis->attrs = g_list_prepend (pis->attrs, attr);
+	}
+	if (pis->italics)
+	{
+		attr = pango_attr_style_new (PANGO_STYLE_ITALIC);
+		pis->attrs = g_list_prepend (pis->attrs, attr);
+	}
+}
+
+static void
+anjuta_print_job_info_style_set_color_attributes (PrintJobInfoStyle *pis)
+{
+	PangoAttribute *attr;
+	
+	/* Use attribute for color */
+	
+	/* Remove background colors */
+	/*attr = pango_attr_background_new (pis->back_color.red, pis->back_color.green, pis->back_color.blue);
+	pis->attrs = g_list_prepend (pis->attrs, attr);*/
+	attr = pango_attr_foreground_new (pis->fore_color.red, pis->fore_color.green, pis->fore_color.blue);
+	pis->attrs = g_list_prepend (pis->attrs, attr);
+}
+
+static void
 anjuta_print_job_info_style_init (PrintJobInfoStyle *pis, PropsID prop,
 						gchar* lang, guint style)
 {
@@ -262,9 +294,23 @@
 	g_free(style_string);
 }
 
+static void
+anjuta_print_job_info_style_destroy (PrintJobInfoStyle *pis) {
+	if (pis) {
+		if (pis->attrs)
+		{
+			g_list_foreach (pis->attrs, (GFunc)pango_attribute_destroy, NULL);
+			g_list_free (pis->attrs);
+		}
+		if (pis->font) pango_font_description_free (pis->font);
+		if (pis->font_name) g_free (pis->font_name);
+		g_free(pis);
+	}
+}
+
 static PrintJobInfoStyle*
 anjuta_print_job_info_style_new (PropsID prop, gchar* lang,
-			guint style, gint font_zoom_factor)
+			guint style, gint font_zoom_factor, gboolean color)
 {
 	PrintJobInfoStyle* pis;
 	
@@ -302,6 +348,7 @@
 	
 	pis->size += font_zoom_factor;
 
+	anjuta_print_job_info_style_clear_attributes	(pis);
 	anjuta_print_job_info_style_load_font (pis);
 	
 	if (!pis->font) {
@@ -328,50 +375,15 @@
 		anjuta_print_job_info_style_destroy (pis);
 		return NULL;
 	}
-	return pis;
-}
 
-#define _PROPER_I18N
-
-static gfloat
-anjuta_print_get_font_height (PrintJobInfo *pji, gint style)
-{
-	PrintJobInfoStyle* pis = anjuta_print_get_style (pji, style);
-	return gnome_font_get_size (pis->font);
-	/*
-	return (gnome_font_get_ascender (pis->font) + 
-				gnome_font_get_descender (pis->font)); */
-}
-
-static gfloat
-anjuta_print_get_text_width (PrintJobInfo *pji, gint style,
-		gboolean utf8, const char *buff)
-{
-	PrintJobInfoStyle* style_info;
-	gfloat width;
+	anjuta_print_job_info_style_set_attributes (pis);
 	
-	style_info = anjuta_print_get_style (pji, style);
-	if (style_info) {
-		width = gnome_font_get_width_utf8 (style_info->font, buff);
-	} else {
-		width = aneditor_command(pji->te->editor_id,
-				ANE_TEXTWIDTH, style, (long)buff);
+	if (color)
+	{
+		anjuta_print_job_info_style_set_color_attributes (pis);
 	}
-	return width;
-}
-
-static gfloat
-anjuta_print_get_text_width_sized (PrintJobInfo *pji,
-		gint style, gboolean utf8, const char *str, gint len)
-{
-	gfloat width;
-	char *buff = g_new (gchar, len+1);
 	
-	strncpy(buff, str, len);
-	buff[len] = '\0';
-	width = anjuta_print_get_text_width (pji, style, utf8, buff);
-	g_free (buff);
-	return width;
+	return pis;
 }
 
 static void 
@@ -380,18 +392,22 @@
 	int i;
 	
 	g_return_if_fail (pji);
-	
-	if (pji->config != NULL)
-		gnome_print_config_unref (pji->config);
 
-	if (pji->pc != NULL)
-		g_object_unref (pji->pc);
+	if (pji->pages != NULL)
+		g_array_free (pji->pages, TRUE);
+
+	if (pji->layout != NULL)
+		g_object_unref (pji->layout);	
+
+	if (pji->line_numbers_layout != NULL)
+		g_object_unref (pji->line_numbers_layout);	
+	
+	if (pji->header_layout != NULL)
+		g_object_unref (pji->header_layout);	
 
-	if (pji->print_job != NULL)
-		g_object_unref (pji->print_job);
+	if (pji->buffer != NULL)
+		g_free(pji->buffer);
 	
-	if (pji->buffer) g_free(pji->buffer);
-	if (pji->styles) g_free(pji->styles);
 	for (i = 0; i < AN_PRINT_MAX_STYLES; i++)
 	{
 		if (pji->styles_pool[i])
@@ -400,158 +416,15 @@
 	g_free(pji);
 }
 
-static void
-anjuta_print_update_page_size_and_margins (PrintJobInfo *pji)
-{
-	const GnomePrintUnit *unit;
-	
-	gnome_print_job_get_page_size_from_config (pji->config, 
-											   &pji->page_width,
-											   &pji->page_height);
-
-	if (gnome_print_config_get_length (pji->config,
-									   (const guchar *)GNOME_PRINT_KEY_PAGE_MARGIN_LEFT, 
-									   &pji->margin_left, &unit)) 
-	{
-		gnome_print_convert_distance (&pji->margin_left, unit,
-									  GNOME_PRINT_PS_UNIT);
-	}
-	
-	if (gnome_print_config_get_length (pji->config,
-									   (const guchar *)GNOME_PRINT_KEY_PAGE_MARGIN_RIGHT, 
-									   &pji->margin_right, &unit)) 
-	{
-		gnome_print_convert_distance (&pji->margin_right, unit,
-									  GNOME_PRINT_PS_UNIT);
-	}
-	
-	if (gnome_print_config_get_length (pji->config,
-									   (const guchar *)GNOME_PRINT_KEY_PAGE_MARGIN_TOP, 
-									   &pji->margin_top, &unit)) 
-	{
-		gnome_print_convert_distance (&pji->margin_top, unit,
-									  GNOME_PRINT_PS_UNIT);
-	}
-	if (gnome_print_config_get_length (pji->config,
-									   (const guchar *)GNOME_PRINT_KEY_PAGE_MARGIN_BOTTOM, 
-									   &pji->margin_bottom, &unit)) 
-	{
-		gnome_print_convert_distance (&pji->margin_bottom, unit,
-									  GNOME_PRINT_PS_UNIT);
-	}
-	if (pji->print_line_numbers <= 0)
-	{
-		pji->margin_numbers = 0.0;
-	}
-	if (pji->print_header)
-	{
-		pji->margin_header  =
-				anjuta_print_get_font_height (pji, AN_PRINT_DEFAULT_TEXT_STYLE);
-		pji->margin_header  *= 2.5;
-	}
-	else
-	{
-		pji->margin_header = 0.0;
-	}
-	if (pji->print_line_numbers > 0)
-	{
-		pji->margin_numbers =
-				anjuta_print_get_text_width (pji, AN_PRINT_LINENUMBER_STYLE,
-											 FALSE, "0");
-		pji->margin_numbers *= 5; /* Digits in linenumbers */
-		pji->margin_numbers += 5; /* Spacer */
-	}
-	else
-	{
-		pji->margin_numbers = 0.0;
-	}
-}
-
 static PrintJobInfo*
-anjuta_print_job_info_new (AnjutaPreferences *p, TextEditor *te)
+anjuta_print_job_info_new (TextEditor *te)
 {
 	PrintJobInfo *pji;
-	gint i;
-	gchar *buffer;
 
 	pji = g_new0(PrintJobInfo, 1);
-	if (NULL == (pji->te = te))
-	{
-		anjuta_util_dialog_error (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET(te))),
-								  _("No file to print!"));
-		g_free(pji);
-		return NULL;
-	}
-	pji->preview = FALSE;
-
-	/* Set print config */
-	if (!print_config)
-	{
-		print_config = gnome_print_config_default ();
-		g_return_val_if_fail (print_config != NULL, NULL);
-
-		gnome_print_config_set (print_config, (const guchar *)"Settings.Transport.Backend", (const guchar *)"lpr");
-		gnome_print_config_set (print_config, (const guchar *)"Printer", (const guchar *)"GENERIC");
-	}
-	pji->config = print_config;
-	gnome_print_config_ref (pji->config);
-	
-	/* Load Buffer to be printed. The buffer loaded is the text/style combination.*/
-	pji->buffer_size = scintilla_send_message(SCINTILLA(pji->te->scintilla), SCI_GETLENGTH, 0, 0);
-	buffer = (gchar *) aneditor_command(pji->te->editor_id, ANE_GETSTYLEDTEXT, 0, pji->buffer_size);
-	if (NULL == buffer)
-	{
-		anjuta_util_dialog_error(NULL, _("Unable to get text buffer for printing"));
-		anjuta_print_job_info_destroy(pji);
-		return NULL;
-	}
-	pji->buffer = g_new(char, pji->buffer_size + 1);
-	pji->styles = g_new(char, pji->buffer_size + 1);
-	pji->buffer[pji->buffer_size] = '\0';
-	pji->styles[pji->buffer_size] = '\0';
-	for (i = 0; i < pji->buffer_size; i++)
-	{
-		pji->buffer[i] = buffer[i << 1];
-		pji->styles[i] = buffer[(i << 1) + 1];
-	}
-	g_free (buffer);
-	
-	/* Zoom factor */
-	pji->zoom_factor = anjuta_preferences_get_int (te->preferences,
-						       TEXT_ZOOM_FACTOR);
-	
-	/* Line number printing details */
-	pji->print_line_numbers =
-		anjuta_preferences_get_int_with_default (p, PRINT_LINENUM_COUNT, 1);
-	pji->print_line_numbers = pji->print_line_numbers >= 0? pji->print_line_numbers: 0;
-	
-	/* Other preferences. */
-	pji->print_header =
-		anjuta_preferences_get_int_with_default (p, PRINT_HEADER, 1);
-	pji->print_color =
-		anjuta_preferences_get_int_with_default (p, PRINT_COLOR, 1);
-	pji->wrapping =
-		anjuta_preferences_get_int_with_default (p, PRINT_WRAP, 1);
-	pji->tab_size =
-		anjuta_preferences_get_int_with_default (p, TAB_SIZE, 8);
-	pji->range_type = GNOME_PRINT_RANGE_ALL;
-	
-	/* State variables initializations */
-	pji->canceled = FALSE;
-	pji->current_style_num = 0;
-	pji->current_style = NULL;
-	pji->current_page = 0;
-	pji->cursor_x = pji->margin_left + pji->margin_numbers;
-	pji->cursor_y = pji->page_height - pji->margin_top - pji->margin_header;
 
-	/* Init style pool */
-	for (i = 0; i < AN_PRINT_MAX_STYLES; i++) pji->styles_pool[i] = NULL;
-	
-	/* Margin settings */
-	// pji->margin_left    = preferences_get_int_with_default(p, PRINT_MARGIN_LEFT, 54);
-	// pji->margin_right   = preferences_get_int_with_default(p, PRINT_MARGIN_RIGHT, 54);
-	// pji->margin_top     = preferences_get_int_with_default(p, PRINT_MARGIN_TOP, 54);
-	// pji->margin_bottom  = preferences_get_int_with_default(p, PRINT_MARGIN_BOTTOM, 54);
+	pji->te = te;
+	pji->pages = g_array_new (FALSE, FALSE, sizeof (PrintPageInfo));
 	
 	return pji;
 }
@@ -567,9 +440,10 @@
 		gchar* language;
 		language = (gchar*) aneditor_command(pji->te->editor_id, ANE_GETLANGUAGE,0, 0);
 		pis = anjuta_print_job_info_style_new (pji->te->props_base,
-						language, style, pji->zoom_factor);
+						language, style, pji->zoom_factor, pji->print_color);
 		pji->styles_pool[style] = pis;
 	}
+	
 	if (!pis && style != AN_PRINT_DEFAULT_TEXT_STYLE) {
 		pis = anjuta_print_get_style (pji, AN_PRINT_DEFAULT_TEXT_STYLE);
 	}
@@ -577,538 +451,526 @@
 	return pis;
 }
 
+/* Change style of main layout */
 static void
-anjuta_print_set_style (PrintJobInfo *pji, gint style)
+anjuta_print_apply_style (PrintJobInfo *pji, gint style, guint start, guint end)
 {
 	PrintJobInfoStyle* pis;
-	gint font_height;
 	
 	pis = anjuta_print_get_style (pji, style);
 	g_return_if_fail (pis != NULL);
-	g_assert (GNOME_IS_FONT(pis->font));
-	gnome_print_setfont(pji->pc, pis->font);
-	if (pji->print_color) {
-		gnome_print_setrgbcolor(pji->pc,
-				(gfloat)pis->fore_color.red/256.0 ,
-				(gfloat)pis->fore_color.green/256.0,
-				(gfloat)pis->fore_color.blue/256.0);
-	}
-	pji->current_style_num = style;
-	pji->current_style = pis;
 	
-	font_height = anjuta_print_get_font_height (pji, style);
+	pango_layout_set_font_description (pji->layout, pis->font);
 
-	pji->current_font_height = (pji->current_font_height > font_height)?
-									pji->current_font_height : font_height;
+	if (pis->attrs)
+	{
+		PangoAttrList *attr_list;
+		PangoAttrList *new_list = NULL;
+		GList *node;
+
+		attr_list = pango_layout_get_attributes (pji->layout);
+		if ((attr_list == NULL) || (start == 0))
+		{
+			attr_list = pango_attr_list_new ();
+			new_list = attr_list;
+		}
+		
+		for (node = g_list_first (pis->attrs); node != NULL; node = g_list_next (node))
+		{
+			PangoAttribute *a = pango_attribute_copy ((PangoAttribute *)node->data);
+			
+			a->start_index = start;
+			a->end_index = end;
+			
+			pango_attr_list_insert (attr_list, a);
+		}
+		pango_layout_set_attributes (pji->layout, attr_list);
+		if (new_list) pango_attr_list_unref (new_list);
+	}
 }
 
 static void
-anjuta_print_new_page (PrintJobInfo *pji)
+anjuta_setup_layout (PrintJobInfo *pji, GtkPrintContext *context)
 {
-	gchar page[256];
-	
-	gnome_print_showpage (pji->pc);
-	
-	DEBUG_PRINT ("%s", "Printing new page...");
-	
-	pji->current_page++;
-	sprintf(page, "%d", pji->current_page);
-	gnome_print_beginpage (pji->pc, (const guchar *)page);
+	g_return_if_fail (pji->layout == NULL);
+
+	/* Layout for header */
 	if (pji->print_header)
-		anjuta_print_show_header(pji);
-	pji->cursor_y = pji->page_height - pji->margin_top - pji->margin_header;
-	pji->cursor_x = pji->margin_left + pji->margin_numbers;
-}
+	{
+		pji->layout = gtk_print_context_create_pango_layout (context);
+		anjuta_print_apply_style (pji, AN_PRINT_DEFAULT_TEXT_STYLE, 0, G_MAXUINT);
 
-static void
-anjuta_print_new_line (PrintJobInfo *pji)
-{
-	if ((pji->cursor_y - pji->current_font_height) < (pji->margin_top)) {
-		anjuta_print_new_page (pji);
-	} else {
-		pji->cursor_x  = pji->margin_left + pji->margin_numbers;
-		pji->cursor_y -=  pji->current_font_height;
+		g_return_if_fail (pji->header_layout == NULL);						
+		pji->header_layout = pji->layout;
 	}
-}
-
-
-static gint
-anjuta_print_show_chars_styled (PrintJobInfo *pji, const char *chars, gint size,
-							   const char style)
-{
-	gfloat width;
 	
-	g_return_val_if_fail (pji != NULL, -1);
-	g_return_val_if_fail (size > 0, -1);
-	
-	if (chars[0] == '\n') {
-		anjuta_print_new_line (pji);
-	} else {
-		width = anjuta_print_get_text_width_sized (pji, style, TRUE, chars, size);
-		
-		/* Determine wrapping */
-		if ((pji->cursor_x + width) > (pji->page_width - pji->margin_right)) {
-			anjuta_print_new_line (pji);
-			if (!pji->wrapping) {
-				return 1;/* Text wrapping, return true */
-			}
-		}
-		
-		/* Set style */
-		anjuta_print_set_style(pji, style);
+	/* Layout for line numbers */
+	if (pji->print_line_numbers)
+	{
+		pji->layout = gtk_print_context_create_pango_layout (context);
+		anjuta_print_apply_style (pji, AN_PRINT_LINENUMBER_STYLE, 0, G_MAXUINT);
 		
-		/* Print it */
-		gnome_print_moveto (pji->pc, pji->cursor_x, pji->cursor_y);
-		gnome_print_show_sized (pji->pc, (const guchar *)chars, size);
-		pji->cursor_x += width;
+		g_return_if_fail (pji->line_numbers_layout == NULL);						
+		pji->line_numbers_layout = pji->layout;
+								
+		pango_layout_set_alignment (pji->line_numbers_layout, PANGO_ALIGN_RIGHT);
 	}
-	return 0; /* Return text wrap status */
-}
-
-static void
-anjuta_print_set_buffer_as_selection (PrintJobInfo *pji)
-{
-	gint from, to;
-	gint proper_from, proper_to;
-	gchar *buffer;
-	gint i;
-	
-	pji->range_start_line = 1;
-	pji->range_end_line = text_editor_get_total_lines (pji->te);
 	
-	from = scintilla_send_message (SCINTILLA (pji->te->scintilla),
-				SCI_GETSELECTIONSTART, 0, 0);
-	to = scintilla_send_message (SCINTILLA (pji->te->scintilla),
-				SCI_GETSELECTIONEND, 0, 0);
-	if (from == to) return;
-	proper_from = MIN(from, to);
-	proper_to   = MAX(from, to);
-	
-	if (pji->buffer) g_free(pji->buffer);
-	pji->buffer = NULL;
-	if (pji->styles) g_free (pji->styles);
-	pji->styles = NULL;
-	
-	/* Print Buffers */
-	/* Load Buffer to be printed. The buffer loaded is the text/style combination.*/
-	pji->buffer_size = proper_to - proper_from;
-	buffer = (gchar *) aneditor_command(pji->te->editor_id, ANE_GETSTYLEDTEXT, proper_from, proper_to);
-	g_return_if_fail(buffer!=NULL);
-	pji->buffer = g_new(char, pji->buffer_size + 1);
-	pji->styles = g_new(char, pji->buffer_size + 1);
-	pji->buffer[pji->buffer_size] = '\0';
-	pji->styles[pji->buffer_size] = '\0';
-	for (i = 0; i < pji->buffer_size; i++)
-	{
-		pji->buffer[i] = buffer[i << 1];
-		pji->styles[i] = buffer[(i << 1) + 1];
-	}
-	g_free (buffer);
-
-	pji->range_start_line = scintilla_send_message (SCINTILLA (pji->te->scintilla),
-				SCI_LINEFROMPOSITION, proper_from, 0);
-	pji->range_end_line = scintilla_send_message (SCINTILLA (pji->te->scintilla),
-				SCI_LINEFROMPOSITION, proper_to, 0);
-	
-	/* Same crapy misalignment */
-	pji->range_start_line++;
-}
-
-static void
-anjuta_print_set_buffer_as_range (PrintJobInfo *pji)
-{
-	gint from, to, i;
-	gint proper_from, proper_to;
-	gchar *buffer;
-	
-	from = scintilla_send_message (SCINTILLA (pji->te->scintilla),
-				SCI_POSITIONFROMLINE, pji->range_start_line - 1, 0);
-	to = scintilla_send_message (SCINTILLA (pji->te->scintilla),
-				SCI_POSITIONFROMLINE, pji->range_end_line, 0);
-	if (from == to) return;
-	to--;
-	proper_from = MIN(from, to);
-	proper_to   = MAX(from, to);
-	
-	if (pji->buffer) g_free(pji->buffer);
-	pji->buffer = NULL;
-	if (pji->styles) g_free (pji->styles);
-	pji->styles = NULL;
+	/* Layout for the text */
+	pji->layout = gtk_print_context_create_pango_layout (context);
+	anjuta_print_apply_style (pji, AN_PRINT_DEFAULT_TEXT_STYLE, 0, G_MAXUINT);
 	
-	/* Print Buffers */
-	/* Load Buffer to be printed. The buffer loaded is the text/style combination.*/
-	pji->buffer_size = proper_to - proper_from;
-	buffer = (gchar *) aneditor_command(pji->te->editor_id, ANE_GETSTYLEDTEXT, proper_from, proper_to);
-	g_return_if_fail(buffer!=NULL);
-	pji->buffer = g_new(char, pji->buffer_size + 1);
-	pji->styles = g_new(char, pji->buffer_size + 1);
-	pji->buffer[pji->buffer_size] = '\0';
-	pji->styles[pji->buffer_size] = '\0';
-	for (i = 0; i < pji->buffer_size; i++)
+	if (pji->wrapping)
 	{
-		pji->buffer[i] = buffer[i << 1];
-		pji->styles[i] = buffer[(i << 1) + 1];
+		pango_layout_set_wrap (pji->layout, PANGO_WRAP_WORD_CHAR);
 	}
-	g_free (buffer);
+	set_layout_tab_width (pji->layout, pji->tab_width);
 }
 
 static void
-anjuta_print_show_header (PrintJobInfo * pji)
+anjuta_print_update_page_size_and_margins (PrintJobInfo *pji, GtkPrintContext *context)
 {
-	guchar *text1 = (guchar *)g_strdup_printf (_("File: %s"), pji->te->filename);
-	guchar *text2 = (guchar *)g_strdup_printf ("%d", pji->current_page);
-	gfloat width;
-	gboolean save_wrapping;
-
-	save_wrapping = pji->wrapping;
-	pji->wrapping = FALSE;
-	
-	/* Print filename on left */
-	pji->cursor_x = pji->margin_left;
-	pji->cursor_y = pji->page_height - pji->margin_top;
-	gnome_print_moveto (pji->pc, pji->cursor_x, pji->cursor_y);
-	anjuta_print_show_chars_styled(pji, (const char *)text1, strlen((const char *)text1),
-								   AN_PRINT_DEFAULT_TEXT_STYLE);
+	GtkPageSetup *page_setup;
 
-	/* Print page number on right */
-	width = anjuta_print_get_text_width (pji, AN_PRINT_DEFAULT_TEXT_STYLE, FALSE, (const char *)text2);
-	pji->cursor_x = pji->page_width - pji->margin_right - width - 2;
-	pji->cursor_y = pji->page_height - pji->margin_top;
-	gnome_print_moveto (pji->pc, pji->cursor_x, pji->cursor_y);
-	anjuta_print_show_chars_styled(pji, (const char *)text2, strlen((const char *)text2),
-								   AN_PRINT_DEFAULT_TEXT_STYLE);
+	page_setup = gtk_print_context_get_page_setup (context);
 	
-	pji->wrapping = save_wrapping;
-	g_free (text1);
-	g_free (text2);
-}
+	pji->margin_left = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_POINTS);
+	pji->margin_right = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_POINTS);
+	pji->margin_top = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_POINTS);
+	pji->margin_bottom = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_POINTS);
 
-static void
-anjuta_print_show_linenum (PrintJobInfo * pji, guint line, guint padding)
-{
-	guchar *line_num = (guchar *)g_strdup_printf ("%u", line);
-	guchar *pad_str, *text;
-	gboolean save_wrapping;
-	gfloat save_x, save_y;
+	pji->page_width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
+	pji->page_height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
+
+	if (pji->print_line_numbers)
+	{
+		gint line_count;
+		gchar *str;
+		gint padding;
+		PangoRectangle rect;		
 		
-	pad_str = (guchar *)g_strnfill(padding - strlen((const char *)line_num), AN_PRINT_LINENUM_PADDING);
-	text = (guchar *)g_strconcat((gchar *)pad_str, line_num, NULL);	
-	g_free(pad_str);
-	g_free(line_num);	
+		line_count = text_editor_get_total_lines (pji->te) + 1;
+		for (padding = 1; line_count >= 10; padding++) line_count /= 10;
+		str = g_strnfill (padding, '9'); 
+		pango_layout_set_text (pji->line_numbers_layout, str, -1);
+		g_free (str);
 		
-	save_wrapping = pji->wrapping;
-	save_x = pji->cursor_x;
-	save_y = pji->cursor_y;
-	
-	pji->wrapping = FALSE;
-	pji->cursor_x = pji->margin_left;
-	gnome_print_moveto (pji->pc, pji->cursor_x, pji->cursor_y);
-	anjuta_print_show_chars_styled(pji, (const char *)text, strlen((const char *)text), 
-								  AN_PRINT_LINENUMBER_STYLE);
-
-	pji->wrapping = save_wrapping;
-	pji->cursor_x = save_x;
-	pji->cursor_y = save_y;
-	g_free (text);
-}
+		pango_layout_get_extents (pji->line_numbers_layout, NULL, &rect);
 
-static void
-anjuta_print_begin (PrintJobInfo * pji)
-{
-	gchar page[256];
-	
-	pji->current_page = 1;
+		pji->numbers_width = ((gdouble) rect.width + (gdouble)AN_PRINT_LINE_NUMBER_SEPARATION) / (gdouble) PANGO_SCALE;
+		pji->numbers_height = (gdouble) rect.height / (gdouble) PANGO_SCALE;
+	}
+	else
+	{
+		pji->numbers_width = 0.0;
+		pji->numbers_height = 0.0;
+	}
 	
-	sprintf(page, "%d", pji->current_page);
-	gnome_print_beginpage (pji->pc, (const guchar *)page);
 	if (pji->print_header)
-		anjuta_print_show_header(pji);
-	pji->cursor_y = pji->page_height - pji->margin_top - pji->margin_header;
-	pji->cursor_x = pji->margin_left + pji->margin_numbers;
-}
+	{
+		PangoContext *pango_context;
+		PangoFontMetrics* font_metrics;
+		const PangoFontDescription *font;
+		gdouble ascent;
+		gdouble descent;
+
+		pango_context = gtk_print_context_create_pango_context (context);
+		font = pango_layout_get_font_description (pji->header_layout);
+		pango_context_set_font_description (pango_context, font);
+
+		font_metrics = pango_context_get_metrics (pango_context, font, gtk_get_default_language());
+
+		ascent = (gdouble) pango_font_metrics_get_ascent (font_metrics);
+		descent = (gdouble) pango_font_metrics_get_descent (font_metrics);
+		pango_font_metrics_unref (font_metrics);
+		g_object_unref (pango_context);
 
-static void
-anjuta_print_end(PrintJobInfo * pji)
-{
-	gnome_print_showpage(pji->pc);
-	gnome_print_context_close (pji->pc);
+		pji->header_height = (gdouble)(ascent + descent) / PANGO_SCALE;
+		pji->header_height *= AN_PRINT_HEADER_SIZE_FACTOR;
+	}
+	else
+	{
+		pji->header_height = 0.0;
+	}
+	
+	pango_layout_set_width (pji->layout, (pji->page_width - pji->margin_left - pji->numbers_width - pji->margin_right) * PANGO_SCALE);
 }
 
 static void
-anjuta_print_progress_response (GtkWidget *dialog, gint res, gpointer data)
+anjuta_print_layout_line (PrintJobInfo *pji)
 {
-	PrintJobInfo *pji = (PrintJobInfo *) data;
+	gchar utf8_char[4];
+	gchar style;
+	const gchar *text;
+	GString *line_buffer;
+	guint last_change;
+	gint current_style;
+		
+	/* Read a complete line */
+	line_buffer = g_string_new (NULL);
+	last_change = 0;
+	current_style = pji->buffer[pji->current_pos *2 + 1];
+	
+	for (text = &pji->buffer[pji->current_pos * 2]; (*text != '\n') && (pji->current_pos < pji->buffer_size); text = &pji->buffer[pji->current_pos * 2])
+	{
+		gint len;
+		
+		/* Buffer contains data bytes merged with style bytes */
+		utf8_char[0] = text[0];
+		utf8_char[1] = text[2];
+		utf8_char[2] = text[4];
+		utf8_char[3] = text[8];
+		style = text[1];
 
-	if (pji->progress_dialog == NULL)
-		return;
-	pji->canceled = TRUE;
-	gtk_widget_destroy (dialog);
-	pji->progress_dialog = NULL;
-}
+		/* Change style if necessary */
+		if (style != current_style)
+		{
+			anjuta_print_apply_style (pji, current_style, last_change, line_buffer->len);
+			last_change = line_buffer->len;
+			current_style= style;
+		}
+		
+		/* Append character */
+		len = g_utf8_next_char (utf8_char) - utf8_char;
+		g_string_append_len (line_buffer, utf8_char, len);
+		
+		pji->current_pos += len;
+	}
+	pji->current_pos++;
+	
+	anjuta_print_apply_style (pji, current_style, last_change, G_MAXUINT);
+	if (line_buffer->len == 0)
+	{
+		/* Empty line, display just one space */
+		pango_layout_set_text (pji->layout," ", 1);
+	}
+	else
+	{
+		pango_layout_set_text (pji->layout,line_buffer->str, line_buffer->len);
+	}
+	
+	g_string_free (line_buffer, TRUE);
+}			
 
-static gboolean
-anjuta_print_progress_delete_event (GtkWidget *dialog,
-									GdkEvent *event, gpointer data)
+static void
+anjuta_draw_header (PrintJobInfo * pji, cairo_t *cr)
 {
-	PrintJobInfo *pji = (PrintJobInfo *) data;
+	gchar *text1 = g_strdup_printf (_("File: %s"), pji->te->filename);
+	gchar *text2 = g_strdup_printf ("%d", pji->current_page + 1);
+	gdouble baseline;
+	gdouble header_width;
+	gdouble layout_width;
+	PangoLayoutIter *iter;
+	PangoRectangle rect;
+	PangoLayoutLine* line;
+	gdouble x;
 
-	if (pji->progress_dialog == NULL)
-		return FALSE;
-	pji->progress_dialog = NULL;
-	pji->canceled = TRUE;
-	return FALSE;
-}
+	pango_cairo_update_layout (cr, pji->header_layout);
+	
+	header_width = pji->page_width - pji->margin_left - pji->margin_right;
 
-static void
-anjuta_print_progress_start(PrintJobInfo * pji)
-{
-	GtkWidget *dialog;
-	GtkWidget *label;
-	GtkWidget *progress_bar;
-	GtkWidget *window;
-
-	window = gtk_widget_get_toplevel (pji->te->scintilla);
-	dialog = gtk_dialog_new_with_buttons (_("Printing..."),
-										  GTK_WINDOW (window),
-										  GTK_DIALOG_DESTROY_WITH_PARENT,
-										  GTK_STOCK_CANCEL,
-										  GTK_RESPONSE_CANCEL, NULL);
-	g_signal_connect (dialog, "response",
-	  				  G_CALLBACK (anjuta_print_progress_response), pji);
-	g_signal_connect (dialog, "delete_event",
-	  				  G_CALLBACK (anjuta_print_progress_delete_event), pji);
-	pji->progress_dialog = dialog;
-	gtk_widget_show (dialog);
-	
-	label = gtk_label_new (_("Printing..."));
-	gtk_widget_show (label);
-	gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog)->vbox),
-								 label, FALSE, FALSE, 0);
-	
-	progress_bar = gtk_progress_bar_new();
-	gtk_widget_show (progress_bar);
-	pji->progress_bar = progress_bar;
-	gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog)->vbox),
-								 progress_bar, FALSE, FALSE, 0);
+	/* Print filename on left */	
+	pango_layout_set_text (pji->header_layout, text1, -1);
+	iter = pango_layout_get_iter (pji->header_layout);
+	baseline = (gdouble) pango_layout_iter_get_baseline (iter) / (gdouble) PANGO_SCALE;	
+	
+	x =pji->margin_left;
+	
+	line = pango_layout_iter_get_line_readonly (iter);
+	pango_layout_iter_free (iter);
+ 	cairo_move_to (cr, x, pji->margin_top + baseline);
+	pango_cairo_show_layout_line (cr, line);
+	
+	/* Print page number on right */
+	pango_layout_set_text (pji->header_layout, text2, -1);
+	iter = pango_layout_get_iter (pji->header_layout);
+	baseline = (gdouble) pango_layout_iter_get_baseline (iter) / (gdouble) PANGO_SCALE;	
+	
+	pango_layout_get_extents (pji->header_layout, NULL, &rect);
+	layout_width = (double) rect.width / (double) PANGO_SCALE;
+	x = pji->margin_left + header_width - layout_width;
+
+	line = pango_layout_iter_get_line_readonly (iter);
+	pango_layout_iter_free (iter);
+ 	cairo_move_to (cr, x, pji->margin_top + baseline);
+	pango_cairo_show_layout_line (cr, line);
+
+	g_free (text1);
+	g_free (text2);
 }
 
 static void
-anjuta_print_progress_tick(PrintJobInfo * pji, guint index)
+anjuta_draw_linenum (PrintJobInfo * pji, cairo_t *cr)
 {
-	gfloat percentage;
+	gchar *text = g_strdup_printf ("%d", pji->current_line);
+	gdouble baseline;
+	gdouble layout_width;
+	PangoLayoutIter *iter;
+	PangoRectangle rect;
+	gdouble x;
+	
+	pango_cairo_update_layout (cr, pji->line_numbers_layout);
+
+	/* Print line number on right */
+	pango_layout_set_text (pji->line_numbers_layout, text, -1);
+	iter = pango_layout_get_iter (pji->line_numbers_layout);
+	baseline = (gdouble) pango_layout_iter_get_baseline (iter) / (gdouble) PANGO_SCALE;	
+	pango_layout_iter_free (iter);
+	
+	pango_layout_get_extents (pji->line_numbers_layout, NULL, &rect);
+	layout_width = (double) rect.width / (double) PANGO_SCALE;
+	x = pji->margin_left + pji->numbers_width - layout_width - AN_PRINT_LINE_NUMBER_SEPARATION;
+		
+ 	cairo_move_to (cr, x, pji->current_height);
+	pango_cairo_show_layout (cr, pji->line_numbers_layout);		
 
-	while (gtk_events_pending())
-		gtk_main_iteration ();
-	if (pji->progress_dialog == NULL)
-		return;
-	percentage = (float)index/pji->buffer_size;
-	if (percentage < 0.0) percentage = 0.0;
-	if (percentage > 1.0) percentage = 1.0;
-	gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pji->progress_bar),
-								   percentage);
+	g_free (text);
 }
 
 static void
-anjuta_print_progress_end (PrintJobInfo * pji)
+anjuta_end_print (GtkPrintOperation        *operation, 
+		   GtkPrintContext          *context,
+		   PrintJobInfo *pji)
 {
-	if (pji->progress_dialog == NULL)
-		return;
-	gtk_widget_destroy (pji->progress_dialog);
-	pji->progress_dialog = NULL;
-	pji->progress_bar = NULL;
+	anjuta_print_job_info_destroy (pji);
 }
 
 static void
-anjuta_print_document (PrintJobInfo * pji)
-{
-	guint i, style_index, ret, current_line, num_lines, padding;
-	gchar *current_pos;
-
-	current_line = 1;
-	
-	anjuta_print_progress_start (pji);
+anjuta_draw_page (GtkPrintOperation        *operation,
+		   GtkPrintContext          *context,
+		   gint                      page_nr,
+		   PrintJobInfo *pji)
+{
+	cairo_t *cr;
+	gdouble x;
+	gboolean done;
+	guint page_end;
+
+	g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));
+
+	pji->current_page = page_nr;
+
+	cr = gtk_print_context_get_cairo_context (context);
+	cairo_set_source_rgb (cr, 0, 0, 0);
+	cairo_translate (cr,
+					 -1 * pji->margin_left,
+					 -1 * pji->margin_top);	
 	
-	switch (pji->range_type) {
-		case GNOME_PRINT_RANGE_SELECTION:
-			anjuta_print_set_buffer_as_selection (pji);
-			current_line = pji->range_start_line;
-			break;
-		
-		case GNOME_PRINT_RANGE_RANGE:
-			anjuta_print_set_buffer_as_range (pji);
-			current_line = pji->range_start_line;
-			break;
-		default:
-			break;
+	if (pji->print_header)
+	{
+		anjuta_draw_header (pji, cr);
 	}
+
+	pji->current_pos = g_array_index (pji->pages, PrintPageInfo, page_nr).pos;
+	pji->current_line = g_array_index (pji->pages, PrintPageInfo, page_nr).line;
+	if (pji->pages->len <= (page_nr + 1))
+	{
+		page_end = pji->buffer_size;
+	}
+	else
+	{
+		page_end = g_array_index (pji->pages, PrintPageInfo, page_nr + 1).pos;
+	}
+
+	pango_cairo_update_layout (cr, pji->layout);
 	
-	num_lines = text_editor_get_total_lines (pji->te)+1;
-	
-	for (padding = 1; num_lines >= 10; padding++)
-		num_lines /= 10;
-	
-	anjuta_print_begin (pji);
-	
-	if (pji->print_line_numbers > 0)
-		anjuta_print_show_linenum (pji, current_line, padding);
+	x = pji->margin_left + pji->numbers_width;
+	pji->current_height = pji->margin_top + pji->header_height;
 	
-	current_pos = pji->buffer;
-	style_index = 0;
-	i = 0;
-	while (current_pos < (pji->buffer + pji->buffer_size))
+	done = pji->current_pos >= page_end;
+	while (!done)
 	{
-		gchar *previous_pos;
-		gchar style;
-		
-		ret = 0;
-		/* text = TEXT_AT(pji->buffer, i); */
-		style = STYLE_AT(pji, style_index);
-		
-		if (current_pos[0] == '\t') {
-			int j;
-			for (j = 0; j < pji->tab_size; j++ ) {
-				ret = anjuta_print_show_chars_styled(pji, " ", 1, style);
-				if (ret == 1) break;
-			}
-		} else {
-			ret = anjuta_print_show_chars_styled(pji, current_pos,
-												 g_utf8_next_char(current_pos)
-													- current_pos,
-												 style);
-		}
-		/* Skip to next line */
-		if (ret == 1) {
-			while ((current_pos < (pji->buffer + pji->buffer_size))
-					&& (current_pos[0] != '\n')) {
-						
-				/* Advance to next character */
-				previous_pos = current_pos;
-				current_pos = g_utf8_next_char (current_pos);
-				style_index += current_pos - previous_pos;
-				i++;
-			}
-		}
-		if (current_pos[0] == '\n' || ret == 1) {
-			current_line++;
-			if (pji->print_line_numbers > 0 &&
-					((current_line) % pji->print_line_numbers == 0))
-				anjuta_print_show_linenum (pji, current_line, padding);
+		PangoRectangle rect;
+		gdouble height;
+		guint start;
+
+		/* Draw line number */
+		if (pji->print_line_numbers)
+		{
+			anjuta_draw_linenum (pji, cr);
 		}
-		if (i % 50 ==  0)
-			anjuta_print_progress_tick (pji, i);
 		
-		/* advance to next character. */
-		previous_pos = current_pos;
-		current_pos = g_utf8_next_char (current_pos);
-		style_index += current_pos - previous_pos;
-		i++;
+		/* Layout one line */
+		start = pji->current_pos;
+		anjuta_print_layout_line (pji);
+		
+		/* Draw line */
+		cairo_move_to (cr, x, pji->current_height);
+		pango_cairo_show_layout (cr, pji->layout);		
+
+		/* Print next line */
+		pango_layout_get_extents (pji->layout, NULL, &rect);
+		height = rect.height / PANGO_SCALE;
+		if (height < pji->numbers_height)
+		{
+			height = pji->numbers_height;
+		}
+		pji->current_height += height;
+		pji->current_line ++;
 		
-		/* Exit if canceled. */
-		if (pji->canceled)
-			break;
+		done = pji->current_pos >= page_end;
 	}
-	anjuta_print_end (pji);
-	anjuta_print_progress_end (pji);
 }
 
 static gboolean
-anjuta_print_run_dialog(PrintJobInfo *pji)
+anjuta_paginate (GtkPrintOperation        *operation, 
+		  GtkPrintContext          *context,
+		  PrintJobInfo *pji)
 {
-	GtkWidget *dialog;
-	gint selection_flag;
-	gint lines;
-
-	if (text_editor_has_selection (pji->te))
-		selection_flag = GNOME_PRINT_RANGE_SELECTION;
-	else
-		selection_flag = GNOME_PRINT_RANGE_SELECTION_UNSENSITIVE;
+	gdouble text_height;
+	guint page_count;
+	gboolean done;
+	
+	text_height = pji->page_height - pji->margin_top - pji->header_height - pji->header_height;
+	page_count = 0;
+	done = pji->current_pos >= pji->buffer_size;
+	
+	/* Mark beginning of a page */
+	if (pji->pages->len == pji->current_page)
+	{
+		PrintPageInfo info = {pji->current_pos, pji->current_line};
+		g_array_append_val (pji->pages, info);
+	}
 	
-	dialog = g_object_new (GNOME_TYPE_PRINT_DIALOG, "print_config",
-						   pji->config, NULL);
+	while (!done && (page_count < AN_PRINT_PAGINATION_CHUNK_SIZE))
+	{
+		PangoRectangle rect;
+		gdouble height;
+		guint start;
 
-	gnome_print_dialog_construct (GNOME_PRINT_DIALOG (dialog),
-								  (const guchar *)_("Print"),
-								  GNOME_PRINT_DIALOG_RANGE |
-								  GNOME_PRINT_DIALOG_COPIES);
-	
-	lines = text_editor_get_total_lines (pji->te);
-	
-	gnome_print_dialog_construct_range_page (GNOME_PRINT_DIALOG (dialog),
-											 GNOME_PRINT_RANGE_ALL |
-											 selection_flag,
-											 1, lines, (const guchar *)"A", (const guchar *)_("Lines"));
-	switch (gtk_dialog_run(GTK_DIALOG (dialog)))
-	{
-		case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
-			break;
-		case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
-			pji->preview = TRUE;
-			break;
-		case -1:
-			return TRUE;
-		default:
-			gtk_widget_destroy (GTK_WIDGET (dialog));
-			return TRUE;
-	}
-	
-	pji->range_type = gnome_print_dialog_get_range_page(GNOME_PRINT_DIALOG (dialog),
-						&pji->range_start_line,
-						&pji->range_end_line);
+		/* Layout one line */
+		start = pji->current_pos;
+		anjuta_print_layout_line (pji);
+		pango_layout_get_extents (pji->layout, NULL, &rect);
+		height = rect.height / PANGO_SCALE;
+		
+		if (height < pji->numbers_height)
+		{
+			height = pji->numbers_height;
+		}
+		
+		pji->current_height += height;
+		if (pji->current_height > text_height)
+		{
+			PrintPageInfo info = {start, pji->current_line};
+			
+			/* New page */
+			pji->current_page++;
+			pji->current_height = height; 
+			g_array_append_val (pji->pages, info);
+			page_count++;
+		}
+		pji->current_line++;
+		
+		done = pji->current_pos >= pji->buffer_size;
+	}
+	
+	gtk_print_operation_set_n_pages (operation, pji->pages->len);
 	
-	gtk_widget_destroy (GTK_WIDGET (dialog));
-	return FALSE;
+	return done;
 }
 
+/* Second function called after displaying print dialog but before start
+ * printing */
 static void
-anjuta_print_preview_real (PrintJobInfo *pji)
-{
-	GtkWidget *gpmp = gnome_print_job_preview_new (pji->print_job,
-												   (const guchar *)_("Print Preview"));
-	gtk_widget_show (GTK_WIDGET (gpmp));
-}
-
-void
-anjuta_print (gboolean preview, AnjutaPreferences *p, TextEditor *te)
+anjuta_print_begin (GtkPrintOperation        *operation, 
+					GtkPrintContext          *context,
+					PrintJobInfo *pji)
 {
-	PrintJobInfo *pji;
-	gboolean cancel = FALSE;
-
-	scintilla_send_message (SCINTILLA (te->scintilla), SCI_COLOURISE, 0, -1);
+	gint i;
 	
-	if (NULL == (pji = anjuta_print_job_info_new(p, te)))
-		return;
-	pji->preview = preview;
-	if (!pji->preview)
-		cancel = anjuta_print_run_dialog(pji);
-	if (cancel)
+	/* Load Buffer to be printed. The buffer loaded is the text/style combination.*/
+	pji->buffer_size = scintilla_send_message(SCINTILLA(pji->te->scintilla), SCI_GETLENGTH, 0, 0);
+	pji->buffer = (gchar *) aneditor_command(pji->te->editor_id, ANE_GETSTYLEDTEXT, 0, pji->buffer_size);
+	if (pji->buffer == NULL)
 	{
+		anjuta_util_dialog_error(NULL, _("Unable to get text buffer for printing"));
+		gtk_print_operation_cancel (operation);
 		anjuta_print_job_info_destroy(pji);
-		return;
 	}
-	g_return_if_fail (pji->config != NULL);
+	
+	/* State variables initializations */
+	g_array_set_size (pji->pages, 0);
+	for (i = 0; i < AN_PRINT_MAX_STYLES; i++) pji->styles_pool[i] = NULL;
+	pji->current_style = 0;
+	pji->current_page = 0;
+	pji->current_pos = 0;
+	pji->current_height = 0.0;
+	pji->current_line = 1;
 
-	pji->print_job = gnome_print_job_new (pji->config);
-	g_return_if_fail (pji->print_job != NULL);
+	/* setup layout */
+	anjuta_setup_layout (pji, context);
+	
+	/* Margin settings */
+	anjuta_print_update_page_size_and_margins (pji, context);
+}
 
-	pji->pc = gnome_print_job_get_context (pji->print_job);
-	g_return_if_fail (pji->pc != NULL);
+/* First print function called before displayed print dialog */
+static GtkPrintOperation*
+anjuta_print_setup (AnjutaPreferences *p, TextEditor *te)
+{
+	PrintJobInfo *pji;
+	GtkPrintOperation* operation;
 
-	anjuta_print_update_page_size_and_margins (pji);
+	scintilla_send_message (SCINTILLA (te->scintilla), SCI_COLOURISE, 0, -1);
 	
-	anjuta_print_document(pji);
+	/* Anjuta print layout object */
+	pji = anjuta_print_job_info_new(te);
 	
-	if (pji->canceled)
+	/* Set preferences */
+	pji->print_line_numbers =
+		anjuta_preferences_get_int_with_default (p, PRINT_LINENUM_COUNT, 1);
+	pji->print_header =
+		anjuta_preferences_get_int_with_default (p, PRINT_HEADER, 1);
+	pji->print_color =
+		anjuta_preferences_get_int_with_default (p, PRINT_COLOR, 1);
+	pji->wrapping =
+		anjuta_preferences_get_int_with_default (p, PRINT_WRAP, 1);
+	pji->tab_width =
+		anjuta_preferences_get_int_with_default (p, TAB_SIZE, 8);
+	pji->zoom_factor = anjuta_preferences_get_int (te->preferences,
+						       TEXT_ZOOM_FACTOR);
+	
+	
+	/* Set progress bar */
+	pji->status = anjuta_shell_get_status (te->shell, NULL);
+	anjuta_status_progress_reset (pji->status);
+	anjuta_status_progress_add_ticks (pji->status, 100);
+
+	/* Gtk print operation object */
+	operation = gtk_print_operation_new ();
+
+	gtk_print_operation_set_job_name (operation, te->filename);
+	gtk_print_operation_set_show_progress (operation, TRUE);
+	
+	g_signal_connect (G_OBJECT (operation), "begin-print", 
+					  G_CALLBACK (anjuta_print_begin), pji);
+	g_signal_connect (G_OBJECT (operation), "paginate", 
+					  G_CALLBACK (anjuta_paginate), pji);
+	g_signal_connect (G_OBJECT (operation), "draw-page", 
+					  G_CALLBACK (anjuta_draw_page), pji);
+	g_signal_connect (G_OBJECT (operation), "end-print", 
+					  G_CALLBACK (anjuta_end_print), pji);
+	
+	return operation;
+}
+
+void
+anjuta_print (gboolean preview, AnjutaPreferences *p, TextEditor *te)
+{
+	GtkPrintOperation* operation;
+
+	if (te == NULL)
 	{
-		anjuta_print_job_info_destroy(pji);
+		anjuta_util_dialog_error (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET(te))),
+								  _("No file to print!"));
 		return;
 	}
 	
-	gnome_print_job_close (pji->print_job);
-	
-	if (pji->preview)
-		anjuta_print_preview_real(pji);
-	else
-		gnome_print_job_print (pji->print_job);
-	
-	anjuta_print_job_info_destroy (pji);
+	operation = anjuta_print_setup (p, te);
+	gtk_print_operation_run (operation, 
+							 preview ? GTK_PRINT_OPERATION_ACTION_PREVIEW :
+								 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
+								 NULL, NULL);
+	g_object_unref (operation);	
 }

Modified: trunk/plugins/editor/print.h
==============================================================================
--- trunk/plugins/editor/print.h	(original)
+++ trunk/plugins/editor/print.h	Tue Jan 20 21:26:42 2009
@@ -1,8 +1,9 @@
 /*
  * print.h
- * Copyright (C) 2002
- *     Biswapesh Chattopadhyay <biswapesh_chatterjee tcscal co in>
- *     Naba Kumar <kh_naba users sourceforge net>
+ *
+ * Copyright (C) 2002 Biswapesh Chattopadhyay <biswapesh_chatterjee tcscal co in>
+ * Copyright (C) 2002 Naba Kumar <kh_naba users sourceforge net>
+ * Copyright (C) 2008 Sebastien Granjoux <seb sfo free fr>
  * 
  * 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,15 +29,10 @@
 
 void anjuta_print (gboolean preview, AnjutaPreferences *p, TextEditor *te);
 
-#define PRINT_PAPER_SIZE           "print.paper.size"
 #define PRINT_HEADER               "print.header"
 #define PRINT_WRAP                 "print.linewrap"
 #define PRINT_LINENUM_COUNT        "print.linenumber.count"
 #define PRINT_LANDSCAPE            "print.landscape"
-#define PRINT_MARGIN_LEFT          "print.margin.left"
-#define PRINT_MARGIN_RIGHT         "print.margin.right"
-#define PRINT_MARGIN_TOP           "print.margin.top"
-#define PRINT_MARGIN_BOTTOM        "print.margin.bottom"
 #define PRINT_COLOR                "print.color"
 
 G_END_DECLS

Modified: trunk/plugins/editor/text_editor.c
==============================================================================
--- trunk/plugins/editor/text_editor.c	(original)
+++ trunk/plugins/editor/text_editor.c	Tue Jan 20 21:26:42 2009
@@ -399,7 +399,7 @@
 		
 		if (error != NULL)
 		{
-			DEBUG_PRINT ("%s", "Error while setting up file monitor: %s",
+			DEBUG_PRINT ("Error while setting up file monitor: %s",
 					   error->message);
 			g_error_free (error);
 		}



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