2007-09-25 [holger] 3.0.1cvs27
[claws.git] / src / printing.c
1 /* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
2  * Copyright (C) 2007 Holger Berndt <hb@claws-mail.org>,
3  * Colin Leroy <colin@colino.net>, and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "printing.h"
25 #include "image_viewer.h"
26
27 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
28
29 #include "gtkutils.h"
30 #include "toolbar.h"
31 #include "prefs_common.h"
32
33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <gtk/gtk.h>
36 #include <pango/pango.h>
37 #include <string.h>
38 #include <math.h>
39
40 typedef struct {
41   PangoLayout *layout;
42   PangoContext *pango_context;
43   char *text;
44   GList *page_breaks;
45   guint npages;
46   GtkTextBuffer *buffer;
47   gint sel_start;
48   gint sel_end;
49   GHashTable *images;
50   gint img_cnt;
51   gdouble zoom;
52 } PrintData;
53
54 typedef struct {
55   GtkPrintOperation *op;
56   GtkPrintOperationPreview *preview;
57   GtkWidget         *dialog;
58   GtkWidget         *scrolled_window;
59   GtkWidget         *area;
60   PrintData         *print_data;
61   GtkWidget         *page_nr_label;
62   GList             *pages_to_print;
63   GList             *current_page;
64   GtkWidget *first;
65   GtkWidget *next;
66   GtkWidget *previous;
67   GtkWidget *last;
68   GtkWidget *close;
69   GtkWidget *zoom_100;
70   GtkWidget *zoom_fit;
71   GtkWidget *zoom_in;
72   GtkWidget *zoom_out;
73   gboolean rendering;
74   gint page_width;
75   gint page_height;
76 } PreviewData;
77
78 /* callbacks */
79 static void     cb_begin_print(GtkPrintOperation*, GtkPrintContext*, gpointer);
80 static void     cb_draw_page(GtkPrintOperation*, GtkPrintContext*, gint,
81                              gpointer);
82 static gboolean cb_preview(GtkPrintOperation*, GtkPrintOperationPreview*,
83                            GtkPrintContext*, GtkWindow*, gpointer);
84 static void     cb_preview_destroy(GtkWindow*, gpointer);
85 static gboolean cb_preview_close(GtkWidget*, GdkEventAny*, gpointer);
86 static void     cb_preview_size_allocate(GtkWidget*, GtkAllocation*);
87 static void     cb_preview_ready(GtkPrintOperationPreview*,
88                                  GtkPrintContext*, gpointer);
89 static gboolean cb_preview_expose(GtkWidget*, GdkEventExpose*, gpointer);
90 static void     cb_preview_got_page_size(GtkPrintOperationPreview*,
91                                          GtkPrintContext*,
92                                          GtkPageSetup*, gpointer);
93 static void     cb_preview_go_first(GtkButton*, gpointer);
94 static void     cb_preview_go_previous(GtkButton*, gpointer);
95 static void     cb_preview_go_next(GtkButton*, gpointer);
96 static void     cb_preview_go_last(GtkButton*, gpointer);
97 static void     cb_preview_btn_close(GtkButton*, gpointer);
98 static void     cb_preview_zoom_100(GtkButton*, gpointer);
99 static void     cb_preview_zoom_fit(GtkButton*, gpointer);
100 static void     cb_preview_zoom_in(GtkButton*, gpointer);
101 static void     cb_preview_zoom_out(GtkButton*, gpointer);
102 static void     cb_preview_request_page_setup(GtkPrintOperation*,
103                                               GtkPrintContext*,
104                                               gint,GtkPageSetup*,gpointer);
105
106 static void     printing_preview_update_zoom_sensitivity(PreviewData*);
107
108 /* variables */
109 static GtkPrintSettings *settings   = NULL;
110 static GtkPageSetup     *page_setup = NULL;
111
112 /* other static functions */
113 static void     printing_layout_set_text_attributes(PrintData*, GtkPrintContext *);
114 static gboolean printing_is_pango_gdk_color_equal(PangoColor*, GdkColor*); 
115 static gint     printing_text_iter_get_offset_bytes(PrintData *, const GtkTextIter*);
116
117 #define PREVIEW_SCALE 72
118 #define PREVIEW_SHADOW_OFFSET 3
119 #define PREVIEW_ZOOM_FAC 1.41
120 #define PREVIEW_ZOOM_MAX 10.
121 #define PREVIEW_ZOOM_MIN 0.2
122
123 static void free_pixbuf(gpointer key, gpointer value, gpointer data)
124 {
125   PangoAttrShape *attr = (PangoAttrShape *) value;
126   g_object_unref(G_OBJECT(attr->data));
127 }
128
129 void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, gint sel_end)
130 {
131   GtkPrintOperation *op;
132   GtkPrintOperationResult res;
133   PrintData *print_data;
134   GtkTextIter start, end;
135   GtkTextBuffer *buffer;
136
137   op = gtk_print_operation_new();
138
139   print_data = g_new0(PrintData,1);
140
141   print_data->zoom = 1.;
142
143   print_data->images = g_hash_table_new(g_direct_hash, g_direct_equal);
144   print_data->pango_context=gtk_widget_get_pango_context(GTK_WIDGET(text_view));
145
146   /* get text */
147   buffer = gtk_text_view_get_buffer(text_view);
148   print_data->buffer = buffer;
149   print_data->sel_start = sel_start;
150   print_data->sel_end = sel_end;
151   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
152     gtk_text_buffer_get_start_iter(buffer, &start);
153     gtk_text_buffer_get_end_iter(buffer, &end);
154   } else {
155     gtk_text_buffer_get_iter_at_offset(buffer, &start, print_data->sel_start);
156     gtk_text_buffer_get_iter_at_offset(buffer, &end, print_data->sel_end);
157   }
158
159   print_data->text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
160
161   if (settings == NULL) {
162     settings = gtk_print_settings_new();
163     gtk_print_settings_set_use_color(settings, prefs_common.print_use_color);
164     gtk_print_settings_set_collate(settings, prefs_common.print_use_collate);
165     gtk_print_settings_set_reverse(settings, prefs_common.print_use_reverse);
166     gtk_print_settings_set_duplex(settings, prefs_common.print_use_duplex);
167   }
168   if (page_setup == NULL) {
169     page_setup = gtk_page_setup_new();
170     if (prefs_common.print_paper_type && *prefs_common.print_paper_type) {
171       GtkPaperSize *paper = gtk_paper_size_new(prefs_common.print_paper_type);
172       gtk_page_setup_set_paper_size(page_setup, paper);
173       gtk_paper_size_free(paper);
174     }
175     gtk_page_setup_set_orientation(page_setup, prefs_common.print_paper_orientation);
176   }
177   
178   /* Config for printing */
179   gtk_print_operation_set_print_settings(op, settings);
180   gtk_print_operation_set_default_page_setup(op, page_setup);
181
182   /* signals */
183   g_signal_connect(op, "begin_print", G_CALLBACK(cb_begin_print), print_data);
184   g_signal_connect(op, "draw_page", G_CALLBACK(cb_draw_page), print_data);
185   g_signal_connect(op, "preview", G_CALLBACK(cb_preview), print_data);
186
187   /* Start printing process */
188   res = gtk_print_operation_run(op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
189                                 parent, NULL);
190
191   if(res == GTK_PRINT_OPERATION_RESULT_ERROR) {
192     GError *error = NULL;
193     gtk_print_operation_get_error(op, &error);
194     debug_print("Error printing message: %s",
195             error ? error->message : "no details");
196   }
197   else if(res == GTK_PRINT_OPERATION_RESULT_APPLY) {
198     /* store settings for next printing session */
199     if(settings != NULL)
200       g_object_unref(settings);
201     settings = g_object_ref(gtk_print_operation_get_print_settings(op));
202     prefs_common.print_use_color = gtk_print_settings_get_use_color(settings);
203     prefs_common.print_use_collate = gtk_print_settings_get_collate(settings);
204     prefs_common.print_use_reverse = gtk_print_settings_get_reverse(settings);
205     prefs_common.print_use_duplex = gtk_print_settings_get_duplex(settings);
206   }
207
208   g_hash_table_foreach(print_data->images, free_pixbuf, NULL);
209   g_hash_table_destroy(print_data->images);
210   if(print_data->text)
211     g_free(print_data->text);
212   g_list_free(print_data->page_breaks);
213   if(print_data->layout)
214     g_object_unref(print_data->layout);
215
216   g_free(print_data);
217
218   g_object_unref(op);
219   debug_print("printing_print finished\n");
220 }
221
222 void printing_page_setup(GtkWindow *parent)
223 {
224   GtkPageSetup *new_page_setup;
225
226   if(settings == NULL) {
227     settings = gtk_print_settings_new();
228     gtk_print_settings_set_use_color(settings, prefs_common.print_use_color);
229     gtk_print_settings_set_collate(settings, prefs_common.print_use_collate);
230     gtk_print_settings_set_reverse(settings, prefs_common.print_use_reverse);
231     gtk_print_settings_set_duplex(settings, prefs_common.print_use_duplex);
232   }
233   if (page_setup == NULL) {
234     page_setup = gtk_page_setup_new();
235     if (prefs_common.print_paper_type && *prefs_common.print_paper_type) {
236       GtkPaperSize *paper = gtk_paper_size_new(prefs_common.print_paper_type);
237       gtk_page_setup_set_paper_size(page_setup, paper);
238       gtk_paper_size_free(paper);
239     }
240     gtk_page_setup_set_orientation(page_setup, prefs_common.print_paper_orientation);
241   }
242
243   new_page_setup = gtk_print_run_page_setup_dialog(parent,page_setup,settings);
244
245   if(page_setup)
246     g_object_unref(page_setup);
247
248   page_setup = new_page_setup;
249   
250   g_free(prefs_common.print_paper_type);
251   prefs_common.print_paper_type = g_strdup(gtk_paper_size_get_name(
252                 gtk_page_setup_get_paper_size(page_setup)));
253   prefs_common.print_paper_orientation = gtk_page_setup_get_orientation(page_setup);
254 }
255
256 static gboolean cb_preview(GtkPrintOperation        *operation,
257                            GtkPrintOperationPreview *preview,
258                            GtkPrintContext          *context,
259                            GtkWindow                *parent,
260                            gpointer                 data)
261 {
262   PrintData *print_data;
263   cairo_t *cr;
264   PreviewData *preview_data;
265   GtkWidget *vbox;
266   GtkWidget *toolbar;
267   GtkWidget *da;
268   GtkWidget *sw;
269   GtkWidget *page;
270   GtkToolItem *separator;
271   static GdkGeometry geometry;
272   GtkWidget *dialog = NULL;
273   GtkTooltips *toolbar_tips = gtk_tooltips_new();
274   GtkWidget *statusbar = gtk_hbox_new(2, FALSE);
275   debug_print("Creating internal print preview\n");
276
277   print_data = (PrintData*) data;
278
279   preview_data = g_new0(PreviewData,1);
280   preview_data->print_data = print_data;
281   preview_data->op = g_object_ref(operation);
282   preview_data->preview = preview;
283
284   /* Window */
285   dialog = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "print_preview");
286   preview_data->dialog = dialog;
287   if(!geometry.min_height) {
288     geometry.min_width = 600;
289     geometry.min_height = 400;
290   }
291   gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
292                                 GDK_HINT_MIN_SIZE);
293   gtk_widget_set_size_request(dialog, prefs_common.print_previewwin_width,
294                               prefs_common.print_previewwin_height);
295   gtk_window_set_title(GTK_WINDOW(dialog), _("Print preview"));
296
297   /* vbox */
298   vbox = gtk_vbox_new(FALSE, 0);
299   gtk_container_add(GTK_CONTAINER(dialog), vbox);
300   
301   /* toolbar */
302   toolbar = gtk_toolbar_new();
303   gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar), GTK_ORIENTATION_HORIZONTAL);
304   switch (prefs_common.toolbar_style) {
305   case TOOLBAR_ICON:
306     gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
307     break;
308   case TOOLBAR_TEXT:
309     gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_TEXT);
310     break;
311   case TOOLBAR_BOTH_HORIZ:
312     gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH_HORIZ);
313     break;
314   case TOOLBAR_BOTH:
315   default:        
316     gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH);
317   }
318   gtk_toolbar_set_show_arrow(GTK_TOOLBAR(toolbar), TRUE);
319         
320   gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
321
322 #define TOOLBAR_ITEM(item,text,tooltip,cb,cbdata) {                                                             \
323         item = GTK_WIDGET(gtk_tool_button_new_from_stock(text));                                        \
324         gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(item), FALSE);                                      \
325         gtk_tool_item_set_is_important(GTK_TOOL_ITEM(item), TRUE);                                      \
326         gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), -1);                              \
327         g_signal_connect (G_OBJECT(item), "clicked", G_CALLBACK(cb), cbdata);                           \
328         gtk_tool_item_set_tooltip(GTK_TOOL_ITEM(item), GTK_TOOLTIPS(toolbar_tips),                      \
329                         tooltip, NULL);                                                                 \
330 }
331
332   TOOLBAR_ITEM(preview_data->first, GTK_STOCK_GOTO_FIRST, _("First page"), cb_preview_go_first, preview_data);
333   TOOLBAR_ITEM(preview_data->previous, GTK_STOCK_GO_BACK, _("Previous page"), cb_preview_go_previous, preview_data);
334   
335   page = gtk_label_new("");
336   gtk_widget_set_size_request(page, 100, -1);
337   preview_data->page_nr_label = page;
338   
339   TOOLBAR_ITEM(preview_data->next, GTK_STOCK_GO_FORWARD, _("Next page"), cb_preview_go_next, preview_data);
340   TOOLBAR_ITEM(preview_data->last, GTK_STOCK_GOTO_LAST, _("Last page"), cb_preview_go_last, preview_data);
341   
342   separator = gtk_separator_tool_item_new();
343   gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(separator), -1);
344
345   TOOLBAR_ITEM(preview_data->zoom_100, GTK_STOCK_ZOOM_100, _("Zoom 100%"), cb_preview_zoom_100, preview_data);
346   TOOLBAR_ITEM(preview_data->zoom_fit, GTK_STOCK_ZOOM_FIT, _("Zoom fit"), cb_preview_zoom_fit, preview_data);
347   TOOLBAR_ITEM(preview_data->zoom_in, GTK_STOCK_ZOOM_IN, _("Zoom in"), cb_preview_zoom_in, preview_data);
348   TOOLBAR_ITEM(preview_data->zoom_out, GTK_STOCK_ZOOM_OUT, _("Zoom out"), cb_preview_zoom_out, preview_data);
349
350   separator = gtk_separator_tool_item_new();
351   gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(separator), -1);
352
353   /* tooltip has to be NULL else it triggers an expose_event */
354   TOOLBAR_ITEM(preview_data->close, GTK_STOCK_CLOSE, NULL, cb_preview_btn_close, preview_data);
355
356   gtk_widget_show(statusbar);
357   gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
358   gtk_box_pack_start(GTK_BOX(statusbar), page, FALSE, FALSE, 0);
359   /* Drawing area */
360   sw = gtk_scrolled_window_new(NULL, NULL);
361   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
362                                  GTK_POLICY_AUTOMATIC,
363                                  GTK_POLICY_AUTOMATIC);
364   gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0);
365   da = gtk_drawing_area_new();
366   gtk_widget_set_double_buffered(da, FALSE);
367   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw),
368                                         da);
369   gtk_widget_realize(da);
370   preview_data->scrolled_window = sw;
371   preview_data->area = da;
372
373   /* cairo context */
374   cr = gdk_cairo_create(da->window);
375   gtk_print_context_set_cairo_context(context, cr, PREVIEW_SCALE, PREVIEW_SCALE);
376   cairo_destroy(cr);
377
378   /* signals */
379   g_signal_connect(dialog, "key_press_event",
380                    G_CALLBACK(cb_preview_close), preview_data);
381   g_signal_connect(dialog, "size_allocate",
382                    G_CALLBACK(cb_preview_size_allocate), NULL);
383   g_signal_connect(dialog, "destroy",G_CALLBACK(cb_preview_destroy),
384                    preview_data);
385   g_signal_connect(preview, "ready", G_CALLBACK(cb_preview_ready),
386                    preview_data);
387   g_signal_connect(preview, "got-page-size",
388                    G_CALLBACK(cb_preview_got_page_size), preview_data);
389
390   g_signal_connect(operation, "request-page-setup",
391                    G_CALLBACK(cb_preview_request_page_setup), preview_data);
392
393   gtk_widget_show_all(dialog);
394   return TRUE;
395 }
396
397 static void cb_preview_destroy(GtkWindow *window, gpointer data)
398 {
399   PreviewData *preview_data;
400   preview_data = (PreviewData*) data;
401
402   if (preview_data->rendering)
403     return;
404   debug_print("Preview window destroyed\n");
405
406   gtk_print_operation_preview_end_preview(preview_data->preview);
407   g_object_unref(preview_data->op);
408   g_list_free(preview_data->pages_to_print);
409
410   g_free(preview_data);
411 }
412
413 static gboolean cb_preview_close(GtkWidget *widget, GdkEventAny *event,
414                                  gpointer data)
415 {
416   PreviewData *preview_data = (PreviewData *)data;
417   if(event->type == GDK_KEY_PRESS)
418     if(((GdkEventKey *)event)->keyval != GDK_Escape)
419       return FALSE;
420   if (preview_data->rendering)
421     return FALSE; 
422   gtk_widget_destroy(widget);
423   return FALSE;
424 }
425
426 static void cb_preview_size_allocate(GtkWidget *widget,
427                                      GtkAllocation *allocation)
428 {
429   g_return_if_fail(allocation != NULL);
430
431   prefs_common.print_previewwin_width = allocation->width;
432   prefs_common.print_previewwin_height = allocation->height;
433 }
434
435 static void cb_preview_ready(GtkPrintOperationPreview *preview,
436                              GtkPrintContext          *context,
437                              gpointer                  data)
438 {
439   PreviewData *preview_data;
440   gint iPage;
441   preview_data = (PreviewData*) data;
442   debug_print("preview_ready %d\n", preview_data->print_data->npages);
443   
444   for(iPage = 0; iPage < (preview_data->print_data->npages); iPage++) {
445     if(gtk_print_operation_preview_is_selected(preview_data->preview, iPage)) {
446       preview_data->pages_to_print = 
447         g_list_prepend(preview_data->pages_to_print, GINT_TO_POINTER(iPage));
448       debug_print("want to print page %d\n",iPage+1);
449     }
450   }
451
452   preview_data->pages_to_print = g_list_reverse(preview_data->pages_to_print);
453   preview_data->current_page = preview_data->pages_to_print;
454
455   g_signal_connect(preview_data->area, "expose_event",
456                    G_CALLBACK(cb_preview_expose),
457                    preview_data);
458
459   gtk_widget_queue_draw(preview_data->area);
460 }
461
462 static void cb_preview_got_page_size(GtkPrintOperationPreview *preview,
463                                      GtkPrintContext          *context,
464                                      GtkPageSetup             *page_setup,
465                                      gpointer                  data)
466 {
467   PreviewData *preview_data;
468   GtkPageOrientation orientation;
469   GtkPaperSize *paper_size;
470   gint paper_width;
471   gint paper_height;
472
473   preview_data = (PreviewData*) data;
474   debug_print("got_page_size\n");
475   orientation  = gtk_page_setup_get_orientation(page_setup);
476   paper_size   = gtk_page_setup_get_paper_size(page_setup);
477   paper_width  = (gint)(gtk_paper_size_get_width(paper_size, GTK_UNIT_INCH)  
478                         * PREVIEW_SCALE);
479   paper_height = (gint)(gtk_paper_size_get_height(paper_size,  GTK_UNIT_INCH)
480                         * PREVIEW_SCALE);
481
482   preview_data->page_width  = paper_width;
483   preview_data->page_height = paper_height;
484
485   debug_print("w/h %d/%d\n", paper_width, paper_height);
486   gtk_widget_set_size_request(GTK_WIDGET(preview_data->area), 
487                               paper_width, paper_height);
488 }
489
490 static gboolean cb_preview_expose(GtkWidget *widget, GdkEventExpose *event,
491                                   gpointer data)
492 {
493   PreviewData *preview_data = data;
494   GdkGC *gc;
495   GdkColor white;
496   GdkColor black;
497   GdkColor gray;
498
499   debug_print("preview_expose (current %p)\n", preview_data->current_page);
500   gdk_window_clear(preview_data->area->window);
501
502   white.red   = 65535;
503   white.green = 65535;
504   white.blue  = 65535;
505   black.red   = 0;
506   black.green = 0;
507   black.blue  = 0;
508   gray.red   = 32700;
509   gray.green = 32700;
510   gray.blue  = 32700;
511
512   gc = gdk_gc_new(GDK_DRAWABLE(preview_data->area->window));
513
514   /* background */
515   gdk_gc_set_rgb_fg_color(gc, &gray);
516   gdk_draw_rectangle(preview_data->area->window, gc, TRUE, 0, 0,
517                      preview_data->area->allocation.width,
518                      preview_data->area->allocation.height);
519
520   /* shadow */
521   gdk_gc_set_rgb_fg_color(gc, &black);
522   gdk_draw_rectangle(preview_data->area->window, gc, TRUE,
523                      PREVIEW_SHADOW_OFFSET, PREVIEW_SHADOW_OFFSET,
524                      preview_data->page_width+PREVIEW_SHADOW_OFFSET,
525                      preview_data->page_height+PREVIEW_SHADOW_OFFSET);
526
527   /* paper */
528   gdk_gc_set_rgb_fg_color(gc, &white);
529   gdk_draw_rectangle(preview_data->area->window, gc, TRUE, 0, 0,
530                      preview_data->page_width,
531                      preview_data->page_height);
532
533   g_object_unref(gc);
534
535   if(preview_data->current_page) {
536     preview_data->rendering = TRUE;
537     gtk_widget_set_sensitive(preview_data->close, FALSE);
538     int cur = GPOINTER_TO_INT(preview_data->current_page->data);
539     gchar *str;
540     str = g_strdup_printf(_("Page %d"), cur+1);
541     gtk_label_set_text(GTK_LABEL(preview_data->page_nr_label), str);
542     g_free(str);
543     gtk_print_operation_preview_render_page(preview_data->preview,
544                                             GPOINTER_TO_INT
545                                             (preview_data->current_page->data));
546
547     gtk_widget_set_sensitive(preview_data->first, preview_data->current_page->prev != NULL);
548     gtk_widget_set_sensitive(preview_data->previous, preview_data->current_page->prev != NULL);
549     gtk_widget_set_sensitive(preview_data->next, preview_data->current_page->next != NULL);
550     gtk_widget_set_sensitive(preview_data->last, preview_data->current_page->next != NULL);
551     gtk_widget_set_sensitive(preview_data->close, TRUE);
552     preview_data->rendering = FALSE;
553
554   }
555   return TRUE;
556 }
557
558 static void cb_preview_go_first(GtkButton *button, gpointer data)
559 {
560   PreviewData *preview_data = (PreviewData*) data;
561   preview_data->current_page = preview_data->pages_to_print;
562   gtk_widget_queue_draw(preview_data->area);
563 }
564
565 static void cb_preview_go_previous(GtkButton *button, gpointer data)
566 {
567   GList *next;
568   PreviewData *preview_data = (PreviewData*) data;
569   next = g_list_previous(preview_data->current_page);
570   if(next)
571     preview_data->current_page = next;
572   gtk_widget_queue_draw(preview_data->area);
573 }
574
575 static void cb_preview_go_next(GtkButton *button, gpointer data)
576 {
577   GList *next;
578   PreviewData *preview_data = (PreviewData*) data;
579   next = g_list_next(preview_data->current_page);
580   if(next)
581     preview_data->current_page = next;
582   gtk_widget_queue_draw(preview_data->area);
583 }
584
585 static void cb_preview_go_last(GtkButton *button, gpointer data)
586 {
587   PreviewData *preview_data = (PreviewData*) data;
588   preview_data->current_page = g_list_last(preview_data->current_page);
589   gtk_widget_queue_draw(preview_data->area);
590 }
591
592 static void cb_preview_btn_close(GtkButton *button, gpointer data)
593 {
594   PreviewData *preview_data = (PreviewData *)data;
595   if (preview_data->rendering)
596     return; 
597   gtk_widget_destroy(preview_data->dialog);
598 }
599
600 static void cb_preview_zoom_100(GtkButton *button, gpointer data)
601 {
602   PreviewData *preview_data = (PreviewData*) data;
603   if(preview_data->print_data->zoom != 1.) {
604     preview_data->print_data->zoom = 1.;
605     gtk_widget_queue_draw(preview_data->area);
606     printing_preview_update_zoom_sensitivity(preview_data);
607   }
608 }
609
610 static void cb_preview_zoom_fit(GtkButton *button, gpointer data)
611 {
612   PreviewData *preview_data = (PreviewData*) data;
613   gdouble zoom_w;
614   gdouble zoom_h;
615   
616   zoom_w = ((gdouble)preview_data->scrolled_window->allocation.width) /
617     ((gdouble)preview_data->page_width/preview_data->print_data->zoom +
618      PREVIEW_SHADOW_OFFSET);
619   zoom_h = ((gdouble)preview_data->scrolled_window->allocation.height) /
620     ((gdouble)preview_data->page_height/preview_data->print_data->zoom +
621      PREVIEW_SHADOW_OFFSET);
622
623   preview_data->print_data->zoom = MIN(zoom_w,zoom_h) - 0.01;
624
625   if(preview_data->print_data->zoom > PREVIEW_ZOOM_MAX)
626     preview_data->print_data->zoom = PREVIEW_ZOOM_MAX;
627   else if(preview_data->print_data->zoom < PREVIEW_ZOOM_MIN)
628     preview_data->print_data->zoom = PREVIEW_ZOOM_MIN;
629
630   printing_preview_update_zoom_sensitivity(preview_data);
631   gtk_widget_queue_draw(preview_data->area);
632 }
633
634 static void cb_preview_zoom_in(GtkButton *button, gpointer data)
635 {
636   PreviewData *preview_data = (PreviewData*) data;
637   gdouble new_zoom;
638   new_zoom =  preview_data->print_data->zoom * PREVIEW_ZOOM_FAC;
639   if(new_zoom <= PREVIEW_ZOOM_MAX) {
640     preview_data->print_data->zoom = new_zoom;
641     printing_preview_update_zoom_sensitivity(preview_data);
642     gtk_widget_queue_draw(preview_data->area);
643   }
644 }
645
646 static void cb_preview_zoom_out(GtkButton *button, gpointer data)
647 {
648   PreviewData *preview_data = (PreviewData*) data;
649   gdouble new_zoom;
650   new_zoom =  preview_data->print_data->zoom / PREVIEW_ZOOM_FAC;
651   if(new_zoom >= PREVIEW_ZOOM_MIN) {
652     preview_data->print_data->zoom = new_zoom;
653     printing_preview_update_zoom_sensitivity(preview_data);
654     gtk_widget_queue_draw(preview_data->area);
655   }
656 }
657
658 static void cb_preview_request_page_setup(GtkPrintOperation *op,
659                                           GtkPrintContext *context,
660                                           gint page_nr,
661                                           GtkPageSetup *setup,gpointer data)
662 {
663   GtkPaperSize *paper_size;
664   GtkPaperSize *old_size;
665   gdouble width;
666   gdouble height;
667   gdouble top_margin;
668   gdouble bottom_margin;
669   gdouble left_margin;
670   gdouble right_margin;
671
672   PreviewData *preview_data = (PreviewData*) data;
673
674   old_size = gtk_page_setup_get_paper_size(setup);
675   width  = gtk_paper_size_get_width(old_size,GTK_UNIT_INCH);
676   height = gtk_paper_size_get_height(old_size,GTK_UNIT_INCH);
677
678   top_margin    = gtk_page_setup_get_top_margin(setup,GTK_UNIT_INCH);
679   bottom_margin = gtk_page_setup_get_bottom_margin(setup,GTK_UNIT_INCH);
680   left_margin   = gtk_page_setup_get_left_margin(setup,GTK_UNIT_INCH);
681   right_margin  = gtk_page_setup_get_right_margin(setup,GTK_UNIT_INCH);
682
683   paper_size = gtk_paper_size_new_custom("preview paper", "preview_paper",
684                                          width*preview_data->print_data->zoom,
685                                          height*preview_data->print_data->zoom,
686                                          GTK_UNIT_INCH);
687   gtk_page_setup_set_paper_size(setup, paper_size);
688   gtk_paper_size_free(paper_size);
689
690   gtk_page_setup_set_top_margin(setup,top_margin*preview_data->print_data->zoom,
691                                 GTK_UNIT_INCH);
692   gtk_page_setup_set_bottom_margin(setup,bottom_margin*preview_data->print_data->zoom,
693                                    GTK_UNIT_INCH);
694   gtk_page_setup_set_left_margin(setup,left_margin*preview_data->print_data->zoom,
695                                  GTK_UNIT_INCH);
696   gtk_page_setup_set_right_margin(setup,right_margin*preview_data->print_data->zoom,
697                                   GTK_UNIT_INCH);
698 }
699
700 static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
701                            gpointer user_data)
702 {
703   double width, height;
704   int num_lines;
705   double page_height;
706   GList *page_breaks;
707   PrintData *print_data;
708   PangoFontDescription *desc;
709   int start, ii;
710   PangoLayoutIter *iter;
711   double start_pos;
712   double line_height =0.;
713
714   print_data = (PrintData*) user_data;
715
716   debug_print("Preparing print job...\n");
717         
718   width  = gtk_print_context_get_width(context);
719   height = gtk_print_context_get_height(context);
720
721   if (print_data->layout == NULL)
722     print_data->layout = gtk_print_context_create_pango_layout(context);
723         
724   if(prefs_common.use_different_print_font)
725     desc = pango_font_description_from_string(prefs_common.printfont);
726   else
727     desc = pango_font_description_copy(
728                 pango_context_get_font_description(print_data->pango_context));
729
730   pango_layout_set_font_description(print_data->layout, desc);
731   pango_font_description_free(desc);
732
733   pango_layout_set_width(print_data->layout, width * PANGO_SCALE);
734   pango_layout_set_text(print_data->layout, print_data->text, -1);
735
736   printing_layout_set_text_attributes(print_data, context);
737
738   num_lines = pango_layout_get_line_count(print_data->layout);
739
740   page_breaks = NULL;
741   page_height = 0;
742   start = 0;
743   ii = 0;
744   start_pos = 0.;
745   iter = pango_layout_get_iter(print_data->layout);
746   do {
747     PangoRectangle logical_rect;
748     PangoLayoutLine *line;
749     PangoAttrShape *attr = NULL;
750     int baseline;
751
752     if(ii >= start) {
753       line = pango_layout_iter_get_line(iter);
754
755       pango_layout_iter_get_line_extents(iter, NULL, &logical_rect);
756       baseline = pango_layout_iter_get_baseline(iter);
757
758       if ((attr = g_hash_table_lookup(print_data->images, GINT_TO_POINTER(pango_layout_iter_get_index(iter)))) != NULL) {
759         line_height = (double)gdk_pixbuf_get_height(GDK_PIXBUF(attr->data));
760       } else {
761         line_height = ((double)logical_rect.height) / PANGO_SCALE;
762       }
763     }
764     if((page_height + line_height) > height) {
765       page_breaks = g_list_prepend(page_breaks, GINT_TO_POINTER(ii));
766       page_height = 0;
767     }
768
769     page_height += line_height;
770     ii++;
771   } while(ii < num_lines && pango_layout_iter_next_line(iter));
772   pango_layout_iter_free(iter);
773
774   page_breaks = g_list_reverse(page_breaks);
775   print_data->npages = g_list_length(page_breaks) + 1;  
776   print_data->page_breaks = page_breaks;
777
778   gtk_print_operation_set_n_pages(op, print_data->npages);
779
780   debug_print("Starting print job...\n");
781 }
782
783 static cairo_surface_t *pixbuf_to_surface(GdkPixbuf *pixbuf)
784 {
785           cairo_surface_t *surface;
786           cairo_format_t format;
787           static const cairo_user_data_key_t key;
788           guchar *pixels = g_malloc(
789                                 4*
790                                 gdk_pixbuf_get_width(pixbuf)*
791                                 gdk_pixbuf_get_height(pixbuf));
792           guchar *src_pixels = gdk_pixbuf_get_pixels (pixbuf);
793           gint width = gdk_pixbuf_get_width(pixbuf);
794           gint height = gdk_pixbuf_get_height(pixbuf);
795           gint nchans = gdk_pixbuf_get_n_channels (pixbuf);
796           gint stride = gdk_pixbuf_get_rowstride (pixbuf);
797           gint j;
798
799           if (nchans == 3)
800             format = CAIRO_FORMAT_RGB24;
801           else
802             format = CAIRO_FORMAT_ARGB32;
803           surface = cairo_image_surface_create_for_data (pixels,         
804                                                 format, width, height, 4*width);
805           cairo_surface_set_user_data (surface, &key, 
806                 pixels, (cairo_destroy_func_t)g_free);
807
808           for (j = height; j; j--) {
809                 guchar *p = src_pixels;
810                 guchar *q = pixels;
811
812                 if (nchans == 3) {
813                         guchar *end = p + 3 * width;
814
815                         while (p < end) {
816 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
817                                 q[0] = p[2];
818                                 q[1] = p[1];
819                                 q[2] = p[0];
820 #else
821                                 q[1] = p[0];
822                                 q[2] = p[1];
823                                 q[3] = p[2];
824 #endif
825                                 p += 3;
826                                 q += 4;
827                         }
828                 } else {
829                         guchar *end = p + 4 * width;
830                         guint t1,t2,t3;
831
832 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
833
834                         while (p < end) {
835 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
836                                 MULT(q[0], p[2], p[3], t1);
837                                 MULT(q[1], p[1], p[3], t2);
838                                 MULT(q[2], p[0], p[3], t3);
839                                 q[3] = p[3];
840 #else
841                                 q[0] = p[3];
842                                 MULT(q[1], p[0], p[3], t1);
843                                 MULT(q[2], p[1], p[3], t2);
844                                 MULT(q[3], p[2], p[3], t3);
845 #endif
846
847                                 p += 4;
848                                 q += 4;
849                         }
850
851 #undef MULT
852                 }
853
854                 src_pixels += stride;
855                 pixels += 4 * width;
856         }
857                 
858         return surface;
859 }
860
861 static void cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
862                          int page_nr, gpointer user_data)
863 {
864   cairo_t *cr;
865   PrintData *print_data;
866   int start, end, ii;
867   GList *pagebreak;
868   PangoLayoutIter *iter;
869   double start_pos;
870   gboolean notlast = TRUE;
871
872   print_data = (PrintData*) user_data;
873
874   if (page_nr == 0) {
875     start = 0;
876   } else {
877     pagebreak = g_list_nth(print_data->page_breaks, page_nr - 1);
878     start = GPOINTER_TO_INT(pagebreak->data);
879   }
880
881   pagebreak = g_list_nth(print_data->page_breaks, page_nr);
882   if(pagebreak == NULL)
883     end = pango_layout_get_line_count(print_data->layout);
884   else
885     end = GPOINTER_TO_INT(pagebreak->data);
886
887   cr = gtk_print_context_get_cairo_context(context);
888   cairo_scale(cr, print_data->zoom, print_data->zoom);
889   cairo_set_source_rgb(cr, 0., 0., 0.);
890
891   ii = 0;
892   start_pos = 0.;
893   iter = pango_layout_get_iter(print_data->layout);
894   do {
895     PangoRectangle logical_rect;
896     PangoLayoutLine *line;
897     PangoAttrShape *attr = NULL;
898     int baseline;
899
900     if(ii >= start) {
901       line = pango_layout_iter_get_line(iter);
902
903       pango_layout_iter_get_line_extents(iter, NULL, &logical_rect);
904       baseline = pango_layout_iter_get_baseline(iter);
905
906       if(ii == start)
907         start_pos = ((double)logical_rect.y) / PANGO_SCALE;
908       
909       cairo_move_to(cr,
910                     ((double)logical_rect.x) / PANGO_SCALE,
911                     ((double)baseline) / PANGO_SCALE - start_pos);
912                     
913       if ((attr = g_hash_table_lookup(print_data->images, GINT_TO_POINTER(pango_layout_iter_get_index(iter)))) != NULL) {
914           cairo_surface_t *surface;
915
916           surface = pixbuf_to_surface(GDK_PIXBUF(attr->data));
917           cairo_set_source_surface (cr, surface, 
918                 ((double)logical_rect.x) / PANGO_SCALE, 
919                 ((double)baseline) / PANGO_SCALE - start_pos);
920           cairo_paint (cr);
921           cairo_surface_destroy (surface);
922       } else {
923         pango_cairo_show_layout_line(cr, line);
924       }
925     }
926     ii++;
927   } while(ii < end && (notlast = pango_layout_iter_next_line(iter)));
928   pango_layout_iter_free(iter);
929   debug_print("Sent page %d to printer\n", page_nr+1);
930 }
931
932 static void printing_layout_set_text_attributes(PrintData *print_data, GtkPrintContext *context)
933 {
934   GtkTextIter iter;
935   PangoAttrList *attr_list;
936   PangoAttribute *attr;
937   GSList *open_attrs, *attr_walk;
938
939   attr_list = pango_attr_list_new();
940   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
941     gtk_text_buffer_get_start_iter(print_data->buffer, &iter);
942   } else {
943     gtk_text_buffer_get_iter_at_offset(print_data->buffer, &iter, print_data->sel_start);
944   }
945
946   open_attrs = NULL;
947   do {
948     gboolean fg_set, bg_set, under_set, strike_set;
949     GSList *tags, *tag_walk;
950     GtkTextTag *tag;
951     GdkColor *color;
952     PangoUnderline underline;
953     gboolean strikethrough;
954     GdkPixbuf *image;
955
956     if (prefs_common.print_imgs && (image = gtk_text_iter_get_pixbuf(&iter)) != NULL) {
957       PangoRectangle rect = {0, 0, 0, 0};
958       gint startpos = printing_text_iter_get_offset_bytes(print_data, &iter);
959       gint h = gdk_pixbuf_get_height(image);
960       gint w = gdk_pixbuf_get_width(image);
961       gint a_h = gtk_print_context_get_height(context);
962       gint a_w = gtk_print_context_get_width(context);
963       gint r_h, r_w;
964       GdkPixbuf *scaled = NULL;
965       image_viewer_get_resized_size(w, h, a_w, a_h, &r_w, &r_h);
966       rect.x = 0;
967       rect.y = 0;
968       rect.width = r_w * PANGO_SCALE;
969       rect.height = r_h * PANGO_SCALE;
970       
971       scaled = gdk_pixbuf_scale_simple(image, r_w, r_h, GDK_INTERP_BILINEAR);
972       attr = pango_attr_shape_new_with_data (&rect, &rect,
973                scaled, NULL, NULL);
974       attr->start_index = startpos;
975       attr->end_index = startpos+1;
976       pango_attr_list_insert(attr_list, attr);
977       g_hash_table_insert(print_data->images, GINT_TO_POINTER(startpos), attr);
978       print_data->img_cnt++;
979     }
980
981     if(gtk_text_iter_ends_tag(&iter, NULL)) {
982       PangoAttrColor *attr_color;
983       PangoAttrInt   *attr_int;
984
985       tags = gtk_text_iter_get_toggled_tags(&iter, FALSE);
986       for(tag_walk = tags; tag_walk != NULL; tag_walk = tag_walk->next) {
987         gboolean found;
988
989         tag = GTK_TEXT_TAG(tag_walk->data);
990         g_object_get(G_OBJECT(tag),
991                      "background-set", &bg_set,
992                      "foreground-set", &fg_set,
993                      "underline-set",&under_set,
994                      "strikethrough-set", &strike_set,
995                      NULL);
996
997         if(fg_set) {
998           found = FALSE;
999           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
1000             attr = (PangoAttribute*)attr_walk->data;
1001             if(attr->klass->type == PANGO_ATTR_FOREGROUND) {
1002               attr_color = (PangoAttrColor*) attr;
1003               g_object_get(G_OBJECT(tag), "foreground_gdk", &color, NULL);
1004               if(printing_is_pango_gdk_color_equal(&(attr_color->color), color)) {
1005                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1006                 pango_attr_list_insert(attr_list, attr);
1007                 found = TRUE;
1008                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
1009                 break;
1010               }
1011               if (color)
1012                 gdk_color_free(color);
1013             }
1014           }
1015           if(!found)
1016             debug_print("Error generating attribute list.\n");
1017         }
1018
1019         if(bg_set) {
1020           found = FALSE;
1021           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
1022             attr = (PangoAttribute*)attr_walk->data;
1023             if(attr->klass->type == PANGO_ATTR_BACKGROUND) {
1024               attr_color = (PangoAttrColor*) attr;
1025               g_object_get(G_OBJECT(tag), "background-gdk", &color, NULL);
1026               if(printing_is_pango_gdk_color_equal(&(attr_color->color), color)) {
1027                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1028                 pango_attr_list_insert(attr_list, attr);
1029                 found = TRUE;
1030                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
1031                 break;
1032               }
1033               if (color)
1034                 gdk_color_free(color);
1035             }
1036           }
1037           if(!found)
1038             debug_print("Error generating attribute list.\n");
1039         }
1040
1041         if(under_set) {
1042           found = FALSE;
1043           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
1044             attr = (PangoAttribute*)attr_walk->data;
1045             if(attr->klass->type == PANGO_ATTR_UNDERLINE) {
1046               attr_int = (PangoAttrInt*)attr;
1047               g_object_get(G_OBJECT(tag), "underline", &underline, NULL);
1048               if(attr_int->value == underline) {
1049                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1050                 pango_attr_list_insert(attr_list, attr);
1051                 found = TRUE;
1052                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
1053                 break;
1054               }
1055             }
1056           }
1057           if(!found)
1058             debug_print("Error generating attribute list.\n");
1059         }
1060
1061         if(strike_set) {
1062           found = FALSE;
1063           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
1064             attr = (PangoAttribute*)attr_walk->data;
1065             if(attr->klass->type == PANGO_ATTR_STRIKETHROUGH) {
1066               attr_int = (PangoAttrInt*)attr;
1067               g_object_get(G_OBJECT(tag), "strikethrough", &strikethrough, NULL);
1068               if(attr_int->value == strikethrough) {
1069                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1070                 pango_attr_list_insert(attr_list, attr);
1071                 found = TRUE;
1072                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
1073                 break;
1074               }
1075             }
1076           }
1077           if(!found)
1078             debug_print("Error generating attribute list.\n");
1079         }
1080
1081       }
1082       g_slist_free(tags);
1083     }
1084
1085     if(gtk_text_iter_begins_tag(&iter, NULL)) {
1086       tags = gtk_text_iter_get_toggled_tags(&iter, TRUE);
1087       for(tag_walk = tags; tag_walk != NULL; tag_walk = tag_walk->next) {
1088         tag = GTK_TEXT_TAG(tag_walk->data);
1089         g_object_get(G_OBJECT(tag),
1090                      "background-set", &bg_set,
1091                      "foreground-set", &fg_set,
1092                      "underline-set", &under_set,
1093                      "strikethrough-set", &strike_set,
1094                      NULL);
1095         if(fg_set) {
1096           g_object_get(G_OBJECT(tag), "foreground-gdk", &color, NULL);
1097           attr = pango_attr_foreground_new(color->red,color->green,color->blue);
1098           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1099           open_attrs = g_slist_prepend(open_attrs, attr);
1100         }
1101         if(bg_set) {
1102           g_object_get(G_OBJECT(tag), "background-gdk", &color, NULL);
1103           attr = pango_attr_background_new(color->red,color->green,color->blue);
1104           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1105           open_attrs = g_slist_prepend(open_attrs, attr);
1106         }
1107         if(under_set) {
1108           g_object_get(G_OBJECT(tag), "underline", &underline, NULL);
1109           attr = pango_attr_underline_new(underline);
1110           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1111           open_attrs = g_slist_prepend(open_attrs, attr);         
1112         }
1113         if(strike_set) {
1114           g_object_get(G_OBJECT(tag), "strikethrough", &strikethrough, NULL);
1115           attr = pango_attr_strikethrough_new(strikethrough);
1116           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1117           open_attrs = g_slist_prepend(open_attrs, attr);         
1118         }
1119       }
1120       g_slist_free(tags);
1121     }
1122     
1123   } while(!gtk_text_iter_is_end(&iter) && gtk_text_iter_forward_to_tag_toggle(&iter, NULL));
1124           
1125   /* close all open attributes */
1126   for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
1127     attr = (PangoAttribute*) attr_walk->data;
1128     attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
1129     pango_attr_list_insert(attr_list, attr);
1130   }
1131   g_slist_free(open_attrs);
1132
1133   pango_layout_set_attributes(print_data->layout, attr_list);
1134   pango_attr_list_unref(attr_list);
1135 }
1136
1137 static gboolean printing_is_pango_gdk_color_equal(PangoColor *p, GdkColor *g)
1138 {
1139   return ((p->red == g->red) && (p->green == g->green) && (p->blue == g->blue));
1140 }
1141
1142 /* Pango has it's attribute in bytes, but GtkTextIter gets only an offset
1143  * in characters, so here we're returning an offset in bytes. 
1144  */
1145 static gint printing_text_iter_get_offset_bytes(PrintData *print_data, const GtkTextIter *iter)
1146 {
1147   gint off_chars;
1148   gint off_bytes;
1149   gchar *text;
1150   GtkTextIter start;
1151
1152   off_chars = gtk_text_iter_get_offset(iter);
1153   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
1154     gtk_text_buffer_get_start_iter(gtk_text_iter_get_buffer(iter), &start);
1155   } else {
1156     gtk_text_buffer_get_iter_at_offset(gtk_text_iter_get_buffer(iter), &start, print_data->sel_start);
1157   }
1158   text = gtk_text_iter_get_text(&start, iter);
1159   off_bytes = strlen(text);
1160   g_free(text);
1161   return off_bytes;
1162 }
1163
1164 static void printing_preview_update_zoom_sensitivity(PreviewData *preview_data)
1165 {
1166   if((preview_data->print_data->zoom * PREVIEW_ZOOM_FAC) > PREVIEW_ZOOM_MAX)
1167     gtk_widget_set_sensitive(preview_data->zoom_in, FALSE);
1168   else
1169     gtk_widget_set_sensitive(preview_data->zoom_in, TRUE);
1170
1171   if((preview_data->print_data->zoom / PREVIEW_ZOOM_FAC) < PREVIEW_ZOOM_MIN)
1172     gtk_widget_set_sensitive(preview_data->zoom_out, FALSE);
1173   else
1174     gtk_widget_set_sensitive(preview_data->zoom_out, TRUE);
1175 }
1176
1177 #endif /* GTK+ >= 2.10.0 */