Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/CPrinterJob.m
38829 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526#import "java_awt_print_PageFormat.h"27#import "java_awt_print_Pageable.h"28#import "sun_lwawt_macosx_CPrinterJob.h"29#import "sun_lwawt_macosx_CPrinterPageDialog.h"3031#import <Cocoa/Cocoa.h>32#import <JavaNativeFoundation/JavaNativeFoundation.h>3334#import "PrinterView.h"35#import "PrintModel.h"36#import "ThreadUtilities.h"37#import "GeomUtilities.h"3839static JNF_CLASS_CACHE(sjc_Paper, "java/awt/print/Paper");40static JNF_CLASS_CACHE(sjc_PageFormat, "java/awt/print/PageFormat");41static JNF_CLASS_CACHE(sjc_CPrinterJob, "sun/lwawt/macosx/CPrinterJob");42static JNF_CLASS_CACHE(sjc_CPrinterDialog, "sun/lwawt/macosx/CPrinterDialog");43static JNF_MEMBER_CACHE(sjm_getNSPrintInfo, sjc_CPrinterJob, "getNSPrintInfo", "()J");44static JNF_MEMBER_CACHE(sjm_printerJob, sjc_CPrinterDialog, "fPrinterJob", "Lsun/lwawt/macosx/CPrinterJob;");4546static NSPrintInfo* createDefaultNSPrintInfo();4748static void makeBestFit(NSPrintInfo* src);4950static void nsPrintInfoToJavaPaper(JNIEnv* env, NSPrintInfo* src, jobject dst);51static void javaPaperToNSPrintInfo(JNIEnv* env, jobject src, NSPrintInfo* dst);5253static void nsPrintInfoToJavaPageFormat(JNIEnv* env, NSPrintInfo* src, jobject dst);54static void javaPageFormatToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobject srcPageFormat, NSPrintInfo* dst);5556static void nsPrintInfoToJavaPrinterJob(JNIEnv* env, NSPrintInfo* src, jobject dstPrinterJob, jobject dstPageable);57static void javaPrinterJobToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobject srcPageable, NSPrintInfo* dst);585960static NSPrintInfo* createDefaultNSPrintInfo(JNIEnv* env, jstring printer)61{62NSPrintInfo* defaultPrintInfo = [[NSPrintInfo sharedPrintInfo] copy];63if (printer != NULL)64{65NSPrinter* nsPrinter = [NSPrinter printerWithName:JNFJavaToNSString(env, printer)];66if (nsPrinter != nil)67{68[defaultPrintInfo setPrinter:nsPrinter];69}70}71[defaultPrintInfo setUpPrintOperationDefaultValues];7273// cmc 05/18/04 radr://3160443 : setUpPrintOperationDefaultValues sets the74// page margins to 72, 72, 90, 90 - need to use [NSPrintInfo imageablePageBounds]75// to get values from the printer.76// NOTE: currently [NSPrintInfo imageablePageBounds] does not update itself when77// the user selects a different printer - see radr://3657453. However, rather than78// directly querying the PPD here, we'll let AppKit printing do the work. The AppKit79// printing bug above is set to be fixed for Tiger.80NSRect imageableRect = [defaultPrintInfo imageablePageBounds];81[defaultPrintInfo setLeftMargin: imageableRect.origin.x];82[defaultPrintInfo setBottomMargin: imageableRect.origin.y]; //top and bottom are flipped because [NSPrintInfo imageablePageBounds] returns a flipped NSRect (bottom-left to top-right).83[defaultPrintInfo setRightMargin: [defaultPrintInfo paperSize].width-imageableRect.origin.x-imageableRect.size.width];84[defaultPrintInfo setTopMargin: [defaultPrintInfo paperSize].height-imageableRect.origin.y-imageableRect.size.height];8586return defaultPrintInfo;87}8889static void makeBestFit(NSPrintInfo* src)90{91// This will look at the NSPrintInfo's margins. If they are out of bounds to the92// imageable area of the page, it will set them to the largest possible size.9394NSRect imageable = [src imageablePageBounds];9596NSSize paperSize = [src paperSize];9798CGFloat fullLeftM = imageable.origin.x;99CGFloat fullRightM = paperSize.width - (imageable.origin.x + imageable.size.width);100101// These are flipped because [NSPrintInfo imageablePageBounds] returns a flipped102// NSRect (bottom-left to top-right).103CGFloat fullTopM = paperSize.height - (imageable.origin.y + imageable.size.height);104CGFloat fullBottomM = imageable.origin.y;105106if (fullLeftM > [src leftMargin])107{108[src setLeftMargin:fullLeftM];109}110111if (fullRightM > [src rightMargin])112{113[src setRightMargin:fullRightM];114}115116if (fullTopM > [src topMargin])117{118[src setTopMargin:fullTopM];119}120121if (fullBottomM > [src bottomMargin])122{123[src setBottomMargin:fullBottomM];124}125}126127// In AppKit Printing, the rectangle is always oriented. In AppKit Printing, setting128// the rectangle will always set the orientation.129// In java printing, the rectangle is oriented if accessed from PageFormat. It is130// not oriented when accessed from Paper.131132static void nsPrintInfoToJavaPaper(JNIEnv* env, NSPrintInfo* src, jobject dst)133{134static JNF_MEMBER_CACHE(jm_setSize, sjc_Paper, "setSize", "(DD)V");135static JNF_MEMBER_CACHE(jm_setImageableArea, sjc_Paper, "setImageableArea", "(DDDD)V");136137jdouble jPaperW, jPaperH;138139// NSPrintInfo paperSize is oriented. java Paper is not oriented. Take140// the -[NSPrintInfo orientation] into account when setting the Paper141// rectangle.142143NSSize paperSize = [src paperSize];144switch ([src orientation]) {145case NSPortraitOrientation:146jPaperW = paperSize.width;147jPaperH = paperSize.height;148break;149150case NSLandscapeOrientation:151jPaperW = paperSize.height;152jPaperH = paperSize.width;153break;154155default:156jPaperW = paperSize.width;157jPaperH = paperSize.height;158break;159}160161JNFCallVoidMethod(env, dst, jm_setSize, jPaperW, jPaperH); // AWT_THREADING Safe (known object - always actual Paper)162163// Set the imageable area from the margins164CGFloat leftM = [src leftMargin];165CGFloat rightM = [src rightMargin];166CGFloat topM = [src topMargin];167CGFloat bottomM = [src bottomMargin];168169jdouble jImageX = leftM;170jdouble jImageY = topM;171jdouble jImageW = jPaperW - (leftM + rightM);172jdouble jImageH = jPaperH - (topM + bottomM);173174JNFCallVoidMethod(env, dst, jm_setImageableArea, jImageX, jImageY, jImageW, jImageH); // AWT_THREADING Safe (known object - always actual Paper)175}176177static void javaPaperToNSPrintInfo(JNIEnv* env, jobject src, NSPrintInfo* dst)178{179AWT_ASSERT_NOT_APPKIT_THREAD;180181static JNF_MEMBER_CACHE(jm_getWidth, sjc_Paper, "getWidth", "()D");182static JNF_MEMBER_CACHE(jm_getHeight, sjc_Paper, "getHeight", "()D");183static JNF_MEMBER_CACHE(jm_getImageableX, sjc_Paper, "getImageableX", "()D");184static JNF_MEMBER_CACHE(jm_getImageableY, sjc_Paper, "getImageableY", "()D");185static JNF_MEMBER_CACHE(jm_getImageableW, sjc_Paper, "getImageableWidth", "()D");186static JNF_MEMBER_CACHE(jm_getImageableH, sjc_Paper, "getImageableHeight", "()D");187188// java Paper is always Portrait oriented. Set NSPrintInfo with this189// rectangle, and it's orientation may change. If necessary, be sure to call190// -[NSPrintInfo setOrientation] after this call, which will then191// adjust the -[NSPrintInfo paperSize] as well.192193jdouble jPhysicalWidth = JNFCallDoubleMethod(env, src, jm_getWidth); // AWT_THREADING Safe (!appKit)194jdouble jPhysicalHeight = JNFCallDoubleMethod(env, src, jm_getHeight); // AWT_THREADING Safe (!appKit)195196[dst setPaperSize:NSMakeSize(jPhysicalWidth, jPhysicalHeight)];197198// Set the margins from the imageable area199jdouble jImageX = JNFCallDoubleMethod(env, src, jm_getImageableX); // AWT_THREADING Safe (!appKit)200jdouble jImageY = JNFCallDoubleMethod(env, src, jm_getImageableY); // AWT_THREADING Safe (!appKit)201jdouble jImageW = JNFCallDoubleMethod(env, src, jm_getImageableW); // AWT_THREADING Safe (!appKit)202jdouble jImageH = JNFCallDoubleMethod(env, src, jm_getImageableH); // AWT_THREADING Safe (!appKit)203204[dst setLeftMargin:(CGFloat)jImageX];205[dst setTopMargin:(CGFloat)jImageY];206[dst setRightMargin:(CGFloat)(jPhysicalWidth - jImageW - jImageX)];207[dst setBottomMargin:(CGFloat)(jPhysicalHeight - jImageH - jImageY)];208}209210static void nsPrintInfoToJavaPageFormat(JNIEnv* env, NSPrintInfo* src, jobject dst)211{212AWT_ASSERT_NOT_APPKIT_THREAD;213214static JNF_MEMBER_CACHE(jm_setOrientation, sjc_PageFormat, "setOrientation", "(I)V");215static JNF_MEMBER_CACHE(jm_setPaper, sjc_PageFormat, "setPaper", "(Ljava/awt/print/Paper;)V");216static JNF_CTOR_CACHE(jm_Paper_ctor, sjc_Paper, "()V");217218jint jOrientation;219NSPrintingOrientation nsOrientation = [src orientation];220switch (nsOrientation) {221case NSPortraitOrientation:222jOrientation = java_awt_print_PageFormat_PORTRAIT;223break;224225case NSLandscapeOrientation:226jOrientation = java_awt_print_PageFormat_LANDSCAPE; //+++gdb Are LANDSCAPE and REVERSE_LANDSCAPE still inverted?227break;228229/*230// AppKit printing doesn't support REVERSE_LANDSCAPE. Radar 2960295.231case NSReverseLandscapeOrientation:232jOrientation = java_awt_print_PageFormat.REVERSE_LANDSCAPE; //+++gdb Are LANDSCAPE and REVERSE_LANDSCAPE still inverted?233break;234*/235236default:237jOrientation = java_awt_print_PageFormat_PORTRAIT;238break;239}240241JNFCallVoidMethod(env, dst, jm_setOrientation, jOrientation); // AWT_THREADING Safe (!appKit)242243// Create a new Paper244jobject paper = JNFNewObject(env, jm_Paper_ctor); // AWT_THREADING Safe (known object)245246nsPrintInfoToJavaPaper(env, src, paper);247248// Set the Paper in the PageFormat249JNFCallVoidMethod(env, dst, jm_setPaper, paper); // AWT_THREADING Safe (!appKit)250251(*env)->DeleteLocalRef(env, paper);252}253254static void javaPageFormatToNSPrintInfo(JNIEnv* env, jobject srcPrintJob, jobject srcPageFormat, NSPrintInfo* dstPrintInfo)255{256AWT_ASSERT_NOT_APPKIT_THREAD;257258static JNF_MEMBER_CACHE(jm_getOrientation, sjc_PageFormat, "getOrientation", "()I");259static JNF_MEMBER_CACHE(jm_getPaper, sjc_PageFormat, "getPaper", "()Ljava/awt/print/Paper;");260static JNF_MEMBER_CACHE(jm_getPrinterName, sjc_CPrinterJob, "getPrinterName", "()Ljava/lang/String;");261262// When setting page information (orientation, size) in NSPrintInfo, set the263// rectangle first. This is because setting the orientation will change the264// rectangle to match.265266// Set up the paper. This will force Portrait since java Paper is267// not oriented. Then setting the NSPrintInfo orientation below268// will flip NSPrintInfo's info as necessary.269jobject paper = JNFCallObjectMethod(env, srcPageFormat, jm_getPaper); // AWT_THREADING Safe (!appKit)270javaPaperToNSPrintInfo(env, paper, dstPrintInfo);271(*env)->DeleteLocalRef(env, paper);272273switch (JNFCallIntMethod(env, srcPageFormat, jm_getOrientation)) { // AWT_THREADING Safe (!appKit)274case java_awt_print_PageFormat_PORTRAIT:275[dstPrintInfo setOrientation:NSPortraitOrientation];276break;277278case java_awt_print_PageFormat_LANDSCAPE:279[dstPrintInfo setOrientation:NSLandscapeOrientation]; //+++gdb Are LANDSCAPE and REVERSE_LANDSCAPE still inverted?280break;281282// AppKit printing doesn't support REVERSE_LANDSCAPE. Radar 2960295.283case java_awt_print_PageFormat_REVERSE_LANDSCAPE:284[dstPrintInfo setOrientation:NSLandscapeOrientation]; //+++gdb Are LANDSCAPE and REVERSE_LANDSCAPE still inverted?285break;286287default:288[dstPrintInfo setOrientation:NSPortraitOrientation];289break;290}291292// <rdar://problem/4022422> NSPrinterInfo is not correctly set to the selected printer293// from the Java side of CPrinterJob. Has always assumed the default printer was the one we wanted.294if (srcPrintJob == NULL) return;295jobject printerNameObj = JNFCallObjectMethod(env, srcPrintJob, jm_getPrinterName);296if (printerNameObj == NULL) return;297NSString *printerName = JNFJavaToNSString(env, printerNameObj);298if (printerName == nil) return;299NSPrinter *printer = [NSPrinter printerWithName:printerName];300if (printer == nil) return;301[dstPrintInfo setPrinter:printer];302}303304static void nsPrintInfoToJavaPrinterJob(JNIEnv* env, NSPrintInfo* src, jobject dstPrinterJob, jobject dstPageable)305{306static JNF_MEMBER_CACHE(jm_setService, sjc_CPrinterJob, "setPrinterServiceFromNative", "(Ljava/lang/String;)V");307static JNF_MEMBER_CACHE(jm_setCopies, sjc_CPrinterJob, "setCopies", "(I)V");308static JNF_MEMBER_CACHE(jm_setCollated, sjc_CPrinterJob, "setCollated", "(Z)V");309static JNF_MEMBER_CACHE(jm_setPageRange, sjc_CPrinterJob, "setPageRange", "(II)V");310311// get the selected printer's name, and set the appropriate PrintService on the Java side312NSString *name = [[src printer] name];313jstring printerName = JNFNSToJavaString(env, name);314JNFCallVoidMethod(env, dstPrinterJob, jm_setService, printerName);315316317NSMutableDictionary* printingDictionary = [src dictionary];318319NSNumber* nsCopies = [printingDictionary objectForKey:NSPrintCopies];320if ([nsCopies respondsToSelector:@selector(integerValue)])321{322JNFCallVoidMethod(env, dstPrinterJob, jm_setCopies, [nsCopies integerValue]); // AWT_THREADING Safe (known object)323}324325NSNumber* nsCollated = [printingDictionary objectForKey:NSPrintMustCollate];326if ([nsCollated respondsToSelector:@selector(boolValue)])327{328JNFCallVoidMethod(env, dstPrinterJob, jm_setCollated, [nsCollated boolValue] ? JNI_TRUE : JNI_FALSE); // AWT_THREADING Safe (known object)329}330331NSNumber* nsPrintAllPages = [printingDictionary objectForKey:NSPrintAllPages];332if ([nsPrintAllPages respondsToSelector:@selector(boolValue)])333{334jint jFirstPage = 0, jLastPage = java_awt_print_Pageable_UNKNOWN_NUMBER_OF_PAGES;335if (![nsPrintAllPages boolValue])336{337NSNumber* nsFirstPage = [printingDictionary objectForKey:NSPrintFirstPage];338if ([nsFirstPage respondsToSelector:@selector(integerValue)])339{340jFirstPage = [nsFirstPage integerValue] - 1;341}342343NSNumber* nsLastPage = [printingDictionary objectForKey:NSPrintLastPage];344if ([nsLastPage respondsToSelector:@selector(integerValue)])345{346jLastPage = [nsLastPage integerValue] - 1;347}348}349350JNFCallVoidMethod(env, dstPrinterJob, jm_setPageRange, jFirstPage, jLastPage); // AWT_THREADING Safe (known object)351}352}353354static void javaPrinterJobToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobject srcPageable, NSPrintInfo* dst)355{356AWT_ASSERT_NOT_APPKIT_THREAD;357358static JNF_CLASS_CACHE(jc_Pageable, "java/awt/print/Pageable");359static JNF_MEMBER_CACHE(jm_getCopies, sjc_CPrinterJob, "getCopiesInt", "()I");360static JNF_MEMBER_CACHE(jm_isCollated, sjc_CPrinterJob, "isCollated", "()Z");361static JNF_MEMBER_CACHE(jm_getFromPage, sjc_CPrinterJob, "getFromPageAttrib", "()I");362static JNF_MEMBER_CACHE(jm_getToPage, sjc_CPrinterJob, "getToPageAttrib", "()I");363static JNF_MEMBER_CACHE(jm_getMinPage, sjc_CPrinterJob, "getMinPageAttrib", "()I");364static JNF_MEMBER_CACHE(jm_getMaxPage, sjc_CPrinterJob, "getMaxPageAttrib", "()I");365static JNF_MEMBER_CACHE(jm_getSelectAttrib, sjc_CPrinterJob, "getSelectAttrib", "()I");366static JNF_MEMBER_CACHE(jm_getNumberOfPages, jc_Pageable, "getNumberOfPages", "()I");367static JNF_MEMBER_CACHE(jm_getPageFormat, sjc_CPrinterJob, "getPageFormatFromAttributes", "()Ljava/awt/print/PageFormat;");368369NSMutableDictionary* printingDictionary = [dst dictionary];370371jint copies = JNFCallIntMethod(env, srcPrinterJob, jm_getCopies); // AWT_THREADING Safe (known object)372[printingDictionary setObject:[NSNumber numberWithInteger:copies] forKey:NSPrintCopies];373374jboolean collated = JNFCallBooleanMethod(env, srcPrinterJob, jm_isCollated); // AWT_THREADING Safe (known object)375[printingDictionary setObject:[NSNumber numberWithBool:collated ? YES : NO] forKey:NSPrintMustCollate];376jint selectID = JNFCallIntMethod(env, srcPrinterJob, jm_getSelectAttrib);377jint fromPage = JNFCallIntMethod(env, srcPrinterJob, jm_getFromPage);378jint toPage = JNFCallIntMethod(env, srcPrinterJob, jm_getToPage);379if (selectID ==0) {380[printingDictionary setObject:[NSNumber numberWithBool:YES] forKey:NSPrintAllPages];381} else if (selectID == 2) {382// In Mac 10.7, Print ALL is deselected if PrintSelection is YES whether383// NSPrintAllPages is YES or NO384[printingDictionary setObject:[NSNumber numberWithBool:NO] forKey:NSPrintAllPages];385[printingDictionary setObject:[NSNumber numberWithBool:YES] forKey:NSPrintSelectionOnly];386} else {387jint minPage = JNFCallIntMethod(env, srcPrinterJob, jm_getMinPage);388jint maxPage = JNFCallIntMethod(env, srcPrinterJob, jm_getMaxPage);389390// for PD_SELECTION or PD_NOSELECTION, check from/to page391// to determine which radio button to select392if (fromPage > minPage || toPage < maxPage) {393[printingDictionary setObject:[NSNumber numberWithBool:NO] forKey:NSPrintAllPages];394} else {395[printingDictionary setObject:[NSNumber numberWithBool:YES] forKey:NSPrintAllPages];396}397}398399// setting fromPage and toPage will not be shown in the dialog if printing All pages400[printingDictionary setObject:[NSNumber numberWithInteger:fromPage] forKey:NSPrintFirstPage];401[printingDictionary setObject:[NSNumber numberWithInteger:toPage] forKey:NSPrintLastPage];402403jobject page = JNFCallObjectMethod(env, srcPrinterJob, jm_getPageFormat);404if (page != NULL) {405javaPageFormatToNSPrintInfo(env, NULL, page, dst);406}407}408409/*410* Class: sun_lwawt_macosx_CPrinterJob411* Method: abortDoc412* Signature: ()V413*/414JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterJob_abortDoc415(JNIEnv *env, jobject jthis)416{417JNF_COCOA_ENTER(env);418// This is only called during the printLoop from the printLoop thread419NSPrintOperation* printLoop = [NSPrintOperation currentOperation];420NSPrintInfo* printInfo = [printLoop printInfo];421[printInfo setJobDisposition:NSPrintCancelJob];422JNF_COCOA_EXIT(env);423}424425/*426* Class: sun_lwawt_macosx_CPrinterJob427* Method: getDefaultPage428* Signature: (Ljava/awt/print/PageFormat;)V429*/430JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterJob_getDefaultPage431(JNIEnv *env, jobject jthis, jobject page)432{433JNF_COCOA_ENTER(env);434NSPrintInfo* printInfo = createDefaultNSPrintInfo(env, NULL);435436nsPrintInfoToJavaPageFormat(env, printInfo, page);437438[printInfo release];439JNF_COCOA_EXIT(env);440}441442/*443* Class: sun_lwawt_macosx_CPrinterJob444* Method: validatePaper445* Signature: (Ljava/awt/print/Paper;Ljava/awt/print/Paper;)V446*/447JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterJob_validatePaper448(JNIEnv *env, jobject jthis, jobject origpaper, jobject newpaper)449{450JNF_COCOA_ENTER(env);451452NSPrintInfo* printInfo = createDefaultNSPrintInfo(env, NULL);453javaPaperToNSPrintInfo(env, origpaper, printInfo);454makeBestFit(printInfo);455nsPrintInfoToJavaPaper(env, printInfo, newpaper);456[printInfo release];457458JNF_COCOA_EXIT(env);459}460461/*462* Class: sun_lwawt_macosx_CPrinterJob463* Method: createNSPrintInfo464* Signature: ()J465*/466JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CPrinterJob_createNSPrintInfo467(JNIEnv *env, jobject jthis)468{469jlong result = -1;470JNF_COCOA_ENTER(env);471// This is used to create the NSPrintInfo for this PrinterJob. Thread472// safety is assured by the java side of this call.473474NSPrintInfo* printInfo = createDefaultNSPrintInfo(env, NULL);475476result = ptr_to_jlong(printInfo);477478JNF_COCOA_EXIT(env);479return result;480}481482/*483* Class: sun_lwawt_macosx_CPrinterJob484* Method: dispose485* Signature: (J)V486*/487JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterJob_dispose488(JNIEnv *env, jobject jthis, jlong nsPrintInfo)489{490JNF_COCOA_ENTER(env);491if (nsPrintInfo != -1)492{493NSPrintInfo* printInfo = (NSPrintInfo*)jlong_to_ptr(nsPrintInfo);494[printInfo release];495}496JNF_COCOA_EXIT(env);497}498499500/*501* Class: sun_lwawt_macosx_CPrinterJob502* Method: printLoop503* Signature: ()V504*/505JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CPrinterJob_printLoop506(JNIEnv *env, jobject jthis, jboolean blocks, jint firstPage, jint lastPage)507{508AWT_ASSERT_NOT_APPKIT_THREAD;509510static JNF_MEMBER_CACHE(jm_getPageFormat, sjc_CPrinterJob, "getPageFormat", "(I)Ljava/awt/print/PageFormat;");511static JNF_MEMBER_CACHE(jm_getPageFormatArea, sjc_CPrinterJob, "getPageFormatArea", "(Ljava/awt/print/PageFormat;)Ljava/awt/geom/Rectangle2D;");512static JNF_MEMBER_CACHE(jm_getPrinterName, sjc_CPrinterJob, "getPrinterName", "()Ljava/lang/String;");513static JNF_MEMBER_CACHE(jm_getPageable, sjc_CPrinterJob, "getPageable", "()Ljava/awt/print/Pageable;");514515jboolean retVal = JNI_FALSE;516517JNF_COCOA_ENTER(env);518// Get the first page's PageFormat for setting things up (This introduces519// and is a facet of the same problem in Radar 2818593/2708932).520jobject page = JNFCallObjectMethod(env, jthis, jm_getPageFormat, 0); // AWT_THREADING Safe (!appKit)521if (page != NULL) {522jobject pageFormatArea = JNFCallObjectMethod(env, jthis, jm_getPageFormatArea, page); // AWT_THREADING Safe (!appKit)523524PrinterView* printerView = [[PrinterView alloc] initWithFrame:JavaToNSRect(env, pageFormatArea) withEnv:env withPrinterJob:jthis];525[printerView setFirstPage:firstPage lastPage:lastPage];526527NSPrintInfo* printInfo = (NSPrintInfo*)jlong_to_ptr(JNFCallLongMethod(env, jthis, sjm_getNSPrintInfo)); // AWT_THREADING Safe (known object)528529// <rdar://problem/4156975> passing jthis CPrinterJob as well, so we can extract the printer name from the current job530javaPageFormatToNSPrintInfo(env, jthis, page, printInfo);531532// <rdar://problem/4093799> NSPrinterInfo is not correctly set to the selected printer533// from the Java side of CPrinterJob. Had always assumed the default printer was the one we wanted.534jobject printerNameObj = JNFCallObjectMethod(env, jthis, jm_getPrinterName);535if (printerNameObj != NULL) {536NSString *printerName = JNFJavaToNSString(env, printerNameObj);537if (printerName != nil) {538NSPrinter *printer = [NSPrinter printerWithName:printerName];539if (printer != nil) [printInfo setPrinter:printer];540}541}542543// <rdar://problem/4367998> JTable.print attributes are ignored544jobject pageable = JNFCallObjectMethod(env, jthis, jm_getPageable); // AWT_THREADING Safe (!appKit)545javaPrinterJobToNSPrintInfo(env, jthis, pageable, printInfo);546547PrintModel* printModel = [[PrintModel alloc] initWithPrintInfo:printInfo];548549(void)[printModel runPrintLoopWithView:printerView waitUntilDone:blocks withEnv:env];550551// Only set this if we got far enough to call runPrintLoopWithView, or we will spin CPrinterJob.print() forever!552retVal = JNI_TRUE;553554[printModel release];555[printerView release];556557if (page != NULL)558{559(*env)->DeleteLocalRef(env, page);560}561562if (pageFormatArea != NULL)563{564(*env)->DeleteLocalRef(env, pageFormatArea);565}566}567JNF_COCOA_EXIT(env);568return retVal;569}570571/*572* Class: sun_lwawt_macosx_CPrinterPageDialog573* Method: showDialog574* Signature: ()Z575*/576JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CPrinterPageDialog_showDialog577(JNIEnv *env, jobject jthis)578{579580static JNF_CLASS_CACHE(jc_CPrinterPageDialog, "sun/lwawt/macosx/CPrinterPageDialog");581static JNF_MEMBER_CACHE(jm_page, jc_CPrinterPageDialog, "fPage", "Ljava/awt/print/PageFormat;");582583jboolean result = JNI_FALSE;584JNF_COCOA_ENTER(env);585jobject printerJob = JNFGetObjectField(env, jthis, sjm_printerJob);586NSPrintInfo* printInfo = (NSPrintInfo*)jlong_to_ptr(JNFCallLongMethod(env, printerJob, sjm_getNSPrintInfo)); // AWT_THREADING Safe (known object)587588jobject page = JNFGetObjectField(env, jthis, jm_page);589590// <rdar://problem/4156975> passing NULL, because only a CPrinterJob has a real printer associated with it591javaPageFormatToNSPrintInfo(env, NULL, page, printInfo);592593PrintModel* printModel = [[PrintModel alloc] initWithPrintInfo:printInfo];594result = [printModel runPageSetup];595[printModel release];596597if (result)598{599nsPrintInfoToJavaPageFormat(env, printInfo, page);600}601602if (printerJob != NULL)603{604(*env)->DeleteLocalRef(env, printerJob);605}606607if (page != NULL)608{609(*env)->DeleteLocalRef(env, page);610}611612JNF_COCOA_EXIT(env);613return result;614}615616/*617* Class: sun_lwawt_macosx_CPrinterJobDialog618* Method: showDialog619* Signature: ()Z620*/621JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CPrinterJobDialog_showDialog622(JNIEnv *env, jobject jthis)623{624static JNF_CLASS_CACHE(jc_CPrinterJobDialog, "sun/lwawt/macosx/CPrinterJobDialog");625static JNF_MEMBER_CACHE(jm_pageable, jc_CPrinterJobDialog, "fPageable", "Ljava/awt/print/Pageable;");626627jboolean result = JNI_FALSE;628JNF_COCOA_ENTER(env);629jobject printerJob = JNFGetObjectField(env, jthis, sjm_printerJob);630NSPrintInfo* printInfo = (NSPrintInfo*)jlong_to_ptr(JNFCallLongMethod(env, printerJob, sjm_getNSPrintInfo)); // AWT_THREADING Safe (known object)631632jobject pageable = JNFGetObjectField(env, jthis, jm_pageable);633634javaPrinterJobToNSPrintInfo(env, printerJob, pageable, printInfo);635636PrintModel* printModel = [[PrintModel alloc] initWithPrintInfo:printInfo];637result = [printModel runJobSetup];638[printModel release];639640if (result)641{642nsPrintInfoToJavaPrinterJob(env, printInfo, printerJob, pageable);643}644645if (printerJob != NULL)646{647(*env)->DeleteLocalRef(env, printerJob);648}649650if (pageable != NULL)651{652(*env)->DeleteLocalRef(env, pageable);653}654655JNF_COCOA_EXIT(env);656return result;657}658659660