From dc50e0637ffa8bc7a369d00a5d8e7656d03c3781 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Tue, 5 Dec 2017 05:16:49 +0100 Subject: [PATCH] clipboard: Add gdk_clipboard_set() This API allows specifying a GType and va_args of a value of that type to set the clipboard contents. This massively simplifies setting weird object types into the clipboard. 2 example patches included in this patch are the GtkTextBuffer and the file list in the file chooser. Using gobject-introspection, this should work without specifying the type, so that you can literlally say clipboard.set ("Hello World") or clipboard.set (pixbuf) which is why I've also marked all other setters as (skip). They just exist in C as wrappers for type safety reasons. --- docs/reference/gdk/gdk4-sections.txt | 3 + gdk/gdkclipboard.c | 109 ++++++--------------------- gdk/gdkclipboard.h | 11 --- gtk/gtkfilechooserwidget.c | 13 +--- gtk/gtktextbuffer.c | 13 +--- 5 files changed, 29 insertions(+), 120 deletions(-) diff --git a/docs/reference/gdk/gdk4-sections.txt b/docs/reference/gdk/gdk4-sections.txt index dccf648e35..acb8d2abb8 100644 --- a/docs/reference/gdk/gdk4-sections.txt +++ b/docs/reference/gdk/gdk4-sections.txt @@ -1304,6 +1304,9 @@ gdk_clipboard_read_texture_finish gdk_clipboard_read_text_async gdk_clipboard_read_text_finish gdk_clipboard_set_content +gdk_clipboard_set +gdk_clipboard_set_valist +gdk_clipboard_set_value gdk_clipboard_set_text gdk_clipboard_set_texture diff --git a/gdk/gdkclipboard.c b/gdk/gdkclipboard.c index befbb08aef..2832fcad50 100644 --- a/gdk/gdkclipboard.c +++ b/gdk/gdkclipboard.c @@ -30,8 +30,6 @@ #include "gdkpipeiostreamprivate.h" #include "gdktexture.h" -#include - /** * SECTION:gdkclipboard * @Short_description: Share data between applications for Copy-and-Paste @@ -1235,88 +1233,7 @@ gdk_clipboard_set_content (GdkClipboard *clipboard, } /** - * gdk_clipboard_set: - * @clipboard: a #GdkClipboard - * @type: type of value to set - * @...: value contents conforming to @type - * - * Sets the clipboard to contain the value collected from the given - * varargs. - **/ -void -gdk_clipboard_set (GdkClipboard *clipboard, - GType type, - ...) -{ - va_list args; - - g_return_if_fail (GDK_IS_CLIPBOARD (clipboard)); - - va_start (args, type); - gdk_clipboard_set_valist (clipboard, type, args); - va_end (args); -} - -/** - * gdk_clipboard_set_valist: (skip) - * @clipboard: a #GdkClipboard - * @type: type of value to set - * @args: varargs containing the value of @type - * - * Sets the clipboard to contain the value collected from the given - * @args. - **/ -void -gdk_clipboard_set_valist (GdkClipboard *clipboard, - GType type, - va_list args) -{ - GValue value = G_VALUE_INIT; - char *error; - - g_return_if_fail (GDK_IS_CLIPBOARD (clipboard)); - - G_VALUE_COLLECT_INIT (&value, type, - args, G_VALUE_NOCOPY_CONTENTS, - &error); - if (error) - { - g_warning ("%s: %s", G_STRLOC, error); - g_free (error); - /* we purposely leak the value here, it might not be - * in a sane state if an error condition occoured - */ - return; - } - - gdk_clipboard_set_value (clipboard, &value); - g_value_unset (&value); -} - -/** - * gdk_clipboard_set_value: (rename-to gdk_clipboard_set) - * @clipboard: a #GdkClipboard - * @value: a #GValue to set - * - * Sets the @clipboard to contain the given @value. - **/ -void -gdk_clipboard_set_value (GdkClipboard *clipboard, - const GValue *value) -{ - GdkContentProvider *provider; - - g_return_if_fail (GDK_IS_CLIPBOARD (clipboard)); - g_return_if_fail (G_IS_VALUE (value)); - - provider = gdk_content_provider_new_for_value (value); - - gdk_clipboard_set_content (clipboard, provider); - g_object_unref (provider); -} - -/** - * gdk_clipboard_set_text: (skip) + * gdk_clipboard_set_text: * @clipboard: a #GdkClipboard * @text: Text to put into the clipboard * @@ -1326,13 +1243,22 @@ void gdk_clipboard_set_text (GdkClipboard *clipboard, const char *text) { + GdkContentProvider *provider; + GValue value = G_VALUE_INIT; + g_return_if_fail (GDK_IS_CLIPBOARD (clipboard)); - gdk_clipboard_set (clipboard, G_TYPE_STRING, text); + g_value_init (&value, G_TYPE_STRING); + g_value_set_string (&value, text); + provider = gdk_content_provider_new_for_value (&value); + g_value_unset (&value); + + gdk_clipboard_set_content (clipboard, provider); + g_object_unref (provider); } /** - * gdk_clipboard_set_texture: (skip) + * gdk_clipboard_set_texture: * @clipboard: a #GdkClipboard * @texture: a #GdkTexture to put into the clipboard * @@ -1342,9 +1268,18 @@ void gdk_clipboard_set_texture (GdkClipboard *clipboard, GdkTexture *texture) { + GdkContentProvider *provider; + GValue value = G_VALUE_INIT; + g_return_if_fail (GDK_IS_CLIPBOARD (clipboard)); g_return_if_fail (GDK_IS_TEXTURE (texture)); - gdk_clipboard_set (clipboard, GDK_TYPE_TEXTURE, texture); + g_value_init (&value, GDK_TYPE_TEXTURE); + g_value_set_object (&value, texture); + provider = gdk_content_provider_new_for_value (&value); + g_value_unset (&value); + + gdk_clipboard_set_content (clipboard, provider); + g_object_unref (provider); } diff --git a/gdk/gdkclipboard.h b/gdk/gdkclipboard.h index 4f338b323b..02b13b01c8 100644 --- a/gdk/gdkclipboard.h +++ b/gdk/gdkclipboard.h @@ -103,17 +103,6 @@ GDK_AVAILABLE_IN_3_94 gboolean gdk_clipboard_set_content (GdkClipboard *clipboard, GdkContentProvider *provider); GDK_AVAILABLE_IN_3_94 -void gdk_clipboard_set (GdkClipboard *clipboard, - GType type, - ...); -GDK_AVAILABLE_IN_3_94 -void gdk_clipboard_set_valist (GdkClipboard *clipboard, - GType type, - va_list args); -GDK_AVAILABLE_IN_3_94 -void gdk_clipboard_set_value (GdkClipboard *clipboard, - const GValue *value); -GDK_AVAILABLE_IN_3_94 void gdk_clipboard_set_text (GdkClipboard *clipboard, const char *text); GDK_AVAILABLE_IN_3_94 diff --git a/gtk/gtkfilechooserwidget.c b/gtk/gtkfilechooserwidget.c index 1d99534d35..dd75977c96 100644 --- a/gtk/gtkfilechooserwidget.c +++ b/gtk/gtkfilechooserwidget.c @@ -1668,19 +1668,10 @@ copy_file_location_cb (GSimpleAction *action, if (selected_files) { GdkClipboard *clipboard; - GdkContentProvider *provider; - GValue value = G_VALUE_INIT; clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl)); - - g_value_init (&value, GDK_TYPE_FILE_LIST); - g_value_take_boxed (&value, selected_files); - - provider = gdk_content_provider_new_for_value (&value); - g_value_unset (&value); - - gdk_clipboard_set_content (clipboard, provider); - g_object_unref (provider); + gdk_clipboard_set (clipboard, GDK_TYPE_FILE_LIST, selected_files); + g_slist_free_full (selected_files, g_object_unref); } } diff --git a/gtk/gtktextbuffer.c b/gtk/gtktextbuffer.c index b7d18056a9..1dba4c4e00 100644 --- a/gtk/gtktextbuffer.c +++ b/gtk/gtktextbuffer.c @@ -3927,19 +3927,10 @@ cut_or_copy (GtkTextBuffer *buffer, if (!gtk_text_iter_equal (&start, &end)) { GtkTextBuffer *contents; - GdkContentProvider *provider; - GValue value = G_VALUE_INIT; contents = create_clipboard_contents_buffer (buffer, &start, &end); - - g_value_init (&value, GTK_TYPE_TEXT_BUFFER); - g_value_take_object (&value, contents); - - provider = gdk_content_provider_new_for_value (&value); - g_value_unset (&value); - - gdk_clipboard_set_content (clipboard, provider); - g_object_unref (provider); + gdk_clipboard_set (clipboard, GTK_TYPE_TEXT_BUFFER, contents); + g_object_unref (contents); if (delete_region_after) {