Code

relocated unix print dialog outside of extension call path
authorkeescook <keescook@users.sourceforge.net>
Sun, 18 Feb 2007 07:36:56 +0000 (07:36 +0000)
committerkeescook <keescook@users.sourceforge.net>
Sun, 18 Feb 2007 07:36:56 +0000 (07:36 +0000)
src/extension/internal/ps.cpp
src/extension/internal/ps.h
src/print.cpp

index ffe55af691fc2576fd43712d5923009894188207..5c455ffe41175d80e42ad14889488f5206b4e3f7 100644 (file)
 #include <gtk/gtkentry.h>
 #include <gtk/gtktooltips.h>
 
-#ifdef HAVE_GTK_UNIX_PRINT
-#include <gtk/gtkprintunixdialog.h>
-#endif
-
 #include <glibmm/i18n.h>
 #include "display/nr-arena-item.h"
 #include "display/canvas-bpath.h"
@@ -106,61 +102,6 @@ PrintPS::~PrintPS(void)
     return;
 }
 
-#ifdef HAVE_GTK_UNIX_PRINT
-static void
-unix_print_complete (GtkPrintJob *print_job,
-                     gpointer user_data,
-                     GError *error)
-{
-    fprintf(stderr,"job finished: %s\n",error ? error->message : "no error");
-}
-
-static void
-unix_print_dialog (const gchar * ps_file, const gchar * jobname)
-{
-    GtkWidget* dlg = gtk_print_unix_dialog_new(_("Print"), NULL);
-
-/*
-    gtk_print_unix_dialog_add_custom_tab (GtkPrintUnixDialog *dialog,
-                                          GtkWidget *child,
-                                          GtkWidget *tab_label);
-*/
-
-    int const response = gtk_dialog_run(GTK_DIALOG(dlg));
-
-    if (response == GTK_RESPONSE_OK) {
-        GtkPrinter* printer = gtk_print_unix_dialog_get_selected_printer(GTK_PRINT_UNIX_DIALOG(dlg));
-
-        fprintf(stderr,"Selected printer '%s'\n",gtk_printer_get_name (printer));
-
-        if (gtk_printer_accepts_ps (printer)) {
-            GtkPrintJob* job = gtk_print_job_new  (jobname, printer,
-              gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dlg)),
-              gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dlg)));
-
-
-            GError * error = NULL;
-            if ( gtk_print_job_set_source_file (job, ps_file, &error)) {
-                fprintf(stderr,"sending...\n");
-                gtk_print_job_send (job, unix_print_complete, NULL, NULL);
-            }
-            else {
-                fprintf(stderr,"Could not set print source: %s\n",error ? error->message : "unknown error");
-            }
-        }
-        else {
-            fprintf(stderr,"Printer can't support PS output\n");
-        }
-    }
-    else if (response == GTK_RESPONSE_APPLY) {
-        fprintf(stderr,"preview not available\n");
-    }
-
-    gtk_widget_destroy(dlg);
-}
-#endif // HAVE_GTK_UNIX_PRINT
-
-
 unsigned int
 PrintPS::setup(Inkscape::Extension::Print * mod)
 {
@@ -171,6 +112,7 @@ PrintPS::setup(Inkscape::Extension::Print * mod)
 #endif
 
     unsigned int ret = FALSE;
+    gchar const *destination = mod->get_param_string("destination");
 
     /* Create dialog */
     GtkTooltips *tt = gtk_tooltips_new();
@@ -178,21 +120,13 @@ PrintPS::setup(Inkscape::Extension::Print * mod)
     gtk_object_sink((GtkObject *) tt);
 
     GtkWidget *dlg = gtk_dialog_new_with_buttons(
-#ifdef HAVE_GTK_UNIX_PRINT
-            _("Print Configuration"),
-#else
-            _("Print Destination"),
-#endif
+            destination ?  _("Print Configuration") : _("Print Destination"),
 //            SP_DT_WIDGET(SP_ACTIVE_DESKTOP)->window,
             NULL,
             (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_DESTROY_WITH_PARENT),
             GTK_STOCK_CANCEL,
             GTK_RESPONSE_CANCEL,
-#ifdef HAVE_GTK_UNIX_PRINT
-            GTK_STOCK_GO_FORWARD,
-#else
-            GTK_STOCK_PRINT,
-#endif
+            destination ? GTK_STOCK_GO_FORWARD : GTK_STOCK_PRINT,
             GTK_RESPONSE_OK,
             NULL);
 
@@ -248,32 +182,31 @@ PrintPS::setup(Inkscape::Extension::Print * mod)
     GtkWidget *l = gtk_label_new(_("Resolution:"));
     gtk_box_pack_end(GTK_BOX(hb), l, FALSE, FALSE, 0);
 
-#ifndef HAVE_GTK_UNIX_PRINT
-    /* Print destination frame */
-    f = gtk_frame_new(_("Print destination"));
-    gtk_box_pack_start(GTK_BOX(vbox), f, FALSE, FALSE, 4);
-    vb = gtk_vbox_new(FALSE, 4);
-    gtk_container_add(GTK_CONTAINER(f), vb);
-    gtk_container_set_border_width(GTK_CONTAINER(vb), 4);
-
-    l = gtk_label_new(_("Printer name (as given by lpstat -p);\n"
-                        "leave empty to use the system default printer.\n"
-                        "Use '> filename' to print to file.\n"
+    GtkWidget *e = NULL;
+    /* if the destination isn't already set, we must prompt for it */
+    if (!destination) {
+        /* Print destination frame */
+        f = gtk_frame_new(_("Print destination"));
+        gtk_box_pack_start(GTK_BOX(vbox), f, FALSE, FALSE, 4);
+        vb = gtk_vbox_new(FALSE, 4);
+        gtk_container_add(GTK_CONTAINER(f), vb);
+        gtk_container_set_border_width(GTK_CONTAINER(vb), 4);
+
+        l = gtk_label_new(_("Printer name (as given by lpstat -p);\n"
+                            "leave empty to use the system default printer.\n"
+                            "Use '> filename' to print to file.\n"
                         "Use '| prog arg...' to pipe to a program."));
-    gtk_box_pack_start(GTK_BOX(vb), l, FALSE, FALSE, 0);
+        gtk_box_pack_start(GTK_BOX(vb), l, FALSE, FALSE, 0);
 
-    GtkWidget *e = gtk_entry_new();
-    if (1) {
-        gchar const *val = mod->get_param_string("destination");
-        gtk_entry_set_text(GTK_ENTRY(e), ( val != NULL
-                                           ? val
+        e = gtk_entry_new();
+        gtk_entry_set_text(GTK_ENTRY(e), ( destination != NULL
+                                           ? destination
                                            : "" ));
-    }
-    gtk_box_pack_start(GTK_BOX(vb), e, FALSE, FALSE, 0);
+        gtk_box_pack_start(GTK_BOX(vb), e, FALSE, FALSE, 0);
 
-    // pressing enter in the destination field is the same as clicking Print:
-    gtk_entry_set_activates_default(GTK_ENTRY(e), TRUE);
-#endif
+        // pressing enter in the destination field is the same as clicking Print:
+        gtk_entry_set_activates_default(GTK_ENTRY(e), TRUE);
+    }
 
     gtk_widget_show_all(vbox);
 
@@ -288,24 +221,17 @@ PrintPS::setup(Inkscape::Extension::Print * mod)
         _bitmap = gtk_toggle_button_get_active((GtkToggleButton *) rb);
         sstr = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
         _dpi = (unsigned int) MAX((int)(atof(sstr)), 1);
-#ifndef HAVE_GTK_UNIX_PRINT
-        /* Arrgh, have to do something */
-        fn = gtk_entry_get_text(GTK_ENTRY(e));
-        /* skip leading whitespace, bug #1068483 */
-        while (fn && *fn==' ') { fn++; }
-        /* g_print("Printing to %s\n", fn); */
-
-        mod->set_param_string("destination", (gchar *)fn);
-#else
-        /* unix print dialog prints to a tempfile */
-        char * filename = strdup("/tmp/inkscape-ps-XXXXXX");
-        int tmpfd = mkstemp(filename);
-        close(tmpfd);
-        Glib::ustring dest = ">";
-        dest+=filename;
-        free(filename);
-        mod->set_param_string("destination", dest.c_str());
-#endif
+
+        /* if the destination was prompted for, record the new value */
+        if (e) {
+            /* Arrgh, have to do something */
+            fn = gtk_entry_get_text(GTK_ENTRY(e));
+            /* skip leading whitespace, bug #1068483 */
+            while (fn && *fn==' ') { fn++; }
+            /* g_print("Printing to %s\n", fn); */
+
+            mod->set_param_string("destination", (gchar *)fn);
+        }
         mod->set_param_bool("bitmap", _bitmap);
         mod->set_param_string("resolution", (gchar *)sstr);
         ret = TRUE;
@@ -364,7 +290,6 @@ PrintPS::begin(Inkscape::Extension::Print *mod, SPDocument *doc)
             epsexport = g_str_has_suffix(fn,".eps");
             while (isspace(*fn)) fn += 1;
             Inkscape::IO::dump_fopen_call(fn, "K");
-            _tmpfilename = fn;
             osf = Inkscape::IO::fopen_utf8name(fn, "w+");
             if (!osf) {
                 fprintf(stderr, "inkscape: fopen(%s): %s\n",
@@ -668,15 +593,6 @@ PrintPS::finish(Inkscape::Extension::Print *mod)
        _stream = _begin_stream;
     }
 
-#ifdef HAVE_GTK_UNIX_PRINT
-    /* redirect output to new print dialog */
-    fseek(_stream, 0, SEEK_SET);
-    Glib::ustring output = _tmpfilename;
-    unix_print_dialog(output.c_str(),"job name");
-    unlink(output.c_str());
-    /* end redirected new print dialog */
-#endif
-
     /* fixme: should really use pclose for popen'd streams */
     fclose(_stream);
     _stream = NULL;
index 5f3388d55c3eb17e38c6d77b73ce993cf1447c09..673cde82c4722e470546b3f31da17f112c3c40b8 100644 (file)
@@ -36,7 +36,6 @@ class PrintPS : public Inkscape::Extension::Implementation::Implementation {
     float _height;
     FILE * _begin_stream;//stream to print prolog and document setup of EPS, if font embedding
     FILE * _stream;//(main) stream to print the (E)PS output, or only the script part following prolog/document setup, if font embedding
-    Glib::ustring _tmpfilename;
 
     unsigned short _dpi;
     bool _bitmap;
index 624021c9a38291c728448fb5c0ef5a210abb4801..52a8d3c8236781337e53c6de3688396ad4c1e3fc 100644 (file)
 #include "extension/system.h"
 #include "print.h"
 
+#ifdef HAVE_GTK_UNIX_PRINT
+#include <gtk/gtk.h>
+#include <glibmm/i18n.h>
+#include <gtk/gtkprintunixdialog.h>
+#endif
+
 #if 0
 # include <extension/internal/ps.h>
 
@@ -130,11 +136,79 @@ sp_print_preview_document(SPDocument *doc)
     return;
 }
 
+#ifdef HAVE_GTK_UNIX_PRINT
+static void
+unix_print_complete (GtkPrintJob *print_job,
+                     gpointer user_data,
+                     GError *error)
+{
+    fprintf(stderr,"print job finished: %s\n",error ? error->message : "no error");
+}
+
+static void
+unix_print_dialog (const gchar * ps_file, const gchar * jobname)
+{
+    Glib::ustring title = _("Print");
+    title += " ";
+    title += jobname;
+    GtkWidget* dlg = gtk_print_unix_dialog_new(title.c_str(), NULL);
+
+    // force output system to only handle our pre-generated PS output
+    gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG(dlg),
+                                                   GTK_PRINT_CAPABILITY_GENERATE_PS);
+
+/*
+ * It would be nice to merge the PrintPS::setup routine with a custom
+ * configuration dialog:
+   
+    gtk_print_unix_dialog_add_custom_tab (GtkPrintUnixDialog *dialog,
+                                          GtkWidget *child,
+                                          GtkWidget *tab_label);
+*/
+
+    int const response = gtk_dialog_run(GTK_DIALOG(dlg));
+
+    if (response == GTK_RESPONSE_OK) {
+        GtkPrinter* printer = gtk_print_unix_dialog_get_selected_printer(GTK_PRINT_UNIX_DIALOG(dlg));
+
+        if (gtk_printer_accepts_ps (printer)) {
+            GtkPrintJob* job = gtk_print_job_new  (jobname, printer,
+              gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dlg)),
+              gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dlg)));
+
+
+            GError * error = NULL;
+            if ( gtk_print_job_set_source_file (job, ps_file, &error)) {
+                gtk_print_job_send (job, unix_print_complete, NULL, NULL);
+            }
+            else {
+                g_warning(_("Could not set print source: %s"),error ? error->message : _("unknown error"));
+            }
+            if (error) g_error_free(error);
+        }
+        else {
+            g_warning(_("Printer '%s' does not support PS output"), gtk_printer_get_name (printer));
+        }
+    }
+    else if (response == GTK_RESPONSE_APPLY) {
+        // since we didn't include the Preview capability,
+        // this should never happen.
+        g_warning(_("Print Preview not available"));
+    }
+
+    gtk_widget_destroy(dlg);
+}
+#endif // HAVE_GTK_UNIX_PRINT
+
+
 void
 sp_print_document(SPDocument *doc, unsigned int direct)
 {
     Inkscape::Extension::Print *mod;
     unsigned int ret;
+#ifdef HAVE_GTK_UNIX_PRINT
+    Glib::ustring tmpfile = "";
+#endif
 
     sp_document_ensure_up_to_date(doc);
 
@@ -142,6 +216,27 @@ sp_print_document(SPDocument *doc, unsigned int direct)
         mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_PS);
     } else {
         mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_DEFAULT);
+
+#ifdef HAVE_GTK_UNIX_PRINT
+        // unix print dialog reads from the exported tempfile
+        gchar  *filename = NULL;
+        GError *error    = NULL;
+        gint tmpfd = g_file_open_tmp("inkscape-ps-XXXXXX",
+                                     &filename,
+                                     &error);
+        if (tmpfd<0) {
+            g_warning(_("Failed to create tempfile for printing: %s"),
+                      error ? error->message : _("unknown error"));
+            if (error) g_error_free(error);
+            return;
+        }
+        tmpfile = filename;
+        g_free(filename);
+        close(tmpfd);
+
+        Glib::ustring destination = ">" + tmpfile;
+        mod->set_param_string("destination", destination.c_str());
+#endif
     }
 
     ret = mod->setup();
@@ -167,6 +262,13 @@ sp_print_document(SPDocument *doc, unsigned int direct)
         mod->root = NULL;
         nr_object_unref((NRObject *) mod->arena);
         mod->arena = NULL;
+
+#ifdef HAVE_GTK_UNIX_PRINT
+        // redirect output to new print dialog
+        unix_print_dialog(tmpfile.c_str(),doc->name ? doc->name : _("SVG Document"));
+        unlink(tmpfile.c_str());
+        // end redirected new print dialog
+#endif
     }
 
     return;