tests: Add a new test app for GdkClipboard

This commit is contained in:
Benjamin Otte 2017-11-20 02:07:31 +01:00
parent c91a38b013
commit 3506ae6085
2 changed files with 168 additions and 0 deletions

View File

@ -26,6 +26,7 @@ gtk_tests = [
['testcairo'],
['testcalendar'],
['testclipboard'],
['testclipboard2'],
['testcolorchooser'],
['testcolorchooser2'],
['testcombo'],

167
tests/testclipboard2.c Normal file
View File

@ -0,0 +1,167 @@
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtk/gtk.h>
static void
clipboard_changed_cb (GdkClipboard *clipboard,
GtkWidget *stack)
{
GtkWidget *child;
gtk_stack_set_visible_child_name (GTK_STACK (stack), "info");
child = gtk_stack_get_child_by_name (GTK_STACK (stack), "image");
gtk_image_clear (GTK_IMAGE (child));
}
static void
pixbuf_loaded_cb (GObject *stream,
GAsyncResult *res,
gpointer data)
{
GdkPixbuf *pixbuf;
GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_stream_finish (res, &error);
if (pixbuf == NULL)
{
g_print ("%s\n", error->message);
g_error_free (error);
return;
}
gtk_image_set_from_pixbuf (data, pixbuf);
g_object_unref (pixbuf);
}
static void
visible_child_changed_cb (GtkWidget *stack,
GParamSpec *pspec,
GdkClipboard *clipboard)
{
const char *visible_child = gtk_stack_get_visible_child_name (GTK_STACK (stack));
if (visible_child == NULL)
{
/* nothing to do here but avoiding crashes in g_str_equal() */
}
else if (g_str_equal (visible_child, "image"))
{
GtkWidget *image = gtk_stack_get_child_by_name (GTK_STACK (stack), "image");
GdkContentFormats *formats;
GInputStream *stream;
formats = gdk_clipboard_get_formats (clipboard);
if (gdk_content_formats_contain_mime_type (formats, "image/png"))
stream = gdk_clipboard_read (clipboard, "image/png");
else
stream = NULL;
if (stream)
{
gdk_pixbuf_new_from_stream_async (stream, NULL, pixbuf_loaded_cb, image);
g_object_unref (stream);
}
}
}
static void
clipboard_formats_change_cb (GdkClipboard *clipboard,
GParamSpec *pspec,
GtkWidget *label)
{
char *s;
s = gdk_content_formats_to_string (gdk_clipboard_get_formats (clipboard));
gtk_label_set_text (GTK_LABEL (label), s);
g_free (s);
}
static GtkWidget *
get_contents_widget (GdkClipboard *clipboard)
{
GtkWidget *stack, *child;
stack = gtk_stack_new ();
gtk_widget_set_hexpand (stack, TRUE);
gtk_widget_set_vexpand (stack, TRUE);
g_signal_connect (stack, "notify::visible-child", G_CALLBACK (visible_child_changed_cb), clipboard);
g_signal_connect (clipboard, "changed", G_CALLBACK (clipboard_changed_cb), stack);
child = gtk_label_new (NULL);
gtk_label_set_line_wrap (GTK_LABEL (child), TRUE);
g_signal_connect (clipboard, "notify::formats", G_CALLBACK (clipboard_formats_change_cb), child);
clipboard_formats_change_cb (clipboard, NULL, child);
gtk_stack_add_titled (GTK_STACK (stack), child, "info", "Info");
child = gtk_image_new ();
gtk_stack_add_titled (GTK_STACK (stack), child, "image", "Image");
return stack;
}
static GtkWidget *
get_clipboard_widget (GdkClipboard *clipboard,
const char *name)
{
GtkWidget *box, *stack, *switcher;
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (box), gtk_label_new (name));
switcher = gtk_stack_switcher_new ();
gtk_container_add (GTK_CONTAINER (box), switcher);
stack = get_contents_widget (clipboard);
gtk_container_add (GTK_CONTAINER (box), stack);
gtk_stack_switcher_set_stack (GTK_STACK_SWITCHER (switcher), GTK_STACK (stack));
return box;
}
static GtkWidget *
get_window_contents (GdkDisplay *display)
{
GtkWidget *box;
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
gtk_container_add (GTK_CONTAINER (box),
get_clipboard_widget (gdk_display_get_clipboard (display),
"Clipboard"));
gtk_container_add (GTK_CONTAINER (box),
get_clipboard_widget (gdk_display_get_primary_clipboard (display),
"Primary Clipboard"));
return box;
}
int
main (int argc, char **argv)
{
GtkWidget *window;
gtk_init ();
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_add (GTK_CONTAINER (window), get_window_contents (gtk_widget_get_display (window)));
gtk_widget_show (window);
gtk_main ();
return 0;
}