qulogic/talkatu

Parents e10028a8caee
Children 6db3b87f0e8a
Add in tooltip support for links. Position is a bit weird, not sure how to address it
--- a/talkatu/talkatuview.c Sat Jul 13 09:38:57 2019 -0500
+++ b/talkatu/talkatuview.c Sat Jul 13 10:02:45 2019 -0500
@@ -78,6 +78,30 @@
G_DEFINE_TYPE_WITH_PRIVATE(TalkatuView, talkatu_view, GTK_TYPE_TEXT_VIEW)
/******************************************************************************
+ * Helpers
+ *****************************************************************************/
+static gchar *
+talkatu_view_url_from_iter(TalkatuView *view, GtkTextIter *iter) {
+ GSList *tags = NULL, *tag = NULL;
+ gchar *url = NULL;
+
+ tags = gtk_text_iter_get_tags(iter);
+ for(tag = tags; tag != NULL; tag = tag->next) {
+ if(tag->data == NULL) {
+ continue;
+ }
+
+ url = g_object_get_data(G_OBJECT(tag->data), "talkatu-anchor-url");
+ if(url != NULL) {
+ break;
+ }
+ }
+ g_slist_free(tags);
+
+ return url;
+}
+
+/******************************************************************************
* Callbacks
*****************************************************************************/
static void
@@ -117,22 +141,9 @@
} else if(event_type == GDK_BUTTON_RELEASE) {
GdkEventButton *event_button = (GdkEventButton *)event;
TalkatuView *view = TALKATU_VIEW(object);
- GSList *tags = NULL, *tag = NULL;
gchar *url;
- /* walk through the tags here and look for the tag that has the url */
- tags = gtk_text_iter_get_tags(iter);
- for(tag = tags; tag != NULL; tag = tag->next) {
- if(tag->data == NULL) {
- continue;
- }
-
- url = g_object_get_data(G_OBJECT(tag->data), "talkatu-anchor-url");
- if(url != NULL) {
- break;
- }
- }
- g_slist_free(tags);
+ url = talkatu_view_url_from_iter(view, iter);
/* if we didn't find a url, bail */
if(url == NULL) {
@@ -326,6 +337,48 @@
}
}
+static gboolean
+talkatu_view_query_tooltip(GtkWidget *widget,
+ gint x,
+ gint y,
+ gboolean keyboard,
+ GtkTooltip *tooltip)
+{
+ GtkTextIter iter;
+ gchar *url = NULL;
+ gint adj_x, adj_y;
+
+ if(keyboard) {
+ return GTK_WIDGET_CLASS(talkatu_view_parent_class)->query_tooltip(widget, x, y, keyboard, tooltip);
+ }
+
+ /* convert the window coordinates to match whats visible */
+ gtk_text_view_window_to_buffer_coords(
+ GTK_TEXT_VIEW(widget),
+ GTK_TEXT_WINDOW_TEXT,
+ x, y,
+ &adj_x, &adj_y
+ );
+
+ /* now find the iter for what we're at */
+ gtk_text_view_get_iter_at_location(
+ GTK_TEXT_VIEW(widget),
+ &iter,
+ adj_x,
+ adj_y
+ );
+
+ /* look for a url, if we have one, add it to tooltip */
+ url = talkatu_view_url_from_iter(TALKATU_VIEW(widget), &iter);
+ if(url != NULL) {
+ gtk_tooltip_set_text(tooltip, url);
+
+ return TRUE;
+ }
+
+ return GTK_WIDGET_CLASS(talkatu_view_parent_class)->query_tooltip(widget, x, y, keyboard, tooltip);
+}
+
/******************************************************************************
* GObject Stuff
*****************************************************************************/
@@ -361,6 +414,11 @@
talkatu_view_init(TalkatuView *view) {
TalkatuViewPrivate *priv = talkatu_view_get_instance_private(view);
+ /* tell the widest class that we support tooltips. This is used to show
+ * link targets, and probably other stuff at some point.
+ */
+ gtk_widget_set_has_tooltip(GTK_WIDGET(view), TRUE);
+
/* set our event mask for the signals we care about */
gtk_widget_set_events(
GTK_WIDGET(view),
@@ -401,6 +459,7 @@
obj_class->set_property = talkatu_view_set_property;
widget_class->popup_menu = talkatu_view_popup_menu;
+ widget_class->query_tooltip = talkatu_view_query_tooltip;
/* add our default signal handlers */
klass->format_activate = talkatu_view_format_activate;