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"
30 #include "gtktextbufferrichtext.h"
33 #ifdef GDK_WINDOWING_X11
37 #ifdef GDK_WINDOWING_WIN32
38 #include "win32/gdkwin32.h"
46 typedef struct _GtkClipboardClass GtkClipboardClass;
48 typedef struct _RequestContentsInfo RequestContentsInfo;
49 typedef struct _RequestTextInfo RequestTextInfo;
50 typedef struct _RequestRichTextInfo RequestRichTextInfo;
51 typedef struct _RequestImageInfo RequestImageInfo;
52 typedef struct _RequestURIInfo RequestURIInfo;
53 typedef struct _RequestTargetsInfo RequestTargetsInfo;
57 GObject parent_instance;
61 GtkClipboardGetFunc get_func;
62 GtkClipboardClearFunc clear_func;
68 gboolean have_selection;
71 GdkAtom *cached_targets;
72 gint n_cached_targets;
74 gulong notify_signal_id;
75 gboolean storing_selection;
76 GMainLoop *store_loop;
78 gint n_storable_targets;
79 GdkAtom *storable_targets;
82 struct _GtkClipboardClass
84 GObjectClass parent_class;
86 void (*owner_change) (GtkClipboard *clipboard,
87 GdkEventOwnerChange *event);
90 struct _RequestContentsInfo
92 GtkClipboardReceivedFunc callback;
96 struct _RequestTextInfo
98 GtkClipboardTextReceivedFunc callback;
102 struct _RequestRichTextInfo
104 GtkClipboardRichTextReceivedFunc callback;
111 struct _RequestImageInfo
113 GtkClipboardImageReceivedFunc callback;
117 struct _RequestURIInfo
119 GtkClipboardURIReceivedFunc callback;
123 struct _RequestTargetsInfo
125 GtkClipboardTargetsReceivedFunc callback;
129 static void gtk_clipboard_class_init (GtkClipboardClass *class);
130 static void gtk_clipboard_finalize (GObject *object);
131 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
132 GdkEventOwnerChange *event);
134 static void clipboard_unset (GtkClipboard *clipboard);
135 static void selection_received (GtkWidget *widget,
136 GtkSelectionData *selection_data,
138 static GtkClipboard *clipboard_peek (GdkDisplay *display,
140 gboolean only_if_exists);
141 static GtkWidget * get_clipboard_widget (GdkDisplay *display);
147 TARGET_COMPOUND_TEXT,
152 static const gchar request_contents_key[] = "gtk-request-contents";
153 static GQuark request_contents_key_id = 0;
155 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
156 static GQuark clipboards_owned_key_id = 0;
158 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
160 G_DEFINE_TYPE (GtkClipboard, gtk_clipboard, G_TYPE_OBJECT)
163 gtk_clipboard_init (GtkClipboard *object)
168 gtk_clipboard_class_init (GtkClipboardClass *class)
170 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
172 gobject_class->finalize = gtk_clipboard_finalize;
174 class->owner_change = gtk_clipboard_owner_change;
177 * GtkClipboard::owner-change:
178 * @clipboard: the #GtkClipboard on which the signal is emitted
179 * @event: the @GdkEventOwnerChange event
181 * The ::owner-change signal is emitted when GTK+ receives an
182 * event that indicates that the ownership of the selection
183 * associated with @clipboard has changed.
187 clipboard_signals[OWNER_CHANGE] =
188 g_signal_new (I_("owner-change"),
189 G_TYPE_FROM_CLASS (gobject_class),
191 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
193 _gtk_marshal_VOID__BOXED,
195 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
199 gtk_clipboard_finalize (GObject *object)
201 GtkClipboard *clipboard;
202 GtkWidget *clipboard_widget = NULL;
203 GSList *clipboards = NULL;
205 clipboard = GTK_CLIPBOARD (object);
207 if (clipboard->display)
209 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
211 if (g_slist_index (clipboards, clipboard) >= 0)
212 g_warning ("GtkClipboard prematurely finalized");
214 clipboards = g_slist_remove (clipboards, clipboard);
216 g_object_set_data (G_OBJECT (clipboard->display), "gtk-clipboard-list",
219 /* don't use get_clipboard_widget() here because it would create the
220 * widget if it doesn't exist.
222 clipboard_widget = g_object_get_data (G_OBJECT (clipboard->display),
223 "gtk-clipboard-widget");
226 clipboard_unset (clipboard);
228 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
229 g_main_loop_quit (clipboard->store_loop);
231 if (clipboard->store_timeout != 0)
232 g_source_remove (clipboard->store_timeout);
234 if (clipboard->notify_signal_id != 0)
235 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
237 g_free (clipboard->storable_targets);
238 g_free (clipboard->cached_targets);
240 G_OBJECT_CLASS (gtk_clipboard_parent_class)->finalize (object);
244 clipboard_display_closed (GdkDisplay *display,
246 GtkClipboard *clipboard)
250 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
251 g_object_run_dispose (G_OBJECT (clipboard));
252 clipboards = g_slist_remove (clipboards, clipboard);
253 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
254 g_object_unref (clipboard);
258 * gtk_clipboard_get_for_display:
259 * @display: the display for which the clipboard is to be retrieved or created
260 * @selection: a #GdkAtom which identifies the clipboard
263 * Returns the clipboard object for the given selection.
264 * Cut/copy/paste menu items and keyboard shortcuts should use
265 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
266 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
267 * for backwards compatibility reasons.)
268 * The currently-selected object or text should be provided on the clipboard
269 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
270 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
271 * to the default clipboard, i.e. they copy the selection to what the
272 * user sees as the clipboard.
274 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
275 * ("CLIPBOARD", FALSE)</literal>. See <ulink
276 * url="http://www.freedesktop.org/Standards/clipboards-spec">
277 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
278 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
279 * selections under the X window system. On Win32 the
280 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
282 * It's possible to have arbitrary named clipboards; if you do invent
283 * new clipboards, you should prefix the selection name with an
284 * underscore (because the ICCCM requires that nonstandard atoms are
285 * underscore-prefixed), and namespace it as well. For example,
286 * if your application called "Foo" has a special-purpose
287 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
289 * Return value: (transfer none): the appropriate clipboard object. If no
290 * clipboard already exists, a new one will
291 * be created. Once a clipboard object has
292 * been created, it is persistent and, since
293 * it is owned by GTK+, must not be freed or
299 gtk_clipboard_get_for_display (GdkDisplay *display,
302 g_return_val_if_fail (display != NULL, NULL); /* See bgo#463773; this is needed because Flash Player sucks */
303 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
304 g_return_val_if_fail (!display->closed, NULL);
306 return clipboard_peek (display, selection, FALSE);
312 * @selection: a #GdkAtom which identifies the clipboard to use
314 * Returns the clipboard object for the given selection.
315 * See gtk_clipboard_get_for_display() for complete details.
317 * Return value: (transfer none): the appropriate clipboard object. If no clipboard
318 * already exists, a new one will be created. Once a clipboard
319 * object has been created, it is persistent and, since it is
320 * owned by GTK+, must not be freed or unreffed.
323 gtk_clipboard_get (GdkAtom selection)
325 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
329 selection_get_cb (GtkWidget *widget,
330 GtkSelectionData *selection_data,
334 GtkClipboard *clipboard;
336 clipboard = gtk_widget_get_clipboard (widget,
337 gtk_selection_data_get_selection (selection_data));
339 if (clipboard && clipboard->get_func)
340 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
344 selection_clear_event_cb (GtkWidget *widget,
345 GdkEventSelection *event)
347 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
351 clipboard_unset (clipboard);
360 make_clipboard_widget (GdkDisplay *display,
363 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
365 g_signal_connect (widget, "selection-received",
366 G_CALLBACK (selection_received), NULL);
370 /* We need this for gdk_x11_get_server_time() */
371 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
373 g_signal_connect (widget, "selection-get",
374 G_CALLBACK (selection_get_cb), NULL);
375 g_signal_connect (widget, "selection-clear-event",
376 G_CALLBACK (selection_clear_event_cb), NULL);
383 get_clipboard_widget (GdkDisplay *display)
385 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
388 clip_widget = make_clipboard_widget (display, TRUE);
389 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
395 /* This function makes a very good guess at what the correct
396 * timestamp for a selection request should be. If there is
397 * a currently processed event, it uses the timestamp for that
398 * event, otherwise it uses the current server time. However,
399 * if the time resulting from that is older than the time used
400 * last time, it uses the time used last time instead.
402 * In order implement this correctly, we never use CurrentTime,
403 * but actually retrieve the actual timestamp from the server.
404 * This is a little slower but allows us to make the guarantee
405 * that the times used by this application will always ascend
406 * and we won't get selections being rejected just because
407 * we are using a correct timestamp from an event, but used
408 * CurrentTime previously.
411 clipboard_get_timestamp (GtkClipboard *clipboard)
413 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
414 guint32 timestamp = gtk_get_current_event_time ();
416 if (timestamp == GDK_CURRENT_TIME)
418 #ifdef GDK_WINDOWING_X11
419 timestamp = gdk_x11_get_server_time (gtk_widget_get_window (clipboard_widget));
420 #elif defined GDK_WINDOWING_WIN32
421 timestamp = GetMessageTime ();
426 if (clipboard->timestamp != GDK_CURRENT_TIME)
428 /* Check to see if clipboard->timestamp is newer than
429 * timestamp, accounting for wraparound.
432 guint32 max = timestamp + 0x80000000;
434 if ((max > timestamp &&
435 (clipboard->timestamp > timestamp &&
436 clipboard->timestamp <= max)) ||
438 (clipboard->timestamp > timestamp ||
439 clipboard->timestamp <= max)))
441 timestamp = clipboard->timestamp;
446 clipboard->timestamp = timestamp;
452 clipboard_owner_destroyed (gpointer data)
454 GSList *clipboards = data;
457 tmp_list = clipboards;
460 GtkClipboard *clipboard = tmp_list->data;
462 clipboard->get_func = NULL;
463 clipboard->clear_func = NULL;
464 clipboard->user_data = NULL;
465 clipboard->have_owner = FALSE;
467 gtk_clipboard_clear (clipboard);
469 tmp_list = tmp_list->next;
472 g_slist_free (clipboards);
476 clipboard_add_owner_notify (GtkClipboard *clipboard)
478 if (!clipboards_owned_key_id)
479 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
481 if (clipboard->have_owner)
482 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
483 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
484 clipboards_owned_key_id),
486 clipboard_owner_destroyed);
490 clipboard_remove_owner_notify (GtkClipboard *clipboard)
492 if (clipboard->have_owner)
493 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
494 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
495 clipboards_owned_key_id),
497 clipboard_owner_destroyed);
501 gtk_clipboard_set_contents (GtkClipboard *clipboard,
502 const GtkTargetEntry *targets,
504 GtkClipboardGetFunc get_func,
505 GtkClipboardClearFunc clear_func,
509 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
511 if (gtk_selection_owner_set_for_display (clipboard->display,
513 clipboard->selection,
514 clipboard_get_timestamp (clipboard)))
516 clipboard->have_selection = TRUE;
518 if (clipboard->n_cached_targets != -1)
520 g_free (clipboard->cached_targets);
521 clipboard->cached_targets = NULL;
522 clipboard->n_cached_targets = -1;
525 if (!(clipboard->have_owner && have_owner) ||
526 clipboard->user_data != user_data)
528 clipboard_unset (clipboard);
530 if (clipboard->get_func)
532 /* Calling unset() caused the clipboard contents to be reset!
533 * Avoid leaking and return
535 if (!(clipboard->have_owner && have_owner) ||
536 clipboard->user_data != user_data)
538 (*clear_func) (clipboard, user_data);
546 clipboard->user_data = user_data;
547 clipboard->have_owner = have_owner;
549 clipboard_add_owner_notify (clipboard);
554 clipboard->get_func = get_func;
555 clipboard->clear_func = clear_func;
557 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
558 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
568 * gtk_clipboard_set_with_data:
569 * @clipboard: a #GtkClipboard
570 * @targets: array containing information about the available forms for the
572 * @n_targets: number of elements in @targets
573 * @get_func: (scope async): function to call to get the actual clipboard data
574 * @clear_func: (scope async): when the clipboard contents are set again,
575 * this function will be called, and @get_func will not be subsequently
577 * @user_data: user data to pass to @get_func and @clear_func.
579 * Virtually sets the contents of the specified clipboard by providing
580 * a list of supported formats for the clipboard data and a function
581 * to call to get the actual data when it is requested.
583 * Return value: %TRUE if setting the clipboard data succeeded.
584 * If setting the clipboard data failed the provided callback
585 * functions will be ignored.
588 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
589 const GtkTargetEntry *targets,
591 GtkClipboardGetFunc get_func,
592 GtkClipboardClearFunc clear_func,
595 g_return_val_if_fail (clipboard != NULL, FALSE);
596 g_return_val_if_fail (targets != NULL, FALSE);
597 g_return_val_if_fail (get_func != NULL, FALSE);
599 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
600 get_func, clear_func, user_data,
605 * gtk_clipboard_set_with_owner:
606 * @clipboard: a #GtkClipboard
607 * @targets: array containing information about the available forms for
609 * @n_targets: number of elements in @targets
610 * @get_func: (scope async): function to call to get the actual clipboard data
611 * @clear_func: (scope async): when the clipboard contents are set again,
612 * this function will be called, and @get_func will not be subsequently
614 * @owner: an object that "owns" the data. This object will be passed
615 * to the callbacks when called
617 * Virtually sets the contents of the specified clipboard by providing
618 * a list of supported formats for the clipboard data and a function
619 * to call to get the actual data when it is requested.
621 * The difference between this function and gtk_clipboard_set_with_data()
622 * is that instead of an generic @user_data pointer, a #GObject is passed
625 * Return value: %TRUE if setting the clipboard data succeeded.
626 * If setting the clipboard data failed the provided callback
627 * functions will be ignored.
630 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
631 const GtkTargetEntry *targets,
633 GtkClipboardGetFunc get_func,
634 GtkClipboardClearFunc clear_func,
637 g_return_val_if_fail (clipboard != NULL, FALSE);
638 g_return_val_if_fail (targets != NULL, FALSE);
639 g_return_val_if_fail (get_func != NULL, FALSE);
640 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
642 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
643 get_func, clear_func, owner,
648 * gtk_clipboard_get_owner:
649 * @clipboard: a #GtkClipboard
651 * If the clipboard contents callbacks were set with
652 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
653 * gtk_clipboard_clear() has not subsequently called, returns the owner set
654 * by gtk_clipboard_set_with_owner().
656 * Return value: (transfer none): the owner of the clipboard, if any;
660 gtk_clipboard_get_owner (GtkClipboard *clipboard)
662 g_return_val_if_fail (clipboard != NULL, NULL);
664 if (clipboard->have_owner)
665 return clipboard->user_data;
671 clipboard_unset (GtkClipboard *clipboard)
673 GtkClipboardClearFunc old_clear_func;
675 gboolean old_have_owner;
676 gint old_n_storable_targets;
678 old_clear_func = clipboard->clear_func;
679 old_data = clipboard->user_data;
680 old_have_owner = clipboard->have_owner;
681 old_n_storable_targets = clipboard->n_storable_targets;
685 clipboard_remove_owner_notify (clipboard);
686 clipboard->have_owner = FALSE;
689 clipboard->n_storable_targets = -1;
690 g_free (clipboard->storable_targets);
691 clipboard->storable_targets = NULL;
693 clipboard->get_func = NULL;
694 clipboard->clear_func = NULL;
695 clipboard->user_data = NULL;
698 old_clear_func (clipboard, old_data);
700 /* If we've transferred the clipboard data to the manager,
703 if (old_have_owner &&
704 old_n_storable_targets != -1)
705 g_object_unref (old_data);
709 * gtk_clipboard_clear:
710 * @clipboard: a #GtkClipboard
712 * Clears the contents of the clipboard. Generally this should only
713 * be called between the time you call gtk_clipboard_set_with_owner()
714 * or gtk_clipboard_set_with_data(),
715 * and when the @clear_func you supplied is called. Otherwise, the
716 * clipboard may be owned by someone else.
719 gtk_clipboard_clear (GtkClipboard *clipboard)
721 g_return_if_fail (clipboard != NULL);
723 if (clipboard->have_selection)
724 gtk_selection_owner_set_for_display (clipboard->display,
726 clipboard->selection,
727 clipboard_get_timestamp (clipboard));
731 text_get_func (GtkClipboard *clipboard,
732 GtkSelectionData *selection_data,
736 gtk_selection_data_set_text (selection_data, data, -1);
740 text_clear_func (GtkClipboard *clipboard,
748 * gtk_clipboard_set_text:
749 * @clipboard: a #GtkClipboard object
750 * @text: a UTF-8 string.
751 * @len: length of @text, in bytes, or -1, in which case
752 * the length will be determined with <function>strlen()</function>.
754 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
755 * make a copy of the text and take responsibility for responding
756 * for requests for the text, and for converting the text into
757 * the requested format.
760 gtk_clipboard_set_text (GtkClipboard *clipboard,
765 GtkTargetEntry *targets;
768 g_return_if_fail (clipboard != NULL);
769 g_return_if_fail (text != NULL);
771 list = gtk_target_list_new (NULL, 0);
772 gtk_target_list_add_text_targets (list, 0);
774 targets = gtk_target_table_new_from_list (list, &n_targets);
779 gtk_clipboard_set_with_data (clipboard,
781 text_get_func, text_clear_func,
782 g_strndup (text, len));
783 gtk_clipboard_set_can_store (clipboard, NULL, 0);
785 gtk_target_table_free (targets, n_targets);
786 gtk_target_list_unref (list);
790 pixbuf_get_func (GtkClipboard *clipboard,
791 GtkSelectionData *selection_data,
795 gtk_selection_data_set_pixbuf (selection_data, data);
799 pixbuf_clear_func (GtkClipboard *clipboard,
802 g_object_unref (data);
806 * gtk_clipboard_set_image:
807 * @clipboard: a #GtkClipboard object
808 * @pixbuf: a #GdkPixbuf
810 * Sets the contents of the clipboard to the given #GdkPixbuf.
811 * GTK+ will take responsibility for responding for requests
812 * for the image, and for converting the image into the
818 gtk_clipboard_set_image (GtkClipboard *clipboard,
822 GtkTargetEntry *targets;
825 g_return_if_fail (clipboard != NULL);
826 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
828 list = gtk_target_list_new (NULL, 0);
829 gtk_target_list_add_image_targets (list, 0, TRUE);
831 targets = gtk_target_table_new_from_list (list, &n_targets);
833 gtk_clipboard_set_with_data (clipboard,
835 pixbuf_get_func, pixbuf_clear_func,
836 g_object_ref (pixbuf));
837 gtk_clipboard_set_can_store (clipboard, NULL, 0);
839 gtk_target_table_free (targets, n_targets);
840 gtk_target_list_unref (list);
844 set_request_contents_info (GtkWidget *widget,
845 RequestContentsInfo *info)
847 if (!request_contents_key_id)
848 request_contents_key_id = g_quark_from_static_string (request_contents_key);
850 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
853 static RequestContentsInfo *
854 get_request_contents_info (GtkWidget *widget)
856 if (!request_contents_key_id)
859 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
863 selection_received (GtkWidget *widget,
864 GtkSelectionData *selection_data,
867 RequestContentsInfo *request_info = get_request_contents_info (widget);
868 set_request_contents_info (widget, NULL);
870 request_info->callback (gtk_widget_get_clipboard (widget, gtk_selection_data_get_selection (selection_data)),
872 request_info->user_data);
874 g_free (request_info);
876 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
877 gtk_widget_destroy (widget);
881 * gtk_clipboard_request_contents:
882 * @clipboard: a #GtkClipboard
883 * @target: an atom representing the form into which the clipboard
884 * owner should convert the selection.
885 * @callback: (scope async): A function to call when the results are received
886 * (or the retrieval fails). If the retrieval fails the length field of
887 * @selection_data will be negative.
888 * @user_data: user data to pass to @callback
890 * Requests the contents of clipboard as the given target.
891 * When the results of the result are later received the supplied callback
895 gtk_clipboard_request_contents (GtkClipboard *clipboard,
897 GtkClipboardReceivedFunc callback,
900 RequestContentsInfo *info;
902 GtkWidget *clipboard_widget;
904 g_return_if_fail (clipboard != NULL);
905 g_return_if_fail (target != GDK_NONE);
906 g_return_if_fail (callback != NULL);
908 clipboard_widget = get_clipboard_widget (clipboard->display);
910 if (get_request_contents_info (clipboard_widget))
911 widget = make_clipboard_widget (clipboard->display, FALSE);
913 widget = clipboard_widget;
915 info = g_new (RequestContentsInfo, 1);
916 info->callback = callback;
917 info->user_data = user_data;
919 set_request_contents_info (widget, info);
921 gtk_selection_convert (widget, clipboard->selection, target,
922 clipboard_get_timestamp (clipboard));
926 request_text_received_func (GtkClipboard *clipboard,
927 GtkSelectionData *selection_data,
930 RequestTextInfo *info = data;
931 gchar *result = NULL;
933 result = (gchar *) gtk_selection_data_get_text (selection_data);
937 /* If we asked for UTF8 and didn't get it, try compound_text;
938 * if we asked for compound_text and didn't get it, try string;
939 * If we asked for anything else and didn't get it, give up.
941 GdkAtom target = gtk_selection_data_get_target (selection_data);
943 if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
945 gtk_clipboard_request_contents (clipboard,
946 gdk_atom_intern_static_string ("COMPOUND_TEXT"),
947 request_text_received_func, info);
950 else if (target == gdk_atom_intern_static_string ("COMPOUND_TEXT"))
952 gtk_clipboard_request_contents (clipboard,
954 request_text_received_func, info);
959 info->callback (clipboard, result, info->user_data);
965 * gtk_clipboard_request_text:
966 * @clipboard: a #GtkClipboard
967 * @callback: (scope async): a function to call when the text is received,
968 * or the retrieval fails. (It will always be called one way or the other.)
969 * @user_data: user data to pass to @callback.
971 * Requests the contents of the clipboard as text. When the text is
972 * later received, it will be converted to UTF-8 if necessary, and
973 * @callback will be called.
975 * The @text parameter to @callback will contain the resulting text if
976 * the request succeeded, or %NULL if it failed. This could happen for
977 * various reasons, in particular if the clipboard was empty or if the
978 * contents of the clipboard could not be converted into text form.
981 gtk_clipboard_request_text (GtkClipboard *clipboard,
982 GtkClipboardTextReceivedFunc callback,
985 RequestTextInfo *info;
987 g_return_if_fail (clipboard != NULL);
988 g_return_if_fail (callback != NULL);
990 info = g_new (RequestTextInfo, 1);
991 info->callback = callback;
992 info->user_data = user_data;
994 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("UTF8_STRING"),
995 request_text_received_func,
1000 request_rich_text_received_func (GtkClipboard *clipboard,
1001 GtkSelectionData *selection_data,
1004 RequestRichTextInfo *info = data;
1005 guint8 *result = NULL;
1008 result = (guint8 *) gtk_selection_data_get_data (selection_data);
1009 length = gtk_selection_data_get_length (selection_data);
1011 info->current_atom++;
1013 if ((!result || length < 1) && (info->current_atom < info->n_atoms))
1015 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1016 request_rich_text_received_func,
1021 info->callback (clipboard, gtk_selection_data_get_target (selection_data),
1024 g_free (info->atoms);
1029 * gtk_clipboard_request_rich_text:
1030 * @clipboard: a #GtkClipboard
1031 * @buffer: a #GtkTextBuffer
1032 * @callback: (scope async): a function to call when the text is received,
1033 * or the retrieval fails. (It will always be called one way or the other.)
1034 * @user_data: user data to pass to @callback.
1036 * Requests the contents of the clipboard as rich text. When the rich
1037 * text is later received, @callback will be called.
1039 * The @text parameter to @callback will contain the resulting rich
1040 * text if the request succeeded, or %NULL if it failed. The @length
1041 * parameter will contain @text's length. This function can fail for
1042 * various reasons, in particular if the clipboard was empty or if the
1043 * contents of the clipboard could not be converted into rich text form.
1048 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
1049 GtkTextBuffer *buffer,
1050 GtkClipboardRichTextReceivedFunc callback,
1053 RequestRichTextInfo *info;
1055 g_return_if_fail (clipboard != NULL);
1056 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1057 g_return_if_fail (callback != NULL);
1059 info = g_new (RequestRichTextInfo, 1);
1060 info->callback = callback;
1063 info->current_atom = 0;
1064 info->user_data = user_data;
1066 info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
1068 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1069 request_rich_text_received_func,
1074 request_image_received_func (GtkClipboard *clipboard,
1075 GtkSelectionData *selection_data,
1078 RequestImageInfo *info = data;
1079 GdkPixbuf *result = NULL;
1081 result = gtk_selection_data_get_pixbuf (selection_data);
1085 /* If we asked for image/png and didn't get it, try image/jpeg;
1086 * if we asked for image/jpeg and didn't get it, try image/gif;
1087 * if we asked for image/gif and didn't get it, try image/bmp;
1088 * If we asked for anything else and didn't get it, give up.
1090 GdkAtom target = gtk_selection_data_get_target (selection_data);
1092 if (target == gdk_atom_intern_static_string ("image/png"))
1094 gtk_clipboard_request_contents (clipboard,
1095 gdk_atom_intern_static_string ("image/jpeg"),
1096 request_image_received_func, info);
1099 else if (target == gdk_atom_intern_static_string ("image/jpeg"))
1101 gtk_clipboard_request_contents (clipboard,
1102 gdk_atom_intern_static_string ("image/gif"),
1103 request_image_received_func, info);
1106 else if (target == gdk_atom_intern_static_string ("image/gif"))
1108 gtk_clipboard_request_contents (clipboard,
1109 gdk_atom_intern_static_string ("image/bmp"),
1110 request_image_received_func, info);
1115 info->callback (clipboard, result, info->user_data);
1119 g_object_unref (result);
1123 * gtk_clipboard_request_image:
1124 * @clipboard: a #GtkClipboard
1125 * @callback: (scope async): a function to call when the image is received,
1126 * or the retrieval fails. (It will always be called one way or the other.)
1127 * @user_data: user data to pass to @callback.
1129 * Requests the contents of the clipboard as image. When the image is
1130 * later received, it will be converted to a #GdkPixbuf, and
1131 * @callback will be called.
1133 * The @pixbuf parameter to @callback will contain the resulting
1134 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1135 * could happen for various reasons, in particular if the clipboard
1136 * was empty or if the contents of the clipboard could not be
1137 * converted into an image.
1142 gtk_clipboard_request_image (GtkClipboard *clipboard,
1143 GtkClipboardImageReceivedFunc callback,
1146 RequestImageInfo *info;
1148 g_return_if_fail (clipboard != NULL);
1149 g_return_if_fail (callback != NULL);
1151 info = g_new (RequestImageInfo, 1);
1152 info->callback = callback;
1153 info->user_data = user_data;
1155 gtk_clipboard_request_contents (clipboard,
1156 gdk_atom_intern_static_string ("image/png"),
1157 request_image_received_func,
1162 request_uris_received_func (GtkClipboard *clipboard,
1163 GtkSelectionData *selection_data,
1166 RequestURIInfo *info = data;
1169 uris = gtk_selection_data_get_uris (selection_data);
1170 info->callback (clipboard, uris, info->user_data);
1173 g_slice_free (RequestURIInfo, info);
1177 * gtk_clipboard_request_uris:
1178 * @clipboard: a #GtkClipboard
1179 * @callback: (scope async): a function to call when the URIs are received,
1180 * or the retrieval fails. (It will always be called one way or the other.)
1181 * @user_data: user data to pass to @callback.
1183 * Requests the contents of the clipboard as URIs. When the URIs are
1184 * later received @callback will be called.
1186 * The @uris parameter to @callback will contain the resulting array of
1187 * URIs if the request succeeded, or %NULL if it failed. This could happen
1188 * for various reasons, in particular if the clipboard was empty or if the
1189 * contents of the clipboard could not be converted into URI form.
1194 gtk_clipboard_request_uris (GtkClipboard *clipboard,
1195 GtkClipboardURIReceivedFunc callback,
1198 RequestURIInfo *info;
1200 g_return_if_fail (clipboard != NULL);
1201 g_return_if_fail (callback != NULL);
1203 info = g_slice_new (RequestURIInfo);
1204 info->callback = callback;
1205 info->user_data = user_data;
1207 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"),
1208 request_uris_received_func,
1213 request_targets_received_func (GtkClipboard *clipboard,
1214 GtkSelectionData *selection_data,
1217 RequestTargetsInfo *info = data;
1218 GdkAtom *targets = NULL;
1221 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1223 info->callback (clipboard, targets, n_targets, info->user_data);
1230 * gtk_clipboard_request_targets:
1231 * @clipboard: a #GtkClipboard
1232 * @callback: (scope async): a function to call when the targets are
1233 * received, or the retrieval fails. (It will always be called
1234 * one way or the other.)
1235 * @user_data: user data to pass to @callback.
1237 * Requests the contents of the clipboard as list of supported targets.
1238 * When the list is later received, @callback will be called.
1240 * The @targets parameter to @callback will contain the resulting targets if
1241 * the request succeeded, or %NULL if it failed.
1246 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1247 GtkClipboardTargetsReceivedFunc callback,
1250 RequestTargetsInfo *info;
1252 g_return_if_fail (clipboard != NULL);
1253 g_return_if_fail (callback != NULL);
1255 /* If the display supports change notification we cache targets */
1256 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1257 clipboard->n_cached_targets != -1)
1259 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1263 info = g_new (RequestTargetsInfo, 1);
1264 info->callback = callback;
1265 info->user_data = user_data;
1267 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"),
1268 request_targets_received_func,
1276 GdkAtom format; /* used by rich text */
1277 gsize length; /* used by rich text */
1281 clipboard_received_func (GtkClipboard *clipboard,
1282 GtkSelectionData *selection_data,
1285 WaitResults *results = data;
1287 if (gtk_selection_data_get_length (selection_data) >= 0)
1288 results->data = gtk_selection_data_copy (selection_data);
1290 g_main_loop_quit (results->loop);
1294 * gtk_clipboard_wait_for_contents:
1295 * @clipboard: a #GtkClipboard
1296 * @target: an atom representing the form into which the clipboard
1297 * owner should convert the selection.
1299 * Requests the contents of the clipboard using the given target.
1300 * This function waits for the data to be received using the main
1301 * loop, so events, timeouts, etc, may be dispatched during the wait.
1303 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1304 * if retrieving the given target failed. If non-%NULL,
1305 * this value must be freed with gtk_selection_data_free()
1306 * when you are finished with it.
1309 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1312 WaitResults results;
1314 g_return_val_if_fail (clipboard != NULL, NULL);
1315 g_return_val_if_fail (target != GDK_NONE, NULL);
1317 results.data = NULL;
1318 results.loop = g_main_loop_new (NULL, TRUE);
1320 gtk_clipboard_request_contents (clipboard, target,
1321 clipboard_received_func,
1324 if (g_main_loop_is_running (results.loop))
1326 GDK_THREADS_LEAVE ();
1327 g_main_loop_run (results.loop);
1328 GDK_THREADS_ENTER ();
1331 g_main_loop_unref (results.loop);
1333 return results.data;
1337 clipboard_text_received_func (GtkClipboard *clipboard,
1341 WaitResults *results = data;
1343 results->data = g_strdup (text);
1344 g_main_loop_quit (results->loop);
1348 * gtk_clipboard_wait_for_text:
1349 * @clipboard: a #GtkClipboard
1351 * Requests the contents of the clipboard as text and converts
1352 * the result to UTF-8 if necessary. This function waits for
1353 * the data to be received using the main loop, so events,
1354 * timeouts, etc, may be dispatched during the wait.
1356 * Return value: a newly-allocated UTF-8 string which must
1357 * be freed with g_free(), or %NULL if retrieving
1358 * the selection data failed. (This could happen
1359 * for various reasons, in particular if the
1360 * clipboard was empty or if the contents of the
1361 * clipboard could not be converted into text form.)
1364 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1366 WaitResults results;
1368 g_return_val_if_fail (clipboard != NULL, NULL);
1370 results.data = NULL;
1371 results.loop = g_main_loop_new (NULL, TRUE);
1373 gtk_clipboard_request_text (clipboard,
1374 clipboard_text_received_func,
1377 if (g_main_loop_is_running (results.loop))
1379 GDK_THREADS_LEAVE ();
1380 g_main_loop_run (results.loop);
1381 GDK_THREADS_ENTER ();
1384 g_main_loop_unref (results.loop);
1386 return results.data;
1390 clipboard_rich_text_received_func (GtkClipboard *clipboard,
1396 WaitResults *results = data;
1398 results->data = g_memdup (text, length);
1399 results->format = format;
1400 results->length = length;
1401 g_main_loop_quit (results->loop);
1405 * gtk_clipboard_wait_for_rich_text:
1406 * @clipboard: a #GtkClipboard
1407 * @buffer: a #GtkTextBuffer
1408 * @format: return location for the format of the returned data
1409 * @length: return location for the length of the returned data
1411 * Requests the contents of the clipboard as rich text. This function
1412 * waits for the data to be received using the main loop, so events,
1413 * timeouts, etc, may be dispatched during the wait.
1415 * Return value: a newly-allocated binary block of data which must
1416 * be freed with g_free(), or %NULL if retrieving
1417 * the selection data failed. (This could happen
1418 * for various reasons, in particular if the
1419 * clipboard was empty or if the contents of the
1420 * clipboard could not be converted into text form.)
1425 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
1426 GtkTextBuffer *buffer,
1430 WaitResults results;
1432 g_return_val_if_fail (clipboard != NULL, NULL);
1433 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1434 g_return_val_if_fail (format != NULL, NULL);
1435 g_return_val_if_fail (length != NULL, NULL);
1437 results.data = NULL;
1438 results.loop = g_main_loop_new (NULL, TRUE);
1440 gtk_clipboard_request_rich_text (clipboard, buffer,
1441 clipboard_rich_text_received_func,
1444 if (g_main_loop_is_running (results.loop))
1446 GDK_THREADS_LEAVE ();
1447 g_main_loop_run (results.loop);
1448 GDK_THREADS_ENTER ();
1451 g_main_loop_unref (results.loop);
1453 *format = results.format;
1454 *length = results.length;
1456 return results.data;
1460 clipboard_image_received_func (GtkClipboard *clipboard,
1464 WaitResults *results = data;
1467 results->data = g_object_ref (pixbuf);
1469 g_main_loop_quit (results->loop);
1473 * gtk_clipboard_wait_for_image:
1474 * @clipboard: a #GtkClipboard
1476 * Requests the contents of the clipboard as image and converts
1477 * the result to a #GdkPixbuf. This function waits for
1478 * the data to be received using the main loop, so events,
1479 * timeouts, etc, may be dispatched during the wait.
1481 * Return value: (transfer full): a newly-allocated #GdkPixbuf
1482 * object which must be disposed with g_object_unref(), or
1483 * %NULL if retrieving the selection data failed. (This could
1484 * happen for various reasons, in particular if the clipboard
1485 * was empty or if the contents of the clipboard could not be
1486 * converted into an image.)
1491 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1493 WaitResults results;
1495 g_return_val_if_fail (clipboard != NULL, NULL);
1497 results.data = NULL;
1498 results.loop = g_main_loop_new (NULL, TRUE);
1500 gtk_clipboard_request_image (clipboard,
1501 clipboard_image_received_func,
1504 if (g_main_loop_is_running (results.loop))
1506 GDK_THREADS_LEAVE ();
1507 g_main_loop_run (results.loop);
1508 GDK_THREADS_ENTER ();
1511 g_main_loop_unref (results.loop);
1513 return results.data;
1517 clipboard_uris_received_func (GtkClipboard *clipboard,
1521 WaitResults *results = data;
1523 results->data = g_strdupv (uris);
1524 g_main_loop_quit (results->loop);
1528 * gtk_clipboard_wait_for_uris:
1529 * @clipboard: a #GtkClipboard
1531 * Requests the contents of the clipboard as URIs. This function waits
1532 * for the data to be received using the main loop, so events,
1533 * timeouts, etc, may be dispatched during the wait.
1535 * Return value: (array zero-terminated=1) (element-type utf8) (transfer full): a newly-allocated
1536 * %NULL-terminated array of strings which must
1537 * be freed with g_strfreev(), or %NULL if
1538 * retrieving the selection data failed. (This
1539 * could happen for various reasons, in particular
1540 * if the clipboard was empty or if the contents of
1541 * the clipboard could not be converted into URI form.)
1546 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
1548 WaitResults results;
1550 g_return_val_if_fail (clipboard != NULL, NULL);
1552 results.data = NULL;
1553 results.loop = g_main_loop_new (NULL, TRUE);
1555 gtk_clipboard_request_uris (clipboard,
1556 clipboard_uris_received_func,
1559 if (g_main_loop_is_running (results.loop))
1561 GDK_THREADS_LEAVE ();
1562 g_main_loop_run (results.loop);
1563 GDK_THREADS_ENTER ();
1566 g_main_loop_unref (results.loop);
1568 return results.data;
1572 * gtk_clipboard_get_display:
1573 * @clipboard: a #GtkClipboard
1575 * Gets the #GdkDisplay associated with @clipboard
1577 * Return value: (transfer none): the #GdkDisplay associated with @clipboard
1582 gtk_clipboard_get_display (GtkClipboard *clipboard)
1584 g_return_val_if_fail (clipboard != NULL, NULL);
1586 return clipboard->display;
1590 * gtk_clipboard_wait_is_text_available:
1591 * @clipboard: a #GtkClipboard
1593 * Test to see if there is text available to be pasted
1594 * This is done by requesting the TARGETS atom and checking
1595 * if it contains any of the supported text targets. This function
1596 * waits for the data to be received using the main loop, so events,
1597 * timeouts, etc, may be dispatched during the wait.
1599 * This function is a little faster than calling
1600 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1603 * Return value: %TRUE is there is text available, %FALSE otherwise.
1606 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1608 GtkSelectionData *data;
1609 gboolean result = FALSE;
1611 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1614 result = gtk_selection_data_targets_include_text (data);
1615 gtk_selection_data_free (data);
1622 * gtk_clipboard_wait_is_rich_text_available:
1623 * @clipboard: a #GtkClipboard
1624 * @buffer: a #GtkTextBuffer
1626 * Test to see if there is rich text available to be pasted
1627 * This is done by requesting the TARGETS atom and checking
1628 * if it contains any of the supported rich text targets. This function
1629 * waits for the data to be received using the main loop, so events,
1630 * timeouts, etc, may be dispatched during the wait.
1632 * This function is a little faster than calling
1633 * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
1636 * Return value: %TRUE is there is rich text available, %FALSE otherwise.
1641 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
1642 GtkTextBuffer *buffer)
1644 GtkSelectionData *data;
1645 gboolean result = FALSE;
1647 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1648 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1650 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1653 result = gtk_selection_data_targets_include_rich_text (data, buffer);
1654 gtk_selection_data_free (data);
1661 * gtk_clipboard_wait_is_image_available:
1662 * @clipboard: a #GtkClipboard
1664 * Test to see if there is an image available to be pasted
1665 * This is done by requesting the TARGETS atom and checking
1666 * if it contains any of the supported image targets. This function
1667 * waits for the data to be received using the main loop, so events,
1668 * timeouts, etc, may be dispatched during the wait.
1670 * This function is a little faster than calling
1671 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1672 * the actual image data.
1674 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1679 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1681 GtkSelectionData *data;
1682 gboolean result = FALSE;
1684 data = gtk_clipboard_wait_for_contents (clipboard,
1685 gdk_atom_intern_static_string ("TARGETS"));
1688 result = gtk_selection_data_targets_include_image (data, FALSE);
1689 gtk_selection_data_free (data);
1696 * gtk_clipboard_wait_is_uris_available:
1697 * @clipboard: a #GtkClipboard
1699 * Test to see if there is a list of URIs available to be pasted
1700 * This is done by requesting the TARGETS atom and checking
1701 * if it contains the URI targets. This function
1702 * waits for the data to be received using the main loop, so events,
1703 * timeouts, etc, may be dispatched during the wait.
1705 * This function is a little faster than calling
1706 * gtk_clipboard_wait_for_uris() since it doesn't need to retrieve
1707 * the actual URI data.
1709 * Return value: %TRUE is there is an URI list available, %FALSE otherwise.
1714 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
1716 GtkSelectionData *data;
1717 gboolean result = FALSE;
1719 data = gtk_clipboard_wait_for_contents (clipboard,
1720 gdk_atom_intern_static_string ("TARGETS"));
1723 result = gtk_selection_data_targets_include_uri (data);
1724 gtk_selection_data_free (data);
1731 * gtk_clipboard_wait_for_targets
1732 * @clipboard: a #GtkClipboard
1733 * @targets: location to store an array of targets. The result
1734 * stored here must be freed with g_free().
1735 * @n_targets: location to store number of items in @targets.
1737 * Returns a list of targets that are present on the clipboard, or %NULL
1738 * if there aren't any targets available. The returned list must be
1739 * freed with g_free().
1740 * This function waits for the data to be received using the main
1741 * loop, so events, timeouts, etc, may be dispatched during the wait.
1743 * Return value: %TRUE if any targets are present on the clipboard,
1749 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1753 GtkSelectionData *data;
1754 gboolean result = FALSE;
1756 g_return_val_if_fail (clipboard != NULL, FALSE);
1758 /* If the display supports change notification we cache targets */
1759 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1760 clipboard->n_cached_targets != -1)
1763 *n_targets = clipboard->n_cached_targets;
1766 *targets = g_memdup (clipboard->cached_targets,
1767 clipboard->n_cached_targets * sizeof (GdkAtom));
1778 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1782 GdkAtom *tmp_targets;
1785 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1787 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1789 clipboard->n_cached_targets = tmp_n_targets;
1790 clipboard->cached_targets = g_memdup (tmp_targets,
1791 tmp_n_targets * sizeof (GdkAtom));
1795 *n_targets = tmp_n_targets;
1798 *targets = tmp_targets;
1800 g_free (tmp_targets);
1802 gtk_selection_data_free (data);
1808 static GtkClipboard *
1809 clipboard_peek (GdkDisplay *display,
1811 gboolean only_if_exists)
1813 GtkClipboard *clipboard = NULL;
1817 if (selection == GDK_NONE)
1818 selection = GDK_SELECTION_CLIPBOARD;
1820 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1822 tmp_list = clipboards;
1825 clipboard = tmp_list->data;
1826 if (clipboard->selection == selection)
1829 tmp_list = tmp_list->next;
1832 if (!tmp_list && !only_if_exists)
1834 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1835 clipboard->selection = selection;
1836 clipboard->display = display;
1837 clipboard->n_cached_targets = -1;
1838 clipboard->n_storable_targets = -1;
1839 clipboards = g_slist_prepend (clipboards, clipboard);
1840 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1841 g_signal_connect (display, "closed",
1842 G_CALLBACK (clipboard_display_closed), clipboard);
1843 gdk_display_request_selection_notification (display, selection);
1850 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1851 GdkEventOwnerChange *event)
1853 if (clipboard->n_cached_targets != -1)
1855 g_free (clipboard->cached_targets);
1856 clipboard->cached_targets = NULL;
1857 clipboard->n_cached_targets = -1;
1862 * gtk_clipboard_wait_is_target_available:
1863 * @clipboard: a #GtkClipboard
1864 * @target: A #GdkAtom indicating which target to look for.
1866 * Checks if a clipboard supports pasting data of a given type. This
1867 * function can be used to determine if a "Paste" menu item should be
1868 * insensitive or not.
1870 * If you want to see if there's text available on the clipboard, use
1871 * gtk_clipboard_wait_is_text_available () instead.
1873 * Return value: %TRUE if the target is available, %FALSE otherwise.
1878 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1883 gboolean retval = FALSE;
1885 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1888 for (i = 0; i < n_targets; i++)
1890 if (targets[i] == target)
1903 * _gtk_clipboard_handle_event:
1904 * @event: a owner change event
1906 * Emits the #GtkClipboard::owner-change signal on the appropriate @clipboard.
1911 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1913 GdkDisplay *display;
1914 GtkClipboard *clipboard;
1916 display = gdk_window_get_display (event->window);
1917 clipboard = clipboard_peek (display, event->selection, TRUE);
1920 g_signal_emit (clipboard,
1921 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1925 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1927 g_main_loop_quit (clipboard->store_loop);
1933 * gtk_clipboard_set_can_store:
1934 * @clipboard: a #GtkClipboard
1935 * @targets: (allow-none): array containing information about which forms
1936 * should be stored or %NULL to indicate that all forms should
1938 * @n_targets: number of elements in @targets
1940 * Hints that the clipboard data should be stored somewhere when the
1941 * application exits or when gtk_clipboard_store () is called.
1943 * This value is reset when the clipboard owner changes.
1944 * Where the clipboard data is stored is platform dependent,
1945 * see gdk_display_store_clipboard () for more information.
1950 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1951 const GtkTargetEntry *targets,
1954 GtkWidget *clipboard_widget;
1956 static const GtkTargetEntry save_targets[] = {
1957 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1960 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1961 g_return_if_fail (n_targets >= 0);
1963 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1966 g_free (clipboard->storable_targets);
1968 clipboard_widget = get_clipboard_widget (clipboard->display);
1970 /* n_storable_targets being -1 means that
1971 * gtk_clipboard_set_can_store hasn't been called since the
1972 * clipboard owner changed. We only want to add SAVE_TARGETS and
1973 * ref the owner once , so we do that here
1975 if (clipboard->n_storable_targets == -1)
1977 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1980 /* Ref the owner so it won't go away */
1981 if (clipboard->have_owner)
1982 g_object_ref (clipboard->user_data);
1985 clipboard->n_storable_targets = n_targets;
1986 clipboard->storable_targets = g_new (GdkAtom, n_targets);
1987 for (i = 0; i < n_targets; i++)
1988 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1992 gtk_clipboard_selection_notify (GtkWidget *widget,
1993 GdkEventSelection *event,
1994 GtkClipboard *clipboard)
1996 if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
1997 clipboard->storing_selection)
1998 g_main_loop_quit (clipboard->store_loop);
2004 * gtk_clipboard_store:
2005 * @clipboard: a #GtkClipboard
2007 * Stores the current clipboard data somewhere so that it will stay
2008 * around after the application has quit.
2013 gtk_clipboard_store (GtkClipboard *clipboard)
2015 GtkWidget *clipboard_widget;
2017 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2019 if (clipboard->n_storable_targets < 0)
2022 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
2025 g_object_ref (clipboard);
2027 clipboard_widget = get_clipboard_widget (clipboard->display);
2028 clipboard->notify_signal_id = g_signal_connect (clipboard_widget,
2029 "selection-notify-event",
2030 G_CALLBACK (gtk_clipboard_selection_notify),
2033 gdk_display_store_clipboard (clipboard->display,
2034 gtk_widget_get_window (clipboard_widget),
2035 clipboard_get_timestamp (clipboard),
2036 clipboard->storable_targets,
2037 clipboard->n_storable_targets);
2039 clipboard->storing_selection = TRUE;
2041 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
2042 clipboard->store_timeout = g_timeout_add_seconds (10, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
2044 if (g_main_loop_is_running (clipboard->store_loop))
2046 GDK_THREADS_LEAVE ();
2047 g_main_loop_run (clipboard->store_loop);
2048 GDK_THREADS_ENTER ();
2051 g_main_loop_unref (clipboard->store_loop);
2052 clipboard->store_loop = NULL;
2054 g_source_remove (clipboard->store_timeout);
2055 clipboard->store_timeout = 0;
2056 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
2057 clipboard->notify_signal_id = 0;
2059 clipboard->storing_selection = FALSE;
2061 g_object_unref (clipboard);
2064 /* Stores all clipboard selections on all displays, called from
2068 _gtk_clipboard_store_all (void)
2070 GtkClipboard *clipboard;
2071 GSList *displays, *list;
2073 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
2078 GdkDisplay *display = list->data;
2080 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
2083 gtk_clipboard_store (clipboard);
2087 g_slist_free (displays);