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