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_BROADWAY
38 #include "broadway/gdkbroadway.h"
41 #ifdef GDK_WINDOWING_WIN32
42 #include "win32/gdkwin32.h"
47 * SECTION:gtkclipboard
48 * @Short_description: Storing data on clipboards
50 * @See_also: #GtkSelection
52 * The #GtkClipboard object represents a clipboard of data shared
53 * between different processes or between different widgets in
54 * the same process. Each clipboard is identified by a name encoded as a
55 * #GdkAtom. (Conversion to and from strings can be done with
56 * gdk_atom_intern() and gdk_atom_name().) The default clipboard
57 * corresponds to the "CLIPBOARD" atom; another commonly used clipboard
58 * is the "PRIMARY" clipboard, which, in X, traditionally contains
59 * the currently selected text.
61 * To support having a number of different formats on the clipboard
62 * at the same time, the clipboard mechanism allows providing
63 * callbacks instead of the actual data. When you set the contents
64 * of the clipboard, you can either supply the data directly (via
65 * functions like gtk_clipboard_set_text()), or you can supply a
66 * callback to be called at a later time when the data is needed (via
67 * gtk_clipboard_set_with_data() or gtk_clipboard_set_with_owner().)
68 * Providing a callback also avoids having to make copies of the data
69 * when it is not needed.
71 * gtk_clipboard_set_with_data() and gtk_clipboard_set_with_owner()
72 * are quite similar; the choice between the two depends mostly on
73 * which is more convenient in a particular situation.
74 * The former is most useful when you want to have a blob of data
75 * with callbacks to convert it into the various data types that you
76 * advertise. When the @clear_func you provided is called, you
77 * simply free the data blob. The latter is more useful when the
78 * contents of clipboard reflect the internal state of a #GObject
79 * (As an example, for the PRIMARY clipboard, when an entry widget
80 * provides the clipboard's contents the contents are simply the
81 * text within the selected region.) If the contents change, the
82 * entry widget can call gtk_clipboard_set_with_owner() to update
83 * the timestamp for clipboard ownership, without having to worry
84 * about @clear_func being called.
86 * Requesting the data from the clipboard is essentially
87 * asynchronous. If the contents of the clipboard are provided within
88 * the same process, then a direct function call will be made to
89 * retrieve the data, but if they are provided by another process,
90 * then the data needs to be retrieved from the other process, which
91 * may take some time. To avoid blocking the user interface, the call
92 * to request the selection, gtk_clipboard_request_contents() takes a
93 * callback that will be called when the contents are received (or
94 * when the request fails.) If you don't want to deal with providing
95 * a separate callback, you can also use gtk_clipboard_wait_for_contents().
96 * What this does is run the GLib main loop recursively waiting for
97 * the contents. This can simplify the code flow, but you still have
98 * to be aware that other callbacks in your program can be called
99 * while this recursive mainloop is running.
101 * Along with the functions to get the clipboard contents as an
102 * arbitrary data chunk, there are also functions to retrieve
103 * it as text, gtk_clipboard_request_text() and
104 * gtk_clipboard_wait_for_text(). These functions take care of
105 * determining which formats are advertised by the clipboard
106 * provider, asking for the clipboard in the best available format
107 * and converting the results into the UTF-8 encoding. (The standard
108 * form for representing strings in GTK+.)
117 typedef struct _GtkClipboardClass GtkClipboardClass;
119 typedef struct _RequestContentsInfo RequestContentsInfo;
120 typedef struct _RequestTextInfo RequestTextInfo;
121 typedef struct _RequestRichTextInfo RequestRichTextInfo;
122 typedef struct _RequestImageInfo RequestImageInfo;
123 typedef struct _RequestURIInfo RequestURIInfo;
124 typedef struct _RequestTargetsInfo RequestTargetsInfo;
128 GObject parent_instance;
132 GtkClipboardGetFunc get_func;
133 GtkClipboardClearFunc clear_func;
139 gboolean have_selection;
142 GdkAtom *cached_targets;
143 gint n_cached_targets;
145 gulong notify_signal_id;
146 gboolean storing_selection;
147 GMainLoop *store_loop;
149 gint n_storable_targets;
150 GdkAtom *storable_targets;
153 struct _GtkClipboardClass
155 GObjectClass parent_class;
157 void (*owner_change) (GtkClipboard *clipboard,
158 GdkEventOwnerChange *event);
161 struct _RequestContentsInfo
163 GtkClipboardReceivedFunc callback;
167 struct _RequestTextInfo
169 GtkClipboardTextReceivedFunc callback;
173 struct _RequestRichTextInfo
175 GtkClipboardRichTextReceivedFunc callback;
182 struct _RequestImageInfo
184 GtkClipboardImageReceivedFunc callback;
188 struct _RequestURIInfo
190 GtkClipboardURIReceivedFunc callback;
194 struct _RequestTargetsInfo
196 GtkClipboardTargetsReceivedFunc callback;
200 static void gtk_clipboard_class_init (GtkClipboardClass *class);
201 static void gtk_clipboard_finalize (GObject *object);
202 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
203 GdkEventOwnerChange *event);
205 static void clipboard_unset (GtkClipboard *clipboard);
206 static void selection_received (GtkWidget *widget,
207 GtkSelectionData *selection_data,
209 static GtkClipboard *clipboard_peek (GdkDisplay *display,
211 gboolean only_if_exists);
212 static GtkWidget * get_clipboard_widget (GdkDisplay *display);
218 TARGET_COMPOUND_TEXT,
223 static const gchar request_contents_key[] = "gtk-request-contents";
224 static GQuark request_contents_key_id = 0;
226 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
227 static GQuark clipboards_owned_key_id = 0;
229 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
231 G_DEFINE_TYPE (GtkClipboard, gtk_clipboard, G_TYPE_OBJECT)
234 gtk_clipboard_init (GtkClipboard *object)
239 gtk_clipboard_class_init (GtkClipboardClass *class)
241 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
243 gobject_class->finalize = gtk_clipboard_finalize;
245 class->owner_change = gtk_clipboard_owner_change;
248 * GtkClipboard::owner-change:
249 * @clipboard: the #GtkClipboard on which the signal is emitted
250 * @event: (type Gdk.EventOwnerChange): the @GdkEventOwnerChange event
252 * The ::owner-change signal is emitted when GTK+ receives an
253 * event that indicates that the ownership of the selection
254 * associated with @clipboard has changed.
258 clipboard_signals[OWNER_CHANGE] =
259 g_signal_new (I_("owner-change"),
260 G_TYPE_FROM_CLASS (gobject_class),
262 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
264 _gtk_marshal_VOID__BOXED,
266 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
270 gtk_clipboard_finalize (GObject *object)
272 GtkClipboard *clipboard;
273 GtkWidget *clipboard_widget = NULL;
274 GSList *clipboards = NULL;
276 clipboard = GTK_CLIPBOARD (object);
278 if (clipboard->display)
280 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
282 if (g_slist_index (clipboards, clipboard) >= 0)
283 g_warning ("GtkClipboard prematurely finalized");
285 clipboards = g_slist_remove (clipboards, clipboard);
287 g_object_set_data (G_OBJECT (clipboard->display), "gtk-clipboard-list",
290 /* don't use get_clipboard_widget() here because it would create the
291 * widget if it doesn't exist.
293 clipboard_widget = g_object_get_data (G_OBJECT (clipboard->display),
294 "gtk-clipboard-widget");
297 clipboard_unset (clipboard);
299 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
300 g_main_loop_quit (clipboard->store_loop);
302 if (clipboard->store_timeout != 0)
303 g_source_remove (clipboard->store_timeout);
305 if (clipboard->notify_signal_id != 0)
306 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
308 g_free (clipboard->storable_targets);
309 g_free (clipboard->cached_targets);
311 G_OBJECT_CLASS (gtk_clipboard_parent_class)->finalize (object);
315 clipboard_display_closed (GdkDisplay *display,
317 GtkClipboard *clipboard)
321 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
322 g_object_run_dispose (G_OBJECT (clipboard));
323 clipboards = g_slist_remove (clipboards, clipboard);
324 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
325 g_object_unref (clipboard);
329 * gtk_clipboard_get_for_display:
330 * @display: the display for which the clipboard is to be retrieved or created
331 * @selection: a #GdkAtom which identifies the clipboard to use.
333 * Returns the clipboard object for the given selection.
334 * Cut/copy/paste menu items and keyboard shortcuts should use
335 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
336 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
337 * for backwards compatibility reasons.)
338 * The currently-selected object or text should be provided on the clipboard
339 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
340 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
341 * to the default clipboard, i.e. they copy the selection to what the
342 * user sees as the clipboard.
344 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
345 * ("CLIPBOARD", FALSE)</literal>. See <ulink
346 * url="http://www.freedesktop.org/Standards/clipboards-spec">
347 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
348 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
349 * selections under the X window system. On Win32 the
350 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
352 * It's possible to have arbitrary named clipboards; if you do invent
353 * new clipboards, you should prefix the selection name with an
354 * underscore (because the ICCCM requires that nonstandard atoms are
355 * underscore-prefixed), and namespace it as well. For example,
356 * if your application called "Foo" has a special-purpose
357 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
359 * Return value: (transfer none): the appropriate clipboard object. If no
360 * clipboard already exists, a new one will be created. Once a clipboard
361 * object has been created, it is persistent and, since it is owned by
362 * GTK+, must not be freed or unrefd.
367 gtk_clipboard_get_for_display (GdkDisplay *display,
370 g_return_val_if_fail (display != NULL, NULL); /* See bgo#463773; this is needed because Flash Player sucks */
371 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
372 g_return_val_if_fail (!gdk_display_is_closed (display), NULL);
374 return clipboard_peek (display, selection, FALSE);
380 * @selection: a #GdkAtom which identifies the clipboard to use
382 * Returns the clipboard object for the given selection.
383 * See gtk_clipboard_get_for_display() for complete details.
385 * Return value: (transfer none): the appropriate clipboard object. If no clipboard
386 * already exists, a new one will be created. Once a clipboard
387 * object has been created, it is persistent and, since it is
388 * owned by GTK+, must not be freed or unreffed.
391 gtk_clipboard_get (GdkAtom selection)
393 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
397 selection_get_cb (GtkWidget *widget,
398 GtkSelectionData *selection_data,
402 GtkClipboard *clipboard;
404 clipboard = gtk_widget_get_clipboard (widget,
405 gtk_selection_data_get_selection (selection_data));
407 if (clipboard && clipboard->get_func)
408 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
412 selection_clear_event_cb (GtkWidget *widget,
413 GdkEventSelection *event)
415 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
419 clipboard_unset (clipboard);
428 make_clipboard_widget (GdkDisplay *display,
431 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
433 g_signal_connect (widget, "selection-received",
434 G_CALLBACK (selection_received), NULL);
438 /* We need this for gdk_x11_get_server_time() */
439 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
441 g_signal_connect (widget, "selection-get",
442 G_CALLBACK (selection_get_cb), NULL);
443 g_signal_connect (widget, "selection-clear-event",
444 G_CALLBACK (selection_clear_event_cb), NULL);
451 get_clipboard_widget (GdkDisplay *display)
453 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
456 clip_widget = make_clipboard_widget (display, TRUE);
457 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
463 /* This function makes a very good guess at what the correct
464 * timestamp for a selection request should be. If there is
465 * a currently processed event, it uses the timestamp for that
466 * event, otherwise it uses the current server time. However,
467 * if the time resulting from that is older than the time used
468 * last time, it uses the time used last time instead.
470 * In order implement this correctly, we never use CurrentTime,
471 * but actually retrieve the actual timestamp from the server.
472 * This is a little slower but allows us to make the guarantee
473 * that the times used by this application will always ascend
474 * and we won't get selections being rejected just because
475 * we are using a correct timestamp from an event, but used
476 * CurrentTime previously.
479 clipboard_get_timestamp (GtkClipboard *clipboard)
481 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
482 guint32 timestamp = gtk_get_current_event_time ();
485 if (timestamp == GDK_CURRENT_TIME)
487 window = gtk_widget_get_window (clipboard_widget);
488 #ifdef GDK_WINDOWING_X11
489 if (GDK_IS_X11_WINDOW (window))
491 timestamp = gdk_x11_get_server_time (gtk_widget_get_window (clipboard_widget));
495 #if defined GDK_WINDOWING_WIN32
496 if (GDK_IS_WIN32_WINDOW (window))
498 timestamp = GetMessageTime ();
502 #if defined GDK_WINDOWING_BROADWAY
503 if (GDK_IS_BROADWAY_WINDOW (window))
505 timestamp = gdk_broadway_get_last_seen_time (window);
510 /* No implementation */
515 if (clipboard->timestamp != GDK_CURRENT_TIME)
517 /* Check to see if clipboard->timestamp is newer than
518 * timestamp, accounting for wraparound.
521 guint32 max = timestamp + 0x80000000;
523 if ((max > timestamp &&
524 (clipboard->timestamp > timestamp &&
525 clipboard->timestamp <= max)) ||
527 (clipboard->timestamp > timestamp ||
528 clipboard->timestamp <= max)))
530 timestamp = clipboard->timestamp;
535 clipboard->timestamp = timestamp;
541 clipboard_owner_destroyed (gpointer data)
543 GSList *clipboards = data;
546 tmp_list = clipboards;
549 GtkClipboard *clipboard = tmp_list->data;
551 clipboard->get_func = NULL;
552 clipboard->clear_func = NULL;
553 clipboard->user_data = NULL;
554 clipboard->have_owner = FALSE;
556 gtk_clipboard_clear (clipboard);
558 tmp_list = tmp_list->next;
561 g_slist_free (clipboards);
565 clipboard_add_owner_notify (GtkClipboard *clipboard)
567 if (!clipboards_owned_key_id)
568 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
570 if (clipboard->have_owner)
571 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
572 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
573 clipboards_owned_key_id),
575 clipboard_owner_destroyed);
579 clipboard_remove_owner_notify (GtkClipboard *clipboard)
581 if (clipboard->have_owner)
582 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
583 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
584 clipboards_owned_key_id),
586 clipboard_owner_destroyed);
590 gtk_clipboard_set_contents (GtkClipboard *clipboard,
591 const GtkTargetEntry *targets,
593 GtkClipboardGetFunc get_func,
594 GtkClipboardClearFunc clear_func,
598 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
600 if (gtk_selection_owner_set_for_display (clipboard->display,
602 clipboard->selection,
603 clipboard_get_timestamp (clipboard)))
605 clipboard->have_selection = TRUE;
607 if (clipboard->n_cached_targets != -1)
609 g_free (clipboard->cached_targets);
610 clipboard->cached_targets = NULL;
611 clipboard->n_cached_targets = -1;
614 if (!(clipboard->have_owner && have_owner) ||
615 clipboard->user_data != user_data)
617 clipboard_unset (clipboard);
619 if (clipboard->get_func)
621 /* Calling unset() caused the clipboard contents to be reset!
622 * Avoid leaking and return
624 if (!(clipboard->have_owner && have_owner) ||
625 clipboard->user_data != user_data)
627 (*clear_func) (clipboard, user_data);
635 clipboard->user_data = user_data;
636 clipboard->have_owner = have_owner;
638 clipboard_add_owner_notify (clipboard);
643 clipboard->get_func = get_func;
644 clipboard->clear_func = clear_func;
646 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
647 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
657 * gtk_clipboard_set_with_data: (skip)
658 * @clipboard: a #GtkClipboard
659 * @targets: (array length=n_targets): array containing information
660 * about the available forms for the clipboard data
661 * @n_targets: number of elements in @targets
662 * @get_func: (scope async): function to call to get the actual clipboard data
663 * @clear_func: (scope async): when the clipboard contents are set again,
664 * this function will be called, and @get_func will not be subsequently
666 * @user_data: user data to pass to @get_func and @clear_func.
668 * Virtually sets the contents of the specified clipboard by providing
669 * a list of supported formats for the clipboard data and a function
670 * to call to get the actual data when it is requested.
672 * Return value: %TRUE if setting the clipboard data succeeded.
673 * If setting the clipboard data failed the provided callback
674 * functions will be ignored.
677 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
678 const GtkTargetEntry *targets,
680 GtkClipboardGetFunc get_func,
681 GtkClipboardClearFunc clear_func,
684 g_return_val_if_fail (clipboard != NULL, FALSE);
685 g_return_val_if_fail (targets != NULL, FALSE);
686 g_return_val_if_fail (get_func != NULL, FALSE);
688 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
689 get_func, clear_func, user_data,
694 * gtk_clipboard_set_with_owner: (skip)
695 * @clipboard: a #GtkClipboard
696 * @targets: (array length=n_targets): array containing information
697 * about the available forms for the clipboard data
698 * @n_targets: number of elements in @targets
699 * @get_func: (scope async): function to call to get the actual clipboard data
700 * @clear_func: (scope async): when the clipboard contents are set again,
701 * this function will be called, and @get_func will not be subsequently
703 * @owner: an object that "owns" the data. This object will be passed
704 * to the callbacks when called
706 * Virtually sets the contents of the specified clipboard by providing
707 * a list of supported formats for the clipboard data and a function
708 * to call to get the actual data when it is requested.
710 * The difference between this function and gtk_clipboard_set_with_data()
711 * is that instead of an generic @user_data pointer, a #GObject is passed
714 * Return value: %TRUE if setting the clipboard data succeeded.
715 * If setting the clipboard data failed the provided callback
716 * functions will be ignored.
719 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
720 const GtkTargetEntry *targets,
722 GtkClipboardGetFunc get_func,
723 GtkClipboardClearFunc clear_func,
726 g_return_val_if_fail (clipboard != NULL, FALSE);
727 g_return_val_if_fail (targets != NULL, FALSE);
728 g_return_val_if_fail (get_func != NULL, FALSE);
729 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
731 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
732 get_func, clear_func, owner,
737 * gtk_clipboard_get_owner:
738 * @clipboard: a #GtkClipboard
740 * If the clipboard contents callbacks were set with
741 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
742 * gtk_clipboard_clear() has not subsequently called, returns the owner set
743 * by gtk_clipboard_set_with_owner().
745 * Return value: (transfer none): the owner of the clipboard, if any;
749 gtk_clipboard_get_owner (GtkClipboard *clipboard)
751 g_return_val_if_fail (clipboard != NULL, NULL);
753 if (clipboard->have_owner)
754 return clipboard->user_data;
760 clipboard_unset (GtkClipboard *clipboard)
762 GtkClipboardClearFunc old_clear_func;
764 gboolean old_have_owner;
765 gint old_n_storable_targets;
767 old_clear_func = clipboard->clear_func;
768 old_data = clipboard->user_data;
769 old_have_owner = clipboard->have_owner;
770 old_n_storable_targets = clipboard->n_storable_targets;
774 clipboard_remove_owner_notify (clipboard);
775 clipboard->have_owner = FALSE;
778 clipboard->n_storable_targets = -1;
779 g_free (clipboard->storable_targets);
780 clipboard->storable_targets = NULL;
782 clipboard->get_func = NULL;
783 clipboard->clear_func = NULL;
784 clipboard->user_data = NULL;
787 old_clear_func (clipboard, old_data);
789 /* If we've transferred the clipboard data to the manager,
792 if (old_have_owner &&
793 old_n_storable_targets != -1)
794 g_object_unref (old_data);
798 * gtk_clipboard_clear:
799 * @clipboard: a #GtkClipboard
801 * Clears the contents of the clipboard. Generally this should only
802 * be called between the time you call gtk_clipboard_set_with_owner()
803 * or gtk_clipboard_set_with_data(),
804 * and when the @clear_func you supplied is called. Otherwise, the
805 * clipboard may be owned by someone else.
808 gtk_clipboard_clear (GtkClipboard *clipboard)
810 g_return_if_fail (clipboard != NULL);
812 if (clipboard->have_selection)
813 gtk_selection_owner_set_for_display (clipboard->display,
815 clipboard->selection,
816 clipboard_get_timestamp (clipboard));
820 text_get_func (GtkClipboard *clipboard,
821 GtkSelectionData *selection_data,
825 gtk_selection_data_set_text (selection_data, data, -1);
829 text_clear_func (GtkClipboard *clipboard,
837 * gtk_clipboard_set_text:
838 * @clipboard: a #GtkClipboard object
839 * @text: a UTF-8 string.
840 * @len: length of @text, in bytes, or -1, in which case
841 * the length will be determined with <function>strlen()</function>.
843 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
844 * make a copy of the text and take responsibility for responding
845 * for requests for the text, and for converting the text into
846 * the requested format.
849 gtk_clipboard_set_text (GtkClipboard *clipboard,
854 GtkTargetEntry *targets;
857 g_return_if_fail (clipboard != NULL);
858 g_return_if_fail (text != NULL);
860 list = gtk_target_list_new (NULL, 0);
861 gtk_target_list_add_text_targets (list, 0);
863 targets = gtk_target_table_new_from_list (list, &n_targets);
868 gtk_clipboard_set_with_data (clipboard,
870 text_get_func, text_clear_func,
871 g_strndup (text, len));
872 gtk_clipboard_set_can_store (clipboard, NULL, 0);
874 gtk_target_table_free (targets, n_targets);
875 gtk_target_list_unref (list);
879 pixbuf_get_func (GtkClipboard *clipboard,
880 GtkSelectionData *selection_data,
884 gtk_selection_data_set_pixbuf (selection_data, data);
888 pixbuf_clear_func (GtkClipboard *clipboard,
891 g_object_unref (data);
895 * gtk_clipboard_set_image:
896 * @clipboard: a #GtkClipboard object
897 * @pixbuf: a #GdkPixbuf
899 * Sets the contents of the clipboard to the given #GdkPixbuf.
900 * GTK+ will take responsibility for responding for requests
901 * for the image, and for converting the image into the
907 gtk_clipboard_set_image (GtkClipboard *clipboard,
911 GtkTargetEntry *targets;
914 g_return_if_fail (clipboard != NULL);
915 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
917 list = gtk_target_list_new (NULL, 0);
918 gtk_target_list_add_image_targets (list, 0, TRUE);
920 targets = gtk_target_table_new_from_list (list, &n_targets);
922 gtk_clipboard_set_with_data (clipboard,
924 pixbuf_get_func, pixbuf_clear_func,
925 g_object_ref (pixbuf));
926 gtk_clipboard_set_can_store (clipboard, NULL, 0);
928 gtk_target_table_free (targets, n_targets);
929 gtk_target_list_unref (list);
933 set_request_contents_info (GtkWidget *widget,
934 RequestContentsInfo *info)
936 if (!request_contents_key_id)
937 request_contents_key_id = g_quark_from_static_string (request_contents_key);
939 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
942 static RequestContentsInfo *
943 get_request_contents_info (GtkWidget *widget)
945 if (!request_contents_key_id)
948 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
952 selection_received (GtkWidget *widget,
953 GtkSelectionData *selection_data,
956 RequestContentsInfo *request_info = get_request_contents_info (widget);
957 set_request_contents_info (widget, NULL);
959 request_info->callback (gtk_widget_get_clipboard (widget, gtk_selection_data_get_selection (selection_data)),
961 request_info->user_data);
963 g_free (request_info);
965 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
966 gtk_widget_destroy (widget);
970 * gtk_clipboard_request_contents:
971 * @clipboard: a #GtkClipboard
972 * @target: an atom representing the form into which the clipboard
973 * owner should convert the selection.
974 * @callback: (scope async): A function to call when the results are received
975 * (or the retrieval fails). If the retrieval fails the length field of
976 * @selection_data will be negative.
977 * @user_data: user data to pass to @callback
979 * Requests the contents of clipboard as the given target.
980 * When the results of the result are later received the supplied callback
984 gtk_clipboard_request_contents (GtkClipboard *clipboard,
986 GtkClipboardReceivedFunc callback,
989 RequestContentsInfo *info;
991 GtkWidget *clipboard_widget;
993 g_return_if_fail (clipboard != NULL);
994 g_return_if_fail (target != GDK_NONE);
995 g_return_if_fail (callback != NULL);
997 clipboard_widget = get_clipboard_widget (clipboard->display);
999 if (get_request_contents_info (clipboard_widget))
1000 widget = make_clipboard_widget (clipboard->display, FALSE);
1002 widget = clipboard_widget;
1004 info = g_new (RequestContentsInfo, 1);
1005 info->callback = callback;
1006 info->user_data = user_data;
1008 set_request_contents_info (widget, info);
1010 gtk_selection_convert (widget, clipboard->selection, target,
1011 clipboard_get_timestamp (clipboard));
1015 request_text_received_func (GtkClipboard *clipboard,
1016 GtkSelectionData *selection_data,
1019 RequestTextInfo *info = data;
1020 gchar *result = NULL;
1022 result = (gchar *) gtk_selection_data_get_text (selection_data);
1026 /* If we asked for UTF8 and didn't get it, try compound_text;
1027 * if we asked for compound_text and didn't get it, try string;
1028 * If we asked for anything else and didn't get it, give up.
1030 GdkAtom target = gtk_selection_data_get_target (selection_data);
1032 if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
1034 gtk_clipboard_request_contents (clipboard,
1035 gdk_atom_intern_static_string ("COMPOUND_TEXT"),
1036 request_text_received_func, info);
1039 else if (target == gdk_atom_intern_static_string ("COMPOUND_TEXT"))
1041 gtk_clipboard_request_contents (clipboard,
1043 request_text_received_func, info);
1048 info->callback (clipboard, result, info->user_data);
1054 * gtk_clipboard_request_text:
1055 * @clipboard: a #GtkClipboard
1056 * @callback: (scope async): a function to call when the text is received,
1057 * or the retrieval fails. (It will always be called one way or the other.)
1058 * @user_data: user data to pass to @callback.
1060 * Requests the contents of the clipboard as text. When the text is
1061 * later received, it will be converted to UTF-8 if necessary, and
1062 * @callback will be called.
1064 * The @text parameter to @callback will contain the resulting text if
1065 * the request succeeded, or %NULL if it failed. This could happen for
1066 * various reasons, in particular if the clipboard was empty or if the
1067 * contents of the clipboard could not be converted into text form.
1070 gtk_clipboard_request_text (GtkClipboard *clipboard,
1071 GtkClipboardTextReceivedFunc callback,
1074 RequestTextInfo *info;
1076 g_return_if_fail (clipboard != NULL);
1077 g_return_if_fail (callback != NULL);
1079 info = g_new (RequestTextInfo, 1);
1080 info->callback = callback;
1081 info->user_data = user_data;
1083 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("UTF8_STRING"),
1084 request_text_received_func,
1089 request_rich_text_received_func (GtkClipboard *clipboard,
1090 GtkSelectionData *selection_data,
1093 RequestRichTextInfo *info = data;
1094 guint8 *result = NULL;
1097 result = (guint8 *) gtk_selection_data_get_data (selection_data);
1098 length = gtk_selection_data_get_length (selection_data);
1100 info->current_atom++;
1102 if ((!result || length < 1) && (info->current_atom < info->n_atoms))
1104 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1105 request_rich_text_received_func,
1110 info->callback (clipboard, gtk_selection_data_get_target (selection_data),
1113 g_free (info->atoms);
1118 * gtk_clipboard_request_rich_text:
1119 * @clipboard: a #GtkClipboard
1120 * @buffer: a #GtkTextBuffer
1121 * @callback: (scope async): a function to call when the text is received,
1122 * or the retrieval fails. (It will always be called one way or the other.)
1123 * @user_data: user data to pass to @callback.
1125 * Requests the contents of the clipboard as rich text. When the rich
1126 * text is later received, @callback will be called.
1128 * The @text parameter to @callback will contain the resulting rich
1129 * text if the request succeeded, or %NULL if it failed. The @length
1130 * parameter will contain @text's length. This function can fail for
1131 * various reasons, in particular if the clipboard was empty or if the
1132 * contents of the clipboard could not be converted into rich text form.
1137 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
1138 GtkTextBuffer *buffer,
1139 GtkClipboardRichTextReceivedFunc callback,
1142 RequestRichTextInfo *info;
1144 g_return_if_fail (clipboard != NULL);
1145 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1146 g_return_if_fail (callback != NULL);
1148 info = g_new (RequestRichTextInfo, 1);
1149 info->callback = callback;
1152 info->current_atom = 0;
1153 info->user_data = user_data;
1155 info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
1157 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1158 request_rich_text_received_func,
1163 request_image_received_func (GtkClipboard *clipboard,
1164 GtkSelectionData *selection_data,
1167 RequestImageInfo *info = data;
1168 GdkPixbuf *result = NULL;
1170 result = gtk_selection_data_get_pixbuf (selection_data);
1174 /* If we asked for image/png and didn't get it, try image/jpeg;
1175 * if we asked for image/jpeg and didn't get it, try image/gif;
1176 * if we asked for image/gif and didn't get it, try image/bmp;
1177 * If we asked for anything else and didn't get it, give up.
1179 GdkAtom target = gtk_selection_data_get_target (selection_data);
1181 if (target == gdk_atom_intern_static_string ("image/png"))
1183 gtk_clipboard_request_contents (clipboard,
1184 gdk_atom_intern_static_string ("image/jpeg"),
1185 request_image_received_func, info);
1188 else if (target == gdk_atom_intern_static_string ("image/jpeg"))
1190 gtk_clipboard_request_contents (clipboard,
1191 gdk_atom_intern_static_string ("image/gif"),
1192 request_image_received_func, info);
1195 else if (target == gdk_atom_intern_static_string ("image/gif"))
1197 gtk_clipboard_request_contents (clipboard,
1198 gdk_atom_intern_static_string ("image/bmp"),
1199 request_image_received_func, info);
1204 info->callback (clipboard, result, info->user_data);
1208 g_object_unref (result);
1212 * gtk_clipboard_request_image:
1213 * @clipboard: a #GtkClipboard
1214 * @callback: (scope async): a function to call when the image is received,
1215 * or the retrieval fails. (It will always be called one way or the other.)
1216 * @user_data: user data to pass to @callback.
1218 * Requests the contents of the clipboard as image. When the image is
1219 * later received, it will be converted to a #GdkPixbuf, and
1220 * @callback will be called.
1222 * The @pixbuf parameter to @callback will contain the resulting
1223 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1224 * could happen for various reasons, in particular if the clipboard
1225 * was empty or if the contents of the clipboard could not be
1226 * converted into an image.
1231 gtk_clipboard_request_image (GtkClipboard *clipboard,
1232 GtkClipboardImageReceivedFunc callback,
1235 RequestImageInfo *info;
1237 g_return_if_fail (clipboard != NULL);
1238 g_return_if_fail (callback != NULL);
1240 info = g_new (RequestImageInfo, 1);
1241 info->callback = callback;
1242 info->user_data = user_data;
1244 gtk_clipboard_request_contents (clipboard,
1245 gdk_atom_intern_static_string ("image/png"),
1246 request_image_received_func,
1251 request_uris_received_func (GtkClipboard *clipboard,
1252 GtkSelectionData *selection_data,
1255 RequestURIInfo *info = data;
1258 uris = gtk_selection_data_get_uris (selection_data);
1259 info->callback (clipboard, uris, info->user_data);
1262 g_slice_free (RequestURIInfo, info);
1266 * gtk_clipboard_request_uris:
1267 * @clipboard: a #GtkClipboard
1268 * @callback: (scope async): a function to call when the URIs are received,
1269 * or the retrieval fails. (It will always be called one way or the other.)
1270 * @user_data: user data to pass to @callback.
1272 * Requests the contents of the clipboard as URIs. When the URIs are
1273 * later received @callback will be called.
1275 * The @uris parameter to @callback will contain the resulting array of
1276 * URIs if the request succeeded, or %NULL if it failed. This could happen
1277 * for various reasons, in particular if the clipboard was empty or if the
1278 * contents of the clipboard could not be converted into URI form.
1283 gtk_clipboard_request_uris (GtkClipboard *clipboard,
1284 GtkClipboardURIReceivedFunc callback,
1287 RequestURIInfo *info;
1289 g_return_if_fail (clipboard != NULL);
1290 g_return_if_fail (callback != NULL);
1292 info = g_slice_new (RequestURIInfo);
1293 info->callback = callback;
1294 info->user_data = user_data;
1296 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"),
1297 request_uris_received_func,
1302 request_targets_received_func (GtkClipboard *clipboard,
1303 GtkSelectionData *selection_data,
1306 RequestTargetsInfo *info = data;
1307 GdkAtom *targets = NULL;
1310 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1312 info->callback (clipboard, targets, n_targets, info->user_data);
1319 * gtk_clipboard_request_targets:
1320 * @clipboard: a #GtkClipboard
1321 * @callback: (scope async): a function to call when the targets are
1322 * received, or the retrieval fails. (It will always be called
1323 * one way or the other.)
1324 * @user_data: user data to pass to @callback.
1326 * Requests the contents of the clipboard as list of supported targets.
1327 * When the list is later received, @callback will be called.
1329 * The @targets parameter to @callback will contain the resulting targets if
1330 * the request succeeded, or %NULL if it failed.
1335 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1336 GtkClipboardTargetsReceivedFunc callback,
1339 RequestTargetsInfo *info;
1341 g_return_if_fail (clipboard != NULL);
1342 g_return_if_fail (callback != NULL);
1344 /* If the display supports change notification we cache targets */
1345 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1346 clipboard->n_cached_targets != -1)
1348 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1352 info = g_new (RequestTargetsInfo, 1);
1353 info->callback = callback;
1354 info->user_data = user_data;
1356 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"),
1357 request_targets_received_func,
1365 GdkAtom format; /* used by rich text */
1366 gsize length; /* used by rich text */
1370 clipboard_received_func (GtkClipboard *clipboard,
1371 GtkSelectionData *selection_data,
1374 WaitResults *results = data;
1376 if (gtk_selection_data_get_length (selection_data) >= 0)
1377 results->data = gtk_selection_data_copy (selection_data);
1379 g_main_loop_quit (results->loop);
1383 * gtk_clipboard_wait_for_contents:
1384 * @clipboard: a #GtkClipboard
1385 * @target: an atom representing the form into which the clipboard
1386 * owner should convert the selection.
1388 * Requests the contents of the clipboard using the given target.
1389 * This function waits for the data to be received using the main
1390 * loop, so events, timeouts, etc, may be dispatched during the wait.
1392 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1393 * if retrieving the given target failed. If non-%NULL,
1394 * this value must be freed with gtk_selection_data_free()
1395 * when you are finished with it.
1398 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1401 WaitResults results;
1403 g_return_val_if_fail (clipboard != NULL, NULL);
1404 g_return_val_if_fail (target != GDK_NONE, NULL);
1406 results.data = NULL;
1407 results.loop = g_main_loop_new (NULL, TRUE);
1409 gtk_clipboard_request_contents (clipboard, target,
1410 clipboard_received_func,
1413 if (g_main_loop_is_running (results.loop))
1415 GDK_THREADS_LEAVE ();
1416 g_main_loop_run (results.loop);
1417 GDK_THREADS_ENTER ();
1420 g_main_loop_unref (results.loop);
1422 return results.data;
1426 clipboard_text_received_func (GtkClipboard *clipboard,
1430 WaitResults *results = data;
1432 results->data = g_strdup (text);
1433 g_main_loop_quit (results->loop);
1437 * gtk_clipboard_wait_for_text:
1438 * @clipboard: a #GtkClipboard
1440 * Requests the contents of the clipboard as text and converts
1441 * the result to UTF-8 if necessary. This function waits for
1442 * the data to be received using the main loop, so events,
1443 * timeouts, etc, may be dispatched during the wait.
1445 * Return value: a newly-allocated UTF-8 string which must
1446 * be freed with g_free(), or %NULL if retrieving
1447 * the selection data failed. (This could happen
1448 * for various reasons, in particular if the
1449 * clipboard was empty or if the contents of the
1450 * clipboard could not be converted into text form.)
1453 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1455 WaitResults results;
1457 g_return_val_if_fail (clipboard != NULL, NULL);
1459 results.data = NULL;
1460 results.loop = g_main_loop_new (NULL, TRUE);
1462 gtk_clipboard_request_text (clipboard,
1463 clipboard_text_received_func,
1466 if (g_main_loop_is_running (results.loop))
1468 GDK_THREADS_LEAVE ();
1469 g_main_loop_run (results.loop);
1470 GDK_THREADS_ENTER ();
1473 g_main_loop_unref (results.loop);
1475 return results.data;
1479 clipboard_rich_text_received_func (GtkClipboard *clipboard,
1485 WaitResults *results = data;
1487 results->data = g_memdup (text, length);
1488 results->format = format;
1489 results->length = length;
1490 g_main_loop_quit (results->loop);
1494 * gtk_clipboard_wait_for_rich_text:
1495 * @clipboard: a #GtkClipboard
1496 * @buffer: a #GtkTextBuffer
1497 * @format: (out): return location for the format of the returned data
1498 * @length: return location for the length of the returned data
1500 * Requests the contents of the clipboard as rich text. This function
1501 * waits for the data to be received using the main loop, so events,
1502 * timeouts, etc, may be dispatched during the wait.
1504 * Return value: (array length=length) (transfer full): a
1505 * newly-allocated binary block of data which must be
1506 * freed with g_free(), or %NULL if retrieving the
1507 * selection data failed. (This could happen for various
1508 * reasons, in particular if the clipboard was empty or
1509 * if the contents of the clipboard could not be
1510 * converted into text form.)
1515 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
1516 GtkTextBuffer *buffer,
1520 WaitResults results;
1522 g_return_val_if_fail (clipboard != NULL, NULL);
1523 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1524 g_return_val_if_fail (format != NULL, NULL);
1525 g_return_val_if_fail (length != NULL, NULL);
1527 results.data = NULL;
1528 results.loop = g_main_loop_new (NULL, TRUE);
1530 gtk_clipboard_request_rich_text (clipboard, buffer,
1531 clipboard_rich_text_received_func,
1534 if (g_main_loop_is_running (results.loop))
1536 GDK_THREADS_LEAVE ();
1537 g_main_loop_run (results.loop);
1538 GDK_THREADS_ENTER ();
1541 g_main_loop_unref (results.loop);
1543 *format = results.format;
1544 *length = results.length;
1546 return results.data;
1550 clipboard_image_received_func (GtkClipboard *clipboard,
1554 WaitResults *results = data;
1557 results->data = g_object_ref (pixbuf);
1559 g_main_loop_quit (results->loop);
1563 * gtk_clipboard_wait_for_image:
1564 * @clipboard: a #GtkClipboard
1566 * Requests the contents of the clipboard as image and converts
1567 * the result to a #GdkPixbuf. This function waits for
1568 * the data to be received using the main loop, so events,
1569 * timeouts, etc, may be dispatched during the wait.
1571 * Return value: (transfer full): a newly-allocated #GdkPixbuf
1572 * object which must be disposed with g_object_unref(), or
1573 * %NULL if retrieving the selection data failed. (This could
1574 * happen for various reasons, in particular if the clipboard
1575 * was empty or if the contents of the clipboard could not be
1576 * converted into an image.)
1581 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1583 WaitResults results;
1585 g_return_val_if_fail (clipboard != NULL, NULL);
1587 results.data = NULL;
1588 results.loop = g_main_loop_new (NULL, TRUE);
1590 gtk_clipboard_request_image (clipboard,
1591 clipboard_image_received_func,
1594 if (g_main_loop_is_running (results.loop))
1596 GDK_THREADS_LEAVE ();
1597 g_main_loop_run (results.loop);
1598 GDK_THREADS_ENTER ();
1601 g_main_loop_unref (results.loop);
1603 return results.data;
1607 clipboard_uris_received_func (GtkClipboard *clipboard,
1611 WaitResults *results = data;
1613 results->data = g_strdupv (uris);
1614 g_main_loop_quit (results->loop);
1618 * gtk_clipboard_wait_for_uris:
1619 * @clipboard: a #GtkClipboard
1621 * Requests the contents of the clipboard as URIs. This function waits
1622 * for the data to be received using the main loop, so events,
1623 * timeouts, etc, may be dispatched during the wait.
1625 * Return value: (array zero-terminated=1) (element-type utf8) (transfer full): a newly-allocated
1626 * %NULL-terminated array of strings which must
1627 * be freed with g_strfreev(), or %NULL if
1628 * retrieving the selection data failed. (This
1629 * could happen for various reasons, in particular
1630 * if the clipboard was empty or if the contents of
1631 * the clipboard could not be converted into URI form.)
1636 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
1638 WaitResults results;
1640 g_return_val_if_fail (clipboard != NULL, NULL);
1642 results.data = NULL;
1643 results.loop = g_main_loop_new (NULL, TRUE);
1645 gtk_clipboard_request_uris (clipboard,
1646 clipboard_uris_received_func,
1649 if (g_main_loop_is_running (results.loop))
1651 GDK_THREADS_LEAVE ();
1652 g_main_loop_run (results.loop);
1653 GDK_THREADS_ENTER ();
1656 g_main_loop_unref (results.loop);
1658 return results.data;
1662 * gtk_clipboard_get_display:
1663 * @clipboard: a #GtkClipboard
1665 * Gets the #GdkDisplay associated with @clipboard
1667 * Return value: (transfer none): the #GdkDisplay associated with @clipboard
1672 gtk_clipboard_get_display (GtkClipboard *clipboard)
1674 g_return_val_if_fail (clipboard != NULL, NULL);
1676 return clipboard->display;
1680 * gtk_clipboard_wait_is_text_available:
1681 * @clipboard: a #GtkClipboard
1683 * Test to see if there is text available to be pasted
1684 * This is done by requesting the TARGETS atom and checking
1685 * if it contains any of the supported text targets. This function
1686 * waits for the data to be received using the main loop, so events,
1687 * timeouts, etc, may be dispatched during the wait.
1689 * This function is a little faster than calling
1690 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1693 * Return value: %TRUE is there is text available, %FALSE otherwise.
1696 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1698 GtkSelectionData *data;
1699 gboolean result = FALSE;
1701 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1704 result = gtk_selection_data_targets_include_text (data);
1705 gtk_selection_data_free (data);
1712 * gtk_clipboard_wait_is_rich_text_available:
1713 * @clipboard: a #GtkClipboard
1714 * @buffer: a #GtkTextBuffer
1716 * Test to see if there is rich text available to be pasted
1717 * This is done by requesting the TARGETS atom and checking
1718 * if it contains any of the supported rich text targets. This function
1719 * waits for the data to be received using the main loop, so events,
1720 * timeouts, etc, may be dispatched during the wait.
1722 * This function is a little faster than calling
1723 * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
1726 * Return value: %TRUE is there is rich text available, %FALSE otherwise.
1731 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
1732 GtkTextBuffer *buffer)
1734 GtkSelectionData *data;
1735 gboolean result = FALSE;
1737 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1738 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1740 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1743 result = gtk_selection_data_targets_include_rich_text (data, buffer);
1744 gtk_selection_data_free (data);
1751 * gtk_clipboard_wait_is_image_available:
1752 * @clipboard: a #GtkClipboard
1754 * Test to see if there is an image available to be pasted
1755 * This is done by requesting the TARGETS atom and checking
1756 * if it contains any of the supported image targets. This function
1757 * waits for the data to be received using the main loop, so events,
1758 * timeouts, etc, may be dispatched during the wait.
1760 * This function is a little faster than calling
1761 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1762 * the actual image data.
1764 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1769 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1771 GtkSelectionData *data;
1772 gboolean result = FALSE;
1774 data = gtk_clipboard_wait_for_contents (clipboard,
1775 gdk_atom_intern_static_string ("TARGETS"));
1778 result = gtk_selection_data_targets_include_image (data, FALSE);
1779 gtk_selection_data_free (data);
1786 * gtk_clipboard_wait_is_uris_available:
1787 * @clipboard: a #GtkClipboard
1789 * Test to see if there is a list of URIs available to be pasted
1790 * This is done by requesting the TARGETS atom and checking
1791 * if it contains the URI targets. This function
1792 * waits for the data to be received using the main loop, so events,
1793 * timeouts, etc, may be dispatched during the wait.
1795 * This function is a little faster than calling
1796 * gtk_clipboard_wait_for_uris() since it doesn't need to retrieve
1797 * the actual URI data.
1799 * Return value: %TRUE is there is an URI list available, %FALSE otherwise.
1804 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
1806 GtkSelectionData *data;
1807 gboolean result = FALSE;
1809 data = gtk_clipboard_wait_for_contents (clipboard,
1810 gdk_atom_intern_static_string ("TARGETS"));
1813 result = gtk_selection_data_targets_include_uri (data);
1814 gtk_selection_data_free (data);
1821 * gtk_clipboard_wait_for_targets
1822 * @clipboard: a #GtkClipboard
1823 * @targets: (out) (array length=n_targets) (transfer container): location
1824 * to store an array of targets. The result stored here must
1825 * be freed with g_free().
1826 * @n_targets: location to store number of items in @targets.
1828 * Returns a list of targets that are present on the clipboard, or %NULL
1829 * if there aren't any targets available. The returned list must be
1830 * freed with g_free().
1831 * This function waits for the data to be received using the main
1832 * loop, so events, timeouts, etc, may be dispatched during the wait.
1834 * Return value: %TRUE if any targets are present on the clipboard,
1840 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1844 GtkSelectionData *data;
1845 gboolean result = FALSE;
1847 g_return_val_if_fail (clipboard != NULL, FALSE);
1849 /* If the display supports change notification we cache targets */
1850 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1851 clipboard->n_cached_targets != -1)
1854 *n_targets = clipboard->n_cached_targets;
1857 *targets = g_memdup (clipboard->cached_targets,
1858 clipboard->n_cached_targets * sizeof (GdkAtom));
1869 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1873 GdkAtom *tmp_targets;
1876 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1878 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1880 clipboard->n_cached_targets = tmp_n_targets;
1881 clipboard->cached_targets = g_memdup (tmp_targets,
1882 tmp_n_targets * sizeof (GdkAtom));
1886 *n_targets = tmp_n_targets;
1889 *targets = tmp_targets;
1891 g_free (tmp_targets);
1893 gtk_selection_data_free (data);
1899 static GtkClipboard *
1900 clipboard_peek (GdkDisplay *display,
1902 gboolean only_if_exists)
1904 GtkClipboard *clipboard = NULL;
1908 if (selection == GDK_NONE)
1909 selection = GDK_SELECTION_CLIPBOARD;
1911 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1913 tmp_list = clipboards;
1916 clipboard = tmp_list->data;
1917 if (clipboard->selection == selection)
1920 tmp_list = tmp_list->next;
1923 if (!tmp_list && !only_if_exists)
1925 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1926 clipboard->selection = selection;
1927 clipboard->display = display;
1928 clipboard->n_cached_targets = -1;
1929 clipboard->n_storable_targets = -1;
1930 clipboards = g_slist_prepend (clipboards, clipboard);
1931 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1932 g_signal_connect (display, "closed",
1933 G_CALLBACK (clipboard_display_closed), clipboard);
1934 gdk_display_request_selection_notification (display, selection);
1941 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1942 GdkEventOwnerChange *event)
1944 if (clipboard->n_cached_targets != -1)
1946 g_free (clipboard->cached_targets);
1947 clipboard->cached_targets = NULL;
1948 clipboard->n_cached_targets = -1;
1953 * gtk_clipboard_wait_is_target_available:
1954 * @clipboard: a #GtkClipboard
1955 * @target: A #GdkAtom indicating which target to look for.
1957 * Checks if a clipboard supports pasting data of a given type. This
1958 * function can be used to determine if a "Paste" menu item should be
1959 * insensitive or not.
1961 * If you want to see if there's text available on the clipboard, use
1962 * gtk_clipboard_wait_is_text_available () instead.
1964 * Return value: %TRUE if the target is available, %FALSE otherwise.
1969 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1974 gboolean retval = FALSE;
1976 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1979 for (i = 0; i < n_targets; i++)
1981 if (targets[i] == target)
1994 * _gtk_clipboard_handle_event:
1995 * @event: a owner change event
1997 * Emits the #GtkClipboard::owner-change signal on the appropriate @clipboard.
2002 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
2004 GdkDisplay *display;
2005 GtkClipboard *clipboard;
2007 display = gdk_window_get_display (event->window);
2008 clipboard = clipboard_peek (display, event->selection, TRUE);
2011 g_signal_emit (clipboard,
2012 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
2016 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
2018 g_main_loop_quit (clipboard->store_loop);
2024 * gtk_clipboard_set_can_store:
2025 * @clipboard: a #GtkClipboard
2026 * @targets: (allow-none) (array length=n_targets): array containing
2027 * information about which forms should be stored or %NULL
2028 * to indicate that all forms should be stored.
2029 * @n_targets: number of elements in @targets
2031 * Hints that the clipboard data should be stored somewhere when the
2032 * application exits or when gtk_clipboard_store () is called.
2034 * This value is reset when the clipboard owner changes.
2035 * Where the clipboard data is stored is platform dependent,
2036 * see gdk_display_store_clipboard () for more information.
2041 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
2042 const GtkTargetEntry *targets,
2045 GtkWidget *clipboard_widget;
2047 static const GtkTargetEntry save_targets[] = {
2048 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
2051 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2052 g_return_if_fail (n_targets >= 0);
2054 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
2057 g_free (clipboard->storable_targets);
2059 clipboard_widget = get_clipboard_widget (clipboard->display);
2061 /* n_storable_targets being -1 means that
2062 * gtk_clipboard_set_can_store hasn't been called since the
2063 * clipboard owner changed. We only want to add SAVE_TARGETS and
2064 * ref the owner once , so we do that here
2066 if (clipboard->n_storable_targets == -1)
2068 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
2071 /* Ref the owner so it won't go away */
2072 if (clipboard->have_owner)
2073 g_object_ref (clipboard->user_data);
2076 clipboard->n_storable_targets = n_targets;
2077 clipboard->storable_targets = g_new (GdkAtom, n_targets);
2078 for (i = 0; i < n_targets; i++)
2079 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
2083 gtk_clipboard_selection_notify (GtkWidget *widget,
2084 GdkEventSelection *event,
2085 GtkClipboard *clipboard)
2087 if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
2088 clipboard->storing_selection)
2089 g_main_loop_quit (clipboard->store_loop);
2095 * gtk_clipboard_store:
2096 * @clipboard: a #GtkClipboard
2098 * Stores the current clipboard data somewhere so that it will stay
2099 * around after the application has quit.
2104 gtk_clipboard_store (GtkClipboard *clipboard)
2106 GtkWidget *clipboard_widget;
2108 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2110 if (clipboard->n_storable_targets < 0)
2113 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
2116 g_object_ref (clipboard);
2118 clipboard_widget = get_clipboard_widget (clipboard->display);
2119 clipboard->notify_signal_id = g_signal_connect (clipboard_widget,
2120 "selection-notify-event",
2121 G_CALLBACK (gtk_clipboard_selection_notify),
2124 gdk_display_store_clipboard (clipboard->display,
2125 gtk_widget_get_window (clipboard_widget),
2126 clipboard_get_timestamp (clipboard),
2127 clipboard->storable_targets,
2128 clipboard->n_storable_targets);
2130 clipboard->storing_selection = TRUE;
2132 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
2133 clipboard->store_timeout = g_timeout_add_seconds (10, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
2135 if (g_main_loop_is_running (clipboard->store_loop))
2137 GDK_THREADS_LEAVE ();
2138 g_main_loop_run (clipboard->store_loop);
2139 GDK_THREADS_ENTER ();
2142 g_main_loop_unref (clipboard->store_loop);
2143 clipboard->store_loop = NULL;
2145 g_source_remove (clipboard->store_timeout);
2146 clipboard->store_timeout = 0;
2147 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
2148 clipboard->notify_signal_id = 0;
2150 clipboard->storing_selection = FALSE;
2152 g_object_unref (clipboard);
2155 /* Stores all clipboard selections on all displays, called from
2159 _gtk_clipboard_store_all (void)
2161 GtkClipboard *clipboard;
2162 GSList *displays, *list;
2164 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
2169 GdkDisplay *display = list->data;
2171 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
2174 gtk_clipboard_store (clipboard);
2178 g_slist_free (displays);