/HelpBox.cs
using System.Collections;
using System.IO;
using System;
using Cairo;
using Pango;
public struct HelpItem {
public string Key;
public string Text;
public HelpItem(string key, string text) {
Key = key;
Text = text;
}
public override string ToString() {
return "<b>" + Key + "</b>\t" + Text;
}
}
public class HelpBox {
private Pango.Context pc;
private Pango.FontDescription titleFont;
private Pango.FontDescription textFont;
private string helpTitle;
private HelpItem[] helpText;
private int Width = 480;
private int Height = 360;
private int Columns = 3;
public HelpBox(Pango.Context pc, string title, HelpItem[] items) {
this.pc = pc;
helpTitle = title;
helpText = items;
init();
}
public HelpBox(Pango.Context pc, string filename) {
StreamReader file = new StreamReader(filename);
string line;
ArrayList items = new ArrayList();
this.pc = pc;
helpTitle = file.ReadLine();
while ((line = file.ReadLine()) != null) {
string[] i = line.Split(null, 2);
if (i.Length == 2) {
items.Add(new HelpItem(i[0], i[1]));
}
}
helpText = new HelpItem[items.Count];
for (int i = 0; i < items.Count; i++) {
helpText[i] = (HelpItem) items[i];
}
init();
}
private void init() {
titleFont = Pango.FontDescription.FromString("Helvetica Bold 24");
textFont = Pango.FontDescription.FromString("Helvetica 16");
Pango.FontMetrics fmTitle = pc.GetMetrics(titleFont, null);
Pango.FontMetrics fmText = pc.GetMetrics(textFont, null);
int hTitle = Pango.Units.ToPixels(fmTitle.Ascent + fmTitle.Descent);
int hText = Pango.Units.ToPixels(fmText.Ascent + fmText.Descent);
Width = 480;
for (int i = 0; i < helpText.Length; i++) {
Pango.Layout layout = new Pango.Layout(pc);
Pango.Rectangle r, r2;
layout.FontDescription = textFont;
layout.SetMarkup(helpText[i].ToString());
layout.GetExtents(out r, out r2);
int pw = Pango.Units.ToPixels(r2.Width);
if ((pw + 8) * Columns > Width) {
Width = (pw + 8) * Columns;
}
}
Height = hTitle + (helpText.Length / 3 + 1) * hText + 12;
}
public void draw(Cairo.Context gr, int width, int height) {
Pango.Layout layout;
Cairo.Rectangle r = new Cairo.Rectangle(
width / 2 - Width / 2,
height / 2 - Height / 2,
Width, Height);
gr.SetSourceRGBA(1.0, 1.0, 1.0, 0.8);
gr.NewPath();
gr.Rectangle(r);
gr.Fill();
gr.SetSourceRGB(0.0, 0.0, 0.0);
gr.NewPath();
gr.Rectangle(r);
gr.Stroke();
gr.MoveTo(r.X + 4, r.Y + 4);
layout = new Pango.Layout(pc);
layout.SetText(helpTitle);
layout.Width = Pango.Units.FromPixels((int)r.Width);
layout.FontDescription = titleFont;
Pango.CairoHelper.ShowLayout(gr, layout);
int ii = 0;
int ColumnWidth = Width / Columns;
int ColumnLines = helpText.Length / Columns + 1;
for (int i = 0; i < Columns; i++) {
gr.MoveTo(r.X + 4 + (i * ColumnWidth), r.Y + 38);
string column = "";
for (int j = 0; j < ColumnLines && ii < helpText.Length; j++) {
column += helpText[ii].ToString() + "\n";
ii++;
}
layout = new Pango.Layout(pc);
layout.SetMarkup(column);
layout.Width = Pango.Units.FromPixels(ColumnWidth);
layout.FontDescription = textFont;
Pango.CairoHelper.ShowLayout(gr, layout);
}
}
}