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 _RequestURIInfo RequestURIInfo;
54 typedef struct _RequestTargetsInfo RequestTargetsInfo;
58 GObject parent_instance;
62 GtkClipboardGetFunc get_func;
63 GtkClipboardClearFunc clear_func;
69 gboolean have_selection;
72 GdkAtom *cached_targets;
73 gint n_cached_targets;
75 guint notify_signal_id;
76 gboolean storing_selection;
77 GMainLoop *store_loop;
79 gint n_storable_targets;
80 GdkAtom *storable_targets;
83 struct _GtkClipboardClass
85 GObjectClass parent_class;
87 void (*owner_change) (GtkClipboard *clipboard,
88 GdkEventOwnerChange *event);
91 struct _RequestContentsInfo
93 GtkClipboardReceivedFunc callback;
97 struct _RequestTextInfo
99 GtkClipboardTextReceivedFunc callback;
103 struct _RequestRichTextInfo
105 GtkClipboardRichTextReceivedFunc callback;
112 struct _RequestImageInfo
114 GtkClipboardImageReceivedFunc callback;
118 struct _RequestURIInfo
120 GtkClipboardURIReceivedFunc callback;
124 struct _RequestTargetsInfo
126 GtkClipboardTargetsReceivedFunc callback;
130 static void gtk_clipboard_class_init (GtkClipboardClass *class);
131 static void gtk_clipboard_finalize (GObject *object);
132 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
133 GdkEventOwnerChange *event);
135 static void clipboard_unset (GtkClipboard *clipboard);
136 static void selection_received (GtkWidget *widget,
137 GtkSelectionData *selection_data,
139 static GtkClipboard *clipboard_peek (GdkDisplay *display,
141 gboolean only_if_exists);
142 static GtkWidget * get_clipboard_widget (GdkDisplay *display);
148 TARGET_COMPOUND_TEXT,
153 static const gchar request_contents_key[] = "gtk-request-contents";
154 static GQuark request_contents_key_id = 0;
156 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
157 static GQuark clipboards_owned_key_id = 0;
159 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
161 G_DEFINE_TYPE (GtkClipboard, gtk_clipboard, G_TYPE_OBJECT)
164 gtk_clipboard_init (GtkClipboard *object)
169 gtk_clipboard_class_init (GtkClipboardClass *class)
171 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
173 gobject_class->finalize = gtk_clipboard_finalize;
175 class->owner_change = gtk_clipboard_owner_change;
178 * GtkClipboard::owner-change:
179 * @clipboard: the #GtkClipboard on which the signal is emitted
180 * @event: the @GdkEventOwnerChange event
182 * The ::owner-change signal is emitted when GTK+ receives an
183 * event that indicates that the ownership of the selection
184 * associated with @clipboard has changed.
188 clipboard_signals[OWNER_CHANGE] =
189 g_signal_new (I_("owner-change"),
190 G_TYPE_FROM_CLASS (gobject_class),
192 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
194 _gtk_marshal_VOID__BOXED,
196 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
200 gtk_clipboard_finalize (GObject *object)
202 GtkClipboard *clipboard;
203 GtkWidget *clipboard_widget = NULL;
204 GSList *clipboards = NULL;
206 clipboard = GTK_CLIPBOARD (object);
208 if (clipboard->display)
210 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
212 if (g_slist_index (clipboards, clipboard) >= 0)
213 g_warning ("GtkClipboard prematurely finalized");
215 clipboards = g_slist_remove (clipboards, clipboard);
217 g_object_set_data (G_OBJECT (clipboard->display), "gtk-clipboard-list",
220 /* don't use get_clipboard_widget() here because it would create the
221 * widget if it doesn't exist.
223 clipboard_widget = g_object_get_data (G_OBJECT (clipboard->display),
224 "gtk-clipboard-widget");
227 clipboard_unset (clipboard);
229 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
230 g_main_loop_quit (clipboard->store_loop);
232 if (clipboard->store_timeout != 0)
233 g_source_remove (clipboard->store_timeout);
235 if (clipboard->notify_signal_id != 0)
236 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
238 g_free (clipboard->storable_targets);
239 g_free (clipboard->cached_targets);
241 G_OBJECT_CLASS (gtk_clipboard_parent_class)->finalize (object);
245 clipboard_display_closed (GdkDisplay *display,
247 GtkClipboard *clipboard)
251 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
252 g_object_run_dispose (G_OBJECT (clipboard));
253 clipboards = g_slist_remove (clipboards, clipboard);
254 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
255 g_object_unref (clipboard);
259 * gtk_clipboard_get_for_display:
260 * @display: the display for which the clipboard is to be retrieved or created
261 * @selection: a #GdkAtom which identifies the clipboard
264 * Returns the clipboard object for the given selection.
265 * Cut/copy/paste menu items and keyboard shortcuts should use
266 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
267 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
268 * for backwards compatibility reasons.)
269 * The currently-selected object or text should be provided on the clipboard
270 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
271 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
272 * to the default clipboard, i.e. they copy the selection to what the
273 * user sees as the clipboard.
275 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
276 * ("CLIPBOARD", FALSE)</literal>. See <ulink
277 * url="http://www.freedesktop.org/Standards/clipboards-spec">
278 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
279 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
280 * selections under the X window system. On Win32 the
281 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
283 * It's possible to have arbitrary named clipboards; if you do invent
284 * new clipboards, you should prefix the selection name with an
285 * underscore (because the ICCCM requires that nonstandard atoms are
286 * underscore-prefixed), and namespace it as well. For example,
287 * if your application called "Foo" has a special-purpose
288 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
290 * Return value: the appropriate clipboard object. If no
291 * clipboard already exists, a new one will
292 * be created. Once a clipboard object has
293 * been created, it is persistent and, since
294 * it is owned by GTK+, must not be freed or
300 gtk_clipboard_get_for_display (GdkDisplay *display,
303 g_return_val_if_fail (display != NULL, NULL); /* See bgo#463773; this is needed because Flash Player sucks */
304 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
305 g_return_val_if_fail (!display->closed, NULL);
307 return clipboard_peek (display, selection, FALSE);
313 * @selection: a #GdkAtom which identifies the clipboard to use
315 * Returns the clipboard object for the given selection.
316 * See gtk_clipboard_get_for_display() for complete details.
318 * Return value: the appropriate clipboard object. If no clipboard
319 * already exists, a new one will be created. Once a clipboard
320 * object has been created, it is persistent and, since it is
321 * owned by GTK+, must not be freed or unreffed.
324 gtk_clipboard_get (GdkAtom selection)
326 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
330 selection_get_cb (GtkWidget *widget,
331 GtkSelectionData *selection_data,
335 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, selection_data->selection);
337 if (clipboard && clipboard->get_func)
338 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
342 selection_clear_event_cb (GtkWidget *widget,
343 GdkEventSelection *event)
345 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
349 clipboard_unset (clipboard);
358 make_clipboard_widget (GdkDisplay *display,
361 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
363 g_signal_connect (widget, "selection-received",
364 G_CALLBACK (selection_received), NULL);
368 /* We need this for gdk_x11_get_server_time() */
369 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
371 g_signal_connect (widget, "selection-get",
372 G_CALLBACK (selection_get_cb), NULL);
373 g_signal_connect (widget, "selection-clear-event",
374 G_CALLBACK (selection_clear_event_cb), NULL);
381 get_clipboard_widget (GdkDisplay *display)
383 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
386 clip_widget = make_clipboard_widget (display, TRUE);
387 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
393 /* This function makes a very good guess at what the correct
394 * timestamp for a selection request should be. If there is
395 * a currently processed event, it uses the timestamp for that
396 * event, otherwise it uses the current server time. However,
397 * if the time resulting from that is older than the time used
398 * last time, it uses the time used last time instead.
400 * In order implement this correctly, we never use CurrentTime,
401 * but actually retrieve the actual timestamp from the server.
402 * This is a little slower but allows us to make the guarantee
403 * that the times used by this application will always ascend
404 * and we won't get selections being rejected just because
405 * we are using a correct timestamp from an event, but used
406 * CurrentTime previously.
409 clipboard_get_timestamp (GtkClipboard *clipboard)
411 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
412 guint32 timestamp = gtk_get_current_event_time ();
414 if (timestamp == GDK_CURRENT_TIME)
416 #ifdef GDK_WINDOWING_X11
417 timestamp = gdk_x11_get_server_time (clipboard_widget->window);
418 #elif defined GDK_WINDOWING_WIN32
419 timestamp = GetMessageTime ();
424 if (clipboard->timestamp != GDK_CURRENT_TIME)
426 /* Check to see if clipboard->timestamp is newer than
427 * timestamp, accounting for wraparound.
430 guint32 max = timestamp + 0x80000000;
432 if ((max > timestamp &&
433 (clipboard->timestamp > timestamp &&
434 clipboard->timestamp <= max)) ||
436 (clipboard->timestamp > timestamp ||
437 clipboard->timestamp <= max)))
439 timestamp = clipboard->timestamp;
444 clipboard->timestamp = timestamp;
450 clipboard_owner_destroyed (gpointer data)
452 GSList *clipboards = data;
455 tmp_list = clipboards;
458 GtkClipboard *clipboard = tmp_list->data;
460 clipboard->get_func = NULL;
461 clipboard->clear_func = NULL;
462 clipboard->user_data = NULL;
463 clipboard->have_owner = FALSE;
465 gtk_clipboard_clear (clipboard);
467 tmp_list = tmp_list->next;
470 g_slist_free (clipboards);
474 clipboard_add_owner_notify (GtkClipboard *clipboard)
476 if (!clipboards_owned_key_id)
477 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
479 if (clipboard->have_owner)
480 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
481 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
482 clipboards_owned_key_id),
484 clipboard_owner_destroyed);
488 clipboard_remove_owner_notify (GtkClipboard *clipboard)
490 if (clipboard->have_owner)
491 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
492 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
493 clipboards_owned_key_id),
495 clipboard_owner_destroyed);
499 gtk_clipboard_set_contents (GtkClipboard *clipboard,
500 const GtkTargetEntry *targets,
502 GtkClipboardGetFunc get_func,
503 GtkClipboardClearFunc clear_func,
507 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
509 if (gtk_selection_owner_set_for_display (clipboard->display,
511 clipboard->selection,
512 clipboard_get_timestamp (clipboard)))
514 clipboard->have_selection = TRUE;
516 if (clipboard->n_cached_targets != -1)
518 g_free (clipboard->cached_targets);
519 clipboard->cached_targets = NULL;
520 clipboard->n_cached_targets = -1;
523 if (!(clipboard->have_owner && have_owner) ||
524 clipboard->user_data != user_data)
526 clipboard_unset (clipboard);
528 if (clipboard->get_func)
530 /* Calling unset() caused the clipboard contents to be reset!
531 * Avoid leaking and return
533 if (!(clipboard->have_owner && have_owner) ||
534 clipboard->user_data != user_data)
536 (*clear_func) (clipboard, user_data);
544 clipboard->user_data = user_data;
545 clipboard->have_owner = have_owner;
547 clipboard_add_owner_notify (clipboard);
552 clipboard->get_func = get_func;
553 clipboard->clear_func = clear_func;
555 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
556 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
566 * gtk_clipboard_set_with_data:
567 * @clipboard: a #GtkClipboard
568 * @targets: array containing information about the available forms for the
570 * @n_targets: number of elements in @targets
571 * @get_func: function to call to get the actual clipboard data
572 * @clear_func: when the clipboard contents are set again, this function will
573 * be called, and @get_func will not be subsequently called.
574 * @user_data: user data to pass to @get_func and @clear_func.
576 * Virtually sets the contents of the specified clipboard by providing
577 * a list of supported formats for the clipboard data and a function
578 * to call to get the actual data when it is requested.
580 * Return value: %TRUE if setting the clipboard data succeeded. If setting
581 * the clipboard data failed the provided callback functions
585 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
586 const GtkTargetEntry *targets,
588 GtkClipboardGetFunc get_func,
589 GtkClipboardClearFunc clear_func,
592 g_return_val_if_fail (clipboard != NULL, FALSE);
593 g_return_val_if_fail (targets != NULL, FALSE);
594 g_return_val_if_fail (get_func != NULL, FALSE);
596 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
597 get_func, clear_func, user_data,
602 * gtk_clipboard_set_with_owner:
603 * @clipboard: a #GtkClipboard
604 * @targets: array containing information about the available forms for the
606 * @n_targets: number of elements in @targets
607 * @get_func: function to call to get the actual clipboard data
608 * @clear_func: when the clipboard contents are set again, this function will
609 * be called, and @get_func will not be subsequently called.
610 * @owner: an object that "owns" the data. This object will be passed
611 * to the callbacks when called.
613 * Virtually sets the contents of the specified clipboard by providing
614 * a list of supported formats for the clipboard data and a function
615 * to call to get the actual data when it is requested.
617 * The difference between this function and gtk_clipboard_set_with_data()
618 * is that instead of an generic @user_data pointer, a #GObject is passed
621 * Return value: %TRUE if setting the clipboard data succeeded. If setting
622 * the clipboard data failed the provided callback functions
626 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
627 const GtkTargetEntry *targets,
629 GtkClipboardGetFunc get_func,
630 GtkClipboardClearFunc clear_func,
633 g_return_val_if_fail (clipboard != NULL, FALSE);
634 g_return_val_if_fail (targets != NULL, FALSE);
635 g_return_val_if_fail (get_func != NULL, FALSE);
636 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
638 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
639 get_func, clear_func, owner,
644 * gtk_clipboard_get_owner:
645 * @clipboard: a #GtkClipboard
647 * If the clipboard contents callbacks were set with
648 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
649 * gtk_clipboard_clear() has not subsequently called, returns the owner set
650 * by gtk_clipboard_set_with_owner().
652 * Return value: the owner of the clipboard, if any; otherwise %NULL.
655 gtk_clipboard_get_owner (GtkClipboard *clipboard)
657 g_return_val_if_fail (clipboard != NULL, NULL);
659 if (clipboard->have_owner)
660 return clipboard->user_data;
666 clipboard_unset (GtkClipboard *clipboard)
668 GtkClipboardClearFunc old_clear_func;
670 gboolean old_have_owner;
671 gint old_n_storable_targets;
673 old_clear_func = clipboard->clear_func;
674 old_data = clipboard->user_data;
675 old_have_owner = clipboard->have_owner;
676 old_n_storable_targets = clipboard->n_storable_targets;
680 clipboard_remove_owner_notify (clipboard);
681 clipboard->have_owner = FALSE;
684 clipboard->n_storable_targets = -1;
685 g_free (clipboard->storable_targets);
686 clipboard->storable_targets = NULL;
688 clipboard->get_func = NULL;
689 clipboard->clear_func = NULL;
690 clipboard->user_data = NULL;
693 old_clear_func (clipboard, old_data);
695 /* If we've transferred the clipboard data to the manager,
698 if (old_have_owner &&
699 old_n_storable_targets != -1)
700 g_object_unref (old_data);
704 * gtk_clipboard_clear:
705 * @clipboard: a #GtkClipboard
707 * Clears the contents of the clipboard. Generally this should only
708 * be called between the time you call gtk_clipboard_set_with_owner()
709 * or gtk_clipboard_set_with_data(),
710 * and when the @clear_func you supplied is called. Otherwise, the
711 * clipboard may be owned by someone else.
714 gtk_clipboard_clear (GtkClipboard *clipboard)
716 g_return_if_fail (clipboard != NULL);
718 if (clipboard->have_selection)
719 gtk_selection_owner_set_for_display (clipboard->display,
721 clipboard->selection,
722 clipboard_get_timestamp (clipboard));
726 text_get_func (GtkClipboard *clipboard,
727 GtkSelectionData *selection_data,
731 gtk_selection_data_set_text (selection_data, data, -1);
735 text_clear_func (GtkClipboard *clipboard,
743 * gtk_clipboard_set_text:
744 * @clipboard: a #GtkClipboard object
745 * @text: a UTF-8 string.
746 * @len: length of @text, in bytes, or -1, in which case
747 * the length will be determined with <function>strlen()</function>.
749 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
750 * make a copy of the text and take responsibility for responding
751 * for requests for the text, and for converting the text into
752 * the requested format.
755 gtk_clipboard_set_text (GtkClipboard *clipboard,
760 GtkTargetEntry *targets;
763 g_return_if_fail (clipboard != NULL);
764 g_return_if_fail (text != NULL);
766 list = gtk_target_list_new (NULL, 0);
767 gtk_target_list_add_text_targets (list, 0);
769 targets = gtk_target_table_new_from_list (list, &n_targets);
774 gtk_clipboard_set_with_data (clipboard,
776 text_get_func, text_clear_func,
777 g_strndup (text, len));
778 gtk_clipboard_set_can_store (clipboard, NULL, 0);
780 gtk_target_table_free (targets, n_targets);
781 gtk_target_list_unref (list);
785 pixbuf_get_func (GtkClipboard *clipboard,
786 GtkSelectionData *selection_data,
790 gtk_selection_data_set_pixbuf (selection_data, data);
794 pixbuf_clear_func (GtkClipboard *clipboard,
797 g_object_unref (data);
801 * gtk_clipboard_set_image:
802 * @clipboard: a #GtkClipboard object
803 * @pixbuf: a #GdkPixbuf
805 * Sets the contents of the clipboard to the given #GdkPixbuf.
806 * GTK+ will take responsibility for responding for requests
807 * for the image, and for converting the image into the
813 gtk_clipboard_set_image (GtkClipboard *clipboard,
817 GtkTargetEntry *targets;
820 g_return_if_fail (clipboard != NULL);
821 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
823 list = gtk_target_list_new (NULL, 0);
824 gtk_target_list_add_image_targets (list, 0, TRUE);
826 targets = gtk_target_table_new_from_list (list, &n_targets);
828 gtk_clipboard_set_with_data (clipboard,
830 pixbuf_get_func, pixbuf_clear_func,
831 g_object_ref (pixbuf));
832 gtk_clipboard_set_can_store (clipboard, NULL, 0);
834 gtk_target_table_free (targets, n_targets);
835 gtk_target_list_unref (list);
839 set_request_contents_info (GtkWidget *widget,
840 RequestContentsInfo *info)
842 if (!request_contents_key_id)
843 request_contents_key_id = g_quark_from_static_string (request_contents_key);
845 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
848 static RequestContentsInfo *
849 get_request_contents_info (GtkWidget *widget)
851 if (!request_contents_key_id)
854 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
858 selection_received (GtkWidget *widget,
859 GtkSelectionData *selection_data,
862 RequestContentsInfo *request_info = get_request_contents_info (widget);
863 set_request_contents_info (widget, NULL);
865 request_info->callback (gtk_widget_get_clipboard (widget, selection_data->selection),
867 request_info->user_data);
869 g_free (request_info);
871 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
872 gtk_widget_destroy (widget);
876 * gtk_clipboard_request_contents:
877 * @clipboard: a #GtkClipboard
878 * @target: an atom representing the form into which the clipboard
879 * owner should convert the selection.
880 * @callback: A function to call when the results are received
881 * (or the retrieval fails). If the retrieval fails
882 * the length field of @selection_data will be
884 * @user_data: user data to pass to @callback
886 * Requests the contents of clipboard as the given target.
887 * When the results of the result are later received the supplied callback
891 gtk_clipboard_request_contents (GtkClipboard *clipboard,
893 GtkClipboardReceivedFunc callback,
896 RequestContentsInfo *info;
898 GtkWidget *clipboard_widget;
900 g_return_if_fail (clipboard != NULL);
901 g_return_if_fail (target != GDK_NONE);
902 g_return_if_fail (callback != NULL);
904 clipboard_widget = get_clipboard_widget (clipboard->display);
906 if (get_request_contents_info (clipboard_widget))
907 widget = make_clipboard_widget (clipboard->display, FALSE);
909 widget = clipboard_widget;
911 info = g_new (RequestContentsInfo, 1);
912 info->callback = callback;
913 info->user_data = user_data;
915 set_request_contents_info (widget, info);
917 gtk_selection_convert (widget, clipboard->selection, target,
918 clipboard_get_timestamp (clipboard));
922 request_text_received_func (GtkClipboard *clipboard,
923 GtkSelectionData *selection_data,
926 RequestTextInfo *info = data;
927 gchar *result = NULL;
929 result = (gchar *) gtk_selection_data_get_text (selection_data);
933 /* If we asked for UTF8 and didn't get it, try compound_text;
934 * if we asked for compound_text and didn't get it, try string;
935 * If we asked for anything else and didn't get it, give up.
937 if (selection_data->target == gdk_atom_intern_static_string ("UTF8_STRING"))
939 gtk_clipboard_request_contents (clipboard,
940 gdk_atom_intern_static_string ("COMPOUND_TEXT"),
941 request_text_received_func, info);
944 else if (selection_data->target == gdk_atom_intern_static_string ("COMPOUND_TEXT"))
946 gtk_clipboard_request_contents (clipboard,
948 request_text_received_func, info);
953 info->callback (clipboard, result, info->user_data);
959 * gtk_clipboard_request_text:
960 * @clipboard: a #GtkClipboard
961 * @callback: a function to call when the text is received,
962 * or the retrieval fails. (It will always be called
963 * one way or the other.)
964 * @user_data: user data to pass to @callback.
966 * Requests the contents of the clipboard as text. When the text is
967 * later received, it will be converted to UTF-8 if necessary, and
968 * @callback will be called.
970 * The @text parameter to @callback will contain the resulting text if
971 * the request succeeded, or %NULL if it failed. This could happen for
972 * various reasons, in particular if the clipboard was empty or if the
973 * contents of the clipboard could not be converted into text form.
976 gtk_clipboard_request_text (GtkClipboard *clipboard,
977 GtkClipboardTextReceivedFunc callback,
980 RequestTextInfo *info;
982 g_return_if_fail (clipboard != NULL);
983 g_return_if_fail (callback != NULL);
985 info = g_new (RequestTextInfo, 1);
986 info->callback = callback;
987 info->user_data = user_data;
989 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("UTF8_STRING"),
990 request_text_received_func,
995 request_rich_text_received_func (GtkClipboard *clipboard,
996 GtkSelectionData *selection_data,
999 RequestRichTextInfo *info = data;
1000 guint8 *result = NULL;
1003 result = selection_data->data;
1004 length = selection_data->length;
1006 info->current_atom++;
1008 if ((!result || length < 1) && (info->current_atom < info->n_atoms))
1010 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1011 request_rich_text_received_func,
1016 info->callback (clipboard, selection_data->target, result, length,
1018 g_free (info->atoms);
1023 * gtk_clipboard_request_rich_text:
1024 * @clipboard: a #GtkClipboard
1025 * @buffer: a #GtkTextBuffer
1026 * @callback: a function to call when the text is received,
1027 * or the retrieval fails. (It will always be called
1028 * one way or the other.)
1029 * @user_data: user data to pass to @callback.
1031 * Requests the contents of the clipboard as rich text. When the rich
1032 * text is later received, @callback will be called.
1034 * The @text parameter to @callback will contain the resulting rich
1035 * text if the request succeeded, or %NULL if it failed. The @length
1036 * parameter will contain @text's length. This function can fail for
1037 * various reasons, in particular if the clipboard was empty or if the
1038 * contents of the clipboard could not be converted into rich text form.
1043 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
1044 GtkTextBuffer *buffer,
1045 GtkClipboardRichTextReceivedFunc callback,
1048 RequestRichTextInfo *info;
1050 g_return_if_fail (clipboard != NULL);
1051 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1052 g_return_if_fail (callback != NULL);
1054 info = g_new (RequestRichTextInfo, 1);
1055 info->callback = callback;
1058 info->current_atom = 0;
1059 info->user_data = user_data;
1061 info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
1063 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1064 request_rich_text_received_func,
1069 request_image_received_func (GtkClipboard *clipboard,
1070 GtkSelectionData *selection_data,
1073 RequestImageInfo *info = data;
1074 GdkPixbuf *result = NULL;
1076 result = gtk_selection_data_get_pixbuf (selection_data);
1080 /* If we asked for image/png and didn't get it, try image/jpeg;
1081 * if we asked for image/jpeg and didn't get it, try image/gif;
1082 * if we asked for image/gif and didn't get it, try image/bmp;
1083 * If we asked for anything else and didn't get it, give up.
1085 if (selection_data->target == gdk_atom_intern_static_string ("image/png"))
1087 gtk_clipboard_request_contents (clipboard,
1088 gdk_atom_intern_static_string ("image/jpeg"),
1089 request_image_received_func, info);
1092 else if (selection_data->target == gdk_atom_intern_static_string ("image/jpeg"))
1094 gtk_clipboard_request_contents (clipboard,
1095 gdk_atom_intern_static_string ("image/gif"),
1096 request_image_received_func, info);
1099 else if (selection_data->target == gdk_atom_intern_static_string ("image/gif"))
1101 gtk_clipboard_request_contents (clipboard,
1102 gdk_atom_intern_static_string ("image/bmp"),
1103 request_image_received_func, info);
1108 info->callback (clipboard, result, info->user_data);
1112 g_object_unref (result);
1116 * gtk_clipboard_request_image:
1117 * @clipboard: a #GtkClipboard
1118 * @callback: a function to call when the image is received,
1119 * or the retrieval fails. (It will always be called
1120 * one way or the other.)
1121 * @user_data: user data to pass to @callback.
1123 * Requests the contents of the clipboard as image. When the image is
1124 * later received, it will be converted to a #GdkPixbuf, and
1125 * @callback will be called.
1127 * The @pixbuf parameter to @callback will contain the resulting
1128 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1129 * could happen for various reasons, in particular if the clipboard
1130 * was empty or if the contents of the clipboard could not be
1131 * converted into an image.
1136 gtk_clipboard_request_image (GtkClipboard *clipboard,
1137 GtkClipboardImageReceivedFunc callback,
1140 RequestImageInfo *info;
1142 g_return_if_fail (clipboard != NULL);
1143 g_return_if_fail (callback != NULL);
1145 info = g_new (RequestImageInfo, 1);
1146 info->callback = callback;
1147 info->user_data = user_data;
1149 gtk_clipboard_request_contents (clipboard,
1150 gdk_atom_intern_static_string ("image/png"),
1151 request_image_received_func,
1156 request_uris_received_func (GtkClipboard *clipboard,
1157 GtkSelectionData *selection_data,
1160 RequestURIInfo *info = data;
1163 uris = gtk_selection_data_get_uris (selection_data);
1164 info->callback (clipboard, uris, info->user_data);
1167 g_slice_free (RequestURIInfo, info);
1171 * gtk_clipboard_request_uris:
1172 * @clipboard: a #GtkClipboard
1173 * @callback: a function to call when the URIs are received,
1174 * or the retrieval fails. (It will always be called
1175 * one way or the other.)
1176 * @user_data: user data to pass to @callback.
1178 * Requests the contents of the clipboard as URIs. When the URIs are
1179 * later received @callback will be called.
1181 * The @uris parameter to @callback will contain the resulting array of
1182 * URIs if the request succeeded, or %NULL if it failed. This could happen
1183 * for various reasons, in particular if the clipboard was empty or if the
1184 * contents of the clipboard could not be converted into URI form.
1189 gtk_clipboard_request_uris (GtkClipboard *clipboard,
1190 GtkClipboardURIReceivedFunc callback,
1193 RequestURIInfo *info;
1195 g_return_if_fail (clipboard != NULL);
1196 g_return_if_fail (callback != NULL);
1198 info = g_slice_new (RequestURIInfo);
1199 info->callback = callback;
1200 info->user_data = user_data;
1202 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"),
1203 request_uris_received_func,
1208 request_targets_received_func (GtkClipboard *clipboard,
1209 GtkSelectionData *selection_data,
1212 RequestTargetsInfo *info = data;
1213 GdkAtom *targets = NULL;
1216 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1218 info->callback (clipboard, targets, n_targets, info->user_data);
1225 * gtk_clipboard_request_targets:
1226 * @clipboard: a #GtkClipboard
1227 * @callback: a function to call when the targets are received,
1228 * or the retrieval fails. (It will always be called
1229 * one way or the other.)
1230 * @user_data: user data to pass to @callback.
1232 * Requests the contents of the clipboard as list of supported targets.
1233 * When the list is later received, @callback will be called.
1235 * The @targets parameter to @callback will contain the resulting targets if
1236 * the request succeeded, or %NULL if it failed.
1241 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1242 GtkClipboardTargetsReceivedFunc callback,
1245 RequestTargetsInfo *info;
1247 g_return_if_fail (clipboard != NULL);
1248 g_return_if_fail (callback != NULL);
1250 /* If the display supports change notification we cache targets */
1251 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1252 clipboard->n_cached_targets != -1)
1254 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1258 info = g_new (RequestTargetsInfo, 1);
1259 info->callback = callback;
1260 info->user_data = user_data;
1262 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"),
1263 request_targets_received_func,
1271 GdkAtom format; /* used by rich text */
1272 gsize length; /* used by rich text */
1276 clipboard_received_func (GtkClipboard *clipboard,
1277 GtkSelectionData *selection_data,
1280 WaitResults *results = data;
1282 if (selection_data->length >= 0)
1283 results->data = gtk_selection_data_copy (selection_data);
1285 g_main_loop_quit (results->loop);
1289 * gtk_clipboard_wait_for_contents:
1290 * @clipboard: a #GtkClipboard
1291 * @target: an atom representing the form into which the clipboard
1292 * owner should convert the selection.
1294 * Requests the contents of the clipboard using the given target.
1295 * This function waits for the data to be received using the main
1296 * loop, so events, timeouts, etc, may be dispatched during the wait.
1298 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1299 * if retrieving the given target failed. If non-%NULL,
1300 * this value must be freed with gtk_selection_data_free()
1301 * when you are finished with it.
1304 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1307 WaitResults results;
1309 g_return_val_if_fail (clipboard != NULL, NULL);
1310 g_return_val_if_fail (target != GDK_NONE, NULL);
1312 results.data = NULL;
1313 results.loop = g_main_loop_new (NULL, TRUE);
1315 gtk_clipboard_request_contents (clipboard, target,
1316 clipboard_received_func,
1319 if (g_main_loop_is_running (results.loop))
1321 GDK_THREADS_LEAVE ();
1322 g_main_loop_run (results.loop);
1323 GDK_THREADS_ENTER ();
1326 g_main_loop_unref (results.loop);
1328 return results.data;
1332 clipboard_text_received_func (GtkClipboard *clipboard,
1336 WaitResults *results = data;
1338 results->data = g_strdup (text);
1339 g_main_loop_quit (results->loop);
1343 * gtk_clipboard_wait_for_text:
1344 * @clipboard: a #GtkClipboard
1346 * Requests the contents of the clipboard as text and converts
1347 * the result to UTF-8 if necessary. This function waits for
1348 * the data to be received using the main loop, so events,
1349 * timeouts, etc, may be dispatched during the wait.
1351 * Return value: a newly-allocated UTF-8 string which must
1352 * be freed with g_free(), or %NULL if retrieving
1353 * the selection data failed. (This could happen
1354 * for various reasons, in particular if the
1355 * clipboard was empty or if the contents of the
1356 * clipboard could not be converted into text form.)
1359 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1361 WaitResults results;
1363 g_return_val_if_fail (clipboard != NULL, NULL);
1365 results.data = NULL;
1366 results.loop = g_main_loop_new (NULL, TRUE);
1368 gtk_clipboard_request_text (clipboard,
1369 clipboard_text_received_func,
1372 if (g_main_loop_is_running (results.loop))
1374 GDK_THREADS_LEAVE ();
1375 g_main_loop_run (results.loop);
1376 GDK_THREADS_ENTER ();
1379 g_main_loop_unref (results.loop);
1381 return results.data;
1385 clipboard_rich_text_received_func (GtkClipboard *clipboard,
1391 WaitResults *results = data;
1393 results->data = g_memdup (text, length);
1394 results->format = format;
1395 results->length = length;
1396 g_main_loop_quit (results->loop);
1400 * gtk_clipboard_wait_for_rich_text:
1401 * @clipboard: a #GtkClipboard
1402 * @buffer: a #GtkTextBuffer
1403 * @format: return location for the format of the returned data
1404 * @length: return location for the length of the returned data
1406 * Requests the contents of the clipboard as rich text. This function
1407 * waits for the data to be received using the main loop, so events,
1408 * timeouts, etc, may be dispatched during the wait.
1410 * Return value: a newly-allocated binary block of data which must
1411 * be freed with g_free(), or %NULL if retrieving
1412 * the selection data failed. (This could happen
1413 * for various reasons, in particular if the
1414 * clipboard was empty or if the contents of the
1415 * clipboard could not be converted into text form.)
1420 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
1421 GtkTextBuffer *buffer,
1425 WaitResults results;
1427 g_return_val_if_fail (clipboard != NULL, NULL);
1428 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1429 g_return_val_if_fail (format != NULL, NULL);
1430 g_return_val_if_fail (length != NULL, NULL);
1432 results.data = NULL;
1433 results.loop = g_main_loop_new (NULL, TRUE);
1435 gtk_clipboard_request_rich_text (clipboard, buffer,
1436 clipboard_rich_text_received_func,
1439 if (g_main_loop_is_running (results.loop))
1441 GDK_THREADS_LEAVE ();
1442 g_main_loop_run (results.loop);
1443 GDK_THREADS_ENTER ();
1446 g_main_loop_unref (results.loop);
1448 *format = results.format;
1449 *length = results.length;
1451 return results.data;
1455 clipboard_image_received_func (GtkClipboard *clipboard,
1459 WaitResults *results = data;
1462 results->data = g_object_ref (pixbuf);
1464 g_main_loop_quit (results->loop);
1468 * gtk_clipboard_wait_for_image:
1469 * @clipboard: a #GtkClipboard
1471 * Requests the contents of the clipboard as image and converts
1472 * the result to a #GdkPixbuf. This function waits for
1473 * the data to be received using the main loop, so events,
1474 * timeouts, etc, may be dispatched during the wait.
1476 * Return value: a newly-allocated #GdkPixbuf object which must
1477 * be disposed with g_object_unref(), or %NULL if
1478 * retrieving the selection data failed. (This
1479 * could happen for various reasons, in particular
1480 * if the clipboard was empty or if the contents of
1481 * the clipboard could not be converted into an image.)
1486 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1488 WaitResults results;
1490 g_return_val_if_fail (clipboard != NULL, NULL);
1492 results.data = NULL;
1493 results.loop = g_main_loop_new (NULL, TRUE);
1495 gtk_clipboard_request_image (clipboard,
1496 clipboard_image_received_func,
1499 if (g_main_loop_is_running (results.loop))
1501 GDK_THREADS_LEAVE ();
1502 g_main_loop_run (results.loop);
1503 GDK_THREADS_ENTER ();
1506 g_main_loop_unref (results.loop);
1508 return results.data;
1512 clipboard_uris_received_func (GtkClipboard *clipboard,
1516 WaitResults *results = data;
1518 results->data = g_strdupv (uris);
1519 g_main_loop_quit (results->loop);
1523 * gtk_clipboard_wait_for_uris:
1524 * @clipboard: a #GtkClipboard
1526 * Requests the contents of the clipboard as URIs. This function waits
1527 * for the data to be received using the main loop, so events,
1528 * timeouts, etc, may be dispatched during the wait.
1530 * Return value: (array zero-terminated=1) (element-type utf8) (transfer full): a newly-allocated
1531 * %NULL-terminated array of strings which must
1532 * be freed with g_strfreev(), or %NULL if
1533 * retrieving the selection data failed. (This
1534 * could happen for various reasons, in particular
1535 * if the clipboard was empty or if the contents of
1536 * the clipboard could not be converted into URI form.)
1541 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
1543 WaitResults results;
1545 g_return_val_if_fail (clipboard != NULL, NULL);
1547 results.data = NULL;
1548 results.loop = g_main_loop_new (NULL, TRUE);
1550 gtk_clipboard_request_uris (clipboard,
1551 clipboard_uris_received_func,
1554 if (g_main_loop_is_running (results.loop))
1556 GDK_THREADS_LEAVE ();
1557 g_main_loop_run (results.loop);
1558 GDK_THREADS_ENTER ();
1561 g_main_loop_unref (results.loop);
1563 return results.data;
1567 * gtk_clipboard_get_display:
1568 * @clipboard: a #GtkClipboard
1570 * Gets the #GdkDisplay associated with @clipboard
1572 * Return value: the #GdkDisplay associated with @clipboard
1577 gtk_clipboard_get_display (GtkClipboard *clipboard)
1579 g_return_val_if_fail (clipboard != NULL, NULL);
1581 return clipboard->display;
1585 * gtk_clipboard_wait_is_text_available:
1586 * @clipboard: a #GtkClipboard
1588 * Test to see if there is text available to be pasted
1589 * This is done by requesting the TARGETS atom and checking
1590 * if it contains any of the supported text targets. This function
1591 * waits for the data to be received using the main loop, so events,
1592 * timeouts, etc, may be dispatched during the wait.
1594 * This function is a little faster than calling
1595 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1598 * Return value: %TRUE is there is text available, %FALSE otherwise.
1601 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1603 GtkSelectionData *data;
1604 gboolean result = FALSE;
1606 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1609 result = gtk_selection_data_targets_include_text (data);
1610 gtk_selection_data_free (data);
1617 * gtk_clipboard_wait_is_rich_text_available:
1618 * @clipboard: a #GtkClipboard
1619 * @buffer: a #GtkTextBuffer
1621 * Test to see if there is rich text available to be pasted
1622 * This is done by requesting the TARGETS atom and checking
1623 * if it contains any of the supported rich text targets. This function
1624 * waits for the data to be received using the main loop, so events,
1625 * timeouts, etc, may be dispatched during the wait.
1627 * This function is a little faster than calling
1628 * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
1631 * Return value: %TRUE is there is rich text available, %FALSE otherwise.
1636 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
1637 GtkTextBuffer *buffer)
1639 GtkSelectionData *data;
1640 gboolean result = FALSE;
1642 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1643 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1645 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1648 result = gtk_selection_data_targets_include_rich_text (data, buffer);
1649 gtk_selection_data_free (data);
1656 * gtk_clipboard_wait_is_image_available:
1657 * @clipboard: a #GtkClipboard
1659 * Test to see if there is an image available to be pasted
1660 * This is done by requesting the TARGETS atom and checking
1661 * if it contains any of the supported image targets. This function
1662 * waits for the data to be received using the main loop, so events,
1663 * timeouts, etc, may be dispatched during the wait.
1665 * This function is a little faster than calling
1666 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1667 * the actual image data.
1669 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1674 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1676 GtkSelectionData *data;
1677 gboolean result = FALSE;
1679 data = gtk_clipboard_wait_for_contents (clipboard,
1680 gdk_atom_intern_static_string ("TARGETS"));
1683 result = gtk_selection_data_targets_include_image (data, FALSE);
1684 gtk_selection_data_free (data);
1691 * gtk_clipboard_wait_is_uris_available:
1692 * @clipboard: a #GtkClipboard
1694 * Test to see if there is a list of URIs available to be pasted
1695 * This is done by requesting the TARGETS atom and checking
1696 * if it contains the URI targets. This function
1697 * waits for the data to be received using the main loop, so events,
1698 * timeouts, etc, may be dispatched during the wait.
1700 * This function is a little faster than calling
1701 * gtk_clipboard_wait_for_uris() since it doesn't need to retrieve
1702 * the actual URI data.
1704 * Return value: %TRUE is there is an URI list available, %FALSE otherwise.
1709 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
1711 GtkSelectionData *data;
1712 gboolean result = FALSE;
1714 data = gtk_clipboard_wait_for_contents (clipboard,
1715 gdk_atom_intern_static_string ("TARGETS"));
1718 result = gtk_selection_data_targets_include_uri (data);
1719 gtk_selection_data_free (data);
1726 * gtk_clipboard_wait_for_targets
1727 * @clipboard: a #GtkClipboard
1728 * @targets: location to store an array of targets. The result
1729 * stored here must be freed with g_free().
1730 * @n_targets: location to store number of items in @targets.
1732 * Returns a list of targets that are present on the clipboard, or %NULL
1733 * if there aren't any targets available. The returned list must be
1734 * freed with g_free().
1735 * This function waits for the data to be received using the main
1736 * loop, so events, timeouts, etc, may be dispatched during the wait.
1738 * Return value: %TRUE if any targets are present on the clipboard,
1744 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1748 GtkSelectionData *data;
1749 gboolean result = FALSE;
1751 g_return_val_if_fail (clipboard != NULL, FALSE);
1753 /* If the display supports change notification we cache targets */
1754 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1755 clipboard->n_cached_targets != -1)
1758 *n_targets = clipboard->n_cached_targets;
1761 *targets = g_memdup (clipboard->cached_targets,
1762 clipboard->n_cached_targets * sizeof (GdkAtom));
1773 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1777 GdkAtom *tmp_targets;
1780 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1782 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1784 clipboard->n_cached_targets = tmp_n_targets;
1785 clipboard->cached_targets = g_memdup (tmp_targets,
1786 tmp_n_targets * sizeof (GdkAtom));
1790 *n_targets = tmp_n_targets;
1793 *targets = tmp_targets;
1795 g_free (tmp_targets);
1797 gtk_selection_data_free (data);
1803 static GtkClipboard *
1804 clipboard_peek (GdkDisplay *display,
1806 gboolean only_if_exists)
1808 GtkClipboard *clipboard = NULL;
1812 if (selection == GDK_NONE)
1813 selection = GDK_SELECTION_CLIPBOARD;
1815 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1817 tmp_list = clipboards;
1820 clipboard = tmp_list->data;
1821 if (clipboard->selection == selection)
1824 tmp_list = tmp_list->next;
1827 if (!tmp_list && !only_if_exists)
1829 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1830 clipboard->selection = selection;
1831 clipboard->display = display;
1832 clipboard->n_cached_targets = -1;
1833 clipboard->n_storable_targets = -1;
1834 clipboards = g_slist_prepend (clipboards, clipboard);
1835 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1836 g_signal_connect (display, "closed",
1837 G_CALLBACK (clipboard_display_closed), clipboard);
1838 gdk_display_request_selection_notification (display, selection);
1845 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1846 GdkEventOwnerChange *event)
1848 if (clipboard->n_cached_targets != -1)
1850 g_free (clipboard->cached_targets);
1851 clipboard->cached_targets = NULL;
1852 clipboard->n_cached_targets = -1;
1857 * gtk_clipboard_wait_is_target_available:
1858 * @clipboard: a #GtkClipboard
1859 * @target: A #GdkAtom indicating which target to look for.
1861 * Checks if a clipboard supports pasting data of a given type. This
1862 * function can be used to determine if a "Paste" menu item should be
1863 * insensitive or not.
1865 * If you want to see if there's text available on the clipboard, use
1866 * gtk_clipboard_wait_is_text_available () instead.
1868 * Return value: %TRUE if the target is available, %FALSE otherwise.
1873 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1878 gboolean retval = FALSE;
1880 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1883 for (i = 0; i < n_targets; i++)
1885 if (targets[i] == target)
1898 * _gtk_clipboard_handle_event:
1899 * @event: a owner change event
1901 * Emits the #GtkClipboard::owner-change signal on the appropriate @clipboard.
1906 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1908 GdkDisplay *display;
1909 GtkClipboard *clipboard;
1911 display = gdk_drawable_get_display (event->window);
1912 clipboard = clipboard_peek (display, event->selection, TRUE);
1915 g_signal_emit (clipboard,
1916 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1920 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1922 g_main_loop_quit (clipboard->store_loop);
1928 * gtk_clipboard_set_can_store:
1929 * @clipboard: a #GtkClipboard
1930 * @targets: array containing information about which forms should be stored
1931 * or %NULL to indicate that all forms should be stored.
1932 * @n_targets: number of elements in @targets
1934 * Hints that the clipboard data should be stored somewhere when the
1935 * application exits or when gtk_clipboard_store () is called.
1937 * This value is reset when the clipboard owner changes.
1938 * Where the clipboard data is stored is platform dependent,
1939 * see gdk_display_store_clipboard () for more information.
1944 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1945 const GtkTargetEntry *targets,
1948 GtkWidget *clipboard_widget;
1950 static const GtkTargetEntry save_targets[] = {
1951 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1954 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1955 g_return_if_fail (n_targets >= 0);
1957 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1960 g_free (clipboard->storable_targets);
1962 clipboard_widget = get_clipboard_widget (clipboard->display);
1964 /* n_storable_targets being -1 means that
1965 * gtk_clipboard_set_can_store hasn't been called since the
1966 * clipboard owner changed. We only want to add SAVE_TARGETS and
1967 * ref the owner once , so we do that here
1969 if (clipboard->n_storable_targets == -1)
1971 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1974 /* Ref the owner so it won't go away */
1975 if (clipboard->have_owner)
1976 g_object_ref (clipboard->user_data);
1979 clipboard->n_storable_targets = n_targets;
1980 clipboard->storable_targets = g_new (GdkAtom, n_targets);
1981 for (i = 0; i < n_targets; i++)
1982 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1986 gtk_clipboard_selection_notify (GtkWidget *widget,
1987 GdkEventSelection *event,
1988 GtkClipboard *clipboard)
1990 if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
1991 clipboard->storing_selection)
1992 g_main_loop_quit (clipboard->store_loop);
1998 * gtk_clipboard_store:
1999 * @clipboard: a #GtkClipboard
2001 * Stores the current clipboard data somewhere so that it will stay
2002 * around after the application has quit.
2007 gtk_clipboard_store (GtkClipboard *clipboard)
2009 GtkWidget *clipboard_widget;
2011 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2013 if (clipboard->n_storable_targets < 0)
2016 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
2019 g_object_ref (clipboard);
2021 clipboard_widget = get_clipboard_widget (clipboard->display);
2022 clipboard->notify_signal_id = g_signal_connect (clipboard_widget,
2023 "selection-notify-event",
2024 G_CALLBACK (gtk_clipboard_selection_notify),
2027 gdk_display_store_clipboard (clipboard->display,
2028 clipboard_widget->window,
2029 clipboard_get_timestamp (clipboard),
2030 clipboard->storable_targets,
2031 clipboard->n_storable_targets);
2033 clipboard->storing_selection = TRUE;
2035 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
2036 clipboard->store_timeout = g_timeout_add_seconds (10, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
2038 if (g_main_loop_is_running (clipboard->store_loop))
2040 GDK_THREADS_LEAVE ();
2041 g_main_loop_run (clipboard->store_loop);
2042 GDK_THREADS_ENTER ();
2045 g_main_loop_unref (clipboard->store_loop);
2046 clipboard->store_loop = NULL;
2048 g_source_remove (clipboard->store_timeout);
2049 clipboard->store_timeout = 0;
2050 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
2051 clipboard->notify_signal_id = 0;
2053 clipboard->storing_selection = FALSE;
2055 g_object_unref (clipboard);
2058 /* Stores all clipboard selections on all displays, called from
2062 _gtk_clipboard_store_all (void)
2064 GtkClipboard *clipboard;
2065 GSList *displays, *list;
2067 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
2072 GdkDisplay *display = list->data;
2074 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
2077 gtk_clipboard_store (clipboard);
2081 g_slist_free (displays);
2085 #define __GTK_CLIPBOARD_C__
2086 #include "gtkaliasdef.c"