1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet132"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * D Port:
46  *     Adam Chrapkowski <adam DOT chrapkowski AT gmail DOT com>
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet132;
49   
50 /*
51  * Printing example snippet: print "Hello World!" in black, outlined in red, to default printer
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  */
56  
57 // org.eclipse.swt
58 import org.eclipse.swt.SWT;
59 // org.eclipse.swt.widgets
60 import org.eclipse.swt.widgets.Display,
61        org.eclipse.swt.widgets.MessageBox,
62        org.eclipse.swt.widgets.Shell;
63 // org.eclipse.swt.graphics
64 import org.eclipse.swt.graphics.Color,
65        org.eclipse.swt.graphics.GC,
66        org.eclipse.swt.graphics.Rectangle;
67 // org.eclipse.swt.printing
68 import org.eclipse.swt.printing.PrintDialog, 
69        org.eclipse.swt.printing.Printer, 
70        org.eclipse.swt.printing.PrinterData;
71 // java
72 import java.lang.all;
73 
74 void main(){
75     Display display = new Display();
76     Shell shell = new Shell(display);
77     shell.open();
78     PrinterData data = Printer.getDefaultPrinterData();
79     if(data is null){
80         MessageBox.showWarning("Warning: No default printer.");
81         return;
82     }
83     Printer printer = new Printer(data);
84     if(printer.startJob("SWT Printing Snippet")){
85         Color black = printer.getSystemColor(SWT.COLOR_BLACK);
86         Color white = printer.getSystemColor(SWT.COLOR_WHITE);
87         Color red = printer.getSystemColor(SWT.COLOR_RED);
88         Rectangle trim = printer.computeTrim(0, 0, 0, 0);
89         Point dpi = printer.getDPI();
90         int leftMargin = dpi.x + trim.x; // one inch from left side of paper
91         int topMargin = dpi.y / 2 + trim.y; // one-half inch from top edge of paper
92         GC gc = new GC(printer);
93         if(printer.startPage()){
94             gc.setBackground(white);
95             gc.setForeground(black);
96             String testString = "Hello World!";
97             Point extent = gc.stringExtent(testString);
98             gc.drawString(testString, leftMargin, topMargin);
99             gc.setForeground(red);
100             gc.drawRectangle(leftMargin, topMargin, extent.x, extent.y);
101             printer.endPage();
102         }
103         gc.dispose();
104         printer.endJob();
105     }
106     printer.dispose();
107     while(!shell.isDisposed()){
108       if(!display.readAndDispatch()) display.sleep();
109     }
110     display.dispose();
111 }