# HG changeset patch # User Bill Baxter # Date 1207532137 -32400 # Node ID 8f8ed69858b4c5d88e0fa4f4a8b77044eb94a2c6 # Parent 88aa91d83c88ff5f745c951785f6fa403e7e2466 Ports of various toolbar snippets (47,49,58,67,153,288) diff -r 88aa91d83c88 -r 8f8ed69858b4 dsss.conf --- a/dsss.conf Mon Apr 07 08:52:14 2008 +0900 +++ b/dsss.conf Mon Apr 07 10:35:37 2008 +0900 @@ -57,6 +57,12 @@ [dwtsnippets/table/Snippet38.d] [dwtsnippets/table/Snippet144.d] [dwtsnippets/text/Snippet258.d] +[dwtsnippets/toolbar/Snippet47.d] +[dwtsnippets/toolbar/Snippet49.d] +[dwtsnippets/toolbar/Snippet58.d] +[dwtsnippets/toolbar/Snippet67.d] +[dwtsnippets/toolbar/Snippet153.d] +[dwtsnippets/toolbar/Snippet288.d] [dwtsnippets/tooltips/Snippet41.d] [dwtsnippets/tray/Snippet143.d] [dwtsnippets/tree/Snippet8.d] diff -r 88aa91d83c88 -r 8f8ed69858b4 dwtsnippets/toolbar/Snippet153.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/toolbar/Snippet153.d Mon Apr 07 10:35:37 2008 +0900 @@ -0,0 +1,62 @@ +/******************************************************************************* + * 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.toolbar.Snippet153; + +/* + * ToolBar example snippet: update a status line when the pointer enters a ToolItem + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.events.MouseEvent; +import dwt.events.MouseMoveListener; +import dwt.graphics.Point; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.ToolBar; +import dwt.widgets.ToolItem; +import dwt.widgets.Label; + +static char[] statusText = ""; +void main() { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setBounds(10, 10, 200, 200); + ToolBar bar = new ToolBar(shell, DWT.BORDER); + bar.setBounds(10, 10, 150, 50); + Label statusLine = new Label(shell, DWT.BORDER); + statusLine.setBounds(10, 90, 150, 30); + (new ToolItem(bar, DWT.NONE)).setText("item 1"); + (new ToolItem(bar, DWT.NONE)).setText("item 2"); + (new ToolItem(bar, DWT.NONE)).setText("item 3"); + bar.addMouseMoveListener(new class MouseMoveListener { + void mouseMove(MouseEvent e) { + ToolItem item = bar.getItem(new Point(e.x, e.y)); + char[] name = ""; + if (item !is null) { + name = item.getText(); + } + if (statusText != name) { + statusLine.setText(name); + statusText = name; + } + } + }); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} + diff -r 88aa91d83c88 -r 8f8ed69858b4 dwtsnippets/toolbar/Snippet288.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/toolbar/Snippet288.d Mon Apr 07 10:35:37 2008 +0900 @@ -0,0 +1,208 @@ +/******************************************************************************* + * Copyright (c) 2008 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 + *******************************************************************************/ +module dwtsnippets.toolbar.Snippet288; + +/* + * Create a ToolBar containing animated GIFs + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.DWTException; +import dwt.graphics.GC; +import dwt.graphics.Color; +import dwt.graphics.Image; +import dwt.graphics.ImageLoader; +import dwt.graphics.ImageData; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.ToolBar; +import dwt.widgets.ToolItem; +import dwt.widgets.FileDialog; +import dwt.dwthelper.Runnable; + +import tango.io.FilePath; +import tango.io.FileConst; +import tango.core.Thread; +import tango.io.Stdout; +import tango.util.Convert; +import tango.core.Exception; + +static Display display; +static Shell shell; +static GC shellGC; +static Color shellBackground; +static ImageLoader[] loader; +static ImageData[][] imageDataArray; +static Thread[] animateThread; +static Image[][] image; +private static ToolItem[] item; +static final bool useGIFBackground = false; + +void main () { + display = new Display(); + Shell shell = new Shell (display); + shellBackground = shell.getBackground(); + FileDialog dialog = new FileDialog(shell, DWT.OPEN | DWT.MULTI); + dialog.setText("Select Multiple Animated GIFs"); + dialog.setFilterExtensions(["*.gif"]); + char[] filename = dialog.open(); + char[][] filenames = dialog.getFileNames(); + int numToolBarItems = filenames.length; + if (numToolBarItems > 0) { + try { + loadAllImages((new FilePath(filename)).parent, filenames); + } catch (DWTException e) { + Stdout.print("There was an error loading an image.").newline; + e.printStackTrace(); + } + ToolBar toolBar = new ToolBar (shell, DWT.FLAT | DWT.BORDER | DWT.WRAP); + item = new ToolItem[numToolBarItems]; + for (int i = 0; i < numToolBarItems; i++) { + item[i] = new ToolItem (toolBar, DWT.PUSH); + item[i].setImage(image[i][0]); + } + toolBar.pack (); + shell.open (); + + startAnimationThreads(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + + for (int i = 0; i < numToolBarItems; i++) { + for (int j = 0; j < image[i].length; j++) { + image[i][j].dispose(); + } + } + display.dispose (); + } + thread_joinAll(); +} + +private static void loadAllImages(char[] directory, char[][] filenames) { + int numItems = filenames.length; + loader.length = numItems; + imageDataArray.length = numItems; + image.length = numItems; + for (int i = 0; i < numItems; i++) { + loader[i] = new ImageLoader(); + int fullWidth = loader[i].logicalScreenWidth; + int fullHeight = loader[i].logicalScreenHeight; + imageDataArray[i] = loader[i].load(directory ~ FileConst.PathSeparatorChar ~ filenames[i]); + int numFramesOfAnimation = imageDataArray[i].length; + image[i] = new Image[numFramesOfAnimation]; + for (int j = 0; j < numFramesOfAnimation; j++) { + if (j == 0) { + //for the first frame of animation, just draw the first frame + image[i][j] = new Image(display, imageDataArray[i][j]); + fullWidth = imageDataArray[i][j].width; + fullHeight = imageDataArray[i][j].height; + } + else { + //after the first frame of animation, draw the background or previous frame first, then the new image data + image[i][j] = new Image(display, fullWidth, fullHeight); + GC gc = new GC(image[i][j]); + gc.setBackground(shellBackground); + gc.fillRectangle(0, 0, fullWidth, fullHeight); + ImageData imageData = imageDataArray[i][j]; + switch (imageData.disposalMethod) { + case DWT.DM_FILL_BACKGROUND: + /* Fill with the background color before drawing. */ + Color bgColor = null; + if (useGIFBackground && loader[i].backgroundPixel != -1) { + bgColor = new Color(display, imageData.palette.getRGB(loader[i].backgroundPixel)); + } + gc.setBackground(bgColor !is null ? bgColor : shellBackground); + gc.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height); + if (bgColor !is null) bgColor.dispose(); + break; + default: + /* Restore the previous image before drawing. */ + gc.drawImage( + image[i][j-1], + 0, + 0, + fullWidth, + fullHeight, + 0, + 0, + fullWidth, + fullHeight); + break; + } + Image newFrame = new Image(display, imageData); + gc.drawImage(newFrame, + 0, + 0, + imageData.width, + imageData.height, + imageData.x, + imageData.y, + imageData.width, + imageData.height); + newFrame.dispose(); + gc.dispose(); + } + } + } +} + +private static void startAnimationThreads() { + animateThread = new Thread[image.length]; + for (int ii = 0; ii < image.length; ii++) { + animateThread[ii] = new class(ii) Thread { + int imageDataIndex = 0; + int id = 0; + this(int _id) { + id = _id; + name = "Animation "~to!(char[])(ii); + isDaemon = true; + super(&run); + } + void run() { + try { + int repeatCount = loader[id].repeatCount; + while (loader[id].repeatCount == 0 || repeatCount > 0) { + imageDataIndex = (imageDataIndex + 1) % imageDataArray[id].length; + if (!display.isDisposed()) { + display.asyncExec(new class Runnable { + public void run() { + if (!item[id].isDisposed()) + item[id].setImage(image[id][imageDataIndex]); + } + }); + } + + /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */ + try { + int ms = imageDataArray[id][imageDataIndex].delayTime * 10; + if (ms < 20) ms += 30; + if (ms < 30) ms += 10; + Thread.sleep(0.001*ms); + } catch (ThreadException e) { + } + + /* If we have just drawn the last image, decrement the repeat count and start again. */ + if (imageDataIndex == imageDataArray[id].length - 1) repeatCount--; + } + } catch (DWTException ex) { + Stdout.print("There was an error animating the GIF").newline; + ex.printStackTrace(); + } + } + }; + animateThread[ii].start(); + } +} + diff -r 88aa91d83c88 -r 8f8ed69858b4 dwtsnippets/toolbar/Snippet47.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/toolbar/Snippet47.d Mon Apr 07 10:35:37 2008 +0900 @@ -0,0 +1,74 @@ +/******************************************************************************* + * 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.toolbar.Snippet47; + +/* + * ToolBar example snippet: create tool bar (normal, hot and disabled images) + * + * 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.Image; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.ToolBar; +import dwt.widgets.ToolItem; + +void main () { + Display display = new Display (); + Shell shell = new Shell (display); + + Image image = new Image (display, 20, 20); + Color color = display.getSystemColor (DWT.COLOR_BLUE); + GC gc = new GC (image); + gc.setBackground (color); + gc.fillRectangle (image.getBounds ()); + gc.dispose (); + + Image disabledImage = new Image (display, 20, 20); + color = display.getSystemColor (DWT.COLOR_GREEN); + gc = new GC (disabledImage); + gc.setBackground (color); + gc.fillRectangle (disabledImage.getBounds ()); + gc.dispose (); + + Image hotImage = new Image (display, 20, 20); + color = display.getSystemColor (DWT.COLOR_RED); + gc = new GC (hotImage); + gc.setBackground (color); + gc.fillRectangle (hotImage.getBounds ()); + gc.dispose (); + + ToolBar bar = new ToolBar (shell, DWT.BORDER | DWT.FLAT); + bar.setSize (200, 32); + for (int i=0; i<12; i++) { + ToolItem item = new ToolItem (bar, 0); + item.setImage (image); + item.setDisabledImage (disabledImage); + item.setHotImage (hotImage); + if (i % 3 == 0) item.setEnabled (false); + } + + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + image.dispose (); + disabledImage.dispose (); + hotImage.dispose (); + display.dispose (); +} + diff -r 88aa91d83c88 -r 8f8ed69858b4 dwtsnippets/toolbar/Snippet49.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/toolbar/Snippet49.d Mon Apr 07 10:35:37 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.toolbar.Snippet49; + +/* + * ToolBar example snippet: create tool bar (wrap on resize) + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.graphics.Rectangle; +import dwt.graphics.Point; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.ToolBar; +import dwt.widgets.ToolItem; +import dwt.widgets.Event; +import dwt.widgets.Listener; + +import tango.util.Convert; + +void main () { + Display display = new Display (); + Shell shell = new Shell (display); + ToolBar toolBar = new ToolBar (shell, DWT.WRAP); + for (int i=0; i<12; i++) { + ToolItem item = new ToolItem (toolBar, DWT.PUSH); + item.setText ("Item " ~ to!(char[])(i)); + } + shell.addListener (DWT.Resize, new class Listener { + void handleEvent (Event e) { + Rectangle rect = shell.getClientArea (); + Point size = toolBar.computeSize (rect.width, DWT.DEFAULT); + toolBar.setSize (size); + } + }); + toolBar.pack (); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + diff -r 88aa91d83c88 -r 8f8ed69858b4 dwtsnippets/toolbar/Snippet58.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/toolbar/Snippet58.d Mon Apr 07 10:35:37 2008 +0900 @@ -0,0 +1,59 @@ +/******************************************************************************* + * 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.toolbar.Snippet58; + +/* + * ToolBar example snippet: place a combo box in a tool bar + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.ToolBar; +import dwt.widgets.ToolItem; +import dwt.widgets.Combo; + +import tango.util.Convert; + +void main () { + Display display = new Display (); + Shell shell = new Shell (display); + ToolBar bar = new ToolBar (shell, DWT.BORDER); + for (int i=0; i<4; i++) { + ToolItem item = new ToolItem (bar, 0); + item.setText ("Item " ~ to!(char[])(i)); + } + ToolItem sep = new ToolItem (bar, DWT.SEPARATOR); + int start = bar.getItemCount (); + for (int i=start; i + *******************************************************************************/ +module dwtsnippets.toolbar.Snippet67; + +/* + * ToolBar example snippet: place a drop down menu in a tool bar + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.graphics.Rectangle; +import dwt.graphics.Point; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.ToolBar; +import dwt.widgets.ToolItem; +import dwt.widgets.Menu; +import dwt.widgets.MenuItem; +import dwt.widgets.Listener; +import dwt.widgets.Event; + +import tango.util.Convert; + +void main () { + Display display = new Display (); + Shell shell = new Shell (display); + ToolBar toolBar = new ToolBar (shell, DWT.NONE); + Menu menu = new Menu (shell, DWT.POP_UP); + for (int i=0; i<8; i++) { + MenuItem item = new MenuItem (menu, DWT.PUSH); + item.setText ("Item " ~ to!(char[])(i)); + } + ToolItem item = new ToolItem (toolBar, DWT.DROP_DOWN); + item.addListener (DWT.Selection, new class Listener { + void handleEvent (Event event) { + if (event.detail == DWT.ARROW) { + Rectangle rect = item.getBounds (); + Point pt = new Point (rect.x, rect.y + rect.height); + pt = toolBar.toDisplay (pt); + menu.setLocation (pt.x, pt.y); + menu.setVisible (true); + } + } + }); + toolBar.pack (); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + menu.dispose (); + display.dispose (); +}