4d0bb06a8b03a4872dafff1c50eabbb2efbe78df
[claws.git] / src / gedit-print.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gedit-print.c
4  * This file is part of gedit
5  *
6  * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi
7  * Copyright (C) 2002  Paolo Maggi  
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, 
22  * Boston, MA 02111-1307, USA. 
23  */
24  
25 /*
26  * Modified by the gedit Team, 1998-2002. See the AUTHORS file for a 
27  * list of people on the gedit Team.  
28  * See the ChangeLog files for a list of changes. 
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #ifdef USE_GNOMEPRINT
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <string.h>     /* For strlen */
42
43 #include <glib/gi18n.h>
44 #include <gtk/gtkframe.h>
45 #include <gtk/gtkhbox.h>
46 #include <gtk/gtkvbox.h>
47 #include <gtk/gtklabel.h>
48 #include <gtk/gtkmain.h>
49 #include <gtk/gtkprogressbar.h>
50 #include <gtk/gtkimage.h>
51 #include <gtk/gtkwindow.h>
52 #include <libgnomeprintui/gnome-print-dialog.h>
53 #include <libgnomeprintui/gnome-print-job-preview.h>
54
55 #include "gedit-print.h"
56 #include "gtk/gtksourceprintjob.h"
57
58 #ifdef DEBUG
59 #  define DEBUG_PRINT "DEBUG_PRINT: %s"
60 #  define gedit_debug(x, y) g_warning(x, y)
61 #else
62 #  define gedit_debug(x, y) do {} while (FALSE);
63 #endif
64
65 enum
66 {
67         PREVIEW_NO,
68         PREVIEW,
69         PREVIEW_FROM_DIALOG
70 };
71
72 typedef struct _GeditPrintJobInfo       GeditPrintJobInfo;
73
74 struct _GeditPrintJobInfo 
75 {
76         GtkTextBuffer     *doc;
77         
78         GtkSourcePrintJob *pjob;
79                 
80         gint               preview;
81
82         gint               range_type;
83
84         gint               first_line_to_print;
85         gint               last_line_to_print;
86
87         /* Popup dialog */
88         GtkWidget         *dialog;
89         GtkWidget         *label;
90         GtkWidget         *progressbar;
91
92         GtkWindow         *parent;
93 };
94
95 static GeditPrintJobInfo* gedit_print_job_info_new      (GtkTextView       *view);
96 static void gedit_print_job_info_destroy                (GeditPrintJobInfo *pji, 
97                                                          gboolean           save_config);
98 static void gedit_print_real                            (GeditPrintJobInfo *pji, 
99                                                          GtkTextIter       *start, 
100                                                          GtkTextIter       *end, 
101                                                          GtkWindow         *parent);
102 static void gedit_print_preview_real                    (GeditPrintJobInfo *pji, 
103                                                          GtkTextIter       *start, 
104                                                          GtkTextIter       *end, 
105                                                          GtkWindow         *parent);
106
107
108 static void
109 gedit_print_job_info_destroy (GeditPrintJobInfo *pji, gboolean save_config)
110 {
111         gedit_debug (DEBUG_PRINT, "");
112
113         g_return_if_fail (pji != NULL);
114
115         if (pji->pjob != NULL)
116                 g_object_unref (pji->pjob);
117
118         g_free (pji);
119 }
120
121 static GtkWidget *
122 get_print_dialog (GeditPrintJobInfo *pji, GtkWindow *parent)
123 {
124         GtkWidget *dialog;
125         gint selection_flag;
126         gint lines;
127         GnomePrintConfig *config;
128
129         gedit_debug (DEBUG_PRINT, "");
130
131         g_return_val_if_fail (pji != NULL, NULL);
132         
133         if (!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (pji->doc), NULL, NULL))
134                 selection_flag = GNOME_PRINT_RANGE_SELECTION_UNSENSITIVE;
135         else
136                 selection_flag = GNOME_PRINT_RANGE_SELECTION;
137         
138         g_return_val_if_fail(pji->pjob != NULL, NULL);
139         config = gtk_source_print_job_get_config (pji->pjob);
140         
141         dialog = g_object_new (GNOME_TYPE_PRINT_DIALOG, "print_config", config, NULL);
142         
143         gnome_print_dialog_construct (GNOME_PRINT_DIALOG (dialog), 
144                                       _("Print"),
145                                       GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
146
147         lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (pji->doc));
148
149         gnome_print_dialog_construct_range_any ( GNOME_PRINT_DIALOG (dialog),
150                                                  (GNOME_PRINT_RANGE_ALL | selection_flag), 
151                                                  NULL, NULL, NULL);
152
153         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
154
155         gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); 
156         gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
157
158         return dialog;
159 }
160
161 static void
162 gedit_print_dialog_response (GtkWidget *dialog, int response, GeditPrintJobInfo *pji)
163 {
164         GtkTextIter start, end;
165
166         pji->range_type = gnome_print_dialog_get_range (GNOME_PRINT_DIALOG (dialog));
167         gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (pji->doc), &start, &end);
168
169         switch (pji->range_type)
170         {
171         case GNOME_PRINT_RANGE_ALL:
172                 break;
173
174         case GNOME_PRINT_RANGE_SELECTION:
175                 gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (pji->doc),
176                                                       &start, &end);
177                 break;
178
179         default:
180                 g_return_if_reached ();
181         }
182
183         switch (response)
184         {
185         case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
186                 gedit_debug (DEBUG_PRINT, "Print button pressed.");
187                 pji->preview = PREVIEW_NO;
188                 gedit_print_real (pji, &start, &end, 
189                                   gtk_window_get_transient_for (GTK_WINDOW (dialog)));
190                 gtk_widget_destroy (dialog);
191                 break;
192
193         case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
194                 gedit_debug (DEBUG_PRINT, "Preview button pressed.");
195                 pji->preview = PREVIEW_FROM_DIALOG;
196                 gedit_print_preview_real (pji, &start, &end, GTK_WINDOW (dialog));
197                 break;
198
199         default:
200                 gtk_widget_destroy (dialog);
201                 gedit_print_job_info_destroy (pji, FALSE);
202         }
203
204
205 static void
206 show_printing_dialog (GeditPrintJobInfo *pji, GtkWindow *parent)
207 {
208         GtkWidget *window;
209         GtkWidget *frame;
210         GtkWidget *hbox;
211         GtkWidget *image;
212         GtkWidget *vbox;
213         GtkWidget *label;
214         GtkWidget *progressbar;
215
216         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
217         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
218         gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
219         gtk_window_set_destroy_with_parent (GTK_WINDOW (window), TRUE);
220         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER_ON_PARENT);
221                 
222         gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
223         gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
224         gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);
225  
226         gtk_window_set_transient_for (GTK_WINDOW (window), parent);
227
228         frame = gtk_frame_new (NULL);
229         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
230         gtk_container_add (GTK_CONTAINER (window), frame);
231
232         hbox = gtk_hbox_new (FALSE, 12);
233         gtk_container_add (GTK_CONTAINER (frame), hbox);
234         gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
235
236         image = gtk_image_new_from_stock ("gtk-print", GTK_ICON_SIZE_DIALOG);
237         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
238
239         vbox = gtk_vbox_new (FALSE, 12);
240         gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
241
242         label = gtk_label_new (_("Preparing pages..."));
243         gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
244         gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
245         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
246
247         progressbar = gtk_progress_bar_new ();
248         gtk_box_pack_start (GTK_BOX (vbox), progressbar, FALSE, FALSE, 0);
249         
250         pji->dialog = window;
251         pji->label = label;
252         pji->progressbar = progressbar;
253         
254         gtk_widget_show_all (pji->dialog);
255
256         /* Update UI */
257         while (gtk_events_pending ())
258                 gtk_main_iteration ();
259 }
260
261 static void
262 page_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
263 {
264         gchar *str;
265         gint page_num = gtk_source_print_job_get_page (pji->pjob);
266         gint total = gtk_source_print_job_get_page_count (pji->pjob);
267
268         if (pji->preview != PREVIEW_NO)
269                 str = g_strdup_printf (_("Rendering page %d of %d..."), page_num, total);
270         else
271                 str = g_strdup_printf (_("Printing page %d of %d..."), page_num, total);
272
273         gtk_label_set_label (GTK_LABEL (pji->label), str);
274         g_free (str);
275
276         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pji->progressbar), 
277                                        1.0 * page_num / total);
278
279         /* Update UI */
280         while (gtk_events_pending ())
281                 gtk_main_iteration ();
282
283 }
284
285 static void
286 preview_finished_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
287 {
288         GnomePrintJob *gjob;
289         GtkWidget *preview = NULL;
290
291         gjob = gtk_source_print_job_get_print_job (job);
292
293         preview = gnome_print_job_preview_new (gjob, _("Print preview"));
294         if (pji->parent != NULL)
295         {
296                 gtk_window_set_transient_for (GTK_WINDOW (preview), pji->parent);
297                 gtk_window_set_modal (GTK_WINDOW (preview), TRUE);
298         }
299         
300         g_object_unref (gjob);
301
302         gtk_widget_destroy (pji->dialog);
303
304         if (pji->preview == PREVIEW)
305                 gedit_print_job_info_destroy (pji, FALSE);
306         else
307         {
308                 g_signal_handlers_disconnect_by_func (pji->pjob, (GCallback) page_cb, pji);
309                 g_signal_handlers_disconnect_by_func (pji->pjob, (GCallback) preview_finished_cb, pji);
310         }
311         
312         gtk_widget_show (preview);
313 }
314
315 static void
316 print_finished_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
317 {
318         GnomePrintJob *gjob;
319
320         gjob = gtk_source_print_job_get_print_job (job);
321
322         gnome_print_job_print (gjob);
323         
324         g_object_unref (gjob);
325
326         gtk_widget_destroy (pji->dialog);
327
328         gedit_print_job_info_destroy (pji, TRUE);
329 }
330
331 void 
332 gedit_print (GtkTextView *view)
333 {
334         GeditPrintJobInfo *pji;
335         GtkWidget *dialog;
336
337         gedit_debug (DEBUG_PRINT, "");
338
339         g_return_if_fail (view != NULL);
340
341         pji = gedit_print_job_info_new (view);
342         pji->preview = PREVIEW_NO;
343
344         dialog = get_print_dialog (pji, GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view))));
345         
346         g_signal_connect (dialog, "response",
347                           G_CALLBACK (gedit_print_dialog_response),
348                           pji);
349
350         gtk_widget_show (dialog);
351 }
352
353 static void 
354 gedit_print_preview_real (GeditPrintJobInfo *pji, 
355                           GtkTextIter       *start, 
356                           GtkTextIter       *end, 
357                           GtkWindow         *parent)
358 {
359         show_printing_dialog (pji, parent);
360
361         pji->parent = parent;
362
363         g_signal_connect (pji->pjob, "begin_page", (GCallback) page_cb, pji);
364         g_signal_connect (pji->pjob, "finished", (GCallback) preview_finished_cb, pji);
365
366         if (!gtk_source_print_job_print_range_async (pji->pjob, start, end))
367         {
368                 /* FIXME */
369                 g_warning ("Async print failed");
370                 gtk_widget_destroy (pji->dialog);
371         }
372 }
373
374 static void 
375 gedit_print_real (GeditPrintJobInfo *pji, 
376                   GtkTextIter       *start, 
377                   GtkTextIter       *end, 
378                   GtkWindow         *parent)
379 {
380         show_printing_dialog (pji, parent);
381
382         g_signal_connect (pji->pjob, "begin_page", (GCallback) page_cb, pji);
383         g_signal_connect (pji->pjob, "finished", (GCallback) print_finished_cb, pji);
384
385         if (!gtk_source_print_job_print_range_async (pji->pjob, start, end))
386         {
387                 /* FIXME */
388                 g_warning ("Async print failed");
389                 gtk_widget_destroy (pji->dialog);
390         }
391 }
392
393 void 
394 gedit_print_preview (GtkTextView *view)
395 {
396         GeditPrintJobInfo *pji;
397         GtkTextIter start, end;
398
399         gedit_debug (DEBUG_PRINT, "");
400                 
401         g_return_if_fail (view != NULL);
402
403         pji = gedit_print_job_info_new (view);
404
405         gtk_text_buffer_get_bounds (pji->doc, &start, &end);
406
407         pji->preview = PREVIEW;
408         gedit_print_preview_real (pji, &start, &end, 
409                                   GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view))));
410 }
411
412 static GeditPrintJobInfo *
413 gedit_print_job_info_new (GtkTextView* view)
414 {       
415         GtkSourcePrintJob *pjob;
416         GnomePrintConfig *config;
417         GeditPrintJobInfo *pji;
418         PangoContext *pango_context;
419         PangoFontDescription *font_desc;
420         GtkTextBuffer *buffer;
421         
422         gedit_debug (DEBUG_PRINT, "");
423         
424         g_return_val_if_fail (view != NULL, NULL);
425
426         buffer = gtk_text_view_get_buffer (view);
427         g_return_val_if_fail (buffer != NULL, NULL);
428
429         config = gnome_print_config_default ();
430         g_return_val_if_fail (config != NULL, NULL);
431
432         gnome_print_config_set_int (config, GNOME_PRINT_KEY_NUM_COPIES, 1);
433         gnome_print_config_set_boolean (config, GNOME_PRINT_KEY_COLLATE, FALSE);
434
435         pjob = gtk_source_print_job_new_with_buffer (config, buffer);
436         gnome_print_config_unref (config);
437
438         gtk_source_print_job_set_highlight (pjob, TRUE);
439         gtk_source_print_job_set_print_numbers (pjob, FALSE);
440         gtk_source_print_job_set_wrap_mode (pjob, gtk_text_view_get_wrap_mode (view));
441         gtk_source_print_job_set_tabs_width (pjob, 8);
442         
443         gtk_source_print_job_set_footer_format (pjob,
444                                                 _("Page %N of %Q"), 
445                                                 NULL, 
446                                                 _("%A %D, %I:%M %p"), 
447                                                 TRUE);
448
449         gtk_source_print_job_set_print_header (pjob, FALSE);
450         gtk_source_print_job_set_print_footer (pjob, TRUE);
451
452         pango_context = gtk_widget_get_pango_context (GTK_WIDGET (view));
453         font_desc = pango_context_get_font_description (pango_context);
454
455         gtk_source_print_job_set_font_desc (pjob, font_desc);
456
457         pji = g_new0 (GeditPrintJobInfo, 1);
458
459         pji->pjob = pjob;
460
461         pji->doc = buffer;
462         pji->preview = PREVIEW_NO;
463         pji->range_type = GNOME_PRINT_RANGE_ALL;
464
465         return pji;
466 }
467
468 #endif