[Gtk-sharp-list] textbuffer urls
cdhowie at gmail.com
Mon Jun 22 10:28:51 EDT 2009
On Sat, Jun 20, 2009 at 5:03 PM, jmauster<evan.arnold at gmail.com> 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
Hope this helps!
More information about the Gtk-sharp-list