2004-07-29 15:40:35 +00:00
|
|
|
/* Clipboard
|
|
|
|
*
|
2017-12-02 13:39:37 +00:00
|
|
|
* GdkClipboard 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>
|
2020-04-26 01:16:02 +00:00
|
|
|
#include "demoimage.h"
|
2004-07-29 15:40:35 +00:00
|
|
|
|
2017-11-26 21:39:47 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
static void
|
2004-07-29 15:40:35 +00:00
|
|
|
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;
|
2017-11-26 21:39:47 +00:00
|
|
|
GdkClipboard *clipboard;
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
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 */
|
2017-11-26 21:39:47 +00:00
|
|
|
clipboard = gtk_widget_get_clipboard (entry);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
/* Set clipboard text */
|
2019-02-28 19:31:36 +00:00
|
|
|
gdk_clipboard_set_text (clipboard, gtk_editable_get_text (GTK_EDITABLE (entry)));
|
2004-07-29 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
static void
|
2017-11-26 21:39:47 +00:00
|
|
|
paste_received (GObject *source_object,
|
|
|
|
GAsyncResult *result,
|
2005-05-25 15:22:43 +00:00
|
|
|
gpointer user_data)
|
2004-07-29 15:40:35 +00:00
|
|
|
{
|
2017-11-26 21:39:47 +00:00
|
|
|
GdkClipboard *clipboard;
|
2004-07-29 15:40:35 +00:00
|
|
|
GtkWidget *entry;
|
2017-11-26 21:39:47 +00:00
|
|
|
char *text;
|
|
|
|
GError *error = NULL;
|
2004-07-29 15:40:35 +00:00
|
|
|
|
2017-11-26 21:39:47 +00:00
|
|
|
clipboard = GDK_CLIPBOARD (source_object);
|
2004-07-29 15:40:35 +00:00
|
|
|
entry = GTK_WIDGET (user_data);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2017-11-26 21:39:47 +00:00
|
|
|
/* Get the resulting text of the read operation */
|
|
|
|
text = gdk_clipboard_read_text_finish (clipboard, result, &error);
|
|
|
|
|
|
|
|
if (text)
|
|
|
|
{
|
|
|
|
/* Set the entry text */
|
2019-02-28 19:31:36 +00:00
|
|
|
gtk_editable_set_text (GTK_EDITABLE (entry), text);
|
2017-11-26 21:39:47 +00:00
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
/* Show an error about why pasting failed.
|
|
|
|
* Usually you probably want to ignore such failures,
|
|
|
|
* but for demonstration purposes, we show the error.
|
|
|
|
*/
|
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Could not paste text: %s",
|
|
|
|
error->message);
|
|
|
|
g_signal_connect (dialog, "response",
|
2020-05-09 14:26:22 +00:00
|
|
|
G_CALLBACK (gtk_window_destroy), NULL);
|
2017-11-26 21:39:47 +00:00
|
|
|
gtk_widget_show (dialog);
|
|
|
|
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
2004-07-29 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
static void
|
2004-07-29 15:40:35 +00:00
|
|
|
paste_button_clicked (GtkWidget *button,
|
2017-11-26 21:39:47 +00:00
|
|
|
gpointer user_data)
|
2004-07-29 15:40:35 +00:00
|
|
|
{
|
|
|
|
GtkWidget *entry;
|
2017-11-26 21:39:47 +00:00
|
|
|
GdkClipboard *clipboard;
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
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 */
|
2017-11-26 21:39:47 +00:00
|
|
|
clipboard = gtk_widget_get_clipboard (entry);
|
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.
|
|
|
|
*/
|
2017-11-26 21:39:47 +00:00
|
|
|
gdk_clipboard_read_text_async (clipboard, NULL, paste_received, entry);
|
2005-05-25 15:22:43 +00:00
|
|
|
}
|
|
|
|
|
2004-07-29 15:40:35 +00:00
|
|
|
GtkWidget *
|
|
|
|
do_clipboard (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *vbox, *hbox;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *entry, *button;
|
2017-08-02 12:59:26 +00:00
|
|
|
GtkWidget *image;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Clipboard");
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2005-05-25 15:22:43 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (vbox, 8);
|
|
|
|
gtk_widget_set_margin_end (vbox, 8);
|
|
|
|
gtk_widget_set_margin_top (vbox, 8);
|
|
|
|
gtk_widget_set_margin_bottom (vbox, 8);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), vbox);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("\"Copy\" will copy the text\nin the entry to the clipboard");
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), label);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (hbox, 8);
|
|
|
|
gtk_widget_set_margin_end (hbox, 8);
|
|
|
|
gtk_widget_set_margin_top (hbox, 8);
|
|
|
|
gtk_widget_set_margin_bottom (hbox, 8);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), hbox);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
/* Create the first entry */
|
|
|
|
entry = gtk_entry_new ();
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), entry);
|
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"));
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), button);
|
2004-07-29 15:40:35 +00:00
|
|
|
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");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), label);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (hbox, 8);
|
|
|
|
gtk_widget_set_margin_end (hbox, 8);
|
|
|
|
gtk_widget_set_margin_top (hbox, 8);
|
|
|
|
gtk_widget_set_margin_bottom (hbox, 8);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), hbox);
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
/* Create the second entry */
|
|
|
|
entry = gtk_entry_new ();
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), entry);
|
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"));
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), button);
|
2004-07-29 15:40:35 +00:00
|
|
|
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");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), label);
|
2005-05-25 15:22:43 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (hbox, 8);
|
|
|
|
gtk_widget_set_margin_end (hbox, 8);
|
|
|
|
gtk_widget_set_margin_top (hbox, 8);
|
|
|
|
gtk_widget_set_margin_bottom (hbox, 8);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), hbox);
|
2005-05-25 15:22:43 +00:00
|
|
|
|
|
|
|
/* Create the first image */
|
2020-04-26 01:16:02 +00:00
|
|
|
image = demo_image_new ("dialog-warning");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), image);
|
2017-08-02 12:59:26 +00:00
|
|
|
|
2005-05-25 15:22:43 +00:00
|
|
|
/* Create the second image */
|
2020-04-26 01:16:02 +00:00
|
|
|
image = demo_image_new ("process-stop");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), image);
|
2017-08-02 12:59:26 +00:00
|
|
|
|
2020-04-26 01:16:02 +00:00
|
|
|
/* Create the third image */
|
|
|
|
image = demo_image_new ("weather-clear");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), image);
|
2004-07-29 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2004-07-29 15:40:35 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2004-07-29 15:40:35 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|