]> www.fi.muni.cz Git - evince.git/blobdiff - libview/ev-print-operation.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[evince.git] / libview / ev-print-operation.c
index 969414e3bdeab13e96a4db7c34cb6460f9bd9133..8c96dae4a225b85dd0b29d8438d901f3ee314704 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #include <config.h>
@@ -285,27 +285,21 @@ void
 ev_print_operation_set_embed_page_setup (EvPrintOperation *op,
                                         gboolean          embed)
 {
-#if GTK_CHECK_VERSION (2, 17, 4)
        EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
 
        g_return_if_fail (EV_IS_PRINT_OPERATION (op));
 
        class->set_embed_page_setup (op, embed);
-#endif
 }
 
 gboolean
 ev_print_operation_get_embed_page_setup (EvPrintOperation *op)
 {
-#if GTK_CHECK_VERSION (2, 17, 4)
        EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
 
        g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), FALSE);
 
        return class->get_embed_page_setup (op);
-#else
-       return FALSE;
-#endif
 }
 
 const gchar *
@@ -324,7 +318,6 @@ ev_print_operation_get_progress (EvPrintOperation *op)
        return op->progress;
 }
 
-#if GTK_CHECK_VERSION (2, 17, 1) | GTKUNIXPRINT_ENABLED
 static void
 ev_print_operation_update_status (EvPrintOperation *op,
                                  gint              page,
@@ -338,11 +331,11 @@ ev_print_operation_update_status (EvPrintOperation *op,
 
        if (page == -1) {
                /* Initial state */
-               op->status = g_strdup (_("Preparing to print ..."));
+               op->status = g_strdup (_("Preparing to print"));
        } else if (page > n_pages) {
-               op->status = g_strdup (_("Finishing..."));
+               op->status = g_strdup (_("Finishing"));
        } else {
-               op->status = g_strdup_printf (_("Printing page %d of %d..."),
+               op->status = g_strdup_printf (_("Printing page %d of %d"),
                                              page, n_pages);
        }
 
@@ -350,7 +343,6 @@ ev_print_operation_update_status (EvPrintOperation *op,
 
        g_signal_emit (op, signals[STATUS_CHANGED], 0);
 }
-#endif
 
 #if GTKUNIXPRINT_ENABLED
 
@@ -863,33 +855,34 @@ export_print_done (EvPrintOperationExport *export)
                g_key_file_free (key_file);
 
                if (!error) {
-                       gint    argc;
-                       gchar **argv;
                        gchar  *cmd;
                        gchar  *quoted_filename;
                        gchar  *quoted_settings_filename;
+                        GAppInfo *app;
+                        GdkAppLaunchContext *ctx;
 
                        quoted_filename = g_shell_quote (export->temp_file);
                        quoted_settings_filename = g_shell_quote (print_settings_file);
                        cmd = g_strdup_printf ("evince-previewer --unlink-tempfile --print-settings %s %s",
                                               quoted_settings_filename, quoted_filename);
 
-                       g_shell_parse_argv (cmd, &argc, &argv, &error);
-
                        g_free (quoted_filename);
                        g_free (quoted_settings_filename);
-                       g_free (cmd);
 
-                       if (!error) {
-                               gdk_spawn_on_screen (gtk_window_get_screen (export->parent_window),
-                                                    NULL, argv, NULL,
-                                                    G_SPAWN_SEARCH_PATH,
-                                                    NULL, NULL, NULL,
-                                                    &error);
-                       }
+                       app = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
 
-                       g_strfreev (argv);
-               }
+                       if (app != NULL) {
+                               ctx = gdk_display_get_app_launch_context (gtk_widget_get_display (GTK_WIDGET (export->parent_window)));
+                               gdk_app_launch_context_set_screen (ctx, gtk_window_get_screen (export->parent_window));
+
+                               g_app_info_launch (app, NULL, G_APP_LAUNCH_CONTEXT (ctx), &error);
+
+                               g_object_unref (app);
+                               g_object_unref (ctx);
+                        }
+
+                       g_free (cmd);
+                }
 
                if (error) {
                        if (print_settings_file)
@@ -1234,7 +1227,7 @@ ev_print_operation_export_print_dialog_response_cb (GtkDialog              *dial
                                                 "%s", _("Invalid page selection"));
                gtk_window_set_title (GTK_WINDOW (message_dialog), _("Warning"));
                gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (message_dialog),
-                                                         "%s", _("Your print range selection does not include any page"));
+                                                         "%s", _("Your print range selection does not include any pages"));
                g_signal_connect (message_dialog, "response",
                                  G_CALLBACK (gtk_widget_destroy),
                                  NULL);
@@ -1319,10 +1312,9 @@ ev_print_operation_export_run (EvPrintOperation *op,
                ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document));
        gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG (dialog),
                                                       capabilities);
-#if GTK_CHECK_VERSION (2, 17, 4)
+
        gtk_print_unix_dialog_set_embed_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
                                                    export->embed_page_setup);
-#endif
 
        gtk_print_unix_dialog_set_current_page (GTK_PRINT_UNIX_DIALOG (dialog),
                                                export->current_page);
@@ -1368,11 +1360,9 @@ static void
 ev_print_operation_export_set_embed_page_setup (EvPrintOperation *op,
                                                gboolean          embed)
 {
-#if GTK_CHECK_VERSION (2, 17, 4)
        EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
 
        export->embed_page_setup = embed;
-#endif
 }
 
 static gboolean
@@ -1502,7 +1492,6 @@ ev_print_operation_export_class_init (EvPrintOperationExportClass *klass)
 
 #endif /* GTKUNIXPRINT_ENABLED */
 
-#if GTK_CHECK_VERSION (2, 17, 1)
 /* Print to cairo interface */
 #define EV_TYPE_PRINT_OPERATION_PRINT         (ev_print_operation_print_get_type())
 #define EV_PRINT_OPERATION_PRINT(object)      (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrint))
@@ -1521,6 +1510,8 @@ typedef enum {
 } EvPrintScale;
 
 #define EV_PRINT_SETTING_PAGE_SCALE "evince-print-setting-page-scale"
+#define EV_PRINT_SETTING_AUTOROTATE "evince-print-setting-page-autorotate"
+#define EV_PRINT_SETTING_PAGE_SIZE  "evince-print-setting-page-size"
 
 struct _EvPrintOperationPrint {
        EvPrintOperation parent;
@@ -1534,6 +1525,10 @@ struct _EvPrintOperationPrint {
         /* Page handling tab */
         GtkWidget   *scale_combo;
         EvPrintScale page_scale;
+       GtkWidget   *autorotate_button;
+       gboolean     autorotate;
+       GtkWidget   *source_button;
+       gboolean     use_source_size;
 };
 
 struct _EvPrintOperationPrintClass {
@@ -1628,7 +1623,10 @@ ev_print_operation_print_cancel (EvPrintOperation *op)
 {
        EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
 
-       gtk_print_operation_cancel (print->op);
+        if (print->job_print)
+                ev_job_cancel (print->job_print);
+        else
+                gtk_print_operation_cancel (print->op);
 }
 
 static void
@@ -1644,23 +1642,17 @@ static void
 ev_print_operation_print_set_embed_page_setup (EvPrintOperation *op,
                                               gboolean          embed)
 {
-#if GTK_CHECK_VERSION (2, 17, 4)
        EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
 
        gtk_print_operation_set_embed_page_setup (print->op, embed);
-#endif
 }
 
 static gboolean
 ev_print_operation_print_get_embed_page_setup (EvPrintOperation *op)
 {
-#if GTK_CHECK_VERSION (2, 17, 4)
        EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op);
 
        return gtk_print_operation_get_embed_page_setup (print->op);
-#else
-       return FALSE;
-#endif
 }
 
 static void
@@ -1691,13 +1683,11 @@ ev_print_operation_print_done (EvPrintOperationPrint  *print,
 static void
 ev_print_operation_print_status_changed (EvPrintOperationPrint *print)
 {
-#ifdef HAVE_GTK_PRINT_OPERATION_GET_N_PAGES_TO_PRINT
        GtkPrintStatus status;
 
        status = gtk_print_operation_get_status (print->op);
        if (status == GTK_PRINT_STATUS_GENERATING_DATA)
                print->n_pages_to_print = gtk_print_operation_get_n_pages_to_print (print->op);
-#endif
 }
 
 static void
@@ -1707,20 +1697,38 @@ print_job_finished (EvJobPrint            *job,
        EvPrintOperation *op = EV_PRINT_OPERATION (print);
 
        gtk_print_operation_draw_page_finish (print->op);
-#ifdef HAVE_GTK_PRINT_OPERATION_GET_N_PAGES_TO_PRINT
+
        print->total++;
        ev_print_operation_update_status (op, print->total,
                                          print->n_pages_to_print,
                                          print->total / (gdouble)print->n_pages_to_print);
-#endif
        ev_job_print_set_cairo (job, NULL);
 }
 
+static gboolean
+draw_page_finish_idle (EvPrintOperationPrint *print)
+{
+        if (ev_job_scheduler_get_running_thread_job () == print->job_print)
+                return TRUE;
+
+        gtk_print_operation_draw_page_finish (print->op);
+
+        return FALSE;
+}
+
 static void
 print_job_cancelled (EvJobPrint            *job,
-                    EvPrintOperationPrint *print)
+                     EvPrintOperationPrint *print)
 {
-       gtk_print_operation_cancel (print->op);
+        /* Finish the current page, so that draw-page
+         * is emitted again and it will cancel the
+         * print operation. If the job is still
+         * running, wait until it finishes.
+         */
+        if (ev_job_scheduler_get_running_thread_job () == print->job_print)
+                g_idle_add ((GSourceFunc)draw_page_finish_idle, print);
+        else
+                gtk_print_operation_draw_page_finish (print->op);
 }
 
 static void
@@ -1731,13 +1739,24 @@ ev_print_operation_print_request_page_setup (EvPrintOperationPrint *print,
 {
        EvPrintOperation *op = EV_PRINT_OPERATION (print);
        gdouble           width, height;
+       GtkPaperSize     *paper_size;
 
        ev_document_get_page_size (op->document, page_nr,
                                   &width, &height);
-       if (width > height)
-               gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
-       else
-               gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_PORTRAIT);
+
+       if (print->use_source_size) {
+               paper_size = gtk_paper_size_new_custom ("custom", "custom",
+                                                       width, height, GTK_UNIT_POINTS);
+               gtk_page_setup_set_paper_size_and_default_margins (setup, paper_size);
+               gtk_paper_size_free (paper_size);
+       }
+
+       if (print->autorotate) {
+               if (width > height)
+                       gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
+               else
+                       gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_PORTRAIT);
+       }
 }
 
 static void
@@ -1747,22 +1766,12 @@ _print_context_get_hard_margins (GtkPrintContext *context,
                                 gdouble         *left,
                                 gdouble         *right)
 {
-#if GTK_CHECK_VERSION (2, 19, 2)
        if (!gtk_print_context_get_hard_margins (context, top, bottom, left, right)) {
                *top = 0;
                *bottom = 0;
                *left = 0;
                *right = 0;
        }
-#else
-       GtkPageSetup *page_setup;
-
-       page_setup = gtk_print_context_get_page_setup (context);
-       *top = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_POINTS);
-       *bottom = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_POINTS);
-       *left = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_POINTS);
-       *right = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_POINTS);
-#endif
 }
 
 static void
@@ -1784,22 +1793,29 @@ ev_print_operation_print_draw_page (EvPrintOperationPrint *print,
                g_signal_connect (G_OBJECT (print->job_print), "finished",
                                  G_CALLBACK (print_job_finished),
                                  (gpointer)print);
-               g_signal_connect (G_OBJECT (print->job_print), "cancelled",
-                                 G_CALLBACK (print_job_cancelled),
-                                 (gpointer)print);
-       }
+                g_signal_connect (G_OBJECT (print->job_print), "cancelled",
+                                  G_CALLBACK (print_job_cancelled),
+                                  (gpointer)print);
+       } else if (g_cancellable_is_cancelled (print->job_print->cancellable)) {
+                gtk_print_operation_cancel (print->op);
+                ev_job_print_set_cairo (EV_JOB_PRINT (print->job_print), NULL);
+                return;
+        }
 
        ev_job_print_set_page (EV_JOB_PRINT (print->job_print), page);
 
        cr = gtk_print_context_get_cairo_context (context);
        cr_width = gtk_print_context_get_width (context);
        cr_height = gtk_print_context_get_height (context);
-       ev_document_get_page_size (op->document, page,
-                                  &width, &height);
+       ev_document_get_page_size (op->document, page, &width, &height);
 
-       _print_context_get_hard_margins (context, &top, &bottom, &left, &right);
+       if (print->page_scale == EV_SCALE_NONE) {
+               /* Center document page on the printed page */
+               if (print->autorotate)
+                       cairo_translate (cr, (cr_width - width) / 2, (cr_height - height) / 2);
+       } else {
+               _print_context_get_hard_margins (context, &top, &bottom, &left, &right);
 
-       if (print->page_scale != EV_SCALE_NONE) {
                x_scale = (cr_width - left - right) / width;
                y_scale = (cr_height - top - bottom) / height;
 
@@ -1808,8 +1824,35 @@ ev_print_operation_print_draw_page (EvPrintOperationPrint *print,
                else
                        scale = y_scale;
 
-               if (print->page_scale == EV_SCALE_FIT_TO_PRINTABLE_AREA || scale < 1.0) {
+               if (print->autorotate) {
+                       double left_right_sides, top_bottom_sides;
+
+                       cairo_translate (cr, (cr_width - scale * width) / 2,
+                                        (cr_height - scale * height) / 2);
+
+                       /* Ensure document page is within the margins. The
+                        * scale guarantees the document will fit in the
+                        * margins so we just need to check each side and
+                        * if it overhangs the margin, translate it to the
+                        * margin. */
+                       left_right_sides = (cr_width - width*scale)/2;
+                       top_bottom_sides = (cr_height - height*scale)/2;
+                       if (left_right_sides < left)
+                               cairo_translate (cr, left - left_right_sides, 0);
+
+                       if (left_right_sides < right)
+                               cairo_translate (cr, -(right - left_right_sides), 0);
+
+                       if (top_bottom_sides < top)
+                               cairo_translate (cr, 0, top - top_bottom_sides);
+
+                       if (top_bottom_sides < bottom)
+                               cairo_translate (cr, 0, -(bottom - top_bottom_sides));
+               } else {
                        cairo_translate (cr, left, top);
+               }
+
+               if (print->page_scale == EV_SCALE_FIT_TO_PRINTABLE_AREA || scale < 1.0) {
                        cairo_scale (cr, scale, scale);
                }
        }
@@ -1826,11 +1869,17 @@ ev_print_operation_print_create_custom_widget (EvPrintOperationPrint *print,
        GtkWidget        *label;
        GtkWidget        *table;
        EvPrintScale      page_scale;
+       gboolean          autorotate;
+       gboolean          use_source_size;
 
        settings = gtk_print_operation_get_print_settings (print->op);
-       page_scale = gtk_print_settings_get_int_with_default (settings, EV_PRINT_SETTING_PAGE_SCALE, 0);
+       page_scale = gtk_print_settings_get_int_with_default (settings, EV_PRINT_SETTING_PAGE_SCALE, 1);
+       autorotate = gtk_print_settings_has_key (settings, EV_PRINT_SETTING_AUTOROTATE) ?
+               gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_AUTOROTATE) :
+               TRUE;
+       use_source_size = gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_PAGE_SIZE);
 
-       table = gtk_table_new (1, 2, FALSE);
+       table = gtk_table_new (3, 2, FALSE);
        gtk_table_set_row_spacings (GTK_TABLE (table), 6);
        gtk_table_set_col_spacings (GTK_TABLE (table), 12);
        gtk_container_set_border_width (GTK_CONTAINER (table), 12);
@@ -1839,10 +1888,10 @@ ev_print_operation_print_create_custom_widget (EvPrintOperationPrint *print,
        gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
        gtk_widget_show (label);
 
-       print->scale_combo = gtk_combo_box_new_text ();
-       gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("None"));
-       gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("Shrink to Printable Area"));
-       gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("Fit to Printable Area"));
+       print->scale_combo = gtk_combo_box_text_new ();
+       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("None"));
+       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("Shrink to Printable Area"));
+       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("Fit to Printable Area"));
        gtk_combo_box_set_active (GTK_COMBO_BOX (print->scale_combo), page_scale);
        gtk_widget_set_tooltip_text (print->scale_combo,
                _("Scale document pages to fit the selected printer page. Select from one of the following:\n"
@@ -1850,13 +1899,28 @@ ev_print_operation_print_create_custom_widget (EvPrintOperationPrint *print,
                  "• \"None\": No page scaling is performed.\n"
                  "\n"
                  "• \"Shrink to Printable Area\": Document pages larger than the printable area"
-                 " are reduced fit the printable area of the printer page.\n"
+                 " are reduced to fit the printable area of the printer page.\n"
                  "\n"
                  "• \"Fit to Printable Area\": Document pages are enlarged or reduced as"
                  " required to fit the printable area of the printer page.\n"));
        gtk_table_attach (GTK_TABLE (table), print->scale_combo, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
        gtk_widget_show (print->scale_combo);
 
+       print->autorotate_button = gtk_check_button_new_with_label (_("Auto Rotate and Center"));
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->autorotate_button), autorotate);
+       gtk_widget_set_tooltip_text (print->autorotate_button,
+               _("Rotate printer page orientation of each page to match orientation of each document page. "
+                 "Document pages will be centered within the printer page."));
+       gtk_table_attach (GTK_TABLE (table), print->autorotate_button, 0, 2, 1, 2, GTK_FILL, 0, 0, 0);
+       gtk_widget_show (print->autorotate_button);
+
+       print->source_button = gtk_check_button_new_with_label (_("Select page size using document page size"));
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->source_button), use_source_size);
+       gtk_widget_set_tooltip_text (print->source_button, _("When enabled, each page will be printed on "
+                                                            "the same size paper as the document page."));
+       gtk_table_attach (GTK_TABLE (table), print->source_button, 0, 2, 2, 3, GTK_FILL, 0, 0, 0);
+       gtk_widget_show (print->source_button);
+
        return G_OBJECT (table);
 }
 
@@ -1867,8 +1931,12 @@ ev_print_operation_print_custom_widget_apply (EvPrintOperationPrint *print,
        GtkPrintSettings *settings;
 
        print->page_scale = gtk_combo_box_get_active (GTK_COMBO_BOX (print->scale_combo));
+       print->autorotate = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->autorotate_button));
+       print->use_source_size = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->source_button));
        settings = gtk_print_operation_get_print_settings (print->op);
        gtk_print_settings_set_int (settings, EV_PRINT_SETTING_PAGE_SCALE, print->page_scale);
+       gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_AUTOROTATE, print->autorotate);
+       gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_PAGE_SIZE, print->use_source_size);
 }
 
 static void
@@ -1954,22 +2022,14 @@ ev_print_operation_print_class_init (EvPrintOperationPrintClass *klass)
 
        g_object_class->finalize = ev_print_operation_print_finalize;
 }
-#endif /* GTK_CHECK_VERSION (2, 17, 1) */
 
-gboolean ev_print_operation_exists_for_document (EvDocument *document)
+gboolean
+ev_print_operation_exists_for_document (EvDocument *document)
 {
 #if GTKUNIXPRINT_ENABLED
-#if GTK_CHECK_VERSION (2, 17, 1)
        return (EV_IS_FILE_EXPORTER(document) || EV_IS_DOCUMENT_PRINT(document));
 #else
-       return EV_IS_FILE_EXPORTER(document);
-#endif
-#else /* ! GTKUNIXPRINT_ENABLED */
-#if GTK_CHECK_VERSION (2, 17, 1)
        return EV_IS_DOCUMENT_PRINT(document);
-#else
-       return FALSE;
-#endif
 #endif /* GTKUNIXPRINT_ENABLED */
 }
 
@@ -1981,15 +2041,15 @@ ev_print_operation_new (EvDocument *document)
 
        g_return_val_if_fail (ev_print_operation_exists_for_document (document), NULL);
 
-#if GTK_CHECK_VERSION (2, 17, 1)
        if (EV_IS_DOCUMENT_PRINT (document))
                op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_PRINT,
                                                       "document", document, NULL));
        else
-#endif
 #if GTKUNIXPRINT_ENABLED
                op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_EXPORT,
                                                       "document", document, NULL));
+#else
+               op = NULL;
 #endif
        return op;
 }