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