1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
5 * Evince is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Evince is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 #include "ev-print-operation.h"
24 #if GTKUNIXPRINT_ENABLED
25 #include <gtk/gtkunixprint.h>
27 #include <glib/gi18n.h>
28 #include <glib/gstdio.h>
32 #include "ev-job-scheduler.h"
46 static guint signals[LAST_SIGNAL] = { 0 };
48 struct _EvPrintOperation {
58 struct _EvPrintOperationClass {
59 GObjectClass parent_class;
61 void (* set_current_page) (EvPrintOperation *op,
63 void (* set_print_settings) (EvPrintOperation *op,
64 GtkPrintSettings *print_settings);
65 GtkPrintSettings *(* get_print_settings) (EvPrintOperation *op);
66 void (* set_default_page_setup) (EvPrintOperation *op,
67 GtkPageSetup *page_setup);
68 GtkPageSetup *(* get_default_page_setup) (EvPrintOperation *op);
69 void (* set_job_name) (EvPrintOperation *op,
70 const gchar *job_name);
71 const gchar *(* get_job_name) (EvPrintOperation *op);
72 void (* run) (EvPrintOperation *op,
74 void (* cancel) (EvPrintOperation *op);
75 void (* get_error) (EvPrintOperation *op,
77 void (* set_embed_page_setup) (EvPrintOperation *op,
79 gboolean (* get_embed_page_setup) (EvPrintOperation *op);
82 void (* done) (EvPrintOperation *op,
83 GtkPrintOperationResult result);
84 void (* begin_print) (EvPrintOperation *op);
85 void (* status_changed) (EvPrintOperation *op);
88 G_DEFINE_ABSTRACT_TYPE (EvPrintOperation, ev_print_operation, G_TYPE_OBJECT)
91 ev_print_operation_finalize (GObject *object)
93 EvPrintOperation *op = EV_PRINT_OPERATION (object);
96 g_object_unref (op->document);
105 (* G_OBJECT_CLASS (ev_print_operation_parent_class)->finalize) (object);
109 ev_print_operation_set_property (GObject *object,
114 EvPrintOperation *op = EV_PRINT_OPERATION (object);
118 op->document = g_value_dup_object (value);
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126 ev_print_operation_init (EvPrintOperation *op)
131 ev_print_operation_class_init (EvPrintOperationClass *klass)
133 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
135 g_object_class->set_property = ev_print_operation_set_property;
136 g_object_class->finalize = ev_print_operation_finalize;
138 g_object_class_install_property (g_object_class,
140 g_param_spec_object ("document",
142 "The document to print",
145 G_PARAM_CONSTRUCT_ONLY));
147 g_signal_new ("done",
148 G_TYPE_FROM_CLASS (g_object_class),
150 G_STRUCT_OFFSET (EvPrintOperationClass, done),
152 g_cclosure_marshal_VOID__ENUM,
154 GTK_TYPE_PRINT_OPERATION_RESULT);
155 signals[BEGIN_PRINT] =
156 g_signal_new ("begin_print",
157 G_TYPE_FROM_CLASS (g_object_class),
159 G_STRUCT_OFFSET (EvPrintOperationClass, begin_print),
161 g_cclosure_marshal_VOID__VOID,
163 signals[STATUS_CHANGED] =
164 g_signal_new ("status_changed",
165 G_TYPE_FROM_CLASS (g_object_class),
167 G_STRUCT_OFFSET (EvPrintOperationClass, status_changed),
169 g_cclosure_marshal_VOID__VOID,
175 ev_print_operation_set_current_page (EvPrintOperation *op,
178 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
180 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
181 g_return_if_fail (current_page >= 0);
183 class->set_current_page (op, current_page);
187 ev_print_operation_set_print_settings (EvPrintOperation *op,
188 GtkPrintSettings *print_settings)
190 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
192 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
193 g_return_if_fail (GTK_IS_PRINT_SETTINGS (print_settings));
195 class->set_print_settings (op, print_settings);
199 ev_print_operation_get_print_settings (EvPrintOperation *op)
201 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
203 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
205 return class->get_print_settings (op);
209 ev_print_operation_set_default_page_setup (EvPrintOperation *op,
210 GtkPageSetup *page_setup)
212 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
214 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
215 g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
217 class->set_default_page_setup (op, page_setup);
221 ev_print_operation_get_default_page_setup (EvPrintOperation *op)
223 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
225 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
227 return class->get_default_page_setup (op);
231 ev_print_operation_set_job_name (EvPrintOperation *op,
232 const gchar *job_name)
234 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
236 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
237 g_return_if_fail (job_name != NULL);
239 class->set_job_name (op, job_name);
243 ev_print_operation_get_job_name (EvPrintOperation *op)
245 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
247 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
249 return class->get_job_name (op);
253 ev_print_operation_run (EvPrintOperation *op,
256 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
258 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
260 class->run (op, parent);
264 ev_print_operation_cancel (EvPrintOperation *op)
266 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
268 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
274 ev_print_operation_get_error (EvPrintOperation *op,
277 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
279 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
281 class->get_error (op, error);
285 ev_print_operation_set_embed_page_setup (EvPrintOperation *op,
288 #if GTK_CHECK_VERSION (2, 17, 4)
289 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
291 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
293 class->set_embed_page_setup (op, embed);
298 ev_print_operation_get_embed_page_setup (EvPrintOperation *op)
300 #if GTK_CHECK_VERSION (2, 17, 4)
301 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
303 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), FALSE);
305 return class->get_embed_page_setup (op);
312 ev_print_operation_get_status (EvPrintOperation *op)
314 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
316 return op->status ? op->status : "";
320 ev_print_operation_get_progress (EvPrintOperation *op)
322 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), 0.0);
327 #if GTK_CHECK_VERSION (2, 17, 1) | GTKUNIXPRINT_ENABLED
329 ev_print_operation_update_status (EvPrintOperation *op,
334 if (op->status && op->progress == progress)
341 op->status = g_strdup (_("Preparing to print ..."));
342 } else if (page > n_pages) {
343 op->status = g_strdup (_("Finishing..."));
345 op->status = g_strdup_printf (_("Printing page %d of %d..."),
349 op->progress = MIN (1.0, progress);
351 g_signal_emit (op, signals[STATUS_CHANGED], 0);
355 #if GTKUNIXPRINT_ENABLED
357 /* Export interface */
358 #define EV_TYPE_PRINT_OPERATION_EXPORT (ev_print_operation_export_get_type())
359 #define EV_PRINT_OPERATION_EXPORT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExport))
360 #define EV_PRINT_OPERATION_EXPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExportClass))
361 #define EV_IS_PRINT_OPERATION_EXPORT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_EXPORT))
363 typedef struct _EvPrintOperationExport EvPrintOperationExport;
364 typedef struct _EvPrintOperationExportClass EvPrintOperationExportClass;
366 static GType ev_print_operation_export_get_type (void) G_GNUC_CONST;
368 static void ev_print_operation_export_begin (EvPrintOperationExport *export);
369 static gboolean export_print_page (EvPrintOperationExport *export);
370 static void export_cancel (EvPrintOperationExport *export);
372 struct _EvPrintOperationExport {
373 EvPrintOperation parent;
375 GtkWindow *parent_window;
379 gboolean print_preview;
383 GtkPageSetup *page_setup;
384 GtkPrintSettings *print_settings;
389 gint pages_per_sheet;
393 gboolean embed_page_setup;
398 EvFileExporterContext fc;
399 gint n_pages_to_print;
400 gint uncollated_copies;
401 gint collated_copies;
402 gint uncollated, collated, total;
404 gint sheet, page_count;
406 gint range, n_ranges;
407 GtkPageRange *ranges;
408 GtkPageRange one_range;
410 gint page, start, end, inc;
413 struct _EvPrintOperationExportClass {
414 EvPrintOperationClass parent_class;
417 G_DEFINE_TYPE (EvPrintOperationExport, ev_print_operation_export, EV_TYPE_PRINT_OPERATION)
419 /* Internal print queue */
420 static GHashTable *print_queue = NULL;
423 queue_free (GQueue *queue)
425 g_queue_foreach (queue, (GFunc)g_object_unref, NULL);
426 g_queue_free (queue);
430 ev_print_queue_init (void)
432 if (G_UNLIKELY (print_queue == NULL)) {
433 print_queue = g_hash_table_new_full (g_direct_hash,
436 (GDestroyNotify)queue_free);
441 remove_document_queue (gpointer data,
445 g_hash_table_remove (print_queue, document);
449 ev_print_queue_is_empty (EvDocument *document)
453 queue = g_hash_table_lookup (print_queue, document);
454 return (!queue || g_queue_is_empty (queue));
458 ev_print_queue_push (EvPrintOperation *op)
462 queue = g_hash_table_lookup (print_queue, op->document);
464 queue = g_queue_new ();
465 g_hash_table_insert (print_queue,
468 g_object_weak_ref (G_OBJECT (op->document),
469 (GWeakNotify)remove_document_queue,
473 g_queue_push_head (queue, g_object_ref (op));
476 static EvPrintOperation *
477 ev_print_queue_pop (EvDocument *document)
479 EvPrintOperation *op;
482 queue = g_hash_table_lookup (print_queue, document);
483 if (!queue || g_queue_is_empty (queue))
486 op = g_queue_pop_tail (queue);
492 static EvPrintOperation *
493 ev_print_queue_peek (EvDocument *document)
497 queue = g_hash_table_lookup (print_queue, document);
498 if (!queue || g_queue_is_empty (queue))
501 return g_queue_peek_tail (queue);
505 ev_print_operation_export_set_current_page (EvPrintOperation *op,
508 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
510 g_return_if_fail (current_page < export->n_pages);
512 export->current_page = current_page;
516 ev_print_operation_export_set_print_settings (EvPrintOperation *op,
517 GtkPrintSettings *print_settings)
519 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
521 if (print_settings == export->print_settings)
524 g_object_ref (print_settings);
525 if (export->print_settings)
526 g_object_unref (export->print_settings);
527 export->print_settings = print_settings;
530 static GtkPrintSettings *
531 ev_print_operation_export_get_print_settings (EvPrintOperation *op)
533 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
535 return export->print_settings;
539 ev_print_operation_export_set_default_page_setup (EvPrintOperation *op,
540 GtkPageSetup *page_setup)
542 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
544 if (page_setup == export->page_setup)
547 g_object_ref (page_setup);
548 if (export->page_setup)
549 g_object_unref (export->page_setup);
550 export->page_setup = page_setup;
553 static GtkPageSetup *
554 ev_print_operation_export_get_default_page_setup (EvPrintOperation *op)
556 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
558 return export->page_setup;
562 ev_print_operation_export_set_job_name (EvPrintOperation *op,
563 const gchar *job_name)
565 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
567 g_free (export->job_name);
568 export->job_name = g_strdup (job_name);
572 ev_print_operation_export_get_job_name (EvPrintOperation *op)
574 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
576 return export->job_name;
580 ev_print_operation_export_set_printer (EvPrintOperationExport *export,
583 if (printer == export->printer)
586 g_object_ref (printer);
588 g_object_unref (export->printer);
589 export->printer = printer;
593 find_range (EvPrintOperationExport *export)
597 range = &export->ranges[export->range];
599 if (export->inc < 0) {
600 export->start = range->end;
601 export->end = range->start - 1;
603 export->start = range->start;
604 export->end = range->end + 1;
609 clamp_ranges (EvPrintOperationExport *export)
611 gint num_of_correct_ranges = 0;
612 gint n_pages_to_print = 0;
614 gboolean null_flag = FALSE;
616 for (i = 0; i < export->n_ranges; i++) {
619 if ((export->ranges[i].start >= 0) &&
620 (export->ranges[i].start < export->n_pages) &&
621 (export->ranges[i].end >= 0) &&
622 (export->ranges[i].end < export->n_pages)) {
623 export->ranges[num_of_correct_ranges] = export->ranges[i];
624 num_of_correct_ranges++;
625 } else if ((export->ranges[i].start >= 0) &&
626 (export->ranges[i].start < export->n_pages) &&
627 (export->ranges[i].end >= export->n_pages)) {
628 export->ranges[i].end = export->n_pages - 1;
629 export->ranges[num_of_correct_ranges] = export->ranges[i];
630 num_of_correct_ranges++;
631 } else if ((export->ranges[i].end >= 0) &&
632 (export->ranges[i].end < export->n_pages) &&
633 (export->ranges[i].start < 0)) {
634 export->ranges[i].start = 0;
635 export->ranges[num_of_correct_ranges] = export->ranges[i];
636 num_of_correct_ranges++;
639 n_pages = export->ranges[i].end - export->ranges[i].start + 1;
640 if (export->page_set == GTK_PAGE_SET_ALL) {
641 n_pages_to_print += n_pages;
642 } else if (n_pages % 2 == 0) {
643 n_pages_to_print += n_pages / 2;
644 } else if (export->page_set == GTK_PAGE_SET_EVEN) {
645 if (n_pages==1 && export->ranges[i].start % 2 == 0)
648 n_pages_to_print += export->ranges[i].start % 2 == 0 ?
649 n_pages / 2 : (n_pages / 2) + 1;
650 } else if (export->page_set == GTK_PAGE_SET_ODD) {
651 if (n_pages==1 && export->ranges[i].start % 2 != 0)
654 n_pages_to_print += export->ranges[i].start % 2 == 0 ?
655 (n_pages / 2) + 1 : n_pages / 2;
659 if (null_flag && !n_pages_to_print) {
662 export->n_ranges = num_of_correct_ranges;
663 export->n_pages_to_print = n_pages_to_print;
669 get_first_and_last_page (EvPrintOperationExport *export,
674 gint first_page = G_MAXINT;
675 gint last_page = G_MININT;
676 gint max_page = export->n_pages - 1;
678 if (export->n_ranges == 0) {
685 for (i = 0; i < export->n_ranges; i++) {
686 if (export->ranges[i].start < first_page)
687 first_page = export->ranges[i].start;
688 if (export->ranges[i].end > last_page)
689 last_page = export->ranges[i].end;
692 *first = MAX (0, first_page);
693 *last = MIN (max_page, last_page);
697 export_print_inc_page (EvPrintOperationExport *export)
700 export->page += export->inc;
702 /* note: when NOT collating, page_count is increased in export_print_page */
703 if (export->collate) {
704 export->page_count++;
705 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
708 if (export->page == export->end) {
709 export->range += export->inc;
710 if (export->range == -1 || export->range == export->n_ranges) {
711 export->uncollated++;
713 /* when printing multiple collated copies & multiple pages per sheet we want to
714 * prevent the next copy bleeding into the last sheet of the previous one
715 * we've reached the last range to be printed now, so this is the time to do it */
716 if (export->pages_per_sheet > 1 && export->collate == 1 &&
717 (export->page_count - 1) % export->pages_per_sheet != 0) {
719 EvPrintOperation *op = EV_PRINT_OPERATION (export);
720 ev_document_doc_mutex_lock ();
722 /* keep track of all blanks but only actualise those
723 * which are in the current odd / even sheet set */
725 export->page_count += export->pages_per_sheet - (export->page_count - 1) % export->pages_per_sheet;
726 if (export->page_set == GTK_PAGE_SET_ALL ||
727 (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) ||
728 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1) ) {
729 ev_file_exporter_end_page (EV_FILE_EXPORTER (op->document));
731 ev_document_doc_mutex_unlock ();
732 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
735 if (export->uncollated == export->uncollated_copies)
738 export->range = export->inc < 0 ? export->n_ranges - 1 : 0;
741 export->page = export->start;
744 /* in/decrement the page number until we reach the first page on the next EVEN or ODD sheet
745 * if we're not collating, we have to make sure that this is done only once! */
746 } while ( export->collate == 1 &&
747 ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 1) ||
748 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 0)));
754 ev_print_operation_export_clear_temp_file (EvPrintOperationExport *export)
756 if (!export->temp_file)
759 g_unlink (export->temp_file);
760 g_free (export->temp_file);
761 export->temp_file = NULL;
765 ev_print_operation_export_run_next (EvPrintOperationExport *export)
767 EvPrintOperation *op = EV_PRINT_OPERATION (export);
768 EvPrintOperation *next;
769 EvDocument *document;
771 /* First pop the current job */
772 document = op->document;
773 ev_print_queue_pop (document);
775 next = ev_print_queue_peek (document);
777 ev_print_operation_export_begin (EV_PRINT_OPERATION_EXPORT (next));
781 gtk_print_job_finished (GtkPrintJob *print_job,
782 EvPrintOperationExport *export,
785 EvPrintOperation *op = EV_PRINT_OPERATION (export);
788 g_set_error_literal (&export->error,
790 GTK_PRINT_ERROR_GENERAL,
792 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
794 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
797 ev_print_operation_export_clear_temp_file (export);
798 g_object_unref (print_job);
800 ev_print_operation_export_run_next (export);
804 export_print_done (EvPrintOperationExport *export)
806 EvPrintOperation *op = EV_PRINT_OPERATION (export);
807 GtkPrintSettings *settings;
808 EvFileExporterCapabilities capabilities;
809 GError *error = NULL;
811 g_assert (export->temp_file != NULL);
813 /* Some printers take into account some print settings,
814 * and others don't. However we have exported the document
815 * to a ps or pdf file according to such print settings. So,
816 * we want to send the exported file to printer with those
817 * settings set to default values.
819 settings = gtk_print_settings_copy (export->print_settings);
820 capabilities = ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document));
822 gtk_print_settings_set_page_ranges (settings, NULL, 0);
823 gtk_print_settings_set_print_pages (settings, GTK_PRINT_PAGES_ALL);
824 if (capabilities & EV_FILE_EXPORTER_CAN_COPIES)
825 gtk_print_settings_set_n_copies (settings, 1);
826 if (capabilities & EV_FILE_EXPORTER_CAN_PAGE_SET)
827 gtk_print_settings_set_page_set (settings, GTK_PAGE_SET_ALL);
828 if (capabilities & EV_FILE_EXPORTER_CAN_SCALE)
829 gtk_print_settings_set_scale (settings, 1.0);
830 if (capabilities & EV_FILE_EXPORTER_CAN_COLLATE)
831 gtk_print_settings_set_collate (settings, FALSE);
832 if (capabilities & EV_FILE_EXPORTER_CAN_REVERSE)
833 gtk_print_settings_set_reverse (settings, FALSE);
834 if (capabilities & EV_FILE_EXPORTER_CAN_NUMBER_UP) {
835 gtk_print_settings_set_number_up (settings, 1);
836 gtk_print_settings_set_int (settings, "cups-"GTK_PRINT_SETTINGS_NUMBER_UP, 1);
839 if (export->print_preview) {
843 gchar *print_settings_file = NULL;
845 key_file = g_key_file_new ();
847 gtk_print_settings_to_key_file (settings, key_file, NULL);
848 gtk_page_setup_to_key_file (export->page_setup, key_file, NULL);
849 g_key_file_set_string (key_file, "Print Job", "title", export->job_name);
851 data = g_key_file_to_data (key_file, &data_len, &error);
855 fd = g_file_open_tmp ("print-settingsXXXXXX", &print_settings_file, &error);
857 g_file_set_contents (print_settings_file, data, data_len, &error);
863 g_key_file_free (key_file);
869 gchar *quoted_filename;
870 gchar *quoted_settings_filename;
872 quoted_filename = g_shell_quote (export->temp_file);
873 quoted_settings_filename = g_shell_quote (print_settings_file);
874 cmd = g_strdup_printf ("evince-previewer --unlink-tempfile --print-settings %s %s",
875 quoted_settings_filename, quoted_filename);
877 g_shell_parse_argv (cmd, &argc, &argv, &error);
879 g_free (quoted_filename);
880 g_free (quoted_settings_filename);
884 gdk_spawn_on_screen (gtk_window_get_screen (export->parent_window),
895 if (print_settings_file)
896 g_unlink (print_settings_file);
897 g_free (print_settings_file);
899 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
900 /* temp_file will be deleted by the previewer */
902 ev_print_operation_export_run_next (export);
907 job = gtk_print_job_new (export->job_name,
911 gtk_print_job_set_source_file (job, export->temp_file, &error);
913 gtk_print_job_send (job,
914 (GtkPrintJobCompleteFunc)gtk_print_job_finished,
915 g_object_ref (export),
916 (GDestroyNotify)g_object_unref);
919 g_object_unref (settings);
922 g_set_error_literal (&export->error,
924 GTK_PRINT_ERROR_GENERAL,
926 g_error_free (error);
927 ev_print_operation_export_clear_temp_file (export);
928 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
930 ev_print_operation_export_run_next (export);
935 export_print_page_idle_finished (EvPrintOperationExport *export)
941 export_job_finished (EvJobExport *job,
942 EvPrintOperationExport *export)
944 EvPrintOperation *op = EV_PRINT_OPERATION (export);
946 if (export->pages_per_sheet == 1 ||
947 ( export->page_count % export->pages_per_sheet == 0 &&
948 ( export->page_set == GTK_PAGE_SET_ALL ||
949 ( export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0 ) ||
950 ( export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1 ) ) ) ) {
952 ev_document_doc_mutex_lock ();
953 ev_file_exporter_end_page (EV_FILE_EXPORTER (op->document));
954 ev_document_doc_mutex_unlock ();
958 export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
959 (GSourceFunc)export_print_page,
961 (GDestroyNotify)export_print_page_idle_finished);
965 export_job_cancelled (EvJobExport *job,
966 EvPrintOperationExport *export)
968 export_cancel (export);
972 export_cancel (EvPrintOperationExport *export)
974 EvPrintOperation *op = EV_PRINT_OPERATION (export);
976 if (export->idle_id > 0)
977 g_source_remove (export->idle_id);
980 if (export->job_export) {
981 g_signal_handlers_disconnect_by_func (export->job_export,
984 g_signal_handlers_disconnect_by_func (export->job_export,
985 export_job_cancelled,
987 g_object_unref (export->job_export);
988 export->job_export = NULL;
991 if (export->fd != -1) {
996 ev_print_operation_export_clear_temp_file (export);
998 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
1000 ev_print_operation_export_run_next (export);
1004 update_progress (EvPrintOperationExport *export)
1006 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1008 ev_print_operation_update_status (op, export->total,
1009 export->n_pages_to_print,
1010 export->total / (gdouble)export->n_pages_to_print);
1014 export_print_page (EvPrintOperationExport *export)
1016 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1018 if (!export->temp_file)
1019 return FALSE; /* cancelled */
1024 /* note: when collating, page_count is increased in export_print_inc_page */
1025 if (!export->collate) {
1026 export->page_count++;
1027 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
1030 if (export->collated == export->collated_copies) {
1031 export->collated = 0;
1032 if (!export_print_inc_page (export)) {
1033 ev_document_doc_mutex_lock ();
1034 ev_file_exporter_end (EV_FILE_EXPORTER (op->document));
1035 ev_document_doc_mutex_unlock ();
1039 update_progress (export);
1040 export_print_done (export);
1046 /* we're not collating and we've reached a sheet from the wrong sheet set */
1047 if (!export->collate &&
1048 ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) ||
1049 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1))) {
1052 export->page_count++;
1054 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
1056 if (export->collated == export->collated_copies) {
1057 export->collated = 0;
1059 if (!export_print_inc_page (export)) {
1060 ev_document_doc_mutex_lock ();
1061 ev_file_exporter_end (EV_FILE_EXPORTER (op->document));
1062 ev_document_doc_mutex_unlock ();
1067 update_progress (export);
1069 export_print_done (export);
1074 } while ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) ||
1075 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1));
1079 if (export->pages_per_sheet == 1 ||
1080 (export->page_count % export->pages_per_sheet == 1 &&
1081 (export->page_set == GTK_PAGE_SET_ALL ||
1082 (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) ||
1083 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1)))) {
1084 ev_document_doc_mutex_lock ();
1085 ev_file_exporter_begin_page (EV_FILE_EXPORTER (op->document));
1086 ev_document_doc_mutex_unlock ();
1089 if (!export->job_export) {
1090 export->job_export = ev_job_export_new (op->document);
1091 g_signal_connect (export->job_export, "finished",
1092 G_CALLBACK (export_job_finished),
1094 g_signal_connect (export->job_export, "cancelled",
1095 G_CALLBACK (export_job_cancelled),
1099 ev_job_export_set_page (EV_JOB_EXPORT (export->job_export), export->page);
1100 ev_job_scheduler_push_job (export->job_export, EV_JOB_PRIORITY_NONE);
1102 update_progress (export);
1108 ev_print_operation_export_begin (EvPrintOperationExport *export)
1110 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1112 if (!export->temp_file)
1113 return; /* cancelled */
1115 ev_document_doc_mutex_lock ();
1116 ev_file_exporter_begin (EV_FILE_EXPORTER (op->document), &export->fc);
1117 ev_document_doc_mutex_unlock ();
1119 export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1120 (GSourceFunc)export_print_page,
1122 (GDestroyNotify)export_print_page_idle_finished);
1126 ev_print_operation_export_print_dialog_response_cb (GtkDialog *dialog,
1128 EvPrintOperationExport *export)
1130 GtkPrintPages print_pages;
1131 GtkPrintSettings *print_settings;
1132 GtkPageSetup *page_setup;
1133 GtkPrinter *printer;
1139 const gchar *file_format;
1141 GError *error = NULL;
1142 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1144 if (response != GTK_RESPONSE_OK &&
1145 response != GTK_RESPONSE_APPLY) {
1146 gtk_widget_destroy (GTK_WIDGET (dialog));
1147 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
1152 export->print_preview = (response == GTK_RESPONSE_APPLY);
1154 printer = gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (dialog));
1155 ev_print_operation_export_set_printer (export, printer);
1157 print_settings = gtk_print_unix_dialog_get_settings (GTK_PRINT_UNIX_DIALOG (dialog));
1158 ev_print_operation_export_set_print_settings (op, print_settings);
1160 page_setup = gtk_print_unix_dialog_get_page_setup (GTK_PRINT_UNIX_DIALOG (dialog));
1161 ev_print_operation_export_set_default_page_setup (op, page_setup);
1163 if (!gtk_printer_accepts_ps (export->printer)) {
1164 gtk_widget_destroy (GTK_WIDGET (dialog));
1166 g_set_error_literal (&export->error,
1168 GTK_PRINT_ERROR_GENERAL,
1169 _("Printing is not supported on this printer."));
1170 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1175 file_format = gtk_print_settings_get (print_settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
1177 filename = g_strdup_printf ("evince_print.%s.XXXXXX", file_format != NULL ? file_format : "");
1178 export->fd = g_file_open_tmp (filename, &export->temp_file, &error);
1180 if (export->fd <= -1) {
1181 gtk_widget_destroy (GTK_WIDGET (dialog));
1183 g_set_error_literal (&export->error,
1185 GTK_PRINT_ERROR_GENERAL,
1187 g_error_free (error);
1188 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1193 export->current_page = gtk_print_unix_dialog_get_current_page (GTK_PRINT_UNIX_DIALOG (dialog));
1194 export->page_set = gtk_print_settings_get_page_set (print_settings);
1195 print_pages = gtk_print_settings_get_print_pages (print_settings);
1197 switch (print_pages) {
1198 case GTK_PRINT_PAGES_CURRENT:
1199 export->ranges = &export->one_range;
1201 export->ranges[0].start = export->current_page;
1202 export->ranges[0].end = export->current_page;
1203 export->n_ranges = 1;
1206 case GTK_PRINT_PAGES_RANGES: {
1209 export->ranges = gtk_print_settings_get_page_ranges (print_settings, &export->n_ranges);
1210 for (i = 0; i < export->n_ranges; i++)
1211 if (export->ranges[i].end == -1 || export->ranges[i].end >= export->n_pages)
1212 export->ranges[i].end = export->n_pages - 1;
1216 g_warning ("Unsupported print pages setting\n");
1217 case GTK_PRINT_PAGES_ALL:
1218 export->ranges = &export->one_range;
1220 export->ranges[0].start = 0;
1221 export->ranges[0].end = export->n_pages - 1;
1222 export->n_ranges = 1;
1227 if (export->n_ranges < 1 || !clamp_ranges (export)) {
1228 GtkWidget *message_dialog;
1230 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
1232 GTK_MESSAGE_WARNING,
1234 "%s", _("Invalid page selection"));
1235 gtk_window_set_title (GTK_WINDOW (message_dialog), _("Warning"));
1236 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (message_dialog),
1237 "%s", _("Your print range selection does not include any page"));
1238 g_signal_connect (message_dialog, "response",
1239 G_CALLBACK (gtk_widget_destroy),
1241 gtk_widget_show (message_dialog);
1244 } else ev_print_operation_update_status (op, -1, -1, 0.0);
1246 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1247 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1248 scale = gtk_print_settings_get_scale (print_settings) * 0.01;
1254 export->pages_per_sheet = MAX (1, gtk_print_settings_get_number_up (print_settings));
1256 export->copies = gtk_print_settings_get_n_copies (print_settings);
1257 export->collate = gtk_print_settings_get_collate (print_settings);
1258 export->reverse = gtk_print_settings_get_reverse (print_settings);
1260 if (export->collate) {
1261 export->uncollated_copies = export->copies;
1262 export->collated_copies = 1;
1264 export->uncollated_copies = 1;
1265 export->collated_copies = export->copies;
1268 if (export->reverse) {
1269 export->range = export->n_ranges - 1;
1275 find_range (export);
1277 export->page = export->start - export->inc;
1278 export->collated = export->collated_copies - 1;
1280 get_first_and_last_page (export, &first_page, &last_page);
1282 export->fc.format = file_format && g_ascii_strcasecmp (file_format, "pdf") == 0 ?
1283 EV_FILE_FORMAT_PDF : EV_FILE_FORMAT_PS;
1284 export->fc.filename = export->temp_file;
1285 export->fc.first_page = MIN (first_page, last_page);
1286 export->fc.last_page = MAX (first_page, last_page);
1287 export->fc.paper_width = width;
1288 export->fc.paper_height = height;
1289 export->fc.duplex = FALSE;
1290 export->fc.pages_per_sheet = export->pages_per_sheet;
1292 if (ev_print_queue_is_empty (op->document))
1293 ev_print_operation_export_begin (export);
1295 ev_print_queue_push (op);
1297 g_signal_emit (op, signals[BEGIN_PRINT], 0);
1299 gtk_widget_destroy (GTK_WIDGET (dialog));
1303 ev_print_operation_export_run (EvPrintOperation *op,
1306 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1308 GtkPrintCapabilities capabilities;
1310 ev_print_queue_init ();
1312 export->parent_window = parent;
1313 export->error = NULL;
1315 dialog = gtk_print_unix_dialog_new (_("Print"), parent);
1316 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1318 capabilities = GTK_PRINT_CAPABILITY_PREVIEW |
1319 ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document));
1320 gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG (dialog),
1322 #if GTK_CHECK_VERSION (2, 17, 4)
1323 gtk_print_unix_dialog_set_embed_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1324 export->embed_page_setup);
1327 gtk_print_unix_dialog_set_current_page (GTK_PRINT_UNIX_DIALOG (dialog),
1328 export->current_page);
1330 gtk_print_unix_dialog_set_settings (GTK_PRINT_UNIX_DIALOG (dialog),
1331 export->print_settings);
1333 if (export->page_setup)
1334 gtk_print_unix_dialog_set_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1335 export->page_setup);
1337 g_signal_connect (dialog, "response",
1338 G_CALLBACK (ev_print_operation_export_print_dialog_response_cb),
1341 gtk_window_present (GTK_WINDOW (dialog));
1345 ev_print_operation_export_cancel (EvPrintOperation *op)
1347 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1349 if (export->job_export &&
1350 !ev_job_is_finished (export->job_export)) {
1351 ev_job_cancel (export->job_export);
1353 export_cancel (export);
1358 ev_print_operation_export_get_error (EvPrintOperation *op,
1361 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1363 g_propagate_error (error, export->error);
1364 export->error = NULL;
1368 ev_print_operation_export_set_embed_page_setup (EvPrintOperation *op,
1371 #if GTK_CHECK_VERSION (2, 17, 4)
1372 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1374 export->embed_page_setup = embed;
1379 ev_print_operation_export_get_embed_page_setup (EvPrintOperation *op)
1381 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1383 return export->embed_page_setup;
1387 ev_print_operation_export_finalize (GObject *object)
1389 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (object);
1391 if (export->idle_id > 0) {
1392 g_source_remove (export->idle_id);
1393 export->idle_id = 0;
1396 if (export->fd != -1) {
1401 if (export->ranges) {
1402 if (export->ranges != &export->one_range)
1403 g_free (export->ranges);
1404 export->ranges = NULL;
1405 export->n_ranges = 0;
1408 if (export->temp_file) {
1409 g_free (export->temp_file);
1410 export->temp_file = NULL;
1413 if (export->job_name) {
1414 g_free (export->job_name);
1415 export->job_name = NULL;
1418 if (export->job_export) {
1419 if (!ev_job_is_finished (export->job_export))
1420 ev_job_cancel (export->job_export);
1421 g_signal_handlers_disconnect_by_func (export->job_export,
1422 export_job_finished,
1424 g_signal_handlers_disconnect_by_func (export->job_export,
1425 export_job_cancelled,
1427 g_object_unref (export->job_export);
1428 export->job_export = NULL;
1431 if (export->error) {
1432 g_error_free (export->error);
1433 export->error = NULL;
1436 if (export->print_settings) {
1437 g_object_unref (export->print_settings);
1438 export->print_settings = NULL;
1441 if (export->page_setup) {
1442 g_object_unref (export->page_setup);
1443 export->page_setup = NULL;
1446 if (export->printer) {
1447 g_object_unref (export->printer);
1448 export->printer = NULL;
1451 (* G_OBJECT_CLASS (ev_print_operation_export_parent_class)->finalize) (object);
1455 ev_print_operation_export_init (EvPrintOperationExport *export)
1457 /* sheets are counted from 1 to be physical */
1462 ev_print_operation_export_constructor (GType type,
1463 guint n_construct_properties,
1464 GObjectConstructParam *construct_params)
1467 EvPrintOperationExport *export;
1468 EvPrintOperation *op;
1470 object = G_OBJECT_CLASS (ev_print_operation_export_parent_class)->constructor (type,
1471 n_construct_properties,
1473 export = EV_PRINT_OPERATION_EXPORT (object);
1474 op = EV_PRINT_OPERATION (object);
1475 export->n_pages = ev_document_get_n_pages (op->document);
1481 ev_print_operation_export_class_init (EvPrintOperationExportClass *klass)
1483 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
1484 EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass);
1486 ev_print_op_class->set_current_page = ev_print_operation_export_set_current_page;
1487 ev_print_op_class->set_print_settings = ev_print_operation_export_set_print_settings;
1488 ev_print_op_class->get_print_settings = ev_print_operation_export_get_print_settings;
1489 ev_print_op_class->set_default_page_setup = ev_print_operation_export_set_default_page_setup;
1490 ev_print_op_class->get_default_page_setup = ev_print_operation_export_get_default_page_setup;
1491 ev_print_op_class->set_job_name = ev_print_operation_export_set_job_name;
1492 ev_print_op_class->get_job_name = ev_print_operation_export_get_job_name;
1493 ev_print_op_class->run = ev_print_operation_export_run;
1494 ev_print_op_class->cancel = ev_print_operation_export_cancel;
1495 ev_print_op_class->get_error = ev_print_operation_export_get_error;
1496 ev_print_op_class->set_embed_page_setup = ev_print_operation_export_set_embed_page_setup;
1497 ev_print_op_class->get_embed_page_setup = ev_print_operation_export_get_embed_page_setup;
1499 g_object_class->constructor = ev_print_operation_export_constructor;
1500 g_object_class->finalize = ev_print_operation_export_finalize;
1503 #endif /* GTKUNIXPRINT_ENABLED */
1505 #if GTK_CHECK_VERSION (2, 17, 1)
1506 /* Print to cairo interface */
1507 #define EV_TYPE_PRINT_OPERATION_PRINT (ev_print_operation_print_get_type())
1508 #define EV_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrint))
1509 #define EV_PRINT_OPERATION_PRINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrintClass))
1510 #define EV_IS_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_PRINT))
1512 typedef struct _EvPrintOperationPrint EvPrintOperationPrint;
1513 typedef struct _EvPrintOperationPrintClass EvPrintOperationPrintClass;
1515 static GType ev_print_operation_print_get_type (void) G_GNUC_CONST;
1517 struct _EvPrintOperationPrint {
1518 EvPrintOperation parent;
1520 GtkPrintOperation *op;
1521 gint n_pages_to_print;
1527 struct _EvPrintOperationPrintClass {
1528 EvPrintOperationClass parent_class;
1531 G_DEFINE_TYPE (EvPrintOperationPrint, ev_print_operation_print, EV_TYPE_PRINT_OPERATION)
1534 ev_print_operation_print_set_current_page (EvPrintOperation *op,
1537 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1539 gtk_print_operation_set_current_page (print->op, current_page);
1543 ev_print_operation_print_set_print_settings (EvPrintOperation *op,
1544 GtkPrintSettings *print_settings)
1546 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1548 gtk_print_operation_set_print_settings (print->op, print_settings);
1551 static GtkPrintSettings *
1552 ev_print_operation_print_get_print_settings (EvPrintOperation *op)
1554 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1556 return gtk_print_operation_get_print_settings (print->op);
1560 ev_print_operation_print_set_default_page_setup (EvPrintOperation *op,
1561 GtkPageSetup *page_setup)
1563 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1565 gtk_print_operation_set_default_page_setup (print->op, page_setup);
1568 static GtkPageSetup *
1569 ev_print_operation_print_get_default_page_setup (EvPrintOperation *op)
1571 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1573 return gtk_print_operation_get_default_page_setup (print->op);
1577 ev_print_operation_print_set_job_name (EvPrintOperation *op,
1578 const gchar *job_name)
1580 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1582 g_free (print->job_name);
1583 print->job_name = g_strdup (job_name);
1585 gtk_print_operation_set_job_name (print->op, print->job_name);
1588 static const gchar *
1589 ev_print_operation_print_get_job_name (EvPrintOperation *op)
1591 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1593 if (!print->job_name) {
1596 g_object_get (print->op, "job_name", &name, NULL);
1597 print->job_name = name;
1600 return print->job_name;
1604 ev_print_operation_print_run (EvPrintOperation *op,
1607 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1609 gtk_print_operation_run (print->op,
1610 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1615 ev_print_operation_print_cancel (EvPrintOperation *op)
1617 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1619 gtk_print_operation_cancel (print->op);
1623 ev_print_operation_print_get_error (EvPrintOperation *op,
1626 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1628 gtk_print_operation_get_error (print->op, error);
1632 ev_print_operation_print_set_embed_page_setup (EvPrintOperation *op,
1635 #if GTK_CHECK_VERSION (2, 17, 4)
1636 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1638 gtk_print_operation_set_embed_page_setup (print->op, embed);
1643 ev_print_operation_print_get_embed_page_setup (EvPrintOperation *op)
1645 #if GTK_CHECK_VERSION (2, 17, 4)
1646 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1648 return gtk_print_operation_get_embed_page_setup (print->op);
1655 ev_print_operation_print_begin_print (EvPrintOperationPrint *print,
1656 GtkPrintContext *context)
1658 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1661 n_pages = ev_document_get_n_pages (op->document);
1662 gtk_print_operation_set_n_pages (print->op, n_pages);
1663 ev_print_operation_update_status (op, -1, n_pages, 0);
1665 g_signal_emit (op, signals[BEGIN_PRINT], 0);
1669 ev_print_operation_print_done (EvPrintOperationPrint *print,
1670 GtkPrintOperationResult result)
1672 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1674 ev_print_operation_update_status (op, 0, print->n_pages_to_print, 1.0);
1676 g_signal_emit (op, signals[DONE], 0, result);
1680 ev_print_operation_print_status_changed (EvPrintOperationPrint *print)
1682 #ifdef HAVE_GTK_PRINT_OPERATION_GET_N_PAGES_TO_PRINT
1683 GtkPrintStatus status;
1685 status = gtk_print_operation_get_status (print->op);
1686 if (status == GTK_PRINT_STATUS_GENERATING_DATA)
1687 print->n_pages_to_print = gtk_print_operation_get_n_pages_to_print (print->op);
1692 print_job_finished (EvJobPrint *job,
1693 EvPrintOperationPrint *print)
1695 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1697 gtk_print_operation_draw_page_finish (print->op);
1698 #ifdef HAVE_GTK_PRINT_OPERATION_GET_N_PAGES_TO_PRINT
1700 ev_print_operation_update_status (op, print->total,
1701 print->n_pages_to_print,
1702 print->total / (gdouble)print->n_pages_to_print);
1704 ev_job_print_set_cairo (job, NULL);
1708 print_job_cancelled (EvJobPrint *job,
1709 EvPrintOperationPrint *print)
1711 gtk_print_operation_cancel (print->op);
1715 ev_print_operation_print_request_page_setup (EvPrintOperationPrint *print,
1716 GtkPrintContext *context,
1718 GtkPageSetup *setup)
1720 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1721 gdouble width, height;
1723 ev_document_get_page_size (op->document, page_nr,
1726 gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
1728 gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_PORTRAIT);
1732 ev_print_operation_print_draw_page (EvPrintOperationPrint *print,
1733 GtkPrintContext *context,
1736 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1738 gdouble cr_width, cr_height;
1739 gdouble width, height, scale;
1741 gtk_print_operation_set_defer_drawing (print->op);
1743 if (!print->job_print) {
1744 print->job_print = ev_job_print_new (op->document);
1745 g_signal_connect (G_OBJECT (print->job_print), "finished",
1746 G_CALLBACK (print_job_finished),
1748 g_signal_connect (G_OBJECT (print->job_print), "cancelled",
1749 G_CALLBACK (print_job_cancelled),
1753 ev_job_print_set_page (EV_JOB_PRINT (print->job_print), page);
1755 cr = gtk_print_context_get_cairo_context (context);
1756 cr_width = gtk_print_context_get_width (context);
1757 cr_height = gtk_print_context_get_height (context);
1758 ev_document_get_page_size (op->document, page,
1760 if (cr_width / width < cr_height / height)
1761 scale = cr_width / width;
1763 scale = cr_height / height;
1764 cairo_scale (cr, scale, scale);
1766 ev_job_print_set_cairo (EV_JOB_PRINT (print->job_print), cr);
1767 ev_job_scheduler_push_job (print->job_print, EV_JOB_PRIORITY_NONE);
1771 ev_print_operation_print_finalize (GObject *object)
1773 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (object);
1776 g_object_unref (print->op);
1780 if (print->job_name) {
1781 g_free (print->job_name);
1782 print->job_name = NULL;
1785 if (print->job_print) {
1786 if (!ev_job_is_finished (print->job_print))
1787 ev_job_cancel (print->job_print);
1788 g_signal_handlers_disconnect_by_func (print->job_print,
1791 g_signal_handlers_disconnect_by_func (print->job_print,
1792 print_job_cancelled,
1794 g_object_unref (print->job_print);
1795 print->job_print = NULL;
1798 (* G_OBJECT_CLASS (ev_print_operation_print_parent_class)->finalize) (object);
1802 ev_print_operation_print_init (EvPrintOperationPrint *print)
1804 print->op = gtk_print_operation_new ();
1805 g_signal_connect_swapped (print->op, "begin_print",
1806 G_CALLBACK (ev_print_operation_print_begin_print),
1808 g_signal_connect_swapped (print->op, "done",
1809 G_CALLBACK (ev_print_operation_print_done),
1811 g_signal_connect_swapped (print->op, "draw_page",
1812 G_CALLBACK (ev_print_operation_print_draw_page),
1814 g_signal_connect_swapped (print->op, "status_changed",
1815 G_CALLBACK (ev_print_operation_print_status_changed),
1817 g_signal_connect_swapped (print->op, "request_page_setup",
1818 G_CALLBACK (ev_print_operation_print_request_page_setup),
1820 gtk_print_operation_set_allow_async (print->op, TRUE);
1824 ev_print_operation_print_class_init (EvPrintOperationPrintClass *klass)
1826 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
1827 EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass);
1829 ev_print_op_class->set_current_page = ev_print_operation_print_set_current_page;
1830 ev_print_op_class->set_print_settings = ev_print_operation_print_set_print_settings;
1831 ev_print_op_class->get_print_settings = ev_print_operation_print_get_print_settings;
1832 ev_print_op_class->set_default_page_setup = ev_print_operation_print_set_default_page_setup;
1833 ev_print_op_class->get_default_page_setup = ev_print_operation_print_get_default_page_setup;
1834 ev_print_op_class->set_job_name = ev_print_operation_print_set_job_name;
1835 ev_print_op_class->get_job_name = ev_print_operation_print_get_job_name;
1836 ev_print_op_class->run = ev_print_operation_print_run;
1837 ev_print_op_class->cancel = ev_print_operation_print_cancel;
1838 ev_print_op_class->get_error = ev_print_operation_print_get_error;
1839 ev_print_op_class->set_embed_page_setup = ev_print_operation_print_set_embed_page_setup;
1840 ev_print_op_class->get_embed_page_setup = ev_print_operation_print_get_embed_page_setup;
1842 g_object_class->finalize = ev_print_operation_print_finalize;
1844 #endif /* GTK_CHECK_VERSION (2, 17, 1) */
1846 gboolean ev_print_operation_exists_for_document (EvDocument *document)
1848 #if GTKUNIXPRINT_ENABLED
1849 #if GTK_CHECK_VERSION (2, 17, 1)
1850 return (EV_IS_FILE_EXPORTER(document) || EV_IS_DOCUMENT_PRINT(document));
1852 return EV_IS_FILE_EXPORTER(document);
1854 #else /* ! GTKUNIXPRINT_ENABLED */
1855 #if GTK_CHECK_VERSION (2, 17, 1)
1856 return EV_IS_DOCUMENT_PRINT(document);
1860 #endif /* GTKUNIXPRINT_ENABLED */
1863 /* Factory method */
1865 ev_print_operation_new (EvDocument *document)
1867 EvPrintOperation *op = NULL;
1869 g_return_val_if_fail (ev_print_operation_exists_for_document (document), NULL);
1871 #if GTK_CHECK_VERSION (2, 17, 1)
1872 if (EV_IS_DOCUMENT_PRINT (document))
1873 op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_PRINT,
1874 "document", document, NULL));
1877 #if GTKUNIXPRINT_ENABLED
1878 op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_EXPORT,
1879 "document", document, NULL));