- even better -- export to svg
[FaRetSys.git] / MainWindow.cs
blobbe2fc9b7d5424383a387b33058d3698edb1b3ac8
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 Cairo.SvgSurface svg = new Cairo.SvgSurface("print.svg", 2048, 2048);
228 Cairo.Context c = new Cairo.Context(svg);
230 schematic.Draw(c);
232 ((IDisposable) c.Target).Dispose();
233 ((IDisposable) c).Dispose();
236 private void OnRun(object o, EventArgs args)
238 if(engine.Running)
239 engine.Stop();
240 else
242 SetRunToStop();
243 engine.Start();
247 private void SetRunToStop()
249 ((Image)ToolbarRun.IconWidget).Pixbuf = new Gdk.Pixbuf(null, "media-playback-stop-22.png");
250 ToolbarRun.Label = Catalog.GetString("Stop");
252 MenuSystemRun.Sensitive = false;
253 MenuSystemStop.Sensitive = true;
255 SchematicBox.PackEnd(progress, false, true, 0);
256 SchematicBox.ShowAll();
259 private void SetRunToStart()
261 ((Image)ToolbarRun.IconWidget).Pixbuf = new Gdk.Pixbuf(null, "media-playback-start-22.png");
262 ToolbarRun.Label = Catalog.GetString("Run");
264 MenuSystemRun.Sensitive = true;
265 MenuSystemStop.Sensitive = false;
267 StatusBar.Pop(1);
268 StatusBar.Push(1, String.Format(Catalog.GetString("Elapsed time: {0}"), engine.ElapsedTime));
270 SchematicBox.Remove(progress);
273 public void EmergencySave()
275 SaveLoad.Save("rescue.xml", schematic);
278 public static void RedrawSchematic()
280 mw.schematic.Redraw();
283 public static void Main(string[] args)
285 Catalog.Init("eithne", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "locale"));
287 Application.Init();
288 Splash s = new Splash();
290 Config.Init(UpdateHandler);
292 PluginDB.LoadPlugins(s);
294 s.Close();
298 mw = new MainWindow(args);
299 mw.Run();
301 catch(Exception e)
303 Console.WriteLine(e);
305 new FatalError(e, mw);
309 private static void UpdateHandler()
311 Block.CheckGConf();
312 Schematic.CheckGConf();
313 Engine2.CheckGConf();
316 public static bool HaveAlpha
318 get { return AlphaChannel; }