Fixed help file parsing
[Nebula.git] / HelpBox.cs
1 using System.Collections;
2 using System.IO;
3 using System;
4 using Cairo;
5 using Pango;
6
7 public struct HelpItem {
8         public string Key;
9         public string Text;
10
11         public HelpItem(string key, string text) {
12                 Key = key;
13                 Text = text;
14         }
15
16         public override string ToString() {
17                 return "<b>" + Key + "</b>\t" + Text;
18         }
19 }
20
21 public class HelpBox {
22         private Pango.Context pc;
23         private Pango.FontDescription titleFont;
24         private Pango.FontDescription textFont;
25         private string helpTitle;
26         private HelpItem[] helpText;
27         private int Width = 480;
28         private int Height = 360;
29         private int Columns = 3;
30
31         public HelpBox(Pango.Context pc, string title, HelpItem[] items) {
32                 this.pc = pc;
33                 helpTitle = title;
34                 helpText = items;
35                 init();
36         }
37
38         public HelpBox(Pango.Context pc, string filename) {
39                 StreamReader file = new StreamReader(filename);
40                 string line;
41                 ArrayList items = new ArrayList();
42
43                 this.pc = pc;
44                 helpTitle = file.ReadLine();
45                 while ((line = file.ReadLine()) != null) {
46                         string[] i = line.Split(null, 2);
47                         if (i.Length == 2) {
48                                 items.Add(new HelpItem(i[0], i[1]));
49                         }
50                 }
51                 helpText = new HelpItem[items.Count];
52                 for (int i = 0; i < items.Count; i++) {
53                         helpText[i] = (HelpItem) items[i];
54                 }
55                 init();
56         }
57
58         private void init() {
59                 titleFont = Pango.FontDescription.FromString("Helvetica Bold 24");
60                 textFont = Pango.FontDescription.FromString("Helvetica 16");
61                 Pango.FontMetrics fmTitle = pc.GetMetrics(titleFont, null);
62                 Pango.FontMetrics fmText = pc.GetMetrics(textFont, null);
63                 int hTitle = Pango.Units.ToPixels(fmTitle.Ascent + fmTitle.Descent);
64                 int hText = Pango.Units.ToPixels(fmText.Ascent + fmText.Descent);
65
66                 Width = 480;
67                 for (int i = 0; i < helpText.Length; i++) {
68                         Pango.Layout layout = new Pango.Layout(pc);
69                         Pango.Rectangle r, r2;
70
71                         layout.FontDescription = textFont;
72                         layout.SetMarkup(helpText[i].ToString());
73                         layout.GetExtents(out r, out r2);
74                         int pw = Pango.Units.ToPixels(r2.Width);
75                         if ((pw + 8) * Columns > Width) {
76                                 Width = (pw + 8) * Columns;
77                         }
78                 }
79                 Height = hTitle + (helpText.Length / 3 + 1) * hText + 12;
80         }
81
82         public void draw(Cairo.Context gr, int width, int height) {
83                 Pango.Layout layout;
84                 Cairo.Rectangle r = new Cairo.Rectangle(
85                         width / 2 - Width / 2,
86                         height / 2 - Height / 2,
87                         Width, Height);
88
89                 gr.SetSourceRGBA(1.0, 1.0, 1.0, 0.8);
90                 gr.NewPath();
91                 gr.Rectangle(r);
92                 gr.Fill();
93
94                 gr.SetSourceRGB(0.0, 0.0, 0.0);
95                 gr.NewPath();
96                 gr.Rectangle(r);
97                 gr.Stroke();
98
99                 gr.MoveTo(r.X + 4, r.Y + 4);
100                 layout = new Pango.Layout(pc);
101                 layout.SetText(helpTitle);
102                 layout.Width = Pango.Units.FromPixels((int)r.Width);
103                 layout.FontDescription = titleFont;
104                 Pango.CairoHelper.ShowLayout(gr, layout);
105
106                 int ii = 0;
107                 int ColumnWidth = Width / Columns;
108                 int ColumnLines = helpText.Length / Columns + 1;
109                 for (int i = 0; i < Columns; i++) {
110                         gr.MoveTo(r.X + 4 + (i * ColumnWidth), r.Y + 38);
111                         string column = "";
112                         for (int j = 0; j < ColumnLines && ii < helpText.Length; j++) {
113                                 column += helpText[ii].ToString() + "\n";
114                                 ii++;
115                         }
116                         layout = new Pango.Layout(pc);
117                         layout.SetMarkup(column);
118                         layout.Width = Pango.Units.FromPixels(ColumnWidth);
119                         layout.FontDescription = textFont;
120                         Pango.CairoHelper.ShowLayout(gr, layout);
121                 }
122         }
123 }