[gtk/clipboard-test] Add a clipboard test client




commit 544817c795fb4e6779b849f05b9a40e6f14ab261
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Apr 26 17:02:50 2021 -0400

    Add a clipboard test client
    
    This tests the simplest cases of copying text
    or a file list.

 tests/meson.build     |   1 +
 tests/testclipboard.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+)
---
diff --git a/tests/meson.build b/tests/meson.build
index c0e5f836b0..894d6a2dac 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,5 +1,6 @@
 gtk_tests = [
   # testname, optional extra sources
+  ['testclipboard'],
   ['testpopup'],
   ['testupload'],
   ['testtransform'],
diff --git a/tests/testclipboard.c b/tests/testclipboard.c
new file mode 100644
index 0000000000..15c3e03be8
--- /dev/null
+++ b/tests/testclipboard.c
@@ -0,0 +1,136 @@
+#include <gtk/gtk.h>
+
+static void
+got_text_cb (GObject      *source,
+             GAsyncResult *result,
+             gpointer      data)
+{
+  GdkClipboard *clipboard = GDK_CLIPBOARD (source);
+  GError *error = NULL;
+  char *text;
+
+  text = gdk_clipboard_read_text_finish (clipboard, result, &error);
+  if (text)
+    {
+      g_print ("%s\n", text);
+      g_free (text);
+    }
+  else
+    {
+      g_print ("ERROR: %s\n", error->message);
+      g_clear_error (&error);
+    }
+
+  exit (0);
+}
+
+static void
+got_file_list (GObject      *source,
+               GAsyncResult *result,
+               gpointer      data)
+{
+  GdkClipboard *clipboard = GDK_CLIPBOARD (source);
+  GError *error = NULL;
+  const GValue *value;
+
+  value = gdk_clipboard_read_value_finish (clipboard, result, &error);
+  if (value)
+    {
+      GSList *l;
+
+      for (l = g_value_get_pointer (value); l; l = l->next)
+        {
+          GFile *file = l->data;
+          g_print ("%s\n", g_file_peek_path (file));
+        }
+    }
+  else
+    {
+      g_print ("ERROR: %s\n", error->message);
+      g_clear_error (&error);
+    }
+
+  exit (0);
+}
+
+static const char *action;
+static const char *type;
+static const char *value;
+
+static void
+do_it (GObject    *object,
+       GParamSpec *pspec)
+{
+  GdkClipboard *clipboard;
+
+  clipboard = gdk_display_get_clipboard (gdk_display_get_default ());
+
+  if (strcmp (action, "set") == 0)
+    {
+      if (strcmp (type, "string") == 0)
+        gdk_clipboard_set_text (clipboard, value);
+      else if (strcmp (type, "file") == 0)
+        {
+          GFile *file = g_file_new_for_uri (value);
+          g_print ("setting clipboard to a file\n");
+          gdk_clipboard_set (clipboard, G_TYPE_FILE, file);
+          g_object_unref (file);
+        }
+      else
+        g_error ("can only set strings");
+    }
+  else if (strcmp (action, "get") == 0)
+    {
+      if (strcmp (type, "string") == 0)
+        gdk_clipboard_read_text_async (clipboard, NULL, got_text_cb, NULL);
+      else if (strcmp (type, "file") == 0)
+        gdk_clipboard_read_value_async (clipboard, GDK_TYPE_FILE_LIST, 0, NULL, got_file_list, NULL);
+      else
+        g_error ("can only get strings");
+    }
+  else
+    g_error ("can only set or get");
+}
+
+int
+main (int argc, char *argv[])
+{
+  GtkWidget *window;
+  gboolean done = FALSE;
+
+  if (argc < 2)
+    g_error ("too few arguments");
+
+  action = argv[1];
+
+  if (strcmp (action, "set") == 0)
+    {
+      if (argc < 4)
+        g_error ("too few arguments for set");
+
+      type = argv[2];
+      value = argv[3];
+    }
+  else if (strcmp (action, "get") == 0)
+    {
+      if (argc < 3)
+        g_error ("too few arguments for get");
+
+      type = argv[2];
+    }
+  else
+    g_error ("can only set or get");
+
+  gtk_init ();
+
+  window = gtk_window_new ();
+
+  gtk_window_present (GTK_WINDOW (window));
+
+  g_signal_connect (window, "notify::is-active", G_CALLBACK (do_it), NULL);
+
+  while (!done)
+    g_main_context_iteration (NULL, TRUE);
+
+  return 0;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]