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);
240 G_OBJECT_CLASS (gtk_clipboard_parent_class)->finalize (object);
244 clipboard_display_closed (GdkDisplay *display,
246 GtkClipboard *clipboard)
250 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
251 g_object_run_dispose (G_OBJECT (clipboard));
252 clipboards = g_slist_remove (clipboards, clipboard);
253 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
254 g_object_unref (clipboard);
258 * gtk_clipboard_get_for_display:
259 * @display: the display for which the clipboard is to be retrieved or created
260 * @selection: a #GdkAtom which identifies the clipboard
263 * Returns the clipboard object for the given selection.
264 * Cut/copy/paste menu items and keyboard shortcuts should use
265 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
266 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
267 * for backwards compatibility reasons.)
268 * The currently-selected object or text should be provided on the clipboard
269 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
270 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
271 * to the default clipboard, i.e. they copy the selection to what the
272 * user sees as the clipboard.
274 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
275 * ("CLIPBOARD", FALSE)</literal>. See <ulink
276 * url="http://www.freedesktop.org/Standards/clipboards-spec">
277 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
278 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
279 * selections under the X window system. On Win32 the
280 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
282 * It's possible to have arbitrary named clipboards; if you do invent
283 * new clipboards, you should prefix the selection name with an
284 * underscore (because the ICCCM requires that nonstandard atoms are
285 * underscore-prefixed), and namespace it as well. For example,
286 * if your application called "Foo" has a special-purpose
287 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
289 * Return value: the appropriate clipboard object. If no
290 * clipboard already exists, a new one will
291 * be created. Once a clipboard object has
292 * been created, it is persistent and, since
293 * it is owned by GTK+, must not be freed or
299 gtk_clipboard_get_for_display (GdkDisplay *display,
302 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
303 g_return_val_if_fail (!display->closed, NULL);
305 return clipboard_peek (display, selection, FALSE);
310 * gtk_clipboard_get():
311 * @selection: a #GdkAtom which identifies the clipboard
314 * Returns the clipboard object for the given selection.
315 * See gtk_clipboard_get_for_display() for complete details.
317 * Return value: the appropriate clipboard object. If no
318 * clipboard already exists, a new one will
319 * be created. Once a clipboard object has
320 * been created, it is persistent and, since
321 * it is owned by GTK+, must not be freed or
325 gtk_clipboard_get (GdkAtom selection)
327 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
331 selection_get_cb (GtkWidget *widget,
332 GtkSelectionData *selection_data,
336 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, selection_data->selection);
338 if (clipboard && clipboard->get_func)
339 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
343 selection_clear_event_cb (GtkWidget *widget,
344 GdkEventSelection *event)
346 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
350 clipboard_unset (clipboard);
359 make_clipboard_widget (GdkDisplay *display,
362 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
364 g_signal_connect (widget, "selection-received",
365 G_CALLBACK (selection_received), NULL);
369 /* We need this for gdk_x11_get_server_time() */
370 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
372 g_signal_connect (widget, "selection-get",
373 G_CALLBACK (selection_get_cb), NULL);
374 g_signal_connect (widget, "selection-clear-event",
375 G_CALLBACK (selection_clear_event_cb), NULL);
382 get_clipboard_widget (GdkDisplay *display)
384 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
387 clip_widget = make_clipboard_widget (display, TRUE);
388 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
394 /* This function makes a very good guess at what the correct
395 * timestamp for a selection request should be. If there is
396 * a currently processed event, it uses the timestamp for that
397 * event, otherwise it uses the current server time. However,
398 * if the time resulting from that is older than the time used
399 * last time, it uses the time used last time instead.
401 * In order implement this correctly, we never use CurrentTime,
402 * but actually retrieve the actual timestamp from the server.
403 * This is a little slower but allows us to make the guarantee
404 * that the times used by this application will always ascend
405 * and we won't get selections being rejected just because
406 * we are using a correct timestamp from an event, but used
407 * CurrentTime previously.
410 clipboard_get_timestamp (GtkClipboard *clipboard)
412 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
413 guint32 timestamp = gtk_get_current_event_time ();
415 if (timestamp == GDK_CURRENT_TIME)
417 #ifdef GDK_WINDOWING_X11
418 timestamp = gdk_x11_get_server_time (clipboard_widget->window);
419 #elif defined GDK_WINDOWING_WIN32
420 timestamp = GetMessageTime ();
425 if (clipboard->timestamp != GDK_CURRENT_TIME)
427 /* Check to see if clipboard->timestamp is newer than
428 * timestamp, accounting for wraparound.
431 guint32 max = timestamp + 0x80000000;
433 if ((max > timestamp &&
434 (clipboard->timestamp > timestamp &&
435 clipboard->timestamp <= max)) ||
437 (clipboard->timestamp > timestamp ||
438 clipboard->timestamp <= max)))
440 timestamp = clipboard->timestamp;
445 clipboard->timestamp = timestamp;
451 clipboard_owner_destroyed (gpointer data)
453 GSList *clipboards = data;
456 tmp_list = clipboards;
459 GtkClipboard *clipboard = tmp_list->data;
461 clipboard->get_func = NULL;
462 clipboard->clear_func = NULL;
463 clipboard->user_data = NULL;
464 clipboard->have_owner = FALSE;
466 gtk_clipboard_clear (clipboard);
468 tmp_list = tmp_list->next;
471 g_slist_free (clipboards);
475 clipboard_add_owner_notify (GtkClipboard *clipboard)
477 if (!clipboards_owned_key_id)
478 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
480 if (clipboard->have_owner)
481 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
482 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
483 clipboards_owned_key_id),
485 clipboard_owner_destroyed);
489 clipboard_remove_owner_notify (GtkClipboard *clipboard)
491 if (clipboard->have_owner)
492 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
493 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
494 clipboards_owned_key_id),
496 clipboard_owner_destroyed);
500 gtk_clipboard_set_contents (GtkClipboard *clipboard,
501 const GtkTargetEntry *targets,
503 GtkClipboardGetFunc get_func,
504 GtkClipboardClearFunc clear_func,
508 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
510 if (gtk_selection_owner_set_for_display (clipboard->display,
512 clipboard->selection,
513 clipboard_get_timestamp (clipboard)))
515 clipboard->have_selection = TRUE;
517 if (!(clipboard->have_owner && have_owner) ||
518 clipboard->user_data != user_data)
520 clipboard_unset (clipboard);
522 if (clipboard->get_func)
524 /* Calling unset() caused the clipboard contents to be reset!
525 * Avoid leaking and return
527 if (!(clipboard->have_owner && have_owner) ||
528 clipboard->user_data != user_data)
530 (*clear_func) (clipboard, user_data);
538 clipboard->user_data = user_data;
539 clipboard->have_owner = have_owner;
541 clipboard_add_owner_notify (clipboard);
546 clipboard->get_func = get_func;
547 clipboard->clear_func = clear_func;
549 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
550 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
560 * gtk_clipboard_set_with_data:
561 * @clipboard: a #GtkClipboard
562 * @targets: array containing information about the available forms for the
564 * @n_targets: number of elements in @targets
565 * @get_func: function to call to get the actual clipboard data
566 * @clear_func: when the clipboard contents are set again, this function will
567 * be called, and @get_func will not be subsequently called.
568 * @user_data: user data to pass to @get_func and @clear_func.
570 * Virtually sets the contents of the specified clipboard by providing
571 * a list of supported formats for the clipboard data and a function
572 * to call to get the actual data when it is requested.
574 * Return value: %TRUE if setting the clipboard data succeeded. If setting
575 * the clipboard data failed the provided callback functions
579 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
580 const GtkTargetEntry *targets,
582 GtkClipboardGetFunc get_func,
583 GtkClipboardClearFunc clear_func,
586 g_return_val_if_fail (clipboard != NULL, FALSE);
587 g_return_val_if_fail (targets != NULL, FALSE);
588 g_return_val_if_fail (get_func != NULL, FALSE);
590 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
591 get_func, clear_func, user_data,
596 * gtk_clipboard_set_with_owner:
597 * @clipboard: a #GtkClipboard
598 * @targets: array containing information about the available forms for the
600 * @n_targets: number of elements in @targets
601 * @get_func: function to call to get the actual clipboard data
602 * @clear_func: when the clipboard contents are set again, this function will
603 * be called, and @get_func will not be subsequently called.
604 * @owner: an object that "owns" the data. This object will be passed
605 * to the callbacks when called.
607 * Virtually sets the contents of the specified clipboard by providing
608 * a list of supported formats for the clipboard data and a function
609 * to call to get the actual data when it is requested.
611 * The difference between this function and gtk_clipboard_set_with_data()
612 * is that instead of an generic @user_data pointer, a #GObject is passed
615 * Return value: %TRUE if setting the clipboard data succeeded. If setting
616 * the clipboard data failed the provided callback functions
620 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
621 const GtkTargetEntry *targets,
623 GtkClipboardGetFunc get_func,
624 GtkClipboardClearFunc clear_func,
627 g_return_val_if_fail (clipboard != NULL, FALSE);
628 g_return_val_if_fail (targets != NULL, FALSE);
629 g_return_val_if_fail (get_func != NULL, FALSE);
630 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
632 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
633 get_func, clear_func, owner,
638 * gtk_clipboard_get_owner:
639 * @clipboard: a #GtkClipboard
641 * If the clipboard contents callbacks were set with
642 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
643 * gtk_clipboard_clear() has not subsequently called, returns the owner set
644 * by gtk_clipboard_set_with_owner().
646 * Return value: the owner of the clipboard, if any; otherwise %NULL.
649 gtk_clipboard_get_owner (GtkClipboard *clipboard)
651 g_return_val_if_fail (clipboard != NULL, NULL);
653 if (clipboard->have_owner)
654 return clipboard->user_data;
660 clipboard_unset (GtkClipboard *clipboard)
662 GtkClipboardClearFunc old_clear_func;
664 gboolean old_have_owner;
665 gint old_n_storable_targets;
667 old_clear_func = clipboard->clear_func;
668 old_data = clipboard->user_data;
669 old_have_owner = clipboard->have_owner;
670 old_n_storable_targets = clipboard->n_storable_targets;
674 clipboard_remove_owner_notify (clipboard);
675 clipboard->have_owner = FALSE;
678 clipboard->n_storable_targets = -1;
679 g_free (clipboard->storable_targets);
680 clipboard->storable_targets = NULL;
682 clipboard->get_func = NULL;
683 clipboard->clear_func = NULL;
684 clipboard->user_data = NULL;
687 old_clear_func (clipboard, old_data);
689 /* If we've transferred the clipboard data to the manager,
692 if (old_have_owner &&
693 old_n_storable_targets != -1)
694 g_object_unref (old_data);
698 * gtk_clipboard_clear:
699 * @clipboard: a #GtkClipboard
701 * Clears the contents of the clipboard. Generally this should only
702 * be called between the time you call gtk_clipboard_set_with_owner()
703 * or gtk_clipboard_set_with_data(),
704 * and when the @clear_func you supplied is called. Otherwise, the
705 * clipboard may be owned by someone else.
708 gtk_clipboard_clear (GtkClipboard *clipboard)
710 g_return_if_fail (clipboard != NULL);
712 if (clipboard->have_selection)
713 gtk_selection_owner_set_for_display (clipboard->display,
715 clipboard->selection,
716 clipboard_get_timestamp (clipboard));
720 text_get_func (GtkClipboard *clipboard,
721 GtkSelectionData *selection_data,
725 gtk_selection_data_set_text (selection_data, data, -1);
729 text_clear_func (GtkClipboard *clipboard,
737 * gtk_clipboard_set_text:
738 * @clipboard: a #GtkClipboard object
739 * @text: a UTF-8 string.
740 * @len: length of @text, in bytes, or -1, in which case
741 * the length will be determined with <function>strlen()</function>.
743 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
744 * make a copy of the text and take responsibility for responding
745 * for requests for the text, and for converting the text into
746 * the requested format.
749 gtk_clipboard_set_text (GtkClipboard *clipboard,
754 GtkTargetEntry *targets;
757 g_return_if_fail (clipboard != NULL);
758 g_return_if_fail (text != NULL);
760 list = gtk_target_list_new (NULL, 0);
761 gtk_target_list_add_text_targets (list, 0);
763 targets = gtk_target_table_new_from_list (list, &n_targets);
768 gtk_clipboard_set_with_data (clipboard,
770 text_get_func, text_clear_func,
771 g_strndup (text, len));
772 gtk_clipboard_set_can_store (clipboard, NULL, 0);
774 gtk_target_table_free (targets, n_targets);
775 gtk_target_list_unref (list);
779 pixbuf_get_func (GtkClipboard *clipboard,
780 GtkSelectionData *selection_data,
784 gtk_selection_data_set_pixbuf (selection_data, data);
788 pixbuf_clear_func (GtkClipboard *clipboard,
791 g_object_unref (data);
795 * gtk_clipboard_set_image:
796 * @clipboard: a #GtkClipboard object
797 * @pixbuf: a #GdkPixbuf
799 * Sets the contents of the clipboard to the given #GdkPixbuf.
800 * GTK+ will take responsibility for responding for requests
801 * for the image, and for converting the image into the
807 gtk_clipboard_set_image (GtkClipboard *clipboard,
811 GtkTargetEntry *targets;
814 g_return_if_fail (clipboard != NULL);
815 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
817 list = gtk_target_list_new (NULL, 0);
818 gtk_target_list_add_image_targets (list, 0, TRUE);
820 targets = gtk_target_table_new_from_list (list, &n_targets);
822 gtk_clipboard_set_with_data (clipboard,
824 pixbuf_get_func, pixbuf_clear_func,
825 g_object_ref (pixbuf));
826 gtk_clipboard_set_can_store (clipboard, NULL, 0);
828 gtk_target_table_free (targets, n_targets);
829 gtk_target_list_unref (list);
833 set_request_contents_info (GtkWidget *widget,
834 RequestContentsInfo *info)
836 if (!request_contents_key_id)
837 request_contents_key_id = g_quark_from_static_string (request_contents_key);
839 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
842 static RequestContentsInfo *
843 get_request_contents_info (GtkWidget *widget)
845 if (!request_contents_key_id)
848 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
852 selection_received (GtkWidget *widget,
853 GtkSelectionData *selection_data,
856 RequestContentsInfo *request_info = get_request_contents_info (widget);
857 set_request_contents_info (widget, NULL);
859 request_info->callback (gtk_widget_get_clipboard (widget, selection_data->selection),
861 request_info->user_data);
863 g_free (request_info);
865 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
866 gtk_widget_destroy (widget);
870 * gtk_clipboard_request_contents:
871 * @clipboard: a #GtkClipboard
872 * @target: an atom representing the form into which the clipboard
873 * owner should convert the selection.
874 * @callback: A function to call when the results are received
875 * (or the retrieval fails). If the retrieval fails
876 * the length field of @selection_data will be
878 * @user_data: user data to pass to @callback
880 * Requests the contents of clipboard as the given target.
881 * When the results of the result are later received the supplied callback
885 gtk_clipboard_request_contents (GtkClipboard *clipboard,
887 GtkClipboardReceivedFunc callback,
890 RequestContentsInfo *info;
892 GtkWidget *clipboard_widget;
894 g_return_if_fail (clipboard != NULL);
895 g_return_if_fail (target != GDK_NONE);
896 g_return_if_fail (callback != NULL);
898 clipboard_widget = get_clipboard_widget (clipboard->display);
900 if (get_request_contents_info (clipboard_widget))
901 widget = make_clipboard_widget (clipboard->display, FALSE);
903 widget = clipboard_widget;
905 info = g_new (RequestContentsInfo, 1);
906 info->callback = callback;
907 info->user_data = user_data;
909 set_request_contents_info (widget, info);
911 gtk_selection_convert (widget, clipboard->selection, target,
912 clipboard_get_timestamp (clipboard));
916 request_text_received_func (GtkClipboard *clipboard,
917 GtkSelectionData *selection_data,
920 RequestTextInfo *info = data;
921 gchar *result = NULL;
923 result = (gchar *) gtk_selection_data_get_text (selection_data);
927 /* If we asked for UTF8 and didn't get it, try compound_text;
928 * if we asked for compound_text and didn't get it, try string;
929 * If we asked for anything else and didn't get it, give up.
931 if (selection_data->target == gdk_atom_intern_static_string ("UTF8_STRING"))
933 gtk_clipboard_request_contents (clipboard,
934 gdk_atom_intern_static_string ("COMPOUND_TEXT"),
935 request_text_received_func, info);
938 else if (selection_data->target == gdk_atom_intern_static_string ("COMPOUND_TEXT"))
940 gtk_clipboard_request_contents (clipboard,
942 request_text_received_func, info);
947 info->callback (clipboard, result, info->user_data);
953 * gtk_clipboard_request_text:
954 * @clipboard: a #GtkClipboard
955 * @callback: a function to call when the text is received,
956 * or the retrieval fails. (It will always be called
957 * one way or the other.)
958 * @user_data: user data to pass to @callback.
960 * Requests the contents of the clipboard as text. When the text is
961 * later received, it will be converted to UTF-8 if necessary, and
962 * @callback will be called.
964 * The @text parameter to @callback will contain the resulting text if
965 * the request succeeded, or %NULL if it failed. This could happen for
966 * various reasons, in particular if the clipboard was empty or if the
967 * contents of the clipboard could not be converted into text form.
970 gtk_clipboard_request_text (GtkClipboard *clipboard,
971 GtkClipboardTextReceivedFunc callback,
974 RequestTextInfo *info;
976 g_return_if_fail (clipboard != NULL);
977 g_return_if_fail (callback != NULL);
979 info = g_new (RequestTextInfo, 1);
980 info->callback = callback;
981 info->user_data = user_data;
983 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("UTF8_STRING"),
984 request_text_received_func,
989 request_rich_text_received_func (GtkClipboard *clipboard,
990 GtkSelectionData *selection_data,
993 RequestRichTextInfo *info = data;
994 guint8 *result = NULL;
997 result = selection_data->data;
998 length = selection_data->length;
1000 info->current_atom++;
1002 if ((!result || length < 1) && (info->current_atom < info->n_atoms))
1004 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1005 request_rich_text_received_func,
1010 info->callback (clipboard, selection_data->target, result, length,
1012 g_free (info->atoms);
1017 * gtk_clipboard_request_rich_text:
1018 * @clipboard: a #GtkClipboard
1019 * @buffer: a #GtkTextBuffer
1020 * @callback: a function to call when the text is received,
1021 * or the retrieval fails. (It will always be called
1022 * one way or the other.)
1023 * @user_data: user data to pass to @callback.
1025 * Requests the contents of the clipboard as rich text. When the rich
1026 * text is later received, @callback will be called.
1028 * The @text parameter to @callback will contain the resulting rich
1029 * text if the request succeeded, or %NULL if it failed. The @length
1030 * parameter will contain @text's length. This function can fail for
1031 * various reasons, in particular if the clipboard was empty or if the
1032 * contents of the clipboard could not be converted into rich text form.
1037 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
1038 GtkTextBuffer *buffer,
1039 GtkClipboardRichTextReceivedFunc callback,
1042 RequestRichTextInfo *info;
1044 g_return_if_fail (clipboard != NULL);
1045 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1046 g_return_if_fail (callback != NULL);
1048 info = g_new (RequestRichTextInfo, 1);
1049 info->callback = callback;
1052 info->current_atom = 0;
1053 info->user_data = user_data;
1055 info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
1057 gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1058 request_rich_text_received_func,
1063 request_image_received_func (GtkClipboard *clipboard,
1064 GtkSelectionData *selection_data,
1067 RequestImageInfo *info = data;
1068 GdkPixbuf *result = NULL;
1070 result = gtk_selection_data_get_pixbuf (selection_data);
1074 /* If we asked for image/png and didn't get it, try image/jpeg;
1075 * if we asked for image/jpeg and didn't get it, try image/gif;
1076 * if we asked for image/gif and didn't get it, try image/bmp;
1077 * If we asked for anything else and didn't get it, give up.
1079 if (selection_data->target == gdk_atom_intern_static_string ("image/png"))
1081 gtk_clipboard_request_contents (clipboard,
1082 gdk_atom_intern_static_string ("image/jpeg"),
1083 request_image_received_func, info);
1086 else if (selection_data->target == gdk_atom_intern_static_string ("image/jpeg"))
1088 gtk_clipboard_request_contents (clipboard,
1089 gdk_atom_intern_static_string ("image/gif"),
1090 request_image_received_func, info);
1093 else if (selection_data->target == gdk_atom_intern_static_string ("image/gif"))
1095 gtk_clipboard_request_contents (clipboard,
1096 gdk_atom_intern_static_string ("image/bmp"),
1097 request_image_received_func, info);
1102 info->callback (clipboard, result, info->user_data);
1106 g_object_unref (result);
1110 * gtk_clipboard_request_image:
1111 * @clipboard: a #GtkClipboard
1112 * @callback: a function to call when the image is received,
1113 * or the retrieval fails. (It will always be called
1114 * one way or the other.)
1115 * @user_data: user data to pass to @callback.
1117 * Requests the contents of the clipboard as image. When the image is
1118 * later received, it will be converted to a #GdkPixbuf, and
1119 * @callback will be called.
1121 * The @pixbuf parameter to @callback will contain the resulting
1122 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1123 * could happen for various reasons, in particular if the clipboard
1124 * was empty or if the contents of the clipboard could not be
1125 * converted into an image.
1130 gtk_clipboard_request_image (GtkClipboard *clipboard,
1131 GtkClipboardImageReceivedFunc callback,
1134 RequestImageInfo *info;
1136 g_return_if_fail (clipboard != NULL);
1137 g_return_if_fail (callback != NULL);
1139 info = g_new (RequestImageInfo, 1);
1140 info->callback = callback;
1141 info->user_data = user_data;
1143 gtk_clipboard_request_contents (clipboard,
1144 gdk_atom_intern_static_string ("image/png"),
1145 request_image_received_func,
1150 request_uris_received_func (GtkClipboard *clipboard,
1151 GtkSelectionData *selection_data,
1154 RequestURIInfo *info = data;
1157 uris = gtk_selection_data_get_uris (selection_data);
1158 info->callback (clipboard, uris, info->user_data);
1161 g_slice_free (RequestURIInfo, info);
1165 * gtk_clipboard_request_uris:
1166 * @clipboard: a #GtkClipboard
1167 * @callback: a function to call when the URIs are received,
1168 * or the retrieval fails. (It will always be called
1169 * one way or the other.)
1170 * @user_data: user data to pass to @callback.
1172 * Requests the contents of the clipboard as URIs. When the URIs are
1173 * later received @callback will be called.
1175 * The @uris parameter to @callback will contain the resulting array of
1176 * URIs if the request succeeded, or %NULL if it failed. This could happen
1177 * for various reasons, in particular if the clipboard was empty or if the
1178 * contents of the clipboard could not be converted into URI form.
1183 gtk_clipboard_request_uris (GtkClipboard *clipboard,
1184 GtkClipboardURIReceivedFunc callback,
1187 RequestURIInfo *info;
1189 g_return_if_fail (clipboard != NULL);
1190 g_return_if_fail (callback != NULL);
1192 info = g_slice_new (RequestURIInfo);
1193 info->callback = callback;
1194 info->user_data = user_data;
1196 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"),
1197 request_uris_received_func,
1202 request_targets_received_func (GtkClipboard *clipboard,
1203 GtkSelectionData *selection_data,
1206 RequestTargetsInfo *info = data;
1207 GdkAtom *targets = NULL;
1210 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1212 info->callback (clipboard, targets, n_targets, info->user_data);
1219 * gtk_clipboard_request_targets:
1220 * @clipboard: a #GtkClipboard
1221 * @callback: a function to call when the targets are received,
1222 * or the retrieval fails. (It will always be called
1223 * one way or the other.)
1224 * @user_data: user data to pass to @callback.
1226 * Requests the contents of the clipboard as list of supported targets.
1227 * When the list is later received, @callback will be called.
1229 * The @targets parameter to @callback will contain the resulting targets if
1230 * the request succeeded, or %NULL if it failed.
1235 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1236 GtkClipboardTargetsReceivedFunc callback,
1239 RequestTargetsInfo *info;
1241 g_return_if_fail (clipboard != NULL);
1242 g_return_if_fail (callback != NULL);
1244 /* If the display supports change notification we cache targets */
1245 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1246 clipboard->n_cached_targets != -1)
1248 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1252 info = g_new (RequestTargetsInfo, 1);
1253 info->callback = callback;
1254 info->user_data = user_data;
1256 gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"),
1257 request_targets_received_func,
1265 GdkAtom format; /* used by rich text */
1266 gsize length; /* used by rich text */
1270 clipboard_received_func (GtkClipboard *clipboard,
1271 GtkSelectionData *selection_data,
1274 WaitResults *results = data;
1276 if (selection_data->length >= 0)
1277 results->data = gtk_selection_data_copy (selection_data);
1279 g_main_loop_quit (results->loop);
1283 * gtk_clipboard_wait_for_contents:
1284 * @clipboard: a #GtkClipboard
1285 * @target: an atom representing the form into which the clipboard
1286 * owner should convert the selection.
1288 * Requests the contents of the clipboard using the given target.
1289 * This function waits for the data to be received using the main
1290 * loop, so events, timeouts, etc, may be dispatched during the wait.
1292 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1293 * if retrieving the given target failed. If non-%NULL,
1294 * this value must be freed with gtk_selection_data_free()
1295 * when you are finished with it.
1298 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1301 WaitResults results;
1303 g_return_val_if_fail (clipboard != NULL, NULL);
1304 g_return_val_if_fail (target != GDK_NONE, NULL);
1306 results.data = NULL;
1307 results.loop = g_main_loop_new (NULL, TRUE);
1309 gtk_clipboard_request_contents (clipboard, target,
1310 clipboard_received_func,
1313 if (g_main_loop_is_running (results.loop))
1315 GDK_THREADS_LEAVE ();
1316 g_main_loop_run (results.loop);
1317 GDK_THREADS_ENTER ();
1320 g_main_loop_unref (results.loop);
1322 return results.data;
1326 clipboard_text_received_func (GtkClipboard *clipboard,
1330 WaitResults *results = data;
1332 results->data = g_strdup (text);
1333 g_main_loop_quit (results->loop);
1337 * gtk_clipboard_wait_for_text:
1338 * @clipboard: a #GtkClipboard
1340 * Requests the contents of the clipboard as text and converts
1341 * the result to UTF-8 if necessary. This function waits for
1342 * the data to be received using the main loop, so events,
1343 * timeouts, etc, may be dispatched during the wait.
1345 * Return value: a newly-allocated UTF-8 string which must
1346 * be freed with g_free(), or %NULL if retrieving
1347 * the selection data failed. (This could happen
1348 * for various reasons, in particular if the
1349 * clipboard was empty or if the contents of the
1350 * clipboard could not be converted into text form.)
1353 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1355 WaitResults results;
1357 g_return_val_if_fail (clipboard != NULL, NULL);
1359 results.data = NULL;
1360 results.loop = g_main_loop_new (NULL, TRUE);
1362 gtk_clipboard_request_text (clipboard,
1363 clipboard_text_received_func,
1366 if (g_main_loop_is_running (results.loop))
1368 GDK_THREADS_LEAVE ();
1369 g_main_loop_run (results.loop);
1370 GDK_THREADS_ENTER ();
1373 g_main_loop_unref (results.loop);
1375 return results.data;
1379 clipboard_rich_text_received_func (GtkClipboard *clipboard,
1385 WaitResults *results = data;
1387 results->data = g_memdup (text, length);
1388 results->format = format;
1389 results->length = length;
1390 g_main_loop_quit (results->loop);
1394 * gtk_clipboard_wait_for_rich_text:
1395 * @clipboard: a #GtkClipboard
1396 * @buffer: a #GtkTextBuffer
1397 * @format: return location for the format of the returned data
1398 * @length: return location for the length of the returned data
1400 * Requests the contents of the clipboard as rich text. This function
1401 * waits for the data to be received using the main loop, so events,
1402 * timeouts, etc, may be dispatched during the wait.
1404 * Return value: a newly-allocated binary block of data which must
1405 * be freed with g_free(), or %NULL if retrieving
1406 * the selection data failed. (This could happen
1407 * for various reasons, in particular if the
1408 * clipboard was empty or if the contents of the
1409 * clipboard could not be converted into text form.)
1414 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
1415 GtkTextBuffer *buffer,
1419 WaitResults results;
1421 g_return_val_if_fail (clipboard != NULL, NULL);
1422 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1423 g_return_val_if_fail (format != NULL, NULL);
1424 g_return_val_if_fail (length != NULL, NULL);
1426 results.data = NULL;
1427 results.loop = g_main_loop_new (NULL, TRUE);
1429 gtk_clipboard_request_rich_text (clipboard, buffer,
1430 clipboard_rich_text_received_func,
1433 if (g_main_loop_is_running (results.loop))
1435 GDK_THREADS_LEAVE ();
1436 g_main_loop_run (results.loop);
1437 GDK_THREADS_ENTER ();
1440 g_main_loop_unref (results.loop);
1442 *format = results.format;
1443 *length = results.length;
1445 return results.data;
1449 clipboard_image_received_func (GtkClipboard *clipboard,
1453 WaitResults *results = data;
1456 results->data = g_object_ref (pixbuf);
1458 g_main_loop_quit (results->loop);
1462 * gtk_clipboard_wait_for_image:
1463 * @clipboard: a #GtkClipboard
1465 * Requests the contents of the clipboard as image and converts
1466 * the result to a #GdkPixbuf. This function waits for
1467 * the data to be received using the main loop, so events,
1468 * timeouts, etc, may be dispatched during the wait.
1470 * Return value: a newly-allocated #GdkPixbuf object which must
1471 * be disposed with g_object_unref(), or %NULL if
1472 * retrieving the selection data failed. (This
1473 * could happen for various reasons, in particular
1474 * if the clipboard was empty or if the contents of
1475 * the clipboard could not be converted into an image.)
1480 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1482 WaitResults results;
1484 g_return_val_if_fail (clipboard != NULL, NULL);
1486 results.data = NULL;
1487 results.loop = g_main_loop_new (NULL, TRUE);
1489 gtk_clipboard_request_image (clipboard,
1490 clipboard_image_received_func,
1493 if (g_main_loop_is_running (results.loop))
1495 GDK_THREADS_LEAVE ();
1496 g_main_loop_run (results.loop);
1497 GDK_THREADS_ENTER ();
1500 g_main_loop_unref (results.loop);
1502 return results.data;
1506 clipboard_uris_received_func (GtkClipboard *clipboard,
1510 WaitResults *results = data;
1512 results->data = g_strdupv (uris);
1513 g_main_loop_quit (results->loop);
1517 * gtk_clipboard_wait_for_uris:
1518 * @clipboard: a #GtkClipboard
1520 * Requests the contents of the clipboard as URIs. This function waits
1521 * for the data to be received using the main loop, so events,
1522 * timeouts, etc, may be dispatched during the wait.
1524 * Return value: a newly-allocated %NULL-terminated array of strings which must
1525 * be freed with g_strfreev(), or %NULL if
1526 * retrieving the selection data failed. (This
1527 * could happen for various reasons, in particular
1528 * if the clipboard was empty or if the contents of
1529 * the clipboard could not be converted into URI form.)
1534 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
1536 WaitResults results;
1538 g_return_val_if_fail (clipboard != NULL, NULL);
1540 results.data = NULL;
1541 results.loop = g_main_loop_new (NULL, TRUE);
1543 gtk_clipboard_request_uris (clipboard,
1544 clipboard_uris_received_func,
1547 if (g_main_loop_is_running (results.loop))
1549 GDK_THREADS_LEAVE ();
1550 g_main_loop_run (results.loop);
1551 GDK_THREADS_ENTER ();
1554 g_main_loop_unref (results.loop);
1556 return results.data;
1560 * gtk_clipboard_get_display:
1561 * @clipboard: a #GtkClipboard
1563 * Gets the #GdkDisplay associated with @clipboard
1565 * Return value: the #GdkDisplay associated with @clipboard
1570 gtk_clipboard_get_display (GtkClipboard *clipboard)
1572 g_return_val_if_fail (clipboard != NULL, NULL);
1574 return clipboard->display;
1578 * gtk_clipboard_wait_is_text_available:
1579 * @clipboard: a #GtkClipboard
1581 * Test to see if there is text available to be pasted
1582 * This is done by requesting the TARGETS atom and checking
1583 * if it contains any of the supported text targets. This function
1584 * waits for the data to be received using the main loop, so events,
1585 * timeouts, etc, may be dispatched during the wait.
1587 * This function is a little faster than calling
1588 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1591 * Return value: %TRUE is there is text available, %FALSE otherwise.
1594 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1596 GtkSelectionData *data;
1597 gboolean result = FALSE;
1599 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1602 result = gtk_selection_data_targets_include_text (data);
1603 gtk_selection_data_free (data);
1610 * gtk_clipboard_wait_is_rich_text_available:
1611 * @clipboard: a #GtkClipboard
1612 * @buffer: a #GtkTextBuffer
1614 * Test to see if there is rich text available to be pasted
1615 * This is done by requesting the TARGETS atom and checking
1616 * if it contains any of the supported rich text targets. This function
1617 * waits for the data to be received using the main loop, so events,
1618 * timeouts, etc, may be dispatched during the wait.
1620 * This function is a little faster than calling
1621 * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
1624 * Return value: %TRUE is there is rich text available, %FALSE otherwise.
1629 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
1630 GtkTextBuffer *buffer)
1632 GtkSelectionData *data;
1633 gboolean result = FALSE;
1635 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1636 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1638 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1641 result = gtk_selection_data_targets_include_rich_text (data, buffer);
1642 gtk_selection_data_free (data);
1649 * gtk_clipboard_wait_is_image_available:
1650 * @clipboard: a #GtkClipboard
1652 * Test to see if there is an image available to be pasted
1653 * This is done by requesting the TARGETS atom and checking
1654 * if it contains any of the supported image targets. This function
1655 * waits for the data to be received using the main loop, so events,
1656 * timeouts, etc, may be dispatched during the wait.
1658 * This function is a little faster than calling
1659 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1660 * the actual image data.
1662 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1667 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1669 GtkSelectionData *data;
1670 gboolean result = FALSE;
1672 data = gtk_clipboard_wait_for_contents (clipboard,
1673 gdk_atom_intern_static_string ("TARGETS"));
1676 result = gtk_selection_data_targets_include_image (data, FALSE);
1677 gtk_selection_data_free (data);
1684 * gtk_clipboard_wait_is_uris_available:
1685 * @clipboard: a #GtkClipboard
1687 * Test to see if there is a list of URIs available to be pasted
1688 * This is done by requesting the TARGETS atom and checking
1689 * if it contains the URI targets. This function
1690 * waits for the data to be received using the main loop, so events,
1691 * timeouts, etc, may be dispatched during the wait.
1693 * This function is a little faster than calling
1694 * gtk_clipboard_wait_for_uris() since it doesn't need to retrieve
1695 * the actual URI data.
1697 * Return value: %TRUE is there is an URI list available, %FALSE otherwise.
1702 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
1704 GtkSelectionData *data;
1705 gboolean result = FALSE;
1707 data = gtk_clipboard_wait_for_contents (clipboard,
1708 gdk_atom_intern_static_string ("TARGETS"));
1711 result = gtk_selection_data_targets_include_uri (data);
1712 gtk_selection_data_free (data);
1719 * gtk_clipboard_wait_for_targets
1720 * @clipboard: a #GtkClipboard
1721 * @targets: location to store an array of targets. The result
1722 * stored here must be freed with g_free().
1723 * @n_targets: location to store number of items in @targets.
1725 * Returns a list of targets that are present on the clipboard, or %NULL
1726 * if there aren't any targets available. The returned list must be
1727 * freed with g_free().
1728 * This function waits for the data to be received using the main
1729 * loop, so events, timeouts, etc, may be dispatched during the wait.
1731 * Return value: %TRUE if any targets are present on the clipboard,
1737 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1741 GtkSelectionData *data;
1742 gboolean result = FALSE;
1744 g_return_val_if_fail (clipboard != NULL, FALSE);
1746 /* If the display supports change notification we cache targets */
1747 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1748 clipboard->n_cached_targets != -1)
1751 *n_targets = clipboard->n_cached_targets;
1754 *targets = g_memdup (clipboard->cached_targets,
1755 clipboard->n_cached_targets * sizeof (GdkAtom));
1766 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1770 GdkAtom *tmp_targets;
1773 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1775 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1777 clipboard->n_cached_targets = tmp_n_targets;
1778 clipboard->cached_targets = g_memdup (tmp_targets,
1779 tmp_n_targets * sizeof (GdkAtom));
1783 *n_targets = tmp_n_targets;
1786 *targets = tmp_targets;
1788 g_free (tmp_targets);
1790 gtk_selection_data_free (data);
1796 static GtkClipboard *
1797 clipboard_peek (GdkDisplay *display,
1799 gboolean only_if_exists)
1801 GtkClipboard *clipboard = NULL;
1805 if (selection == GDK_NONE)
1806 selection = GDK_SELECTION_CLIPBOARD;
1808 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1810 tmp_list = clipboards;
1813 clipboard = tmp_list->data;
1814 if (clipboard->selection == selection)
1817 tmp_list = tmp_list->next;
1820 if (!tmp_list && !only_if_exists)
1822 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1823 clipboard->selection = selection;
1824 clipboard->display = display;
1825 clipboard->n_cached_targets = -1;
1826 clipboard->n_storable_targets = -1;
1827 clipboards = g_slist_prepend (clipboards, clipboard);
1828 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1829 g_signal_connect (display, "closed",
1830 G_CALLBACK (clipboard_display_closed), clipboard);
1831 gdk_display_request_selection_notification (display, selection);
1838 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1839 GdkEventOwnerChange *event)
1841 if (clipboard->n_cached_targets != -1)
1843 clipboard->n_cached_targets = -1;
1844 g_free (clipboard->cached_targets);
1849 * gtk_clipboard_wait_is_target_available:
1850 * @clipboard: a #GtkClipboard
1851 * @target: A #GdkAtom indicating which target to look for.
1853 * Checks if a clipboard supports pasting data of a given type. This
1854 * function can be used to determine if a "Paste" menu item should be
1855 * insensitive or not.
1857 * If you want to see if there's text available on the clipboard, use
1858 * gtk_clipboard_wait_is_text_available () instead.
1860 * Return value: %TRUE if the target is available, %FALSE otherwise.
1865 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1870 gboolean retval = FALSE;
1872 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1875 for (i = 0; i < n_targets; i++)
1877 if (targets[i] == target)
1890 * _gtk_clipboard_handle_event:
1891 * @event: a owner change event
1893 * Emits the #GtkClipboard::owner-change signal on the appropriate @clipboard.
1898 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1900 GdkDisplay *display;
1901 GtkClipboard *clipboard;
1903 display = gdk_drawable_get_display (event->window);
1904 clipboard = clipboard_peek (display, event->selection, TRUE);
1907 g_signal_emit (clipboard,
1908 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1912 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1914 g_main_loop_quit (clipboard->store_loop);
1920 * gtk_clipboard_set_can_store:
1921 * @clipboard: a #GtkClipboard
1922 * @targets: array containing information about which forms should be stored
1923 * or %NULL to indicate that all forms should be stored.
1924 * @n_targets: number of elements in @targets
1926 * Hints that the clipboard data should be stored somewhere when the
1927 * application exits or when gtk_clipboard_store () is called.
1929 * This value is reset when the clipboard owner changes.
1930 * Where the clipboard data is stored is platform dependent,
1931 * see gdk_display_store_clipboard () for more information.
1936 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1937 const GtkTargetEntry *targets,
1940 GtkWidget *clipboard_widget;
1942 static const GtkTargetEntry save_targets[] = {
1943 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1946 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1947 g_return_if_fail (n_targets >= 0);
1949 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1952 g_free (clipboard->storable_targets);
1954 clipboard_widget = get_clipboard_widget (clipboard->display);
1956 /* n_storable_targets being -1 means that
1957 * gtk_clipboard_set_can_store hasn't been called since the
1958 * clipboard owner changed. We only want to add SAVE_TARGETS and
1959 * ref the owner once , so we do that here
1961 if (clipboard->n_storable_targets == -1)
1963 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1966 /* Ref the owner so it won't go away */
1967 if (clipboard->have_owner)
1968 g_object_ref (clipboard->user_data);
1971 clipboard->n_storable_targets = n_targets;
1972 clipboard->storable_targets = g_new (GdkAtom, n_targets);
1973 for (i = 0; i < n_targets; i++)
1974 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1978 gtk_clipboard_selection_notify (GtkWidget *widget,
1979 GdkEventSelection *event,
1980 GtkClipboard *clipboard)
1982 if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
1983 clipboard->storing_selection)
1984 g_main_loop_quit (clipboard->store_loop);
1990 * gtk_clipboard_store:
1991 * @clipboard: a #GtkClipboard
1993 * Stores the current clipboard data somewhere so that it will stay
1994 * around after the application has quit.
1999 gtk_clipboard_store (GtkClipboard *clipboard)
2001 GtkWidget *clipboard_widget;
2003 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2005 if (clipboard->n_storable_targets < 0)
2008 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
2011 g_object_ref (clipboard);
2013 clipboard_widget = get_clipboard_widget (clipboard->display);
2014 clipboard->notify_signal_id = g_signal_connect (clipboard_widget,
2015 "selection-notify-event",
2016 G_CALLBACK (gtk_clipboard_selection_notify),
2019 gdk_display_store_clipboard (clipboard->display,
2020 clipboard_widget->window,
2021 clipboard_get_timestamp (clipboard),
2022 clipboard->storable_targets,
2023 clipboard->n_storable_targets);
2025 clipboard->storing_selection = TRUE;
2027 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
2028 clipboard->store_timeout = g_timeout_add_seconds (10, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
2030 if (g_main_loop_is_running (clipboard->store_loop))
2032 GDK_THREADS_LEAVE ();
2033 g_main_loop_run (clipboard->store_loop);
2034 GDK_THREADS_ENTER ();
2037 g_main_loop_unref (clipboard->store_loop);
2038 clipboard->store_loop = NULL;
2040 g_source_remove (clipboard->store_timeout);
2041 clipboard->store_timeout = 0;
2042 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
2043 clipboard->notify_signal_id = 0;
2045 clipboard->storing_selection = FALSE;
2047 g_object_unref (clipboard);
2050 /* Stores all clipboard selections on all displays, called from
2054 _gtk_clipboard_store_all (void)
2056 GtkClipboard *clipboard;
2057 GSList *displays, *list;
2059 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
2064 GdkDisplay *display = list->data;
2066 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
2069 gtk_clipboard_store (clipboard);
2073 g_slist_free (displays);
2077 #define __GTK_CLIPBOARD_C__
2078 #include "gtkaliasdef.c"