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