Replace linkhover() with mousetargetchanged()
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 79a71fe..af994bb 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -35,14 +35,16 @@ char *argv0;
 #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
 
 enum { AtomFind, AtomGo, AtomUri, AtomLast };
+
 enum {
-       ClkDoc   = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
-       ClkLink  = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
-       ClkImg   = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
-       ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
-       ClkSel   = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
-       ClkEdit  = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
-       ClkAny   = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
+       OnDoc   = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
+       OnLink  = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
+       OnImg   = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
+       OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
+       OnEdit  = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
+       OnBar   = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
+       OnSel   = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
+       OnAny   = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
 };
 
 typedef union Arg Arg;
@@ -57,7 +59,8 @@ typedef struct Client {
        Window xid;
        WebKitWebView *view;
        WebKitWebInspector *inspector;
-       char *title, *linkhover;
+       WebKitHitTestResult *mousepos;
+       const char *title, *targeturi;
        const char *needle;
        gint progress;
        struct Client *next;
@@ -149,8 +152,8 @@ static void inspector_finished(WebKitWebInspector *i, Client *c);
 
 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
                          GdkModifierType mods, Client *c);
-static void linkhover(WebKitWebView *v, const char* t, const char* l,
-                      Client *c);
+static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
+               guint modifiers, Client *c);
 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
                              Client *c);
 static void loaduri(Client *c, const Arg *arg);
@@ -179,7 +182,7 @@ static void setup(void);
 static void sigchld(int unused);
 static void spawn(Client *c, const Arg *arg);
 static void stop(Client *c, const Arg *arg);
-static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
+static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
 static void titlechangeleave(void *a, void *b, Client *c);
 static void toggle(Client *c, const Arg *arg);
 static void togglecookiepolicy(Client *c, const Arg *arg);
@@ -689,14 +692,24 @@ keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
 }
 
 void
-linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
+mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
+    Client *c)
 {
-       if (l) {
-               c->linkhover = copystr(&c->linkhover, l);
-       } else if (c->linkhover) {
-               free(c->linkhover);
-               c->linkhover = NULL;
-       }
+       WebKitHitTestResultContext hc;
+
+       /* Keep the hit test to know where is the pointer on the next click */
+       c->mousepos = h;
+
+       hc = webkit_hit_test_result_get_context(h);
+
+       if (hc & OnLink)
+               c->targeturi = webkit_hit_test_result_get_link_uri(h);
+       else if (hc & OnImg)
+               c->targeturi = webkit_hit_test_result_get_image_uri(h);
+       else if (hc & OnMedia)
+               c->targeturi = webkit_hit_test_result_get_media_uri(h);
+       else
+               c->targeturi = NULL;
        updatetitle(c);
 }
 
@@ -865,10 +878,10 @@ newview(Client *c, WebKitWebView *rv)
 
        g_signal_connect(G_OBJECT(v),
                         "notify::title",
-                        G_CALLBACK(titlechange), c);
+                        G_CALLBACK(titlechanged), c);
        g_signal_connect(G_OBJECT(v),
-                        "hovering-over-link",
-                        G_CALLBACK(linkhover), c);
+                        "mouse-target-changed",
+                        G_CALLBACK(mousetargetchanged), c);
        g_signal_connect(G_OBJECT(v),
                         "geolocation-policy-decision-requested",
                         G_CALLBACK(geopolicyrequested), c);
@@ -1277,13 +1290,10 @@ stop(Client *c, const Arg *arg)
 }
 
 void
-titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
+titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
 {
-       const gchar *t = webkit_web_view_get_title(view);
-       if (t) {
-               c->title = copystr(&c->title, t);
-               updatetitle(c);
-       }
+       c->title = webkit_web_view_get_title(c->view);
+       updatetitle(c);
 }
 
 void