I've no idea here...
[gtkD.git] / installer / ui / Main.d
blob4c3e393427d2728f2dff005b515b3f63a0dec3b2
1 /*
2 * This file is part of duit.
4 * duit 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.
9 * duit 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 duit; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 module ui.Main;
21 //version = leds;
22 version = duitdev;
23 version = duittest;
24 //version = difiles
25 //version = compiler;
26 version(linux)
28 version = duitgl;
29 version = duitgltest;
31 version = dmd;
32 //version = gdc;
34 private import ui.Installer;
36 private import ui.duitLogo;
38 private import gtk.Duit;
39 private import gtk.Alignment;
40 private import gtk.MainWindow;
41 private import gtk.VBox;
42 private import gtk.HBox;
43 private import gtk.Button;
44 private import gtk.Label;
45 private import gtk.Frame;
46 private import gtk.ButtonBox;
47 private import gtk.HButtonBox;
48 private import gtk.Entry;
49 private import gtk.CheckButton;
50 private import gtk.Notebook;
51 private import gtk.Widget;
52 private import gtk.Image;
53 private import gtk.gtktypes;
54 private import gtk.SizeGroup;
56 private import gdk.Event;
57 private import std.stdio;
59 private import std.c.stdlib;
60 private import std.string;
62 interface InstallerUI
64 void setUserPanel(UserPanel panel);
65 void addSelection(char[] id, CheckButton check);
66 CheckButton getSelectionButton(char[] id);
67 bool getSelection(char[] id);
68 void setSelection(char[] id, bool state);
69 void setSensitive(char[] id, bool state);
71 void addDirectory(char[] id, Entry entry);
72 char[] getDirectory(char[] id);
73 void setDirectory(char[] id, char[] value);
75 //void nextPanel();
76 //void prevPanel();
79 public class MainInstaller : MainWindow , InstallerUI
82 HBox topBox;
83 VBox leftBox;
84 VBox tasksBox;
85 VBox mainBox;
86 Notebook notebook;
87 ButtonBox buttonBox;
89 Button backButton;
90 Button nextButton;
91 Button exitButton;
92 Button installButton;
94 UserPanel[] userPanels;
95 UserPanel currentUserPanel;
96 CheckButton[char[]] selections;
97 Entry[char[]] directories;
99 int movingDirection; // going back or next, 0=don't know
101 this()
103 super("Duit and Leds installer");
104 setup();
105 addPanels();
106 setUserPanel(0);
107 showAll();
110 public void addSelection(char[] id, CheckButton check)
112 selections[id] = check;
115 public CheckButton getSelectionButton(char[] id)
117 CheckButton check;
118 if ( id in selections )
120 check = selections[id];
122 return check;
125 public bool getSelection(char[] id)
127 if ( id in selections )
129 return cast(bool)selections[id].getActive();
131 return false;
133 public void setSelection(char[] id, bool active)
135 if ( id in selections )
137 selections[id].setActive(active);
140 public void setSensitive(char[] id, bool active)
142 if ( id in selections )
144 selections[id].setSensitive(active);
148 public void addDirectory(char[] id, Entry entry)
150 directories[id] = entry;
152 public char[] getDirectory(char[] id)
154 if ( id in directories )
156 return directories[id].getText();
158 return null;
160 public void setDirectory(char[] id, char[] value)
162 if ( id in directories )
164 return directories[id].setText(value);
168 public void nextPanel()
170 movingDirection = 1;
171 notebook.nextPage();
172 setUserPanel(notebook.getCurrentPage());
175 public void prevPanel()
177 movingDirection = -1;
178 notebook.prevPage();
179 setUserPanel(notebook.getCurrentPage());
182 private import gtk.HSeparator;
183 private void setup()
185 topBox = new HBox(false, 2);
186 leftBox = new VBox(false, 7);
187 tasksBox = new VBox(false, 7);
188 mainBox = new VBox(false, 2);
189 notebook = new Notebook;
190 notebook.setShowTabs(false);
191 notebook.setSizeRequest(400,300);
192 buttonBox = HButtonBox.createActionBox();
194 exitButton = makeButton(null, StockID.QUIT, "Exit", "exit");
195 buttonBox.packStart(Alignment.west(exitButton), true, true, 7);
197 backButton = makeButton(buttonBox, StockID.GO_BACK, "Back", "back");
198 nextButton = makeButton(buttonBox, StockID.GO_FORWARD, "Next", "next");
199 installButton = makeButton(buttonBox, StockID.OK, "Install", "install");
201 // Button logo = new Button();
202 // logo.setImage(getDuitLogo());
203 // logo.addOnButtonPress(&logoClicked);
204 // logo.setRelief(ReliefStyle.NONE);
205 // logo.setFocusOnClick(false);
207 mainBox.packStart(Alignment.west(getDuitLogo()), false, false, 2);
208 mainBox.packStart(notebook, true, true, 2);
209 mainBox.packStart(buttonBox, false, false, 2);
211 topBox.packStart(new Frame(tasksBox,""), false, false, 2);
212 topBox.packStart(mainBox, true, true, 2);
214 add(topBox);
217 private import gtk.PopupBox;
219 private int logoClicked(GdkEventButton* event, Widget widget)
222 PopupBox.information(this, "Help with the graphics design is apreciated", "DuitLogo");
223 return false;
226 private Button makeButton(ButtonBox box,StockID id, char[] label, char[] action)
228 Button button = new Button();
229 button.setActionName(action);
230 button.removeAll();
231 button.setImage(new Image(id, IconSize.BUTTON));
232 button.setLabel(label);
233 if ( box !is null )
235 box.packStart(button, false, false, 7);
237 button.addOnClicked(&userActions);
238 return button;
241 private void addPanels()
243 addPanel(new SelectionsPanel(this));
244 version(duitdev)addPanel(new DevPanel(this));
245 version(leds) addPanel(new LedsPanel(this));
246 version(compiler) addPanel(new MainCompilerPanel(this));
247 addPanel(new LocalPanel(this));
248 addPanel(new InstallPanel(this));
251 CheckButton[char[]] tasksList;
253 private void addPanel(UserPanel userPanel)
255 notebook.appendPage(userPanel.getWidget(), userPanel.getName());
257 HBox hbox = new HBox(false, 1);
258 CheckButton check = new CheckButton();
259 hbox.packStart(check, false, false, 4);
261 hbox.packStart(Alignment.west(new Label(userPanel.getName())), false, false, 4);
262 tasksBox.packStart(hbox, false, false, 4);
263 tasksList[userPanel.getName()] = check;
264 check.setSensitive(false);
265 userPanels ~= userPanel;
268 private void userActions(Button button)
270 char[] action = button.getActionName();
271 switch ( action )
273 case "exit":
274 Duit.exit(0);
275 break;
277 case "back":
278 prevPanel();
279 break;
281 case "next":
282 nextPanel();
283 break;
285 case "install":
286 hide();
287 new Installer(this);
288 break;
290 default:
291 writefln("userAction = %s", action);
292 break;
297 private void setUserPanel(uint number)
299 if ( number < userPanels.length )
301 UserPanel panel = userPanels[number];
302 setUserPanel(panel);
306 public void setUserPanel(UserPanel panel)
308 if ( currentUserPanel !is null )
310 currentUserPanel.exited();
312 while ( !panel.selected()
313 && movingDirection != 0
316 if ( movingDirection > 0 )
318 nextPanel();
320 else
322 prevPanel();
324 return;
326 backButton.setSensitive(panel.allowBack());
327 nextButton.setSensitive(panel.allowNext());
328 bool allowInstall = true;
329 foreach ( UserPanel userPanel ; userPanels )
331 if ( !userPanel.complete() )
333 allowInstall = false;
335 if ( userPanel.getName in tasksList )
337 tasksList[userPanel.getName()].setActive(userPanel.complete());
340 installButton.setSensitive(allowInstall);
341 currentUserPanel = panel;
345 class UserPanel
347 InstallerUI installerUI;
349 bool completed = false;
353 abstract Widget getWidget();
354 abstract char[] getName();
355 public this(InstallerUI installerUI)
357 this.installerUI = installerUI;
360 public bool allowNext()
362 return true;
364 public bool allowBack()
366 return true;
368 public bool selected()
370 return true;
372 public bool exited()
374 return selected();
376 public bool complete()
378 return completed;
381 protected HBox newSelection(char[] selection, char[] id)
383 HBox box = new HBox(false, 2);
385 Button button = new Button(StockID.HELP, &userHelp, true);
386 button.setActionName(id);
387 button.setImage(new Image(StockID.HELP, IconSize.MENU));
388 CheckButton check = new CheckButton(selection, &userSelection);
389 check.setActionName(id);
391 box.packStart(button, false, false, 2);
392 box.packStart(check, false, false, 2);
393 installerUI.addSelection(id, check);
394 return box;
397 protected HBox newDirectory(char[] label, char[] id, SizeGroup sizeGroup)
399 HBox box = new HBox(false, 2);
400 Button button = new Button(StockID.HELP, &userHelp, true);
401 button.setActionName(id);
402 button.setImage(new Image(StockID.HELP, IconSize.MENU));
403 Entry entry = new Entry();
405 box.packStart(button, false, false, 2);
406 Label l = new Label(label);
407 Alignment alig = Alignment.west(l);
408 sizeGroup.addWidget(alig);
409 box.packStart(alig, false, false, 2);
410 box.packStart(entry, true, true, 2);
411 button = new Button(StockID.INDEX, &userDirectory);
412 button.setActionName(id);
413 button.setLabel("Browse");
414 button.setImage(new Image(StockID.INDEX, IconSize.MENU));
415 box.packStart(button, false, false, 2);
416 installerUI.addDirectory(id, entry);
417 return box;
420 void userSelection(CheckButton check)
422 switch ( check.getActionName() )
425 case "leds":
426 bool active = cast(bool)check.getActive();
427 installerUI.setSensitive("ledsDuit", active);
428 installerUI.setSensitive("ledsLeds", active);
429 installerUI.setSensitive("ledsDool", active);
430 installerUI.setSensitive("ledsDante", active);
431 installerUI.setSensitive("ledsPhobos", active);
432 break;
434 default:
435 // nothing
436 break;
440 void userHelp(Button button)
444 private import gtk.FileChooserDialog;
445 private import gtk.FileChooser;
446 void userDirectory(Button button)
448 char[] id = button.getActionName();
449 char[] dir = installerUI.getDirectory(id);
450 if ( dir != null )
452 FileChooserDialog chooserDialog = new FileChooserDialog(
453 "Select directory",
454 mainInstaller,
455 FileChooserAction.SELECT_FOLDER
457 FileChooser chooser = chooserDialog.getFileChooser();
458 chooser.setCurrentFolder(dir);
459 ResponseType response = cast(ResponseType)chooserDialog.run();
460 if ( response == ResponseType.GTK_RESPONSE_OK )
462 installerUI.setDirectory(id, chooser.getCurrentFolder());
464 chooserDialog.hide(); // is it released ?
469 class SelectionsPanel : UserPanel
471 VBox vbox;
473 public this(InstallerUI installerUI)
475 super(installerUI);
477 public char[] getName()
479 return "Selections";
481 public Widget getWidget()
483 if ( vbox is null )
485 vbox = new VBox(false, 2);
486 setup();
488 return vbox;
491 public bool allowBack()
493 return false;
496 public bool selected()
498 completed = true;
499 return true;
502 private void setup()
504 vbox.packStart(new Label("Select the components to install"), false, false, 14);
505 vbox.packStart(newSelection("Install Duit", "duit"), false, false, 2);
506 version(duitdev) vbox.packStart(newSelection("Install Duit for development", "duitDev"), false, false, 2);
507 version(duitgl) vbox.packStart(newSelection("Install Duitgl", "duitgl"), false, false, 2);
508 version(duitdev) version(duitgl) vbox.packStart(newSelection("Install Duitgl for development", "duiglDev"), false, false, 2);
509 version(leds) vbox.packStart(newSelection("Install Leds", "leds"), false, false, 2);
511 installerUI.setSelection("duit", true);
512 version(duitdev) installerUI.setSelection("duitDev", true);
513 version(duitgl) installerUI.setSelection("duitgl", true);
514 version(duitdev) version(duitgl) installerUI.setSelection("duiglDev", true);
515 version(leds) installerUI.setSelection("leds", true);
521 class DevPanel : UserPanel
523 VBox vbox;
525 public this(InstallerUI installerUI)
527 super(installerUI);
529 public char[] getName()
531 return "Development for Duit";
533 public Widget getWidget()
535 if ( vbox is null )
537 vbox = new VBox(false, 2);
538 setup();
540 return vbox;
543 public bool selected()
545 completed = true;
546 return installerUI.getSelection("duitDev")
547 || installerUI.getSelection("duitglDev");
550 private void setup()
552 vbox.packStart(new Label("Select Duit modules"), false, false, 14);
553 version(difiles) vbox.packStart(newSelection("Duit .di files", "duitdi"), false, false, 2);
554 vbox.packStart(newSelection("Duit source files", "duitSource"), false, false, 2);
555 vbox.packStart(newSelection("DuitTests - general UI tests", "duitTests"), false, false, 2);
556 version(duitgl) vbox.packStart(newSelection("SimpleGL - simplest GL test", "duitglSimple"), false, false, 2);
557 version(duitgl) vbox.packStart(newSelection("ShapesGL - showing more of openGL integration", "duitglShapes"), false, false, 2);
559 version(difiles) installerUI.setSelection("duitdi", true);
560 installerUI.setSelection("duitSource", true);
561 installerUI.setSelection("duitTests", true);
562 version(duitgl) installerUI.setSelection("duitglSimple", true);
563 version(duitgl) installerUI.setSelection("duitglShapes", true);
570 class LedsPanel : UserPanel
572 VBox vbox;
574 public this(InstallerUI installerUI)
576 super(installerUI);
578 public char[] getName()
580 return "Leds projects";
582 public Widget getWidget()
584 if ( vbox is null )
586 vbox = new VBox(false, 2);
587 setup();
589 return vbox;
592 public bool selected()
594 completed = true;
595 return installerUI.getSelection("leds");
598 private void setup()
600 vbox.packStart(new Label("Select the leds projects to create"), false, false, 14);
601 vbox.packStart(newSelection("Create a leds project for Duit", "ledsDuit"), false, false, 2);
602 vbox.packStart(newSelection("Create a leds project for Leds", "ledsLeds"), false, false, 2);
603 vbox.packStart(newSelection("Create a leds project for Dool", "ledsDool"), false, false, 2);
604 vbox.packStart(newSelection("Create a leds project for Dante", "ledsDante"), false, false, 2);
605 vbox.packStart(newSelection("Create a leds project for phobos", "ledsPhobos"), false, false, 2);
607 installerUI.setSelection("ledsDuit", true);
608 installerUI.setSelection("ledsLeds", true);
609 installerUI.setSelection("ledsDool", true);
610 installerUI.setSelection("ledsDante", true);
611 installerUI.setSelection("ledsPhobos", true);
618 class MainCompilerPanel : UserPanel
620 VBox vbox;
622 public this(InstallerUI installerUI)
624 super(installerUI);
626 public char[] getName()
628 return "D Compiler";
630 public Widget getWidget()
632 if ( vbox is null )
634 vbox = new VBox(false, 2);
635 setup();
637 return vbox;
640 public bool selected()
642 completed = true;
643 return true;
646 private void setup()
648 vbox.packStart(new Label("Select your D compiler"), false, false, 14);
650 vbox.packStart(new Label("For the installation"), false, false, 7);
651 vbox.packStart(newSelection("Digital Mars DMD", "instalCompilerDMD"), false, false, 2);
652 vbox.packStart(newSelection("gnu D GDC", "installCompilerGDC"), false, false, 2);
654 vbox.packStart(new Label("For your leds projects"), false, false, 7);
655 vbox.packStart(newSelection("Digital Mars DMD", "ledsCompilerDMD"), false, false, 2);
656 vbox.packStart(newSelection("gnu D GDC", "ledsCompilerGDC"), false, false, 2);
658 installerUI.setSelection("instalCompilerDMD", true);
659 installerUI.setSelection("installCompilerGDC", false);
660 version(leds) installerUI.setSelection("ledsCompilerDMD", true);
661 version(leds) installerUI.setSelection("ledsCompilerGDC", false);
663 installerUI.setSensitive("instalCompilerDMD", false);
664 installerUI.setSensitive("installCompilerGDC", false);
665 version(leds) installerUI.setSensitive("ledsCompilerDMD", false);
666 version(leds) installerUI.setSensitive("ledsCompilerGDC", false);
673 class LocalPanel : UserPanel
675 VBox vbox;
676 SizeGroup sizeGroup;
678 char[] home;
680 public this(InstallerUI installerUI)
682 super(installerUI);
683 sizeGroup = new SizeGroup(SizeGroupMode.HORIZONTAL);
684 version(Win32)
686 home = std.string.toString(getenv("HOMEPATH"));
688 else
690 home = std.string.toString(getenv("HOME"));
694 public char[] getName()
696 return "Local";
699 private import std.path;
700 private import std.file;
702 private bool validDir(char[] dir)
704 bool valid = dir.length > 0;
705 return valid;
708 public bool validDMDHome()
710 bool valid = false;
711 char[] dir = installerUI.getDirectory("dmdHome");
712 valid = validDir(dir);
713 if ( valid )
717 version(Win32) char[] dmd = std.path.join(std.path.join(dir ,"bin"), "dmd.exe");
718 else char[] dmd = std.path.join(std.path.join(dir ,"bin"), "dmd");
719 std.file.isfile(dmd);
720 version(Win32)
722 char[] dm = std.path.join(std.path.join(dir,"..\\dm"),"bin");
723 char[] link = std.path.join(dm, "link.exe");
724 std.file.isfile(link);
725 char[] lib = std.path.join(dm, "lib.exe");
726 std.file.isfile(lib);
728 else char[] link = std.path.join(std.path.join(dir ,"bin"), "dmd");
729 valid = true;
731 catch ( FileException pe )
733 valid = false;
737 return valid;
740 // class GuessFile
741 // {
742 // char[] guess;
743 // char[] fetch;
744 // char[] userChoice;
745 // bool create;
746 // bool confirmed;
748 // bool isDir(char[] dir)
749 // {
750 // bool ok = false;
751 // try
752 // {
753 // ok = cast(bool)std.file.isfile(dir);
754 // }
755 // catch ( FileException fe )
756 // {
757 // ok = false;
758 // }
759 // return ok;
760 // }
762 // bool exists(char[] fileName)
763 // {
764 // bool ok = false;
765 // try
766 // {
767 // ok = cast(bool)std.file.exists(fileName);
768 // }
769 // catch ( FileException fe )
770 // {
771 // ok = false;
772 // }
773 // return ok;
774 // }
775 // }
777 public char[] guessDMDHome()
779 char[] guess;
780 version(Win32)
782 guess = "\\dmd";
784 else
786 guess = std.path.join(home, "dmd");
788 return guess;
791 public char[] guessGtkHome()
793 char[] guess;
794 version(Win32)
796 guess = "\\Program Files\\Common Files\\GTK\\2.0\\bin";
798 else
800 guess = "/usr/lib";
802 return guess;
805 public char[] guessDuitLibHome()
807 char[] guess;
808 version(Win32)
810 guess = "\\Program Files\\Common Files\\GTK\\2.0\\bin";
812 else
814 guess = "/home/ruimt/usr/lib";
816 return guess;
819 public char[] guessDuitDevHome()
821 char[] guess;
822 version(Win32)
824 guess = "\\testRelease";
826 else
828 guess = std.path.join(home, "devel/D/testInstall/Duit1");
830 return guess;
833 public char[] guessLedsHome()
835 char[] guess;
836 version(Win32)
838 guess = "\\Program Files\\Common Files\\org\\dsource\\leds";
840 else
842 guess = std.path.join(home, "devel/D/Leds1");
844 return guess;
848 public bool selected()
850 completed = true;
851 version(dmd)
853 if ( completed )
855 completed = validDMDHome();
858 version(duitdev)
860 if ( completed )
862 completed = installerUI.getDirectory("duitDevHome").length > 0;
865 version(leds)
867 if ( completed )
869 completed = installerUI.getDirectory("ledsHome").length > 0;
873 if ( completed )
875 completed = installerUI.getDirectory("gtkHome").length > 0;
877 if ( completed )
879 completed = installerUI.getDirectory("duitLibHome").length > 0;
882 return true;
884 public Widget getWidget()
886 if ( vbox is null )
888 vbox = new VBox(false, 2);
889 setup();
891 return vbox;
893 private void setup()
895 vbox.packStart(new Label("Select the paths for installation"), false, false, 14);
896 vbox.packStart(newDirectory("DMD home", "dmdHome", sizeGroup), false, false, 2);
897 vbox.packStart(newDirectory("Gtk lib home", "gtkHome", sizeGroup), false, false, 2);
898 vbox.packStart(newDirectory("Duit lib home", "duitLibHome", sizeGroup), false, false, 2);
899 version(duitdev) vbox.packStart(newDirectory("Duit dev home", "duitDevHome", sizeGroup), false, false, 2);
900 version(leds) vbox.packStart(newDirectory("leds home", "ledsHome", sizeGroup), false, false, 2);
902 installerUI.setDirectory("dmdHome", guessDMDHome());
903 installerUI.setDirectory("gtkHome", guessGtkHome());
904 installerUI.setDirectory("duitLibHome", guessDuitLibHome());
905 version(duitdev) installerUI.setDirectory("duitDevHome", guessDuitDevHome());
906 version(leds) installerUI.setDirectory("ledsHome", guessLedsHome());
910 class InstallPanel : UserPanel
912 VBox vbox;
913 //bool installed = false;
914 public this(InstallerUI installerUI)
916 super(installerUI);
918 public bool selected()
920 completed = true;
921 return true;
923 public char[] getName()
925 return "Install";
927 public Widget getWidget()
929 if ( vbox is null )
931 vbox = new VBox(false, 2);
932 setup();
934 return vbox;
937 bool allowNext()
939 return false;
942 private void setup()
944 Label label = new Label(
945 "Thank you for installing Duit\n"
946 ~"press Install to continue"
948 vbox.packStart(label, true, true, 2);
950 version(duittest) vbox.packStart(newSelection("Create desktop icon for duit tests", "duitTestsIcon"), false, false, 2);
951 version(leds) vbox.packStart(newSelection("Create desktop icon for leds", "ledsIcon"), false, false, 2);
952 version(leds) vbox.packStart(newSelection("set leds as your D editor", "ledsDEditor"), false, false, 2);
954 version(duittest) installerUI.setSelection("duitTestsIcon", false);
955 version(leds) installerUI.setSelection("ledsIcon", false);
956 version(leds) installerUI.setSelection("ledsDEditor", false);
958 version(duittest) installerUI.setSensitive("duitTestsIcon", false);
959 version(leds) installerUI.setSensitive("ledsIcon", false);
960 version(leds) installerUI.setSensitive("ledsDEditor", false);
964 MainInstaller mainInstaller;
966 int main(char[][] args)
968 version(Win32)
970 Duit.init(args);
972 else
974 Duit.initMultiThread(args);
976 mainInstaller = new MainInstaller();
977 Duit.main();
978 return 0;