Added text scrolling
[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                 SelectInput(display);
35                 window.DeleteEvent += delegate(object o, DeleteEventArgs e) {
36                         Application.Quit();
37                 };
38
39                 Application.Run();
40         }
41
42         static void SelectInput(IKeyPress w) {
43                 window.KeyPressEvent -= display.KeyPress;
44                 window.KeyPressEvent += w.KeyPress;
45         }
46
47         [GLib.ConnectBefore ()]
48         static void KeyPress(object o, KeyPressEventArgs args) {
49                 if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
50                         switch(args.Event.Key) {
51                         case Gdk.Key.n:
52                                 doc = new TextDocument();
53                                 display.SetDocument(doc);
54                                 break;
55                         case Gdk.Key.o:
56                                 TextInput filename = new FileSelector();
57                                 stack.PackEnd(filename);
58                                 SelectInput(filename);
59                                 filename.Show();
60                                 filename.Selected += delegate(object o2, TextInputEventArgs t) {
61                                         if (t.Value != null) {
62                                                 doc = new TextDocument(t.Value);
63                                                 display.SetDocument(doc);
64                                         }
65                                         SelectInput(display);
66                                         stack.Remove(filename);
67                                         filename.Destroy();
68                                 };
69                                 break;
70                         case Gdk.Key.q:
71                                 Application.Quit();
72                                 break;
73                         }
74                 } else if ((args.Event.State & Gdk.ModifierType.Mod1Mask) != 0) {
75                         switch(args.Event.Key) {
76                         case Gdk.Key.Return:
77                                 if (fullscreened)
78                                         ((Window)o).Unfullscreen();
79                                 else
80                                         ((Window)o).Fullscreen();
81                                 fullscreened = ! fullscreened;
82                                 break;
83                         }
84                 }
85                 display.Update();
86         }
87 }