Re: gtk_text_buffer with clickable links?



Andreas Rönnquist wrote:
Hi!

I have managed to make a gtk_text_buffer and colorize what appears to be links using 
gtk_text_buffer_apply_tag and some simple search algorithm.

Does someone have any resources explaining how to make those clickable - and (later problem) to open them in 
"default webbrowser" or such, and also make them underlined when mouse is hovering above them?

I have found an example, but that involves inserting actual buttons into the textview, and that isn't really 
what I am after. (There must be a simpler way to do what I want..)

Help and pointers to good documentation/tutorials/examples would be much appreciated.
I have looked at source that includes the requested behaviour, but it contains a _lot_ of other stuff too, so 
it is quite hard to make out what is important.

I would like to keep it as simple as possible (Editable textbuffer isn't at all required, also I am not 
interested in full syntax highligthing or similar. - as simple as possible.)

/Andreas


I needed to do this quite a while back, and at the time I took a look at the GAIM code to see how they did it. It was a matter of connecting an event to the tag used to mark the text, and when activated, you'd open the URL.

You can check the code I wrote by downloading the source @ http://aoeex.com/projects/easydict/ The relevant functions are textview_add_url(), textview_url_activate(), and textview_motion_notify_cb().

Here's snippets of the functions relevant parts:
void textview_add_url(const gchar *txtURL){
...
        if (!url){
                url=gtk_text_tag_new("url_tag");
g_object_set(url, "underline", PANGO_UNDERLINE_SINGLE, "underline-set", true, NULL);
                g_object_set(url, "foreground-gdk", &blue, NULL);
                g_signal_connect(G_OBJECT(url), "event",
                                 G_CALLBACK(textview_url_activate), NULL);
                gtk_text_tag_table_add(tagTbl, url);
        }
...
}


gboolean textview_url_activate(GtkTextTag *tag, __attribute__((unused))GObject *arg1, GdkEvent *event, GtkTextIter *arg2, __attribute__((unused))gpointer d){
        GdkEventButton *event_btn=(GdkEventButton*)event;
        if (event->type == GDK_BUTTON_RELEASE && event_btn->button == 1){
...

                link=gtk_text_iter_get_text(&start, &end);
                if (link){
                        gint memNeeded=strlen(BROWSER_CMDLINE)+strlen(link)+1;
                        gchar *cmd_line=KLIB_malloc(memNeeded);
                        snprintf(cmd_line, memNeeded, BROWSER_CMDLINE, link);
                        system(cmd_line);
                }
        }
        return false;
}


gboolean textview_motion_notify_cb(GtkWidget *textview, GdkEventMotion *event, __attribute__((unused))gpointer d){
...
        if (gtk_text_iter_has_tag(&iter, tag)){
                GdkCursor *cur=gdk_cursor_new(GDK_HAND2);
                gdk_window_set_cursor(event->window, cur);
        }
        else {
                GdkCursor *cur=gdk_cursor_new(GDK_XTERM);
                gdk_window_set_cursor(event->window, cur);
        }
        return false;
}
--
Keith Maika
http://aoeex.com/ - Personal site
http://www.gnu.org/philosophy/can-you-trust.html - Can you trust your computer? http://www.gnu.org/philosophy/no-word-attachments.html - Support Open Communication.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]