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
54 /* reverse order is important */
65 GList *source_selection;
68 GtkTextBuffer *page_text;
69 gchar *template_string;
71 ImageLoader *job_loader;
73 GList *print_pixbuf_queue;
74 gboolean job_render_finished;
79 static gint print_layout_page_count(PrintWindow *pw)
83 images = g_list_length(pw->source_selection);
85 if (images < 1 ) return 0;
90 static gboolean print_job_render_image(PrintWindow *pw);
92 static void print_job_render_image_loader_done(ImageLoader *il, gpointer data)
94 auto pw = static_cast<PrintWindow *>(data);
97 pixbuf = image_loader_get_pixbuf(il);
100 pw->print_pixbuf_queue = g_list_append(pw->print_pixbuf_queue, pixbuf);
102 image_loader_free(pw->job_loader);
103 pw->job_loader = nullptr;
107 if (!print_job_render_image(pw))
109 pw->job_render_finished = TRUE;
113 static gboolean print_job_render_image(PrintWindow *pw)
115 FileData *fd = nullptr;
117 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, pw->job_page));
118 if (!fd) return FALSE;
120 image_loader_free(pw->job_loader);
121 pw->job_loader = nullptr;
123 pw->job_loader = image_loader_new(fd);
124 g_signal_connect(G_OBJECT(pw->job_loader), "done",
125 (GCallback)print_job_render_image_loader_done, pw);
127 if (!image_loader_start(pw->job_loader))
129 image_loader_free(pw->job_loader);
130 pw->job_loader= nullptr;
135 #pragma GCC diagnostic push
136 #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
137 static void font_activated_cb(GtkFontChooser *widget, gchar *fontname, gpointer option)
139 option = g_strdup(fontname);
143 gq_gtk_widget_destroy(GTK_WIDGET(widget));
145 #pragma GCC diagnostic pop
147 static void font_response_cb(GtkDialog *dialog, int response_id, gpointer option)
151 if (response_id == GTK_RESPONSE_OK)
153 font = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
155 option = g_strdup(font);
159 gq_gtk_widget_destroy(GTK_WIDGET(dialog));
162 static void print_set_font_cb(GtkWidget *widget, gpointer data)
167 if (g_strcmp0(static_cast<const gchar *>(data), "Image text font") == 0)
169 option = options->printer.image_font;
173 option = options->printer.page_font;
176 dialog = gtk_font_chooser_dialog_new(static_cast<const gchar *>(data), GTK_WINDOW(gtk_widget_get_toplevel(widget)));
177 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
178 gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dialog), static_cast<const gchar *>(option));
180 g_signal_connect(dialog, "font-activated", G_CALLBACK(font_activated_cb), option);
181 g_signal_connect(dialog, "response", G_CALLBACK(font_response_cb), option);
183 gtk_widget_show(dialog);
186 static gint set_toggle(GSList *list, TextPosition pos)
188 GtkToggleButton *current_sel;
189 GtkToggleButton *new_sel;
192 current_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, pos)->data);
193 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(current_sel)))
200 new_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, new_pos)->data);
201 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(new_sel), TRUE);
206 static void image_text_position_h1_cb(GtkWidget *widget, gpointer data)
208 auto pw = static_cast<PrintWindow *>(data);
211 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
213 new_set = set_toggle(pw->page_group, HEADER_1);
216 options->printer.page_text_position = new_set;
218 options->printer.image_text_position = HEADER_1;
222 static void image_text_position_h2_cb(GtkWidget *widget, gpointer data)
224 auto pw = static_cast<PrintWindow *>(data);
227 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
229 new_set = set_toggle(pw->page_group, HEADER_2);
232 options->printer.page_text_position = new_set;
234 options->printer.image_text_position = HEADER_2;
238 static void image_text_position_f1_cb(GtkWidget *widget, gpointer data)
240 auto pw = static_cast<PrintWindow *>(data);
243 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
245 new_set = set_toggle(pw->page_group, FOOTER_1);
248 options->printer.page_text_position = new_set;
250 options->printer.image_text_position = FOOTER_1;
254 static void image_text_position_f2_cb(GtkWidget *widget, gpointer data)
256 auto pw = static_cast<PrintWindow *>(data);
259 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
261 new_set = set_toggle(pw->page_group, FOOTER_2);
264 options->printer.page_text_position = new_set;
266 options->printer.image_text_position = FOOTER_2;
270 static void page_text_position_h1_cb(GtkWidget *widget, gpointer data)
272 auto pw = static_cast<PrintWindow *>(data);
275 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
277 new_set = set_toggle(pw->image_group, HEADER_1);
280 options->printer.image_text_position = new_set;
282 options->printer.page_text_position = HEADER_1;
286 static void page_text_position_h2_cb(GtkWidget *widget, gpointer data)
288 auto pw = static_cast<PrintWindow *>(data);
291 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
293 new_set = set_toggle(pw->image_group, HEADER_2);
296 options->printer.image_text_position = new_set;
298 options->printer.page_text_position = HEADER_2;
302 static void page_text_position_f1_cb(GtkWidget *widget, gpointer data)
304 auto pw = static_cast<PrintWindow *>(data);
307 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
309 new_set = set_toggle(pw->image_group, FOOTER_1);
312 options->printer.image_text_position = new_set;
314 options->printer.page_text_position = FOOTER_1;
318 static void page_text_position_f2_cb(GtkWidget *widget, gpointer data)
320 auto pw = static_cast<PrintWindow *>(data);
323 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
325 new_set = set_toggle(pw->image_group, FOOTER_2);
328 options->printer.image_text_position = new_set;
330 options->printer.page_text_position = FOOTER_2;
334 static void set_print_image_text_string(gchar **template_string, const gchar *value)
336 g_assert(template_string);
338 g_free(*template_string);
339 *template_string = g_strdup(value);
342 static void image_text_template_view_changed_cb(GtkWidget *, gpointer data)
344 GtkWidget *pTextView;
345 GtkTextBuffer *pTextBuffer;
349 pTextView = GTK_WIDGET(data);
351 pTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pTextView));
352 gtk_text_buffer_get_start_iter(pTextBuffer, &iStart);
353 gtk_text_buffer_get_end_iter(pTextBuffer, &iEnd);
355 set_print_image_text_string(&options->printer.template_string,
356 gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE));
360 PRE_FORMATTED_COLUMNS = 4
362 static void print_text_menu(GtkWidget *box, PrintWindow *pw)
369 GtkWidget *image_text_button;
370 GtkWidget *page_text_button;
372 GtkWidget *page_text_view;
373 GtkWidget *image_text_template_view;
375 GtkWidget *scrolled_pre_formatted;
376 GtkTextBuffer *buffer;
378 group = pref_group_new(box, FALSE, _("Image text"), GTK_ORIENTATION_VERTICAL);
380 image_text_button = pref_checkbox_new_int(group, _("Show image text"),
381 options->printer.show_image_text, &options->printer.show_image_text);
383 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
385 pref_checkbox_link_sensitivity(image_text_button, subgroup);
387 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
388 gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
390 /* order is important */
391 button1 = pref_radiobutton_new(hbox, nullptr, _("Header 1"),
392 options->printer.image_text_position == HEADER_1,
393 G_CALLBACK(image_text_position_h1_cb), pw);
394 button1 = pref_radiobutton_new(hbox, button1, _("Header 2"),
395 options->printer.image_text_position == HEADER_2,
396 G_CALLBACK(image_text_position_h2_cb), pw);
397 button1 = pref_radiobutton_new(hbox, button1, _("Footer 1"),
398 options->printer.image_text_position == FOOTER_1,
399 G_CALLBACK(image_text_position_f1_cb), pw);
400 button1 = pref_radiobutton_new(hbox, button1, _("Footer 2"),
401 options->printer.image_text_position == FOOTER_2,
402 G_CALLBACK(image_text_position_f2_cb), pw);
403 gtk_widget_show(hbox);
404 pw->image_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1)));
406 image_text_template_view = gtk_text_view_new();
408 scrolled_pre_formatted = osd_new(PRE_FORMATTED_COLUMNS, image_text_template_view);
409 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled_pre_formatted, FALSE, FALSE, 0);
410 gtk_widget_show(scrolled_pre_formatted);
411 gtk_widget_show(subgroup);
413 gtk_widget_set_tooltip_markup(image_text_template_view,
414 _("Extensive formatting options are shown in the Help file"));
416 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
417 gtk_widget_set_size_request(scrolled, 200, 50);
418 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
419 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
420 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
421 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
422 gtk_widget_show(scrolled);
424 gq_gtk_container_add(GTK_WIDGET(scrolled), image_text_template_view);
425 gtk_widget_show(image_text_template_view);
427 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(image_text_template_view));
428 if (options->printer.template_string) gtk_text_buffer_set_text(buffer, options->printer.template_string, -1);
429 g_signal_connect(G_OBJECT(buffer), "changed",
430 G_CALLBACK(image_text_template_view_changed_cb), image_text_template_view);
432 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
434 button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
435 G_CALLBACK(print_set_font_cb), const_cast<char *>("Image text font"));
437 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
438 gtk_widget_show(button);
440 pref_spacer(group, PREF_PAD_GAP);
442 group = pref_group_new(box, FALSE, _("Page text"), GTK_ORIENTATION_VERTICAL);
444 page_text_button = pref_checkbox_new_int(group, _("Show page text"),
445 options->printer.show_page_text, &options->printer.show_page_text);
447 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
448 pref_checkbox_link_sensitivity(page_text_button, subgroup);
450 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
451 gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
453 /* order is important */
454 button2 = pref_radiobutton_new(hbox, nullptr, _("Header 1"),
455 options->printer.page_text_position == HEADER_1,
456 G_CALLBACK(page_text_position_h1_cb), pw);
457 button2 = pref_radiobutton_new(hbox, button2, _("Header 2"),
458 options->printer.page_text_position == HEADER_2,
459 G_CALLBACK(page_text_position_h2_cb), pw);
460 button2 = pref_radiobutton_new(hbox, button2, _("Footer 1"),
461 options->printer.page_text_position == FOOTER_1,
462 G_CALLBACK(page_text_position_f1_cb), pw);
463 button2 = pref_radiobutton_new(hbox, button2, _("Footer 2"),
464 options->printer.page_text_position == FOOTER_2,
465 G_CALLBACK(page_text_position_f2_cb), pw);
466 gtk_widget_show(hbox);
467 pw->page_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button2)));
469 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
470 gtk_widget_set_size_request(scrolled, 50, 50);
471 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
472 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
473 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
474 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
475 gtk_widget_show(scrolled);
477 page_text_view = gtk_text_view_new();
478 pw->page_text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page_text_view ));
479 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(pw->page_text), options->printer.page_text, -1);
480 g_object_ref(pw->page_text);
482 gtk_widget_set_tooltip_markup(page_text_view, (_("Text shown on each page of a single or multi-page print job")));
483 gq_gtk_container_add(GTK_WIDGET(scrolled), page_text_view);
484 gtk_widget_show(page_text_view);
486 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
488 button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
489 G_CALLBACK(print_set_font_cb), const_cast<char *>("Page text font"));
491 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
492 gtk_widget_show(button);
495 static gboolean paginate_cb(GtkPrintOperation *, GtkPrintContext *, gpointer data)
497 auto pw = static_cast<PrintWindow *>(data);
499 if (pw->job_render_finished)
507 gchar *form_image_text(const gchar *template_string, FileData *fd, PrintWindow *pw, gint page_nr, gint total)
510 gchar *text = nullptr;
514 gchar *collection_name;
516 if (!fd) return nullptr;
520 vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
522 window_title = g_strdup(gtk_window_get_title(GTK_WINDOW(pw->parent)));
523 delimiter = g_strstr_len(window_title, -1, " - Collection - ");
526 collection_name = g_strndup(window_title, delimiter - window_title);
530 collection_name = nullptr;
532 g_free(window_title);
536 osd_template_insert(vars, "collection", collection_name, OSDT_NONE);
539 osd_template_insert(vars, "number", g_strdup_printf("%d", page_nr + 1), OSDT_NO_DUP);
540 osd_template_insert(vars, "total", g_strdup_printf("%d", total), OSDT_NO_DUP);
541 osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
542 osd_template_insert(vars, "date", fd ? (const_cast<gchar *>(text_from_time(fd->date))) : "", OSDT_NONE);
543 osd_template_insert(vars, "size", fd ? (text_from_size_abrev(fd->size)) : g_strdup(""), OSDT_FREE);
549 w = gdk_pixbuf_get_width(fd->pixbuf);
550 h = gdk_pixbuf_get_height(fd->pixbuf);
552 osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
553 osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
554 osd_template_insert(vars, "res", g_strdup_printf("%d × %d", w, h), OSDT_FREE);
558 osd_template_insert(vars, "width", nullptr, OSDT_NONE);
559 osd_template_insert(vars, "height", nullptr, OSDT_NONE);
560 osd_template_insert(vars, "res", nullptr, OSDT_NONE);
563 text = image_osd_mkinfo(template_string, fd, vars);
564 g_hash_table_destroy(vars);
566 g_free(collection_name);
571 static void draw_page(GtkPrintOperation *, GtkPrintContext *context, gint page_nr, gpointer data)
573 auto pw = static_cast<PrintWindow *>(data);
576 gdouble context_width;
577 gdouble context_height;
578 gdouble pixbuf_image_width;
579 gdouble pixbuf_image_height;
580 gdouble width_offset;
581 gdouble height_offset;
583 GdkPixbuf *rotated = nullptr;
584 PangoLayout *layout_image = nullptr;
585 PangoLayout *layout_page = nullptr;
586 PangoFontDescription *desc;
587 GString *image_text = g_string_new(nullptr);
588 GString *page_text = g_string_new(nullptr);
589 PangoRectangle ink_rect;
590 PangoRectangle logical_rect;
594 gdouble image_text_width;
595 gdouble image_text_height;
596 gdouble page_text_width;
597 gdouble page_text_height;
600 gdouble pango_height;
601 gdouble pango_image_height;
602 gdouble pango_page_height;
608 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, page_nr));
609 total = g_list_length(pw->source_selection);
611 pixbuf = static_cast<GdkPixbuf *>(g_list_nth_data(pw->print_pixbuf_queue, page_nr));
612 if (fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
614 rotated = pixbuf_apply_orientation(pixbuf, fd->exif_orientation);
618 pixbuf_image_width = gdk_pixbuf_get_width(pixbuf);
619 pixbuf_image_height = gdk_pixbuf_get_height(pixbuf);
621 if (options->printer.show_image_text)
623 image_text = g_string_append(image_text, form_image_text(options->printer.template_string, fd, pw, page_nr, total));
626 if (options->printer.show_page_text)
628 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
630 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
631 page_text = g_string_append(page_text, tmp);
636 cr = gtk_print_context_get_cairo_context(context);
637 context_width = gtk_print_context_get_width(context);
638 context_height = gtk_print_context_get_height(context);
640 pango_image_height = 0;
641 pango_page_height = 0;
642 image_text_width = 0;
645 if (image_text->len > 0)
647 layout_image = pango_cairo_create_layout(cr);
649 pango_layout_set_text(layout_image, image_text->str, -1);
650 desc = pango_font_description_from_string(options->printer.image_font);
651 pango_layout_set_font_description(layout_image, desc);
653 pango_layout_get_extents(layout_image, &ink_rect, &logical_rect);
654 image_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
655 image_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
657 pango_layout_set_alignment(layout_image, PANGO_ALIGN_CENTER);
658 pango_layout_set_text(layout_image, image_text->str, -1);
660 pango_image_height = image_text_height + PRINT_TEXT_PADDING * 2;
662 pango_font_description_free(desc);
665 if (page_text->len > 0)
667 layout_page = pango_cairo_create_layout(cr);
669 pango_layout_set_text(layout_page, page_text->str, -1);
670 desc = pango_font_description_from_string(options->printer.page_font);
671 pango_layout_set_font_description(layout_page, desc);
673 pango_layout_get_extents(layout_page, &ink_rect, &logical_rect);
674 page_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
675 page_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
677 pango_layout_set_alignment(layout_page, PANGO_ALIGN_CENTER);
678 pango_layout_set_text(layout_page, page_text->str, -1);
680 pango_page_height = page_text_height + PRINT_TEXT_PADDING * 2;
682 pango_font_description_free(desc);
685 pango_height = pango_image_height + pango_page_height;
687 if ((context_width / pixbuf_image_width) < ((context_height - pango_height) / pixbuf_image_height))
690 scale = context_width / pixbuf_image_width;
691 h = pixbuf_image_height * scale;
692 height_offset = (context_height - (h + pango_height)) / 2;
697 h = context_height - pango_height ;
698 scale = (context_height - pango_height) / pixbuf_image_height;
699 w = pixbuf_image_width * scale;
701 width_offset = (context_width - (pixbuf_image_width * scale)) / 2;
704 incr_y = height_offset;
706 if (options->printer.page_text_position == HEADER_1 && page_text->len > 0)
708 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
709 pango_cairo_show_layout(cr, layout_page);
711 incr_y = incr_y + pango_page_height;
714 if (options->printer.image_text_position == HEADER_1 && image_text->len > 0)
716 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
717 pango_cairo_show_layout(cr, layout_image);
719 incr_y = incr_y + pango_image_height;
722 if (options->printer.page_text_position == HEADER_2 && page_text->len > 0)
724 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
725 pango_cairo_show_layout(cr, layout_page);
727 incr_y = incr_y + pango_page_height;
730 if (options->printer.image_text_position == HEADER_2 && image_text->len > 0)
732 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
733 pango_cairo_show_layout(cr, layout_image);
735 incr_y = incr_y + pango_image_height;
741 if (options->printer.page_text_position == FOOTER_1 && page_text->len > 0)
743 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
744 pango_cairo_show_layout(cr, layout_page);
746 incr_y = incr_y + pango_page_height;
749 if (options->printer.image_text_position == FOOTER_1 && image_text->len > 0)
751 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
752 pango_cairo_show_layout(cr, layout_image);
754 incr_y = incr_y + pango_image_height;
757 if (options->printer.page_text_position == FOOTER_2 && page_text->len > 0)
759 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
760 pango_cairo_show_layout(cr, layout_page);
762 incr_y = incr_y + pango_page_height;
765 if (options->printer.image_text_position == FOOTER_2 && image_text->len > 0)
767 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
768 pango_cairo_show_layout(cr, layout_image);
771 cairo_scale(cr, scale, scale);
773 cairo_rectangle(cr, width_offset * scale , image_y, pixbuf_image_width / scale, pixbuf_image_height / scale);
774 gdk_cairo_set_source_pixbuf(cr, pixbuf, width_offset / scale, image_y / scale);
777 if (image_text->len > 0)
779 g_object_unref(layout_image);
780 g_string_free(image_text, TRUE);
782 if (page_text->len > 0)
784 g_object_unref(layout_page);
785 g_string_free(page_text, TRUE);
788 if (rotated) g_object_unref(rotated);
791 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *, gpointer user_data)
793 auto pw = static_cast<PrintWindow *>(user_data);
796 page_count = print_layout_page_count(pw);
797 gtk_print_operation_set_n_pages (operation, page_count);
799 print_job_render_image(pw);
803 GObject *option_tab_cb(GtkPrintOperation *, gpointer user_data)
805 auto pw = static_cast<PrintWindow *>(user_data);
807 return G_OBJECT(pw->vbox);
810 static void print_pref_store(PrintWindow *pw)
816 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
817 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
818 g_free(options->printer.page_text);
819 options->printer.page_text = g_strdup(tmp);
823 static void end_print_cb(GtkPrintOperation *operation, GtkPrintContext *, gpointer data)
825 auto pw = static_cast<PrintWindow *>(data);
829 GtkPrintSettings *print_settings;
830 GtkPageSetup *page_setup;
831 GError *error = nullptr;
833 print_settings = gtk_print_operation_get_print_settings(operation);
834 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
836 gtk_print_settings_to_file(print_settings, path, &error);
839 log_printf("Error: Print settings save failed:\n%s", error->message);
844 g_object_unref(print_settings);
846 page_setup = gtk_print_operation_get_default_page_setup(operation);
847 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
849 gtk_page_setup_to_file(page_setup, path, &error);
852 log_printf("Error: Print page setup save failed:\n%s", error->message);
857 g_object_unref(page_setup);
859 print_pref_store(pw);
861 work = pw->print_pixbuf_queue;
864 pixbuf = static_cast<GdkPixbuf *>(work->data);
867 g_object_unref(pixbuf);
871 g_list_free(pw->print_pixbuf_queue);
872 g_object_unref(pw->page_text);
876 static void print_response_cb(GtkDialog *dialog, gint, gpointer)
878 gq_gtk_widget_destroy(GTK_WIDGET(dialog));
881 void print_window_new(FileData *, GList *selection, GList *, GtkWidget *parent)
884 GtkPrintOperation *operation;
885 GtkPageSetup *page_setup;
888 GError *error = nullptr;
890 GtkPrintSettings *settings;
892 auto pw = g_new0(PrintWindow, 1);
894 pw->source_selection = file_data_process_groups_in_selection(selection, FALSE, nullptr);
896 if (print_layout_page_count(pw) == 0)
903 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
904 gtk_container_set_border_width(GTK_CONTAINER(vbox), PREF_PAD_BORDER);
905 gtk_widget_show(vbox);
907 print_text_menu(vbox, pw);
910 pw->print_pixbuf_queue = nullptr;
911 pw->job_render_finished = FALSE;
914 operation = gtk_print_operation_new();
915 settings = gtk_print_settings_new();
917 gtk_print_operation_set_custom_tab_label(operation, _("Options"));
918 gtk_print_operation_set_use_full_page(operation, TRUE);
919 gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
920 gtk_print_operation_set_embed_page_setup(operation, TRUE);
921 gtk_print_operation_set_allow_async (operation, TRUE);
922 dir = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
925 dir = g_get_home_dir();
928 uri = g_build_filename("file:/", dir, "geeqie-file.pdf", NULL);
929 gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
932 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
933 gtk_print_settings_load_file(settings, path, &error);
936 log_printf("Error: Printer settings load failed:\n%s", error->message);
940 gtk_print_operation_set_print_settings(operation, settings);
943 page_setup = gtk_page_setup_new();
944 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
945 gtk_page_setup_load_file(page_setup, path, &error);
948 log_printf("Error: Print page setup load failed:\n%s", error->message);
952 gtk_print_operation_set_default_page_setup(operation, page_setup);
955 g_signal_connect (G_OBJECT (operation), "begin-print",
956 G_CALLBACK (begin_print), pw);
957 g_signal_connect (G_OBJECT (operation), "draw-page",
958 G_CALLBACK (draw_page), pw);
959 g_signal_connect (G_OBJECT (operation), "end-print",
960 G_CALLBACK (end_print_cb), pw);
961 g_signal_connect (G_OBJECT (operation), "create-custom-widget",
962 G_CALLBACK (option_tab_cb), pw);
963 g_signal_connect (G_OBJECT (operation), "paginate",
964 G_CALLBACK (paginate_cb), pw);
966 gtk_print_operation_set_n_pages(operation, print_layout_page_count(pw));
968 gtk_print_operation_run(operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
969 GTK_WINDOW (parent), &error);
975 dialog = gtk_message_dialog_new(GTK_WINDOW (parent),
976 GTK_DIALOG_DESTROY_WITH_PARENT,
979 "%s", error->message);
980 g_error_free (error);
982 g_signal_connect(dialog, "response", G_CALLBACK(print_response_cb), NULL);
984 gtk_widget_show (dialog);
987 g_object_unref(page_setup);
988 g_object_unref(settings);
990 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */