Adding middle-click behaviour to open a tab when embedded or a window when
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index f7cba3e..097ecc9 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -21,6 +21,7 @@
 #include <sys/file.h>
 
 #define LENGTH(x)               (sizeof x / sizeof x[0])
+#define CLEANMASK(mask)         (mask & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
 
 enum { AtomFind, AtomGo, AtomUri, AtomLast };
 
@@ -65,6 +66,7 @@ static char *progname;
 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);
@@ -88,7 +90,7 @@ static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
 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);
@@ -140,6 +142,23 @@ buildpath(const char *path) {
        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)
@@ -215,7 +234,7 @@ decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, We
        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;
@@ -360,7 +379,7 @@ keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
        updatewinid(c);
        for(i = 0; i < LENGTH(keys); i++) {
                if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
-                               && (ev->state & keys[i].mod) == keys[i].mod
+                               && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
                                && keys[i].func) {
                        keys[i].func(c, &(keys[i].arg));
                        processed = TRUE;
@@ -403,7 +422,7 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
                setatom(c, AtomUri, uri);
                break;
        case WEBKIT_LOAD_FINISHED:
-               c->progress = 0;
+               c->progress = 100;
                update(c);
                break;
        default:
@@ -493,6 +512,7 @@ newclient(void) {
        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();
@@ -567,14 +587,14 @@ newrequest(SoupSession *s, SoupMessage *msg, gpointer v) {
 }
 
 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;
@@ -648,31 +668,37 @@ reload(Client *c, const Arg *arg) {
 
 void
 scroll_h(Client *c, const Arg *arg) {
- scroll(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(c->scroll)), arg);
+       scroll(gtk_scrolled_window_get_hadjustment(
+                               GTK_SCROLLED_WINDOW(c->scroll)), arg);
 }
 
 void
 scroll_v(Client *c, const Arg *arg) {
- scroll(gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll)), arg);
+       scroll(gtk_scrolled_window_get_vadjustment(
+                               GTK_SCROLLED_WINDOW(c->scroll)), arg);
 }
 
 void
 scroll(GtkAdjustment *a, const Arg *arg) {
- gdouble v;
+       gdouble v;
+
+       v = gtk_adjustment_get_value(a);
+       switch (arg->i){
+       case +10000:
+       case -10000:
+               v += gtk_adjustment_get_page_increment(a) *
+                       (arg->i / 10000);
+               break;
+       case +20000:
+       case -20000:
+       default:
+               v += gtk_adjustment_get_step_increment(a) * arg->i;
+       }
 
- v = gtk_adjustment_get_value(a);
- switch (arg->i){
- case +10000:
- case -10000:
- v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000); break;
- case +20000:
- case -20000:
- default:
- v += gtk_adjustment_get_step_increment(a) * arg->i;
- }
- v = MAX(v, 0.0);
- v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
- gtk_adjustment_set_value(a, v);
+       v = MAX(v, 0.0);
+       v = MIN(v, gtk_adjustment_get_upper(a) -
+                       gtk_adjustment_get_page_size(a));
+       gtk_adjustment_set_value(a, v);
 }
 
 void
@@ -782,7 +808,8 @@ spawn(Client *c, const Arg *arg) {
 void
 eval(Client *c, const Arg *arg) {
        WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
-       evalscript(webkit_web_frame_get_global_context(frame), ((char **)arg->v)[0], "");
+       evalscript(webkit_web_frame_get_global_context(frame),
+                       ((char **)arg->v)[0], "");
 }
 
 void
@@ -857,7 +884,7 @@ main(int argc, char *argv[]) {
                switch(argv[i][1]) {
                case 'e':
                        if(++i < argc)
-                               embed = atoi(argv[i]);
+                               embed = strtol(argv[i], NULL, 0);
                        else
                                usage();
                        break;