jdk/src/windows/native/sun/windows/awt_PrintJob.cpp
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6825 795e9fe949d3
child 8744 5f8a7e06e9a7
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6825
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2456
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2456
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2456
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2456
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2456
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
    26
#include "awt.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <math.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <windef.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <wtypes.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <winuser.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <commdlg.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <winspool.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "awt_Toolkit.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "awt_Component.h"
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
    36
#include "awt_Dialog.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "awt_Font.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "awt_PrintDialog.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "awt_PrintControl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "awt_Window.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "ComCtl32Util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include <sun_awt_windows_WPrinterJob.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <jlong_md.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <float.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#define DEBUG_PRINTING  0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/* Round 'num' to the nearest integer and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * the result as a long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define ROUND_TO_LONG(num)    ((long) floor((num) + 0.5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/* Round 'num' to the nearest integer and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the result as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#define ROUND_TO_INT(num)     ((int) floor((num) + 0.5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * WPrintJob native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
extern "C" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/*** Private Constants ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
static char *kJavaIntStr = "I";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
static char *kJavaLongStr = "J";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/* 2D printing uses 3 byte BGR pixels in Raster printing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
static int J2DRasterBPP = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Class Names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static const char *PRINTEREXCEPTION_STR = "java/awt/print/PrinterException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * The following strings are the names of instance variables in WPrintJob2D.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
static const char *PRINTPAPERSIZE_STR = "mPrintPaperSize"; // The paper size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
static const char *XRES_STR = "mPrintXRes";     // The x dpi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
static const char *YRES_STR = "mPrintYRes";     // The y dpi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
static const char *PHYSX_STR = "mPrintPhysX";   // pixel x of printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
static const char *PHYSY_STR = "mPrintPhysY";   // pixel y of printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
static const char *PHYSW_STR = "mPrintWidth";   // pixel wid of printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
static const char *PHYSH_STR = "mPrintHeight";  // pixel hgt of printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
static const char *PAGEW_STR = "mPageWidth";    // pixel wid of page
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
static const char *PAGEH_STR = "mPageHeight";   // pixel hgt of page
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
static const char *DRIVER_COPIES_STR = "driverDoesMultipleCopies";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
static const char *DRIVER_COLLATE_STR = "driverDoesCollation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
static const char *USER_COLLATE_STR = "userRequestedCollation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
static const char *NO_DEFAULTPRINTER_STR = "noDefaultPrinter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
static const char *LANDSCAPE_270_STR = "landscapeRotates270";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
// public int java.awt.print.PrinterJob.getCopies()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
static const char *GETCOPIES_STR = "getCopies";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
static const char *GETCOPIES_SIG = "()I";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Methods and fields in awt.print.PageFormat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
// public Paper getPaper()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
static const char *GETPAPER_STR = "getPaper";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
static const char *GETPAPER_SIG = "()Ljava/awt/print/Paper;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
// public void setPaper(Paper paper)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
static const char *SETPAPER_STR = "setPaper";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
static const char *SETPAPER_SIG = "(Ljava/awt/print/Paper;)V";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
// public int getOrientation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
static const char *GETORIENT_STR = "getOrientation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
static const char *GETORIENT_SIG = "()I";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
// public void setOrientation(int orientation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
static const char *SETORIENT_STR = "setOrientation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
static const char *SETORIENT_SIG = "(I)V";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
static const int PAGEFORMAT_LANDSCAPE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
static const int PAGEFORMAT_PORTRAIT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
//static const int PAGEFORMAT_REVERSELANDSCAPE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
// instance variables for PrintRequestAttribute settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
static const char *ATTSIDES_STR = "mAttSides";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
static const char *ATTCHROMATICITY_STR = "mAttChromaticity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
static const char *ATTXRES_STR = "mAttXRes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
static const char *ATTYRES_STR = "mAttYRes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
static const char *ATTQUALITY_STR = "mAttQuality";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
static const char *ATTCOLLATE_STR = "mAttCollate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
static const char *ATTCOPIES_STR = "mAttCopies";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
static const char *ATTMEDIASZNAME_STR = "mAttMediaSizeName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
static const char *ATTMEDIATRAY_STR = "mAttMediaTray";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * Methods in awt.print.Paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
// public void setSize(double width, double height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
static const char *SETSIZE_STR = "setSize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
static const char *SETSIZE_SIG = "(DD)V";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
// protected void setImageableArea(double x, double y, double width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
//                                                  double height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
static const char *SETIMAGEABLE_STR = "setImageableArea";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
static const char *SETIMAGEABLE_SIG = "(DDDD)V";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
// public double getWidth()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
static const char *GETWIDTH_STR = "getWidth";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
static const char *GETWIDTH_SIG = "()D";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
// public double getHeight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
static const char *GETHEIGHT_STR = "getHeight";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
static const char *GETHEIGHT_SIG = "()D";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
// public double getImageableX()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
static const char *GETIMG_X_STR = "getImageableX";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
static const char *GETIMG_X_SIG = "()D";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
// public double getImageableY()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
static const char *GETIMG_Y_STR = "getImageableY";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
static const char *GETIMG_Y_SIG = "()D";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
// public double getImageableWidth()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
static const char *GETIMG_W_STR = "getImageableWidth";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
static const char *GETIMG_W_SIG = "()D";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
// public double getImageableHeight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
static const char *GETIMG_H_STR = "getImageableHeight";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
static const char *GETIMG_H_SIG = "()D";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
/* Multiply a Window's MM_HIENGLISH value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * (1000th of an inch) by this number to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * get a value in 72nds of an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
static const double HIENGLISH_TO_POINTS = (72.0 / 1000.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
/* Multiply a Window's MM_HIMETRIC value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * (100ths of a millimeter) by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * number to get a value in 72nds of an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
static const double HIMETRIC_TO_POINTS = (72.0 / 2540.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
/* Multiply a Window's MM_LOMETRIC value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * (10ths of a millimeter) by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * number to get a value in 72nds of an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
static const double LOMETRIC_TO_POINTS = (72.0 / 254.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
/* Multiply a measurement in 1/72's of an inch by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * value to convert it to Window's MM_HIENGLISH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * (1000th of an inch) units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
static const double POINTS_TO_HIENGLISH = (1000.0 / 72.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
/* Multiply a measurement in 1/72's of an inch by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * value to convert it to Window's MM_HIMETRIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * (100th of an millimeter) units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
static const double POINTS_TO_HIMETRIC = (2540.0 / 72.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
/* Multiply a measurement in 1/72's of an inch by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * value to convert it to Window's MM_LOMETRIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * (10th of an millimeter) units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
static const double POINTS_TO_LOMETRIC = (254.0 / 72.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
jfieldID AwtPrintDialog::pageID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
/*** Private Macros ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
/* A Page Setup paint hook passes a word describing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
   orientation and type of page being displayed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
   dialog. These macros break the word down into meaningful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
   values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
#define PRINTER_TYPE_MASK   (0x0003)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
#define PORTRAIT_MASK       (0x0004)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
#define ENVELOPE_MASK       (0x0008)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
#define IS_ENVELOPE(param)  (((param) & ENVELOPE_MASK) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
#define IS_PORTRAIT(param)  (((param) & PORTRAIT_MASK) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
/*      If the Pagable does not know the number of pages in the document,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        then we limit the print dialog to this number of pages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
#define MAX_UNKNOWN_PAGES 9999
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
/* When making a font that is already at least bold,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * bolder then we increase the LOGFONT lfWeight field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * by this amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
#define EMBOLDEN_WEIGHT   (100)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
/* The lfWeight field of a GDI LOGFONT structure should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 * exceed this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
#define MAX_FONT_WEIGHT   (1000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
/*** Private Variable Types ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    jdouble x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    jdouble y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    jdouble width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    jdouble height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
} RectDouble;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
/*** Private Prototypes ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
static UINT CALLBACK pageDlgHook(HWND hDlg, UINT msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                 WPARAM wParam, LPARAM lParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
static void initPrinter(JNIEnv *env, jobject self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
static HDC getDefaultPrinterDC(JNIEnv *env, jobject printerJob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
static void pageFormatToSetup(JNIEnv *env, jobject job, jobject page,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                              PAGESETUPDLG *setup, HDC hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
static WORD getOrientationFromDevMode2(HGLOBAL hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
static WORD getOrientationFromDevMode(JNIEnv *env, jobject self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
static void setOrientationInDevMode(HGLOBAL hDevMode, jboolean isPortrait);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
static void doPrintBand(JNIEnv *env, jboolean browserPrinting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        HDC printDC, jbyteArray imageArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        jint x, jint y, jint width, jint height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
static int bitsToDevice(HDC printDC, jbyte *image, long destX, long destY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        long width, long height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
static void retrievePaperInfo(const PAGESETUPDLG *setup, POINT *paperSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                              RECT *margins, jint *orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                              HDC hdc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
static jint getCopies(JNIEnv *env, jobject printerJob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
static jobject getPaper(JNIEnv *env, jobject page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
static void setPaper(JNIEnv *env, jobject page, jobject paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
static jint getPageFormatOrientation(JNIEnv *env, jobject page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
static void setPageFormatOrientation(JNIEnv *env, jobject page, jint orient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
static void getPaperValues(JNIEnv *env, jobject paper, RectDouble *paperSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                          RectDouble *margins, BOOL widthAsMargin=TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
static void setPaperValues(JNIEnv *env, jobject paper, const POINT *paperSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                            const RECT *margins, int units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
static long convertFromPoints(double value, int units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
static double convertToPoints(long value, int units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
void setCapabilities(JNIEnv *env, jobject self, HDC printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
static inline WORD getPrintPaperSize(JNIEnv *env, jobject self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
static inline void setPrintPaperSize(JNIEnv *env, jobject self, WORD sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
static jint getIntField(JNIEnv *env, jobject self, const char *fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
static jlong getLongField(JNIEnv *env, jobject self, const char *fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
static void setIntField(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                            const char *fieldName, jint value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
static void setLongField(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            const char *fieldName, jlong value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
static jfieldID getIdOfIntField(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            const char *fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
static jfieldID getIdOfLongField(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            const char *fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
static void setBooleanField(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            const char *fieldName, jboolean value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
static jbyte *findNonWhite(jbyte *image, long sy, long width, long height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                           long scanLineStride, long *numLinesP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
static jbyte *findWhite(jbyte *image, long sy, long width, long height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                           long scanLineStride, long *numLines);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
static void dumpDevMode(HGLOBAL hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
static void dumpPrinterCaps(HANDLE hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
static void throwPrinterException(JNIEnv *env, DWORD err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
static void matchPaperSize(HDC printDC, HGLOBAL hDevMode, HGLOBAL hDevNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                           double origWid, double origHgt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                           double* newHgt, double *newWid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                           WORD* paperSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
/***********************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
static jboolean jFontToWFontW(JNIEnv *env, HDC printDC, jstring fontName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        jfloat fontSize, jboolean isBold, jboolean isItalic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        jint rotation, jfloat awScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
static jboolean jFontToWFontA(JNIEnv *env, HDC printDC, jstring fontName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        jfloat fontSize, jboolean isBold, jboolean isItalic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        jint rotation, jfloat awScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
static int CALLBACK fontEnumProcW(ENUMLOGFONTEXW  *lpelfe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                 NEWTEXTMETRICEX *lpntme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                 int FontType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                 LPARAM lParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
static int CALLBACK fontEnumProcA(ENUMLOGFONTEXA  *logfont,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                  NEWTEXTMETRICEX  *lpntme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                  int FontType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                  LPARAM lParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
static int embolden(int currentWeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
static BOOL getPrintableArea(HDC pdc, HANDLE hDevMode, RectDouble *margin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 * WPageDialog native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
Java_sun_awt_windows_WPageDialog_initIDs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    AwtPrintDialog::pageID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        env->GetFieldID(cls, "page", "Ljava/awt/print/PageFormat;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    DASSERT(AwtPrintDialog::pageID != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 * WPageDialogPeer native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 * Class:     sun_awt_windows_WPageDialogPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
 * Method:    show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 * Signature: ()Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
Java_sun_awt_windows_WPageDialogPeer__1show(JNIEnv *env, jobject peer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    // as peer object is used later on another thread, create global ref here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    jobject peerGlobalRef = env->NewGlobalRef(peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    DASSERT(peerGlobalRef != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    jobject target = env->GetObjectField(peerGlobalRef, AwtObject::targetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    jobject parent = env->GetObjectField(peerGlobalRef, AwtPrintDialog::parentID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    jobject page = env->GetObjectField(target, AwtPrintDialog::pageID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    DASSERT(page != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    jobject self = env->GetObjectField(target, AwtPrintDialog::controlID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    DASSERT(self != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    AwtComponent *awtParent = (parent != NULL) ? (AwtComponent *)JNI_GET_PDATA(parent) : NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    HWND hwndOwner = awtParent ? awtParent->GetHWnd() : NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    jboolean doIt = JNI_FALSE; // Assume the user will cancel the dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    PAGESETUPDLG setup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    memset(&setup, 0, sizeof(setup));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    setup.lStructSize = sizeof(setup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
      Fix for 6488834.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
      To disable Win32 native parent modality we have to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
      hwndOwner field to either NULL or some hidden window. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
      parentless dialogs we use NULL to show them in the taskbar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
      and for all other dialogs AwtToolkit's HWND is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    if (awtParent != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        setup.hwndOwner = AwtToolkit::GetInstance().GetHWnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        setup.hwndOwner = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    setup.hDevMode = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    setup.hDevNames = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    setup.Flags = PSD_RETURNDEFAULT | PSD_DEFAULTMINMARGINS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    // setup.ptPaperSize =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    // setup.rtMinMargin =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    // setup.rtMargin =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    setup.hInstance = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    setup.lCustData = (LPARAM)peerGlobalRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    setup.lpfnPageSetupHook = reinterpret_cast<LPPAGESETUPHOOK>(pageDlgHook);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    setup.lpfnPagePaintHook = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    setup.lpPageSetupTemplateName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    setup.hPageSetupTemplate = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /* Because the return default flag is set, this first call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * will not display the dialog but will return default values, inc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * including hDevMode, hDevName, ptPaperSize, and rtMargin values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * We can use the devmode to set the orientation of the page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * and the size of the page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * The units used by the user is also needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    if (AwtPrintControl::getPrintHDMode(env, self) == NULL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        AwtPrintControl::getPrintHDName(env,self) == NULL) {
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
   425
        (void)::PageSetupDlg(&setup);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        /* check if hDevMode and hDevNames are set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * If both are null, then there is no default printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if ((setup.hDevMode == NULL) && (setup.hDevNames == NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int measure = PSD_INTHOUSANDTHSOFINCHES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        int sz = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IMEASURE, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (sz > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
          LPTSTR str = (LPTSTR)safe_Malloc(sizeof(TCHAR) * sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
          if (str != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            sz = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IMEASURE, str, sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (sz > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
              if (_tcscmp(TEXT("0"), str) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                measure = PSD_INHUNDREDTHSOFMILLIMETERS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            free((LPTSTR)str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        setup.Flags |= measure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        setup.hDevMode = AwtPrintControl::getPrintHDMode(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        setup.hDevNames = AwtPrintControl::getPrintHDName(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /* Move page size and orientation from the PageFormat object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * into the Windows setup structure so that the format can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * be displayed in the dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    pageFormatToSetup(env, self, page, &setup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                      AwtPrintControl::getPrintDC(env, self));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    setup.lpfnPageSetupHook = reinterpret_cast<LPPAGESETUPHOOK>(pageDlgHook);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    setup.Flags = PSD_ENABLEPAGESETUPHOOK | PSD_MARGINS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    AwtDialog::CheckInstallModalHook();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
   463
    BOOL ret = ::PageSetupDlg(&setup);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    if (ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        jobject paper = getPaper(env, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        int units = setup.Flags & PSD_INTHOUSANDTHSOFINCHES ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                                                MM_HIENGLISH :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                                                MM_HIMETRIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        POINT paperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        RECT margins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        jint orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        /* The printer may have been changed, and we track that change,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         * but then need to get a new DC for the current printer so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * we validate the paper size correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (setup.hDevNames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            DEVNAMES* names = (DEVNAMES*)::GlobalLock(setup.hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (names != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                LPTSTR printer = (LPTSTR)names+names->wDeviceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                HDC newDC = ::CreateDC(TEXT("WINSPOOL"), printer, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                if (newDC != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    HDC oldDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    if (oldDC != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                         ::DeleteDC(oldDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                AwtPrintControl::setPrintDC(env, self, newDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            ::GlobalUnlock(setup.hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        /* Get the Windows paper and margins description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        retrievePaperInfo(&setup, &paperSize, &margins, &orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                          AwtPrintControl::getPrintDC(env, self));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        /* Convert the Windows' paper and margins description
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * and place them into a Paper instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        setPaperValues(env, paper, &paperSize, &margins, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        /* Put the updated Paper instance and the orientation into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * the PageFormat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        setPaper(env, page, paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        setPageFormatOrientation(env, page, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (setup.hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            DEVMODE *devmode = (DEVMODE *)::GlobalLock(setup.hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (devmode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                if (devmode->dmFields & DM_PAPERSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    setPrintPaperSize(env, self, devmode->dmPaperSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            ::GlobalUnlock(setup.hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        doIt = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    DASSERT(env->GetLongField(peer, AwtComponent::hwndID) == 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    AwtDialog::CheckUninstallModalHook();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    AwtDialog::ModalActivateNextWindow(NULL, target, peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    HGLOBAL oldG = AwtPrintControl::getPrintHDMode(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    if (setup.hDevMode != oldG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        AwtPrintControl::setPrintHDMode(env, self, setup.hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    oldG = AwtPrintControl::getPrintHDName(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    if (setup.hDevNames != oldG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        AwtPrintControl::setPrintHDName(env, self, setup.hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    env->DeleteGlobalRef(peerGlobalRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    if (target != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        env->DeleteLocalRef(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    if (parent != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        env->DeleteLocalRef(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    env->DeleteLocalRef(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    env->DeleteLocalRef(self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    return doIt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    CATCH_BAD_ALLOC_RET(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
 * WPrinterJob native methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
 * Class:   sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
 * Method:  setCopies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
Java_sun_awt_windows_WPrinterJob_setNativeCopies(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                                           jint copies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    HGLOBAL hDevMode = AwtPrintControl::getPrintHDMode(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    if (hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
      DEVMODE *devmode = (DEVMODE *)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
      if (devmode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        short nCopies = (copies < (jint)SHRT_MAX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
          ? static_cast<short>(copies) : SHRT_MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        devmode->dmCopies = nCopies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        devmode->dmFields |= DM_COPIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
      ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
 * Method:    getDefaultPage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
 * Signature: (Ljava/awt/print/PageFormat;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
Java_sun_awt_windows_WPrinterJob_getDefaultPage(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                                                jobject page) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
  TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
  // devnames and dc are initialized at setting of Print Service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
  // through print dialog or start of printing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
  // None of those may have happened yet, so call initPrinter()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
  initPrinter(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
  HANDLE hDevNames = AwtPrintControl::getPrintHDName(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
  HDC hdc = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
  if ((hDevNames == NULL) || (hdc == NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
  DEVNAMES *devnames = (DEVNAMES *)::GlobalLock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
  if (devnames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    LPTSTR lpdevnames = (LPTSTR)devnames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    LPTSTR printerName = _tcsdup(lpdevnames+devnames->wDeviceOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    HANDLE      hPrinter = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    LPDEVMODE   pDevMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /* Start by opening the printer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    if (!::OpenPrinter(printerName, &hPrinter, NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
      if (hPrinter != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
      ::GlobalUnlock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
      free ((LPTSTR) printerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    if (!AwtPrintControl::getDevmode(hPrinter, printerName, &pDevMode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        /* if failure, cleanup and return failure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        if (pDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            ::GlobalFree(pDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        ::GlobalUnlock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        free ((LPTSTR) printerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
885
cdf589db9213 6708509: print dialog is not displayed when default paper is custom
jgodinez
parents: 2
diff changeset
   635
    if ((pDevMode->dmFields & DM_PAPERSIZE) ||
cdf589db9213 6708509: print dialog is not displayed when default paper is custom
jgodinez
parents: 2
diff changeset
   636
          (pDevMode->dmFields & DM_PAPERWIDTH) ||
cdf589db9213 6708509: print dialog is not displayed when default paper is custom
jgodinez
parents: 2
diff changeset
   637
          (pDevMode->dmFields & DM_PAPERLENGTH)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        POINT paperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        RECT margins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        jint orientation = PAGEFORMAT_PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (hdc != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
          int units = MM_HIENGLISH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
          int sz = GetLocaleInfo(LOCALE_USER_DEFAULT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                 LOCALE_IMEASURE, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
          if (sz > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            LPTSTR str = (LPTSTR)safe_Malloc(sizeof(TCHAR) * sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (str != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
              sz = GetLocaleInfo(LOCALE_USER_DEFAULT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                 LOCALE_IMEASURE, str, sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
              if (sz > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                if (_tcscmp(TEXT("0"), str) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                  units = MM_HIMETRIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
              free((LPTSTR)str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
          int width = ::GetDeviceCaps(hdc, PHYSICALWIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
          int height = ::GetDeviceCaps(hdc, PHYSICALHEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
          int resx = ::GetDeviceCaps(hdc, LOGPIXELSX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
          int resy = ::GetDeviceCaps(hdc, LOGPIXELSY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
          double w = (double)width/resx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
          double h = (double)height/resy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
          paperSize.x = convertFromPoints(w*72, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
          paperSize.y = convertFromPoints(h*72, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
          // set margins to 1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
          margins.left = convertFromPoints(72, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
          margins.top = convertFromPoints(72, units);;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
          margins.right = convertFromPoints(72, units);;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
          margins.bottom = convertFromPoints(72, units);;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
          jobject paper = getPaper(env, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
          setPaperValues(env, paper, &paperSize, &margins, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
          setPaper(env, page, paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
          if ((pDevMode->dmFields & DM_ORIENTATION) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
              (pDevMode->dmOrientation == DMORIENT_LANDSCAPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
              orientation = PAGEFORMAT_LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
          setPageFormatOrientation(env, page, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        setBooleanField(env, self, NO_DEFAULTPRINTER_STR, (jint)JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    ::GlobalFree(pDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    free ((LPTSTR) printerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
  ::GlobalUnlock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
  CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
 * Method:    validatePaper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
 * Signature: (Ljava/awt/print/Paper;Ljava/awt/print/Paper;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
 * Query the current or default printer to find all paper sizes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
 * supports and find the closest matching to the origPaper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
 * For the matching size, validate the margins and printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
 * against the printer's capabilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
Java_sun_awt_windows_WPrinterJob_validatePaper(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                                         jobject origPaper, jobject newPaper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /* If the print dialog has been displayed or a DC has otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * been created, use that. Else get a DC for the default printer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * which we discard before returning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    HDC printDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    HGLOBAL hDevMode = AwtPrintControl::getPrintHDMode(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    HGLOBAL hDevNames = AwtPrintControl::getPrintHDName(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    BOOL privateDC = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    if (printDC == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        PRINTDLG pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        memset(&pd, 0, sizeof(PRINTDLG));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        pd.lStructSize = sizeof(PRINTDLG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
   736
        if (::PrintDlg(&pd)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            printDC = pd.hDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            hDevMode = pd.hDevMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            hDevNames = pd.hDevNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            privateDC = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    if (printDC == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
       return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /* We try to mitigate the effects of floating point rounding errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * by only setting a value if it would differ from the value in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * target by at least 0.10 points = 1/720 inches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * eg if the values present in the target are close to the calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * values then we accept the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    const double epsilon = 0.10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    jdouble paperWidth, paperHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    WORD dmPaperSize = getPrintPaperSize(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    double ix, iy, iw, ih, pw, ph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    jmethodID getID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    jclass paperClass = env->GetObjectClass(origPaper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    getID = env->GetMethodID(paperClass, GETWIDTH_STR, GETWIDTH_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    pw = env->CallDoubleMethod(origPaper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    getID = env->GetMethodID(paperClass, GETHEIGHT_STR, GETHEIGHT_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    ph = env->CallDoubleMethod(origPaper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    getID = env->GetMethodID(paperClass, GETIMG_X_STR, GETIMG_X_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    ix = env->CallDoubleMethod(origPaper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    getID = env->GetMethodID(paperClass, GETIMG_Y_STR, GETIMG_Y_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    iy = env->CallDoubleMethod(origPaper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    getID = env->GetMethodID(paperClass, GETIMG_W_STR, GETIMG_W_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    iw = env->CallDoubleMethod(origPaper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    getID = env->GetMethodID(paperClass, GETIMG_H_STR, GETIMG_H_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    ih = env->CallDoubleMethod(origPaper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    matchPaperSize(printDC, hDevMode, hDevNames, pw, ph,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                   &paperWidth, &paperHeight, &dmPaperSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /* Validate margins and imageable area */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    // pixels per inch in x and y direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    jint xPixelRes = GetDeviceCaps(printDC, LOGPIXELSX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    jint yPixelRes = GetDeviceCaps(printDC, LOGPIXELSY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    // x & y coord of printable area in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    jint xPixelOrg = GetDeviceCaps(printDC, PHYSICALOFFSETX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    jint yPixelOrg = GetDeviceCaps(printDC, PHYSICALOFFSETY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    // width & height of printable area in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    jint imgPixelWid = GetDeviceCaps(printDC, HORZRES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    jint imgPixelHgt = GetDeviceCaps(printDC, VERTRES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    // if the values were obtained from a rotated device, swap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    if (getOrientationFromDevMode2(hDevMode) == DMORIENT_LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
      jint tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
      tmp = xPixelRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
      xPixelRes = yPixelRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
      yPixelRes = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
      tmp = xPixelOrg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
      xPixelOrg = yPixelOrg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
      yPixelOrg = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
      tmp = imgPixelWid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
      imgPixelWid = imgPixelHgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
      imgPixelHgt = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    // page imageable area in 1/72"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    jdouble imgX = (jdouble)((xPixelOrg * 72)/(jdouble)xPixelRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    jdouble imgY = (jdouble)((yPixelOrg * 72)/(jdouble)yPixelRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    jdouble imgWid = (jdouble)((imgPixelWid * 72)/(jdouble)xPixelRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    jdouble imgHgt = (jdouble)((imgPixelHgt * 72)/(jdouble)yPixelRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /* Check each of the individual values is within range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Then make sure imageable area is placed within imageable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Allow for a small floating point error in the comparisons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    if (ix < 0.0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        ix = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    if (iy < 0.0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        iy = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    if (iw < 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        iw = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    if (ih < 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        ih = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    if ((ix + epsilon) < imgX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
         ix = imgX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    if ((iy + epsilon) < imgY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         iy = imgY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    if (iw + epsilon > imgWid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        iw = imgWid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    if (ih + epsilon > imgHgt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        ih = imgHgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    if ((ix + iw + epsilon) > (imgX+imgWid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        ix = (imgX+imgWid) - iw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    if ((iy + ih + epsilon) > (imgY+imgHgt)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        iy = (imgY+imgHgt) - ih;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    jmethodID setSizeID = env->GetMethodID(paperClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                        SETSIZE_STR, SETSIZE_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    jmethodID setImageableID = env->GetMethodID(paperClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                                        SETIMAGEABLE_STR, SETIMAGEABLE_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    env->CallVoidMethod(newPaper, setSizeID, paperWidth, paperHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    env->CallVoidMethod(newPaper, setImageableID, ix, iy, iw, ih);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /* Free any resources allocated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    if (privateDC == TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if (printDC != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            /* In this case we know that this DC has no GDI objects to free */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
             ::DeleteDC(printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        if (hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            ::GlobalFree(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (hDevNames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            ::GlobalFree(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
static void initPrinter(JNIEnv *env, jobject self) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    HDC printDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * The print device context will be NULL if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * user never okayed a print dialog. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * will happen most often when the java application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * decides not to present a print dialog to the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * We create a device context for the default printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    if (printDC == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        printDC = getDefaultPrinterDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if (printDC){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            AwtPrintControl::setPrintDC(env, self, printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            setCapabilities(env, self, printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
 * Method:    initPrinter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
Java_sun_awt_windows_WPrinterJob_initPrinter(JNIEnv *env, jobject self) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    initPrinter(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    // check for collation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    HGLOBAL hDevNames = AwtPrintControl::getPrintHDName(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    if (hDevNames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        DWORD dmFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        DEVNAMES *devnames = (DEVNAMES *)::GlobalLock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        if (devnames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            LPTSTR lpdevnames = (LPTSTR)devnames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            LPTSTR printername = lpdevnames+devnames->wDeviceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            LPTSTR port = lpdevnames+devnames->wOutputOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            dmFields = ::DeviceCapabilities(printername, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                                            DC_FIELDS, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            int devLandRotation = (int)DeviceCapabilities(printername, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                        DC_ORIENTATION, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            ::GlobalUnlock(devnames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            if (devLandRotation == 270) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
              setBooleanField(env, self, LANDSCAPE_270_STR, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
              setBooleanField(env, self, LANDSCAPE_270_STR, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        if (dmFields & DM_COLLATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            setBooleanField(env, self, DRIVER_COLLATE_STR, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            setBooleanField(env, self, DRIVER_COLLATE_STR, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
static bool setPrintReqAttribute(JNIEnv *env, jobject self, DEVMODE* devmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    /* The xRes/yRes fields are only initialised if there is a resolution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * attribute. Otherwise they both will be zero, in which case default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * resolution should be fine. Consider calling getXRes()/getResY()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * rather than accessing the fields directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    int xRes=getIntField(env, self, ATTXRES_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    int yRes=getIntField(env, self, ATTYRES_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    int quality=getIntField(env, self, ATTQUALITY_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    int printColor = getIntField(env, self, ATTCHROMATICITY_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    int sides = getIntField(env, self, ATTSIDES_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    int collate = getIntField(env, self, ATTCOLLATE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    int copies = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    jclass myClass = env->GetObjectClass(self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    // There may be cases when driver reports it cannot handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    // multiple copies although it actually can .  So this modification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    // handles that, to make sure that we report copies = 1 because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    // we already emulated multiple copies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    jfieldID fieldId = env->GetFieldID(myClass, DRIVER_COPIES_STR, "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    if (env->GetBooleanField(self, fieldId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
      copies = getIntField(env, self, ATTCOPIES_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    } // else "driverDoesMultipleCopies" is false, copies should be 1 (default)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    int mediatray = getIntField(env, self, ATTMEDIATRAY_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    int mediaszname = getIntField(env, self, ATTMEDIASZNAME_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    bool ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    if (quality && quality < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        if (quality != devmode->dmPrintQuality) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            devmode->dmPrintQuality = quality;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            devmode->dmFields |= DM_PRINTQUALITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            // ret of "false" means that setCapabilities needs to be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        /* If we didn't set quality, maybe we have resolution settings. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        if (xRes && (xRes != devmode->dmPrintQuality)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            devmode->dmPrintQuality = xRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            devmode->dmFields |= DM_PRINTQUALITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (yRes && (yRes != devmode->dmYResolution)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            devmode->dmYResolution = yRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            devmode->dmFields |= DM_YRESOLUTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    if (printColor && (printColor != devmode->dmColor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        devmode->dmColor = printColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        devmode->dmFields |= DM_COLOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    if (sides && (sides != devmode->dmDuplex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        devmode->dmDuplex = sides;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        devmode->dmFields |= DM_DUPLEX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    if ((collate != -1) && (collate != devmode->dmCollate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        devmode->dmCollate = collate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        devmode->dmFields |= DM_COLLATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    if (copies && (copies != devmode->dmCopies)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        devmode->dmCopies = copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        devmode->dmFields |= DM_COPIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    if (mediatray && (mediatray != devmode->dmDefaultSource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        devmode->dmDefaultSource = mediatray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        devmode->dmFields |= DM_DEFAULTSOURCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    if (mediaszname && (mediaszname != devmode->dmPaperSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        devmode->dmPaperSize = mediaszname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        devmode->dmFields |= DM_PAPERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
static LPTSTR GetPrinterPort(JNIEnv *env, LPTSTR printer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
  HANDLE hPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
  if (::OpenPrinter(printer, &hPrinter, NULL) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
  DWORD bytesReturned, bytesNeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
  ::GetPrinter(hPrinter, 2, NULL, 0, &bytesNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
  PRINTER_INFO_2* info2 = (PRINTER_INFO_2*)::GlobalAlloc(GPTR, bytesNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
  if (info2 == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
      ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
  int ret = ::GetPrinter(hPrinter, 2, (LPBYTE)info2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                         bytesNeeded, &bytesReturned);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
  ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
  if (!ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    ::GlobalFree(info2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
6825
795e9fe949d3 6989721: awt native code compiler warnings
art
parents: 5506
diff changeset
  1053
  LPTSTR port = _wcsdup(info2->pPortName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
  ::GlobalFree(info2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
  return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
static jboolean isFilePort(LPTSTR port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    return wcscmp(port, TEXT("FILE:")) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
 * This is called when printing is about to start and we have not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
 * a file destination - which is in fact the 99.99% case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
 * We can discover from the DEVNAMES if the DC is actually associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
 * with "FILE:", which is going to occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
 * 1) if the native print dialog was used and print to file was selected, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
 * 2) the printer driver is configured to print to file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
 * In that former case we have a conflict since if the destination is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
 * file, JDK will normally supply that destination to StartDoc, so what
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
 * must have happened is the app de-associated the job from the file, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
 * the printer DC etc is still hooked up to the file. If we find
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
 * the DEVNAMES specified is set to "FILE:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
 * First find out if the DC was associated with a FILE. If it is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
 * then unless that is its normal configuration, we'll get a new DC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
 * If the default destination ends with ":", this is sufficient clue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
 * to windows it must be a device. Otherwise we need to create a new DC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
LPTSTR VerifyDestination(JNIEnv *env, jobject wPrinterJob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    LPTSTR dest = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    HDC printDC = AwtPrintControl::getPrintDC(env, wPrinterJob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    HGLOBAL hDevNames = AwtPrintControl::getPrintHDName(env, wPrinterJob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    if (hDevNames == NULL || printDC == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    DEVNAMES *devnames = (DEVNAMES *)::GlobalLock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    if (devnames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        LPTSTR lpdevnames = (LPTSTR)devnames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        LPTSTR printer = lpdevnames+devnames->wDeviceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        LPTSTR port = lpdevnames+devnames->wOutputOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (port != NULL && isFilePort(port)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            LPTSTR defPort = GetPrinterPort(env, printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            if (!isFilePort(defPort)) { // not a FILE: port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                int len = wcslen(defPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                if (len > 0 && port[len-1] == L':') { // is a device port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    dest = defPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    /* We need to create a new DC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    HDC newDC = ::CreateDC(TEXT("WINSPOOL"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                                           printer, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    AwtPrintControl::setPrintDC(env, wPrinterJob, newDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    DeleteDC(printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            if (dest != defPort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                free(defPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        ::GlobalUnlock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
 * Method:    startDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
Java_sun_awt_windows_WPrinterJob__1startDoc(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                                            jstring dest, jstring jobname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    int err = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    LPTSTR destination = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    if (dest != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        destination = (LPTSTR)JNU_GetStringPlatformChars(env, dest, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        destination = VerifyDestination(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    LPTSTR docname = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    if (jobname != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        LPTSTR tmp = (LPTSTR)JNU_GetStringPlatformChars(env, jobname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        docname = _tcsdup(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        JNU_ReleaseStringPlatformChars(env, jobname, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        docname = TEXT("Java Printing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    initPrinter(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    HDC printDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    /* We do our own rotation, so device must be in portrait mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * This should be in effect only whilst we are printing, so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * if the app displays the native dialog again for the same printerjob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * instance, it shows the setting the user expects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * So in EndDoc, and AbortDoc or if we fail out of this function,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * we need to restore this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    HGLOBAL hDevMode = AwtPrintControl::getPrintHDMode(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    if (printDC != NULL && hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        DEVMODE *devmode = (DEVMODE *)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        if (devmode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                devmode->dmFields |= DM_ORIENTATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                devmode->dmOrientation = DMORIENT_PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                /* set attribute values into devmode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                bool ret = setPrintReqAttribute(env, self, devmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                ::ResetDC(printDC, devmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                if (!ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                      Need to read in updated device capabilities because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                      print quality has been changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    setCapabilities(env, self, printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    if (printDC){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        DOCINFO docInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        memset(&docInfo, 0, sizeof(DOCINFO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        docInfo.cbSize = sizeof (DOCINFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        docInfo.lpszDocName = docname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        TCHAR fullPath[_MAX_PATH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        if (destination != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            _tfullpath(fullPath, destination, _MAX_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            docInfo.lpszOutput = fullPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        docInfo.fwType = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        err = ::StartDoc(printDC, &docInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        free((void*)docInfo.lpszDocName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        if (err <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            err = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            err = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        if (dest != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            JNU_ReleaseStringPlatformChars(env, dest, destination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        jclass printerException = env->FindClass(PRINTEREXCEPTION_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        env->ThrowNew(printerException, "No printer found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    if (err && err != ERROR_CANCELLED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        throwPrinterException(env, err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    if (err == ERROR_CANCELLED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    CATCH_BAD_ALLOC_RET(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
 * Method:    endDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
Java_sun_awt_windows_WPrinterJob_endDoc(JNIEnv *env, jobject self) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    HDC printDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    if (printDC != NULL){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        ::EndDoc(printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
 * Method:    abortDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
Java_sun_awt_windows_WPrinterJob_abortDoc(JNIEnv *env, jobject self) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    HDC printDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    if (printDC != NULL){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
         ::AbortDoc(printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
static void DeletePrintDC(HDC printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    if (printDC==NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    /* Free any GDI objects we may have selected into the DC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * It is not harmful to call DeleteObject if the retrieved objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * happen to be stock objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    HBRUSH hbrush = (HBRUSH)::SelectObject(printDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                                           ::GetStockObject(BLACK_BRUSH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    if (hbrush != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        ::DeleteObject(hbrush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    HPEN hpen = (HPEN)::SelectObject(printDC, ::GetStockObject(BLACK_PEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    if (hpen != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        ::DeleteObject(hpen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    HFONT hfont = (HFONT)::SelectObject(printDC,::GetStockObject(SYSTEM_FONT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    if (hfont != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        ::DeleteObject(hfont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    ::DeleteDC(printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
 * Method:    deleteDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
 * Called after WPrinterJob has been GCed, not before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
Java_sun_awt_windows_WPrinterJob_deleteDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
(JNIEnv *env, jclass wpjClass, jlong dc, jlong devmode, jlong devnames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    TRY_NO_VERIFY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    DeletePrintDC((HDC)dc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    if ((HGLOBAL)devmode != NULL){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
         ::GlobalFree((HGLOBAL)devmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    if ((HGLOBAL)devnames != NULL){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         ::GlobalFree((HGLOBAL)devnames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
 * Method:    deviceStartPage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
 * Signature: (Ljava/awt/print/PageFormat;Ljava/awt/print/Printable;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_deviceStartPage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
(JNIEnv *env, jobject self, jobject format, jobject painter, jint pageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
 jboolean pageChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    HDC printDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    if (printDC != NULL){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        LONG retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        HGLOBAL hDevMode = AwtPrintControl::getPrintHDMode(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        HGLOBAL hDevNames = AwtPrintControl::getPrintHDName(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        WORD dmPaperSize = getPrintPaperSize(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
          // Unless the PageFormat has been changed, do not set the paper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
          // size for a new page. Doing so is unnecessary, perhaps expensive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
          // and can lead some printers to emit the paper prematurely in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
          // duplex mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        if (hDevMode != NULL && hDevNames != NULL && pageChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            RectDouble paperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            RectDouble margins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            jobject paper = getPaper(env, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            getPaperValues(env, paper, &paperSize, &margins);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            double paperWidth, paperHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            matchPaperSize(printDC, hDevMode, hDevNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                           paperSize.width,  paperSize.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                           &paperWidth, &paperHeight, &dmPaperSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            DEVMODE *devmode = (DEVMODE *)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            if (devmode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                if (dmPaperSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                  devmode->dmFields |= DM_PAPERLENGTH | DM_PAPERWIDTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                    | DM_PAPERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                  devmode->dmPaperSize = DMPAPER_USER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                  devmode->dmPaperWidth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    (short)(convertFromPoints(paperSize.width, MM_LOMETRIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                  devmode->dmPaperLength =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                    (short)(convertFromPoints(paperSize.height, MM_LOMETRIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                  // sync with public devmode settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    DEVNAMES *devnames = (DEVNAMES *)::GlobalLock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                    if (devnames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                      LPTSTR lpdevnames = (LPTSTR)devnames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                      LPTSTR printerName = _tcsdup(lpdevnames+devnames->wDeviceOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                      HANDLE hPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                      if (::OpenPrinter(printerName, &hPrinter, NULL)== TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                        // Need to call DocumentProperties to update change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                        // in paper setting because some drivers do not update
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                        // it with a simple call to ResetDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        retval = ::DocumentProperties(NULL, hPrinter,printerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                                             devmode, devmode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                                             DM_IN_BUFFER|DM_OUT_BUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                        RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                        ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                        free ((char*)printerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                    ::GlobalUnlock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                  } // sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                  HDC res = ::ResetDC(printDC, devmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                  RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                }  // if (dmPaperSize == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                // if DocumentProperties() fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
               if (retval < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                  ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        ::StartPage(printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        /* The origin for a glyph will be along the left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
         * edge of its bnounding box at the base line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
         * The coincides with the Java text glyph origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        ::SetTextAlign(printDC, TA_LEFT | TA_BASELINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        /* The background mode is used when GDI draws text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
         * hatched brushes and poen that are not solid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
         * We set the mode to transparentso that when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
         * drawing text only the glyphs themselves are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
         * drawn. The boundingbox of the string is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
         * erased to the background color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        ::SetBkMode(printDC, TRANSPARENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
 * Method:    deviceEndPage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
 * Signature: (Ljava/awt/print/PageFormat;Ljava/awt/print/Printable;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_deviceEndPage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
(JNIEnv *env, jobject self, jobject format, jobject painter, jint pageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    HDC printDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    if (printDC != NULL){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        ::EndPage(printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
 * Class:     sun_awt_windows_WEmbeddedFrame
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
 * Method:    isPrinterDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
 * Signature: (J)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
JNIEXPORT jboolean JNICALL Java_sun_awt_windows_WEmbeddedFrame_isPrinterDC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    (JNIEnv *env, jobject self, jlong hdc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    HDC realHDC = (HDC)hdc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    if (realHDC == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    int technology = GetDeviceCaps(realHDC, TECHNOLOGY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
#if DEBUG_PRINTING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     FILE *file = fopen("c:\\plog.txt", "a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     fprintf(file,"tech is %d\n", technology);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     fclose(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
#endif //DEBUG_PRINTING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    switch (GetDeviceCaps(realHDC, TECHNOLOGY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    case DT_RASPRINTER :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    case DT_RASDISPLAY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    case DT_METAFILE   :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        if (GetObjectType(realHDC) == OBJ_ENHMETADC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    default : return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
 * Class:     sun_awt_windows_WEmbeddedFrame
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
 * Method:    printBand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
 * Signature: (J[BIIIIIIIII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
JNIEXPORT void JNICALL Java_sun_awt_windows_WEmbeddedFrame_printBand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
  (JNIEnv *env, jobject self, jlong theHDC, jbyteArray imageArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
   jint offset, jint srcX,  jint srcY,  jint srcWidth,  jint srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
   jint destX, jint destY, jint destWidth, jint destHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    if (theHDC == NULL || imageArray == NULL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        srcWidth <= 0 || srcHeight == 0 || destWidth == 0 || destHeight <=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    HDC hDC = (HDC)theHDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    /* The code below is commented out until its proven necessary. In its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * original form of PatBlit(hDC, destX,destY,destWidth, destHeight ..)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * it resulted in the PS driver showing a white fringe, perhaps because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * the PS driver enclosed the specified area rather than filling its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * interior. The code is believed to have been there to prevent such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * artefacts rather than cause them. This may have been related to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * the earlier implementation using findNonWhite(..) and breaking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * image blit up into multiple blit calls. This currently looks as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * its unnecessary as the driver performs adequate compression where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * such all white spans exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
//     HGDIOBJ oldBrush =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
//      ::SelectObject(hDC, AwtBrush::Get(RGB(0xff, 0xff, 0xff))->GetHandle());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
//     ::PatBlt(hDC, destX+1, destY+1, destWidth-2, destHeight-2, PATCOPY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
//     ::SelectObject(hDC, oldBrush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    jbyte *image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        image = (jbyte *)env->GetPrimitiveArrayCritical(imageArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            BITMAPINFOHEADER bmiHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            DWORD*                 bmiColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        } bitMapHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        memset(&bitMapHeader,0,sizeof(bitMapHeader));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        bitMapHeader.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        bitMapHeader.bmiHeader.biWidth = srcWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        bitMapHeader.bmiHeader.biHeight = srcHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        bitMapHeader.bmiHeader.biPlanes = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        bitMapHeader.bmiHeader.biBitCount = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        bitMapHeader.bmiHeader.biCompression = BI_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        int result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            ::StretchDIBits(hDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                            destX,         // left of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                            destY,         // top of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                            destWidth,     // width of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                            destHeight,    // height of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                            srcX,          // left of source rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                            srcY,          // top of source rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                            srcWidth,      // number of 1st source scan line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                            srcHeight,     // number of source scan lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                            image+offset,  // points to the DIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                            (BITMAPINFO *)&bitMapHeader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                            DIB_RGB_COLORS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                            SRCCOPY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
#if DEBUG_PRINTING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     FILE *file = fopen("c:\\plog.txt", "a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     fprintf(file,"sh=%d dh=%d sy=%d dy=%d result=%d\n", srcHeight, destHeight, srcY, destY, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     fclose(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
#endif //DEBUG_PRINTING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    } catch (...) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        if (image != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            env->ReleasePrimitiveArrayCritical(imageArray, image, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        throw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    env->ReleasePrimitiveArrayCritical(imageArray, image, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
 * Method:    printBand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
 * Signature: ([BIIII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_printBand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
  (JNIEnv *env, jobject self, jbyteArray imageArray, jint x, jint y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
   jint width, jint height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    HDC printDC = AwtPrintControl::getPrintDC(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    doPrintBand(env, JNI_FALSE, printDC, imageArray, x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
 * Method:    beginPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_beginPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
(JNIEnv *env , jobject self, jlong printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    (void) ::BeginPath((HDC)printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
 * Method:    endPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_endPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
(JNIEnv *env, jobject self, jlong printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    (void) ::EndPath((HDC)printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
 * Method:    fillPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_fillPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
(JNIEnv *env, jobject self, jlong printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    (void) ::FillPath((HDC)printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
 * Method:    closeFigure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_closeFigure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
(JNIEnv *env, jobject self, jlong printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    (void) ::CloseFigure((HDC)printDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
 * Method:    lineTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
 * Signature: (JFF)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_lineTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
(JNIEnv *env, jobject self, jlong printDC, jfloat x, jfloat y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    (void) ::LineTo((HDC)printDC, ROUND_TO_LONG(x), ROUND_TO_LONG(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
 * Method:    moveTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
 * Signature: (JFF)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_moveTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
(JNIEnv *env, jobject self, jlong printDC, jfloat x, jfloat y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    (void) ::MoveToEx((HDC)printDC, ROUND_TO_LONG(x), ROUND_TO_LONG(y), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
 * Method:    polyBezierTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
 * Signature: (JFFFFFF)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_polyBezierTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
(JNIEnv *env, jobject self, jlong printDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
 jfloat control1x, jfloat control1y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
 jfloat control2x, jfloat control2y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
 jfloat endX, jfloat endY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    POINT points[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    points[0].x = ROUND_TO_LONG(control1x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    points[0].y = ROUND_TO_LONG(control1y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    points[1].x = ROUND_TO_LONG(control2x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    points[1].y = ROUND_TO_LONG(control2y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    points[2].x = ROUND_TO_LONG(endX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    points[2].y = ROUND_TO_LONG(endY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    (void) ::PolyBezierTo((HDC)printDC, points, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
 * Method:    setPolyFillMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
 * Signature: (JI)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_setPolyFillMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
(JNIEnv *env, jobject self, jlong printDC, jint fillRule) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    (void) ::SetPolyFillMode((HDC)printDC, fillRule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
 * Method:    selectSolidBrush
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
 * Signature: (JIII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_selectSolidBrush
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
(JNIEnv *env, jobject self, jlong printDC, jint red, jint green, jint blue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
    HBRUSH colorBrush = ::CreateSolidBrush(RGB(red, green, blue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    HBRUSH oldBrush = (HBRUSH)::SelectObject((HDC)printDC, colorBrush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    DeleteObject(oldBrush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
 * Method:    getPenX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
 * Signature: (J)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
JNIEXPORT jint JNICALL Java_sun_awt_windows_WPrinterJob_getPenX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
(JNIEnv *env, jobject self, jlong printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    POINT where;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    ::GetCurrentPositionEx((HDC)printDC, &where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    return (jint) where.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    CATCH_BAD_ALLOC_RET(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
 * Method:    getPenY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
 * Signature: (J)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
JNIEXPORT jint JNICALL Java_sun_awt_windows_WPrinterJob_getPenY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
(JNIEnv *env, jobject self, jlong printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    POINT where;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    ::GetCurrentPositionEx((HDC)printDC, &where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    return (jint) where.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    CATCH_BAD_ALLOC_RET(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
 * Method:    selectClipPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_selectClipPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
(JNIEnv *env, jobject self, jlong printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    ::SelectClipPath((HDC)printDC, RGN_COPY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
 * Method:    frameRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
 * Signature: (JFFFF)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_frameRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
(JNIEnv *env, jobject self, jlong printDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
 jfloat x, jfloat y, jfloat width, jfloat height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
  TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
  POINT points[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
  points[0].x = ROUND_TO_LONG(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
  points[0].y = ROUND_TO_LONG(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
  points[1].x = ROUND_TO_LONG(x+width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
  points[1].y = ROUND_TO_LONG(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
  points[2].x = ROUND_TO_LONG(x+width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
  points[2].y = ROUND_TO_LONG(y+height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
  points[3].x = ROUND_TO_LONG(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
  points[3].y = ROUND_TO_LONG(y+height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
  points[4].x = ROUND_TO_LONG(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
  points[4].y = ROUND_TO_LONG(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
  ::Polyline((HDC)printDC, points, sizeof(points)/sizeof(points[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
  CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
 * Method:    fillRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
 * Signature: (JFFFFIII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_fillRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
(JNIEnv *env, jobject self, jlong printDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
 jfloat x, jfloat y, jfloat width, jfloat height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
 jint red, jint green, jint blue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
  TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
  RECT rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
  rect.left = ROUND_TO_LONG(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
  rect.top = ROUND_TO_LONG(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
  rect.right = ROUND_TO_LONG(x+width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
  rect.bottom = ROUND_TO_LONG(y+height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
  HBRUSH brush = ::CreateSolidBrush(RGB(red, green, blue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
  if (brush != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    ::FillRect((HDC)printDC, (LPRECT) &rect, brush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    DeleteObject(brush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
  CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
 * Method:    selectPen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
 * Signature: (JFIII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_selectPen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
(JNIEnv *env, jobject self, jlong printDC, jfloat width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
 jint red, jint green, jint blue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
  TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
  HPEN hpen =  ::CreatePen(PS_SOLID, ROUND_TO_LONG(width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                           RGB(red, green, blue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
  if (hpen != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    HPEN oldpen = (HPEN) ::SelectObject((HDC)printDC, hpen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    if (oldpen != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
      DeleteObject(oldpen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
  CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
 * Method:    selectStylePen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
 * Signature: (JJJFIII)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
JNIEXPORT jboolean JNICALL Java_sun_awt_windows_WPrinterJob_selectStylePen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
(JNIEnv *env, jobject self, jlong printDC, jlong cap, jlong join, jfloat width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
 jint red, jint green, jint blue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
  TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
  LOGBRUSH logBrush;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
  logBrush.lbStyle = PS_SOLID ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
  logBrush.lbColor = RGB(red, green, blue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
  logBrush.lbHatch = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
  HPEN hpen =  ::ExtCreatePen(PS_GEOMETRIC | PS_SOLID | (DWORD)cap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                              | (DWORD)join, ROUND_TO_LONG(width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                              &logBrush, 0, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
  if (hpen != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    HPEN oldpen = (HPEN) ::SelectObject((HDC)printDC, hpen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    if (oldpen != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
      DeleteObject(oldpen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
  return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
  CATCH_BAD_ALLOC_RET (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
 * Method:    setFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
 * Signature: (JLjava/lang/String;FZZIF)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
JNIEXPORT jboolean JNICALL Java_sun_awt_windows_WPrinterJob_setFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
  (JNIEnv *env, jobject self, jlong printDC, jstring fontName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
   jfloat fontSize, jboolean isBold, jboolean isItalic, jint rotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
   jfloat awScale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    jboolean didSetFont = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1878
    didSetFont = jFontToWFontW(env, (HDC)printDC,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                               fontName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                               fontSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                               isBold,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                               isItalic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                               rotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                               awScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    return didSetFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
 * Try to convert a java font to a GDI font. On entry, 'printDC',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
 * is the device context we want to draw into. 'fontName' is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
 * the name of the font to be matched and 'fontSize' is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
 * size of the font in device coordinates. If there is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
 * equivalent GDI font then this function sets that font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
 * into 'printDC' and returns a 'true'. If there is no equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
 * font then 'false' is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
static jboolean jFontToWFontA(JNIEnv *env, HDC printDC, jstring fontName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                        jfloat fontSize, jboolean isBold, jboolean isItalic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                        jint rotation, jfloat awScale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    LOGFONTA lf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
    LOGFONTA matchedLogFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    BOOL foundFont = false;     // Assume we didn't find a matching GDI font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    memset(&matchedLogFont, 0, sizeof(matchedLogFont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1908
    LPCWSTR fontNameW = JNU_GetStringPlatformChars(env, fontName, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    /* Some fontnames of Non-ASCII fonts like 'MS Minchou' are themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * Non-ASCII.  They are assumed to be written in Unicode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * Hereby, they are converted into platform codeset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    int maxlen = static_cast<int>(sizeof(lf.lfFaceName)) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    // maxlen is int due to cbMultiByte parameter is int
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1917
    int destLen = WideCharToMultiByte(CP_ACP,        // convert to ASCII code page
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1918
                                      0,             // flags
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1919
                                      fontNameW,     // Unicode string
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1920
                                      -1,            // Unicode length is calculated automatically
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                                      lf.lfFaceName, // Put ASCII string here
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1922
                                      maxlen,        // max len
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1923
                                      NULL,          // default handling of unmappables
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  1924
                                      NULL);         // do not care if def char is used
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    /* If WideCharToMultiByte succeeded then the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * of bytes it copied into the face name buffer will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * be creater than zero and we just need to NULL terminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * the string. If there was an error then the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * bytes copied is zero and we can not match the font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    if (destLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        DASSERT(destLen < sizeof(lf.lfFaceName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        lf.lfFaceName[destLen] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        lf.lfCharSet = DEFAULT_CHARSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        lf.lfPitchAndFamily = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        foundFont = !EnumFontFamiliesExA((HDC)printDC, &lf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                                        (FONTENUMPROCA) fontEnumProcA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                                        (LPARAM) &matchedLogFont, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    if (foundFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        /* Build a font of the requested size with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
         * width modifications. A negative font height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
         * tells GDI that we want that values absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
         * value as the font's point size. If the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         * is successfully built then set it as the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         * GDI font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        matchedLogFont.lfHeight = -ROUND_TO_LONG(fontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        matchedLogFont.lfWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        matchedLogFont.lfEscapement = rotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        matchedLogFont.lfOrientation = rotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        matchedLogFont.lfUnderline = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        matchedLogFont.lfStrikeOut = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        /* Force bold or italic if requested. The font name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
           such as Arial Bold may have already set a weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
           so here we just try to increase it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        if (isBold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            matchedLogFont.lfWeight = embolden(matchedLogFont.lfWeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            matchedLogFont.lfWeight = FW_REGULAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        if (isItalic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            matchedLogFont.lfItalic = 0xff;     // TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            matchedLogFont.lfItalic = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        HFONT font = CreateFontIndirectA(&matchedLogFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        if (font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            HFONT oldFont = (HFONT)::SelectObject(printDC, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            if (oldFont != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                ::DeleteObject(oldFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                if (awScale != 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                    TEXTMETRIC tm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                    DWORD avgWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                    GetTextMetrics(printDC, &tm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                    avgWidth = tm.tmAveCharWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                    matchedLogFont.lfWidth = (LONG)((fabs)(avgWidth*awScale));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                    font = CreateFontIndirectA(&matchedLogFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                    if (font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                        oldFont = (HFONT)::SelectObject(printDC, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                        if (oldFont != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                            ::DeleteObject(oldFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                            GetTextMetrics(printDC, &tm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                            foundFont = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                        foundFont = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                foundFont = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            foundFont = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2009
    JNU_ReleaseStringPlatformChars(env, fontName, fontNameW);
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2010
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    return foundFont ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
 * Try to convert a java font to a GDI font. On entry, 'printDC',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
 * is the device context we want to draw into. 'fontName' is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
 * the name of the font to be matched and 'fontSize' is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
 * size of the font in device coordinates. If there is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
 * equivalent GDI font then this function sets that font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
 * into 'printDC' and returns a 'true'. If there is no equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
 * font then 'false' is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
static jboolean jFontToWFontW(JNIEnv *env, HDC printDC, jstring fontName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                        jfloat fontSize, jboolean isBold, jboolean isItalic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                        jint rotation, jfloat awScale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    LOGFONTW lf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    LOGFONTW matchedLogFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    BOOL foundFont = false;     // Assume we didn't find a matching GDI font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    memset(&matchedLogFont, 0, sizeof(matchedLogFont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2033
    LPCWSTR fontNameW = JNU_GetStringPlatformChars(env, fontName, NULL);
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2034
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    /* Describe the GDI fonts we want enumerated. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * simply supply the java font name and let GDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * do the matching. If the java font name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * longer than the GDI maximum font lenght then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * we can't convert the font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     */
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2041
    size_t nameLen = wcslen(fontNameW);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    if (nameLen < (sizeof(lf.lfFaceName) / sizeof(lf.lfFaceName[0]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2044
        wcscpy(lf.lfFaceName, fontNameW);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        lf.lfCharSet = DEFAULT_CHARSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        lf.lfPitchAndFamily = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2049
        foundFont = !::EnumFontFamiliesEx((HDC)printDC, &lf,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                                        (FONTENUMPROCW) fontEnumProcW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                                        (LPARAM) &matchedLogFont, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2054
    JNU_ReleaseStringPlatformChars(env, fontName, fontNameW);
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2055
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    if (!foundFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
    /* Build a font of the requested size with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * width modifications. A negative font height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * tells GDI that we want that values absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * value as the font's point size. If the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * is successfully built then set it as the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * GDI font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    matchedLogFont.lfHeight = -ROUND_TO_LONG(fontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    matchedLogFont.lfWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    matchedLogFont.lfEscapement = rotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    matchedLogFont.lfOrientation = rotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    matchedLogFont.lfUnderline = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
    matchedLogFont.lfStrikeOut = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
    /* Force bold or italic if requested. The font name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * such as Arial Bold may have already set a weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * so here we just try to increase it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    if (isBold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        matchedLogFont.lfWeight = embolden(matchedLogFont.lfWeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        matchedLogFont.lfWeight = FW_REGULAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
    if (isItalic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        matchedLogFont.lfItalic = 0xff;     // TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        matchedLogFont.lfItalic = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    //Debug: dumpLogFont(&matchedLogFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2092
    HFONT font = ::CreateFontIndirect(&matchedLogFont);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    if (font == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    HFONT oldFont = (HFONT)::SelectObject(printDC, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    if (oldFont == NULL) { // select failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        ::DeleteObject(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
    ::DeleteObject(oldFont); // no longer needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    /* If there is a non-uniform scale then get a new version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * of the font with an average width that is condensed or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * expanded to match the average width scaling factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * This is not valid for shearing transforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
    if (awScale != 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        TEXTMETRIC tm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        DWORD avgWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        GetTextMetrics(printDC, &tm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        avgWidth = tm.tmAveCharWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        matchedLogFont.lfWidth = (LONG)((fabs)(avgWidth*awScale));
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2115
        font = ::CreateFontIndirect(&matchedLogFont);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        if (font == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        oldFont = (HFONT)::SelectObject(printDC, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        if (oldFont == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
            ::DeleteObject(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            ::DeleteObject(oldFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
 * Invoked by GDI as a result of the EnumFontFamiliesExW
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
 * call this routine choses a GDI font that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
 * a Java font. When a match is found then function
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
 * returns a zero result to terminate the EnumFontFamiliesExW
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
 * call. The information about the chosen font is copied into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
 * the LOGFONTW structure pointed to by 'lParam'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
static int CALLBACK fontEnumProcW(ENUMLOGFONTEXW *logfont,// logical-font data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                    NEWTEXTMETRICEX *lpntme,              // physical-font data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                    int FontType,                         // type of font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                    LPARAM lParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    LOGFONTW *matchedLogFont = (LOGFONTW *) lParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
    int stop = 0;          // Take the first style found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    if (matchedLogFont != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        *matchedLogFont = logfont->elfLogFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
    return stop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
 * Invoked by GDI as a result of the EnumFontFamiliesExA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
 * call this routine choses a GDI font that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
 * a Java font. When a match is found then function
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
 * returns a zero result to terminate the EnumFontFamiliesExA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
 * call. The information about the chosen font is copied into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
 * the LOGFONTA structure pointed to by 'lParam'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
static int CALLBACK fontEnumProcA(ENUMLOGFONTEXA *logfont,// logical-font data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                    NEWTEXTMETRICEX *lpntme,              // physical-font data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    int FontType,                         // type of font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                    LPARAM lParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    LOGFONTA *matchedLogFont = (LOGFONTA *) lParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    int stop = 0;          // Take the first style found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
    if (matchedLogFont != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        *matchedLogFont = logfont->elfLogFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
    return stop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
 * Given the weight of a font from a GDI LOGFONT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
 * structure, return a new weight indicating a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
 * bolder font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
static int embolden(int currentWeight)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
    /* If the font is less than bold then make
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * it bold. In real life this will mean making
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * a FW_NORMAL font bold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    if (currentWeight < FW_BOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
        currentWeight = FW_BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
    /* If the font is already bold or bolder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * then just increase the weight. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * not be visible with GDI in Win95 or NT4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        currentWeight += EMBOLDEN_WEIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        if (currentWeight > MAX_FONT_WEIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            currentWeight = MAX_FONT_WEIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    return currentWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
 * Method:    setTextColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
 * Signature: (JIII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_setTextColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
(JNIEnv *env, jobject self, jlong printDC, jint red, jint green, jint blue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
    (void) ::SetTextColor( (HDC)printDC, RGB(red, green, blue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
JNIEXPORT jint JNICALL Java_sun_awt_windows_WPrinterJob_getGDIAdvance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
    (JNIEnv *env, jobject self, jlong printDC, jstring text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    SIZE size;
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2222
    LPCWSTR wText = JNU_GetStringPlatformChars(env, text, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    size_t strLen = wcslen(wText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
    BOOL ok = GetTextExtentPoint32((HDC)printDC, wText, (int)strLen, &size);
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2225
    JNU_ReleaseStringPlatformChars(env, text, wText);
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2226
    return ok ? size.cx : 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
 * ETO_PDY is conditionally defined in wingdi.h as it is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
 * only on Windows 2000 and later. ie it requires the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
 * define that it is targeting these APIS by placing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
 * #define _WIN32_WINNT 0x0500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
 * and perhaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
 * #define WINVER 0x5000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
 * before including the headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
 * But this causes many problems for AWT headers subsequently included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
 * So instead hard code the value of the flag as our own macro
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
 * If for any reason this code is executed on Win 9x then this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
 * not be understood and the advances array will be misinterpreted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
 * So we don't use that it in that case and restrict ourselves to x advances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
 * Its possible in some cases that text would then not print as expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
 * However we will not normally supply y advances so this is a less likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
 * code path and its not worth worrying about in we will not in future
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
 * support win9x - and definitely not to this extent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
#define J2D_ETO_PDY 0x2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
 * Method:    textOut
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
 * Signature: (JLjava/lang/String;BFF[F)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
 * Generate GDI text calls for the unicode string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
 * <code>text</code> into the device context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
 * <code>printDC</code>. The text string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
 * positioned at <code>x</code>, <code>y</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
 * The positioning of each glyph in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
 * is determined by windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
 * If 'glyphCodes' is true then the string is 16 bit glyph indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
 * into the font, not character codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
 * strLen needs to be passed in for the glyphCodes case since its possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
 * the missing glyph code may be present, and that is always zero, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
 * would be misinterpreted by GDI and the string functions as null termination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
 * of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_textOut
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
(JNIEnv *env, jobject self, jlong printDC, jstring text, jint strLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     boolean glyphCodes, jfloat x, jfloat y, jfloatArray positions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
    long posX = ROUND_TO_LONG(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    long posY = ROUND_TO_LONG(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
    int flags = (glyphCodes !=0) ? ETO_GLYPH_INDEX : 0;
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2277
    LPCWSTR wText = JNU_GetStringPlatformChars(env, text, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    int *advances = NULL, *xadvances = NULL, *xyadvances = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    BOOL useYAdvances = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
    jfloat *glyphPos = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
    if (positions != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        glyphPos = env->GetFloatArrayElements(positions, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
    /* We need to convert positions relative to the origin of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     * into advances relative to the previous glyph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     * We expect to be able to allocate these small arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * If we fail then we'll print the glyphs using their built-in advances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * Because the array is of inter-character advances we only need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * strLen - 1 entries but Windows looks at the advance between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     * the last character and the non-existent character we allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     * space for that as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     * We supply only the advances that are needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     * - Default advances (ie none) if GDI advances are what we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * - Only X advances if the Y advances are all zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * We allocate two arrays so we can figure out on the fly which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * we need.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * Note that we have to add the 'error' or difference between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     * rounded advance and the floating point advance back into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * calculation of the next advance else the sum of the integer-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * rounded advances will drift away from the true advance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
    if (glyphPos != NULL && strLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
         xadvances = (int*)safe_Malloc(strLen * sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
         xyadvances = (int*)safe_Malloc(strLen * sizeof(int) * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    if (xadvances != NULL && xyadvances != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        int *inxAdvances = xadvances;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
        int *inxyAdvances = xyadvances;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        jfloat *inGlyphPos = glyphPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        jfloat lastX = *inGlyphPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        jfloat lastY = *inGlyphPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        jfloat errorX = 0, errorY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        for (int i = 1; i < strLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            jfloat thisX = *inGlyphPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
            jfloat thisY = *inGlyphPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            jfloat xAdvance = thisX - lastX + errorX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            jfloat yAdvance = thisY - lastY + errorY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            int xadv = ROUND_TO_INT(xAdvance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            errorX = xAdvance - xadv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            int yadv = ROUND_TO_INT(yAdvance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            errorY = yAdvance - yadv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            if (yadv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                useYAdvances = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            *inxAdvances++ = xadv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            *inxyAdvances++ = xadv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            *inxyAdvances++ = yadv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            lastX = thisX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            lastY = thisY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        /* This is the advance from the last character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
         * It is not technically needed, but the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
         * drivers, as opposed to the PostScript driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
         * will fail to print the entire string if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
         * value is absurdly large or absurdly negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        *inxAdvances = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        *inxyAdvances++ = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        *inxyAdvances = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2348
    if (useYAdvances) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        advances = xyadvances;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        flags |= J2D_ETO_PDY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        advances = xadvances;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
    /* Done with the float array parameter, so release it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
    if (glyphPos != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        env->ReleaseFloatArrayElements(positions, glyphPos, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2360
    BOOL drawn = ::ExtTextOut((HDC)printDC,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                    posX, posY,     // starting position for the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                    flags,          // glyphCodes?, y advances?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                    NULL,           // optional clipping-opaquing rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                    wText,          // the Unicode text to draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                    static_cast<UINT>(strLen),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                    advances);      // intercharacter advances or NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
    if (xadvances != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        free(xadvances);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    if (xyadvances != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        free(xyadvances);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    }
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2374
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2375
    JNU_ReleaseStringPlatformChars(env, text, wText);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
 * Scans a 24 bit RGB DIB image looking for the first non-white line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
 * On entry, if scanLineStride is negative, 'image' points at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
 * bottom of the DIB, which is where the first scan line is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
 * Alternatively, if scanLineStride is positive, it's a top-down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
 * DIB and 'image'  points to the top scan line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
 * 'numLinesP', on entry, is the number of scan lines in the image while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
 * 'width' is the number of 24 bit pixels on each line. If a non-white
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
 * line is found in the DIB, then a pointer to the first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
 * working from the bottom, non-white scan line is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
 * and the number of remaining scan lines is returned in  *'numLinesP'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
 * Pixels are 3 byte BGR triples, so any byte that is not 0xff indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
 * its a component of a non-white pixel. So we don't need to combine bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
 * into pixels. Simply scan the image looking for any byte that is not 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
static jbyte *findNonWhite(jbyte *image, long sy, long width, long height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                          long scanLineStride, long *numLinesP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
    long found = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
    long numLines = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    jbyte *startLine = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
    unsigned char *inLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
    const unsigned char cc = (unsigned char)0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
    assert(image != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    assert(0 <= sy && sy < height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
    assert(0 < width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
    assert(0 < height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
    assert(numLinesP != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
    for (numLines = 0; sy < height; numLines++, sy++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        inLine = (unsigned char*)startLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        for (long colcomp = 0; colcomp < abs(scanLineStride); colcomp++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            if (*inLine++ != cc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                found = sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        if(found != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        startLine += scanLineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
    *numLinesP = numLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
    return found == -1 ? NULL : startLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
/* Find the 1st scanline that's entirely white.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
 * The starting scanline pointed to by 'image' may be part way through the DIB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
 * If an all white scanline is found, the return value points to the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
 * of the last scanline with a non-white pixel. If no all white scanlines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
 * are found, the starting scanline is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
 * '*numLinesP' returns the number of non-white scan lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
 * Skip the 1st scanline as its always non-white.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
 * If passed scanLineStride is negative, the DIB is bottom-up,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
 * otherwise it's top-down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
static jbyte *findWhite(jbyte *image, long sy, long width, long height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                        long scanLineStride, long *numLinesP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
    long numLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    jbyte *startLine = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    unsigned char *inLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
    jbyte *found = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    long white;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    const unsigned char cc = (unsigned char)0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
    assert(image != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    assert(0 <= sy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    assert(0 < width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    assert(0 < height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
    assert(numLinesP != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
    ++sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
    for(numLines = 1; sy < height; numLines++, sy++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        startLine += scanLineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        inLine = (unsigned char*)startLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        white = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        for (long colcomp = 0; colcomp < abs(scanLineStride); colcomp++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
            if (*inLine++ != cc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                white = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
        if (white != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
           found = startLine - scanLineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
           break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
    *numLinesP = numLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
    return found == NULL ? startLine : found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
 * Reverses the bitmap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
 * Returns pointer to reversed bitmap (DWORD aligned).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
 * Returns NULL if unsuccessful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
 * NOTE: Caller must free the pointer returned by calling free.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
static jbyte* reverseDIB(jbyte* imageBits, long srcWidth, long srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                          int bitsperpixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
    /* get width in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     * If the image is 24bpp, its srcWidth*3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * If the image is 8bpp, its just srcWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * If the image is 1bpp or 4bpp one then its rounded up to the next byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
    long imgWidthByteSz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
    switch (bitsperpixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
    case 24 : imgWidthByteSz = srcWidth * 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
    case 8 :  imgWidthByteSz = srcWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
    case 1 :  imgWidthByteSz = (srcWidth + 7) / 8 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
    case 4 :  imgWidthByteSz = (srcWidth + 1) / 2 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
    default : /* not expected but this is OK for any exact multiple of 8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        imgWidthByteSz = srcWidth * bitsperpixel / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
    int padBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
    /* make it DWORD aligned */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
    if ((imgWidthByteSz % sizeof(DWORD)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        padBytes = sizeof(DWORD) - (imgWidthByteSz % sizeof(DWORD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
    long newImgSize = (imgWidthByteSz+padBytes) * ROUND_TO_LONG(srcHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
    jbyte* alignedImage = (jbyte*) safe_Malloc(newImgSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    if (alignedImage != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
        memset(alignedImage, 0xff, newImgSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        jbyte* imgLinePtr = alignedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
        for (long i=ROUND_TO_LONG(srcHeight)-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
            memcpy(imgLinePtr, imageBits+(i*imgWidthByteSz),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                   imgWidthByteSz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
            imgLinePtr += (imgWidthByteSz + padBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
        return alignedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
 * Method:    drawImageIntRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
 * Signature: (J[IFFFFFFFFII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_drawImageIntRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
  (JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
   jlong printDC, jintArray image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
   jfloat destX, jfloat destY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
   jfloat destWidth, jfloat destHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
   jfloat srcX, jfloat srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
   jfloat srcWidth, jfloat srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
   jint srcBitMapWidth, jint srcBitMapHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
    assert(printDC != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
    assert(image != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
    assert(srcX >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
    assert(srcY >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
    assert(srcWidth > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
    assert(srcHeight > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    assert(srcBitMapWidth > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
    assert(srcBitMapHeight > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
    static int alphaMask =  0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
    static int redMask =    0x00ff0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
    static int greenMask =  0x0000ff00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
    static int blueMask =   0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
    struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
        BITMAPV4HEADER header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
        DWORD          masks[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
    } dib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
    memset(&dib,0,sizeof(dib));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
    dib.header.bV4Size = sizeof(dib.header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
    dib.header.bV4Width = srcBitMapWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
    dib.header.bV4Height = -srcBitMapHeight;    // Top down DIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
    dib.header.bV4Planes = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
    dib.header.bV4BitCount = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
    dib.header.bV4V4Compression = BI_BITFIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
    dib.header.bV4SizeImage = 0;        // It's the default size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
    dib.header.bV4XPelsPerMeter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
    dib.header.bV4YPelsPerMeter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
    dib.header.bV4ClrUsed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
    dib.header.bV4ClrImportant = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
    dib.header.bV4RedMask = redMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
    dib.header.bV4GreenMask = greenMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
    dib.header.bV4BlueMask = blueMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
    dib.header.bV4AlphaMask = alphaMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
    dib.masks[0] = redMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
    dib.masks[1] = greenMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
    dib.masks[2] = blueMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
    dib.masks[3] = alphaMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
    jint *imageBits = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        imageBits = (jint *)env->GetPrimitiveArrayCritical(image, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        if (printDC){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            result = ::StretchDIBits( (HDC)printDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                                      ROUND_TO_LONG(destX),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                                      ROUND_TO_LONG(destY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                                      ROUND_TO_LONG(destWidth),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                                      ROUND_TO_LONG(destHeight),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                                      ROUND_TO_LONG(srcX),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                                      ROUND_TO_LONG(srcY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                                      ROUND_TO_LONG(srcWidth),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                                      ROUND_TO_LONG(srcHeight),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                                      imageBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                                      (BITMAPINFO *)&dib,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                                      DIB_RGB_COLORS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                                      SRCCOPY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
    } catch (...) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        if (imageBits != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            env->ReleasePrimitiveArrayCritical(image, imageBits, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        throw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
    env->ReleasePrimitiveArrayCritical(image, imageBits, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
 * Class:     sun_awt_windows_WPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
 * Method:    drawDIBImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
 * Signature: (J[BFFFFFFFFI[B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
JNIEXPORT void JNICALL Java_sun_awt_windows_WPrinterJob_drawDIBImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
  (JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
   jlong printDC, jbyteArray image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
   jfloat destX, jfloat destY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
   jfloat destWidth, jfloat destHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
   jfloat srcX, jfloat srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
   jfloat srcWidth, jfloat srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
   jint bitCount, jbyteArray bmiColorsArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
    assert(printDC != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
    assert(image != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
    assert(srcX >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
    assert(srcY >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
    assert(srcWidth > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
    assert(srcHeight > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
#define MAXCOLS 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
    struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        BITMAPINFOHEADER bmiHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        RGBQUAD         bmiColors[MAXCOLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    } bmi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
    memset(&bmi, 0, sizeof(bmi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
    bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
    bmi.bmiHeader.biWidth = ROUND_TO_LONG(srcWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
    bmi.bmiHeader.biHeight = ROUND_TO_LONG(srcHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
    bmi.bmiHeader.biPlanes = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
    bmi.bmiHeader.biBitCount = (WORD)bitCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
    bmi.bmiHeader.biCompression = BI_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
    bmi.bmiHeader.biSizeImage = 0;        // It's the default size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
    bmi.bmiHeader.biXPelsPerMeter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
    bmi.bmiHeader.biYPelsPerMeter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
    bmi.bmiHeader.biClrUsed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
    bmi.bmiHeader.biClrImportant = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
    jint *imageBits = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
        if (bmiColorsArray != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            BYTE* bmiCols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            int numCols = 1<<bitCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            if (numCols > MAXCOLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                numCols = MAXCOLS; /* don't write past end of struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            bmiCols = (BYTE*)env->GetPrimitiveArrayCritical(bmiColorsArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
            memcpy(&(bmi.bmiColors[0]), bmiCols, (numCols*4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
            env->ReleasePrimitiveArrayCritical(bmiColorsArray, bmiCols, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        imageBits = (jint *)env->GetPrimitiveArrayCritical(image, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        // Workaround for drivers/apps that do not support top-down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        // Because we don't know if they support or not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        // always send bottom-up DIBs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
        jbyte *dibImage = reverseDIB((jbyte*)imageBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                                     (long)srcWidth, (long)srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                                     bitCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        if (dibImage != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
          if (printDC){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
            result = ::StretchDIBits( (HDC)printDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                                      ROUND_TO_LONG(destX),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                                      ROUND_TO_LONG(destY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                                      ROUND_TO_LONG(destWidth),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                                      ROUND_TO_LONG(destHeight),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                                      ROUND_TO_LONG(srcX),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                                      ROUND_TO_LONG(srcY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                                      ROUND_TO_LONG(srcWidth),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                                      ROUND_TO_LONG(srcHeight),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                                      dibImage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                                      (BITMAPINFO*)(&bmi),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                                      DIB_RGB_COLORS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                                      SRCCOPY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
          free(dibImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        } /* if (dibImage != NULL) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
    } catch (...) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
        if (imageBits != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
            env->ReleasePrimitiveArrayCritical(image, imageBits, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
        JNU_ThrowInternalError(env, "Problem in WPrinterJob_drawDIBImage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
    env->ReleasePrimitiveArrayCritical(image, imageBits, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
 * An utility function to print passed image byte array to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
 * the printDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
 * browserPrinting flag controls whether the image array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
 * used as top-down (browserPrinting == JNI_TRUE) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
 * bottom-up (browserPrinting == JNI_FALSE) DIB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
static void doPrintBand(JNIEnv *env, jboolean browserPrinting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                        HDC printDC, jbyteArray imageArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
                        jint x, jint y, jint width, jint height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
    jbyte *image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        long scanLineStride = J2DRasterBPP * width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        image = (jbyte *)env->GetPrimitiveArrayCritical(imageArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        jbyte *startImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
        jbyte *endImage = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
        long startY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
        long numLines = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
        if (browserPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
            /* for browser printing use top-down approach */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
            startImage =  image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
            /* when printing to a real printer dc, the dib
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
               should bottom-up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
            startImage =  image + (scanLineStride * (height - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            scanLineStride = -scanLineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            startImage = findNonWhite(startImage, startY, width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                                      scanLineStride, &numLines);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
            if (startImage != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                startY += numLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                endImage = findWhite(startImage, startY, width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                                     scanLineStride, &numLines);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                if (browserPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                    /* passing -numLines as height to indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                       we treat the image as a top-down DIB */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                    bitsToDevice(printDC, startImage, x, y + startY, width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                                 -numLines);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                    bitsToDevice(printDC, endImage, x, y + startY, width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                                 numLines);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                startImage = endImage + scanLineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                startY += numLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
        } while (startY < height && startImage != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
    } catch (...) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
        if (image != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            env->ReleasePrimitiveArrayCritical(imageArray, image, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        throw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
    env->ReleasePrimitiveArrayCritical(imageArray, image, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
static FILE* outfile = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
static int bitsToDevice(HDC printDC, jbyte *image, long destX, long destY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                        long width, long height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
    assert(printDC != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
    assert(image != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
    assert(destX >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
    assert(destY >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
    assert(width > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
    /* height could be negative to indicate that this is a top-down DIB */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
//      assert(height > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
    struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
        BITMAPINFOHEADER bmiHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
        DWORD*             bmiColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
    } bitMapHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
    memset(&bitMapHeader,0,sizeof(bitMapHeader));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
    bitMapHeader.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
    bitMapHeader.bmiHeader.biWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
    bitMapHeader.bmiHeader.biHeight = height; // does -height work ever?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
    bitMapHeader.bmiHeader.biPlanes = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
    bitMapHeader.bmiHeader.biBitCount = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
    bitMapHeader.bmiHeader.biCompression = BI_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
    bitMapHeader.bmiHeader.biSizeImage = 0;     // It's the default size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
    bitMapHeader.bmiHeader.biXPelsPerMeter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
    bitMapHeader.bmiHeader.biYPelsPerMeter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
    bitMapHeader.bmiHeader.biClrUsed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
    bitMapHeader.bmiHeader.biClrImportant = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
    bitMapHeader.bmiColors = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
    height = abs(height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
    // Workaround for drivers/apps that do not support top-down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    // Because we don't know if they support or not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
    // always send bottom-up DIBs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
    if (bitMapHeader.bmiHeader.biHeight < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
      jbyte *dibImage = reverseDIB(image, width, height, 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
      if (dibImage != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
        bitMapHeader.bmiHeader.biWidth = ROUND_TO_LONG(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
        bitMapHeader.bmiHeader.biHeight = ROUND_TO_LONG(height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
        if (printDC){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
          result = ::SetDIBitsToDevice(printDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                                ROUND_TO_LONG(destX),   // left of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
                                ROUND_TO_LONG(destY),   // top of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
                                ROUND_TO_LONG(width),   // width of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                                ROUND_TO_LONG(height),  // height of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                                0,      // left of source rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                                0,      // top of source rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                                0,      // line number of 1st source scan line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                                ROUND_TO_LONG(height),  // number of scan lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                                dibImage,       // points to the DIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                                (BITMAPINFO *)&bitMapHeader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                                DIB_RGB_COLORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
        free (dibImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
      if (printDC){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
          result = ::SetDIBitsToDevice(printDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
                                destX,  // left of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
                                destY,  // top of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
                                width,  // width of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                                height, // height of dest rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
                                0,      // left of source rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
                                0,      // top of source rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
                                0,      // line number of 1st source scan line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
                                height, // number of source scan lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
                                image,  // points to the DIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                                (BITMAPINFO *)&bitMapHeader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                                DIB_RGB_COLORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
LRESULT CALLBACK PageDialogWndProc(HWND hWnd, UINT message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                                   WPARAM wParam, LPARAM lParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
    switch (message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        case WM_COMMAND: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
            if ((LOWORD(wParam) == IDOK) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
                (LOWORD(wParam) == IDCANCEL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                // If we recieve on of these two notifications, the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                // is about to be closed. It's time to unblock all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                // windows blocked by this dialog, as doing so from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
                // WM_DESTROY handler is too late
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
                jobject peer = (jobject)(::GetProp(hWnd, ModalDialogPeerProp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                env->CallVoidMethod(peer, AwtPrintDialog::setHWndMID, (jlong)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
2456
45ee87a35349 6792023: Print suspends on Windows 2000 Pro since 6u12 b01
dcherepanov
parents: 1954
diff changeset
  2888
    WNDPROC lpfnWndProc = (WNDPROC)(::GetProp(hWnd, NativeDialogWndProcProp));
45ee87a35349 6792023: Print suspends on Windows 2000 Pro since 6u12 b01
dcherepanov
parents: 1954
diff changeset
  2889
    return ComCtl32Util::GetInstance().DefWindowProc(lpfnWndProc, hWnd, message, wParam, lParam);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
 * Called by the Page Setup dialog this routine makes sure the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
 * print dialog becomes the front most window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
static UINT CALLBACK pageDlgHook(HWND hDlg, UINT msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                                 WPARAM wParam, LPARAM lParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
    switch(msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        case WM_INITDIALOG: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
            PAGESETUPDLG *psd = (PAGESETUPDLG *)lParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
            jobject peer = (jobject)(psd->lCustData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
            env->CallVoidMethod(peer, AwtPrintDialog::setHWndMID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
                                (jlong)hDlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
            ::SetProp(hDlg, ModalDialogPeerProp, reinterpret_cast<HANDLE>(peer));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
            SetForegroundWindow(hDlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
            // set appropriate icon for parentless dialogs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
            jobject awtParent = env->GetObjectField(peer, AwtPrintDialog::parentID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
            if (awtParent == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                ::SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_BIG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                              (LPARAM)AwtToolkit::GetInstance().GetAwtIcon());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                env->DeleteLocalRef(awtParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
            // subclass dialog's parent to receive additional messages
2456
45ee87a35349 6792023: Print suspends on Windows 2000 Pro since 6u12 b01
dcherepanov
parents: 1954
diff changeset
  2923
            WNDPROC lpfnWndProc = ComCtl32Util::GetInstance().SubclassHWND(hDlg,
45ee87a35349 6792023: Print suspends on Windows 2000 Pro since 6u12 b01
dcherepanov
parents: 1954
diff changeset
  2924
                                                                           PageDialogWndProc);
45ee87a35349 6792023: Print suspends on Windows 2000 Pro since 6u12 b01
dcherepanov
parents: 1954
diff changeset
  2925
            ::SetProp(hDlg, NativeDialogWndProcProp, reinterpret_cast<HANDLE>(lpfnWndProc));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
        case WM_DESTROY: {
2456
45ee87a35349 6792023: Print suspends on Windows 2000 Pro since 6u12 b01
dcherepanov
parents: 1954
diff changeset
  2930
            WNDPROC lpfnWndProc = (WNDPROC)(::GetProp(hDlg, NativeDialogWndProcProp));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
            ComCtl32Util::GetInstance().UnsubclassHWND(hDlg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                                                       PageDialogWndProc,
2456
45ee87a35349 6792023: Print suspends on Windows 2000 Pro since 6u12 b01
dcherepanov
parents: 1954
diff changeset
  2933
                                                       lpfnWndProc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
            ::RemoveProp(hDlg, ModalDialogPeerProp);
2456
45ee87a35349 6792023: Print suspends on Windows 2000 Pro since 6u12 b01
dcherepanov
parents: 1954
diff changeset
  2935
            ::RemoveProp(hDlg, NativeDialogWndProcProp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
    return (UINT) FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
    CATCH_BAD_ALLOC_RET(TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
 *      Create and return a printer device context for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
 *      default printer. If there is no default printer then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
 *      return NULL. This fn is used when printing is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
 *      and no user dialog was created. So despite its name, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
 *      needs to return a DC which reflects all the applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
 *      settings which the driver might support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
 *      The number of copies is the most important setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
static HDC getDefaultPrinterDC(JNIEnv *env, jobject printerJob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
    HDC printDC = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
    int devWillDoCopies = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
    PRINTDLG pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
    memset(&pd, 0, sizeof(PRINTDLG));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
    pd.lStructSize = sizeof(PRINTDLG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
    pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 885
diff changeset
  2963
    if (::PrintDlg(&pd)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        printDC = pd.hDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        /* Find out how many copies the driver can do, and use driver's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
         * dmCopies if requested number is within that limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
        int maxCopies = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
        int nCopies = getCopies(env, printerJob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
        SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
        if (pd.hDevNames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
            DEVNAMES *devnames = (DEVNAMES *)::GlobalLock(pd.hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            if (devnames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                LPTSTR lpdevnames = (LPTSTR)devnames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                LPTSTR printer = lpdevnames+devnames->wDeviceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                LPTSTR port = lpdevnames+devnames->wOutputOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
                // if DeviceCapabilities fails, return value is -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                maxCopies = (int)::DeviceCapabilities(printer, port, DC_COPIES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                                                      NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                if (maxCopies > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
                    devWillDoCopies = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
            ::GlobalUnlock(pd.hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        if ((maxCopies >= nCopies) && (pd.hDevMode != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
            DEVMODE *devmode = (DEVMODE *)::GlobalLock(pd.hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
            if (devmode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                if ((devmode->dmFields & DM_COPIES) && (nCopies > 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                    devmode->dmCopies = nCopies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                    HDC tmpDC = ::ResetDC(pd.hDC, devmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
                    RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                    if (tmpDC != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                        printDC = tmpDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
            ::GlobalUnlock(pd.hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        /* Not pretty that this is set in a separate place then the DC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        if (pd.hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
            AwtPrintControl::setPrintHDMode(env, printerJob, pd.hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
        if (pd.hDevNames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
            AwtPrintControl::setPrintHDName(env, printerJob, pd.hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        setBooleanField(env, printerJob, DRIVER_COPIES_STR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                        (devWillDoCopies ? JNI_TRUE : JNI_FALSE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
        setBooleanField(env, printerJob, DRIVER_COLLATE_STR, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
        setBooleanField(env, printerJob, USER_COLLATE_STR, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
    return printDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
 * Move the description of the page's size and orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
 * from the PageFormat object 'page' into the structure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
 * 'setup' used by Windows to display the Page Setup dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
static void pageFormatToSetup(JNIEnv *env, jobject job,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
                              jobject page, PAGESETUPDLG *setup, HDC hDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
    RectDouble paperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
    RectDouble margins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
    /* Move the orientation from PageFormat to Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
    jint orient = getPageFormatOrientation(env, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
    int gdiOrientation = (orient == PAGEFORMAT_PORTRAIT) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
        DMORIENT_PORTRAIT : DMORIENT_LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
    setOrientationInDevMode(setup->hDevMode, orient == PAGEFORMAT_PORTRAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
    int units = (setup->Flags & PSD_INTHOUSANDTHSOFINCHES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                                                ? MM_HIENGLISH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                                                : MM_HIMETRIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
    jobject paper = getPaper(env, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
    getPaperValues(env, paper, &paperSize, &margins);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
    // Setting the paper size appears to be a futile exercise, as its not one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
    // of the values you can initialise - its an out-only arg. Margins are OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
    // set it into the DEVMODE if there is one ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
    setup->ptPaperSize.x = convertFromPoints(paperSize.width, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
    setup->ptPaperSize.y = convertFromPoints(paperSize.height, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
    if (setup->hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
        double paperWidth, paperHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
        WORD dmPaperSize = getPrintPaperSize(env, job);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
        matchPaperSize(hDC, setup->hDevMode, setup->hDevNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                       paperSize.width,  paperSize.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                       &paperWidth, &paperHeight, &dmPaperSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
        DEVMODE *devmode = (DEVMODE *)::GlobalLock(setup->hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
        if (devmode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
          if (dmPaperSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            devmode->dmFields |= DM_PAPERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
            devmode->dmPaperSize = dmPaperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
          else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
            devmode->dmFields |= DM_PAPERLENGTH | DM_PAPERWIDTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
              | DM_PAPERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            devmode->dmPaperSize = DMPAPER_USER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
            devmode->dmPaperWidth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
              (short)(convertFromPoints(paperSize.width, MM_LOMETRIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
            devmode->dmPaperLength =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
              (short)(convertFromPoints(paperSize.height, MM_LOMETRIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        ::GlobalUnlock(setup->hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
    // When setting up these values, account for the orientation of the Paper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
    // in the PageFormat. In the margins Rect when in portrait mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
    // width is really right margin, height is really bottom margin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
    if (orient == PAGEFORMAT_PORTRAIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
        setup->rtMargin.left = convertFromPoints(margins.x, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        setup->rtMargin.top  = convertFromPoints(margins.y, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        setup->rtMargin.right = convertFromPoints(margins.width, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
        setup->rtMargin.bottom = convertFromPoints(margins.height, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
    } else if (orient == PAGEFORMAT_LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
        setup->rtMargin.left = convertFromPoints(margins.height, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
        setup->rtMargin.top  = convertFromPoints(margins.x, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
        setup->rtMargin.right = convertFromPoints(margins.y, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
        setup->rtMargin.bottom = convertFromPoints(margins.width, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
    } else { // reverse landscape
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
        setup->rtMargin.left = convertFromPoints(margins.y, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
        setup->rtMargin.top  = convertFromPoints(margins.width, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        setup->rtMargin.right = convertFromPoints(margins.height, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        setup->rtMargin.bottom = convertFromPoints(margins.x, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
    // Set page size here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
 * Return an array of POINTS describing the paper sizes supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
 * by the driver identified by 'deviceName' and 'portName'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
 * If there is an error, then NULL is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
static POINT *getPaperSizeList(LPCTSTR deviceName, LPCTSTR portName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
    DWORD numPaperSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
    POINT *paperSizes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
    SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
    numPaperSizes = DeviceCapabilities(deviceName, portName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
                                       DC_PAPERSIZE, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
    if (numPaperSizes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
        paperSizes = (POINT *)safe_Malloc(sizeof(*paperSizes) * numPaperSizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
        DWORD result = DeviceCapabilities(deviceName, portName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
                                          DC_PAPERSIZE, (LPTSTR) paperSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
                                          NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
        if (result == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
            free((char *) paperSizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
            paperSizes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
    RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    return paperSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
static WORD getOrientationFromDevMode2(HGLOBAL hDevMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
    WORD orient = DMORIENT_PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
    if (hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
        LPDEVMODE devMode = (LPDEVMODE) GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
        if ((devMode != NULL) && (devMode->dmFields & DM_ORIENTATION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
            orient = devMode->dmOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
        GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
    return orient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
 * Get the orientation of the paper described by the printer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
 * handle to a device mode structure 'hDevMode'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
static WORD getOrientationFromDevMode(JNIEnv *env, jobject self) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
    return getOrientationFromDevMode2(AwtPrintControl::getPrintHDMode(env, self));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
 * Set the orientation of the paper described by the printer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
 * handle to a device mode structure 'hDevMode'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
static void setOrientationInDevMode(HGLOBAL hDevMode, jboolean isPortrait) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
    if (hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        LPDEVMODE devMode = (LPDEVMODE) GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
        if (devMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
            devMode->dmOrientation = isPortrait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
                                    ? DMORIENT_PORTRAIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
                                    : DMORIENT_LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            devMode->dmFields |= DM_ORIENTATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
        GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
 * Return the paper size and margins for the page
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
 * adjusted to take into account the portrait or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
 * landscape orientation of the page. On entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
 * 'setup' is a filled in structure as returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
 * by PageSetupDlg(). 'paperSize', 'margins',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
 * and 'orientation' all point to caller allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
 * space while will be filled in by this routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
 * with the size, in unknown Windows units, of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
 * the paper, of the margins, and an indicator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
 * whether the page is in portrait or landscape
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
 * orientation, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
static void retrievePaperInfo(const PAGESETUPDLG *setup, POINT *paperSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
                              RECT *margins, jint *orientation, HDC hdc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
    int orientationKnown = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
    *paperSize = setup->ptPaperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
    int gdiOrientation = DMORIENT_PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
    /* Usually the setup dialog will tell us the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
     * orientation of the page, but it may not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
    if (setup->hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
        gdiOrientation = getOrientationFromDevMode2(setup->hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
        orientationKnown = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
    /* The driver didn't tell us the paper orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
     * so we declare it landscape if the paper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
     * is wider than it is long. Square paper is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
     * declared to be portait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
    if (orientationKnown == FALSE && paperSize->x > paperSize->y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
        gdiOrientation = DMORIENT_LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
    *margins = setup->rtMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
    // compare margin from page setup dialog with our device printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
    RectDouble deviceMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
    if (getPrintableArea(hdc, setup->hDevMode, &deviceMargin) == TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
        RECT devMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
        int units = (setup->Flags & PSD_INTHOUSANDTHSOFINCHES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
          ? MM_HIENGLISH : MM_HIMETRIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
        devMargin.left = convertFromPoints(deviceMargin.x*72, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        devMargin.top = convertFromPoints(deviceMargin.y*72, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
        devMargin.bottom = paperSize->y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
          - convertFromPoints(deviceMargin.height*72, units)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
          - devMargin.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
        devMargin.right = paperSize->x
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
          - convertFromPoints(deviceMargin.width*72, units)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
          - devMargin.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
        if (margins->left < devMargin.left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
            margins->left = devMargin.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
        if (margins->top < devMargin.top) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
            margins->top = devMargin.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
        if (margins->bottom < devMargin.bottom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
            margins->bottom = devMargin.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
        if (margins->right < devMargin.right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
            margins->right = devMargin.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
    /* The Paper class expresses the page size in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     * portait mode while Windows returns the paper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
     * size adjusted for the orientation. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
     * orientation is landscape then we want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
     * flip the width and height to get a portait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
     * description of the page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
    if (gdiOrientation != DMORIENT_PORTRAIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
        long hold = paperSize->x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
        paperSize->x = paperSize->y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
        paperSize->y = hold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
        margins->left = setup->rtMargin.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
        margins->right = setup->rtMargin.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
        margins->top = setup->rtMargin.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
        margins->bottom = setup->rtMargin.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
    if (gdiOrientation == DMORIENT_PORTRAIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
        *orientation = PAGEFORMAT_PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
        *orientation = PAGEFORMAT_LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
 * Return the number of copies to be printed for a printerJob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
static jint getCopies(JNIEnv *env, jobject printerJob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
    // Because this function may call client Java code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
    // we can't run it on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
    jclass printerJobClass = env->GetObjectClass(printerJob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
    jmethodID getCopiesID = env->GetMethodID(printerJobClass, GETCOPIES_STR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
                                             GETCOPIES_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
    jint copies = env->CallIntMethod(printerJob, getCopiesID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
    return copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
 * Return a copy of the Paper object attached to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
 * PageFormat object 'page.'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
static jobject getPaper(JNIEnv *env, jobject page) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
    // Because this function may call client Java code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
    // we can't run it on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
    jclass pageClass = env->GetObjectClass(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
    jmethodID getPaperID = env->GetMethodID(pageClass, GETPAPER_STR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                                                        GETPAPER_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
    return env->CallObjectMethod(page, getPaperID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
 * Set the Paper object for a PageFormat instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
 * 'paper' is the new Paper object that must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
 * set into 'page'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
static void setPaper(JNIEnv *env, jobject page, jobject paper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
    // Because this function may call client Java code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
    // we can't run it on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
    jclass pageClass = env->GetObjectClass(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
    jmethodID setPaperID = env->GetMethodID(pageClass, SETPAPER_STR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                                                        SETPAPER_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
    env->CallVoidMethod(page, setPaperID, paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
 * Return the integer ID for the orientation in the PageFormat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
 * Caution: this is the Java spec ID, not the GDI ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
static jint getPageFormatOrientation(JNIEnv *env, jobject page) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
    // Because this function may call client Java code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
    // we can't run it on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
    jclass pageClass = env->GetObjectClass(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
    jmethodID getOrientID = env->GetMethodID(pageClass, GETORIENT_STR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                                                        GETORIENT_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
    return env->CallIntMethod(page, getOrientID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
static void setPageFormatOrientation(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
                                     jobject page, jint orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
    // Because this function may call client Java code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
    // we can't run it on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
    jclass pageClass = env->GetObjectClass(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
    jmethodID setOrientID = env->GetMethodID(pageClass, SETORIENT_STR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                                                        SETORIENT_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
    env->CallVoidMethod(page, setOrientID, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
 * Pull the paper size and margins out of the paper object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
 * return them in points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
static void getPaperValues(JNIEnv *env, jobject paper, RectDouble *paperSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                          RectDouble *margins, BOOL widthAsMargin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
    // Because this function may call client Java code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
    // we can't run it on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
    jmethodID getID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
    paperSize->x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
    paperSize->y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
    jclass paperClass = env->GetObjectClass(paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
    getID = env->GetMethodID(paperClass, GETWIDTH_STR, GETWIDTH_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
    paperSize->width = env->CallDoubleMethod(paper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
    getID = env->GetMethodID(paperClass, GETHEIGHT_STR, GETHEIGHT_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
    paperSize->height = env->CallDoubleMethod(paper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
    getID = env->GetMethodID(paperClass, GETIMG_X_STR, GETIMG_X_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
    margins->x = env->CallDoubleMethod(paper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
    if (margins-> x < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
        margins-> x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    getID = env->GetMethodID(paperClass, GETIMG_Y_STR, GETIMG_Y_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
    margins->y = env->CallDoubleMethod(paper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
    if (margins-> y < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
        margins-> y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
    getID = env->GetMethodID(paperClass, GETIMG_W_STR, GETIMG_W_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
    if (widthAsMargin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
        margins->width = paperSize->width - margins->x
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
                                      - env->CallDoubleMethod(paper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
        margins->width = env->CallDoubleMethod(paper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
    if (margins->width < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
        margins->width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
    getID = env->GetMethodID(paperClass, GETIMG_H_STR, GETIMG_H_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
    if (widthAsMargin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
        margins->height = paperSize->height - margins->y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
                                        - env->CallDoubleMethod(paper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
        margins->height = env->CallDoubleMethod(paper, getID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
    if (margins->height < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
        margins->height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
 * Given a RECT specifying the margins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
 * for the page and an indication of whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
 * the units are 1000ths of an inch (MM_HIENGLISH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
 * or 100ths of a millimeter (MM_HIMETRIC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
 * convert the margins to 72nds of an inch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
 * and set them into the PageFormat insance provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
static void setPaperValues(JNIEnv *env, jobject paper, const POINT *paperSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                                         const RECT *margins, int units) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
    // Because this function may call client Java code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
    // we can't run it on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
    DASSERT(AwtToolkit::MainThread() != ::GetCurrentThreadId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
    jclass paperClass = env->GetObjectClass(paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
    jmethodID setSizeID = env->GetMethodID(paperClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
                                        SETSIZE_STR, SETSIZE_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
    jmethodID setImageableID = env->GetMethodID(paperClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
                                        SETIMAGEABLE_STR, SETIMAGEABLE_SIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
    /* Set the physical size of the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
    jdouble paperWidth = convertToPoints(paperSize->x, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
    jdouble paperHeight = convertToPoints(paperSize->y, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
    env->CallVoidMethod(paper, setSizeID, paperWidth, paperHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
    /* Set the margins of the paper. In Windows' margin RECT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     * the right and bottom parts of the structure are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     * really the right and bottom of the imageable rectangle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
     * but rather the right and bottom margins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
    jdouble x = convertToPoints(margins->left, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
    jdouble y = convertToPoints(margins->top, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
    long intWidth = paperSize->x - margins->left - margins->right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
    long intHeight = paperSize->y - margins->top - margins->bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
    jdouble width = convertToPoints(intWidth, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
    jdouble height = convertToPoints(intHeight, units);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
    env->CallVoidMethod(paper, setImageableID, x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
 * Convert 'value' a measurement in 1/72's of an inch to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
 * the units specified by 'units' - either MM_HIENGLISH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
 * MM_HIMETRIC, or MM_LOMETRIC. The converted value is returned as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
 * a long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
static long convertFromPoints(double value, int units) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
    double conversion = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
    switch (units){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
     case MM_HIENGLISH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
        conversion = POINTS_TO_HIENGLISH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
     case MM_HIMETRIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
        conversion = POINTS_TO_HIMETRIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
     case MM_LOMETRIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
        conversion = POINTS_TO_LOMETRIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
     default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
        assert(FALSE);  // Unsupported unit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
    // Adding 0.5 ensures that the integer portion has the expected magnitude
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
    // before truncation occurs as result of converting from double to long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
    return (long) ((value * conversion) + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
 * Convert a measurement, 'value', from the units
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
 * specified by 'units', either MM_HIENGLISH or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
 * MM_HIMETRIC to 1/72's of an inch and returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
 * as a double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
static double convertToPoints(long value, int units) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
    double convertedValue = (double)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
    switch (units){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
    case MM_HIENGLISH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
        //convertedValue *= HIENGLISH_TO_POINTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
        // this order of calculation is for bug 4191615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
        convertedValue = (convertedValue*72.0) / 1000.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
    case MM_HIMETRIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
        convertedValue *= HIMETRIC_TO_POINTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
    case MM_LOMETRIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
        convertedValue *= LOMETRIC_TO_POINTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
        assert(FALSE);  // Unsupported unit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
    //Need to round off to the precision of the initial value. FIX.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
    return convertedValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
 *      Ask the printer device context, 'printDC' about
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
 *      its capabilities and set these into the WPrintJob2D
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
 *      object 'self'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
void setCapabilities(JNIEnv *env, jobject self, HDC printDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
    // width of page in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
    jint pageWid = GetDeviceCaps(printDC, PHYSICALWIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
    setIntField(env, self, PAGEW_STR, pageWid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
    // height of page in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
    jint pageHgt = GetDeviceCaps(printDC, PHYSICALHEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
    setIntField(env, self, PAGEH_STR, pageHgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
    // x scaling factor of printer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
    jint xsf = GetDeviceCaps(printDC, SCALINGFACTORX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
    // x scaling factor of printer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
    jint ysf = GetDeviceCaps(printDC, SCALINGFACTORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
    if (getOrientationFromDevMode(env, self) == DMORIENT_LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
        // because we do our own rotation, we should force
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
        // orientation to portrait so we will get correct page dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
        HGLOBAL hDevMode = AwtPrintControl::getPrintHDMode(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
        if (hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
            DEVMODE *devmode = (DEVMODE*)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
            if (devmode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
                devmode->dmFields |= DM_ORIENTATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
                devmode->dmOrientation = DMORIENT_PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
                SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
                ::ResetDC(printDC, devmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
                RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
            GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
    // pixels per inch in x direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
    jint xRes = GetDeviceCaps(printDC, LOGPIXELSX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
    setIntField(env, self, XRES_STR, xRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
    // pixels per inch in y direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
    jint yRes = GetDeviceCaps(printDC, LOGPIXELSY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
    setIntField(env, self, YRES_STR, yRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
    // x coord of printable area in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
    jint xOrg = GetDeviceCaps(printDC, PHYSICALOFFSETX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
    setIntField(env, self, PHYSX_STR, xOrg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
    // y coord of printable area in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
    jint yOrg = GetDeviceCaps(printDC, PHYSICALOFFSETY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
    setIntField(env, self, PHYSY_STR, yOrg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
    // width of printable area in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
    jint printWid = GetDeviceCaps(printDC, HORZRES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
    setIntField(env, self, PHYSW_STR, printWid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
    // height of printable area in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
    jint printHgt = GetDeviceCaps(printDC, VERTRES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
    setIntField(env, self, PHYSH_STR, printHgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
static inline WORD getPrintPaperSize(JNIEnv *env, jobject self) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
    return (WORD)getIntField(env, self, PRINTPAPERSIZE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
static inline void setPrintPaperSize(JNIEnv *env, jobject self, WORD sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
    setIntField(env, self, PRINTPAPERSIZE_STR, (jint)sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
 *      Return the java int value of the field 'fieldName' in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
 *      java instance 'self'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
static jint getIntField(JNIEnv *env, jobject self, const char *fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
    jfieldID fieldId = getIdOfIntField(env, self, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
    return env->GetIntField(self, fieldId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
 *      Return the java long value of the field 'fieldName' in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
 *      java instance 'self'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
static jlong getLongField(JNIEnv *env, jobject self, const char *fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
    jfieldID fieldId = getIdOfLongField(env, self, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
    return env->GetLongField(self, fieldId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
 *      Set the int field named 'fieldName' of the java instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
 *      'self' to the value 'value'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
static void setIntField(JNIEnv *env, jobject self, const char *fieldName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                                                                jint value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
    jfieldID fieldId = getIdOfIntField(env, self, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
    env->SetIntField(self, fieldId, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
 *      Set the long field named 'fieldName' of the java instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
 *      'self' to the value 'value'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
static void setLongField(JNIEnv *env, jobject self, const char *fieldName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
                                                                jlong value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
    jfieldID fieldId = getIdOfLongField(env, self, fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
    env->SetLongField(self, fieldId, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
 *      Return the field id of the java instance 'self' of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
 *      java int field named 'fieldName'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
static jfieldID getIdOfIntField(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
                                                const char *fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
    jclass myClass = env->GetObjectClass(self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
    jfieldID fieldId = env->GetFieldID(myClass, fieldName, kJavaIntStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
    DASSERT(fieldId != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
    return fieldId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
 *      Return the field id of the java instance 'self' of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
 *      java long field named 'fieldName'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
static jfieldID getIdOfLongField(JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
                                                const char *fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
    jclass myClass = env->GetObjectClass(self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
    jfieldID fieldId = env->GetFieldID(myClass, fieldName, kJavaLongStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
    DASSERT(fieldId != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
    return fieldId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
static void setBooleanField(JNIEnv *env, jobject self, const char *fieldName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
                                                              jboolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
    jclass myClass = env->GetObjectClass(self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
    jfieldID fieldId = env->GetFieldID(myClass, fieldName, "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
    DASSERT(fieldId != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
    env->SetBooleanField(self, fieldId, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
 *  Throw a PrinterException with a string describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
 *  the Window's system error 'err'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
static void throwPrinterException(JNIEnv *env, DWORD err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
    char errStr[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
    TCHAR t_errStr[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
    jclass printerException = env->FindClass(PRINTEREXCEPTION_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
    errStr[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
                  NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
                  err,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
                  t_errStr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
                  sizeof(t_errStr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
                  NULL );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
    WideCharToMultiByte(CP_UTF8, 0, t_errStr, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
                        errStr, sizeof(errStr), NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
    env->ThrowNew(printerException, errStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
 * Finds the closest matching paper size for the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
 * Parameters are in 72ndths of an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
 * paperSize is the win32 integer identifier for a paper size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
 * Requires an initialised set of printer device structures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
 * Updates the printDC to specify the matched paper size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
 * If the passed in paper size is non-zero, its taken to be a windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
 * paper size "name", and we check that paper size against the paper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
 * we are matching and prefer that name over other names which also match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
 * the size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
static void matchPaperSize(HDC printDC, HGLOBAL hDevMode, HGLOBAL hDevNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
                           double origWid, double origHgt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
                           double* newWid, double *newHgt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
                           WORD* paperSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
    const double epsilon = 0.50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
    const double tolerance = (1.0 * 72.0);  // # inches * 72
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
    *newWid = origWid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
    *newHgt = origHgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
   /* 1st check if the DC/Devmode has as its current papersize a paper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
    * which matches the paper specified. If yes, then we can skip hunting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
    * for the match and in the process we avoid finding a "name" for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
    * the paper size which isn't the one the user specified in the page
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
    * setup dialog. For example "11x17" is also "Ledger".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
    if (printDC != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
      // pixels per inch in x and y direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
      jint xPixelRes = GetDeviceCaps(printDC, LOGPIXELSX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
      jint yPixelRes = GetDeviceCaps(printDC, LOGPIXELSY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
      // width and height of page in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
      jint pagePixelWid = GetDeviceCaps(printDC, PHYSICALWIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
      jint pagePixelHgt = GetDeviceCaps(printDC, PHYSICALHEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
      // page size in 1/72"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
      jdouble paperWidth = (jdouble)((pagePixelWid * 72)/(jdouble)xPixelRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
      jdouble paperHeight = (jdouble)((pagePixelHgt * 72)/(jdouble)yPixelRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
      if ((fabs(origWid - paperWidth) < epsilon) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
          (fabs(origHgt - paperHeight) < epsilon) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
          (*paperSize == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
        *newWid = origWid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
        *newHgt = origHgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
        if (hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
          DEVMODE *devmode = (DEVMODE *)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
          if (devmode != NULL && (devmode->dmFields & DM_PAPERSIZE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
            *paperSize = devmode->dmPaperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
          ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
    /* begin trying to match papers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
    LPTSTR printer = NULL, port = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
    if (hDevNames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
        DEVNAMES *devnames = (DEVNAMES *)::GlobalLock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
        if (devnames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
            LPTSTR lpdevnames = (LPTSTR)devnames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
            printer = _tcsdup(lpdevnames+devnames->wDeviceOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
            port = _tcsdup(lpdevnames+devnames->wOutputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
        ::GlobalUnlock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
    //REMIND: code duplicated in AwtPrintControl::getNearestMatchingPaper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
    int numPaperSizes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
    WORD *papers = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
    POINT *paperSizes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
    SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
    numPaperSizes = (int)DeviceCapabilities(printer, port, DC_PAPERSIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
                                            NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
    if (numPaperSizes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
        papers = (WORD*)safe_Malloc(sizeof(WORD) * numPaperSizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
        paperSizes = (POINT *)safe_Malloc(sizeof(*paperSizes) * numPaperSizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
        DWORD result1 = DeviceCapabilities(printer, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
                                           DC_PAPERS, (LPTSTR) papers, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
        DWORD result2 = DeviceCapabilities(printer, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
                                           DC_PAPERSIZE, (LPTSTR) paperSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
                                           NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
        if (result1 == -1 || result2 == -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
            free((char *) papers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
            papers = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
            free((char *) paperSizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
            paperSizes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
    RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
    double closestWid = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
    double closestHgt = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
    WORD   closestMatch = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
    if (paperSizes != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
        /* Paper sizes are in 0.1mm units. Convert to 1/72"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
         * For each paper size, compute the difference from the paper size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
         * passed in. Use a least-squares difference, so paper much different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
         * in x or y should score poorly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
        double diffw = origWid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
        double diffh = origHgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
        double least_square = diffw * diffw + diffh * diffh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
        double tmp_ls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
        double widpts, hgtpts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
        for (int i=0;i<numPaperSizes;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
            widpts = paperSizes[i].x * LOMETRIC_TO_POINTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
            hgtpts = paperSizes[i].y * LOMETRIC_TO_POINTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
            if ((fabs(origWid - widpts) < epsilon) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
                (fabs(origHgt - hgtpts) < epsilon)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
              if ((*paperSize == 0) || ((*paperSize !=0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
                                        (papers[i]==*paperSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
                closestWid = origWid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
                closestHgt = origHgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
                closestMatch = papers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
            diffw = fabs(widpts - origWid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
            diffh = fabs(hgtpts - origHgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
            tmp_ls = diffw * diffw + diffh * diffh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
            if ((diffw < tolerance) && (diffh < tolerance) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
                (tmp_ls < least_square)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
              least_square = tmp_ls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
              closestWid = widpts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
              closestHgt = hgtpts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
              closestMatch = papers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
    if (closestWid > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
        *newWid = closestWid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
    if (closestHgt > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
        *newHgt = closestHgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
    *paperSize = closestMatch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
    /* At this point we have the paper which is the closest match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
     * We now need to select the paper into the DEVMODE, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
     * get a DC which matches so we can get the margins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
    if ((printDC != NULL) && (hDevMode != NULL) && (closestMatch != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
        DEVMODE *devmode = (DEVMODE *)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
        if ((devmode != NULL) && (closestMatch != devmode->dmPaperSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
            devmode->dmFields |= DM_PAPERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
            devmode->dmPaperSize = closestMatch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
            ::ResetDC(printDC, devmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
            RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
        ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
    if (printer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
        free((char *)printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
    if (port != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
        free((char *)port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
    if (papers != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
        free((char *)papers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
    if (paperSizes != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
        free((char *)paperSizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
static BOOL SetPrinterDevice(LPTSTR pszDeviceName, HGLOBAL* p_hDevMode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
                             HGLOBAL* p_hDevNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
  // Open printer and obtain PRINTER_INFO_2 structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
  HANDLE hPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
  if (::OpenPrinter(pszDeviceName, &hPrinter, NULL) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
  DWORD dwBytesReturned, dwBytesNeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
  ::GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
  PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)::GlobalAlloc(GPTR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
                                                    dwBytesNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
  if (p2 == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
    ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
  if (::GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
                   &dwBytesReturned) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
    ::GlobalFree(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
    ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
  DEVMODE *pDevMode = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
  HGLOBAL  hDevMode = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
  /* If GetPrinter didn't fill in the DEVMODE, try to get it by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
     DocumentProperties...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
  if (p2->pDevMode == NULL){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
    SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
    LONG bytesNeeded = ::DocumentProperties(NULL, hPrinter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
                                          pszDeviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
                                          NULL, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
    RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
   if (bytesNeeded <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
      ::GlobalFree(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
      ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
    hDevMode = ::GlobalAlloc(GHND, bytesNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
    if (hDevMode == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
      ::GlobalFree(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
      ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
    pDevMode = (DEVMODE*)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
    if (pDevMode == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
      ::GlobalFree(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
      ::GlobalFree(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
      ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
    LONG lFlag = ::DocumentProperties(NULL, hPrinter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
                                    pszDeviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
                                    pDevMode, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
                                    DM_OUT_BUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
    RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
    if (lFlag != IDOK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
      ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
      ::GlobalFree(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
      ::GlobalFree(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
      ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
    // Allocate a global handle for DEVMODE and copy DEVMODE data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
    hDevMode = ::GlobalAlloc(GHND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
                             (sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
    if (hDevMode == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
      ::GlobalFree(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
      ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
    pDevMode = (DEVMODE*)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
    if (pDevMode == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
      ::GlobalFree(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
      ::GlobalFree(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
      ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
    memcpy(pDevMode, p2->pDevMode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
           sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
  ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
  ::ClosePrinter(hPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
  // Compute size of DEVNAMES structure you'll need.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
  // All sizes are WORD as in DEVNAMES structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
  // All offsets are in characters, not in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
  WORD drvNameLen = static_cast<WORD>(_tcslen(p2->pDriverName));  // driver name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
  WORD ptrNameLen = static_cast<WORD>(_tcslen(p2->pPrinterName)); // printer name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
  WORD porNameLen = static_cast<WORD>(_tcslen(p2->pPortName));    // port name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
  WORD devNameSize = static_cast<WORD>(sizeof(DEVNAMES)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
    (ptrNameLen + porNameLen + drvNameLen + 3)*sizeof(TCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
  // Allocate a global handle big enough to hold DEVNAMES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
  HGLOBAL   hDevNames = ::GlobalAlloc(GHND, devNameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
  DEVNAMES* pDevNames = (DEVNAMES*)::GlobalLock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
  // Copy the DEVNAMES information from PRINTER_INFO_2 structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
  pDevNames->wDriverOffset = sizeof(DEVNAMES)/sizeof(TCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
  memcpy((LPTSTR)pDevNames + pDevNames->wDriverOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
         p2->pDriverName, drvNameLen*sizeof(TCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
   pDevNames->wDeviceOffset = static_cast<WORD>(sizeof(DEVNAMES)/sizeof(TCHAR)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
   drvNameLen + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
   memcpy((LPTSTR)pDevNames + pDevNames->wDeviceOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
       p2->pPrinterName, ptrNameLen*sizeof(TCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
   pDevNames->wOutputOffset = static_cast<WORD>(sizeof(DEVNAMES)/sizeof(TCHAR)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
     drvNameLen + ptrNameLen + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
   memcpy((LPTSTR)pDevNames + pDevNames->wOutputOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
          p2->pPortName, porNameLen*sizeof(TCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
   pDevNames->wDefault = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
   ::GlobalUnlock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
   ::GlobalFree(p2);   // free PRINTER_INFO_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
   *p_hDevMode = hDevMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
   *p_hDevNames = hDevNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
   return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
Java_sun_awt_windows_WPrinterJob_setNativePrintService(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
                                                       jobject name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
                                                       jstring printer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
    LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
                                                            printer, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
    HDC hDC = AwtPrintControl::getPrintDC(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
    if (hDC != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
        DeletePrintDC(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
      hDC = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
    SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
    hDC = ::CreateDC(TEXT("WINSPOOL"), printerName, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
    RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
    if (hDC == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
        jclass printerException = env->FindClass(PRINTEREXCEPTION_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
        env->ThrowNew(printerException, "Invalid name of PrintService.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
        JNU_ReleaseStringPlatformChars(env, printer, printerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
    AwtPrintControl::setPrintDC(env, name, hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
    HANDLE hDevMode = AwtPrintControl::getPrintHDMode(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
    if (hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
      ::GlobalFree(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
      hDevMode = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
    HANDLE hDevNames = AwtPrintControl::getPrintHDName(env, name);;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
    if (hDevNames != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
      ::GlobalFree(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
      hDevNames = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
    SetPrinterDevice(printerName, &hDevMode, &hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
    AwtPrintControl::setPrintHDMode(env, name, hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
    AwtPrintControl::setPrintHDName(env, name, hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
    // Driver capability for copies & collation are not set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
    // when printDialog and getDefaultPrinterDC are not called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
    // set DRIVER_COPIES_STR and DRIVER_COLLATE_STR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
    DEVMODE *devmode = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
    if (hDevMode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
        devmode = (DEVMODE *)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
        DASSERT(!IsBadReadPtr(devmode, sizeof(DEVMODE)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
    if (devmode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
        if (devmode->dmFields & DM_COPIES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
          setBooleanField(env, name, DRIVER_COPIES_STR, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
        if (devmode->dmFields & DM_COLLATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
           setBooleanField(env, name, DRIVER_COLLATE_STR, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
        ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
    setCapabilities(env, name, hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
    JNU_ReleaseStringPlatformChars(env, printer, printerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
Java_sun_awt_windows_WPrinterJob_getNativePrintService(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
                                                       jobject name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
    jstring printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
    HANDLE hDevNames = AwtPrintControl::getPrintHDName(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
    if (hDevNames == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
    DEVNAMES* pDevNames = (DEVNAMES*)::GlobalLock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
    printer = JNU_NewStringPlatform(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
                                    (LPTSTR)pDevNames+pDevNames->wDeviceOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
    ::GlobalUnlock(hDevNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
    return printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
    CATCH_BAD_ALLOC_RET(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
static BOOL getPrintableArea(HDC pdc, HANDLE hDevMode, RectDouble *margin)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
    if (pdc == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
    DEVMODE *pDevMode = (DEVMODE*)::GlobalLock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
    if (pDevMode == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
    SAVE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
    ::ResetDC(pdc, pDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
    RESTORE_CONTROLWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
    int left = GetDeviceCaps(pdc, PHYSICALOFFSETX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
    int top = GetDeviceCaps(pdc, PHYSICALOFFSETY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
    int width = GetDeviceCaps(pdc, HORZRES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
    int height = GetDeviceCaps(pdc, VERTRES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
    int resx = GetDeviceCaps(pdc, LOGPIXELSX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
    int resy = GetDeviceCaps(pdc, LOGPIXELSY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
    margin->x = (jdouble)left/resx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
    margin->y =(jdouble)top/resy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
    margin->width = (jdouble)width/resx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
    margin->height = (jdouble)height/resy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
    ::GlobalUnlock(hDevMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
Java_sun_awt_windows_WPrinterJob_initIDs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
    TRY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
    AwtPrintDialog::controlID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
      env->GetFieldID(cls, "pjob", "Ljava/awt/print/PrinterJob;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
    jclass printDialogPeerClass = env->FindClass("Lsun/awt/windows/WPrintDialogPeer;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
    AwtPrintDialog::setHWndMID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
      env->GetMethodID(printDialogPeerClass, "setHWnd", "(J)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
    DASSERT(AwtPrintDialog::controlID != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
    DASSERT(AwtPrintDialog::setHWndMID != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
    AwtPrintControl::initIDs(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
    CATCH_BAD_ALLOC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
} /* extern "C" */