1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
4 * This file is part of gedit
6 * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi
7 * Copyright (C) 2002 Paolo Maggi
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.
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.
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/>.
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.
35 #include <sys/types.h>
39 #include <string.h> /* For strlen */
41 #include <glib/gi18n.h>
42 #include <gtk/gtkframe.h>
43 #include <gtk/gtkhbox.h>
44 #include <gtk/gtkvbox.h>
45 #include <gtk/gtklabel.h>
46 #include <gtk/gtkmain.h>
47 #include <gtk/gtkprogressbar.h>
48 #include <gtk/gtkimage.h>
49 #include <gtk/gtkwindow.h>
50 #include <libgnomeprintui/gnome-print-dialog.h>
51 #include <libgnomeprintui/gnome-print-job-preview.h>
53 #include "gedit-print.h"
54 #include "gtk/gtksourceprintjob.h"
55 #include "mainwindow.h"
56 #include "prefs_common.h"
59 # define DEBUG_PRINT "DEBUG_PRINT: %s"
60 # define gedit_debug(x, y) g_warning(x, y)
62 # define gedit_debug(x, y) do {} while (FALSE);
72 typedef struct _GeditPrintJobInfo GeditPrintJobInfo
;
74 struct _GeditPrintJobInfo
78 GtkSourcePrintJob
*pjob
;
84 gint first_line_to_print
;
85 gint last_line_to_print
;
90 GtkWidget
*progressbar
;
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
,
103 static void gedit_print_preview_real (GeditPrintJobInfo
*pji
,
110 gedit_print_job_info_destroy (GeditPrintJobInfo
*pji
, gboolean save_config
)
112 gedit_debug (DEBUG_PRINT
, "");
114 g_return_if_fail (pji
!= NULL
);
116 if (pji
->pjob
!= NULL
)
117 g_object_unref (pji
->pjob
);
123 get_print_dialog (GeditPrintJobInfo
*pji
, GtkWindow
*parent
)
128 GnomePrintConfig
*config
;
130 gedit_debug (DEBUG_PRINT
, "");
132 g_return_val_if_fail (pji
!= NULL
, NULL
);
134 if (!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (pji
->doc
), NULL
, NULL
))
135 selection_flag
= GNOME_PRINT_RANGE_SELECTION_UNSENSITIVE
;
137 selection_flag
= GNOME_PRINT_RANGE_SELECTION
;
139 g_return_val_if_fail(pji
->pjob
!= NULL
, NULL
);
140 config
= gtk_source_print_job_get_config (pji
->pjob
);
142 dialog
= g_object_new (GNOME_TYPE_PRINT_DIALOG
, "print_config", config
, NULL
);
144 gnome_print_dialog_construct (GNOME_PRINT_DIALOG (dialog
),
146 GNOME_PRINT_DIALOG_RANGE
| GNOME_PRINT_DIALOG_COPIES
);
148 lines
= gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (pji
->doc
));
150 gnome_print_dialog_construct_range_any ( GNOME_PRINT_DIALOG (dialog
),
151 (GNOME_PRINT_RANGE_ALL
| selection_flag
),
154 gtk_window_set_transient_for (GTK_WINDOW (dialog
), parent
);
156 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
157 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog
), TRUE
);
163 gedit_print_dialog_response (GtkWidget
*dialog
, int response
, GeditPrintJobInfo
*pji
)
165 GtkTextIter start
, end
;
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
);
170 switch (pji
->range_type
)
172 case GNOME_PRINT_RANGE_ALL
:
175 case GNOME_PRINT_RANGE_SELECTION
:
176 gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (pji
->doc
),
181 g_return_if_reached ();
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
);
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
));
201 gtk_widget_destroy (dialog
);
207 show_printing_dialog (GeditPrintJobInfo
*pji
, GtkWindow
*parent
)
215 GtkWidget
*progressbar
;
217 window
= gtkut_window_new(GTK_WINDOW_TOPLEVEL
, "gedit-print");
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
);
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
);
227 gtk_window_set_transient_for (GTK_WINDOW (window
), parent
);
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
);
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);
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);
240 vbox
= gtk_vbox_new (FALSE
, 12);
241 gtk_box_pack_start (GTK_BOX (hbox
), vbox
, FALSE
, FALSE
, 0);
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);
248 progressbar
= gtk_progress_bar_new ();
249 gtk_box_pack_start (GTK_BOX (vbox
), progressbar
, FALSE
, FALSE
, 0);
251 pji
->dialog
= window
;
253 pji
->progressbar
= progressbar
;
255 gtk_widget_show_all (pji
->dialog
);
258 while (gtk_events_pending ())
259 gtk_main_iteration ();
263 page_cb (GtkSourcePrintJob
*job
, GeditPrintJobInfo
*pji
)
266 gint page_num
= gtk_source_print_job_get_page (pji
->pjob
);
267 gint total
= gtk_source_print_job_get_page_count (pji
->pjob
);
269 if (pji
->preview
!= PREVIEW_NO
)
270 str
= g_strdup_printf (_("Rendering page %d of %d..."), page_num
, total
);
272 str
= g_strdup_printf (_("Printing page %d of %d..."), page_num
, total
);
274 gtk_label_set_label (GTK_LABEL (pji
->label
), str
);
277 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pji
->progressbar
),
278 (total
== 0) ? 0 : 1.0 * page_num
/ total
);
281 while (gtk_events_pending ())
282 gtk_main_iteration ();
287 preview_finished_cb (GtkSourcePrintJob
*job
, GeditPrintJobInfo
*pji
)
290 GtkWidget
*preview
= NULL
;
292 gjob
= gtk_source_print_job_get_print_job (job
);
294 preview
= gnome_print_job_preview_new (gjob
, _("Print preview"));
295 if (pji
->parent
!= NULL
)
297 gtk_window_set_transient_for (GTK_WINDOW (preview
), pji
->parent
);
298 gtk_window_set_modal (GTK_WINDOW (preview
), TRUE
);
301 g_object_unref (gjob
);
303 gtk_widget_destroy (pji
->dialog
);
305 if (pji
->preview
== PREVIEW
)
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
);
313 gtk_widget_show (preview
);
317 print_finished_cb (GtkSourcePrintJob
*job
, GeditPrintJobInfo
*pji
)
321 gjob
= gtk_source_print_job_get_print_job (job
);
323 gnome_print_job_print (gjob
);
325 g_object_unref (gjob
);
327 gtk_widget_destroy (pji
->dialog
);
333 gedit_print (GtkTextView
*view
)
335 GeditPrintJobInfo
*pji
;
339 gedit_debug (DEBUG_PRINT
, "");
341 g_return_if_fail (view
!= NULL
);
343 pji
= gedit_print_job_info_new (view
);
344 pji
->preview
= PREVIEW_NO
;
346 parent
= gtk_widget_get_toplevel (GTK_WIDGET (view
));
347 if (parent
== NULL
|| !GTK_IS_WINDOW(parent
))
348 parent
= mainwindow_get_mainwindow()->window
;
350 dialog
= get_print_dialog (pji
, GTK_WINDOW (parent
));
352 g_signal_connect (dialog
, "response",
353 G_CALLBACK (gedit_print_dialog_response
),
356 gtk_widget_show (dialog
);
357 while (pji
->done
!= TRUE
)
358 gtk_main_iteration();
359 gedit_print_job_info_destroy (pji
, pji
->preview
? FALSE
:TRUE
);
364 gedit_print_preview_real (GeditPrintJobInfo
*pji
,
369 show_printing_dialog (pji
, parent
);
371 pji
->parent
= parent
;
373 g_signal_connect (pji
->pjob
, "begin_page", (GCallback
) page_cb
, pji
);
374 g_signal_connect (pji
->pjob
, "finished", (GCallback
) preview_finished_cb
, pji
);
376 if (!gtk_source_print_job_print_range_async (pji
->pjob
, start
, end
))
379 g_warning ("Async print failed");
380 gtk_widget_destroy (pji
->dialog
);
385 gedit_print_real (GeditPrintJobInfo
*pji
,
390 show_printing_dialog (pji
, parent
);
392 g_signal_connect (pji
->pjob
, "begin_page", (GCallback
) page_cb
, pji
);
393 g_signal_connect (pji
->pjob
, "finished", (GCallback
) print_finished_cb
, pji
);
395 if (!gtk_source_print_job_print_range_async (pji
->pjob
, start
, end
))
398 g_warning ("Async print failed");
399 gtk_widget_destroy (pji
->dialog
);
403 static GeditPrintJobInfo
*
404 gedit_print_job_info_new (GtkTextView
* view
)
406 GtkSourcePrintJob
*pjob
;
407 GnomePrintConfig
*config
;
408 GeditPrintJobInfo
*pji
;
409 PangoContext
*pango_context
;
410 PangoFontDescription
*font_desc
;
411 GtkTextBuffer
*buffer
;
413 gedit_debug (DEBUG_PRINT
, "");
415 g_return_val_if_fail (view
!= NULL
, NULL
);
417 buffer
= gtk_text_view_get_buffer (view
);
418 g_return_val_if_fail (buffer
!= NULL
, NULL
);
420 config
= gnome_print_config_default ();
421 g_return_val_if_fail (config
!= NULL
, NULL
);
423 gnome_print_config_set_int (config
, GNOME_PRINT_KEY_NUM_COPIES
, 1);
424 gnome_print_config_set_boolean (config
, GNOME_PRINT_KEY_COLLATE
, FALSE
);
426 pjob
= gtk_source_print_job_new_with_buffer (config
, buffer
);
427 gnome_print_config_unref (config
);
429 gtk_source_print_job_set_highlight (pjob
, TRUE
);
430 gtk_source_print_job_set_print_numbers (pjob
, FALSE
);
431 gtk_source_print_job_set_wrap_mode (pjob
, gtk_text_view_get_wrap_mode (view
));
432 gtk_source_print_job_set_tabs_width (pjob
, 8);
434 gtk_source_print_job_set_footer_format (pjob
,
437 prefs_common
.date_format
,
440 gtk_source_print_job_set_print_header (pjob
, FALSE
);
441 gtk_source_print_job_set_print_footer (pjob
, TRUE
);
443 pango_context
= gtk_widget_get_pango_context (GTK_WIDGET (view
));
444 if (prefs_common
.use_different_print_font
)
446 font_desc
= pango_font_description_from_string (prefs_common
.printfont
);
450 font_desc
= pango_context_get_font_description (pango_context
);
453 gtk_source_print_job_set_font_desc (pjob
, font_desc
);
454 if (prefs_common
.use_different_print_font
)
456 pango_font_description_free (font_desc
);
459 pji
= g_new0 (GeditPrintJobInfo
, 1);
464 pji
->preview
= PREVIEW_NO
;
465 pji
->range_type
= GNOME_PRINT_RANGE_ALL
;