-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathSnippet133.d
More file actions
executable file
·407 lines (377 loc) · 13.9 KB
/
Copy pathSnippet133.d
File metadata and controls
executable file
·407 lines (377 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env dub
/+
dub.sdl:
name "snippet133"
dependency "dwt" path="../../../../../../"
lflags "/subsystem:console:4" platform="x86_omf"
libs \
"atk-1.0" \
"cairo" \
"dl" \
"fontconfig" \
"gdk-x11-2.0" \
"gdk_pixbuf-2.0" \
"glib-2.0" \
"gmodule-2.0" \
"gnomeui-2" \
"gnomevfs-2" \
"gobject-2.0" \
"gthread-2.0" \
"gtk-x11-2.0" \
"pango-1.0" \
"pangocairo-1.0" \
"X11" \
"Xcomposite" \
"Xcursor" \
"Xdamage" \
"Xext" \
"Xfixes" \
"Xi" \
"Xinerama" \
"Xrandr" \
"Xrender" \
"Xtst" \
platform="linux"
+/
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* D Port:
* Adam Chrapkowski <adam DOT chrapkowski AT gmail DOT com>
*******************************************************************************/
module org.eclipse.swt.snippets.Snippet133;
/*
* Printing example snippet: print text to printer, with word wrap and pagination
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
// org.eclipse.swt
import org.eclipse.swt.SWT;
// org.eclipse.swt.graphics
import org.eclipse.swt.graphics.Color,
org.eclipse.swt.graphics.Font,
org.eclipse.swt.graphics.FontData,
org.eclipse.swt.graphics.GC,
org.eclipse.swt.graphics.Rectangle,
org.eclipse.swt.graphics.RGB;
// org.eclipse.swt.widgets
import org.eclipse.swt.widgets.Display,
org.eclipse.swt.widgets.ColorDialog,
org.eclipse.swt.widgets.FileDialog,
org.eclipse.swt.widgets.FontDialog,
org.eclipse.swt.widgets.Menu,
org.eclipse.swt.widgets.MenuItem,
org.eclipse.swt.widgets.MessageBox,
org.eclipse.swt.widgets.Shell,
org.eclipse.swt.widgets.Text;
// org.eclipse.swt.events
import org.eclipse.swt.events.SelectionAdapter,
org.eclipse.swt.events.SelectionEvent;
// org.eclipse.swt.layout
import org.eclipse.swt.layout.FillLayout;
// org.eclipse.swt.printing
import org.eclipse.swt.printing.PrintDialog,
org.eclipse.swt.printing.Printer,
org.eclipse.swt.printing.PrinterData;
// java
import java.lang.all;
// tango
//import tango.core.Thread;
version(Tango){
import tango.io.device.File;
import tango.text.Unicode;
} else { // Phobos
import std.file;
import std.ascii; //FIXME: no Unicode support in std.ascii.isPrintable
}
void main(){
(new Snippet133).open();
}
class Snippet133{
Display display;
Shell shell;
Text text;
Font font;
Color foregroundColor, backgroundColor;
Printer printer;
GC gc;
FontData[] printerFontData;
RGB printerForeground, printerBackground;
int lineHeight = 0;
int tabWidth = 0;
int leftMargin, rightMargin, topMargin, bottomMargin;
int x, y;
int index, end;
String textToPrint;
String tabs;
StringBuffer wordBuffer;
public void
open(){
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Print Text");
text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
Menu menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
MenuItem item = new MenuItem(menuBar, SWT.CASCADE);
item.setText("&File");
Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(fileMenu);
item = new MenuItem(fileMenu, SWT.PUSH);
item.setText("&Open...");
item.setAccelerator(SWT.CTRL + 'O');
item.addSelectionListener(new class SelectionAdapter{
override
public void widgetSelected(SelectionEvent event) {
menuOpen();
}
});
item = new MenuItem(fileMenu, SWT.PUSH);
item.setText("Font...");
item.addSelectionListener(new class SelectionAdapter{
override
public void widgetSelected(SelectionEvent event){
menuFont();
}
});
item = new MenuItem(fileMenu, SWT.PUSH);
item.setText("Foreground Color...");
item.addSelectionListener(new class SelectionAdapter{
override
public void widgetSelected(SelectionEvent event){
menuForegroundColor();
}
});
item = new MenuItem(fileMenu, SWT.PUSH);
item.setText("Background Color...");
item.addSelectionListener(new class SelectionAdapter{
override
public void widgetSelected(SelectionEvent event) {
menuBackgroundColor();
}
});
item = new MenuItem(fileMenu, SWT.PUSH);
item.setText("&Print...");
item.setAccelerator(SWT.CTRL + 'P');
item.addSelectionListener(new class SelectionAdapter{
override
public void widgetSelected(SelectionEvent event) {
menuPrint();
}
});
new MenuItem(fileMenu, SWT.SEPARATOR);
item = new MenuItem(fileMenu, SWT.PUSH);
item.setText("E&xit");
item.addSelectionListener(new class SelectionAdapter{
override
public void widgetSelected(SelectionEvent event){
System.exit(0);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
if (font !is null) font.dispose();
if (foregroundColor !is null) foregroundColor.dispose();
if (backgroundColor !is null) backgroundColor.dispose();
display.dispose();
}
private void menuOpen(){
String textString;
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterExtensions(["*.java", "*.*"]);
String name = dialog.open();
if(name is null) return;
try{
try{
version(Tango){
textString = cast(char[])File.get(name);
} else { // Phobos
textString = cast(String)std.file.read(name);
}
}
catch (IOException e){
MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
box.setMessage("Error reading file:\n" ~ name);
box.open();
return;
}
}
catch(Exception e){
MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
box.setMessage("File not found:\n" ~ name);
box.open();
return;
}
text.setText(textString);
}
private void
menuFont(){
FontDialog fontDialog = new FontDialog(shell);
fontDialog.setFontList(text.getFont().getFontData());
FontData fontData = fontDialog.open();
if(fontData !is null){
if(font !is null) font.dispose();
font = new Font(display, fontData);
text.setFont(font);
}
}
private void
menuForegroundColor(){
ColorDialog colorDialog = new ColorDialog(shell);
colorDialog.setRGB(text.getForeground().getRGB());
RGB rgb = colorDialog.open();
if(rgb !is null){
if(foregroundColor !is null) foregroundColor.dispose();
foregroundColor = new Color(display, rgb);
text.setForeground(foregroundColor);
}
}
private void
menuBackgroundColor(){
ColorDialog colorDialog = new ColorDialog(shell);
colorDialog.setRGB(text.getBackground().getRGB());
RGB rgb = colorDialog.open();
if(rgb !is null){
if(backgroundColor !is null) backgroundColor.dispose();
backgroundColor = new Color(display, rgb);
text.setBackground(backgroundColor);
}
}
private void
menuPrint(){
PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
PrinterData data = dialog.open();
if(data is null) return;
if(data.printToFile){
data.fileName = "print.out"; // you probably want to ask the user for a filename
}
/* Get the text to print from the Text widget (you could get it from anywhere, i.e. your java model) */
textToPrint = text.getText();
/* Get the font & foreground & background data. */
printerFontData = text.getFont().getFontData();
printerForeground = text.getForeground().getRGB();
printerBackground = text.getBackground().getRGB();
/* Do the printing in a background thread so that spooling does not freeze the UI. */
printer = new Printer(data);
Thread printingThread = new class ("Printing") Thread{
private void
run(){
print(printer);
printer.dispose();
}
public
this(String o_name){
//this.name = o_name;
super(&run);
}
};
printingThread.start();
}
private void
print(Printer printer){
if(printer.startJob("Text")){ // the string is the job name - shows up in the printer's job list
Rectangle clientArea = printer.getClientArea();
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
Point dpi = printer.getDPI();
leftMargin = dpi.x + trim.x; // one inch from left side of paper
rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one inch from right side of paper
topMargin = dpi.y + trim.y; // one inch from top edge of paper
bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one inch from bottom edge of paper
/* Create a buffer for computing tab width. */
int tabSize = 4; // is tab width a user setting in your UI?
StringBuffer tabBuffer = new StringBuffer(tabSize);
for (int i = 0; i < tabSize; i++) tabBuffer.append(' ');
tabs = tabBuffer.toString();
/* Create printer GC, and create and set the printer font & foreground color. */
gc = new GC(printer);
Font printerFont = new Font(printer, printerFontData);
Color printerForegroundColor = new Color(printer, printerForeground);
Color printerBackgroundColor = new Color(printer, printerBackground);
gc.setFont(printerFont);
gc.setForeground(printerForegroundColor);
gc.setBackground(printerBackgroundColor);
tabWidth = gc.stringExtent(tabs).x;
lineHeight = gc.getFontMetrics().getHeight();
/* Print text to current gc using word wrap */
printText();
printer.endJob();
/* Cleanup graphics resources used in printing */
printerFont.dispose();
printerForegroundColor.dispose();
printerBackgroundColor.dispose();
gc.dispose();
}
}
private void
printText(){
printer.startPage();
wordBuffer = new StringBuffer();
x = leftMargin;
y = topMargin;
index = 0;
end = cast(int)textToPrint.length;
while(index < end){
char c = textToPrint.charAt(index);
index++;
if(c != 0){
if(c == 0x0a || c == 0x0d){
if(c == 0x0d && index < end && textToPrint.charAt(index) == 0x0a){
index++; // if this is cr-lf, skip the lf
}
printWordBuffer();
newline();
}
else{
if(c != '\t'){
wordBuffer.append(c);
}
if(isPrintable(c)){
printWordBuffer();
if (c == '\t'){
x += tabWidth;
}
}
}
}
}
if (y + lineHeight <= bottomMargin) {
printer.endPage();
}
}
private void
printWordBuffer(){
if(wordBuffer.length > 0){
String word = wordBuffer.toString();
int wordWidth = gc.stringExtent(word).x;
if(x + wordWidth > rightMargin){
/* word doesn't fit on current line, so wrap */
newline();
}
gc.drawString(word, x, y, false);
x += wordWidth;
wordBuffer = new StringBuffer();
}
}
private void
newline(){
x = leftMargin;
y += lineHeight;
if(y + lineHeight > bottomMargin){
printer.endPage();
if(index + 1 < end){
y = topMargin;
printer.startPage();
}
}
}
}