Mercurial > projects > dwt-samples
changeset 90:29bf9ba4756b
Port of all canvas snippets
author | Bill Baxter <bill@billbaxter.com> |
---|---|
date | Tue, 20 May 2008 12:10:54 +0900 |
parents | cbba80cceb7a |
children | 961ca8a76cad 37fab5543b9a |
files | snippets/canvas/Snippet21.d snippets/canvas/Snippet245.d snippets/canvas/Snippet275.d snippets/canvas/Snippet290.d snippets/canvas/Snippet48.d snippets/dsss.conf |
diffstat | 6 files changed, 406 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snippets/canvas/Snippet21.d Tue May 20 12:10:54 2008 +0900 @@ -0,0 +1,100 @@ +/******************************************************************************* + * 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: + * Bill Baxter <wbaxter> at gmail com + *******************************************************************************/ +module snippets.canvas.Snippet21; + +/* + * Canvas example snippet: implement tab traversal (behave like a tab group) + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.graphics.Color; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Listener; +import dwt.widgets.Event; +import dwt.widgets.Button; +import dwt.widgets.Text; +import dwt.widgets.Canvas; + +import tango.io.Stdout; + +import dwt.dwthelper.utils; + +void main () { + Display display = new Display (); + final Color red = display.getSystemColor (DWT.COLOR_RED); + final Color blue = display.getSystemColor (DWT.COLOR_BLUE); + Shell shell = new Shell (display); + Button b = new Button (shell, DWT.PUSH); + b.setBounds (10, 10, 100, 32); + b.setText ("Button"); + shell.setDefaultButton (b); + final Canvas c = new Canvas (shell, DWT.BORDER); + c.setBounds (10, 50, 100, 32); + + void onTraverse(Event e, Canvas c) { + switch (e.detail) { + /* Do tab group traversal */ + case DWT.TRAVERSE_ESCAPE: + case DWT.TRAVERSE_RETURN: + case DWT.TRAVERSE_TAB_NEXT: + case DWT.TRAVERSE_TAB_PREVIOUS: + case DWT.TRAVERSE_PAGE_NEXT: + case DWT.TRAVERSE_PAGE_PREVIOUS: + e.doit = true; + break; + } + } + + void onFocusIn(Event e, Canvas c) { + c.setBackground (red); + } + + void onFocusOut(Event e, Canvas c) { + c.setBackground (blue); + } + + void onKeyDown (Event e, Canvas c) { + Stdout("KEY").newline; + for (int i=0; i<64; i++) { + Color c1 = red, c2 = blue; + if (c.isFocusControl ()) { + c1 = blue; c2 = red; + } + c.setBackground (c1); + c.update (); + c.setBackground (c2); + } + } + + c.addListener (DWT.Traverse, dgListener(&onTraverse, c)); + c.addListener (DWT.FocusIn, dgListener(&onFocusIn, c)); + c.addListener (DWT.FocusOut, dgListener(&onFocusOut, c)); + c.addListener (DWT.KeyDown, dgListener(&onKeyDown, c)); + + Text t = new Text (shell, DWT.SINGLE | DWT.BORDER); + t.setBounds (10, 85, 100, 32); + + Text r = new Text (shell, DWT.MULTI | DWT.BORDER); + r.setBounds (10, 120, 100, 32); + + c.setFocus (); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snippets/canvas/Snippet245.d Tue May 20 12:10:54 2008 +0900 @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 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: + * Bill Baxter <wbaxter> at gmail com + *******************************************************************************/ +module snippets.canvas.Snippet245; + +/* + * Canvas snippet: paint a circle in a canvas + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ + +import dwt.graphics.Rectangle; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.events.PaintListener; +import dwt.events.PaintEvent; + +void main() +{ + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.addPaintListener(new class(shell) PaintListener { + Shell shell; + this(Shell s) { this.shell = s; } + public void paintControl(PaintEvent event) { + Rectangle rect = shell.getClientArea(); + event.gc.drawOval(0, 0, rect.width - 1, rect.height - 1); + } + }); + shell.setBounds(10, 10, 200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snippets/canvas/Snippet275.d Tue May 20 12:10:54 2008 +0900 @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2000, 2007 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: + * Bill Baxter <wbaxter> at gmail com + *******************************************************************************/ +module snippets.canvas.Snippet275; + +/* + * Canvas snippet: update a portion of a Canvas frequently + * + * 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.Color; +import dwt.graphics.Point; +import dwt.graphics.Image; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Event; +import dwt.widgets.Listener; +import dwt.widgets.Canvas; + +import dwt.dwthelper.System; +import dwt.dwthelper.utils; +import dwt.dwthelper.Runnable; + +import tango.util.Convert; + +static String value; +public static void main () { + final int INTERVAL = 888; + final Display display = new Display (); + final Image image = new Image (display, 750, 750); + GC gc = new GC (image); + gc.setBackground (display.getSystemColor (DWT.COLOR_RED)); + gc.fillRectangle (image.getBounds ()); + gc.dispose (); + + Shell shell = new Shell (display); + shell.setBounds (10, 10, 790, 790); + final Canvas canvas = new Canvas (shell, DWT.NONE); + canvas.setBounds (10, 10, 750, 750); + + void onPaint (Event event) { + value = to!(char[])(System.currentTimeMillis ()); + event.gc.drawImage (image, 0, 0); + event.gc.drawString (value, 10, 10, true); + } + canvas.addListener (DWT.Paint, dgListener(&onPaint)); + + display.timerExec (INTERVAL, new class Runnable { + void run () { + if (canvas.isDisposed ()) return; + // canvas.redraw (); // <-- bad, damages more than is needed + GC gc = new GC (canvas); + Point extent = gc.stringExtent (value ~ '0'); + gc.dispose (); + canvas.redraw (10, 10, extent.x, extent.y, false); + display.timerExec (INTERVAL, this); + } + }); + shell.open (); + while (!shell.isDisposed ()){ + if (!display.readAndDispatch ()) display.sleep (); + } + image.dispose (); + display.dispose (); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snippets/canvas/Snippet290.d Tue May 20 12:10:54 2008 +0900 @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 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: + * Bill Baxter <wbaxter> at gmail com + *******************************************************************************/ +module snippets.canvas.Snippet290; + +/* + * Canvas snippet: ignore 2nd mouse up event after double-click + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ + +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.events.MouseEvent; +import dwt.events.MouseAdapter; + +import tango.io.Stdout; + +void main() { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.addMouseListener(new class MouseAdapter { + public void mouseUp(MouseEvent e) { + if (e.count == 1) { + Stdout("Mouse up").newline; + } + } + public void mouseDoubleClick(MouseEvent e) { + Stdout("Double-click").newline; + } + }); + shell.setBounds(10, 10, 200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snippets/canvas/Snippet48.d Tue May 20 12:10:54 2008 +0900 @@ -0,0 +1,129 @@ +/******************************************************************************* + * 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: + * Bill Baxter <wbaxter> at gmail com + *******************************************************************************/ +module snippets.canvas.Snippet48; + +/* + * Canvas example snippet: scroll an image (flicker free, no double buffering) + * + * 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.Point; +import dwt.graphics.Rectangle; +import dwt.graphics.Color; +import dwt.graphics.Image; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Event; +import dwt.widgets.Listener; +import dwt.widgets.FileDialog; +import dwt.widgets.Canvas; +import dwt.widgets.ScrollBar; +import dwt.layout.FillLayout; + +import dwt.dwthelper.utils; + +void main () { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout(new FillLayout()); + Image originalImage = null; + FileDialog dialog = new FileDialog (shell, DWT.OPEN); + dialog.setText ("Open an image file or cancel"); + String string = dialog.open (); + if (string !is null) { + originalImage = new Image (display, string); + } + if (originalImage is null) { + int width = 150, height = 200; + originalImage = new Image (display, width, height); + GC gc = new GC (originalImage); + gc.fillRectangle (0, 0, width, height); + gc.drawLine (0, 0, width, height); + gc.drawLine (0, height, width, 0); + gc.drawText ("Default Image", 10, 10); + gc.dispose (); + } + final Image image = originalImage; + final Point origin = new Point (0, 0); + final Canvas canvas = new Canvas (shell, DWT.NO_BACKGROUND | + DWT.NO_REDRAW_RESIZE | DWT.V_SCROLL | DWT.H_SCROLL); + final ScrollBar hBar = canvas.getHorizontalBar (); + void onHBarSelection (Event e) { + int hSelection = hBar.getSelection (); + int destX = -hSelection - origin.x; + Rectangle rect = image.getBounds (); + canvas.scroll (destX, 0, 0, 0, rect.width, rect.height, false); + origin.x = -hSelection; + } + final ScrollBar vBar = canvas.getVerticalBar (); + void onVBarSelection(Event e) { + int vSelection = vBar.getSelection (); + int destY = -vSelection - origin.y; + Rectangle rect = image.getBounds (); + canvas.scroll (0, destY, 0, 0, rect.width, rect.height, false); + origin.y = -vSelection; + } + void onResize(Event e) { + Rectangle rect = image.getBounds (); + Rectangle client = canvas.getClientArea (); + hBar.setMaximum (rect.width); + vBar.setMaximum (rect.height); + hBar.setThumb (Math.min (rect.width, client.width)); + vBar.setThumb (Math.min (rect.height, client.height)); + int hPage = rect.width - client.width; + int vPage = rect.height - client.height; + int hSelection = hBar.getSelection (); + int vSelection = vBar.getSelection (); + if (hSelection >= hPage) { + if (hPage <= 0) hSelection = 0; + origin.x = -hSelection; + } + if (vSelection >= vPage) { + if (vPage <= 0) vSelection = 0; + origin.y = -vSelection; + } + canvas.redraw (); + } + void onPaint (Event e) { + GC gc = e.gc; + gc.drawImage (image, origin.x, origin.y); + Rectangle rect = image.getBounds (); + Rectangle client = canvas.getClientArea (); + int marginWidth = client.width - rect.width; + if (marginWidth > 0) { + gc.fillRectangle (rect.width, 0, marginWidth, client.height); + } + int marginHeight = client.height - rect.height; + if (marginHeight > 0) { + gc.fillRectangle (0, rect.height, client.width, marginHeight); + } + } + + hBar.addListener (DWT.Selection, dgListener(&onHBarSelection)); + vBar.addListener (DWT.Selection, dgListener(&onVBarSelection)); + canvas.addListener (DWT.Resize, dgListener(&onResize)); + canvas.addListener (DWT.Paint, dgListener(&onPaint)); + + shell.setSize (200, 150); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + originalImage.dispose(); + display.dispose (); +} +
--- a/snippets/dsss.conf Mon May 19 23:42:48 2008 +0900 +++ b/snippets/dsss.conf Tue May 20 12:10:54 2008 +0900 @@ -17,6 +17,11 @@ [button/Snippet293.d] +[canvas/Snippet21.d] +[canvas/Snippet48.d] +[canvas/Snippet245.d] +[canvas/Snippet275.d] +[canvas/Snippet290.d] [control/Snippet25.d] [control/Snippet62.d] [combo/Snippet26.d]