?? printer.java
字號:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-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.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 TCHAR profile; static TCHAR appName; static TCHAR keyName; static { profile = new TCHAR(0, "PrinterPorts", true); //$NON-NLS-1$ appName = new TCHAR(0, "windows", true); //$NON-NLS-1$ keyName = new TCHAR(0, "device", true); //$NON-NLS-1$ } /** * Returns an array of <code>PrinterData</code> objects * representing all available printers. * * @return the list of available printers */public static PrinterData[] getPrinterList() { int length = 1024; /* Use the character encoding for the default locale */ TCHAR buf = new TCHAR(0, length); TCHAR nullBuf = new TCHAR(0, 1); int n = OS.GetProfileString(profile, null, nullBuf, buf, length); if (n == 0) return new PrinterData[0]; String[] deviceNames = new String[5]; int nameCount = 0; int index = 0; for (int i = 0; i < n; i++) { if (buf.tcharAt(i) == 0) { if (nameCount == deviceNames.length) { String[] newNames = new String[deviceNames.length + 5]; System.arraycopy(deviceNames, 0, newNames, 0, deviceNames.length); deviceNames = newNames; } deviceNames[nameCount] = buf.toString(index, i - index); nameCount++; index = i + 1; } } PrinterData printerList[] = new PrinterData[nameCount]; for (int p = 0; p < nameCount; p++) { String device = deviceNames[p]; String driver = ""; //$NON-NLS-1$ if (OS.GetProfileString(profile, new TCHAR(0, device, true), nullBuf, buf, length) > 0) { int commaIndex = 0; while (buf.tcharAt(commaIndex) != ',' && commaIndex < length) commaIndex++; if (commaIndex < length) { driver = buf.toString(0, commaIndex); } } printerList[p] = new PrinterData(driver, device); } return printerList;}/** * Returns a <code>PrinterData</code> object representing * the default printer or <code>null</code> if there is no * printer available on the System. * * @return the default printer data or null * * @since 2.1 */public static PrinterData getDefaultPrinterData() { String deviceName = null; int length = 1024; /* Use the character encoding for the default locale */ TCHAR buf = new TCHAR(0, length); TCHAR nullBuf = new TCHAR(0, 1); int n = OS.GetProfileString(appName, keyName, nullBuf, buf, length); if (n == 0) return null; int commaIndex = 0; while(buf.tcharAt(commaIndex) != ',' && commaIndex < length) commaIndex++; if (commaIndex < length) { deviceName = buf.toString(0, commaIndex); } String driver = ""; //$NON-NLS-1$ if (OS.GetProfileString(profile, new TCHAR(0, deviceName, true), nullBuf, buf, length) > 0) { commaIndex = 0; while (buf.tcharAt(commaIndex) != ',' && commaIndex < length) commaIndex++; if (commaIndex < length) { driver = buf.toString(0, commaIndex); } } return new PrinterData(driver, deviceName);}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;}/** * Constructs a new printer representing the default printer. * <p> * 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. * <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 * </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));}/** * 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; /* Use the character encoding for the default locale */ TCHAR driver = new TCHAR(0, data.driver, true); TCHAR device = new TCHAR(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 */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); int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; if ((data.style & mask) != 0) { data.layout = (data.style & SWT.RIGHT_TO_LEFT) != 0 ? OS.LAYOUT_RTL : 0; } else { data.style |= SWT.LEFT_TO_RIGHT; } 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 hDC the platform specific GC handle * @param data the platform specific GC data */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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -