comparison dwtx/draw2d/PrintOperation.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.PrintOperation;
14
15 import dwt.dwthelper.utils;
16
17 static import dwt.graphics.Rectangle;
18 static import dwt.graphics.Point;
19
20 import dwt.DWT;
21 import dwt.graphics.GC;
22 import dwt.printing.Printer;
23 import dwtx.draw2d.geometry.Insets;
24 import dwtx.draw2d.geometry.Rectangle;
25 import dwtx.draw2d.PrinterGraphics;
26 import dwtx.draw2d.SWTGraphics;
27
28
29 /**
30 * Implementation of draw2d's printing capabilities.
31 *
32 * @author Dan Lee
33 * @author Eric Bordeau
34 */
35 public abstract class PrintOperation {
36
37 private GC printerGC; // Note: Only one GC instance should be created per print job
38 private Insets printMargin;
39 private Printer printer;
40 private PrinterGraphics printerGraphics;
41 private SWTGraphics g;
42
43 /**
44 * Creates a new PrintOperation
45 */
46 public this() {
47 printMargin = new Insets(0, 0, 0, 0);
48 }
49
50 /**
51 * Creates a new PrintOperation on Printer p
52 * @param p The printer to print on
53 */
54 public this(Printer p) {
55 this();
56 setPrinter(p);
57 }
58
59 /**
60 * Disposes the PrinterGraphics and GC objects associated with this PrintOperation.
61 */
62 protected void cleanup() {
63 if (g !is null) {
64 printerGraphics.dispose();
65 g.dispose();
66 }
67 if (printerGC !is null)
68 printerGC.dispose();
69 }
70
71 /**
72 * Returns a new PrinterGraphics setup for the Printer associated with this
73 * PrintOperation.
74 *
75 * @return PrinterGraphics The new PrinterGraphics
76 */
77 protected PrinterGraphics getFreshPrinterGraphics() {
78 if (printerGraphics !is null) {
79 printerGraphics.dispose();
80 g.dispose();
81 printerGraphics = null;
82 g = null;
83 }
84 g = new SWTGraphics(printerGC);
85 printerGraphics = new PrinterGraphics(g, printer);
86 setupGraphicsForPage(printerGraphics);
87 return printerGraphics;
88 }
89
90 /**
91 * This method is invoked by the {@link #run(String)} method to determine the orientation
92 * of the GC to be used for printing. This default implementation always returns
93 * DWT.LEFT_TO_RIGHT.
94 * @return DWT.LEFT_TO_RIGHT or DWT.RIGHT_TO_LEFT
95 * @since 3.1
96 * @TODO Make protected post-3.1
97 */
98 int getGraphicsOrientation() {
99 return DWT.LEFT_TO_RIGHT;
100 }
101
102 /**
103 * Returns the printer.
104 * @return Printer
105 */
106 public Printer getPrinter() {
107 return printer;
108 }
109
110 /**
111 * Returns a Rectangle that represents the region that can be printed to. The x, y,
112 * height, and width values are using the printers coordinates.
113 * @return the print region
114 */
115 public Rectangle getPrintRegion() {
116 dwt.graphics.Rectangle.Rectangle trim = printer.computeTrim(0, 0, 0, 0);
117 dwt.graphics.Point.Point printerDPI = printer.getDPI();
118 Insets notAvailable = new Insets(-trim.y, -trim.x,
119 trim.height + trim.y, trim.width + trim.x);
120 Insets userPreferred = new Insets(
121 (printMargin.top * printerDPI.x) / 72,
122 (printMargin.left * printerDPI.x) / 72,
123 (printMargin.bottom * printerDPI.x) / 72,
124 (printMargin.right * printerDPI.x) / 72);
125 Rectangle paperBounds = new Rectangle(printer.getBounds());
126 Rectangle printRegion = paperBounds.getCropped(notAvailable);
127 printRegion.intersect(paperBounds.getCropped(userPreferred));
128 printRegion.translate(trim.x, trim.y);
129 return printRegion;
130 }
131
132 /**
133 * This method contains all operations performed to sourceFigure prior to being printed.
134 */
135 protected void preparePrintSource() { }
136
137 /**
138 * This method is responsible for printing pages. (A page is printed by calling
139 * Printer.startPage(), followed by painting to the PrinterGraphics object, and then
140 * calling Printer.endPage()).
141 */
142 protected abstract void printPages();
143
144 /**
145 * This method contains all operations performed to
146 * sourceFigure after being printed.
147 */
148 protected void restorePrintSource() { }
149
150 /**
151 * Sets the print job into motion.
152 *
153 * @param jobName A String representing the name of the print job
154 */
155 public void run(String jobName) {
156 preparePrintSource();
157 if (printer.startJob(jobName)) {
158 printerGC = new GC(getPrinter(), getGraphicsOrientation());
159 printPages();
160 printer.endJob();
161 }
162 restorePrintSource();
163 cleanup();
164 }
165
166 /**
167 * Sets the printer.
168 * @param printer The printer to set
169 */
170 public void setPrinter(Printer printer) {
171 this.printer = printer;
172 }
173
174 /**
175 * Sets the page margin in pels (logical pixels) to the passed Insets.(72 pels is 1 inch)
176 *
177 * @param margin The margin to set on the page
178 */
179 public void setPrintMargin(Insets margin) {
180 printMargin = margin;
181 }
182
183 /**
184 * Manipulates the PrinterGraphics to position it to paint in the desired region of the
185 * page. (Default is the top left corner of the page).
186 *
187 * @param pg The PrinterGraphics to setup
188 */
189 protected void setupGraphicsForPage(PrinterGraphics pg) {
190 Rectangle printRegion = getPrintRegion();
191 pg.clipRect(printRegion);
192 pg.translate(printRegion.getTopLeft());
193 }
194
195 }