textbuffer urls

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

textbuffer urls

by jmauster :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I'm building out a textbuffer to be a rich text editor. How do I apply a tag which functions as a link? (For functionality much like the Add Link button at the top of this post...)  Or to put it more technically, how do I apply hyperlinks in textbuffers?

You'd be surprised at how hard it is to google textbuffer & link, or textbuffer & url, or textbuffer & hyperlink, as these always result in posts with people talking about posts regarding textbuffers in general.

And guidance will be much appreciated!

Thanks,

Re: textbuffer urls

by Chris Howie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Jun 20, 2009 at 5:03 PM, jmauster<evan.arnold@...> wrote:

> I'm building out a textbuffer to be a rich text editor. How do I apply a tag
> which functions as a link? (For functionality much like the Add Link button
> at the top of this post...)  Or to put it more technically, how do I apply
> hyperlinks in textbuffers?
>
> You'd be surprised at how hard it is to google textbuffer & link, or
> textbuffer & url, or textbuffer & hyperlink, as these always result in posts
> with people talking about posts regarding textbuffers in general.
>
> And guidance will be much appreciated!

I don't have the time to dig into the API at the moment, but I can
give you a basic outline of what you need to do using what I remember
of the API.

To create a link:

* Create a TextTag with no name ("new TextTag(null)").
* Set the visual appearance you want (blue, underlined, etc.).
* Store the hyperlink target somewhere, probably a Dictionary<TextTag,
string> would be the most useful.
* Don't forget to add the tag to the TextBuffer.TagTable.
* Apply the tag to the text where you want the link.

Then during construction of the buffer you need to attach a handler to
the mouse movement signal (I forget what it's called) as well as the
clicked signal.  When either are fired you will have to:

* Obtain the X,Y coordinates of the movement/click.
* Map these somehow to a TextIter.

Then, in the movement handler you will then want to conditionally set
the mouse pointer to the "hand" pointer or the normal one, based on
whether or not a hyperlink tag is present.  You could use a LINQ query
to determine this, something like "bool hyperlink = (from i in
iter.Tags where dictionary.ContainsKey(i) select i).FirstOrDefault()
!= null;" (this is from memory, you may have to tweak it).

In the click handler you can do something like "TextTag linktag =
(from i in iter.Tags where dictionary.ContainsKey(i) select
i).FirstOrDefault();" and then if linktag != null you can do "string
url = dictionary[linktag];" to obtain the URL you stored and use it
however you wish.

Don't forget that when you remove text containing a link you should
remove the TextTag from the TextBuffer.TagTable and also
dictionary.Remove(tag).

Hope this helps!

--
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

Re: textbuffer urls

by jmauster :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks - that was striaghtforward to implement. Also, I'm able to save the content of the textbuffer as more/less html by replacing the texttags as anchor tags. (or any other format)
 
Now, and perhaps this might be hard, is is possible to display the text WITH the URL functionality? From what I understand the pango mark-up doesn't recognize anchor tags, likewise the cellrenderertext doesn't appear to user iters.
 
Any ideas on how to put URL functionality into a Treeview Cell?
 
Thanks,

Evan

On Mon, Jun 22, 2009 at 9:28 AM, Chris Howie <cdhowie@...> wrote:

To create a link:

* Create a TextTag with no name ("new TextTag(null)").
* Set the visual appearance you want (blue, underlined, etc.).
* Store the hyperlink target somewhere, probably a Dictionary<TextTag,
string> would be the most useful.
* Don't forget to add the tag to the TextBuffer.TagTable.
* Apply the tag to the text where you want the link.

Then during construction of the buffer you need to attach a handler to
the mouse movement signal (I forget what it's called) as well as the
clicked signal.  When either are fired you will have to:

* Obtain the X,Y coordinates of the movement/click.
* Map these somehow to a TextIter.

Then, in the movement handler you will then want to conditionally set
the mouse pointer to the "hand" pointer or the normal one, based on
whether or not a hyperlink tag is present.  You could use a LINQ query
to determine this, something like "bool hyperlink = (from i in
iter.Tags where dictionary.ContainsKey(i) select i).FirstOrDefault()
!= null;" (this is from memory, you may have to tweak it).

In the click handler you can do something like "TextTag linktag =
(from i in iter.Tags where dictionary.ContainsKey(i) select
i).FirstOrDefault();" and then if linktag != null you can do "string
url = dictionary[linktag];" to obtain the URL you stored and use it
however you wish.

Don't forget that when you remove text containing a link you should
remove the TextTag from the TextBuffer.TagTable and also
dictionary.Remove(tag).

Hope this helps!

--
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

Re: textbuffer urls

by Chris Howie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jun 23, 2009 at 10:21 AM, Evan Arnold<evan.arnold@...> wrote:
> Now, and perhaps this might be hard, is is possible to display the text WITH
> the URL functionality?

I am not sure what you mean by this.

> Any ideas on how to put URL functionality into a Treeview Cell?

Is this related to your previous question?  I still don't quite know
what you are trying to ask.

--
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

Re: textbuffer urls

by jmauster :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sorry, let me clarify (now, having had coffee).
 
I have a textbuffer which is sort of working like a Rich Text Editor. Let's say a user adds a link to the textbuffer using the method you described up above.
 
After text is entered in the textbuffer and the user hits submit, it moves into a treeview. I can swap <b> tags for bold texttags and <i> tags for italic texttags, but I am not sure what to do for places where a user has entered a link.  
 
Do you think there is sufficient flexibility in TreeView to show links as underlined and blue (shouldn't be too difficult) but also to have the on-click-opens-a-browser?
 
Does that make more sense?
 
Thanks again for your help,


 
On Tue, Jun 23, 2009 at 9:24 AM, Chris Howie <cdhowie@...> wrote:
On Tue, Jun 23, 2009 at 10:21 AM, Evan Arnold<evan.arnold@...> wrote:
> Now, and perhaps this might be hard, is is possible to display the text WITH
> the URL functionality?

I am not sure what you mean by this.

> Any ideas on how to put URL functionality into a Treeview Cell?

Is this related to your previous question?  I still don't quite know
what you are trying to ask.

--


_______________________________________________
Gtk-sharp-list maillist  -  Gtk-sharp-list@...
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Re: textbuffer urls

by Chris Howie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jun 23, 2009 at 11:20 AM, Evan Arnold<evan.arnold@...> wrote:
> Sorry, let me clarify (now, having had coffee).

I feel ya.  :)

> I have a textbuffer which is sort of working like a Rich Text Editor. Let's
> say a user adds a link to the textbuffer using the method you described up
> above.
>
> After text is entered in the textbuffer and the user hits submit, it moves
> into a treeview. I can swap <b> tags for bold texttags and <i> tags for
> italic texttags, but I am not sure what to do for places where a user has
> entered a link.
>
> Do you think there is sufficient flexibility in TreeView to show links as
> underlined and blue (shouldn't be too difficult) but also to have the
> on-click-opens-a-browser?
>
> Does that make more sense?

Yes, it does.  The visual representation should be no problem at all;
have a look at <http://library.gnome.org/devel/pango/stable/PangoMarkupFormat.html>.
 Wrapping the links in something like <span color="blue"
underline="single">...</span> will probably get you what you want
visually.

As far as the on-click handling I have to say that honestly I have no
idea how you're going to pull that off.  :)

--
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