changeset 1136:1a5b0908659b

Initial implementation of the DynamicWindows Print object for GTK3.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 09 Sep 2011 00:28:25 +0000
parents 6828a01ecf3c
children e24e5a13ff2c
files dw.h gtk3/dw.c
diffstat 2 files changed, 94 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/dw.h	Wed Sep 07 01:52:54 2011 +0000
+++ b/dw.h	Fri Sep 09 00:28:25 2011 +0000
@@ -1330,6 +1330,8 @@
    void *data, *result;
 } DWDialog;
 
+typedef void *HPRINT;
+
 #define DW_SIGNAL_FUNC(a) ((void *)a)
 
 #define DW_DESKTOP               HWND_DESKTOP
@@ -1685,5 +1687,8 @@
 HWND API dw_calendar_new(unsigned long id);
 void API dw_calendar_set_date( HWND window, unsigned int year, unsigned int month, unsigned int day );
 void API dw_calendar_get_date( HWND window, unsigned int *year, unsigned int *month, unsigned int *day );
+HPRINT API dw_print_new(unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata);
+int API dw_print_run(HPRINT print, unsigned long flags);
+void API dw_print_cancel(HPRINT print);
 
 #endif
--- a/gtk3/dw.c	Wed Sep 07 01:52:54 2011 +0000
+++ b/gtk3/dw.c	Fri Sep 09 00:28:25 2011 +0000
@@ -10193,8 +10193,95 @@
    DW_MUTEX_UNLOCK;
 }
 
-/*
- * Returns a pointer to a static buffer which containes the
+/* Internal function to create the drawable pixmap and call the function */
+static void _dw_draw_page(GtkPrintOperation *operation, GtkPrintContext *context, int page_nr)
+{
+   cairo_t *cr = gtk_print_context_get_cairo_context(context);
+   void *drawdata = g_object_get_data(G_OBJECT(operation), "_dw_drawdata");
+   int (*drawfunc)(HPRINT, HPIXMAP, int, void *) = g_object_get_data(G_OBJECT(operation), "_dw_drawfunc");
+   int result = 0;
+   HPIXMAP pixmap;
+
+   if(cr && drawfunc && (pixmap = calloc(1,sizeof(struct _hpixmap))))
+   {
+      pixmap->image = cairo_get_group_target(cr);
+      pixmap->handle = (HWND)operation;
+      pixmap->width = gtk_print_context_get_width(context);
+      pixmap->height = gtk_print_context_get_height(context);
+      result = drawfunc((HPRINT)operation, pixmap, page_nr, drawdata);
+      if(result)
+         gtk_print_operation_draw_page_finish(operation);
+      free(pixmap);
+   }
+}
+
+/*
+ * Creates a new print object.
+ * Parameters:
+ *       flags: Flags to initially configure the print object.
+ *       pages: Number of pages to print.
+ *       drawfunc: The pointer to the function to be used as the callback.
+ *       drawdata: User data to be passed to the handler function.
+ * Returns:
+ *       A handle to the print object.
+ */
+HPRINT API dw_print_new(unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata)
+{
+   GtkPrintOperation *op;
+   int _locked_by_me = FALSE;
+   
+   if(!drawfunc)
+      return NULL;
+
+   DW_MUTEX_LOCK;   
+   if((op = gtk_print_operation_new()))
+   {
+      gtk_print_operation_set_n_pages(op, pages);
+      g_object_set_data(G_OBJECT(op), "_dw_drawfunc", drawfunc);
+      g_object_set_data(G_OBJECT(op), "_dw_drawdata", drawdata);
+      g_signal_connect(op, "draw_page", G_CALLBACK(_dw_draw_page), NULL);
+   }
+   DW_MUTEX_UNLOCK;
+   return (HPRINT)op;
+}
+
+/*
+ * Runs the print job, causing the draw page callbacks to fire.
+ * Parameters:
+ *       print: Handle to the print object returned by dw_print_new().
+ *       flags: Flags to run the print job.
+ * Returns:
+ *       DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success.
+ */
+int API dw_print_run(HPRINT print, unsigned long flags)
+{
+   GtkPrintOperationResult res;
+   GtkPrintOperation *op = (GtkPrintOperation *)print;
+   int _locked_by_me = FALSE;
+   
+   DW_MUTEX_LOCK;
+   res = gtk_print_operation_run(op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, NULL, NULL);
+   DW_MUTEX_UNLOCK;
+   return (res == GTK_PRINT_OPERATION_RESULT_ERROR ? DW_ERROR_UNKNOWN : DW_ERROR_NONE);
+}
+
+/*
+ * Cancels the print job, typically called from a draw page callback.
+ * Parameters:
+ *       print: Handle to the print object returned by dw_print_new().
+ */
+void API dw_print_cancel(HPRINT print)
+{
+   int _locked_by_me = FALSE;
+   GtkPrintOperation *op = (GtkPrintOperation *)print;
+   
+   DW_MUTEX_LOCK;
+   gtk_print_operation_cancel(op);
+   DW_MUTEX_UNLOCK;
+}
+
+/*
+ * Returns a pointer to a static buffer which contains the
  * current user directory.  Or the root directory (C:\ on
  * OS/2 and Windows).
  */