Commit 2ad2b9e0 authored by Sebastian Ramacher's avatar Sebastian Ramacher

Use signals for selected images and text

The page widget shouldn't have to care what should be done with selected images
and text.
Signed-off-by: Sebastian Ramacher's avatarSebastian Ramacher <sebastian+dev@ramacher.at>
parent d48f1e02
......@@ -541,3 +541,42 @@ cb_unknown_command(girara_session_t* session, const char* input)
return true;
}
void
cb_page_widget_text_selected(ZathuraPage* page, const char* text, void* data)
{
g_return_if_fail(page != NULL);
g_return_if_fail(text != NULL);
g_return_if_fail(data != NULL);
zathura_t* zathura = data;
GdkAtom* selection = get_selection(zathura);
/* copy to clipboard */
if (selection != NULL) {
gtk_clipboard_set_text(gtk_clipboard_get(*selection), text, -1);
char* stripped_text = g_strdelimit(g_strdup(text), "\n\t\r\n", ' ');
girara_notify(zathura->ui.session, GIRARA_INFO, _("Copied selected text to clipboard: %s"), stripped_text);
g_free(stripped_text);
}
g_free(selection);
}
void
cb_page_widget_image_selected(ZathuraPage* page, GdkPixbuf* pixbuf, void* data)
{
g_return_if_fail(page != NULL);
g_return_if_fail(pixbuf != NULL);
g_return_if_fail(data != NULL);
zathura_t* zathura = data;
GdkAtom* selection = get_selection(zathura);
if (selection != NULL) {
gtk_clipboard_set_image(gtk_clipboard_get(*selection), pixbuf);
}
g_free(selection);
}
......@@ -202,4 +202,17 @@ void cb_setting_recolor_keep_hue_change(girara_session_t* session, const char* n
*/
bool cb_unknown_command(girara_session_t* session, const char* input);
/**
* Emitted when text has been selected in the page widget
*
* @param widget page view widget
* @param text selected text
* @param data user data
*/
void cb_page_widget_text_selected(ZathuraPage* page, const char* text,
void* data);
void cb_page_widget_image_selected(ZathuraPage* page, GdkPixbuf* pixbuf,
void* data);
#endif // CALLBACKS_H
......@@ -90,6 +90,14 @@ enum properties_e {
PROP_LAST_VIEW,
};
enum {
TEXT_SELECTED,
IMAGE_SELECTED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static void
zathura_page_widget_class_init(ZathuraPageClass* class)
{
......@@ -135,6 +143,29 @@ zathura_page_widget_class_init(ZathuraPageClass* class)
g_param_spec_boolean("draw-search-results", "draw-search-results", "Set to true if search results should be drawn", FALSE, G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property(object_class, PROP_LAST_VIEW,
g_param_spec_int64("last-view", "last-view", "Last time the page has been viewed", -1, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
/* add signals */
signals[TEXT_SELECTED] = g_signal_new("text-selected",
ZATHURA_TYPE_PAGE,
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
1,
G_TYPE_STRING);
signals[IMAGE_SELECTED] = g_signal_new("image-selected",
ZATHURA_TYPE_PAGE,
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
1,
G_TYPE_OBJECT);
}
static void
......@@ -669,20 +700,8 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b
char* text = zathura_page_get_text(priv->page, tmp, NULL);
if (text != NULL) {
if (strlen(text) > 0) {
GdkAtom* selection = get_selection(priv->zathura);
/* copy to clipboard */
if (selection != NULL) {
gtk_clipboard_set_text(gtk_clipboard_get(*selection), text, -1);
}
if (priv->page != NULL && document != NULL && priv->zathura != NULL) {
char* stripped_text = g_strdelimit(g_strdup(text), "\n\t\r\n", ' ');
girara_notify(priv->zathura->ui.session, GIRARA_INFO, _("Copied selected text to clipboard: %s"), stripped_text);
g_free(stripped_text);
}
g_free(selection);
/* emit text-selected signal */
g_signal_emit(ZATHURA_PAGE(widget), signals[TEXT_SELECTED], 0, text);
}
g_free(text);
......@@ -837,14 +856,9 @@ cb_menu_image_copy(GtkMenuItem* item, ZathuraPage* page)
GdkPixbuf* pixbuf = gdk_pixbuf_get_from_drawable(NULL, pixmap, NULL, 0, 0, 0,
0, width, height);
GdkAtom* selection = get_selection(priv->zathura);
if (selection != NULL) {
gtk_clipboard_set_image(gtk_clipboard_get(*selection), pixbuf);
gtk_clipboard_set_image(gtk_clipboard_get(*selection), pixbuf);
}
g_signal_emit(page, signals[IMAGE_SELECTED], 0, pixbuf);
g_object_unref(pixbuf);
g_free(selection);
/* reset */
priv->images.current = NULL;
#endif
......
......@@ -4,6 +4,7 @@
#define PAGE_WIDGET_H
#include <gtk/gtk.h>
#include "types.h"
#include "document.h"
/**
......@@ -14,9 +15,6 @@
* Before the properties contain the correct values, 'draw-links' has to be set
* to TRUE at least one time.
* */
typedef struct zathura_page_widget_s ZathuraPage;
typedef struct zathura_page_widget_class_s ZathuraPageClass;
struct zathura_page_widget_s
{
GtkDrawingArea parent;
......
......@@ -15,6 +15,11 @@ typedef struct zathura_document_s zathura_document_t;
* Page
*/
typedef struct zathura_page_s zathura_page_t;
/**
* Page widget
*/
typedef struct zathura_page_widget_s ZathuraPage;
typedef struct zathura_page_widget_class_s ZathuraPageClass;
/**
* Zathura
*/
......
......@@ -736,6 +736,11 @@ document_open(zathura_t* zathura, const char* path, const char* password,
page_calc_height_width(page, &page_height, &page_width, true);
gtk_widget_set_size_request(page_widget, page_width, page_height);
g_signal_connect(G_OBJECT(page_widget), "text-selected",
G_CALLBACK(cb_page_widget_text_selected), zathura);
g_signal_connect(G_OBJECT(page_widget), "image-selected",
G_CALLBACK(cb_page_widget_text_selected), zathura);
}
/* view mode */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment