1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * gtkD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with gtkD; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
17  */
18 
19 module TestTreeView;
20 
21 private import gtk.ScrolledWindow;
22 
23 private import gtk.TreeView;
24 private import gtk.TreePath;
25 private import gtk.TreeViewColumn;
26 private import gtk.TreeIter;
27 private import gtk.TreeStore;
28 private import gtk.TreeModel;
29 //private import ddi.Pixbuf;
30 private import gtk.TreeSelection;
31 private import gtk.CellRendererText;
32 
33 private import gtk.Image;
34 //private import ddi.Pixbuf;
35 
36 private import gtk.TreeNode;
37 
38 private import std.stdio;
39 
40 
41 /* Don't copy this bad example; inline RGB data is always a better
42  * idea than inline XPMs.
43  */
44 
45 static string[]  book_closed_xpm = [
46 "16 16 6 1",
47 "       c None s None",
48 ".      c black",
49 "X      c red",
50 "o      c yellow",
51 "O      c #808080",
52 "#      c white",
53 "                ",
54 "       ..       ",
55 "     ..XX.      ",
56 "   ..XXXXX.     ",
57 " ..XXXXXXXX.    ",
58 ".ooXXXXXXXXX.   ",
59 "..ooXXXXXXXXX.  ",
60 ".X.ooXXXXXXXXX. ",
61 ".XX.ooXXXXXX..  ",
62 " .XX.ooXXX..#O  ",
63 "  .XX.oo..##OO. ",
64 "   .XX..##OO..  ",
65 "    .X.#OO..    ",
66 "     ..O..      ",
67 "      ..        ",
68 "                "
69 ];
70 
71 /**
72  * This tests the GtkD tree widget
73  * \todo report bug to ng: nested class with same name of other class was not used<br>
74  * test if this works:<br>
75  * class A {}<br>
76  * class B { <br>
77  *  class A{} <br>
78  *  method (){ new A;}<br>
79  * }<br>
80  */
81 class TestTreeView : ScrolledWindow
82 {
83 	Image image;
84 	//Pixbuf pixbuf;
85 
86 	enum columns
87 	{
88 		//gtkDObject = GtkDType.PIXBUF,
89 		gtkDObject = GType.STRING,
90 		gtkObject = GType.STRING,
91 		desc = GType.STRING,
92 		percentDone = GType.STRING,
93 		author = GType.STRING
94 	}
95 
96 	this()
97 	{
98 		super(null,null);
99 
100 		TreeView treeView = setup();
101 		addWithViewport(treeView);
102 
103 		treeView.addOnRowActivated(&rowActivatedCallback);
104 		treeView.addOnMoveCursor(&moveCursorCallBack);
105 	}
106 
107 	void rowActivatedCallback(TreePath path, TreeViewColumn column, TreeView treeView)
108 	{
109 		writefln("rowActivateCallback for %X",treeView);
110 		writefln("rowActivateCallback for path %s",path.toString());
111 	}
112 
113 	bool moveCursorCallBack(GtkMovementStep step, int direction, TreeView treeView)
114 	{
115 		writefln("moveCursorCallBack for %X",treeView);
116 		writefln("moveCursorCallBack row = %d",direction);
117 		return false;
118 	}
119 
120 	TreeIter [12] h;
121 	int stack = 0;
122 	void push(TreeIter ti)
123 	{
124 		h[stack++]=ti;
125 	}
126 	TreeIter peek()
127 	{
128 		if ( stack == 0)
129 		{
130 			return null;
131 		}
132 		return h[stack-1];
133 	}
134 
135 	TreeIter pop()
136 	{
137 		if ( stack == 0)
138 		{
139 			return null;
140 		}
141 		return h[--stack];
142 	}
143 
144 	class HTreeNode : TreeNode
145 	{
146 
147 		string gtkDL;
148 		string gtk;
149 		string desc;
150 		int percent;
151 		string author;
152 		//TestTreeView ttv;
153 
154 		this(string gtkDL,string gtk,string desc,int percent)//, TestTreeView ttv)
155 		{
156 			this.gtkDL = gtkDL;
157 			this.gtk = gtk;
158 			this.desc = desc;
159 			this.percent = percent;
160 			this.author = "Ant";
161 			//this.ttv = ttv;
162 
163 		}
164 
165 		//Pixbuf getNodePixbuf(int colum)
166 		//{
167 		//	return pixbuf;
168 		//}
169 
170 		int columnCount()
171 		{
172 			return 5;
173 		}
174 
175 		string getNodeValue(int column)
176 		{
177 			string value;
178 			switch (column)
179 			{
180 				//case 0: value = pixbuf; break;
181 				case 0: value = gtkDL; break;
182 				case 1: value = gtk; break;
183 				case 2: value = desc; break;
184 				case 3:
185 					switch(percent)
186 					{
187 						case -1: value = "uggly: won't get in"; break;
188 						case 0: value = "unknown"; break;
189 						case 1: value = "not yet";break;
190 						case 98: value = "Almost"; break;
191 						case 99: value = "AGAIG"; break;
192 						case 100: value = "Complete"; break;
193 						default:value = "%"; break;
194 					}
195 					break;
196 
197 				case 4: value = author; break;
198 				default: value = "N/A"; break;
199 			}
200 			return value;
201 		}
202 	}
203 
204 	TreeView setup()
205 	{
206 
207 		class TTreeStore : TreeStore
208 		{
209 			this()
210 			{
211 				//this.nCols = nCols;
212 				static GType [5] columns = [
213 					//GtkDType.PIXBUF,
214 					GType.STRING,
215 					GType.STRING,
216 					GType.STRING,
217 					GType.STRING,
218 					GType.STRING];
219 				super(columns);
220 
221 			}
222 		}
223 
224 		TTreeStore testTreeStore = new TTreeStore();
225 		TreeView treeView = new TreeView(testTreeStore);
226 		treeView.setRulesHint(true);
227 
228 		TreeSelection ts = treeView.getSelection();
229 		ts.setMode(SelectionMode.MULTIPLE);
230 
231 		//TreeViewColumn column = new TreeViewColumn("GtkDObject",new CellRendererPixbuf(),"GdkPixbuf", 0);
232 		TreeViewColumn column = new TreeViewColumn("GtkDObject",new CellRendererText(),"text", 0);
233 		treeView.appendColumn(column);
234 		column.setResizable(true);
235 		column.setReorderable(true);
236 		column.setSortColumnId(0);
237 		column.setSortIndicator(true);
238 		//pixbuf = new Pixbuf(book_closed_xpm);
239 		//image = new Image(pixbuf);
240 		//column.setWidget(image);
241 
242 		column = new TreeViewColumn("GtkObject",new CellRendererText(),"text", 1);
243 		treeView.appendColumn(column);
244 		column.setResizable(true);
245 		column.setReorderable(true);
246 		column.setSortColumnId(1);
247 		column.setSortIndicator(true);
248 
249 		column = new TreeViewColumn("Desc",new CellRendererText(),"text", 2);
250 		treeView.appendColumn(column);
251 		column.setResizable(true);
252 		column.setReorderable(true);
253 		column.setSortColumnId(2);
254 		column.setSortIndicator(true);
255 
256 		column = new TreeViewColumn("Complete",new CellRendererText(),"text", 3);
257 		treeView.appendColumn(column);
258 		column.setResizable(true);
259 		column.setReorderable(true);
260 		column.setSortColumnId(3);
261 		column.setSortIndicator(true);
262 
263 		column = new TreeViewColumn("Author",new CellRendererText(),"text", 4);
264 		treeView.appendColumn(column);
265 		column.setResizable(true);
266 		column.setReorderable(true);
267 		column.setSortColumnId(4);
268 		column.setSortIndicator(true);
269 
270 		TreeIter iter;
271 		TreeIter iterTop = testTreeStore.createIter(null);
272 
273 		TreeIter peekIter(bool pushIt)
274 		{
275 			TreeIter iter = testTreeStore.append(peek());
276 			if ( pushIt )
277 			{
278 				push(iter);
279 			}
280 			return iter;
281 		}
282 
283 		TreeIter popIter(bool pushIt)
284 		{
285 			TreeIter iter = testTreeStore.append(pop());
286 			if ( pushIt )
287 			{
288 				push(iter);
289 			}
290 			return iter;
291 		}
292 
293 		push(iterTop);
294 		testTreeStore.set(iterTop, new HTreeNode("ObjectG","GObject","Parent of the main GtkD objects",90));
295 
296 		testTreeStore.set(peekIter(true), new HTreeNode("ObjectGtk","GtkObject","The main gtk object",100));
297 		testTreeStore.set(peekIter(true), new HTreeNode("Misc","GtkMisc","Comon for Most widgets",100));
298 
299 		testTreeStore.set(peekIter(true), new HTreeNode("Label","GtkLabel","Display only widget",100));
300 		testTreeStore.set(popIter(false), new HTreeNode("AccelLabel","GtkAccelLabel","A label with a thing",1));
301 		testTreeStore.set(peekIter(false), new HTreeNode("Arrow","GtkArrow","A standard arrow",100));
302 		testTreeStore.set(popIter(false), new HTreeNode("Image","GtkImage","A image",100));
303 		testTreeStore.set(peekIter(true), new HTreeNode("Container","GtkContainer","Comon for all widgets that can contain other widgets",90));
304 		testTreeStore.set(peekIter(false), new HTreeNode("Bin","GtkBin","Comon for many widgets",90));
305 
306 		testTreeStore.set(peekIter(false), new HTreeNode("Alignment","GtkAlignment","Aligns a widget in a larger area",100));
307 		testTreeStore.set(peekIter(true), new HTreeNode("Frame","GtkFrame","",100));
308 		testTreeStore.set(popIter(false), new HTreeNode("AspectFrame","GtkAspectFrame","",100));
309 		testTreeStore.set(peekIter(true), new HTreeNode("Button","GtkButton","",90));
310 
311 		testTreeStore.set(peekIter(true), new HTreeNode("ToggleButton","GtkToggleButton","",0));
312 		testTreeStore.set(peekIter(false), new HTreeNode("CheckButton","GtkCheckButton","",0));
313 		testTreeStore.set(popIter(false), new HTreeNode("RadioButton","GtkRadioButton","",0));
314 		testTreeStore.set(popIter(false), new HTreeNode("OptionMenu","GtkOptionMenu","",0));
315 		testTreeStore.set(peekIter(true), new HTreeNode("Item","GtkItem","",0));
316 		testTreeStore.set(peekIter(true), new HTreeNode("MenuItem","GtkMenuItem","",0));
317 		testTreeStore.set(peekIter(true), new HTreeNode("CheckMenuItem","GtkCheckMenuItem","",0));
318 		testTreeStore.set(popIter(false), new HTreeNode("RadioMenuItem","GtkRadioMenuItem","",0));
319 		testTreeStore.set(peekIter(false), new HTreeNode("ImageMenuItem","GtkImageMenuItem","",0));
320 		testTreeStore.set(peekIter(false), new HTreeNode("SeparatorMenuItem","GtkSeparatorMenuItem","",0));
321 		testTreeStore.set(popIter(false), new HTreeNode("TearoffMenuItem","GtkTearoffMenuItem","",0));
322 		testTreeStore.set(peekIter(true), new HTreeNode("Window","GtkWindow","",0));
323 		testTreeStore.set(peekIter(true), new HTreeNode("Dialog","GtkDialog","",0));
324 		testTreeStore.set(peekIter(false), new HTreeNode("ColorSelectionDialog","GtkColorSelectionDialog","",0));
325 		testTreeStore.set(peekIter(false), new HTreeNode("FileSelection","GtkFileSelection","",0));
326 		testTreeStore.set(peekIter(false), new HTreeNode("FontSelectionDialog","GtkFontSelectionDialog","",0));
327 		testTreeStore.set(peekIter(false), new HTreeNode("InputDialog","GtkInputDialog","",0));
328 		testTreeStore.set(popIter(false), new HTreeNode("MessageDialog","GtkMessageDialog","",0));
329 		testTreeStore.set(popIter(false), new HTreeNode("Plug","GtkPlug","",0));
330 		testTreeStore.set(peekIter(false), new HTreeNode("EventBox","GtkEventBox","",0));
331 		testTreeStore.set(peekIter(false), new HTreeNode("HandleBox","GtkHandleBox","",0));
332 		testTreeStore.set(peekIter(false), new HTreeNode("ScrolledWindow","GtkScrolledWindow","",0));
333 		testTreeStore.set(popIter(false), new HTreeNode("Viewport","GtkViewport","",0));
334 
335 
336 		testTreeStore.set(peekIter(true), new HTreeNode("Box","GtkBox","",0));
337 		testTreeStore.set(peekIter(true), new HTreeNode("ButtonBox","GtkButtonBox","",0));
338 		testTreeStore.set(peekIter(false), new HTreeNode("HButtonBox","GtkHButtonBox","",0));
339 		testTreeStore.set(popIter(false), new HTreeNode("VButtonBox","GtkVButtonBox","",0));
340 		testTreeStore.set(peekIter(true), new HTreeNode("VBox","GtkVBox","",100));
341 		testTreeStore.set(peekIter(false), new HTreeNode("ColorSelection","GtkColorSelection","",100));
342 		testTreeStore.set(peekIter(false), new HTreeNode("FontSelection","GtkFontSelection","",100));
343 		testTreeStore.set(popIter(false), new HTreeNode("GammaCurve","GtkGammaCurve","",0));
344 		testTreeStore.set(popIter(true), new HTreeNode("HBox","GtkHBox","",100));
345 		testTreeStore.set(peekIter(false), new HTreeNode("Combo","GtkCombo","",0));
346 		testTreeStore.set(popIter(false), new HTreeNode("Statusbar","GtkStatusbar","",0));
347 		testTreeStore.set(peekIter(false), new HTreeNode("Fixed","GtkFixed","",0));
348 		testTreeStore.set(peekIter(true), new HTreeNode("Paned","GtkPaned","",0));
349 		testTreeStore.set(peekIter(false), new HTreeNode("HPaned","GtkHPaned","",0));
350 		testTreeStore.set(popIter(false), new HTreeNode("VPaned","GtkVPaned","",0));
351 		testTreeStore.set(peekIter(false), new HTreeNode("Layout","GtkLayout","",0));
352 		testTreeStore.set(peekIter(true), new HTreeNode("MenuShell","GtkMenuShell","",0));
353 		testTreeStore.set(peekIter(false), new HTreeNode("MenuBar","GtkMenuBar","",0));
354 		testTreeStore.set(popIter(false), new HTreeNode("Menu","GtkMenu","",0));
355 		testTreeStore.set(peekIter(false), new HTreeNode("Notebook","GtkNotebook","",0));
356 		testTreeStore.set(peekIter(false), new HTreeNode("Socket","GtkSocket","",0));
357 		testTreeStore.set(peekIter(false), new HTreeNode("Table","GtkTable","",0));
358 		testTreeStore.set(peekIter(false), new HTreeNode("TextView","GtkTextView","",0));
359 		testTreeStore.set(peekIter(false), new HTreeNode("Toolbar","GtkToolbar","",0));
360 		testTreeStore.set(popIter(false), new HTreeNode("TreeView","GtkTreeView","",0));
361 		testTreeStore.set(peekIter(false), new HTreeNode("Calendar","GtkCalendar","",0));
362 
363 		testTreeStore.set(peekIter(true), new HTreeNode("DrawingArea","GtkDrawingArea","",0));
364 		testTreeStore.set(popIter(false), new HTreeNode("Curve","GtkCurve","",0));
365 		testTreeStore.set(peekIter(true), new HTreeNode("Editable","GtkEditable","",0));
366 		testTreeStore.set(peekIter(true), new HTreeNode("Entry","GtkEntry","",0));
367 		testTreeStore.set(popIter(false), new HTreeNode("SpinButton","GtkSpinButton","",0));
368 		pop();
369 		testTreeStore.set(peekIter(true), new HTreeNode("Ruler","GtkRuler","",0));
370 		testTreeStore.set(peekIter(false), new HTreeNode("HRuler","GtkHRuler","",0));
371 		testTreeStore.set(popIter(false), new HTreeNode("VRuler","GtkVRuler","",0));
372 		testTreeStore.set(peekIter(true), new HTreeNode("Range","GtkRange","",0));
373 		testTreeStore.set(peekIter(true), new HTreeNode("Scale","GtkScale","",0));
374 		testTreeStore.set(peekIter(true), new HTreeNode("HScale","GtkHScale","",0));
375 		testTreeStore.set(popIter(false), new HTreeNode("VScale","GtkVScale","",0));
376 		testTreeStore.set(popIter(true), new HTreeNode("Scrollbar","GtkScrollbar","",0));
377 		testTreeStore.set(peekIter(false), new HTreeNode("HScrollbar","GtkHScrollbar","",0));
378 		testTreeStore.set(popIter(false), new HTreeNode("VScrollbar","GtkVScrollbar","",0));
379 		pop();
380 		testTreeStore.set(peekIter(true), new HTreeNode("Separator","GtkSeparator","",0));
381 		testTreeStore.set(peekIter(false), new HTreeNode("HSeparator","GtkHSeparator","",0));
382 		testTreeStore.set(popIter(false), new HTreeNode("VSeparator","GtkVSeparator","",0));
383 		testTreeStore.set(peekIter(false), new HTreeNode("Invisible","GtkInvisible","",0));
384 		testTreeStore.set(peekIter(false), new HTreeNode("Preview","GtkPreview","",0));
385 		testTreeStore.set(popIter(false), new HTreeNode("ProgressBar","GtkProgressBar","",0));
386 		testTreeStore.set(peekIter(false), new HTreeNode("Adjustment","GtkAdjustment","Values for the range widget (scroolbars, spin buttons...)",0));
387 		testTreeStore.set(peekIter(true), new HTreeNode("CellRenderer","GtkCellRenderer","",0));
388 		testTreeStore.set(peekIter(false), new HTreeNode("CellRendererPixbuf","GtkCellRendererPixbuf","",0));
389 		testTreeStore.set(peekIter(false), new HTreeNode("CellRendererText","GtkCellRendererText","",0));
390 		testTreeStore.set(popIter(false), new HTreeNode("CellRendererToggle","GtkCellRendererToggle","",0));
391 		testTreeStore.set(peekIter(false), new HTreeNode("ItemFactory","GtkItemFactory","",1));
392 		testTreeStore.set(peekIter(false), new HTreeNode("Tooltips","GtkTooltips","",0));
393 		testTreeStore.set(popIter(false), new HTreeNode("TreeViewColumn","GtkTreeViewColumn","",0));
394 		push(iterTop);
395 		testTreeStore.set(peekIter(false), new HTreeNode("ListG","GList","a double linked list used to comboBox and stuf",0));
396 
397 		return treeView;
398 
399 	}
400 
401 
402 }