- add progress to edge, dct and fft plugins
[FaRetSys.git] / MainWindow.cs
bloba10ea1b6960cbd63172909a84cae7bd0cbf4df89
1 using System;
2 using System.Collections;
3 using System.IO;
4 using Gtk;
5 using Glade;
6 using Mono.Unix;
8 namespace Eithne
10 public class MainWindow
12 [Widget] Window EithneWindow;
13 [Widget] Statusbar StatusBar;
14 [Widget] ImageMenuItem MenuFileNew;
15 [Widget] ImageMenuItem MenuFileOpen;
16 [Widget] ImageMenuItem MenuFileSave;
17 [Widget] ImageMenuItem MenuFileSaveAs;
18 [Widget] ImageMenuItem MenuFilePreferences;
19 [Widget] ImageMenuItem MenuFileQuit;
20 [Widget] ImageMenuItem MenuSystemRun;
21 [Widget] ImageMenuItem MenuSystemStop;
22 [Widget] ImageMenuItem MenuHelpPluginList;
23 [Widget] ImageMenuItem MenuHelpAbout;
24 [Widget] ScrolledWindow PluginToolboxSocket;
25 [Widget] ScrolledWindow SchematicSocket;
26 [Widget] ToolButton ToolbarNew;
27 [Widget] ToolButton ToolbarOpen;
28 [Widget] ToolButton ToolbarSave;
29 [Widget] ToolButton ToolbarRun;
30 [Widget] VBox SchematicBox;
32 private Schematic schematic;
33 private Engine2 engine;
34 private ProgressBar progress = new ProgressBar();
35 private string filename = "";
37 private static MainWindow mw = null;
39 private static bool AlphaChannel = false;
41 private string Filename
43 get { return filename; }
44 set
46 filename = value;
48 if(filename == "")
49 EithneWindow.Title = About.Name;
50 else
51 EithneWindow.Title = About.Name + " (" + filename + ")";
55 public MainWindow(string[] args)
57 Glade.XML gxml = new Glade.XML("MainWindow.glade", "EithneWindow");
58 gxml.BindFields(this);
60 if(EithneWindow.Screen.RgbaColormap != null)
62 AlphaChannel = true;
63 EithneWindow.Colormap = EithneWindow.Screen.RgbaColormap;
66 EithneWindow.IconList = new Gdk.Pixbuf[2] {new Gdk.Pixbuf(null, "icon-48.png"), new Gdk.Pixbuf(null, "icon-16.png")};
68 EithneWindow.DeleteEvent += OnWindowDelete;
70 EithneWindow.Title = About.Name;
72 StatusBar.Push(1, String.Format(Catalog.GetString("Welcome to {0}!"), About.Name));
74 MenuFileNew.Image = new Image(null, "document-new.png");
75 MenuFileNew.Activated += OnNew;
76 MenuFileOpen.Image = new Image(null, "document-open.png");
77 MenuFileOpen.Activated += OnLoad;
78 MenuFileSave.Image = new Image(null, "document-save.png");
79 MenuFileSave.Activated += OnSave;
80 MenuFileSaveAs.Image = new Image(null, "document-save-as.png");
81 MenuFileSaveAs.Activated += OnSaveAs;
82 MenuFilePreferences.Image = new Image(null, "preferences-desktop.png");
83 MenuFilePreferences.Activated += delegate(object o, EventArgs eargs) { new Preferences(); };
84 MenuFileQuit.Image = new Image(null, "system-log-out.png");
85 MenuFileQuit.Activated += OnWindowDelete;
86 MenuSystemRun.Image = new Image(null, "media-playback-start.png");
87 MenuSystemRun.Activated += OnRun;
88 MenuSystemStop.Image = new Image(null, "media-playback-stop.png");
89 MenuSystemStop.Activated += OnRun;
90 MenuHelpPluginList.Image = new Image(null, "plugin-16.png");
91 MenuHelpPluginList.Activated += delegate(object o, EventArgs eargs) { new PluginList(); };
92 MenuHelpAbout.Image = new Image(null, "help-browser.png");
93 MenuHelpAbout.Activated += delegate(object o, EventArgs eargs) { new About(); };
95 ToolbarNew.IconWidget = new Image(null, "document-new-22.png");
96 ToolbarNew.Clicked += OnNew;
97 ToolbarOpen.IconWidget = new Image(null, "document-open-22.png");
98 ToolbarOpen.Clicked += OnLoad;
99 ToolbarSave.IconWidget = new Image(null, "document-save-22.png");
100 ToolbarSave.Clicked += OnSave;
101 ToolbarRun.IconWidget = new Image(null, "media-playback-start-22.png");
102 ToolbarRun.Clicked += OnRun;
104 schematic = new Schematic(StatusBar);
105 PluginToolboxSocket.AddWithViewport(new PluginToolbox(StatusBar, schematic));
106 SchematicSocket.AddWithViewport(schematic);
108 engine = new Engine2(schematic, SetRunToStart, progress.Pulse);
110 progress.PulseStep = 0.05;
113 private void Run()
115 EithneWindow.ShowAll();
117 Application.Run();
120 private void OnWindowDelete(object o, EventArgs args)
122 if(engine.Running)
123 engine.Stop();
125 Application.Quit();
128 private void OnNew(object o, EventArgs args)
130 DialogQuestion q = new DialogQuestion(Catalog.GetString("Do you really want to discard old schematic?"));
132 if(q.Run())
134 schematic.Clear();
135 Filename = "";
137 StatusBar.Pop(1);
138 StatusBar.Push(1, Catalog.GetString("New system schematic ready"));
142 private void OnLoad(object o, EventArgs args)
144 FileChooserDialog fs = new FileChooserDialog(Catalog.GetString("Select file to load..."), EithneWindow,
145 FileChooserAction.Open, new object[] {Catalog.GetString("Cancel"), ResponseType.Cancel,
146 Catalog.GetString("Load"), ResponseType.Accept});
148 FileFilter filter = new FileFilter();
149 filter.Name = String.Format(Catalog.GetString("{0} system schematic"), About.Name);
150 filter.AddPattern("*.xml");
151 fs.AddFilter(filter);
152 filter = new FileFilter();
153 filter.Name = Catalog.GetString("All files");
154 filter.AddPattern("*");
155 fs.AddFilter(filter);
157 fs.Response += delegate(object obj, ResponseArgs eargs)
159 if(eargs.ResponseId == ResponseType.Accept)
161 ArrayList tmp = SaveLoad.Load(fs.Filename, schematic);
162 if(tmp != null)
164 schematic.Load(tmp);
165 Filename = fs.Filename;
167 StatusBar.Pop(1);
168 StatusBar.Push(1, String.Format(Catalog.GetString("{0} loaded"), Filename));
172 fs.Run();
173 fs.Destroy();
176 private void OnSaveAs(object o, EventArgs args)
178 FileChooserDialog fs = new FileChooserDialog(Catalog.GetString("Save as..."), EithneWindow, FileChooserAction.Save,
179 new object[] {Catalog.GetString("Cancel"), ResponseType.Cancel, Catalog.GetString("Save"),
180 ResponseType.Accept});
182 FileFilter filter = new FileFilter();
183 filter.Name = String.Format(Catalog.GetString("{0} system schematic"), About.Name);
184 filter.AddPattern("*.xml");
185 fs.AddFilter(filter);
186 filter = new FileFilter();
187 filter.Name = Catalog.GetString("All files");
188 filter.AddPattern("*");
189 fs.AddFilter(filter);
191 fs.Response += delegate(object obj, ResponseArgs eargs)
193 if(eargs.ResponseId == ResponseType.Accept)
195 Filename = fs.Filename;
197 if(!Path.HasExtension(Filename))
198 Filename = Path.ChangeExtension(Filename, ".xml");
200 SaveLoad.Save(Filename, schematic);
202 StatusBar.Pop(1);
203 StatusBar.Push(1, String.Format(Catalog.GetString("{0} saved"), Filename));
206 fs.Run();
207 fs.Destroy();
210 private void OnSave(object o, EventArgs args)
212 if(Filename == "")
213 OnSaveAs(o, args);
214 else
216 SaveLoad.Save(Filename, schematic);
218 StatusBar.Pop(1);
219 StatusBar.Push(1, String.Format(Catalog.GetString("{0} saved"), Filename));
223 private void OnRun(object o, EventArgs args)
225 if(engine.Running)
226 engine.Stop();
227 else
229 SetRunToStop();
230 engine.Start();
234 private void SetRunToStop()
236 ((Image)ToolbarRun.IconWidget).Pixbuf = new Gdk.Pixbuf(null, "media-playback-stop-22.png");
237 ToolbarRun.Label = Catalog.GetString("Stop");
239 MenuSystemRun.Sensitive = false;
240 MenuSystemStop.Sensitive = true;
242 SchematicBox.PackEnd(progress, false, true, 0);
243 SchematicBox.ShowAll();
246 private void SetRunToStart()
248 ((Image)ToolbarRun.IconWidget).Pixbuf = new Gdk.Pixbuf(null, "media-playback-start-22.png");
249 ToolbarRun.Label = Catalog.GetString("Run");
251 MenuSystemRun.Sensitive = true;
252 MenuSystemStop.Sensitive = false;
254 StatusBar.Pop(1);
255 StatusBar.Push(1, String.Format(Catalog.GetString("Elapsed time: {0}"), engine.ElapsedTime));
257 SchematicBox.Remove(progress);
260 public void EmergencySave()
262 SaveLoad.Save("rescue.xml", schematic);
265 public static void RedrawSchematic()
267 mw.schematic.Redraw();
270 public static void Main(string[] args)
272 Catalog.Init("eithne", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "locale"));
274 Application.Init();
275 Splash s = new Splash();
277 Config.Init(UpdateHandler);
279 PluginDB.LoadPlugins(s);
281 s.Close();
285 mw = new MainWindow(args);
286 mw.Run();
288 catch(Exception e)
290 Console.WriteLine(e);
292 new FatalError(e, mw);
296 private static void UpdateHandler()
298 Block.CheckGConf();
299 Schematic.CheckGConf();
300 Engine2.CheckGConf();
303 public static bool HaveAlpha
305 get { return AlphaChannel; }