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