1c71df66ae398ea1ae5af326434c391dd598f18e
[geeqie.git] / src / print.cc
1 /*
2  * Copyright (C) 2018 The Geeqie Team
3  *
4  * Author: Colin Clark
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "print.h"
22
23 #include <config.h>
24
25 #include "compat.h"
26 #include "debug.h"
27 #include "exif.h"
28 #include "filedata.h"
29 #include "image-load.h"
30 #include "intl.h"
31 #include "main-defines.h"
32 #include "options.h"
33 #include "osd.h"
34 #include "pixbuf-util.h"
35 #include "ui-misc.h"
36 #include "ui-fileops.h"
37
38 #define PRINT_SETTINGS "print_settings" // filename save printer settings
39 #define PAGE_SETUP "page_setup" // filename save page setup
40
41 /* padding between objects */
42 #define PRINT_TEXT_PADDING 3.0
43
44 /* method to use when scaling down image data */
45 #define PRINT_MAX_INTERP GDK_INTERP_BILINEAR
46
47 /* reverse order is important */
48 enum TextPosition {
49         FOOTER_2,
50         FOOTER_1,
51         HEADER_2,
52         HEADER_1
53 };
54
55 struct PrintWindow
56 {
57         GtkWidget *vbox;
58         GList *source_selection;
59
60         gint job_page;
61         GtkTextBuffer *page_text;
62         gchar *template_string;
63         GtkWidget *parent;
64         ImageLoader     *job_loader;
65
66         GList *print_pixbuf_queue;
67         gboolean job_render_finished;
68         GSList *image_group;
69         GSList *page_group;
70 };
71
72 static gint print_layout_page_count(PrintWindow *pw)
73 {
74         gint images;
75
76         images = g_list_length(pw->source_selection);
77
78         if (images < 1 ) return 0;
79
80         return images;
81 }
82
83 static gboolean print_job_render_image(PrintWindow *pw);
84
85 static void print_job_render_image_loader_done(ImageLoader *il, gpointer data)
86 {
87         auto pw = static_cast<PrintWindow *>(data);
88         GdkPixbuf *pixbuf;
89
90         pixbuf = image_loader_get_pixbuf(il);
91
92         g_object_ref(pixbuf);
93         pw->print_pixbuf_queue = g_list_append(pw->print_pixbuf_queue, pixbuf);
94
95         image_loader_free(pw->job_loader);
96         pw->job_loader = nullptr;
97
98         pw->job_page++;
99
100         if (!print_job_render_image(pw))
101                 {
102                 pw->job_render_finished = TRUE;
103                 }
104 }
105
106 static gboolean print_job_render_image(PrintWindow *pw)
107 {
108         FileData *fd = nullptr;
109
110         fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, pw->job_page));
111         if (!fd) return FALSE;
112
113         image_loader_free(pw->job_loader);
114         pw->job_loader = nullptr;
115
116         pw->job_loader = image_loader_new(fd);
117         g_signal_connect(G_OBJECT(pw->job_loader), "done",
118                                                 (GCallback)print_job_render_image_loader_done, pw);
119
120         if (!image_loader_start(pw->job_loader))
121                 {
122                 image_loader_free(pw->job_loader);
123                 pw->job_loader= nullptr;
124                 }
125
126         return TRUE;
127 }
128 #pragma GCC diagnostic push
129 #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
130 static void font_activated_cb(GtkFontChooser *widget, gchar *fontname, gpointer option)
131 {
132         option = g_strdup(fontname);
133
134         g_free(fontname);
135
136         gq_gtk_widget_destroy(GTK_WIDGET(widget));
137 }
138 #pragma GCC diagnostic pop
139
140 static void font_response_cb(GtkDialog *dialog, int response_id, gpointer option)
141 {
142         gchar *font;
143
144         if (response_id == GTK_RESPONSE_OK)
145                 {
146                 font = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
147                 g_free(option);
148                 option = g_strdup(font);
149                 g_free(font);
150                 }
151
152         gq_gtk_widget_destroy(GTK_WIDGET(dialog));
153 }
154
155 static void print_set_font_cb(GtkWidget *widget, gpointer data)
156 {
157         gpointer option;
158         GtkWidget *dialog;
159
160         if (g_strcmp0(static_cast<const gchar *>(data), "Image text font") == 0)
161                 {
162                 option = options->printer.image_font;
163                 }
164         else
165                 {
166                 option = options->printer.page_font;
167                 }
168
169         dialog = gtk_font_chooser_dialog_new(static_cast<const gchar *>(data), GTK_WINDOW(gtk_widget_get_toplevel(widget)));
170         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
171         gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dialog), static_cast<const gchar *>(option));
172
173         g_signal_connect(dialog, "font-activated", G_CALLBACK(font_activated_cb), option);
174         g_signal_connect(dialog, "response", G_CALLBACK(font_response_cb), option);
175
176         gtk_widget_show(dialog);
177 }
178
179 static gint set_toggle(GSList *list, TextPosition pos)
180 {
181         GtkToggleButton *current_sel;
182         GtkToggleButton *new_sel;
183         gint new_pos = - 1;
184
185         current_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, pos)->data);
186         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(current_sel)))
187                 {
188                 new_pos = (pos - 1);
189                 if (new_pos < 0)
190                         {
191                         new_pos = HEADER_1;
192                         }
193                 new_sel = static_cast<GtkToggleButton *>(g_slist_nth(list, new_pos)->data);
194                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(new_sel), TRUE);
195                 }
196         return new_pos;
197 }
198
199 static void image_text_position_h1_cb(GtkWidget *widget, gpointer data)
200 {
201         auto pw = static_cast<PrintWindow *>(data);
202         gint new_set;
203
204         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
205                 {
206                 new_set = set_toggle(pw->page_group, HEADER_1);
207                 if (new_set >= 0)
208                         {
209                         options->printer.page_text_position = new_set;
210                         }
211                 options->printer.image_text_position = HEADER_1;
212                 }
213 }
214
215 static void image_text_position_h2_cb(GtkWidget *widget, gpointer data)
216 {
217         auto pw = static_cast<PrintWindow *>(data);
218         gint new_set;
219
220         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
221                 {
222                 new_set = set_toggle(pw->page_group, HEADER_2);
223                 if (new_set >= 0)
224                         {
225                         options->printer.page_text_position = new_set;
226                         }
227                 options->printer.image_text_position = HEADER_2;
228                 }
229 }
230
231 static void image_text_position_f1_cb(GtkWidget *widget, gpointer data)
232 {
233         auto pw = static_cast<PrintWindow *>(data);
234         gint new_set;
235
236         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
237                 {
238                 new_set = set_toggle(pw->page_group, FOOTER_1);
239                 if (new_set >= 0)
240                         {
241                         options->printer.page_text_position = new_set;
242                         }
243                 options->printer.image_text_position = FOOTER_1;
244                 }
245 }
246
247 static void image_text_position_f2_cb(GtkWidget *widget, gpointer data)
248 {
249         auto pw = static_cast<PrintWindow *>(data);
250         gint new_set;
251
252         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
253                 {
254                 new_set = set_toggle(pw->page_group, FOOTER_2);
255                 if (new_set >= 0)
256                         {
257                         options->printer.page_text_position = new_set;
258                         }
259                 options->printer.image_text_position = FOOTER_2;
260                 }
261 }
262
263 static void page_text_position_h1_cb(GtkWidget *widget, gpointer data)
264 {
265         auto pw = static_cast<PrintWindow *>(data);
266         gint new_set;
267
268         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
269                 {
270                 new_set = set_toggle(pw->image_group, HEADER_1);
271                 if (new_set >= 0)
272                         {
273                         options->printer.image_text_position = new_set;
274                         }
275                 options->printer.page_text_position = HEADER_1;
276                 }
277 }
278
279 static void page_text_position_h2_cb(GtkWidget *widget, gpointer data)
280 {
281         auto pw = static_cast<PrintWindow *>(data);
282         gint new_set;
283
284         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
285                 {
286                 new_set = set_toggle(pw->image_group, HEADER_2);
287                 if (new_set >= 0)
288                         {
289                         options->printer.image_text_position = new_set;
290                         }
291                 options->printer.page_text_position = HEADER_2;
292                 }
293 }
294
295 static void page_text_position_f1_cb(GtkWidget *widget, gpointer data)
296 {
297         auto pw = static_cast<PrintWindow *>(data);
298         gint new_set;
299
300         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
301                 {
302                 new_set = set_toggle(pw->image_group, FOOTER_1);
303                 if (new_set >= 0)
304                         {
305                         options->printer.image_text_position = new_set;
306                         }
307                 options->printer.page_text_position = FOOTER_1;
308                 }
309 }
310
311 static void page_text_position_f2_cb(GtkWidget *widget, gpointer data)
312 {
313         auto pw = static_cast<PrintWindow *>(data);
314         gint new_set;
315
316         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
317                 {
318                 new_set = set_toggle(pw->image_group, FOOTER_2);
319                 if (new_set >= 0)
320                         {
321                         options->printer.image_text_position = new_set;
322                         }
323                 options->printer.page_text_position = FOOTER_2;
324                 }
325 }
326
327 static void set_print_image_text_string(gchar **template_string, const gchar *value)
328 {
329         g_assert(template_string);
330
331         g_free(*template_string);
332         *template_string = g_strdup(value);
333 }
334
335 static void image_text_template_view_changed_cb(GtkWidget *, gpointer data)
336 {
337         GtkWidget *pTextView;
338         GtkTextBuffer *pTextBuffer;
339         GtkTextIter iStart;
340         GtkTextIter iEnd;
341
342         pTextView = GTK_WIDGET(data);
343
344         pTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pTextView));
345         gtk_text_buffer_get_start_iter(pTextBuffer, &iStart);
346         gtk_text_buffer_get_end_iter(pTextBuffer, &iEnd);
347
348         set_print_image_text_string(&options->printer.template_string,
349                                           gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE));
350 }
351
352 enum {
353         PRE_FORMATTED_COLUMNS = 4
354 };
355 static void print_text_menu(GtkWidget *box, PrintWindow *pw)
356 {
357         GtkWidget *group;
358         GtkWidget *hbox;
359         GtkWidget *button;
360         GtkWidget *button1;
361         GtkWidget *button2;
362         GtkWidget *image_text_button;
363         GtkWidget *page_text_button;
364         GtkWidget *subgroup;
365         GtkWidget *page_text_view;
366         GtkWidget *image_text_template_view;
367         GtkWidget *scrolled;
368         GtkWidget *scrolled_pre_formatted;
369         GtkTextBuffer *buffer;
370
371         group = pref_group_new(box, FALSE, _("Image text"), GTK_ORIENTATION_VERTICAL);
372
373         image_text_button = pref_checkbox_new_int(group, _("Show image text"),
374                                                                                 options->printer.show_image_text, &options->printer.show_image_text);
375
376         subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
377
378         pref_checkbox_link_sensitivity(image_text_button, subgroup);
379
380         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
381         gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
382
383         /* order is important */
384         button1 = pref_radiobutton_new(hbox, nullptr,  "Header 1",
385                                                         options->printer.image_text_position == HEADER_1,
386                                                         G_CALLBACK(image_text_position_h1_cb), pw);
387         button1 = pref_radiobutton_new(hbox, button1,  "Header 2",
388                                                         options->printer.image_text_position == HEADER_2,
389                                                         G_CALLBACK(image_text_position_h2_cb), pw);
390         button1 = pref_radiobutton_new(hbox, button1, "Footer 1",
391                                                         options->printer.image_text_position == FOOTER_1,
392                                                         G_CALLBACK(image_text_position_f1_cb), pw);
393         button1 = pref_radiobutton_new(hbox, button1, "Footer 2",
394                                                         options->printer.image_text_position == FOOTER_2,
395                                                         G_CALLBACK(image_text_position_f2_cb), pw);
396         gtk_widget_show(hbox);
397         pw->image_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1)));
398
399         image_text_template_view = gtk_text_view_new();
400
401         scrolled_pre_formatted = osd_new(PRE_FORMATTED_COLUMNS, image_text_template_view);
402         gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled_pre_formatted, FALSE, FALSE, 0);
403         gtk_widget_show(scrolled_pre_formatted);
404         gtk_widget_show(subgroup);
405
406         gtk_widget_set_tooltip_markup(image_text_template_view,
407                                         _("Extensive formatting options are shown in the Help file"));
408
409         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
410         gtk_widget_set_size_request(scrolled, 200, 50);
411         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
412         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
413                                                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
414         gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
415         gtk_widget_show(scrolled);
416
417         gq_gtk_container_add(GTK_WIDGET(scrolled), image_text_template_view);
418         gtk_widget_show(image_text_template_view);
419
420         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(image_text_template_view));
421         if (options->printer.template_string) gtk_text_buffer_set_text(buffer, options->printer.template_string, -1);
422         g_signal_connect(G_OBJECT(buffer), "changed",
423                          G_CALLBACK(image_text_template_view_changed_cb), image_text_template_view);
424
425         hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
426
427         button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
428                                  G_CALLBACK(print_set_font_cb), const_cast<char *>("Image text font"));
429
430         gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
431         gtk_widget_show(button);
432
433         pref_spacer(group, PREF_PAD_GAP);
434
435         group = pref_group_new(box, FALSE, _("Page text"), GTK_ORIENTATION_VERTICAL);
436
437         page_text_button = pref_checkbox_new_int(group, _("Show page text"),
438                                           options->printer.show_page_text, &options->printer.show_page_text);
439
440         subgroup = pref_box_new(group, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
441         pref_checkbox_link_sensitivity(page_text_button, subgroup);
442
443         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
444         gq_gtk_box_pack_start(GTK_BOX(subgroup), hbox, FALSE, FALSE, 0);
445
446         /* order is important */
447         button2 = pref_radiobutton_new(hbox, nullptr, "Header 1",
448                                                         options->printer.page_text_position == HEADER_1,
449                                                         G_CALLBACK(page_text_position_h1_cb), pw);
450         button2 = pref_radiobutton_new(hbox, button2,  "Header 2",
451                                                         options->printer.page_text_position == HEADER_2,
452                                                         G_CALLBACK(page_text_position_h2_cb), pw);
453         button2 = pref_radiobutton_new(hbox, button2, "Footer 1",
454                                                         options->printer.page_text_position == FOOTER_1,
455                                                         G_CALLBACK(page_text_position_f1_cb), pw);
456         button2 = pref_radiobutton_new(hbox, button2, "Footer 2",
457                                                         options->printer.page_text_position == FOOTER_2,
458                                                         G_CALLBACK(page_text_position_f2_cb), pw);
459         gtk_widget_show(hbox);
460         pw->page_group = (gtk_radio_button_get_group(GTK_RADIO_BUTTON(button2)));
461
462         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
463         gtk_widget_set_size_request(scrolled, 50, 50);
464         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
465         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
466                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
467         gq_gtk_box_pack_start(GTK_BOX(subgroup), scrolled, TRUE, TRUE, 5);
468         gtk_widget_show(scrolled);
469
470         page_text_view = gtk_text_view_new();
471         pw->page_text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page_text_view ));
472         gtk_text_buffer_set_text(GTK_TEXT_BUFFER(pw->page_text), options->printer.page_text, -1);
473         g_object_ref(pw->page_text);
474
475         gtk_widget_set_tooltip_markup(page_text_view, (_("Text shown on each page of a single or multi-page print job")));
476         gq_gtk_container_add(GTK_WIDGET(scrolled), page_text_view);
477         gtk_widget_show(page_text_view);
478
479         hbox = pref_box_new(subgroup, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
480
481         button = pref_button_new(nullptr, GQ_ICON_SELECT_FONT, _("Font"),
482                                  G_CALLBACK(print_set_font_cb), const_cast<char *>("Page text font"));
483
484         gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
485         gtk_widget_show(button);
486 }
487
488 static gboolean paginate_cb(GtkPrintOperation *, GtkPrintContext *, gpointer data)
489 {
490         auto pw = static_cast<PrintWindow *>(data);
491
492         if (pw->job_render_finished)
493                 {
494                 return TRUE;
495                 }
496
497         return FALSE;
498 }
499
500 gchar *form_image_text(const gchar *template_string, FileData *fd, PrintWindow *pw, gint page_nr, gint total)
501 {
502         const gchar *name;
503         gchar *text = nullptr;
504         GHashTable *vars;
505         gchar *window_title;
506         gchar *delimiter;
507         gchar *collection_name;
508
509         if (!fd) return nullptr;
510
511         name = fd->name;
512
513         vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
514
515         window_title = g_strdup(gtk_window_get_title(GTK_WINDOW(pw->parent)));
516         delimiter = g_strstr_len(window_title, -1, " - Collection - ");
517         if (delimiter)
518                 {
519                 collection_name = g_strndup(window_title, delimiter - window_title);
520                 }
521         else
522                 {
523                 collection_name = nullptr;
524                 }
525         g_free(window_title);
526
527         if (collection_name)
528                 {
529                 osd_template_insert(vars, "collection", collection_name, OSDT_NONE);
530                 }
531
532         osd_template_insert(vars, "number", g_strdup_printf("%d", page_nr + 1), OSDT_NO_DUP);
533         osd_template_insert(vars, "total", g_strdup_printf("%d", total), OSDT_NO_DUP);
534         osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
535         osd_template_insert(vars, "date", fd ? (const_cast<gchar *>(text_from_time(fd->date))) : "", OSDT_NONE);
536         osd_template_insert(vars, "size", fd ? (text_from_size_abrev(fd->size)) : g_strdup(""), OSDT_FREE);
537
538         if (fd->pixbuf)
539                 {
540                 gint w;
541                 gint h;
542                 w = gdk_pixbuf_get_width(fd->pixbuf);
543                 h = gdk_pixbuf_get_height(fd->pixbuf);
544
545                 osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
546                 osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
547                 osd_template_insert(vars, "res", g_strdup_printf("%d Ã— %d", w, h), OSDT_FREE);
548                 }
549         else
550                 {
551                 osd_template_insert(vars, "width", nullptr, OSDT_NONE);
552                 osd_template_insert(vars, "height", nullptr, OSDT_NONE);
553                 osd_template_insert(vars, "res", nullptr, OSDT_NONE);
554                 }
555
556         text = image_osd_mkinfo(template_string, fd, vars);
557         g_hash_table_destroy(vars);
558
559         g_free(collection_name);
560
561         return text;
562 }
563
564 static void draw_page(GtkPrintOperation *, GtkPrintContext *context, gint page_nr, gpointer data)
565 {
566         auto pw = static_cast<PrintWindow *>(data);
567         FileData *fd;
568         cairo_t *cr;
569         gdouble context_width;
570         gdouble context_height;
571         gdouble pixbuf_image_width;
572         gdouble pixbuf_image_height;
573         gdouble width_offset;
574         gdouble height_offset;
575         GdkPixbuf *pixbuf;
576         GdkPixbuf *rotated = nullptr;
577         PangoLayout *layout_image = nullptr;
578         PangoLayout *layout_page = nullptr;
579         PangoFontDescription *desc;
580         GString *image_text = g_string_new(nullptr);
581         GString *page_text = g_string_new(nullptr);
582         PangoRectangle ink_rect;
583         PangoRectangle logical_rect;
584         gdouble w;
585         gdouble h;
586         gdouble scale;
587         gdouble image_text_width;
588         gdouble image_text_height;
589         gdouble page_text_width;
590         gdouble page_text_height;
591         gint image_y;
592         gint incr_y;
593         gdouble pango_height;
594         gdouble pango_image_height;
595         gdouble pango_page_height;
596         GtkTextIter start;
597         GtkTextIter end;
598         gchar *tmp;
599         gint total;
600
601         fd = static_cast<FileData *>(g_list_nth_data(pw->source_selection, page_nr));
602         total = g_list_length(pw->source_selection);
603
604         pixbuf = static_cast<GdkPixbuf *>(g_list_nth_data(pw->print_pixbuf_queue, page_nr));
605         if (fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
606                 {
607                 rotated = pixbuf_apply_orientation(pixbuf, fd->exif_orientation);
608                 pixbuf = rotated;
609                 }
610
611         pixbuf_image_width = gdk_pixbuf_get_width(pixbuf);
612         pixbuf_image_height = gdk_pixbuf_get_height(pixbuf);
613
614         if (options->printer.show_image_text)
615                 {
616                 image_text = g_string_append(image_text, form_image_text(options->printer.template_string, fd, pw, page_nr, total));
617                 }
618
619         if (options->printer.show_page_text)
620                 {
621                 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
622
623                 tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
624                 page_text = g_string_append(page_text, tmp);
625
626                 g_free(tmp);
627                 }
628
629         cr = gtk_print_context_get_cairo_context(context);
630         context_width = gtk_print_context_get_width(context);
631         context_height = gtk_print_context_get_height(context);
632
633         pango_image_height = 0;
634         pango_page_height = 0;
635         image_text_width = 0;
636         page_text_width = 0;
637
638         if (image_text->len > 0)
639                 {
640                 layout_image = pango_cairo_create_layout(cr);
641
642                 pango_layout_set_text(layout_image, image_text->str, -1);
643                 desc = pango_font_description_from_string(options->printer.image_font);
644                 pango_layout_set_font_description(layout_image, desc);
645
646                 pango_layout_get_extents(layout_image, &ink_rect, &logical_rect);
647                 image_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
648                 image_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
649
650                 pango_layout_set_alignment(layout_image, PANGO_ALIGN_CENTER);
651                 pango_layout_set_text(layout_image, image_text->str, -1);
652
653                 pango_image_height = image_text_height + PRINT_TEXT_PADDING * 2;
654
655                 pango_font_description_free(desc);
656                 }
657
658         if (page_text->len > 0)
659                 {
660                 layout_page = pango_cairo_create_layout(cr);
661
662                 pango_layout_set_text(layout_page, page_text->str, -1);
663                 desc = pango_font_description_from_string(options->printer.page_font);
664                 pango_layout_set_font_description(layout_page, desc);
665
666                 pango_layout_get_extents(layout_page, &ink_rect, &logical_rect);
667                 page_text_width = (static_cast<gdouble>(logical_rect.width) / PANGO_SCALE) ;
668                 page_text_height = (static_cast<gdouble>(logical_rect.height) / PANGO_SCALE);
669
670                 pango_layout_set_alignment(layout_page, PANGO_ALIGN_CENTER);
671                 pango_layout_set_text(layout_page, page_text->str, -1);
672
673                 pango_page_height = page_text_height + PRINT_TEXT_PADDING * 2;
674
675                 pango_font_description_free(desc);
676                 }
677
678         pango_height = pango_image_height + pango_page_height;
679
680         if ((context_width / pixbuf_image_width) < ((context_height - pango_height) / pixbuf_image_height))
681                 {
682                 w = context_width;
683                 scale = context_width / pixbuf_image_width;
684                 h = pixbuf_image_height * scale;
685                 height_offset = (context_height - (h + pango_height)) / 2;
686                 width_offset = 0;
687                 }
688         else
689                 {
690                 h = context_height - pango_height ;
691                 scale = (context_height - pango_height) / pixbuf_image_height;
692                 w = pixbuf_image_width * scale;
693                 height_offset = 0;
694                 width_offset = (context_width - (pixbuf_image_width * scale)) / 2;
695                 }
696
697         incr_y = height_offset;
698
699         if (options->printer.page_text_position == HEADER_1 && page_text->len > 0)
700                 {
701                 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
702                 pango_cairo_show_layout(cr, layout_page);
703
704                 incr_y = incr_y + pango_page_height;
705                 }
706
707         if (options->printer.image_text_position == HEADER_1 && image_text->len > 0)
708                 {
709                 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
710                 pango_cairo_show_layout(cr, layout_image);
711
712                 incr_y = incr_y + pango_image_height;
713                 }
714
715         if (options->printer.page_text_position == HEADER_2 && page_text->len > 0)
716                 {
717                 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
718                 pango_cairo_show_layout(cr, layout_page);
719
720                 incr_y = incr_y + pango_page_height;
721                 }
722
723         if (options->printer.image_text_position == HEADER_2 && image_text->len > 0)
724                 {
725                 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
726                 pango_cairo_show_layout(cr, layout_image);
727
728                 incr_y = incr_y + pango_image_height;
729                 }
730
731         image_y = incr_y;
732         incr_y = incr_y + h;
733
734         if (options->printer.page_text_position == FOOTER_1 && page_text->len > 0)
735                 {
736                 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y + PRINT_TEXT_PADDING);
737                 pango_cairo_show_layout(cr, layout_page);
738
739                 incr_y = incr_y + pango_page_height;
740                 }
741
742         if (options->printer.image_text_position == FOOTER_1 && image_text->len > 0)
743                 {
744                 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
745                 pango_cairo_show_layout(cr, layout_image);
746
747                 incr_y = incr_y + pango_image_height;
748                 }
749
750         if (options->printer.page_text_position == FOOTER_2 && page_text->len > 0)
751                 {
752                 cairo_move_to(cr, (w / 2) - (page_text_width / 2) + width_offset, incr_y);
753                 pango_cairo_show_layout(cr, layout_page);
754
755                 incr_y = incr_y + pango_page_height;
756                 }
757
758         if (options->printer.image_text_position == FOOTER_2 && image_text->len > 0)
759                 {
760                 cairo_move_to(cr, (w / 2) - (image_text_width / 2) + width_offset, incr_y);
761                 pango_cairo_show_layout(cr, layout_image);
762                 }
763
764         cairo_scale(cr, scale, scale);
765
766         cairo_rectangle(cr,  width_offset * scale , image_y, pixbuf_image_width / scale, pixbuf_image_height / scale);
767         gdk_cairo_set_source_pixbuf(cr, pixbuf, width_offset / scale, image_y / scale);
768         cairo_fill(cr);
769
770         if (image_text->len > 0)
771                 {
772                 g_object_unref(layout_image);
773                 g_string_free(image_text, TRUE);
774                 }
775         if (page_text->len > 0)
776                 {
777                 g_object_unref(layout_page);
778                 g_string_free(page_text, TRUE);
779                 }
780
781         if (rotated) g_object_unref(rotated);
782 }
783
784 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *, gpointer user_data)
785 {
786         auto pw = static_cast<PrintWindow *>(user_data);
787         gint page_count;
788
789         page_count = print_layout_page_count(pw);
790         gtk_print_operation_set_n_pages (operation, page_count);
791
792         print_job_render_image(pw);
793 }
794
795
796 GObject *option_tab_cb(GtkPrintOperation *, gpointer user_data)
797 {
798         auto pw = static_cast<PrintWindow *>(user_data);
799
800         return G_OBJECT(pw->vbox);
801 }
802
803 static void print_pref_store(PrintWindow *pw)
804 {
805         gchar *tmp;
806         GtkTextIter start;
807         GtkTextIter end;
808
809         gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pw->page_text), &start, &end);
810         tmp = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pw->page_text), &start, &end, FALSE);
811         g_free(options->printer.page_text);
812         options->printer.page_text = g_strdup(tmp);
813         g_free(tmp);
814 }
815
816 static void end_print_cb(GtkPrintOperation *operation, GtkPrintContext *, gpointer data)
817 {
818         auto pw = static_cast<PrintWindow *>(data);
819         GList *work;
820         GdkPixbuf *pixbuf;
821         gchar *path;
822         GtkPrintSettings *print_settings;
823         GtkPageSetup *page_setup;
824         GError *error = nullptr;
825
826         print_settings = gtk_print_operation_get_print_settings(operation);
827         path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
828
829         gtk_print_settings_to_file(print_settings, path, &error);
830         if (error)
831                 {
832                 log_printf("Error: Print settings save failed:\n%s", error->message);
833                 g_error_free(error);
834                 error = nullptr;
835                 }
836         g_free(path);
837         g_object_unref(print_settings);
838
839         page_setup = gtk_print_operation_get_default_page_setup(operation);
840         path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
841
842         gtk_page_setup_to_file(page_setup, path, &error);
843         if (error)
844                 {
845                 log_printf("Error: Print page setup save failed:\n%s", error->message);
846                 g_error_free(error);
847                 error = nullptr;
848                 }
849         g_free(path);
850         g_object_unref(page_setup);
851
852         print_pref_store(pw);
853
854         work = pw->print_pixbuf_queue;
855         while (work)
856                 {
857                 pixbuf = static_cast<GdkPixbuf *>(work->data);
858                 if (pixbuf)
859                         {
860                         g_object_unref(pixbuf);
861                         }
862                 work = work->next;
863                 }
864         g_list_free(pw->print_pixbuf_queue);
865         g_object_unref(pw->page_text);
866         g_free(pw);
867 }
868
869 static void print_response_cb(GtkDialog *dialog, gint, gpointer)
870 {
871         gq_gtk_widget_destroy(GTK_WIDGET(dialog));
872 }
873
874 void print_window_new(FileData *, GList *selection, GList *, GtkWidget *parent)
875 {
876         GtkWidget *vbox;
877         GtkPrintOperation *operation;
878         GtkPageSetup *page_setup;
879         gchar *uri;
880         const gchar *dir;
881         GError *error = nullptr;
882         gchar *path;
883         GtkPrintSettings *settings;
884
885         auto pw = g_new0(PrintWindow, 1);
886
887         pw->source_selection = file_data_process_groups_in_selection(selection, FALSE, nullptr);
888
889         if (print_layout_page_count(pw) == 0)
890                 {
891                 return;
892                 }
893
894         pw->parent = parent;
895
896         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
897         gtk_container_set_border_width(GTK_CONTAINER(vbox), PREF_PAD_BORDER);
898         gtk_widget_show(vbox);
899
900         print_text_menu(vbox, pw);
901         pw->vbox = vbox;
902
903         pw->print_pixbuf_queue = nullptr;
904         pw->job_render_finished = FALSE;
905         pw->job_page = 0;
906
907         operation = gtk_print_operation_new();
908         settings = gtk_print_settings_new();
909
910         gtk_print_operation_set_custom_tab_label(operation, "Options");
911         gtk_print_operation_set_use_full_page(operation, TRUE);
912         gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
913         gtk_print_operation_set_embed_page_setup(operation, TRUE);
914         gtk_print_operation_set_allow_async (operation, TRUE);
915         dir = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
916         if (dir == nullptr)
917                 {
918                 dir = g_get_home_dir();
919                 }
920
921         uri = g_build_filename("file:/", dir, "geeqie-file.pdf", NULL);
922         gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
923         g_free(uri);
924
925         path = g_build_filename(get_rc_dir(), PRINT_SETTINGS, NULL);
926         gtk_print_settings_load_file(settings, path, &error);
927         if (error)
928                 {
929                 log_printf("Error: Printer settings load failed:\n%s", error->message);
930                 g_error_free(error);
931                 error = nullptr;
932                 }
933         gtk_print_operation_set_print_settings(operation, settings);
934         g_free(path);
935
936         page_setup = gtk_page_setup_new();
937         path = g_build_filename(get_rc_dir(), PAGE_SETUP, NULL);
938         gtk_page_setup_load_file(page_setup, path, &error);
939         if (error)
940                 {
941                 log_printf("Error: Print page setup load failed:\n%s", error->message);
942                 g_error_free(error);
943                 error = nullptr;
944                 }
945         gtk_print_operation_set_default_page_setup(operation, page_setup);
946         g_free(path);
947
948         g_signal_connect (G_OBJECT (operation), "begin-print",
949                                         G_CALLBACK (begin_print), pw);
950         g_signal_connect (G_OBJECT (operation), "draw-page",
951                                         G_CALLBACK (draw_page), pw);
952         g_signal_connect (G_OBJECT (operation), "end-print",
953                                         G_CALLBACK (end_print_cb), pw);
954         g_signal_connect (G_OBJECT (operation), "create-custom-widget",
955                                         G_CALLBACK (option_tab_cb), pw);
956         g_signal_connect (G_OBJECT (operation), "paginate",
957                                         G_CALLBACK (paginate_cb), pw);
958
959         gtk_print_operation_set_n_pages(operation, print_layout_page_count(pw));
960
961         gtk_print_operation_run(operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
962                                                                                                 GTK_WINDOW (parent), &error);
963
964         if (error)
965                 {
966                 GtkWidget *dialog;
967
968                 dialog = gtk_message_dialog_new(GTK_WINDOW (parent),
969                                                                 GTK_DIALOG_DESTROY_WITH_PARENT,
970                                                                 GTK_MESSAGE_ERROR,
971                                                                 GTK_BUTTONS_CLOSE,
972                                                                 "%s", error->message);
973                 g_error_free (error);
974
975                 g_signal_connect(dialog, "response", G_CALLBACK(print_response_cb), NULL);
976
977                 gtk_widget_show (dialog);
978                 }
979
980         g_object_unref(page_setup);
981         g_object_unref(settings);
982 }
983 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */