evolvedmicrobe
evolvedmicrobe

Reputation: 2722

Color Text Background in GTK#

I am using GTK# and a TextWidget to display editable text. I want the background color of each item of text to be determined by the character (so that all "A" are red, all "G" are green, all "C" blue, etc.).

It seems this might be possible, but does anyone know an efficient way to tell GTK# to color input text this way?

Upvotes: 0

Views: 1595

Answers (1)

Matt Ward
Matt Ward

Reputation: 47937

You can change the colour of the text in a Gtk.TextView by using a TextTag.

An example below creates an error tag, which highlights the text with a red background when the text is inserted.

var textView = new Gtk.TextView ();

var errorTag = new TextTag ("error");
errorTag.Background = "#dc3122";
errorTag.Foreground = "white";
errorTag.Weight = Pango.Weight.Bold;
textView.Buffer.TagTable.Add (errorTag);

string text = "foo";

// Insert text with tag.
TextIter start = textView.Buffer.EndIter;
textView.Buffer.InsertWithTags (ref start, text, errorTag);

text = "bar";

// Insert text then apply tag.
textView.Buffer.Insert (ref start, text);
start = textView.Buffer.GetIterAtOffset (5);
TextIter end = textView.Buffer.GetIterAtOffset (6);
textView.Buffer.ApplyTag (errorTag, start, end);

var vbox = new Gtk.VBox ();
Add (vbox);
vbox.PackStart (textView);

vbox.ShowAll ();

Upvotes: 1

Related Questions