print progress and cancellation



Here is a quick patch to make  GtkPrintOperation show a
progress dialog while the print operation is running. It also
allows the user to cancel the print operation.

To get the dialog, you need to call

gtk_print_operation_set_show_progress (op, TRUE)

Comments ?

Matthias
Index: gtk/gtk.symbols
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtk.symbols,v
retrieving revision 1.108
diff -u -r1.108 gtk.symbols
--- gtk/gtk.symbols	19 May 2006 19:25:50 -0000	1.108
+++ gtk/gtk.symbols	19 May 2006 20:10:32 -0000
@@ -2696,6 +2696,7 @@
 gtk_print_operation_set_show_dialog
 gtk_print_operation_set_pdf_target
 gtk_print_operation_set_track_print_status
+gtk_print_operation_set_show_progress
 gtk_print_operation_run
 gtk_print_operation_run_async
 gtk_print_operation_get_status
Index: gtk/gtkprintoperation-private.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkprintoperation-private.h,v
retrieving revision 1.7
diff -u -r1.7 gtkprintoperation-private.h
--- gtk/gtkprintoperation-private.h	19 May 2006 19:25:50 -0000	1.7
+++ gtk/gtkprintoperation-private.h	19 May 2006 20:10:32 -0000
@@ -39,6 +39,7 @@
   guint use_full_page      : 1;
   guint show_dialog        : 1;
   guint track_print_status : 1;
+  guint show_progress      : 1;
   guint cancelled          : 1;
 
   guint print_pages_idle_id;
@@ -76,6 +77,7 @@
 									  GError           **error);
 
 typedef void (* GtkPrintOperationPrintFunc) (GtkPrintOperation *op,
+					     GtkWindow         *parent,
 					     gboolean           wait);
 
 void _gtk_print_operation_platform_backend_run_dialog_async (GtkPrintOperation          *op,
Index: gtk/gtkprintoperation-unix.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkprintoperation-unix.c,v
retrieving revision 1.12
diff -u -r1.12 gtkprintoperation-unix.c
--- gtk/gtkprintoperation-unix.c	15 May 2006 16:22:37 -0000	1.12
+++ gtk/gtkprintoperation-unix.c	19 May 2006 20:10:32 -0000
@@ -274,7 +274,7 @@
   if (rdata->print_cb)
     {
       if (rdata->do_print)
-        rdata->print_cb (op, FALSE); 
+        rdata->print_cb (op, rdata->parent, FALSE); 
       else
        _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL); 
     }
Index: gtk/gtkprintoperation-win32.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkprintoperation-win32.c,v
retrieving revision 1.6
diff -u -r1.6 gtkprintoperation-win32.c
--- gtk/gtkprintoperation-win32.c	17 May 2006 07:36:59 -0000	1.6
+++ gtk/gtkprintoperation-win32.c	19 May 2006 20:10:33 -0000
@@ -1479,7 +1479,7 @@
 
   _gtk_print_operation_platform_backend_run_dialog (op, parent, &do_print, NULL);
   if (do_print)
-    print_cb (op, FALSE);
+    print_cb (op, parent, FALSE);
   else
     _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
 }
Index: gtk/gtkprintoperation.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkprintoperation.c,v
retrieving revision 1.15
diff -u -r1.15 gtkprintoperation.c
--- gtk/gtkprintoperation.c	19 May 2006 19:25:50 -0000	1.15
+++ gtk/gtkprintoperation.c	19 May 2006 20:10:34 -0000
@@ -25,6 +25,7 @@
 #include <cairo-pdf.h>
 #include "gtkintl.h"
 #include "gtkprivate.h"
+#include "gtkmessagedialog.h"
 #include "gtkalias.h"
 
 #define GTK_PRINT_OPERATION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_PRINT_OPERATION, GtkPrintOperationPrivate))
@@ -50,6 +51,7 @@
   PROP_TRACK_PRINT_STATUS,
   PROP_UNIT,
   PROP_SHOW_DIALOG,
+  PROP_SHOW_PROGRESS,
   PROP_PDF_TARGET,
   PROP_STATUS,
   PROP_STATUS_STRING
@@ -122,6 +124,7 @@
   priv->current_page = -1;
   priv->use_full_page = FALSE;
   priv->show_dialog = TRUE;
+  priv->show_progress = FALSE;
   priv->pdf_target = NULL;
   priv->track_print_status = FALSE;
 
@@ -168,6 +171,9 @@
     case PROP_SHOW_DIALOG:
       gtk_print_operation_set_show_dialog (op, g_value_get_boolean (value));
       break;
+    case PROP_SHOW_PROGRESS:
+      gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
+      break;
     case PROP_PDF_TARGET:
       gtk_print_operation_set_pdf_target (op, g_value_get_string (value));
       break;
@@ -215,6 +221,9 @@
     case PROP_SHOW_DIALOG:
       g_value_set_boolean (value, priv->show_dialog);
       break;
+    case PROP_SHOW_PROGRESS:
+      g_value_set_boolean (value, priv->show_progress);
+      break;
     case PROP_PDF_TARGET:
       g_value_set_string (value, priv->pdf_target);
       break;
@@ -573,6 +582,24 @@
 							 TRUE,
 							 GTK_PARAM_READWRITE));
   
+
+  /**
+   * GtkPrintOperation:show-progress:
+   *
+   * Determines whether to show a progress dialog during the 
+   * print operation.
+   *
+   * Since: 2.10
+   */
+  g_object_class_install_property (gobject_class,
+				   PROP_SHOW_PROGRESS,
+				   g_param_spec_boolean ("show-progress",
+							 P_("Show Dialog"),
+							 P_("TRUE if a progress dialog is shown while printing."),
+							 FALSE,
+							 GTK_PARAM_READWRITE));
+  
+  
   /**
    * GtkPrintOperation:pdf-target:
    *
@@ -972,7 +999,6 @@
     }
 }
 
-
 void
 _gtk_print_operation_set_status (GtkPrintOperation *op,
 				 GtkPrintStatus     status,
@@ -1122,6 +1148,37 @@
     }
 }
 
+
+/**
+ * gtk_print_operation_set_show_progress:
+ * @op: a #GtkPrintOperation
+ * @show_progress: %TRUE to show a progress dialog
+ * 
+ * If @show_progress is %TRUE, the print operation will show a 
+ * progress dialog during the print operation.
+ * 
+ * Since: 2.10
+ */
+void
+gtk_print_operation_set_show_progress (GtkPrintOperation  *op,
+				       gboolean            show_progress)
+{
+  GtkPrintOperationPrivate *priv;
+
+  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
+
+  priv = op->priv;
+
+  show_progress = show_progress != FALSE;
+
+  if (priv->show_progress != show_progress)
+    {
+      priv->show_progress = show_progress;
+
+      g_object_notify (G_OBJECT (op), "show-progress");
+    }
+}
+
 /**
  * gtk_print_operation_set_pdf_target:
  * @op: a #GtkPrintOperation
@@ -1284,7 +1341,7 @@
   GtkPrintOperation *op;
   gint uncollated_copies;
   gint collated_copies;
-  gint uncollated, collated;
+  gint uncollated, collated, total;
 
   gint range, num_ranges;
   GtkPageRange *ranges;
@@ -1294,6 +1351,8 @@
 
   GtkPageSetup *initial_page_setup;
   GtkPrintContext *print_context;  
+
+  GtkWidget *progress;
 } PrintPagesData;
 
 static void
@@ -1351,6 +1410,9 @@
   data = (PrintPagesData*)user_data;
   data->op->priv->print_pages_idle_id = 0;
 
+  if (data->progress)
+    gtk_widget_destroy (data->progress);
+
   g_object_unref (data->print_context);
   g_object_unref (data->initial_page_setup);
 
@@ -1358,6 +1420,34 @@
   g_free (data);
 }
 
+static void
+update_progress (PrintPagesData *data)
+{
+  GtkPrintOperationPrivate *priv; 
+  gchar *text = NULL;
+  
+  priv = data->op->priv;
+ 
+  if (data->progress)
+    {
+      if (priv->status == GTK_PRINT_STATUS_PREPARING)
+	{
+	  if (priv->nr_of_pages > 0)
+	    text = g_strdup_printf (_("Preparing %d"), priv->nr_of_pages);
+	  else
+	    text = g_strdup (_("Preparing"));
+	}
+      else if (priv->status == GTK_PRINT_STATUS_GENERATING_DATA)
+	text = g_strdup_printf (_("Printing %d"), data->total);
+      
+      if (text)
+	{
+	  g_object_set (data->progress, "text", text, NULL);
+	  g_free (text);
+	}
+    }
+ }
+
 static gboolean
 print_pages_idle (gpointer user_data)
 {
@@ -1432,6 +1522,7 @@
       goto out;
     }
 
+  data->total++;
   data->collated++;
   if (data->collated == data->collated_copies)
     {
@@ -1491,6 +1582,8 @@
       
       done = TRUE;
     }
+ 
+  update_progress (data);
 
   GDK_THREADS_LEAVE ();
 
@@ -1498,7 +1591,19 @@
 }
   
 static void
+handle_progress_response (GtkWidget *dialog, 
+			  gint       response,
+			  gpointer   data)
+{
+  GtkPrintOperation *op = (GtkPrintOperation *)data;
+
+  gtk_widget_hide (dialog);
+  gtk_print_operation_cancel (op);
+}
+
+static void
 print_pages (GtkPrintOperation *op,
+	     GtkWindow         *parent,
 	     gboolean           wait)
 {
   GtkPrintOperationPrivate *priv = op->priv;
@@ -1506,9 +1611,21 @@
   GtkPrintContext *print_context;
   int uncollated_copies, collated_copies;
   PrintPagesData *data;
+  GtkWidget *progress;
  
   _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);  
 
+  if (priv->show_progress)
+    {
+      progress = gtk_message_dialog_new (parent, 0, 
+					 GTK_MESSAGE_OTHER,
+					 GTK_BUTTONS_CANCEL,
+					 _("Preparing"));
+      g_signal_connect (progress, "response", 
+			G_CALLBACK (handle_progress_response), op);
+      gtk_window_present (progress);
+    }
+
   print_context = _gtk_print_context_new (op);
 
   initial_page_setup = create_page_setup (op);
@@ -1533,6 +1650,7 @@
   data->collated_copies = collated_copies;
   data->initial_page_setup = initial_page_setup;
   data->print_context = print_context;
+  data->progress = progress;
 
   if (wait)
     {
@@ -1601,7 +1719,7 @@
 							       &do_print,
 							       error);
   if (do_print)
-    print_pages (op, TRUE);
+    print_pages (op, parent, TRUE);
   else 
     _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
 
@@ -1642,7 +1760,7 @@
     {
       run_pdf (op, parent, &do_print, NULL);
       if (do_print)
-	print_pages (op, FALSE);
+	print_pages (op, parent, FALSE);
       else 
 	_gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
     }
Index: gtk/gtkprintoperation.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkprintoperation.h,v
retrieving revision 1.7
diff -u -r1.7 gtkprintoperation.h
--- gtk/gtkprintoperation.h	19 May 2006 19:25:50 -0000	1.7
+++ gtk/gtkprintoperation.h	19 May 2006 20:10:34 -0000
@@ -132,6 +132,8 @@
 								    const gchar        *filename);
 void                    gtk_print_operation_set_track_print_status (GtkPrintOperation  *op,
 								    gboolean            track_status);
+void                    gtk_print_operation_set_show_progress      (GtkPrintOperation  *op,
+								    gboolean            show_progress);
 GtkPrintOperationResult gtk_print_operation_run                    (GtkPrintOperation  *op,
 								    GtkWindow          *parent,
 								    GError            **error);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]