1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
3 * Copyright (C) 2004 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Global clipboard abstraction.
26 #include "gtkclipboard.h"
27 #include "gtkinvisible.h"
29 #include "gtkmarshalers.h"
32 #ifdef GDK_WINDOWING_X11
36 #ifdef GDK_WINDOWING_WIN32
37 #include "win32/gdkwin32.h"
45 typedef struct _GtkClipboardClass GtkClipboardClass;
47 typedef struct _RequestContentsInfo RequestContentsInfo;
48 typedef struct _RequestTextInfo RequestTextInfo;
49 typedef struct _RequestImageInfo RequestImageInfo;
50 typedef struct _RequestTargetsInfo RequestTargetsInfo;
54 GObject parent_instance;
58 GtkClipboardGetFunc get_func;
59 GtkClipboardClearFunc clear_func;
65 gboolean have_selection;
68 GdkAtom *cached_targets;
69 gint n_cached_targets;
71 guint notify_signal_id;
72 gboolean storing_selection;
73 GMainLoop *store_loop;
75 gint n_storable_targets;
76 GdkAtom *storable_targets;
79 struct _GtkClipboardClass
81 GObjectClass parent_class;
83 void (*owner_change) (GtkClipboard *clipboard,
84 GdkEventOwnerChange *event);
87 struct _RequestContentsInfo
89 GtkClipboardReceivedFunc callback;
93 struct _RequestTextInfo
95 GtkClipboardTextReceivedFunc callback;
99 struct _RequestImageInfo
101 GtkClipboardImageReceivedFunc callback;
105 struct _RequestTargetsInfo
107 GtkClipboardTargetsReceivedFunc callback;
111 static void gtk_clipboard_class_init (GtkClipboardClass *class);
112 static void gtk_clipboard_finalize (GObject *object);
113 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
114 GdkEventOwnerChange *event);
116 static void clipboard_unset (GtkClipboard *clipboard);
117 static void selection_received (GtkWidget *widget,
118 GtkSelectionData *selection_data,
120 static GtkClipboard *clipboard_peek (GdkDisplay *display,
122 gboolean only_if_exists);
123 static GtkWidget * get_clipboard_widget (GdkDisplay *display);
129 TARGET_COMPOUND_TEXT,
134 static const gchar request_contents_key[] = "gtk-request-contents";
135 static GQuark request_contents_key_id = 0;
137 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
138 static GQuark clipboards_owned_key_id = 0;
140 static GObjectClass *parent_class;
141 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
144 gtk_clipboard_get_type (void)
146 static GType clipboard_type = 0;
150 static const GTypeInfo clipboard_info =
152 sizeof (GtkClipboardClass),
153 NULL, /* base_init */
154 NULL, /* base_finalize */
155 (GClassInitFunc) gtk_clipboard_class_init,
156 NULL, /* class_finalize */
157 NULL, /* class_data */
158 sizeof (GtkClipboard),
160 (GInstanceInitFunc) NULL,
163 clipboard_type = g_type_register_static (G_TYPE_OBJECT, g_intern_static_string ("GtkClipboard"),
167 return clipboard_type;
171 gtk_clipboard_class_init (GtkClipboardClass *class)
173 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
175 parent_class = g_type_class_peek_parent (class);
177 gobject_class->finalize = gtk_clipboard_finalize;
179 class->owner_change = gtk_clipboard_owner_change;
181 clipboard_signals[OWNER_CHANGE] =
182 g_signal_new ("owner_change",
183 G_TYPE_FROM_CLASS (gobject_class),
185 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
187 _gtk_marshal_VOID__BOXED,
189 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
193 gtk_clipboard_finalize (GObject *object)
195 GtkClipboard *clipboard;
196 GtkWidget *clipboard_widget;
199 clipboard = GTK_CLIPBOARD (object);
201 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
202 if (g_slist_index (clipboards, clipboard) >= 0)
203 g_warning ("GtkClipboard prematurely finalized");
205 clipboard_widget = get_clipboard_widget (clipboard->display);
207 clipboard_unset (clipboard);
209 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
210 clipboards = g_slist_remove (clipboards, clipboard);
211 g_object_set_data (G_OBJECT (clipboard->display), "gtk-clipboard-list", clipboards);
213 if (g_main_loop_is_running (clipboard->store_loop))
215 g_main_loop_quit (clipboard->store_loop);
216 g_main_loop_unref (clipboard->store_loop);
219 if (clipboard->store_timeout != 0)
220 g_source_remove (clipboard->store_timeout);
222 if (clipboard->notify_signal_id != 0)
223 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
225 g_free (clipboard->storable_targets);
227 G_OBJECT_CLASS (parent_class)->finalize (object);
231 clipboard_display_closed (GdkDisplay *display,
233 GtkClipboard *clipboard)
237 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
238 g_object_run_dispose (G_OBJECT (clipboard));
239 clipboards = g_slist_remove (clipboards, clipboard);
240 g_object_set_data (G_OBJECT (display), "gtk-clipboard-list", clipboards);
241 g_object_unref (clipboard);
245 * gtk_clipboard_get_for_display:
246 * @display: the display for which the clipboard is to be retrieved or created
247 * @selection: a #GdkAtom which identifies the clipboard
250 * Returns the clipboard object for the given selection.
251 * Cut/copy/paste menu items and keyboard shortcuts should use
252 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
253 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
254 * for backwards compatibility reasons.)
255 * The currently-selected object or text should be provided on the clipboard
256 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
257 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
258 * to the default clipboard, i.e. they copy the selection to what the
259 * user sees as the clipboard.
261 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
262 * ("CLIPBOARD", FALSE)</literal>. See <ulink
263 * url="http://www.freedesktop.org/Standards/clipboards-spec">
264 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
265 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
266 * selections under the X window system. On Win32 the
267 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
269 * It's possible to have arbitrary named clipboards; if you do invent
270 * new clipboards, you should prefix the selection name with an
271 * underscore (because the ICCCM requires that nonstandard atoms are
272 * underscore-prefixed), and namespace it as well. For example,
273 * if your application called "Foo" has a special-purpose
274 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
276 * Return value: the appropriate clipboard object. If no
277 * clipboard already exists, a new one will
278 * be created. Once a clipboard object has
279 * been created, it is persistent and, since
280 * it is owned by GTK+, must not be freed or
286 gtk_clipboard_get_for_display (GdkDisplay *display,
289 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
290 g_return_val_if_fail (!display->closed, NULL);
292 return clipboard_peek (display, selection, FALSE);
297 * gtk_clipboard_get():
298 * @selection: a #GdkAtom which identifies the clipboard
301 * Returns the clipboard object for the given selection.
302 * See gtk_clipboard_get_for_display() for complete details.
304 * Return value: the appropriate clipboard object. If no
305 * clipboard already exists, a new one will
306 * be created. Once a clipboard object has
307 * been created, it is persistent and, since
308 * it is owned by GTK+, must not be freed or
312 gtk_clipboard_get (GdkAtom selection)
314 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
318 selection_get_cb (GtkWidget *widget,
319 GtkSelectionData *selection_data,
323 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, selection_data->selection);
325 if (clipboard && clipboard->get_func)
326 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
330 selection_clear_event_cb (GtkWidget *widget,
331 GdkEventSelection *event)
333 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
337 clipboard_unset (clipboard);
346 make_clipboard_widget (GdkDisplay *display,
349 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
351 g_signal_connect (widget, "selection_received",
352 G_CALLBACK (selection_received), NULL);
356 /* We need this for gdk_x11_get_server_time() */
357 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
359 g_signal_connect (widget, "selection_get",
360 G_CALLBACK (selection_get_cb), NULL);
361 g_signal_connect (widget, "selection_clear_event",
362 G_CALLBACK (selection_clear_event_cb), NULL);
369 get_clipboard_widget (GdkDisplay *display)
371 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
374 clip_widget = make_clipboard_widget (display, TRUE);
375 g_object_set_data (G_OBJECT (display), "gtk-clipboard-widget", clip_widget);
381 /* This function makes a very good guess at what the correct
382 * timestamp for a selection request should be. If there is
383 * a currently processed event, it uses the timestamp for that
384 * event, otherwise it uses the current server time. However,
385 * if the time resulting from that is older than the time used
386 * last time, it uses the time used last time instead.
388 * In order implement this correctly, we never use CurrentTime,
389 * but actually retrieve the actual timestamp from the server.
390 * This is a little slower but allows us to make the guarantee
391 * that the times used by this application will always ascend
392 * and we won't get selections being rejected just because
393 * we are using a correct timestamp from an event, but used
394 * CurrentTime previously.
397 clipboard_get_timestamp (GtkClipboard *clipboard)
399 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
400 guint32 timestamp = gtk_get_current_event_time ();
402 if (timestamp == GDK_CURRENT_TIME)
404 #ifdef GDK_WINDOWING_X11
405 timestamp = gdk_x11_get_server_time (clipboard_widget->window);
406 #elif defined GDK_WINDOWING_WIN32
407 timestamp = GetMessageTime ();
412 if (clipboard->timestamp != GDK_CURRENT_TIME)
414 /* Check to see if clipboard->timestamp is newer than
415 * timestamp, accounting for wraparound.
418 guint32 max = timestamp + 0x80000000;
420 if ((max > timestamp &&
421 (clipboard->timestamp > timestamp &&
422 clipboard->timestamp <= max)) ||
424 (clipboard->timestamp > timestamp ||
425 clipboard->timestamp <= max)))
427 timestamp = clipboard->timestamp;
432 clipboard->timestamp = timestamp;
438 clipboard_owner_destroyed (gpointer data)
440 GSList *clipboards = data;
443 tmp_list = clipboards;
446 GtkClipboard *clipboard = tmp_list->data;
448 clipboard->get_func = NULL;
449 clipboard->clear_func = NULL;
450 clipboard->user_data = NULL;
451 clipboard->have_owner = FALSE;
453 gtk_clipboard_clear (clipboard);
455 tmp_list = tmp_list->next;
458 g_slist_free (clipboards);
462 clipboard_add_owner_notify (GtkClipboard *clipboard)
464 if (!clipboards_owned_key_id)
465 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
467 if (clipboard->have_owner)
468 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
469 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
470 clipboards_owned_key_id),
472 clipboard_owner_destroyed);
476 clipboard_remove_owner_notify (GtkClipboard *clipboard)
478 if (clipboard->have_owner)
479 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
480 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
481 clipboards_owned_key_id),
483 clipboard_owner_destroyed);
487 gtk_clipboard_set_contents (GtkClipboard *clipboard,
488 const GtkTargetEntry *targets,
490 GtkClipboardGetFunc get_func,
491 GtkClipboardClearFunc clear_func,
495 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
497 if (gtk_selection_owner_set_for_display (clipboard->display,
499 clipboard->selection,
500 clipboard_get_timestamp (clipboard)))
502 clipboard->have_selection = TRUE;
504 if (!(clipboard->have_owner && have_owner) ||
505 clipboard->user_data != user_data)
507 clipboard_unset (clipboard);
509 if (clipboard->get_func)
511 /* Calling unset() caused the clipboard contents to be reset!
512 * Avoid leaking and return
514 if (!(clipboard->have_owner && have_owner) ||
515 clipboard->user_data != user_data)
517 (*clear_func) (clipboard, user_data);
525 clipboard->user_data = user_data;
526 clipboard->have_owner = have_owner;
528 clipboard_add_owner_notify (clipboard);
533 clipboard->get_func = get_func;
534 clipboard->clear_func = clear_func;
536 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
537 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
547 * gtk_clipboard_set_with_data:
548 * @clipboard: a #GtkClipboard
549 * @targets: array containing information about the available forms for the
551 * @n_targets: number of elements in @targets
552 * @get_func: function to call to get the actual clipboard data
553 * @clear_func: when the clipboard contents are set again, this function will
554 * be called, and @get_func will not be subsequently called.
555 * @user_data: user data to pass to @get_func and @clear_func.
557 * Virtually sets the contents of the specified clipboard by providing
558 * a list of supported formats for the clipboard data and a function
559 * to call to get the actual data when it is requested.
561 * Return value: %TRUE if setting the clipboard data succeeded. If setting
562 * the clipboard data failed the provided callback functions
566 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
567 const GtkTargetEntry *targets,
569 GtkClipboardGetFunc get_func,
570 GtkClipboardClearFunc clear_func,
573 g_return_val_if_fail (clipboard != NULL, FALSE);
574 g_return_val_if_fail (targets != NULL, FALSE);
575 g_return_val_if_fail (get_func != NULL, FALSE);
577 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
578 get_func, clear_func, user_data,
583 * gtk_clipboard_set_with_owner:
584 * @clipboard: a #GtkClipboard
585 * @targets: array containing information about the available forms for the
587 * @n_targets: number of elements in @targets
588 * @get_func: function to call to get the actual clipboard data
589 * @clear_func: when the clipboard contents are set again, this function will
590 * be called, and @get_func will not be subsequently called.
591 * @owner: an object that "owns" the data. This object will be passed
592 * to the callbacks when called.
594 * Virtually sets the contents of the specified clipboard by providing
595 * a list of supported formats for the clipboard data and a function
596 * to call to get the actual data when it is requested.
598 * The difference between this function and gtk_clipboard_set_with_data()
599 * is that instead of an generic @user_data pointer, a #GObject is passed
602 * Return value: %TRUE if setting the clipboard data succeeded. If setting
603 * the clipboard data failed the provided callback functions
607 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
608 const GtkTargetEntry *targets,
610 GtkClipboardGetFunc get_func,
611 GtkClipboardClearFunc clear_func,
614 g_return_val_if_fail (clipboard != NULL, FALSE);
615 g_return_val_if_fail (targets != NULL, FALSE);
616 g_return_val_if_fail (get_func != NULL, FALSE);
617 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
619 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
620 get_func, clear_func, owner,
625 * gtk_clipboard_get_owner:
626 * @clipboard: a #GtkClipboard
628 * If the clipboard contents callbacks were set with
629 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
630 * gtk_clipboard_clear() has not subsequently called, returns the owner set
631 * by gtk_clipboard_set_with_owner().
633 * Return value: the owner of the clipboard, if any; otherwise %NULL.
636 gtk_clipboard_get_owner (GtkClipboard *clipboard)
638 g_return_val_if_fail (clipboard != NULL, NULL);
640 if (clipboard->have_owner)
641 return clipboard->user_data;
647 clipboard_unset (GtkClipboard *clipboard)
649 GtkClipboardClearFunc old_clear_func;
651 gboolean old_have_owner;
652 gint old_n_storable_targets;
654 old_clear_func = clipboard->clear_func;
655 old_data = clipboard->user_data;
656 old_have_owner = clipboard->have_owner;
657 old_n_storable_targets = clipboard->n_storable_targets;
661 clipboard_remove_owner_notify (clipboard);
662 clipboard->have_owner = FALSE;
665 clipboard->n_storable_targets = -1;
666 g_free (clipboard->storable_targets);
667 clipboard->storable_targets = NULL;
669 clipboard->get_func = NULL;
670 clipboard->clear_func = NULL;
671 clipboard->user_data = NULL;
674 old_clear_func (clipboard, old_data);
676 /* If we've transferred the clipboard data to the manager,
679 if (old_have_owner &&
680 old_n_storable_targets != -1)
681 g_object_unref (old_data);
685 * gtk_clipboard_clear:
686 * @clipboard: a #GtkClipboard
688 * Clears the contents of the clipboard. Generally this should only
689 * be called between the time you call gtk_clipboard_set_with_owner()
690 * or gtk_clipboard_set_with_data(),
691 * and when the @clear_func you supplied is called. Otherwise, the
692 * clipboard may be owned by someone else.
695 gtk_clipboard_clear (GtkClipboard *clipboard)
697 g_return_if_fail (clipboard != NULL);
699 if (clipboard->have_selection)
700 gtk_selection_owner_set_for_display (clipboard->display,
702 clipboard->selection,
703 clipboard_get_timestamp (clipboard));
707 text_get_func (GtkClipboard *clipboard,
708 GtkSelectionData *selection_data,
712 gtk_selection_data_set_text (selection_data, data, -1);
716 text_clear_func (GtkClipboard *clipboard,
724 * gtk_clipboard_set_text:
725 * @clipboard: a #GtkClipboard object
726 * @text: a UTF-8 string.
727 * @len: length of @text, in bytes, or -1, in which case
728 * the length will be determined with <function>strlen()</function>.
730 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
731 * make a copy of the text and take responsibility for responding
732 * for requests for the text, and for converting the text into
733 * the requested format.
736 gtk_clipboard_set_text (GtkClipboard *clipboard,
742 GtkTargetEntry *targets;
745 g_return_if_fail (clipboard != NULL);
746 g_return_if_fail (text != NULL);
748 list = gtk_target_list_new (NULL, 0);
749 gtk_target_list_add_text_targets (list, 0);
751 n_targets = g_list_length (list->list);
752 targets = g_new0 (GtkTargetEntry, n_targets);
753 for (l = list->list, i = 0; l; l = l->next, i++)
755 GtkTargetPair *pair = (GtkTargetPair *)l->data;
756 targets[i].target = gdk_atom_name (pair->target);
762 gtk_clipboard_set_with_data (clipboard,
764 text_get_func, text_clear_func,
765 g_strndup (text, len));
766 gtk_clipboard_set_can_store (clipboard, NULL, 0);
768 for (i = 0; i < n_targets; i++)
769 g_free (targets[i].target);
771 gtk_target_list_unref (list);
775 pixbuf_get_func (GtkClipboard *clipboard,
776 GtkSelectionData *selection_data,
780 gtk_selection_data_set_pixbuf (selection_data, data);
784 pixbuf_clear_func (GtkClipboard *clipboard,
787 g_object_unref (data);
791 * gtk_clipboard_set_image:
792 * @clipboard: a #GtkClipboard object
793 * @pixbuf: a #GdkPixbuf
795 * Sets the contents of the clipboard to the given #GdkPixbuf.
796 * GTK+ will take responsibility for responding for requests
797 * for the image, and for converting the image into the
803 gtk_clipboard_set_image (GtkClipboard *clipboard,
808 GtkTargetEntry *targets;
811 g_return_if_fail (clipboard != NULL);
812 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
814 list = gtk_target_list_new (NULL, 0);
815 gtk_target_list_add_image_targets (list, 0, TRUE);
817 n_targets = g_list_length (list->list);
818 targets = g_new0 (GtkTargetEntry, n_targets);
819 for (l = list->list, i = 0; l; l = l->next, i++)
821 GtkTargetPair *pair = (GtkTargetPair *)l->data;
822 targets[i].target = gdk_atom_name (pair->target);
825 gtk_clipboard_set_with_data (clipboard,
827 pixbuf_get_func, pixbuf_clear_func,
828 g_object_ref (pixbuf));
829 gtk_clipboard_set_can_store (clipboard, NULL, 0);
831 for (i = 0; i < n_targets; i++)
832 g_free (targets[i].target);
834 gtk_target_list_unref (list);
838 set_request_contents_info (GtkWidget *widget,
839 RequestContentsInfo *info)
841 if (!request_contents_key_id)
842 request_contents_key_id = g_quark_from_static_string (request_contents_key);
844 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
847 static RequestContentsInfo *
848 get_request_contents_info (GtkWidget *widget)
850 if (!request_contents_key_id)
853 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
857 selection_received (GtkWidget *widget,
858 GtkSelectionData *selection_data,
861 RequestContentsInfo *request_info = get_request_contents_info (widget);
862 set_request_contents_info (widget, NULL);
864 request_info->callback (gtk_widget_get_clipboard (widget, selection_data->selection),
866 request_info->user_data);
868 g_free (request_info);
870 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
871 gtk_widget_destroy (widget);
875 * gtk_clipboard_request_contents:
876 * @clipboard: a #GtkClipboard
877 * @target: an atom representing the form into which the clipboard
878 * owner should convert the selection.
879 * @callback: A function to call when the results are received
880 * (or the retrieval fails). If the retrieval fails
881 * the length field of @selection_data will be
883 * @user_data: user data to pass to @callback
885 * Requests the contents of clipboard as the given target.
886 * When the results of the result are later received the supplied callback
890 gtk_clipboard_request_contents (GtkClipboard *clipboard,
892 GtkClipboardReceivedFunc callback,
895 RequestContentsInfo *info;
897 GtkWidget *clipboard_widget;
899 g_return_if_fail (clipboard != NULL);
900 g_return_if_fail (target != GDK_NONE);
901 g_return_if_fail (callback != NULL);
903 clipboard_widget = get_clipboard_widget (clipboard->display);
905 if (get_request_contents_info (clipboard_widget))
906 widget = make_clipboard_widget (clipboard->display, FALSE);
908 widget = clipboard_widget;
910 info = g_new (RequestContentsInfo, 1);
911 info->callback = callback;
912 info->user_data = user_data;
914 set_request_contents_info (widget, info);
916 gtk_selection_convert (widget, clipboard->selection, target,
917 clipboard_get_timestamp (clipboard));
921 request_text_received_func (GtkClipboard *clipboard,
922 GtkSelectionData *selection_data,
925 RequestTextInfo *info = data;
926 gchar *result = NULL;
928 result = gtk_selection_data_get_text (selection_data);
932 /* If we asked for UTF8 and didn't get it, try compound_text;
933 * if we asked for compound_text and didn't get it, try string;
934 * If we asked for anything else and didn't get it, give up.
936 if (selection_data->target == gdk_atom_intern ("UTF8_STRING", FALSE))
938 gtk_clipboard_request_contents (clipboard,
939 gdk_atom_intern ("COMPOUND_TEXT", FALSE),
940 request_text_received_func, info);
943 else if (selection_data->target == gdk_atom_intern ("COMPOUND_TEXT", FALSE))
945 gtk_clipboard_request_contents (clipboard,
947 request_text_received_func, info);
952 info->callback (clipboard, result, info->user_data);
958 * gtk_clipboard_request_text:
959 * @clipboard: a #GtkClipboard
960 * @callback: a function to call when the text is received,
961 * or the retrieval fails. (It will always be called
962 * one way or the other.)
963 * @user_data: user data to pass to @callback.
965 * Requests the contents of the clipboard as text. When the text is
966 * later received, it will be converted to UTF-8 if necessary, and
967 * @callback will be called.
969 * The @text parameter to @callback will contain the resulting text if
970 * the request succeeded, or %NULL if it failed. This could happen for
971 * various reasons, in particular if the clipboard was empty or if the
972 * contents of the clipboard could not be converted into text form.
975 gtk_clipboard_request_text (GtkClipboard *clipboard,
976 GtkClipboardTextReceivedFunc callback,
979 RequestTextInfo *info;
981 g_return_if_fail (clipboard != NULL);
982 g_return_if_fail (callback != NULL);
984 info = g_new (RequestTextInfo, 1);
985 info->callback = callback;
986 info->user_data = user_data;
988 gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("UTF8_STRING", FALSE),
989 request_text_received_func,
994 request_image_received_func (GtkClipboard *clipboard,
995 GtkSelectionData *selection_data,
998 RequestImageInfo *info = data;
999 GdkPixbuf *result = NULL;
1001 result = gtk_selection_data_get_pixbuf (selection_data);
1005 /* If we asked for image/png and didn't get it, try image/jpeg;
1006 * if we asked for image/jpeg and didn't get it, try image/gif;
1007 * if we asked for image/gif and didn't get it, try image/bmp;
1008 * If we asked for anything else and didn't get it, give up.
1010 if (selection_data->target == gdk_atom_intern ("image/png", FALSE))
1012 gtk_clipboard_request_contents (clipboard,
1013 gdk_atom_intern ("image/jpeg", FALSE),
1014 request_image_received_func, info);
1017 else if (selection_data->target == gdk_atom_intern ("image/jpeg", FALSE))
1019 gtk_clipboard_request_contents (clipboard,
1020 gdk_atom_intern ("image/gif", FALSE),
1021 request_image_received_func, info);
1024 else if (selection_data->target == gdk_atom_intern ("image/gif", FALSE))
1026 gtk_clipboard_request_contents (clipboard,
1027 gdk_atom_intern ("image/bmp", FALSE),
1028 request_image_received_func, info);
1033 info->callback (clipboard, result, info->user_data);
1035 g_object_unref (result);
1039 * gtk_clipboard_request_image:
1040 * @clipboard: a #GtkClipboard
1041 * @callback: a function to call when the image is received,
1042 * or the retrieval fails. (It will always be called
1043 * one way or the other.)
1044 * @user_data: user data to pass to @callback.
1046 * Requests the contents of the clipboard as image. When the image is
1047 * later received, it will be converted to a #GdkPixbuf, and
1048 * @callback will be called.
1050 * The @pixbuf parameter to @callback will contain the resulting
1051 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1052 * could happen for various reasons, in particular if the clipboard
1053 * was empty or if the contents of the clipboard could not be
1054 * converted into an image.
1059 gtk_clipboard_request_image (GtkClipboard *clipboard,
1060 GtkClipboardImageReceivedFunc callback,
1063 RequestImageInfo *info;
1065 g_return_if_fail (clipboard != NULL);
1066 g_return_if_fail (callback != NULL);
1068 info = g_new (RequestImageInfo, 1);
1069 info->callback = callback;
1070 info->user_data = user_data;
1072 gtk_clipboard_request_contents (clipboard,
1073 gdk_atom_intern ("image/png", FALSE),
1074 request_image_received_func,
1079 request_targets_received_func (GtkClipboard *clipboard,
1080 GtkSelectionData *selection_data,
1083 RequestTargetsInfo *info = data;
1084 GdkAtom *targets = NULL;
1087 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1089 info->callback (clipboard, targets, n_targets, info->user_data);
1096 * gtk_clipboard_request_targets:
1097 * @clipboard: a #GtkClipboard
1098 * @callback: a function to call when the targets are received,
1099 * or the retrieval fails. (It will always be called
1100 * one way or the other.)
1101 * @user_data: user data to pass to @callback.
1103 * Requests the contents of the clipboard as list of supported targets.
1104 * When the list is later received, @callback will be called.
1106 * The @targets parameter to @callback will contain the resulting targets if
1107 * the request succeeded, or %NULL if it failed.
1112 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1113 GtkClipboardTargetsReceivedFunc callback,
1116 RequestTargetsInfo *info;
1118 g_return_if_fail (clipboard != NULL);
1119 g_return_if_fail (callback != NULL);
1121 /* If the display supports change notification we cache targets */
1122 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1123 clipboard->n_cached_targets != -1)
1125 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1129 info = g_new (RequestTargetsInfo, 1);
1130 info->callback = callback;
1131 info->user_data = user_data;
1133 gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE),
1134 request_targets_received_func,
1145 clipboard_received_func (GtkClipboard *clipboard,
1146 GtkSelectionData *selection_data,
1149 WaitResults *results = data;
1151 if (selection_data->length >= 0)
1152 results->data = gtk_selection_data_copy (selection_data);
1154 g_main_loop_quit (results->loop);
1158 * gtk_clipboard_wait_for_contents:
1159 * @clipboard: a #GtkClipboard
1160 * @target: an atom representing the form into which the clipboard
1161 * owner should convert the selection.
1163 * Requests the contents of the clipboard using the given target.
1164 * This function waits for the data to be received using the main
1165 * loop, so events, timeouts, etc, may be dispatched during the wait.
1167 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1168 * if retrieving the given target failed. If non-%NULL,
1169 * this value must be freed with gtk_selection_data_free()
1170 * when you are finished with it.
1173 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1176 WaitResults results;
1178 g_return_val_if_fail (clipboard != NULL, NULL);
1179 g_return_val_if_fail (target != GDK_NONE, NULL);
1181 results.data = NULL;
1182 results.loop = g_main_loop_new (NULL, TRUE);
1184 gtk_clipboard_request_contents (clipboard, target,
1185 clipboard_received_func,
1188 if (g_main_loop_is_running (results.loop))
1190 GDK_THREADS_LEAVE ();
1191 g_main_loop_run (results.loop);
1192 GDK_THREADS_ENTER ();
1195 g_main_loop_unref (results.loop);
1197 return results.data;
1201 clipboard_text_received_func (GtkClipboard *clipboard,
1205 WaitResults *results = data;
1207 results->data = g_strdup (text);
1208 g_main_loop_quit (results->loop);
1212 * gtk_clipboard_wait_for_text:
1213 * @clipboard: a #GtkClipboard
1215 * Requests the contents of the clipboard as text and converts
1216 * the result to UTF-8 if necessary. This function waits for
1217 * the data to be received using the main loop, so events,
1218 * timeouts, etc, may be dispatched during the wait.
1220 * Return value: a newly-allocated UTF-8 string which must
1221 * be freed with g_free(), or %NULL if retrieving
1222 * the selection data failed. (This could happen
1223 * for various reasons, in particular if the
1224 * clipboard was empty or if the contents of the
1225 * clipboard could not be converted into text form.)
1228 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1230 WaitResults results;
1232 g_return_val_if_fail (clipboard != NULL, NULL);
1234 results.data = NULL;
1235 results.loop = g_main_loop_new (NULL, TRUE);
1237 gtk_clipboard_request_text (clipboard,
1238 clipboard_text_received_func,
1241 if (g_main_loop_is_running (results.loop))
1243 GDK_THREADS_LEAVE ();
1244 g_main_loop_run (results.loop);
1245 GDK_THREADS_ENTER ();
1248 g_main_loop_unref (results.loop);
1250 return results.data;
1254 clipboard_image_received_func (GtkClipboard *clipboard,
1258 WaitResults *results = data;
1261 results->data = g_object_ref (pixbuf);
1263 g_main_loop_quit (results->loop);
1267 * gtk_clipboard_wait_for_image:
1268 * @clipboard: a #GtkClipboard
1270 * Requests the contents of the clipboard as image and converts
1271 * the result to a #GdkPixbuf. This function waits for
1272 * the data to be received using the main loop, so events,
1273 * timeouts, etc, may be dispatched during the wait.
1275 * Return value: a newly-allocated #GdkPixbuf object which must
1276 * be disposed with g_object_unref(), or %NULL if
1277 * retrieving the selection data failed. (This
1278 * could happen for various reasons, in particular
1279 * if the clipboard was empty or if the contents of
1280 * the clipboard could not be converted into an image.)
1285 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1287 WaitResults results;
1289 g_return_val_if_fail (clipboard != NULL, NULL);
1291 results.data = NULL;
1292 results.loop = g_main_loop_new (NULL, TRUE);
1294 gtk_clipboard_request_image (clipboard,
1295 clipboard_image_received_func,
1298 if (g_main_loop_is_running (results.loop))
1300 GDK_THREADS_LEAVE ();
1301 g_main_loop_run (results.loop);
1302 GDK_THREADS_ENTER ();
1305 g_main_loop_unref (results.loop);
1307 return results.data;
1311 * gtk_clipboard_get_display:
1312 * @clipboard: a #GtkClipboard
1314 * Gets the #GdkDisplay associated with @clipboard
1316 * Return value: the #GdkDisplay associated with @clipboard
1321 gtk_clipboard_get_display (GtkClipboard *clipboard)
1323 g_return_val_if_fail (clipboard != NULL, NULL);
1325 return clipboard->display;
1329 * gtk_clipboard_wait_is_text_available:
1330 * @clipboard: a #GtkClipboard
1332 * Test to see if there is text available to be pasted
1333 * This is done by requesting the TARGETS atom and checking
1334 * if it contains any of the supported text targets. This function
1335 * waits for the data to be received using the main loop, so events,
1336 * timeouts, etc, may be dispatched during the wait.
1338 * This function is a little faster than calling
1339 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1342 * Return value: %TRUE is there is text available, %FALSE otherwise.
1345 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1347 GtkSelectionData *data;
1348 gboolean result = FALSE;
1350 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1353 result = gtk_selection_data_targets_include_text (data);
1354 gtk_selection_data_free (data);
1361 * gtk_clipboard_wait_is_image_available:
1362 * @clipboard: a #GtkClipboard
1364 * Test to see if there is an image available to be pasted
1365 * This is done by requesting the TARGETS atom and checking
1366 * if it contains any of the supported image targets. This function
1367 * waits for the data to be received using the main loop, so events,
1368 * timeouts, etc, may be dispatched during the wait.
1370 * This function is a little faster than calling
1371 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1372 * the actual image data.
1374 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1379 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1381 GtkSelectionData *data;
1382 gboolean result = FALSE;
1384 data = gtk_clipboard_wait_for_contents (clipboard,
1385 gdk_atom_intern ("TARGETS", FALSE));
1388 result = gtk_selection_data_targets_include_image (data, FALSE);
1389 gtk_selection_data_free (data);
1396 * gtk_clipboard_wait_for_targets
1397 * @clipboard: a #GtkClipboard
1398 * @targets: location to store an array of targets. The result
1399 * stored here must be freed with g_free().
1400 * @n_targets: location to store number of items in @targets.
1402 * Returns a list of targets that are present on the clipboard, or %NULL
1403 * if there aren't any targets available. The returned list must be
1404 * freed with g_free().
1405 * This function waits for the data to be received using the main
1406 * loop, so events, timeouts, etc, may be dispatched during the wait.
1408 * Return value: %TRUE if any targets are present on the clipboard,
1414 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1418 GtkSelectionData *data;
1419 gboolean result = FALSE;
1421 g_return_val_if_fail (clipboard != NULL, FALSE);
1423 /* If the display supports change notification we cache targets */
1424 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1425 clipboard->n_cached_targets != -1)
1428 *n_targets = clipboard->n_cached_targets;
1431 *targets = g_memdup (clipboard->cached_targets,
1432 clipboard->n_cached_targets * sizeof (GdkAtom));
1443 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1447 GdkAtom *tmp_targets;
1450 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1452 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1454 clipboard->n_cached_targets = tmp_n_targets;
1455 clipboard->cached_targets = g_memdup (tmp_targets,
1456 tmp_n_targets * sizeof (GdkAtom));
1460 *n_targets = tmp_n_targets;
1463 *targets = tmp_targets;
1465 g_free (tmp_targets);
1467 gtk_selection_data_free (data);
1473 static GtkClipboard *
1474 clipboard_peek (GdkDisplay *display,
1476 gboolean only_if_exists)
1478 GtkClipboard *clipboard = NULL;
1482 if (selection == GDK_NONE)
1483 selection = GDK_SELECTION_CLIPBOARD;
1485 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1487 tmp_list = clipboards;
1490 clipboard = tmp_list->data;
1491 if (clipboard->selection == selection)
1494 tmp_list = tmp_list->next;
1497 if (!tmp_list && !only_if_exists)
1499 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1500 clipboard->selection = selection;
1501 clipboard->display = display;
1502 clipboard->n_cached_targets = -1;
1503 clipboard->n_storable_targets = -1;
1504 clipboards = g_slist_prepend (clipboards, clipboard);
1505 g_object_set_data (G_OBJECT (display), "gtk-clipboard-list", clipboards);
1506 g_signal_connect (display, "closed",
1507 G_CALLBACK (clipboard_display_closed), clipboard);
1508 gdk_display_request_selection_notification (display, selection);
1515 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1516 GdkEventOwnerChange *event)
1518 if (clipboard->n_cached_targets != -1)
1520 clipboard->n_cached_targets = -1;
1521 g_free (clipboard->cached_targets);
1526 * gtk_clipboard_wait_is_target_available:
1527 * @clipboard: a #GtkClipboard
1528 * @target: A #GdkAtom indicating which target to look for.
1530 * Checks if a clipboard supports pasting data of a given type. This
1531 * function can be used to determine if a "Paste" menu item should be
1532 * insensitive or not.
1534 * If you want to see if there's text available on the clipboard, use
1535 * gtk_clipboard_wait_is_text_available () instead.
1537 * Return value: %TRUE if the target is available, %FALSE otherwise.
1542 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1547 gboolean retval = FALSE;
1549 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1552 for (i = 0; i < n_targets; i++)
1554 if (targets[i] == target)
1567 * _gtk_clipboard_handle_event:
1568 * @event: a owner change event
1570 * Emits the ::owner_change signal on the appropriate @clipboard.
1575 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1577 GdkDisplay *display;
1578 GtkClipboard *clipboard;
1580 display = gdk_drawable_get_display (event->window);
1581 clipboard = clipboard_peek (display, event->selection, TRUE);
1584 g_signal_emit (clipboard,
1585 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1589 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1591 g_main_loop_quit (clipboard->store_loop);
1597 * gtk_clipboard_set_can_store:
1598 * @clipboard: a #GtkClipboard
1599 * @targets: array containing information about which forms should be stored
1600 * or %NULL to indicate that all forms should be stored.
1601 * @n_targets: number of elements in @targets
1603 * Hints that the clipboard data should be stored somewhere when the
1604 * application exits or when gtk_clipboard_store () is called.
1606 * This value is reset when the clipboard owner changes.
1607 * Where the clipboard data is stored is platform dependent,
1608 * see gdk_display_store_clipboard () for more information.
1613 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1614 const GtkTargetEntry *targets,
1617 GtkWidget *clipboard_widget;
1619 static const GtkTargetEntry save_targets[] = {
1620 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1623 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1624 g_return_if_fail (n_targets >= 0);
1626 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1629 g_free (clipboard->storable_targets);
1631 clipboard_widget = get_clipboard_widget (clipboard->display);
1633 /* n_storable_targets being -1 means that
1634 * gtk_clipboard_set_can_store hasn't been called since the
1635 * clipboard owner changed. We only want to add SAVE_TARGETS and
1636 * ref the owner once , so we do that here
1638 if (clipboard->n_storable_targets == -1)
1640 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1643 /* Ref the owner so it won't go away */
1644 if (clipboard->have_owner)
1645 g_object_ref (clipboard->user_data);
1648 clipboard->n_storable_targets = n_targets;
1649 clipboard->storable_targets = g_new (GdkAtom, n_targets);
1650 for (i = 0; i < n_targets; i++)
1651 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1655 gtk_clipboard_selection_notify (GtkWidget *widget,
1656 GdkEventSelection *event,
1657 GtkClipboard *clipboard)
1659 if (event->selection == gdk_atom_intern ("CLIPBOARD_MANAGER", FALSE) &&
1660 clipboard->storing_selection)
1661 g_main_loop_quit (clipboard->store_loop);
1667 * gtk_clipboard_store:
1668 * @clipboard: a #GtkClipboard
1670 * Stores the current clipboard data somewhere so that it will stay
1671 * around after the application has quit.
1676 gtk_clipboard_store (GtkClipboard *clipboard)
1678 GtkWidget *clipboard_widget;
1680 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1682 if (clipboard->n_storable_targets < 0)
1685 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
1688 clipboard_widget = get_clipboard_widget (clipboard->display);
1689 clipboard->notify_signal_id = g_signal_connect (clipboard_widget, "selection_notify_event",
1690 G_CALLBACK (gtk_clipboard_selection_notify), clipboard);
1692 gdk_display_store_clipboard (clipboard->display,
1693 clipboard_widget->window,
1694 clipboard_get_timestamp (clipboard),
1695 clipboard->storable_targets,
1696 clipboard->n_storable_targets);
1698 clipboard->storing_selection = TRUE;
1700 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
1701 clipboard->store_timeout = g_timeout_add (10000, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
1703 if (g_main_loop_is_running (clipboard->store_loop))
1705 GDK_THREADS_LEAVE ();
1706 g_main_loop_run (clipboard->store_loop);
1707 GDK_THREADS_ENTER ();
1710 g_main_loop_unref (clipboard->store_loop);
1711 clipboard->store_loop = NULL;
1713 g_source_remove (clipboard->store_timeout);
1714 clipboard->store_timeout = 0;
1715 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
1716 clipboard->notify_signal_id = 0;
1718 clipboard->storing_selection = FALSE;
1721 /* Stores all clipboard selections on all displays, called from
1725 _gtk_clipboard_store_all (void)
1727 GtkClipboard *clipboard;
1728 GSList *displays, *list;
1730 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
1735 GdkDisplay *display = list->data;
1737 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
1740 gtk_clipboard_store (clipboard);
1744 g_slist_free (displays);
1748 #define __GTK_CLIPBOARD_C__
1749 #include "gtkaliasdef.c"