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