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 "image-load.h"
28 #include "pixbuf-util.h"
30 #include "ui-fileops.h"
32 #define PRINT_SETTINGS "print_settings" // filename save printer settings
33 #define PAGE_SETUP "page_setup" // filename save page setup
35 /* padding between objects */
36 #define PRINT_TEXT_PADDING 3.0
38 /* method to use when scaling down image data */
39 #define PRINT_MAX_INTERP GDK_INTERP_BILINEAR
41 /* reverse order is important */
52 GList *source_selection;
55 GtkTextBuffer *page_text;
56 gchar *template_string;
58 ImageLoader *job_loader;
60 GList *print_pixbuf_queue;
61 gboolean job_render_finished;
66 static gint print_layout_page_count(PrintWindow *pw)
70 images = g_list_length(pw->source_selection);
72 if (images < 1 ) return 0;
77 static gboolean print_job_render_image(PrintWindow *pw);
79 static void print_job_render_image_loader_done(ImageLoader *il, gpointer data)
81 auto pw = static_cast<PrintWindow *>(data);
84 pixbuf = image_loader_get_pixbuf(il);
87 pw->print_pixbuf_queue = g_list_append(pw->print_pixbuf_queue, pixbuf);
89 image_loader_free(pw->job_loader);
90 pw->job_loader = nullptr;
94 if (!print_job_render_image(pw))
96 pw->job_render_finished = TRUE;
100 static gboolean print_job_render_image(PrintWindow *pw)
102 FileData *fd = nullptr;
104 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, pw->job_page));
105 if (!fd) return FALSE;
107 image_loader_free(pw->job_loader);
108 pw->job_loader = nullptr;
110 pw->job_loader = image_loader_new(fd);
111 g_signal_connect(G_OBJECT(pw->job_loader), "done",
112 (GCallback)print_job_render_image_loader_done, pw);
114 if (!image_loader_start(pw->job_loader))
116 image_loader_free(pw->job_loader);
117 pw->job_loader= nullptr;
122 #pragma GCC diagnostic push
123 #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
124 static void font_activated_cb(GtkFontChooser *widget, gchar *fontname, gpointer option)
126 option = g_strdup(fontname);
130 gq_gtk_widget_destroy(GTK_WIDGET(widget));
132 #pragma GCC diagnostic pop
134 static void font_response_cb(GtkDialog *dialog, int response_id, gpointer option)
138 if (response_id == GTK_RESPONSE_OK)
140 font = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
142 option = g_strdup(font);
146 gq_gtk_widget_destroy(GTK_WIDGET(dialog));
149 static void print_set_font_cb(GtkWidget *widget, gpointer data)
154 if (g_strcmp0(static_cast<const gchar *>(data), "Image text font") == 0)
156 option = options->printer.image_font;
160 option = options->printer.page_font;
163 dialog = gtk_font_chooser_dialog_new(static_cast<const gchar *>(data), GTK_WINDOW(gtk_widget_get_toplevel(widget)));
164 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
165 gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dialog), static_cast<const gchar *>(option));
167 g_signal_connect(dialog, "font-activated", G_CALLBACK(font_activated_cb), option);
168 g_signal_connect(dialog, "response", G_CALLBACK(font_response_cb), option);
170 gtk_widget_show(dialog);
173 static gint set_toggle(GSList *list, TextPosition pos)
175 GtkToggleButton *current_sel;
176 GtkToggleButton *new_sel;
179 current_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, pos)->data);
180 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(current_sel)))
187 new_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, new_pos)->data);
188 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(new_sel), TRUE);
193 static void image_text_position_h1_cb(GtkWidget *widget, gpointer data)
195 auto pw = static_cast<PrintWindow *>(data);
198 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
200 new_set = set_toggle(pw->page_group, HEADER_1);
203 options->printer.page_text_position = new_set;
205 options->printer.image_text_position = HEADER_1;
209 static void image_text_position_h2_cb(GtkWidget *widget, gpointer data)
211 auto pw = static_cast<PrintWindow *>(data);
214 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
216 new_set = set_toggle(pw->page_group, HEADER_2);
219 options->printer.page_text_position = new_set;
221 options->printer.image_text_position = HEADER_2;
225 static void image_text_position_f1_cb(GtkWidget *widget, gpointer data)
227 auto pw = static_cast<PrintWindow *>(data);
230 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
232 new_set = set_toggle(pw->page_group, FOOTER_1);
235 options->printer.page_text_position = new_set;
237 options->printer.image_text_position = FOOTER_1;
241 static void image_text_position_f2_cb(GtkWidget *widget, gpointer data)
243 auto pw = static_cast<PrintWindow *>(data);
246 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
248 new_set = set_toggle(pw->page_group, FOOTER_2);
251 options->printer.page_text_position = new_set;
253 options->printer.image_text_position = FOOTER_2;
257 static void page_text_position_h1_cb(GtkWidget *widget, gpointer data)
259 auto pw = static_cast<PrintWindow *>(data);
262 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
264 new_set = set_toggle(pw->image_group, HEADER_1);
267 options->printer.image_text_position = new_set;
269 options->printer.page_text_position = HEADER_1;
273 static void page_text_position_h2_cb(GtkWidget *widget, gpointer data)
275 auto pw = static_cast<PrintWindow *>(data);
278 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
280 new_set = set_toggle(pw->image_group, HEADER_2);
283 options->printer.image_text_position = new_set;
285 options->printer.page_text_position = HEADER_2;
289 static void page_text_position_f1_cb(GtkWidget *widget, gpointer data)
291 auto pw = static_cast<PrintWindow *>(data);
294 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
296 new_set = set_toggle(pw->image_group, FOOTER_1);
299 options->printer.image_text_position = new_set;
301 options->printer.page_text_position = FOOTER_1;
305 static void page_text_position_f2_cb(GtkWidget *widget, gpointer data)
307 auto pw = static_cast<PrintWindow *>(data);
310 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
312 new_set = set_toggle(pw->image_group, FOOTER_2);
315 options->printer.image_text_position = new_set;
317 options->printer.page_text_position = FOOTER_2;
321 static void set_print_image_text_string(gchar **template_string, const gchar *value)
323 g_assert(template_string);
325 g_free(*template_string);
326 *template_string = g_strdup(value);
329 static void image_text_template_view_changed_cb(GtkWidget *, gpointer data)
331 GtkWidget *pTextView;
332 GtkTextBuffer *pTextBuffer;
336 pTextView = GTK_WIDGET(data);
338 pTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pTextView));
339 gtk_text_buffer_get_start_iter(pTextBuffer, &iStart);
340 gtk_text_buffer_get_end_iter(pTextBuffer, &iEnd);
342 set_print_image_text_string(&options->printer.template_string,
343 gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE));
346 #define PRE_FORMATTED_COLUMNS 4
347 static void print_text_menu(GtkWidget *box, PrintWindow *pw)
354 GtkWidget *image_text_button;
355 GtkWidget *page_text_button;
357 GtkWidget *page_text_view;
358 GtkWidget *image_text_template_view;
360 GtkWidget *scrolled_pre_formatted;
361 GtkTextBuffer *buffer;
363 group = pref_group_new(box, FALSE, _("Image text"), GTK_ORIENTATION_VERTICAL);
365 image_text_button = pref_checkbox_new_int(group, _("Show image text"),
366 options->printer.show_image_text, &options->printer.show_image_text);
368 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
370 pref_checkbox_link_sensitivity(image_text_button, subgroup);
372 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
373 gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
375 /* order is important */
376 button1 = pref_radiobutton_new(hbox, nullptr, "Header 1",
377 options->printer.image_text_position == HEADER_1,
378 G_CALLBACK(image_text_position_h1_cb), pw);
379 button1 = pref_radiobutton_new(hbox, button1, "Header 2",
380 options->printer.image_text_position == HEADER_2,
381 G_CALLBACK(image_text_position_h2_cb), pw);
382 button1 = pref_radiobutton_new(hbox, button1, "Footer 1",
383 options->printer.image_text_position == FOOTER_1,
384 G_CALLBACK(image_text_position_f1_cb), pw);
385 button1 = pref_radiobutton_new(hbox, button1, "Footer 2",
386 options->printer.image_text_position == FOOTER_2,
387 G_CALLBACK(image_text_position_f2_cb), pw);
388 gtk_widget_show(hbox);
389 pw->image_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1)));
391 image_text_template_view = gtk_text_view_new();
393 scrolled_pre_formatted = osd_new(PRE_FORMATTED_COLUMNS, image_text_template_view);
394 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled_pre_formatted, FALSE, FALSE, 0);
395 gtk_widget_show(scrolled_pre_formatted);
396 gtk_widget_show(subgroup);
398 gtk_widget_set_tooltip_markup(image_text_template_view,
399 _("Extensive formatting options are shown in the Help file"));
401 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
402 gtk_widget_set_size_request(scrolled, 200, 50);
403 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
404 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
405 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
406 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
407 gtk_widget_show(scrolled);
409 gq_gtk_container_add(GTK_WIDGET(scrolled), image_text_template_view);
410 gtk_widget_show(image_text_template_view);
412 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(image_text_template_view));
413 if (options->printer.template_string) gtk_text_buffer_set_text(buffer, options->printer.template_string, -1);
414 g_signal_connect(G_OBJECT(buffer), "changed",
415 G_CALLBACK(image_text_template_view_changed_cb), image_text_template_view);
417 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
419 button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
420 G_CALLBACK(print_set_font_cb), const_cast<char *>("Image text font"));
422 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
423 gtk_widget_show(button);
425 pref_spacer(group, PREF_PAD_GAP);
427 group = pref_group_new(box, FALSE, _("Page text"), GTK_ORIENTATION_VERTICAL);
429 page_text_button = pref_checkbox_new_int(group, _("Show page text"),
430 options->printer.show_page_text, &options->printer.show_page_text);
432 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
433 pref_checkbox_link_sensitivity(page_text_button, subgroup);
435 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
436 gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
438 /* order is important */
439 button2 = pref_radiobutton_new(hbox, nullptr, "Header 1",
440 options->printer.page_text_position == HEADER_1,
441 G_CALLBACK(page_text_position_h1_cb), pw);
442 button2 = pref_radiobutton_new(hbox, button2, "Header 2",
443 options->printer.page_text_position == HEADER_2,
444 G_CALLBACK(page_text_position_h2_cb), pw);
445 button2 = pref_radiobutton_new(hbox, button2, "Footer 1",
446 options->printer.page_text_position == FOOTER_1,
447 G_CALLBACK(page_text_position_f1_cb), pw);
448 button2 = pref_radiobutton_new(hbox, button2, "Footer 2",
449 options->printer.page_text_position == FOOTER_2,
450 G_CALLBACK(page_text_position_f2_cb), pw);
451 gtk_widget_show(hbox);
452 pw->page_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button2)));
454 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
455 gtk_widget_set_size_request(scrolled, 50, 50);
456 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
457 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
458 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
459 gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
460 gtk_widget_show(scrolled);
462 page_text_view = gtk_text_view_new();
463 pw->page_text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page_text_view ));
464 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(pw->page_text), options->printer.page_text, -1);
465 g_object_ref(pw->page_text);
467 gtk_widget_set_tooltip_markup(page_text_view, (_("Text shown on each page of a single or multi-page print job")));
468 gq_gtk_container_add(GTK_WIDGET(scrolled), page_text_view);
469 gtk_widget_show(page_text_view);
471 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
473 button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
474 G_CALLBACK(print_set_font_cb), const_cast<char *>("Page text font"));
476 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
477 gtk_widget_show(button);
480 static gboolean paginate_cb(GtkPrintOperation *, GtkPrintContext *, gpointer data)
482 auto pw = static_cast<PrintWindow *>(data);
484 if (pw->job_render_finished)
494 gchar *form_image_text(const gchar *template_string, FileData *fd, PrintWindow *pw, gint page_nr, gint total)
497 gchar *text = nullptr;
501 gchar *collection_name;
503 if (!fd) return nullptr;
507 vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
509 window_title = g_strdup(gtk_window_get_title(GTK_WINDOW(pw->parent)));
510 delimiter = g_strstr_len(window_title, -1, " - Collection - ");
513 collection_name = g_strndup(window_title, delimiter - window_title);
517 collection_name = nullptr;
519 g_free(window_title);
523 osd_template_insert(vars, "collection", collection_name, OSDT_NONE);
526 osd_template_insert(vars, "number", g_strdup_printf("%d", page_nr + 1), OSDT_NO_DUP);
527 osd_template_insert(vars, "total", g_strdup_printf("%d", total), OSDT_NO_DUP);
528 osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
529 osd_template_insert(vars, "date", fd ? (const_cast<gchar *>(text_from_time(fd->date))) : "", OSDT_NONE);
530 osd_template_insert(vars, "size", fd ? (text_from_size_abrev(fd->size)) : g_strdup(""), OSDT_FREE);
535 w = gdk_pixbuf_get_width(fd->pixbuf);
536 h = gdk_pixbuf_get_height(fd->pixbuf);
538 osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
539 osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
540 osd_template_insert(vars, "res", g_strdup_printf("%d × %d", w, h), OSDT_FREE);
544 osd_template_insert(vars, "width", nullptr, OSDT_NONE);
545 osd_template_insert(vars, "height", nullptr, OSDT_NONE);
546 osd_template_insert(vars, "res", nullptr, OSDT_NONE);
549 text = image_osd_mkinfo(template_string, fd, vars);
550 g_hash_table_destroy(vars);
552 g_free(collection_name);
557 static void draw_page(GtkPrintOperation *, GtkPrintContext *context, gint page_nr, gpointer data)
559 auto pw = static_cast<PrintWindow *>(data);
562 gdouble context_width, context_height;
563 gdouble pixbuf_image_width, pixbuf_image_height;
564 gdouble width_offset;
565 gdouble height_offset;
567 GdkPixbuf *rotated = nullptr;
568 PangoLayout *layout_image = nullptr;
569 PangoLayout *layout_page = nullptr;
570 PangoFontDescription *desc;
571 GString *image_text = g_string_new(nullptr);
572 GString *page_text = g_string_new(nullptr);
573 PangoRectangle ink_rect, logical_rect;
575 gdouble image_text_width, image_text_height, page_text_width, page_text_height;
578 gdouble pango_height;
579 gdouble pango_image_height;
580 gdouble pango_page_height;
581 GtkTextIter start, end;
585 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, page_nr));
586 total = g_list_length(pw->source_selection);
588 pixbuf = static_cast<GdkPixbuf *>(g_list_nth_data(pw->print_pixbuf_queue, page_nr));
589 if (fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
591 rotated = pixbuf_apply_orientation(pixbuf, fd->exif_orientation);
595 pixbuf_image_width = gdk_pixbuf_get_width(pixbuf);
596 pixbuf_image_height = gdk_pixbuf_get_height(pixbuf);
598 if (options->printer.show_image_text)
600 image_text = g_string_append(image_text, form_image_text(options->printer.template_string, fd, pw, page_nr, total));
603 if (options->printer.show_page_text)
605 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
607 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
608 page_text = g_string_append(page_text, tmp);
613 cr = gtk_print_context_get_cairo_context(context);
614 context_width = gtk_print_context_get_width(context);
615 context_height = gtk_print_context_get_height(context);
617 pango_image_height = 0;
618 pango_page_height = 0;
619 image_text_width = 0;
622 if (image_text->len > 0)
624 layout_image = pango_cairo_create_layout(cr);
626 pango_layout_set_text(layout_image, image_text->str, -1);
627 desc = pango_font_description_from_string(options->printer.image_font);
628 pango_layout_set_font_description(layout_image, desc);
630 pango_layout_get_extents(layout_image, &ink_rect, &logical_rect);
631 image_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
632 image_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
634 pango_layout_set_alignment(layout_image, PANGO_ALIGN_CENTER);
635 pango_layout_set_text(layout_image, image_text->str, -1);
637 pango_image_height = image_text_height + PRINT_TEXT_PADDING * 2;
639 pango_font_description_free(desc);
642 if (page_text->len > 0)
644 layout_page = pango_cairo_create_layout(cr);
646 pango_layout_set_text(layout_page, page_text->str, -1);
647 desc = pango_font_description_from_string(options->printer.page_font);
648 pango_layout_set_font_description(layout_page, desc);
650 pango_layout_get_extents(layout_page, &ink_rect, &logical_rect);
651 page_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
652 page_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
654 pango_layout_set_alignment(layout_page, PANGO_ALIGN_CENTER);
655 pango_layout_set_text(layout_page, page_text->str, -1);
657 pango_page_height = page_text_height + PRINT_TEXT_PADDING * 2;
659 pango_font_description_free(desc);
662 pango_height = pango_image_height + pango_page_height;
664 if ((context_width / pixbuf_image_width) < ((context_height - pango_height) / pixbuf_image_height))
667 scale = context_width / pixbuf_image_width;
668 h = pixbuf_image_height * scale;
669 height_offset = (context_height - (h + pango_height)) / 2;
674 h = context_height - pango_height ;
675 scale = (context_height - pango_height) / pixbuf_image_height;
676 w = pixbuf_image_width * scale;
678 width_offset = (context_width - (pixbuf_image_width * scale)) / 2;
681 incr_y = height_offset;
683 if (options->printer.page_text_position == HEADER_1 && page_text->len > 0)
685 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
686 pango_cairo_show_layout(cr, layout_page);
688 incr_y = incr_y + pango_page_height;
691 if (options->printer.image_text_position == HEADER_1 && image_text->len > 0)
693 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
694 pango_cairo_show_layout(cr, layout_image);
696 incr_y = incr_y + pango_image_height;
699 if (options->printer.page_text_position == HEADER_2 && 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_2 && image_text->len > 0)
709 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
710 pango_cairo_show_layout(cr, layout_image);
712 incr_y = incr_y + pango_image_height;
718 if (options->printer.page_text_position == FOOTER_1 && page_text->len > 0)
720 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
721 pango_cairo_show_layout(cr, layout_page);
723 incr_y = incr_y + pango_page_height;
726 if (options->printer.image_text_position == FOOTER_1 && image_text->len > 0)
728 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
729 pango_cairo_show_layout(cr, layout_image);
731 incr_y = incr_y + pango_image_height;
734 if (options->printer.page_text_position == FOOTER_2 && page_text->len > 0)
736 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
737 pango_cairo_show_layout(cr, layout_page);
739 incr_y = incr_y + pango_page_height;
742 if (options->printer.image_text_position == FOOTER_2 && 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);
748 cairo_scale(cr, scale, scale);
750 cairo_rectangle(cr, width_offset * scale , image_y, pixbuf_image_width / scale, pixbuf_image_height / scale);
751 gdk_cairo_set_source_pixbuf(cr, pixbuf, width_offset / scale, image_y / scale);
754 if (image_text->len > 0)
756 g_object_unref(layout_image);
757 g_string_free(image_text, TRUE);
759 if (page_text->len > 0)
761 g_object_unref(layout_page);
762 g_string_free(page_text, TRUE);
765 if (rotated) g_object_unref(rotated);
768 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *, gpointer user_data)
770 auto pw = static_cast<PrintWindow *>(user_data);
773 page_count = print_layout_page_count(pw);
774 gtk_print_operation_set_n_pages (operation, page_count);
776 print_job_render_image(pw);
780 GObject *option_tab_cb(GtkPrintOperation *, gpointer user_data)
782 auto pw = static_cast<PrintWindow *>(user_data);
784 return G_OBJECT(pw->vbox);
787 static void print_pref_store(PrintWindow *pw)
790 GtkTextIter start, end;
792 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
793 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
794 g_free(options->printer.page_text);
795 options->printer.page_text = g_strdup(tmp);
799 static void end_print_cb(GtkPrintOperation *operation, GtkPrintContext *, gpointer data)
801 auto pw = static_cast<PrintWindow *>(data);
805 GtkPrintSettings *print_settings;
806 GtkPageSetup *page_setup;
807 GError *error = nullptr;
809 print_settings = gtk_print_operation_get_print_settings(operation);
810 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
812 gtk_print_settings_to_file(print_settings, path, &error);
815 log_printf("Error: Print settings save failed:\n%s", error->message);
820 g_object_unref(print_settings);
822 page_setup = gtk_print_operation_get_default_page_setup(operation);
823 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
825 gtk_page_setup_to_file(page_setup, path, &error);
828 log_printf("Error: Print page setup save failed:\n%s", error->message);
833 g_object_unref(page_setup);
835 print_pref_store(pw);
837 work = pw->print_pixbuf_queue;
840 pixbuf = static_cast<GdkPixbuf *>(work->data);
843 g_object_unref(pixbuf);
847 g_list_free(pw->print_pixbuf_queue);
848 g_object_unref(pw->page_text);
852 static void print_response_cb(GtkDialog *dialog, gint, gpointer)
854 gq_gtk_widget_destroy(GTK_WIDGET(dialog));
857 void print_window_new(FileData *, GList *selection, GList *, GtkWidget *parent)
860 GtkPrintOperation *operation;
861 GtkPageSetup *page_setup;
864 GError *error = nullptr;
866 GtkPrintSettings *settings;
868 auto pw = g_new0(PrintWindow, 1);
870 pw->source_selection = file_data_process_groups_in_selection(selection, FALSE, nullptr);
872 if (print_layout_page_count(pw) == 0)
879 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
880 gtk_container_set_border_width(GTK_CONTAINER(vbox), PREF_PAD_BORDER);
881 gtk_widget_show(vbox);
883 print_text_menu(vbox, pw);
886 pw->print_pixbuf_queue = nullptr;
887 pw->job_render_finished = FALSE;
890 operation = gtk_print_operation_new();
891 settings = gtk_print_settings_new();
893 gtk_print_operation_set_custom_tab_label(operation, "Options");
894 gtk_print_operation_set_use_full_page(operation, TRUE);
895 gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
896 gtk_print_operation_set_embed_page_setup(operation, TRUE);
897 gtk_print_operation_set_allow_async (operation, TRUE);
898 dir = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
901 dir = g_get_home_dir();
904 uri = g_build_filename("file:/", dir, "geeqie-file.pdf", NULL);
905 gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
908 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
909 gtk_print_settings_load_file(settings, path, &error);
912 log_printf("Error: Printer settings load failed:\n%s", error->message);
916 gtk_print_operation_set_print_settings(operation, settings);
919 page_setup = gtk_page_setup_new();
920 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
921 gtk_page_setup_load_file(page_setup, path, &error);
924 log_printf("Error: Print page setup load failed:\n%s", error->message);
928 gtk_print_operation_set_default_page_setup(operation, page_setup);
931 g_signal_connect (G_OBJECT (operation), "begin-print",
932 G_CALLBACK (begin_print), pw);
933 g_signal_connect (G_OBJECT (operation), "draw-page",
934 G_CALLBACK (draw_page), pw);
935 g_signal_connect (G_OBJECT (operation), "end-print",
936 G_CALLBACK (end_print_cb), pw);
937 g_signal_connect (G_OBJECT (operation), "create-custom-widget",
938 G_CALLBACK (option_tab_cb), pw);
939 g_signal_connect (G_OBJECT (operation), "paginate",
940 G_CALLBACK (paginate_cb), pw);
942 gtk_print_operation_set_n_pages(operation, print_layout_page_count(pw));
944 gtk_print_operation_run(operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
945 GTK_WINDOW (parent), &error);
951 dialog = gtk_message_dialog_new(GTK_WINDOW (parent),
952 GTK_DIALOG_DESTROY_WITH_PARENT,
955 "%s", error->message);
956 g_error_free (error);
958 g_signal_connect(dialog, "response", G_CALLBACK(print_response_cb), NULL);
960 gtk_widget_show (dialog);
963 g_object_unref(page_setup);
964 g_object_unref(settings);
966 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */