textbuffer text as string with markup.

View: New views
4 Messages — Rating Filter:   Alert me  

textbuffer text as string with markup.

by jmauster :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Let's say I have a TextView called textNote. And let's say that there are some tags that have been applied (bold, italics).

When I use this code in an eventhandler:

string text = textNote.Buffer.Text;
textNote.Buffer.Text = string.Empty;
textNote.Buffer.Text = text;

I end up losing the markup. Why is that? How can I preserve the markup in a string?

Thanks,

Evan

Re: textbuffer text as string with markup.

by jmauster :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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!

Thanks,



jmauster wrote:
Hi,

Let's say I have a TextView called textNote. And let's say that there are some tags that have been applied (bold, italics).

When I use this code in an eventhandler:

string text = textNote.Buffer.Text;
textNote.Buffer.Text = string.Empty;
textNote.Buffer.Text = text;

I end up losing the markup. Why is that? How can I preserve the markup in a string?

Thanks,

Evan

Re: textbuffer text as string with markup.

by Chris Howie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Jun 13, 2009 at 1:03 PM, jmauster<evan.arnold@...> 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
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
_______________________________________________
Gtk-sharp-list maillist  -  Gtk-sharp-list@...
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Parent Message unknown Re: textbuffer text as string with markup.

by Chris Howie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Jun 20, 2009 at 12:11 PM, Evan Arnold<evan.arnold@...> wrote:
> Chris,
>
> Thanks a ton. Although a pain, that completely solved the problem.

No problem.  While it is a pain to do it this way, at least it's possible.  :)

--
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
_______________________________________________
Gtk-sharp-list maillist  -  Gtk-sharp-list@...
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list