Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'bundles/org.eclipse.swt/Eclipse SWT Printing')
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Printing/common/org/eclipse/swt/printing/PrinterData.java158
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Printing/common/org/eclipse/swt/printing/package.html13
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Printing/motif/org/eclipse/swt/printing/PrintDialog.java51
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Printing/motif/org/eclipse/swt/printing/Printer.java261
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/PrintDialog.java254
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/Printer.java499
6 files changed, 1236 insertions, 0 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/common/org/eclipse/swt/printing/PrinterData.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/common/org/eclipse/swt/printing/PrinterData.java
new file mode 100755
index 0000000000..95c9419789
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/common/org/eclipse/swt/printing/PrinterData.java
@@ -0,0 +1,158 @@
+package org.eclipse.swt.printing;
+
+import org.eclipse.swt.graphics.*;
+
+/**
+ * Instances of this class are descriptions of a print job
+ * in terms of the printer, and the scope and type of printing
+ * that is desired. For example, the number of pages and copies
+ * can be specified, as well as whether or not the print job
+ * should go to a file.
+ * <p>
+ * Application code does <em>not</em> need to explicitly release the
+ * resources managed by each instance when those instances are no longer
+ * required, and thus no <code>dispose()</code> method is provided.
+ * </p>
+ *
+ * @see Printer
+ * @see Printer#getPrinterList
+ * @see PrintDialog#open
+ */
+
+public final class PrinterData extends DeviceData {
+
+ /**
+ * the printer driver
+ * On Windows systems, this is the name of the driver (often "winspool").
+ * On X/Window systems, this is the name of a display connection to the
+ * Xprt server (the default is ":1").
+ */
+ public String driver;
+
+ /**
+ * the name of the printer
+ * On Windows systems, this is the name of the 'device'.
+ * On X/Window systems, this is the printer's 'name'.
+ */
+ public String name;
+
+ /**
+ * the scope of the print job, expressed as one of the following values:
+ * <dl>
+ * <dt><code>ALL_PAGES</code></dt>
+ * <dd>Print all pages in the current document</dd>
+ * <dt><code>PAGE_RANGE</code></dt>
+ * <dd>Print the range of pages specified by startPage and endPage</dd>
+ * <dt><code>SELECTION</code></dt>
+ * <dd>Print the current selection</dd>
+ * </dl>
+ */
+ public int scope = ALL_PAGES;
+
+ /**
+ * the start page of a page range, used when scope is PAGE_RANGE
+ */
+ public int startPage = 0;
+
+ /**
+ * the end page of a page range, used when scope is PAGE_RANGE
+ */
+ public int endPage = 0;
+
+ /**
+ * whether or not the print job should go to a file
+ */
+ public boolean printToFile = false;
+
+ /**
+ * the name of the file to print to if printToFile is true.
+ * Note that this field is ignored if printToFile is false.
+ */
+ public String fileName;
+
+ /**
+ * the number of copies to print.
+ * Note that this field may be controlled by the printer driver
+ * In other words, the printer itself may be capable of printing
+ * multiple copies, and if so, the value of this field will always be 1.
+ */
+ public int copyCount = 1;
+
+ /**
+ * whether or not the printer should collate the printed paper
+ * Note that this field may be controlled by the printer driver.
+ * In other words, the printer itself may be capable of doing the
+ * collation, and if so, the value of this field will always be false.
+ */
+ public boolean collate = false;
+
+ /**
+ * <code>scope</code> field value indicating that
+ * all pages should be printed
+ */
+ public static final int ALL_PAGES = 0;
+
+ /**
+ * <code>scope</code> field value indicating that
+ * the range of pages specified by startPage and endPage
+ * should be printed
+ */
+ public static final int PAGE_RANGE = 1;
+
+ /**
+ * <code>scope</code> field value indicating that
+ * the current selection should be printed
+ */
+ public static final int SELECTION = 2;
+
+ /**
+ * private, platform-specific data
+ * On Windows, this contains a copy of the DEVMODE struct
+ * returned from the <code>PrintDialog</code>.
+ * This field is not currently used on the X/Window System.
+ */
+ byte [] otherData;
+
+ /**
+ * Constructs an instance of this class that can be
+ * used to print to the default printer.
+ *
+ * @exception IllegalArgumentException <ul>
+ * <li>ERROR_UNSPECIFIED - if there are no valid printers
+ * </ul>
+ */
+ public PrinterData() {
+ PrinterData data = Printer.getDefaultPrinterData();
+ this.driver = data.driver;
+ this.name = data.name;
+ }
+
+ /**
+ * Constructs an instance of this class with the given
+ * printer driver and printer name.
+ *
+ * @param driver the printer driver for the printer
+ * @param name the name of the printer
+ *
+ * @exception IllegalArgumentException <ul>
+ * <li>ERROR_UNSPECIFIED - if there are no valid printers
+ * </ul>
+ *
+ * @see #driver
+ * @see #name
+ */
+ public PrinterData(String driver, String name) {
+ this.driver = driver;
+ this.name = name;
+ }
+
+ /**
+ * Returns a string containing a concise, human-readable
+ * description of the receiver.
+ *
+ * @return a string representation of the receiver
+ */
+ public String toString() {
+ return "PrinterData {" + "driver = " + driver + ", name = " + name + "}";
+ }
+} \ No newline at end of file
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/common/org/eclipse/swt/printing/package.html b/bundles/org.eclipse.swt/Eclipse SWT Printing/common/org/eclipse/swt/printing/package.html
new file mode 100755
index 0000000000..52216655f0
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/common/org/eclipse/swt/printing/package.html
@@ -0,0 +1,13 @@
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+ <meta name="Author" content="IBM">
+ <title>Package-level Javadoc</title>
+</head>
+<body>
+Brief overview will be provided here.
+<h2>
+Package Specification</h2>
+Detailed overview will be provided here.
+</body>
+</html>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/motif/org/eclipse/swt/printing/PrintDialog.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/motif/org/eclipse/swt/printing/PrintDialog.java
new file mode 100755
index 0000000000..0defeee996
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/motif/org/eclipse/swt/printing/PrintDialog.java
@@ -0,0 +1,51 @@
+package org.eclipse.swt.printing;
+
+import org.eclipse.swt.*;
+import org.eclipse.swt.graphics.*;
+import org.eclipse.swt.widgets.*;
+import org.eclipse.swt.internal.motif.*;
+
+public class PrintDialog extends Dialog {
+ int scope = PrinterData.ALL_PAGES;
+ int startPage = -1, endPage = -1;
+ boolean printToFile = false;
+
+public PrintDialog (Shell parent) {
+ this (parent, SWT.PRIMARY_MODAL);
+}
+public PrintDialog (Shell parent, int style) {
+ super (parent, style);
+}
+public PrinterData open() {
+ /* Return the first printer in the list */
+ PrinterData[] printers = Printer.getPrinterList();
+ if (printers.length > 0) return printers[0];
+ return null;
+}
+public int getScope() {
+ return scope;
+}
+public void setScope(int scope) {
+ this.scope = scope;
+}
+public int getStartPage() {
+ return startPage;
+}
+public void setStartPage(int startPage) {
+ this.startPage = startPage;
+}
+public int getEndPage() {
+ return endPage;
+}
+public void setEndPage(int endPage) {
+ this.endPage = endPage;
+}
+public boolean getPrintToFile() {
+ return printToFile;
+}
+public void setPrintToFile(boolean printToFile) {
+ this.printToFile = printToFile;
+}
+protected void checkSubclass() {
+}
+}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/motif/org/eclipse/swt/printing/Printer.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/motif/org/eclipse/swt/printing/Printer.java
new file mode 100755
index 0000000000..d5b306051a
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/motif/org/eclipse/swt/printing/Printer.java
@@ -0,0 +1,261 @@
+package org.eclipse.swt.printing;
+
+import org.eclipse.swt.*;
+import org.eclipse.swt.graphics.*;
+import org.eclipse.swt.internal.*;
+import org.eclipse.swt.internal.motif.*;
+
+public final class Printer extends Device {
+ PrinterData data;
+ int printContext, xScreen, xDrawable;
+ int defaultFontList;
+
+public static PrinterData[] getPrinterList() {
+ SWT.error(SWT.ERROR_NOT_IMPLEMENTED);
+ /* Connect to the default X print server */
+ //byte [] buffer = Converter.wcsToMbcs(null, XDefaultPrintServer, true);
+ //int pdpy = OS.XOpenDisplay(buffer);
+ int pdpy = xPrinter;
+ if (pdpy == 0) {
+ /* no print server */
+ SWT.error(SWT.ERROR_IO);
+ }
+
+ /* Get the list of printers */
+ int [] listCount = new int[1];
+ int plist = OS.XpGetPrinterList(pdpy, null, listCount);
+ int printerCount = listCount[0];
+ if (plist == 0 || printerCount == 0) {
+ /* no printers */
+ //OS.XCloseDisplay(pdpy);
+ SWT.error(SWT.ERROR_IO);
+ }
+
+ /* Copy the printer names into PrinterData objects */
+ int [] stringPointers = new int [printerCount * 2];
+ OS.memmove(stringPointers, plist, printerCount * 2 * 4);
+ PrinterData printerList[] = new PrinterData[printerCount];
+ for (int i = 0; i < printerCount; i++) {
+ String name = "";
+ int address = stringPointers[i * 2];
+ if (address != 0) {
+ int length = OS.strlen(address);
+ byte[] buffer = new byte [length];
+ OS.memmove(buffer, address, length);
+ name = new String(Converter.mbcsToWcs(null, buffer));
+ }
+ printerList[i] = new PrinterData(Device.XDefaultPrintServer, name);
+ }
+ OS.XpFreePrinterList(plist);
+ //OS.XCloseDisplay(pdpy);
+ return printerList;
+}
+
+static PrinterData getDefaultPrinterData() {
+ /* Use the first printer in the list as the default */
+ PrinterData[] list = getPrinterList();
+ if (list.length == 0) {
+ /* no printers */
+ SWT.error(SWT.ERROR_NO_HANDLES);
+ }
+ return list[0];
+}
+
+public Printer() {
+ this(getDefaultPrinterData());
+}
+
+public Printer(PrinterData data) {
+ super(data);
+}
+
+protected void create(DeviceData deviceData) {
+ SWT.error(SWT.ERROR_NOT_IMPLEMENTED);
+ data = (PrinterData)deviceData;
+
+ /* Open the display for the X print server */
+ //byte[] displayName = Converter.wcsToMbcs(null, data.driver, true);
+ //xDisplay = OS.XOpenDisplay(displayName);
+ xDisplay = xPrinter;
+ if (xDisplay == 0) {
+ /* no print server */
+ SWT.error(SWT.ERROR_NO_HANDLES);
+ }
+}
+
+protected void init() {
+ super.init();
+
+ /* Create the printContext for the printer */
+ byte[] name = Converter.wcsToMbcs(null, data.name, true);
+ printContext = OS.XpCreateContext(xDisplay, name);
+ if (printContext == OS.None) {
+ /* can't create print context */
+ //OS.XCloseDisplay(xDisplay);
+ SWT.error(SWT.ERROR_NO_HANDLES);
+ }
+
+ /* Set the printContext into the display */
+ OS.XpSetContext(xDisplay, printContext);
+
+ /* Get the printer's screen */
+ xScreen = OS.XpGetScreenOfContext(xDisplay, printContext);
+
+ /* Initialize Motif */
+ int widgetClass = OS.TopLevelShellWidgetClass();
+ int shellHandle = OS.XtAppCreateShell(null, null, widgetClass, xDisplay, null, 0);
+ OS.XtDestroyWidget(shellHandle);
+
+ /* Initialize the default font */
+ byte [] buffer = Converter.wcsToMbcs(null, "-*-courier-medium-r-*-*-*-120-*-*-*-*-*-*", true);
+ int fontListEntry = OS.XmFontListEntryLoad(xDisplay, buffer, 0, OS.XmFONTLIST_DEFAULT_TAG);
+ if (fontListEntry == 0) SWT.error(SWT.ERROR_NO_HANDLES);
+ defaultFontList = OS.XmFontListAppendEntry(0, fontListEntry);
+ OS.XmFontListEntryFree(new int[]{fontListEntry});
+}
+
+protected void destroy() {
+ //if (xDisplay != 0) OS.XCloseDisplay(xDisplay);
+}
+
+public int internal_new_GC(GCData data) {
+ if (xDrawable == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
+ int xGC = OS.XCreateGC(xDisplay, xDrawable, 0, null);
+ if (xGC == 0) SWT.error(SWT.ERROR_NO_HANDLES);
+ if (data != null) {
+ data.device = this;
+ data.display = xDisplay;
+ data.drawable = xDrawable; // not valid until after startJob
+ data.fontList = defaultFontList;
+ data.colormap = OS.XDefaultColormapOfScreen(xScreen);
+ }
+ return xGC;
+}
+
+public void internal_dispose_GC(int xGC, GCData data) {
+ OS.XFreeGC(xDisplay, xGC);
+}
+
+public boolean startJob(String jobName) {
+ checkDevice();
+ byte [] buffer = Converter.wcsToMbcs(null, "*job-name: " + jobName, true);
+ OS.XpSetAttributes(xDisplay, printContext, OS.XPJobAttr, buffer, OS.XPAttrMerge);
+ OS.XpStartJob(xDisplay, OS.XPSpool);
+
+ /* Create the xDrawable */
+ XRectangle rect = new XRectangle();
+ short [] width = new short [1];
+ short [] height = new short [1];
+ OS.XpGetPageDimensions(xDisplay, printContext, width, height, rect);
+ xDrawable = OS.XCreateWindow(xDisplay, OS.XRootWindowOfScreen(xScreen),
+ 0, 0, rect.width, rect.height, 0,
+ OS.CopyFromParent, OS.CopyFromParent, OS.CopyFromParent, 0, 0);
+ return true;
+}
+
+public void endJob() {
+ checkDevice();
+ OS.XpEndJob(xDisplay);
+ OS.XFlush(xDisplay);
+}
+
+public void cancelJob() {
+ checkDevice();
+ OS.XpCancelJob(xDisplay, true);
+}
+
+public boolean startPage() {
+ checkDevice();
+ OS.XpStartPage(xDisplay, xDrawable);
+ return true;
+}
+
+public void endPage() {
+ checkDevice();
+ OS.XpEndPage(xDisplay);
+}
+
+public Point getDPI() {
+ checkDevice();
+ byte [] buffer = Converter.wcsToMbcs(null, "default-printer-resolution", true);
+ int pool = OS.XpGetOneAttribute(xDisplay, printContext, OS.XPDocAttr, buffer);
+ int length = OS.strlen(pool);
+ buffer = new byte[length];
+ OS.memmove(buffer, pool, length);
+ OS.XtFree(pool);
+ String resolution = new String(buffer, 0, buffer.length);
+ int res = 300; // default
+ if (resolution.length() == 0) {
+ /* If we can't get the info from the DocAttrs, ask the printer. */
+ buffer = Converter.wcsToMbcs(null, "printer-resolutions-supported", true);
+ pool = OS.XpGetOneAttribute(xDisplay, printContext, OS.XPPrinterAttr, buffer);
+ length = OS.strlen(pool);
+ buffer = new byte[length];
+ OS.memmove(buffer, pool, length);
+ OS.XtFree(pool);
+ int n = 0;
+ while (!Character.isWhitespace((char)buffer[n]) && n < buffer.length) n++;
+ resolution = new String(buffer, 0, n);
+ }
+ if (resolution.length() != 0) {
+ try {
+ res = Integer.parseInt(resolution);
+ } catch (NumberFormatException ex) {}
+ }
+ return new Point(res, res);
+}
+
+public Rectangle getBounds() {
+ checkDevice();
+ XRectangle rect = new XRectangle();
+ short [] width = new short [1];
+ short [] height = new short [1];
+ OS.XpGetPageDimensions(xDisplay, printContext, width, height, rect);
+ return new Rectangle(0, 0, width[0], height[0]);
+}
+
+public Rectangle getClientArea() {
+ checkDevice();
+ XRectangle rect = new XRectangle();
+ OS.XpGetPageDimensions(xDisplay, printContext, new short [1], new short [1], rect);
+ return new Rectangle(0, 0, rect.width, rect.height);
+}
+
+public Rectangle computeTrim(int x, int y, int width, int height) {
+ checkDevice();
+ XRectangle rect = new XRectangle();
+ short [] paperWidth = new short [1];
+ short [] paperHeight = new short [1];
+ OS.XpGetPageDimensions(xDisplay, printContext, paperWidth, paperHeight, rect);
+ int hTrim = paperWidth[0] - rect.width;
+ int vTrim = paperHeight[0] - rect.height;
+ return new Rectangle(x - rect.x, y - rect.y, width + hTrim, height + vTrim);
+}
+
+public PrinterData getPrinterData() {
+ return data;
+}
+
+protected void checkDevice() {
+ if (xDisplay == 0) SWT.error(SWT.ERROR_DEVICE_DISPOSED);
+}
+
+protected void release() {
+ super.release();
+ if (defaultFontList != 0) {
+ OS.XmFontListFree(defaultFontList);
+ defaultFontList = 0;
+ }
+ if (printContext != 0) {
+ OS.XpDestroyContext(xDisplay, printContext);
+ printContext = 0;
+ }
+ if (xDrawable != 0) {
+ OS.XDestroyWindow(xDisplay, xDrawable);
+ xDrawable = 0;
+ }
+ xScreen = 0;
+ data = null;
+}
+
+}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/PrintDialog.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/PrintDialog.java
new file mode 100755
index 0000000000..b85b43d811
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/PrintDialog.java
@@ -0,0 +1,254 @@
+package org.eclipse.swt.printing;
+
+import org.eclipse.swt.*;
+import org.eclipse.swt.graphics.*;
+import org.eclipse.swt.widgets.*;
+import org.eclipse.swt.internal.win32.*;
+
+/**
+ * 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>
+ */
+
+public class PrintDialog extends Dialog {
+ int scope = PrinterData.ALL_PAGES;
+ int startPage = -1, endPage = -1;
+ boolean printToFile = false;
+
+/**
+ * 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
+ * for all SWT widget classes should include a comment which
+ * describes the style constants which are applicable to the class.
+ * </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, style);
+}
+
+/**
+ * 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>ALL_PAGES</code></dt>
+ * <dd>Print all pages in the current document</dd>
+ * <dt><code>PAGE_RANGE</code></dt>
+ * <dd>Print the range of pages specified by startPage and endPage</dd>
+ * <dt><code>SELECTION</code></dt>
+ * <dd>Print the current selection</dd>
+ * </dl>
+ *
+ * @return the scope setting that the user selected
+ */
+public int getScope() {
+ return scope;
+}
+
+/**
+ * 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>ALL_PAGES</code></dt>
+ * <dd>Print all pages in the current document</dd>
+ * <dt><code>PAGE_RANGE</code></dt>
+ * <dd>Print the range of pages specified by startPage and endPage</dd>
+ * <dt><code>SELECTION</code></dt>
+ * <dd>Print the current selection</dd>
+ * </dl>
+ *
+ * @param int the scope setting when the dialog is opened
+ */
+public void setScope(int scope) {
+ this.scope = scope;
+}
+
+/**
+ * Returns the start page setting that the user selected
+ * before pressing OK in the dialog.
+ * <p>
+ * Note that this value is only valid if the scope is <code>PAGE_RANGE</code>.
+ * </p>
+ *
+ * @return the start page setting that the user selected
+ */
+public int getStartPage() {
+ return startPage;
+}
+
+/**
+ * Sets the start page that the user will see when the dialog
+ * is opened.
+ *
+ * @param int the startPage setting when the dialog is opened
+ */
+public void setStartPage(int startPage) {
+ this.startPage = startPage;
+}
+
+/**
+ * Returns the end page setting that the user selected
+ * before pressing OK in the dialog.
+ * <p>
+ * Note that this value is only valid if the scope is <code>PAGE_RANGE</code>.
+ * </p>
+ *
+ * @return the end page setting that the user selected
+ */
+public int getEndPage() {
+ return endPage;
+}
+
+/**
+ * Sets the end page that the user will see when the dialog
+ * is opened.
+ *
+ * @param int the end page setting when the dialog is opened
+ */
+public void setEndPage(int endPage) {
+ this.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 printToFile;
+}
+
+/**
+ * Sets the 'Print to file' setting that the user will see
+ * when the dialog is opened.
+ *
+ * @param boolean the 'Print to file' setting when the dialog is opened
+ */
+public void setPrintToFile(boolean printToFile) {
+ this.printToFile = printToFile;
+}
+
+protected void checkSubclass() {
+}
+
+/**
+ * Makes the receiver visible and brings it to the front
+ * of the display.
+ *
+ * @return a printer data object describing the desired print job parameters
+ *
+ * @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() {
+ PRINTDLG pd = new PRINTDLG();
+ pd.lStructSize = PRINTDLG.sizeof;
+ pd.Flags = OS.PD_USEDEVMODECOPIESANDCOLLATE;
+ if (printToFile) pd.Flags |= OS.PD_PRINTTOFILE;
+ switch (scope) {
+ case PrinterData.PAGE_RANGE: pd.Flags |= OS.PD_PAGENUMS; break;
+ case PrinterData.SELECTION: pd.Flags |= OS.PD_SELECTION; break;
+ default: pd.Flags |= OS.PD_ALLPAGES;
+ }
+ pd.nMinPage = 1;
+ pd.nMaxPage = -1;
+ pd.nFromPage = (short) startPage;
+ pd.nToPage = (short) endPage;
+ if (OS.PrintDlg(pd)) {
+ /* Get driver and device from the DEVNAMES struct */
+ int ptr = OS.GlobalLock(pd.hDevNames);
+ int size = OS.GlobalSize(ptr);
+ byte [] DEVNAMES = new byte[size];
+ OS.MoveMemory(DEVNAMES, ptr, size);
+ OS.GlobalUnlock(ptr);
+ int driverOffset = (DEVNAMES[0] & 0xFF) | ((DEVNAMES[1] & 0xFF) << 8);
+ int i = 0;
+ while (driverOffset + i < size) {
+ if (DEVNAMES[driverOffset + i] == 0) break;
+ else i++;
+ }
+ String driver = new String(DEVNAMES, driverOffset, i);
+ int deviceOffset = (DEVNAMES[2] & 0xFF) | ((DEVNAMES[3] & 0xFF) << 8);
+ i = 0;
+ while (deviceOffset + i < size) {
+ if (DEVNAMES[deviceOffset + i] == 0) break;
+ else i++;
+ }
+ String device = new String(DEVNAMES, deviceOffset, i);
+
+ /* Create PrinterData object and set fields from PRINTDLG */
+ PrinterData data = new PrinterData(driver, device);
+ if ((pd.Flags & OS.PD_PAGENUMS) != 0) {
+ data.scope = PrinterData.PAGE_RANGE;
+ data.startPage = pd.nFromPage;
+ data.endPage = pd.nToPage;
+ } else if ((pd.Flags & OS.PD_SELECTION) != 0) {
+ data.scope = PrinterData.SELECTION;
+ }
+ data.printToFile = (pd.Flags & OS.PD_PRINTTOFILE) != 0;
+ data.copyCount = pd.nCopies;
+ data.collate = (pd.Flags & OS.PD_COLLATE) != 0;
+
+ /* Bulk-save the printer-specific settings in the DEVMODE struct */
+ ptr = OS.GlobalLock(pd.hDevMode);
+ size = OS.GlobalSize(ptr);
+ data.otherData = new byte[size];
+ OS.MoveMemory(data.otherData, ptr, size);
+ OS.GlobalUnlock(ptr);
+
+ return data;
+ }
+ return null;
+}
+} \ No newline at end of file
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/Printer.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/Printer.java
new file mode 100755
index 0000000000..0d6f3eadc7
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/Printer.java
@@ -0,0 +1,499 @@
+package org.eclipse.swt.printing;
+
+import org.eclipse.swt.*;
+import org.eclipse.swt.graphics.*;
+import org.eclipse.swt.internal.*;
+import org.eclipse.swt.internal.win32.*;
+
+/**
+ * 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
+ */
+public final class Printer extends Device {
+ /**
+ * the handle to the printer DC
+ * (Warning: This field is platform dependent)
+ */
+ public int handle;
+
+ /**
+ * the printer data describing this printer
+ */
+ PrinterData data;
+
+ /**
+ * whether or not a GC was created for this printer
+ */
+ boolean isGCCreated = false;
+
+ /**
+ * strings used to access the Windows registry
+ * (Warning: These fields are platform dependent)
+ */
+ static byte[] profile;
+ static byte[] appName;
+ static byte[] keyName;
+ static {
+ profile = Converter.wcsToMbcs(0, "PrinterPorts", true);
+ appName = Converter.wcsToMbcs(0, "windows", true);
+ keyName = Converter.wcsToMbcs(0, "device", true);
+ }
+
+/**
+ * Returns an array of <code>PrinterData</code> objects
+ * representing all available printers.
+ *
+ * @exception IllegalArgumentException <ul>
+ * <li>ERROR_UNSPECIFIED - if there are no valid printers
+ * </ul>
+ *
+ * @return the list of available printers
+ */
+public static PrinterData[] getPrinterList() {
+ byte[] buf = new byte[1024];
+ int n = OS.GetProfileString(profile, null, new byte[] {0}, buf, buf.length);
+ if (n == 0) SWT.error(SWT.ERROR_UNSPECIFIED);
+ byte[][] deviceNames = new byte[5][];
+ int nameCount = 0;
+ int index = 0;
+ for (int i = 0; i < n; i++) {
+ if (buf[i] == 0) {
+ if (nameCount == deviceNames.length) {
+ byte[][] newNames = new byte[deviceNames.length + 5][];
+ System.arraycopy(deviceNames, 0, newNames, 0, deviceNames.length);
+ deviceNames = newNames;
+ }
+ deviceNames[nameCount] = new byte[i - index + 1];
+ System.arraycopy(buf, index, deviceNames[nameCount], 0, i - index);
+ nameCount++;
+ index = i + 1;
+ }
+ }
+ PrinterData printerList[] = new PrinterData[nameCount];
+ for (int p = 0; p < nameCount; p++) {
+ String device = new String(deviceNames[p], 0, deviceNames[p].length - 1);
+ String driver = "";
+ if (OS.GetProfileString(profile, deviceNames[p], new byte [] {0}, buf, buf.length) > 0) {
+ int commaIndex = 0;
+ while (buf[commaIndex] != ',' && commaIndex < buf.length) commaIndex++;
+ if (commaIndex < buf.length) {
+ byte[] driverName = new byte[commaIndex + 1];
+ System.arraycopy(buf, 0, driverName, 0, commaIndex);
+ driver = new String(driverName, 0, driverName.length - 1);
+ }
+ }
+ printerList[p] = new PrinterData(driver, device);
+ }
+ return printerList;
+}
+
+/**
+ * Returns a <code>PrinterData</code> object representing
+ * the default printer.
+ *
+ * @exception IllegalArgumentException <ul>
+ * <li>ERROR_UNSPECIFIED - if there are no valid printers
+ * </ul>
+ *
+ * @return the default printer
+ */
+static PrinterData getDefaultPrinterData() {
+ byte [] deviceName = null;
+ byte[] buf = new byte[1024];
+ int n = OS.GetProfileString(appName, keyName, new byte[] {0}, buf, buf.length);
+ if (n == 0) SWT.error(SWT.ERROR_UNSPECIFIED);
+ int commaIndex = 0;
+ while(buf[commaIndex] != ',' && commaIndex < buf.length) commaIndex++;
+ if (commaIndex < buf.length) {
+ deviceName = new byte[commaIndex + 1];
+ System.arraycopy(buf, 0, deviceName, 0, commaIndex);
+ }
+ String device = new String(deviceName, 0, deviceName.length - 1);
+ String driver = "";
+ if (OS.GetProfileString(profile, deviceName, new byte [] {0}, buf, buf.length) > 0) {
+ commaIndex = 0;
+ while (buf[commaIndex] != ',' && commaIndex < buf.length) commaIndex++;
+ if (commaIndex < buf.length) {
+ byte[] driverName = new byte[commaIndex + 1];
+ System.arraycopy(buf, 0, driverName, 0, commaIndex);
+ driver = new String(driverName, 0, driverName.length - 1);
+ }
+ }
+ return new PrinterData(driver, device);
+}
+
+/**
+ * Constructs a new printer representing the default printer.
+ * <p>
+ * You must dispose the printer when it is no longer required.
+ * </p>
+ *
+ * @exception IllegalArgumentException <ul>
+ * <li>ERROR_UNSPECIFIED - if there are no valid printers
+ * </ul>
+ *
+ * @see #dispose
+ */
+public Printer() {
+ this(getDefaultPrinterData());
+}
+
+/**
+ * Constructs a new printer given a <code>PrinterData</code>
+ * object representing the desired printer.
+ * <p>
+ * You must dispose the printer when it is no longer required.
+ * </p>
+ *
+ * @param data the printer data for the specified printer
+ *
+ * @exception IllegalArgumentException <ul>
+ * <li>ERROR_INVALID_ARGUMENT - if the specified printer data does not represent a valid printer
+ * <li>ERROR_UNSPECIFIED - if there are no valid printers
+ * </ul>
+ *
+ * @see #dispose
+ */
+public Printer(PrinterData data) {
+ super(data);
+}
+
+/**
+ * Creates the printer handle.
+ * This method is called internally by the instance creation
+ * mechanism of the <code>Device</code> class.
+ */
+protected void create(DeviceData deviceData) {
+ data = (PrinterData)deviceData;
+ byte[] driver = Converter.wcsToMbcs(0, data.driver, true);
+ byte[] device = Converter.wcsToMbcs(0, data.name, true);
+ int lpInitData = 0;
+ byte buffer [] = data.otherData;
+ int hHeap = OS.GetProcessHeap();
+ if (buffer != null && buffer.length != 0) {
+ lpInitData = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, buffer.length);
+ OS.MoveMemory(lpInitData, buffer, buffer.length);
+ }
+ handle = OS.CreateDC(driver, device, 0, lpInitData);
+ if (lpInitData != 0) OS.HeapFree(hHeap, 0, lpInitData);
+ if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
+}
+
+/**
+ * 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
+ *
+ * @private
+ */
+public int internal_new_GC(GCData data) {
+ if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
+ if (data != null) {
+ if (isGCCreated) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
+ data.device = this;
+ data.hFont = OS.GetCurrentObject(handle, OS.OBJ_FONT);
+ isGCCreated = true;
+ }
+ return handle;
+}
+
+/**
+ * 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 handle the platform specific GC handle
+ * @param data the platform specific GC data
+ *
+ * @private
+ */
+public void internal_dispose_GC(int hDC, GCData data) {
+ if (data != null) isGCCreated = false;
+}
+
+/**
+ * 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>
+ *
+ * @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();
+ DOCINFO di = new DOCINFO();
+ di.cbSize = DOCINFO.sizeof;
+ int hHeap = OS.GetProcessHeap();
+ int lpszDocName = 0;
+ if (jobName != null && jobName.length() != 0) {
+ byte [] buffer = Converter.wcsToMbcs(0, jobName, true);
+ lpszDocName = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, buffer.length);
+ OS.MoveMemory(lpszDocName, buffer, buffer.length);
+ di.lpszDocName = lpszDocName;
+ }
+ int lpszOutput = 0;
+ if (data.printToFile && data.fileName != null) {
+ byte [] buffer = Converter.wcsToMbcs(0, data.fileName, true);
+ lpszOutput = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, buffer.length);
+ OS.MoveMemory(lpszOutput, buffer, buffer.length);
+ di.lpszOutput = lpszOutput;
+ }
+ int rc = OS.StartDoc(handle, di);
+ if (lpszDocName != 0) OS.HeapFree(hHeap, 0, lpszDocName);
+ if (lpszOutput != 0) OS.HeapFree(hHeap, 0, lpszOutput);
+ return rc > 0;
+}
+
+/**
+ * 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();
+ OS.EndDoc(handle);
+}
+
+/**
+ * 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();
+ OS.AbortDoc(handle);
+}
+
+/**
+ * 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();
+ int rc = OS.StartPage(handle);
+ if (rc <= 0) OS.AbortDoc(handle);
+ return rc > 0;
+}
+
+/**
+ * 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();
+ OS.EndPage(handle);
+}
+
+/**
+ * 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();
+ int dpiX = OS.GetDeviceCaps(handle, OS.LOGPIXELSX);
+ int dpiY = OS.GetDeviceCaps(handle, OS.LOGPIXELSY);
+ return new Point(dpiX, dpiY);
+}
+
+/**
+ * Returns a rectangle describing the receiver's size and location.
+ * For a printer, this is the size of a page, in pixels.
+ *
+ * @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();
+ int width = OS.GetDeviceCaps(handle, OS.PHYSICALWIDTH);
+ int height = OS.GetDeviceCaps(handle, OS.PHYSICALHEIGHT);
+ return new Rectangle(0, 0, width, height);
+}
+
+/**
+ * Returns a rectangle which describes the area of the
+ * receiver which is capable of displaying data.
+ * For a printer, this is the size of the printable area
+ * of a page, in pixels.
+ *
+ * @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();
+ int width = OS.GetDeviceCaps(handle, OS.HORZRES);
+ int height = OS.GetDeviceCaps(handle, OS.VERTRES);
+ return new Rectangle(0, 0, width, height);
+}
+
+/**
+ * Given a desired <em>client area</em> for the receiver
+ * (as described by the arguments), returns the bounding
+ * rectangle which would be required to produce that client
+ * area.
+ * <p>
+ * In other words, it returns a rectangle such that, if the
+ * receiver's bounds were set to that rectangle, the area
+ * of the receiver which is capable of displaying data
+ * (that is, not covered by the "trimmings") would be the
+ * rectangle described by the arguments (relative to the
+ * receiver's parent).
+ * </p>
+ * Note that there is no setBounds for a printer. This method
+ * is usually used by passing in the client area (the 'printable
+ * area') of the printer. It can also be useful to pass in 0, 0, 0, 0.
+ *
+ * @return the required bounds to produce the given client area
+ *
+ * @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();
+ int printX = -OS.GetDeviceCaps(handle, OS.PHYSICALOFFSETX);
+ int printY = -OS.GetDeviceCaps(handle, OS.PHYSICALOFFSETY);
+ int printWidth = OS.GetDeviceCaps(handle, OS.HORZRES);
+ int printHeight = OS.GetDeviceCaps(handle, OS.VERTRES);
+ int paperWidth = OS.GetDeviceCaps(handle, OS.PHYSICALWIDTH);
+ int paperHeight = OS.GetDeviceCaps(handle, OS.PHYSICALHEIGHT);
+ int hTrim = paperWidth - printWidth;
+ int vTrim = paperHeight - printHeight;
+ return new Rectangle(x + printX, y + printY, width + hTrim, height + vTrim);
+}
+
+/**
+ * Returns an array of <code>FontData</code>s representing the receiver.
+ * On Windows, only one FontData will be returned per font. On X however,
+ * a <code>Font</code> object <em>may</em> be composed of multiple X
+ * fonts. To support this case, we return an array of font data objects.
+ *
+ * @return an array of font data objects describing the receiver
+ */
+public PrinterData getPrinterData() {
+ return data;
+}
+
+/**
+ * Checks the validity of this device.
+ *
+ * @exception SWTException <ul>
+ * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
+ * </ul>
+ */
+protected void checkDevice() {
+ if (handle == 0) SWT.error(SWT.ERROR_DEVICE_DISPOSED);
+}
+
+/**
+ * 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();
+ data = null;
+}
+
+/**
+ * Destroys the printer handle.
+ * This method is called internally by the dispose
+ * mechanism of the <code>Device</code> class.
+ */
+protected void destroy() {
+ if (handle != 0) OS.DeleteDC(handle);
+ handle = 0;
+}
+
+}

Back to the top