I’ve been needing a label widget with support for embedded URLs lately for a couple of projects, but nothing was available that met my needs. So last night I put together libsexy, which will be my testing ground for experimental widgets that do things that I consider very wrong but very cool.
The first widget I put into libsexy is SexyUrlLabel. It’s an actual GtkLabel subclass with a custom sexy_url_label_set_markup() function. This function takes the same markup that gtk_label_set_markup() takes, except it also groks <a href=”…”>…<a>. The link turns into the standard blue text with an underline. Moving the mouse over the link changes the cursor to the standard hand cursor. Clicking it will emit a url_clicked signal, and right-clicking it will pop up a menu with “Open Link” and “Copy Link Address.”
The only problem I have encountered so far is that if you have a link that spans multiple lines, the hyperlink won’t work on the second line except for parts below the top line’s link. I’d like to blame Pango for this, but I know someone will tell me a way to make this work 🙂 I’m currently getting the X and Y coordinates based on the range that Pango specifies for the attribute, but this doesn’t handle line wraps well. Suggestions are welcome.
This will be going into notification-daemon probably today. I won’t be around after today until next Saturday or Sunday. I’m going to Disneyland with my girlfriend and my family, and won’t have net access (nor do I want it). If anyone messes around with the widget and has any patches, please send them to me and I’ll get back to you when I’m next available.
Update: notification-daemon now uses SexyUrlLabel. Markup (as per the spec) is supported and links are supported. Have fun!
You could try storing the character range instead of x/y coordinates in the url_list items, and then determining if it’s a link based on whether the mouse pointer is over that range of characters (by getting the x/y char pos using the pango_layout_xy_to_index() func, and comparing the result to the url items’ ranges).