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