8016485: Windows native print dialog does not reflect default printer settings
authorprr
Thu, 13 Jun 2013 13:02:37 -0700
changeset 18116 f26d5963f5cb
parent 18115 dbd9791b69d2
child 18117 b85edc85d626
8016485: Windows native print dialog does not reflect default printer settings Reviewed-by: jgodinez, jchen
jdk/src/windows/classes/sun/awt/windows/WPrinterJob.java
jdk/src/windows/classes/sun/print/Win32PrintService.java
jdk/src/windows/native/sun/windows/WPrinterJob.cpp
jdk/src/windows/native/sun/windows/awt_PrintControl.cpp
--- a/jdk/src/windows/classes/sun/awt/windows/WPrinterJob.java	Fri Jun 07 10:26:29 2013 -0700
+++ b/jdk/src/windows/classes/sun/awt/windows/WPrinterJob.java	Thu Jun 13 13:02:37 2013 -0700
@@ -1269,11 +1269,13 @@
         mLastFontFamily = null;
     }
 
+    private boolean defaultCopies = true;
     /**
      * Set the number of copies to be printed.
      */
     public void setCopies(int copies) {
         super.setCopies(copies);
+        defaultCopies = false;
         mAttCopies = copies;
         setNativeCopies(copies);
     }
@@ -1529,8 +1531,9 @@
     }
 
     /* SheetCollate */
-    private final boolean getCollateAttrib() {
-        return (mAttCollate == 1);
+    private final int getCollateAttrib() {
+        // -1 means unset, 0 uncollated, 1 collated.
+        return mAttCollate;
     }
 
     private void setCollateAttrib(Attribute attr) {
@@ -1553,6 +1556,10 @@
         int orient = PageFormat.PORTRAIT;
         OrientationRequested orientReq = (attributes == null) ? null :
             (OrientationRequested)attributes.get(OrientationRequested.class);
+        if (orientReq == null) {
+            orientReq = (OrientationRequested)
+               myService.getDefaultAttributeValue(OrientationRequested.class);
+        }
         if (orientReq != null) {
             if (orientReq == OrientationRequested.REVERSE_LANDSCAPE) {
                 orient = PageFormat.REVERSE_LANDSCAPE;
@@ -1573,7 +1580,11 @@
 
     /* Copies and Page Range. */
     private final int getCopiesAttrib() {
-        return getCopiesInt();
+        if (defaultCopies) {
+            return 0;
+        } else {
+            return getCopiesInt();
+        }
      }
 
     private final void setRangeCopiesAttribute(int from, int to,
@@ -1584,6 +1595,7 @@
                 attributes.add(new PageRanges(from, to));
                 setPageRange(from, to);
             }
+            defaultCopies = false;
             attributes.add(new Copies(copies));
             /* Since this is called from native to tell Java to sync
              * up with native, we don't call this class's own setCopies()
--- a/jdk/src/windows/classes/sun/print/Win32PrintService.java	Fri Jun 07 10:26:29 2013 -0700
+++ b/jdk/src/windows/classes/sun/print/Win32PrintService.java	Thu Jun 13 13:02:37 2013 -0700
@@ -180,6 +180,9 @@
     private static final int DMDUP_VERTICAL = 2;
     private static final int DMDUP_HORIZONTAL = 3;
     private static final int DMCOLLATE_TRUE = 1;
+    private static final int DMCOLOR_MONOCHROME = 1;
+    private static final int DMCOLOR_COLOR = 2;
+
 
     // media sizes with indices above dmPaperToPrintService' length
     private static final int DMPAPER_A2 = 66;
@@ -1041,6 +1044,7 @@
         int defOrient = defaults[5];
         int defSides = defaults[6];
         int defCollate = defaults[7];
+        int defColor = defaults[8];
 
         if (category == Copies.class) {
             if (defCopies > 0) {
@@ -1049,11 +1053,10 @@
                 return new Copies(1);
             }
         } else if (category == Chromaticity.class) {
-            int caps = getPrinterCapabilities();
-            if ((caps & DEVCAP_COLOR) == 0) {
+            if (defColor == DMCOLOR_COLOR) {
+                return Chromaticity.COLOR;
+            } else {
                 return Chromaticity.MONOCHROME;
-            } else {
-                return Chromaticity.COLOR;
             }
         } else if (category == JobName.class) {
             return new JobName("Java Printing", null);
--- a/jdk/src/windows/native/sun/windows/WPrinterJob.cpp	Fri Jun 07 10:26:29 2013 -0700
+++ b/jdk/src/windows/native/sun/windows/WPrinterJob.cpp	Thu Jun 13 13:02:37 2013 -0700
@@ -750,7 +750,7 @@
 
 
 #define GETDEFAULT_ERROR        -50
-#define NDEFAULT 8
+#define NDEFAULT 9
 
 JNIEXPORT jintArray JNICALL
 Java_sun_print_Win32PrintService_getDefaultSettings(JNIEnv *env,
@@ -859,6 +859,11 @@
       defIndices[7] = pDevMode->dmCollate;
   }
 
+  if (pDevMode->dmFields & DM_COLOR) {
+      defIndices[8] = pDevMode->dmColor;
+  }
+
+
   GlobalFree(pDevMode);
   ::ClosePrinter(hPrinter);
 
--- a/jdk/src/windows/native/sun/windows/awt_PrintControl.cpp	Fri Jun 07 10:26:29 2013 -0700
+++ b/jdk/src/windows/native/sun/windows/awt_PrintControl.cpp	Thu Jun 13 13:02:37 2013 -0700
@@ -252,7 +252,7 @@
     AwtPrintControl::getCopiesID =
       env->GetMethodID(cls, "getCopiesAttrib", "()I");
     AwtPrintControl::getCollateID =
-      env->GetMethodID(cls, "getCollateAttrib","()Z");
+      env->GetMethodID(cls, "getCollateAttrib","()I");
     AwtPrintControl::getOrientID =
       env->GetMethodID(cls, "getOrientAttrib", "()I");
     AwtPrintControl::getFromPageID =
@@ -690,12 +690,6 @@
     pd.Flags = PD_ENABLEPRINTHOOK | PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE;
     pd.lpfnPrintHook = (LPPRINTHOOKPROC)PrintDlgHook;
 
-    if (env->CallBooleanMethod(printCtrl, AwtPrintControl::getCollateID)) {
-        pd.Flags |= PD_COLLATE;
-    }
-
-    pd.nCopies = (WORD)env->CallIntMethod(printCtrl,
-                                          AwtPrintControl::getCopiesID);
     pd.nFromPage = (WORD)env->CallIntMethod(printCtrl,
                                             AwtPrintControl::getFromPageID);
     pd.nToPage = (WORD)env->CallIntMethod(printCtrl,
@@ -729,37 +723,52 @@
       DEVMODE *devmode = (DEVMODE *)::GlobalLock(pd.hDevMode);
       DASSERT(!IsBadWritePtr(devmode, sizeof(DEVMODE)));
 
-      devmode->dmFields |= DM_COPIES | DM_COLLATE | DM_ORIENTATION |
-          DM_PAPERSIZE | DM_PRINTQUALITY | DM_COLOR | DM_DUPLEX;
-
-      devmode->dmCopies = pd.nCopies;
+      WORD copies = (WORD)env->CallIntMethod(printCtrl,
+                                             AwtPrintControl::getCopiesID);
+      if (copies > 0) {
+          devmode->dmFields |= DM_COPIES;
+          devmode->dmCopies = copies;
+      }
 
       jint orient = env->CallIntMethod(printCtrl,
                                        AwtPrintControl::getOrientID);
-      if (orient == 0) {
+      if (orient == 0) {  // PageFormat.LANDSCAPE == 0
+        devmode->dmFields |= DM_ORIENTATION;
         devmode->dmOrientation = DMORIENT_LANDSCAPE;
-      } else if (orient == 1) {
+      } else if (orient == 1) { // PageFormat.PORTRAIT == 1
+        devmode->dmFields |= DM_ORIENTATION;
         devmode->dmOrientation = DMORIENT_PORTRAIT;
       }
 
-      devmode->dmCollate = (pd.Flags & PD_COLLATE) ? DMCOLLATE_TRUE
-        : DMCOLLATE_FALSE;
+      // -1 means unset, so we'll accept the printer default.
+      int collate = env->CallIntMethod(printCtrl,
+                                       AwtPrintControl::getCollateID);
+      if (collate == 1) {
+        devmode->dmFields |= DM_COLLATE;
+        devmode->dmCollate = DMCOLLATE_TRUE;
+      } else if (collate == 0) {
+        devmode->dmFields |= DM_COLLATE;
+        devmode->dmCollate = DMCOLLATE_FALSE;
+      }
 
       int quality = env->CallIntMethod(printCtrl,
                                        AwtPrintControl::getQualityID);
       if (quality) {
+        devmode->dmFields |= DM_PRINTQUALITY;
         devmode->dmPrintQuality = quality;
       }
 
       int color = env->CallIntMethod(printCtrl,
                                      AwtPrintControl::getColorID);
       if (color) {
+        devmode->dmFields |= DM_COLOR;
         devmode->dmColor = color;
       }
 
       int sides = env->CallIntMethod(printCtrl,
                                      AwtPrintControl::getSidesID);
       if (sides) {
+        devmode->dmFields |= DM_DUPLEX;
         devmode->dmDuplex = (int)sides;
       }
 
@@ -771,6 +780,7 @@
 
       double newWid = 0.0, newHt = 0.0;
       if (wid_ht != NULL && wid_ht[0] != 0 && wid_ht[1] != 0) {
+        devmode->dmFields |= DM_PAPERSIZE;
         devmode->dmPaperSize = AwtPrintControl::getNearestMatchingPaper(
                                              printName,
                                              portName,