# HG changeset patch # User Bill Baxter # Date 1207498864 -32400 # Node ID 6b88d308c210aa8252746b9d6c3e03acf6d5b9b5 # Parent e1edfebec7fa60973204b36843d12d4fb50128d8 Ports of several gc Snippets: 10,66,207,215 215 has a crash bug currently. diff -r e1edfebec7fa -r 6b88d308c210 dsss.conf --- a/dsss.conf Mon Apr 07 00:36:08 2008 +0900 +++ b/dsss.conf Mon Apr 07 01:21:04 2008 +0900 @@ -39,6 +39,10 @@ [dwtsnippets/directorydialog/Snippet33.d] [dwtsnippets/expandbar/Snippet223.d] [dwtsnippets/filedialog/Snippet72.d] +[dwtsnippets/gc/Snippet10.d] +[dwtsnippets/gc/Snippet66.d] +[dwtsnippets/gc/Snippet207.d] +[dwtsnippets/gc/Snippet215.d] [dwtsnippets/menu/Snippet29.d] [dwtsnippets/menu/Snippet97.d] [dwtsnippets/sash/Snippet107.d] diff -r e1edfebec7fa -r 6b88d308c210 dwtsnippets/gc/Snippet10.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/gc/Snippet10.d Mon Apr 07 01:21:04 2008 +0900 @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2000, 2005 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 + * Port to the D programming language: + * Bill Baxter + *******************************************************************************/ +module dwtsnippets.gc.Snippet10; + +/* + * Draw using transformations, paths and alpha blending + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + * + * @since 3.1 + */ +import dwt.DWT; +import dwt.graphics.GC; +import dwt.graphics.Path; +import dwt.graphics.Transform; +import dwt.graphics.Font; +import dwt.graphics.FontData; +import dwt.graphics.Image; +import dwt.graphics.Rectangle; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Listener; +import dwt.widgets.Event; + + +void main() { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setText("Advanced Graphics"); + FontData fd = shell.getFont().getFontData()[0]; + Font font = new Font(display, fd.getName(), 60., DWT.BOLD | DWT.ITALIC); + Image image = new Image(display, 640, 480); + Rectangle rect = image.getBounds(); + GC gc = new GC(image); + gc.setBackground(display.getSystemColor(DWT.COLOR_RED)); + gc.fillOval(rect.x, rect.y, rect.width, rect.height); + gc.dispose(); + shell.addListener(DWT.Paint, new class() Listener { + public void handleEvent(Event event) { + GC gc = event.gc; + Transform tr = new Transform(display); + tr.translate(50, 120); + tr.rotate(-30); + gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, rect.width / 2, rect.height / 2); + gc.setAlpha(100); + gc.setTransform(tr); + Path path = new Path(display); + path.addString("DWT", 0, 0, font); + gc.setBackground(display.getSystemColor(DWT.COLOR_GREEN)); + gc.setForeground(display.getSystemColor(DWT.COLOR_BLUE)); + gc.fillPath(path); + gc.drawPath(path); + tr.dispose(); + path.dispose(); + } + }); + shell.setSize(shell.computeSize(rect.width / 2, rect.height / 2)); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + image.dispose(); + font.dispose(); + display.dispose(); +} + diff -r e1edfebec7fa -r 6b88d308c210 dwtsnippets/gc/Snippet207.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/gc/Snippet207.d Mon Apr 07 01:21:04 2008 +0900 @@ -0,0 +1,129 @@ +/******************************************************************************* + * Copyright (c) 2000, 2005 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 + * Port to the D programming language: + * Bill Baxter + *******************************************************************************/ +module dwtsnippets.gc.Snippet207; + +/* + * Use transformation matrices to reflect, rotate and shear images + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.events.PaintEvent; +import dwt.events.PaintListener; +import dwt.graphics.GC; +import dwt.graphics.Transform; +import dwt.graphics.Font; +import dwt.graphics.Rectangle; +import dwt.graphics.Image; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Canvas; + +import Math=tango.math.Math; + +void main() { + Display display = new Display(); + + Image image = new Image(display, 110, 60); + GC gc = new GC(image); + Font font = new Font(display, "Times", 30., DWT.BOLD); + gc.setFont(font); + gc.setBackground(display.getSystemColor(DWT.COLOR_RED)); + gc.fillRectangle(0, 0, 110, 60); + gc.setForeground(display.getSystemColor(DWT.COLOR_WHITE)); + gc.drawText("DWT", 10, 10, true); + font.dispose(); + gc.dispose(); + + Rectangle rect = image.getBounds(); + Shell shell = new Shell(display); + shell.setText("Matrix Tranformations"); + shell.setLayout(new FillLayout()); + Canvas canvas = new Canvas(shell, DWT.DOUBLE_BUFFERED); + canvas.addPaintListener(new class() PaintListener { + public void paintControl(PaintEvent e) { + GC gc = e.gc; + gc.setAdvanced(true); + if (!gc.getAdvanced()){ + gc.drawText("Advanced graphics not supported", 30, 30, true); + return; + } + + // Original image + int x = 30, y = 30; + gc.drawImage(image, x, y); + x += rect.width + 30; + + Transform transform = new Transform(display); + + // Note that the tranform is applied to the whole GC therefore + // the coordinates need to be adjusted too. + + // Reflect around the y axis. + transform.setElements(-1, 0, 0, 1, 0 ,0); + gc.setTransform(transform); + gc.drawImage(image, -1*x-rect.width, y); + + x = 30; y += rect.height + 30; + + // Reflect around the x axis. + transform.setElements(1, 0, 0, -1, 0, 0); + gc.setTransform(transform); + gc.drawImage(image, x, -1*y-rect.height); + + x += rect.width + 30; + + // Reflect around the x and y axes + transform.setElements(-1, 0, 0, -1, 0, 0); + gc.setTransform(transform); + gc.drawImage(image, -1*x-rect.width, -1*y-rect.height); + + x = 30; y += rect.height + 30; + + // Shear in the x-direction + transform.setElements(1, 0, -1, 1, 0, 0); + gc.setTransform(transform); + gc.drawImage(image, 300, y); + + // Shear in y-direction + transform.setElements(1, -1, 0, 1, 0, 0); + gc.setTransform(transform); + gc.drawImage(image, 150, 475); + + // Rotate by 45 degrees + float cos45 = Math.cos(45); + float sin45 = Math.sin(45); + transform.setElements(cos45, sin45, -sin45, cos45, 0, 0); + gc.setTransform(transform); + gc.drawImage(image, 350, 100); + + transform.dispose(); + } + }); + + shell.setSize(350, 550); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + image.dispose(); + display.dispose(); +} + + + + + diff -r e1edfebec7fa -r 6b88d308c210 dwtsnippets/gc/Snippet215.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/gc/Snippet215.d Mon Apr 07 01:21:04 2008 +0900 @@ -0,0 +1,78 @@ +/******************************************************************************* + * 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 + * Port to the D programming language: + * Bill Baxter + *******************************************************************************/ +module dwtsnippets.gc.Snippet215; + +/* + * GC example snippet: take a screen shot with a GC + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.graphics.GC; +import dwt.graphics.Image; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Listener; +import dwt.widgets.Event; +import dwt.widgets.Button; +import dwt.widgets.Canvas; +import dwt.custom.ScrolledComposite; +import dwt.layout.FillLayout; +import dwt.events.PaintListener; + +void main() { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + Button button = new Button(shell, DWT.PUSH); + button.setText("Capture"); + button.addListener(DWT.Selection, new class() Listener { + public void handleEvent(Event event) { + + /* Take the screen shot */ + GC gc = new GC(display); + final Image image = new Image(display, display.getBounds()); + gc.copyArea(image, 0, 0); + gc.dispose(); + + Shell popup = new Shell(shell, DWT.SHELL_TRIM); + popup.setLayout(new FillLayout()); + popup.setText("Image"); + popup.setBounds(50, 50, 200, 200); + popup.addListener(DWT.Close, new class() Listener { + public void handleEvent(Event e) { + image.dispose(); + } + }); + + ScrolledComposite sc = new ScrolledComposite (popup, DWT.V_SCROLL | DWT.H_SCROLL); + Canvas canvas = new Canvas(sc, DWT.NONE); + sc.setContent(canvas); + canvas.setBounds(display.getBounds ()); + canvas.addPaintListener(new class() PaintListener { + public void paintControl(PaintEvent e) { + e.gc.drawImage(image, 0, 0); + } + }); + popup.open(); + } + }); + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} + diff -r e1edfebec7fa -r 6b88d308c210 dwtsnippets/gc/Snippet66.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/gc/Snippet66.d Mon Apr 07 01:21:04 2008 +0900 @@ -0,0 +1,56 @@ +/******************************************************************************* + * 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 + * Port to the D programming language: + * Bill Baxter + *******************************************************************************/ +module dwtsnippets.gc.Snippet66; + +/* + * GC example snippet: implement a simple scribble program + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.graphics.GC; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Listener; +import dwt.widgets.Event; + +void main () { + Display display = new Display (); + final Shell shell = new Shell (display); + Listener listener = new class() Listener { + int lastX = 0, lastY = 0; + public void handleEvent (Event event) { + switch (event.type) { + case DWT.MouseMove: + if ((event.stateMask & DWT.BUTTON1) == 0) break; + GC gc = new GC (shell); + gc.drawLine (lastX, lastY, event.x, event.y); + gc.dispose (); + //FALL THROUGH + case DWT.MouseDown: + lastX = event.x; + lastY = event.y; + break; + } + } + }; + shell.addListener (DWT.MouseDown, listener); + shell.addListener (DWT.MouseMove, listener); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +