1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 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
|
* Sven M?ller - Added tiling support
|
11
|
*******************************************************************************/
|
12
|
package org.eclipse.draw2d;
|
13
|
|
14
|
import org.eclipse.swt.graphics.Color;
|
15
|
import org.eclipse.swt.printing.Printer;
|
16
|
import org.eclipse.swt.widgets.Display;
|
17
|
|
18
|
import org.eclipse.draw2d.geometry.Rectangle;
|
19
|
import org.eclipse.draw2d.rap.swt.SWT;
|
20
|
|
21
|
/**
|
22
|
* Class responsible for printing Figures.
|
23
|
*
|
24
|
* @author Dan Lee
|
25
|
* @author Eric Bordeau
|
26
|
* @author Sven M?ller
|
27
|
*/
|
28
|
public class PrintFigureOperation extends PrintOperation {
|
29
|
|
30
|
/**
|
31
|
* The default print mode. Prints at 100% scale and tiles horizontally
|
32
|
* and/or vertically, if necessary.
|
33
|
*/
|
34
|
public static final int TILE = 1;
|
35
|
/**
|
36
|
* A print mode that scales the printer graphics so that the entire printed
|
37
|
* image fits on one page.
|
38
|
*/
|
39
|
public static final int FIT_PAGE = 2;
|
40
|
/**
|
41
|
* A print mode that scales the printer graphics so that the width of the
|
42
|
* printed image fits on one page and tiles vertically, if necessary.
|
43
|
*/
|
44
|
public static final int FIT_WIDTH = 3;
|
45
|
/**
|
46
|
* A print mode that scales the printer graphics so that the height of the
|
47
|
* printed image fits on one page and tiles horizontally, if necessary.
|
48
|
*/
|
49
|
public static final int FIT_HEIGHT = 4;
|
50
|
|
51
|
private IFigure printSource;
|
52
|
private Color oldBGColor;
|
53
|
private int printMode = TILE;
|
54
|
|
55
|
/**
|
56
|
* Constructor for PrintFigureOperation.
|
57
|
* <p>
|
58
|
* Note: Descendants must call setPrintSource(IFigure) to set the IFigure
|
59
|
* that is to be printed.
|
60
|
*
|
61
|
* @see org.eclipse.draw2d.PrintOperation#PrintOperation(Printer)
|
62
|
*/
|
63
|
protected PrintFigureOperation(Printer p) {
|
64
|
super(p);
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Constructor for PrintFigureOperation.
|
69
|
*
|
70
|
* @param p
|
71
|
* Printer to print on
|
72
|
* @param srcFigure
|
73
|
* Figure to print
|
74
|
*/
|
75
|
public PrintFigureOperation(Printer p, IFigure srcFigure) {
|
76
|
super(p);
|
77
|
setPrintSource(srcFigure);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* @return SWT.RIGHT_TO_LEFT if the print source is mirrored;
|
82
|
* SWT.LEFT_TO_RIGHT otherwise
|
83
|
* @see org.eclipse.draw2d.PrintOperation#getGraphicsOrientation()
|
84
|
*/
|
85
|
int getGraphicsOrientation() {
|
86
|
return getPrintSource().isMirrored() ? SWT.RIGHT_TO_LEFT
|
87
|
: SWT.LEFT_TO_RIGHT;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Returns the current print mode. The print mode is one of:
|
92
|
* {@link #FIT_HEIGHT}, {@link #FIT_PAGE}, or {@link #FIT_WIDTH}.
|
93
|
*
|
94
|
* @return the print mode
|
95
|
*/
|
96
|
protected int getPrintMode() {
|
97
|
return printMode;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Returns the printSource.
|
102
|
*
|
103
|
* @return IFigure The source IFigure
|
104
|
*/
|
105
|
protected IFigure getPrintSource() {
|
106
|
return printSource;
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* @see org.eclipse.draw2d.PrintOperation#preparePrintSource()
|
111
|
*/
|
112
|
protected void preparePrintSource() {
|
113
|
oldBGColor = getPrintSource().getLocalBackgroundColor();
|
114
|
getPrintSource().setBackgroundColor(ColorConstants.white);
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Prints the pages based on the current print mode.
|
119
|
*
|
120
|
* @see org.eclipse.draw2d.PrintOperation#printPages()
|
121
|
*/
|
122
|
protected void printPages() {
|
123
|
Graphics graphics = getFreshPrinterGraphics();
|
124
|
IFigure figure = getPrintSource();
|
125
|
setupPrinterGraphicsFor(graphics, figure);
|
126
|
Rectangle bounds = figure.getBounds();
|
127
|
int x = bounds.x, y = bounds.y;
|
128
|
Rectangle clipRect = new Rectangle();
|
129
|
while (y < bounds.y + bounds.height) {
|
130
|
while (x < bounds.x + bounds.width) {
|
131
|
graphics.pushState();
|
132
|
getPrinter().startPage();
|
133
|
graphics.translate(-x, -y);
|
134
|
graphics.getClip(clipRect);
|
135
|
clipRect.setLocation(x, y);
|
136
|
graphics.clipRect(clipRect);
|
137
|
figure.paint(graphics);
|
138
|
getPrinter().endPage();
|
139
|
graphics.popState();
|
140
|
x += clipRect.width;
|
141
|
}
|
142
|
x = bounds.x;
|
143
|
y += clipRect.height;
|
144
|
}
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* @see org.eclipse.draw2d.PrintOperation#restorePrintSource()
|
149
|
*/
|
150
|
protected void restorePrintSource() {
|
151
|
getPrintSource().setBackgroundColor(oldBGColor);
|
152
|
oldBGColor = null;
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Sets the print mode. Possible values are {@link #TILE},
|
157
|
* {@link #FIT_HEIGHT}, {@link #FIT_WIDTH} and {@link #FIT_PAGE}.
|
158
|
*
|
159
|
* @param mode
|
160
|
* the print mode
|
161
|
*/
|
162
|
public void setPrintMode(int mode) {
|
163
|
printMode = mode;
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Sets the printSource.
|
168
|
*
|
169
|
* @param printSource
|
170
|
* The printSource to set
|
171
|
*/
|
172
|
protected void setPrintSource(IFigure printSource) {
|
173
|
this.printSource = printSource;
|
174
|
}
|
175
|
|
176
|
/**
|
177
|
* Sets up Graphics object for the given IFigure.
|
178
|
*
|
179
|
* @param graphics
|
180
|
* The Graphics to setup
|
181
|
* @param figure
|
182
|
* The IFigure used to setup graphics
|
183
|
*/
|
184
|
protected void setupPrinterGraphicsFor(Graphics graphics, IFigure figure) {
|
185
|
double dpiScale = (double) getPrinter().getDPI().x
|
186
|
/ Display.getCurrent().getDPI().x;
|
187
|
|
188
|
Rectangle printRegion = getPrintRegion();
|
189
|
// put the print region in display coordinates
|
190
|
printRegion.width /= dpiScale;
|
191
|
printRegion.height /= dpiScale;
|
192
|
|
193
|
Rectangle bounds = figure.getBounds();
|
194
|
double xScale = (double) printRegion.width / bounds.width;
|
195
|
double yScale = (double) printRegion.height / bounds.height;
|
196
|
switch (getPrintMode()) {
|
197
|
case FIT_PAGE:
|
198
|
graphics.scale(Math.min(xScale, yScale) * dpiScale);
|
199
|
break;
|
200
|
case FIT_WIDTH:
|
201
|
graphics.scale(xScale * dpiScale);
|
202
|
break;
|
203
|
case FIT_HEIGHT:
|
204
|
graphics.scale(yScale * dpiScale);
|
205
|
break;
|
206
|
default:
|
207
|
graphics.scale(dpiScale);
|
208
|
}
|
209
|
graphics.setForegroundColor(figure.getForegroundColor());
|
210
|
graphics.setBackgroundColor(figure.getBackgroundColor());
|
211
|
graphics.setFont(figure.getFont());
|
212
|
}
|
213
|
|
214
|
}
|