2007-09-18 [colin] 3.0.1cvs4
[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  * 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
26 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
27
28 #include "prefs_common.h"
29
30 #include <glib/gi18n.h>
31 #include <pango/pango.h>
32 #include <string.h>
33
34 typedef struct {
35   PangoLayout *layout;
36   PangoContext *pango_context;
37   char *text;
38   GList *page_breaks;
39   GtkTextBuffer *buffer;
40   gint sel_start;
41   gint sel_end;
42 } PrintData;
43
44
45 /* callbacks */
46 static void cb_begin_print(GtkPrintOperation*, GtkPrintContext*, gpointer);
47 static void cb_draw_page(GtkPrintOperation*, GtkPrintContext*, gint, gpointer);
48
49 /* variables */
50 static GtkPrintSettings *settings   = NULL;
51 static GtkPageSetup     *page_setup = NULL;
52
53 /* other static functions */
54 static void     printing_layout_set_text_attributes(PrintData*);
55 static gboolean printing_is_pango_gdk_color_equal(PangoColor*, GdkColor*); 
56 static gint     printing_text_iter_get_offset_bytes(PrintData *, const GtkTextIter*);
57
58 void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, gint sel_end)
59 {
60   GtkPrintOperation *op;
61   GtkPrintOperationResult res;
62   PrintData *print_data;
63   GtkTextIter start, end;
64   GtkTextBuffer *buffer;
65
66   op = gtk_print_operation_new();
67
68   print_data = g_new0(PrintData,1);
69
70   print_data->pango_context=gtk_widget_get_pango_context(GTK_WIDGET(text_view));
71
72   /* get text */
73   buffer = gtk_text_view_get_buffer(text_view);
74   print_data->buffer = buffer;
75   print_data->sel_start = sel_start;
76   print_data->sel_end = sel_end;
77   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
78     gtk_text_buffer_get_start_iter(buffer, &start);
79     gtk_text_buffer_get_end_iter(buffer, &end);
80   } else {
81     gtk_text_buffer_get_iter_at_offset(buffer, &start, print_data->sel_start);
82     gtk_text_buffer_get_iter_at_offset(buffer, &end, print_data->sel_end);
83   }
84
85   print_data->text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
86
87   /* Config for printing */
88   if(settings != NULL)
89     gtk_print_operation_set_print_settings(op, settings);
90   if(page_setup != NULL)
91     gtk_print_operation_set_default_page_setup(op, page_setup);
92
93   /* signals */
94   g_signal_connect(op, "begin_print", G_CALLBACK(cb_begin_print), print_data);
95   g_signal_connect(op, "draw_page", G_CALLBACK(cb_draw_page), print_data);
96
97   /* Start printing process */
98   res = gtk_print_operation_run(op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
99                                 parent, NULL);
100
101   if(res == GTK_PRINT_OPERATION_RESULT_ERROR) {
102     GError *error = NULL;
103     gtk_print_operation_get_error(op, &error);
104     debug_print("Error printing message: %s",
105             error ? error->message : "no details");
106   }
107   else if(res == GTK_PRINT_OPERATION_RESULT_APPLY) {
108     /* store settings for next printing session */
109     if(settings != NULL)
110       g_object_unref(settings);
111     settings = g_object_ref(gtk_print_operation_get_print_settings(op));
112   }
113
114   if(print_data->text)
115     g_free(print_data->text);
116   g_list_free(print_data->page_breaks);
117   if(print_data->layout)
118     g_object_unref(print_data->layout);
119   g_free(print_data);
120
121   g_object_unref(op);
122   debug_print("printing_print finished\n");
123 }
124
125 void printing_page_setup(GtkWindow *parent)
126 {
127   GtkPageSetup *new_page_setup;
128
129   if(settings == NULL)
130     settings = gtk_print_settings_new();
131
132   new_page_setup = gtk_print_run_page_setup_dialog(parent,page_setup,settings);
133
134   if(page_setup)
135     g_object_unref(page_setup);
136
137   page_setup = new_page_setup;
138 }
139
140 static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
141                            gpointer user_data)
142 {
143   double width, height;
144   int num_lines, line;
145   double page_height;
146   GList *page_breaks;
147   PrintData *print_data;
148   PangoLayoutLine *layout_line;
149   PangoFontDescription *desc;
150
151   debug_print("Preparing print job...\n");
152         
153   print_data = (PrintData*) user_data;
154   width  = gtk_print_context_get_width(context);
155   height = gtk_print_context_get_height(context);
156
157   print_data->layout = gtk_print_context_create_pango_layout(context);
158         
159   if(prefs_common.use_different_print_font)
160     desc = pango_font_description_from_string(prefs_common.printfont);
161   else
162     desc = pango_font_description_copy(
163                 pango_context_get_font_description(print_data->pango_context));
164
165   pango_layout_set_font_description(print_data->layout, desc);
166   pango_font_description_free(desc);
167
168   pango_layout_set_width(print_data->layout, width * PANGO_SCALE);
169   pango_layout_set_text(print_data->layout, print_data->text, -1);
170
171   printing_layout_set_text_attributes(print_data);
172
173   num_lines = pango_layout_get_line_count(print_data->layout);
174
175   page_breaks = NULL;
176   page_height = 0;
177   for(line = 0; line < num_lines; line++) {
178     PangoRectangle ink_rect, logical_rect;
179     double line_height;
180
181 #define PANGO_HAS_READONLY 0
182 #if defined(PANGO_VERSION_CHECK)
183 #if PANGO_VERSION_CHECK(1,16,0)
184 #define PANGO_HAS_READONLY 1
185 #endif
186 #endif
187
188 #if PANGO_HAS_READONLY
189     layout_line = pango_layout_get_line_readonly(print_data->layout, line);
190 #else
191     layout_line = pango_layout_get_line(print_data->layout, line);
192 #endif
193     pango_layout_line_get_extents(layout_line, &ink_rect, &logical_rect);
194
195     line_height = ((double)logical_rect.height) / PANGO_SCALE;
196
197     /* TODO: Check if there is a pixbuf in that line */
198     
199     if((page_height + line_height) > height) {
200       page_breaks = g_list_prepend(page_breaks, GINT_TO_POINTER(line));
201       page_height = 0;
202     }
203
204     page_height += line_height;
205   }
206
207   page_breaks = g_list_reverse(page_breaks);
208   gtk_print_operation_set_n_pages(op, g_list_length(page_breaks) + 1);
209         
210   print_data->page_breaks = page_breaks;
211
212   debug_print("Starting print job...\n");
213 }
214
215 static void cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
216                          int page_nr, gpointer user_data)
217 {
218   cairo_t *cr;
219   PrintData *print_data;
220   int start, end, ii;
221   GList *pagebreak;
222   PangoLayoutIter *iter;
223   double start_pos;
224         
225   print_data = (PrintData*) user_data;
226
227   if (page_nr == 0)
228     start = 0;
229   else {
230     pagebreak = g_list_nth(print_data->page_breaks, page_nr - 1);
231     start = GPOINTER_TO_INT(pagebreak->data);
232   }
233
234   pagebreak = g_list_nth(print_data->page_breaks, page_nr);
235   if(pagebreak == NULL)
236     end = pango_layout_get_line_count(print_data->layout);
237   else
238     end = GPOINTER_TO_INT(pagebreak->data);
239
240   cr = gtk_print_context_get_cairo_context(context);
241   cairo_set_source_rgb(cr, 0., 0., 0.);
242
243   ii = 0;
244   start_pos = 0.;
245   iter = pango_layout_get_iter(print_data->layout);
246   do {
247     PangoRectangle logical_rect;
248     PangoLayoutLine *line;
249     int baseline;
250
251     if(ii >= start) {
252       line = pango_layout_iter_get_line(iter);
253
254       pango_layout_iter_get_line_extents(iter, NULL, &logical_rect);
255       baseline = pango_layout_iter_get_baseline(iter);
256
257       if(ii == start)
258         start_pos = ((double)logical_rect.y) / PANGO_SCALE;
259       
260       cairo_move_to(cr,
261                     ((double)logical_rect.x) / PANGO_SCALE,
262                     ((double)baseline) / PANGO_SCALE - start_pos);
263       pango_cairo_show_layout_line(cr, line);
264     }
265     ii++;
266   } while(ii < end && pango_layout_iter_next_line(iter));
267
268   pango_layout_iter_free(iter);
269         
270   debug_print("Sent page %d to printer\n", page_nr+1);
271 }
272
273 static void printing_layout_set_text_attributes(PrintData *print_data)
274 {
275   GtkTextIter iter;
276   PangoAttrList *attr_list;
277   PangoAttribute *attr;
278   GSList *open_attrs, *attr_walk;
279
280   attr_list = pango_attr_list_new();
281   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
282     gtk_text_buffer_get_start_iter(print_data->buffer, &iter);
283   } else {
284     gtk_text_buffer_get_iter_at_offset(print_data->buffer, &iter, print_data->sel_start);
285   }
286
287   open_attrs = NULL;
288   do {
289     gboolean fg_set, bg_set, under_set, strike_set;
290     GSList *tags, *tag_walk;
291     GtkTextTag *tag;
292     GdkColor *color;
293     PangoUnderline underline;
294     gboolean strikethrough;
295
296     if(gtk_text_iter_ends_tag(&iter, NULL)) {
297       PangoAttrColor *attr_color;
298       PangoAttrInt   *attr_int;
299
300       tags = gtk_text_iter_get_toggled_tags(&iter, FALSE);
301       for(tag_walk = tags; tag_walk != NULL; tag_walk = tag_walk->next) {
302         gboolean found;
303
304         tag = GTK_TEXT_TAG(tag_walk->data);
305         g_object_get(G_OBJECT(tag),
306                      "background-set", &bg_set,
307                      "foreground-set", &fg_set,
308                      "underline-set",&under_set,
309                      "strikethrough-set", &strike_set,
310                      NULL);
311
312         if(fg_set) {
313           found = FALSE;
314           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
315             attr = (PangoAttribute*)attr_walk->data;
316             if(attr->klass->type == PANGO_ATTR_FOREGROUND) {
317               attr_color = (PangoAttrColor*) attr;
318               g_object_get(G_OBJECT(tag), "foreground_gdk", &color, NULL);
319               if(printing_is_pango_gdk_color_equal(&(attr_color->color), color)) {
320                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
321                 pango_attr_list_insert(attr_list, attr);
322                 found = TRUE;
323                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
324                 break;
325               }
326             }
327           }
328           if(!found)
329             debug_print("Error generating attribute list.\n");
330         }
331
332         if(bg_set) {
333           found = FALSE;
334           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
335             attr = (PangoAttribute*)attr_walk->data;
336             if(attr->klass->type == PANGO_ATTR_BACKGROUND) {
337               attr_color = (PangoAttrColor*) attr;
338               g_object_get(G_OBJECT(tag), "background-gdk", &color, NULL);
339               if(printing_is_pango_gdk_color_equal(&(attr_color->color), color)) {
340                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
341                 pango_attr_list_insert(attr_list, attr);
342                 found = TRUE;
343                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
344                 break;
345               }
346             }
347           }
348           if(!found)
349             debug_print("Error generating attribute list.\n");
350         }
351
352         if(under_set) {
353           found = FALSE;
354           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
355             attr = (PangoAttribute*)attr_walk->data;
356             if(attr->klass->type == PANGO_ATTR_UNDERLINE) {
357               attr_int = (PangoAttrInt*)attr;
358               g_object_get(G_OBJECT(tag), "underline", &underline, NULL);
359               if(attr_int->value == underline) {
360                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
361                 pango_attr_list_insert(attr_list, attr);
362                 found = TRUE;
363                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
364                 break;
365               }
366             }
367           }
368           if(!found)
369             debug_print("Error generating attribute list.\n");
370         }
371
372         if(strike_set) {
373           found = FALSE;
374           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
375             attr = (PangoAttribute*)attr_walk->data;
376             if(attr->klass->type == PANGO_ATTR_STRIKETHROUGH) {
377               attr_int = (PangoAttrInt*)attr;
378               g_object_get(G_OBJECT(tag), "strikethrough", &strikethrough, NULL);
379               if(attr_int->value == strikethrough) {
380                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
381                 pango_attr_list_insert(attr_list, attr);
382                 found = TRUE;
383                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
384                 break;
385               }
386             }
387           }
388           if(!found)
389             debug_print("Error generating attribute list.\n");
390         }
391
392       }
393       g_slist_free(tags);
394     }
395
396     if(gtk_text_iter_begins_tag(&iter, NULL)) {
397       tags = gtk_text_iter_get_toggled_tags(&iter, TRUE);
398       for(tag_walk = tags; tag_walk != NULL; tag_walk = tag_walk->next) {
399         tag = GTK_TEXT_TAG(tag_walk->data);
400         g_object_get(G_OBJECT(tag),
401                      "background-set", &bg_set,
402                      "foreground-set", &fg_set,
403                      "underline-set", &under_set,
404                      "strikethrough-set", &strike_set,
405                      NULL);
406         if(fg_set) {
407           g_object_get(G_OBJECT(tag), "foreground-gdk", &color, NULL);
408           attr = pango_attr_foreground_new(color->red,color->green,color->blue);
409           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
410           open_attrs = g_slist_prepend(open_attrs, attr);
411         }
412         if(bg_set) {
413           g_object_get(G_OBJECT(tag), "background-gdk", &color, NULL);
414           attr = pango_attr_background_new(color->red,color->green,color->blue);
415           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
416           open_attrs = g_slist_prepend(open_attrs, attr);
417         }
418         if(under_set) {
419           g_object_get(G_OBJECT(tag), "underline", &underline, NULL);
420           attr = pango_attr_underline_new(underline);
421           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
422           open_attrs = g_slist_prepend(open_attrs, attr);         
423         }
424         if(strike_set) {
425           g_object_get(G_OBJECT(tag), "strikethrough", &strikethrough, NULL);
426           attr = pango_attr_strikethrough_new(strikethrough);
427           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
428           open_attrs = g_slist_prepend(open_attrs, attr);         
429         }
430       }
431       g_slist_free(tags);
432     }
433     
434   } while(!gtk_text_iter_is_end(&iter) && gtk_text_iter_forward_to_tag_toggle(&iter, NULL));
435           
436   /* close all open attributes */
437   for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
438     attr = (PangoAttribute*) attr_walk->data;
439     attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
440     pango_attr_list_insert(attr_list, attr);
441   }
442   g_slist_free(open_attrs);
443
444   pango_layout_set_attributes(print_data->layout, attr_list);
445   pango_attr_list_unref(attr_list);
446 }
447
448 static gboolean printing_is_pango_gdk_color_equal(PangoColor *p, GdkColor *g)
449 {
450   return ((p->red == g->red) && (p->green == g->green) && (p->blue == g->blue));
451 }
452
453 /* Pango has it's attribute in bytes, but GtkTextIter gets only an offset
454  * in characters, so here we're returning an offset in bytes. 
455  */
456 static gint printing_text_iter_get_offset_bytes(PrintData *print_data, const GtkTextIter *iter)
457 {
458   gint off_chars;
459   gint off_bytes;
460   gchar *text;
461   GtkTextIter start;
462
463   off_chars = gtk_text_iter_get_offset(iter);
464   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
465     gtk_text_buffer_get_start_iter(gtk_text_iter_get_buffer(iter), &start);
466   } else {
467     gtk_text_buffer_get_iter_at_offset(gtk_text_iter_get_buffer(iter), &start, print_data->sel_start);
468   }
469   text = gtk_text_iter_get_slice(&start, iter);
470   off_bytes = strlen(text);
471   g_free(text);
472   return off_bytes;
473 }
474
475 #endif /* GTK+ >= 2.10.0 */