2004-07-29 15:40:35 +00:00
|
|
|
/* Clipboard
|
|
|
|
*
|
|
|
|
* GtkClipboard is used for clipboard handling. This demo shows how to
|
2008-08-11 19:12:58 +00:00
|
|
|
* copy and paste text to and from the clipboard.
|
2005-05-25 15:22:43 +00:00
|
|
|
*
|
2008-08-11 19:12:58 +00:00
|
|
|
* It also shows how to transfer images via the clipboard or via
|
2005-05-25 15:22:43 +00:00
|
|
|
* drag-and-drop, and how to make clipboard contents persist after
|
|
|
|
* the application exits. Clipboard persistence requires a clipboard
|
|
|
|
* manager to run.
|
2004-07-29 15:40:35 +00:00
|
|
|
*/
|
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
#include <glib/gi18n.h>
|
2004-07-29 15:40:35 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
copy_button_clicked (GtkWidget *button,
|
2005-05-25 15:22:43 +00:00
|
|
|
gpointer user_data)
|
2004-07-29 15:40:35 +00:00
|
|
|
{
|
|
|
|
GtkWidget *entry;
|
|
|
|
GtkClipboard *clipboard;
|
|
|
|
|
|
|
|
entry = GTK_WIDGET (user_data);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
/* Get the clipboard object */
|
|
|
|
clipboard = gtk_widget_get_clipboard (entry,
|
2005-05-25 15:22:43 +00:00
|
|
|
GDK_SELECTION_CLIPBOARD);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
/* Set clipboard text */
|
|
|
|
gtk_clipboard_set_text (clipboard, gtk_entry_get_text (GTK_ENTRY (entry)), -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
paste_received (GtkClipboard *clipboard,
|
2005-05-25 15:22:43 +00:00
|
|
|
const gchar *text,
|
|
|
|
gpointer user_data)
|
2004-07-29 15:40:35 +00:00
|
|
|
{
|
|
|
|
GtkWidget *entry;
|
|
|
|
|
|
|
|
entry = GTK_WIDGET (user_data);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
/* Set the entry text */
|
2005-10-27 04:31:33 +00:00
|
|
|
if(text)
|
|
|
|
gtk_entry_set_text (GTK_ENTRY (entry), text);
|
2004-07-29 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
paste_button_clicked (GtkWidget *button,
|
2005-05-25 15:22:43 +00:00
|
|
|
gpointer user_data)
|
2004-07-29 15:40:35 +00:00
|
|
|
{
|
|
|
|
GtkWidget *entry;
|
|
|
|
GtkClipboard *clipboard;
|
|
|
|
|
|
|
|
entry = GTK_WIDGET (user_data);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
/* Get the clipboard object */
|
|
|
|
clipboard = gtk_widget_get_clipboard (entry,
|
2005-05-25 15:22:43 +00:00
|
|
|
GDK_SELECTION_CLIPBOARD);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
/* Request the contents of the clipboard, contents_received will be
|
|
|
|
called when we do get the contents.
|
|
|
|
*/
|
|
|
|
gtk_clipboard_request_text (clipboard,
|
2005-05-25 15:22:43 +00:00
|
|
|
paste_received, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GdkPixbuf *
|
|
|
|
get_image_pixbuf (GtkImage *image)
|
|
|
|
{
|
2013-06-26 13:50:05 +00:00
|
|
|
const gchar *icon_name;
|
2005-05-25 15:22:43 +00:00
|
|
|
GtkIconSize size;
|
2013-06-21 17:46:54 +00:00
|
|
|
GtkIconTheme *icon_theme;
|
|
|
|
int width;
|
2005-05-25 15:22:43 +00:00
|
|
|
|
|
|
|
switch (gtk_image_get_storage_type (image))
|
|
|
|
{
|
|
|
|
case GTK_IMAGE_PIXBUF:
|
|
|
|
return g_object_ref (gtk_image_get_pixbuf (image));
|
2013-06-21 17:46:54 +00:00
|
|
|
case GTK_IMAGE_ICON_NAME:
|
|
|
|
gtk_image_get_icon_name (image, &icon_name, &size);
|
2013-06-26 13:50:05 +00:00
|
|
|
icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (image)));
|
2013-06-21 17:46:54 +00:00
|
|
|
gtk_icon_size_lookup (size, &width, NULL);
|
|
|
|
return gtk_icon_theme_load_icon (icon_theme,
|
|
|
|
icon_name,
|
|
|
|
width,
|
|
|
|
GTK_ICON_LOOKUP_GENERIC_FALLBACK,
|
|
|
|
NULL);
|
2005-05-25 15:22:43 +00:00
|
|
|
default:
|
2008-08-11 19:12:58 +00:00
|
|
|
g_warning ("Image storage type %d not handled",
|
2005-05-25 15:22:43 +00:00
|
|
|
gtk_image_get_storage_type (image));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
drag_begin (GtkWidget *widget,
|
|
|
|
GdkDragContext *context,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
|
|
|
pixbuf = get_image_pixbuf (GTK_IMAGE (data));
|
|
|
|
gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
}
|
|
|
|
|
2008-08-11 19:12:58 +00:00
|
|
|
void
|
2015-06-28 13:23:28 +00:00
|
|
|
drag_data_get (GtkWidget *widget,
|
|
|
|
GdkDragContext *context,
|
|
|
|
GtkSelectionData *selection_data,
|
|
|
|
guint info,
|
|
|
|
guint time,
|
|
|
|
gpointer data)
|
2005-05-25 15:22:43 +00:00
|
|
|
{
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
|
|
|
pixbuf = get_image_pixbuf (GTK_IMAGE (data));
|
|
|
|
gtk_selection_data_set_pixbuf (selection_data, pixbuf);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
drag_data_received (GtkWidget *widget,
|
|
|
|
GdkDragContext *context,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
GtkSelectionData *selection_data,
|
|
|
|
guint info,
|
|
|
|
guint32 time,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
2010-12-15 23:57:13 +00:00
|
|
|
if (gtk_selection_data_get_length (selection_data) > 0)
|
2005-05-25 15:22:43 +00:00
|
|
|
{
|
|
|
|
pixbuf = gtk_selection_data_get_pixbuf (selection_data);
|
|
|
|
gtk_image_set_from_pixbuf (GTK_IMAGE (data), pixbuf);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
copy_image (GtkMenuItem *item,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkClipboard *clipboard;
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
|
|
|
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
|
|
|
|
pixbuf = get_image_pixbuf (GTK_IMAGE (data));
|
|
|
|
|
|
|
|
gtk_clipboard_set_image (clipboard, pixbuf);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
paste_image (GtkMenuItem *item,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkClipboard *clipboard;
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
|
|
|
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
|
|
|
|
pixbuf = gtk_clipboard_wait_for_image (clipboard);
|
|
|
|
|
|
|
|
if (pixbuf)
|
|
|
|
{
|
|
|
|
gtk_image_set_from_pixbuf (GTK_IMAGE (data), pixbuf);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
button_press (GtkWidget *widget,
|
|
|
|
GdkEventButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *menu;
|
|
|
|
GtkWidget *item;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2012-01-25 18:09:54 +00:00
|
|
|
if (button->button != GDK_BUTTON_SECONDARY)
|
2005-05-25 15:22:43 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
menu = gtk_menu_new ();
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
item = gtk_menu_item_new_with_mnemonic (_("_Copy"));
|
2005-05-25 15:22:43 +00:00
|
|
|
g_signal_connect (item, "activate", G_CALLBACK (copy_image), data);
|
|
|
|
gtk_widget_show (item);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
|
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
item = gtk_menu_item_new_with_mnemonic (_("_Paste"));
|
2005-05-25 15:22:43 +00:00
|
|
|
g_signal_connect (item, "activate", G_CALLBACK (paste_image), data);
|
|
|
|
gtk_widget_show (item);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
|
|
|
|
|
2016-07-12 18:08:36 +00:00
|
|
|
gtk_menu_popup_at_pointer (GTK_MENU (menu), (GdkEvent *) button);
|
2005-05-25 15:22:43 +00:00
|
|
|
return TRUE;
|
2004-07-29 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_clipboard (GtkWidget *do_widget)
|
|
|
|
{
|
2015-06-28 13:23:28 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *vbox, *hbox;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *entry, *button;
|
2005-05-25 15:22:43 +00:00
|
|
|
GtkWidget *ebox, *image;
|
|
|
|
GtkClipboard *clipboard;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2009-08-31 05:08:31 +00:00
|
|
|
gtk_window_set_screen (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_screen (do_widget));
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Clipboard");
|
2009-08-31 05:08:31 +00:00
|
|
|
|
2005-05-25 15:22:43 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
|
|
|
G_CALLBACK (gtk_widget_destroyed), &window);
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2004-07-29 15:40:35 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
|
|
|
|
label = gtk_label_new ("\"Copy\" will copy the text\nin the entry to the clipboard");
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
|
2004-07-29 15:40:35 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
/* Create the first entry */
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
/* Create the button */
|
2013-06-21 17:46:54 +00:00
|
|
|
button = gtk_button_new_with_mnemonic (_("_Copy"));
|
2004-07-29 15:40:35 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
|
|
|
|
g_signal_connect (button, "clicked",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (copy_button_clicked), entry);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("\"Paste\" will paste the text from the clipboard to the entry");
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
|
2004-07-29 15:40:35 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
/* Create the second entry */
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
/* Create the button */
|
2013-06-21 17:46:54 +00:00
|
|
|
button = gtk_button_new_with_mnemonic (_("_Paste"));
|
2004-07-29 15:40:35 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
|
|
|
|
g_signal_connect (button, "clicked",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (paste_button_clicked), entry);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2005-05-25 15:22:43 +00:00
|
|
|
label = gtk_label_new ("Images can be transferred via the clipboard, too");
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
|
2005-05-25 15:22:43 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
/* Create the first image */
|
2013-06-21 17:46:54 +00:00
|
|
|
image = gtk_image_new_from_icon_name ("dialog-warning",
|
|
|
|
GTK_ICON_SIZE_BUTTON);
|
2005-05-25 15:22:43 +00:00
|
|
|
ebox = gtk_event_box_new ();
|
|
|
|
gtk_container_add (GTK_CONTAINER (ebox), image);
|
|
|
|
gtk_container_add (GTK_CONTAINER (hbox), ebox);
|
|
|
|
|
|
|
|
/* make ebox a drag source */
|
|
|
|
gtk_drag_source_set (ebox, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
|
|
|
|
gtk_drag_source_add_image_targets (ebox);
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (ebox, "drag-begin",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (drag_begin), image);
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (ebox, "drag-data-get",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (drag_data_get), image);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2005-05-25 15:22:43 +00:00
|
|
|
/* accept drops on ebox */
|
2008-08-11 19:12:58 +00:00
|
|
|
gtk_drag_dest_set (ebox, GTK_DEST_DEFAULT_ALL,
|
2005-05-25 15:22:43 +00:00
|
|
|
NULL, 0, GDK_ACTION_COPY);
|
|
|
|
gtk_drag_dest_add_image_targets (ebox);
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (ebox, "drag-data-received",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (drag_data_received), image);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2005-05-25 15:22:43 +00:00
|
|
|
/* context menu on ebox */
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (ebox, "button-press-event",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (button_press), image);
|
|
|
|
|
|
|
|
/* Create the second image */
|
2013-06-21 17:46:54 +00:00
|
|
|
image = gtk_image_new_from_icon_name ("process-stop",
|
|
|
|
GTK_ICON_SIZE_BUTTON);
|
2005-05-25 15:22:43 +00:00
|
|
|
ebox = gtk_event_box_new ();
|
|
|
|
gtk_container_add (GTK_CONTAINER (ebox), image);
|
|
|
|
gtk_container_add (GTK_CONTAINER (hbox), ebox);
|
|
|
|
|
|
|
|
/* make ebox a drag source */
|
|
|
|
gtk_drag_source_set (ebox, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
|
|
|
|
gtk_drag_source_add_image_targets (ebox);
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (ebox, "drag-begin",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (drag_begin), image);
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (ebox, "drag-data-get",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (drag_data_get), image);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2005-05-25 15:22:43 +00:00
|
|
|
/* accept drops on ebox */
|
2008-08-11 19:12:58 +00:00
|
|
|
gtk_drag_dest_set (ebox, GTK_DEST_DEFAULT_ALL,
|
2005-05-25 15:22:43 +00:00
|
|
|
NULL, 0, GDK_ACTION_COPY);
|
|
|
|
gtk_drag_dest_add_image_targets (ebox);
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (ebox, "drag-data-received",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (drag_data_received), image);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2005-05-25 15:22:43 +00:00
|
|
|
/* context menu on ebox */
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (ebox, "button-press-event",
|
2005-05-25 15:22:43 +00:00
|
|
|
G_CALLBACK (button_press), image);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2005-05-25 15:22:43 +00:00
|
|
|
/* tell the clipboard manager to make the data persistent */
|
|
|
|
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
|
|
|
|
gtk_clipboard_set_can_store (clipboard, NULL, 0);
|
2004-07-29 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2004-07-29 15:40:35 +00:00
|
|
|
gtk_widget_show_all (window);
|
|
|
|
else
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_widget_destroy (window);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|