Fixed help file parsing
[Nebula.git] / main.cs
1 using System;
2 using System.Reflection;
3 using System.Runtime.InteropServices;
4 using Cairo;
5 using Gtk;
6
7 public class Nebula {
8         static Gtk.Window window;
9         static VBox stack;
10         static TextDisplay display;
11         static TextDocument doc;
12         static bool fullscreened = false;
13
14         static void Main (string[] args) {
15                 Application.Init ();
16                 window = new Gtk.Window("Nebula");
17                 stack = new VBox(false, 0);
18
19                 if (args.Length > 0) {
20                         doc = new TextDocument(args[0]);
21                 } else {
22                         doc = new TextDocument();
23                 }
24                 display = new TextDisplay();
25                 display.SetDocument(doc);
26
27                 stack.Add(display);
28
29                 window.Add(stack);
30                 window.Resize(640,480);
31                 window.ShowAll();
32
33                 window.KeyPressEvent += new KeyPressEventHandler(KeyPress);
34                 window.KeyReleaseEvent += display.KeyRelease;
35                 SelectInput(display);
36                 window.DeleteEvent += delegate(object o, DeleteEventArgs e) {
37                         Application.Quit();
38                 };
39
40                 Application.Run();
41         }
42
43         static void SelectInput(IKeyPress w) {
44                 window.KeyPressEvent -= display.KeyPress;
45                 window.KeyPressEvent += w.KeyPress;
46         }
47
48         [GLib.ConnectBefore ()]
49         static void KeyPress(object o, KeyPressEventArgs args) {
50                 TextInput input;
51                 if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
52                         switch(args.Event.Key) {
53                         case Gdk.Key.n:
54                                 doc = new TextDocument();
55                                 display.SetDocument(doc);
56                                 break;
57                         case Gdk.Key.o:
58                                 input = new FileSelector();
59                                 stack.PackEnd(input);
60                                 SelectInput(input);
61                                 input.Show();
62                                 input.Selected += delegate(object o2, TextInputEventArgs t) {
63                                         if (t.Value != null) {
64                                                 doc = new TextDocument(t.Value);
65                                                 display.SetDocument(doc);
66                                         }
67                                         SelectInput(display);
68                                         stack.Remove(input);
69                                         input = null;
70                                 };
71                                 break;
72                         case Gdk.Key.s:
73                                 if (doc.filename == null) {
74                                         input = new FileSelector();
75                                         stack.PackEnd(input);
76                                         SelectInput(input);
77                                         input.Show();
78                                         input.Selected += delegate(object o2, TextInputEventArgs t) {
79                                                 if (t.Value != null) {
80                                                         doc.Save(t.Value);
81                                                 }
82                                                 SelectInput(display);
83                                                 stack.Remove(input);
84                                                 input.Destroy();
85                                         };
86                                 } else {
87                                         doc.Save();
88                                 }
89                                 break;
90                         case Gdk.Key.q:
91                                 Application.Quit();
92                                 break;
93                         }
94                 } else if ((args.Event.State & Gdk.ModifierType.Mod1Mask) != 0) {
95                         switch(args.Event.Key) {
96                         case Gdk.Key.Return:
97                                 if (fullscreened)
98                                         ((Window)o).Unfullscreen();
99                                 else
100                                         ((Window)o).Fullscreen();
101                                 fullscreened = ! fullscreened;
102                                 break;
103                         }
104                 }
105                 display.Update();
106         }
107 }