Recently I’ve been playing around with GTK# for writing my own little text editor (just for fun). Initially I started the project with Scala, JavaFX and Jython; however there were just too many quirks with SBT and JavaFX, as well as long startup / compilation times that let me ditch them after all. Instead I decided to give C#, GTK# and IronPython a try and so far I’m very pleased. Everything just seems to work and compilation / start up times are much much better.
The only thing that took me a while to figure out today was how to set the tab size of the TextView component. I should probably read a proper GTK tutorial some time, but in the end I got it working using just the API reference. Here’s the code:
public static class TextViewExtensions { /// <summary> /// Sets the tabulator width of this textview to a multiple of the /// width of a space. /// </summary> /// <param name="textview">A TextView component</param> /// <param name="font">Font used in the textview</param> /// <param name="numSpaces">Number of spaces</param> public static void AdjustTabSize(this TextView textview, FontDescription font, int numSpaces) { int charWidth = 0, charHeight = 0; var layout = textview.CreatePangoLayout("A"); var tabs = new TabArray(30, true); layout.FontDescription = font; layout.GetPixelSize(out charWidth, out charHeight); for (int i = 0; i < tabs.Size; i++) { tabs.SetTab(i, TabAlign.Left, i * charWidth * numSpaces); } textview.Tabs = tabs; } }
Thanks for this code.
It helped me a lot today.
Thanks a lot for that.