[Gtk-sharp-list] TextView utf8 string problem

Przemyslaw Sowa przemos@klub.chip.pl
Sun, 20 Jun 2004 12:59:28 +0200


I have problem with TextView and unicode strings. When I'm trying to add
some text with unicode character that is more then one byte long (for
example the biohazard sign \u2623 or any Polish letter) I gets error
message:

(<unknown>:1990): Gtk-CRITICAL **: file gtktextbuffer.c: line 557
(gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len,
NULL)' failed

But when I add the same string to ListView everything is OK.

Here is a sample app that shows the problem:

-cut-
using System;
using Gtk;

namespace TextViewTest
{
class TextViewTestApp : Window
{
public static void Main(string[] args)
{
Application.Init ();
new TextViewTestApp ();
Application.Run ();
}

public TextViewTestApp () : base ("TextViewTest Window")
{
// Create window and widgets
this.SetDefaultSize (400, 300);
this.DeleteEvent += new DeleteEventHandler (OnWindowDelete);
this.BorderWidth = 12;

HBox hbox = new HBox (true, 12);
this.Add (hbox);
ScrolledWindow scrolledWindow = new ScrolledWindow (null, null);
scrolledWindow.ShadowType = ShadowType.In;
hbox.PackStart (scrolledWindow);
TreeView treeview = new TreeView ();
treeview.HeadersVisible = false;
scrolledWindow.Add (treeview);

scrolledWindow = new ScrolledWindow (null, null);
scrolledWindow.ShadowType = ShadowType.In;
hbox.PackEnd (scrolledWindow);
TextView textview = new TextView ();
scrolledWindow.Add (textview);

// This is a string with unicode character
string unicodeString = "biohazard sign \u2623";

// This line produces fallowing error message:
// (<unknown>:1965): Gtk-CRITICAL **: file gtktextbuffer.c:
// line 557 (gtk_text_buffer_emit_insert): assertion
// `g_utf8_validate (text, len, NULL)' failed
textview.Buffer.SetText (unicodeString);

// But adding the string to a TreeView works as expected
ListStore store = new ListStore (typeof (string));
  store.AppendValues (unicodeString);
treeview.Model = store;
treeview.AppendColumn ("", new CellRendererText (), "text", 0);

// Console (Gnome Terminal) displays the string correctly
Console.WriteLine (unicodeString);

this.ShowAll ();
}

void OnWindowDelete (object o, DeleteEventArgs args)
{
Application.Quit ();
}
}
}
-cut-

I don't know what is wrong with this code?

Przemyslaw Sowa