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