2 * Copyright (C) 2018 The Geeqie Team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "image-load.h"
31 #include "main-defines.h"
34 #include "pixbuf-util.h"
35 #include "ui-fileops.h"
38 #define PRINT_SETTINGS "print_settings" // filename save printer settings
39 #define PAGE_SETUP "page_setup" // filename save page setup
41 /* padding between objects */
42 #define PRINT_TEXT_PADDING 3.0
44 /* method to use when scaling down image data */
45 #define PRINT_MAX_INTERP GDK_INTERP_BILINEAR
47 /* reverse order is important */
58 GList *source_selection;
61 GtkTextBuffer *page_text;
62 gchar *template_string;
64 ImageLoader *job_loader;
66 GList *print_pixbuf_queue;
67 gboolean job_render_finished;
72 static gint print_layout_page_count(PrintWindow *pw)
76 images = g_list_length(pw->source_selection);
78 if (images < 1 ) return 0;
83 static gboolean print_job_render_image(PrintWindow *pw);
85 static void print_job_render_image_loader_done(ImageLoader *il, gpointer data)
87 auto pw = static_cast<PrintWindow *>(data);
90 pixbuf = image_loader_get_pixbuf(il);
93 pw->print_pixbuf_queue = g_list_append(pw->print_pixbuf_queue, pixbuf);
95 image_loader_free(pw->job_loader);
96 pw->job_loader = nullptr;
100 if (!print_job_render_image(pw))
102 pw->job_render_finished = TRUE;
106 static gboolean print_job_render_image(PrintWindow *pw)
108 FileData *fd = nullptr;
110 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, pw->job_page));
111 if (!fd) return FALSE;
113 image_loader_free(pw->job_loader);
114 pw->job_loader = nullptr;
116 pw->job_loader = image_loader_new(fd);
117 g_signal_connect(G_OBJECT(pw->job_loader), "done",
118 (GCallback)print_job_render_image_loader_done, pw);
120 if (!image_loader_start(pw->job_loader))
122 image_loader_free(pw->job_loader);
123 pw->job_loader= nullptr;
128 #pragma GCC diagnostic push
129 #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
130 static void font_activated_cb(GtkFontChooser *widget, gchar *fontname, gpointer option)
132 option = g_strdup(fontname);
136 gq_gtk_widget_destroy(GTK_WIDGET(widget));
138 #pragma GCC diagnostic pop
140 static void font_response_cb(GtkDialog *dialog, int response_id, gpointer option)
144 if (response_id == GTK_RESPONSE_OK)
146 font = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
148 option = g_strdup(font);
152 gq_gtk_widget_destroy(GTK_WIDGET(dialog));
155 static void print_set_font_cb(GtkWidget *widget, gpointer data)
160 if (g_strcmp0(static_cast<const gchar *>(data), "Image text font") == 0)
162 option = options->printer.image_font;
166 option = options->printer.page_font;
169 dialog = gtk_font_chooser_dialog_new(static_cast<const gchar *>(data), GTK_WINDOW(gtk_widget_get_toplevel(widget)));
170 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
171 gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dialog), static_cast<const gchar *>(option));
173 g_signal_connect(dialog, "font-activated", G_CALLBACK(font_activated_cb), option);
174 g_signal_connect(dialog, "response", G_CALLBACK(font_response_cb), option);
176 gtk_widget_show(dialog);
179 static gint set_toggle(GSList *list, TextPosition pos)
181 GtkToggleButton *current_sel;
182 GtkToggleButton *new_sel;
185 current_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, pos)->data);
186 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(current_sel)))
193 new_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, new_pos)->data);
194 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(new_sel), TRUE);
199 static void image_text_position_h1_cb(GtkWidget *widget, gpointer data)
201 auto pw = static_cast<PrintWindow *>(data);
204 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
206 new_set = set_toggle(pw->page_group, HEADER_1);
209 options->printer.page_text_position = new_set;
211 options->printer.image_text_position = HEADER_1;
215 static void image_text_position_h2_cb(GtkWidget *widget, gpointer data)
217 auto pw = static_cast<PrintWindow *>(data);
220 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
222 new_set = set_toggle(pw->page_group, HEADER_2);
225 options->printer.page_text_position = new_set;
227 options->printer.image_text_position = HEADER_2;
231 static void image_text_position_f1_cb(GtkWidget *widget, gpointer data)
233 auto pw = static_cast<PrintWindow *>(data);
236 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
238 new_set = set_toggle(pw->page_group, FOOTER_1);
241 options->printer.page_text_position = new_set;
243 options->printer.image_text_position = FOOTER_1;
247 static void image_text_position_f2_cb(GtkWidget *widget, gpointer data)
249 auto pw = static_cast<PrintWindow *>(data);
252 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
254 new_set = set_toggle(pw->page_group, FOOTER_2);
257 options->printer.page_text_position = new_set;
259 options->printer.image_text_position = FOOTER_2;
263 static void page_text_position_h1_cb(GtkWidget *widget, gpointer data)
265 auto pw = static_cast<PrintWindow *>(data);
268 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
270 new_set = set_toggle(pw->image_group, HEADER_1);
273 options->printer.image_text_position = new_set;
275 options->printer.page_text_position = HEADER_1;
279 static void page_text_position_h2_cb(GtkWidget *widget, gpointer data)
281 auto pw = static_cast<PrintWindow *>(data);
284 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
286 new_set = set_toggle(pw->image_group, HEADER_2);
289 options->printer.image_text_position = new_set;
291 options->printer.page_text_position = HEADER_2;
295 static void page_text_position_f1_cb(GtkWidget *widget, gpointer data)
297 auto pw = static_cast<PrintWindow *>(data);
300 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
302 new_set = set_toggle(pw->image_group, FOOTER_1);
305 options->printer.image_text_position = new_set;
307 options->printer.page_text_position = FOOTER_1;
311 static void page_text_position_f2_cb(GtkWidget *widget, gpointer data)
313 auto pw = static_cast<PrintWindow *>(data);
316 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
318 new_set = set_toggle(pw->image_group, FOOTER_2);
321 options->printer.image_text_position = new_set;
323 options->printer.page_text_position = FOOTER_2;
327 static void set_print_image_text_string(gchar **template_string, const gchar *value)
329 g_assert(template_string);
331 g_free(*template_string);
332 *template_string = g_strdup(value);
335 static void image_text_template_view_changed_cb(GtkWidget *, gpointer data)
337 GtkWidget *pTextView;
338 GtkTextBuffer *pTextBuffer;
342 pTextView = GTK_WIDGET(data);
344 pTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pTextView));
345 gtk_text_buffer_get_start_iter(pTextBuffer, &iStart);
346 gtk_text_buffer_get_end_iter(pTextBuffer, &iEnd);
348 set_print_image_text_string(&options->printer.template_string,
349 gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE));
353 PRE_FORMATTED_COLUMNS = 4
355 static void print_text_menu(GtkWidget *box, PrintWindow *pw)
362 GtkWidget *image_text_button;
363 GtkWidget *page_text_button;
365 GtkWidget *page_text_view;
366 GtkWidget *image_text_template_view;
368 GtkWidget *scrolled_pre_formatted;
369 GtkTextBuffer *buffer;
371 group = pref_group_new(box, FALSE, _("Image text"), GTK_ORIENTATION_VERTICAL);
373 image_text_button = pref_checkbox_new_int(group, _("Show image text"),
374 options->printer.show_image_text, &options->printer.show_image_text);
376 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
378 pref_checkbox_link_sensitivity(image_text_button, subgroup);
380 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
381 gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
383 /* order is important */
384 button1 = pref_radiobutton_new(hbox, nullptr, "Header 1",
385 options->printer.image_text_position == HEADER_1,
386 G_CALLBACK(image_text_position_h1_cb), pw);
387 button1 = pref_radiobutton_new(hbox, button1, "Header 2",
388 options->printer.image_text_position == HEADER_2,
389 G_CALLBACK(image_text_position_h2_cb), pw);
390 button1 = pref_radiobutton_new(hbox, button1, "Footer 1",
391 options->printer.image_text_position == FOOTER_1,
392 G_CALLBACK(image_text_position_f1_cb), pw);
393 button1 = pref_radiobutton_new(hbox, button1, "Footer 2",
394 options->printer.image_text_position == FOOTER_2,
395 G_CALLBACK(image_text_position_f2_cb), pw);
396 gtk_widget_show(hbox);
397 pw->image_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1)));
399 image_text_template_view = gtk_text_view_new();
401 scrolled_pre_formatted = osd_new(PRE_FORMATTED_COLUMNS, image_text_template_view);
402 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled_pre_formatted, FALSE, FALSE, 0);
403 gtk_widget_show(scrolled_pre_formatted);
404 gtk_widget_show(subgroup);
406 gtk_widget_set_tooltip_markup(image_text_template_view,
407 _("Extensive formatting options are shown in the Help file"));
409 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
410 gtk_widget_set_size_request(scrolled, 200, 50);
411 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
412 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
413 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
414 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
415 gtk_widget_show(scrolled);
417 gq_gtk_container_add(GTK_WIDGET(scrolled), image_text_template_view);
418 gtk_widget_show(image_text_template_view);
420 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(image_text_template_view));
421 if (options->printer.template_string) gtk_text_buffer_set_text(buffer, options->printer.template_string, -1);
422 g_signal_connect(G_OBJECT(buffer), "changed",
423 G_CALLBACK(image_text_template_view_changed_cb), image_text_template_view);
425 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
427 button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
428 G_CALLBACK(print_set_font_cb), const_cast<char *>("Image text font"));
430 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
431 gtk_widget_show(button);
433 pref_spacer(group, PREF_PAD_GAP);
435 group = pref_group_new(box, FALSE, _("Page text"), GTK_ORIENTATION_VERTICAL);
437 page_text_button = pref_checkbox_new_int(group, _("Show page text"),
438 options->printer.show_page_text, &options->printer.show_page_text);
440 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
441 pref_checkbox_link_sensitivity(page_text_button, subgroup);
443 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
444 gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
446 /* order is important */
447 button2 = pref_radiobutton_new(hbox, nullptr, "Header 1",
448 options->printer.page_text_position == HEADER_1,
449 G_CALLBACK(page_text_position_h1_cb), pw);
450 button2 = pref_radiobutton_new(hbox, button2, "Header 2",
451 options->printer.page_text_position == HEADER_2,
452 G_CALLBACK(page_text_position_h2_cb), pw);
453 button2 = pref_radiobutton_new(hbox, button2, "Footer 1",
454 options->printer.page_text_position == FOOTER_1,
455 G_CALLBACK(page_text_position_f1_cb), pw);
456 button2 = pref_radiobutton_new(hbox, button2, "Footer 2",
457 options->printer.page_text_position == FOOTER_2,
458 G_CALLBACK(page_text_position_f2_cb), pw);
459 gtk_widget_show(hbox);
460 pw->page_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button2)));
462 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
463 gtk_widget_set_size_request(scrolled, 50, 50);
464 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
465 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
466 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
467 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
468 gtk_widget_show(scrolled);
470 page_text_view = gtk_text_view_new();
471 pw->page_text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page_text_view ));
472 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(pw->page_text), options->printer.page_text, -1);
473 g_object_ref(pw->page_text);
475 gtk_widget_set_tooltip_markup(page_text_view, (_("Text shown on each page of a single or multi-page print job")));
476 gq_gtk_container_add(GTK_WIDGET(scrolled), page_text_view);
477 gtk_widget_show(page_text_view);
479 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
481 button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
482 G_CALLBACK(print_set_font_cb), const_cast<char *>("Page text font"));
484 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
485 gtk_widget_show(button);
488 static gboolean paginate_cb(GtkPrintOperation *, GtkPrintContext *, gpointer data)
490 auto pw = static_cast<PrintWindow *>(data);
492 if (pw->job_render_finished)
500 gchar *form_image_text(const gchar *template_string, FileData *fd, PrintWindow *pw, gint page_nr, gint total)
503 gchar *text = nullptr;
507 gchar *collection_name;
509 if (!fd) return nullptr;
513 vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
515 window_title = g_strdup(gtk_window_get_title(GTK_WINDOW(pw->parent)));
516 delimiter = g_strstr_len(window_title, -1, " - Collection - ");
519 collection_name = g_strndup(window_title, delimiter - window_title);
523 collection_name = nullptr;
525 g_free(window_title);
529 osd_template_insert(vars, "collection", collection_name, OSDT_NONE);
532 osd_template_insert(vars, "number", g_strdup_printf("%d", page_nr + 1), OSDT_NO_DUP);
533 osd_template_insert(vars, "total", g_strdup_printf("%d", total), OSDT_NO_DUP);
534 osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
535 osd_template_insert(vars, "date", fd ? (const_cast<gchar *>(text_from_time(fd->date))) : "", OSDT_NONE);
536 osd_template_insert(vars, "size", fd ? (text_from_size_abrev(fd->size)) : g_strdup(""), OSDT_FREE);
542 w = gdk_pixbuf_get_width(fd->pixbuf);
543 h = gdk_pixbuf_get_height(fd->pixbuf);
545 osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
546 osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
547 osd_template_insert(vars, "res", g_strdup_printf("%d × %d", w, h), OSDT_FREE);
551 osd_template_insert(vars, "width", nullptr, OSDT_NONE);
552 osd_template_insert(vars, "height", nullptr, OSDT_NONE);
553 osd_template_insert(vars, "res", nullptr, OSDT_NONE);
556 text = image_osd_mkinfo(template_string, fd, vars);
557 g_hash_table_destroy(vars);
559 g_free(collection_name);
564 static void draw_page(GtkPrintOperation *, GtkPrintContext *context, gint page_nr, gpointer data)
566 auto pw = static_cast<PrintWindow *>(data);
569 gdouble context_width;
570 gdouble context_height;
571 gdouble pixbuf_image_width;
572 gdouble pixbuf_image_height;
573 gdouble width_offset;
574 gdouble height_offset;
576 GdkPixbuf *rotated = nullptr;
577 PangoLayout *layout_image = nullptr;
578 PangoLayout *layout_page = nullptr;
579 PangoFontDescription *desc;
580 GString *image_text = g_string_new(nullptr);
581 GString *page_text = g_string_new(nullptr);
582 PangoRectangle ink_rect;
583 PangoRectangle logical_rect;
587 gdouble image_text_width;
588 gdouble image_text_height;
589 gdouble page_text_width;
590 gdouble page_text_height;
593 gdouble pango_height;
594 gdouble pango_image_height;
595 gdouble pango_page_height;
601 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, page_nr));
602 total = g_list_length(pw->source_selection);
604 pixbuf = static_cast<GdkPixbuf *>(g_list_nth_data(pw->print_pixbuf_queue, page_nr));
605 if (fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
607 rotated = pixbuf_apply_orientation(pixbuf, fd->exif_orientation);
611 pixbuf_image_width = gdk_pixbuf_get_width(pixbuf);
612 pixbuf_image_height = gdk_pixbuf_get_height(pixbuf);
614 if (options->printer.show_image_text)
616 image_text = g_string_append(image_text, form_image_text(options->printer.template_string, fd, pw, page_nr, total));
619 if (options->printer.show_page_text)
621 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
623 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
624 page_text = g_string_append(page_text, tmp);
629 cr = gtk_print_context_get_cairo_context(context);
630 context_width = gtk_print_context_get_width(context);
631 context_height = gtk_print_context_get_height(context);
633 pango_image_height = 0;
634 pango_page_height = 0;
635 image_text_width = 0;
638 if (image_text->len > 0)
640 layout_image = pango_cairo_create_layout(cr);
642 pango_layout_set_text(layout_image, image_text->str, -1);
643 desc = pango_font_description_from_string(options->printer.image_font);
644 pango_layout_set_font_description(layout_image, desc);
646 pango_layout_get_extents(layout_image, &ink_rect, &logical_rect);
647 image_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
648 image_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
650 pango_layout_set_alignment(layout_image, PANGO_ALIGN_CENTER);
651 pango_layout_set_text(layout_image, image_text->str, -1);
653 pango_image_height = image_text_height + PRINT_TEXT_PADDING * 2;
655 pango_font_description_free(desc);
658 if (page_text->len > 0)
660 layout_page = pango_cairo_create_layout(cr);
662 pango_layout_set_text(layout_page, page_text->str, -1);
663 desc = pango_font_description_from_string(options->printer.page_font);
664 pango_layout_set_font_description(layout_page, desc);
666 pango_layout_get_extents(layout_page, &ink_rect, &logical_rect);
667 page_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
668 page_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
670 pango_layout_set_alignment(layout_page, PANGO_ALIGN_CENTER);
671 pango_layout_set_text(layout_page, page_text->str, -1);
673 pango_page_height = page_text_height + PRINT_TEXT_PADDING * 2;
675 pango_font_description_free(desc);
678 pango_height = pango_image_height + pango_page_height;
680 if ((context_width / pixbuf_image_width) < ((context_height - pango_height) / pixbuf_image_height))
683 scale = context_width / pixbuf_image_width;
684 h = pixbuf_image_height * scale;
685 height_offset = (context_height - (h + pango_height)) / 2;
690 h = context_height - pango_height ;
691 scale = (context_height - pango_height) / pixbuf_image_height;
692 w = pixbuf_image_width * scale;
694 width_offset = (context_width - (pixbuf_image_width * scale)) / 2;
697 incr_y = height_offset;
699 if (options->printer.page_text_position == HEADER_1 && page_text->len > 0)
701 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
702 pango_cairo_show_layout(cr, layout_page);
704 incr_y = incr_y + pango_page_height;
707 if (options->printer.image_text_position == HEADER_1 && image_text->len > 0)
709 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
710 pango_cairo_show_layout(cr, layout_image);
712 incr_y = incr_y + pango_image_height;
715 if (options->printer.page_text_position == HEADER_2 && page_text->len > 0)
717 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
718 pango_cairo_show_layout(cr, layout_page);
720 incr_y = incr_y + pango_page_height;
723 if (options->printer.image_text_position == HEADER_2 && image_text->len > 0)
725 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
726 pango_cairo_show_layout(cr, layout_image);
728 incr_y = incr_y + pango_image_height;
734 if (options->printer.page_text_position == FOOTER_1 && page_text->len > 0)
736 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
737 pango_cairo_show_layout(cr, layout_page);
739 incr_y = incr_y + pango_page_height;
742 if (options->printer.image_text_position == FOOTER_1 && image_text->len > 0)
744 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
745 pango_cairo_show_layout(cr, layout_image);
747 incr_y = incr_y + pango_image_height;
750 if (options->printer.page_text_position == FOOTER_2 && page_text->len > 0)
752 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
753 pango_cairo_show_layout(cr, layout_page);
755 incr_y = incr_y + pango_page_height;
758 if (options->printer.image_text_position == FOOTER_2 && image_text->len > 0)
760 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
761 pango_cairo_show_layout(cr, layout_image);
764 cairo_scale(cr, scale, scale);
766 cairo_rectangle(cr, width_offset * scale , image_y, pixbuf_image_width / scale, pixbuf_image_height / scale);
767 gdk_cairo_set_source_pixbuf(cr, pixbuf, width_offset / scale, image_y / scale);
770 if (image_text->len > 0)
772 g_object_unref(layout_image);
773 g_string_free(image_text, TRUE);
775 if (page_text->len > 0)
777 g_object_unref(layout_page);
778 g_string_free(page_text, TRUE);
781 if (rotated) g_object_unref(rotated);
784 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *, gpointer user_data)
786 auto pw = static_cast<PrintWindow *>(user_data);
789 page_count = print_layout_page_count(pw);
790 gtk_print_operation_set_n_pages (operation, page_count);
792 print_job_render_image(pw);
796 GObject *option_tab_cb(GtkPrintOperation *, gpointer user_data)
798 auto pw = static_cast<PrintWindow *>(user_data);
800 return G_OBJECT(pw->vbox);
803 static void print_pref_store(PrintWindow *pw)
809 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
810 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
811 g_free(options->printer.page_text);
812 options->printer.page_text = g_strdup(tmp);
816 static void end_print_cb(GtkPrintOperation *operation, GtkPrintContext *, gpointer data)
818 auto pw = static_cast<PrintWindow *>(data);
822 GtkPrintSettings *print_settings;
823 GtkPageSetup *page_setup;
824 GError *error = nullptr;
826 print_settings = gtk_print_operation_get_print_settings(operation);
827 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
829 gtk_print_settings_to_file(print_settings, path, &error);
832 log_printf("Error: Print settings save failed:\n%s", error->message);
837 g_object_unref(print_settings);
839 page_setup = gtk_print_operation_get_default_page_setup(operation);
840 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
842 gtk_page_setup_to_file(page_setup, path, &error);
845 log_printf("Error: Print page setup save failed:\n%s", error->message);
850 g_object_unref(page_setup);
852 print_pref_store(pw);
854 work = pw->print_pixbuf_queue;
857 pixbuf = static_cast<GdkPixbuf *>(work->data);
860 g_object_unref(pixbuf);
864 g_list_free(pw->print_pixbuf_queue);
865 g_object_unref(pw->page_text);
869 static void print_response_cb(GtkDialog *dialog, gint, gpointer)
871 gq_gtk_widget_destroy(GTK_WIDGET(dialog));
874 void print_window_new(FileData *, GList *selection, GList *, GtkWidget *parent)
877 GtkPrintOperation *operation;
878 GtkPageSetup *page_setup;
881 GError *error = nullptr;
883 GtkPrintSettings *settings;
885 auto pw = g_new0(PrintWindow, 1);
887 pw->source_selection = file_data_process_groups_in_selection(selection, FALSE, nullptr);
889 if (print_layout_page_count(pw) == 0)
896 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
897 gtk_container_set_border_width(GTK_CONTAINER(vbox), PREF_PAD_BORDER);
898 gtk_widget_show(vbox);
900 print_text_menu(vbox, pw);
903 pw->print_pixbuf_queue = nullptr;
904 pw->job_render_finished = FALSE;
907 operation = gtk_print_operation_new();
908 settings = gtk_print_settings_new();
910 gtk_print_operation_set_custom_tab_label(operation, "Options");
911 gtk_print_operation_set_use_full_page(operation, TRUE);
912 gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
913 gtk_print_operation_set_embed_page_setup(operation, TRUE);
914 gtk_print_operation_set_allow_async (operation, TRUE);
915 dir = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
918 dir = g_get_home_dir();
921 uri = g_build_filename("file:/", dir, "geeqie-file.pdf", NULL);
922 gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
925 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
926 gtk_print_settings_load_file(settings, path, &error);
929 log_printf("Error: Printer settings load failed:\n%s", error->message);
933 gtk_print_operation_set_print_settings(operation, settings);
936 page_setup = gtk_page_setup_new();
937 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
938 gtk_page_setup_load_file(page_setup, path, &error);
941 log_printf("Error: Print page setup load failed:\n%s", error->message);
945 gtk_print_operation_set_default_page_setup(operation, page_setup);
948 g_signal_connect (G_OBJECT (operation), "begin-print",
949 G_CALLBACK (begin_print), pw);
950 g_signal_connect (G_OBJECT (operation), "draw-page",
951 G_CALLBACK (draw_page), pw);
952 g_signal_connect (G_OBJECT (operation), "end-print",
953 G_CALLBACK (end_print_cb), pw);
954 g_signal_connect (G_OBJECT (operation), "create-custom-widget",
955 G_CALLBACK (option_tab_cb), pw);
956 g_signal_connect (G_OBJECT (operation), "paginate",
957 G_CALLBACK (paginate_cb), pw);
959 gtk_print_operation_set_n_pages(operation, print_layout_page_count(pw));
961 gtk_print_operation_run(operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
962 GTK_WINDOW (parent), &error);
968 dialog = gtk_message_dialog_new(GTK_WINDOW (parent),
969 GTK_DIALOG_DESTROY_WITH_PARENT,
972 "%s", error->message);
973 g_error_free (error);
975 g_signal_connect(dialog, "response", G_CALLBACK(print_response_cb), NULL);
977 gtk_widget_show (dialog);
980 g_object_unref(page_setup);
981 g_object_unref(settings);
983 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */