[ghex] Port from libgnomeprint to GtkPrint



commit 8594b5601e5eb662e5ad16d02f77390f203e4f43
Author: RafaÅ MuÅyÅo <galtgendo gmail com>
Date:   Thu Aug 4 09:42:36 2011 +0300

    Port from libgnomeprint to GtkPrint
    
    https://bugzilla.gnome.org/show_bug.cgi?id=508563

 configure.ac      |    3 +-
 src/preferences.c |    5 +-
 src/preferences.h |    3 -
 src/print.c       |  411 ++++++++++++++++++++--------------------------------
 src/print.h       |   30 ++--
 src/ui.c          |  175 +++--------------------
 6 files changed, 194 insertions(+), 433 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 5b5708e..ca12d77 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,8 +43,7 @@ AC_SUBST(LIBGTKHEX_CFLAGS)
 PKG_CHECK_MODULES(GHEX,
 		  gio-2.0                       \
 		  gtk+-2.0 >= 2.24.0 		\
-		  gconf-2.0			\
-		  libgnomeprintui-2.2)
+		  gconf-2.0)
 AC_SUBST(GHEX_CFLAGS)
 AC_SUBST(GHEX_LIBS)
 
diff --git a/src/preferences.c b/src/preferences.c
index 53dfcf3..ed6720f 100644
--- a/src/preferences.c
+++ b/src/preferences.c
@@ -35,8 +35,7 @@
 
 #define MAX_MAX_UNDO_DEPTH 100000
 
-static void select_font_cb(GtkWidget *w, const gchar *font_name,
-						   PropertyUI *pui);
+static void select_font_cb(GtkWidget *w, PropertyUI *pui);
 static void select_display_font_cb(GtkWidget *w, PropertyUI *pui);
 static void max_undo_changed_cb(GtkAdjustment *adj, PropertyUI *pui);
 static void box_size_changed_cb(GtkAdjustment *adj, PropertyUI *pui);
@@ -509,7 +508,7 @@ select_display_font_cb(GtkWidget *w, PropertyUI *pui)
 }
 
 static void
-select_font_cb(GtkWidget *w, const gchar *font_name, PropertyUI *pui)
+select_font_cb(GtkWidget *w, PropertyUI *pui)
 {
 	if(w == pui->df_button) {
 		if(data_font_name)
diff --git a/src/preferences.h b/src/preferences.h
index a70d4f0..0374247 100644
--- a/src/preferences.h
+++ b/src/preferences.h
@@ -24,8 +24,6 @@
 #ifndef __GHEX_PREFERENCES_H__
 #define __GHEX_PREFERENCES_H__
 
-#include <libgnomeprint/gnome-font.h>
-
 G_BEGIN_DECLS
 
 typedef struct _PropertyUI PropertyUI;
@@ -38,7 +36,6 @@ struct _PropertyUI {
 	GtkWidget *paper_sel, *print_font_sel;
 	GtkWidget *df_button, *hf_button;
 	GtkWidget *df_label, *hf_label;
-	GnomeFont *data_font, *header_font;
 };
 
 extern PropertyUI *prefs_ui;
diff --git a/src/print.c b/src/print.c
index 06fddef..d0fa9a8 100644
--- a/src/print.c
+++ b/src/print.c
@@ -29,26 +29,16 @@
 
 #include <glib/gi18n.h>
 
-#include <libgnomeprintui/gnome-print-dialog.h>
-
 #include "print.h"
 #include "gtkhex.h"
 #include "ui.h"
 
-#define is_printable(c) (((((guchar)c)>=0x20) && (((guchar)c)<=0x7F))?1:0)
-
-/* Taken from gedit SnM */
-#define CM(v) ((v) * 72.0 / 2.54)
-#define A4_WIDTH (210.0 * 72 / 25.4)
-#define A4_HEIGHT (297.0 * 72 / 25.4)
-
-const guchar *def_paper; /* Using guchar instead -- SnM */
+#define is_printable(c) (((((guchar)c)>=0x20) && (((guchar)c)<0x7F))?1:0)
 
 gchar *data_font_name, *header_font_name;
 gint shaded_box_size;
 
 static void print_header(GHexPrintJobInfo *pji, unsigned int page);
-static void end_page(GnomePrintContext *pc);
 static void print_row(GHexPrintJobInfo *pji, unsigned int offset,
 					  unsigned int bytes, int row);
 static void format_hex(HexDocument *doc, guint gt, gchar *out,
@@ -58,79 +48,101 @@ static void format_ascii(HexDocument *doc, gchar *out,
 static void print_shaded_boxes( GHexPrintJobInfo *pji, guint page,
 								guint max_row);
 static void print_shaded_box( GHexPrintJobInfo *pji, guint row, guint rows);
-static gboolean print_verify_fonts (void);
 
 static void print_header(GHexPrintJobInfo *pji, unsigned int page)
 {
-	guchar* text1 = g_filename_to_utf8(pji->doc->file_name, -1, NULL,
+	PangoLayout *layout;
+	cairo_t *cr = gtk_print_context_get_cairo_context (pji->pc);
+	gchar *text1 = g_filename_to_utf8 (pji->doc->file_name, -1, NULL,
 									   NULL, NULL);
-	guchar* text2 = g_strdup_printf(_("Page: %i/%i"),page,pji->pages);
-	guchar* pagetext = g_strdup_printf("%d", page);
-	gfloat x, y, len;
-	
-	gnome_print_beginpage(pji->pc, pagetext);
-	gnome_print_setfont(pji->pc, pji->h_font);
+	gchar *text2 = g_strdup_printf (_("Page: %i/%i"), page, pji->pages);
+	gchar *pagetext = g_strdup_printf ("%d", page);
+	gdouble x, y;
+	gint width, height;
+
+	layout = gtk_print_context_create_pango_layout (pji->pc);
+	pango_layout_set_text (layout, pagetext, -1);
+	pango_layout_set_font_description (layout, pji->h_font);
+	pango_layout_set_indent (layout, 0);
+	cairo_move_to (cr, 0, 0);
+	pango_cairo_show_layout (cr, layout);
+	g_object_unref (layout);
+
 	/* Print the file name */
-	y = pji->page_height - pji->margin_top -
-		2.1*gnome_font_get_ascender(pji->h_font) -
-		1.1*gnome_font_get_descender(pji->h_font);
-	len = gnome_font_get_width_utf8 (pji->h_font, text1);
-	x = pji->page_width/2 - len/2;
-	gnome_print_moveto(pji->pc, x, y);
-	gnome_print_show(pji->pc, text1);
+	layout = gtk_print_context_create_pango_layout (pji->pc);
+	pango_layout_set_text (layout, text1, -1);
+	pango_layout_set_font_description (layout, pji->h_font);
+	pango_layout_set_indent (layout, 0);
+	pango_layout_get_pixel_size (layout, &width, &height);
+	x = (gtk_print_context_get_width (pji->pc) - width) / 2;
+	y = height;
+	cairo_move_to (cr, x, y);
+	pango_cairo_show_layout (cr, layout);
+	g_object_unref (layout);
 
 	/* Print the page/pages  */
-	y = pji->page_height - pji->margin_top -
-		gnome_font_get_ascender(pji->h_font);
-	len = gnome_font_get_width_utf8 (pji->h_font, text2);
-	x = pji->page_width - len - 36;
-	gnome_print_moveto(pji->pc, x, y);
-	gnome_print_show(pji->pc, text2);
+	layout = gtk_print_context_create_pango_layout (pji->pc);
+	pango_layout_set_text (layout, text2, -1);
+	pango_layout_set_font_description (layout, pji->h_font);
+	pango_layout_set_indent (layout, 0);
+	pango_layout_get_pixel_size (layout, &width, &height);
+	x = gtk_print_context_get_width (pji->pc) - width - 36;
+	cairo_move_to (cr, x, 0);
+	pango_cairo_show_layout (cr, layout);
+	g_object_unref (layout);
 
 	g_free(text1);
 	g_free(text2);
 	g_free(pagetext);
 }
 
-#define TEMP_LEN 256
-
 static void print_row(GHexPrintJobInfo *pji, unsigned int offset,
 					  unsigned int bytes, int row)
 {
-	gfloat x, y;
+	PangoLayout *layout;
+	gdouble x, y;
+	const int TEMP_LEN = 256;
 	gchar *temp = g_malloc(TEMP_LEN + 1);
+	cairo_t *cr = gtk_print_context_get_cairo_context (pji->pc);
 
-	y = pji->page_height -
-		pji->margin_top -
-		pji->header_height -
+	y = pji->header_height +
 		(pji->font_char_height*(row + 1));
 	/* Print Offset */ 
-	x = pji->margin_left;
-	gnome_print_moveto(pji->pc, x , y);
+	cairo_move_to (cr, 0, y);
+	layout = gtk_print_context_create_pango_layout (pji->pc);
 	g_snprintf(temp, TEMP_LEN, "%08X", offset);
-	gnome_print_show(pji->pc, temp);
+	pango_layout_set_text (layout, temp, -1);
+	pango_layout_set_font_description (layout, pji->d_font);
+	pango_layout_set_indent (layout, 0);
+	pango_cairo_show_layout (cr, layout);
+	g_object_unref (layout);
 	/* Print Hex */
-	x = pji->margin_left +
-		pji->font_char_width*pji->offset_chars +
+	x = pji->font_char_width*pji->offset_chars +
 		pji->pad_size ;
-	gnome_print_moveto(pji->pc, x, y);
+	cairo_move_to (cr, x, y);
 	format_hex(pji->doc, pji->gt, temp, offset, offset + bytes);
-	gnome_print_show(pji->pc, temp);
+	layout = gtk_print_context_create_pango_layout (pji->pc);
+	pango_layout_set_text (layout, temp, -1);
+	pango_layout_set_font_description (layout, pji->d_font);
+	pango_layout_set_indent (layout, 0);
+	pango_cairo_show_layout (cr, layout);
+	g_object_unref (layout);
 	/* Print Ascii */
-	x = 2*pji->pad_size + pji->margin_left + pji->font_char_width*
-		(pji->offset_chars + pji->bytes_per_row*(2 + (1/((float)pji->gt))));
-	gnome_print_moveto(pji->pc, x, y);
+	x = 2*pji->pad_size + pji->font_char_width*
+		(pji->offset_chars + 2*pji->bytes_per_row
+		+ pji->bytes_per_row/pji->gt - 1);
+	cairo_move_to (cr, x, y);
 	format_ascii(pji->doc, temp, offset, offset + bytes);
-	gnome_print_show(pji->pc, temp);
+	layout = gtk_print_context_create_pango_layout (pji->pc);
+	pango_layout_set_text (layout, temp, -1);
+	pango_layout_set_font_description (layout, pji->d_font);
+	pango_layout_set_indent (layout, 0);
+	pango_cairo_show_layout (cr, layout);
+	g_object_unref (layout);
 
 	g_free(temp);
 }
 
-static void end_page(GnomePrintContext *pc)
-{
-	gnome_print_showpage(pc);
-}
-
 static void format_hex(HexDocument *doc, guint gt, gchar *out,
 					   guint start, guint end)
 {
@@ -178,136 +190,25 @@ static void print_shaded_boxes(GHexPrintJobInfo *pji, guint page,
 	for(i = box_size + 1;
 		i <= pji->rows_per_page && i <= max_row;
 		i += box_size*2)
-		print_shaded_box(pji, i, ((i + box_size - 1) > max_row ?
+		print_shaded_box (pji, i+1, ((i + box_size - 1) > max_row ?
 								  max_row - i + 1 : box_size));
 }
 
 static void print_shaded_box(GHexPrintJobInfo *pji, guint row, guint rows)
 {
-	gfloat box_top = 0 ;
-	gfloat box_bottom = 0;
-	gfloat box_right = 0;
-	gfloat box_left = 0;
-	gfloat box_adjustment_top = 0;
-	gfloat box_adjustment_bottom = 0;
-
-	box_adjustment_top = pji->font_char_height*0.25;
-	box_adjustment_bottom = pji->font_char_height*0.125;
-	box_top = pji->page_height - pji->margin_top - pji->header_height -
-		      row*pji->font_char_height - box_adjustment_top;
-	box_bottom = box_top - rows*pji->font_char_height + box_adjustment_bottom;
-	box_left   = pji->margin_left;
-	box_right  = pji->page_width - pji->margin_right;
-	
-	gnome_print_setrgbcolor(pji->pc, 0.95, 0.95, 0.95);
-	gnome_print_moveto(pji->pc, box_right, box_top);
-	gnome_print_lineto(pji->pc, box_left,  box_top);
-	gnome_print_lineto(pji->pc, box_left,  box_bottom);
-	gnome_print_lineto(pji->pc, box_right, box_bottom);
-	gnome_print_lineto(pji->pc, box_right, box_top);
-	gnome_print_closepath(pji->pc);
-	gnome_print_fill(pji->pc);
-	gnome_print_setrgbcolor(pji->pc, 0.0, 0.0, 0.0);
-}
-
-static gboolean print_verify_fonts()
-{
-	GnomeFont *test_font;
-	guchar *test_font_name;
-
-	test_font_name = g_strdup(data_font_name); 
-	test_font = gnome_font_find_closest_from_full_name(test_font_name);
-	if(test_font == NULL)
-	{
-		gchar *errstr =
-			g_strdup_printf(_("GHex could not find the font \"%s\".\n"
-							  "GHex is unable to print without this font "
-							  "installed."),
-							test_font_name);
-		display_error_dialog (ghex_window_get_active(), errstr);
-		g_free(errstr);
-		return FALSE;
-	}
-	gnome_font_unref(test_font);
-	g_free(test_font_name);
-	
-	test_font_name = g_strdup(header_font_name);
-	test_font = gnome_font_find_closest_from_full_name(test_font_name);
-	if(test_font==NULL)
-	{
-		gchar *errstr =
-			g_strdup_printf(_("GHex could not find the font \"%s\".\n"
-							  "GHex is unable to print without this font "
-							  "installed."),
-							test_font_name);
-		display_error_dialog (ghex_window_get_active(), errstr);
-		g_free(errstr);
-		return FALSE;
-	}
-	gnome_font_unref(test_font);
-	g_free(test_font_name);	
-	return TRUE;
-}
-
-void
-ghex_print_update_page_size_and_margins (HexDocument *doc,
-										 GHexPrintJobInfo *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,
-									   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,
-									   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,
-									   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,
-									   GNOME_PRINT_KEY_PAGE_MARGIN_BOTTOM,
-				&pji->margin_bottom, &unit))
-	{
-		gnome_print_convert_distance (&pji->margin_bottom, unit,
-									  GNOME_PRINT_PS_UNIT);
-	}
-
-	pji->printable_width = pji->page_width -
-		pji->margin_left -
-		pji->margin_right;
-	pji->printable_height = pji->page_height -
-		pji->margin_top -
-		pji->margin_bottom;
-
-	pji->bytes_per_row = (pji->printable_width - pji->pad_size*2 -
-						(pji->offset_chars *
-						 pji->font_char_width))/
-				((3 + (1/((float)pji->gt))) *
-				 pji->font_char_width);
-	pji->bytes_per_row -= pji->bytes_per_row % pji->gt;
-	pji->rows_per_page = (pji->printable_height - pji->header_height) /
-		pji->font_char_height - 1;
-	pji->pages = (((doc->file_size/pji->bytes_per_row) + 1)/
-				pji->rows_per_page) + 1;
-	pji->page_first = 1;
-	pji->page_last = pji->pages;
+	gdouble box_top;
+	cairo_t *cr = gtk_print_context_get_cairo_context (pji->pc);
+
+	box_top = pji->header_height + row * pji->font_char_height;
+
+	cairo_save (cr);
+	cairo_set_source_rgb (cr, 0.90, 0.90, 0.90);
+	cairo_rectangle (cr,
+	                 0, box_top,
+	                 gtk_print_context_get_width(pji->pc),
+	                 rows * pji->font_char_height);
+	cairo_fill (cr);
+	cairo_restore (cr);
 }
 
 /**
@@ -324,22 +225,20 @@ GHexPrintJobInfo *
 ghex_print_job_info_new(HexDocument *doc, guint group_type)
 {
 	GHexPrintJobInfo *pji;
-	GnomeFont *d_font;
-	GnomeFont *h_font;
-	guint32 glyph;
-	ArtPoint point;
+	PangoFontDescription *d_font;
+	PangoFontDescription *h_font;
 
-	if (!doc || !print_verify_fonts())
+	if (!doc)
 		return NULL;
 
 	/* Create the header and data fonts */
-	d_font = gnome_font_find_closest_from_full_name(data_font_name);
+	d_font = pango_font_description_from_string (data_font_name);
 	if (!d_font)
 		return NULL;
 
-	h_font = gnome_font_find_closest_from_full_name(header_font_name);
+	h_font = pango_font_description_from_string (header_font_name);
 	if (!h_font) {
-		gnome_font_unref(d_font);
+		pango_font_description_free (d_font);
 		return NULL;
 	}
 
@@ -352,30 +251,11 @@ ghex_print_job_info_new(HexDocument *doc, guint group_type)
 
 	pji->doc = doc;
 
-	pji->page_width = A4_WIDTH;
-	pji->page_height = A4_HEIGHT;
-
-	pji->margin_top = CM (1);
-	pji->margin_bottom = CM (1);
-	pji->margin_left = CM (1);
-	pji->margin_right = CM (1);
-	pji->header_height = 2.5 * gnome_font_get_size (pji->h_font);
-
-	/* Get font character width */
-	glyph = ' ';
-	gnome_font_get_glyph_stdadvance(GNOME_FONT(d_font), glyph, &point);
-	pji->font_char_width = point.x;
-
-	pji->font_char_height = gnome_font_get_size (GNOME_FONT(d_font));
-
-	/* Add 10% spacing between lines */
-	pji->font_char_height *= 1.1;
 	pji->pad_size = .5 * 72;
 	pji->offset_chars = 8;
 
 	pji->preview = FALSE;
 	pji->config = NULL;
-	pji->range = GNOME_PRINT_RANGE_ALL;
 
 	return pji;
 }
@@ -389,14 +269,11 @@ ghex_print_job_info_new(HexDocument *doc, guint group_type)
 void
 ghex_print_job_info_destroy(GHexPrintJobInfo *pji)
 {
-	gnome_font_unref(pji->h_font);
-	gnome_font_unref(pji->d_font);
+	pango_font_description_free (pji->h_font);
+	pango_font_description_free (pji->d_font);
 
 	if (pji->config != NULL)
-		gnome_print_config_unref (pji->config);
-
-	if (pji->pc != NULL)
-		g_object_unref (pji->pc);
+		g_object_unref (pji->config);
 
 	if (pji->master != NULL)
 		g_object_unref (pji->master);
@@ -404,54 +281,82 @@ ghex_print_job_info_destroy(GHexPrintJobInfo *pji)
 	g_free(pji);
 }
 
-/**
- * ghex_print_job_execute:
- * @pji: Pointer to the GHexPrintJobInfo object.
- *
- * Performs the printing job described by the GHexPrintJobInfo object.
- **/
 void
-ghex_print_job_execute(GHexPrintJobInfo *pji,
-					   void (*progress_func)(gint, gint, gpointer),
-					   gpointer data)
+begin_print (GtkPrintOperation *operation,
+             GtkPrintContext   *context,
+             gpointer           data)
 {
-	gint i;
-	gint j;
+    PangoLayout *layout;
+    GHexPrintJobInfo *pji = (GHexPrintJobInfo *)data;
+    pji->pc = context;
+    gint font_width, font_height;
+    gint printable_width, printable_height;
+
+    layout = gtk_print_context_create_pango_layout (context);
+    pango_layout_set_text (layout, " ", -1);
+    pango_layout_set_font_description (layout, pji->h_font);
+    pango_layout_set_indent (layout, 0);
+    pango_layout_get_pixel_size (layout, NULL, &font_height);
+    pji->header_height = 2 * font_height;
+    g_object_unref (layout);
+
+    layout = gtk_print_context_create_pango_layout (context);
+    pango_layout_set_font_description (layout, pji->d_font);
+    pango_layout_set_indent (layout, 0);
+    pango_layout_set_text (layout, " ", -1);
+    pango_layout_get_pixel_size (layout, &font_width, &font_height);
+    pji->font_char_width = font_width;
+    pji->font_char_height = font_height;
+    g_object_unref (layout);
+
+    printable_height = gtk_print_context_get_height (pji->pc);
+    printable_width = gtk_print_context_get_width (pji->pc);
+
+    pji->bytes_per_row = (printable_width - pji->pad_size * 2 -
+                          (pji->offset_chars *
+                           pji->font_char_width)) / pji->font_char_width;
+    pji->bytes_per_row /= 3*pji->gt + 1;
+    pji->bytes_per_row *= pji->gt;
+    pji->rows_per_page = (printable_height - pji->header_height) /
+                          pji->font_char_height - 2;
+    pji->pages = (((pji->doc->file_size/pji->bytes_per_row) + 1)/
+                   pji->rows_per_page) + 1;
+    gtk_print_operation_set_n_pages (pji->master, pji->pages);
+}
 
-	g_return_if_fail(pji != NULL);
+void
+print_page (GtkPrintOperation *operation,
+            GtkPrintContext   *context,
+            gint               page_nr,
+            gpointer           data)
+{
+	gint j, max_row;
 
-	pji->pc = gnome_print_job_get_context(pji->master);
+	GHexPrintJobInfo *pji = (GHexPrintJobInfo *)data;
+	g_return_if_fail(pji != NULL);
 
+	pji->pc = context;
 	g_return_if_fail(pji->pc != NULL);
 
-	for(i = pji->page_first; i <= pji->page_last; i++) {
-		int max_row;
-		print_header(pji, i);
-		gnome_print_setfont(pji->pc, pji->d_font);
-		max_row = (pji->bytes_per_row*pji->rows_per_page*(i) >
-				pji->doc->file_size ?
-				(int)((pji->doc->file_size-1)-
-				      (pji->bytes_per_row *
-				       pji->rows_per_page*(i-1))) /
-				pji->bytes_per_row + 1:
-				pji->rows_per_page);
-		print_shaded_boxes(pji, i, max_row);
-		for(j = 1; j <= pji->rows_per_page; j++) {
-			int file_offset = pji->bytes_per_row*(j - 1) +
-				pji->bytes_per_row*pji->rows_per_page*(i - 1);
-			int length;
-			length = (file_offset + pji->bytes_per_row >
-					pji->doc->file_size ?
-					pji->doc->file_size - file_offset :
-					pji->bytes_per_row);
-			if(file_offset >= pji->doc->file_size)
-				break;
-			print_row(pji, file_offset, length, j);
-		}
-		end_page(pji->pc);
-		progress_func(i - pji->page_first + 1,
-					  pji->page_last - pji->page_first + 1, data);
+	print_header (pji, page_nr+1);
+	max_row = (pji->bytes_per_row*pji->rows_per_page*(page_nr+1) >
+			pji->doc->file_size ?
+			(int)((pji->doc->file_size-1)-
+			      (pji->bytes_per_row *
+			       pji->rows_per_page*(page_nr))) /
+			       pji->bytes_per_row + 1:
+			       pji->rows_per_page);
+	print_shaded_boxes (pji, page_nr, max_row);
+	for (j = 1; j <= pji->rows_per_page; j++) {
+		int file_offset = pji->bytes_per_row*(j - 1) +
+			pji->bytes_per_row*pji->rows_per_page*(page_nr);
+		int length = (file_offset + pji->bytes_per_row >
+			pji->doc->file_size ?
+			pji->doc->file_size - file_offset :
+			pji->bytes_per_row);
+		if (file_offset >= pji->doc->file_size)
+			break;
+		print_row (pji, file_offset, length, j);
 	}
-	gnome_print_job_close(pji->master);
 }
 
diff --git a/src/print.h b/src/print.h
index 82e9735..ebe3a8a 100644
--- a/src/print.h
+++ b/src/print.h
@@ -24,8 +24,7 @@
 #ifndef __GHEX_PRINT_H__
 #define __GHEX_PRINT_H__
 
-#include <libgnomeprint/gnome-print.h>
-#include <libgnomeprint/gnome-print-job.h>
+#include <gtk/gtk.h>
 
 #include "hex-document.h"
 
@@ -34,25 +33,22 @@ G_BEGIN_DECLS
 typedef struct _GHexPrintJobInfo  GHexPrintJobInfo;
 
 struct _GHexPrintJobInfo {
-	GnomePrintJob *master;
-	GnomePrintContext *pc;
-	GnomePrintConfig *config;
+	GtkPrintOperation *master;
+	GtkPrintContext *pc;
+	GtkPrintSettings *config;
 
-	GnomeFont *d_font, *h_font;
+	PangoFontDescription *d_font, *h_font;
 	HexDocument *doc;
 
 	int   pages;
 	gint range;
 	gint page_first;
 	gint page_last;
-	gdouble page_width, page_height;
-	gdouble margin_top, margin_bottom, margin_left, margin_right;
-	gdouble printable_width, printable_height;
 
 	gdouble header_height;
 	
-	gdouble font_char_width;
-	gdouble font_char_height;
+	gint font_char_width;
+	gint font_char_height;
 
 	int   bytes_per_row, rows_per_page;
 	gdouble pad_size;
@@ -62,11 +58,13 @@ struct _GHexPrintJobInfo {
 };
 
 /* printing */
-void ghex_print_job_execute(GHexPrintJobInfo *pji,
-			    void (*progress_func)(gint, gint, gpointer),
-			    gpointer data);
-void ghex_print_update_page_size_and_margins (HexDocument *doc,
-					      GHexPrintJobInfo *pji);
+void begin_print (GtkPrintOperation *operation,
+                  GtkPrintContext   *context,
+                  gpointer           data);
+void print_page (GtkPrintOperation *operation,
+                 GtkPrintContext   *context,
+                 gint               page_nr,
+                 gpointer           data);
 GHexPrintJobInfo *ghex_print_job_info_new(HexDocument *doc, guint group_type);
 void ghex_print_job_info_destroy(GHexPrintJobInfo *pji);
 
diff --git a/src/ui.c b/src/ui.c
index 97eee71..4c8035d 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -28,9 +28,6 @@
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 
-#include <libgnomeprint/gnome-print.h>
-#include <libgnomeprintui/gnome-print-dialog.h>
-#include <libgnomeprintui/gnome-print-job-preview.h>
 
 #include "ui.h"
 #include "ghex-window.h"
@@ -40,9 +37,6 @@
 #include "chartable.h"
 
 static void ghex_print(GtkHex *gh, gboolean preview);
-static void ghex_print_run_dialog(GHexPrintJobInfo *pji);
-static void ghex_print_preview_real(GHexPrintJobInfo *pji);
-static void ghex_print_document_real (GHexPrintJobInfo *pji, gboolean preview);
 
 guint group_type[3] = {
 	GROUP_BYTE,
@@ -776,10 +770,23 @@ ghex_print(GtkHex *gh, gboolean preview)
 {
 	GHexPrintJobInfo *pji;
 	HexDocument *doc;
+	GtkPrintOperationResult result;
+	GError *error = NULL;
 
 	doc = gh->document;
 
 	pji = ghex_print_job_info_new(doc, gh->group_type);
+	pji->master = gtk_print_operation_new ();
+	pji->config = gtk_print_settings_new ();
+	gtk_print_settings_set_paper_size (pji->config, gtk_paper_size_new (GTK_PAPER_NAME_A4));
+	gtk_print_operation_set_unit (pji->master, GTK_UNIT_POINTS);
+	gtk_print_operation_set_print_settings (pji->master, pji->config);
+	gtk_print_operation_set_embed_page_setup (pji->master, TRUE);
+	gtk_print_operation_set_show_progress (pji->master, TRUE);
+	g_signal_connect (pji->master, "draw-page",
+	                  G_CALLBACK (print_page), pji);
+	g_signal_connect (pji->master, "begin-print",
+	                  G_CALLBACK (begin_print), pji);
 
 	if (!pji)
 		return;
@@ -787,159 +794,15 @@ ghex_print(GtkHex *gh, gboolean preview)
 	pji->preview = preview;
 
 	if (!pji->preview)
-		ghex_print_run_dialog(pji);
-	else
-		pji->config = gnome_print_config_default();
-
-	if (pji->preview) {
-		ghex_print_document_real(pji, preview);
-		ghex_print_job_info_destroy(pji);
-	}
-}
-
-static void
-ghex_print_progress(gint page, gint ofpages, gpointer data)
-{
-	GtkProgressBar *progress_bar = GTK_PROGRESS_BAR(data);
-	gchar *progress_str;
-
-	gtk_progress_bar_set_fraction(progress_bar,
-								  (gdouble)page/(gdouble)ofpages);
-	progress_str = g_strdup_printf("%d/%d", page, ofpages);
-	gtk_progress_bar_set_text(progress_bar, progress_str);	
-	g_free(progress_str);
-	while(gtk_events_pending())
-		gtk_main_iteration();
-}
-
-static gboolean
-ignore_cb(GtkWidget *w, GdkEventAny *e, gpointer user_data)
-{
-	return TRUE;
-}
-
-static void
-ghex_print_document_real(GHexPrintJobInfo *pji, gboolean preview)
-{
-	GtkWidget *progress_dialog, *progress_bar;
-
-	g_return_if_fail (pji->config != NULL);
-
-	pji->master = gnome_print_job_new (pji->config);
-
-	g_return_if_fail (pji->master != NULL);
-
-	ghex_print_update_page_size_and_margins (pji->doc, pji);
-
-	progress_dialog = gtk_dialog_new();
-	gtk_window_set_resizable(GTK_WINDOW(progress_dialog), FALSE);
-	gtk_window_set_modal(GTK_WINDOW(progress_dialog), TRUE);
-	g_signal_connect(G_OBJECT(progress_dialog), "delete-event",
-					 G_CALLBACK(ignore_cb), NULL);
-	gtk_window_set_title(GTK_WINDOW(progress_dialog),
-						 _("Printing file..."));
-	progress_bar = gtk_progress_bar_new();
-	gtk_widget_show(progress_bar);
-	gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(progress_dialog))),
-					  progress_bar);
-	gtk_widget_show(progress_dialog);
-
-	ghex_print_job_execute(pji, ghex_print_progress, progress_bar);
-
-	gtk_widget_destroy(progress_dialog);
-
-	if(pji->preview)
-		ghex_print_preview_real(pji);
+		result = gtk_print_operation_run (pji->master, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, NULL, &error);
 	else
-		gnome_print_job_print(pji->master);
-}
-
-static void
-ghex_print_dialog_response (GtkWidget *dialog, int response,
-							GHexPrintJobInfo *pji)
-{
-	pji->preview = FALSE;
- 
-	switch (response) {
-	case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
-		break;
-	case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
-		pji->preview = TRUE;
-		break;
-	default:
-		gtk_widget_destroy(dialog);
-		ghex_print_job_info_destroy (pji);
-		return;
-	}
+		result = gtk_print_operation_run (pji->master, GTK_PRINT_OPERATION_ACTION_PREVIEW, NULL, &error);
 
-	pji->range = gnome_print_dialog_get_range_page
-		(GNOME_PRINT_DIALOG(dialog), &pji->page_first, &pji->page_last);
-	
-	ghex_print_document_real (pji, pji->preview);
-	
-	if (response == GNOME_PRINT_DIALOG_RESPONSE_PRINT) {
-		ghex_print_job_info_destroy (pji);
-		gtk_widget_destroy(dialog);
-	}
-}
-
-/**
- * ghex_print_run_dialog
- * @pji: Pointer to a GHexPrintJobInfo object.
- *
- * Return value: TRUE if cancel was clicked, FALSE otherwise.
- *
- * Runs the GHex print dialog.
- **/
-static void
-ghex_print_run_dialog(GHexPrintJobInfo *pji)
-{
-	GtkWidget *dialog;
-
-	dialog = gnome_print_dialog_new(pji->master,
-			     (const char *) _("Print Hex Document"),
-			     GNOME_PRINT_DIALOG_RANGE);
-
-	if (pji->config == NULL) {
-		pji->config = gnome_print_dialog_get_config(
-						GNOME_PRINT_DIALOG(dialog));
+	if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
+		g_print ("%s\n", error->message);
+		g_error_free (error);
 	}
-
-	ghex_print_update_page_size_and_margins (pji->doc, pji);
-
-	gnome_print_dialog_construct_range_page((GnomePrintDialog *)dialog,
-			GNOME_PRINT_RANGE_ALL | GNOME_PRINT_RANGE_RANGE,
-			1, pji->pages, "A", _("Pages"));
-
-	if(ghex_window_get_active() != NULL)
-		gtk_window_set_transient_for(GTK_WINDOW(dialog),
-					     GTK_WINDOW(ghex_window_get_active()));
-
-	g_signal_connect (dialog, "response",
-			  G_CALLBACK (ghex_print_dialog_response),
-			  pji);
-
-	gtk_widget_show (dialog);
-}
-
-/**
- * ghex_print_preview_real:
- * @pji: Pointer to a GHexPrintJobInfo object.
- *
- * Previews the print job.
- **/
-static void
-ghex_print_preview_real(GHexPrintJobInfo *pji)
-{
-	GtkWidget *preview;
-	gchar *title;
-
-	title = g_strdup_printf(_("GHex (%s): Print Preview"),
-			pji->doc->file_name);
-	preview = gnome_print_job_preview_new(pji->master, title);
-	g_free(title);
-
-	gtk_widget_show(preview);
+	ghex_print_job_info_destroy (pji);
 }
 
 void



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