I work on GUI project(http://smartdict.net) based on ruby-gnome2. I want to insert a web link into Gtk::TextBuffer element. How can I do that?
It's a ruby project but solutions on C or Python would be useful as well. Thanks.
I work on GUI project(http://smartdict.net) based on ruby-gnome2. I want to insert a web link into Gtk::TextBuffer element. How can I do that?
It's a ruby project but solutions on C or Python would be useful as well. Thanks.
This is how I did it:
As Micah said, you create a tag that makes the text look like a link (blue and underlined). Then connect a signal to the TextView, so when the user clicks on it, it will call a method to open the url. You'll need to devise a way for the method to know the link to open by keeping track of them in an array, or parsing the link based on the cursor position. That part is up to you.
class myTextView < Gtk::TextView
def initialize
signal_connect("button_release_event") { open_url() }
buffer.create_tag("blue", { "foreground" => "#0000FF", "underline" => Pango::UNDERLINE_SINGLE })
start_iter, end_iter = get_line_iters()
buffer.apply_tag("blue", start_iter, end_iter)
end
def open_url()
# open link here
end
end
This is a standard Gtk solution. I would suggest using visualruby:
While I haven't done this myself, I would imagine you could do it 2 ways:
Style the text yourself (blue with an underline) using text tags and handle the launching of the URL yourself.
Use a gtk_text_buffer_insert_child_anchor
to specify where in the buffer to insert the link, and then gtk_text_view_add_child_at_anchor
to insert a GtkLinkButton
into the text view.