changeset 94:799099686955

New tree snippet ports
author Bill Baxter <bill@billbaxter.com>
date Thu, 22 May 2008 13:47:53 +0900
parents 961ca8a76cad
children a35db4f4af8c
files snippets/dsss.conf snippets/tree/Snippet170.d snippets/tree/Snippet193.d snippets/tree/Snippet226.d
diffstat 4 files changed, 329 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/snippets/dsss.conf	Thu May 22 03:05:49 2008 +0900
+++ b/snippets/dsss.conf	Thu May 22 13:47:53 2008 +0900
@@ -64,7 +64,10 @@
 [tray/Snippet143.d]
 [tree/Snippet8.d]
 [tree/Snippet15.d]
+[tree/Snippet170.d]
+[tree/Snippet193.d]
 [tree/Snippet220.d]
+[tree/Snippet226.d]
 [treeeditor/Snippet111.d]
 
 version(Derelict){
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/tree/Snippet170.d	Thu May 22 13:47:53 2008 +0900
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * 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
+ * D Port:
+ *     Bill Baxter <wbaxter@gmail.com>
+ *******************************************************************************/
+module snippets.tree.Snippet170;
+
+/*
+ * Tree example snippet: Create a Tree with columns
+ * 
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ * 
+ * @since 3.1
+ */
+
+import dwt.DWT;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Tree;
+import dwt.widgets.TreeItem;
+import dwt.widgets.TreeColumn;
+import dwt.layout.FillLayout;
+
+import tango.util.Convert;
+
+import dwt.dwthelper.utils;
+
+void main() {
+    Display display = new Display();
+    final Shell shell = new Shell(display);
+    shell.setLayout(new FillLayout());
+    Tree tree = new Tree(shell, DWT.BORDER | DWT.H_SCROLL | DWT.V_SCROLL);
+    tree.setHeaderVisible(true);
+    TreeColumn column1 = new TreeColumn(tree, DWT.LEFT);
+    column1.setText("Column 1");
+    column1.setWidth(200);
+    TreeColumn column2 = new TreeColumn(tree, DWT.CENTER);
+    column2.setText("Column 2");
+    column2.setWidth(200);
+    TreeColumn column3 = new TreeColumn(tree, DWT.RIGHT);
+    column3.setText("Column 3");
+    column3.setWidth(200);
+    for (int i = 0; i < 4; i++) {
+        TreeItem item = new TreeItem(tree, DWT.NONE);
+        item.setText([ "item " ~ to!(String)(i), "abc", "defghi" ]);
+        for (int j = 0; j < 4; j++) {
+            TreeItem subItem = new TreeItem(item, DWT.NONE);
+            subItem.setText([ "subitem " ~ to!(String)(j), "jklmnop", "qrs" ]);
+            for (int k = 0; k < 4; k++) {
+                TreeItem subsubItem = new TreeItem(subItem, DWT.NONE);
+                subsubItem.setText([ "subsubitem " ~ to!(String)(k), "tuv", "wxyz" ]);
+            }
+        }
+    }
+    shell.pack();
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch()) {
+            display.sleep();
+        }
+    }
+    display.dispose();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/tree/Snippet193.d	Thu May 22 13:47:53 2008 +0900
@@ -0,0 +1,119 @@
+/*******************************************************************************
+ * 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
+ * D Port:
+ *     Bill Baxter <wbaxter@gmail.com>
+ *******************************************************************************/
+module snippets.tree.Snippet193;
+
+/*
+ * Tree example snippet: allow user to reorder columns by dragging and programmatically.
+ * 
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ * 
+ * @since 3.2
+ */
+import dwt.DWT;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Tree;
+import dwt.widgets.TreeItem;
+import dwt.widgets.TreeColumn;
+import dwt.widgets.Button;
+import dwt.widgets.Listener;
+import dwt.widgets.Event;
+import dwt.layout.RowLayout;
+import dwt.layout.RowData;
+
+import tango.util.Convert;
+import tango.io.Stdout;
+
+import dwt.dwthelper.utils;
+
+
+void main() {
+    Display display = new Display();
+    Shell shell = new Shell(display);
+    shell.setLayout(new RowLayout(DWT.HORIZONTAL));
+    final Tree tree = new Tree(shell, DWT.BORDER | DWT.CHECK);
+    tree.setLayoutData(new RowData(-1, 300));
+    tree.setHeaderVisible(true);
+    TreeColumn column = new TreeColumn(tree, DWT.LEFT);
+    column.setText("Column 0");
+    column = new TreeColumn(tree, DWT.CENTER);
+    column.setText("Column 1");
+    column = new TreeColumn(tree, DWT.LEFT);
+    column.setText("Column 2");
+    column = new TreeColumn(tree, DWT.RIGHT);
+    column.setText("Column 3");
+    column = new TreeColumn(tree, DWT.CENTER);
+    column.setText("Column 4");
+    for (int i = 0; i < 5; i++) {
+        TreeItem item = new TreeItem(tree, DWT.NONE);
+        auto istr = to!(String)(i);
+        String[] text = [istr~":0",
+                         istr~":1",
+                         istr~":2",
+                         istr~":3",
+                         istr~":4"];
+        item.setText(text);
+        for (int j = 0; j < 5; j++) {
+            auto jstr = to!(String)(j);
+            TreeItem subItem = new TreeItem(item, DWT.NONE);
+            text = [istr~","~jstr~":0",
+                    istr~","~jstr~":1", 
+                    istr~","~jstr~":2",
+                    istr~","~jstr~":3",
+                    istr~","~jstr~":4"];
+            subItem.setText(text);
+            for (int k = 0; k < 5; k++) {
+                auto kstr = to!(String)(k);
+                TreeItem subsubItem = new TreeItem(subItem, DWT.NONE);
+                text = [istr~","~jstr~","~kstr~":0",
+                        istr~","~jstr~","~kstr~":1",
+                        istr~","~jstr~","~kstr~":2",
+                        istr~","~jstr~","~kstr~":3",
+                        istr~","~jstr~","~kstr~":4"];
+                subsubItem.setText(text);
+            }
+        }
+    }
+    Listener listener = new class Listener {
+        public void handleEvent(Event e) {
+            Stdout.print("Move "~e.widget.toString).newline;
+        }
+    };
+    TreeColumn[] columns = tree.getColumns();
+    for (int i = 0; i < columns.length; i++) {
+        columns[i].setWidth(100);
+        columns[i].setMoveable(true);
+        columns[i].addListener(DWT.Move, listener);
+    }
+    Button b = new Button(shell, DWT.PUSH);
+    b.setText("invert column order");
+    b.addListener(DWT.Selection, new class Listener {
+        public void handleEvent(Event e) {
+            int[] order = tree.getColumnOrder();
+            for (int i = 0; i < order.length / 2; i++) {
+                int temp = order[i];
+                order[i] = order[order.length - i - 1];
+                order[order.length - i - 1] = temp;
+            }
+            tree.setColumnOrder(order);
+        }
+    });
+    shell.pack();
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch())
+            display.sleep();
+    }
+    display.dispose();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/tree/Snippet226.d	Thu May 22 13:47:53 2008 +0900
@@ -0,0 +1,136 @@
+/*******************************************************************************
+ * 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@gmail.com>
+ *******************************************************************************/
+module snippets.tree.Snippet226;
+
+/* 
+ * Tree example snippet: Draw a custom gradient selection
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ * 
+ * @since 3.3
+ */
+import dwt.DWT;
+import dwt.graphics.GC;
+import dwt.graphics.Rectangle;
+import dwt.graphics.Region;
+import dwt.graphics.Color;
+import dwt.layout.FillLayout;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Tree;
+import dwt.widgets.TreeItem;
+import dwt.widgets.TreeColumn;
+import dwt.widgets.Listener;
+import dwt.widgets.Event;
+
+import tango.util.Convert;
+
+import dwt.dwthelper.utils;
+
+void main() 
+{
+	final Display display = new Display();
+	Shell shell = new Shell(display);
+	shell.setText("Custom gradient selection for Tree");
+	shell.setLayout(new FillLayout());
+	final Tree tree = new Tree(shell, DWT.MULTI | DWT.FULL_SELECTION);
+	tree.setHeaderVisible(true);
+	tree.setLinesVisible(true);
+	int columnCount = 4;
+	for (int i=0; i<columnCount; i++) {
+        auto istr = to!(String)(i);
+		TreeColumn column = new TreeColumn(tree, DWT.NONE);
+		column.setText("Column " ~ istr);	
+	}
+	int itemCount = 3;
+	for (int i=0; i<itemCount; i++) {
+        auto istr = to!(String)(i);
+		TreeItem item1 = new TreeItem(tree, DWT.NONE);
+		item1.setText("item "~istr);
+		for (int c=1; c < columnCount; c++) {
+            auto cstr = to!(String)(c);
+			item1.setText(c, "item ["~istr~"-"~cstr~"]");
+		}
+		for (int j=0; j<itemCount; j++) {
+            auto jstr = to!(String)(j);
+			TreeItem item2 = new TreeItem(item1, DWT.NONE);
+			item2.setText("item ["~istr~" "~jstr~"]");
+			for (int c=1; c<columnCount; c++) {
+                auto cstr = to!(String)(c);
+				item2.setText(c, "item ["~istr~" "~jstr~"-"~cstr~"]");
+			}
+			for (int k=0; k<itemCount; k++) {
+                auto kstr = to!(String)(k);
+				TreeItem item3 = new TreeItem(item2, DWT.NONE);
+				item3.setText("item ["~istr~" "~jstr~" "~kstr~"]");
+				for (int c=1; c<columnCount; c++) {
+                    auto cstr = to!(String)(c);
+					item3.setText(c, "item ["~istr~" "~jstr~" "~kstr~"-"~cstr~"]");
+				}
+			}
+		}
+	}
+
+	/*
+	 * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
+	 * Therefore, it is critical for performance that these methods be
+	 * as efficient as possible.
+	 */
+	tree.addListener(DWT.EraseItem, new class Listener {
+		public void handleEvent(Event event) {
+			event.detail &= ~DWT.HOT;
+			if ((event.detail & DWT.SELECTED) != 0) {
+				GC gc = event.gc;
+				Rectangle area = tree.getClientArea();
+				/*
+				 * If you wish to paint the selection beyond the end of
+				 * last column, you must change the clipping region.
+				 */
+				int columnCount = tree.getColumnCount();
+				if (event.index == columnCount - 1 || columnCount == 0) {
+					int width = area.x + area.width - event.x;
+					if (width > 0) {
+						Region region = new Region();
+						gc.getClipping(region);
+						region.add(event.x, event.y, width, event.height); 
+						gc.setClipping(region);
+						region.dispose();
+					}
+				}
+				gc.setAdvanced(true);
+				if (gc.getAdvanced()) gc.setAlpha(127);								
+				Rectangle rect = event.getBounds();
+				Color foreground = gc.getForeground();
+				Color background = gc.getBackground();
+				gc.setForeground(display.getSystemColor(DWT.COLOR_RED));
+				gc.setBackground(display.getSystemColor(DWT.COLOR_LIST_BACKGROUND));
+				gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
+				// restore colors for subsequent drawing
+				gc.setForeground(foreground);
+				gc.setBackground(background);
+				event.detail &= ~DWT.SELECTED;					
+			}						
+		}
+	});		
+	for (int i=0; i<columnCount; i++) {
+		tree.getColumn(i).pack();
+	}	
+	tree.setSelection(tree.getItem(0));
+	shell.setSize(500, 200);
+	shell.open();
+	while (!shell.isDisposed()) {
+		if (!display.readAndDispatch()) display.sleep();
+	}
+	display.dispose();	
+}