forked from openjdk/jdk
/
awt_PrintJob.cpp
4438 lines (3763 loc) · 146 KB
/
awt_PrintJob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "awt.h"
#include <math.h>
#include <windef.h>
#include <wtypes.h>
#include <winuser.h>
#include <commdlg.h>
#include <winspool.h>
#include "awt_Toolkit.h"
#include "awt_Component.h"
#include "awt_Dialog.h"
#include "awt_Font.h"
#include "awt_PrintDialog.h"
#include "awt_PrintControl.h"
#include "awt_Window.h"
#include "ComCtl32Util.h"
#include <sun_awt_windows_WPrinterJob.h>
#include <jlong_md.h>
#include <float.h>
#define DEBUG_PRINTING 0
/* Round 'num' to the nearest integer and return
* the result as a long.
*/
#define ROUND_TO_LONG(num) ((long) floor((num) + 0.5))
/* Round 'num' to the nearest integer and return
* the result as an int.
*/
#define ROUND_TO_INT(num) ((int) floor((num) + 0.5))
/************************************************************************
* WPrintJob native methods
*/
extern "C" {
/*** Private Constants ***/
static char *kJavaIntStr = "I";
static char *kJavaLongStr = "J";
/* 2D printing uses 3 byte BGR pixels in Raster printing */
static int J2DRasterBPP = 3;
/*
* Class Names
*/
static const char *PRINTEREXCEPTION_STR = "java/awt/print/PrinterException";
/*
* The following strings are the names of instance variables in WPrintJob2D.
*/
static const char *PRINTPAPERSIZE_STR = "mPrintPaperSize"; // The paper size
static const char *XRES_STR = "mPrintXRes"; // The x dpi.
static const char *YRES_STR = "mPrintYRes"; // The y dpi.
static const char *PHYSX_STR = "mPrintPhysX"; // pixel x of printable area
static const char *PHYSY_STR = "mPrintPhysY"; // pixel y of printable area
static const char *PHYSW_STR = "mPrintWidth"; // pixel wid of printable area
static const char *PHYSH_STR = "mPrintHeight"; // pixel hgt of printable area
static const char *PAGEW_STR = "mPageWidth"; // pixel wid of page
static const char *PAGEH_STR = "mPageHeight"; // pixel hgt of page
static const char *DRIVER_COPIES_STR = "driverDoesMultipleCopies";
static const char *DRIVER_COLLATE_STR = "driverDoesCollation";
static const char *USER_COLLATE_STR = "userRequestedCollation";
static const char *NO_DEFAULTPRINTER_STR = "noDefaultPrinter";
static const char *LANDSCAPE_270_STR = "landscapeRotates270";
// public int java.awt.print.PrinterJob.getCopies()
static const char *GETCOPIES_STR = "getCopies";
static const char *GETCOPIES_SIG = "()I";
/*
* Methods and fields in awt.print.PageFormat.
*/
// public Paper getPaper()
static const char *GETPAPER_STR = "getPaper";
static const char *GETPAPER_SIG = "()Ljava/awt/print/Paper;";
// public void setPaper(Paper paper)
static const char *SETPAPER_STR = "setPaper";
static const char *SETPAPER_SIG = "(Ljava/awt/print/Paper;)V";
// public int getOrientation()
static const char *GETORIENT_STR = "getOrientation";
static const char *GETORIENT_SIG = "()I";
// public void setOrientation(int orientation)
static const char *SETORIENT_STR = "setOrientation";
static const char *SETORIENT_SIG = "(I)V";
static const int PAGEFORMAT_LANDSCAPE = 0;
static const int PAGEFORMAT_PORTRAIT = 1;
//static const int PAGEFORMAT_REVERSELANDSCAPE = 2;
// instance variables for PrintRequestAttribute settings
static const char *ATTSIDES_STR = "mAttSides";
static const char *ATTCHROMATICITY_STR = "mAttChromaticity";
static const char *ATTXRES_STR = "mAttXRes";
static const char *ATTYRES_STR = "mAttYRes";
static const char *ATTQUALITY_STR = "mAttQuality";
static const char *ATTCOLLATE_STR = "mAttCollate";
static const char *ATTCOPIES_STR = "mAttCopies";
static const char *ATTMEDIASZNAME_STR = "mAttMediaSizeName";
static const char *ATTMEDIATRAY_STR = "mAttMediaTray";
/*
* Methods in awt.print.Paper.
*/
// public void setSize(double width, double height)
static const char *SETSIZE_STR = "setSize";
static const char *SETSIZE_SIG = "(DD)V";
// protected void setImageableArea(double x, double y, double width,
// double height)
static const char *SETIMAGEABLE_STR = "setImageableArea";
static const char *SETIMAGEABLE_SIG = "(DDDD)V";
// public double getWidth()
static const char *GETWIDTH_STR = "getWidth";
static const char *GETWIDTH_SIG = "()D";
// public double getHeight()
static const char *GETHEIGHT_STR = "getHeight";
static const char *GETHEIGHT_SIG = "()D";
// public double getImageableX()
static const char *GETIMG_X_STR = "getImageableX";
static const char *GETIMG_X_SIG = "()D";
// public double getImageableY()
static const char *GETIMG_Y_STR = "getImageableY";
static const char *GETIMG_Y_SIG = "()D";
// public double getImageableWidth()
static const char *GETIMG_W_STR = "getImageableWidth";
static const char *GETIMG_W_SIG = "()D";
// public double getImageableHeight()
static const char *GETIMG_H_STR = "getImageableHeight";
static const char *GETIMG_H_SIG = "()D";
/* Multiply a Window's MM_HIENGLISH value
* (1000th of an inch) by this number to
* get a value in 72nds of an inch.
*/
static const double HIENGLISH_TO_POINTS = (72.0 / 1000.0);
/* Multiply a Window's MM_HIMETRIC value
* (100ths of a millimeter) by this
* number to get a value in 72nds of an inch.
*/
static const double HIMETRIC_TO_POINTS = (72.0 / 2540.0);
/* Multiply a Window's MM_LOMETRIC value
* (10ths of a millimeter) by this
* number to get a value in 72nds of an inch.
*/
static const double LOMETRIC_TO_POINTS = (72.0 / 254.0);
/* Multiply a measurement in 1/72's of an inch by this
* value to convert it to Window's MM_HIENGLISH
* (1000th of an inch) units.
*/
static const double POINTS_TO_HIENGLISH = (1000.0 / 72.0);
/* Multiply a measurement in 1/72's of an inch by this
* value to convert it to Window's MM_HIMETRIC
* (100th of an millimeter) units.
*/
static const double POINTS_TO_HIMETRIC = (2540.0 / 72.0);
/* Multiply a measurement in 1/72's of an inch by this
* value to convert it to Window's MM_LOMETRIC
* (10th of an millimeter) units.
*/
static const double POINTS_TO_LOMETRIC = (254.0 / 72.0);
jfieldID AwtPrintDialog::pageID;
/*** Private Macros ***/
/* A Page Setup paint hook passes a word describing the
orientation and type of page being displayed in the
dialog. These macros break the word down into meaningful
values.
*/
#define PRINTER_TYPE_MASK (0x0003)
#define PORTRAIT_MASK (0x0004)
#define ENVELOPE_MASK (0x0008)
#define IS_ENVELOPE(param) (((param) & ENVELOPE_MASK) != 0)
#define IS_PORTRAIT(param) (((param) & PORTRAIT_MASK) != 0)
/* If the Pagable does not know the number of pages in the document,
then we limit the print dialog to this number of pages.
*/
#define MAX_UNKNOWN_PAGES 9999
/* When making a font that is already at least bold,
* bolder then we increase the LOGFONT lfWeight field
* by this amount.
*/
#define EMBOLDEN_WEIGHT (100)
/* The lfWeight field of a GDI LOGFONT structure should not
* exceed this value.
*/
#define MAX_FONT_WEIGHT (1000)
/*** Private Variable Types ***/
typedef struct {
jdouble x;
jdouble y;
jdouble width;
jdouble height;
} RectDouble;
/*** Private Prototypes ***/
static UINT CALLBACK pageDlgHook(HWND hDlg, UINT msg,
WPARAM wParam, LPARAM lParam);
static void initPrinter(JNIEnv *env, jobject self);
static HDC getDefaultPrinterDC(JNIEnv *env, jobject printerJob);
static void pageFormatToSetup(JNIEnv *env, jobject job, jobject page,
PAGESETUPDLG *setup, HDC hDC);
static WORD getOrientationFromDevMode2(HGLOBAL hDevMode);
static WORD getOrientationFromDevMode(JNIEnv *env, jobject self);
static void setOrientationInDevMode(HGLOBAL hDevMode, jboolean isPortrait);
static void doPrintBand(JNIEnv *env, jboolean browserPrinting,
HDC printDC, jbyteArray imageArray,
jint x, jint y, jint width, jint height);
static int bitsToDevice(HDC printDC, jbyte *image, long destX, long destY,
long width, long height);
static void retrievePaperInfo(const PAGESETUPDLG *setup, POINT *paperSize,
RECT *margins, jint *orientation,
HDC hdc);
static jint getCopies(JNIEnv *env, jobject printerJob);
static jobject getPaper(JNIEnv *env, jobject page);
static void setPaper(JNIEnv *env, jobject page, jobject paper);
static jint getPageFormatOrientation(JNIEnv *env, jobject page);
static void setPageFormatOrientation(JNIEnv *env, jobject page, jint orient);
static void getPaperValues(JNIEnv *env, jobject paper, RectDouble *paperSize,
RectDouble *margins, BOOL widthAsMargin=TRUE);
static void setPaperValues(JNIEnv *env, jobject paper, const POINT *paperSize,
const RECT *margins, int units);
static long convertFromPoints(double value, int units);
static double convertToPoints(long value, int units);
void setCapabilities(JNIEnv *env, jobject self, HDC printDC);
static inline WORD getPrintPaperSize(JNIEnv *env, jboolean* err, jobject self);
static inline jboolean setPrintPaperSize(JNIEnv *env, jobject self, WORD sz);
static jint getIntField(JNIEnv *env, jboolean* err, jobject self, const char *fieldName);
static jboolean setIntField(JNIEnv *env, jobject self,
const char *fieldName, jint value);
static jboolean getBooleanField(JNIEnv *env, jboolean* err, jobject self,
const char *fieldName);
static jboolean setBooleanField(JNIEnv *env, jobject self,
const char *fieldName, jboolean value);
static jbyte *findNonWhite(jbyte *image, long sy, long width, long height,
long scanLineStride, long *numLinesP);
static jbyte *findWhite(jbyte *image, long sy, long width, long height,
long scanLineStride, long *numLines);
static void dumpDevMode(HGLOBAL hDevMode);
static void dumpPrinterCaps(HANDLE hDevNames);
static void throwPrinterException(JNIEnv *env, DWORD err);
static void matchPaperSize(HDC printDC, HGLOBAL hDevMode, HGLOBAL hDevNames,
double origWid, double origHgt,
double* newHgt, double *newWid,
WORD* paperSize);
/***********************************************************************/
static jboolean jFontToWFontW(JNIEnv *env, HDC printDC, jstring fontName,
jfloat fontSize, jboolean isBold, jboolean isItalic,
jint rotation, jfloat awScale);
static jboolean jFontToWFontA(JNIEnv *env, HDC printDC, jstring fontName,
jfloat fontSize, jboolean isBold, jboolean isItalic,
jint rotation, jfloat awScale);
static int CALLBACK fontEnumProcW(ENUMLOGFONTEXW *lpelfe,
NEWTEXTMETRICEX *lpntme,
int FontType,
LPARAM lParam);
static int CALLBACK fontEnumProcA(ENUMLOGFONTEXA *logfont,
NEWTEXTMETRICEX *lpntme,
int FontType,
LPARAM lParam);
static int embolden(int currentWeight);
static BOOL getPrintableArea(HDC pdc, HANDLE hDevMode, RectDouble *margin);
/************************************************************************
* DocumentProperties native support
*/
/* Values must match those defined in WPrinterJob.java */
static const DWORD SET_COLOR = 0x00000200;
static const DWORD SET_ORIENTATION = 0x00004000;
static const DWORD SET_COLLATED = 0x00008000;
static const DWORD SET_DUP_VERTICAL = 0x00000010;
static const DWORD SET_DUP_HORIZONTAL = 0x00000020;
static const DWORD SET_RES_HIGH = 0x00000040;
static const DWORD SET_RES_LOW = 0x00000080;
/*
* Copy DEVMODE state back into JobAttributes.
*/
static void UpdateJobAttributes(JNIEnv *env,
jobject wJob,
jobject attrSet,
DEVMODE *devmode) {
DWORD dmValues = 0;
int xRes = 0, yRes = 0;
if (devmode->dmFields & DM_COLOR) {
if (devmode->dmColor == DMCOLOR_COLOR) {
dmValues |= SET_COLOR;
}
}
if (devmode->dmFields & DM_ORIENTATION) {
if (devmode->dmOrientation == DMORIENT_LANDSCAPE) {
dmValues |= SET_ORIENTATION;
}
}
if (devmode->dmFields & DM_COLLATE &&
devmode->dmCollate == DMCOLLATE_TRUE) {
dmValues |= SET_COLLATED;
}
if (devmode->dmFields & DM_PRINTQUALITY) {
/* value < 0 indicates quality setting.
* value > 0 indicates X resolution. In that case
* hopefully we will also find y-resolution specified.
* If its not, assume its the same as x-res.
* Maybe Java code should try to reconcile this against
* the printers claimed set of supported resolutions.
*/
if (devmode->dmPrintQuality < 0) {
if (devmode->dmPrintQuality == DMRES_HIGH) {
dmValues |= SET_RES_HIGH;
} else if ((devmode->dmPrintQuality == DMRES_LOW) ||
(devmode->dmPrintQuality == DMRES_DRAFT)) {
dmValues |= SET_RES_LOW;
}
/* else if (devmode->dmPrintQuality == DMRES_MEDIUM)
* will set to NORMAL.
*/
} else {
xRes = devmode->dmPrintQuality;
yRes = (devmode->dmFields & DM_YRESOLUTION) ?
devmode->dmYResolution : devmode->dmPrintQuality;
}
}
if (devmode->dmFields & DM_DUPLEX) {
if (devmode->dmDuplex == DMDUP_HORIZONTAL) {
dmValues |= SET_DUP_HORIZONTAL;
} else if (devmode->dmDuplex == DMDUP_VERTICAL) {
dmValues |= SET_DUP_VERTICAL;
}
}
env->CallVoidMethod(wJob, AwtPrintControl::setJobAttributesID, attrSet,
devmode->dmFields, dmValues, devmode->dmCopies,
devmode->dmPaperSize, devmode->dmPaperWidth,
devmode->dmPaperLength, devmode->dmDefaultSource,
xRes, yRes);
}
JNIEXPORT jboolean JNICALL
Java_sun_awt_windows_WPrinterJob_showDocProperties(JNIEnv *env,
jobject wJob,
jlong hWndParent,
jobject attrSet,
jint dmFields,
jshort copies,
jshort collate,
jshort color,
jshort duplex,
jshort orient,
jshort paper,
jshort bin,
jshort xres_quality,
jshort yres)
{
TRY;
HGLOBAL hDevMode = AwtPrintControl::getPrintHDMode(env, wJob);
HGLOBAL hDevNames = AwtPrintControl::getPrintHDName(env, wJob);
DEVMODE *devmode = NULL;
DEVNAMES *devnames = NULL;
LONG rval = IDCANCEL;
jboolean ret = JNI_FALSE;
if (hDevMode != NULL && hDevNames != NULL) {
devmode = (DEVMODE *)::GlobalLock(hDevMode);
devnames = (DEVNAMES *)::GlobalLock(hDevNames);
LPTSTR lpdevnames = (LPTSTR)devnames;
// No need to call _tcsdup as we won't unlock until we are done.
LPTSTR printerName = lpdevnames+devnames->wDeviceOffset;
LPTSTR portName = lpdevnames+devnames->wOutputOffset;
HANDLE hPrinter;
if (::OpenPrinter(printerName, &hPrinter, NULL) == TRUE) {
devmode->dmFields |= dmFields;
devmode->dmCopies = copies;
devmode->dmCollate = collate;
devmode->dmColor = color;
devmode->dmDuplex = duplex;
devmode->dmOrientation = orient;
devmode->dmPrintQuality = xres_quality;
devmode->dmYResolution = yres;
devmode->dmPaperSize = paper;
devmode->dmDefaultSource = bin;
rval = ::DocumentProperties((HWND)hWndParent,
hPrinter, printerName, devmode, devmode,
DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
if (rval == IDOK) {
UpdateJobAttributes(env, wJob, attrSet, devmode);
ret = JNI_TRUE;
}
VERIFY(::ClosePrinter(hPrinter));
}
::GlobalUnlock(hDevNames);
::GlobalUnlock(hDevMode);
}
return ret;
CATCH_BAD_ALLOC_RET(0);
}
/************************************************************************
* WPageDialog native methods
*/
JNIEXPORT void JNICALL
Java_sun_awt_windows_WPageDialog_initIDs(JNIEnv *env, jclass cls)
{
TRY;
AwtPrintDialog::pageID =
env->GetFieldID(cls, "page", "Ljava/awt/print/PageFormat;");
DASSERT(AwtPrintDialog::pageID != NULL);
CATCH_BAD_ALLOC;
}
/************************************************************************
* WPageDialogPeer native methods
*/
/*
* Class: sun_awt_windows_WPageDialogPeer
* Method: show
* Signature: ()Z
*
*/
JNIEXPORT jboolean JNICALL
Java_sun_awt_windows_WPageDialogPeer__1show(JNIEnv *env, jobject peer)
{
TRY;
// as peer object is used later on another thread, create global ref here
jobject peerGlobalRef = env->NewGlobalRef(peer);
DASSERT(peerGlobalRef != NULL);
jobject target = env->GetObjectField(peerGlobalRef, AwtObject::targetID);
jobject parent = env->GetObjectField(peerGlobalRef, AwtPrintDialog::parentID);
jobject page = env->GetObjectField(target, AwtPrintDialog::pageID);
DASSERT(page != NULL);
jobject self = env->GetObjectField(target, AwtPrintDialog::controlID);
DASSERT(self != NULL);
AwtComponent *awtParent = (parent != NULL) ? (AwtComponent *)JNI_GET_PDATA(parent) : NULL;
HWND hwndOwner = awtParent ? awtParent->GetHWnd() : NULL;
jboolean doIt = JNI_FALSE; // Assume the user will cancel the dialog.
PAGESETUPDLG setup;
memset(&setup, 0, sizeof(setup));
setup.lStructSize = sizeof(setup);
HWND parentID = AwtPrintControl::getParentID(env, self);
if (parentID != NULL && ::IsWindow(parentID)) {
// windows native modality is requested (used by JavaFX).
setup.hwndOwner = parentID;
}
/*
Fix for 6488834.
To disable Win32 native parent modality we have to set
hwndOwner field to either NULL or some hidden window. For
parentless dialogs we use NULL to show them in the taskbar,
and for all other dialogs AwtToolkit's HWND is used.
*/
else if (awtParent != NULL)
{
setup.hwndOwner = AwtToolkit::GetInstance().GetHWnd();
}
else
{
setup.hwndOwner = NULL;
}
setup.hDevMode = NULL;
setup.hDevNames = NULL;
setup.Flags = PSD_RETURNDEFAULT | PSD_DEFAULTMINMARGINS;
// setup.ptPaperSize =
// setup.rtMinMargin =
// setup.rtMargin =
setup.hInstance = NULL;
setup.lCustData = (LPARAM)peerGlobalRef;
setup.lpfnPageSetupHook = reinterpret_cast<LPPAGESETUPHOOK>(pageDlgHook);
setup.lpfnPagePaintHook = NULL;
setup.lpPageSetupTemplateName = NULL;
setup.hPageSetupTemplate = NULL;
/* Because the return default flag is set, this first call
* will not display the dialog but will return default values, inc
* including hDevMode, hDevName, ptPaperSize, and rtMargin values.
* We can use the devmode to set the orientation of the page
* and the size of the page.
* The units used by the user is also needed.
*/
if (AwtPrintControl::getPrintHDMode(env, self) == NULL ||
AwtPrintControl::getPrintHDName(env,self) == NULL) {
(void)::PageSetupDlg(&setup);
/* check if hDevMode and hDevNames are set.
* If both are null, then there is no default printer.
*/
if ((setup.hDevMode == NULL) && (setup.hDevNames == NULL)) {
doIt = JNI_FALSE;
goto done;
}
} else {
int measure = PSD_INTHOUSANDTHSOFINCHES;
int sz = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IMEASURE, NULL, 0);
if (sz > 0) {
LPTSTR str = (LPTSTR)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, sizeof(TCHAR), sz);
if (str != NULL) {
sz = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IMEASURE, str, sz);
if (sz > 0) {
if (_tcscmp(TEXT("0"), str) == 0) {
measure = PSD_INHUNDREDTHSOFMILLIMETERS;
}
}
free((LPTSTR)str);
}
}
setup.Flags |= measure;
setup.hDevMode = AwtPrintControl::getPrintHDMode(env, self);
setup.hDevNames = AwtPrintControl::getPrintHDName(env, self);
}
/* Move page size and orientation from the PageFormat object
* into the Windows setup structure so that the format can
* be displayed in the dialog.
*/
pageFormatToSetup(env, self, page, &setup, AwtPrintControl::getPrintDC(env, self));
if (env->ExceptionCheck()) {
doIt = JNI_FALSE;
goto done;
}
setup.lpfnPageSetupHook = reinterpret_cast<LPPAGESETUPHOOK>(pageDlgHook);
setup.Flags = PSD_ENABLEPAGESETUPHOOK | PSD_MARGINS;
AwtDialog::CheckInstallModalHook();
BOOL ret = ::PageSetupDlg(&setup);
if (ret) {
jobject paper = getPaper(env, page);
if (paper == NULL) {
doIt = JNI_FALSE;
goto done;
}
int units = setup.Flags & PSD_INTHOUSANDTHSOFINCHES ?
MM_HIENGLISH :
MM_HIMETRIC;
POINT paperSize;
RECT margins;
jint orientation;
/* The printer may have been changed, and we track that change,
* but then need to get a new DC for the current printer so that
* we validate the paper size correctly
*/
if (setup.hDevNames != NULL) {
DEVNAMES* names = (DEVNAMES*)::GlobalLock(setup.hDevNames);
if (names != NULL) {
LPTSTR printer = (LPTSTR)names+names->wDeviceOffset;
SAVE_CONTROLWORD
HDC newDC = ::CreateDC(TEXT("WINSPOOL"), printer, NULL, NULL);
RESTORE_CONTROLWORD
if (newDC != NULL) {
HDC oldDC = AwtPrintControl::getPrintDC(env, self);
if (oldDC != NULL) {
::DeleteDC(oldDC);
}
}
AwtPrintControl::setPrintDC(env, self, newDC);
}
::GlobalUnlock(setup.hDevNames);
}
/* Get the Windows paper and margins description.
*/
retrievePaperInfo(&setup, &paperSize, &margins, &orientation,
AwtPrintControl::getPrintDC(env, self));
/* Convert the Windows' paper and margins description
* and place them into a Paper instance.
*/
setPaperValues(env, paper, &paperSize, &margins, units);
if (env->ExceptionCheck()) {
doIt = JNI_FALSE;
goto done;
}
/*
* Put the updated Paper instance and the orientation into
* the PageFormat.
*/
setPaper(env, page, paper);
if (env->ExceptionCheck()) {
doIt = JNI_FALSE;
goto done;
}
setPageFormatOrientation(env, page, orientation);
if (env->ExceptionCheck()) {
doIt = JNI_FALSE;
goto done;
}
if (setup.hDevMode != NULL) {
DEVMODE *devmode = (DEVMODE *)::GlobalLock(setup.hDevMode);
if (devmode != NULL) {
if (devmode->dmFields & DM_PAPERSIZE) {
jboolean err = setPrintPaperSize(env, self, devmode->dmPaperSize);
if (err) {
doIt = JNI_FALSE;
goto done;
}
}
}
::GlobalUnlock(setup.hDevMode);
}
doIt = JNI_TRUE;
}
AwtDialog::CheckUninstallModalHook();
AwtDialog::ModalActivateNextWindow(NULL, target, peer);
HGLOBAL oldG = AwtPrintControl::getPrintHDMode(env, self);
if (setup.hDevMode != oldG) {
AwtPrintControl::setPrintHDMode(env, self, setup.hDevMode);
}
oldG = AwtPrintControl::getPrintHDName(env, self);
if (setup.hDevNames != oldG) {
AwtPrintControl::setPrintHDName(env, self, setup.hDevNames);
}
done:
env->DeleteGlobalRef(peerGlobalRef);
if (target != NULL) {
env->DeleteLocalRef(target);
}
if (parent != NULL) {
env->DeleteLocalRef(parent);
}
env->DeleteLocalRef(page);
env->DeleteLocalRef(self);
return doIt;
CATCH_BAD_ALLOC_RET(0);
}
/************************************************************************
* WPrinterJob native methods
*/
/*
* Class: sun_awt_windows_WPrinterJob
* Method: setCopies
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_sun_awt_windows_WPrinterJob_setNativeCopies(JNIEnv *env, jobject self,
jint copies) {
HGLOBAL hDevMode = AwtPrintControl::getPrintHDMode(env, self);
if (hDevMode != NULL) {
DEVMODE *devmode = (DEVMODE *)::GlobalLock(hDevMode);
if (devmode != NULL) {
short nCopies = (copies < (jint)SHRT_MAX)
? static_cast<short>(copies) : SHRT_MAX;
devmode->dmCopies = nCopies;
devmode->dmFields |= DM_COPIES;
}
::GlobalUnlock(hDevMode);
}
}
/*
* Class: sun_awt_windows_WPrinterJob
* Method: getDefaultPage
* Signature: (Ljava/awt/print/PageFormat;)V
*/
JNIEXPORT void JNICALL
Java_sun_awt_windows_WPrinterJob_getDefaultPage(JNIEnv *env, jobject self,
jobject page) {
TRY;
// devnames and dc are initialized at setting of Print Service,
// through print dialog or start of printing
// None of those may have happened yet, so call initPrinter()
initPrinter(env, self);
JNU_CHECK_EXCEPTION(env);
HANDLE hDevNames = AwtPrintControl::getPrintHDName(env, self);
HDC hdc = AwtPrintControl::getPrintDC(env, self);
if ((hDevNames == NULL) || (hdc == NULL)) {
return;
}
DEVNAMES *devnames = (DEVNAMES *)::GlobalLock(hDevNames);
if (devnames != NULL) {
LPTSTR lpdevnames = (LPTSTR)devnames;
LPTSTR printerName = _tcsdup(lpdevnames+devnames->wDeviceOffset);
HANDLE hPrinter = NULL;
LPDEVMODE pDevMode;
/* Start by opening the printer */
if (!::OpenPrinter(printerName, &hPrinter, NULL)) {
if (hPrinter != NULL) {
::ClosePrinter(hPrinter);
}
::GlobalUnlock(hDevNames);
free ((LPTSTR) printerName);
return;
}
if (!AwtPrintControl::getDevmode(hPrinter, printerName, &pDevMode)) {
/* if failure, cleanup and return failure */
if (pDevMode != NULL) {
::GlobalFree(pDevMode);
}
::ClosePrinter(hPrinter);
::GlobalUnlock(hDevNames);
free ((LPTSTR) printerName);
return ;
}
if ((pDevMode->dmFields & DM_PAPERSIZE) ||
(pDevMode->dmFields & DM_PAPERWIDTH) ||
(pDevMode->dmFields & DM_PAPERLENGTH)) {
POINT paperSize;
RECT margins;
jint orientation = PAGEFORMAT_PORTRAIT;
if (hdc != NULL) {
int units = MM_HIENGLISH;
int sz = GetLocaleInfo(LOCALE_USER_DEFAULT,
LOCALE_IMEASURE, NULL, 0);
if (sz > 0) {
LPTSTR str = (LPTSTR)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, sizeof(TCHAR), sz);
if (str != NULL) {
sz = GetLocaleInfo(LOCALE_USER_DEFAULT,
LOCALE_IMEASURE, str, sz);
if (sz > 0) {
if (_tcscmp(TEXT("0"), str) == 0) {
units = MM_HIMETRIC;
}
}
free((LPTSTR)str);
}
}
int width = ::GetDeviceCaps(hdc, PHYSICALWIDTH);
int height = ::GetDeviceCaps(hdc, PHYSICALHEIGHT);
int resx = ::GetDeviceCaps(hdc, LOGPIXELSX);
int resy = ::GetDeviceCaps(hdc, LOGPIXELSY);
double w = (double)width/resx;
double h = (double)height/resy;
paperSize.x = convertFromPoints(w*72, units);
paperSize.y = convertFromPoints(h*72, units);
// set margins to 1"
margins.left = convertFromPoints(72, units);
margins.top = convertFromPoints(72, units);;
margins.right = convertFromPoints(72, units);;
margins.bottom = convertFromPoints(72, units);;
jobject paper = getPaper(env, page);
if (paper == NULL) {
goto done;
}
setPaperValues(env, paper, &paperSize, &margins, units);
if (env->ExceptionCheck()) goto done;
setPaper(env, page, paper);
if (env->ExceptionCheck()) goto done;
if ((pDevMode->dmFields & DM_ORIENTATION) &&
(pDevMode->dmOrientation == DMORIENT_LANDSCAPE)) {
orientation = PAGEFORMAT_LANDSCAPE;
}
setPageFormatOrientation(env, page, orientation);
}
} else {
setBooleanField(env, self, NO_DEFAULTPRINTER_STR, (jint)JNI_TRUE);
}
done:
::GlobalFree(pDevMode);
free ((LPTSTR) printerName);
::ClosePrinter(hPrinter);
}
::GlobalUnlock(hDevNames);
CATCH_BAD_ALLOC;
}
/*
* Class: sun_awt_windows_WPrinterJob
* Method: validatePaper
* Signature: (Ljava/awt/print/Paper;Ljava/awt/print/Paper;)V
*
* Query the current or default printer to find all paper sizes it
* supports and find the closest matching to the origPaper.
* For the matching size, validate the margins and printable area
* against the printer's capabilities.
*/
JNIEXPORT void JNICALL
Java_sun_awt_windows_WPrinterJob_validatePaper(JNIEnv *env, jobject self,
jobject origPaper, jobject newPaper) {
TRY;
/* If the print dialog has been displayed or a DC has otherwise
* been created, use that. Else get a DC for the default printer
* which we discard before returning.
*/
HDC printDC = AwtPrintControl::getPrintDC(env, self);
HGLOBAL hDevMode = AwtPrintControl::getPrintHDMode(env, self);
HGLOBAL hDevNames = AwtPrintControl::getPrintHDName(env, self);
BOOL privateDC = FALSE;
if (printDC == NULL) {
PRINTDLG pd;
memset(&pd, 0, sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
if (::PrintDlg(&pd)) {
printDC = pd.hDC;
hDevMode = pd.hDevMode;
hDevNames = pd.hDevNames;
privateDC = TRUE;
}
}
JNI_CHECK_NULL_GOTO(printDC, "Invalid printDC", done);
/* We try to mitigate the effects of floating point rounding errors
* by only setting a value if it would differ from the value in the
* target by at least 0.10 points = 1/720 inches.
* eg if the values present in the target are close to the calculated
* values then we accept the target.
*/
const double epsilon = 0.10;
jdouble paperWidth, paperHeight;
jboolean err;
WORD dmPaperSize = getPrintPaperSize(env, &err, self);
if (err) goto done;
double ix, iy, iw, ih, pw, ph;
DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
jmethodID getID;
jclass paperClass = env->GetObjectClass(origPaper);
JNI_CHECK_NULL_GOTO(paperClass, "paper class not found", done);
getID = env->GetMethodID(paperClass, GETWIDTH_STR, GETWIDTH_SIG);
JNI_CHECK_NULL_GOTO(getID, "no getWidth method", done);
pw = env->CallDoubleMethod(origPaper, getID);
getID = env->GetMethodID(paperClass, GETHEIGHT_STR, GETHEIGHT_SIG);
JNI_CHECK_NULL_GOTO(getID, "no getHeight method", done);
ph = env->CallDoubleMethod(origPaper, getID);
getID = env->GetMethodID(paperClass, GETIMG_X_STR, GETIMG_X_SIG);
JNI_CHECK_NULL_GOTO(getID, "no getX method", done);
ix = env->CallDoubleMethod(origPaper, getID);
getID = env->GetMethodID(paperClass, GETIMG_Y_STR, GETIMG_Y_SIG);
JNI_CHECK_NULL_GOTO(getID, "no getY method", done);
iy = env->CallDoubleMethod(origPaper, getID);
getID = env->GetMethodID(paperClass, GETIMG_W_STR, GETIMG_W_SIG);
JNI_CHECK_NULL_GOTO(getID, "no getW method", done);
iw = env->CallDoubleMethod(origPaper, getID);
getID = env->GetMethodID(paperClass, GETIMG_H_STR, GETIMG_H_SIG);
JNI_CHECK_NULL_GOTO(getID, "no getH method", done);
ih = env->CallDoubleMethod(origPaper, getID);
matchPaperSize(printDC, hDevMode, hDevNames, pw, ph,
&paperWidth, &paperHeight, &dmPaperSize);
/* Validate margins and imageable area */
// pixels per inch in x and y direction
jint xPixelRes = GetDeviceCaps(printDC, LOGPIXELSX);
jint yPixelRes = GetDeviceCaps(printDC, LOGPIXELSY);
// x & y coord of printable area in pixels
jint xPixelOrg = GetDeviceCaps(printDC, PHYSICALOFFSETX);
jint yPixelOrg = GetDeviceCaps(printDC, PHYSICALOFFSETY);
// width & height of printable area in pixels
jint imgPixelWid = GetDeviceCaps(printDC, HORZRES);
jint imgPixelHgt = GetDeviceCaps(printDC, VERTRES);
// The DC may be obtained when we first selected the printer as a
// result of a call to setNativePrintService.
// If the Devmode was obtained later on from the DocumentProperties dialog
// the DC won't have been updated and its settings may be for PORTRAIT.
// This may happen in other cases too, but was observed for the above.
// To get a DC compatible with this devmode we should really call
// CreateDC() again to get a DC for the devmode we are using.
// The changes for that are a lot more risk, so to minimize that
// risk, assume its not LANDSCAPE unless width > height, even if the
// devmode says its LANDSCAPE.
// if the values were obtained from a rotated device, swap.
if ((getOrientationFromDevMode2(hDevMode) == DMORIENT_LANDSCAPE) &&
(imgPixelWid > imgPixelHgt)) {
jint tmp;
tmp = xPixelRes;
xPixelRes = yPixelRes;
yPixelRes = tmp;
tmp = xPixelOrg;
xPixelOrg = yPixelOrg;
yPixelOrg = tmp;