2005-10-26 [colin] 1.9.15cvs111
[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 #include "mainwindow.h"
58 #include "prefs_common.h"
59
60 #ifdef DEBUG
61 #  define DEBUG_PRINT "DEBUG_PRINT: %s"
62 #  define gedit_debug(x, y) g_warning(x, y)
63 #else
64 #  define gedit_debug(x, y) do {} while (FALSE);
65 #endif
66
67 enum
68 {
69         PREVIEW_NO,
70         PREVIEW,
71         PREVIEW_FROM_DIALOG
72 };
73
74 typedef struct _GeditPrintJobInfo       GeditPrintJobInfo;
75
76 struct _GeditPrintJobInfo 
77 {
78         GtkTextBuffer     *doc;
79         
80         GtkSourcePrintJob *pjob;
81                 
82         gint               preview;
83
84         gint               range_type;
85
86         gint               first_line_to_print;
87         gint               last_line_to_print;
88
89         /* Popup dialog */
90         GtkWidget         *dialog;
91         GtkWidget         *label;
92         GtkWidget         *progressbar;
93
94         GtkWindow         *parent;
95 };
96
97 static GeditPrintJobInfo* gedit_print_job_info_new      (GtkTextView       *view);
98 static void gedit_print_job_info_destroy                (GeditPrintJobInfo *pji, 
99                                                          gboolean           save_config);
100 static void gedit_print_real                            (GeditPrintJobInfo *pji, 
101                                                          GtkTextIter       *start, 
102                                                          GtkTextIter       *end, 
103                                                          GtkWindow         *parent);
104 static void gedit_print_preview_real                    (GeditPrintJobInfo *pji, 
105                                                          GtkTextIter       *start, 
106                                                          GtkTextIter       *end, 
107                                                          GtkWindow         *parent);
108
109
110 static void
111 gedit_print_job_info_destroy (GeditPrintJobInfo *pji, gboolean save_config)
112 {
113         gedit_debug (DEBUG_PRINT, "");
114
115         g_return_if_fail (pji != NULL);
116
117         if (pji->pjob != NULL)
118                 g_object_unref (pji->pjob);
119
120         g_free (pji);
121 }
122
123 static GtkWidget *
124 get_print_dialog (GeditPrintJobInfo *pji, GtkWindow *parent)
125 {
126         GtkWidget *dialog;
127         gint selection_flag;
128         gint lines;
129         GnomePrintConfig *config;
130
131         gedit_debug (DEBUG_PRINT, "");
132
133         g_return_val_if_fail (pji != NULL, NULL);
134         
135         if (!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (pji->doc), NULL, NULL))
136                 selection_flag = GNOME_PRINT_RANGE_SELECTION_UNSENSITIVE;
137         else
138                 selection_flag = GNOME_PRINT_RANGE_SELECTION;
139         
140         g_return_val_if_fail(pji->pjob != NULL, NULL);
141         config = gtk_source_print_job_get_config (pji->pjob);
142         
143         dialog = g_object_new (GNOME_TYPE_PRINT_DIALOG, "print_config", config, NULL);
144         
145         gnome_print_dialog_construct (GNOME_PRINT_DIALOG (dialog), 
146                                       _("Print"),
147                                       GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
148
149         lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (pji->doc));
150
151         gnome_print_dialog_construct_range_any ( GNOME_PRINT_DIALOG (dialog),
152                                                  (GNOME_PRINT_RANGE_ALL | selection_flag), 
153                                                  NULL, NULL, NULL);
154
155         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
156
157         gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); 
158         gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
159
160         return dialog;
161 }
162
163 static void
164 gedit_print_dialog_response (GtkWidget *dialog, int response, GeditPrintJobInfo *pji)
165 {
166         GtkTextIter start, end;
167
168         pji->range_type = gnome_print_dialog_get_range (GNOME_PRINT_DIALOG (dialog));
169         gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (pji->doc), &start, &end);
170
171         switch (pji->range_type)
172         {
173         case GNOME_PRINT_RANGE_ALL:
174                 break;
175
176         case GNOME_PRINT_RANGE_SELECTION:
177                 gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (pji->doc),
178                                                       &start, &end);
179                 break;
180
181         default:
182                 g_return_if_reached ();
183         }
184
185         switch (response)
186         {
187         case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
188                 gedit_debug (DEBUG_PRINT, "Print button pressed.");
189                 pji->preview = PREVIEW_NO;
190                 gedit_print_real (pji, &start, &end, 
191                                   gtk_window_get_transient_for (GTK_WINDOW (dialog)));
192                 gtk_widget_destroy (dialog);
193                 break;
194
195         case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
196                 gedit_debug (DEBUG_PRINT, "Preview button pressed.");
197                 pji->preview = PREVIEW_FROM_DIALOG;
198                 gedit_print_preview_real (pji, &start, &end, GTK_WINDOW (dialog));
199                 break;
200
201         default:
202                 gtk_widget_destroy (dialog);
203                 gedit_print_job_info_destroy (pji, FALSE);
204         }
205
206
207 static void
208 show_printing_dialog (GeditPrintJobInfo *pji, GtkWindow *parent)
209 {
210         GtkWidget *window;
211         GtkWidget *frame;
212         GtkWidget *hbox;
213         GtkWidget *image;
214         GtkWidget *vbox;
215         GtkWidget *label;
216         GtkWidget *progressbar;
217
218         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
219         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
220         gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
221         gtk_window_set_destroy_with_parent (GTK_WINDOW (window), TRUE);
222         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER_ON_PARENT);
223                 
224         gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
225         gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
226         gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);
227  
228         gtk_window_set_transient_for (GTK_WINDOW (window), parent);
229
230         frame = gtk_frame_new (NULL);
231         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
232         gtk_container_add (GTK_CONTAINER (window), frame);
233
234         hbox = gtk_hbox_new (FALSE, 12);
235         gtk_container_add (GTK_CONTAINER (frame), hbox);
236         gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
237
238         image = gtk_image_new_from_stock ("gtk-print", GTK_ICON_SIZE_DIALOG);
239         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
240
241         vbox = gtk_vbox_new (FALSE, 12);
242         gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
243
244         label = gtk_label_new (_("Preparing pages..."));
245         gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
246         gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
247         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
248
249         progressbar = gtk_progress_bar_new ();
250         gtk_box_pack_start (GTK_BOX (vbox), progressbar, FALSE, FALSE, 0);
251         
252         pji->dialog = window;
253         pji->label = label;
254         pji->progressbar = progressbar;
255         
256         gtk_widget_show_all (pji->dialog);
257
258         /* Update UI */
259         while (gtk_events_pending ())
260                 gtk_main_iteration ();
261 }
262
263 static void
264 page_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
265 {
266         gchar *str;
267         gint page_num = gtk_source_print_job_get_page (pji->pjob);
268         gint total = gtk_source_print_job_get_page_count (pji->pjob);
269
270         if (pji->preview != PREVIEW_NO)
271                 str = g_strdup_printf (_("Rendering page %d of %d..."), page_num, total);
272         else
273                 str = g_strdup_printf (_("Printing page %d of %d..."), page_num, total);
274
275         gtk_label_set_label (GTK_LABEL (pji->label), str);
276         g_free (str);
277
278         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pji->progressbar), 
279                                        1.0 * page_num / total);
280
281         /* Update UI */
282         while (gtk_events_pending ())
283                 gtk_main_iteration ();
284
285 }
286
287 static void
288 preview_finished_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
289 {
290         GnomePrintJob *gjob;
291         GtkWidget *preview = NULL;
292
293         gjob = gtk_source_print_job_get_print_job (job);
294
295         preview = gnome_print_job_preview_new (gjob, _("Print preview"));
296         if (pji->parent != NULL)
297         {
298                 gtk_window_set_transient_for (GTK_WINDOW (preview), pji->parent);
299                 gtk_window_set_modal (GTK_WINDOW (preview), TRUE);
300         }
301         
302         g_object_unref (gjob);
303
304         gtk_widget_destroy (pji->dialog);
305
306         if (pji->preview == PREVIEW)
307                 gedit_print_job_info_destroy (pji, FALSE);
308         else
309         {
310                 g_signal_handlers_disconnect_by_func (pji->pjob, (GCallback) page_cb, pji);
311                 g_signal_handlers_disconnect_by_func (pji->pjob, (GCallback) preview_finished_cb, pji);
312         }
313         
314         gtk_widget_show (preview);
315 }
316
317 static void
318 print_finished_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
319 {
320         GnomePrintJob *gjob;
321
322         gjob = gtk_source_print_job_get_print_job (job);
323
324         gnome_print_job_print (gjob);
325         
326         g_object_unref (gjob);
327
328         gtk_widget_destroy (pji->dialog);
329
330         gedit_print_job_info_destroy (pji, TRUE);
331 }
332
333 void 
334 gedit_print (GtkTextView *view)
335 {
336         GeditPrintJobInfo *pji;
337         GtkWidget *dialog;
338         GtkWidget *parent;
339
340         gedit_debug (DEBUG_PRINT, "");
341
342         g_return_if_fail (view != NULL);
343
344         pji = gedit_print_job_info_new (view);
345         pji->preview = PREVIEW_NO;
346         
347         parent = gtk_widget_get_toplevel (GTK_WIDGET (view));
348         if (parent == NULL || !GTK_IS_WINDOW(parent))
349                 parent = mainwindow_get_mainwindow()->window;
350
351         dialog = get_print_dialog (pji, GTK_WINDOW (parent));
352         
353         g_signal_connect (dialog, "response",
354                           G_CALLBACK (gedit_print_dialog_response),
355                           pji);
356
357         gtk_widget_show (dialog);
358 }
359
360 static void 
361 gedit_print_preview_real (GeditPrintJobInfo *pji, 
362                           GtkTextIter       *start, 
363                           GtkTextIter       *end, 
364                           GtkWindow         *parent)
365 {
366         show_printing_dialog (pji, parent);
367
368         pji->parent = parent;
369
370         g_signal_connect (pji->pjob, "begin_page", (GCallback) page_cb, pji);
371         g_signal_connect (pji->pjob, "finished", (GCallback) preview_finished_cb, pji);
372
373         if (!gtk_source_print_job_print_range_async (pji->pjob, start, end))
374         {
375                 /* FIXME */
376                 g_warning ("Async print failed");
377                 gtk_widget_destroy (pji->dialog);
378         }
379 }
380
381 static void 
382 gedit_print_real (GeditPrintJobInfo *pji, 
383                   GtkTextIter       *start, 
384                   GtkTextIter       *end, 
385                   GtkWindow         *parent)
386 {
387         show_printing_dialog (pji, parent);
388
389         g_signal_connect (pji->pjob, "begin_page", (GCallback) page_cb, pji);
390         g_signal_connect (pji->pjob, "finished", (GCallback) print_finished_cb, pji);
391
392         if (!gtk_source_print_job_print_range_async (pji->pjob, start, end))
393         {
394                 /* FIXME */
395                 g_warning ("Async print failed");
396                 gtk_widget_destroy (pji->dialog);
397         }
398 }
399
400 void 
401 gedit_print_preview (GtkTextView *view)
402 {
403         GeditPrintJobInfo *pji;
404         GtkTextIter start, end;
405
406         gedit_debug (DEBUG_PRINT, "");
407                 
408         g_return_if_fail (view != NULL);
409
410         pji = gedit_print_job_info_new (view);
411
412         gtk_text_buffer_get_bounds (pji->doc, &start, &end);
413
414         pji->preview = PREVIEW;
415         gedit_print_preview_real (pji, &start, &end, 
416                                   GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view))));
417 }
418
419 static GeditPrintJobInfo *
420 gedit_print_job_info_new (GtkTextView* view)
421 {       
422         GtkSourcePrintJob *pjob;
423         GnomePrintConfig *config;
424         GeditPrintJobInfo *pji;
425         PangoContext *pango_context;
426         PangoFontDescription *font_desc;
427         GtkTextBuffer *buffer;
428         
429         gedit_debug (DEBUG_PRINT, "");
430         
431         g_return_val_if_fail (view != NULL, NULL);
432
433         buffer = gtk_text_view_get_buffer (view);
434         g_return_val_if_fail (buffer != NULL, NULL);
435
436         config = gnome_print_config_default ();
437         g_return_val_if_fail (config != NULL, NULL);
438
439         gnome_print_config_set_int (config, GNOME_PRINT_KEY_NUM_COPIES, 1);
440         gnome_print_config_set_boolean (config, GNOME_PRINT_KEY_COLLATE, FALSE);
441
442         pjob = gtk_source_print_job_new_with_buffer (config, buffer);
443         gnome_print_config_unref (config);
444
445         gtk_source_print_job_set_highlight (pjob, TRUE);
446         gtk_source_print_job_set_print_numbers (pjob, FALSE);
447         gtk_source_print_job_set_wrap_mode (pjob, gtk_text_view_get_wrap_mode (view));
448         gtk_source_print_job_set_tabs_width (pjob, 8);
449         
450         gtk_source_print_job_set_footer_format (pjob,
451                                                 _("Page %N of %Q"), 
452                                                 NULL, 
453                                                 prefs_common.date_format, 
454                                                 TRUE);
455
456         gtk_source_print_job_set_print_header (pjob, FALSE);
457         gtk_source_print_job_set_print_footer (pjob, TRUE);
458
459         pango_context = gtk_widget_get_pango_context (GTK_WIDGET (view));
460         font_desc = pango_context_get_font_description (pango_context);
461
462         gtk_source_print_job_set_font_desc (pjob, font_desc);
463
464         pji = g_new0 (GeditPrintJobInfo, 1);
465
466         pji->pjob = pjob;
467
468         pji->doc = buffer;
469         pji->preview = PREVIEW_NO;
470         pji->range_type = GNOME_PRINT_RANGE_ALL;
471
472         return pji;
473 }
474
475 #endif