Skip to content

Commit ca459f1

Browse files
committed
Enhance Snippet391 to allow direct calling of print method
1 parent 7a03c60 commit ca459f1

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

  • examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet391.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Snippet391 {
3535

3636
private static int boxSizeCm = 10;
3737
private static boolean considerMonitorZoom = false;
38+
private static boolean useControlPrint = false;
3839

3940
public static void main(String[] args) {
4041
Display display = new Display();
@@ -85,6 +86,28 @@ public static void main(String[] args) {
8586
canvas.redraw();
8687
});
8788

89+
// Paint mode radio buttons
90+
Group modeGroup = new Group(controlPanel, SWT.NONE);
91+
modeGroup.setText("Paint Mode");
92+
modeGroup.setLayout(new GridLayout(1, false));
93+
GridData modeData = new GridData(SWT.FILL, SWT.CENTER, true, false);
94+
modeData.horizontalSpan = 2;
95+
modeGroup.setLayoutData(modeData);
96+
97+
Button directPaintRadio = new Button(modeGroup, SWT.RADIO);
98+
directPaintRadio.setText("Direct Paint");
99+
directPaintRadio.setSelection(true);
100+
101+
Button controlPrintRadio = new Button(modeGroup, SWT.RADIO);
102+
controlPrintRadio.setText("Control Print");
103+
104+
directPaintRadio.addListener(SWT.Selection, e -> {
105+
useControlPrint = !directPaintRadio.getSelection();
106+
});
107+
controlPrintRadio.addListener(SWT.Selection, e -> {
108+
useControlPrint = controlPrintRadio.getSelection();
109+
});
110+
88111
// Separator
89112
Label separator = new Label(controlPanel, SWT.SEPARATOR | SWT.HORIZONTAL);
90113
GridData sepData = new GridData(SWT.FILL, SWT.CENTER, true, false);
@@ -97,7 +120,7 @@ public static void main(String[] args) {
97120
GridData printData = new GridData(SWT.FILL, SWT.CENTER, true, false);
98121
printData.horizontalSpan = 2;
99122
printButton.setLayoutData(printData);
100-
printButton.addListener(SWT.Selection, e -> printToPrinter(shell));
123+
printButton.addListener(SWT.Selection, e -> printToPrinter(shell, canvas));
101124

102125
// Create PDF button
103126
Button pdfButton = new Button(controlPanel, SWT.PUSH);
@@ -203,7 +226,7 @@ public static void drawTestPattern(GC gc, Device device, Rectangle rect, Monitor
203226
gc.setForeground(originalFg);
204227
}
205228

206-
private static void printToPrinter(Shell shell) {
229+
private static void printToPrinter(Shell shell, Canvas canvas) {
207230
PrintDialog printDialog = new PrintDialog(shell);
208231
PrinterData data = printDialog.open();
209232
if (data == null) {
@@ -214,8 +237,12 @@ private static void printToPrinter(Shell shell) {
214237
if (printer.startJob("Drawing Test - Snippet 391")) {
215238
GC gc = new GC(printer);
216239
if (printer.startPage()) {
217-
Rectangle printArea = printer.getClientArea();
218-
drawTestPattern(gc, printer, printArea, null);
240+
if (useControlPrint) {
241+
canvas.print(gc);
242+
} else {
243+
Rectangle printArea = printer.getClientArea();
244+
drawTestPattern(gc, printer, printArea, null);
245+
}
219246
printer.endPage();
220247
}
221248
gc.dispose();
@@ -232,7 +259,7 @@ private static void printToPrinter(Shell shell) {
232259
private static void createPdf(Shell shell, Canvas canvas) {
233260
try {
234261
String tempDir = System.getProperty("java.io.tmpdir");
235-
String pdfPath = tempDir + "/drawing_test_snippet391.pdf";
262+
String pdfPath = tempDir +(useControlPrint? "/drawing_test_snippet391_control.pdf":"/drawing_test_snippet391_direct.pdf");
236263

237264
// Use exact canvas size in points (1 point = 1 pixel at 72 DPI)
238265
Point canvasSize = canvas.getSize();
@@ -242,8 +269,12 @@ private static void createPdf(Shell shell, Canvas canvas) {
242269
PDFDocument pdf = new PDFDocument(pdfPath, widthPoints, heightPoints);
243270
GC gc = new GC(pdf);
244271

245-
Rectangle pdfRect = new Rectangle(0, 0, canvasSize.x, canvasSize.y);
246-
drawTestPattern(gc, pdf, pdfRect, null);
272+
if (useControlPrint) {
273+
canvas.print(gc);
274+
} else {
275+
Rectangle pdfRect = new Rectangle(0, 0, canvasSize.x, canvasSize.y);
276+
drawTestPattern(gc, pdf, pdfRect, null);
277+
}
247278

248279
gc.dispose();
249280
pdf.dispose();

0 commit comments

Comments
 (0)