summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa
diff options
context:
space:
mode:
authorFelipe Heidrich <fheidric>2009-06-30 22:00:12 +0000
committerFelipe Heidrich <fheidric>2009-06-30 22:00:12 +0000
commitf664d297f7bb009784868bf3fcf0b3e3bb9a646b (patch)
tree54012fe4929893eef4891c88cbbf5841272ff433 /bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa
parentbc18a5e014088ce811f09c603b88361094486062 (diff)
downloadeclipse.platform.swt-f664d297f7bb009784868bf3fcf0b3e3bb9a646b.tar.gz
eclipse.platform.swt-f664d297f7bb009784868bf3fcf0b3e3bb9a646b.tar.xz
eclipse.platform.swt-f664d297f7bb009784868bf3fcf0b3e3bb9a646b.zip
*** empty log message ***
Diffstat (limited to 'bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa')
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/PrintDialog.java377
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/Printer.java613
2 files changed, 0 insertions, 990 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/PrintDialog.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/PrintDialog.java
deleted file mode 100755
index 5b3d03b038..0000000000
--- a/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/PrintDialog.java
+++ /dev/null
@@ -1,377 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2009 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.swt.printing;
-
-import org.eclipse.swt.*;
-import org.eclipse.swt.printing.PrinterData;
-import org.eclipse.swt.widgets.*;
-import org.eclipse.swt.internal.*;
-import org.eclipse.swt.internal.cocoa.*;
-
-/**
- * Instances of this class allow the user to select
- * a printer and various print-related parameters
- * prior to starting a print job.
- * <p>
- * IMPORTANT: This class is intended to be subclassed <em>only</em>
- * within the SWT 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">SWT Example: ControlExample, Dialog tab</a>
- * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
- * @noextend This class is not intended to be subclassed by clients.
- */
-public class PrintDialog extends Dialog {
- PrinterData printerData = new PrinterData();
- int returnCode;
-
- // the following Callbacks are never freed
- static Callback dialogCallback5;
- static final byte[] SWT_OBJECT = {'S', 'W', 'T', '_', 'O', 'B', 'J', 'E', 'C', 'T', '\0'};
-
-/**
- * Constructs a new instance of this class given only its parent.
- *
- * @param parent a composite control which will be the parent of the new instance (cannot be null)
- *
- * @exception IllegalArgumentException <ul>
- * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
- * </ul>
- * @exception SWTException <ul>
- * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
- * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
- * </ul>
- *
- * @see SWT
- * @see Widget#checkSubclass
- * @see Widget#getStyle
- */
-public PrintDialog (Shell parent) {
- this (parent, SWT.PRIMARY_MODAL);
-}
-
-/**
- * Constructs a new instance of this class given its parent
- * and a style value describing its behavior and appearance.
- * <p>
- * The style value is either one of the style constants defined in
- * class <code>SWT</code> which is applicable to instances of this
- * class, or must be built by <em>bitwise OR</em>'ing together
- * (that is, using the <code>int</code> "|" operator) two or more
- * of those <code>SWT</code> style constants. The class description
- * lists the style constants that are applicable to the class.
- * Style bits are also inherited from superclasses.
- * </p>
- *
- * @param parent a composite control which will be the parent of the new instance (cannot be null)
- * @param style the style of control to construct
- *
- * @exception IllegalArgumentException <ul>
- * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
- * </ul>
- * @exception SWTException <ul>
- * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
- * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
- * </ul>
- *
- * @see SWT
- * @see Widget#checkSubclass
- * @see Widget#getStyle
- */
-public PrintDialog (Shell parent, int style) {
- super (parent, checkStyle(parent, style));
- checkSubclass ();
-}
-
-static int checkStyle (Shell parent, int style) {
- int mask = SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL;
- if ((style & SWT.SHEET) != 0) {
- if (getSheetEnabled ()) {
- if (parent == null) {
- style &= ~SWT.SHEET;
- }
- } else {
- style &= ~SWT.SHEET;
- }
- if ((style & mask) == 0) {
- style |= parent == null ? SWT.APPLICATION_MODAL : SWT.PRIMARY_MODAL;
- }
- }
- return style;
-}
-
-/**
- * Sets the printer data that will be used when the dialog
- * is opened.
- * <p>
- * Setting the printer data to null is equivalent to
- * resetting all data fields to their default values.
- * </p>
- *
- * @param data the data that will be used when the dialog is opened or null to use default data
- *
- * @since 3.4
- */
-public void setPrinterData(PrinterData data) {
- this.printerData = data;
-}
-
-/**
- * Returns the printer data that will be used when the dialog
- * is opened.
- *
- * @return the data that will be used when the dialog is opened
- *
- * @since 3.4
- */
-public PrinterData getPrinterData() {
- return printerData;
-}
-
-/**
- * Makes the receiver visible and brings it to the front
- * of the display.
- *
- * @return a printer data object describing the desired print job parameters,
- * or null if the dialog was canceled, no printers were found, or an error occurred
- *
- * @exception SWTException <ul>
- * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
- * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
- * </ul>
- */
-public PrinterData open() {
- PrinterData data = null;
- NSPrintPanel panel = NSPrintPanel.printPanel();
- NSPrintInfo printInfo = new NSPrintInfo(NSPrintInfo.sharedPrintInfo().copy());
- printInfo.setOrientation(printerData.orientation == PrinterData.LANDSCAPE ? OS.NSLandscapeOrientation : OS.NSPortraitOrientation);
- NSMutableDictionary dict = printInfo.dictionary();
- dict.setValue(NSNumber.numberWithBool(printerData.collate), OS.NSPrintMustCollate);
- dict.setValue(NSNumber.numberWithInt(printerData.copyCount), OS.NSPrintCopies);
- if (printerData.printToFile) {
- dict.setValue(OS.NSPrintSaveJob, OS.NSPrintJobDisposition);
- }
- if (printerData.fileName != null && printerData.fileName.length() > 0) {
- dict.setValue(NSString.stringWith(printerData.fileName), OS.NSPrintSavePath);
- }
- dict.setValue(NSNumber.numberWithBool(printerData.scope == PrinterData.ALL_PAGES), OS.NSPrintAllPages);
- if (printerData.scope == PrinterData.PAGE_RANGE) {
- dict.setValue(NSNumber.numberWithInt(printerData.startPage), OS.NSPrintFirstPage);
- dict.setValue(NSNumber.numberWithInt(printerData.endPage), OS.NSPrintLastPage);
- }
- panel.setOptions(OS.NSPrintPanelShowsPageSetupAccessory | panel.options());
- int response;
- if ((getStyle () & SWT.SHEET) != 0) {
- initClasses();
- SWTPrintPanelDelegate delegate = (SWTPrintPanelDelegate)new SWTPrintPanelDelegate().alloc().init();
- int /*long*/ jniRef = OS.NewGlobalRef(this);
- if (jniRef == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- OS.object_setInstanceVariable(delegate.id, SWT_OBJECT, jniRef);
- returnCode = -1;
- Shell parent = getParent();
- panel.beginSheetWithPrintInfo(printInfo, parent.view.window(), delegate, OS.sel_panelDidEnd_returnCode_contextInfo_, 0);
- NSApplication application = NSApplication.sharedApplication();
- while (returnCode == -1) application.run();
- if (delegate != null) delegate.release();
- if (jniRef != 0) OS.DeleteGlobalRef(jniRef);
- response = returnCode;
- } else {
- response = (int)/*64*/panel.runModalWithPrintInfo(printInfo);
- }
- if (response != 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() != 0 ? PrinterData.ALL_PAGES : PrinterData.PAGE_RANGE;
- if (data.scope == 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() != 0;
- data.collate = false; //TODO: Only set to false if the printer does the collate internally (most printers do)
- data.copyCount = new NSNumber(dict.objectForKey(OS.NSPrintCopies)).intValue();
- data.copyCount = 1; //TODO: Only set to 1 if the printer does the copy internally (most printers do)
- data.orientation = printInfo.orientation() == OS.NSLandscapeOrientation ? PrinterData.LANDSCAPE : PrinterData.PORTRAIT;
- NSData nsData = NSKeyedArchiver.archivedDataWithRootObject(printInfo);
- data.otherData = new byte[(int)/*64*/nsData.length()];
- OS.memmove(data.otherData, nsData.bytes(), data.otherData.length);
- printerData = data;
- }
- printInfo.release();
- return data;
-}
-
-/**
- * Returns the print job scope that the user selected
- * before pressing OK in the dialog. This will be one
- * of the following values:
- * <dl>
- * <dt><code>PrinterData.ALL_PAGES</code></dt>
- * <dd>Print all pages in the current document</dd>
- * <dt><code>PrinterData.PAGE_RANGE</code></dt>
- * <dd>Print the range of pages specified by startPage and endPage</dd>
- * <dt><code>PrinterData.SELECTION</code></dt>
- * <dd>Print the current selection</dd>
- * </dl>
- *
- * @return the scope setting that the user selected
- */
-public int getScope() {
- return printerData.scope;
-}
-
-static boolean getSheetEnabled () {
- return !"false".equals(System.getProperty("org.eclipse.swt.sheet"));
-}
-
-static int /*long*/ dialogProc(int /*long*/ id, int /*long*/ sel, int /*long*/ arg0, int /*long*/ arg1, int /*long*/ arg2) {
- int /*long*/ [] jniRef = new int /*long*/ [1];
- OS.object_getInstanceVariable(id, SWT_OBJECT, jniRef);
- if (jniRef[0] == 0) return 0;
- if (sel == OS.sel_panelDidEnd_returnCode_contextInfo_) {
- PrintDialog dialog = (PrintDialog)OS.JNIGetObject(jniRef[0]);
- if (dialog == null) return 0;
- dialog.panelDidEnd_returnCode_contextInfo(id, sel, arg0, arg1, arg2);
- }
- return 0;
-}
-
-void initClasses () {
- String className = "SWTPrintPanelDelegate";
- if (OS.objc_lookUpClass (className) != 0) return;
-
- dialogCallback5 = new Callback(getClass(), "dialogProc", 5);
- int /*long*/ dialogProc5 = dialogCallback5.getAddress();
- if (dialogProc5 == 0) SWT.error (SWT.ERROR_NO_MORE_CALLBACKS);
-
- byte[] types = {'*','\0'};
- int size = C.PTR_SIZEOF, align = C.PTR_SIZEOF == 4 ? 2 : 3;
- int /*long*/ cls = OS.objc_allocateClassPair(OS.class_NSObject, className, 0);
- OS.class_addIvar(cls, SWT_OBJECT, size, (byte)align, types);
- OS.class_addMethod(cls, OS.sel_panelDidEnd_returnCode_contextInfo_, dialogProc5, "@:@i@");
- OS.objc_registerClassPair(cls);
-}
-
-void panelDidEnd_returnCode_contextInfo(int /*long*/ id, int /*long*/ sel, int /*long*/ alert, int /*long*/ returnCode, int /*long*/ contextInfo) {
- this.returnCode = (int)/*64*/returnCode;
- NSApplication application = NSApplication.sharedApplication();
- application.stop(null);
-}
-
-/**
- * Sets the scope of the print job. The user will see this
- * setting when the dialog is opened. This can have one of
- * the following values:
- * <dl>
- * <dt><code>PrinterData.ALL_PAGES</code></dt>
- * <dd>Print all pages in the current document</dd>
- * <dt><code>PrinterData.PAGE_RANGE</code></dt>
- * <dd>Print the range of pages specified by startPage and endPage</dd>
- * <dt><code>PrinterData.SELECTION</code></dt>
- * <dd>Print the current selection</dd>
- * </dl>
- *
- * @param scope the scope setting when the dialog is opened
- */
-public void setScope(int scope) {
- printerData.scope = scope;
-}
-
-/**
- * Returns the start page setting that the user selected
- * 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>PrinterData.PAGE_RANGE</code>.
- * </p>
- *
- * @return the start page setting that the user selected
- */
-public int getStartPage() {
- return printerData.startPage;
-}
-
-/**
- * Sets the start page that the user will see when the dialog
- * 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>PrinterData.PAGE_RANGE</code>.
- * </p>
- *
- * @param startPage the startPage setting when the dialog is opened
- */
-public void setStartPage(int startPage) {
- printerData.startPage = startPage;
-}
-
-/**
- * Returns the end page setting that the user selected
- * 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>PrinterData.PAGE_RANGE</code>.
- * </p>
- *
- * @return the end page setting that the user selected
- */
-public int getEndPage() {
- return printerData.endPage;
-}
-
-/**
- * Sets the end page that the user will see when the dialog
- * 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>PrinterData.PAGE_RANGE</code>.
- * </p>
- *
- * @param endPage the end page setting when the dialog is opened
- */
-public void setEndPage(int endPage) {
- printerData.endPage = endPage;
-}
-
-/**
- * Returns the 'Print to file' setting that the user selected
- * before pressing OK in the dialog.
- *
- * @return the 'Print to file' setting that the user selected
- */
-public boolean getPrintToFile() {
- return printerData.printToFile;
-}
-
-/**
- * Sets the 'Print to file' setting that the user will see
- * when the dialog is opened.
- *
- * @param printToFile the 'Print to file' setting when the dialog is opened
- */
-public void setPrintToFile(boolean printToFile) {
- printerData.printToFile = printToFile;
-}
-
-protected void checkSubclass() {
- String name = getClass().getName();
- String validName = PrintDialog.class.getName();
- if (!validName.equals(name)) {
- SWT.error(SWT.ERROR_INVALID_SUBCLASS);
- }
-}
-}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/Printer.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/Printer.java
deleted file mode 100755
index 91e63fdcc4..0000000000
--- a/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/Printer.java
+++ /dev/null
@@ -1,613 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2009 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.swt.printing;
-
-import org.eclipse.swt.*;
-import org.eclipse.swt.graphics.*;
-import org.eclipse.swt.internal.cocoa.*;
-
-/**
- * Instances of this class are used to print to a printer.
- * Applications create a GC on a printer using <code>new GC(printer)</code>
- * and then draw on the printer GC using the usual graphics calls.
- * <p>
- * A <code>Printer</code> object may be constructed by providing
- * a <code>PrinterData</code> object which identifies the printer.
- * A <code>PrintDialog</code> presents a print dialog to the user
- * and returns an initialized instance of <code>PrinterData</code>.
- * Alternatively, calling <code>new Printer()</code> will construct a
- * printer object for the user's default printer.
- * </p><p>
- * Application code must explicitly invoke the <code>Printer.dispose()</code>
- * method to release the operating system resources managed by each instance
- * when those instances are no longer required.
- * </p>
- *
- * @see PrinterData
- * @see PrintDialog
- * @see <a href="http://www.eclipse.org/swt/snippets/#printing">Printing snippets</a>
- * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
- */
-public final class Printer extends Device {
- PrinterData data;
- NSPrinter printer;
- NSPrintInfo printInfo;
- NSPrintOperation operation;
- NSView view;
- NSWindow window;
- boolean isGCCreated;
-
- static final String DRIVER = "Mac";
-
-/**
- * Returns an array of <code>PrinterData</code> objects
- * representing all available printers. If there are no
- * printers, the array will be empty.
- *
- * @return an array of PrinterData objects representing the available printers
- */
-public static PrinterData[] getPrinterList() {
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- NSArray printers = NSPrinter.printerNames();
- int count = (int)/*64*/printers.count();
- PrinterData[] result = new PrinterData[count];
- for (int i = 0; i < count; i++) {
- NSString str = new NSString(printers.objectAtIndex(i));
- result[i] = new PrinterData(DRIVER, str.getString());
- }
- return result;
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Returns a <code>PrinterData</code> object representing
- * the default printer or <code>null</code> if there is no
- * default printer.
- *
- * @return the default printer data or null
- *
- * @since 2.1
- */
-public static PrinterData getDefaultPrinterData() {
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- NSPrinter printer = NSPrintInfo.defaultPrinter();
- if (printer == null) return null;
- NSString str = printer.name();
- return new PrinterData(DRIVER, str.getString());
- } finally {
- if (pool != null) pool.release();
- }
-
-}
-
-/**
- * Constructs a new printer representing the default printer.
- * <p>
- * Note: You must dispose the printer when it is no longer required.
- * </p>
- *
- * @exception SWTError <ul>
- * <li>ERROR_NO_HANDLES - if there are no valid printers
- * </ul>
- *
- * @see Device#dispose
- */
-public Printer() {
- this(null);
-}
-
-/**
- * Constructs a new printer given a <code>PrinterData</code>
- * object representing the desired printer. If the argument
- * is null, then the default printer will be used.
- * <p>
- * Note: You must dispose the printer when it is no longer required.
- * </p>
- *
- * @param data the printer data for the specified printer, or null to use the default printer
- *
- * @exception IllegalArgumentException <ul>
- * <li>ERROR_INVALID_ARGUMENT - if the specified printer data does not represent a valid printer
- * </ul>
- * @exception SWTError <ul>
- * <li>ERROR_NO_HANDLES - if there are no valid printers
- * </ul>
- *
- * @see Device#dispose
- */
-public Printer(PrinterData data) {
- super (checkNull(data));
-}
-
-/**
- * Given a <em>client area</em> (as described by the arguments),
- * returns a rectangle, relative to the client area's coordinates,
- * that is the client area expanded by the printer's trim (or minimum margins).
- * <p>
- * Most printers have a minimum margin on each edge of the paper where the
- * printer device is unable to print. This margin is known as the "trim."
- * This method can be used to calculate the printer's minimum margins
- * by passing in a client area of 0, 0, 0, 0 and then using the resulting
- * x and y coordinates (which will be <= 0) to determine the minimum margins
- * for the top and left edges of the paper, and the resulting width and height
- * (offset by the resulting x and y) to determine the minimum margins for the
- * bottom and right edges of the paper, as follows:
- * <ul>
- * <li>The left trim width is -x pixels</li>
- * <li>The top trim height is -y pixels</li>
- * <li>The right trim width is (x + width) pixels</li>
- * <li>The bottom trim height is (y + height) pixels</li>
- * </ul>
- * </p>
- *
- * @param x the x coordinate of the client area
- * @param y the y coordinate of the client area
- * @param width the width of the client area
- * @param height the height of the client area
- * @return a rectangle, relative to the client area's coordinates, that is
- * the client area expanded by the printer's trim (or minimum margins)
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- *
- * @see #getBounds
- * @see #getClientArea
- */
-public Rectangle computeTrim(int x, int y, int width, int height) {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- NSSize paperSize = printInfo.paperSize();
- NSRect bounds = printInfo.imageablePageBounds();
- Point dpi = getDPI (), screenDPI = getIndependentDPI();
- float scaling = scalingFactor();
- x -= (bounds.x * dpi.x / screenDPI.x) / scaling;
- y -= (bounds.y * dpi.y / screenDPI.y) / scaling;
- width += ((paperSize.width - bounds.width) * dpi.x / screenDPI.x) / scaling;
- height += ((paperSize.height - bounds.height) * dpi.y / screenDPI.y) / scaling;
- return new Rectangle(x, y, width, height);
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Creates the printer handle.
- * This method is called internally by the instance creation
- * mechanism of the <code>Device</code> class.
- * @param deviceData the device data
- */
-protected void create(DeviceData deviceData) {
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- NSApplication.sharedApplication();
- data = (PrinterData)deviceData;
- if (data.otherData != null) {
- NSData nsData = NSData.dataWithBytes(data.otherData, data.otherData.length);
- printInfo = new NSPrintInfo(NSKeyedUnarchiver.unarchiveObjectWithData(nsData).id);
- } else {
- printInfo = NSPrintInfo.sharedPrintInfo();
- }
- printInfo.retain();
- printer = NSPrinter.printerWithName(NSString.stringWith(data.name));
- if (printer != null) {
- printer.retain();
- printInfo.setPrinter(printer);
- }
- printInfo.setOrientation(data.orientation == PrinterData.LANDSCAPE ? OS.NSLandscapeOrientation : OS.NSPortraitOrientation);
- NSMutableDictionary dict = printInfo.dictionary();
- if (data.collate != false) dict.setValue(NSNumber.numberWithBool(data.collate), OS.NSPrintMustCollate);
- if (data.copyCount != 1) dict.setValue(NSNumber.numberWithInt(data.copyCount), OS.NSPrintCopies);
- if (data.printToFile) {
- dict.setValue(OS.NSPrintSaveJob, OS.NSPrintJobDisposition);
- if (data.fileName != null) dict.setValue(NSString.stringWith(data.fileName), OS.NSPrintSavePath);
- }
- /*
- * Bug in Cocoa. For some reason, the output still goes to the printer when
- * the user chooses the preview button. The fix is to reset the job disposition.
- */
- NSString job = printInfo.jobDisposition();
- if (job.isEqual(new NSString(OS.NSPrintPreviewJob()))) {
- printInfo.setJobDisposition(job);
- }
- NSRect rect = new NSRect();
- window = (NSWindow)new NSWindow().alloc();
- window.initWithContentRect(rect, OS.NSBorderlessWindowMask, OS.NSBackingStoreBuffered, false);
- String className = "SWTPrinterView"; //$NON-NLS-1$
- if (OS.objc_lookUpClass(className) == 0) {
- int /*long*/ cls = OS.objc_allocateClassPair(OS.class_NSView, className, 0);
- OS.class_addMethod(cls, OS.sel_isFlipped, OS.isFlipped_CALLBACK(), "@:");
- OS.objc_registerClassPair(cls);
- }
- view = (NSView)new SWTPrinterView().alloc();
- view.initWithFrame(rect);
- window.setContentView(view);
- operation = NSPrintOperation.printOperationWithView(view, printInfo);
- operation.retain();
- operation.setShowsPrintPanel(false);
- operation.setShowsProgressPanel(false);
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Destroys the printer handle.
- * This method is called internally by the dispose
- * mechanism of the <code>Device</code> class.
- */
-protected void destroy() {
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- if (printer != null) printer.release();
- if (printInfo != null) printInfo.release();
- if (view != null) view.release();
- if (window != null) window.release();
- if (operation != null) operation.release();
- printer = null;
- printInfo = null;
- view = null;
- window = null;
- operation = null;
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Invokes platform specific functionality to allocate a new GC handle.
- * <p>
- * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
- * API for <code>Printer</code>. It is marked public only so that it
- * can be shared within the packages provided by SWT. It is not
- * available on all platforms, and should never be called from
- * application code.
- * </p>
- *
- * @param data the platform specific GC data
- * @return the platform specific GC handle
- */
-public int /*long*/ internal_new_GC(GCData data) {
- if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- if (data != null) {
- if (isGCCreated) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
- data.device = this;
- data.background = getSystemColor(SWT.COLOR_WHITE).handle;
- data.foreground = getSystemColor(SWT.COLOR_BLACK).handle;
- data.font = getSystemFont ();
- float scaling = scalingFactor();
- Point dpi = getDPI (), screenDPI = getIndependentDPI();
- NSSize size = printInfo.paperSize();
- size.width = (size.width * (dpi.x / screenDPI.x)) / scaling;
- size.height = (size.height * dpi.y / screenDPI.y) / scaling;
- data.size = size;
- isGCCreated = true;
- }
- return operation.context().id;
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-protected void init () {
- super.init();
-}
-
-/**
- * Invokes platform specific functionality to dispose a GC handle.
- * <p>
- * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
- * API for <code>Printer</code>. It is marked public only so that it
- * can be shared within the packages provided by SWT. It is not
- * available on all platforms, and should never be called from
- * application code.
- * </p>
- *
- * @param hDC the platform specific GC handle
- * @param data the platform specific GC data
- */
-public void internal_dispose_GC(int /*long*/ context, GCData data) {
- if (data != null) isGCCreated = false;
-}
-
-/**
- * Releases any internal state prior to destroying this printer.
- * This method is called internally by the dispose
- * mechanism of the <code>Device</code> class.
- */
-protected void release () {
- super.release();
-}
-
-float scalingFactor() {
- return new NSNumber(printInfo.dictionary().objectForKey(OS.NSPrintScalingFactor)).floatValue();
-}
-
-/**
- * Starts a print job and returns true if the job started successfully
- * and false otherwise.
- * <p>
- * This must be the first method called to initiate a print job,
- * followed by any number of startPage/endPage calls, followed by
- * endJob. Calling startPage, endPage, or endJob before startJob
- * will result in undefined behavior.
- * </p>
- *
- * @param jobName the name of the print job to start
- * @return true if the job started successfully and false otherwise.
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- *
- * @see #startPage
- * @see #endPage
- * @see #endJob
- */
-public boolean startJob(String jobName) {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- if (jobName != null && jobName.length() != 0) {
- operation.setJobTitle(NSString.stringWith(jobName));
- }
- printInfo.setUpPrintOperationDefaultValues();
- NSPrintOperation.setCurrentOperation(operation);
- NSGraphicsContext context = operation.createContext();
- if (context != null) {
- view.beginDocument();
- return true;
- }
- return false;
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Ends the current print job.
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- *
- * @see #startJob
- * @see #startPage
- * @see #endPage
- */
-public void endJob() {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- view.endDocument();
- operation.deliverResult();
- operation.destroyContext();
- operation.cleanUpOperation();
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Cancels a print job in progress.
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- */
-public void cancelJob() {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- operation.destroyContext();
- operation.cleanUpOperation();
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-static DeviceData checkNull (PrinterData data) {
- if (data == null) data = new PrinterData();
- if (data.driver == null || data.name == null) {
- PrinterData defaultPrinter = getDefaultPrinterData();
- if (defaultPrinter == null) SWT.error(SWT.ERROR_NO_HANDLES);
- data.driver = defaultPrinter.driver;
- data.name = defaultPrinter.name;
- }
- return data;
-}
-
-/**
- * Starts a page and returns true if the page started successfully
- * and false otherwise.
- * <p>
- * After calling startJob, this method may be called any number of times
- * along with a matching endPage.
- * </p>
- *
- * @return true if the page started successfully and false otherwise.
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- *
- * @see #endPage
- * @see #startJob
- * @see #endJob
- */
-public boolean startPage() {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- float scaling = scalingFactor();
- NSSize paperSize = printInfo.paperSize();
- paperSize.width /= scaling;
- paperSize.height /= scaling;
- NSRect rect = new NSRect();
- rect.width = paperSize.width;
- rect.height = paperSize.height;
- view.beginPageInRect(rect, new NSPoint());
- NSRect imageBounds = printInfo.imageablePageBounds();
- imageBounds.x /= scaling;
- imageBounds.y /= scaling;
- imageBounds.width /= scaling;
- imageBounds.height /= scaling;
- NSBezierPath.bezierPathWithRect(imageBounds).setClip();
- NSAffineTransform transform = NSAffineTransform.transform();
- transform.translateXBy(imageBounds.x, imageBounds.y);
- Point dpi = getDPI (), screenDPI = getIndependentDPI();
- transform.scaleXBy(screenDPI.x / (float)dpi.x, screenDPI.y / (float)dpi.y);
- transform.concat();
- operation.context().saveGraphicsState();
- return true;
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Ends the current page.
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- *
- * @see #startPage
- * @see #startJob
- * @see #endJob
- */
-public void endPage() {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- operation.context().restoreGraphicsState();
- view.endPage();
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Returns a point whose x coordinate is the horizontal
- * dots per inch of the printer, and whose y coordinate
- * is the vertical dots per inch of the printer.
- *
- * @return the horizontal and vertical DPI
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- */
-public Point getDPI() {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- //TODO get output resolution
- return getIndependentDPI();
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-Point getIndependentDPI() {
- return super.getDPI();
-}
-
-/**
- * Returns a rectangle describing the receiver's size and location.
- * <p>
- * For a printer, this is the size of the physical page, in pixels.
- * </p>
- *
- * @return the bounding rectangle
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- *
- * @see #getClientArea
- * @see #computeTrim
- */
-public Rectangle getBounds() {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- NSSize size = printInfo.paperSize();
- float scaling = scalingFactor();
- Point dpi = getDPI (), screenDPI = getIndependentDPI();
- return new Rectangle (0, 0, (int)((size.width * dpi.x / screenDPI.x) / scaling), (int)((size.height * dpi.y / screenDPI.y) / scaling));
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Returns a rectangle which describes the area of the
- * receiver which is capable of displaying data.
- * <p>
- * For a printer, this is the size of the printable area
- * of the page, in pixels.
- * </p>
- *
- * @return the client area
- *
- * @exception SWTException <ul>
- * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
- * </ul>
- *
- * @see #getBounds
- * @see #computeTrim
- */
-public Rectangle getClientArea() {
- checkDevice();
- NSAutoreleasePool pool = null;
- if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
- try {
- float scaling = scalingFactor();
- NSRect rect = printInfo.imageablePageBounds();
- Point dpi = getDPI (), screenDPI = getIndependentDPI();
- return new Rectangle(0, 0, (int)((rect.width * dpi.x / screenDPI.x) / scaling), (int)((rect.height * dpi.y / screenDPI.y) / scaling));
- } finally {
- if (pool != null) pool.release();
- }
-}
-
-/**
- * Returns a <code>PrinterData</code> object representing the
- * target printer for this print job.
- *
- * @return a PrinterData object describing the receiver
- */
-public PrinterData getPrinterData() {
- checkDevice();
- return data;
-}
-}