[gtk: 1/4] Fixed bug that prevented GTK from storing printer driver preferences in the print dialogue on Window



commit fc15a409cc328771eb37bb44cdc1322158e6e935
Author: Rick Yorgason <rick firefang com>
Date:   Sat Oct 27 06:00:20 2018 -0700

    Fixed bug that prevented GTK from storing printer driver preferences in the print dialogue on Windows.

 gtk/gtkprintoperation-win32.c | 77 ++++++++++++++++++++++++++++---------------
 1 file changed, 51 insertions(+), 26 deletions(-)
---
diff --git a/gtk/gtkprintoperation-win32.c b/gtk/gtkprintoperation-win32.c
index c484209f5b..9185c04d25 100644
--- a/gtk/gtkprintoperation-win32.c
+++ b/gtk/gtkprintoperation-win32.c
@@ -952,9 +952,10 @@ dialog_to_print_settings (GtkPrintOperation *op,
 
 static HANDLE
 devmode_from_settings (GtkPrintSettings *settings,
-                      GtkPageSetup *page_setup)
+                      GtkPageSetup *page_setup,
+                      HANDLE hDevModeParam)
 {
-  HANDLE hDevMode;
+  HANDLE hDevMode = hDevModeParam;
   LPDEVMODEW devmode;
   guchar *extras;
   GtkPaperSize *paper_size;
@@ -962,31 +963,42 @@ devmode_from_settings (GtkPrintSettings *settings,
   gsize extras_len;
   const char *val;
 
-  extras = NULL;
-  extras_len = 0;
-  extras_base64 = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_WIN32_DRIVER_EXTRA);
-  if (extras_base64)
-    extras = g_base64_decode (extras_base64, &extras_len);
+  // If we already provided a valid hDevMode, don't initialize a new one; just lock the one we have
+  if (hDevMode)
+    {
+      devmode = GlobalLock (hDevMode);
+    }
+  else
+    {
+      extras = NULL;
+      extras_len = 0;
+      extras_base64 = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_WIN32_DRIVER_EXTRA);
+      if (extras_base64)
+        extras = g_base64_decode (extras_base64, &extras_len);
   
-  hDevMode = GlobalAlloc (GMEM_MOVEABLE, 
-                         sizeof (DEVMODEW) + extras_len);
+      hDevMode = GlobalAlloc (GMEM_MOVEABLE, 
+                             sizeof (DEVMODEW) + extras_len);
 
-  devmode = GlobalLock (hDevMode);
+      devmode = GlobalLock (hDevMode);
 
-  memset (devmode, 0, sizeof (DEVMODEW));
+      memset (devmode, 0, sizeof (DEVMODEW));
   
-  devmode->dmSpecVersion = DM_SPECVERSION;
-  devmode->dmSize = sizeof (DEVMODEW);
+      devmode->dmSpecVersion = DM_SPECVERSION;
+      devmode->dmSize = sizeof (DEVMODEW);
   
-  devmode->dmDriverExtra = 0;
-  if (extras && extras_len > 0)
-    {
-      devmode->dmDriverExtra = extras_len;
-      memcpy (((char *)devmode) + sizeof (DEVMODEW), extras, extras_len);
+      devmode->dmDriverExtra = 0;
+      if (extras && extras_len > 0)
+        {
+          devmode->dmDriverExtra = extras_len;
+          memcpy (((char *)devmode) + sizeof (DEVMODEW), extras, extras_len);
+        }
+      g_free (extras);
+  
+      if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_WIN32_DRIVER_VERSION))
+      {
+        devmode->dmDriverVersion = gtk_print_settings_get_int (settings, 
GTK_PRINT_SETTINGS_WIN32_DRIVER_VERSION);
+      }
     }
-  g_free (extras);
-  if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_WIN32_DRIVER_VERSION))
-    devmode->dmDriverVersion = gtk_print_settings_get_int (settings, 
GTK_PRINT_SETTINGS_WIN32_DRIVER_VERSION);
   
   if (page_setup ||
       gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
@@ -1240,12 +1252,25 @@ dialog_from_print_settings (GtkPrintOperation *op,
        }
     }
   
+  // If we have a printer saved, restore our settings
   printer = gtk_print_settings_get_printer (settings);
   if (printer)
+  {
     printdlgex->hDevNames = gtk_print_win32_devnames_to_win32_from_printer_name (printer);
   
-  printdlgex->hDevMode = devmode_from_settings (settings,
-                                               op->priv->default_page_setup);
+    printdlgex->hDevMode = devmode_from_settings (settings,
+                                                 op->priv->default_page_setup, NULL);
+  }
+  else
+  {
+    // Otherwise, use the default settings
+    DWORD FlagsCopy = printdlgex->Flags;
+    printdlgex->Flags |= PD_RETURNDEFAULT;
+    PrintDlgExW (printdlgex);
+    printdlgex->Flags = FlagsCopy;
+
+    devmode_from_settings (settings, op->priv->default_page_setup, printdlgex->hDevMode);
+  }
 }
 
 typedef struct {
@@ -1421,7 +1446,7 @@ pageDlgProc (HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
       if (message == WM_SIZE)
         {
           gtk_widget_queue_resize (op_win32->embed_widget);
-        }
+    }
 
       return FALSE;
     }
@@ -1577,7 +1602,7 @@ gtk_print_operation_run_without_dialog (GtkPrintOperation *op,
     }
 
   hDevNames = gtk_print_win32_devnames_to_win32_from_printer_name (printer);
-  hDevMode = devmode_from_settings (settings, op->priv->default_page_setup);
+  hDevMode = devmode_from_settings (settings, op->priv->default_page_setup, NULL);
 
   /* Create a printer DC for the print settings and page setup provided. */
   pdn = GlobalLock (hDevNames);
@@ -2075,7 +2100,7 @@ gtk_print_run_page_setup_dialog (GtkWindow        *parent,
     pagesetupdlg->hwndOwner = NULL;
 
   pagesetupdlg->Flags = PSD_DEFAULTMINMARGINS;
-  pagesetupdlg->hDevMode = devmode_from_settings (settings, page_setup);
+  pagesetupdlg->hDevMode = devmode_from_settings (settings, page_setup, NULL);
   pagesetupdlg->hDevNames = NULL;
   printer = gtk_print_settings_get_printer (settings);
   if (printer)


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