Make »Copy image address« work.
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 906d145..3b3476a 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -29,7 +29,7 @@
 char *argv0;
 
 #define LENGTH(x)               (sizeof x / sizeof x[0])
-#define CLEANMASK(mask)                (mask & (MODKEY|GDK_SHIFT_MASK))
+#define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
 #define COOKIEJAR_TYPE          (cookiejar_get_type ())
 #define COOKIEJAR(obj)          (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
 
@@ -148,8 +148,9 @@ static void navigate(Client *c, const Arg *arg);
 static Client *newclient(void);
 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
-static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
-static void popupactivate(GtkMenuItem *menu, Client *);
+static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
+               WebKitHitTestResult *target, gboolean keyboard, Client *c);
+static void menuactivate(GtkMenuItem *item, Client *c);
 static void print(Client *c, const Arg *arg);
 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
                gpointer d);
@@ -164,8 +165,7 @@ static void sigchld(int unused);
 static void source(Client *c, const Arg *arg);
 static void spawn(Client *c, const Arg *arg);
 static void stop(Client *c, const Arg *arg);
-static void titlechange(WebKitWebView *v, WebKitWebFrame *frame,
-               const char *title, Client *c);
+static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
 static void toggle(Client *c, const Arg *arg);
 static void togglecookiepolicy(Client *c, const Arg *arg);
 static void togglegeolocation(Client *c, const Arg *arg);
@@ -656,7 +656,7 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
 
 static void
 loaduri(Client *c, const Arg *arg) {
-       char *u, *rp;
+       char *u = NULL, *rp;
        const char *uri = (char *)arg->v;
        Arg a = { .b = FALSE };
        struct stat st;
@@ -683,9 +683,9 @@ loaduri(Client *c, const Arg *arg) {
                webkit_web_view_load_uri(c->view, u);
                c->progress = 0;
                c->title = copystr(&c->title, u);
-               g_free(u);
                updatetitle(c);
        }
+       g_free(u);
 }
 
 static void
@@ -707,6 +707,9 @@ newclient(void) {
        if(!(c = calloc(1, sizeof(Client))))
                die("Cannot malloc!\n");
 
+       c->title = NULL;
+       c->progress = 100;
+
        /* Window */
        if(embed) {
                c->win = gtk_plug_new(embed);
@@ -746,7 +749,7 @@ newclient(void) {
        c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
 
        g_signal_connect(G_OBJECT(c->view),
-                       "title-changed",
+                       "notify::title",
                        G_CALLBACK(titlechange), c);
        g_signal_connect(G_OBJECT(c->view),
                        "hovering-over-link",
@@ -779,8 +782,8 @@ newclient(void) {
                        "button-release-event",
                        G_CALLBACK(buttonrelease), c);
        g_signal_connect(G_OBJECT(c->view),
-                       "populate-popup",
-                       G_CALLBACK(populatepopup), c);
+                       "context-menu",
+                       G_CALLBACK(contextmenu), c);
        g_signal_connect(G_OBJECT(c->view),
                        "resource-request-starting",
                        G_CALLBACK(beforerequest), c);
@@ -845,6 +848,8 @@ newclient(void) {
                        kioskmode ^ 1, NULL);
        g_object_set(G_OBJECT(settings), "default-font-size",
                        defaultfontsize, NULL);
+       g_object_set(G_OBJECT(settings), "resizable-text-areas",
+                       1, NULL);
 
        /*
         * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
@@ -890,7 +895,6 @@ newclient(void) {
        if(hidebackground)
                webkit_web_view_set_transparent(c->view, TRUE);
 
-       c->title = NULL;
        c->next = clients;
        clients = c;
 
@@ -910,11 +914,13 @@ newclient(void) {
 static void
 newwindow(Client *c, const Arg *arg, gboolean noembed) {
        guint i = 0;
-       const char *cmd[14], *uri;
+       const char *cmd[16], *uri;
        const Arg a = { .v = (void *)cmd };
        char tmp[64];
 
        cmd[i++] = argv0;
+       cmd[i++] = "-a";
+       cmd[i++] = cookiepolicies;
        if(!enablescrollbars)
                cmd[i++] = "-b";
        if(embed && !noembed) {
@@ -942,24 +948,27 @@ newwindow(Client *c, const Arg *arg, gboolean noembed) {
        spawn(NULL, &a);
 }
 
-static void
-populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
-       GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
+static gboolean
+contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
+               gboolean keyboard, Client *c) {
+       GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
 
        for(GList *l = items; l; l = l->next) {
-               g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
+               g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
        }
 
        g_list_free(items);
+       return FALSE;
 }
 
 static void
-popupactivate(GtkMenuItem *menu, Client *c) {
+menuactivate(GtkMenuItem *item, Client *c) {
        /*
         * context-menu-action-2000     open link
         * context-menu-action-1        open link in window
         * context-menu-action-2        download linked file
         * context-menu-action-3        copy link location
+        * context-menu-action-7        copy image address
         * context-menu-action-13       reload
         * context-menu-action-10       back
         * context-menu-action-11       forward
@@ -967,10 +976,10 @@ popupactivate(GtkMenuItem *menu, Client *c) {
         */
 
        GtkAction *a = NULL;
-       const char *name;
-       GtkClipboard *prisel;
+       const char *name, *uri;
+       GtkClipboard *prisel, *clpbrd;
 
-       a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
+       a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
        if(a == NULL)
                return;
 
@@ -978,7 +987,13 @@ popupactivate(GtkMenuItem *menu, Client *c) {
        if(!g_strcmp0(name, "context-menu-action-3")) {
                prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
                gtk_clipboard_set_text(prisel, c->linkhover, -1);
-       }
+       } else if(!g_strcmp0(name, "context-menu-action-7")) {
+               prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+               clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
+               uri = gtk_clipboard_wait_for_text(clpbrd);
+               if(uri)
+                       gtk_clipboard_set_text(prisel, uri, -1);
+  }
 }
 
 static void
@@ -1174,9 +1189,12 @@ stop(Client *c, const Arg *arg) {
 }
 
 static void
-titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
-       c->title = copystr(&c->title, t);
-       updatetitle(c);
+titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
+       const gchar *t = webkit_web_view_get_title(view);
+       if (t) {
+               c->title = copystr(&c->title, t);
+               updatetitle(c);
+       }
 }
 
 static void
@@ -1333,16 +1351,18 @@ updatetitle(Client *c) {
                                        pagestat, c->linkhover);
                } else if(c->progress != 100) {
                        t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
-                                       togglestat, pagestat, c->title);
+                                       togglestat, pagestat,
+                                       (c->title == NULL)? "" : c->title);
                } else {
                        t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
-                                       c->title);
+                                       (c->title == NULL)? "" : c->title);
                }
 
                gtk_window_set_title(GTK_WINDOW(c->win), t);
                g_free(t);
        } else {
-               gtk_window_set_title(GTK_WINDOW(c->win), c->title);
+               gtk_window_set_title(GTK_WINDOW(c->win),
+                               (c->title == NULL)? "" : c->title);
        }
 }
 
@@ -1386,11 +1406,15 @@ zoom(Client *c, const Arg *arg) {
 int
 main(int argc, char *argv[]) {
        Arg arg;
+       Client *c;
 
        memset(&arg, 0, sizeof(arg));
 
        /* command line args */
        ARGBEGIN {
+       case 'a':
+               cookiepolicies = EARGF(usage());
+               break;
        case 'b':
                enablescrollbars = 0;
                break;
@@ -1455,7 +1479,7 @@ main(int argc, char *argv[]) {
                useragent = EARGF(usage());
                break;
        case 'v':
-               die("surf-"VERSION", ©2009-2012 surf engineers, "
+               die("surf-"VERSION", ©2009-2014 surf engineers, "
                                "see LICENSE for details\n");
        case 'x':
                showxid = TRUE;
@@ -1470,9 +1494,12 @@ main(int argc, char *argv[]) {
                arg.v = argv[0];
 
        setup();
-       newclient();
-       if(arg.v)
+       c = newclient();
+       if(arg.v) {
                loaduri(clients, &arg);
+       } else {
+               updatetitle(c);
+       }
 
        gtk_main();
        cleanup();