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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
290 g_return_if_fail (EV_IS_PRINT_OPERATION (op));
292 class->set_embed_page_setup (op, embed);
296 ev_print_operation_get_embed_page_setup (EvPrintOperation *op)
298 EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
300 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), FALSE);
302 return class->get_embed_page_setup (op);
306 ev_print_operation_get_status (EvPrintOperation *op)
308 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
310 return op->status ? op->status : "";
314 ev_print_operation_get_progress (EvPrintOperation *op)
316 g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), 0.0);
322 ev_print_operation_update_status (EvPrintOperation *op,
327 if (op->status && op->progress == progress)
334 op->status = g_strdup (_("Preparing to print…"));
335 } else if (page > n_pages) {
336 op->status = g_strdup (_("Finishing…"));
338 op->status = g_strdup_printf (_("Printing page %d of %d…"),
342 op->progress = MIN (1.0, progress);
344 g_signal_emit (op, signals[STATUS_CHANGED], 0);
347 #if GTKUNIXPRINT_ENABLED
349 /* Export interface */
350 #define EV_TYPE_PRINT_OPERATION_EXPORT (ev_print_operation_export_get_type())
351 #define EV_PRINT_OPERATION_EXPORT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExport))
352 #define EV_PRINT_OPERATION_EXPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExportClass))
353 #define EV_IS_PRINT_OPERATION_EXPORT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_EXPORT))
355 typedef struct _EvPrintOperationExport EvPrintOperationExport;
356 typedef struct _EvPrintOperationExportClass EvPrintOperationExportClass;
358 static GType ev_print_operation_export_get_type (void) G_GNUC_CONST;
360 static void ev_print_operation_export_begin (EvPrintOperationExport *export);
361 static gboolean export_print_page (EvPrintOperationExport *export);
362 static void export_cancel (EvPrintOperationExport *export);
364 struct _EvPrintOperationExport {
365 EvPrintOperation parent;
367 GtkWindow *parent_window;
371 gboolean print_preview;
375 GtkPageSetup *page_setup;
376 GtkPrintSettings *print_settings;
381 gint pages_per_sheet;
385 gboolean embed_page_setup;
390 EvFileExporterContext fc;
391 gint n_pages_to_print;
392 gint uncollated_copies;
393 gint collated_copies;
394 gint uncollated, collated, total;
396 gint sheet, page_count;
398 gint range, n_ranges;
399 GtkPageRange *ranges;
400 GtkPageRange one_range;
402 gint page, start, end, inc;
405 struct _EvPrintOperationExportClass {
406 EvPrintOperationClass parent_class;
409 G_DEFINE_TYPE (EvPrintOperationExport, ev_print_operation_export, EV_TYPE_PRINT_OPERATION)
411 /* Internal print queue */
412 static GHashTable *print_queue = NULL;
415 queue_free (GQueue *queue)
417 g_queue_foreach (queue, (GFunc)g_object_unref, NULL);
418 g_queue_free (queue);
422 ev_print_queue_init (void)
424 if (G_UNLIKELY (print_queue == NULL)) {
425 print_queue = g_hash_table_new_full (g_direct_hash,
428 (GDestroyNotify)queue_free);
433 remove_document_queue (gpointer data,
437 g_hash_table_remove (print_queue, document);
441 ev_print_queue_is_empty (EvDocument *document)
445 queue = g_hash_table_lookup (print_queue, document);
446 return (!queue || g_queue_is_empty (queue));
450 ev_print_queue_push (EvPrintOperation *op)
454 queue = g_hash_table_lookup (print_queue, op->document);
456 queue = g_queue_new ();
457 g_hash_table_insert (print_queue,
460 g_object_weak_ref (G_OBJECT (op->document),
461 (GWeakNotify)remove_document_queue,
465 g_queue_push_head (queue, g_object_ref (op));
468 static EvPrintOperation *
469 ev_print_queue_pop (EvDocument *document)
471 EvPrintOperation *op;
474 queue = g_hash_table_lookup (print_queue, document);
475 if (!queue || g_queue_is_empty (queue))
478 op = g_queue_pop_tail (queue);
484 static EvPrintOperation *
485 ev_print_queue_peek (EvDocument *document)
489 queue = g_hash_table_lookup (print_queue, document);
490 if (!queue || g_queue_is_empty (queue))
493 return g_queue_peek_tail (queue);
497 ev_print_operation_export_set_current_page (EvPrintOperation *op,
500 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
502 g_return_if_fail (current_page < export->n_pages);
504 export->current_page = current_page;
508 ev_print_operation_export_set_print_settings (EvPrintOperation *op,
509 GtkPrintSettings *print_settings)
511 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
513 if (print_settings == export->print_settings)
516 g_object_ref (print_settings);
517 if (export->print_settings)
518 g_object_unref (export->print_settings);
519 export->print_settings = print_settings;
522 static GtkPrintSettings *
523 ev_print_operation_export_get_print_settings (EvPrintOperation *op)
525 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
527 return export->print_settings;
531 ev_print_operation_export_set_default_page_setup (EvPrintOperation *op,
532 GtkPageSetup *page_setup)
534 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
536 if (page_setup == export->page_setup)
539 g_object_ref (page_setup);
540 if (export->page_setup)
541 g_object_unref (export->page_setup);
542 export->page_setup = page_setup;
545 static GtkPageSetup *
546 ev_print_operation_export_get_default_page_setup (EvPrintOperation *op)
548 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
550 return export->page_setup;
554 ev_print_operation_export_set_job_name (EvPrintOperation *op,
555 const gchar *job_name)
557 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
559 g_free (export->job_name);
560 export->job_name = g_strdup (job_name);
564 ev_print_operation_export_get_job_name (EvPrintOperation *op)
566 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
568 return export->job_name;
572 ev_print_operation_export_set_printer (EvPrintOperationExport *export,
575 if (printer == export->printer)
578 g_object_ref (printer);
580 g_object_unref (export->printer);
581 export->printer = printer;
585 find_range (EvPrintOperationExport *export)
589 range = &export->ranges[export->range];
591 if (export->inc < 0) {
592 export->start = range->end;
593 export->end = range->start - 1;
595 export->start = range->start;
596 export->end = range->end + 1;
601 clamp_ranges (EvPrintOperationExport *export)
603 gint num_of_correct_ranges = 0;
604 gint n_pages_to_print = 0;
606 gboolean null_flag = FALSE;
608 for (i = 0; i < export->n_ranges; i++) {
611 if ((export->ranges[i].start >= 0) &&
612 (export->ranges[i].start < export->n_pages) &&
613 (export->ranges[i].end >= 0) &&
614 (export->ranges[i].end < export->n_pages)) {
615 export->ranges[num_of_correct_ranges] = export->ranges[i];
616 num_of_correct_ranges++;
617 } else if ((export->ranges[i].start >= 0) &&
618 (export->ranges[i].start < export->n_pages) &&
619 (export->ranges[i].end >= export->n_pages)) {
620 export->ranges[i].end = export->n_pages - 1;
621 export->ranges[num_of_correct_ranges] = export->ranges[i];
622 num_of_correct_ranges++;
623 } else if ((export->ranges[i].end >= 0) &&
624 (export->ranges[i].end < export->n_pages) &&
625 (export->ranges[i].start < 0)) {
626 export->ranges[i].start = 0;
627 export->ranges[num_of_correct_ranges] = export->ranges[i];
628 num_of_correct_ranges++;
631 n_pages = export->ranges[i].end - export->ranges[i].start + 1;
632 if (export->page_set == GTK_PAGE_SET_ALL) {
633 n_pages_to_print += n_pages;
634 } else if (n_pages % 2 == 0) {
635 n_pages_to_print += n_pages / 2;
636 } else if (export->page_set == GTK_PAGE_SET_EVEN) {
637 if (n_pages==1 && export->ranges[i].start % 2 == 0)
640 n_pages_to_print += export->ranges[i].start % 2 == 0 ?
641 n_pages / 2 : (n_pages / 2) + 1;
642 } else if (export->page_set == GTK_PAGE_SET_ODD) {
643 if (n_pages==1 && export->ranges[i].start % 2 != 0)
646 n_pages_to_print += export->ranges[i].start % 2 == 0 ?
647 (n_pages / 2) + 1 : n_pages / 2;
651 if (null_flag && !n_pages_to_print) {
654 export->n_ranges = num_of_correct_ranges;
655 export->n_pages_to_print = n_pages_to_print;
661 get_first_and_last_page (EvPrintOperationExport *export,
666 gint first_page = G_MAXINT;
667 gint last_page = G_MININT;
668 gint max_page = export->n_pages - 1;
670 if (export->n_ranges == 0) {
677 for (i = 0; i < export->n_ranges; i++) {
678 if (export->ranges[i].start < first_page)
679 first_page = export->ranges[i].start;
680 if (export->ranges[i].end > last_page)
681 last_page = export->ranges[i].end;
684 *first = MAX (0, first_page);
685 *last = MIN (max_page, last_page);
689 export_print_inc_page (EvPrintOperationExport *export)
692 export->page += export->inc;
694 /* note: when NOT collating, page_count is increased in export_print_page */
695 if (export->collate) {
696 export->page_count++;
697 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
700 if (export->page == export->end) {
701 export->range += export->inc;
702 if (export->range == -1 || export->range == export->n_ranges) {
703 export->uncollated++;
705 /* when printing multiple collated copies & multiple pages per sheet we want to
706 * prevent the next copy bleeding into the last sheet of the previous one
707 * we've reached the last range to be printed now, so this is the time to do it */
708 if (export->pages_per_sheet > 1 && export->collate == 1 &&
709 (export->page_count - 1) % export->pages_per_sheet != 0) {
711 EvPrintOperation *op = EV_PRINT_OPERATION (export);
712 ev_document_doc_mutex_lock ();
714 /* keep track of all blanks but only actualise those
715 * which are in the current odd / even sheet set */
717 export->page_count += export->pages_per_sheet - (export->page_count - 1) % export->pages_per_sheet;
718 if (export->page_set == GTK_PAGE_SET_ALL ||
719 (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) ||
720 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1) ) {
721 ev_file_exporter_end_page (EV_FILE_EXPORTER (op->document));
723 ev_document_doc_mutex_unlock ();
724 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
727 if (export->uncollated == export->uncollated_copies)
730 export->range = export->inc < 0 ? export->n_ranges - 1 : 0;
733 export->page = export->start;
736 /* in/decrement the page number until we reach the first page on the next EVEN or ODD sheet
737 * if we're not collating, we have to make sure that this is done only once! */
738 } while ( export->collate == 1 &&
739 ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 1) ||
740 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 0)));
746 ev_print_operation_export_clear_temp_file (EvPrintOperationExport *export)
748 if (!export->temp_file)
751 g_unlink (export->temp_file);
752 g_free (export->temp_file);
753 export->temp_file = NULL;
757 ev_print_operation_export_run_next (EvPrintOperationExport *export)
759 EvPrintOperation *op = EV_PRINT_OPERATION (export);
760 EvPrintOperation *next;
761 EvDocument *document;
763 /* First pop the current job */
764 document = op->document;
765 ev_print_queue_pop (document);
767 next = ev_print_queue_peek (document);
769 ev_print_operation_export_begin (EV_PRINT_OPERATION_EXPORT (next));
773 gtk_print_job_finished (GtkPrintJob *print_job,
774 EvPrintOperationExport *export,
777 EvPrintOperation *op = EV_PRINT_OPERATION (export);
780 g_set_error_literal (&export->error,
782 GTK_PRINT_ERROR_GENERAL,
784 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
786 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
789 ev_print_operation_export_clear_temp_file (export);
790 g_object_unref (print_job);
792 ev_print_operation_export_run_next (export);
796 export_print_done (EvPrintOperationExport *export)
798 EvPrintOperation *op = EV_PRINT_OPERATION (export);
799 GtkPrintSettings *settings;
800 EvFileExporterCapabilities capabilities;
801 GError *error = NULL;
803 g_assert (export->temp_file != NULL);
805 /* Some printers take into account some print settings,
806 * and others don't. However we have exported the document
807 * to a ps or pdf file according to such print settings. So,
808 * we want to send the exported file to printer with those
809 * settings set to default values.
811 settings = gtk_print_settings_copy (export->print_settings);
812 capabilities = ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document));
814 gtk_print_settings_set_page_ranges (settings, NULL, 0);
815 gtk_print_settings_set_print_pages (settings, GTK_PRINT_PAGES_ALL);
816 if (capabilities & EV_FILE_EXPORTER_CAN_COPIES)
817 gtk_print_settings_set_n_copies (settings, 1);
818 if (capabilities & EV_FILE_EXPORTER_CAN_PAGE_SET)
819 gtk_print_settings_set_page_set (settings, GTK_PAGE_SET_ALL);
820 if (capabilities & EV_FILE_EXPORTER_CAN_SCALE)
821 gtk_print_settings_set_scale (settings, 1.0);
822 if (capabilities & EV_FILE_EXPORTER_CAN_COLLATE)
823 gtk_print_settings_set_collate (settings, FALSE);
824 if (capabilities & EV_FILE_EXPORTER_CAN_REVERSE)
825 gtk_print_settings_set_reverse (settings, FALSE);
826 if (capabilities & EV_FILE_EXPORTER_CAN_NUMBER_UP) {
827 gtk_print_settings_set_number_up (settings, 1);
828 gtk_print_settings_set_int (settings, "cups-"GTK_PRINT_SETTINGS_NUMBER_UP, 1);
831 if (export->print_preview) {
835 gchar *print_settings_file = NULL;
837 key_file = g_key_file_new ();
839 gtk_print_settings_to_key_file (settings, key_file, NULL);
840 gtk_page_setup_to_key_file (export->page_setup, key_file, NULL);
841 g_key_file_set_string (key_file, "Print Job", "title", export->job_name);
843 data = g_key_file_to_data (key_file, &data_len, &error);
847 fd = g_file_open_tmp ("print-settingsXXXXXX", &print_settings_file, &error);
849 g_file_set_contents (print_settings_file, data, data_len, &error);
855 g_key_file_free (key_file);
859 gchar *quoted_filename;
860 gchar *quoted_settings_filename;
862 GdkAppLaunchContext *ctx;
864 quoted_filename = g_shell_quote (export->temp_file);
865 quoted_settings_filename = g_shell_quote (print_settings_file);
866 cmd = g_strdup_printf ("evince-previewer --unlink-tempfile --print-settings %s %s",
867 quoted_settings_filename, quoted_filename);
869 g_free (quoted_filename);
870 g_free (quoted_settings_filename);
872 app = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
875 ctx = gdk_display_get_app_launch_context (gtk_widget_get_display (GTK_WIDGET (export->parent_window)));
876 gdk_app_launch_context_set_screen (ctx, gtk_window_get_screen (export->parent_window));
878 g_app_info_launch (app, NULL, G_APP_LAUNCH_CONTEXT (ctx), &error);
880 g_object_unref (app);
881 g_object_unref (ctx);
888 if (print_settings_file)
889 g_unlink (print_settings_file);
890 g_free (print_settings_file);
892 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
893 /* temp_file will be deleted by the previewer */
895 ev_print_operation_export_run_next (export);
900 job = gtk_print_job_new (export->job_name,
904 gtk_print_job_set_source_file (job, export->temp_file, &error);
906 gtk_print_job_send (job,
907 (GtkPrintJobCompleteFunc)gtk_print_job_finished,
908 g_object_ref (export),
909 (GDestroyNotify)g_object_unref);
912 g_object_unref (settings);
915 g_set_error_literal (&export->error,
917 GTK_PRINT_ERROR_GENERAL,
919 g_error_free (error);
920 ev_print_operation_export_clear_temp_file (export);
921 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
923 ev_print_operation_export_run_next (export);
928 export_print_page_idle_finished (EvPrintOperationExport *export)
934 export_job_finished (EvJobExport *job,
935 EvPrintOperationExport *export)
937 EvPrintOperation *op = EV_PRINT_OPERATION (export);
939 if (export->pages_per_sheet == 1 ||
940 ( export->page_count % export->pages_per_sheet == 0 &&
941 ( export->page_set == GTK_PAGE_SET_ALL ||
942 ( export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0 ) ||
943 ( export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1 ) ) ) ) {
945 ev_document_doc_mutex_lock ();
946 ev_file_exporter_end_page (EV_FILE_EXPORTER (op->document));
947 ev_document_doc_mutex_unlock ();
951 export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
952 (GSourceFunc)export_print_page,
954 (GDestroyNotify)export_print_page_idle_finished);
958 export_job_cancelled (EvJobExport *job,
959 EvPrintOperationExport *export)
961 export_cancel (export);
965 export_cancel (EvPrintOperationExport *export)
967 EvPrintOperation *op = EV_PRINT_OPERATION (export);
969 if (export->idle_id > 0)
970 g_source_remove (export->idle_id);
973 if (export->job_export) {
974 g_signal_handlers_disconnect_by_func (export->job_export,
977 g_signal_handlers_disconnect_by_func (export->job_export,
978 export_job_cancelled,
980 g_object_unref (export->job_export);
981 export->job_export = NULL;
984 if (export->fd != -1) {
989 ev_print_operation_export_clear_temp_file (export);
991 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
993 ev_print_operation_export_run_next (export);
997 update_progress (EvPrintOperationExport *export)
999 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1001 ev_print_operation_update_status (op, export->total,
1002 export->n_pages_to_print,
1003 export->total / (gdouble)export->n_pages_to_print);
1007 export_print_page (EvPrintOperationExport *export)
1009 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1011 if (!export->temp_file)
1012 return FALSE; /* cancelled */
1017 /* note: when collating, page_count is increased in export_print_inc_page */
1018 if (!export->collate) {
1019 export->page_count++;
1020 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
1023 if (export->collated == export->collated_copies) {
1024 export->collated = 0;
1025 if (!export_print_inc_page (export)) {
1026 ev_document_doc_mutex_lock ();
1027 ev_file_exporter_end (EV_FILE_EXPORTER (op->document));
1028 ev_document_doc_mutex_unlock ();
1032 update_progress (export);
1033 export_print_done (export);
1039 /* we're not collating and we've reached a sheet from the wrong sheet set */
1040 if (!export->collate &&
1041 ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) ||
1042 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1))) {
1045 export->page_count++;
1047 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
1049 if (export->collated == export->collated_copies) {
1050 export->collated = 0;
1052 if (!export_print_inc_page (export)) {
1053 ev_document_doc_mutex_lock ();
1054 ev_file_exporter_end (EV_FILE_EXPORTER (op->document));
1055 ev_document_doc_mutex_unlock ();
1060 update_progress (export);
1062 export_print_done (export);
1067 } while ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) ||
1068 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1));
1072 if (export->pages_per_sheet == 1 ||
1073 (export->page_count % export->pages_per_sheet == 1 &&
1074 (export->page_set == GTK_PAGE_SET_ALL ||
1075 (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) ||
1076 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1)))) {
1077 ev_document_doc_mutex_lock ();
1078 ev_file_exporter_begin_page (EV_FILE_EXPORTER (op->document));
1079 ev_document_doc_mutex_unlock ();
1082 if (!export->job_export) {
1083 export->job_export = ev_job_export_new (op->document);
1084 g_signal_connect (export->job_export, "finished",
1085 G_CALLBACK (export_job_finished),
1087 g_signal_connect (export->job_export, "cancelled",
1088 G_CALLBACK (export_job_cancelled),
1092 ev_job_export_set_page (EV_JOB_EXPORT (export->job_export), export->page);
1093 ev_job_scheduler_push_job (export->job_export, EV_JOB_PRIORITY_NONE);
1095 update_progress (export);
1101 ev_print_operation_export_begin (EvPrintOperationExport *export)
1103 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1105 if (!export->temp_file)
1106 return; /* cancelled */
1108 ev_document_doc_mutex_lock ();
1109 ev_file_exporter_begin (EV_FILE_EXPORTER (op->document), &export->fc);
1110 ev_document_doc_mutex_unlock ();
1112 export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1113 (GSourceFunc)export_print_page,
1115 (GDestroyNotify)export_print_page_idle_finished);
1119 ev_print_operation_export_print_dialog_response_cb (GtkDialog *dialog,
1121 EvPrintOperationExport *export)
1123 GtkPrintPages print_pages;
1124 GtkPrintSettings *print_settings;
1125 GtkPageSetup *page_setup;
1126 GtkPrinter *printer;
1132 const gchar *file_format;
1134 GError *error = NULL;
1135 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1137 if (response != GTK_RESPONSE_OK &&
1138 response != GTK_RESPONSE_APPLY) {
1139 gtk_widget_destroy (GTK_WIDGET (dialog));
1140 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
1145 export->print_preview = (response == GTK_RESPONSE_APPLY);
1147 printer = gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (dialog));
1148 ev_print_operation_export_set_printer (export, printer);
1150 print_settings = gtk_print_unix_dialog_get_settings (GTK_PRINT_UNIX_DIALOG (dialog));
1151 ev_print_operation_export_set_print_settings (op, print_settings);
1153 page_setup = gtk_print_unix_dialog_get_page_setup (GTK_PRINT_UNIX_DIALOG (dialog));
1154 ev_print_operation_export_set_default_page_setup (op, page_setup);
1156 if (!gtk_printer_accepts_ps (export->printer)) {
1157 gtk_widget_destroy (GTK_WIDGET (dialog));
1159 g_set_error_literal (&export->error,
1161 GTK_PRINT_ERROR_GENERAL,
1162 _("Printing is not supported on this printer."));
1163 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1168 file_format = gtk_print_settings_get (print_settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
1170 filename = g_strdup_printf ("evince_print.%s.XXXXXX", file_format != NULL ? file_format : "");
1171 export->fd = g_file_open_tmp (filename, &export->temp_file, &error);
1173 if (export->fd <= -1) {
1174 gtk_widget_destroy (GTK_WIDGET (dialog));
1176 g_set_error_literal (&export->error,
1178 GTK_PRINT_ERROR_GENERAL,
1180 g_error_free (error);
1181 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1186 export->current_page = gtk_print_unix_dialog_get_current_page (GTK_PRINT_UNIX_DIALOG (dialog));
1187 export->page_set = gtk_print_settings_get_page_set (print_settings);
1188 print_pages = gtk_print_settings_get_print_pages (print_settings);
1190 switch (print_pages) {
1191 case GTK_PRINT_PAGES_CURRENT:
1192 export->ranges = &export->one_range;
1194 export->ranges[0].start = export->current_page;
1195 export->ranges[0].end = export->current_page;
1196 export->n_ranges = 1;
1199 case GTK_PRINT_PAGES_RANGES: {
1202 export->ranges = gtk_print_settings_get_page_ranges (print_settings, &export->n_ranges);
1203 for (i = 0; i < export->n_ranges; i++)
1204 if (export->ranges[i].end == -1 || export->ranges[i].end >= export->n_pages)
1205 export->ranges[i].end = export->n_pages - 1;
1209 g_warning ("Unsupported print pages setting\n");
1210 case GTK_PRINT_PAGES_ALL:
1211 export->ranges = &export->one_range;
1213 export->ranges[0].start = 0;
1214 export->ranges[0].end = export->n_pages - 1;
1215 export->n_ranges = 1;
1220 if (export->n_ranges < 1 || !clamp_ranges (export)) {
1221 GtkWidget *message_dialog;
1223 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
1225 GTK_MESSAGE_WARNING,
1227 "%s", _("Invalid page selection"));
1228 gtk_window_set_title (GTK_WINDOW (message_dialog), _("Warning"));
1229 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (message_dialog),
1230 "%s", _("Your print range selection does not include any pages"));
1231 g_signal_connect (message_dialog, "response",
1232 G_CALLBACK (gtk_widget_destroy),
1234 gtk_widget_show (message_dialog);
1237 } else ev_print_operation_update_status (op, -1, -1, 0.0);
1239 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1240 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1241 scale = gtk_print_settings_get_scale (print_settings) * 0.01;
1247 export->pages_per_sheet = MAX (1, gtk_print_settings_get_number_up (print_settings));
1249 export->copies = gtk_print_settings_get_n_copies (print_settings);
1250 export->collate = gtk_print_settings_get_collate (print_settings);
1251 export->reverse = gtk_print_settings_get_reverse (print_settings);
1253 if (export->collate) {
1254 export->uncollated_copies = export->copies;
1255 export->collated_copies = 1;
1257 export->uncollated_copies = 1;
1258 export->collated_copies = export->copies;
1261 if (export->reverse) {
1262 export->range = export->n_ranges - 1;
1268 find_range (export);
1270 export->page = export->start - export->inc;
1271 export->collated = export->collated_copies - 1;
1273 get_first_and_last_page (export, &first_page, &last_page);
1275 export->fc.format = file_format && g_ascii_strcasecmp (file_format, "pdf") == 0 ?
1276 EV_FILE_FORMAT_PDF : EV_FILE_FORMAT_PS;
1277 export->fc.filename = export->temp_file;
1278 export->fc.first_page = MIN (first_page, last_page);
1279 export->fc.last_page = MAX (first_page, last_page);
1280 export->fc.paper_width = width;
1281 export->fc.paper_height = height;
1282 export->fc.duplex = FALSE;
1283 export->fc.pages_per_sheet = export->pages_per_sheet;
1285 if (ev_print_queue_is_empty (op->document))
1286 ev_print_operation_export_begin (export);
1288 ev_print_queue_push (op);
1290 g_signal_emit (op, signals[BEGIN_PRINT], 0);
1292 gtk_widget_destroy (GTK_WIDGET (dialog));
1296 ev_print_operation_export_run (EvPrintOperation *op,
1299 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1301 GtkPrintCapabilities capabilities;
1303 ev_print_queue_init ();
1305 export->parent_window = parent;
1306 export->error = NULL;
1308 dialog = gtk_print_unix_dialog_new (_("Print"), parent);
1309 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1311 capabilities = GTK_PRINT_CAPABILITY_PREVIEW |
1312 ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document));
1313 gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG (dialog),
1316 gtk_print_unix_dialog_set_embed_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1317 export->embed_page_setup);
1319 gtk_print_unix_dialog_set_current_page (GTK_PRINT_UNIX_DIALOG (dialog),
1320 export->current_page);
1322 gtk_print_unix_dialog_set_settings (GTK_PRINT_UNIX_DIALOG (dialog),
1323 export->print_settings);
1325 if (export->page_setup)
1326 gtk_print_unix_dialog_set_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1327 export->page_setup);
1329 g_signal_connect (dialog, "response",
1330 G_CALLBACK (ev_print_operation_export_print_dialog_response_cb),
1333 gtk_window_present (GTK_WINDOW (dialog));
1337 ev_print_operation_export_cancel (EvPrintOperation *op)
1339 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1341 if (export->job_export &&
1342 !ev_job_is_finished (export->job_export)) {
1343 ev_job_cancel (export->job_export);
1345 export_cancel (export);
1350 ev_print_operation_export_get_error (EvPrintOperation *op,
1353 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1355 g_propagate_error (error, export->error);
1356 export->error = NULL;
1360 ev_print_operation_export_set_embed_page_setup (EvPrintOperation *op,
1363 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1365 export->embed_page_setup = embed;
1369 ev_print_operation_export_get_embed_page_setup (EvPrintOperation *op)
1371 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1373 return export->embed_page_setup;
1377 ev_print_operation_export_finalize (GObject *object)
1379 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (object);
1381 if (export->idle_id > 0) {
1382 g_source_remove (export->idle_id);
1383 export->idle_id = 0;
1386 if (export->fd != -1) {
1391 if (export->ranges) {
1392 if (export->ranges != &export->one_range)
1393 g_free (export->ranges);
1394 export->ranges = NULL;
1395 export->n_ranges = 0;
1398 if (export->temp_file) {
1399 g_free (export->temp_file);
1400 export->temp_file = NULL;
1403 if (export->job_name) {
1404 g_free (export->job_name);
1405 export->job_name = NULL;
1408 if (export->job_export) {
1409 if (!ev_job_is_finished (export->job_export))
1410 ev_job_cancel (export->job_export);
1411 g_signal_handlers_disconnect_by_func (export->job_export,
1412 export_job_finished,
1414 g_signal_handlers_disconnect_by_func (export->job_export,
1415 export_job_cancelled,
1417 g_object_unref (export->job_export);
1418 export->job_export = NULL;
1421 if (export->error) {
1422 g_error_free (export->error);
1423 export->error = NULL;
1426 if (export->print_settings) {
1427 g_object_unref (export->print_settings);
1428 export->print_settings = NULL;
1431 if (export->page_setup) {
1432 g_object_unref (export->page_setup);
1433 export->page_setup = NULL;
1436 if (export->printer) {
1437 g_object_unref (export->printer);
1438 export->printer = NULL;
1441 (* G_OBJECT_CLASS (ev_print_operation_export_parent_class)->finalize) (object);
1445 ev_print_operation_export_init (EvPrintOperationExport *export)
1447 /* sheets are counted from 1 to be physical */
1452 ev_print_operation_export_constructor (GType type,
1453 guint n_construct_properties,
1454 GObjectConstructParam *construct_params)
1457 EvPrintOperationExport *export;
1458 EvPrintOperation *op;
1460 object = G_OBJECT_CLASS (ev_print_operation_export_parent_class)->constructor (type,
1461 n_construct_properties,
1463 export = EV_PRINT_OPERATION_EXPORT (object);
1464 op = EV_PRINT_OPERATION (object);
1465 export->n_pages = ev_document_get_n_pages (op->document);
1471 ev_print_operation_export_class_init (EvPrintOperationExportClass *klass)
1473 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
1474 EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass);
1476 ev_print_op_class->set_current_page = ev_print_operation_export_set_current_page;
1477 ev_print_op_class->set_print_settings = ev_print_operation_export_set_print_settings;
1478 ev_print_op_class->get_print_settings = ev_print_operation_export_get_print_settings;
1479 ev_print_op_class->set_default_page_setup = ev_print_operation_export_set_default_page_setup;
1480 ev_print_op_class->get_default_page_setup = ev_print_operation_export_get_default_page_setup;
1481 ev_print_op_class->set_job_name = ev_print_operation_export_set_job_name;
1482 ev_print_op_class->get_job_name = ev_print_operation_export_get_job_name;
1483 ev_print_op_class->run = ev_print_operation_export_run;
1484 ev_print_op_class->cancel = ev_print_operation_export_cancel;
1485 ev_print_op_class->get_error = ev_print_operation_export_get_error;
1486 ev_print_op_class->set_embed_page_setup = ev_print_operation_export_set_embed_page_setup;
1487 ev_print_op_class->get_embed_page_setup = ev_print_operation_export_get_embed_page_setup;
1489 g_object_class->constructor = ev_print_operation_export_constructor;
1490 g_object_class->finalize = ev_print_operation_export_finalize;
1493 #endif /* GTKUNIXPRINT_ENABLED */
1495 /* Print to cairo interface */
1496 #define EV_TYPE_PRINT_OPERATION_PRINT (ev_print_operation_print_get_type())
1497 #define EV_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrint))
1498 #define EV_PRINT_OPERATION_PRINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrintClass))
1499 #define EV_IS_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_PRINT))
1501 typedef struct _EvPrintOperationPrint EvPrintOperationPrint;
1502 typedef struct _EvPrintOperationPrintClass EvPrintOperationPrintClass;
1504 static GType ev_print_operation_print_get_type (void) G_GNUC_CONST;
1508 EV_SCALE_SHRINK_TO_PRINTABLE_AREA,
1509 EV_SCALE_FIT_TO_PRINTABLE_AREA
1512 #define EV_PRINT_SETTING_PAGE_SCALE "evince-print-setting-page-scale"
1513 #define EV_PRINT_SETTING_AUTOROTATE "evince-print-setting-page-autorotate"
1514 #define EV_PRINT_SETTING_PAGE_SIZE "evince-print-setting-page-size"
1516 struct _EvPrintOperationPrint {
1517 EvPrintOperation parent;
1519 GtkPrintOperation *op;
1520 gint n_pages_to_print;
1525 /* Page handling tab */
1526 GtkWidget *scale_combo;
1527 EvPrintScale page_scale;
1528 GtkWidget *autorotate_button;
1529 gboolean autorotate;
1530 GtkWidget *source_button;
1531 gboolean use_source_size;
1534 struct _EvPrintOperationPrintClass {
1535 EvPrintOperationClass parent_class;
1538 G_DEFINE_TYPE (EvPrintOperationPrint, ev_print_operation_print, EV_TYPE_PRINT_OPERATION)
1541 ev_print_operation_print_set_current_page (EvPrintOperation *op,
1544 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1546 gtk_print_operation_set_current_page (print->op, current_page);
1550 ev_print_operation_print_set_print_settings (EvPrintOperation *op,
1551 GtkPrintSettings *print_settings)
1553 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1555 gtk_print_operation_set_print_settings (print->op, print_settings);
1558 static GtkPrintSettings *
1559 ev_print_operation_print_get_print_settings (EvPrintOperation *op)
1561 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1563 return gtk_print_operation_get_print_settings (print->op);
1567 ev_print_operation_print_set_default_page_setup (EvPrintOperation *op,
1568 GtkPageSetup *page_setup)
1570 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1572 gtk_print_operation_set_default_page_setup (print->op, page_setup);
1575 static GtkPageSetup *
1576 ev_print_operation_print_get_default_page_setup (EvPrintOperation *op)
1578 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1580 return gtk_print_operation_get_default_page_setup (print->op);
1584 ev_print_operation_print_set_job_name (EvPrintOperation *op,
1585 const gchar *job_name)
1587 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1589 g_free (print->job_name);
1590 print->job_name = g_strdup (job_name);
1592 gtk_print_operation_set_job_name (print->op, print->job_name);
1595 static const gchar *
1596 ev_print_operation_print_get_job_name (EvPrintOperation *op)
1598 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1600 if (!print->job_name) {
1603 g_object_get (print->op, "job_name", &name, NULL);
1604 print->job_name = name;
1607 return print->job_name;
1611 ev_print_operation_print_run (EvPrintOperation *op,
1614 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1616 gtk_print_operation_run (print->op,
1617 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1622 ev_print_operation_print_cancel (EvPrintOperation *op)
1624 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1626 if (print->job_print)
1627 ev_job_cancel (print->job_print);
1629 gtk_print_operation_cancel (print->op);
1633 ev_print_operation_print_get_error (EvPrintOperation *op,
1636 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1638 gtk_print_operation_get_error (print->op, error);
1642 ev_print_operation_print_set_embed_page_setup (EvPrintOperation *op,
1645 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1647 gtk_print_operation_set_embed_page_setup (print->op, embed);
1651 ev_print_operation_print_get_embed_page_setup (EvPrintOperation *op)
1653 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1655 return gtk_print_operation_get_embed_page_setup (print->op);
1659 ev_print_operation_print_begin_print (EvPrintOperationPrint *print,
1660 GtkPrintContext *context)
1662 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1665 n_pages = ev_document_get_n_pages (op->document);
1666 gtk_print_operation_set_n_pages (print->op, n_pages);
1667 ev_print_operation_update_status (op, -1, n_pages, 0);
1669 g_signal_emit (op, signals[BEGIN_PRINT], 0);
1673 ev_print_operation_print_done (EvPrintOperationPrint *print,
1674 GtkPrintOperationResult result)
1676 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1678 ev_print_operation_update_status (op, 0, print->n_pages_to_print, 1.0);
1680 g_signal_emit (op, signals[DONE], 0, result);
1684 ev_print_operation_print_status_changed (EvPrintOperationPrint *print)
1686 GtkPrintStatus status;
1688 status = gtk_print_operation_get_status (print->op);
1689 if (status == GTK_PRINT_STATUS_GENERATING_DATA)
1690 print->n_pages_to_print = gtk_print_operation_get_n_pages_to_print (print->op);
1694 print_job_finished (EvJobPrint *job,
1695 EvPrintOperationPrint *print)
1697 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1699 gtk_print_operation_draw_page_finish (print->op);
1702 ev_print_operation_update_status (op, print->total,
1703 print->n_pages_to_print,
1704 print->total / (gdouble)print->n_pages_to_print);
1705 ev_job_print_set_cairo (job, NULL);
1709 draw_page_finish_idle (EvPrintOperationPrint *print)
1711 if (ev_job_scheduler_get_running_thread_job () == print->job_print)
1714 gtk_print_operation_draw_page_finish (print->op);
1720 print_job_cancelled (EvJobPrint *job,
1721 EvPrintOperationPrint *print)
1723 /* Finish the current page, so that draw-page
1724 * is emitted again and it will cancel the
1725 * print operation. If the job is still
1726 * running, wait until it finishes.
1728 if (ev_job_scheduler_get_running_thread_job () == print->job_print)
1729 g_idle_add ((GSourceFunc)draw_page_finish_idle, print);
1731 gtk_print_operation_draw_page_finish (print->op);
1735 ev_print_operation_print_request_page_setup (EvPrintOperationPrint *print,
1736 GtkPrintContext *context,
1738 GtkPageSetup *setup)
1740 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1741 gdouble width, height;
1742 GtkPaperSize *paper_size;
1744 ev_document_get_page_size (op->document, page_nr,
1747 if (print->use_source_size) {
1748 paper_size = gtk_paper_size_new_custom ("custom", "custom",
1749 width, height, GTK_UNIT_POINTS);
1750 gtk_page_setup_set_paper_size_and_default_margins (setup, paper_size);
1751 gtk_paper_size_free (paper_size);
1754 if (print->autorotate) {
1756 gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
1758 gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_PORTRAIT);
1763 _print_context_get_hard_margins (GtkPrintContext *context,
1769 if (!gtk_print_context_get_hard_margins (context, top, bottom, left, right)) {
1778 ev_print_operation_print_draw_page (EvPrintOperationPrint *print,
1779 GtkPrintContext *context,
1782 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1784 gdouble cr_width, cr_height;
1785 gdouble width, height, scale;
1786 gdouble x_scale, y_scale;
1787 gdouble top, bottom, left, right;
1789 gtk_print_operation_set_defer_drawing (print->op);
1791 if (!print->job_print) {
1792 print->job_print = ev_job_print_new (op->document);
1793 g_signal_connect (G_OBJECT (print->job_print), "finished",
1794 G_CALLBACK (print_job_finished),
1796 g_signal_connect (G_OBJECT (print->job_print), "cancelled",
1797 G_CALLBACK (print_job_cancelled),
1799 } else if (g_cancellable_is_cancelled (print->job_print->cancellable)) {
1800 gtk_print_operation_cancel (print->op);
1801 ev_job_print_set_cairo (EV_JOB_PRINT (print->job_print), NULL);
1805 ev_job_print_set_page (EV_JOB_PRINT (print->job_print), page);
1807 cr = gtk_print_context_get_cairo_context (context);
1808 cr_width = gtk_print_context_get_width (context);
1809 cr_height = gtk_print_context_get_height (context);
1810 ev_document_get_page_size (op->document, page, &width, &height);
1812 if (print->page_scale == EV_SCALE_NONE) {
1813 /* Center document page on the printed page */
1814 if (print->autorotate)
1815 cairo_translate (cr, (cr_width - width) / 2, (cr_height - height) / 2);
1817 _print_context_get_hard_margins (context, &top, &bottom, &left, &right);
1819 x_scale = (cr_width - left - right) / width;
1820 y_scale = (cr_height - top - bottom) / height;
1822 if (x_scale < y_scale)
1827 if (print->autorotate) {
1828 double left_right_sides, top_bottom_sides;
1830 cairo_translate (cr, (cr_width - scale * width) / 2,
1831 (cr_height - scale * height) / 2);
1833 /* Ensure document page is within the margins. The
1834 * scale guarantees the document will fit in the
1835 * margins so we just need to check each side and
1836 * if it overhangs the margin, translate it to the
1838 left_right_sides = (cr_width - width*scale)/2;
1839 top_bottom_sides = (cr_height - height*scale)/2;
1840 if (left_right_sides < left)
1841 cairo_translate (cr, left - left_right_sides, 0);
1843 if (left_right_sides < right)
1844 cairo_translate (cr, -(right - left_right_sides), 0);
1846 if (top_bottom_sides < top)
1847 cairo_translate (cr, 0, top - top_bottom_sides);
1849 if (top_bottom_sides < bottom)
1850 cairo_translate (cr, 0, -(bottom - top_bottom_sides));
1852 cairo_translate (cr, left, top);
1855 if (print->page_scale == EV_SCALE_FIT_TO_PRINTABLE_AREA || scale < 1.0) {
1856 cairo_scale (cr, scale, scale);
1860 ev_job_print_set_cairo (EV_JOB_PRINT (print->job_print), cr);
1861 ev_job_scheduler_push_job (print->job_print, EV_JOB_PRIORITY_NONE);
1865 ev_print_operation_print_create_custom_widget (EvPrintOperationPrint *print,
1866 GtkPrintContext *context)
1868 GtkPrintSettings *settings;
1871 EvPrintScale page_scale;
1872 gboolean autorotate;
1873 gboolean use_source_size;
1875 settings = gtk_print_operation_get_print_settings (print->op);
1876 page_scale = gtk_print_settings_get_int_with_default (settings, EV_PRINT_SETTING_PAGE_SCALE, 1);
1877 autorotate = gtk_print_settings_has_key (settings, EV_PRINT_SETTING_AUTOROTATE) ?
1878 gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_AUTOROTATE) :
1880 use_source_size = gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_PAGE_SIZE);
1882 table = gtk_table_new (3, 2, FALSE);
1883 gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1884 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1885 gtk_container_set_border_width (GTK_CONTAINER (table), 12);
1887 label = gtk_label_new (_("Page Scaling:"));
1888 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
1889 gtk_widget_show (label);
1891 print->scale_combo = gtk_combo_box_text_new ();
1892 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("None"));
1893 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("Shrink to Printable Area"));
1894 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("Fit to Printable Area"));
1895 gtk_combo_box_set_active (GTK_COMBO_BOX (print->scale_combo), page_scale);
1896 gtk_widget_set_tooltip_text (print->scale_combo,
1897 _("Scale document pages to fit the selected printer page. Select from one of the following:\n"
1899 "• \"None\": No page scaling is performed.\n"
1901 "• \"Shrink to Printable Area\": Document pages larger than the printable area"
1902 " are reduced to fit the printable area of the printer page.\n"
1904 "• \"Fit to Printable Area\": Document pages are enlarged or reduced as"
1905 " required to fit the printable area of the printer page.\n"));
1906 gtk_table_attach (GTK_TABLE (table), print->scale_combo, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
1907 gtk_widget_show (print->scale_combo);
1909 print->autorotate_button = gtk_check_button_new_with_label (_("Auto Rotate and Center"));
1910 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->autorotate_button), autorotate);
1911 gtk_widget_set_tooltip_text (print->autorotate_button,
1912 _("Rotate printer page orientation of each page to match orientation of each document page. "
1913 "Document pages will be centered within the printer page."));
1914 gtk_table_attach (GTK_TABLE (table), print->autorotate_button, 0, 2, 1, 2, GTK_FILL, 0, 0, 0);
1915 gtk_widget_show (print->autorotate_button);
1917 print->source_button = gtk_check_button_new_with_label (_("Select page size using document page size"));
1918 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->source_button), use_source_size);
1919 gtk_widget_set_tooltip_text (print->source_button, _("When enabled, each page will be printed on "
1920 "the same size paper as the document page."));
1921 gtk_table_attach (GTK_TABLE (table), print->source_button, 0, 2, 2, 3, GTK_FILL, 0, 0, 0);
1922 gtk_widget_show (print->source_button);
1924 return G_OBJECT (table);
1928 ev_print_operation_print_custom_widget_apply (EvPrintOperationPrint *print,
1929 GtkPrintContext *context)
1931 GtkPrintSettings *settings;
1933 print->page_scale = gtk_combo_box_get_active (GTK_COMBO_BOX (print->scale_combo));
1934 print->autorotate = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->autorotate_button));
1935 print->use_source_size = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->source_button));
1936 settings = gtk_print_operation_get_print_settings (print->op);
1937 gtk_print_settings_set_int (settings, EV_PRINT_SETTING_PAGE_SCALE, print->page_scale);
1938 gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_AUTOROTATE, print->autorotate);
1939 gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_PAGE_SIZE, print->use_source_size);
1943 ev_print_operation_print_finalize (GObject *object)
1945 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (object);
1948 g_object_unref (print->op);
1952 if (print->job_name) {
1953 g_free (print->job_name);
1954 print->job_name = NULL;
1957 if (print->job_print) {
1958 if (!ev_job_is_finished (print->job_print))
1959 ev_job_cancel (print->job_print);
1960 g_signal_handlers_disconnect_by_func (print->job_print,
1963 g_signal_handlers_disconnect_by_func (print->job_print,
1964 print_job_cancelled,
1966 g_object_unref (print->job_print);
1967 print->job_print = NULL;
1970 (* G_OBJECT_CLASS (ev_print_operation_print_parent_class)->finalize) (object);
1974 ev_print_operation_print_init (EvPrintOperationPrint *print)
1976 print->op = gtk_print_operation_new ();
1977 g_signal_connect_swapped (print->op, "begin_print",
1978 G_CALLBACK (ev_print_operation_print_begin_print),
1980 g_signal_connect_swapped (print->op, "done",
1981 G_CALLBACK (ev_print_operation_print_done),
1983 g_signal_connect_swapped (print->op, "draw_page",
1984 G_CALLBACK (ev_print_operation_print_draw_page),
1986 g_signal_connect_swapped (print->op, "status_changed",
1987 G_CALLBACK (ev_print_operation_print_status_changed),
1989 g_signal_connect_swapped (print->op, "request_page_setup",
1990 G_CALLBACK (ev_print_operation_print_request_page_setup),
1992 g_signal_connect_swapped (print->op, "create_custom_widget",
1993 G_CALLBACK (ev_print_operation_print_create_custom_widget),
1995 g_signal_connect_swapped (print->op, "custom_widget_apply",
1996 G_CALLBACK (ev_print_operation_print_custom_widget_apply),
1998 gtk_print_operation_set_allow_async (print->op, TRUE);
1999 gtk_print_operation_set_use_full_page (print->op, TRUE);
2000 gtk_print_operation_set_unit (print->op, GTK_UNIT_POINTS);
2001 gtk_print_operation_set_custom_tab_label (print->op, _("Page Handling"));
2005 ev_print_operation_print_class_init (EvPrintOperationPrintClass *klass)
2007 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
2008 EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass);
2010 ev_print_op_class->set_current_page = ev_print_operation_print_set_current_page;
2011 ev_print_op_class->set_print_settings = ev_print_operation_print_set_print_settings;
2012 ev_print_op_class->get_print_settings = ev_print_operation_print_get_print_settings;
2013 ev_print_op_class->set_default_page_setup = ev_print_operation_print_set_default_page_setup;
2014 ev_print_op_class->get_default_page_setup = ev_print_operation_print_get_default_page_setup;
2015 ev_print_op_class->set_job_name = ev_print_operation_print_set_job_name;
2016 ev_print_op_class->get_job_name = ev_print_operation_print_get_job_name;
2017 ev_print_op_class->run = ev_print_operation_print_run;
2018 ev_print_op_class->cancel = ev_print_operation_print_cancel;
2019 ev_print_op_class->get_error = ev_print_operation_print_get_error;
2020 ev_print_op_class->set_embed_page_setup = ev_print_operation_print_set_embed_page_setup;
2021 ev_print_op_class->get_embed_page_setup = ev_print_operation_print_get_embed_page_setup;
2023 g_object_class->finalize = ev_print_operation_print_finalize;
2027 ev_print_operation_exists_for_document (EvDocument *document)
2029 #if GTKUNIXPRINT_ENABLED
2030 return (EV_IS_FILE_EXPORTER(document) || EV_IS_DOCUMENT_PRINT(document));
2032 return EV_IS_DOCUMENT_PRINT(document);
2033 #endif /* GTKUNIXPRINT_ENABLED */
2036 /* Factory method */
2038 ev_print_operation_new (EvDocument *document)
2040 EvPrintOperation *op = NULL;
2042 g_return_val_if_fail (ev_print_operation_exists_for_document (document), NULL);
2044 if (EV_IS_DOCUMENT_PRINT (document))
2045 op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_PRINT,
2046 "document", document, NULL));
2048 #if GTKUNIXPRINT_ENABLED
2049 op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_EXPORT,
2050 "document", document, NULL));