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.
26 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <glib-object.h>
29 #include <pango/pango.h>
30 #include <pango/pangocairo.h>
36 #include "image-load.h"
38 #include "main-defines.h"
41 #include "pixbuf-util.h"
42 #include "ui-fileops.h"
45 #define PRINT_SETTINGS "print_settings" // filename save printer settings
46 #define PAGE_SETUP "page_setup" // filename save page setup
48 /* padding between objects */
49 #define PRINT_TEXT_PADDING 3.0
51 /* method to use when scaling down image data */
52 #define PRINT_MAX_INTERP GDK_INTERP_BILINEAR
57 constexpr gint PRE_FORMATTED_COLUMNS = 4;
61 /* reverse order is important */
72 GList *source_selection;
75 GtkTextBuffer *page_text;
76 gchar *template_string;
78 ImageLoader *job_loader;
80 GList *print_pixbuf_queue;
81 gboolean job_render_finished;
86 static gint print_layout_page_count(PrintWindow *pw)
90 images = g_list_length(pw->source_selection);
92 if (images < 1 ) return 0;
97 static gboolean print_job_render_image(PrintWindow *pw);
99 static void print_job_render_image_loader_done(ImageLoader *il, gpointer data)
101 auto pw = static_cast<PrintWindow *>(data);
104 pixbuf = image_loader_get_pixbuf(il);
106 g_object_ref(pixbuf);
107 pw->print_pixbuf_queue = g_list_append(pw->print_pixbuf_queue, pixbuf);
109 image_loader_free(pw->job_loader);
110 pw->job_loader = nullptr;
114 if (!print_job_render_image(pw))
116 pw->job_render_finished = TRUE;
120 static gboolean print_job_render_image(PrintWindow *pw)
122 FileData *fd = nullptr;
124 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, pw->job_page));
125 if (!fd) return FALSE;
127 image_loader_free(pw->job_loader);
128 pw->job_loader = nullptr;
130 pw->job_loader = image_loader_new(fd);
131 g_signal_connect(G_OBJECT(pw->job_loader), "done",
132 (GCallback)print_job_render_image_loader_done, pw);
134 if (!image_loader_start(pw->job_loader))
136 image_loader_free(pw->job_loader);
137 pw->job_loader= nullptr;
142 #pragma GCC diagnostic push
143 #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
144 static void font_activated_cb(GtkFontChooser *widget, gchar *fontname, gpointer option)
146 option = g_strdup(fontname);
150 gq_gtk_widget_destroy(GTK_WIDGET(widget));
152 #pragma GCC diagnostic pop
154 static void font_response_cb(GtkDialog *dialog, int response_id, gpointer option)
158 if (response_id == GTK_RESPONSE_OK)
160 font = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
162 option = g_strdup(font);
166 gq_gtk_widget_destroy(GTK_WIDGET(dialog));
169 static void print_set_font_cb(GtkWidget *widget, gpointer data)
174 if (g_strcmp0(static_cast<const gchar *>(data), "Image text font") == 0)
176 option = options->printer.image_font;
180 option = options->printer.page_font;
183 dialog = gtk_font_chooser_dialog_new(static_cast<const gchar *>(data), GTK_WINDOW(gtk_widget_get_toplevel(widget)));
184 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
185 gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dialog), static_cast<const gchar *>(option));
187 g_signal_connect(dialog, "font-activated", G_CALLBACK(font_activated_cb), option);
188 g_signal_connect(dialog, "response", G_CALLBACK(font_response_cb), option);
190 gtk_widget_show(dialog);
193 static gint set_toggle(GSList *list, TextPosition pos)
195 GtkToggleButton *current_sel;
196 GtkToggleButton *new_sel;
199 current_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, pos)->data);
200 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(current_sel)))
207 new_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, new_pos)->data);
208 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(new_sel), TRUE);
213 static void image_text_position_h1_cb(GtkWidget *widget, gpointer data)
215 auto pw = static_cast<PrintWindow *>(data);
218 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
220 new_set = set_toggle(pw->page_group, HEADER_1);
223 options->printer.page_text_position = new_set;
225 options->printer.image_text_position = HEADER_1;
229 static void image_text_position_h2_cb(GtkWidget *widget, gpointer data)
231 auto pw = static_cast<PrintWindow *>(data);
234 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
236 new_set = set_toggle(pw->page_group, HEADER_2);
239 options->printer.page_text_position = new_set;
241 options->printer.image_text_position = HEADER_2;
245 static void image_text_position_f1_cb(GtkWidget *widget, gpointer data)
247 auto pw = static_cast<PrintWindow *>(data);
250 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
252 new_set = set_toggle(pw->page_group, FOOTER_1);
255 options->printer.page_text_position = new_set;
257 options->printer.image_text_position = FOOTER_1;
261 static void image_text_position_f2_cb(GtkWidget *widget, gpointer data)
263 auto pw = static_cast<PrintWindow *>(data);
266 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
268 new_set = set_toggle(pw->page_group, FOOTER_2);
271 options->printer.page_text_position = new_set;
273 options->printer.image_text_position = FOOTER_2;
277 static void page_text_position_h1_cb(GtkWidget *widget, gpointer data)
279 auto pw = static_cast<PrintWindow *>(data);
282 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
284 new_set = set_toggle(pw->image_group, HEADER_1);
287 options->printer.image_text_position = new_set;
289 options->printer.page_text_position = HEADER_1;
293 static void page_text_position_h2_cb(GtkWidget *widget, gpointer data)
295 auto pw = static_cast<PrintWindow *>(data);
298 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
300 new_set = set_toggle(pw->image_group, HEADER_2);
303 options->printer.image_text_position = new_set;
305 options->printer.page_text_position = HEADER_2;
309 static void page_text_position_f1_cb(GtkWidget *widget, gpointer data)
311 auto pw = static_cast<PrintWindow *>(data);
314 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
316 new_set = set_toggle(pw->image_group, FOOTER_1);
319 options->printer.image_text_position = new_set;
321 options->printer.page_text_position = FOOTER_1;
325 static void page_text_position_f2_cb(GtkWidget *widget, gpointer data)
327 auto pw = static_cast<PrintWindow *>(data);
330 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
332 new_set = set_toggle(pw->image_group, FOOTER_2);
335 options->printer.image_text_position = new_set;
337 options->printer.page_text_position = FOOTER_2;
341 static void set_print_image_text_string(gchar **template_string, const gchar *value)
343 g_assert(template_string);
345 g_free(*template_string);
346 *template_string = g_strdup(value);
349 static void image_text_template_view_changed_cb(GtkWidget *, gpointer data)
351 GtkWidget *pTextView;
352 GtkTextBuffer *pTextBuffer;
356 pTextView = GTK_WIDGET(data);
358 pTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pTextView));
359 gtk_text_buffer_get_start_iter(pTextBuffer, &iStart);
360 gtk_text_buffer_get_end_iter(pTextBuffer, &iEnd);
362 set_print_image_text_string(&options->printer.template_string,
363 gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE));
366 static void print_text_menu(GtkWidget *box, PrintWindow *pw)
373 GtkWidget *image_text_button;
374 GtkWidget *page_text_button;
376 GtkWidget *page_text_view;
377 GtkWidget *image_text_template_view;
379 GtkWidget *scrolled_pre_formatted;
380 GtkTextBuffer *buffer;
382 group = pref_group_new(box, FALSE, _("Image text"), GTK_ORIENTATION_VERTICAL);
384 image_text_button = pref_checkbox_new_int(group, _("Show image text"),
385 options->printer.show_image_text, &options->printer.show_image_text);
387 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
389 pref_checkbox_link_sensitivity(image_text_button, subgroup);
391 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
392 gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
394 /* order is important */
395 button1 = pref_radiobutton_new(hbox, nullptr, _("Header 1"),
396 options->printer.image_text_position == HEADER_1,
397 G_CALLBACK(image_text_position_h1_cb), pw);
398 button1 = pref_radiobutton_new(hbox, button1, _("Header 2"),
399 options->printer.image_text_position == HEADER_2,
400 G_CALLBACK(image_text_position_h2_cb), pw);
401 button1 = pref_radiobutton_new(hbox, button1, _("Footer 1"),
402 options->printer.image_text_position == FOOTER_1,
403 G_CALLBACK(image_text_position_f1_cb), pw);
404 button1 = pref_radiobutton_new(hbox, button1, _("Footer 2"),
405 options->printer.image_text_position == FOOTER_2,
406 G_CALLBACK(image_text_position_f2_cb), pw);
407 gtk_widget_show(hbox);
408 pw->image_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1)));
410 image_text_template_view = gtk_text_view_new();
412 scrolled_pre_formatted = osd_new(PRE_FORMATTED_COLUMNS, image_text_template_view);
413 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled_pre_formatted, FALSE, FALSE, 0);
414 gtk_widget_show(scrolled_pre_formatted);
415 gtk_widget_show(subgroup);
417 gtk_widget_set_tooltip_markup(image_text_template_view,
418 _("Extensive formatting options are shown in the Help file"));
420 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
421 gtk_widget_set_size_request(scrolled, 200, 50);
422 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
423 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
424 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
425 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
426 gtk_widget_show(scrolled);
428 gq_gtk_container_add(GTK_WIDGET(scrolled), image_text_template_view);
429 gtk_widget_show(image_text_template_view);
431 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(image_text_template_view));
432 if (options->printer.template_string) gtk_text_buffer_set_text(buffer, options->printer.template_string, -1);
433 g_signal_connect(G_OBJECT(buffer), "changed",
434 G_CALLBACK(image_text_template_view_changed_cb), image_text_template_view);
436 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
438 button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
439 G_CALLBACK(print_set_font_cb), const_cast<char *>("Image text font"));
441 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
442 gtk_widget_show(button);
444 pref_spacer(group, PREF_PAD_GAP);
446 group = pref_group_new(box, FALSE, _("Page text"), GTK_ORIENTATION_VERTICAL);
448 page_text_button = pref_checkbox_new_int(group, _("Show page text"),
449 options->printer.show_page_text, &options->printer.show_page_text);
451 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
452 pref_checkbox_link_sensitivity(page_text_button, subgroup);
454 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
455 gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
457 /* order is important */
458 button2 = pref_radiobutton_new(hbox, nullptr, _("Header 1"),
459 options->printer.page_text_position == HEADER_1,
460 G_CALLBACK(page_text_position_h1_cb), pw);
461 button2 = pref_radiobutton_new(hbox, button2, _("Header 2"),
462 options->printer.page_text_position == HEADER_2,
463 G_CALLBACK(page_text_position_h2_cb), pw);
464 button2 = pref_radiobutton_new(hbox, button2, _("Footer 1"),
465 options->printer.page_text_position == FOOTER_1,
466 G_CALLBACK(page_text_position_f1_cb), pw);
467 button2 = pref_radiobutton_new(hbox, button2, _("Footer 2"),
468 options->printer.page_text_position == FOOTER_2,
469 G_CALLBACK(page_text_position_f2_cb), pw);
470 gtk_widget_show(hbox);
471 pw->page_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button2)));
473 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
474 gtk_widget_set_size_request(scrolled, 50, 50);
475 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
476 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
477 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
478 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
479 gtk_widget_show(scrolled);
481 page_text_view = gtk_text_view_new();
482 pw->page_text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page_text_view ));
483 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(pw->page_text), options->printer.page_text, -1);
484 g_object_ref(pw->page_text);
486 gtk_widget_set_tooltip_markup(page_text_view, (_("Text shown on each page of a single or multi-page print job")));
487 gq_gtk_container_add(GTK_WIDGET(scrolled), page_text_view);
488 gtk_widget_show(page_text_view);
490 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
492 button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
493 G_CALLBACK(print_set_font_cb), const_cast<char *>("Page text font"));
495 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
496 gtk_widget_show(button);
499 static gboolean paginate_cb(GtkPrintOperation *, GtkPrintContext *, gpointer data)
501 auto pw = static_cast<PrintWindow *>(data);
503 if (pw->job_render_finished)
511 gchar *form_image_text(const gchar *template_string, FileData *fd, PrintWindow *pw, gint page_nr, gint total)
514 gchar *text = nullptr;
518 gchar *collection_name;
520 if (!fd) return nullptr;
524 vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
526 window_title = g_strdup(gtk_window_get_title(GTK_WINDOW(pw->parent)));
527 delimiter = g_strstr_len(window_title, -1, " - Collection - ");
530 collection_name = g_strndup(window_title, delimiter - window_title);
534 collection_name = nullptr;
536 g_free(window_title);
540 osd_template_insert(vars, "collection", collection_name, OSDT_NONE);
543 osd_template_insert(vars, "number", g_strdup_printf("%d", page_nr + 1), OSDT_NO_DUP);
544 osd_template_insert(vars, "total", g_strdup_printf("%d", total), OSDT_NO_DUP);
545 osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
546 osd_template_insert(vars, "date", fd ? (const_cast<gchar *>(text_from_time(fd->date))) : "", OSDT_NONE);
547 osd_template_insert(vars, "size", fd ? (text_from_size_abrev(fd->size)) : g_strdup(""), OSDT_FREE);
553 w = gdk_pixbuf_get_width(fd->pixbuf);
554 h = gdk_pixbuf_get_height(fd->pixbuf);
556 osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
557 osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
558 osd_template_insert(vars, "res", g_strdup_printf("%d × %d", w, h), OSDT_FREE);
562 osd_template_insert(vars, "width", nullptr, OSDT_NONE);
563 osd_template_insert(vars, "height", nullptr, OSDT_NONE);
564 osd_template_insert(vars, "res", nullptr, OSDT_NONE);
567 text = image_osd_mkinfo(template_string, fd, vars);
568 g_hash_table_destroy(vars);
570 g_free(collection_name);
575 static void draw_page(GtkPrintOperation *, GtkPrintContext *context, gint page_nr, gpointer data)
577 auto pw = static_cast<PrintWindow *>(data);
580 gdouble context_width;
581 gdouble context_height;
582 gdouble pixbuf_image_width;
583 gdouble pixbuf_image_height;
584 gdouble width_offset;
585 gdouble height_offset;
587 GdkPixbuf *rotated = nullptr;
588 PangoLayout *layout_image = nullptr;
589 PangoLayout *layout_page = nullptr;
590 PangoFontDescription *desc;
591 GString *image_text = g_string_new(nullptr);
592 GString *page_text = g_string_new(nullptr);
593 PangoRectangle ink_rect;
594 PangoRectangle logical_rect;
598 gdouble image_text_width;
599 gdouble image_text_height;
600 gdouble page_text_width;
601 gdouble page_text_height;
604 gdouble pango_height;
605 gdouble pango_image_height;
606 gdouble pango_page_height;
612 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, page_nr));
613 total = g_list_length(pw->source_selection);
615 pixbuf = static_cast<GdkPixbuf *>(g_list_nth_data(pw->print_pixbuf_queue, page_nr));
616 if (fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
618 rotated = pixbuf_apply_orientation(pixbuf, fd->exif_orientation);
622 pixbuf_image_width = gdk_pixbuf_get_width(pixbuf);
623 pixbuf_image_height = gdk_pixbuf_get_height(pixbuf);
625 if (options->printer.show_image_text)
627 image_text = g_string_append(image_text, form_image_text(options->printer.template_string, fd, pw, page_nr, total));
630 if (options->printer.show_page_text)
632 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
634 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
635 page_text = g_string_append(page_text, tmp);
640 cr = gtk_print_context_get_cairo_context(context);
641 context_width = gtk_print_context_get_width(context);
642 context_height = gtk_print_context_get_height(context);
644 pango_image_height = 0;
645 pango_page_height = 0;
646 image_text_width = 0;
649 if (image_text->len > 0)
651 layout_image = pango_cairo_create_layout(cr);
653 pango_layout_set_text(layout_image, image_text->str, -1);
654 desc = pango_font_description_from_string(options->printer.image_font);
655 pango_layout_set_font_description(layout_image, desc);
657 pango_layout_get_extents(layout_image, &ink_rect, &logical_rect);
658 image_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
659 image_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
661 pango_layout_set_alignment(layout_image, PANGO_ALIGN_CENTER);
662 pango_layout_set_text(layout_image, image_text->str, -1);
664 pango_image_height = image_text_height + PRINT_TEXT_PADDING * 2;
666 pango_font_description_free(desc);
669 if (page_text->len > 0)
671 layout_page = pango_cairo_create_layout(cr);
673 pango_layout_set_text(layout_page, page_text->str, -1);
674 desc = pango_font_description_from_string(options->printer.page_font);
675 pango_layout_set_font_description(layout_page, desc);
677 pango_layout_get_extents(layout_page, &ink_rect, &logical_rect);
678 page_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
679 page_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
681 pango_layout_set_alignment(layout_page, PANGO_ALIGN_CENTER);
682 pango_layout_set_text(layout_page, page_text->str, -1);
684 pango_page_height = page_text_height + PRINT_TEXT_PADDING * 2;
686 pango_font_description_free(desc);
689 pango_height = pango_image_height + pango_page_height;
691 if ((context_width / pixbuf_image_width) < ((context_height - pango_height) / pixbuf_image_height))
694 scale = context_width / pixbuf_image_width;
695 h = pixbuf_image_height * scale;
696 height_offset = (context_height - (h + pango_height)) / 2;
701 h = context_height - pango_height ;
702 scale = (context_height - pango_height) / pixbuf_image_height;
703 w = pixbuf_image_width * scale;
705 width_offset = (context_width - (pixbuf_image_width * scale)) / 2;
708 incr_y = height_offset;
710 if (options->printer.page_text_position == HEADER_1 && page_text->len > 0)
712 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
713 pango_cairo_show_layout(cr, layout_page);
715 incr_y = incr_y + pango_page_height;
718 if (options->printer.image_text_position == HEADER_1 && image_text->len > 0)
720 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
721 pango_cairo_show_layout(cr, layout_image);
723 incr_y = incr_y + pango_image_height;
726 if (options->printer.page_text_position == HEADER_2 && page_text->len > 0)
728 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
729 pango_cairo_show_layout(cr, layout_page);
731 incr_y = incr_y + pango_page_height;
734 if (options->printer.image_text_position == HEADER_2 && image_text->len > 0)
736 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
737 pango_cairo_show_layout(cr, layout_image);
739 incr_y = incr_y + pango_image_height;
745 if (options->printer.page_text_position == FOOTER_1 && page_text->len > 0)
747 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
748 pango_cairo_show_layout(cr, layout_page);
750 incr_y = incr_y + pango_page_height;
753 if (options->printer.image_text_position == FOOTER_1 && image_text->len > 0)
755 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
756 pango_cairo_show_layout(cr, layout_image);
758 incr_y = incr_y + pango_image_height;
761 if (options->printer.page_text_position == FOOTER_2 && page_text->len > 0)
763 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
764 pango_cairo_show_layout(cr, layout_page);
766 incr_y = incr_y + pango_page_height;
769 if (options->printer.image_text_position == FOOTER_2 && image_text->len > 0)
771 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
772 pango_cairo_show_layout(cr, layout_image);
775 cairo_scale(cr, scale, scale);
777 cairo_rectangle(cr, width_offset * scale , image_y, pixbuf_image_width / scale, pixbuf_image_height / scale);
778 gdk_cairo_set_source_pixbuf(cr, pixbuf, width_offset / scale, image_y / scale);
781 if (image_text->len > 0)
783 g_object_unref(layout_image);
784 g_string_free(image_text, TRUE);
786 if (page_text->len > 0)
788 g_object_unref(layout_page);
789 g_string_free(page_text, TRUE);
792 if (rotated) g_object_unref(rotated);
795 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *, gpointer user_data)
797 auto pw = static_cast<PrintWindow *>(user_data);
800 page_count = print_layout_page_count(pw);
801 gtk_print_operation_set_n_pages (operation, page_count);
803 print_job_render_image(pw);
807 GObject *option_tab_cb(GtkPrintOperation *, gpointer user_data)
809 auto pw = static_cast<PrintWindow *>(user_data);
811 return G_OBJECT(pw->vbox);
814 static void print_pref_store(PrintWindow *pw)
820 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
821 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
822 g_free(options->printer.page_text);
823 options->printer.page_text = g_strdup(tmp);
827 static void end_print_cb(GtkPrintOperation *operation, GtkPrintContext *, gpointer data)
829 auto pw = static_cast<PrintWindow *>(data);
833 GtkPrintSettings *print_settings;
834 GtkPageSetup *page_setup;
835 GError *error = nullptr;
837 print_settings = gtk_print_operation_get_print_settings(operation);
838 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
840 gtk_print_settings_to_file(print_settings, path, &error);
843 log_printf("Error: Print settings save failed:\n%s", error->message);
848 g_object_unref(print_settings);
850 page_setup = gtk_print_operation_get_default_page_setup(operation);
851 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
853 gtk_page_setup_to_file(page_setup, path, &error);
856 log_printf("Error: Print page setup save failed:\n%s", error->message);
861 g_object_unref(page_setup);
863 print_pref_store(pw);
865 work = pw->print_pixbuf_queue;
868 pixbuf = static_cast<GdkPixbuf *>(work->data);
871 g_object_unref(pixbuf);
875 g_list_free(pw->print_pixbuf_queue);
876 g_object_unref(pw->page_text);
880 static void print_response_cb(GtkDialog *dialog, gint, gpointer)
882 gq_gtk_widget_destroy(GTK_WIDGET(dialog));
885 void print_window_new(FileData *, GList *selection, GList *, GtkWidget *parent)
888 GtkPrintOperation *operation;
889 GtkPageSetup *page_setup;
892 GError *error = nullptr;
894 GtkPrintSettings *settings;
896 auto pw = g_new0(PrintWindow, 1);
898 pw->source_selection = file_data_process_groups_in_selection(selection, FALSE, nullptr);
900 if (print_layout_page_count(pw) == 0)
907 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
908 gtk_container_set_border_width(GTK_CONTAINER(vbox), PREF_PAD_BORDER);
909 gtk_widget_show(vbox);
911 print_text_menu(vbox, pw);
914 pw->print_pixbuf_queue = nullptr;
915 pw->job_render_finished = FALSE;
918 operation = gtk_print_operation_new();
919 settings = gtk_print_settings_new();
921 gtk_print_operation_set_custom_tab_label(operation, _("Options"));
922 gtk_print_operation_set_use_full_page(operation, TRUE);
923 gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
924 gtk_print_operation_set_embed_page_setup(operation, TRUE);
925 gtk_print_operation_set_allow_async (operation, TRUE);
926 dir = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
929 dir = g_get_home_dir();
932 uri = g_build_filename("file:/", dir, "geeqie-file.pdf", NULL);
933 gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
936 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
937 gtk_print_settings_load_file(settings, path, &error);
940 log_printf("Error: Printer settings load failed:\n%s", error->message);
944 gtk_print_operation_set_print_settings(operation, settings);
947 page_setup = gtk_page_setup_new();
948 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
949 gtk_page_setup_load_file(page_setup, path, &error);
952 log_printf("Error: Print page setup load failed:\n%s", error->message);
956 gtk_print_operation_set_default_page_setup(operation, page_setup);
959 g_signal_connect (G_OBJECT (operation), "begin-print",
960 G_CALLBACK (begin_print), pw);
961 g_signal_connect (G_OBJECT (operation), "draw-page",
962 G_CALLBACK (draw_page), pw);
963 g_signal_connect (G_OBJECT (operation), "end-print",
964 G_CALLBACK (end_print_cb), pw);
965 g_signal_connect (G_OBJECT (operation), "create-custom-widget",
966 G_CALLBACK (option_tab_cb), pw);
967 g_signal_connect (G_OBJECT (operation), "paginate",
968 G_CALLBACK (paginate_cb), pw);
970 gtk_print_operation_set_n_pages(operation, print_layout_page_count(pw));
972 gtk_print_operation_run(operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
973 GTK_WINDOW (parent), &error);
979 dialog = gtk_message_dialog_new(GTK_WINDOW (parent),
980 GTK_DIALOG_DESTROY_WITH_PARENT,
983 "%s", error->message);
984 g_error_free (error);
986 g_signal_connect(dialog, "response", G_CALLBACK(print_response_cb), NULL);
988 gtk_widget_show (dialog);
991 g_object_unref(page_setup);
992 g_object_unref(settings);
994 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */