From 50892f443fe85d25926b6ab7feb73c34c4b72930 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Wed, 26 Mar 2014 21:50:02 +0100 Subject: [PATCH] Allow to copy link location via context menu. --- src/main.c | 30 ++++++++++++++++++++++++++++++ src/main.h | 1 + 2 files changed, 31 insertions(+) diff --git a/src/main.c b/src/main.c index c6ae00b..cdab2db 100644 --- a/src/main.c +++ b/src/main.c @@ -43,6 +43,9 @@ static char **args; VbCore vb; /* callbacks */ +static gboolean context_menu_cb(WebKitWebView *view, GtkWidget *menu, + WebKitHitTestResult *hitTestResult, gboolean keyboard, gpointer data); +static void context_menu_activate_cb(GtkMenuItem *item, gpointer data); static void webview_progress_cb(WebKitWebView *view, GParamSpec *pspec); static void webview_download_progress_cb(WebKitWebView *view, GParamSpec *pspec); static void webview_load_status_cb(WebKitWebView *view, GParamSpec *pspec); @@ -376,6 +379,28 @@ static gboolean hide_message() return false; } +static gboolean context_menu_cb(WebKitWebView *view, GtkWidget *menu, + WebKitHitTestResult *hitTestResult, gboolean keyboard, gpointer data) +{ + 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(context_menu_activate_cb), NULL); + } + g_list_free(items); + + return false; +} + +static void context_menu_activate_cb(GtkMenuItem *item, gpointer data) +{ + WebKitContextMenuAction action = webkit_context_menu_item_get_action(item); + if (action == WEBKIT_CONTEXT_MENU_ACTION_COPY_LINK_TO_CLIPBOARD) { + vb_set_clipboard( + &((Arg){VB_CLIPBOARD_PRIMARY|VB_CLIPBOARD_SECONDARY, vb.state.linkhover}) + ); + } +} + static void webview_progress_cb(WebKitWebView *view, GParamSpec *pspec) { vb.state.progress = webkit_web_view_get_progress(view) * 100; @@ -717,6 +742,7 @@ static void setup_signals() g_signal_connect(vb.gui.window, "destroy", G_CALLBACK(destroy_window_cb), NULL); g_object_connect( G_OBJECT(vb.gui.webview), + "signal::context-menu", G_CALLBACK(context_menu_cb), NULL, "signal::notify::progress", G_CALLBACK(webview_progress_cb), NULL, "signal::notify::load-status", G_CALLBACK(webview_load_status_cb), NULL, "signal::button-release-event", G_CALLBACK(button_relase_cb), NULL, @@ -873,6 +899,10 @@ static void hover_link_cb(WebKitWebView *webview, const char *title, const char { char *message; if (link) { + /* save the uri to have this if the user want's to copy the link + * location via context menu */ + OVERWRITE_STRING(vb.state.linkhover, link); + message = g_strconcat("Link: ", link, NULL); gtk_label_set_text(GTK_LABEL(vb.gui.statusbar.left), message); g_free(message); diff --git a/src/main.h b/src/main.h index b7903e8..2ae0797 100644 --- a/src/main.h +++ b/src/main.h @@ -284,6 +284,7 @@ typedef struct { #define PROMPT_SIZE 4 char prompt[PROMPT_SIZE]; /* current prompt ':', 'g;t', '/' including nul */ gdouble marks[VB_MARK_SIZE]; /* holds marks set to page with 'm{markchar}' */ + char *linkhover; /* the uri of the curret hovered link */ } State; typedef struct { -- 2.20.1