alternative to assert
[gtkD.git] / gtkD / demos / gtkD / TestWindow.d
blobcfe4b5207def310a0dff3a42e64698a8c6b302d5
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 2.1 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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 module gtkD.TestWindow;
21 //debug = 1;
23 //version=cairo;
25 version(cairo)private import cairo.clock;
27 private import gtk.Version;
28 private import gtk.Table;
30 version(Tango)private import tango.text.convert.Layout;
31 version(Tango) private import tango.core.Thread;
32 else private import std.thread;
33 version(Tango) private import tango.math.Random;
34 else private import std.random;
36 import gdk.Threads;
39 private import gtkc.gtktypes;
40 private import gtk.GtkD;
41 private import gtk.MainWindow;
42 private import gtk.Adjustment;
43 private import gtk.AccelGroup;
45 private import gtkD.TestEntries;
47 //private import gtkD.gtkDTree;
48 private import gtkD.TestStock;
49 private import gtkD.TestDrawingArea;
50 private import gtkD.TestScales;
51 private import gtkD.TestText;
52 //private import gtkD.TestTree;
53 private import gtkD.TestTreeView;
54 private import gtkD.TestTreeView1;
55 private import gtkD.TestImage;
56 private import gtkD.TestThemes;
57 private import gtkD.TestAspectFrame;
58 private import gtkD.TestIdle;
59 private import gtkD.TTextView;
60 //private import gtkD.TEditableCells;
62 private import gtk.MenuItem;
63 private import gtk.Widget;
64 private import gtk.MenuBar;
65 private import gtk.Notebook;
66 //private import gtk.ComboBoxTextEntry;
67 private import gtk.FileChooserDialog;
68 private import gtk.FileSelection;
69 private import gtk.FontSelectionDialog;
70 private import gtk.ColorSelectionDialog;
71 private import gtk.InputDialog;
72 private import gtk.Button;
73 private import gtk.VBox;
74 private import gtk.MessageDialog;
75 private import gtk.Frame;
76 private import gtk.ObjectGtk;
77 private import gtk.HButtonBox;
78 private import gtk.Statusbar;
79 private import gtk.Menu;
80 private import gtk.HandleBox;
81 private import gtk.Toolbar;
82 private import gtk.RadioButton;
83 private import gtk.CheckButton;
84 private import gtk.ToggleButton;
85 //private import gtk.ListItem;
86 private import gtk.HBox;
87 private import gtk.Arrow;
88 //private import gtk.ListG;
89 //private import gtk.OptionMenu;
90 private import gtk.ButtonBox;
91 private import gtk.Calendar;
92 private import gtk.VButtonBox;
93 private import gtk.SpinButton;
94 private import gtk.ListStore;
95 private import gtk.TreeIter;
96 private import gtk.TreeView;
97 private import gtk.TreeViewColumn;
98 private import gtk.CellRendererText;
99 //private import gtk.SListG;
100 //private import ddi.Drawable;
101 private import gtk.Window;
103 private import gtk.ScrolledWindow;
104 private import gtk.MessageDialog;
108 version(Tango) private import tango.core.Memory;
109 else private import std.gc;
110 private import glib.ListSG;
112 private import gtk.Label;
113 private import glib.ListG;
114 private import gtk.ComboBoxEntry;
115 private import gtk.Paned;
116 private import gtk.HPaned;
117 private import gtk.VPaned;
119 private import gtk.Calendar;
120 version(Tango) private import tango.io.Stdout;
121 version(Tango) private import tango.stdc.stdio;
122 else private import std.stdio;
123 private import gtk.VButtonBox;
124 private import gtk.FileChooserButton;
125 private import gdk.Drawable;
127 private import gtk.AboutDialog;
129 private import gtk.TreeStore;
130 private import gdk.Pixbuf;
131 private import gtk.ComboBox;
133 private import gtk.TreePath;
134 private import gtk.CellRenderer;
135 private import gtk.CellRendererPixbuf;
138 * This tests the DUI widgets
142 class TestWindow : MainWindow
145 * Executed when the user tries to close the window
146 * @return true to refuse to close the window
148 public: int windowDelete(GdkEvent* event, Widget widget)
151 debug(events) printf("TestWindow.widgetDelete : this and widget to delete %X %X\n",this,window);
152 MessageDialog d = new MessageDialog(
153 this,
154 GtkDialogFlags.MODAL,
155 MessageType.QUESTION,
156 ButtonsType.YES_NO,
157 "Are you sure you want' to exit these GtkDTests?");
158 int responce = d.run();
159 if ( responce == ResponseType.GTK_RESPONSE_YES )
161 GtkD.exit(0);
163 d.destroy();
164 return true;
167 void anyButtonExits(Button button)
169 GtkD.exit(0);
172 void popupAbout(MenuItem item)
174 MessageDialog d = new MessageDialog(
175 this,
176 GtkDialogFlags.MODAL,
177 MessageType.INFO,
178 ButtonsType.OK,
179 "GtkD D (graphic) User Interface\n"
180 "D OO wrapper for GTK+\n"
181 "by Antonio Monteiro.\n"
182 "GtkD is released under the LGPL license\n"
183 "\n"
184 "Send comments and suggestions to gtkDoolkit@yahoo.ca\n"
185 "or go to the Dui group on dsource.org\n"
187 d.run();
188 d.destroy();
192 this()
194 debug(1) writefln("instantiating TestWindow 1");
195 super("GtkD tests");
196 debug(1) writefln("instantiating TestWindow 2");
197 setup();
198 debug(1) writefln("instantiating TestWindow 3");
199 showAll();
200 debug(1) writefln("instantiating TestWindow 4");
202 char[] versionCompare = Version.checkVersion(2,8,0);
203 debug(1) writefln("instantiating TestWindow 5");
204 if ( versionCompare.length > 0 )
206 MessageDialog d = new MessageDialog(this,
207 GtkDialogFlags.MODAL,
208 MessageType.WARNING,
209 ButtonsType.OK,
210 "GtkD : Gtk+ version missmatch\n" ~ versionCompare ~
211 "\nYou might run into problems!"
212 "\n\nPress OK to continue");
213 debug(1) writefln("instantiating TestWindow 6");
214 d.run();
215 debug(1) writefln("instantiating TestWindow 7");
216 d.destroy();
218 debug(1) writefln("instantiating TestWindow 8");
222 void setup()
224 debug(1)writefln("TestWindow.setup 1");
225 //Frame.defaultBorder = 7;
228 VBox mainBox = new VBox(false,3);
229 //Table table = new Table(1,5,false);
230 //table.attach(getMenuBar(),0,1,0,1,AttachOptions.EXPAND,AttachOptions.SHRINK,0,0); // adding to the window
231 mainBox.packStart(getMenuBar(),false,false,0);
232 //table.attach(getToolbar(),0,1,1,2,AttachOptions.EXPAND,AttachOptions.SHRINK,0,0);
233 mainBox.packStart(getToolbar(),false,false,0);
235 debug(1)writefln("TestWindow.setup 2");
237 Notebook notebook = setNotebook();
238 notebook.setBorderWidth(10);
239 //table.attach(notebook,0,1,2,3,AttachOptions.EXPAND,AttachOptions.FILL,4,4);
240 mainBox.packStart(notebook,true,true,0);
242 Button cancelButton = new Button(StockID.CANCEL, &anyButtonExits);
243 Button exitButton = new Button(StockID.QUIT, &anyButtonExits);
244 Button quitButton = new Button(StockID.OK, &anyButtonExits);
246 ButtonBox bBox = HButtonBox.createActionBox();
248 debug(1)writefln("TestWindow.setup 3");
250 bBox.packEnd(exitButton,0,0,10);
251 bBox.packEnd(cancelButton,0,0,10);
252 bBox.packEnd(quitButton,0,0,10);
253 //table.attach(bBox,0,1,3,4,AttachOptions.EXPAND,AttachOptions.SHRINK,0,0);
254 mainBox.packStart(bBox,false,false,0);
256 debug(1)writefln("TestWindow.setup 4");
258 Statusbar statusbar = new Statusbar();
260 debug(1)writefln("TestWindow.setup 5");
262 //table.attach(statusbar,0,1,4,5,AttachOptions.EXPAND,AttachOptions.EXPAND,0,0);
263 //add(table);
264 //mainBox.packStart(table,true,true,0);
266 mainBox.packStart(statusbar,false,true,0);
267 add(mainBox);
268 debug(1)writefln("TestWindow.setup 5.1");
269 notebook.appendPage(new TestEntries,"Entry");
270 testEventBox(notebook);
271 testButtons(notebook);
272 debug(1)writefln("TestWindow.setup 5.2");
273 notebook.appendPage(new TestStock,"Stock");
274 testLists(notebook);
275 testNotebook(notebook);
276 debug(1)writefln("TestWindow.setup 5.3");
277 testPaned(notebook);
278 testDialogs(notebook);
279 testViewport(notebook);
280 version(Win32)
282 // todo - threads are still broken on windows
284 else
286 testThreads(notebook);
288 debug(1)writefln("TestWindow.setup 5.4");
289 notebook.appendPage(new TestScales,"Scales");
290 testSpinButton(notebook);
291 debug(1)writefln("TestWindow.setup 5.5");
292 notebook.appendPage(new Label("Deprecated,\nuse TreeView\ninstead"),"Tree");
293 notebook.appendPage(new TestTreeView,"TreeView");
294 notebook.appendPage(new TestTreeView1,"TreeView 1");
295 debug(1)writefln("TestWindow.setup 5.6");
296 testList(notebook);
297 notebook.appendPage(new Frame(new TestDrawingArea,"Drawing Area"),"Drawing");
298 debug(1)writefln("TestWindow.setup 5.7");
299 notebook.appendPage(new TestText,"Text");
300 notebook.appendPage(new TestImage(this),"Image");
301 //notebook.appendPage(new TestThemes(this),"Themes");
302 debug(1)writefln("TestWindow.setup 5.8");
303 notebook.appendPage(new TestAspectFrame(),"Aspect Frame");
304 notebook.appendPage(new TestIdle(),"Idle");
306 version(cairo)notebook.appendPage(new Clock(), "Cairo");
308 gtkDemo(notebook);
309 debug(1)writefln("TestWindow.setup 6");
313 MenuBar getMenuBar()
316 AccelGroup accelGroup = new AccelGroup();
318 addAccelGroup(accelGroup);
321 MenuBar menuBar = new MenuBar();
323 Menu menu = menuBar.append("_File");;
325 MenuItem item = new MenuItem(&onMenuActivate, "_New","file.new", true, accelGroup, 'n');
326 //item.addAccelerator("activate",accelGroup,'n',GdkModifierType.CONTROL_MASK,GtkAccelFlags.VISIBLE);
328 menu.append(item);
329 menu.append(new MenuItem(&onMenuActivate, "_Open","file.open", true, accelGroup, 'o'));
330 menu.append(new MenuItem(&onMenuActivate, "_Close","file.close", true, accelGroup, 'c'));
331 menu.append(new MenuItem(&onMenuActivate, "E_xit","file.exit", true, accelGroup, 'x'));
334 menu = menuBar.append("_Edit");
335 menu.append(new MenuItem(&onMenuActivate,"_Find","edit.find", true, accelGroup, 'f'));
336 menu.append(new MenuItem(&onMenuActivate,"_Search","edit.search", true, accelGroup, 's'));
338 menu = menuBar.append("_Help");
339 menu.append(new MenuItem(&onMenuActivate,"_About","help.about", true, accelGroup, 'a',GdkModifierType.CONTROL_MASK|GdkModifierType.SHIFT_MASK));
344 return menuBar;
347 class GtkDAbout : AboutDialog
349 this()
351 char** names = (new char*[2]).ptr;
352 int i = 0;
353 names[i++] = cast(char*)"Antonio Monteiro (binding/wrapping/proxying/decorating for D)";
354 names[i++] = cast(char*)"www.gtk.org (base C library)";
355 setAuthors(names);
356 setDocumenters(names);
357 setArtists(names);
358 setLicense("License is LGPL");
359 setWebsite("http://lisdev.com");
363 void onMenuActivate(MenuItem menuItem)
365 char[] action = menuItem.getActionName();
366 switch( action )
368 case "help.about":
369 (new AboutDialog()).showAll();
370 // MessageDialog d = new MessageDialog(
371 // this,
372 // GtkDialogFlags.MODAL,
373 // MessageType.INFO,
374 // ButtonsType.OK,
375 // "DUI D (graphic) User Interface\n"
376 // "an implementation through GTK+\n"
377 // "by Antonio Monteiro.\n"
378 // "DUI is released under the LGPL license\n"
379 // "\n"
380 // "Send comments and suggestions to gtkDoolkit@yahoo.ca\n"
381 // "or go to the yahoo group\n"
382 // "http://groups.yahoo.com/group/gtkDoolkit\n"
383 // "(Group email: gtkDoolkit@yahoogroups.com)\n"
384 // "\n"
385 // "See detailed information at DUI home page\n"
386 // "http://ca.geocities.com/gtkDoolkit\n"
387 // );
388 // d.run();
389 // d.destroy();
390 break;
391 default:
392 MessageDialog d = new MessageDialog(
393 this,
394 GtkDialogFlags.MODAL,
395 MessageType.INFO,
396 ButtonsType.OK,
397 "You pressed menu item "~action);
398 d.run();
399 d.destroy();
400 break;
405 Widget getToolbar()
407 HandleBox handleBox = new HandleBox();
408 Toolbar toolbar = new Toolbar();
409 toolbar.appendWidget(new Button(StockID.OPEN, true),"Toolbar button 1","Private text 1");
410 toolbar.appendWidget(new Button(StockID.CLOSE, true),"Toolbar button 2","Private text 2");
411 toolbar.appendSpace();
412 toolbar.appendWidget(new Button(StockID.SAVE, true),"Toolbar button 3","Private text 3");
413 toolbar.appendWidget(new Button(StockID.SAVE_AS, true),"Toolbar button 4","Private text 4");
415 handleBox.add(toolbar);
417 return handleBox;
421 Notebook setNotebook()
423 Notebook notebook = new Notebook();
424 notebook.addOnSwitchPage(&onNotebookSwitchPage);
425 notebook.setTabPos(PositionType.LEFT);
426 return notebook;
429 void onNotebookSwitchPage(GtkNotebookPage *notePage, uint pageNumber, Notebook notebook)
431 //writefln("Notebook switch to page %s", pageNumber);
432 // fullCollect helps finding objects that shouldn't have been collected
433 version(Tango) { /*??? no fullCollect on tango ???*/}
434 else std.gc.fullCollect();
435 //writefln("exiting Notebook switch to page %s", pageNumber);
438 //private import gtkD.EventBox;
440 void testEventBox(Notebook notebook)
443 // EventBox eventBox = new EventBox();
444 // eventBox.add(new Label("label on event box"));
445 // notebook.appendPage(eventBox,"Buttons");
447 //EventBox eventBox = new EventBox();
448 //eventBox.add(new Label("label on event box"));
449 notebook.appendPage(new Label("just a simple label"),new Label("label"));
453 void testButtons(Notebook notebook)
456 Table table = new Table(2,12,0);
458 //ListSG listSG = new ListSG();
460 RadioButton radio1Button = new RadioButton(cast(ListSG)null,"Option 1");
461 RadioButton radio2Button = new RadioButton(radio1Button,"Option 2");
462 RadioButton radio3Button = new RadioButton(radio2Button,"Option 3");
463 RadioButton radio4Button = new RadioButton(radio3Button,"Option 4");
464 RadioButton radio5Button = new RadioButton(radio4Button,"Option 5");
465 RadioButton radio6Button = new RadioButton(radio5Button,"Option 6");
466 RadioButton radio7Button = new RadioButton(radio6Button,"Option 7");
467 RadioButton radio8Button = new RadioButton(radio7Button,"Option 8");
468 RadioButton radio9Button = new RadioButton(radio8Button,"Option 9");
469 table.attach(radio1Button,0,1,0,1,AttachOptions.FILL,AttachOptions.FILL,4,4);
470 table.attach(radio2Button,0,1,1,2,AttachOptions.FILL,AttachOptions.FILL,4,4);
471 table.attach(radio3Button,0,1,2,3,AttachOptions.FILL,AttachOptions.FILL,4,4);
472 table.attach(radio4Button,0,1,3,4,AttachOptions.FILL,AttachOptions.FILL,4,4);
473 table.attach(radio5Button,0,1,4,5,AttachOptions.FILL,AttachOptions.FILL,4,4);
474 table.attach(radio6Button,0,1,5,6,AttachOptions.FILL,AttachOptions.FILL,4,4);
475 table.attach(radio7Button,0,1,6,7,AttachOptions.FILL,AttachOptions.FILL,4,4);
476 table.attach(radio8Button,0,1,7,8,AttachOptions.FILL,AttachOptions.FILL,4,4);
477 table.attach(radio9Button,0,1,8,9,AttachOptions.FILL,AttachOptions.FILL,4,4);
479 CheckButton editableButton = new CheckButton("editable");
480 CheckButton visibleButton = new CheckButton("visible");
481 table.attach(editableButton,0,1,9,10,AttachOptions.FILL,AttachOptions.FILL,4,4);
482 table.attach(visibleButton,0,1,10,11,AttachOptions.FILL,AttachOptions.FILL,4,4);
484 ToggleButton toggleButton = new ToggleButton("Toggle this");
485 table.attach(toggleButton,0,1,11,12,AttachOptions.FILL,AttachOptions.FILL,4,4);
487 Button button = new Button("test events");
488 table.attach(button,0,1,12,13,AttachOptions.FILL,AttachOptions.FILL,4,4);
490 void bActivate(Button button)
492 printf("button Activate\n");
494 void bClicked(Button button)
496 printf("button Clicked\n");
498 void bEnter(Button button)
500 printf("button Enter\n");
502 void bLeave(Button button)
504 printf("button Leave\n");
506 void bPressed(Button button)
508 printf("button Pressed\n");
510 void bReleased(Button button)
512 printf("button Released\n");
514 button.addOnActivate(&bActivate);
515 button.addOnClicked(&bClicked);
516 button.addOnEnter(&bEnter);
517 button.addOnLeave(&bLeave);
518 button.addOnPressed(&bPressed);
519 button.addOnReleased(&bReleased);
521 // for ( ShadowType shadow = 0; shadow<=ShadowType.max ; shadow ++)
522 // {
523 // for ( DirectionType direction = 0 ; direction<=DirectionType.max ; direction++)
524 // {
525 // table.attach(new Arrow(direction,shadow),direction+1,direction+2,shadow,shadow+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
526 // table.attach(new Arrow(direction,shadow),direction+1,direction+2,shadow,shadow+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
527 // table.attach(new Arrow(direction,shadow),direction+1,direction+2,shadow,shadow+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
528 // table.attach(new Arrow(direction,shadow),direction+1,direction+2,shadow,shadow+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
529 // }
530 // }
533 notebook.appendPage(new Frame(table,"Buttons"),"Buttons");
537 class ComboStore : TreeStore
539 this()
541 //int* i = cast(int*)pixbufGetType();
543 GType[] columns;
544 columns ~= GType.STRING;
545 columns ~= GType.STRING;
546 super(columns);
552 void testLists(Notebook notebook)
554 VBox mainBox = new VBox(false,3);
556 // comboBoxTextEntry from a list of strings
557 mainBox.packStart(new Label("String combo"),false,true,0);
558 static char[][] cbList = ["item 1","item 2","item 3","item 4","item 5","item 6","item 7","item 8","item 9"];
559 comboText = new ComboBoxEntry();
560 char[] entry3 = "Combo box text entry 3";
561 comboText.appendText("Combo box text entry 1");
562 comboText.appendText("Combo box text entry 2");
563 comboText.appendText(entry3);
564 comboText.appendText("Combo box text entry 4");
565 comboText.appendText("Combo box text entry 5");
566 comboText.setActive(2);
568 ButtonBox actionBox1 = HButtonBox.createActionBox();
569 actionBox1.packStart(comboText,false,false,0);
571 Button showCombo1 = new Button("ShowCombo", &showTextCombo);
572 actionBox1.packStart(showCombo1,false,false,0);
574 mainBox.packStart(new Frame(actionBox1, "Text entry ComboBox"),false,false,0);
578 // TODO combo with widgets
581 // new ComboBox + TreeModel
583 ComboStore comboStore = new ComboStore();
585 TreeIter iterFirst; // to set the first active iter
586 TreeIter iterChild;
587 TreeIter iterTop = comboStore.append(null);
588 comboStore.setValue(iterTop, 0, "Paganini" );
589 iterFirst = comboStore.append(iterTop);
590 comboStore.setValue(iterFirst, 0, "Nicolo" );
592 iterTop = comboStore.append(null);
593 comboStore.setValue(iterTop, 0, "List" );
594 iterChild = comboStore.append(iterTop);
595 comboStore.setValue(iterChild, 0, "Franz" );
597 iterTop = comboStore.append(null);
598 comboStore.setValue(iterTop, 0, "Beethoven" );
599 iterChild = comboStore.append(iterTop);
600 comboStore.setValue(iterChild, 0, "Ludwic" );
601 iterChild = comboStore.append(iterTop);
602 comboStore.setValue(iterChild, 0, "Maria" );
603 iterChild = comboStore.append(iterTop);
604 comboStore.setValue(iterChild, 0, "Van" );
606 iterTop = comboStore.append(null);
607 comboStore.setValue(iterTop, 0, "Bach" );
608 iterChild = comboStore.append(iterTop);
609 comboStore.setValue(iterChild, 0, "Johann" );
610 iterChild = comboStore.append(iterTop);
611 comboStore.setValue(iterChild, 0, "Sebastian" );
613 ComboBox treeCombo = new ComboBox(comboStore);
614 treeCombo.setWrapWidth(1);
615 CellRenderer renderer = new CellRendererText();
616 treeCombo.packStart(renderer, true);
617 treeCombo.addAttribute(renderer, "text",0);
619 // TODO something is wrong gettign the ier from the path
620 //TreePath path = new TreePath("0,0");
621 //comboStore.getIter(iterChild, path);
622 //treeCombo.setActiveIter(iterChild);
623 // use a previously set iter
624 treeCombo.setActiveIter(iterFirst);
626 mainBox.packStart(new Frame(treeCombo,"Tree Combo box"),false,true,0);
628 simpleCombo = new ComboBox();
629 simpleCombo.appendText("Top");
630 simpleCombo.appendText("Bottom");
631 simpleCombo.appendText("Left");
632 simpleCombo.appendText("Right");
633 simpleCombo.setActive(0);
635 // actions
637 ButtonBox actionBox = HButtonBox.createActionBox();
638 actionBox.packStart(simpleCombo,false,false,0);
640 Button showCombo = new Button("ShowCombo", &showSimpleCombo);
641 actionBox.packStart(showCombo,false,false,0);
643 mainBox.packStart(new Frame(actionBox, "Simple text list"),false,false,0);
645 notebook.appendPage(new Frame(mainBox,"Lists"),"Lists");
648 ComboBox simpleCombo;
649 ComboBoxEntry comboText;
651 void showTextCombo(Button button)
653 printf("Combo selected text = %.*s\n",comboText.getActiveText());
656 void showSimpleCombo(Button button)
658 printf("Combo selected text = %.*s\n",simpleCombo.getActiveText());
661 class NB : Notebook
663 void itemActivated(MenuItem menu_item)
665 printf("NB.activateCallback\n");
667 void buttonClicked(Button button)
669 setCurrentPage(2);
672 void switchPage(Notebook notebook, /*NotebookPage page,*/ guint pageNumber)
674 printf("new page = %d\n",pageNumber);
679 NB nb;
681 void posLeft(Button button)
683 nb.setTabPos(PositionType.LEFT);
685 void posRight(Button button)
687 nb.setTabPos(PositionType.RIGHT);
689 void posTop(Button button)
691 nb.setTabPos(PositionType.TOP);
693 void posBottom(Button button)
695 nb.setTabPos(PositionType.BOTTOM);
699 * tests notebook
700 * @param nodebook
702 void testNotebook(Notebook notebook)
705 nb = new NB();
706 nb.setTabPos(PositionType.min);
707 nb.popupEnable();
708 nb.setShowBorder(false);
709 nb.setBorderWidth(0);
711 class PageMove
713 Notebook notebook;
714 PositionType toPos;
715 this(Notebook notebook,PositionType toPos)
717 this.notebook = notebook;
718 this.toPos = toPos;
720 void buttonClickedCallback()
722 notebook.setTabPos(toPos);
726 PageMove toLeft = new PageMove(nb,PositionType.LEFT);
727 PageMove toRight = new PageMove(nb,PositionType.RIGHT);
728 PageMove toTop = new PageMove(nb,PositionType.TOP);
729 PageMove toBottom = new PageMove(nb,PositionType.BOTTOM);
732 notebook.appendPage(nb,new Label("Notebook"));
734 for( char i = '1' ; i<'5' ; i++ )
736 Table table = new Table(2,2,true);
737 Button left = new Button("Left "~ i);
738 left.addOnClicked(&posLeft);
739 Button right = new Button("Right "~ i);
740 right.addOnClicked(&posRight);
741 Button top = new Button("top "~ i);
742 top.addOnClicked(&posTop);
743 Button bottom = new Button("bottom "~ i);
744 bottom.addOnClicked(&posBottom);
746 table.attach(top,0,1,0,1,AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
747 table.attach(right,1,2,0,1,AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
748 table.attach(left,0,1,1,2,AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
749 table.attach(bottom,1,2,1,2,AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
750 if ( i == 3 )
753 // MenuItem menuItem = new MenuItem("menu page 3",&nb.itemActivated);
755 // Menu menu = new Menu();
756 // menu.append(new MenuItem("page 3.1",&nb.itemActivated));
757 // menu.append(new MenuItem("page 3.2",&nb.itemActivated));
758 // menu.append(new MenuItem("page 3.3",&nb.itemActivated));
759 // menu.append(new MenuItem("page 3.4",&nb.itemActivated));
761 // menuItem.setSubmenu(menu);
763 HBox box = new HBox(false,0);
764 box.setBorderWidth(0);
765 Button button = new Button("Page " ~ i ~ "\0");
766 button.addOnClicked(&nb.buttonClicked);
767 button.setBorderWidth(0);
768 button.setRelief(ReliefStyle.NONE);
769 CheckButton checkButton = new CheckButton();
770 checkButton.setBorderWidth(0);
771 box.packStart(checkButton,false,false,0);
772 box.packStart(button,false,false,0);
773 //nb.appendPageMenu(table,box,menuItem);
774 //nb.appendPage(table,box);
776 else
778 nb.appendPage(table,new Label("Page " ~ i ~ "\0"));
781 //nb.addOnSwitchPage(&nb.switchPage);
784 void testPaned(Notebook notebook)
787 Paned mainPaned = new VPaned();
788 Paned p0 = mainPaned;
789 Button p1;
790 Paned p2;
792 bool h = true;
793 for ( char c='1' ; c<='5' ; c++ )
795 p1 = new Button("Pane "~c);
796 if ( h ) p2 = new HPaned();
797 else p2 = new VPaned();
799 p0.add1(p1);
800 p0.add2(p2);
801 p0 = p2;
802 h = !h;
805 notebook.appendPage(mainPaned,new Label("Paned"));
809 FileChooserDialog fcd;
810 FileSelection fs;
811 FontSelectionDialog f;
812 ColorSelectionDialog d;
813 InputDialog id;
815 void showFileChooser(Button button)
817 char[][] a;
818 ResponseType[] r;
819 a ~= "Lets go!";
820 a ~= "Please don't";
821 r ~= ResponseType.GTK_RESPONSE_OK;
822 r ~= ResponseType.GTK_RESPONSE_CANCEL;
823 if ( fcd is null )
825 fcd = new FileChooserDialog("File Chooser", this, FileChooserAction.OPEN, a, r);
828 fcd.getFileChooser().setSelectMultiple(true);
829 fcd.run();
830 // writefln("file selected = %s",fcd.getFileName());
832 // foreach ( int i, char[] selection ; fs.getSelections())
833 // {
834 // printf("File(s) selected [%d] %.*s\n",i,selection);
835 // }
836 fcd.hide();
841 void showFileSel(Button button)
843 if ( fs is null )
845 fs = new FileSelection("File Selection");
847 //fs.showAll();
848 //fs.setMultiple(true);
849 fs.run();
850 //printf("file selected = %.*s\n",fs.getFileName());
851 // char[][] selections = fs.getSelections();
852 // for ( int i=0 ;i<selections.length ; i++)
853 // {
854 // printf("File(s) selected [%d] %.*s\n",i,selections[i]);
855 // }
856 fs.hide();
859 void showColor(Button button)
861 if ( d is null )
863 d = new ColorSelectionDialog("Select the color");
865 d.run();
866 d.hide();
869 void showInput(Button button)
871 if ( id is null )
873 id = new InputDialog();
875 id.run();
876 id.hide();
879 void showCalendar(Button button)
881 Window calWin = new Window("Calendar");
882 Calendar calendar = new Calendar();
883 //calendar.setTitle("No Title");
884 calWin.add(calendar);
885 calWin.showAll();
888 void showFont(Button button)
890 if ( f is null )
892 f = new FontSelectionDialog("Select the font");
894 f.run();
895 char[] fontName = f.getFontName();
896 f.hide();
899 Button fontButton;
901 void testDialogs(Notebook notebook)
904 ButtonBox bBox = VButtonBox.createActionBox();
906 if ( fcd is null )
908 fcd = new FileChooserDialog("File Chooser", this, FileChooserAction.OPEN);
912 Button fileChooser = new Button("File Chooser", &showFileChooser);
913 FileChooserButton fcb = new FileChooserButton(fcd);
915 Button fileSel = new Button("File Selection", &showFileSel);
916 Button color = new Button("Color Dialog", &showColor);
917 Button input = new Button("Input Dialog", &showInput);
918 Button calendar = new Button("Calendar Dialog", &showCalendar);
919 fontButton = new Button("Font Dialog", &showFont);
920 //fontButton.modifyFont("[Newspaper][16]");
922 bBox.packStart(fileChooser,0,0,10);
923 bBox.packStart(fcb,0,0,10);
924 bBox.packStart(fileSel,0,0,10);
925 bBox.packStart(color,0,0,10);
926 bBox.packStart(input,0,0,10);
927 bBox.packStart(calendar,0,0,10);
928 bBox.packStart(fontButton,0,0,10);
930 notebook.appendPage(new Frame(bBox,"Dialogs"),new Label("Dialogs"));
934 Button[] threadTestButtons;
935 static T1[] t1s;
937 class T1 : Thread
939 int num;
941 this(int num)
943 super(&run);
944 this.num = num;
947 version(Tango) void run()
949 runCommon();
951 else int run()
953 return runCommon();
957 int runCommon()
959 version(Tango) Random random = new Random();
960 while(1)
962 version(Tango) int buttonNum = random.next(threadTestButtons.length);
963 else int buttonNum = rand()%threadTestButtons.length;
964 Button button = threadTestButtons[buttonNum];
965 gdkThreadsEnter();
966 button.removeAll();
967 version(Tango) button.setLabel( (new Layout!(char))("{}", num));
968 else button.setLabel(std.string.format("%s", num));
969 gdkThreadsLeave();
970 yield();
972 return 1;
976 void testThreads(Notebook notebook)
979 Table grid = new Table(8,8,0);
980 for ( int i = 0 ; i<8 ; i++)
982 for ( int j = 0 ; j<8; j++)
984 version(Tango) Button button = new Button((new Layout!(char))("{}",(j+8*i)));
985 else Button button = new Button(std.string.format("%s",(j+8*i)));
986 threadTestButtons ~= button;
987 grid.attach( button,
988 i,i+1,
989 j,j+1,
990 AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
992 t1s ~= new T1(j+8*i);
996 void stop(Button button)
998 foreach ( T1 t ; t1s )
1000 version(Tango)
1002 if ( t.isRunning() )
1004 t.sleep();
1007 else switch( t.getState() )
1009 case Thread.TS.RUNNING:
1010 t.pause();
1011 break;
1013 default:
1014 break;
1019 void go(Button button)
1021 foreach ( T1 t ; t1s )
1023 version(Tango)
1025 if ( !t.isRunning() )
1027 // todo t.go();
1028 // just send a interruped - how?
1031 else switch( t.getState() )
1033 case Thread.TS.INITIAL:
1034 t.start();
1035 break;
1037 default:
1038 t.resume();
1039 break;
1044 VBox vbox = new VBox(false, 2);
1045 vbox.packStart(grid, true, true,2);
1046 ButtonBox actions = HButtonBox.createActionBox();
1048 Button button = new Button(StockID.STOP, &stop);
1049 actions.packStart(button, false, false, 7);
1050 button = new Button(StockID.OK, &go);
1051 actions.packStart(button, false, false, 7);
1053 vbox.packStart(actions, false, false, 2);
1055 notebook.appendPage(vbox,new Label("Threads"));
1060 void testViewport(Notebook notebook)
1063 Table grid = new Table(21,21,0);
1064 for ( int i = 0 ; i<21 ; i++)
1066 for ( int j = 0 ; j<21; j++)
1068 if ( (i == j) || (i+j==20) || (i==10) || (j==10))
1070 grid.attach(
1071 new Button("Button"),
1072 i,i+1,
1073 j,j+1,
1074 AttachOptions.SHRINK,AttachOptions.SHRINK,4,4);
1079 ScrolledWindow sw = new ScrolledWindow(null,null);
1080 sw.addWithViewport(grid);
1081 notebook.appendPage(sw,new Label("Viewport"));
1084 void testSpinButton(Notebook notebook)
1086 Table table = new Table(2,1,false);
1088 SpinButton spin = new SpinButton(new Adjustment(1.0, 0.0, 100.0, 1.0, 10.0, 10.0),1,0);
1089 table.attach(new Label("Sping this:"),0,1,0,1,AttachOptions.FILL,AttachOptions.FILL,4,4);
1090 table.attach(spin,1,2,0,1,AttachOptions.FILL,AttachOptions.FILL,4,4);
1092 notebook.appendPage(table,"Spin Buttons");
1096 void testList(Notebook notebook)
1098 class TestListStore : ListStore
1100 this()
1102 static GType [3] columns = [
1103 GType.STRING,
1104 GType.STRING,
1105 GType.STRING
1107 super(columns);
1111 TestListStore testListStore = new TestListStore();
1113 TreeIter iterTop = testListStore.createIter();
1115 static int [3] cols = [0,1,2];
1116 char[][] vals;
1117 vals ~= "Antonio";
1118 vals ~= "Canada";
1119 vals ~= "Ontario";
1120 testListStore.set(iterTop,cols,vals);
1122 testListStore.append(iterTop);
1123 char[][] vals1;
1124 vals1 ~= "John Reimer";
1125 vals1 ~= "Canada";
1126 vals1 ~= "BC";
1127 testListStore.set(iterTop,cols,vals1);
1129 testListStore.append(iterTop);
1130 char[][] vals2;
1131 vals2 ~= "Friend of GtkD 2";
1132 vals2 ~= "Poland";
1133 vals2 ~= "Torun";
1134 testListStore.set(iterTop,cols,vals2);
1136 testListStore.append(iterTop);
1137 char[][] vals3;
1138 vals3 ~= "Friend of GtkD 3";
1139 vals3 ~= "Norway";
1140 vals3 ~= "Norway";
1141 testListStore.set(iterTop,cols,vals3);
1143 TreeView treeView = new TreeView(testListStore);
1144 treeView.setHeadersClickable(true);
1145 treeView.setRulesHint(true);
1147 //CellRendererText cellText = new CellRendererText();
1148 TreeViewColumn column = new TreeViewColumn("Author",new CellRendererText(),"text", 0);
1149 treeView.appendColumn(column);
1150 column.setResizable(true);
1151 column.setReorderable(true);
1152 column.setSortColumnId(0);
1153 column.setSortIndicator(true);
1155 column = new TreeViewColumn("Country",new CellRendererText(),"text", 1);
1156 treeView.appendColumn(column);
1157 column.setResizable(true);
1158 column.setReorderable(true);
1159 column.setSortColumnId(1);
1160 column.setSortIndicator(true);
1162 column = new TreeViewColumn("Province",new CellRendererText(),"text", 2);
1163 treeView.appendColumn(column);
1164 column.setResizable(true);
1165 column.setReorderable(true);
1166 column.setSortColumnId(2);
1167 column.setSortIndicator(true);
1169 //notebook.appendPage(treeView,"ListView");
1170 ScrolledWindow sw = new ScrolledWindow(null,null);
1171 sw.addWithViewport(treeView);
1172 notebook.appendPage(sw,"ListView");
1176 void testDelete(Notebook notebook)
1178 // ulong l = 0;
1179 // while(true)
1180 // {
1181 // int i =0;
1182 // while(i<1 )
1183 // {
1184 // //Button b = new Button("Button");
1185 // Drawable d = getDrawable();
1186 // delete d;
1187 // ++i;
1188 // }
1189 // ++l;
1190 // printf("testDelete count = %d\n\0",l*i);
1191 // }
1195 void gtkDemo(Notebook notebook)
1197 void showTTextView(Button button)
1199 new TTextView();
1202 void showTEditableCells(Button button)
1204 // new TEditableCells();
1207 ButtonBox vBBox = VButtonBox.createActionBox();
1209 Button button = new Button("Text View");
1210 button.addOnClicked(&showTTextView);
1211 vBBox.packStart(button,false,false,4);
1213 button = new Button("Editable Cells");
1214 button.addOnClicked(&showTEditableCells);
1215 vBBox.packStart(button,false,false,4);
1217 notebook.appendPage(vBBox,"gtk-demo");
1223 private import gtkc.Loader;
1225 void main(char[][] args)
1228 Linker.dumpFailedLoads();
1230 version(Win32)
1232 // todo threads are still broken on windows...
1233 Gtk.init(args);
1235 else
1237 Gtk.initMultiThread(args);
1240 TestWindow window = new TestWindow();
1242 debug(1)writefln("before GtkD.main");
1243 Gtk.main();
1244 debug(1)writefln("after GtkD.main");