[hackers] [surf] Adding middle-click behaviour to open a tab when embedded or a window when || Christoph Lohmann

From: <hg_AT_suckless.org>
Date: Sat, 13 Oct 2012 07:18:55 +0200 (CEST)

changeset: 247:451b71650216
tag: tip
user: Christoph Lohmann <20h_AT_r-36.net>
date: Sat Oct 13 07:16:08 2012 +0200
files: surf.c
description:
Adding middle-click behaviour to open a tab when embedded or a window when
not. Thanks to Carlos Pita <carlosjosepita_AT_gmail.com>!


diff -r 1fd52653b84d -r 451b71650216 surf.c
--- a/surf.c Fri Oct 12 11:48:38 2012 +0200
+++ b/surf.c Sat Oct 13 07:16:08 2012 +0200
_AT_@ -66,6 +66,7 @@
 static gboolean loadimage = 1, plugin = 1, script = 1;
 
 static char *buildpath(const char *path);
+static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl);
 static void cleanup(void);
 static void clipboard(Client *c, const Arg *arg);
 static char *copystr(char **str, const char *src);
_AT_@ -89,7 +90,7 @@
 static void loaduri(Client *c, const Arg *arg);
 static void navigate(Client *c, const Arg *arg);
 static Client *newclient(void);
-static void newwindow(Client *c, const Arg *arg);
+static void newwindow(Client *c, const Arg *arg, gboolean noembed);
 static void newrequest(SoupSession *s, SoupMessage *msg, gpointer v);
 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
 static void print(Client *c, const Arg *arg);
_AT_@ -141,6 +142,23 @@
         return apath;
 }
 
+static gboolean
+buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
+ WebKitHitTestResultContext context;
+ WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web, e);
+ Arg arg;
+
+ g_object_get(result, "context", &context, NULL);
+ if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
+ if(e->button == 2) {
+ g_object_get(result, "link-uri", &arg.v, NULL);
+ newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
+ return true;
+ }
+ }
+ return false;
+}
+
 void
 cleanup(void) {
         while(clients)
_AT_@ -216,7 +234,7 @@
         if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
                 webkit_web_policy_decision_ignore(p);
                 arg.v = (void *)webkit_network_request_get_uri(r);
- newwindow(NULL, &arg);
+ newwindow(NULL, &arg, 0);
                 return TRUE;
         }
         return FALSE;
_AT_@ -404,7 +422,7 @@
                 setatom(c, AtomUri, uri);
                 break;
         case WEBKIT_LOAD_FINISHED:
- c->progress = 0;
+ c->progress = 100;
                 update(c);
                 break;
         default:
_AT_@ -494,6 +512,7 @@
         g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
         g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
         g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
+ g_signal_connect(G_OBJECT(c->view), "button-release-event", G_CALLBACK(buttonrelease), c);
 
         /* Indicator */
         c->indicator = gtk_drawing_area_new();
_AT_@ -568,14 +587,14 @@
 }
 
 void
-newwindow(Client *c, const Arg *arg) {
+newwindow(Client *c, const Arg *arg, gboolean noembed) {
         guint i = 0;
         const char *cmd[10], *uri;
         const Arg a = { .v = (void *)cmd };
         char tmp[64];
 
         cmd[i++] = progname;
- if(embed) {
+ if(embed && !noembed) {
                 cmd[i++] = "-e";
                 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
                 cmd[i++] = tmp;
Received on Sat Oct 13 2012 - 07:18:55 CEST

This archive was generated by hypermail 2.3.0 : Sat Oct 13 2012 - 07:24:12 CEST