From 2ad2b9e0c4bf1a32e099d6262d25b10fddb32c0b Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sat, 31 Aug 2013 13:12:41 +0200 Subject: [PATCH] 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 --- callbacks.c | 39 +++++++++++++++++++++++++++++++++++ callbacks.h | 13 ++++++++++++ page-widget.c | 56 ++++++++++++++++++++++++++++++++------------------- page-widget.h | 4 +--- types.h | 5 +++++ zathura.c | 5 +++++ 6 files changed, 98 insertions(+), 24 deletions(-) diff --git a/callbacks.c b/callbacks.c index 1c49df0..992ff23 100644 --- a/callbacks.c +++ b/callbacks.c @@ -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); +} diff --git a/callbacks.h b/callbacks.h index d879732..e1af85d 100644 --- a/callbacks.h +++ b/callbacks.h @@ -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 diff --git a/page-widget.c b/page-widget.c index d5ebd96..5f0cf26 100644 --- a/page-widget.c +++ b/page-widget.c @@ -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); + g_signal_emit(page, signals[IMAGE_SELECTED], 0, pixbuf); + g_object_unref(pixbuf); - if (selection != NULL) { - gtk_clipboard_set_image(gtk_clipboard_get(*selection), pixbuf); - gtk_clipboard_set_image(gtk_clipboard_get(*selection), pixbuf); - } - - g_free(selection); /* reset */ priv->images.current = NULL; #endif diff --git a/page-widget.h b/page-widget.h index b9a1c38..1a54708 100644 --- a/page-widget.h +++ b/page-widget.h @@ -4,6 +4,7 @@ #define PAGE_WIDGET_H #include +#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; diff --git a/types.h b/types.h index 50bacb6..e3d915e 100644 --- a/types.h +++ b/types.h @@ -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 */ diff --git a/zathura.c b/zathura.c index 182a99a..c647cc9 100644 --- a/zathura.c +++ b/zathura.c @@ -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 */