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 TestWindow;
20 
21 //debug = 1;
22 
23 //version=cairo;
24 
25 version(cairo)private import cairo.clock;
26 
27 private import gtk.Version;
28 private import gtk.Table;
29 
30 version(Tango)
31 {
32 	private import stdlib = tango.stdc.stdlib : exit;
33 	private import tango.text.convert.Layout;
34 	private import tango.io.Stdout;
35 	private import tango.core.Thread;
36 	private import tango.math.random.Kiss;
37 
38 	version = DRuntime;
39 }
40 else version(D_Version2)
41 {
42 	private import stdlib = core.stdc.stdlib : exit;
43 	private import core.thread;
44 	private import std.random;
45 
46 	version = DRuntime;
47 }
48 else
49 {
50 	private import stdlib = std.c.stdlib : exit;
51 	private import std.thread;
52 	private import std.random;
53 }
54 
55 import gdk.Threads;
56 
57 private import gtk.Main;
58 private import gtk.MainWindow;
59 private import gtk.Adjustment;
60 private import gtk.AccelGroup;
61 
62 private import TestEntries;
63 
64 //private import gtkDTree;
65 private import TestStock;
66 private import TestDrawingArea;
67 private import TestScales;
68 private import TestText;
69 //private import TestTree;
70 private import TestTreeView;
71 private import TestTreeView1;
72 private import TestImage;
73 private import TestThemes;
74 private import TestAspectFrame;
75 private import TestIdle;
76 private import TTextView;
77 //private import TEditableCells;
78 
79 private import gtk.MenuItem;
80 private import gtk.Widget;
81 private import gtk.MenuBar;
82 private import gtk.Notebook;
83 private import gtk.ComboBoxText;
84 private import gtk.FileChooserDialog;
85 private import gtk.FontSelectionDialog;
86 private import gtk.ColorSelectionDialog;
87 private import gtk.Button;
88 private import gtk.VBox;
89 private import gtk.MessageDialog;
90 private import gtk.Frame;
91 private import gtk.HButtonBox;
92 private import gtk.Statusbar;
93 private import gtk.Menu;
94 private import gtk.HandleBox;
95 private import gtk.Toolbar;
96 private import gtk.SeparatorToolItem;
97 private import gtk.ToolButton;
98 private import gtk.RadioButton;
99 private import gtk.CheckButton;
100 private import gtk.ToggleButton;
101 //private import gtk.ListItem;
102 private import gtk.HBox;
103 private import gtk.Arrow;
104 //private import gtk.ListG;
105 //private import gtk.OptionMenu;
106 private import gtk.ButtonBox;
107 private import gtk.Calendar;
108 private import gtk.VButtonBox;
109 private import gtk.SpinButton;
110 private import gtk.ListStore;
111 private import gtk.TreeIter;
112 private import gtk.TreeView;
113 private import gtk.TreeViewColumn;
114 private import gtk.CellRendererText;
115 //private import gtk.SListG;
116 //private import ddi.Drawable;
117 private import gtk.Window;
118 
119 private import gtk.ScrolledWindow;
120 private import gtk.MessageDialog;
121 
122 
123 
124 version(Tango) private import tango.core.Memory;
125 else version(D_Version2) private import core.memory;
126 else private import std.gc;
127 
128 private import glib.ListSG;
129 
130 private import glib.Str;
131 private import gtk.Label;
132 private import glib.ListG;
133 private import gtk.Paned;
134 private import gtk.HPaned;
135 private import gtk.VPaned;
136 
137 private import gtk.Calendar;
138 version(Tango) private import tango.io.Stdout;
139 else private import std.stdio;
140 private import gtk.VButtonBox;
141 private import gtk.FileChooserButton;
142 
143 private import gtk.AboutDialog;
144 private import gtk.Dialog;
145 
146 private import gtk.TreeStore;
147 private import gdk.Pixbuf;
148 private import gtk.ComboBox;
149 
150 private import gtk.TreePath;
151 private import gtk.CellRenderer;
152 private import gtk.CellRendererPixbuf;
153 
154 /**
155  * This tests the DUI widgets
156  */
157 
158 
159 class TestWindow : MainWindow
160 {
161 	/**
162 	 * Executed when the user tries to close the window
163 	 * @return true to refuse to close the window
164 	 */
165 	public: int windowDelete(GdkEvent* event, Widget widget)
166 	{
167 
168 		debug(events) printf("TestWindow.widgetDelete : this and widget to delete %X %X\n",this,window);
169 		MessageDialog d = new MessageDialog(
170 										this,
171 										GtkDialogFlags.MODAL,
172 										MessageType.QUESTION,
173 										ButtonsType.YES_NO,
174 										"Are you sure you want' to exit these GtkDTests?");
175 		int responce = d.run();
176 		if ( responce == ResponseType.YES )
177 		{
178 			stdlib.exit(0);
179 		}
180 		d.destroy();
181 		return true;
182 	}
183 
184 	void anyButtonExits(Button button)
185 	{
186 			stdlib.exit(0);
187 	}
188 
189 	this()
190 	{
191 		super("GtkD tests");
192 		setup();
193 		showAll();
194 
195 		string versionCompare = Version.checkVersion(3,0,0);
196 
197 		if ( versionCompare.length > 0 )
198 		{
199 			MessageDialog d = new MessageDialog(this,
200 										GtkDialogFlags.MODAL,
201 										MessageType.WARNING,
202 										ButtonsType.OK,
203 										"GtkD : Gtk+ version missmatch\n" ~ versionCompare ~
204 										"\nYou might run into problems!"
205 										"\n\nPress OK to continue");
206 			d.run();
207 			d.destroy();
208 		}
209 	}
210 
211 	void setup()
212 	{
213 		//Frame.defaultBorder = 7;
214 
215 		VBox mainBox = new VBox(false,0);
216 		mainBox.packStart(getMenuBar(),false,false,0);
217 		mainBox.packStart(getToolbar(),false,false,0);
218 
219 		Notebook notebook = setNotebook();
220 		notebook.setBorderWidth(10);
221 		mainBox.packStart(notebook,true,true,0);
222 
223 		Button cancelButton = new Button(StockID.CANCEL, &anyButtonExits);
224 		Button exitButton = new Button(StockID.QUIT, &anyButtonExits);
225 		Button quitButton = new Button(StockID.OK, &anyButtonExits);
226 
227 		ButtonBox bBox = HButtonBox.createActionBox();
228 
229 		bBox.packEnd(exitButton,0,0,10);
230 		bBox.packEnd(cancelButton,0,0,10);
231 		bBox.packEnd(quitButton,0,0,10);
232 		mainBox.packStart(bBox,false,false,0);
233 
234 		Statusbar statusbar = new Statusbar();
235 
236 		mainBox.packStart(statusbar,false,true,0);
237 		add(mainBox);
238 
239 		notebook.appendPage(new TestEntries,"Entry");
240 		testEventBox(notebook);
241 
242 		testButtons(notebook);
243 
244 		notebook.appendPage(new TestStock,"Stock");
245 		testLists(notebook);
246 		testNotebook(notebook);
247 		testPaned(notebook);
248 		testDialogs(notebook);
249 		testViewport(notebook);
250 
251 		version(Windows)
252 		{
253 			// todo - threads are still broken on windows
254 		}
255 		else
256 		{
257 			testThreads(notebook);
258 		}
259 
260 		notebook.appendPage(new TestScales,"Scales");
261 			testSpinButton(notebook);
262 
263 		notebook.appendPage(new TestTreeView,"TreeView");
264 		notebook.appendPage(new TestTreeView1,"TreeView 1");
265 			testList(notebook);
266 
267 		version(linux) {
268 			// this block crashes gtk+ on win32 for some reason
269 			notebook.appendPage(new Frame(new TestDrawingArea,"Drawing Area"),"Drawing");
270 			notebook.appendPage(new TestText,"Text");
271 			notebook.appendPage(new TestImage(this),"Image");
272 			//TestThemes seems to be unfinished.
273 			//notebook.appendPage(new TestThemes(this),"Themes");
274 			notebook.appendPage(new TestAspectFrame(),"Aspect Frame");
275 			notebook.appendPage(new TestIdle(),"Idle");
276 			version(cairo)notebook.appendPage(new Clock(), "Cairo");
277 			gtkDemo(notebook);
278 		}
279 	}
280 
281 	MenuBar getMenuBar()
282 	{
283 
284 		AccelGroup accelGroup = new AccelGroup();
285 
286 		addAccelGroup(accelGroup);
287 
288 
289 		MenuBar menuBar = new MenuBar();
290 
291 		Menu menu = menuBar.append("_File");;
292 
293 		MenuItem item = new MenuItem(&onMenuActivate, "_New","file.new", true, accelGroup, 'n');
294 		//item.addAccelerator("activate",accelGroup,'n',GdkModifierType.CONTROL_MASK,GtkAccelFlags.VISIBLE);
295 
296 		menu.append(item);
297 		menu.append(new MenuItem(&onMenuActivate, "_Open","file.open", true, accelGroup, 'o'));
298 		menu.append(new MenuItem(&onMenuActivate, "_Close","file.close", true, accelGroup, 'c'));
299 		menu.append(new MenuItem(&onMenuActivate, "E_xit","file.exit", true, accelGroup, 'x'));
300 
301 
302 		menu = menuBar.append("_Edit");
303 
304 		menu.append(new MenuItem(&onMenuActivate,"_Find","edit.find", true, accelGroup, 'f'));
305 		menu.append(new MenuItem(&onMenuActivate,"_Search","edit.search", true, accelGroup, 's'));
306 
307 		menu = menuBar.append("_Help");
308 		menu.append(new MenuItem(&onMenuActivate,"_About","help.about", true, accelGroup, 'a',GdkModifierType.CONTROL_MASK|GdkModifierType.SHIFT_MASK));
309 
310 
311 
312 
313 		return menuBar;
314 	}
315 
316 	class GtkDAbout : AboutDialog
317 	{
318 		this()
319 		{
320 			string[] names;
321 			names ~= "Antonio Monteiro (binding/wrapping/proxying/decorating for D)";
322 			names ~= "www.gtk.org (base C library)";
323 
324 			setAuthors( names );
325 			setDocumenters( names );
326 			setArtists( names );
327 			setLicense("License is LGPL");
328 			setWebsite("http://lisdev.com");
329 		}
330 	}
331 
332 	void onMenuActivate(MenuItem menuItem)
333 	{
334 		string action = menuItem.getActionName();
335 		switch( action )
336 		{
337 			case "help.about":
338 				GtkDAbout dlg = new GtkDAbout();
339 				dlg.addOnResponse(&onDialogResponse);
340 				dlg.showAll();
341 				break;
342 			default:
343 				MessageDialog d = new MessageDialog(
344 					this,
345 					GtkDialogFlags.MODAL,
346 					MessageType.INFO,
347 					ButtonsType.OK,
348 					"You pressed menu item "~action);
349 				d.run();
350 				d.destroy();
351 			break;
352 		}
353 
354 	}
355 
356 	void onDialogResponse(int response, Dialog dlg)
357 	{
358 		if(response == GtkResponseType.CANCEL)
359 			dlg.destroy();
360 	}
361 
362 	Widget getToolbar()
363 	{
364 		HandleBox handleBox = new HandleBox();
365 		Toolbar toolbar = new Toolbar();
366 		toolbar.insert(new ToolButton(StockID.OPEN));
367 		toolbar.insert(new ToolButton(StockID.CLOSE));
368 		toolbar.insert(new SeparatorToolItem());
369 		toolbar.insert(new ToolButton(StockID.SAVE));
370 		toolbar.insert(new ToolButton(StockID.SAVE_AS));
371 
372 		handleBox.add(toolbar);
373 
374 		return handleBox;
375 
376 	}
377 
378 	Notebook setNotebook()
379 	{
380 		Notebook notebook = new Notebook();
381 		notebook.addOnSwitchPage(&onNotebookSwitchPage);
382 		notebook.setTabPos(PositionType.LEFT);
383 		return notebook;
384 	}
385 
386 	void onNotebookSwitchPage(Widget notePage, uint pageNumber, Notebook notebook)
387 	{
388 		//writefln("Notebook switch to page %s", pageNumber);
389 		// fullCollect helps finding objects that shouldn't have been collected
390 		version(Tango) GC.collect();
391 		else version(D_Version2) GC.collect();
392 		else std.gc.fullCollect();
393 		//writefln("exiting Notebook switch to page %s", pageNumber);
394 	}
395 
396 	//private import EventBox;
397 
398 	void testEventBox(Notebook notebook)
399 	{
400 
401 //		EventBox eventBox = new EventBox();
402 //		eventBox.add(new Label("label on event box"));
403 //		notebook.appendPage(eventBox,"Buttons");
404 
405 		//EventBox eventBox = new EventBox();
406 		//eventBox.add(new Label("label on event box"));
407 		notebook.appendPage(new Label("just a simple label"),new Label("label"));
408 
409 	}
410 
411 	void testButtons(Notebook notebook)
412 	{
413 
414 		Table table = new Table(2,12,0);
415 
416 		//ListSG listSG = new ListSG();
417 
418 		RadioButton radio1Button = new RadioButton(cast(ListSG)null,"Option 1");
419 		RadioButton radio2Button = new RadioButton(radio1Button,"Option 2");
420 		RadioButton radio3Button = new RadioButton(radio2Button,"Option 3");
421 		RadioButton radio4Button = new RadioButton(radio3Button,"Option 4");
422 		RadioButton radio5Button = new RadioButton(radio4Button,"Option 5");
423 		RadioButton radio6Button = new RadioButton(radio5Button,"Option 6");
424 		RadioButton radio7Button = new RadioButton(radio6Button,"Option 7");
425 		RadioButton radio8Button = new RadioButton(radio7Button,"Option 8");
426 		RadioButton radio9Button = new RadioButton(radio8Button,"Option 9");
427 		table.attach(radio1Button,0,1,0,1,AttachOptions.FILL,AttachOptions.FILL,4,4);
428 		table.attach(radio2Button,0,1,1,2,AttachOptions.FILL,AttachOptions.FILL,4,4);
429 		table.attach(radio3Button,0,1,2,3,AttachOptions.FILL,AttachOptions.FILL,4,4);
430 		table.attach(radio4Button,0,1,3,4,AttachOptions.FILL,AttachOptions.FILL,4,4);
431 		table.attach(radio5Button,0,1,4,5,AttachOptions.FILL,AttachOptions.FILL,4,4);
432 		table.attach(radio6Button,0,1,5,6,AttachOptions.FILL,AttachOptions.FILL,4,4);
433 		table.attach(radio7Button,0,1,6,7,AttachOptions.FILL,AttachOptions.FILL,4,4);
434 		table.attach(radio8Button,0,1,7,8,AttachOptions.FILL,AttachOptions.FILL,4,4);
435 		table.attach(radio9Button,0,1,8,9,AttachOptions.FILL,AttachOptions.FILL,4,4);
436 
437 		CheckButton editableButton = new CheckButton("editable");
438 		CheckButton visibleButton = new CheckButton("visible");
439 		table.attach(editableButton,0,1,9,10,AttachOptions.FILL,AttachOptions.FILL,4,4);
440 		table.attach(visibleButton,0,1,10,11,AttachOptions.FILL,AttachOptions.FILL,4,4);
441 
442 		ToggleButton toggleButton = new ToggleButton("Toggle this");
443 		table.attach(toggleButton,0,1,11,12,AttachOptions.FILL,AttachOptions.FILL,4,4);
444 
445 		Button button = new Button("test events");
446 		table.attach(button,0,1,12,13,AttachOptions.FILL,AttachOptions.FILL,4,4);
447 
448 		void bActivate(Button button)
449 		{
450 			version(Tango) Stdout("button Activate").newline;
451 			else printf("button Activate\n");
452 		}
453 		void bClicked(Button button)
454 		{
455 			version(Tango) Stdout("button Clicked").newline;
456 			else printf("button Clicked\n");
457 		}
458 		void bEnter(Button button)
459 		{
460 			version(Tango) Stdout("button Enter").newline;
461 			else printf("button Enter\n");
462 		}
463 		void bLeave(Button button)
464 		{
465 			version(Tango) Stdout("button Leave").newline;
466 			else printf("button Leave\n");
467 		}
468 		void bPressed(Button button)
469 		{
470 			version(Tango) Stdout("button Pressed").newline;
471 			else printf("button Pressed\n");
472 		}
473 		void bReleased(Button button)
474 		{
475 			version(Tango) Stdout("button Released").newline;
476 			else printf("button Released\n");
477 		}
478 		button.addOnActivate(&bActivate);
479 		button.addOnClicked(&bClicked);
480 		button.addOnEnter(&bEnter);
481 		button.addOnLeave(&bLeave);
482 		button.addOnPressed(&bPressed);
483 		button.addOnReleased(&bReleased);
484 
485 //		for ( ShadowType shadow = 0; shadow<=ShadowType.max ; shadow ++)
486 //		{
487 //			for ( DirectionType direction = 0 ; direction<=DirectionType.max ; direction++)
488 //			{
489 //				table.attach(new Arrow(direction,shadow),direction+1,direction+2,shadow,shadow+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
490 //				table.attach(new Arrow(direction,shadow),direction+1,direction+2,shadow,shadow+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
491 //				table.attach(new Arrow(direction,shadow),direction+1,direction+2,shadow,shadow+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
492 //				table.attach(new Arrow(direction,shadow),direction+1,direction+2,shadow,shadow+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
493 //			}
494 //		}
495 
496 
497 		notebook.appendPage(new Frame(table,"Buttons"),"Buttons");
498 
499 	}
500 
501 	class ComboStore : TreeStore
502 	{
503 		this()
504 		{
505 			//int* i = cast(int*)pixbufGetType();
506 
507 			GType[] columns;
508 			columns ~= GType.STRING;
509 			columns ~= GType.STRING;
510 			super(columns);
511 		}
512 
513 	}
514 
515 
516 	void testLists(Notebook notebook)
517 	{
518 		VBox mainBox = new VBox(false,3);
519 
520 		// comboBoxTextEntry from a list of strings
521 		mainBox.packStart(new Label("String combo"),false,true,0);
522 		static string[] cbList = ["item 1","item 2","item 3","item 4","item 5","item 6","item 7","item 8","item 9"];
523 		comboText = new ComboBoxText();
524 		string entry3 = "Combo box text entry 3";
525 		comboText.appendText("Combo box text entry 1");
526 		comboText.appendText("Combo box text entry 2");
527 		comboText.appendText(entry3);
528 		comboText.appendText("Combo box text entry 4");
529 		comboText.appendText("Combo box text entry 5");
530 		comboText.setActive(2);
531 
532 		ButtonBox actionBox1 = HButtonBox.createActionBox();
533 		actionBox1.packStart(comboText,false,false,0);
534 
535 		Button showCombo1 = new Button("ShowCombo", &showTextCombo);
536 		actionBox1.packStart(showCombo1,false,false,0);
537 
538 		mainBox.packStart(new Frame(actionBox1, "Text entry ComboBox"),false,false,0);
539 
540 
541 
542 		// TODO combo with widgets
543 
544 
545 		// new ComboBox + TreeModel
546 
547 		ComboStore comboStore = new ComboStore();
548 
549 		TreeIter iterFirst;	// to set the first active iter
550 		TreeIter iterChild;
551 		TreeIter iterTop = comboStore.append(null);
552 		comboStore.setValue(iterTop, 0, "Paganini" );
553 		iterFirst = comboStore.append(iterTop);
554 		comboStore.setValue(iterFirst, 0, "Nicolo" );
555 
556 		iterTop = comboStore.append(null);
557 		comboStore.setValue(iterTop, 0, "List" );
558 		iterChild = comboStore.append(iterTop);
559 		comboStore.setValue(iterChild, 0, "Franz" );
560 
561 		iterTop = comboStore.append(null);
562 		comboStore.setValue(iterTop, 0, "Beethoven" );
563 		iterChild = comboStore.append(iterTop);
564 		comboStore.setValue(iterChild, 0, "Ludwic" );
565 		iterChild = comboStore.append(iterTop);
566 		comboStore.setValue(iterChild, 0, "Maria" );
567 		iterChild = comboStore.append(iterTop);
568 		comboStore.setValue(iterChild, 0, "Van" );
569 
570 		iterTop = comboStore.append(null);
571 		comboStore.setValue(iterTop, 0, "Bach" );
572 		iterChild = comboStore.append(iterTop);
573 		comboStore.setValue(iterChild, 0, "Johann" );
574 		iterChild = comboStore.append(iterTop);
575 		comboStore.setValue(iterChild, 0, "Sebastian" );
576 
577 		ComboBox treeCombo = new ComboBox(comboStore);
578 		treeCombo.setWrapWidth(1);
579 		CellRenderer renderer = new CellRendererText();
580 		treeCombo.packStart(renderer, true);
581 		treeCombo.addAttribute(renderer, "text",0);
582 		
583 		// TODO something is wrong gettign the ier from the path
584 		//TreePath path = new TreePath("0,0");
585 		//comboStore.getIter(iterChild, path);
586 		//treeCombo.setActiveIter(iterChild);
587 		// use a previously set iter
588 		treeCombo.setActiveIter(iterFirst);
589 
590 		mainBox.packStart(new Frame(treeCombo,"Tree Combo box"),false,true,0);
591 
592 		simpleCombo = new ComboBoxText();
593 		simpleCombo.appendText("Top");
594 		simpleCombo.appendText("Bottom");
595 		simpleCombo.appendText("Left");
596 		simpleCombo.appendText("Right");
597 		simpleCombo.setActive(0);
598 
599 		// actions
600 
601 		ButtonBox actionBox = HButtonBox.createActionBox();
602 		actionBox.packStart(simpleCombo,false,false,0);
603 
604 		Button showCombo = new Button("ShowCombo", &showSimpleCombo);
605 		actionBox.packStart(showCombo,false,false,0);
606 
607 		mainBox.packStart(new Frame(actionBox, "Simple text list"),false,false,0);
608 
609 		notebook.appendPage(new Frame(mainBox,"Lists"),"Lists");
610 
611 	}
612 	ComboBoxText simpleCombo;
613 	ComboBoxText comboText;
614 
615 	void showTextCombo(Button button)
616 	{
617 		version(Tango) Stdout("Combo selected text = ")(comboText.getActiveText()).newline;
618 		else writefln("Combo selected text = %s",comboText.getActiveText());
619 	}
620 
621 	void showSimpleCombo(Button button)
622 	{
623 		version(Tango) Stdout("Combo selected text = ")(simpleCombo.getActiveText()).newline;
624 		else writefln("Combo selected text = %s",simpleCombo.getActiveText());
625 	}
626 
627 	class NB : Notebook
628 	{
629 		void itemActivated(MenuItem menu_item)
630 		{
631 			version(Tango) Stdout("NB.activateCallback").newline;
632 			else printf("NB.activateCallback\n");
633 		}
634 		void buttonClicked(Button button)
635 		{
636 			setCurrentPage(2);
637 		}
638 
639 		void switchPage(Notebook notebook, /*NotebookPage page,*/ guint pageNumber)
640 		{
641 			version(Tango) Stdout("new page = ")(pageNumber).newline;
642 			else printf("new page = %d\n",pageNumber);
643 		}
644 	}
645 
646 
647 	NB nb;
648 
649 	void posLeft(Button button)
650 	{
651 		nb.setTabPos(PositionType.LEFT);
652 	}
653 	void posRight(Button button)
654 	{
655 		nb.setTabPos(PositionType.RIGHT);
656 	}
657 	void posTop(Button button)
658 	{
659 		nb.setTabPos(PositionType.TOP);
660 	}
661 	void posBottom(Button button)
662 	{
663 		nb.setTabPos(PositionType.BOTTOM);
664 	}
665 
666 	/**
667 	 * tests notebook
668 	 * @param nodebook
669 	 */
670 	void testNotebook(Notebook notebook)
671 	{
672 
673       	nb = new NB();
674       	nb.setTabPos(PositionType.min);
675       	nb.popupEnable();
676 		nb.setShowBorder(false);
677 		nb.setBorderWidth(0);
678 
679       	class PageMove
680       	{
681       		Notebook notebook;
682       		PositionType toPos;
683       		this(Notebook notebook,PositionType toPos)
684       		{
685       			this.notebook = notebook;
686       			this.toPos = toPos;
687       		}
688       		void buttonClickedCallback()
689       		{
690       			notebook.setTabPos(toPos);
691       		}
692       	}
693 
694 		PageMove toLeft = new PageMove(nb,PositionType.LEFT);
695 		PageMove toRight = new PageMove(nb,PositionType.RIGHT);
696 		PageMove toTop = new PageMove(nb,PositionType.TOP);
697 		PageMove toBottom = new PageMove(nb,PositionType.BOTTOM);
698 
699 
700 		notebook.appendPage(nb,new Label("Notebook"));
701 
702       	for( char i = '1' ; i<'5' ; i++ )
703       	{
704       		Table table = new Table(2,2,true);
705 			Button left = new Button("Left "~ i);
706 			left.addOnClicked(&posLeft);
707 			Button right = new Button("Right "~ i);
708 			right.addOnClicked(&posRight);
709 			Button top = new Button("top "~ i);
710 			top.addOnClicked(&posTop);
711 			Button bottom = new Button("bottom "~ i);
712 			bottom.addOnClicked(&posBottom);
713 
714 			table.attach(top,0,1,0,1,AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
715 			table.attach(right,1,2,0,1,AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
716 			table.attach(left,0,1,1,2,AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
717 			table.attach(bottom,1,2,1,2,AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
718       		if ( i == 3 )
719 			{
720 
721 //				MenuItem menuItem = new MenuItem("menu page 3",&nb.itemActivated);
722 //
723 //				Menu menu = new Menu();
724 //				menu.append(new MenuItem("page 3.1",&nb.itemActivated));
725 //				menu.append(new MenuItem("page 3.2",&nb.itemActivated));
726 //				menu.append(new MenuItem("page 3.3",&nb.itemActivated));
727 //				menu.append(new MenuItem("page 3.4",&nb.itemActivated));
728 //
729 //				menuItem.setSubmenu(menu);
730 
731 				HBox box = new HBox(false,0);
732 				box.setBorderWidth(0);
733 				Button button = new Button("Page " ~ i ~ "\0");
734 				button.addOnClicked(&nb.buttonClicked);
735 				button.setBorderWidth(0);
736 				button.setRelief(ReliefStyle.NONE);
737 				CheckButton checkButton = new CheckButton();
738 				checkButton.setBorderWidth(0);
739 				box.packStart(checkButton,false,false,0);
740 				box.packStart(button,false,false,0);
741 				//nb.appendPageMenu(table,box,menuItem);
742 				//nb.appendPage(table,box);
743 			}
744 			else
745 			{
746 				nb.appendPage(table,new Label("Page " ~ i ~ "\0"));
747 			}
748       	}
749 		//nb.addOnSwitchPage(&nb.switchPage);
750 	}
751 
752 	void testPaned(Notebook notebook)
753 	{
754 
755 		Paned mainPaned = new VPaned();
756 		Paned p0 = mainPaned;
757 		Button p1;
758 		Paned p2;
759 
760 		bool h = true;
761 		for ( char c='1' ; c<='5' ; c++ )
762 		{
763 			p1 = new Button("Pane "~c);
764 			if ( h ) p2 = new HPaned();
765 			else p2 = new VPaned();
766 
767 			p0.add1(p1);
768 			p0.add2(p2);
769 			p0 = p2;
770 			h = !h;
771 		}
772 
773 		notebook.appendPage(mainPaned,new Label("Paned"));
774 
775 	}
776 
777 	FileChooserDialog fcd;
778 	FontSelectionDialog f;
779 	ColorSelectionDialog d;
780 
781 	void showFileChooser(Button button)
782 	{
783 		string[] a;
784 		ResponseType[] r;
785 		a ~= "Lets go!";
786 		a ~= "Please don't";
787 		r ~= ResponseType.OK;
788 		r ~= ResponseType.CANCEL;
789 		if ( fcd  is  null )
790 		{
791 			fcd = new FileChooserDialog("File Chooser", this, FileChooserAction.OPEN, a, r);
792 		}
793 
794 		fcd.setSelectMultiple(true);
795 		fcd.run();
796 //		writefln("file selected = %s",fcd.getFileName());
797 //
798 //		foreach ( int i, string selection ; fs.getSelections())
799 //		{
800 //			printf("File(s) selected [%d] %.*s\n",i,selection);
801 //		}
802 		fcd.hide();
803 	}
804 
805 	void showColor(Button button)
806 	{
807 		if ( d  is  null )
808 		{
809 			d = new ColorSelectionDialog("Select the color");
810 		}
811 		d.run();
812 		d.hide();
813 	}
814 
815 	void showCalendar(Button button)
816 	{
817 		Window calWin = new Window("Calendar");
818 		Calendar calendar = new Calendar();
819 		//calendar.setTitle("No Title");
820 		calWin.add(calendar);
821 		calWin.showAll();
822 	}
823 
824 	void showFont(Button button)
825 	{
826 		if ( f  is  null )
827 		{
828 			f = new FontSelectionDialog("Select the font");
829 		}
830 		f.run();
831 		string fontName = f.getFontName();
832 		f.hide();
833 	}
834 
835 	Button fontButton;
836 
837 	void testDialogs(Notebook notebook)
838 	{
839 
840 		ButtonBox bBox = VButtonBox.createActionBox();
841 
842 		if ( fcd  is  null )
843 		{
844 			fcd = new FileChooserDialog("File Chooser", this, FileChooserAction.OPEN);
845 		}
846 
847 
848 		Button fileChooser = new Button("File Chooser", &showFileChooser);
849 		FileChooserButton fcb = new FileChooserButton(fcd);
850 
851 		Button color = new Button("Color Dialog", &showColor);
852 		Button calendar = new Button("Calendar Dialog", &showCalendar);
853 		fontButton = new Button("Font Dialog", &showFont);
854 		//fontButton.modifyFont("[Newspaper][16]");
855 
856 		bBox.packStart(fileChooser,0,0,10);
857 		bBox.packStart(fcb,0,0,10);
858 		bBox.packStart(color,0,0,10);
859 		bBox.packStart(calendar,0,0,10);
860 		bBox.packStart(fontButton,0,0,10);
861 
862 		notebook.appendPage(new Frame(bBox,"Dialogs"),new Label("Dialogs"));
863 
864 	}
865 
866 	version(D_Version2)
867 		mixin("__gshared Button[] threadTestButtons;");
868 	else
869 		Button[] threadTestButtons;
870 
871 	static T1[] t1s;
872 
873 	class T1 : Thread
874 	{
875 		int num;
876 
877 		this(int num)
878 		{
879 			super(&run);
880 			this.num = num;
881 		}
882 
883 		version(DRuntime) void run()
884 		{
885 			runCommon();
886 		}
887 		else override int run()
888 		{
889 			return runCommon();
890 		}
891 
892 
893 		int runCommon()
894 		{
895 			while(1)
896 			{
897 				version(Tango) size_t buttonNum = Kiss.instance.natural(threadTestButtons.length);
898 				else version(D_Version2) size_t buttonNum = uniform(0, threadTestButtons.length);
899 				else size_t buttonNum = rand()%threadTestButtons.length;
900 				Button button = threadTestButtons[buttonNum];
901 
902 				threadsEnter();
903 				button.removeAll();
904 				version(Tango) button.setLabel( (new Layout!(char))("{}", num));
905 				else           button.setLabel(std..string.format("%s", num));
906 				threadsLeave();
907 				yield();
908 			}
909 			assert(0);
910 		}
911 	}
912 
913 	void testThreads(Notebook notebook)
914 	{
915 
916 		Table grid = new Table(8,8,0);
917 		for ( int i = 0 ; i<8 ; i++)
918 		{
919 			for ( int j = 0 ; j<8; j++)
920 			{
921 				version(Tango) Button button = new Button((new Layout!(char))("{}",(j+8*i)));
922 				else Button button = new Button(std..string.format("%s",(j+8*i)));
923 				threadTestButtons ~= button;
924 				grid.attach( button,
925 							i,i+1,
926 							j,j+1,
927 							AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
928 
929 				t1s ~= new T1(j+8*i);
930 			}
931 		}
932 
933 		void stop(Button button)
934 		{
935 			foreach ( T1 t ; t1s )
936 			{
937 				version(Tango)
938 				{
939 					if ( t.isRunning() )
940 					{
941 						t.sleep(100.0);
942 					}
943 				}
944 				else version(D_Version2)
945 				{
946 					if ( t.isRunning() )
947 					{
948 						t.sleep(dur!("hnsecs")(100));
949 					}
950 				}
951 				else switch( t.getState() )
952 				{
953 					case Thread.TS.RUNNING:
954 						t.pause();
955 						break;
956 
957 					default:
958 						break;
959 				}
960 			}
961 		}
962 
963 		void go(Button button)
964 		{
965 			foreach ( T1 t ; t1s )
966 			{
967 				version(DRuntime)
968 				{
969 					if ( !t.isRunning() )
970 					{
971 						t.start();
972 					}
973 				}
974 				else switch( t.getState() )
975 				{
976 					case Thread.TS.INITIAL:
977 						t.start();
978 						break;
979 
980 					default:
981 						t.resume();
982 						break;
983 				}
984 			}
985 		}
986 
987 		VBox vbox = new VBox(false, 2);
988 		vbox.packStart(grid, true, true,2);
989 		ButtonBox actions = HButtonBox.createActionBox();
990 
991 		Button button = new Button(StockID.STOP, &stop);
992 		actions.packStart(button, false, false, 7);
993 		button = new Button(StockID.OK, &go);
994 		actions.packStart(button, false, false, 7);
995 
996 		vbox.packStart(actions, false, false, 2);
997 
998 		notebook.appendPage(vbox,new Label("Threads"));
999 	}
1000 
1001 	void testViewport(Notebook notebook)
1002 	{
1003 
1004 		Table grid = new Table(21,21,0);
1005 		for ( int i = 0 ; i<21 ; i++)
1006 		{
1007 			for ( int j = 0 ; j<21; j++)
1008 			{
1009 				if ( (i == j) || (i+j==20) || (i==10) || (j==10))
1010 				{
1011 					grid.attach(
1012 						new Button("Button"),
1013 							i,i+1,
1014 							j,j+1,
1015 							AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
1016 				}
1017 			}
1018 		}
1019 
1020 		ScrolledWindow sw = new ScrolledWindow(null,null);
1021 		sw.addWithViewport(grid);
1022 		notebook.appendPage(sw,new Label("Viewport"));
1023 	}
1024 
1025 	void testSpinButton(Notebook notebook)
1026 	{
1027 		Table table = new Table(2,1,false);
1028 
1029 		SpinButton spin = new SpinButton(new Adjustment(1.0, 0.0, 100.0, 1.0, 10.0, 0),1,0);
1030 		table.attach(new Label("Spin this:"),0,1,0,1,AttachOptions.FILL,AttachOptions.FILL,4,4);
1031 		table.attach(spin,1,2,0,1,AttachOptions.FILL,AttachOptions.FILL,4,4);
1032 
1033 		notebook.appendPage(table,"Spin Buttons");
1034 
1035 	}
1036 
1037 	void testList(Notebook notebook)
1038 	{
1039 		debug(Tango) Stdout("TestWindow.testList 1").newline;
1040 
1041 		class TestListStore : ListStore
1042 		{
1043 			this()
1044 			{
1045 				static GType [3] columns = [
1046 								GType.STRING,
1047 								GType.STRING,
1048 								GType.STRING
1049 								];
1050 				super(columns);
1051 			}
1052 		}
1053 
1054 		TestListStore testListStore = new TestListStore();
1055 
1056 		TreeIter iterTop = testListStore.createIter();
1057 
1058 		static int [3] cols = [0,1,2];
1059 		string[] vals;
1060 		vals ~= "Antonio";
1061 		vals ~= "Canada";
1062 		vals ~= "Ontario";
1063 		testListStore.set(iterTop,cols,vals);
1064 
1065 		testListStore.append(iterTop);
1066 		string[] vals1;
1067 		vals1 ~= "John Reimer";
1068 		vals1 ~= "Canada";
1069 		vals1 ~= "BC";
1070 		testListStore.set(iterTop,cols,vals1);
1071 
1072 		testListStore.append(iterTop);
1073 		string[] vals2;
1074 		vals2 ~= "Friend of GtkD 2";
1075 		vals2 ~= "Poland";
1076 		vals2 ~= "Torun";
1077 		testListStore.set(iterTop,cols,vals2);
1078 
1079 		testListStore.append(iterTop);
1080 		string[] vals3;
1081 		vals3 ~= "Friend of GtkD 3";
1082 		vals3 ~= "Norway";
1083 		vals3 ~= "Norway";
1084 		testListStore.set(iterTop,cols,vals3);
1085 
1086 		TreeView treeView = new TreeView(testListStore);
1087 		treeView.setHeadersClickable(true);
1088 		treeView.setRulesHint(true);
1089 
1090 		//CellRendererText cellText = new CellRendererText();
1091 		TreeViewColumn column = new TreeViewColumn("Author",new CellRendererText(),"text", 0);
1092 		treeView.appendColumn(column);
1093 		column.setResizable(true);
1094 		column.setReorderable(true);
1095 		column.setSortColumnId(0);
1096 		column.setSortIndicator(true);
1097 
1098 		column = new TreeViewColumn("Country",new CellRendererText(),"text", 1);
1099 		treeView.appendColumn(column);
1100 		column.setResizable(true);
1101 		column.setReorderable(true);
1102 		column.setSortColumnId(1);
1103 		column.setSortIndicator(true);
1104 
1105 		column = new TreeViewColumn("Province",new CellRendererText(),"text", 2);
1106 		treeView.appendColumn(column);
1107 		column.setResizable(true);
1108 		column.setReorderable(true);
1109 		column.setSortColumnId(2);
1110 		column.setSortIndicator(true);
1111 
1112 		//notebook.appendPage(treeView,"ListView");
1113 		ScrolledWindow sw = new ScrolledWindow(null,null);
1114 		sw.addWithViewport(treeView);
1115 		notebook.appendPage(sw,"ListView");
1116 
1117 		debug(Tango) Stdout("TestWindow.testList END").newline;
1118 	}
1119 
1120 	void testDelete(Notebook notebook)
1121 	{
1122 //		ulong l = 0;
1123 //		while(true)
1124 //		{
1125 //			int i =0;
1126 //			while(i<1 )
1127 //			{
1128 //				//Button b = new Button("Button");
1129 //				Drawable d = getDrawable();
1130 //				delete d;
1131 //				++i;
1132 //			}
1133 //			++l;
1134 //			printf("testDelete count = %d\n\0",l*i);
1135 //		}
1136 
1137 	}
1138 
1139 	void gtkDemo(Notebook notebook)
1140 	{
1141 		debug(Tango) Stdout("TestWindow.gtkDemo 1").newline;
1142 		void showTTextView(Button button)
1143 		{
1144 			new TTextView();
1145 		}
1146 
1147 		debug(Tango) Stdout("TestWindow.gtkDemo 2").newline;
1148 		void showTEditableCells(Button button)
1149 		{
1150 		//	new TEditableCells();
1151 		}
1152 
1153 		debug(Tango) Stdout("TestWindow.gtkDemo 3").newline;
1154 		ButtonBox vBBox = VButtonBox.createActionBox();
1155 
1156 		debug(Tango) Stdout("TestWindow.gtkDemo 4").newline;
1157 		Button button = new Button("Text View");
1158 		button.addOnClicked(&showTTextView);
1159 		vBBox.packStart(button,false,false,4);
1160 
1161 		debug(Tango) Stdout("TestWindow.gtkDemo 5").newline;
1162 		button = new Button("Editable Cells");
1163 		button.addOnClicked(&showTEditableCells);
1164 		vBBox.packStart(button,false,false,4);
1165 
1166 		debug(Tango) Stdout("TestWindow.gtkDemo 6").newline;
1167 		notebook.appendPage(vBBox,"gtk-demo");
1168 
1169 		debug(Tango) Stdout("TestWindow.gtkDemo 7").newline;
1170 
1171 	}
1172 
1173 }
1174 
1175 private import gtkc.Loader;
1176 
1177 void main(string[] args)
1178 {
1179 	Linker.dumpLoadLibraries();
1180 	Linker.dumpFailedLoads();
1181 
1182 	version(Windows)
1183 	{
1184 		// todo threads are still broken on windows...
1185 		Main.init(args);
1186 	}
1187 	else
1188 	{
1189 		Main.initMultiThread(args);
1190 	}
1191 
1192 	TestWindow window = new TestWindow();
1193 
1194 	debug(1)writefln("before Main.run");
1195 	Main.run();
1196 	debug(1)writefln("after Main.run");
1197 }