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"
34 #ifdef GDK_WINDOWING_X11
38 #ifdef GDK_WINDOWING_WIN32
39 #include "win32/gdkwin32.h"
47 typedef struct _GtkClipboardClass GtkClipboardClass;
49 typedef struct _RequestContentsInfo RequestContentsInfo;
50 typedef struct _RequestTextInfo RequestTextInfo;
51 typedef struct _RequestRichTextInfo RequestRichTextInfo;
52 typedef struct _RequestImageInfo RequestImageInfo;
53 typedef struct _RequestTargetsInfo RequestTargetsInfo;
57 GObject parent_instance;
61 GtkClipboardGetFunc get_func;
62 GtkClipboardClearFunc clear_func;
68 gboolean have_selection;
71 GdkAtom *cached_targets;
72 gint n_cached_targets;
74 guint notify_signal_id;
75 gboolean storing_selection;
76 GMainLoop *store_loop;
78 gint n_storable_targets;
79 GdkAtom *storable_targets;
82 struct _GtkClipboardClass
84 GObjectClass parent_class;
86 void (*owner_change) (GtkClipboard *clipboard,
87 GdkEventOwnerChange *event);
90 struct _RequestContentsInfo
92 GtkClipboardReceivedFunc callback;
96 struct _RequestTextInfo
98 GtkClipboardTextReceivedFunc callback;
102 struct _RequestRichTextInfo
104 GtkClipboardRichTextReceivedFunc callback;
111 struct _RequestImageInfo
113 GtkClipboardImageReceivedFunc callback;
117 struct _RequestTargetsInfo
119 GtkClipboardTargetsReceivedFunc callback;
123 static void gtk_clipboard_class_init (GtkClipboardClass *class);
124 static void gtk_clipboard_finalize (GObject *object);
125 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
126 GdkEventOwnerChange *event);
128 static void clipboard_unset (GtkClipboard *clipboard);
129 static void selection_received (GtkWidget *widget,
130 GtkSelectionData *selection_data,
132 static GtkClipboard *clipboard_peek (GdkDisplay *display,
134 gboolean only_if_exists);
135 static GtkWidget * get_clipboard_widget (GdkDisplay *display);
141 TARGET_COMPOUND_TEXT,
146 static const gchar request_contents_key[] = "gtk-request-contents";
147 static GQuark request_contents_key_id = 0;
149 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
150 static GQuark clipboards_owned_key_id = 0;
152 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
154 G_DEFINE_TYPE (GtkClipboard, gtk_clipboard, G_TYPE_OBJECT)
157 gtk_clipboard_init (GtkClipboard *object)
162 gtk_clipboard_class_init (GtkClipboardClass *class)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
166 gobject_class->finalize = gtk_clipboard_finalize;
168 class->owner_change = gtk_clipboard_owner_change;
171 * GtkClipboard::owner-change:
172 * @clipboard: the #GtkClipboard on which the signal is emitted
173 * @event: the @GdkEventOwnerChange event
175 * The ::owner-change signal is emitted when GTK+ receives an
176 * event that indicates that the ownership of the selection
177 * associated with @clipboard has changed.
181 clipboard_signals[OWNER_CHANGE] =
182 g_signal_new (I_("owner_change"),
183 G_TYPE_FROM_CLASS (gobject_class),
185 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
187 _gtk_marshal_VOID__BOXED,
189 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
193 gtk_clipboard_finalize (GObject *object)
195 GtkClipboard *clipboard;
196 GtkWidget *clipboard_widget;
199 clipboard = GTK_CLIPBOARD (object);
201 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
202 if (g_slist_index (clipboards, clipboard) >= 0)
203 g_warning ("GtkClipboard prematurely finalized");
205 /* don't use get_clipboard_widget() here because it would create the
206 * widget if it doesn't exist.
208 clipboard_widget = g_object_get_data (G_OBJECT (clipboard->display),
209 "gtk-clipboard-widget");
211 clipboard_unset (clipboard);
213 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
214 clipboards = g_slist_remove (clipboards, clipboard);
215 g_object_set_data (G_OBJECT (clipboard->display), I_("gtk-clipboard-list"), clipboards);
217 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
218 g_main_loop_quit (clipboard->store_loop);
220 if (clipboard->store_timeout != 0)
221 g_source_remove (clipboard->store_timeout);
223 if (clipboard->notify_signal_id != 0)
224 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
226 g_free (clipboard->storable_targets);
228 G_OBJECT_CLASS (gtk_clipboard_parent_class)->finalize (object);
232 clipboard_display_closed (GdkDisplay *display,
234 GtkClipboard *clipboard)
238 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
239 g_object_run_dispose (G_OBJECT (clipboard));
240 clipboards = g_slist_remove (clipboards, clipboard);
241 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
242 g_object_unref (clipboard);
246 * gtk_clipboard_get_for_display:
247 * @display: the display for which the clipboard is to be retrieved or created
248 * @selection: a #GdkAtom which identifies the clipboard
251 * Returns the clipboard object for the given selection.
252 * Cut/copy/paste menu items and keyboard shortcuts should use
253 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
254 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
255 * for backwards compatibility reasons.)
256 * The currently-selected object or text should be provided on the clipboard
257 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
258 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
259 * to the default clipboard, i.e. they copy the selection to what the
260 * user sees as the clipboard.
262 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
263 * ("CLIPBOARD", FALSE)</literal>. See <ulink
264 * url="http://www.freedesktop.org/Standards/clipboards-spec">
265 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
266 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
267 * selections under the X window system. On Win32 the
268 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
270 * It's possible to have arbitrary named clipboards; if you do invent
271 * new clipboards, you should prefix the selection name with an
272 * underscore (because the ICCCM requires that nonstandard atoms are
273 * underscore-prefixed), and namespace it as well. For example,
274 * if your application called "Foo" has a special-purpose
275 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
277 * Return value: the appropriate clipboard object. If no
278 * clipboard already exists, a new one will
279 * be created. Once a clipboard object has
280 * been created, it is persistent and, since
281 * it is owned by GTK+, must not be freed or
287 gtk_clipboard_get_for_display (GdkDisplay *display,
290 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
291 g_return_val_if_fail (!display->closed, NULL);
293 return clipboard_peek (display, selection, FALSE);
298 * gtk_clipboard_get():
299 * @selection: a #GdkAtom which identifies the clipboard
302 * Returns the clipboard object for the given selection.
303 * See gtk_clipboard_get_for_display() for complete details.
305 * Return value: the appropriate clipboard object. If no
306 * clipboard already exists, a new one will
307 * be created. Once a clipboard object has
308 * been created, it is persistent and, since
309 * it is owned by GTK+, must not be freed or
313 gtk_clipboard_get (GdkAtom selection)
315 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
319 selection_get_cb (GtkWidget *widget,
320 GtkSelectionData *selection_data,
324 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, selection_data->selection);
326 if (clipboard && clipboard->get_func)
327 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
331 selection_clear_event_cb (GtkWidget *widget,
332 GdkEventSelection *event)
334 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
338 clipboard_unset (clipboard);
347 make_clipboard_widget (GdkDisplay *display,
350 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
352 g_signal_connect (widget, "selection_received",
353 G_CALLBACK (selection_received), NULL);
357 /* We need this for gdk_x11_get_server_time() */
358 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
360 g_signal_connect (widget, "selection_get",
361 G_CALLBACK (selection_get_cb), NULL);
362 g_signal_connect (widget, "selection_clear_event",
363 G_CALLBACK (selection_clear_event_cb), NULL);
370 get_clipboard_widget (GdkDisplay *display)
372 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
375 clip_widget = make_clipboard_widget (display, TRUE);
376 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
382 /* This function makes a very good guess at what the correct
383 * timestamp for a selection request should be. If there is
384 * a currently processed event, it uses the timestamp for that
385 * event, otherwise it uses the current server time. However,
386 * if the time resulting from that is older than the time used
387 * last time, it uses the time used last time instead.
389 * In order implement this correctly, we never use CurrentTime,
390 * but actually retrieve the actual timestamp from the server.
391 * This is a little slower but allows us to make the guarantee
392 * that the times used by this application will always ascend
393 * and we won't get selections being rejected just because
394 * we are using a correct timestamp from an event, but used
395 * CurrentTime previously.
398 clipboard_get_timestamp (GtkClipboard *clipboard)
400 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
401 guint32 timestamp = gtk_get_current_event_time ();
403 if (timestamp == GDK_CURRENT_TIME)
405 #ifdef GDK_WINDOWING_X11
406 timestamp = gdk_x11_get_server_time (clipboard_widget->window);
407 #elif defined GDK_WINDOWING_WIN32
408 timestamp = GetMessageTime ();
413 if (clipboard->timestamp != GDK_CURRENT_TIME)
415 /* Check to see if clipboard->timestamp is newer than
416 * timestamp, accounting for wraparound.
419 guint32 max = timestamp + 0x80000000;
421 if ((max > timestamp &&
422 (clipboard->timestamp > timestamp &&
423 clipboard->timestamp <= max)) ||
425 (clipboard->timestamp > timestamp ||
426 clipboard->timestamp <= max)))
428 timestamp = clipboard->timestamp;
433 clipboard->timestamp = timestamp;
439 clipboard_owner_destroyed (gpointer data)
441 GSList *clipboards = data;
444 tmp_list = clipboards;
447 GtkClipboard *clipboard = tmp_list->data;
449 clipboard->get_func = NULL;
450 clipboard->clear_func = NULL;
451 clipboard->user_data = NULL;
452 clipboard->have_owner = FALSE;
454 gtk_clipboard_clear (clipboard);
456 tmp_list = tmp_list->next;
459 g_slist_free (clipboards);
463 clipboard_add_owner_notify (GtkClipboard *clipboard)
465 if (!clipboards_owned_key_id)
466 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
468 if (clipboard->have_owner)
469 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
470 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
471 clipboards_owned_key_id),
473 clipboard_owner_destroyed);
477 clipboard_remove_owner_notify (GtkClipboard *clipboard)
479 if (clipboard->have_owner)
480 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
481 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
482 clipboards_owned_key_id),
484 clipboard_owner_destroyed);
488 gtk_clipboard_set_contents (GtkClipboard *clipboard,
489 const GtkTargetEntry *targets,
491 GtkClipboardGetFunc get_func,
492 GtkClipboardClearFunc clear_func,
496 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
498 if (gtk_selection_owner_set_for_display (clipboard->display,
500 clipboard->selection,
501 clipboard_get_timestamp (clipboard)))
503 clipboard->have_selection = TRUE;
505 if (!(clipboard->have_owner && have_owner) ||
506 clipboard->user_data != user_data)
508 clipboard_unset (clipboard);
510 if (clipboard->get_func)
512 /* Calling unset() caused the clipboard contents to be reset!
513 * Avoid leaking and return
515 if (!(clipboard->have_owner && have_owner) ||
516 clipboard->user_data != user_data)
518 (*clear_func) (clipboard, user_data);
526 clipboard->user_data = user_data;
527 clipboard->have_owner = have_owner;
529 clipboard_add_owner_notify (clipboard);
534 clipboard->get_func = get_func;
535 clipboard->clear_func = clear_func;
537 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
538 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
548 * gtk_clipboard_set_with_data:
549 * @clipboard: a #GtkClipboard
550 * @targets: array containing information about the available forms for the
552 * @n_targets: number of elements in @targets
553 * @get_func: function to call to get the actual clipboard data
554 * @clear_func: when the clipboard contents are set again, this function will
555 * be called, and @get_func will not be subsequently called.
556 * @user_data: user data to pass to @get_func and @clear_func.
558 * Virtually sets the contents of the specified clipboard by providing
559 * a list of supported formats for the clipboard data and a function
560 * to call to get the actual data when it is requested.
562 * Return value: %TRUE if setting the clipboard data succeeded. If setting
563 * the clipboard data failed the provided callback functions
567 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
568 const GtkTargetEntry *targets,
570 GtkClipboardGetFunc get_func,
571 GtkClipboardClearFunc clear_func,
574 g_return_val_if_fail (clipboard != NULL, FALSE);
575 g_return_val_if_fail (targets != NULL, FALSE);
576 g_return_val_if_fail (get_func != NULL, FALSE);
578 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
579 get_func, clear_func, user_data,
584 * gtk_clipboard_set_with_owner:
585 * @clipboard: a #GtkClipboard
586 * @targets: array containing information about the available forms for the
588 * @n_targets: number of elements in @targets
589 * @get_func: function to call to get the actual clipboard data
590 * @clear_func: when the clipboard contents are set again, this function will
591 * be called, and @get_func will not be subsequently called.
592 * @owner: an object that "owns" the data. This object will be passed
593 * to the callbacks when called.
595 * Virtually sets the contents of the specified clipboard by providing
596 * a list of supported formats for the clipboard data and a function
597 * to call to get the actual data when it is requested.
599 * The difference between this function and gtk_clipboard_set_with_data()
600 * is that instead of an generic @user_data pointer, a #GObject is passed
603 * Return value: %TRUE if setting the clipboard data succeeded. If setting
604 * the clipboard data failed the provided callback functions
608 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
609 const GtkTargetEntry *targets,
611 GtkClipboardGetFunc get_func,
612 GtkClipboardClearFunc clear_func,
615 g_return_val_if_fail (clipboard != NULL, FALSE);
616 g_return_val_if_fail (targets != NULL, FALSE);
617 g_return_val_if_fail (get_func != NULL, FALSE);
618 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
620 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
621 get_func, clear_func, owner,
626 * gtk_clipboard_get_owner:
627 * @clipboard: a #GtkClipboard
629 * If the clipboard contents callbacks were set with
630 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
631 * gtk_clipboard_clear() has not subsequently called, returns the owner set
632 * by gtk_clipboard_set_with_owner().
634 * Return value: the owner of the clipboard, if any; otherwise %NULL.
637 gtk_clipboard_get_owner (GtkClipboard *clipboard)
639 g_return_val_if_fail (clipboard != NULL, NULL);
641 if (clipboard->have_owner)
642 return clipboard->user_data;
648 clipboard_unset (GtkClipboard *clipboard)
650 GtkClipboardClearFunc old_clear_func;
652 gboolean old_have_owner;
653 gint old_n_storable_targets;
655 old_clear_func = clipboard->clear_func;
656 old_data = clipboard->user_data;
657 old_have_owner = clipboard->have_owner;
658 old_n_storable_targets = clipboard->n_storable_targets;
662 clipboard_remove_owner_notify (clipboard);
663 clipboard->have_owner = FALSE;
666 clipboard->n_storable_targets = -1;
667 g_free (clipboard->storable_targets);
668 clipboard->storable_targets = NULL;
670 clipboard->get_func = NULL;
671 clipboard->clear_func = NULL;
672 clipboard->user_data = NULL;
675 old_clear_func (clipboard, old_data);
677 /* If we've transferred the clipboard data to the manager,
680 if (old_have_owner &&
681 old_n_storable_targets != -1)
682 g_object_unref (old_data);
686 * gtk_clipboard_clear:
687 * @clipboard: a #GtkClipboard
689 * Clears the contents of the clipboard. Generally this should only
690 * be called between the time you call gtk_clipboard_set_with_owner()
691 * or gtk_clipboard_set_with_data(),
692 * and when the @clear_func you supplied is called. Otherwise, the
693 * clipboard may be owned by someone else.
696 gtk_clipboard_clear (GtkClipboard *clipboard)
698 g_return_if_fail (clipboard != NULL);
700 if (clipboard->have_selection)
701 gtk_selection_owner_set_for_display (clipboard->display,
703 clipboard->selection,
704 clipboard_get_timestamp (clipboard));
708 text_get_func (GtkClipboard *clipboard,
709 GtkSelectionData *selection_data,
713 gtk_selection_data_set_text (selection_data, data, -1);
717 text_clear_func (GtkClipboard *clipboard,
725 * gtk_clipboard_set_text:
726 * @clipboard: a #GtkClipboard object
727 * @text: a UTF-8 string.
728 * @len: length of @text, in bytes, or -1, in which case
729 * the length will be determined with <function>strlen()</function>.
731 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
732 * make a copy of the text and take responsibility for responding
733 * for requests for the text, and for converting the text into
734 * the requested format.
737 gtk_clipboard_set_text (GtkClipboard *clipboard,
742 GtkTargetEntry *targets;
745 g_return_if_fail (clipboard != NULL);
746 g_return_if_fail (text != NULL);
748 list = gtk_target_list_new (NULL, 0);
749 gtk_target_list_add_text_targets (list, 0);
751 targets = gtk_target_table_new_from_list (list, &n_targets);
756 gtk_clipboard_set_with_data (clipboard,
758 text_get_func, text_clear_func,
759 g_strndup (text, len));
760 gtk_clipboard_set_can_store (clipboard, NULL, 0);
762 gtk_target_table_free (targets, n_targets);
763 gtk_target_list_unref (list);
767 pixbuf_get_func (GtkClipboard *clipboard,
768 GtkSelectionData *selection_data,
772 gtk_selection_data_set_pixbuf (selection_data, data);
776 pixbuf_clear_func (GtkClipboard *clipboard,
779 g_object_unref (data);
783 * gtk_clipboard_set_image:
784 * @clipboard: a #GtkClipboard object
785 * @pixbuf: a #GdkPixbuf
787 * Sets the contents of the clipboard to the given #GdkPixbuf.
788 * GTK+ will take responsibility for responding for requests
789 * for the image, and for converting the image into the
795 gtk_clipboard_set_image (GtkClipboard *clipboard,
799 GtkTargetEntry *targets;
802 g_return_if_fail (clipboard != NULL);
803 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
805 list = gtk_target_list_new (NULL, 0);
806 gtk_target_list_add_image_targets (list, 0, TRUE);
808 targets = gtk_target_table_new_from_list (list, &n_targets);
810 gtk_clipboard_set_with_data (clipboard,
812 pixbuf_get_func, pixbuf_clear_func,
813 g_object_ref (pixbuf));
814 gtk_clipboard_set_can_store (clipboard, NULL, 0);
816 gtk_target_table_free (targets, n_targets);
817 gtk_target_list_unref (list);
821 set_request_contents_info (GtkWidget *widget,
822 RequestContentsInfo *info)
824 if (!request_contents_key_id)
825 request_contents_key_id = g_quark_from_static_string (request_contents_key);
827 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
830 static RequestContentsInfo *
831 get_request_contents_info (GtkWidget *widget)
833 if (!request_contents_key_id)
836 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
840 selection_received (GtkWidget *widget,
841 GtkSelectionData *selection_data,
844 RequestContentsInfo *request_info = get_request_contents_info (widget);
845 set_request_contents_info (widget, NULL);
847 request_info->callback (gtk_widget_get_clipboard (widget, selection_data->selection),
849 request_info->user_data);
851 g_free (request_info);
853 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
854 gtk_widget_destroy (widget);
858 * gtk_clipboard_request_contents:
859 * @clipboard: a #GtkClipboard
860 * @target: an atom representing the form into which the clipboard
861 * owner should convert the selection.
862 * @callback: A function to call when the results are received
863 * (or the retrieval fails). If the retrieval fails
864 * the length field of @selection_data will be
866 * @user_data: user data to pass to @callback
868 * Requests the contents of clipboard as the given target.
869 * When the results of the result are later received the supplied callback
873 gtk_clipboard_request_contents (GtkClipboard *clipboard,
875 GtkClipboardReceivedFunc callback,
878 RequestContentsInfo *info;
880 GtkWidget *clipboard_widget;
882 g_return_if_fail (clipboard != NULL);
883 g_return_if_fail (target != GDK_NONE);
884 g_return_if_fail (callback != NULL);
886 clipboard_widget = get_clipboard_widget (clipboard->display);
888 if (get_request_contents_info (clipboard_widget))
889 widget = make_clipboard_widget (clipboard->display, FALSE);
891 widget = clipboard_widget;
893 info = g_new (RequestContentsInfo, 1);
894 info->callback = callback;
895 info->user_data = user_data;
897 set_request_contents_info (widget, info);
899 gtk_selection_convert (widget, clipboard->selection, target,
900 clipboard_get_timestamp (clipboard));
904 request_text_received_func (GtkClipboard *clipboard,
905 GtkSelectionData *selection_data,
908 RequestTextInfo *info = data;
909 gchar *result = NULL;
911 result = (gchar *) gtk_selection_data_get_text (selection_data);
915 /* If we asked for UTF8 and didn't get it, try compound_text;
916 * if we asked for compound_text and didn't get it, try string;
917 * If we asked for anything else and didn't get it, give up.
919 if (selection_data->target == gdk_atom_intern_static_string ("UTF8_STRING"))
921 gtk_clipboard_request_contents (clipboard,
922 gdk_atom_intern_static_string ("COMPOUND_TEXT"),
923 request_text_received_func, info);
926 else if (selection_data->target == gdk_atom_intern_static_string ("COMPOUND_TEXT"))
928 gtk_clipboard_request_contents (clipboard,
930 request_text_received_func, info);
935 info->callback (clipboard, result, info->user_data);
941 * gtk_clipboard_request_text:
942 * @clipboard: a #GtkClipboard
943 * @callback: a function to call when the text is received,
944 * or the retrieval fails. (It will always be called
945 * one way or the other.)
946 * @user_data: user data to pass to @callback.
948 * Requests the contents of the clipboard as text. When the text is
949 * later received, it will be converted to UTF-8 if necessary, and
950 * @callback will be called.
952 * The @text parameter to @callback will contain the resulting text if
953 * the request succeeded, or %NULL if it failed. This could happen for
954 * various reasons, in particular if the clipboard was empty or if the
955 * contents of the clipboard could not be converted into text form.
958 gtk_clipboard_request_text (GtkClipboard *clipboard,
959 GtkClipboardTextReceivedFunc callback,
962 RequestTextInfo *info;
964 g_return_if_fail (clipboard != NULL);
965 g_return_if_fail (callback != NULL);
967 info = g_new (RequestTextInfo, 1);
968 info->callback = callback;
969 info->user_data = user_data;
971 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("UTF8_STRING"),
972 request_text_received_func,
977 request_rich_text_received_func (GtkClipboard *clipboard,
978 GtkSelectionData *selection_data,
981 RequestRichTextInfo *info = data;
982 guint8 *result = NULL;
985 result = selection_data->data;
986 length = selection_data->length;
988 info->current_atom++;
990 if ((!result || length < 1) && (info->current_atom < info->n_atoms))
992 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
993 request_rich_text_received_func,
998 info->callback (clipboard, selection_data->target, result, length,
1000 g_free (info->atoms);
1005 * gtk_clipboard_request_rich_text:
1006 * @clipboard: a #GtkClipboard
1007 * @buffer: a #GtkTextBuffer
1008 * @callback: a function to call when the text is received,
1009 * or the retrieval fails. (It will always be called
1010 * one way or the other.)
1011 * @user_data: user data to pass to @callback.
1013 * Requests the contents of the clipboard as rich text. When the rich
1014 * text is later received, @callback will be called.
1016 * The @text parameter to @callback will contain the resulting rich
1017 * text if the request succeeded, or %NULL if it failed. The @length
1018 * parameter will contain @text's length. This function can fail for
1019 * various reasons, in particular if the clipboard was empty or if the
1020 * contents of the clipboard could not be converted into rich text form.
1025 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
1026 GtkTextBuffer *buffer,
1027 GtkClipboardRichTextReceivedFunc callback,
1030 RequestRichTextInfo *info;
1032 g_return_if_fail (clipboard != NULL);
1033 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1034 g_return_if_fail (callback != NULL);
1036 info = g_new (RequestRichTextInfo, 1);
1037 info->callback = callback;
1040 info->current_atom = 0;
1041 info->user_data = user_data;
1043 info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
1045 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1046 request_rich_text_received_func,
1051 request_image_received_func (GtkClipboard *clipboard,
1052 GtkSelectionData *selection_data,
1055 RequestImageInfo *info = data;
1056 GdkPixbuf *result = NULL;
1058 result = gtk_selection_data_get_pixbuf (selection_data);
1062 /* If we asked for image/png and didn't get it, try image/jpeg;
1063 * if we asked for image/jpeg and didn't get it, try image/gif;
1064 * if we asked for image/gif and didn't get it, try image/bmp;
1065 * If we asked for anything else and didn't get it, give up.
1067 if (selection_data->target == gdk_atom_intern_static_string ("image/png"))
1069 gtk_clipboard_request_contents (clipboard,
1070 gdk_atom_intern_static_string ("image/jpeg"),
1071 request_image_received_func, info);
1074 else if (selection_data->target == gdk_atom_intern_static_string ("image/jpeg"))
1076 gtk_clipboard_request_contents (clipboard,
1077 gdk_atom_intern_static_string ("image/gif"),
1078 request_image_received_func, info);
1081 else if (selection_data->target == gdk_atom_intern_static_string ("image/gif"))
1083 gtk_clipboard_request_contents (clipboard,
1084 gdk_atom_intern_static_string ("image/bmp"),
1085 request_image_received_func, info);
1090 info->callback (clipboard, result, info->user_data);
1094 g_object_unref (result);
1098 * gtk_clipboard_request_image:
1099 * @clipboard: a #GtkClipboard
1100 * @callback: a function to call when the image is received,
1101 * or the retrieval fails. (It will always be called
1102 * one way or the other.)
1103 * @user_data: user data to pass to @callback.
1105 * Requests the contents of the clipboard as image. When the image is
1106 * later received, it will be converted to a #GdkPixbuf, and
1107 * @callback will be called.
1109 * The @pixbuf parameter to @callback will contain the resulting
1110 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1111 * could happen for various reasons, in particular if the clipboard
1112 * was empty or if the contents of the clipboard could not be
1113 * converted into an image.
1118 gtk_clipboard_request_image (GtkClipboard *clipboard,
1119 GtkClipboardImageReceivedFunc callback,
1122 RequestImageInfo *info;
1124 g_return_if_fail (clipboard != NULL);
1125 g_return_if_fail (callback != NULL);
1127 info = g_new (RequestImageInfo, 1);
1128 info->callback = callback;
1129 info->user_data = user_data;
1131 gtk_clipboard_request_contents (clipboard,
1132 gdk_atom_intern_static_string ("image/png"),
1133 request_image_received_func,
1138 request_targets_received_func (GtkClipboard *clipboard,
1139 GtkSelectionData *selection_data,
1142 RequestTargetsInfo *info = data;
1143 GdkAtom *targets = NULL;
1146 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1148 info->callback (clipboard, targets, n_targets, info->user_data);
1155 * gtk_clipboard_request_targets:
1156 * @clipboard: a #GtkClipboard
1157 * @callback: a function to call when the targets are received,
1158 * or the retrieval fails. (It will always be called
1159 * one way or the other.)
1160 * @user_data: user data to pass to @callback.
1162 * Requests the contents of the clipboard as list of supported targets.
1163 * When the list is later received, @callback will be called.
1165 * The @targets parameter to @callback will contain the resulting targets if
1166 * the request succeeded, or %NULL if it failed.
1171 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1172 GtkClipboardTargetsReceivedFunc callback,
1175 RequestTargetsInfo *info;
1177 g_return_if_fail (clipboard != NULL);
1178 g_return_if_fail (callback != NULL);
1180 /* If the display supports change notification we cache targets */
1181 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1182 clipboard->n_cached_targets != -1)
1184 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1188 info = g_new (RequestTargetsInfo, 1);
1189 info->callback = callback;
1190 info->user_data = user_data;
1192 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"),
1193 request_targets_received_func,
1201 GdkAtom format; /* used by rich text */
1202 gsize length; /* used by rich text */
1206 clipboard_received_func (GtkClipboard *clipboard,
1207 GtkSelectionData *selection_data,
1210 WaitResults *results = data;
1212 if (selection_data->length >= 0)
1213 results->data = gtk_selection_data_copy (selection_data);
1215 g_main_loop_quit (results->loop);
1219 * gtk_clipboard_wait_for_contents:
1220 * @clipboard: a #GtkClipboard
1221 * @target: an atom representing the form into which the clipboard
1222 * owner should convert the selection.
1224 * Requests the contents of the clipboard using the given target.
1225 * This function waits for the data to be received using the main
1226 * loop, so events, timeouts, etc, may be dispatched during the wait.
1228 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1229 * if retrieving the given target failed. If non-%NULL,
1230 * this value must be freed with gtk_selection_data_free()
1231 * when you are finished with it.
1234 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1237 WaitResults results;
1239 g_return_val_if_fail (clipboard != NULL, NULL);
1240 g_return_val_if_fail (target != GDK_NONE, NULL);
1242 results.data = NULL;
1243 results.loop = g_main_loop_new (NULL, TRUE);
1245 gtk_clipboard_request_contents (clipboard, target,
1246 clipboard_received_func,
1249 if (g_main_loop_is_running (results.loop))
1251 GDK_THREADS_LEAVE ();
1252 g_main_loop_run (results.loop);
1253 GDK_THREADS_ENTER ();
1256 g_main_loop_unref (results.loop);
1258 return results.data;
1262 clipboard_text_received_func (GtkClipboard *clipboard,
1266 WaitResults *results = data;
1268 results->data = g_strdup (text);
1269 g_main_loop_quit (results->loop);
1273 * gtk_clipboard_wait_for_text:
1274 * @clipboard: a #GtkClipboard
1276 * Requests the contents of the clipboard as text and converts
1277 * the result to UTF-8 if necessary. This function waits for
1278 * the data to be received using the main loop, so events,
1279 * timeouts, etc, may be dispatched during the wait.
1281 * Return value: a newly-allocated UTF-8 string which must
1282 * be freed with g_free(), or %NULL if retrieving
1283 * the selection data failed. (This could happen
1284 * for various reasons, in particular if the
1285 * clipboard was empty or if the contents of the
1286 * clipboard could not be converted into text form.)
1289 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1291 WaitResults results;
1293 g_return_val_if_fail (clipboard != NULL, NULL);
1295 results.data = NULL;
1296 results.loop = g_main_loop_new (NULL, TRUE);
1298 gtk_clipboard_request_text (clipboard,
1299 clipboard_text_received_func,
1302 if (g_main_loop_is_running (results.loop))
1304 GDK_THREADS_LEAVE ();
1305 g_main_loop_run (results.loop);
1306 GDK_THREADS_ENTER ();
1309 g_main_loop_unref (results.loop);
1311 return results.data;
1315 clipboard_rich_text_received_func (GtkClipboard *clipboard,
1321 WaitResults *results = data;
1323 results->data = g_memdup (text, length);
1324 results->format = format;
1325 results->length = length;
1326 g_main_loop_quit (results->loop);
1330 * gtk_clipboard_wait_for_rich_text:
1331 * @clipboard: a #GtkClipboard
1332 * @buffer: a #GtkTextBuffer
1333 * @format: return location for the format of the returned data
1334 * @length: return location for the length of the returned data
1336 * Requests the contents of the clipboard as rich text. This function
1337 * waits for the data to be received using the main loop, so events,
1338 * timeouts, etc, may be dispatched during the wait.
1340 * Return value: a newly-allocated binary block of data which must
1341 * be freed with g_free(), or %NULL if retrieving
1342 * the selection data failed. (This could happen
1343 * for various reasons, in particular if the
1344 * clipboard was empty or if the contents of the
1345 * clipboard could not be converted into text form.)
1350 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
1351 GtkTextBuffer *buffer,
1355 WaitResults results;
1357 g_return_val_if_fail (clipboard != NULL, NULL);
1358 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1359 g_return_val_if_fail (format != NULL, NULL);
1360 g_return_val_if_fail (length != NULL, NULL);
1362 results.data = NULL;
1363 results.loop = g_main_loop_new (NULL, TRUE);
1365 gtk_clipboard_request_rich_text (clipboard, buffer,
1366 clipboard_rich_text_received_func,
1369 if (g_main_loop_is_running (results.loop))
1371 GDK_THREADS_LEAVE ();
1372 g_main_loop_run (results.loop);
1373 GDK_THREADS_ENTER ();
1376 g_main_loop_unref (results.loop);
1378 *format = results.format;
1379 *length = results.length;
1381 return results.data;
1385 clipboard_image_received_func (GtkClipboard *clipboard,
1389 WaitResults *results = data;
1392 results->data = g_object_ref (pixbuf);
1394 g_main_loop_quit (results->loop);
1398 * gtk_clipboard_wait_for_image:
1399 * @clipboard: a #GtkClipboard
1401 * Requests the contents of the clipboard as image and converts
1402 * the result to a #GdkPixbuf. This function waits for
1403 * the data to be received using the main loop, so events,
1404 * timeouts, etc, may be dispatched during the wait.
1406 * Return value: a newly-allocated #GdkPixbuf object which must
1407 * be disposed with g_object_unref(), or %NULL if
1408 * retrieving the selection data failed. (This
1409 * could happen for various reasons, in particular
1410 * if the clipboard was empty or if the contents of
1411 * the clipboard could not be converted into an image.)
1416 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1418 WaitResults results;
1420 g_return_val_if_fail (clipboard != NULL, NULL);
1422 results.data = NULL;
1423 results.loop = g_main_loop_new (NULL, TRUE);
1425 gtk_clipboard_request_image (clipboard,
1426 clipboard_image_received_func,
1429 if (g_main_loop_is_running (results.loop))
1431 GDK_THREADS_LEAVE ();
1432 g_main_loop_run (results.loop);
1433 GDK_THREADS_ENTER ();
1436 g_main_loop_unref (results.loop);
1438 return results.data;
1442 * gtk_clipboard_get_display:
1443 * @clipboard: a #GtkClipboard
1445 * Gets the #GdkDisplay associated with @clipboard
1447 * Return value: the #GdkDisplay associated with @clipboard
1452 gtk_clipboard_get_display (GtkClipboard *clipboard)
1454 g_return_val_if_fail (clipboard != NULL, NULL);
1456 return clipboard->display;
1460 * gtk_clipboard_wait_is_text_available:
1461 * @clipboard: a #GtkClipboard
1463 * Test to see if there is text available to be pasted
1464 * This is done by requesting the TARGETS atom and checking
1465 * if it contains any of the supported text targets. This function
1466 * waits for the data to be received using the main loop, so events,
1467 * timeouts, etc, may be dispatched during the wait.
1469 * This function is a little faster than calling
1470 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1473 * Return value: %TRUE is there is text available, %FALSE otherwise.
1476 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1478 GtkSelectionData *data;
1479 gboolean result = FALSE;
1481 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1484 result = gtk_selection_data_targets_include_text (data);
1485 gtk_selection_data_free (data);
1492 * gtk_clipboard_wait_is_rich_text_available:
1493 * @clipboard: a #GtkClipboard
1494 * @buffer: a #GtkTextBuffer
1496 * Test to see if there is rich text available to be pasted
1497 * This is done by requesting the TARGETS atom and checking
1498 * if it contains any of the supported rich text targets. This function
1499 * waits for the data to be received using the main loop, so events,
1500 * timeouts, etc, may be dispatched during the wait.
1502 * This function is a little faster than calling
1503 * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
1506 * Return value: %TRUE is there is rich text available, %FALSE otherwise.
1511 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
1512 GtkTextBuffer *buffer)
1514 GtkSelectionData *data;
1515 gboolean result = FALSE;
1517 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1518 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1520 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1523 result = gtk_selection_data_targets_include_rich_text (data, buffer);
1524 gtk_selection_data_free (data);
1531 * gtk_clipboard_wait_is_image_available:
1532 * @clipboard: a #GtkClipboard
1534 * Test to see if there is an image available to be pasted
1535 * This is done by requesting the TARGETS atom and checking
1536 * if it contains any of the supported image targets. This function
1537 * waits for the data to be received using the main loop, so events,
1538 * timeouts, etc, may be dispatched during the wait.
1540 * This function is a little faster than calling
1541 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1542 * the actual image data.
1544 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1549 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1551 GtkSelectionData *data;
1552 gboolean result = FALSE;
1554 data = gtk_clipboard_wait_for_contents (clipboard,
1555 gdk_atom_intern_static_string ("TARGETS"));
1558 result = gtk_selection_data_targets_include_image (data, FALSE);
1559 gtk_selection_data_free (data);
1566 * gtk_clipboard_wait_for_targets
1567 * @clipboard: a #GtkClipboard
1568 * @targets: location to store an array of targets. The result
1569 * stored here must be freed with g_free().
1570 * @n_targets: location to store number of items in @targets.
1572 * Returns a list of targets that are present on the clipboard, or %NULL
1573 * if there aren't any targets available. The returned list must be
1574 * freed with g_free().
1575 * This function waits for the data to be received using the main
1576 * loop, so events, timeouts, etc, may be dispatched during the wait.
1578 * Return value: %TRUE if any targets are present on the clipboard,
1584 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1588 GtkSelectionData *data;
1589 gboolean result = FALSE;
1591 g_return_val_if_fail (clipboard != NULL, FALSE);
1593 /* If the display supports change notification we cache targets */
1594 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1595 clipboard->n_cached_targets != -1)
1598 *n_targets = clipboard->n_cached_targets;
1601 *targets = g_memdup (clipboard->cached_targets,
1602 clipboard->n_cached_targets * sizeof (GdkAtom));
1613 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1617 GdkAtom *tmp_targets;
1620 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1622 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1624 clipboard->n_cached_targets = tmp_n_targets;
1625 clipboard->cached_targets = g_memdup (tmp_targets,
1626 tmp_n_targets * sizeof (GdkAtom));
1630 *n_targets = tmp_n_targets;
1633 *targets = tmp_targets;
1635 g_free (tmp_targets);
1637 gtk_selection_data_free (data);
1643 static GtkClipboard *
1644 clipboard_peek (GdkDisplay *display,
1646 gboolean only_if_exists)
1648 GtkClipboard *clipboard = NULL;
1652 if (selection == GDK_NONE)
1653 selection = GDK_SELECTION_CLIPBOARD;
1655 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1657 tmp_list = clipboards;
1660 clipboard = tmp_list->data;
1661 if (clipboard->selection == selection)
1664 tmp_list = tmp_list->next;
1667 if (!tmp_list && !only_if_exists)
1669 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1670 clipboard->selection = selection;
1671 clipboard->display = display;
1672 clipboard->n_cached_targets = -1;
1673 clipboard->n_storable_targets = -1;
1674 clipboards = g_slist_prepend (clipboards, clipboard);
1675 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1676 g_signal_connect (display, "closed",
1677 G_CALLBACK (clipboard_display_closed), clipboard);
1678 gdk_display_request_selection_notification (display, selection);
1685 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1686 GdkEventOwnerChange *event)
1688 if (clipboard->n_cached_targets != -1)
1690 clipboard->n_cached_targets = -1;
1691 g_free (clipboard->cached_targets);
1696 * gtk_clipboard_wait_is_target_available:
1697 * @clipboard: a #GtkClipboard
1698 * @target: A #GdkAtom indicating which target to look for.
1700 * Checks if a clipboard supports pasting data of a given type. This
1701 * function can be used to determine if a "Paste" menu item should be
1702 * insensitive or not.
1704 * If you want to see if there's text available on the clipboard, use
1705 * gtk_clipboard_wait_is_text_available () instead.
1707 * Return value: %TRUE if the target is available, %FALSE otherwise.
1712 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1717 gboolean retval = FALSE;
1719 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1722 for (i = 0; i < n_targets; i++)
1724 if (targets[i] == target)
1737 * _gtk_clipboard_handle_event:
1738 * @event: a owner change event
1740 * Emits the #GtkClipboard::owner-change signal on the appropriate @clipboard.
1745 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1747 GdkDisplay *display;
1748 GtkClipboard *clipboard;
1750 display = gdk_drawable_get_display (event->window);
1751 clipboard = clipboard_peek (display, event->selection, TRUE);
1754 g_signal_emit (clipboard,
1755 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1759 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1761 g_main_loop_quit (clipboard->store_loop);
1767 * gtk_clipboard_set_can_store:
1768 * @clipboard: a #GtkClipboard
1769 * @targets: array containing information about which forms should be stored
1770 * or %NULL to indicate that all forms should be stored.
1771 * @n_targets: number of elements in @targets
1773 * Hints that the clipboard data should be stored somewhere when the
1774 * application exits or when gtk_clipboard_store () is called.
1776 * This value is reset when the clipboard owner changes.
1777 * Where the clipboard data is stored is platform dependent,
1778 * see gdk_display_store_clipboard () for more information.
1783 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1784 const GtkTargetEntry *targets,
1787 GtkWidget *clipboard_widget;
1789 static const GtkTargetEntry save_targets[] = {
1790 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1793 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1794 g_return_if_fail (n_targets >= 0);
1796 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1799 g_free (clipboard->storable_targets);
1801 clipboard_widget = get_clipboard_widget (clipboard->display);
1803 /* n_storable_targets being -1 means that
1804 * gtk_clipboard_set_can_store hasn't been called since the
1805 * clipboard owner changed. We only want to add SAVE_TARGETS and
1806 * ref the owner once , so we do that here
1808 if (clipboard->n_storable_targets == -1)
1810 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1813 /* Ref the owner so it won't go away */
1814 if (clipboard->have_owner)
1815 g_object_ref (clipboard->user_data);
1818 clipboard->n_storable_targets = n_targets;
1819 clipboard->storable_targets = g_new (GdkAtom, n_targets);
1820 for (i = 0; i < n_targets; i++)
1821 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1825 gtk_clipboard_selection_notify (GtkWidget *widget,
1826 GdkEventSelection *event,
1827 GtkClipboard *clipboard)
1829 if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
1830 clipboard->storing_selection)
1831 g_main_loop_quit (clipboard->store_loop);
1837 * gtk_clipboard_store:
1838 * @clipboard: a #GtkClipboard
1840 * Stores the current clipboard data somewhere so that it will stay
1841 * around after the application has quit.
1846 gtk_clipboard_store (GtkClipboard *clipboard)
1848 GtkWidget *clipboard_widget;
1850 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1852 if (clipboard->n_storable_targets < 0)
1855 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
1858 g_object_ref (clipboard);
1860 clipboard_widget = get_clipboard_widget (clipboard->display);
1861 clipboard->notify_signal_id = g_signal_connect (clipboard_widget, "selection_notify_event",
1862 G_CALLBACK (gtk_clipboard_selection_notify), clipboard);
1864 gdk_display_store_clipboard (clipboard->display,
1865 clipboard_widget->window,
1866 clipboard_get_timestamp (clipboard),
1867 clipboard->storable_targets,
1868 clipboard->n_storable_targets);
1870 clipboard->storing_selection = TRUE;
1872 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
1873 clipboard->store_timeout = g_timeout_add (10000, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
1875 if (g_main_loop_is_running (clipboard->store_loop))
1877 GDK_THREADS_LEAVE ();
1878 g_main_loop_run (clipboard->store_loop);
1879 GDK_THREADS_ENTER ();
1882 g_main_loop_unref (clipboard->store_loop);
1883 clipboard->store_loop = NULL;
1885 g_source_remove (clipboard->store_timeout);
1886 clipboard->store_timeout = 0;
1887 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
1888 clipboard->notify_signal_id = 0;
1890 clipboard->storing_selection = FALSE;
1892 g_object_unref (clipboard);
1895 /* Stores all clipboard selections on all displays, called from
1899 _gtk_clipboard_store_all (void)
1901 GtkClipboard *clipboard;
1902 GSList *displays, *list;
1904 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
1909 GdkDisplay *display = list->data;
1911 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
1914 gtk_clipboard_store (clipboard);
1918 g_slist_free (displays);
1922 #define __GTK_CLIPBOARD_C__
1923 #include "gtkaliasdef.c"