Using the Mono GtkSourceView Control 14 February 2006 at 00:00
The GtkSourceView control is an excellent editing component, but documentation on it's use is few and far between. I thought I'd add this simple example on how to use the control.

Other items to note:

  • You will need to add a reference gtksourceview-sharp to your project
  • The default folder for the lang files is /usr/share/gtksourceview-x.x/language-specs/

using System;
using Gtk;
using GtkSourceView;

public class MyClass {

    public MyClass(string [] args) {

        Application.Init();	

        Window window = new Window("MyApp");
        window.DefaultWidth = 600;
        window.DefaultHeight = 600;
		
        SourceView sourceView = null;

        // create the manager object that is used to manage the languages.  If you want to use your own
        // .lang files, add the directoy to the LangFilesDirs collection
        SourceLanguagesManager slm = new SourceLanguagesManager();
        slm.LangFilesDirs.Append("/home/mylangfiles");
	
        // load the language you want to use to highlight from a mime type - if the mime type does not exist in
        // any of the lang files, lang will come back as null	
        SourceLanguage lang = slm.GetLanguageFromMimeType("text/x-php");
        if (lang != null) {
                // create new source buffer and sourceview objects with the language
                SourceBuffer buffer = new SourceBuffer(lang);
                buffer.Highlight = true;
                sourceView = new SourceView(buffer);
                // set your own tag styles if you like - the descriptions have ASCII characters surrounded by "@"
                // non-alpha characters
                SourceTagStyle sts = lang.GetTagStyle("C@43@@43@@32@Line@32@Comment");
                sts.Italic = true;
                sts.Foreground = new Gdk.Color(255, 0, 0);
                lang.SetTagStyle("C@43@@43@@32@Line@32@Comment", sts);
        }
        else {
                sourceView = new SourceView();
        }
        sourceView.ShowLineNumbers = true;
	
        // add the control to the window, and display	
        window.Add(sourceView);
        window.ShowAll();
        window.DeleteEvent += new DeleteEventHandler (OnWinDelete);
        Application.Run();	
    }
	
    public static void OnWinDelete (object obj, DeleteEventArgs args) {
        Application.Quit ();
    }  
}