Initial commit
[Nebula.git] / FileInput.cs
1 using System;
2 using System.IO;
3 using System.Collections;
4 using Cairo;
5 using Gtk;
6 using Gdk;
7
8 class FileSelector : TextInput {
9         private string filestr;
10
11         public FileSelector() : base(Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar) {
12                 Complete();
13         }
14
15         protected override void draw(Cairo.Context gr, int width, int height) {
16                 base.draw(gr, width, height);
17
18                 gr.LineWidth = 1.0;
19                 gr.NewPath();
20                 gr.MoveTo(0, 13.5);
21                 gr.LineTo(width, 13.5);
22                 gr.Stroke();
23
24                 if (filestr != null) {
25                         Pango.Layout layout = new Pango.Layout(this.PangoContext);
26                         layout.Width = Pango.Units.FromPixels(width);
27                         layout.FontDescription = Pango.FontDescription.FromString("Courier 12");
28                         layout.Wrap = Pango.WrapMode.WordChar;
29                         layout.SetMarkup(filestr);
30                         gr.MoveTo(0, 15);
31                         Pango.CairoHelper.ShowLayout(gr, layout);
32                 }
33         }
34
35         [GLib.ConnectBefore()]
36         public override void KeyPress(object o, KeyPressEventArgs args) {
37                 base.KeyPress(o, args);
38                 switch(args.Event.Key) {
39                 case Gdk.Key.Tab:
40                         Complete();
41                         QueueDraw();
42                         break;
43                 }
44         }
45
46         private void Complete() {
47                 string dir = System.IO.Path.GetDirectoryName(Value);
48                 string search = System.IO.Path.GetFileName(Value);
49                 string[] files = Directory.GetFileSystemEntries(dir, search + "*");
50                 filestr = null;
51                 if (files.Length == 1 && Directory.Exists(files[0])) {
52                         Value = files[0] + System.IO.Path.DirectorySeparatorChar;
53                         Complete();
54                         return;
55                 } else if (files.Length > 0) {
56                         int i;
57                         filestr = "";
58
59                         string common = System.IO.Path.GetFileName(files[0]);
60
61                         for (i = 0; i < files.Length; i++) {
62                                 files[i] = System.IO.Path.GetFileName(files[i]);
63                                 common = CommonPart(common, files[i]);
64                         }
65
66                         Value = dir + System.IO.Path.DirectorySeparatorChar + common;
67                         cursor = Value.Length;
68
69                         for (i = 0; i < files.Length; i++) {
70                                 string f = files[i];
71                                 filestr += "<span color=\"#7F7F7F\">" + common + "</span>" + f.Substring(common.Length) + "  ";
72                         }
73                 }
74         }
75
76         private string CommonPart(string s1, string s2) {
77                 CharEnumerator i1 = s1.GetEnumerator();
78                 CharEnumerator i2 = s2.GetEnumerator();
79                 int c = 0;
80
81                 while (i1.MoveNext() && i2.MoveNext()) {
82                         if (i1.Current != i2.Current) break;
83                         c++;
84                 }
85                 return s1.Substring(0, c);
86         }
87 }