1 11 package org.eclipse.swt.printing; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.internal.win32.*; 17 18 38 public final class Printer extends Device { 39 49 public int handle; 50 51 54 PrinterData data; 55 56 59 boolean isGCCreated = false; 60 61 64 static TCHAR profile; 65 static TCHAR appName; 66 static TCHAR keyName; 67 static { 68 profile = new TCHAR(0, "PrinterPorts", true); appName = new TCHAR(0, "windows", true); keyName = new TCHAR(0, "device", true); } 72 73 79 public static PrinterData[] getPrinterList() { 80 int length = 1024; 81 82 TCHAR buf = new TCHAR(0, length); 83 TCHAR nullBuf = new TCHAR(0, 1); 84 int n = OS.GetProfileString(profile, null, nullBuf, buf, length); 85 if (n == 0) return new PrinterData[0]; 86 String [] deviceNames = new String [5]; 87 int nameCount = 0; 88 int index = 0; 89 for (int i = 0; i < n; i++) { 90 if (buf.tcharAt(i) == 0) { 91 if (nameCount == deviceNames.length) { 92 String [] newNames = new String [deviceNames.length + 5]; 93 System.arraycopy(deviceNames, 0, newNames, 0, deviceNames.length); 94 deviceNames = newNames; 95 } 96 deviceNames[nameCount] = buf.toString(index, i - index); 97 nameCount++; 98 index = i + 1; 99 } 100 } 101 PrinterData printerList[] = new PrinterData[nameCount]; 102 for (int p = 0; p < nameCount; p++) { 103 String device = deviceNames[p]; 104 String driver = ""; if (OS.GetProfileString(profile, new TCHAR(0, device, true), nullBuf, buf, length) > 0) { 106 int commaIndex = 0; 107 while (buf.tcharAt(commaIndex) != ',' && commaIndex < length) commaIndex++; 108 if (commaIndex < length) { 109 driver = buf.toString(0, commaIndex); 110 } 111 } 112 printerList[p] = new PrinterData(driver, device); 113 } 114 return printerList; 115 } 116 117 126 public static PrinterData getDefaultPrinterData() { 127 String deviceName = null; 128 int length = 1024; 129 130 TCHAR buf = new TCHAR(0, length); 131 TCHAR nullBuf = new TCHAR(0, 1); 132 int n = OS.GetProfileString(appName, keyName, nullBuf, buf, length); 133 if (n == 0) return null; 134 int commaIndex = 0; 135 while(buf.tcharAt(commaIndex) != ',' && commaIndex < length) commaIndex++; 136 if (commaIndex < length) { 137 deviceName = buf.toString(0, commaIndex); 138 } 139 String driver = ""; if (OS.GetProfileString(profile, new TCHAR(0, deviceName, true), nullBuf, buf, length) > 0) { 141 commaIndex = 0; 142 while (buf.tcharAt(commaIndex) != ',' && commaIndex < length) commaIndex++; 143 if (commaIndex < length) { 144 driver = buf.toString(0, commaIndex); 145 } 146 } 147 return new PrinterData(driver, deviceName); 148 } 149 150 static DeviceData checkNull (PrinterData data) { 151 if (data == null) data = new PrinterData(); 152 if (data.driver == null || data.name == null) { 153 PrinterData defaultPrinter = getDefaultPrinterData(); 154 if (defaultPrinter == null) SWT.error(SWT.ERROR_NO_HANDLES); 155 data.driver = defaultPrinter.driver; 156 data.name = defaultPrinter.name; 157 } 158 return data; 159 } 160 161 173 public Printer() { 174 this(null); 175 } 176 177 195 public Printer(PrinterData data) { 196 super(checkNull(data)); 197 } 198 199 205 protected void create(DeviceData deviceData) { 206 data = (PrinterData)deviceData; 207 208 TCHAR driver = new TCHAR(0, data.driver, true); 209 TCHAR device = new TCHAR(0, data.name, true); 210 int lpInitData = 0; 211 byte buffer [] = data.otherData; 212 int hHeap = OS.GetProcessHeap(); 213 if (buffer != null && buffer.length != 0) { 214 215 lpInitData = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, buffer.length); 216 OS.MoveMemory(lpInitData, buffer, buffer.length); 217 } 218 handle = OS.CreateDC(driver, device, 0, lpInitData); 219 if (lpInitData != 0) OS.HeapFree(hHeap, 0, lpInitData); 220 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 221 } 222 223 236 public int internal_new_GC(GCData data) { 237 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 238 if (data != null) { 239 if (isGCCreated) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 240 int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; 241 if ((data.style & mask) != 0) { 242 data.layout = (data.style & SWT.RIGHT_TO_LEFT) != 0 ? OS.LAYOUT_RTL : 0; 243 } else { 244 data.style |= SWT.LEFT_TO_RIGHT; 245 } 246 data.device = this; 247 data.hFont = OS.GetCurrentObject(handle, OS.OBJ_FONT); 248 isGCCreated = true; 249 } 250 return handle; 251 } 252 253 266 public void internal_dispose_GC(int hDC, GCData data) { 267 if (data != null) isGCCreated = false; 268 } 269 270 291 public boolean startJob(String jobName) { 292 checkDevice(); 293 DOCINFO di = new DOCINFO(); 294 di.cbSize = DOCINFO.sizeof; 295 int hHeap = OS.GetProcessHeap(); 296 int lpszDocName = 0; 297 if (jobName != null && jobName.length() != 0) { 298 299 TCHAR buffer = new TCHAR(0, jobName, true); 300 int byteCount = buffer.length() * TCHAR.sizeof; 301 lpszDocName = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 302 OS.MoveMemory(lpszDocName, buffer, byteCount); 303 di.lpszDocName = lpszDocName; 304 } 305 int lpszOutput = 0; 306 if (data.printToFile && data.fileName != null) { 307 308 TCHAR buffer = new TCHAR(0, data.fileName, true); 309 int byteCount = buffer.length() * TCHAR.sizeof; 310 lpszOutput = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 311 OS.MoveMemory(lpszOutput, buffer, byteCount); 312 di.lpszOutput = lpszOutput; 313 } 314 int rc = OS.StartDoc(handle, di); 315 if (lpszDocName != 0) OS.HeapFree(hHeap, 0, lpszDocName); 316 if (lpszOutput != 0) OS.HeapFree(hHeap, 0, lpszOutput); 317 return rc > 0; 318 } 319 320 331 public void endJob() { 332 checkDevice(); 333 OS.EndDoc(handle); 334 } 335 336 343 public void cancelJob() { 344 checkDevice(); 345 OS.AbortDoc(handle); 346 } 347 348 366 public boolean startPage() { 367 checkDevice(); 368 int rc = OS.StartPage(handle); 369 if (rc <= 0) OS.AbortDoc(handle); 370 return rc > 0; 371 } 372 373 384 public void endPage() { 385 checkDevice(); 386 OS.EndPage(handle); 387 } 388 389 400 public Point getDPI() { 401 checkDevice(); 402 int dpiX = OS.GetDeviceCaps(handle, OS.LOGPIXELSX); 403 int dpiY = OS.GetDeviceCaps(handle, OS.LOGPIXELSY); 404 return new Point(dpiX, dpiY); 405 } 406 407 420 public Rectangle getBounds() { 421 checkDevice(); 422 int width = OS.GetDeviceCaps(handle, OS.PHYSICALWIDTH); 423 int height = OS.GetDeviceCaps(handle, OS.PHYSICALHEIGHT); 424 return new Rectangle(0, 0, width, height); 425 } 426 427 442 public Rectangle getClientArea() { 443 checkDevice(); 444 int width = OS.GetDeviceCaps(handle, OS.HORZRES); 445 int height = OS.GetDeviceCaps(handle, OS.VERTRES); 446 return new Rectangle(0, 0, width, height); 447 } 448 449 480 public Rectangle computeTrim(int x, int y, int width, int height) { 481 checkDevice(); 482 int printX = -OS.GetDeviceCaps(handle, OS.PHYSICALOFFSETX); 483 int printY = -OS.GetDeviceCaps(handle, OS.PHYSICALOFFSETY); 484 int printWidth = OS.GetDeviceCaps(handle, OS.HORZRES); 485 int printHeight = OS.GetDeviceCaps(handle, OS.VERTRES); 486 int paperWidth = OS.GetDeviceCaps(handle, OS.PHYSICALWIDTH); 487 int paperHeight = OS.GetDeviceCaps(handle, OS.PHYSICALHEIGHT); 488 int hTrim = paperWidth - printWidth; 489 int vTrim = paperHeight - printHeight; 490 return new Rectangle(x + printX, y + printY, width + hTrim, height + vTrim); 491 } 492 493 499 public PrinterData getPrinterData() { 500 return data; 501 } 502 503 510 protected void checkDevice() { 511 if (handle == 0) SWT.error(SWT.ERROR_DEVICE_DISPOSED); 512 } 513 514 519 protected void release() { 520 super.release(); 521 data = null; 522 } 523 524 529 protected void destroy() { 530 if (handle != 0) OS.DeleteDC(handle); 531 handle = 0; 532 } 533 534 } 535 | Popular Tags |