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