[Gtk-sharp-list] textbuffer text as string with markup.

Chris Howie cdhowie at gmail.com
Mon Jun 15 13:57:51 EDT 2009

On Sat, Jun 13, 2009 at 1:03 PM, jmauster<evan.arnold at gmail.com> wrote:
> Hi guys,
> I know it's silly to reply to my own text, but now that I've done some more
> research, I think I should clarify the problem. Sorry if seeing this in your
> inbox again is annoying.
> I have a treeview and a textview with some simple markup being done like so:
>                protected virtual void OnBoldActionActivated (object sender,
> System.EventArgs e)
>                {
>                        TextIter startIter;
>                        TextIter stopIter;
>                        TextBuffer buffer = textNote.Buffer;
>                        if (buffer.GetSelectionBounds(out startIter, out stopIter)) {
>                                buffer.ApplyTag("bold", startIter, stopIter);
>                        }
>                }
> As some point, I create a data object like so:
> TestObject testObj = new TestObj(textNote.Buffer.Text);
> And the model for my treeview is a List<TestObject>.
> The render method is written like this:
>                        TestObject testObj = (TestObject) model.GetValue(iter, 0);
>                        (cell as Gtk.CellRendererText).Markup = testObj.Text;
> however - somewhere in there my markup is getting lost.
> Any feedback would be very much appreciated! I know this has to be a pretty
> basic question - so even a few pointers in the right direction would be very
> well awesome!

The issue you're seeing is that TextBuffer.Text does not return any
markup.  Since tags are not represented as markup, there is no simple
property that will turn the contents of a TextBuffer into a string
with Pango markup.

If you want to hack it manually, have a look at TextBuffer.StartIter
and all of the properties and methods on TextIter.  Specifically, you
will want to examine TextIter.ForwardChar(), TextIter.Tags,
TextIter.BeginsTag(), and TextIter.EndsTag().  You can construct a
LINQ query something like these to find the tags that begin and end on
the current iter:

IEnumerable<TextTag> openedtags = from i in iter.Tags where
iter.BeginsTag(i) select i;
IEnumerable<TextTag> closedtags = from i in iter.Tags where
iter.EndsTag(i) select i;

Then you can use StringBuilder to build a string with markup, by
extracting the style information.  If this seems like a pain in the
ass, that's because it is.  :)

Chris Howie

More information about the Gtk-sharp-list mailing list