[gtk/clipboard-test: 1/2] Add a clipboard test client




commit 977031548d3a8a1d9dd8e58dfd41d3b7655c951b
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 | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 191 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..92a3a6c717
--- /dev/null
+++ b/tests/testclipboard.c
@@ -0,0 +1,190 @@
+#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 (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)
+    {
+      GFile *file = g_value_get_object (value);
+      char *path = g_file_get_path (file);
+      g_print ("%s\n", path);
+      g_free (path);
+    }
+  else
+    {
+      g_print ("ERROR: %s\n", error->message);
+      g_clear_error (&error);
+    }
+
+  exit (0);
+}
+
+static void
+got_color (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)
+    {
+      GdkRGBA *color = g_value_get_boxed (value);
+      char *s = gdk_rgba_to_string (color);
+      g_print ("%s\n", s);
+      g_free (s);
+    }
+  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, "info") == 0)
+    {
+      GdkContentFormats *formats;
+      char *s;
+
+      formats = gdk_clipboard_get_formats (clipboard);
+      s = gdk_content_formats_to_string (formats);
+      g_print ("%s\n", s);
+      g_free (s);
+    }
+  else if (strcmp (action, "set") == 0)
+    {
+      GdkContentFormats *formats;
+      char *s;
+
+      if (strcmp (type, "string") == 0)
+        gdk_clipboard_set_text (clipboard, value);
+      else if (strcmp (type, "file") == 0)
+        {
+          GFile *file;
+
+          file = g_file_new_for_commandline_arg (value);
+          gdk_clipboard_set (clipboard, G_TYPE_FILE, file);
+          g_object_unref (file);
+        }
+      else if (strcmp (type, "color") == 0)
+        {
+          GdkRGBA color;
+
+          gdk_rgba_parse (&color, value);
+          gdk_clipboard_set (clipboard, GDK_TYPE_RGBA, &color);
+        }
+      else
+        g_error ("can't set %s", type);
+
+      formats = gdk_clipboard_get_formats (clipboard);
+      s = gdk_content_formats_to_string (formats);
+      g_print ("%s\n", s);
+      g_free (s);
+    }
+  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, G_TYPE_FILE, 0, NULL, got_file, NULL);
+      else if (strcmp (type, "color") == 0)
+        gdk_clipboard_read_value_async (clipboard, GDK_TYPE_RGBA, 0, NULL, got_color, NULL);
+      else
+        g_error ("can't get %s", type);
+    }
+  else
+    g_error ("can only set, get or info");
+}
+
+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, "info") == 0)
+    {
+    }
+  else 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]