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 2 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, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330,
22
* Boston, MA 02111-1307, USA.
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.
35
#include <sys/types.h>
39
#include <string.h> /* For strlen */
41
#include <glib/gi18n.h>
42
#include <libgnomeprintui/gnome-print-dialog.h>
43
#include <libgnomeprintui/gnome-print-job-preview.h>
45
#include "gedit-print.h"
46
#include "gtksourceprintjob.h"
49
# define DEBUG_PRINT "DEBUG_PRINT: %s"
50
# define gedit_debug(x, y) g_warning(x, y)
52
# define gedit_debug(x, y) do {} while (FALSE);
62
typedef struct _GeditPrintJobInfo GeditPrintJobInfo;
64
struct _GeditPrintJobInfo
68
GtkSourcePrintJob *pjob;
74
gint first_line_to_print;
75
gint last_line_to_print;
80
GtkWidget *progressbar;
85
static GeditPrintJobInfo* gedit_print_job_info_new (GtkTextView *view);
86
static void gedit_print_job_info_destroy (GeditPrintJobInfo *pji,
87
gboolean save_config);
88
static void gedit_print_real (GeditPrintJobInfo *pji,
92
static void gedit_print_preview_real (GeditPrintJobInfo *pji,
99
gedit_print_job_info_destroy (GeditPrintJobInfo *pji, gboolean save_config)
101
gedit_debug (DEBUG_PRINT, "");
103
g_return_if_fail (pji != NULL);
105
if (pji->pjob != NULL)
106
g_object_unref (pji->pjob);
112
get_print_dialog (GeditPrintJobInfo *pji, GtkWindow *parent)
117
GnomePrintConfig *config;
119
gedit_debug (DEBUG_PRINT, "");
121
g_return_val_if_fail (pji != NULL, NULL);
123
if (!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (pji->doc), NULL, NULL))
124
selection_flag = GNOME_PRINT_RANGE_SELECTION_UNSENSITIVE;
126
selection_flag = GNOME_PRINT_RANGE_SELECTION;
128
g_return_val_if_fail(pji->pjob != NULL, NULL);
129
config = gtk_source_print_job_get_config (pji->pjob);
131
dialog = g_object_new (GNOME_TYPE_PRINT_DIALOG, "print_config", config, NULL);
133
gnome_print_dialog_construct (GNOME_PRINT_DIALOG (dialog),
134
(guchar *) _("Print"),
135
GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
137
lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (pji->doc));
139
gnome_print_dialog_construct_range_any ( GNOME_PRINT_DIALOG (dialog),
140
(GNOME_PRINT_RANGE_ALL | selection_flag),
143
gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
145
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
146
gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
152
gedit_print_dialog_response (GtkWidget *dialog, int response, GeditPrintJobInfo *pji)
154
GtkTextIter start, end;
156
pji->range_type = gnome_print_dialog_get_range (GNOME_PRINT_DIALOG (dialog));
157
gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (pji->doc), &start, &end);
159
switch (pji->range_type)
161
case GNOME_PRINT_RANGE_ALL:
164
case GNOME_PRINT_RANGE_SELECTION:
165
gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (pji->doc),
170
g_return_if_reached ();
175
case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
176
gedit_debug (DEBUG_PRINT, "Print button pressed.");
177
pji->preview = PREVIEW_NO;
178
gedit_print_real (pji, &start, &end,
179
gtk_window_get_transient_for (GTK_WINDOW (dialog)));
180
gtk_widget_destroy (dialog);
183
case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
184
gedit_debug (DEBUG_PRINT, "Preview button pressed.");
185
pji->preview = PREVIEW_FROM_DIALOG;
186
gedit_print_preview_real (pji, &start, &end, GTK_WINDOW (dialog));
190
gtk_widget_destroy (dialog);
191
gedit_print_job_info_destroy (pji, FALSE);
196
show_printing_dialog (GeditPrintJobInfo *pji, GtkWindow *parent)
204
GtkWidget *progressbar;
206
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
207
gtk_window_set_modal (GTK_WINDOW (window), TRUE);
208
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
209
gtk_window_set_destroy_with_parent (GTK_WINDOW (window), TRUE);
210
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER_ON_PARENT);
212
gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
213
gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
214
gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);
216
gtk_window_set_transient_for (GTK_WINDOW (window), parent);
218
frame = gtk_frame_new (NULL);
219
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
220
gtk_container_add (GTK_CONTAINER (window), frame);
222
hbox = gtk_hbox_new (FALSE, 12);
223
gtk_container_add (GTK_CONTAINER (frame), hbox);
224
gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
226
image = gtk_image_new_from_stock ("gtk-print", GTK_ICON_SIZE_DIALOG);
227
gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
229
vbox = gtk_vbox_new (FALSE, 12);
230
gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
232
label = gtk_label_new (_("Preparing pages..."));
233
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
234
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
235
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
237
progressbar = gtk_progress_bar_new ();
238
gtk_box_pack_start (GTK_BOX (vbox), progressbar, FALSE, FALSE, 0);
240
pji->dialog = window;
242
pji->progressbar = progressbar;
244
gtk_widget_show_all (pji->dialog);
247
while (gtk_events_pending ())
248
gtk_main_iteration ();
252
page_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
255
gint page_num = gtk_source_print_job_get_page (pji->pjob);
256
gint total = gtk_source_print_job_get_page_count (pji->pjob);
258
if (pji->preview != PREVIEW_NO)
259
str = g_strdup_printf (_("Rendering page %d of %d..."), page_num, total);
261
str = g_strdup_printf (_("Printing page %d of %d..."), page_num, total);
263
gtk_label_set_label (GTK_LABEL (pji->label), str);
266
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pji->progressbar),
267
1.0 * page_num / total);
270
while (gtk_events_pending ())
271
gtk_main_iteration ();
276
preview_finished_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
279
GtkWidget *preview = NULL;
281
gjob = gtk_source_print_job_get_print_job (job);
283
preview = gnome_print_job_preview_new (gjob, (guchar *) _("Print preview"));
284
if (pji->parent != NULL)
286
gtk_window_set_transient_for (GTK_WINDOW (preview), pji->parent);
287
gtk_window_set_modal (GTK_WINDOW (preview), TRUE);
290
g_object_unref (gjob);
292
gtk_widget_destroy (pji->dialog);
294
if (pji->preview == PREVIEW)
295
gedit_print_job_info_destroy (pji, FALSE);
298
g_signal_handlers_disconnect_by_func (pji->pjob, (GCallback) page_cb, pji);
299
g_signal_handlers_disconnect_by_func (pji->pjob, (GCallback) preview_finished_cb, pji);
302
gtk_widget_show (preview);
306
print_finished_cb (GtkSourcePrintJob *job, GeditPrintJobInfo *pji)
310
gjob = gtk_source_print_job_get_print_job (job);
312
gnome_print_job_print (gjob);
314
g_object_unref (gjob);
316
gtk_widget_destroy (pji->dialog);
318
gedit_print_job_info_destroy (pji, TRUE);
322
gedit_print (GtkTextView *view)
324
GeditPrintJobInfo *pji;
327
gedit_debug (DEBUG_PRINT, "");
329
g_return_if_fail (view != NULL);
331
pji = gedit_print_job_info_new (view);
332
pji->preview = PREVIEW_NO;
334
dialog = get_print_dialog (pji, GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view))));
336
g_signal_connect (dialog, "response",
337
G_CALLBACK (gedit_print_dialog_response),
340
gtk_widget_show (dialog);
344
gedit_print_preview_real (GeditPrintJobInfo *pji,
349
show_printing_dialog (pji, parent);
351
pji->parent = parent;
353
g_signal_connect (pji->pjob, "begin_page", (GCallback) page_cb, pji);
354
g_signal_connect (pji->pjob, "finished", (GCallback) preview_finished_cb, pji);
356
if (!gtk_source_print_job_print_range_async (pji->pjob, start, end))
359
g_warning ("Async print failed");
360
gtk_widget_destroy (pji->dialog);
365
gedit_print_real (GeditPrintJobInfo *pji,
370
show_printing_dialog (pji, parent);
372
g_signal_connect (pji->pjob, "begin_page", (GCallback) page_cb, pji);
373
g_signal_connect (pji->pjob, "finished", (GCallback) print_finished_cb, pji);
375
if (!gtk_source_print_job_print_range_async (pji->pjob, start, end))
378
g_warning ("Async print failed");
379
gtk_widget_destroy (pji->dialog);
384
gedit_print_preview (GtkTextView *view)
386
GeditPrintJobInfo *pji;
387
GtkTextIter start, end;
389
gedit_debug (DEBUG_PRINT, "");
391
g_return_if_fail (view != NULL);
393
pji = gedit_print_job_info_new (view);
395
gtk_text_buffer_get_bounds (pji->doc, &start, &end);
397
pji->preview = PREVIEW;
398
gedit_print_preview_real (pji, &start, &end,
399
GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view))));
402
static GeditPrintJobInfo *
403
gedit_print_job_info_new (GtkTextView* view)
405
GtkSourcePrintJob *pjob;
406
GnomePrintConfig *config;
407
GeditPrintJobInfo *pji;
408
PangoContext *pango_context;
409
PangoFontDescription *font_desc;
410
GtkTextBuffer *buffer;
412
gedit_debug (DEBUG_PRINT, "");
414
g_return_val_if_fail (view != NULL, NULL);
416
buffer = gtk_text_view_get_buffer (view);
417
g_return_val_if_fail (buffer != NULL, NULL);
419
config = gnome_print_config_default ();
420
g_return_val_if_fail (config != NULL, NULL);
422
gnome_print_config_set_int (config, (const guchar *) GNOME_PRINT_KEY_NUM_COPIES, 1);
423
gnome_print_config_set_boolean (config, (const guchar *) GNOME_PRINT_KEY_COLLATE, FALSE);
425
pjob = gtk_source_print_job_new_with_buffer (config, buffer);
426
gnome_print_config_unref (config);
428
gtk_source_print_job_set_highlight (pjob, TRUE);
429
gtk_source_print_job_set_print_numbers (pjob, FALSE);
430
gtk_source_print_job_set_wrap_mode (pjob, gtk_text_view_get_wrap_mode (view));
431
gtk_source_print_job_set_tabs_width (pjob, 8);
433
gtk_source_print_job_set_footer_format (pjob,
436
/* xgettext:no-c-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
font_desc = pango_context_get_font_description (pango_context);
446
gtk_source_print_job_set_font_desc (pjob, font_desc);
448
pji = g_new0 (GeditPrintJobInfo, 1);
453
pji->preview = PREVIEW_NO;
454
pji->range_type = GNOME_PRINT_RANGE_ALL;