# HG changeset patch # User Adam Chrapkowski # Date 1220462844 -7200 # Node ID 513c72ba21f1a3b7f74067af4cb1e2e387221d24 # Parent 0beffa2869ad9462aa33a63b5ddb30701f61b93d added snippets\priniting\Snippet132.d snippets\priniting\Snippet133.d changed snippets\dsss.conf diff -r 0beffa2869ad -r 513c72ba21f1 snippets/dsss.conf --- a/snippets/dsss.conf Tue Sep 02 01:05:38 2008 +0200 +++ b/snippets/dsss.conf Wed Sep 03 19:27:24 2008 +0200 @@ -91,6 +91,8 @@ [menu/Snippet286.d] [menu/Snippet29.d] [menu/Snippet97.d] +[printing/Snippet132.d] +[printing/Snippet133.d] [program/Snippet32.d] [sash/Snippet107.d] [sashform/Snippet109.d] diff -r 0beffa2869ad -r 513c72ba21f1 snippets/printing/Snippet132.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snippets/printing/Snippet132.d Wed Sep 03 19:27:24 2008 +0200 @@ -0,0 +1,77 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +module printing.Snippet132; + +/* + * Printing example snippet: print "Hello World!" in black, outlined in red, to default printer + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ + +// dwt +import dwt.DWT; +// dwt.widgets +import dwt.widgets.Display, + dwt.widgets.MessageBox, + dwt.widgets.Shell; +// dwt.graphics +import dwt.graphics.Color, + dwt.graphics.GC, + dwt.graphics.Rectangle; +// dwt.printing +import dwt.printing.PrintDialog, + dwt.printing.Printer, + dwt.printing.PrinterData; +// dwthelper +import dwt.dwthelper.utils; + +public void +main(String [] args){ + Display display = new Display(); + Shell shell = new Shell(display); + shell.open(); + PrinterData data = Printer.getDefaultPrinterData(); + if(data is null){ + MessageBox.showWarning("Warning: No default printer."); + return; + } + Printer printer = new Printer(data); + if(printer.startJob("DWT Printing Snippet")){ + Color black = printer.getSystemColor(DWT.COLOR_BLACK); + Color white = printer.getSystemColor(DWT.COLOR_WHITE); + Color red = printer.getSystemColor(DWT.COLOR_RED); + Rectangle trim = printer.computeTrim(0, 0, 0, 0); + Point dpi = printer.getDPI(); + int leftMargin = dpi.x + trim.x; // one inch from left side of paper + int topMargin = dpi.y / 2 + trim.y; // one-half inch from top edge of paper + GC gc = new GC(printer); + if(printer.startPage()){ + gc.setBackground(white); + gc.setForeground(black); + String testString = "Hello World!"; + Point extent = gc.stringExtent(testString); + gc.drawString(testString, leftMargin, topMargin); + gc.setForeground(red); + gc.drawRectangle(leftMargin, topMargin, extent.x, extent.y); + printer.endPage(); + } + gc.dispose(); + printer.endJob(); + } + printer.dispose(); + while(!shell.isDisposed()){ + if(!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} \ No newline at end of file diff -r 0beffa2869ad -r 513c72ba21f1 snippets/printing/Snippet133.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snippets/printing/Snippet133.d Wed Sep 03 19:27:24 2008 +0200 @@ -0,0 +1,365 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +module printing.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/ + */ + +// dwt +import dwt.DWT; +// dwt.graphics +import dwt.graphics.Color, + dwt.graphics.Font, + dwt.graphics.FontData, + dwt.graphics.GC, + dwt.graphics.Rectangle, + dwt.graphics.RGB; +// dwt.widgets +import dwt.widgets.Display, + dwt.widgets.ColorDialog, + dwt.widgets.FileDialog, + dwt.widgets.FontDialog, + dwt.widgets.Menu, + dwt.widgets.MenuItem, + dwt.widgets.MessageBox, + dwt.widgets.Shell, + dwt.widgets.Text; +// dwt.events +import dwt.events.SelectionAdapter, + dwt.events.SelectionEvent; +// dwt.layout +import dwt.layout.FillLayout; +// dwt.printing +import dwt.printing.PrintDialog, + dwt.printing.Printer, + dwt.printing.PrinterData; +// dwt.dwthelper +import dwt.dwthelper.utils; + +// tango +import tango.core.Thread; +import tango.io.File; +import tango.text.Unicode; + +public void +main(String[] args){ + (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, DWT.BORDER | DWT.MULTI | DWT.V_SCROLL | DWT.H_SCROLL); + + Menu menuBar = new Menu(shell, DWT.BAR); + shell.setMenuBar(menuBar); + MenuItem item = new MenuItem(menuBar, DWT.CASCADE); + item.setText("&File"); + Menu fileMenu = new Menu(shell, DWT.DROP_DOWN); + item.setMenu(fileMenu); + item = new MenuItem(fileMenu, DWT.PUSH); + item.setText("&Open..."); + item.setAccelerator(DWT.CTRL + 'O'); + item.addSelectionListener(new class() SelectionAdapter{ + public void + widgetSelected(SelectionEvent event) { + menuOpen(); + } + }); + item = new MenuItem(fileMenu, DWT.PUSH); + item.setText("Font..."); + item.addSelectionListener(new class() SelectionAdapter{ + public void + widgetSelected(SelectionEvent event){ + menuFont(); + } + }); + item = new MenuItem(fileMenu, DWT.PUSH); + item.setText("Foreground Color..."); + item.addSelectionListener(new class() SelectionAdapter{ + public void + widgetSelected(SelectionEvent event){ + menuForegroundColor(); + } + }); + item = new MenuItem(fileMenu, DWT.PUSH); + item.setText("Background Color..."); + item.addSelectionListener(new class() SelectionAdapter{ + public void + widgetSelected(SelectionEvent event) { + menuBackgroundColor(); + } + }); + item = new MenuItem(fileMenu, DWT.PUSH); + item.setText("&Print..."); + item.setAccelerator(DWT.CTRL + 'P'); + item.addSelectionListener(new class() SelectionAdapter{ + public void + widgetSelected(SelectionEvent event) { + menuPrint(); + } + }); + new MenuItem(fileMenu, DWT.SEPARATOR); + item = new MenuItem(fileMenu, DWT.PUSH); + item.setText("E&xit"); + item.addSelectionListener(new class() SelectionAdapter{ + 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, DWT.OPEN); + dialog.setFilterExtensions(["*.java", "*.*"]); + String name = dialog.open(); + if(name is null) return; + + try{ + scope File file = new File(name); + try{ + textString = cast(char[])file.read; + } + catch (IOException e){ + MessageBox box = new MessageBox(shell, DWT.ICON_ERROR); + box.setMessage("Error reading file:\n" ~ name); + box.open(); + return; + } + } + catch(Exception e){ + MessageBox box = new MessageBox(shell, DWT.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, DWT.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(char[] 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 = 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(); + } + } + } +} +