- renamed aa to offset
[FaRetSys.git] / MainWindow.cs
blob1b610bf3fa471aba0c80f29f3d3df2f5a15541c2
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 MenuFilePrint;
19 [Widget] ImageMenuItem MenuFilePreferences;
20 [Widget] ImageMenuItem MenuFileQuit;
21 [Widget] ImageMenuItem MenuSystemRun;
22 [Widget] ImageMenuItem MenuSystemStop;
23 [Widget] ImageMenuItem MenuHelpPluginList;
24 [Widget] ImageMenuItem MenuHelpAbout;
25 [Widget] ScrolledWindow PluginToolboxSocket;
26 [Widget] ScrolledWindow SchematicSocket;
27 [Widget] ToolButton ToolbarNew;
28 [Widget] ToolButton ToolbarOpen;
29 [Widget] ToolButton ToolbarSave;
30 [Widget] ToolButton ToolbarRun;
31 [Widget] VBox SchematicBox;
33 private Schematic schematic;
34 private Engine2 engine;
35 private ProgressBar progress = new ProgressBar();
36 private string filename = "";
38 private static MainWindow mw = null;
40 private static bool AlphaChannel = false;
42 private string Filename
44 get { return filename; }
45 set
47 filename = value;
49 if(filename == "")
50 EithneWindow.Title = About.Name;
51 else
52 EithneWindow.Title = About.Name + " (" + filename + ")";
56 public MainWindow(string[] args)
58 Glade.XML gxml = new Glade.XML("MainWindow.glade", "EithneWindow");
59 gxml.BindFields(this);
61 if(EithneWindow.Screen.RgbaColormap != null)
63 AlphaChannel = true;
64 EithneWindow.Colormap = EithneWindow.Screen.RgbaColormap;
67 EithneWindow.IconList = new Gdk.Pixbuf[2] {new Gdk.Pixbuf(null, "icon-48.png"), new Gdk.Pixbuf(null, "icon-16.png")};
69 EithneWindow.DeleteEvent += OnWindowDelete;
71 EithneWindow.Title = About.Name;
73 StatusBar.Push(1, String.Format(Catalog.GetString("Welcome to {0}!"), About.Name));
75 MenuFileNew.Image = new Image(null, "document-new.png");
76 MenuFileNew.Activated += OnNew;
77 MenuFileOpen.Image = new Image(null, "document-open.png");
78 MenuFileOpen.Activated += OnLoad;
79 MenuFileSave.Image = new Image(null, "document-save.png");
80 MenuFileSave.Activated += OnSave;
81 MenuFileSaveAs.Image = new Image(null, "document-save-as.png");
82 MenuFileSaveAs.Activated += OnSaveAs;
83 MenuFilePrint.Activated += OnPrint;
84 MenuFilePreferences.Image = new Image(null, "preferences-desktop.png");
85 MenuFilePreferences.Activated += delegate(object o, EventArgs eargs) { new Preferences(); };
86 MenuFileQuit.Image = new Image(null, "system-log-out.png");
87 MenuFileQuit.Activated += OnWindowDelete;
88 MenuSystemRun.Image = new Image(null, "media-playback-start.png");
89 MenuSystemRun.Activated += OnRun;
90 MenuSystemStop.Image = new Image(null, "media-playback-stop.png");
91 MenuSystemStop.Activated += OnRun;
92 MenuHelpPluginList.Image = new Image(null, "plugin-16.png");
93 MenuHelpPluginList.Activated += delegate(object o, EventArgs eargs) { new PluginList(); };
94 MenuHelpAbout.Image = new Image(null, "help-browser.png");
95 MenuHelpAbout.Activated += delegate(object o, EventArgs eargs) { new About(); };
97 ToolbarNew.IconWidget = new Image(null, "document-new-22.png");
98 ToolbarNew.Clicked += OnNew;
99 ToolbarOpen.IconWidget = new Image(null, "document-open-22.png");
100 ToolbarOpen.Clicked += OnLoad;
101 ToolbarSave.IconWidget = new Image(null, "document-save-22.png");
102 ToolbarSave.Clicked += OnSave;
103 ToolbarRun.IconWidget = new Image(null, "media-playback-start-22.png");
104 ToolbarRun.Clicked += OnRun;
106 schematic = new Schematic(StatusBar);
107 PluginToolboxSocket.AddWithViewport(new PluginToolbox(StatusBar, schematic));
108 SchematicSocket.AddWithViewport(schematic);
110 engine = new Engine2(schematic, SetRunToStart, progress.Pulse);
112 progress.PulseStep = 0.05;
115 private void Run()
117 EithneWindow.ShowAll();
119 Application.Run();
122 private void OnWindowDelete(object o, EventArgs args)
124 if(engine.Running)
125 engine.Stop();
127 Application.Quit();
130 private void OnNew(object o, EventArgs args)
132 DialogQuestion q = new DialogQuestion(Catalog.GetString("Do you really want to discard old schematic?"));
134 if(q.Run())
136 schematic.Clear();
137 Filename = "";
139 StatusBar.Pop(1);
140 StatusBar.Push(1, Catalog.GetString("New system schematic ready"));
144 private void OnLoad(object o, EventArgs args)
146 FileChooserDialog fs = new FileChooserDialog(Catalog.GetString("Select file to load..."), EithneWindow,
147 FileChooserAction.Open, new object[] {Catalog.GetString("Cancel"), ResponseType.Cancel,
148 Catalog.GetString("Load"), ResponseType.Accept});
150 FileFilter filter = new FileFilter();
151 filter.Name = String.Format(Catalog.GetString("{0} system schematic"), About.Name);
152 filter.AddPattern("*.xml");
153 fs.AddFilter(filter);
154 filter = new FileFilter();
155 filter.Name = Catalog.GetString("All files");
156 filter.AddPattern("*");
157 fs.AddFilter(filter);
159 fs.Response += delegate(object obj, ResponseArgs eargs)
161 if(eargs.ResponseId == ResponseType.Accept)
163 ArrayList tmp = SaveLoad.Load(fs.Filename, schematic);
164 if(tmp != null)
166 schematic.Load(tmp);
167 Filename = fs.Filename;
169 StatusBar.Pop(1);
170 StatusBar.Push(1, String.Format(Catalog.GetString("{0} loaded"), Filename));
174 fs.Run();
175 fs.Destroy();
178 private void OnSaveAs(object o, EventArgs args)
180 FileChooserDialog fs = new FileChooserDialog(Catalog.GetString("Save as..."), EithneWindow, FileChooserAction.Save,
181 new object[] {Catalog.GetString("Cancel"), ResponseType.Cancel, Catalog.GetString("Save"),
182 ResponseType.Accept});
184 FileFilter filter = new FileFilter();
185 filter.Name = String.Format(Catalog.GetString("{0} system schematic"), About.Name);
186 filter.AddPattern("*.xml");
187 fs.AddFilter(filter);
188 filter = new FileFilter();
189 filter.Name = Catalog.GetString("All files");
190 filter.AddPattern("*");
191 fs.AddFilter(filter);
193 fs.Response += delegate(object obj, ResponseArgs eargs)
195 if(eargs.ResponseId == ResponseType.Accept)
197 Filename = fs.Filename;
199 if(!Path.HasExtension(Filename))
200 Filename = Path.ChangeExtension(Filename, ".xml");
202 SaveLoad.Save(Filename, schematic);
204 StatusBar.Pop(1);
205 StatusBar.Push(1, String.Format(Catalog.GetString("{0} saved"), Filename));
208 fs.Run();
209 fs.Destroy();
212 private void OnSave(object o, EventArgs args)
214 if(Filename == "")
215 OnSaveAs(o, args);
216 else
218 SaveLoad.Save(Filename, schematic);
220 StatusBar.Pop(1);
221 StatusBar.Push(1, String.Format(Catalog.GetString("{0} saved"), Filename));
225 private void OnPrint(object o, EventArgs args)
227 FileChooserDialog fs = new FileChooserDialog(Catalog.GetString("Export as..."), EithneWindow, FileChooserAction.Save,
228 new object[] {Catalog.GetString("Cancel"), ResponseType.Cancel, Catalog.GetString("Export"),
229 ResponseType.Accept});
231 FileFilter filter = new FileFilter();
232 filter.Name = String.Format(Catalog.GetString("SVG image"), About.Name);
233 filter.AddPattern("*.svg");
234 fs.AddFilter(filter);
235 filter = new FileFilter();
236 filter.Name = Catalog.GetString("All files");
237 filter.AddPattern("*");
238 fs.AddFilter(filter);
240 fs.Response += delegate(object obj, ResponseArgs eargs)
242 if(eargs.ResponseId == ResponseType.Accept)
244 Filename = fs.Filename;
246 if(!Path.HasExtension(Filename))
247 Filename = Path.ChangeExtension(Filename, ".svg");
249 Cairo.SvgSurface svg = new Cairo.SvgSurface(Filename, 2048, 2048);
250 Cairo.Context c = new Cairo.Context(svg);
252 schematic.Draw(c);
254 ((IDisposable) c.Target).Dispose();
255 ((IDisposable) c).Dispose();
257 StatusBar.Pop(1);
258 StatusBar.Push(1, String.Format(Catalog.GetString("{0} saved"), Filename));
261 fs.Run();
262 fs.Destroy();
265 private void OnRun(object o, EventArgs args)
267 if(engine.Running)
268 engine.Stop();
269 else
271 SetRunToStop();
272 engine.Start();
276 private void SetRunToStop()
278 ((Image)ToolbarRun.IconWidget).Pixbuf = new Gdk.Pixbuf(null, "media-playback-stop-22.png");
279 ToolbarRun.Label = Catalog.GetString("Stop");
281 MenuSystemRun.Sensitive = false;
282 MenuSystemStop.Sensitive = true;
284 SchematicBox.PackEnd(progress, false, true, 0);
285 SchematicBox.ShowAll();
288 private void SetRunToStart()
290 ((Image)ToolbarRun.IconWidget).Pixbuf = new Gdk.Pixbuf(null, "media-playback-start-22.png");
291 ToolbarRun.Label = Catalog.GetString("Run");
293 MenuSystemRun.Sensitive = true;
294 MenuSystemStop.Sensitive = false;
296 StatusBar.Pop(1);
297 StatusBar.Push(1, String.Format(Catalog.GetString("Elapsed time: {0}"), engine.ElapsedTime));
299 SchematicBox.Remove(progress);
302 public void EmergencySave()
304 SaveLoad.Save("rescue.xml", schematic);
307 public static void RedrawSchematic()
309 mw.schematic.Redraw();
312 public static void Main(string[] args)
314 Catalog.Init("eithne", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "locale"));
316 if(!GLib.Thread.Supported)
317 GLib.Thread.Init();
318 Application.Init();
319 Splash s = new Splash();
321 Config.Init(UpdateHandler);
323 PluginDB.LoadPlugins(s);
325 s.Close();
329 mw = new MainWindow(args);
330 mw.Run();
332 catch(Exception e)
334 Console.WriteLine(e);
336 new FatalError(e, mw);
340 private static void UpdateHandler()
342 Block.CheckGConf();
343 Schematic.CheckGConf();
344 Engine2.CheckGConf();
347 public static bool HaveAlpha
349 get { return AlphaChannel; }