Mercurial > projects > dwt-mac
diff dwt/printing/PrintDialog.d @ 45:d8635bb48c7c
Merge with SWT 3.5
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 01 Dec 2008 17:07:00 +0100 |
parents | e831403a80a9 |
children | 10760eb00d08 |
line wrap: on
line diff
--- a/dwt/printing/PrintDialog.d Tue Oct 21 15:20:04 2008 +0200 +++ b/dwt/printing/PrintDialog.d Mon Dec 01 17:07:00 2008 +0100 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2007 IBM Corporation and others. + * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -14,8 +14,15 @@ import dwt.DWT; import dwt.DWTException; +import dwt.internal.cocoa.NSData; +import dwt.internal.cocoa.NSKeyedArchiver; +import dwt.internal.cocoa.NSMutableDictionary; +import dwt.internal.cocoa.NSNumber; import dwt.internal.cocoa.NSPrintInfo; import dwt.internal.cocoa.NSPrintPanel; +import dwt.internal.cocoa.NSPrinter; +import dwt.internal.cocoa.NSString; +import dwt.internal.cocoa.OS; import dwt.widgets.Dialog; import dwt.widgets.Shell; import dwt.widgets.Widget; @@ -28,8 +35,12 @@ * IMPORTANT: This class is intended to be subclassed <em>only</em> * within the DWT implementation. * </p> + * + * @see <a href="http://www.eclipse.org/swt/snippets/#printing">Printing snippets</a> + * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a> + * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> */ -public class PrintDialog : Dialog { +public class PrintDialog extends Dialog { PrinterData printerData; int scope = PrinterData.ALL_PAGES; int startPage = 1, endPage = 1; @@ -52,7 +63,7 @@ * @see Widget#checkSubclass * @see Widget#getStyle */ -public this (Shell parent) { +public PrintDialog (Shell parent) { this (parent, DWT.PRIMARY_MODAL); } @@ -84,7 +95,7 @@ * @see Widget#checkSubclass * @see Widget#getStyle */ -public this (Shell parent, int style) { +public PrintDialog (Shell parent, int style) { super (parent, style); checkSubclass (); } @@ -125,96 +136,47 @@ * </ul> */ public PrinterData open() { + PrinterData data = null; NSPrintPanel panel = NSPrintPanel.printPanel(); - NSPrintInfo printInfo =cast(NSPrintInfo)new NSPrintInfo().alloc(); - printInfo.initWithDictionary(null); - panel.runModalWithPrintInfo(printInfo); + NSPrintInfo printInfo = new NSPrintInfo(NSPrintInfo.sharedPrintInfo().copy()); + NSMutableDictionary dict = printInfo.dictionary(); + if (printToFile) { + dict.setValue(OS.NSPrintSaveJob, OS.NSPrintJobDisposition); + } + //TODO - setting range not work. why? + dict.setValue(NSNumber.numberWithBool(scope is PrinterData.ALL_PAGES), OS.NSPrintAllPages); + if (scope is PrinterData.PAGE_RANGE) { + dict.setValue(NSNumber.numberWithInt(startPage), OS.NSPrintFirstPage); + dict.setValue(NSNumber.numberWithInt(endPage), OS.NSPrintLastPage); + } + panel.setOptions(OS.NSPrintPanelShowsPageSetupAccessory | panel.options()); + if (panel.runModalWithPrintInfo(printInfo) !is OS.NSCancelButton) { + NSPrinter printer = printInfo.printer(); + NSString str = printer.name(); + data = new PrinterData(Printer.DRIVER, str.getString()); + data.printToFile = printInfo.jobDisposition().isEqual(OS.NSPrintSaveJob); + if (data.printToFile) { + NSString filename = new NSString(dict.objectForKey(OS.NSPrintSavePath)); + data.fileName = filename.getString(); + } + data.scope = new NSNumber(dict.objectForKey(OS.NSPrintAllPages)).intValue() !is 0 ? PrinterData.ALL_PAGES : PrinterData.PAGE_RANGE; + if (data.scope is PrinterData.PAGE_RANGE) { + data.startPage = new NSNumber(dict.objectForKey(OS.NSPrintFirstPage)).intValue(); + data.endPage = new NSNumber(dict.objectForKey(OS.NSPrintLastPage)).intValue(); + } + data.collate = new NSNumber(dict.objectForKey(OS.NSPrintMustCollate)).intValue() !is 0; + data.copyCount = new NSNumber(dict.objectForKey(OS.NSPrintCopies)).intValue(); + NSData nsData = NSKeyedArchiver.archivedDataWithRootObject(printInfo); + data.otherData = new byte[(int)/*64*/nsData.length()]; + OS.memmove(data.otherData, nsData.bytes(), data.otherData.length); + + printToFile = data.printToFile; + scope = data.scope; + startPage = data.startPage; + endPage = data.endPage; + } printInfo.release(); -// int[] buffer = new int[1]; -// if (OS.PMCreateSession(buffer) is OS.noErr) { -// int printSession = buffer[0]; -// if (OS.PMCreatePrintSettings(buffer) is OS.noErr) { -// int printSettings = buffer[0]; -// OS.PMSessionDefaultPrintSettings(printSession, printSettings); -// if (OS.PMCreatePageFormat(buffer) is OS.noErr) { -// int pageFormat = buffer[0]; -// OS.PMSessionDefaultPageFormat(printSession, pageFormat); -// OS.PMSessionSetDestination(printSession, printSettings, cast(short) (printToFile ? OS.kPMDestinationFile : OS.kPMDestinationPrinter), 0, 0); -// if (scope is PrinterData.PAGE_RANGE) { -// OS.PMSetFirstPage(printSettings, startPage, false); -// OS.PMSetLastPage(printSettings, endPage, false); -// OS.PMSetPageRange(printSettings, startPage, endPage); -// } else { -// OS.PMSetPageRange(printSettings, 1, OS.kPMPrintAllPages); -// } -// bool[] accepted = new bool [1]; -// OS.PMSessionPageSetupDialog(printSession, pageFormat, accepted); -// if (accepted[0]) { -// OS.PMSessionPrintDialog(printSession, printSettings, pageFormat, accepted); -// if (accepted[0]) { -// short[] destType = new short[1]; -// OS.PMSessionGetDestinationType(printSession, printSettings, destType); -// String name = Printer.getCurrentPrinterName(printSession); -// String driver = Printer.DRIVER; -// switch (destType[0]) { -// case OS.kPMDestinationFax: driver = Printer.FAX_DRIVER; break; -// case OS.kPMDestinationFile: driver = Printer.FILE_DRIVER; break; -// case OS.kPMDestinationPreview: driver = Printer.PREVIEW_DRIVER; break; -// case OS.kPMDestinationPrinter: driver = Printer.PRINTER_DRIVER; break; -// } -// PrinterData data = new PrinterData(driver, name); -// if (destType[0] is OS.kPMDestinationFile) { -// data.printToFile = true; -// OS.PMSessionCopyDestinationLocation(printSession, printSettings, buffer); -// int fileName = OS.CFURLCopyFileSystemPath(buffer[0],OS.kCFURLPOSIXPathStyle); -// OS.CFRelease(buffer[0]); -// data.fileName = Printer.getString(fileName); -// OS.CFRelease(fileName); -// } -// OS.PMGetCopies(printSettings, buffer); -// data.copyCount = buffer[0]; -// OS.PMGetFirstPage(printSettings, buffer); -// data.startPage = buffer[0]; -// OS.PMGetLastPage(printSettings, buffer); -// data.endPage = buffer[0]; -// OS.PMGetPageRange(printSettings, null, buffer); -// if (data.startPage is 1 && data.endPage is OS.kPMPrintAllPages) { -// data.scope = PrinterData.ALL_PAGES; -// } else { -// data.scope = PrinterData.PAGE_RANGE; -// } -// bool[] collate = new bool[1]; -// OS.PMGetCollate(printSettings, collate); -// data.collate = collate[0]; -// -// /* Serialize settings */ -// int[] flatSettings = new int[1]; -// OS.PMFlattenPrintSettings(printSettings, flatSettings); -// int[] flatFormat = new int[1]; -// OS.PMFlattenPageFormat(pageFormat, flatFormat); -// int settingsLength = OS.GetHandleSize (flatSettings[0]); -// int formatLength = OS.GetHandleSize (flatFormat[0]); -// byte[] otherData = data.otherData = new byte[settingsLength + formatLength + 8]; -// int offset = 0; -// offset = Printer.packData(flatSettings[0], otherData, offset); -// offset = Printer.packData(flatFormat[0], otherData, offset); -// OS.DisposeHandle(flatSettings[0]); -// OS.DisposeHandle(flatFormat[0]); -// -// scope = data.scope; -// startPage = data.startPage; -// endPage = data.endPage; -// printToFile = data.printToFile; -// return data; -// } -// } -// OS.PMRelease(pageFormat); -// } -// OS.PMRelease(printSettings); -// } -// OS.PMRelease(printSession); -// } - return null; + return data; } /** @@ -222,11 +184,11 @@ * before pressing OK in the dialog. This will be one * of the following values: * <dl> - * <dt><code>ALL_PAGES</code></dt> + * <dt><code>PrinterData.ALL_PAGES</code></dt> * <dd>Print all pages in the current document</dd> - * <dt><code>PAGE_RANGE</code></dt> + * <dt><code>PrinterData.PAGE_RANGE</code></dt> * <dd>Print the range of pages specified by startPage and endPage</dd> - * <dt><code>SELECTION</code></dt> + * <dt><code>PrinterData.SELECTION</code></dt> * <dd>Print the current selection</dd> * </dl> * @@ -241,11 +203,11 @@ * setting when the dialog is opened. This can have one of * the following values: * <dl> - * <dt><code>ALL_PAGES</code></dt> + * <dt><code>PrinterData.ALL_PAGES</code></dt> * <dd>Print all pages in the current document</dd> - * <dt><code>PAGE_RANGE</code></dt> + * <dt><code>PrinterData.PAGE_RANGE</code></dt> * <dd>Print the range of pages specified by startPage and endPage</dd> - * <dt><code>SELECTION</code></dt> + * <dt><code>PrinterData.SELECTION</code></dt> * <dd>Print the current selection</dd> * </dl> * @@ -260,7 +222,7 @@ * before pressing OK in the dialog. * <p> * This value can be from 1 to the maximum number of pages for the platform. - * Note that it is only valid if the scope is <code>PAGE_RANGE</code>. + * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>. * </p> * * @return the start page setting that the user selected @@ -274,7 +236,7 @@ * is opened. * <p> * This value can be from 1 to the maximum number of pages for the platform. - * Note that it is only valid if the scope is <code>PAGE_RANGE</code>. + * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>. * </p> * * @param startPage the startPage setting when the dialog is opened @@ -288,7 +250,7 @@ * before pressing OK in the dialog. * <p> * This value can be from 1 to the maximum number of pages for the platform. - * Note that it is only valid if the scope is <code>PAGE_RANGE</code>. + * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>. * </p> * * @return the end page setting that the user selected @@ -302,7 +264,7 @@ * is opened. * <p> * This value can be from 1 to the maximum number of pages for the platform. - * Note that it is only valid if the scope is <code>PAGE_RANGE</code>. + * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>. * </p> * * @param endPage the end page setting when the dialog is opened