1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet193" 5 dependency "dwt" path="../../../../../../" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "glib-2.0" \ 14 "gmodule-2.0" \ 15 "gnomeui-2" \ 16 "gnomevfs-2" \ 17 "gobject-2.0" \ 18 "gthread-2.0" \ 19 "gtk-x11-2.0" \ 20 "pango-1.0" \ 21 "pangocairo-1.0" \ 22 "X11" \ 23 "Xcomposite" \ 24 "Xcursor" \ 25 "Xdamage" \ 26 "Xext" \ 27 "Xfixes" \ 28 "Xi" \ 29 "Xinerama" \ 30 "Xrandr" \ 31 "Xrender" \ 32 "Xtst" \ 33 platform="linux" 34 +/ 35 36 /******************************************************************************* 37 * Copyright (c) 2000, 2005 IBM Corporation and others. 38 * All rights reserved. This program and the accompanying materials 39 * are made available under the terms of the Eclipse Public License v1.0 40 * which accompanies this distribution, and is available at 41 * http://www.eclipse.org/legal/epl-v10.html 42 * 43 * Contributors: 44 * IBM Corporation - initial API and implementation 45 * D Port: 46 * Bill Baxter <wbaxter@gmail.com> 47 *******************************************************************************/ 48 module org.eclipse.swt.snippets.Snippet193; 49 50 /* 51 * Tree example snippet: allow user to reorder columns by dragging and programmatically. 52 * 53 * For a list of all SWT example snippets see 54 * http://www.eclipse.org/swt/snippets/ 55 * 56 * @since 3.2 57 */ 58 import org.eclipse.swt.SWT; 59 import org.eclipse.swt.widgets.Display; 60 import org.eclipse.swt.widgets.Shell; 61 import org.eclipse.swt.widgets.Tree; 62 import org.eclipse.swt.widgets.TreeItem; 63 import org.eclipse.swt.widgets.TreeColumn; 64 import org.eclipse.swt.widgets.Button; 65 import org.eclipse.swt.widgets.Listener; 66 import org.eclipse.swt.widgets.Event; 67 import org.eclipse.swt.layout.RowLayout; 68 import org.eclipse.swt.layout.RowData; 69 70 version(Tango){ 71 import tango.util.Convert; 72 import tango.io.Stdout; 73 } else { // Phobos 74 import std.conv; 75 import std.stdio; 76 } 77 78 import java.lang.all; 79 80 81 void main() { 82 Display display = new Display(); 83 Shell shell = new Shell(display); 84 shell.setLayout(new RowLayout(SWT.HORIZONTAL)); 85 Tree tree = new Tree(shell, SWT.BORDER | SWT.CHECK); 86 tree.setLayoutData(new RowData(-1, 300)); 87 tree.setHeaderVisible(true); 88 TreeColumn column = new TreeColumn(tree, SWT.LEFT); 89 column.setText("Column 0"); 90 column = new TreeColumn(tree, SWT.CENTER); 91 column.setText("Column 1"); 92 column = new TreeColumn(tree, SWT.LEFT); 93 column.setText("Column 2"); 94 column = new TreeColumn(tree, SWT.RIGHT); 95 column.setText("Column 3"); 96 column = new TreeColumn(tree, SWT.CENTER); 97 column.setText("Column 4"); 98 for (int i = 0; i < 5; i++) { 99 TreeItem item = new TreeItem(tree, SWT.NONE); 100 auto istr = to!(String)(i); 101 String[] text = [istr~":0", 102 istr~":1", 103 istr~":2", 104 istr~":3", 105 istr~":4"]; 106 item.setText(text); 107 for (int j = 0; j < 5; j++) { 108 auto jstr = to!(String)(j); 109 TreeItem subItem = new TreeItem(item, SWT.NONE); 110 text = [istr~","~jstr~":0", 111 istr~","~jstr~":1", 112 istr~","~jstr~":2", 113 istr~","~jstr~":3", 114 istr~","~jstr~":4"]; 115 subItem.setText(text); 116 for (int k = 0; k < 5; k++) { 117 auto kstr = to!(String)(k); 118 TreeItem subsubItem = new TreeItem(subItem, SWT.NONE); 119 text = [istr~","~jstr~","~kstr~":0", 120 istr~","~jstr~","~kstr~":1", 121 istr~","~jstr~","~kstr~":2", 122 istr~","~jstr~","~kstr~":3", 123 istr~","~jstr~","~kstr~":4"]; 124 subsubItem.setText(text); 125 } 126 } 127 } 128 Listener listener = new class Listener { 129 public void handleEvent(Event e) { 130 version(Tango){ 131 Stdout.print("Move "~e.widget.toString).newline; 132 } else { 133 writeln("Move "~e.widget.toString); 134 } 135 } 136 }; 137 TreeColumn[] columns = tree.getColumns(); 138 for (int i = 0; i < columns.length; i++) { 139 columns[i].setWidth(100); 140 columns[i].setMoveable(true); 141 columns[i].addListener(SWT.Move, listener); 142 } 143 Button b = new Button(shell, SWT.PUSH); 144 b.setText("invert column order"); 145 b.addListener(SWT.Selection, new class Listener { 146 public void handleEvent(Event e) { 147 int[] order = tree.getColumnOrder(); 148 for (int i = 0; i < order.length / 2; i++) { 149 int temp = order[i]; 150 order[i] = order[order.length - i - 1]; 151 order[order.length - i - 1] = temp; 152 } 153 tree.setColumnOrder(order); 154 } 155 }); 156 shell.pack(); 157 shell.open(); 158 while (!shell.isDisposed()) { 159 if (!display.readAndDispatch()) 160 display.sleep(); 161 } 162 display.dispose(); 163 }