From 52337572af2d511322bd9ea94579162af72af056 Mon Sep 17 00:00:00 2001 From: Colin Leroy Date: Wed, 10 Oct 2007 16:32:11 +0000 Subject: [PATCH] 2007-10-10 [colin] 3.0.2cvs52 * src/printing.c * src/printing.h Generalize a bit printing, so we can factorize most of the code. --- ChangeLog | 7 +++ PATCHSETS | 1 + configure.ac | 2 +- src/printing.c | 118 +++++++++++++++++++++++++++++++++++-------------- src/printing.h | 16 +++++++ 5 files changed, 110 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6a2b3ed78..67c44461f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-10-10 [colin] 3.0.2cvs52 + + * src/printing.c + * src/printing.h + Generalize a bit printing, so we can factorize + most of the code. + 2007-10-10 [paul] 3.0.2cvs51 * src/addrduplicates.c diff --git a/PATCHSETS b/PATCHSETS index d425a8561..e95eed24d 100644 --- a/PATCHSETS +++ b/PATCHSETS @@ -2964,3 +2964,4 @@ ( cvs diff -u -r 1.9.2.17 -r 1.9.2.18 src/addressadd.c; cvs diff -u -r 1.28.2.35 -r 1.28.2.36 src/addrindex.c; cvs diff -u -r 1.14.2.33 -r 1.14.2.34 src/editaddress.c; cvs diff -u -r 1.1.2.5 -r 1.1.2.6 src/editaddress_other_attributes_ldap.h; cvs diff -u -r 1.8.2.24 -r 1.8.2.25 src/headerview.c; cvs diff -u -r 1.94.2.157 -r 1.94.2.158 src/messageview.c; cvs diff -u -r 1.395.2.329 -r 1.395.2.330 src/summaryview.c; cvs diff -u -r 1.96.2.185 -r 1.96.2.186 src/textview.c; ) > 3.0.2cvs49.patchset ( cvs diff -u -r 1.9.2.18 -r 1.9.2.19 src/addressadd.c; cvs diff -u -r 1.28.2.36 -r 1.28.2.37 src/addrindex.c; cvs diff -u -r 1.14.2.34 -r 1.14.2.35 src/editaddress.c; ) > 3.0.2cvs50.patchset ( cvs diff -u -r 1.1.2.4 -r 1.1.2.5 src/addrduplicates.c; ) > 3.0.2cvs51.patchset +( cvs diff -u -r 1.1.2.9 -r 1.1.2.10 src/printing.c; cvs diff -u -r 1.1.2.2 -r 1.1.2.3 src/printing.h; ) > 3.0.2cvs52.patchset diff --git a/configure.ac b/configure.ac index a3cf54dee..af72d8075 100644 --- a/configure.ac +++ b/configure.ac @@ -11,7 +11,7 @@ MINOR_VERSION=0 MICRO_VERSION=2 INTERFACE_AGE=0 BINARY_AGE=0 -EXTRA_VERSION=51 +EXTRA_VERSION=52 EXTRA_RELEASE= EXTRA_GTK2_VERSION= diff --git a/src/printing.c b/src/printing.c index 6f45d127f..cb980d781 100644 --- a/src/printing.c +++ b/src/printing.c @@ -37,19 +37,20 @@ #include #include -typedef struct { +struct _PrintData { + PrintRenderer *renderer; + gpointer renderer_data; PangoLayout *layout; PangoContext *pango_context; - char *text; + gpointer to_print; GList *page_breaks; guint npages; - GtkTextBuffer *buffer; gint sel_start; gint sel_end; GHashTable *images; gint img_cnt; gdouble zoom; -} PrintData; +}; typedef struct { GtkPrintOperation *op; @@ -76,8 +77,8 @@ typedef struct { } PreviewData; /* callbacks */ -static void cb_begin_print(GtkPrintOperation*, GtkPrintContext*, gpointer); -static void cb_draw_page(GtkPrintOperation*, GtkPrintContext*, gint, +static void printing_textview_cb_begin_print(GtkPrintOperation*, GtkPrintContext*, gpointer); +static void printing_textview_cb_draw_page(GtkPrintOperation*, GtkPrintContext*, gint, gpointer); static gboolean cb_preview(GtkPrintOperation*, GtkPrintOperationPreview*, GtkPrintContext*, GtkWindow*, gpointer); @@ -126,37 +127,50 @@ static void free_pixbuf(gpointer key, gpointer value, gpointer data) g_object_unref(G_OBJECT(attr->data)); } -void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, gint sel_end) +gpointer printing_get_renderer_data(PrintData *print_data) { + if (!print_data) + return NULL; + return print_data->renderer_data; +} + +gdouble printing_get_zoom(PrintData *print_data) +{ + if (!print_data) + return 1.0; + return print_data->zoom; +} + +void printing_set_n_pages(PrintData *print_data, gint n_pages) +{ + if (!print_data) + return; + print_data->npages = n_pages; +} + +void printing_print_full(GtkWindow *parent, PrintRenderer *renderer, gpointer renderer_data, + gint sel_start, gint sel_end) +{ GtkPrintOperation *op; GtkPrintOperationResult res; PrintData *print_data; - GtkTextIter start, end; - GtkTextBuffer *buffer; op = gtk_print_operation_new(); print_data = g_new0(PrintData,1); + print_data->renderer = renderer; + print_data->renderer_data = renderer_data; + print_data->sel_start = sel_start; + print_data->sel_end = sel_end; + print_data->zoom = 1.; print_data->images = g_hash_table_new(g_direct_hash, g_direct_equal); - print_data->pango_context=gtk_widget_get_pango_context(GTK_WIDGET(text_view)); + + print_data->pango_context = renderer->get_pango_context(renderer_data); - /* get text */ - buffer = gtk_text_view_get_buffer(text_view); - print_data->buffer = buffer; - print_data->sel_start = sel_start; - print_data->sel_end = sel_end; - if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) { - gtk_text_buffer_get_start_iter(buffer, &start); - gtk_text_buffer_get_end_iter(buffer, &end); - } else { - gtk_text_buffer_get_iter_at_offset(buffer, &start, print_data->sel_start); - gtk_text_buffer_get_iter_at_offset(buffer, &end, print_data->sel_end); - } - - print_data->text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE); + print_data->to_print = renderer->get_data_to_print(renderer_data, sel_start, sel_end); if (settings == NULL) { settings = gtk_print_settings_new(); @@ -180,8 +194,8 @@ void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, g gtk_print_operation_set_default_page_setup(op, page_setup); /* signals */ - g_signal_connect(op, "begin_print", G_CALLBACK(cb_begin_print), print_data); - g_signal_connect(op, "draw_page", G_CALLBACK(cb_draw_page), print_data); + g_signal_connect(op, "begin_print", G_CALLBACK(renderer->cb_begin_print), print_data); + g_signal_connect(op, "draw_page", G_CALLBACK(renderer->cb_draw_page), print_data); g_signal_connect(op, "preview", G_CALLBACK(cb_preview), print_data); /* Start printing process */ @@ -207,8 +221,8 @@ void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, g g_hash_table_foreach(print_data->images, free_pixbuf, NULL); g_hash_table_destroy(print_data->images); - if(print_data->text) - g_free(print_data->text); + if(print_data->to_print) + g_free(print_data->to_print); g_list_free(print_data->page_breaks); if(print_data->layout) g_object_unref(print_data->layout); @@ -219,6 +233,42 @@ void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, g debug_print("printing_print finished\n"); } +static PangoContext *printing_textview_get_pango_context(gpointer data) +{ + return gtk_widget_get_pango_context(GTK_WIDGET(data)); +} + +static gpointer printing_textview_get_data_to_print(gpointer data, gint sel_start, gint sel_end) +{ + GtkTextView *text_view = GTK_TEXT_VIEW(data); + GtkTextBuffer *buffer = gtk_text_view_get_buffer(text_view); + GtkTextIter start, end; + + if (sel_start < 0 || sel_end <= sel_start) { + gtk_text_buffer_get_start_iter(buffer, &start); + gtk_text_buffer_get_end_iter(buffer, &end); + } else { + gtk_text_buffer_get_iter_at_offset(buffer, &start, sel_start); + gtk_text_buffer_get_iter_at_offset(buffer, &end, sel_end); + } + + return gtk_text_buffer_get_text(buffer, &start, &end, FALSE); +} + +void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, gint sel_end) +{ + PrintRenderer *textview_renderer = g_new0(PrintRenderer, 1); + + textview_renderer->get_pango_context = printing_textview_get_pango_context; + textview_renderer->get_data_to_print = printing_textview_get_data_to_print; + textview_renderer->cb_begin_print = printing_textview_cb_begin_print; + textview_renderer->cb_draw_page = printing_textview_cb_draw_page; + + printing_print_full(parent, textview_renderer, text_view, sel_start, sel_end); + + g_free(textview_renderer); +} + void printing_page_setup(GtkWindow *parent) { GtkPageSetup *new_page_setup; @@ -697,7 +747,7 @@ static void cb_preview_request_page_setup(GtkPrintOperation *op, GTK_UNIT_INCH); } -static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context, +static void printing_textview_cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context, gpointer user_data) { double width, height; @@ -731,7 +781,7 @@ static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context, pango_font_description_free(desc); pango_layout_set_width(print_data->layout, width * PANGO_SCALE); - pango_layout_set_text(print_data->layout, print_data->text, -1); + pango_layout_set_text(print_data->layout, (char *)print_data->to_print, -1); printing_layout_set_text_attributes(print_data, context); @@ -858,7 +908,7 @@ static cairo_surface_t *pixbuf_to_surface(GdkPixbuf *pixbuf) return surface; } -static void cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context, +static void printing_textview_cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context, int page_nr, gpointer user_data) { cairo_t *cr; @@ -935,12 +985,14 @@ static void printing_layout_set_text_attributes(PrintData *print_data, GtkPrintC PangoAttrList *attr_list; PangoAttribute *attr; GSList *open_attrs, *attr_walk; + GtkTextView *text_view = GTK_TEXT_VIEW(print_data->renderer_data); + GtkTextBuffer *buffer = gtk_text_view_get_buffer(text_view); attr_list = pango_attr_list_new(); if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) { - gtk_text_buffer_get_start_iter(print_data->buffer, &iter); + gtk_text_buffer_get_start_iter(buffer, &iter); } else { - gtk_text_buffer_get_iter_at_offset(print_data->buffer, &iter, print_data->sel_start); + gtk_text_buffer_get_iter_at_offset(buffer, &iter, print_data->sel_start); } open_attrs = NULL; diff --git a/src/printing.h b/src/printing.h index 788780695..ce48f9cd3 100644 --- a/src/printing.h +++ b/src/printing.h @@ -28,8 +28,24 @@ #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT) +typedef struct _PrintData PrintData; + +typedef struct _PrintRenderer { + PangoContext *(*get_pango_context)(gpointer renderer_data); + gpointer (*get_data_to_print)(gpointer renderer_data, gint sel_start, gint sel_end); + void (*cb_begin_print)(GtkPrintOperation *op, GtkPrintContext *context, + gpointer user_data); + void (*cb_draw_page)(GtkPrintOperation* op, GtkPrintContext*, gint page_nr, + gpointer user_data); +} PrintRenderer; + void printing_print(GtkTextView*, GtkWindow*, gint, gint); +void printing_print_full(GtkWindow *parent, PrintRenderer *renderer, gpointer renderer_data, + gint sel_start, gint sel_end); void printing_page_setup(GtkWindow*); +gpointer printing_get_renderer_data(PrintData *print_data); +gdouble printing_get_zoom(PrintData *print_data); +void printing_set_n_pages(PrintData *print_data, gint n_pages); #endif /* GTK+ >= 2.10.0 */