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
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.
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.
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/>.
26 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
28 #include "prefs_common.h"
30 #include <glib/gi18n.h>
31 #include <pango/pango.h>
36 PangoContext *pango_context;
39 GtkTextBuffer *buffer;
46 static void cb_begin_print(GtkPrintOperation*, GtkPrintContext*, gpointer);
47 static void cb_draw_page(GtkPrintOperation*, GtkPrintContext*, gint, gpointer);
50 static GtkPrintSettings *settings = NULL;
51 static GtkPageSetup *page_setup = NULL;
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*);
58 void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, gint sel_end)
60 GtkPrintOperation *op;
61 GtkPrintOperationResult res;
62 PrintData *print_data;
63 GtkTextIter start, end;
64 GtkTextBuffer *buffer;
66 op = gtk_print_operation_new();
68 print_data = g_new0(PrintData,1);
70 print_data->pango_context=gtk_widget_get_pango_context(GTK_WIDGET(text_view));
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);
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);
85 print_data->text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
87 /* Config for printing */
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);
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);
97 /* Start printing process */
98 res = gtk_print_operation_run(op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
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");
107 else if(res == GTK_PRINT_OPERATION_RESULT_APPLY) {
108 /* store settings for next printing session */
110 g_object_unref(settings);
111 settings = g_object_ref(gtk_print_operation_get_print_settings(op));
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);
122 debug_print("printing_print finished\n");
125 void printing_page_setup(GtkWindow *parent)
127 GtkPageSetup *new_page_setup;
130 settings = gtk_print_settings_new();
132 new_page_setup = gtk_print_run_page_setup_dialog(parent,page_setup,settings);
135 g_object_unref(page_setup);
137 page_setup = new_page_setup;
140 static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
143 double width, height;
147 PrintData *print_data;
148 PangoLayoutLine *layout_line;
149 PangoFontDescription *desc;
151 debug_print("Preparing print job...\n");
153 print_data = (PrintData*) user_data;
154 width = gtk_print_context_get_width(context);
155 height = gtk_print_context_get_height(context);
157 print_data->layout = gtk_print_context_create_pango_layout(context);
159 if(prefs_common.use_different_print_font)
160 desc = pango_font_description_from_string(prefs_common.printfont);
162 desc = pango_font_description_copy(
163 pango_context_get_font_description(print_data->pango_context));
165 pango_layout_set_font_description(print_data->layout, desc);
166 pango_font_description_free(desc);
168 pango_layout_set_width(print_data->layout, width * PANGO_SCALE);
169 pango_layout_set_text(print_data->layout, print_data->text, -1);
171 printing_layout_set_text_attributes(print_data);
173 num_lines = pango_layout_get_line_count(print_data->layout);
177 for(line = 0; line < num_lines; line++) {
178 PangoRectangle ink_rect, logical_rect;
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
188 #if PANGO_HAS_READONLY
189 layout_line = pango_layout_get_line_readonly(print_data->layout, line);
191 layout_line = pango_layout_get_line(print_data->layout, line);
193 pango_layout_line_get_extents(layout_line, &ink_rect, &logical_rect);
195 line_height = ((double)logical_rect.height) / PANGO_SCALE;
197 /* TODO: Check if there is a pixbuf in that line */
199 if((page_height + line_height) > height) {
200 page_breaks = g_list_prepend(page_breaks, GINT_TO_POINTER(line));
204 page_height += line_height;
207 page_breaks = g_list_reverse(page_breaks);
208 gtk_print_operation_set_n_pages(op, g_list_length(page_breaks) + 1);
210 print_data->page_breaks = page_breaks;
212 debug_print("Starting print job...\n");
215 static void cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
216 int page_nr, gpointer user_data)
219 PrintData *print_data;
222 PangoLayoutIter *iter;
225 print_data = (PrintData*) user_data;
230 pagebreak = g_list_nth(print_data->page_breaks, page_nr - 1);
231 start = GPOINTER_TO_INT(pagebreak->data);
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);
238 end = GPOINTER_TO_INT(pagebreak->data);
240 cr = gtk_print_context_get_cairo_context(context);
241 cairo_set_source_rgb(cr, 0., 0., 0.);
245 iter = pango_layout_get_iter(print_data->layout);
247 PangoRectangle logical_rect;
248 PangoLayoutLine *line;
252 line = pango_layout_iter_get_line(iter);
254 pango_layout_iter_get_line_extents(iter, NULL, &logical_rect);
255 baseline = pango_layout_iter_get_baseline(iter);
258 start_pos = ((double)logical_rect.y) / PANGO_SCALE;
261 ((double)logical_rect.x) / PANGO_SCALE,
262 ((double)baseline) / PANGO_SCALE - start_pos);
263 pango_cairo_show_layout_line(cr, line);
266 } while(ii < end && pango_layout_iter_next_line(iter));
268 pango_layout_iter_free(iter);
270 debug_print("Sent page %d to printer\n", page_nr+1);
273 static void printing_layout_set_text_attributes(PrintData *print_data)
276 PangoAttrList *attr_list;
277 PangoAttribute *attr;
278 GSList *open_attrs, *attr_walk;
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);
284 gtk_text_buffer_get_iter_at_offset(print_data->buffer, &iter, print_data->sel_start);
289 gboolean fg_set, bg_set, under_set, strike_set;
290 GSList *tags, *tag_walk;
293 PangoUnderline underline;
294 gboolean strikethrough;
296 if(gtk_text_iter_ends_tag(&iter, NULL)) {
297 PangoAttrColor *attr_color;
298 PangoAttrInt *attr_int;
300 tags = gtk_text_iter_get_toggled_tags(&iter, FALSE);
301 for(tag_walk = tags; tag_walk != NULL; tag_walk = tag_walk->next) {
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,
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);
323 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
329 debug_print("Error generating attribute list.\n");
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);
343 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
349 debug_print("Error generating attribute list.\n");
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);
363 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
369 debug_print("Error generating attribute list.\n");
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);
383 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
389 debug_print("Error generating attribute list.\n");
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,
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);
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);
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);
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);
434 } while(!gtk_text_iter_is_end(&iter) && gtk_text_iter_forward_to_tag_toggle(&iter, NULL));
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);
442 g_slist_free(open_attrs);
444 pango_layout_set_attributes(print_data->layout, attr_list);
445 pango_attr_list_unref(attr_list);
448 static gboolean printing_is_pango_gdk_color_equal(PangoColor *p, GdkColor *g)
450 return ((p->red == g->red) && (p->green == g->green) && (p->blue == g->blue));
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.
456 static gint printing_text_iter_get_offset_bytes(PrintData *print_data, const GtkTextIter *iter)
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);
467 gtk_text_buffer_get_iter_at_offset(gtk_text_iter_get_buffer(iter), &start, print_data->sel_start);
469 text = gtk_text_iter_get_slice(&start, iter);
470 off_bytes = strlen(text);
475 #endif /* GTK+ >= 2.10.0 */