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