comparison dwt/printing/Printer.d @ 120:7046ca5a6d77

Ported dwt.printing.Printer
author Jacob Carlborg <doob@me.com>
date Wed, 31 Dec 2008 16:16:40 +0100
parents d9893755f670
children 3d4579727e0e
comparison
equal deleted inserted replaced
119:d9893755f670 120:7046ca5a6d77
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 *
11 * Port to the D programming language:
12 * Jacob Carlborg <doob@me.com>
10 *******************************************************************************/ 13 *******************************************************************************/
11 module dwt.printing.Printer; 14 module dwt.printing.Printer;
12 15
13 import dwt.dwthelper.utils; 16 import dwt.dwthelper.utils;
14 17
35 import dwt.internal.cocoa.NSString; 38 import dwt.internal.cocoa.NSString;
36 import dwt.internal.cocoa.NSView; 39 import dwt.internal.cocoa.NSView;
37 import dwt.internal.cocoa.NSWindow; 40 import dwt.internal.cocoa.NSWindow;
38 import dwt.internal.cocoa.OS; 41 import dwt.internal.cocoa.OS;
39 42
43 import Carbon = dwt.internal.c.Carbon;
44 import dwt.internal.objc.cocoa.Cocoa;
45 import dwt.printing.PrinterData;
46
40 /** 47 /**
41 * Instances of this class are used to print to a printer. 48 * Instances of this class are used to print to a printer.
42 * Applications create a GC on a printer using <code>new GC(printer)</code> 49 * Applications create a GC on a printer using <code>new GC(printer)</code>
43 * and then draw on the printer GC using the usual graphics calls. 50 * and then draw on the printer GC using the usual graphics calls.
44 * <p> 51 * <p>
76 * 83 *
77 * @return the list of available printers 84 * @return the list of available printers
78 */ 85 */
79 public static PrinterData[] getPrinterList() { 86 public static PrinterData[] getPrinterList() {
80 NSArray printers = NSPrinter.printerNames(); 87 NSArray printers = NSPrinter.printerNames();
81 int count = (int)/*64*/printers.count(); 88 NSUInteger count = printers.count();
82 PrinterData[] result = new PrinterData[count]; 89 PrinterData[] result = new PrinterData[count];
83 for (int i = 0; i < count; i++) { 90 for (NSUInteger i = 0; i < count; i++) {
84 NSString str = new NSString(printers.objectAtIndex(i)); 91 NSString str = new NSString(printers.objectAtIndex(i));
85 result[i] = new PrinterData(DRIVER, str.getString()); 92 result[i] = new PrinterData(DRIVER, str.getString());
86 } 93 }
87 return result; 94 return result;
88 } 95 }
114 * <li>ERROR_NO_HANDLES - if there are no valid printers 121 * <li>ERROR_NO_HANDLES - if there are no valid printers
115 * </ul> 122 * </ul>
116 * 123 *
117 * @see Device#dispose 124 * @see Device#dispose
118 */ 125 */
119 public Printer() { 126 public this() {
120 this(null); 127 this(null);
121 } 128 }
122 129
123 /** 130 /**
124 * Constructs a new printer given a <code>PrinterData</code> 131 * Constructs a new printer given a <code>PrinterData</code>
136 * <li>ERROR_NO_HANDLES - if there are no valid printers 143 * <li>ERROR_NO_HANDLES - if there are no valid printers
137 * </ul> 144 * </ul>
138 * 145 *
139 * @see Device#dispose 146 * @see Device#dispose
140 */ 147 */
141 public Printer(PrinterData data) { 148 public this(PrinterData data) {
142 super (checkNull(data)); 149 super (checkNull(data));
143 } 150 }
144 151
145 /** 152 /**
146 * Given a <em>client area</em> (as described by the arguments), 153 * Given a <em>client area</em> (as described by the arguments),
194 * This method is called internally by the instance creation 201 * This method is called internally by the instance creation
195 * mechanism of the <code>Device</code> class. 202 * mechanism of the <code>Device</code> class.
196 * @param deviceData the device data 203 * @param deviceData the device data
197 */ 204 */
198 protected void create(DeviceData deviceData) { 205 protected void create(DeviceData deviceData) {
199 data = (PrinterData)deviceData; 206 data = cast(PrinterData)deviceData;
200 if (data.otherData !is null) { 207 if (data.otherData !is null) {
201 NSData nsData = NSData.dataWithBytes(data.otherData, data.otherData.length); 208 NSData nsData = NSData.dataWithBytes(data.otherData.ptr, data.otherData.length);
202 printInfo = new NSPrintInfo(NSKeyedUnarchiver.unarchiveObjectWithData(nsData).id); 209 printInfo = new NSPrintInfo(NSKeyedUnarchiver.unarchiveObjectWithData(nsData).id);
203 } else { 210 } else {
204 printInfo = NSPrintInfo.sharedPrintInfo(); 211 printInfo = NSPrintInfo.sharedPrintInfo();
205 } 212 }
206 printInfo.retain(); 213 printInfo.retain();
212 /* 219 /*
213 * Bug in Cocoa. For some reason, the output still goes to the printer when 220 * Bug in Cocoa. For some reason, the output still goes to the printer when
214 * the user chooses the preview button. The fix is to reset the job disposition. 221 * the user chooses the preview button. The fix is to reset the job disposition.
215 */ 222 */
216 NSString job = printInfo.jobDisposition(); 223 NSString job = printInfo.jobDisposition();
217 if (job.isEqual(new NSString(OS.NSPrintPreviewJob()))) { 224 if (job.isEqual(new NSString(OS.NSPrintPreviewJob_))) {
218 printInfo.setJobDisposition(job); 225 printInfo.setJobDisposition(job);
219 } 226 }
220 NSRect rect = NSRect(); 227 NSRect rect = NSRect();
221 window = (NSWindow)new NSWindow().alloc(); 228 window = cast(NSWindow)(new NSWindow()).alloc();
222 window.initWithContentRect(rect, OS.NSBorderlessWindowMask, OS.NSBackingStoreBuffered, false); 229 window.initWithContentRect(rect, OS.NSBorderlessWindowMask, OS.NSBackingStoreBuffered, false);
223 view = (NSView)new NSView().alloc(); 230 view = cast(NSView)(new NSView()).alloc();
224 view.initWithFrame(rect); 231 view.initWithFrame(rect);
225 window.setContentView(view); 232 window.setContentView(view);
226 operation = NSPrintOperation.printOperationWithView(view, printInfo); 233 operation = NSPrintOperation.printOperationWithView(view, printInfo);
227 operation.retain(); 234 operation.retain();
228 operation.setShowsPrintPanel(false); 235 operation.setShowsPrintPanel(false);
257 * </p> 264 * </p>
258 * 265 *
259 * @param data the platform specific GC data 266 * @param data the platform specific GC data
260 * @return the platform specific GC handle 267 * @return the platform specific GC handle
261 */ 268 */
262 public int /*long*/ internal_new_GC(GCData data) { 269 public objc.id internal_new_GC(GCData data) {
263 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 270 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
264 if (data !is null) { 271 if (data !is null) {
265 if (isGCCreated) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 272 if (isGCCreated) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
266 data.device = this; 273 data.device = this;
267 data.background = getSystemColor(DWT.COLOR_WHITE).handle; 274 data.background = getSystemColor(DWT.COLOR_WHITE).handle;
268 data.foreground = getSystemColor(DWT.COLOR_BLACK).handle; 275 data.foreground = getSystemColor(DWT.COLOR_BLACK).handle;
269 data.font = getSystemFont (); 276 data.font = getSystemFont ();
270 data.size = printInfo.paperSize(); 277 data.sizeStruct = printInfo.paperSize();
278 data.size = &data.sizeStruct;
271 isGCCreated = true; 279 isGCCreated = true;
272 } 280 }
273 return operation.context().id; 281 return operation.context().id;
274 } 282 }
275 283
276 protected void init () { 284 protected void init_ () {
277 super.init(); 285 super.init_();
278 } 286 }
279 287
280 /** 288 /**
281 * Invokes platform specific functionality to dispose a GC handle. 289 * Invokes platform specific functionality to dispose a GC handle.
282 * <p> 290 * <p>
288 * </p> 296 * </p>
289 * 297 *
290 * @param hDC the platform specific GC handle 298 * @param hDC the platform specific GC handle
291 * @param data the platform specific GC data 299 * @param data the platform specific GC data
292 */ 300 */
293 public void internal_dispose_GC(int /*long*/ context, GCData data) { 301 public void internal_dispose_GC(objc.id context, GCData data) {
294 if (data !is null) isGCCreated = false; 302 if (data !is null) isGCCreated = false;
295 } 303 }
296 304
297 /** 305 /**
298 * Releases any internal state prior to destroying this printer. 306 * Releases any internal state prior to destroying this printer.
411 NSBezierPath.bezierPathWithRect(imageBounds).setClip(); 419 NSBezierPath.bezierPathWithRect(imageBounds).setClip();
412 NSAffineTransform transform = NSAffineTransform.transform(); 420 NSAffineTransform transform = NSAffineTransform.transform();
413 transform.translateXBy(imageBounds.x, rect.height - imageBounds.y); 421 transform.translateXBy(imageBounds.x, rect.height - imageBounds.y);
414 transform.scaleXBy(1, -1); 422 transform.scaleXBy(1, -1);
415 Point dpi = getDPI (), screenDPI = getIndependentDPI(); 423 Point dpi = getDPI (), screenDPI = getIndependentDPI();
416 transform.scaleXBy(screenDPI.x / (float)dpi.x, screenDPI.y / (float)dpi.y); 424 transform.scaleXBy(screenDPI.x / cast(Carbon.CGFloat)dpi.x, screenDPI.y / cast(Carbon.CGFloat)dpi.y);
417 transform.concat(); 425 transform.concat();
418 return true; 426 return true;
419 } 427 }
420 428
421 /** 429 /**
472 */ 480 */
473 public Rectangle getBounds() { 481 public Rectangle getBounds() {
474 checkDevice(); 482 checkDevice();
475 NSSize size = printInfo.paperSize(); 483 NSSize size = printInfo.paperSize();
476 Point dpi = getDPI (), screenDPI = getIndependentDPI(); 484 Point dpi = getDPI (), screenDPI = getIndependentDPI();
477 return new Rectangle (0, 0, (int)(size.width * dpi.x / screenDPI.x), (int)(size.height * dpi.y / screenDPI.y)); 485 return new Rectangle (0, 0, cast(int)(size.width * dpi.x / screenDPI.x), cast(int)(size.height * dpi.y / screenDPI.y));
478 } 486 }
479 487
480 /** 488 /**
481 * Returns a rectangle which describes the area of the 489 * Returns a rectangle which describes the area of the
482 * receiver which is capable of displaying data. 490 * receiver which is capable of displaying data.
496 */ 504 */
497 public Rectangle getClientArea() { 505 public Rectangle getClientArea() {
498 checkDevice(); 506 checkDevice();
499 NSRect rect = printInfo.imageablePageBounds(); 507 NSRect rect = printInfo.imageablePageBounds();
500 Point dpi = getDPI (), screenDPI = getIndependentDPI(); 508 Point dpi = getDPI (), screenDPI = getIndependentDPI();
501 return new Rectangle(0, 0, (int)(rect.width * dpi.x / screenDPI.x), (int)(rect.height * dpi.y / screenDPI.y)); 509 return new Rectangle(0, 0, cast(int)(rect.width * dpi.x / screenDPI.x), cast(int)(rect.height * dpi.y / screenDPI.y));
502 } 510 }
503 511
504 /** 512 /**
505 * Returns a <code>PrinterData</code> object representing the 513 * Returns a <code>PrinterData</code> object representing the
506 * target printer for this print job. 514 * target printer for this print job.