[recipes] Accept images from the clipboard
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes] Accept images from the clipboard
- Date: Mon, 30 Jan 2017 00:08:36 +0000 (UTC)
commit c2e16746ef6ac3c6f864c9b2d834f9328956c089
Author: Matthias Clasen <mclasen redhat com>
Date: Mon Jan 30 01:07:19 2017 +0100
Accept images from the clipboard
This makes Ctrl-V over the image viewer work to paste an
image from the clipboard, when editing a recipe.
src/gr-image-viewer.c | 80 ++++++++++++++++++++++++++++++++++++++----------
1 files changed, 63 insertions(+), 17 deletions(-)
---
diff --git a/src/gr-image-viewer.c b/src/gr-image-viewer.c
index 2de0157..ef77349 100644
--- a/src/gr-image-viewer.c
+++ b/src/gr-image-viewer.c
@@ -20,7 +20,11 @@
#include "config.h"
+#include <sys/stat.h>
+#include <sys/types.h>
+
#include <glib/gi18n.h>
+#include <gio/gunixoutputstream.h>
#include "gr-image-viewer.h"
#include "gr-images.h"
@@ -308,6 +312,58 @@ preview_selected (GrImageViewer *viewer)
}
}
+static void
+add_image (GrImageViewer *viewer,
+ GrRotatedImage *ri,
+ gboolean select)
+{
+ g_array_append_vals (viewer->images, ri, 1);
+ ri = &g_array_index (viewer->images, GrRotatedImage, viewer->images->len - 1);
+ ri->path = g_strdup (ri->path);
+
+ populate_preview (viewer);
+ if (select)
+ viewer->index = viewer->images->len - 1;
+ set_current_image (viewer);
+
+ g_object_notify (G_OBJECT (viewer), "images");
+}
+
+static void
+image_received (GtkClipboard *clipboard,
+ GdkPixbuf *pixbuf,
+ gpointer data)
+{
+ GrImageViewer *viewer = data;
+
+ if (pixbuf) {
+ GrRotatedImage ri;
+ g_autofree char *dir = NULL;
+ g_autofree char *path = NULL;
+ int fd;
+ g_autoptr(GOutputStream) stream = NULL;
+ g_autoptr(GError) error = NULL;
+
+ dir = g_build_filename (g_get_user_data_dir (), "recipes", NULL);
+ g_mkdir_with_parents (dir, S_IRWXU | S_IRWXG | S_IRWXO);
+
+ path = g_build_filename (dir, "clipboardXXXXXX.png", NULL);
+ fd = g_mkstemp (path);
+ stream = g_unix_output_stream_new (fd, TRUE);
+
+ if (!gdk_pixbuf_save_to_stream (pixbuf, stream, "png", NULL, &error, NULL)) {
+ g_message ("Saving clipboard contents failed: %s", error->message);
+ return;
+ }
+
+ ri.path = g_strdup (path);
+ ri.angle = 0;
+ ri.dark_text = FALSE;
+
+ add_image (viewer, &ri, TRUE);
+ }
+}
+
static gboolean
key_press_event (GtkWidget *widget,
GdkEvent *event,
@@ -331,6 +387,13 @@ key_press_event (GtkWidget *widget,
next_image (viewer);
return TRUE;
}
+ else if (key->keyval == GDK_KEY_v && (key->state & GDK_CONTROL_MASK) != 0) {
+ GtkClipboard *clipboard;
+
+ clipboard = gtk_widget_get_clipboard (GTK_WIDGET (viewer), GDK_SELECTION_CLIPBOARD);
+ gtk_clipboard_request_image (clipboard, image_received, viewer);
+ return TRUE;
+ }
return FALSE;
}
@@ -444,23 +507,6 @@ gr_image_viewer_class_init (GrImageViewerClass *klass)
gtk_widget_class_bind_template_callback (widget_class, preview_selected);
}
-static void
-add_image (GrImageViewer *viewer,
- GrRotatedImage *ri,
- gboolean select)
-{
- g_array_append_vals (viewer->images, ri, 1);
- ri = &g_array_index (viewer->images, GrRotatedImage, viewer->images->len - 1);
- ri->path = g_strdup (ri->path);
-
- populate_preview (viewer);
- if (select)
- viewer->index = viewer->images->len - 1;
- set_current_image (viewer);
-
- g_object_notify (G_OBJECT (viewer), "images");
-}
-
void
gr_image_viewer_set_images (GrImageViewer *viewer,
GArray *images)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]