Skip to content

Setting the Tab Size of a TextView in GTK-Sharp

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;
	}
}
Advertisements

1 reply »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Bunkerbewohner1

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

My Del.icio.us

%d bloggers like this: