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;
123 static void print_set_font_cb(GtkWidget *widget, gpointer data)
127 if (g_strcmp0(static_cast<const gchar *>(data), "Image text font") == 0)
129 option = options->printer.image_font;
133 option = options->printer.page_font;
138 PangoFontDescription *font_desc;
140 dialog = gtk_font_chooser_dialog_new(static_cast<const gchar *>(data), GTK_WINDOW(gtk_widget_get_toplevel(widget)));
141 gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dialog), static_cast<const gchar *>(option));
143 if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_CANCEL)
145 font_desc = gtk_font_chooser_get_font_desc(GTK_FONT_CHOOSER(dialog));
146 font = pango_font_description_to_string(font_desc);
148 option = g_strdup(font);
152 gtk_widget_destroy(dialog);
155 static gint set_toggle(GSList *list, TextPosition pos)
157 GtkToggleButton *current_sel;
158 GtkToggleButton *new_sel;
161 current_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, pos)->data);
162 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(current_sel)))
169 new_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, new_pos)->data);
170 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(new_sel), TRUE);
175 static void image_text_position_h1_cb(GtkWidget *widget, gpointer data)
177 auto pw = static_cast<PrintWindow *>(data);
180 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
182 new_set = set_toggle(pw->page_group, HEADER_1);
185 options->printer.page_text_position = new_set;
187 options->printer.image_text_position = HEADER_1;
191 static void image_text_position_h2_cb(GtkWidget *widget, gpointer data)
193 auto pw = static_cast<PrintWindow *>(data);
196 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
198 new_set = set_toggle(pw->page_group, HEADER_2);
201 options->printer.page_text_position = new_set;
203 options->printer.image_text_position = HEADER_2;
207 static void image_text_position_f1_cb(GtkWidget *widget, gpointer data)
209 auto pw = static_cast<PrintWindow *>(data);
212 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
214 new_set = set_toggle(pw->page_group, FOOTER_1);
217 options->printer.page_text_position = new_set;
219 options->printer.image_text_position = FOOTER_1;
223 static void image_text_position_f2_cb(GtkWidget *widget, gpointer data)
225 auto pw = static_cast<PrintWindow *>(data);
228 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
230 new_set = set_toggle(pw->page_group, FOOTER_2);
233 options->printer.page_text_position = new_set;
235 options->printer.image_text_position = FOOTER_2;
239 static void page_text_position_h1_cb(GtkWidget *widget, gpointer data)
241 auto pw = static_cast<PrintWindow *>(data);
244 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
246 new_set = set_toggle(pw->image_group, HEADER_1);
249 options->printer.image_text_position = new_set;
251 options->printer.page_text_position = HEADER_1;
255 static void page_text_position_h2_cb(GtkWidget *widget, gpointer data)
257 auto pw = static_cast<PrintWindow *>(data);
260 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
262 new_set = set_toggle(pw->image_group, HEADER_2);
265 options->printer.image_text_position = new_set;
267 options->printer.page_text_position = HEADER_2;
271 static void page_text_position_f1_cb(GtkWidget *widget, gpointer data)
273 auto pw = static_cast<PrintWindow *>(data);
276 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
278 new_set = set_toggle(pw->image_group, FOOTER_1);
281 options->printer.image_text_position = new_set;
283 options->printer.page_text_position = FOOTER_1;
287 static void page_text_position_f2_cb(GtkWidget *widget, gpointer data)
289 auto pw = static_cast<PrintWindow *>(data);
292 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
294 new_set = set_toggle(pw->image_group, FOOTER_2);
297 options->printer.image_text_position = new_set;
299 options->printer.page_text_position = FOOTER_2;
303 static void set_print_image_text_string(gchar **template_string, const gchar *value)
305 g_assert(template_string);
307 g_free(*template_string);
308 *template_string = g_strdup(value);
311 static void image_text_template_view_changed_cb(GtkWidget *, gpointer data)
313 GtkWidget *pTextView;
314 GtkTextBuffer *pTextBuffer;
318 pTextView = GTK_WIDGET(data);
320 pTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pTextView));
321 gtk_text_buffer_get_start_iter(pTextBuffer, &iStart);
322 gtk_text_buffer_get_end_iter(pTextBuffer, &iEnd);
324 set_print_image_text_string(&options->printer.template_string,
325 gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE));
328 #define PRE_FORMATTED_COLUMNS 4
329 static void print_text_menu(GtkWidget *box, PrintWindow *pw)
336 GtkWidget *image_text_button;
337 GtkWidget *page_text_button;
339 GtkWidget *page_text_view;
340 GtkWidget *image_text_template_view;
342 GtkWidget *scrolled_pre_formatted;
343 GtkTextBuffer *buffer;
345 group = pref_group_new(box, FALSE, _("Image text"), GTK_ORIENTATION_VERTICAL);
347 image_text_button = pref_checkbox_new_int(group, _("Show image text"),
348 options->printer.show_image_text, &options->printer.show_image_text);
350 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
352 pref_checkbox_link_sensitivity(image_text_button, subgroup);
354 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
355 gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
357 /* order is important */
358 button1 = pref_radiobutton_new(hbox, nullptr, "Header 1",
359 options->printer.image_text_position == HEADER_1,
360 G_CALLBACK(image_text_position_h1_cb), pw);
361 button1 = pref_radiobutton_new(hbox, button1, "Header 2",
362 options->printer.image_text_position == HEADER_2,
363 G_CALLBACK(image_text_position_h2_cb), pw);
364 button1 = pref_radiobutton_new(hbox, button1, "Footer 1",
365 options->printer.image_text_position == FOOTER_1,
366 G_CALLBACK(image_text_position_f1_cb), pw);
367 button1 = pref_radiobutton_new(hbox, button1, "Footer 2",
368 options->printer.image_text_position == FOOTER_2,
369 G_CALLBACK(image_text_position_f2_cb), pw);
370 gtk_widget_show(hbox);
371 pw->image_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1)));
373 image_text_template_view = gtk_text_view_new();
375 scrolled_pre_formatted = osd_new(PRE_FORMATTED_COLUMNS, image_text_template_view);
376 gtk_box_pack_start(GTK_BOX(subgroup), scrolled_pre_formatted, FALSE, FALSE, 0);
377 gtk_widget_show(scrolled_pre_formatted);
378 gtk_widget_show(subgroup);
380 gtk_widget_set_tooltip_markup(image_text_template_view,
381 _("Extensive formatting options are shown in the Help file"));
383 scrolled = gtk_scrolled_window_new(nullptr, nullptr);
384 gtk_widget_set_size_request(scrolled, 200, 50);
385 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
386 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
387 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
388 gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
389 gtk_widget_show(scrolled);
391 gtk_container_add(GTK_CONTAINER(scrolled), image_text_template_view);
392 gtk_widget_show(image_text_template_view);
394 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(image_text_template_view));
395 if (options->printer.template_string) gtk_text_buffer_set_text(buffer, options->printer.template_string, -1);
396 g_signal_connect(G_OBJECT(buffer), "changed",
397 G_CALLBACK(image_text_template_view_changed_cb), image_text_template_view);
399 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
401 button = pref_button_new(nullptr, GTK_STOCK_SELECT_FONT, _("Font"), FALSE,
402 G_CALLBACK(print_set_font_cb), const_cast<char *>("Image text font"));
404 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
405 gtk_widget_show(button);
407 pref_spacer(group, PREF_PAD_GAP);
409 group = pref_group_new(box, FALSE, _("Page text"), GTK_ORIENTATION_VERTICAL);
411 page_text_button = pref_checkbox_new_int(group, _("Show page text"),
412 options->printer.show_page_text, &options->printer.show_page_text);
414 subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
415 pref_checkbox_link_sensitivity(page_text_button, subgroup);
417 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
418 gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
420 /* order is important */
421 button2 = pref_radiobutton_new(hbox, nullptr, "Header 1",
422 options->printer.page_text_position == HEADER_1,
423 G_CALLBACK(page_text_position_h1_cb), pw);
424 button2 = pref_radiobutton_new(hbox, button2, "Header 2",
425 options->printer.page_text_position == HEADER_2,
426 G_CALLBACK(page_text_position_h2_cb), pw);
427 button2 = pref_radiobutton_new(hbox, button2, "Footer 1",
428 options->printer.page_text_position == FOOTER_1,
429 G_CALLBACK(page_text_position_f1_cb), pw);
430 button2 = pref_radiobutton_new(hbox, button2, "Footer 2",
431 options->printer.page_text_position == FOOTER_2,
432 G_CALLBACK(page_text_position_f2_cb), pw);
433 gtk_widget_show(hbox);
434 pw->page_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button2)));
436 scrolled = gtk_scrolled_window_new(nullptr, nullptr);
437 gtk_widget_set_size_request(scrolled, 50, 50);
438 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
439 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
440 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
441 gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
442 gtk_widget_show(scrolled);
444 page_text_view = gtk_text_view_new();
445 pw->page_text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page_text_view ));
446 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(pw->page_text), options->printer.page_text, -1);
447 g_object_ref(pw->page_text);
449 gtk_widget_set_tooltip_markup(page_text_view, (_("Text shown on each page of a single or multi-page print job")));
450 gtk_container_add(GTK_CONTAINER(scrolled), page_text_view);
451 gtk_widget_show(page_text_view);
453 hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
455 button = pref_button_new(nullptr, GTK_STOCK_SELECT_FONT, _("Font"), FALSE,
456 G_CALLBACK(print_set_font_cb), const_cast<char *>("Page text font"));
458 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
459 gtk_widget_show(button);
462 static gboolean paginate_cb(GtkPrintOperation *, GtkPrintContext *, gpointer data)
464 auto pw = static_cast<PrintWindow *>(data);
466 if (pw->job_render_finished)
476 gchar *form_image_text(const gchar *template_string, FileData *fd, PrintWindow *pw, gint page_nr, gint total)
479 gchar *text = nullptr;
483 gchar *collection_name;
485 if (!fd) return nullptr;
489 vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
491 window_title = g_strdup(gtk_window_get_title(GTK_WINDOW(pw->parent)));
492 delimiter = g_strstr_len(window_title, -1, " - Collection - ");
495 collection_name = g_strndup(window_title, delimiter - window_title);
499 collection_name = nullptr;
501 g_free(window_title);
505 osd_template_insert(vars, "collection", collection_name, OSDT_NONE);
508 osd_template_insert(vars, "number", g_strdup_printf("%d", page_nr + 1), OSDT_NO_DUP);
509 osd_template_insert(vars, "total", g_strdup_printf("%d", total), OSDT_NO_DUP);
510 osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
511 osd_template_insert(vars, "date", fd ? (const_cast<gchar *>(text_from_time(fd->date))) : "", OSDT_NONE);
512 osd_template_insert(vars, "size", fd ? (text_from_size_abrev(fd->size)) : g_strdup(""), OSDT_FREE);
517 w = gdk_pixbuf_get_width(fd->pixbuf);
518 h = gdk_pixbuf_get_height(fd->pixbuf);
520 osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
521 osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
522 osd_template_insert(vars, "res", g_strdup_printf("%d × %d", w, h), OSDT_FREE);
526 osd_template_insert(vars, "width", nullptr, OSDT_NONE);
527 osd_template_insert(vars, "height", nullptr, OSDT_NONE);
528 osd_template_insert(vars, "res", nullptr, OSDT_NONE);
531 text = image_osd_mkinfo(template_string, fd, vars);
532 g_hash_table_destroy(vars);
534 g_free(collection_name);
539 static void draw_page(GtkPrintOperation *, GtkPrintContext *context, gint page_nr, gpointer data)
541 auto pw = static_cast<PrintWindow *>(data);
544 gdouble context_width, context_height;
545 gdouble pixbuf_image_width, pixbuf_image_height;
546 gdouble width_offset;
547 gdouble height_offset;
549 GdkPixbuf *rotated = nullptr;
550 PangoLayout *layout_image = nullptr;
551 PangoLayout *layout_page = nullptr;
552 PangoFontDescription *desc;
553 GString *image_text = g_string_new(nullptr);
554 GString *page_text = g_string_new(nullptr);
555 PangoRectangle ink_rect, logical_rect;
557 gdouble image_text_width, image_text_height, page_text_width, page_text_height;
560 gdouble pango_height;
561 gdouble pango_image_height;
562 gdouble pango_page_height;
563 GtkTextIter start, end;
567 fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, page_nr));
568 total = g_list_length(pw->source_selection);
570 pixbuf = static_cast<GdkPixbuf *>(g_list_nth_data(pw->print_pixbuf_queue, page_nr));
571 if (fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
573 rotated = pixbuf_apply_orientation(pixbuf, fd->exif_orientation);
577 pixbuf_image_width = gdk_pixbuf_get_width(pixbuf);
578 pixbuf_image_height = gdk_pixbuf_get_height(pixbuf);
580 if (options->printer.show_image_text)
582 image_text = g_string_append(image_text, form_image_text(options->printer.template_string, fd, pw, page_nr, total));
585 if (options->printer.show_page_text)
587 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
589 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
590 page_text = g_string_append(page_text, tmp);
595 cr = gtk_print_context_get_cairo_context(context);
596 context_width = gtk_print_context_get_width(context);
597 context_height = gtk_print_context_get_height(context);
599 pango_image_height = 0;
600 pango_page_height = 0;
601 image_text_width = 0;
604 if (image_text->len > 0)
606 layout_image = pango_cairo_create_layout(cr);
608 pango_layout_set_text(layout_image, image_text->str, -1);
609 desc = pango_font_description_from_string(options->printer.image_font);
610 pango_layout_set_font_description(layout_image, desc);
612 pango_layout_get_extents(layout_image, &ink_rect, &logical_rect);
613 image_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
614 image_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
616 pango_layout_set_alignment(layout_image, PANGO_ALIGN_CENTER);
617 pango_layout_set_text(layout_image, image_text->str, -1);
619 pango_image_height = image_text_height + PRINT_TEXT_PADDING * 2;
621 pango_font_description_free(desc);
624 if (page_text->len > 0)
626 layout_page = pango_cairo_create_layout(cr);
628 pango_layout_set_text(layout_page, page_text->str, -1);
629 desc = pango_font_description_from_string(options->printer.page_font);
630 pango_layout_set_font_description(layout_page, desc);
632 pango_layout_get_extents(layout_page, &ink_rect, &logical_rect);
633 page_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
634 page_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
636 pango_layout_set_alignment(layout_page, PANGO_ALIGN_CENTER);
637 pango_layout_set_text(layout_page, page_text->str, -1);
639 pango_page_height = page_text_height + PRINT_TEXT_PADDING * 2;
641 pango_font_description_free(desc);
644 pango_height = pango_image_height + pango_page_height;
646 if ((context_width / pixbuf_image_width) < ((context_height - pango_height) / pixbuf_image_height))
649 scale = context_width / pixbuf_image_width;
650 h = pixbuf_image_height * scale;
651 height_offset = (context_height - (h + pango_height)) / 2;
656 h = context_height - pango_height ;
657 scale = (context_height - pango_height) / pixbuf_image_height;
658 w = pixbuf_image_width * scale;
660 width_offset = (context_width - (pixbuf_image_width * scale)) / 2;
663 incr_y = height_offset;
665 if (options->printer.page_text_position == HEADER_1 && page_text->len > 0)
667 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
668 pango_cairo_show_layout(cr, layout_page);
670 incr_y = incr_y + pango_page_height;
673 if (options->printer.image_text_position == HEADER_1 && image_text->len > 0)
675 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
676 pango_cairo_show_layout(cr, layout_image);
678 incr_y = incr_y + pango_image_height;
681 if (options->printer.page_text_position == HEADER_2 && page_text->len > 0)
683 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
684 pango_cairo_show_layout(cr, layout_page);
686 incr_y = incr_y + pango_page_height;
689 if (options->printer.image_text_position == HEADER_2 && image_text->len > 0)
691 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
692 pango_cairo_show_layout(cr, layout_image);
694 incr_y = incr_y + pango_image_height;
700 if (options->printer.page_text_position == FOOTER_1 && page_text->len > 0)
702 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
703 pango_cairo_show_layout(cr, layout_page);
705 incr_y = incr_y + pango_page_height;
708 if (options->printer.image_text_position == FOOTER_1 && image_text->len > 0)
710 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
711 pango_cairo_show_layout(cr, layout_image);
713 incr_y = incr_y + pango_image_height;
716 if (options->printer.page_text_position == FOOTER_2 && page_text->len > 0)
718 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
719 pango_cairo_show_layout(cr, layout_page);
721 incr_y = incr_y + pango_page_height;
724 if (options->printer.image_text_position == FOOTER_2 && image_text->len > 0)
726 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
727 pango_cairo_show_layout(cr, layout_image);
730 cairo_scale(cr, scale, scale);
732 cairo_rectangle(cr, width_offset * scale , image_y, pixbuf_image_width / scale, pixbuf_image_height / scale);
733 gdk_cairo_set_source_pixbuf(cr, pixbuf, width_offset / scale, image_y / scale);
736 if (image_text->len > 0)
738 g_object_unref(layout_image);
739 g_string_free(image_text, TRUE);
741 if (page_text->len > 0)
743 g_object_unref(layout_page);
744 g_string_free(page_text, TRUE);
747 if (rotated) g_object_unref(rotated);
750 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *, gpointer user_data)
752 auto pw = static_cast<PrintWindow *>(user_data);
755 page_count = print_layout_page_count(pw);
756 gtk_print_operation_set_n_pages (operation, page_count);
758 print_job_render_image(pw);
762 GObject *option_tab_cb(GtkPrintOperation *, gpointer user_data)
764 auto pw = static_cast<PrintWindow *>(user_data);
766 return G_OBJECT(pw->vbox);
769 static void print_pref_store(PrintWindow *pw)
772 GtkTextIter start, end;
774 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
775 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
776 g_free(options->printer.page_text);
777 options->printer.page_text = g_strdup(tmp);
781 static void end_print_cb(GtkPrintOperation *operation, GtkPrintContext *, gpointer data)
783 auto pw = static_cast<PrintWindow *>(data);
787 GtkPrintSettings *print_settings;
788 GtkPageSetup *page_setup;
789 GError *error = nullptr;
791 print_settings = gtk_print_operation_get_print_settings(operation);
792 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
794 gtk_print_settings_to_file(print_settings, path, &error);
797 log_printf("Error: Print settings save failed:\n%s", error->message);
802 g_object_unref(print_settings);
804 page_setup = gtk_print_operation_get_default_page_setup(operation);
805 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
807 gtk_page_setup_to_file(page_setup, path, &error);
810 log_printf("Error: Print page setup save failed:\n%s", error->message);
815 g_object_unref(page_setup);
817 print_pref_store(pw);
819 work = pw->print_pixbuf_queue;
822 pixbuf = static_cast<GdkPixbuf *>(work->data);
825 g_object_unref(pixbuf);
829 g_list_free(pw->print_pixbuf_queue);
830 g_object_unref(pw->page_text);
834 void print_window_new(FileData *, GList *selection, GList *, GtkWidget *parent)
837 GtkPrintOperation *operation;
838 GtkPageSetup *page_setup;
841 GError *error = nullptr;
843 GtkPrintSettings *settings;
845 auto pw = g_new0(PrintWindow, 1);
847 pw->source_selection = file_data_process_groups_in_selection(selection, FALSE, nullptr);
849 if (print_layout_page_count(pw) == 0)
856 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
857 gtk_container_set_border_width(GTK_CONTAINER(vbox), PREF_PAD_BORDER);
858 gtk_widget_show(vbox);
860 print_text_menu(vbox, pw);
863 pw->print_pixbuf_queue = nullptr;
864 pw->job_render_finished = FALSE;
867 operation = gtk_print_operation_new();
868 settings = gtk_print_settings_new();
870 gtk_print_operation_set_custom_tab_label(operation, "Options");
871 gtk_print_operation_set_use_full_page(operation, TRUE);
872 gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
873 gtk_print_operation_set_embed_page_setup(operation, TRUE);
874 gtk_print_operation_set_allow_async (operation, TRUE);
875 dir = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
878 dir = g_get_home_dir();
881 uri = g_build_filename("file:/", dir, "geeqie-file.pdf", NULL);
882 gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
885 path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
886 gtk_print_settings_load_file(settings, path, &error);
889 log_printf("Error: Printer settings load failed:\n%s", error->message);
893 gtk_print_operation_set_print_settings(operation, settings);
896 page_setup = gtk_page_setup_new();
897 path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
898 gtk_page_setup_load_file(page_setup, path, &error);
901 log_printf("Error: Print page setup load failed:\n%s", error->message);
905 gtk_print_operation_set_default_page_setup(operation, page_setup);
908 g_signal_connect (G_OBJECT (operation), "begin-print",
909 G_CALLBACK (begin_print), pw);
910 g_signal_connect (G_OBJECT (operation), "draw-page",
911 G_CALLBACK (draw_page), pw);
912 g_signal_connect (G_OBJECT (operation), "end-print",
913 G_CALLBACK (end_print_cb), pw);
914 g_signal_connect (G_OBJECT (operation), "create-custom-widget",
915 G_CALLBACK (option_tab_cb), pw);
916 g_signal_connect (G_OBJECT (operation), "paginate",
917 G_CALLBACK (paginate_cb), pw);
919 gtk_print_operation_set_n_pages(operation, print_layout_page_count(pw));
921 gtk_print_operation_run(operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
922 GTK_WINDOW (parent), &error);
928 dialog = gtk_message_dialog_new(GTK_WINDOW (parent),
929 GTK_DIALOG_DESTROY_WITH_PARENT,
932 "%s", error->message);
933 g_error_free (error);
935 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL);
937 gtk_widget_show (dialog);
940 g_object_unref(page_setup);
941 g_object_unref(settings);
943 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */