2017-11-20 01:07:31 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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));
|
2017-11-22 13:19:02 +00:00
|
|
|
|
|
|
|
|
|
child = gtk_stack_get_child_by_name (GTK_STACK (stack), "text");
|
|
|
|
|
gtk_label_set_text (GTK_LABEL (child), "");
|
2017-11-20 01:07:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-11-22 09:44:30 +00:00
|
|
|
|
pixbuf_loaded_cb (GObject *clipboard,
|
2017-11-20 01:07:31 +00:00
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer data)
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
2017-11-22 12:21:25 +00:00
|
|
|
|
GdkPixbuf *pixbuf;
|
2017-11-20 01:07:31 +00:00
|
|
|
|
|
2017-11-22 12:21:25 +00:00
|
|
|
|
pixbuf = gdk_clipboard_read_pixbuf_finish (GDK_CLIPBOARD (clipboard), res, &error);
|
|
|
|
|
if (pixbuf == NULL)
|
2017-11-20 01:07:31 +00:00
|
|
|
|
{
|
|
|
|
|
g_print ("%s\n", error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 12:21:25 +00:00
|
|
|
|
gtk_image_set_from_pixbuf (data, pixbuf);
|
|
|
|
|
g_object_unref (pixbuf);
|
2017-11-22 08:25:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 13:19:02 +00:00
|
|
|
|
static void
|
|
|
|
|
text_loaded_cb (GObject *clipboard,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer data)
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
|
|
text = gdk_clipboard_read_text_finish (GDK_CLIPBOARD (clipboard), res, &error);
|
|
|
|
|
if (text == NULL)
|
|
|
|
|
{
|
|
|
|
|
g_print ("%s\n", error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gtk_label_set_text (data, text);
|
|
|
|
|
g_free (text);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 01:07:31 +00:00
|
|
|
|
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");
|
|
|
|
|
|
2017-11-22 12:21:25 +00:00
|
|
|
|
gdk_clipboard_read_pixbuf_async (clipboard,
|
|
|
|
|
NULL,
|
|
|
|
|
pixbuf_loaded_cb,
|
|
|
|
|
image);
|
2017-11-20 01:07:31 +00:00
|
|
|
|
}
|
2017-11-22 13:19:02 +00:00
|
|
|
|
else if (g_str_equal (visible_child, "text"))
|
|
|
|
|
{
|
|
|
|
|
GtkWidget *label = gtk_stack_get_child_by_name (GTK_STACK (stack), "text");
|
|
|
|
|
|
|
|
|
|
gdk_clipboard_read_text_async (clipboard,
|
|
|
|
|
NULL,
|
|
|
|
|
text_loaded_cb,
|
|
|
|
|
label);
|
|
|
|
|
}
|
2017-11-20 01:07:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
2017-11-22 13:19:02 +00:00
|
|
|
|
child = gtk_label_new (NULL);
|
|
|
|
|
gtk_label_set_line_wrap (GTK_LABEL (child), TRUE);
|
|
|
|
|
gtk_stack_add_titled (GTK_STACK (stack), child, "text", "Text");
|
|
|
|
|
|
2017-11-20 01:07:31 +00:00
|
|
|
|
return stack;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 05:17:37 +00:00
|
|
|
|
static void
|
|
|
|
|
provider_button_clicked_cb (GtkWidget *button,
|
|
|
|
|
GdkClipboard *clipboard)
|
|
|
|
|
{
|
|
|
|
|
gdk_clipboard_set_content (clipboard,
|
|
|
|
|
g_object_get_data (G_OBJECT (button), "provider"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
add_provider_button (GtkWidget *box,
|
|
|
|
|
GdkContentProvider *provider,
|
|
|
|
|
GdkClipboard *clipboard,
|
|
|
|
|
const char *name)
|
|
|
|
|
{
|
|
|
|
|
GtkWidget *button;
|
|
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label (name);
|
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (provider_button_clicked_cb), clipboard);
|
|
|
|
|
g_object_set_data_full (G_OBJECT (button), "provider", provider, g_object_unref);
|
|
|
|
|
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), button);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
|
get_button_list (GdkClipboard *clipboard)
|
|
|
|
|
{
|
|
|
|
|
GtkWidget *box;
|
|
|
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
|
|
|
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
|
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), gtk_label_new ("Set Clipboard:"));
|
|
|
|
|
|
|
|
|
|
g_value_init (&value, GDK_TYPE_PIXBUF);
|
|
|
|
|
g_value_take_object (&value, gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
|
|
|
|
|
"utilities-terminal",
|
|
|
|
|
48, 0, NULL));
|
|
|
|
|
add_provider_button (box,
|
|
|
|
|
gdk_content_provider_new_for_value (&value),
|
|
|
|
|
clipboard,
|
2017-11-24 09:13:23 +00:00
|
|
|
|
"GdkPixbuf");
|
2017-11-24 05:17:37 +00:00
|
|
|
|
g_value_unset (&value);
|
|
|
|
|
|
|
|
|
|
g_value_init (&value, G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (&value, "Hello Clipboard ☺");
|
|
|
|
|
add_provider_button (box,
|
|
|
|
|
gdk_content_provider_new_for_value (&value),
|
|
|
|
|
clipboard,
|
2017-11-24 09:13:23 +00:00
|
|
|
|
"gchararry");
|
2017-11-24 05:17:37 +00:00
|
|
|
|
g_value_unset (&value);
|
|
|
|
|
|
2017-11-24 09:13:23 +00:00
|
|
|
|
add_provider_button (box,
|
|
|
|
|
gdk_content_provider_new_for_bytes ("text/plain;charset=utf-8",
|
|
|
|
|
g_bytes_new_static ("𝕳𝖊𝖑𝖑𝖔 𝖀𝖓𝖎𝖈𝖔𝖉𝖊",
|
|
|
|
|
strlen ("𝕳𝖊𝖑𝖑𝖔 𝖀𝖓𝖎𝖈𝖔𝖉𝖊") + 1)),
|
|
|
|
|
clipboard,
|
|
|
|
|
"text/plain");
|
|
|
|
|
|
2017-11-24 05:17:37 +00:00
|
|
|
|
return box;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 01:07:31 +00:00
|
|
|
|
static GtkWidget *
|
|
|
|
|
get_clipboard_widget (GdkClipboard *clipboard,
|
|
|
|
|
const char *name)
|
|
|
|
|
{
|
2017-11-24 05:17:37 +00:00
|
|
|
|
GtkWidget *vbox, *hbox, *stack, *switcher;
|
2017-11-20 01:07:31 +00:00
|
|
|
|
|
2017-11-24 05:17:37 +00:00
|
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (hbox), vbox);
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), gtk_label_new (name));
|
2017-11-20 01:07:31 +00:00
|
|
|
|
switcher = gtk_stack_switcher_new ();
|
2017-11-24 05:17:37 +00:00
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), switcher);
|
2017-11-20 01:07:31 +00:00
|
|
|
|
stack = get_contents_widget (clipboard);
|
2017-11-24 05:17:37 +00:00
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), stack);
|
2017-11-20 01:07:31 +00:00
|
|
|
|
gtk_stack_switcher_set_stack (GTK_STACK_SWITCHER (switcher), GTK_STACK (stack));
|
2017-11-24 05:17:37 +00:00
|
|
|
|
gtk_container_add (GTK_CONTAINER (hbox), get_button_list (clipboard));
|
2017-11-20 01:07:31 +00:00
|
|
|
|
|
2017-11-24 05:17:37 +00:00
|
|
|
|
return hbox;
|
2017-11-20 01:07:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|