Mercurial > projects > dwt-samples
comparison snippets/printing/Snippet133.d @ 165:513c72ba21f1
added
snippets\priniting\Snippet132.d
snippets\priniting\Snippet133.d
changed
snippets\dsss.conf
author | Adam Chrapkowski <adam.chrapkowski@gmail.com> |
---|---|
date | Wed, 03 Sep 2008 19:27:24 +0200 |
parents | |
children | 9824a8aba726 |
comparison
equal
deleted
inserted
replaced
164:0beffa2869ad | 165:513c72ba21f1 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2004 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 * D Port: | |
11 * Adam Chrapkowski <adam DOT chrapkowski AT gmail DOT com> | |
12 *******************************************************************************/ | |
13 module printing.Snippet133; | |
14 | |
15 /* | |
16 * Printing example snippet: print text to printer, with word wrap and pagination | |
17 * | |
18 * For a list of all SWT example snippets see | |
19 * http://www.eclipse.org/swt/snippets/ | |
20 */ | |
21 | |
22 // dwt | |
23 import dwt.DWT; | |
24 // dwt.graphics | |
25 import dwt.graphics.Color, | |
26 dwt.graphics.Font, | |
27 dwt.graphics.FontData, | |
28 dwt.graphics.GC, | |
29 dwt.graphics.Rectangle, | |
30 dwt.graphics.RGB; | |
31 // dwt.widgets | |
32 import dwt.widgets.Display, | |
33 dwt.widgets.ColorDialog, | |
34 dwt.widgets.FileDialog, | |
35 dwt.widgets.FontDialog, | |
36 dwt.widgets.Menu, | |
37 dwt.widgets.MenuItem, | |
38 dwt.widgets.MessageBox, | |
39 dwt.widgets.Shell, | |
40 dwt.widgets.Text; | |
41 // dwt.events | |
42 import dwt.events.SelectionAdapter, | |
43 dwt.events.SelectionEvent; | |
44 // dwt.layout | |
45 import dwt.layout.FillLayout; | |
46 // dwt.printing | |
47 import dwt.printing.PrintDialog, | |
48 dwt.printing.Printer, | |
49 dwt.printing.PrinterData; | |
50 // dwt.dwthelper | |
51 import dwt.dwthelper.utils; | |
52 | |
53 // tango | |
54 import tango.core.Thread; | |
55 import tango.io.File; | |
56 import tango.text.Unicode; | |
57 | |
58 public void | |
59 main(String[] args){ | |
60 (new Snippet133).open(); | |
61 } | |
62 | |
63 class Snippet133{ | |
64 Display display; | |
65 Shell shell; | |
66 Text text; | |
67 Font font; | |
68 Color foregroundColor, backgroundColor; | |
69 | |
70 Printer printer; | |
71 GC gc; | |
72 FontData[] printerFontData; | |
73 RGB printerForeground, printerBackground; | |
74 | |
75 int lineHeight = 0; | |
76 int tabWidth = 0; | |
77 int leftMargin, rightMargin, topMargin, bottomMargin; | |
78 int x, y; | |
79 int index, end; | |
80 String textToPrint; | |
81 String tabs; | |
82 StringBuffer wordBuffer; | |
83 | |
84 public void | |
85 open(){ | |
86 display = new Display(); | |
87 shell = new Shell(display); | |
88 shell.setLayout(new FillLayout()); | |
89 shell.setText("Print Text"); | |
90 text = new Text(shell, DWT.BORDER | DWT.MULTI | DWT.V_SCROLL | DWT.H_SCROLL); | |
91 | |
92 Menu menuBar = new Menu(shell, DWT.BAR); | |
93 shell.setMenuBar(menuBar); | |
94 MenuItem item = new MenuItem(menuBar, DWT.CASCADE); | |
95 item.setText("&File"); | |
96 Menu fileMenu = new Menu(shell, DWT.DROP_DOWN); | |
97 item.setMenu(fileMenu); | |
98 item = new MenuItem(fileMenu, DWT.PUSH); | |
99 item.setText("&Open..."); | |
100 item.setAccelerator(DWT.CTRL + 'O'); | |
101 item.addSelectionListener(new class() SelectionAdapter{ | |
102 public void | |
103 widgetSelected(SelectionEvent event) { | |
104 menuOpen(); | |
105 } | |
106 }); | |
107 item = new MenuItem(fileMenu, DWT.PUSH); | |
108 item.setText("Font..."); | |
109 item.addSelectionListener(new class() SelectionAdapter{ | |
110 public void | |
111 widgetSelected(SelectionEvent event){ | |
112 menuFont(); | |
113 } | |
114 }); | |
115 item = new MenuItem(fileMenu, DWT.PUSH); | |
116 item.setText("Foreground Color..."); | |
117 item.addSelectionListener(new class() SelectionAdapter{ | |
118 public void | |
119 widgetSelected(SelectionEvent event){ | |
120 menuForegroundColor(); | |
121 } | |
122 }); | |
123 item = new MenuItem(fileMenu, DWT.PUSH); | |
124 item.setText("Background Color..."); | |
125 item.addSelectionListener(new class() SelectionAdapter{ | |
126 public void | |
127 widgetSelected(SelectionEvent event) { | |
128 menuBackgroundColor(); | |
129 } | |
130 }); | |
131 item = new MenuItem(fileMenu, DWT.PUSH); | |
132 item.setText("&Print..."); | |
133 item.setAccelerator(DWT.CTRL + 'P'); | |
134 item.addSelectionListener(new class() SelectionAdapter{ | |
135 public void | |
136 widgetSelected(SelectionEvent event) { | |
137 menuPrint(); | |
138 } | |
139 }); | |
140 new MenuItem(fileMenu, DWT.SEPARATOR); | |
141 item = new MenuItem(fileMenu, DWT.PUSH); | |
142 item.setText("E&xit"); | |
143 item.addSelectionListener(new class() SelectionAdapter{ | |
144 public void | |
145 widgetSelected(SelectionEvent event){ | |
146 System.exit(0); | |
147 } | |
148 }); | |
149 | |
150 shell.open(); | |
151 while (!shell.isDisposed()) { | |
152 if (!display.readAndDispatch()) display.sleep(); | |
153 } | |
154 if (font !is null) font.dispose(); | |
155 if (foregroundColor !is null) foregroundColor.dispose(); | |
156 if (backgroundColor !is null) backgroundColor.dispose(); | |
157 display.dispose(); | |
158 } | |
159 | |
160 private void | |
161 menuOpen(){ | |
162 String textString; | |
163 FileDialog dialog = new FileDialog(shell, DWT.OPEN); | |
164 dialog.setFilterExtensions(["*.java", "*.*"]); | |
165 String name = dialog.open(); | |
166 if(name is null) return; | |
167 | |
168 try{ | |
169 scope File file = new File(name); | |
170 try{ | |
171 textString = cast(char[])file.read; | |
172 } | |
173 catch (IOException e){ | |
174 MessageBox box = new MessageBox(shell, DWT.ICON_ERROR); | |
175 box.setMessage("Error reading file:\n" ~ name); | |
176 box.open(); | |
177 return; | |
178 } | |
179 } | |
180 catch(Exception e){ | |
181 MessageBox box = new MessageBox(shell, DWT.ICON_ERROR); | |
182 box.setMessage("File not found:\n" ~ name); | |
183 box.open(); | |
184 return; | |
185 } | |
186 text.setText(textString); | |
187 } | |
188 | |
189 private void | |
190 menuFont(){ | |
191 FontDialog fontDialog = new FontDialog(shell); | |
192 fontDialog.setFontList(text.getFont().getFontData()); | |
193 FontData fontData = fontDialog.open(); | |
194 if(fontData !is null){ | |
195 if(font !is null) font.dispose(); | |
196 font = new Font(display, fontData); | |
197 text.setFont(font); | |
198 } | |
199 } | |
200 | |
201 private void | |
202 menuForegroundColor(){ | |
203 ColorDialog colorDialog = new ColorDialog(shell); | |
204 colorDialog.setRGB(text.getForeground().getRGB()); | |
205 RGB rgb = colorDialog.open(); | |
206 if(rgb !is null){ | |
207 if(foregroundColor !is null) foregroundColor.dispose(); | |
208 foregroundColor = new Color(display, rgb); | |
209 text.setForeground(foregroundColor); | |
210 } | |
211 } | |
212 | |
213 private void | |
214 menuBackgroundColor(){ | |
215 ColorDialog colorDialog = new ColorDialog(shell); | |
216 colorDialog.setRGB(text.getBackground().getRGB()); | |
217 RGB rgb = colorDialog.open(); | |
218 if(rgb !is null){ | |
219 if(backgroundColor !is null) backgroundColor.dispose(); | |
220 backgroundColor = new Color(display, rgb); | |
221 text.setBackground(backgroundColor); | |
222 } | |
223 } | |
224 | |
225 private void | |
226 menuPrint(){ | |
227 PrintDialog dialog = new PrintDialog(shell, DWT.NONE); | |
228 PrinterData data = dialog.open(); | |
229 if(data is null) return; | |
230 if(data.printToFile){ | |
231 data.fileName = "print.out"; // you probably want to ask the user for a filename | |
232 } | |
233 | |
234 /* Get the text to print from the Text widget (you could get it from anywhere, i.e. your java model) */ | |
235 textToPrint = text.getText(); | |
236 | |
237 /* Get the font & foreground & background data. */ | |
238 printerFontData = text.getFont().getFontData(); | |
239 printerForeground = text.getForeground().getRGB(); | |
240 printerBackground = text.getBackground().getRGB(); | |
241 | |
242 /* Do the printing in a background thread so that spooling does not freeze the UI. */ | |
243 printer = new Printer(data); | |
244 Thread printingThread = new class ("Printing") Thread{ | |
245 private void | |
246 run(){ | |
247 print(printer); | |
248 printer.dispose(); | |
249 } | |
250 public | |
251 this(char[] o_name){ | |
252 this.name = o_name; | |
253 super(&run); | |
254 } | |
255 }; | |
256 printingThread.start(); | |
257 } | |
258 | |
259 private void | |
260 print(Printer printer){ | |
261 if(printer.startJob("Text")){ // the string is the job name - shows up in the printer's job list | |
262 Rectangle clientArea = printer.getClientArea(); | |
263 Rectangle trim = printer.computeTrim(0, 0, 0, 0); | |
264 Point dpi = printer.getDPI(); | |
265 leftMargin = dpi.x + trim.x; // one inch from left side of paper | |
266 rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one inch from right side of paper | |
267 topMargin = dpi.y + trim.y; // one inch from top edge of paper | |
268 bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one inch from bottom edge of paper | |
269 | |
270 /* Create a buffer for computing tab width. */ | |
271 int tabSize = 4; // is tab width a user setting in your UI? | |
272 StringBuffer tabBuffer = new StringBuffer(tabSize); | |
273 for (int i = 0; i < tabSize; i++) tabBuffer.append(' '); | |
274 tabs = tabBuffer.toString(); | |
275 | |
276 /* Create printer GC, and create and set the printer font & foreground color. */ | |
277 gc = new GC(printer); | |
278 Font printerFont = new Font(printer, printerFontData); | |
279 Color printerForegroundColor = new Color(printer, printerForeground); | |
280 Color printerBackgroundColor = new Color(printer, printerBackground); | |
281 | |
282 gc.setFont(printerFont); | |
283 gc.setForeground(printerForegroundColor); | |
284 gc.setBackground(printerBackgroundColor); | |
285 tabWidth = gc.stringExtent(tabs).x; | |
286 lineHeight = gc.getFontMetrics().getHeight(); | |
287 | |
288 /* Print text to current gc using word wrap */ | |
289 printText(); | |
290 printer.endJob(); | |
291 | |
292 /* Cleanup graphics resources used in printing */ | |
293 printerFont.dispose(); | |
294 printerForegroundColor.dispose(); | |
295 printerBackgroundColor.dispose(); | |
296 gc.dispose(); | |
297 } | |
298 } | |
299 | |
300 private void | |
301 printText(){ | |
302 printer.startPage(); | |
303 wordBuffer = new StringBuffer(); | |
304 x = leftMargin; | |
305 y = topMargin; | |
306 index = 0; | |
307 end = textToPrint.length; | |
308 while(index < end){ | |
309 char c = textToPrint.charAt(index); | |
310 index++; | |
311 if(c != 0){ | |
312 if(c == 0x0a || c == 0x0d){ | |
313 if(c == 0x0d && index < end && textToPrint.charAt(index) == 0x0a){ | |
314 index++; // if this is cr-lf, skip the lf | |
315 } | |
316 printWordBuffer(); | |
317 newline(); | |
318 } | |
319 else{ | |
320 if(c != '\t'){ | |
321 wordBuffer.append(c); | |
322 } | |
323 if(isPrintable(c)){ | |
324 printWordBuffer(); | |
325 if (c == '\t'){ | |
326 x += tabWidth; | |
327 } | |
328 } | |
329 } | |
330 } | |
331 } | |
332 if (y + lineHeight <= bottomMargin) { | |
333 printer.endPage(); | |
334 } | |
335 } | |
336 | |
337 private void | |
338 printWordBuffer(){ | |
339 if(wordBuffer.length > 0){ | |
340 String word = wordBuffer.toString(); | |
341 int wordWidth = gc.stringExtent(word).x; | |
342 if(x + wordWidth > rightMargin){ | |
343 /* word doesn't fit on current line, so wrap */ | |
344 newline(); | |
345 } | |
346 gc.drawString(word, x, y, false); | |
347 x += wordWidth; | |
348 wordBuffer = new StringBuffer(); | |
349 } | |
350 } | |
351 | |
352 private void | |
353 newline(){ | |
354 x = leftMargin; | |
355 y += lineHeight; | |
356 if(y + lineHeight > bottomMargin){ | |
357 printer.endPage(); | |
358 if(index + 1 < end){ | |
359 y = topMargin; | |
360 printer.startPage(); | |
361 } | |
362 } | |
363 } | |
364 } | |
365 |