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, see <http://www.gnu.org/licenses/>.
18 * Global clipboard abstraction.
24 #include "gtkclipboard.h"
25 #include "gtkinvisible.h"
27 #include "gtkmarshalers.h"
28 #include "gtktextbufferrichtext.h"
31 #ifdef GDK_WINDOWING_X11
35 #ifdef GDK_WINDOWING_BROADWAY
36 #include "broadway/gdkbroadway.h"
39 #ifdef GDK_WINDOWING_WIN32
40 #include "win32/gdkwin32.h"
45 * SECTION:gtkclipboard
46 * @Short_description: Storing data on clipboards
48 * @See_also: #GtkSelection
50 * The #GtkClipboard object represents a clipboard of data shared
51 * between different processes or between different widgets in
52 * the same process. Each clipboard is identified by a name encoded as a
53 * #GdkAtom. (Conversion to and from strings can be done with
54 * gdk_atom_intern() and gdk_atom_name().) The default clipboard
55 * corresponds to the "CLIPBOARD" atom; another commonly used clipboard
56 * is the "PRIMARY" clipboard, which, in X, traditionally contains
57 * the currently selected text.
59 * To support having a number of different formats on the clipboard
60 * at the same time, the clipboard mechanism allows providing
61 * callbacks instead of the actual data. When you set the contents
62 * of the clipboard, you can either supply the data directly (via
63 * functions like gtk_clipboard_set_text()), or you can supply a
64 * callback to be called at a later time when the data is needed (via
65 * gtk_clipboard_set_with_data() or gtk_clipboard_set_with_owner().)
66 * Providing a callback also avoids having to make copies of the data
67 * when it is not needed.
69 * gtk_clipboard_set_with_data() and gtk_clipboard_set_with_owner()
70 * are quite similar; the choice between the two depends mostly on
71 * which is more convenient in a particular situation.
72 * The former is most useful when you want to have a blob of data
73 * with callbacks to convert it into the various data types that you
74 * advertise. When the @clear_func you provided is called, you
75 * simply free the data blob. The latter is more useful when the
76 * contents of clipboard reflect the internal state of a #GObject
77 * (As an example, for the PRIMARY clipboard, when an entry widget
78 * provides the clipboard's contents the contents are simply the
79 * text within the selected region.) If the contents change, the
80 * entry widget can call gtk_clipboard_set_with_owner() to update
81 * the timestamp for clipboard ownership, without having to worry
82 * about @clear_func being called.
84 * Requesting the data from the clipboard is essentially
85 * asynchronous. If the contents of the clipboard are provided within
86 * the same process, then a direct function call will be made to
87 * retrieve the data, but if they are provided by another process,
88 * then the data needs to be retrieved from the other process, which
89 * may take some time. To avoid blocking the user interface, the call
90 * to request the selection, gtk_clipboard_request_contents() takes a
91 * callback that will be called when the contents are received (or
92 * when the request fails.) If you don't want to deal with providing
93 * a separate callback, you can also use gtk_clipboard_wait_for_contents().
94 * What this does is run the GLib main loop recursively waiting for
95 * the contents. This can simplify the code flow, but you still have
96 * to be aware that other callbacks in your program can be called
97 * while this recursive mainloop is running.
99 * Along with the functions to get the clipboard contents as an
100 * arbitrary data chunk, there are also functions to retrieve
101 * it as text, gtk_clipboard_request_text() and
102 * gtk_clipboard_wait_for_text(). These functions take care of
103 * determining which formats are advertised by the clipboard
104 * provider, asking for the clipboard in the best available format
105 * and converting the results into the UTF-8 encoding. (The standard
106 * form for representing strings in GTK+.)
115 typedef struct _GtkClipboardClass GtkClipboardClass;
117 typedef struct _RequestContentsInfo RequestContentsInfo;
118 typedef struct _RequestTextInfo RequestTextInfo;
119 typedef struct _RequestRichTextInfo RequestRichTextInfo;
120 typedef struct _RequestImageInfo RequestImageInfo;
121 typedef struct _RequestURIInfo RequestURIInfo;
122 typedef struct _RequestTargetsInfo RequestTargetsInfo;
126 GObject parent_instance;
130 GtkClipboardGetFunc get_func;
131 GtkClipboardClearFunc clear_func;
137 gboolean have_selection;
140 GdkAtom *cached_targets;
141 gint n_cached_targets;
143 gulong notify_signal_id;
144 gboolean storing_selection;
145 GMainLoop *store_loop;
147 gint n_storable_targets;
148 GdkAtom *storable_targets;
151 struct _GtkClipboardClass
153 GObjectClass parent_class;
155 void (*owner_change) (GtkClipboard *clipboard,
156 GdkEventOwnerChange *event);
159 struct _RequestContentsInfo
161 GtkClipboardReceivedFunc callback;
165 struct _RequestTextInfo
167 GtkClipboardTextReceivedFunc callback;
171 struct _RequestRichTextInfo
173 GtkClipboardRichTextReceivedFunc callback;
180 struct _RequestImageInfo
182 GtkClipboardImageReceivedFunc callback;
186 struct _RequestURIInfo
188 GtkClipboardURIReceivedFunc callback;
192 struct _RequestTargetsInfo
194 GtkClipboardTargetsReceivedFunc callback;
198 static void gtk_clipboard_class_init (GtkClipboardClass *class);
199 static void gtk_clipboard_finalize (GObject *object);
200 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
201 GdkEventOwnerChange *event);
203 static void clipboard_unset (GtkClipboard *clipboard);
204 static void selection_received (GtkWidget *widget,
205 GtkSelectionData *selection_data,
207 static GtkClipboard *clipboard_peek (GdkDisplay *display,
209 gboolean only_if_exists);
210 static GtkWidget * get_clipboard_widget (GdkDisplay *display);
216 TARGET_COMPOUND_TEXT,
221 static const gchar request_contents_key[] = "gtk-request-contents";
222 static GQuark request_contents_key_id = 0;
224 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
225 static GQuark clipboards_owned_key_id = 0;
227 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
229 G_DEFINE_TYPE (GtkClipboard, gtk_clipboard, G_TYPE_OBJECT)
232 gtk_clipboard_init (GtkClipboard *object)
237 gtk_clipboard_class_init (GtkClipboardClass *class)
239 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
241 gobject_class->finalize = gtk_clipboard_finalize;
243 class->owner_change = gtk_clipboard_owner_change;
246 * GtkClipboard::owner-change:
247 * @clipboard: the #GtkClipboard on which the signal is emitted
248 * @event: (type Gdk.EventOwnerChange): the @GdkEventOwnerChange event
250 * The ::owner-change signal is emitted when GTK+ receives an
251 * event that indicates that the ownership of the selection
252 * associated with @clipboard has changed.
256 clipboard_signals[OWNER_CHANGE] =
257 g_signal_new (I_("owner-change"),
258 G_TYPE_FROM_CLASS (gobject_class),
260 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
262 _gtk_marshal_VOID__BOXED,
264 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
268 gtk_clipboard_finalize (GObject *object)
270 GtkClipboard *clipboard;
271 GtkWidget *clipboard_widget = NULL;
272 GSList *clipboards = NULL;
274 clipboard = GTK_CLIPBOARD (object);
276 if (clipboard->display)
278 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
280 if (g_slist_index (clipboards, clipboard) >= 0)
281 g_warning ("GtkClipboard prematurely finalized");
283 clipboards = g_slist_remove (clipboards, clipboard);
285 g_object_set_data (G_OBJECT (clipboard->display), "gtk-clipboard-list",
288 /* don't use get_clipboard_widget() here because it would create the
289 * widget if it doesn't exist.
291 clipboard_widget = g_object_get_data (G_OBJECT (clipboard->display),
292 "gtk-clipboard-widget");
295 clipboard_unset (clipboard);
297 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
298 g_main_loop_quit (clipboard->store_loop);
300 if (clipboard->store_timeout != 0)
301 g_source_remove (clipboard->store_timeout);
303 if (clipboard->notify_signal_id != 0)
304 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
306 g_free (clipboard->storable_targets);
307 g_free (clipboard->cached_targets);
309 G_OBJECT_CLASS (gtk_clipboard_parent_class)->finalize (object);
313 clipboard_display_closed (GdkDisplay *display,
315 GtkClipboard *clipboard)
319 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
320 g_object_run_dispose (G_OBJECT (clipboard));
321 clipboards = g_slist_remove (clipboards, clipboard);
322 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
323 g_object_unref (clipboard);
327 * gtk_clipboard_get_for_display:
328 * @display: the display for which the clipboard is to be retrieved or created
329 * @selection: a #GdkAtom which identifies the clipboard to use.
331 * Returns the clipboard object for the given selection.
332 * Cut/copy/paste menu items and keyboard shortcuts should use
333 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
334 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
335 * for backwards compatibility reasons.)
336 * The currently-selected object or text should be provided on the clipboard
337 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
338 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
339 * to the default clipboard, i.e. they copy the selection to what the
340 * user sees as the clipboard.
342 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
343 * ("CLIPBOARD", FALSE)</literal>. See <ulink
344 * url="http://www.freedesktop.org/Standards/clipboards-spec">
345 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
346 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
347 * selections under the X window system. On Win32 the
348 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
350 * It's possible to have arbitrary named clipboards; if you do invent
351 * new clipboards, you should prefix the selection name with an
352 * underscore (because the ICCCM requires that nonstandard atoms are
353 * underscore-prefixed), and namespace it as well. For example,
354 * if your application called "Foo" has a special-purpose
355 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
357 * Return value: (transfer none): the appropriate clipboard object. If no
358 * clipboard already exists, a new one will be created. Once a clipboard
359 * object has been created, it is persistent and, since it is owned by
360 * GTK+, must not be freed or unrefd.
365 gtk_clipboard_get_for_display (GdkDisplay *display,
368 g_return_val_if_fail (display != NULL, NULL); /* See bgo#463773; this is needed because Flash Player sucks */
369 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
370 g_return_val_if_fail (!gdk_display_is_closed (display), NULL);
372 return clipboard_peek (display, selection, FALSE);
378 * @selection: a #GdkAtom which identifies the clipboard to use
380 * Returns the clipboard object for the given selection.
381 * See gtk_clipboard_get_for_display() for complete details.
383 * Return value: (transfer none): the appropriate clipboard object. If no clipboard
384 * already exists, a new one will be created. Once a clipboard
385 * object has been created, it is persistent and, since it is
386 * owned by GTK+, must not be freed or unreffed.
389 gtk_clipboard_get (GdkAtom selection)
391 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
395 selection_get_cb (GtkWidget *widget,
396 GtkSelectionData *selection_data,
400 GtkClipboard *clipboard;
402 clipboard = gtk_widget_get_clipboard (widget,
403 gtk_selection_data_get_selection (selection_data));
405 if (clipboard && clipboard->get_func)
406 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
410 selection_clear_event_cb (GtkWidget *widget,
411 GdkEventSelection *event)
413 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
417 clipboard_unset (clipboard);
426 make_clipboard_widget (GdkDisplay *display,
429 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
431 g_signal_connect (widget, "selection-received",
432 G_CALLBACK (selection_received), NULL);
436 /* We need this for gdk_x11_get_server_time() */
437 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
439 g_signal_connect (widget, "selection-get",
440 G_CALLBACK (selection_get_cb), NULL);
441 g_signal_connect (widget, "selection-clear-event",
442 G_CALLBACK (selection_clear_event_cb), NULL);
449 get_clipboard_widget (GdkDisplay *display)
451 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
454 clip_widget = make_clipboard_widget (display, TRUE);
455 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
461 /* This function makes a very good guess at what the correct
462 * timestamp for a selection request should be. If there is
463 * a currently processed event, it uses the timestamp for that
464 * event, otherwise it uses the current server time. However,
465 * if the time resulting from that is older than the time used
466 * last time, it uses the time used last time instead.
468 * In order implement this correctly, we never use CurrentTime,
469 * but actually retrieve the actual timestamp from the server.
470 * This is a little slower but allows us to make the guarantee
471 * that the times used by this application will always ascend
472 * and we won't get selections being rejected just because
473 * we are using a correct timestamp from an event, but used
474 * CurrentTime previously.
477 clipboard_get_timestamp (GtkClipboard *clipboard)
479 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
480 guint32 timestamp = gtk_get_current_event_time ();
483 if (timestamp == GDK_CURRENT_TIME)
485 window = gtk_widget_get_window (clipboard_widget);
486 #ifdef GDK_WINDOWING_X11
487 if (GDK_IS_X11_WINDOW (window))
489 timestamp = gdk_x11_get_server_time (gtk_widget_get_window (clipboard_widget));
493 #if defined GDK_WINDOWING_WIN32
494 if (GDK_IS_WIN32_WINDOW (window))
496 timestamp = GetMessageTime ();
500 #if defined GDK_WINDOWING_BROADWAY
501 if (GDK_IS_BROADWAY_WINDOW (window))
503 timestamp = gdk_broadway_get_last_seen_time (window);
508 /* No implementation */
513 if (clipboard->timestamp != GDK_CURRENT_TIME)
515 /* Check to see if clipboard->timestamp is newer than
516 * timestamp, accounting for wraparound.
519 guint32 max = timestamp + 0x80000000;
521 if ((max > timestamp &&
522 (clipboard->timestamp > timestamp &&
523 clipboard->timestamp <= max)) ||
525 (clipboard->timestamp > timestamp ||
526 clipboard->timestamp <= max)))
528 timestamp = clipboard->timestamp;
533 clipboard->timestamp = timestamp;
539 clipboard_owner_destroyed (gpointer data)
541 GSList *clipboards = data;
544 tmp_list = clipboards;
547 GtkClipboard *clipboard = tmp_list->data;
549 clipboard->get_func = NULL;
550 clipboard->clear_func = NULL;
551 clipboard->user_data = NULL;
552 clipboard->have_owner = FALSE;
554 gtk_clipboard_clear (clipboard);
556 tmp_list = tmp_list->next;
559 g_slist_free (clipboards);
563 clipboard_add_owner_notify (GtkClipboard *clipboard)
565 if (!clipboards_owned_key_id)
566 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
568 if (clipboard->have_owner)
569 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
570 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
571 clipboards_owned_key_id),
573 clipboard_owner_destroyed);
577 clipboard_remove_owner_notify (GtkClipboard *clipboard)
579 if (clipboard->have_owner)
580 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
581 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
582 clipboards_owned_key_id),
584 clipboard_owner_destroyed);
588 gtk_clipboard_set_contents (GtkClipboard *clipboard,
589 const GtkTargetEntry *targets,
591 GtkClipboardGetFunc get_func,
592 GtkClipboardClearFunc clear_func,
596 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
598 if (gtk_selection_owner_set_for_display (clipboard->display,
600 clipboard->selection,
601 clipboard_get_timestamp (clipboard)))
603 clipboard->have_selection = TRUE;
605 if (clipboard->n_cached_targets != -1)
607 g_free (clipboard->cached_targets);
608 clipboard->cached_targets = NULL;
609 clipboard->n_cached_targets = -1;
612 if (!(clipboard->have_owner && have_owner) ||
613 clipboard->user_data != user_data)
615 clipboard_unset (clipboard);
617 clipboard->user_data = user_data;
618 clipboard->have_owner = have_owner;
620 clipboard_add_owner_notify (clipboard);
623 clipboard->get_func = get_func;
624 clipboard->clear_func = clear_func;
626 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
627 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
637 * gtk_clipboard_set_with_data: (skip)
638 * @clipboard: a #GtkClipboard
639 * @targets: (array length=n_targets): array containing information
640 * about the available forms for the clipboard data
641 * @n_targets: number of elements in @targets
642 * @get_func: (scope async): function to call to get the actual clipboard data
643 * @clear_func: (scope async): when the clipboard contents are set again,
644 * this function will be called, and @get_func will not be subsequently
646 * @user_data: user data to pass to @get_func and @clear_func.
648 * Virtually sets the contents of the specified clipboard by providing
649 * a list of supported formats for the clipboard data and a function
650 * to call to get the actual data when it is requested.
652 * Return value: %TRUE if setting the clipboard data succeeded.
653 * If setting the clipboard data failed the provided callback
654 * functions will be ignored.
657 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
658 const GtkTargetEntry *targets,
660 GtkClipboardGetFunc get_func,
661 GtkClipboardClearFunc clear_func,
664 g_return_val_if_fail (clipboard != NULL, FALSE);
665 g_return_val_if_fail (targets != NULL, FALSE);
666 g_return_val_if_fail (get_func != NULL, FALSE);
668 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
669 get_func, clear_func, user_data,
674 * gtk_clipboard_set_with_owner: (skip)
675 * @clipboard: a #GtkClipboard
676 * @targets: (array length=n_targets): array containing information
677 * about the available forms for the clipboard data
678 * @n_targets: number of elements in @targets
679 * @get_func: (scope async): function to call to get the actual clipboard data
680 * @clear_func: (scope async): when the clipboard contents are set again,
681 * this function will be called, and @get_func will not be subsequently
683 * @owner: an object that "owns" the data. This object will be passed
684 * to the callbacks when called
686 * Virtually sets the contents of the specified clipboard by providing
687 * a list of supported formats for the clipboard data and a function
688 * to call to get the actual data when it is requested.
690 * The difference between this function and gtk_clipboard_set_with_data()
691 * is that instead of an generic @user_data pointer, a #GObject is passed
694 * Return value: %TRUE if setting the clipboard data succeeded.
695 * If setting the clipboard data failed the provided callback
696 * functions will be ignored.
699 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
700 const GtkTargetEntry *targets,
702 GtkClipboardGetFunc get_func,
703 GtkClipboardClearFunc clear_func,
706 g_return_val_if_fail (clipboard != NULL, FALSE);
707 g_return_val_if_fail (targets != NULL, FALSE);
708 g_return_val_if_fail (get_func != NULL, FALSE);
709 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
711 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
712 get_func, clear_func, owner,
717 * gtk_clipboard_get_owner:
718 * @clipboard: a #GtkClipboard
720 * If the clipboard contents callbacks were set with
721 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
722 * gtk_clipboard_clear() has not subsequently called, returns the owner set
723 * by gtk_clipboard_set_with_owner().
725 * Return value: (transfer none): the owner of the clipboard, if any;
729 gtk_clipboard_get_owner (GtkClipboard *clipboard)
731 g_return_val_if_fail (clipboard != NULL, NULL);
733 if (clipboard->have_owner)
734 return clipboard->user_data;
740 clipboard_unset (GtkClipboard *clipboard)
742 GtkClipboardClearFunc old_clear_func;
744 gboolean old_have_owner;
745 gint old_n_storable_targets;
747 old_clear_func = clipboard->clear_func;
748 old_data = clipboard->user_data;
749 old_have_owner = clipboard->have_owner;
750 old_n_storable_targets = clipboard->n_storable_targets;
754 clipboard_remove_owner_notify (clipboard);
755 clipboard->have_owner = FALSE;
758 clipboard->n_storable_targets = -1;
759 g_free (clipboard->storable_targets);
760 clipboard->storable_targets = NULL;
762 clipboard->get_func = NULL;
763 clipboard->clear_func = NULL;
764 clipboard->user_data = NULL;
767 old_clear_func (clipboard, old_data);
769 /* If we've transferred the clipboard data to the manager,
772 if (old_have_owner &&
773 old_n_storable_targets != -1)
774 g_object_unref (old_data);
778 * gtk_clipboard_clear:
779 * @clipboard: a #GtkClipboard
781 * Clears the contents of the clipboard. Generally this should only
782 * be called between the time you call gtk_clipboard_set_with_owner()
783 * or gtk_clipboard_set_with_data(),
784 * and when the @clear_func you supplied is called. Otherwise, the
785 * clipboard may be owned by someone else.
788 gtk_clipboard_clear (GtkClipboard *clipboard)
790 g_return_if_fail (clipboard != NULL);
792 if (clipboard->have_selection)
793 gtk_selection_owner_set_for_display (clipboard->display,
795 clipboard->selection,
796 clipboard_get_timestamp (clipboard));
800 text_get_func (GtkClipboard *clipboard,
801 GtkSelectionData *selection_data,
805 gtk_selection_data_set_text (selection_data, data, -1);
809 text_clear_func (GtkClipboard *clipboard,
817 * gtk_clipboard_set_text:
818 * @clipboard: a #GtkClipboard object
819 * @text: a UTF-8 string.
820 * @len: length of @text, in bytes, or -1, in which case
821 * the length will be determined with <function>strlen()</function>.
823 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
824 * make a copy of the text and take responsibility for responding
825 * for requests for the text, and for converting the text into
826 * the requested format.
829 gtk_clipboard_set_text (GtkClipboard *clipboard,
834 GtkTargetEntry *targets;
837 g_return_if_fail (clipboard != NULL);
838 g_return_if_fail (text != NULL);
840 list = gtk_target_list_new (NULL, 0);
841 gtk_target_list_add_text_targets (list, 0);
843 targets = gtk_target_table_new_from_list (list, &n_targets);
848 gtk_clipboard_set_with_data (clipboard,
850 text_get_func, text_clear_func,
851 g_strndup (text, len));
852 gtk_clipboard_set_can_store (clipboard, NULL, 0);
854 gtk_target_table_free (targets, n_targets);
855 gtk_target_list_unref (list);
859 pixbuf_get_func (GtkClipboard *clipboard,
860 GtkSelectionData *selection_data,
864 gtk_selection_data_set_pixbuf (selection_data, data);
868 pixbuf_clear_func (GtkClipboard *clipboard,
871 g_object_unref (data);
875 * gtk_clipboard_set_image:
876 * @clipboard: a #GtkClipboard object
877 * @pixbuf: a #GdkPixbuf
879 * Sets the contents of the clipboard to the given #GdkPixbuf.
880 * GTK+ will take responsibility for responding for requests
881 * for the image, and for converting the image into the
887 gtk_clipboard_set_image (GtkClipboard *clipboard,
891 GtkTargetEntry *targets;
894 g_return_if_fail (clipboard != NULL);
895 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
897 list = gtk_target_list_new (NULL, 0);
898 gtk_target_list_add_image_targets (list, 0, TRUE);
900 targets = gtk_target_table_new_from_list (list, &n_targets);
902 gtk_clipboard_set_with_data (clipboard,
904 pixbuf_get_func, pixbuf_clear_func,
905 g_object_ref (pixbuf));
906 gtk_clipboard_set_can_store (clipboard, NULL, 0);
908 gtk_target_table_free (targets, n_targets);
909 gtk_target_list_unref (list);
913 set_request_contents_info (GtkWidget *widget,
914 RequestContentsInfo *info)
916 if (!request_contents_key_id)
917 request_contents_key_id = g_quark_from_static_string (request_contents_key);
919 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
922 static RequestContentsInfo *
923 get_request_contents_info (GtkWidget *widget)
925 if (!request_contents_key_id)
928 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
932 selection_received (GtkWidget *widget,
933 GtkSelectionData *selection_data,
936 RequestContentsInfo *request_info = get_request_contents_info (widget);
937 set_request_contents_info (widget, NULL);
939 request_info->callback (gtk_widget_get_clipboard (widget, gtk_selection_data_get_selection (selection_data)),
941 request_info->user_data);
943 g_free (request_info);
945 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
946 gtk_widget_destroy (widget);
950 * gtk_clipboard_request_contents:
951 * @clipboard: a #GtkClipboard
952 * @target: an atom representing the form into which the clipboard
953 * owner should convert the selection.
954 * @callback: (scope async): A function to call when the results are received
955 * (or the retrieval fails). If the retrieval fails the length field of
956 * @selection_data will be negative.
957 * @user_data: user data to pass to @callback
959 * Requests the contents of clipboard as the given target.
960 * When the results of the result are later received the supplied callback
964 gtk_clipboard_request_contents (GtkClipboard *clipboard,
966 GtkClipboardReceivedFunc callback,
969 RequestContentsInfo *info;
971 GtkWidget *clipboard_widget;
973 g_return_if_fail (clipboard != NULL);
974 g_return_if_fail (target != GDK_NONE);
975 g_return_if_fail (callback != NULL);
977 clipboard_widget = get_clipboard_widget (clipboard->display);
979 if (get_request_contents_info (clipboard_widget))
980 widget = make_clipboard_widget (clipboard->display, FALSE);
982 widget = clipboard_widget;
984 info = g_new (RequestContentsInfo, 1);
985 info->callback = callback;
986 info->user_data = user_data;
988 set_request_contents_info (widget, info);
990 gtk_selection_convert (widget, clipboard->selection, target,
991 clipboard_get_timestamp (clipboard));
995 request_text_received_func (GtkClipboard *clipboard,
996 GtkSelectionData *selection_data,
999 RequestTextInfo *info = data;
1000 gchar *result = NULL;
1002 result = (gchar *) gtk_selection_data_get_text (selection_data);
1006 /* If we asked for UTF8 and didn't get it, try compound_text;
1007 * if we asked for compound_text and didn't get it, try string;
1008 * If we asked for anything else and didn't get it, give up.
1010 GdkAtom target = gtk_selection_data_get_target (selection_data);
1012 if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
1014 gtk_clipboard_request_contents (clipboard,
1015 gdk_atom_intern_static_string ("COMPOUND_TEXT"),
1016 request_text_received_func, info);
1019 else if (target == gdk_atom_intern_static_string ("COMPOUND_TEXT"))
1021 gtk_clipboard_request_contents (clipboard,
1023 request_text_received_func, info);
1028 info->callback (clipboard, result, info->user_data);
1034 * gtk_clipboard_request_text:
1035 * @clipboard: a #GtkClipboard
1036 * @callback: (scope async): a function to call when the text is received,
1037 * or the retrieval fails. (It will always be called one way or the other.)
1038 * @user_data: user data to pass to @callback.
1040 * Requests the contents of the clipboard as text. When the text is
1041 * later received, it will be converted to UTF-8 if necessary, and
1042 * @callback will be called.
1044 * The @text parameter to @callback will contain the resulting text if
1045 * the request succeeded, or %NULL if it failed. This could happen for
1046 * various reasons, in particular if the clipboard was empty or if the
1047 * contents of the clipboard could not be converted into text form.
1050 gtk_clipboard_request_text (GtkClipboard *clipboard,
1051 GtkClipboardTextReceivedFunc callback,
1054 RequestTextInfo *info;
1056 g_return_if_fail (clipboard != NULL);
1057 g_return_if_fail (callback != NULL);
1059 info = g_new (RequestTextInfo, 1);
1060 info->callback = callback;
1061 info->user_data = user_data;
1063 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("UTF8_STRING"),
1064 request_text_received_func,
1069 request_rich_text_received_func (GtkClipboard *clipboard,
1070 GtkSelectionData *selection_data,
1073 RequestRichTextInfo *info = data;
1074 guint8 *result = NULL;
1077 result = (guint8 *) gtk_selection_data_get_data (selection_data);
1078 length = gtk_selection_data_get_length (selection_data);
1080 info->current_atom++;
1082 if ((!result || length < 1) && (info->current_atom < info->n_atoms))
1084 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1085 request_rich_text_received_func,
1090 info->callback (clipboard, gtk_selection_data_get_target (selection_data),
1093 g_free (info->atoms);
1098 * gtk_clipboard_request_rich_text:
1099 * @clipboard: a #GtkClipboard
1100 * @buffer: a #GtkTextBuffer
1101 * @callback: (scope async): a function to call when the text is received,
1102 * or the retrieval fails. (It will always be called one way or the other.)
1103 * @user_data: user data to pass to @callback.
1105 * Requests the contents of the clipboard as rich text. When the rich
1106 * text is later received, @callback will be called.
1108 * The @text parameter to @callback will contain the resulting rich
1109 * text if the request succeeded, or %NULL if it failed. The @length
1110 * parameter will contain @text's length. This function can fail for
1111 * various reasons, in particular if the clipboard was empty or if the
1112 * contents of the clipboard could not be converted into rich text form.
1117 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
1118 GtkTextBuffer *buffer,
1119 GtkClipboardRichTextReceivedFunc callback,
1122 RequestRichTextInfo *info;
1124 g_return_if_fail (clipboard != NULL);
1125 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1126 g_return_if_fail (callback != NULL);
1128 info = g_new (RequestRichTextInfo, 1);
1129 info->callback = callback;
1132 info->current_atom = 0;
1133 info->user_data = user_data;
1135 info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
1137 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1138 request_rich_text_received_func,
1143 request_image_received_func (GtkClipboard *clipboard,
1144 GtkSelectionData *selection_data,
1147 RequestImageInfo *info = data;
1148 GdkPixbuf *result = NULL;
1150 result = gtk_selection_data_get_pixbuf (selection_data);
1154 /* If we asked for image/png and didn't get it, try image/jpeg;
1155 * if we asked for image/jpeg and didn't get it, try image/gif;
1156 * if we asked for image/gif and didn't get it, try image/bmp;
1157 * If we asked for anything else and didn't get it, give up.
1159 GdkAtom target = gtk_selection_data_get_target (selection_data);
1161 if (target == gdk_atom_intern_static_string ("image/png"))
1163 gtk_clipboard_request_contents (clipboard,
1164 gdk_atom_intern_static_string ("image/jpeg"),
1165 request_image_received_func, info);
1168 else if (target == gdk_atom_intern_static_string ("image/jpeg"))
1170 gtk_clipboard_request_contents (clipboard,
1171 gdk_atom_intern_static_string ("image/gif"),
1172 request_image_received_func, info);
1175 else if (target == gdk_atom_intern_static_string ("image/gif"))
1177 gtk_clipboard_request_contents (clipboard,
1178 gdk_atom_intern_static_string ("image/bmp"),
1179 request_image_received_func, info);
1184 info->callback (clipboard, result, info->user_data);
1188 g_object_unref (result);
1192 * gtk_clipboard_request_image:
1193 * @clipboard: a #GtkClipboard
1194 * @callback: (scope async): a function to call when the image is received,
1195 * or the retrieval fails. (It will always be called one way or the other.)
1196 * @user_data: user data to pass to @callback.
1198 * Requests the contents of the clipboard as image. When the image is
1199 * later received, it will be converted to a #GdkPixbuf, and
1200 * @callback will be called.
1202 * The @pixbuf parameter to @callback will contain the resulting
1203 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1204 * could happen for various reasons, in particular if the clipboard
1205 * was empty or if the contents of the clipboard could not be
1206 * converted into an image.
1211 gtk_clipboard_request_image (GtkClipboard *clipboard,
1212 GtkClipboardImageReceivedFunc callback,
1215 RequestImageInfo *info;
1217 g_return_if_fail (clipboard != NULL);
1218 g_return_if_fail (callback != NULL);
1220 info = g_new (RequestImageInfo, 1);
1221 info->callback = callback;
1222 info->user_data = user_data;
1224 gtk_clipboard_request_contents (clipboard,
1225 gdk_atom_intern_static_string ("image/png"),
1226 request_image_received_func,
1231 request_uris_received_func (GtkClipboard *clipboard,
1232 GtkSelectionData *selection_data,
1235 RequestURIInfo *info = data;
1238 uris = gtk_selection_data_get_uris (selection_data);
1239 info->callback (clipboard, uris, info->user_data);
1242 g_slice_free (RequestURIInfo, info);
1246 * gtk_clipboard_request_uris:
1247 * @clipboard: a #GtkClipboard
1248 * @callback: (scope async): a function to call when the URIs are received,
1249 * or the retrieval fails. (It will always be called one way or the other.)
1250 * @user_data: user data to pass to @callback.
1252 * Requests the contents of the clipboard as URIs. When the URIs are
1253 * later received @callback will be called.
1255 * The @uris parameter to @callback will contain the resulting array of
1256 * URIs if the request succeeded, or %NULL if it failed. This could happen
1257 * for various reasons, in particular if the clipboard was empty or if the
1258 * contents of the clipboard could not be converted into URI form.
1263 gtk_clipboard_request_uris (GtkClipboard *clipboard,
1264 GtkClipboardURIReceivedFunc callback,
1267 RequestURIInfo *info;
1269 g_return_if_fail (clipboard != NULL);
1270 g_return_if_fail (callback != NULL);
1272 info = g_slice_new (RequestURIInfo);
1273 info->callback = callback;
1274 info->user_data = user_data;
1276 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"),
1277 request_uris_received_func,
1282 request_targets_received_func (GtkClipboard *clipboard,
1283 GtkSelectionData *selection_data,
1286 RequestTargetsInfo *info = data;
1287 GdkAtom *targets = NULL;
1290 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1292 info->callback (clipboard, targets, n_targets, info->user_data);
1299 * gtk_clipboard_request_targets:
1300 * @clipboard: a #GtkClipboard
1301 * @callback: (scope async): a function to call when the targets are
1302 * received, or the retrieval fails. (It will always be called
1303 * one way or the other.)
1304 * @user_data: user data to pass to @callback.
1306 * Requests the contents of the clipboard as list of supported targets.
1307 * When the list is later received, @callback will be called.
1309 * The @targets parameter to @callback will contain the resulting targets if
1310 * the request succeeded, or %NULL if it failed.
1315 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1316 GtkClipboardTargetsReceivedFunc callback,
1319 RequestTargetsInfo *info;
1321 g_return_if_fail (clipboard != NULL);
1322 g_return_if_fail (callback != NULL);
1324 /* If the display supports change notification we cache targets */
1325 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1326 clipboard->n_cached_targets != -1)
1328 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1332 info = g_new (RequestTargetsInfo, 1);
1333 info->callback = callback;
1334 info->user_data = user_data;
1336 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"),
1337 request_targets_received_func,
1345 GdkAtom format; /* used by rich text */
1346 gsize length; /* used by rich text */
1350 clipboard_received_func (GtkClipboard *clipboard,
1351 GtkSelectionData *selection_data,
1354 WaitResults *results = data;
1356 if (gtk_selection_data_get_length (selection_data) >= 0)
1357 results->data = gtk_selection_data_copy (selection_data);
1359 g_main_loop_quit (results->loop);
1363 * gtk_clipboard_wait_for_contents:
1364 * @clipboard: a #GtkClipboard
1365 * @target: an atom representing the form into which the clipboard
1366 * owner should convert the selection.
1368 * Requests the contents of the clipboard using the given target.
1369 * This function waits for the data to be received using the main
1370 * loop, so events, timeouts, etc, may be dispatched during the wait.
1372 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1373 * if retrieving the given target failed. If non-%NULL,
1374 * this value must be freed with gtk_selection_data_free()
1375 * when you are finished with it.
1378 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1381 WaitResults results;
1383 g_return_val_if_fail (clipboard != NULL, NULL);
1384 g_return_val_if_fail (target != GDK_NONE, NULL);
1386 results.data = NULL;
1387 results.loop = g_main_loop_new (NULL, TRUE);
1389 gtk_clipboard_request_contents (clipboard, target,
1390 clipboard_received_func,
1393 if (g_main_loop_is_running (results.loop))
1395 gdk_threads_leave ();
1396 g_main_loop_run (results.loop);
1397 gdk_threads_enter ();
1400 g_main_loop_unref (results.loop);
1402 return results.data;
1406 clipboard_text_received_func (GtkClipboard *clipboard,
1410 WaitResults *results = data;
1412 results->data = g_strdup (text);
1413 g_main_loop_quit (results->loop);
1417 * gtk_clipboard_wait_for_text:
1418 * @clipboard: a #GtkClipboard
1420 * Requests the contents of the clipboard as text and converts
1421 * the result to UTF-8 if necessary. This function waits for
1422 * the data to be received using the main loop, so events,
1423 * timeouts, etc, may be dispatched during the wait.
1425 * Return value: a newly-allocated UTF-8 string which must
1426 * be freed with g_free(), or %NULL if retrieving
1427 * the selection data failed. (This could happen
1428 * for various reasons, in particular if the
1429 * clipboard was empty or if the contents of the
1430 * clipboard could not be converted into text form.)
1433 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1435 WaitResults results;
1437 g_return_val_if_fail (clipboard != NULL, NULL);
1439 results.data = NULL;
1440 results.loop = g_main_loop_new (NULL, TRUE);
1442 gtk_clipboard_request_text (clipboard,
1443 clipboard_text_received_func,
1446 if (g_main_loop_is_running (results.loop))
1448 gdk_threads_leave ();
1449 g_main_loop_run (results.loop);
1450 gdk_threads_enter ();
1453 g_main_loop_unref (results.loop);
1455 return results.data;
1459 clipboard_rich_text_received_func (GtkClipboard *clipboard,
1465 WaitResults *results = data;
1467 results->data = g_memdup (text, length);
1468 results->format = format;
1469 results->length = length;
1470 g_main_loop_quit (results->loop);
1474 * gtk_clipboard_wait_for_rich_text:
1475 * @clipboard: a #GtkClipboard
1476 * @buffer: a #GtkTextBuffer
1477 * @format: (out): return location for the format of the returned data
1478 * @length: return location for the length of the returned data
1480 * Requests the contents of the clipboard as rich text. This function
1481 * waits for the data to be received using the main loop, so events,
1482 * timeouts, etc, may be dispatched during the wait.
1484 * Return value: (array length=length) (transfer full): a
1485 * newly-allocated binary block of data which must be
1486 * freed with g_free(), or %NULL if retrieving the
1487 * selection data failed. (This could happen for various
1488 * reasons, in particular if the clipboard was empty or
1489 * if the contents of the clipboard could not be
1490 * converted into text form.)
1495 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
1496 GtkTextBuffer *buffer,
1500 WaitResults results;
1502 g_return_val_if_fail (clipboard != NULL, NULL);
1503 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1504 g_return_val_if_fail (format != NULL, NULL);
1505 g_return_val_if_fail (length != NULL, NULL);
1507 results.data = NULL;
1508 results.loop = g_main_loop_new (NULL, TRUE);
1510 gtk_clipboard_request_rich_text (clipboard, buffer,
1511 clipboard_rich_text_received_func,
1514 if (g_main_loop_is_running (results.loop))
1516 gdk_threads_leave ();
1517 g_main_loop_run (results.loop);
1518 gdk_threads_enter ();
1521 g_main_loop_unref (results.loop);
1523 *format = results.format;
1524 *length = results.length;
1526 return results.data;
1530 clipboard_image_received_func (GtkClipboard *clipboard,
1534 WaitResults *results = data;
1537 results->data = g_object_ref (pixbuf);
1539 g_main_loop_quit (results->loop);
1543 * gtk_clipboard_wait_for_image:
1544 * @clipboard: a #GtkClipboard
1546 * Requests the contents of the clipboard as image and converts
1547 * the result to a #GdkPixbuf. This function waits for
1548 * the data to be received using the main loop, so events,
1549 * timeouts, etc, may be dispatched during the wait.
1551 * Return value: (transfer full): a newly-allocated #GdkPixbuf
1552 * object which must be disposed with g_object_unref(), or
1553 * %NULL if retrieving the selection data failed. (This could
1554 * happen for various reasons, in particular if the clipboard
1555 * was empty or if the contents of the clipboard could not be
1556 * converted into an image.)
1561 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1563 WaitResults results;
1565 g_return_val_if_fail (clipboard != NULL, NULL);
1567 results.data = NULL;
1568 results.loop = g_main_loop_new (NULL, TRUE);
1570 gtk_clipboard_request_image (clipboard,
1571 clipboard_image_received_func,
1574 if (g_main_loop_is_running (results.loop))
1576 gdk_threads_leave ();
1577 g_main_loop_run (results.loop);
1578 gdk_threads_enter ();
1581 g_main_loop_unref (results.loop);
1583 return results.data;
1587 clipboard_uris_received_func (GtkClipboard *clipboard,
1591 WaitResults *results = data;
1593 results->data = g_strdupv (uris);
1594 g_main_loop_quit (results->loop);
1598 * gtk_clipboard_wait_for_uris:
1599 * @clipboard: a #GtkClipboard
1601 * Requests the contents of the clipboard as URIs. This function waits
1602 * for the data to be received using the main loop, so events,
1603 * timeouts, etc, may be dispatched during the wait.
1605 * Return value: (array zero-terminated=1) (element-type utf8) (transfer full): a newly-allocated
1606 * %NULL-terminated array of strings which must
1607 * be freed with g_strfreev(), or %NULL if
1608 * retrieving the selection data failed. (This
1609 * could happen for various reasons, in particular
1610 * if the clipboard was empty or if the contents of
1611 * the clipboard could not be converted into URI form.)
1616 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
1618 WaitResults results;
1620 g_return_val_if_fail (clipboard != NULL, NULL);
1622 results.data = NULL;
1623 results.loop = g_main_loop_new (NULL, TRUE);
1625 gtk_clipboard_request_uris (clipboard,
1626 clipboard_uris_received_func,
1629 if (g_main_loop_is_running (results.loop))
1631 gdk_threads_leave ();
1632 g_main_loop_run (results.loop);
1633 gdk_threads_enter ();
1636 g_main_loop_unref (results.loop);
1638 return results.data;
1642 * gtk_clipboard_get_display:
1643 * @clipboard: a #GtkClipboard
1645 * Gets the #GdkDisplay associated with @clipboard
1647 * Return value: (transfer none): the #GdkDisplay associated with @clipboard
1652 gtk_clipboard_get_display (GtkClipboard *clipboard)
1654 g_return_val_if_fail (clipboard != NULL, NULL);
1656 return clipboard->display;
1660 * gtk_clipboard_wait_is_text_available:
1661 * @clipboard: a #GtkClipboard
1663 * Test to see if there is text available to be pasted
1664 * This is done by requesting the TARGETS atom and checking
1665 * if it contains any of the supported text targets. This function
1666 * waits for the data to be received using the main loop, so events,
1667 * timeouts, etc, may be dispatched during the wait.
1669 * This function is a little faster than calling
1670 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1673 * Return value: %TRUE is there is text available, %FALSE otherwise.
1676 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1678 GtkSelectionData *data;
1679 gboolean result = FALSE;
1681 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1684 result = gtk_selection_data_targets_include_text (data);
1685 gtk_selection_data_free (data);
1692 * gtk_clipboard_wait_is_rich_text_available:
1693 * @clipboard: a #GtkClipboard
1694 * @buffer: a #GtkTextBuffer
1696 * Test to see if there is rich text available to be pasted
1697 * This is done by requesting the TARGETS atom and checking
1698 * if it contains any of the supported rich text targets. This function
1699 * waits for the data to be received using the main loop, so events,
1700 * timeouts, etc, may be dispatched during the wait.
1702 * This function is a little faster than calling
1703 * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
1706 * Return value: %TRUE is there is rich text available, %FALSE otherwise.
1711 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
1712 GtkTextBuffer *buffer)
1714 GtkSelectionData *data;
1715 gboolean result = FALSE;
1717 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1718 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1720 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1723 result = gtk_selection_data_targets_include_rich_text (data, buffer);
1724 gtk_selection_data_free (data);
1731 * gtk_clipboard_wait_is_image_available:
1732 * @clipboard: a #GtkClipboard
1734 * Test to see if there is an image available to be pasted
1735 * This is done by requesting the TARGETS atom and checking
1736 * if it contains any of the supported image targets. This function
1737 * waits for the data to be received using the main loop, so events,
1738 * timeouts, etc, may be dispatched during the wait.
1740 * This function is a little faster than calling
1741 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1742 * the actual image data.
1744 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1749 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1751 GtkSelectionData *data;
1752 gboolean result = FALSE;
1754 data = gtk_clipboard_wait_for_contents (clipboard,
1755 gdk_atom_intern_static_string ("TARGETS"));
1758 result = gtk_selection_data_targets_include_image (data, FALSE);
1759 gtk_selection_data_free (data);
1766 * gtk_clipboard_wait_is_uris_available:
1767 * @clipboard: a #GtkClipboard
1769 * Test to see if there is a list of URIs available to be pasted
1770 * This is done by requesting the TARGETS atom and checking
1771 * if it contains the URI targets. This function
1772 * waits for the data to be received using the main loop, so events,
1773 * timeouts, etc, may be dispatched during the wait.
1775 * This function is a little faster than calling
1776 * gtk_clipboard_wait_for_uris() since it doesn't need to retrieve
1777 * the actual URI data.
1779 * Return value: %TRUE is there is an URI list available, %FALSE otherwise.
1784 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
1786 GtkSelectionData *data;
1787 gboolean result = FALSE;
1789 data = gtk_clipboard_wait_for_contents (clipboard,
1790 gdk_atom_intern_static_string ("TARGETS"));
1793 result = gtk_selection_data_targets_include_uri (data);
1794 gtk_selection_data_free (data);
1801 * gtk_clipboard_wait_for_targets:
1802 * @clipboard: a #GtkClipboard
1803 * @targets: (out) (array length=n_targets) (transfer container): location
1804 * to store an array of targets. The result stored here must
1805 * be freed with g_free().
1806 * @n_targets: location to store number of items in @targets.
1808 * Returns a list of targets that are present on the clipboard, or %NULL
1809 * if there aren't any targets available. The returned list must be
1810 * freed with g_free().
1811 * This function waits for the data to be received using the main
1812 * loop, so events, timeouts, etc, may be dispatched during the wait.
1814 * Return value: %TRUE if any targets are present on the clipboard,
1820 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1824 GtkSelectionData *data;
1825 gboolean result = FALSE;
1827 g_return_val_if_fail (clipboard != NULL, FALSE);
1829 /* If the display supports change notification we cache targets */
1830 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1831 clipboard->n_cached_targets != -1)
1834 *n_targets = clipboard->n_cached_targets;
1837 *targets = g_memdup (clipboard->cached_targets,
1838 clipboard->n_cached_targets * sizeof (GdkAtom));
1849 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1853 GdkAtom *tmp_targets;
1856 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1858 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1860 clipboard->n_cached_targets = tmp_n_targets;
1861 clipboard->cached_targets = g_memdup (tmp_targets,
1862 tmp_n_targets * sizeof (GdkAtom));
1866 *n_targets = tmp_n_targets;
1869 *targets = tmp_targets;
1871 g_free (tmp_targets);
1873 gtk_selection_data_free (data);
1879 static GtkClipboard *
1880 clipboard_peek (GdkDisplay *display,
1882 gboolean only_if_exists)
1884 GtkClipboard *clipboard = NULL;
1888 if (selection == GDK_NONE)
1889 selection = GDK_SELECTION_CLIPBOARD;
1891 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1893 tmp_list = clipboards;
1896 clipboard = tmp_list->data;
1897 if (clipboard->selection == selection)
1900 tmp_list = tmp_list->next;
1903 if (!tmp_list && !only_if_exists)
1905 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1906 clipboard->selection = selection;
1907 clipboard->display = display;
1908 clipboard->n_cached_targets = -1;
1909 clipboard->n_storable_targets = -1;
1910 clipboards = g_slist_prepend (clipboards, clipboard);
1911 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1912 g_signal_connect (display, "closed",
1913 G_CALLBACK (clipboard_display_closed), clipboard);
1914 gdk_display_request_selection_notification (display, selection);
1921 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1922 GdkEventOwnerChange *event)
1924 if (clipboard->n_cached_targets != -1)
1926 g_free (clipboard->cached_targets);
1927 clipboard->cached_targets = NULL;
1928 clipboard->n_cached_targets = -1;
1933 * gtk_clipboard_wait_is_target_available:
1934 * @clipboard: a #GtkClipboard
1935 * @target: A #GdkAtom indicating which target to look for.
1937 * Checks if a clipboard supports pasting data of a given type. This
1938 * function can be used to determine if a "Paste" menu item should be
1939 * insensitive or not.
1941 * If you want to see if there's text available on the clipboard, use
1942 * gtk_clipboard_wait_is_text_available () instead.
1944 * Return value: %TRUE if the target is available, %FALSE otherwise.
1949 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1954 gboolean retval = FALSE;
1956 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1959 for (i = 0; i < n_targets; i++)
1961 if (targets[i] == target)
1974 * _gtk_clipboard_handle_event:
1975 * @event: a owner change event
1977 * Emits the #GtkClipboard::owner-change signal on the appropriate @clipboard.
1982 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1984 GdkDisplay *display;
1985 GtkClipboard *clipboard;
1987 display = gdk_window_get_display (event->window);
1988 clipboard = clipboard_peek (display, event->selection, TRUE);
1991 g_signal_emit (clipboard,
1992 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1996 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1998 g_main_loop_quit (clipboard->store_loop);
2000 return G_SOURCE_REMOVE;
2004 * gtk_clipboard_set_can_store:
2005 * @clipboard: a #GtkClipboard
2006 * @targets: (allow-none) (array length=n_targets): array containing
2007 * information about which forms should be stored or %NULL
2008 * to indicate that all forms should be stored.
2009 * @n_targets: number of elements in @targets
2011 * Hints that the clipboard data should be stored somewhere when the
2012 * application exits or when gtk_clipboard_store () is called.
2014 * This value is reset when the clipboard owner changes.
2015 * Where the clipboard data is stored is platform dependent,
2016 * see gdk_display_store_clipboard () for more information.
2021 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
2022 const GtkTargetEntry *targets,
2025 GtkWidget *clipboard_widget;
2027 static const GtkTargetEntry save_targets[] = {
2028 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
2031 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2032 g_return_if_fail (n_targets >= 0);
2034 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
2037 g_free (clipboard->storable_targets);
2039 clipboard_widget = get_clipboard_widget (clipboard->display);
2041 /* n_storable_targets being -1 means that
2042 * gtk_clipboard_set_can_store hasn't been called since the
2043 * clipboard owner changed. We only want to add SAVE_TARGETS and
2044 * ref the owner once , so we do that here
2046 if (clipboard->n_storable_targets == -1)
2048 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
2051 /* Ref the owner so it won't go away */
2052 if (clipboard->have_owner)
2053 g_object_ref (clipboard->user_data);
2056 clipboard->n_storable_targets = n_targets;
2057 clipboard->storable_targets = g_new (GdkAtom, n_targets);
2058 for (i = 0; i < n_targets; i++)
2059 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
2063 gtk_clipboard_selection_notify (GtkWidget *widget,
2064 GdkEventSelection *event,
2065 GtkClipboard *clipboard)
2067 if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
2068 clipboard->storing_selection)
2069 g_main_loop_quit (clipboard->store_loop);
2075 * gtk_clipboard_store:
2076 * @clipboard: a #GtkClipboard
2078 * Stores the current clipboard data somewhere so that it will stay
2079 * around after the application has quit.
2084 gtk_clipboard_store (GtkClipboard *clipboard)
2086 GtkWidget *clipboard_widget;
2088 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2090 if (clipboard->n_storable_targets < 0)
2093 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
2096 g_object_ref (clipboard);
2098 clipboard_widget = get_clipboard_widget (clipboard->display);
2099 clipboard->notify_signal_id = g_signal_connect (clipboard_widget,
2100 "selection-notify-event",
2101 G_CALLBACK (gtk_clipboard_selection_notify),
2104 gdk_display_store_clipboard (clipboard->display,
2105 gtk_widget_get_window (clipboard_widget),
2106 clipboard_get_timestamp (clipboard),
2107 clipboard->storable_targets,
2108 clipboard->n_storable_targets);
2110 clipboard->storing_selection = TRUE;
2112 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
2113 clipboard->store_timeout = g_timeout_add_seconds (10, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
2115 if (g_main_loop_is_running (clipboard->store_loop))
2117 gdk_threads_leave ();
2118 g_main_loop_run (clipboard->store_loop);
2119 gdk_threads_enter ();
2122 g_main_loop_unref (clipboard->store_loop);
2123 clipboard->store_loop = NULL;
2125 g_source_remove (clipboard->store_timeout);
2126 clipboard->store_timeout = 0;
2127 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
2128 clipboard->notify_signal_id = 0;
2130 clipboard->storing_selection = FALSE;
2132 g_object_unref (clipboard);
2135 /* Stores all clipboard selections on all displays, called from
2139 _gtk_clipboard_store_all (void)
2141 GtkClipboard *clipboard;
2142 GSList *displays, *list;
2144 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
2149 GdkDisplay *display = list->data;
2151 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
2154 gtk_clipboard_store (clipboard);
2158 g_slist_free (displays);