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