Skip to content

Instantly share code, notes, and snippets.

@jleedev
Created October 22, 2010 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jleedev/640881 to your computer and use it in GitHub Desktop.
Save jleedev/640881 to your computer and use it in GitHub Desktop.
Gtk# font viewer
bin
*.pidb
*.userprefs
using Pango;
using System;
using System.Collections.Generic;
using Gtk;
namespace fontviewer {
public class FontList: TreeView {
public new ListStore Model {
get { return (ListStore) base.Model; }
set { base.Model = value; }
}
public FontList() {
Model = new ListStore(typeof (string), typeof (int), typeof(FontFamily));
TreeViewColumn columnCount = new TreeViewColumn();
columnCount.Title = "Fonts";
AppendColumn(columnCount);
CellRendererText cellCount = new CellRendererText();
columnCount.PackStart(cellCount, true);
columnCount.AddAttribute(cellCount, "text", 1);
TreeViewColumn columnName = new TreeViewColumn();
columnName.Title = "Family";
AppendColumn(columnName);
CellRendererText cellName = new CellRendererText();
columnName.PackStart(cellName, true);
columnName.AddAttribute(cellName, "text", 0);
FontFamily[] f = PangoContext.Families;
Array.Sort(f, new SortFontFamilies());
int sansIndex = 0;
for (int i = 0; i < f.Length; ++i) {
FontFamily it = f[i];
if (it.Name.ToLower().Equals("sans")) sansIndex = i;
Model.AppendValues(it.Name, it.Faces.Length, it);
}
TreePath p = new TreePath(new int[] { sansIndex });
Selection.SelectPath(p);
ScrollToCell(p, columnName, false, 0, 0);
}
public string SelectedFamily {
get {
String family = "sans";
TreeIter iter;
Selection.GetSelected(out iter);
try { family = (string) Model.GetValue(iter, 0); }
catch(Exception) {}
if (family == null) family = "sans";
return family;
}
set {
throw new NotImplementedException();
}
}
private class SortFontFamilies: IComparer<FontFamily> {
public int Compare(FontFamily a, FontFamily b) {
if (a == null && b == null) return 0;
if (a == null) return -1;
if (b == null) return 1;
return String.Compare(a.Name, b.Name);
}
}
}
}
<Project name="fontviewer" fileversion="2.0" language="C#" clr-version="Net_2_0" ctype="DotNetProject">
<Configurations active="Debug">
<Configuration name="Debug" ctype="DotNetProjectConfiguration">
<Output directory="bin/Debug" assembly="fontviewer" />
<Build debugmode="True" target="Exe" />
<Execution runwithwarnings="True" consolepause="True" runtime="MsNet" clr-version="Net_2_0" />
<CodeGeneration compiler="Mcs" warninglevel="4" optimize="True" unsafecodeallowed="False" generateoverflowchecks="True" definesymbols="DEBUG" generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
</Configuration>
<Configuration name="Release" ctype="DotNetProjectConfiguration">
<Output directory="bin/Release" assembly="fontviewer" />
<Build debugmode="False" target="Exe" />
<Execution runwithwarnings="True" consolepause="True" runtime="MsNet" clr-version="Net_2_0" />
<CodeGeneration compiler="Mcs" warninglevel="4" optimize="True" unsafecodeallowed="False" generateoverflowchecks="True" generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
</Configuration>
</Configurations>
<Contents>
<File name="MainWindow.cs" subtype="Code" buildaction="Compile" />
<File name="FontList.cs" subtype="Code" buildaction="Compile" />
</Contents>
<References>
<ProjectReference type="Gac" localcopy="True" refto="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<ProjectReference type="Gac" localcopy="True" refto="pango-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
</References>
</Project>
<Combine name="fontviewer" fileversion="2.0">
<Configurations active="Debug">
<Configuration name="Debug" ctype="CombineConfiguration">
<Entry build="True" name="fontviewer" configuration="Debug" />
</Configuration>
<Configuration name="Release" ctype="CombineConfiguration">
<Entry build="True" name="fontviewer" configuration="Release" />
</Configuration>
</Configurations>
<StartMode startupentry="fontviewer" single="True">
<Execute type="None" entry="fontviewer" />
</StartMode>
<Entries>
<Entry filename="fontviewer.mdp" />
</Entries>
</Combine>
using Pango;
using System;
using System.Collections.Generic;
using Gtk;
namespace fontviewer {
public class MainWindow: Window {
public static void Main(string[] args) {
Application.Init();
new MainWindow();
Application.Run();
}
public MainWindow(): base (WindowType.Toplevel) {
Build();
TreeViewColumn columnVariant = new TreeViewColumn();
columnVariant.Title = "Variant";
faceList.AppendColumn(columnVariant);
CellRendererText cellVariant = new CellRendererText();
columnVariant.PackStart(cellVariant, true);
columnVariant.AddAttribute(cellVariant, "text", 0);
fontList.Selection.Changed += new EventHandler(fontListSelected);
sizeScale.Value = 8;
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a) {
Application.Quit();
a.RetVal = true;
}
protected void fontListSelected (object o, EventArgs args) {
ListStore faceModel = new ListStore(typeof (string));
TreeIter iter;
fontList.Selection.GetSelected(out iter);
if (iter.UserData.ToInt32() != 0) {
FontFamily family = (FontFamily) fontList.Model.GetValue(iter, 2);
FontFace[] f = family.Faces;
faceList.Model = faceModel;
for (int i = 0; i < f.Length; ++i) {
FontFace it = f[i];
faceModel.AppendValues(it.FaceName);
}
}
updateFont();
}
int getFontSize() {
return (int) (Pango.Scale.PangoScale * sizeScale.Value);
}
protected void OnSizeScaleValueChanged (object sender, EventArgs e) {
updateFont();
}
void updateFont() {
FontDescription font = new FontDescription();
font.Family = fontList.SelectedFamily;
font.Size = getFontSize();
preview.ModifyFont(font);
}
private VPaned vbox1;
private HBox hbox1;
private ScrolledWindow fontScroll;
private FontList fontList;
private ScrolledWindow faceScroll;
private TreeView faceList;
private VScale sizeScale;
private ScrolledWindow previewScroll;
private TextView preview;
protected virtual void Build() {
// Widget MainWindow
Name = "MainWindow";
Title = "Font Viewer";
vbox1 = new VPaned();
vbox1.Position = 200;
hbox1 = new HBox();
hbox1.Spacing = 6;
fontScroll = new ScrolledWindow();
fontScroll.HscrollbarPolicy = PolicyType.Never;
fontList = new FontList();
fontScroll.Add(fontList);
hbox1.Add(fontScroll);
Box.BoxChild w2 = (Box.BoxChild) hbox1[fontScroll];
w2.Expand = false;
faceScroll = new ScrolledWindow();
faceScroll.HscrollbarPolicy = PolicyType.Never;
faceList = new TreeView();
faceScroll.Add(faceList);
hbox1.Add(faceScroll);
Box.BoxChild w4 = (Box.BoxChild) hbox1[faceScroll];
w4.Expand = false;
sizeScale = new VScale(0, 100, 1);
sizeScale.Adjustment.PageIncrement = 8;
sizeScale.Inverted = true;
sizeScale.ValuePos = PositionType.Left;
hbox1.Add(sizeScale);
Box.BoxChild w5 = (Box.BoxChild) hbox1[sizeScale];
w5.Expand = false;
previewScroll = new ScrolledWindow();
previewScroll.ShadowType = ((ShadowType)(1));
preview = new TextView();
preview.Buffer.Text = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
preview.AcceptsTab = false;
preview.WrapMode = Gtk.WrapMode.Char;
previewScroll.Add(preview);
vbox1.Add(hbox1);
vbox1.Add(previewScroll);
Add(vbox1);
DefaultWidth = 640;
DefaultHeight = 480;
ShowAll();
DeleteEvent += new DeleteEventHandler(OnDeleteEvent);
sizeScale.ValueChanged += new System.EventHandler(OnSizeScaleValueChanged);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment