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);
861 gchar *quoted_filename;
862 gchar *quoted_settings_filename;
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_shell_parse_argv (cmd, &argc, &argv, &error);
871 g_free (quoted_filename);
872 g_free (quoted_settings_filename);
876 gdk_spawn_on_screen (gtk_window_get_screen (export->parent_window),
887 if (print_settings_file)
888 g_unlink (print_settings_file);
889 g_free (print_settings_file);
891 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
892 /* temp_file will be deleted by the previewer */
894 ev_print_operation_export_run_next (export);
899 job = gtk_print_job_new (export->job_name,
903 gtk_print_job_set_source_file (job, export->temp_file, &error);
905 gtk_print_job_send (job,
906 (GtkPrintJobCompleteFunc)gtk_print_job_finished,
907 g_object_ref (export),
908 (GDestroyNotify)g_object_unref);
911 g_object_unref (settings);
914 g_set_error_literal (&export->error,
916 GTK_PRINT_ERROR_GENERAL,
918 g_error_free (error);
919 ev_print_operation_export_clear_temp_file (export);
920 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
922 ev_print_operation_export_run_next (export);
927 export_print_page_idle_finished (EvPrintOperationExport *export)
933 export_job_finished (EvJobExport *job,
934 EvPrintOperationExport *export)
936 EvPrintOperation *op = EV_PRINT_OPERATION (export);
938 if (export->pages_per_sheet == 1 ||
939 ( export->page_count % export->pages_per_sheet == 0 &&
940 ( export->page_set == GTK_PAGE_SET_ALL ||
941 ( export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0 ) ||
942 ( export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1 ) ) ) ) {
944 ev_document_doc_mutex_lock ();
945 ev_file_exporter_end_page (EV_FILE_EXPORTER (op->document));
946 ev_document_doc_mutex_unlock ();
950 export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
951 (GSourceFunc)export_print_page,
953 (GDestroyNotify)export_print_page_idle_finished);
957 export_job_cancelled (EvJobExport *job,
958 EvPrintOperationExport *export)
960 export_cancel (export);
964 export_cancel (EvPrintOperationExport *export)
966 EvPrintOperation *op = EV_PRINT_OPERATION (export);
968 if (export->idle_id > 0)
969 g_source_remove (export->idle_id);
972 if (export->job_export) {
973 g_signal_handlers_disconnect_by_func (export->job_export,
976 g_signal_handlers_disconnect_by_func (export->job_export,
977 export_job_cancelled,
979 g_object_unref (export->job_export);
980 export->job_export = NULL;
983 if (export->fd != -1) {
988 ev_print_operation_export_clear_temp_file (export);
990 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
992 ev_print_operation_export_run_next (export);
996 update_progress (EvPrintOperationExport *export)
998 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1000 ev_print_operation_update_status (op, export->total,
1001 export->n_pages_to_print,
1002 export->total / (gdouble)export->n_pages_to_print);
1006 export_print_page (EvPrintOperationExport *export)
1008 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1010 if (!export->temp_file)
1011 return FALSE; /* cancelled */
1016 /* note: when collating, page_count is increased in export_print_inc_page */
1017 if (!export->collate) {
1018 export->page_count++;
1019 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
1022 if (export->collated == export->collated_copies) {
1023 export->collated = 0;
1024 if (!export_print_inc_page (export)) {
1025 ev_document_doc_mutex_lock ();
1026 ev_file_exporter_end (EV_FILE_EXPORTER (op->document));
1027 ev_document_doc_mutex_unlock ();
1031 update_progress (export);
1032 export_print_done (export);
1038 /* we're not collating and we've reached a sheet from the wrong sheet set */
1039 if (!export->collate &&
1040 ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) ||
1041 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1))) {
1044 export->page_count++;
1046 export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
1048 if (export->collated == export->collated_copies) {
1049 export->collated = 0;
1051 if (!export_print_inc_page (export)) {
1052 ev_document_doc_mutex_lock ();
1053 ev_file_exporter_end (EV_FILE_EXPORTER (op->document));
1054 ev_document_doc_mutex_unlock ();
1059 update_progress (export);
1061 export_print_done (export);
1066 } while ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) ||
1067 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1));
1071 if (export->pages_per_sheet == 1 ||
1072 (export->page_count % export->pages_per_sheet == 1 &&
1073 (export->page_set == GTK_PAGE_SET_ALL ||
1074 (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) ||
1075 (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1)))) {
1076 ev_document_doc_mutex_lock ();
1077 ev_file_exporter_begin_page (EV_FILE_EXPORTER (op->document));
1078 ev_document_doc_mutex_unlock ();
1081 if (!export->job_export) {
1082 export->job_export = ev_job_export_new (op->document);
1083 g_signal_connect (export->job_export, "finished",
1084 G_CALLBACK (export_job_finished),
1086 g_signal_connect (export->job_export, "cancelled",
1087 G_CALLBACK (export_job_cancelled),
1091 ev_job_export_set_page (EV_JOB_EXPORT (export->job_export), export->page);
1092 ev_job_scheduler_push_job (export->job_export, EV_JOB_PRIORITY_NONE);
1094 update_progress (export);
1100 ev_print_operation_export_begin (EvPrintOperationExport *export)
1102 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1104 if (!export->temp_file)
1105 return; /* cancelled */
1107 ev_document_doc_mutex_lock ();
1108 ev_file_exporter_begin (EV_FILE_EXPORTER (op->document), &export->fc);
1109 ev_document_doc_mutex_unlock ();
1111 export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1112 (GSourceFunc)export_print_page,
1114 (GDestroyNotify)export_print_page_idle_finished);
1118 ev_print_operation_export_print_dialog_response_cb (GtkDialog *dialog,
1120 EvPrintOperationExport *export)
1122 GtkPrintPages print_pages;
1123 GtkPrintSettings *print_settings;
1124 GtkPageSetup *page_setup;
1125 GtkPrinter *printer;
1131 const gchar *file_format;
1133 GError *error = NULL;
1134 EvPrintOperation *op = EV_PRINT_OPERATION (export);
1136 if (response != GTK_RESPONSE_OK &&
1137 response != GTK_RESPONSE_APPLY) {
1138 gtk_widget_destroy (GTK_WIDGET (dialog));
1139 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
1144 export->print_preview = (response == GTK_RESPONSE_APPLY);
1146 printer = gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (dialog));
1147 ev_print_operation_export_set_printer (export, printer);
1149 print_settings = gtk_print_unix_dialog_get_settings (GTK_PRINT_UNIX_DIALOG (dialog));
1150 ev_print_operation_export_set_print_settings (op, print_settings);
1152 page_setup = gtk_print_unix_dialog_get_page_setup (GTK_PRINT_UNIX_DIALOG (dialog));
1153 ev_print_operation_export_set_default_page_setup (op, page_setup);
1155 if (!gtk_printer_accepts_ps (export->printer)) {
1156 gtk_widget_destroy (GTK_WIDGET (dialog));
1158 g_set_error_literal (&export->error,
1160 GTK_PRINT_ERROR_GENERAL,
1161 _("Printing is not supported on this printer."));
1162 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1167 file_format = gtk_print_settings_get (print_settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
1169 filename = g_strdup_printf ("evince_print.%s.XXXXXX", file_format != NULL ? file_format : "");
1170 export->fd = g_file_open_tmp (filename, &export->temp_file, &error);
1172 if (export->fd <= -1) {
1173 gtk_widget_destroy (GTK_WIDGET (dialog));
1175 g_set_error_literal (&export->error,
1177 GTK_PRINT_ERROR_GENERAL,
1179 g_error_free (error);
1180 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1185 export->current_page = gtk_print_unix_dialog_get_current_page (GTK_PRINT_UNIX_DIALOG (dialog));
1186 export->page_set = gtk_print_settings_get_page_set (print_settings);
1187 print_pages = gtk_print_settings_get_print_pages (print_settings);
1189 switch (print_pages) {
1190 case GTK_PRINT_PAGES_CURRENT:
1191 export->ranges = &export->one_range;
1193 export->ranges[0].start = export->current_page;
1194 export->ranges[0].end = export->current_page;
1195 export->n_ranges = 1;
1198 case GTK_PRINT_PAGES_RANGES: {
1201 export->ranges = gtk_print_settings_get_page_ranges (print_settings, &export->n_ranges);
1202 for (i = 0; i < export->n_ranges; i++)
1203 if (export->ranges[i].end == -1 || export->ranges[i].end >= export->n_pages)
1204 export->ranges[i].end = export->n_pages - 1;
1208 g_warning ("Unsupported print pages setting\n");
1209 case GTK_PRINT_PAGES_ALL:
1210 export->ranges = &export->one_range;
1212 export->ranges[0].start = 0;
1213 export->ranges[0].end = export->n_pages - 1;
1214 export->n_ranges = 1;
1219 if (export->n_ranges < 1 || !clamp_ranges (export)) {
1220 GtkWidget *message_dialog;
1222 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
1224 GTK_MESSAGE_WARNING,
1226 "%s", _("Invalid page selection"));
1227 gtk_window_set_title (GTK_WINDOW (message_dialog), _("Warning"));
1228 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (message_dialog),
1229 "%s", _("Your print range selection does not include any pages"));
1230 g_signal_connect (message_dialog, "response",
1231 G_CALLBACK (gtk_widget_destroy),
1233 gtk_widget_show (message_dialog);
1236 } else ev_print_operation_update_status (op, -1, -1, 0.0);
1238 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1239 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1240 scale = gtk_print_settings_get_scale (print_settings) * 0.01;
1246 export->pages_per_sheet = MAX (1, gtk_print_settings_get_number_up (print_settings));
1248 export->copies = gtk_print_settings_get_n_copies (print_settings);
1249 export->collate = gtk_print_settings_get_collate (print_settings);
1250 export->reverse = gtk_print_settings_get_reverse (print_settings);
1252 if (export->collate) {
1253 export->uncollated_copies = export->copies;
1254 export->collated_copies = 1;
1256 export->uncollated_copies = 1;
1257 export->collated_copies = export->copies;
1260 if (export->reverse) {
1261 export->range = export->n_ranges - 1;
1267 find_range (export);
1269 export->page = export->start - export->inc;
1270 export->collated = export->collated_copies - 1;
1272 get_first_and_last_page (export, &first_page, &last_page);
1274 export->fc.format = file_format && g_ascii_strcasecmp (file_format, "pdf") == 0 ?
1275 EV_FILE_FORMAT_PDF : EV_FILE_FORMAT_PS;
1276 export->fc.filename = export->temp_file;
1277 export->fc.first_page = MIN (first_page, last_page);
1278 export->fc.last_page = MAX (first_page, last_page);
1279 export->fc.paper_width = width;
1280 export->fc.paper_height = height;
1281 export->fc.duplex = FALSE;
1282 export->fc.pages_per_sheet = export->pages_per_sheet;
1284 if (ev_print_queue_is_empty (op->document))
1285 ev_print_operation_export_begin (export);
1287 ev_print_queue_push (op);
1289 g_signal_emit (op, signals[BEGIN_PRINT], 0);
1291 gtk_widget_destroy (GTK_WIDGET (dialog));
1295 ev_print_operation_export_run (EvPrintOperation *op,
1298 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1300 GtkPrintCapabilities capabilities;
1302 ev_print_queue_init ();
1304 export->parent_window = parent;
1305 export->error = NULL;
1307 dialog = gtk_print_unix_dialog_new (_("Print"), parent);
1308 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1310 capabilities = GTK_PRINT_CAPABILITY_PREVIEW |
1311 ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document));
1312 gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG (dialog),
1315 gtk_print_unix_dialog_set_embed_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1316 export->embed_page_setup);
1318 gtk_print_unix_dialog_set_current_page (GTK_PRINT_UNIX_DIALOG (dialog),
1319 export->current_page);
1321 gtk_print_unix_dialog_set_settings (GTK_PRINT_UNIX_DIALOG (dialog),
1322 export->print_settings);
1324 if (export->page_setup)
1325 gtk_print_unix_dialog_set_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1326 export->page_setup);
1328 g_signal_connect (dialog, "response",
1329 G_CALLBACK (ev_print_operation_export_print_dialog_response_cb),
1332 gtk_window_present (GTK_WINDOW (dialog));
1336 ev_print_operation_export_cancel (EvPrintOperation *op)
1338 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1340 if (export->job_export &&
1341 !ev_job_is_finished (export->job_export)) {
1342 ev_job_cancel (export->job_export);
1344 export_cancel (export);
1349 ev_print_operation_export_get_error (EvPrintOperation *op,
1352 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1354 g_propagate_error (error, export->error);
1355 export->error = NULL;
1359 ev_print_operation_export_set_embed_page_setup (EvPrintOperation *op,
1362 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1364 export->embed_page_setup = embed;
1368 ev_print_operation_export_get_embed_page_setup (EvPrintOperation *op)
1370 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1372 return export->embed_page_setup;
1376 ev_print_operation_export_finalize (GObject *object)
1378 EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (object);
1380 if (export->idle_id > 0) {
1381 g_source_remove (export->idle_id);
1382 export->idle_id = 0;
1385 if (export->fd != -1) {
1390 if (export->ranges) {
1391 if (export->ranges != &export->one_range)
1392 g_free (export->ranges);
1393 export->ranges = NULL;
1394 export->n_ranges = 0;
1397 if (export->temp_file) {
1398 g_free (export->temp_file);
1399 export->temp_file = NULL;
1402 if (export->job_name) {
1403 g_free (export->job_name);
1404 export->job_name = NULL;
1407 if (export->job_export) {
1408 if (!ev_job_is_finished (export->job_export))
1409 ev_job_cancel (export->job_export);
1410 g_signal_handlers_disconnect_by_func (export->job_export,
1411 export_job_finished,
1413 g_signal_handlers_disconnect_by_func (export->job_export,
1414 export_job_cancelled,
1416 g_object_unref (export->job_export);
1417 export->job_export = NULL;
1420 if (export->error) {
1421 g_error_free (export->error);
1422 export->error = NULL;
1425 if (export->print_settings) {
1426 g_object_unref (export->print_settings);
1427 export->print_settings = NULL;
1430 if (export->page_setup) {
1431 g_object_unref (export->page_setup);
1432 export->page_setup = NULL;
1435 if (export->printer) {
1436 g_object_unref (export->printer);
1437 export->printer = NULL;
1440 (* G_OBJECT_CLASS (ev_print_operation_export_parent_class)->finalize) (object);
1444 ev_print_operation_export_init (EvPrintOperationExport *export)
1446 /* sheets are counted from 1 to be physical */
1451 ev_print_operation_export_constructor (GType type,
1452 guint n_construct_properties,
1453 GObjectConstructParam *construct_params)
1456 EvPrintOperationExport *export;
1457 EvPrintOperation *op;
1459 object = G_OBJECT_CLASS (ev_print_operation_export_parent_class)->constructor (type,
1460 n_construct_properties,
1462 export = EV_PRINT_OPERATION_EXPORT (object);
1463 op = EV_PRINT_OPERATION (object);
1464 export->n_pages = ev_document_get_n_pages (op->document);
1470 ev_print_operation_export_class_init (EvPrintOperationExportClass *klass)
1472 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
1473 EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass);
1475 ev_print_op_class->set_current_page = ev_print_operation_export_set_current_page;
1476 ev_print_op_class->set_print_settings = ev_print_operation_export_set_print_settings;
1477 ev_print_op_class->get_print_settings = ev_print_operation_export_get_print_settings;
1478 ev_print_op_class->set_default_page_setup = ev_print_operation_export_set_default_page_setup;
1479 ev_print_op_class->get_default_page_setup = ev_print_operation_export_get_default_page_setup;
1480 ev_print_op_class->set_job_name = ev_print_operation_export_set_job_name;
1481 ev_print_op_class->get_job_name = ev_print_operation_export_get_job_name;
1482 ev_print_op_class->run = ev_print_operation_export_run;
1483 ev_print_op_class->cancel = ev_print_operation_export_cancel;
1484 ev_print_op_class->get_error = ev_print_operation_export_get_error;
1485 ev_print_op_class->set_embed_page_setup = ev_print_operation_export_set_embed_page_setup;
1486 ev_print_op_class->get_embed_page_setup = ev_print_operation_export_get_embed_page_setup;
1488 g_object_class->constructor = ev_print_operation_export_constructor;
1489 g_object_class->finalize = ev_print_operation_export_finalize;
1492 #endif /* GTKUNIXPRINT_ENABLED */
1494 /* Print to cairo interface */
1495 #define EV_TYPE_PRINT_OPERATION_PRINT (ev_print_operation_print_get_type())
1496 #define EV_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrint))
1497 #define EV_PRINT_OPERATION_PRINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrintClass))
1498 #define EV_IS_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_PRINT))
1500 typedef struct _EvPrintOperationPrint EvPrintOperationPrint;
1501 typedef struct _EvPrintOperationPrintClass EvPrintOperationPrintClass;
1503 static GType ev_print_operation_print_get_type (void) G_GNUC_CONST;
1507 EV_SCALE_SHRINK_TO_PRINTABLE_AREA,
1508 EV_SCALE_FIT_TO_PRINTABLE_AREA
1511 #define EV_PRINT_SETTING_PAGE_SCALE "evince-print-setting-page-scale"
1512 #define EV_PRINT_SETTING_AUTOROTATE "evince-print-setting-page-autorotate"
1513 #define EV_PRINT_SETTING_PAGE_SIZE "evince-print-setting-page-size"
1515 struct _EvPrintOperationPrint {
1516 EvPrintOperation parent;
1518 GtkPrintOperation *op;
1519 gint n_pages_to_print;
1524 /* Page handling tab */
1525 GtkWidget *scale_combo;
1526 EvPrintScale page_scale;
1527 GtkWidget *autorotate_button;
1528 gboolean autorotate;
1529 GtkWidget *source_button;
1530 gboolean use_source_size;
1533 struct _EvPrintOperationPrintClass {
1534 EvPrintOperationClass parent_class;
1537 G_DEFINE_TYPE (EvPrintOperationPrint, ev_print_operation_print, EV_TYPE_PRINT_OPERATION)
1540 ev_print_operation_print_set_current_page (EvPrintOperation *op,
1543 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1545 gtk_print_operation_set_current_page (print->op, current_page);
1549 ev_print_operation_print_set_print_settings (EvPrintOperation *op,
1550 GtkPrintSettings *print_settings)
1552 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1554 gtk_print_operation_set_print_settings (print->op, print_settings);
1557 static GtkPrintSettings *
1558 ev_print_operation_print_get_print_settings (EvPrintOperation *op)
1560 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1562 return gtk_print_operation_get_print_settings (print->op);
1566 ev_print_operation_print_set_default_page_setup (EvPrintOperation *op,
1567 GtkPageSetup *page_setup)
1569 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1571 gtk_print_operation_set_default_page_setup (print->op, page_setup);
1574 static GtkPageSetup *
1575 ev_print_operation_print_get_default_page_setup (EvPrintOperation *op)
1577 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1579 return gtk_print_operation_get_default_page_setup (print->op);
1583 ev_print_operation_print_set_job_name (EvPrintOperation *op,
1584 const gchar *job_name)
1586 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1588 g_free (print->job_name);
1589 print->job_name = g_strdup (job_name);
1591 gtk_print_operation_set_job_name (print->op, print->job_name);
1594 static const gchar *
1595 ev_print_operation_print_get_job_name (EvPrintOperation *op)
1597 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1599 if (!print->job_name) {
1602 g_object_get (print->op, "job_name", &name, NULL);
1603 print->job_name = name;
1606 return print->job_name;
1610 ev_print_operation_print_run (EvPrintOperation *op,
1613 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1615 gtk_print_operation_run (print->op,
1616 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1621 ev_print_operation_print_cancel (EvPrintOperation *op)
1623 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1625 gtk_print_operation_cancel (print->op);
1629 ev_print_operation_print_get_error (EvPrintOperation *op,
1632 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1634 gtk_print_operation_get_error (print->op, error);
1638 ev_print_operation_print_set_embed_page_setup (EvPrintOperation *op,
1641 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1643 gtk_print_operation_set_embed_page_setup (print->op, embed);
1647 ev_print_operation_print_get_embed_page_setup (EvPrintOperation *op)
1649 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
1651 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 GtkPrintStatus status;
1684 status = gtk_print_operation_get_status (print->op);
1685 if (status == GTK_PRINT_STATUS_GENERATING_DATA)
1686 print->n_pages_to_print = gtk_print_operation_get_n_pages_to_print (print->op);
1690 print_job_finished (EvJobPrint *job,
1691 EvPrintOperationPrint *print)
1693 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1695 gtk_print_operation_draw_page_finish (print->op);
1698 ev_print_operation_update_status (op, print->total,
1699 print->n_pages_to_print,
1700 print->total / (gdouble)print->n_pages_to_print);
1701 ev_job_print_set_cairo (job, NULL);
1705 print_job_cancelled (EvJobPrint *job,
1706 EvPrintOperationPrint *print)
1708 gtk_print_operation_cancel (print->op);
1712 ev_print_operation_print_request_page_setup (EvPrintOperationPrint *print,
1713 GtkPrintContext *context,
1715 GtkPageSetup *setup)
1717 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1718 gdouble width, height;
1719 GtkPaperSize *paper_size;
1721 ev_document_get_page_size (op->document, page_nr,
1724 if (print->use_source_size) {
1725 paper_size = gtk_paper_size_new_custom ("custom", "custom",
1726 width, height, GTK_UNIT_POINTS);
1727 gtk_page_setup_set_paper_size_and_default_margins (setup, paper_size);
1728 gtk_paper_size_free (paper_size);
1731 if (print->autorotate) {
1733 gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
1735 gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_PORTRAIT);
1740 _print_context_get_hard_margins (GtkPrintContext *context,
1746 if (!gtk_print_context_get_hard_margins (context, top, bottom, left, right)) {
1755 ev_print_operation_print_draw_page (EvPrintOperationPrint *print,
1756 GtkPrintContext *context,
1759 EvPrintOperation *op = EV_PRINT_OPERATION (print);
1761 gdouble cr_width, cr_height;
1762 gdouble width, height, scale;
1763 gdouble x_scale, y_scale;
1764 gdouble top, bottom, left, right;
1766 gtk_print_operation_set_defer_drawing (print->op);
1768 if (!print->job_print) {
1769 print->job_print = ev_job_print_new (op->document);
1770 g_signal_connect (G_OBJECT (print->job_print), "finished",
1771 G_CALLBACK (print_job_finished),
1773 g_signal_connect (G_OBJECT (print->job_print), "cancelled",
1774 G_CALLBACK (print_job_cancelled),
1778 ev_job_print_set_page (EV_JOB_PRINT (print->job_print), page);
1780 cr = gtk_print_context_get_cairo_context (context);
1781 cr_width = gtk_print_context_get_width (context);
1782 cr_height = gtk_print_context_get_height (context);
1783 ev_document_get_page_size (op->document, page, &width, &height);
1785 if (print->page_scale == EV_SCALE_NONE) {
1786 /* Center document page on the printed page */
1787 if (print->autorotate)
1788 cairo_translate (cr, (cr_width - width) / 2, (cr_height - height) / 2);
1790 _print_context_get_hard_margins (context, &top, &bottom, &left, &right);
1792 x_scale = (cr_width - left - right) / width;
1793 y_scale = (cr_height - top - bottom) / height;
1795 if (x_scale < y_scale)
1800 if (print->autorotate) {
1801 double left_right_sides, top_bottom_sides;
1803 cairo_translate (cr, (cr_width - scale * width) / 2,
1804 (cr_height - scale * height) / 2);
1806 /* Ensure document page is within the margins. The
1807 * scale guarantees the document will fit in the
1808 * margins so we just need to check each side and
1809 * if it overhangs the margin, translate it to the
1811 left_right_sides = (cr_width - width*scale)/2;
1812 top_bottom_sides = (cr_height - height*scale)/2;
1813 if (left_right_sides < left)
1814 cairo_translate (cr, left - left_right_sides, 0);
1816 if (left_right_sides < right)
1817 cairo_translate (cr, -(right - left_right_sides), 0);
1819 if (top_bottom_sides < top)
1820 cairo_translate (cr, 0, top - top_bottom_sides);
1822 if (top_bottom_sides < bottom)
1823 cairo_translate (cr, 0, -(bottom - top_bottom_sides));
1825 cairo_translate (cr, left, top);
1828 if (print->page_scale == EV_SCALE_FIT_TO_PRINTABLE_AREA || scale < 1.0) {
1829 cairo_scale (cr, scale, scale);
1833 ev_job_print_set_cairo (EV_JOB_PRINT (print->job_print), cr);
1834 ev_job_scheduler_push_job (print->job_print, EV_JOB_PRIORITY_NONE);
1838 ev_print_operation_print_create_custom_widget (EvPrintOperationPrint *print,
1839 GtkPrintContext *context)
1841 GtkPrintSettings *settings;
1844 EvPrintScale page_scale;
1845 gboolean autorotate;
1846 gboolean use_source_size;
1848 settings = gtk_print_operation_get_print_settings (print->op);
1849 page_scale = gtk_print_settings_get_int_with_default (settings, EV_PRINT_SETTING_PAGE_SCALE, 0);
1850 autorotate = gtk_print_settings_has_key (settings, EV_PRINT_SETTING_AUTOROTATE) ?
1851 gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_AUTOROTATE) :
1853 use_source_size = gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_PAGE_SIZE);
1855 table = gtk_table_new (3, 2, FALSE);
1856 gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1857 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1858 gtk_container_set_border_width (GTK_CONTAINER (table), 12);
1860 label = gtk_label_new (_("Page Scaling:"));
1861 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
1862 gtk_widget_show (label);
1864 print->scale_combo = gtk_combo_box_new_text ();
1865 gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("None"));
1866 gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("Shrink to Printable Area"));
1867 gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("Fit to Printable Area"));
1868 gtk_combo_box_set_active (GTK_COMBO_BOX (print->scale_combo), page_scale);
1869 gtk_widget_set_tooltip_text (print->scale_combo,
1870 _("Scale document pages to fit the selected printer page. Select from one of the following:\n"
1872 "• \"None\": No page scaling is performed.\n"
1874 "• \"Shrink to Printable Area\": Document pages larger than the printable area"
1875 " are reduced to fit the printable area of the printer page.\n"
1877 "• \"Fit to Printable Area\": Document pages are enlarged or reduced as"
1878 " required to fit the printable area of the printer page.\n"));
1879 gtk_table_attach (GTK_TABLE (table), print->scale_combo, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
1880 gtk_widget_show (print->scale_combo);
1882 print->autorotate_button = gtk_check_button_new_with_label (_("Auto Rotate and Center"));
1883 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->autorotate_button), autorotate);
1884 gtk_widget_set_tooltip_text (print->autorotate_button,
1885 _("Rotate printer page orientation of each page to match orientation of each document page. "
1886 "Document pages will be centered within the printer page."));
1887 gtk_table_attach (GTK_TABLE (table), print->autorotate_button, 0, 2, 1, 2, GTK_FILL, 0, 0, 0);
1888 gtk_widget_show (print->autorotate_button);
1890 print->source_button = gtk_check_button_new_with_label (_("Select page size using document page size"));
1891 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->source_button), use_source_size);
1892 gtk_widget_set_tooltip_text (print->source_button, _("When enabled, each page will be printed on "
1893 "the same size paper as the document page."));
1894 gtk_table_attach (GTK_TABLE (table), print->source_button, 0, 2, 2, 3, GTK_FILL, 0, 0, 0);
1895 gtk_widget_show (print->source_button);
1897 return G_OBJECT (table);
1901 ev_print_operation_print_custom_widget_apply (EvPrintOperationPrint *print,
1902 GtkPrintContext *context)
1904 GtkPrintSettings *settings;
1906 print->page_scale = gtk_combo_box_get_active (GTK_COMBO_BOX (print->scale_combo));
1907 print->autorotate = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->autorotate_button));
1908 print->use_source_size = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->source_button));
1909 settings = gtk_print_operation_get_print_settings (print->op);
1910 gtk_print_settings_set_int (settings, EV_PRINT_SETTING_PAGE_SCALE, print->page_scale);
1911 gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_AUTOROTATE, print->autorotate);
1912 gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_PAGE_SIZE, print->use_source_size);
1916 ev_print_operation_print_finalize (GObject *object)
1918 EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (object);
1921 g_object_unref (print->op);
1925 if (print->job_name) {
1926 g_free (print->job_name);
1927 print->job_name = NULL;
1930 if (print->job_print) {
1931 if (!ev_job_is_finished (print->job_print))
1932 ev_job_cancel (print->job_print);
1933 g_signal_handlers_disconnect_by_func (print->job_print,
1936 g_signal_handlers_disconnect_by_func (print->job_print,
1937 print_job_cancelled,
1939 g_object_unref (print->job_print);
1940 print->job_print = NULL;
1943 (* G_OBJECT_CLASS (ev_print_operation_print_parent_class)->finalize) (object);
1947 ev_print_operation_print_init (EvPrintOperationPrint *print)
1949 print->op = gtk_print_operation_new ();
1950 g_signal_connect_swapped (print->op, "begin_print",
1951 G_CALLBACK (ev_print_operation_print_begin_print),
1953 g_signal_connect_swapped (print->op, "done",
1954 G_CALLBACK (ev_print_operation_print_done),
1956 g_signal_connect_swapped (print->op, "draw_page",
1957 G_CALLBACK (ev_print_operation_print_draw_page),
1959 g_signal_connect_swapped (print->op, "status_changed",
1960 G_CALLBACK (ev_print_operation_print_status_changed),
1962 g_signal_connect_swapped (print->op, "request_page_setup",
1963 G_CALLBACK (ev_print_operation_print_request_page_setup),
1965 g_signal_connect_swapped (print->op, "create_custom_widget",
1966 G_CALLBACK (ev_print_operation_print_create_custom_widget),
1968 g_signal_connect_swapped (print->op, "custom_widget_apply",
1969 G_CALLBACK (ev_print_operation_print_custom_widget_apply),
1971 gtk_print_operation_set_allow_async (print->op, TRUE);
1972 gtk_print_operation_set_use_full_page (print->op, TRUE);
1973 gtk_print_operation_set_unit (print->op, GTK_UNIT_POINTS);
1974 gtk_print_operation_set_custom_tab_label (print->op, _("Page Handling"));
1978 ev_print_operation_print_class_init (EvPrintOperationPrintClass *klass)
1980 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
1981 EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass);
1983 ev_print_op_class->set_current_page = ev_print_operation_print_set_current_page;
1984 ev_print_op_class->set_print_settings = ev_print_operation_print_set_print_settings;
1985 ev_print_op_class->get_print_settings = ev_print_operation_print_get_print_settings;
1986 ev_print_op_class->set_default_page_setup = ev_print_operation_print_set_default_page_setup;
1987 ev_print_op_class->get_default_page_setup = ev_print_operation_print_get_default_page_setup;
1988 ev_print_op_class->set_job_name = ev_print_operation_print_set_job_name;
1989 ev_print_op_class->get_job_name = ev_print_operation_print_get_job_name;
1990 ev_print_op_class->run = ev_print_operation_print_run;
1991 ev_print_op_class->cancel = ev_print_operation_print_cancel;
1992 ev_print_op_class->get_error = ev_print_operation_print_get_error;
1993 ev_print_op_class->set_embed_page_setup = ev_print_operation_print_set_embed_page_setup;
1994 ev_print_op_class->get_embed_page_setup = ev_print_operation_print_get_embed_page_setup;
1996 g_object_class->finalize = ev_print_operation_print_finalize;
2000 ev_print_operation_exists_for_document (EvDocument *document)
2002 #if GTKUNIXPRINT_ENABLED
2003 return (EV_IS_FILE_EXPORTER(document) || EV_IS_DOCUMENT_PRINT(document));
2005 return EV_IS_DOCUMENT_PRINT(document);
2006 #endif /* GTKUNIXPRINT_ENABLED */
2009 /* Factory method */
2011 ev_print_operation_new (EvDocument *document)
2013 EvPrintOperation *op = NULL;
2015 g_return_val_if_fail (ev_print_operation_exists_for_document (document), NULL);
2017 if (EV_IS_DOCUMENT_PRINT (document))
2018 op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_PRINT,
2019 "document", document, NULL));
2021 #if GTKUNIXPRINT_ENABLED
2022 op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_EXPORT,
2023 "document", document, NULL));