summaryrefslogtreecommitdiff
path: root/demos
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@gnome.org>2004-07-29 15:40:35 +0000
committerAnders Carlsson <andersca@src.gnome.org>2004-07-29 15:40:35 +0000
commit705a7066c3f9cfc20479c78fbf41164f46712a45 (patch)
tree629f292f58496178c3df944ced2c85edd936e7d1 /demos
parentdb87e3fa9095d72e4cffe617e62357bd11b6b4af (diff)
downloadgtk+-705a7066c3f9cfc20479c78fbf41164f46712a45.tar.gz
Add clipboard demo.
2004-07-29 Anders Carlsson <andersca@gnome.org> * demos/gtk-demo/Makefile.am: * demos/gtk-demo/clipboard.c: (copy_button_clicked), (paste_received), (paste_button_clicked), (do_clipboard): Add clipboard demo.
Diffstat (limited to 'demos')
-rw-r--r--demos/gtk-demo/Makefile.am1
-rw-r--r--demos/gtk-demo/clipboard.c124
2 files changed, 125 insertions, 0 deletions
diff --git a/demos/gtk-demo/Makefile.am b/demos/gtk-demo/Makefile.am
index 7557fb91bb..65a52ca9b8 100644
--- a/demos/gtk-demo/Makefile.am
+++ b/demos/gtk-demo/Makefile.am
@@ -8,6 +8,7 @@ demos = \
appwindow.c \
button_box.c \
changedisplay.c \
+ clipboard.c \
colorsel.c \
dialog.c \
drawingarea.c \
diff --git a/demos/gtk-demo/clipboard.c b/demos/gtk-demo/clipboard.c
new file mode 100644
index 0000000000..c4f04c7540
--- /dev/null
+++ b/demos/gtk-demo/clipboard.c
@@ -0,0 +1,124 @@
+/* Clipboard
+ *
+ * GtkClipboard is used for clipboard handling. This demo shows how to
+ * copy and paste text to and from the clipboard.
+ */
+
+#include <config.h>
+#include <gtk/gtk.h>
+#include <string.h>
+
+static GtkWidget *window = NULL;
+
+void
+copy_button_clicked (GtkWidget *button,
+ gpointer user_data)
+{
+ GtkWidget *entry;
+ GtkClipboard *clipboard;
+
+ entry = GTK_WIDGET (user_data);
+
+ /* Get the clipboard object */
+ clipboard = gtk_widget_get_clipboard (entry,
+ GDK_SELECTION_CLIPBOARD);
+
+ /* Set clipboard text */
+ gtk_clipboard_set_text (clipboard, gtk_entry_get_text (GTK_ENTRY (entry)), -1);
+}
+
+void
+paste_received (GtkClipboard *clipboard,
+ const gchar *text,
+ gpointer user_data)
+{
+ GtkWidget *entry;
+
+ entry = GTK_WIDGET (user_data);
+
+ /* Set the entry text */
+ gtk_entry_set_text (GTK_ENTRY (entry), text);
+}
+
+void
+paste_button_clicked (GtkWidget *button,
+ gpointer user_data)
+{
+ GtkWidget *entry;
+ GtkClipboard *clipboard;
+
+ entry = GTK_WIDGET (user_data);
+
+ /* Get the clipboard object */
+ clipboard = gtk_widget_get_clipboard (entry,
+ GDK_SELECTION_CLIPBOARD);
+
+ /* Request the contents of the clipboard, contents_received will be
+ called when we do get the contents.
+ */
+ gtk_clipboard_request_text (clipboard,
+ paste_received, entry);
+}
+
+GtkWidget *
+do_clipboard (GtkWidget *do_widget)
+{
+ if (!window)
+ {
+ GtkWidget *vbox, *hbox;
+ GtkWidget *label;
+ GtkWidget *entry, *button;
+
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ vbox = gtk_vbox_new (FALSE, 0);
+ gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
+
+ gtk_container_add (GTK_CONTAINER (window), vbox);
+
+ label = gtk_label_new ("\"Copy\" will copy the text\nin the entry to the clipboard");
+
+ gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
+
+ hbox = gtk_hbox_new (FALSE, 4);
+ 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);
+
+ /* Create the button */
+ button = gtk_button_new_from_stock (GTK_STOCK_COPY);
+ gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
+ g_signal_connect (button, "clicked",
+ G_CALLBACK (copy_button_clicked), entry);
+
+ 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);
+
+ hbox = gtk_hbox_new (FALSE, 4);
+ 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);
+
+ /* Create the button */
+ button = gtk_button_new_from_stock (GTK_STOCK_PASTE);
+ gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
+ g_signal_connect (button, "clicked",
+ G_CALLBACK (paste_button_clicked), entry);
+
+ }
+
+ if (!GTK_WIDGET_VISIBLE (window))
+ gtk_widget_show_all (window);
+ else
+ {
+ gtk_widget_destroy (window);
+ window = NULL;
+ }
+
+ return window;
+}