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"
33 #ifdef GDK_WINDOWING_X11
37 #ifdef GDK_WINDOWING_WIN32
38 #include "win32/gdkwin32.h"
46 typedef struct _GtkClipboardClass GtkClipboardClass;
48 typedef struct _RequestContentsInfo RequestContentsInfo;
49 typedef struct _RequestTextInfo RequestTextInfo;
50 typedef struct _RequestImageInfo RequestImageInfo;
51 typedef struct _RequestTargetsInfo RequestTargetsInfo;
55 GObject parent_instance;
59 GtkClipboardGetFunc get_func;
60 GtkClipboardClearFunc clear_func;
66 gboolean have_selection;
69 GdkAtom *cached_targets;
70 gint n_cached_targets;
72 guint notify_signal_id;
73 gboolean storing_selection;
74 GMainLoop *store_loop;
76 gint n_storable_targets;
77 GdkAtom *storable_targets;
80 struct _GtkClipboardClass
82 GObjectClass parent_class;
84 void (*owner_change) (GtkClipboard *clipboard,
85 GdkEventOwnerChange *event);
88 struct _RequestContentsInfo
90 GtkClipboardReceivedFunc callback;
94 struct _RequestTextInfo
96 GtkClipboardTextReceivedFunc callback;
100 struct _RequestImageInfo
102 GtkClipboardImageReceivedFunc callback;
106 struct _RequestTargetsInfo
108 GtkClipboardTargetsReceivedFunc callback;
112 static void gtk_clipboard_class_init (GtkClipboardClass *class);
113 static void gtk_clipboard_finalize (GObject *object);
114 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
115 GdkEventOwnerChange *event);
117 static void clipboard_unset (GtkClipboard *clipboard);
118 static void selection_received (GtkWidget *widget,
119 GtkSelectionData *selection_data,
121 static GtkClipboard *clipboard_peek (GdkDisplay *display,
123 gboolean only_if_exists);
124 static GtkWidget * get_clipboard_widget (GdkDisplay *display);
130 TARGET_COMPOUND_TEXT,
135 static const gchar request_contents_key[] = "gtk-request-contents";
136 static GQuark request_contents_key_id = 0;
138 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
139 static GQuark clipboards_owned_key_id = 0;
141 static GObjectClass *parent_class;
142 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
145 gtk_clipboard_get_type (void)
147 static GType clipboard_type = 0;
151 static const GTypeInfo clipboard_info =
153 sizeof (GtkClipboardClass),
154 NULL, /* base_init */
155 NULL, /* base_finalize */
156 (GClassInitFunc) gtk_clipboard_class_init,
157 NULL, /* class_finalize */
158 NULL, /* class_data */
159 sizeof (GtkClipboard),
161 (GInstanceInitFunc) NULL,
164 clipboard_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkClipboard"),
168 return clipboard_type;
172 gtk_clipboard_class_init (GtkClipboardClass *class)
174 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
176 parent_class = g_type_class_peek_parent (class);
178 gobject_class->finalize = gtk_clipboard_finalize;
180 class->owner_change = gtk_clipboard_owner_change;
182 clipboard_signals[OWNER_CHANGE] =
183 g_signal_new (I_("owner_change"),
184 G_TYPE_FROM_CLASS (gobject_class),
186 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
188 _gtk_marshal_VOID__BOXED,
190 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
194 gtk_clipboard_finalize (GObject *object)
196 GtkClipboard *clipboard;
197 GtkWidget *clipboard_widget;
200 clipboard = GTK_CLIPBOARD (object);
202 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
203 if (g_slist_index (clipboards, clipboard) >= 0)
204 g_warning ("GtkClipboard prematurely finalized");
206 clipboard_widget = get_clipboard_widget (clipboard->display);
208 clipboard_unset (clipboard);
210 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
211 clipboards = g_slist_remove (clipboards, clipboard);
212 g_object_set_data (G_OBJECT (clipboard->display), I_("gtk-clipboard-list"), clipboards);
214 if (g_main_loop_is_running (clipboard->store_loop))
216 g_main_loop_quit (clipboard->store_loop);
217 g_main_loop_unref (clipboard->store_loop);
220 if (clipboard->store_timeout != 0)
221 g_source_remove (clipboard->store_timeout);
223 if (clipboard->notify_signal_id != 0)
224 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
226 g_free (clipboard->storable_targets);
228 G_OBJECT_CLASS (parent_class)->finalize (object);
232 clipboard_display_closed (GdkDisplay *display,
234 GtkClipboard *clipboard)
238 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
239 g_object_run_dispose (G_OBJECT (clipboard));
240 clipboards = g_slist_remove (clipboards, clipboard);
241 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
242 g_object_unref (clipboard);
246 * gtk_clipboard_get_for_display:
247 * @display: the display for which the clipboard is to be retrieved or created
248 * @selection: a #GdkAtom which identifies the clipboard
251 * Returns the clipboard object for the given selection.
252 * Cut/copy/paste menu items and keyboard shortcuts should use
253 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
254 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
255 * for backwards compatibility reasons.)
256 * The currently-selected object or text should be provided on the clipboard
257 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
258 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
259 * to the default clipboard, i.e. they copy the selection to what the
260 * user sees as the clipboard.
262 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
263 * ("CLIPBOARD", FALSE)</literal>. See <ulink
264 * url="http://www.freedesktop.org/Standards/clipboards-spec">
265 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
266 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
267 * selections under the X window system. On Win32 the
268 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
270 * It's possible to have arbitrary named clipboards; if you do invent
271 * new clipboards, you should prefix the selection name with an
272 * underscore (because the ICCCM requires that nonstandard atoms are
273 * underscore-prefixed), and namespace it as well. For example,
274 * if your application called "Foo" has a special-purpose
275 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
277 * Return value: the appropriate clipboard object. If no
278 * clipboard already exists, a new one will
279 * be created. Once a clipboard object has
280 * been created, it is persistent and, since
281 * it is owned by GTK+, must not be freed or
287 gtk_clipboard_get_for_display (GdkDisplay *display,
290 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
291 g_return_val_if_fail (!display->closed, NULL);
293 return clipboard_peek (display, selection, FALSE);
298 * gtk_clipboard_get():
299 * @selection: a #GdkAtom which identifies the clipboard
302 * Returns the clipboard object for the given selection.
303 * See gtk_clipboard_get_for_display() for complete details.
305 * Return value: the appropriate clipboard object. If no
306 * clipboard already exists, a new one will
307 * be created. Once a clipboard object has
308 * been created, it is persistent and, since
309 * it is owned by GTK+, must not be freed or
313 gtk_clipboard_get (GdkAtom selection)
315 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
319 selection_get_cb (GtkWidget *widget,
320 GtkSelectionData *selection_data,
324 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, selection_data->selection);
326 if (clipboard && clipboard->get_func)
327 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
331 selection_clear_event_cb (GtkWidget *widget,
332 GdkEventSelection *event)
334 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
338 clipboard_unset (clipboard);
347 make_clipboard_widget (GdkDisplay *display,
350 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
352 g_signal_connect (widget, "selection_received",
353 G_CALLBACK (selection_received), NULL);
357 /* We need this for gdk_x11_get_server_time() */
358 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
360 g_signal_connect (widget, "selection_get",
361 G_CALLBACK (selection_get_cb), NULL);
362 g_signal_connect (widget, "selection_clear_event",
363 G_CALLBACK (selection_clear_event_cb), NULL);
370 get_clipboard_widget (GdkDisplay *display)
372 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
375 clip_widget = make_clipboard_widget (display, TRUE);
376 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
382 /* This function makes a very good guess at what the correct
383 * timestamp for a selection request should be. If there is
384 * a currently processed event, it uses the timestamp for that
385 * event, otherwise it uses the current server time. However,
386 * if the time resulting from that is older than the time used
387 * last time, it uses the time used last time instead.
389 * In order implement this correctly, we never use CurrentTime,
390 * but actually retrieve the actual timestamp from the server.
391 * This is a little slower but allows us to make the guarantee
392 * that the times used by this application will always ascend
393 * and we won't get selections being rejected just because
394 * we are using a correct timestamp from an event, but used
395 * CurrentTime previously.
398 clipboard_get_timestamp (GtkClipboard *clipboard)
400 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
401 guint32 timestamp = gtk_get_current_event_time ();
403 if (timestamp == GDK_CURRENT_TIME)
405 #ifdef GDK_WINDOWING_X11
406 timestamp = gdk_x11_get_server_time (clipboard_widget->window);
407 #elif defined GDK_WINDOWING_WIN32
408 timestamp = GetMessageTime ();
413 if (clipboard->timestamp != GDK_CURRENT_TIME)
415 /* Check to see if clipboard->timestamp is newer than
416 * timestamp, accounting for wraparound.
419 guint32 max = timestamp + 0x80000000;
421 if ((max > timestamp &&
422 (clipboard->timestamp > timestamp &&
423 clipboard->timestamp <= max)) ||
425 (clipboard->timestamp > timestamp ||
426 clipboard->timestamp <= max)))
428 timestamp = clipboard->timestamp;
433 clipboard->timestamp = timestamp;
439 clipboard_owner_destroyed (gpointer data)
441 GSList *clipboards = data;
444 tmp_list = clipboards;
447 GtkClipboard *clipboard = tmp_list->data;
449 clipboard->get_func = NULL;
450 clipboard->clear_func = NULL;
451 clipboard->user_data = NULL;
452 clipboard->have_owner = FALSE;
454 gtk_clipboard_clear (clipboard);
456 tmp_list = tmp_list->next;
459 g_slist_free (clipboards);
463 clipboard_add_owner_notify (GtkClipboard *clipboard)
465 if (!clipboards_owned_key_id)
466 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
468 if (clipboard->have_owner)
469 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
470 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
471 clipboards_owned_key_id),
473 clipboard_owner_destroyed);
477 clipboard_remove_owner_notify (GtkClipboard *clipboard)
479 if (clipboard->have_owner)
480 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
481 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
482 clipboards_owned_key_id),
484 clipboard_owner_destroyed);
488 gtk_clipboard_set_contents (GtkClipboard *clipboard,
489 const GtkTargetEntry *targets,
491 GtkClipboardGetFunc get_func,
492 GtkClipboardClearFunc clear_func,
496 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
498 if (gtk_selection_owner_set_for_display (clipboard->display,
500 clipboard->selection,
501 clipboard_get_timestamp (clipboard)))
503 clipboard->have_selection = TRUE;
505 if (!(clipboard->have_owner && have_owner) ||
506 clipboard->user_data != user_data)
508 clipboard_unset (clipboard);
510 if (clipboard->get_func)
512 /* Calling unset() caused the clipboard contents to be reset!
513 * Avoid leaking and return
515 if (!(clipboard->have_owner && have_owner) ||
516 clipboard->user_data != user_data)
518 (*clear_func) (clipboard, user_data);
526 clipboard->user_data = user_data;
527 clipboard->have_owner = have_owner;
529 clipboard_add_owner_notify (clipboard);
534 clipboard->get_func = get_func;
535 clipboard->clear_func = clear_func;
537 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
538 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
548 * gtk_clipboard_set_with_data:
549 * @clipboard: a #GtkClipboard
550 * @targets: array containing information about the available forms for the
552 * @n_targets: number of elements in @targets
553 * @get_func: function to call to get the actual clipboard data
554 * @clear_func: when the clipboard contents are set again, this function will
555 * be called, and @get_func will not be subsequently called.
556 * @user_data: user data to pass to @get_func and @clear_func.
558 * Virtually sets the contents of the specified clipboard by providing
559 * a list of supported formats for the clipboard data and a function
560 * to call to get the actual data when it is requested.
562 * Return value: %TRUE if setting the clipboard data succeeded. If setting
563 * the clipboard data failed the provided callback functions
567 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
568 const GtkTargetEntry *targets,
570 GtkClipboardGetFunc get_func,
571 GtkClipboardClearFunc clear_func,
574 g_return_val_if_fail (clipboard != NULL, FALSE);
575 g_return_val_if_fail (targets != NULL, FALSE);
576 g_return_val_if_fail (get_func != NULL, FALSE);
578 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
579 get_func, clear_func, user_data,
584 * gtk_clipboard_set_with_owner:
585 * @clipboard: a #GtkClipboard
586 * @targets: array containing information about the available forms for the
588 * @n_targets: number of elements in @targets
589 * @get_func: function to call to get the actual clipboard data
590 * @clear_func: when the clipboard contents are set again, this function will
591 * be called, and @get_func will not be subsequently called.
592 * @owner: an object that "owns" the data. This object will be passed
593 * to the callbacks when called.
595 * Virtually sets the contents of the specified clipboard by providing
596 * a list of supported formats for the clipboard data and a function
597 * to call to get the actual data when it is requested.
599 * The difference between this function and gtk_clipboard_set_with_data()
600 * is that instead of an generic @user_data pointer, a #GObject is passed
603 * Return value: %TRUE if setting the clipboard data succeeded. If setting
604 * the clipboard data failed the provided callback functions
608 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
609 const GtkTargetEntry *targets,
611 GtkClipboardGetFunc get_func,
612 GtkClipboardClearFunc clear_func,
615 g_return_val_if_fail (clipboard != NULL, FALSE);
616 g_return_val_if_fail (targets != NULL, FALSE);
617 g_return_val_if_fail (get_func != NULL, FALSE);
618 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
620 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
621 get_func, clear_func, owner,
626 * gtk_clipboard_get_owner:
627 * @clipboard: a #GtkClipboard
629 * If the clipboard contents callbacks were set with
630 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
631 * gtk_clipboard_clear() has not subsequently called, returns the owner set
632 * by gtk_clipboard_set_with_owner().
634 * Return value: the owner of the clipboard, if any; otherwise %NULL.
637 gtk_clipboard_get_owner (GtkClipboard *clipboard)
639 g_return_val_if_fail (clipboard != NULL, NULL);
641 if (clipboard->have_owner)
642 return clipboard->user_data;
648 clipboard_unset (GtkClipboard *clipboard)
650 GtkClipboardClearFunc old_clear_func;
652 gboolean old_have_owner;
653 gint old_n_storable_targets;
655 old_clear_func = clipboard->clear_func;
656 old_data = clipboard->user_data;
657 old_have_owner = clipboard->have_owner;
658 old_n_storable_targets = clipboard->n_storable_targets;
662 clipboard_remove_owner_notify (clipboard);
663 clipboard->have_owner = FALSE;
666 clipboard->n_storable_targets = -1;
667 g_free (clipboard->storable_targets);
668 clipboard->storable_targets = NULL;
670 clipboard->get_func = NULL;
671 clipboard->clear_func = NULL;
672 clipboard->user_data = NULL;
675 old_clear_func (clipboard, old_data);
677 /* If we've transferred the clipboard data to the manager,
680 if (old_have_owner &&
681 old_n_storable_targets != -1)
682 g_object_unref (old_data);
686 * gtk_clipboard_clear:
687 * @clipboard: a #GtkClipboard
689 * Clears the contents of the clipboard. Generally this should only
690 * be called between the time you call gtk_clipboard_set_with_owner()
691 * or gtk_clipboard_set_with_data(),
692 * and when the @clear_func you supplied is called. Otherwise, the
693 * clipboard may be owned by someone else.
696 gtk_clipboard_clear (GtkClipboard *clipboard)
698 g_return_if_fail (clipboard != NULL);
700 if (clipboard->have_selection)
701 gtk_selection_owner_set_for_display (clipboard->display,
703 clipboard->selection,
704 clipboard_get_timestamp (clipboard));
708 text_get_func (GtkClipboard *clipboard,
709 GtkSelectionData *selection_data,
713 gtk_selection_data_set_text (selection_data, data, -1);
717 text_clear_func (GtkClipboard *clipboard,
725 * gtk_clipboard_set_text:
726 * @clipboard: a #GtkClipboard object
727 * @text: a UTF-8 string.
728 * @len: length of @text, in bytes, or -1, in which case
729 * the length will be determined with <function>strlen()</function>.
731 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
732 * make a copy of the text and take responsibility for responding
733 * for requests for the text, and for converting the text into
734 * the requested format.
737 gtk_clipboard_set_text (GtkClipboard *clipboard,
743 GtkTargetEntry *targets;
746 g_return_if_fail (clipboard != NULL);
747 g_return_if_fail (text != NULL);
749 list = gtk_target_list_new (NULL, 0);
750 gtk_target_list_add_text_targets (list, 0);
752 n_targets = g_list_length (list->list);
753 targets = g_new0 (GtkTargetEntry, n_targets);
754 for (l = list->list, i = 0; l; l = l->next, i++)
756 GtkTargetPair *pair = (GtkTargetPair *)l->data;
757 targets[i].target = gdk_atom_name (pair->target);
763 gtk_clipboard_set_with_data (clipboard,
765 text_get_func, text_clear_func,
766 g_strndup (text, len));
767 gtk_clipboard_set_can_store (clipboard, NULL, 0);
769 for (i = 0; i < n_targets; i++)
770 g_free (targets[i].target);
772 gtk_target_list_unref (list);
776 pixbuf_get_func (GtkClipboard *clipboard,
777 GtkSelectionData *selection_data,
781 gtk_selection_data_set_pixbuf (selection_data, data);
785 pixbuf_clear_func (GtkClipboard *clipboard,
788 g_object_unref (data);
792 * gtk_clipboard_set_image:
793 * @clipboard: a #GtkClipboard object
794 * @pixbuf: a #GdkPixbuf
796 * Sets the contents of the clipboard to the given #GdkPixbuf.
797 * GTK+ will take responsibility for responding for requests
798 * for the image, and for converting the image into the
804 gtk_clipboard_set_image (GtkClipboard *clipboard,
809 GtkTargetEntry *targets;
812 g_return_if_fail (clipboard != NULL);
813 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
815 list = gtk_target_list_new (NULL, 0);
816 gtk_target_list_add_image_targets (list, 0, TRUE);
818 n_targets = g_list_length (list->list);
819 targets = g_new0 (GtkTargetEntry, n_targets);
820 for (l = list->list, i = 0; l; l = l->next, i++)
822 GtkTargetPair *pair = (GtkTargetPair *)l->data;
823 targets[i].target = gdk_atom_name (pair->target);
826 gtk_clipboard_set_with_data (clipboard,
828 pixbuf_get_func, pixbuf_clear_func,
829 g_object_ref (pixbuf));
830 gtk_clipboard_set_can_store (clipboard, NULL, 0);
832 for (i = 0; i < n_targets; i++)
833 g_free (targets[i].target);
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 = 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 ("UTF8_STRING", FALSE))
939 gtk_clipboard_request_contents (clipboard,
940 gdk_atom_intern ("COMPOUND_TEXT", FALSE),
941 request_text_received_func, info);
944 else if (selection_data->target == gdk_atom_intern ("COMPOUND_TEXT", FALSE))
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 ("UTF8_STRING", FALSE),
990 request_text_received_func,
995 request_image_received_func (GtkClipboard *clipboard,
996 GtkSelectionData *selection_data,
999 RequestImageInfo *info = data;
1000 GdkPixbuf *result = NULL;
1002 result = gtk_selection_data_get_pixbuf (selection_data);
1006 /* If we asked for image/png and didn't get it, try image/jpeg;
1007 * if we asked for image/jpeg and didn't get it, try image/gif;
1008 * if we asked for image/gif and didn't get it, try image/bmp;
1009 * If we asked for anything else and didn't get it, give up.
1011 if (selection_data->target == gdk_atom_intern ("image/png", FALSE))
1013 gtk_clipboard_request_contents (clipboard,
1014 gdk_atom_intern ("image/jpeg", FALSE),
1015 request_image_received_func, info);
1018 else if (selection_data->target == gdk_atom_intern ("image/jpeg", FALSE))
1020 gtk_clipboard_request_contents (clipboard,
1021 gdk_atom_intern ("image/gif", FALSE),
1022 request_image_received_func, info);
1025 else if (selection_data->target == gdk_atom_intern ("image/gif", FALSE))
1027 gtk_clipboard_request_contents (clipboard,
1028 gdk_atom_intern ("image/bmp", FALSE),
1029 request_image_received_func, info);
1034 info->callback (clipboard, result, info->user_data);
1036 g_object_unref (result);
1040 * gtk_clipboard_request_image:
1041 * @clipboard: a #GtkClipboard
1042 * @callback: a function to call when the image is received,
1043 * or the retrieval fails. (It will always be called
1044 * one way or the other.)
1045 * @user_data: user data to pass to @callback.
1047 * Requests the contents of the clipboard as image. When the image is
1048 * later received, it will be converted to a #GdkPixbuf, and
1049 * @callback will be called.
1051 * The @pixbuf parameter to @callback will contain the resulting
1052 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1053 * could happen for various reasons, in particular if the clipboard
1054 * was empty or if the contents of the clipboard could not be
1055 * converted into an image.
1060 gtk_clipboard_request_image (GtkClipboard *clipboard,
1061 GtkClipboardImageReceivedFunc callback,
1064 RequestImageInfo *info;
1066 g_return_if_fail (clipboard != NULL);
1067 g_return_if_fail (callback != NULL);
1069 info = g_new (RequestImageInfo, 1);
1070 info->callback = callback;
1071 info->user_data = user_data;
1073 gtk_clipboard_request_contents (clipboard,
1074 gdk_atom_intern ("image/png", FALSE),
1075 request_image_received_func,
1080 request_targets_received_func (GtkClipboard *clipboard,
1081 GtkSelectionData *selection_data,
1084 RequestTargetsInfo *info = data;
1085 GdkAtom *targets = NULL;
1088 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1090 info->callback (clipboard, targets, n_targets, info->user_data);
1097 * gtk_clipboard_request_targets:
1098 * @clipboard: a #GtkClipboard
1099 * @callback: a function to call when the targets are received,
1100 * or the retrieval fails. (It will always be called
1101 * one way or the other.)
1102 * @user_data: user data to pass to @callback.
1104 * Requests the contents of the clipboard as list of supported targets.
1105 * When the list is later received, @callback will be called.
1107 * The @targets parameter to @callback will contain the resulting targets if
1108 * the request succeeded, or %NULL if it failed.
1113 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1114 GtkClipboardTargetsReceivedFunc callback,
1117 RequestTargetsInfo *info;
1119 g_return_if_fail (clipboard != NULL);
1120 g_return_if_fail (callback != NULL);
1122 /* If the display supports change notification we cache targets */
1123 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1124 clipboard->n_cached_targets != -1)
1126 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1130 info = g_new (RequestTargetsInfo, 1);
1131 info->callback = callback;
1132 info->user_data = user_data;
1134 gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE),
1135 request_targets_received_func,
1146 clipboard_received_func (GtkClipboard *clipboard,
1147 GtkSelectionData *selection_data,
1150 WaitResults *results = data;
1152 if (selection_data->length >= 0)
1153 results->data = gtk_selection_data_copy (selection_data);
1155 g_main_loop_quit (results->loop);
1159 * gtk_clipboard_wait_for_contents:
1160 * @clipboard: a #GtkClipboard
1161 * @target: an atom representing the form into which the clipboard
1162 * owner should convert the selection.
1164 * Requests the contents of the clipboard using the given target.
1165 * This function waits for the data to be received using the main
1166 * loop, so events, timeouts, etc, may be dispatched during the wait.
1168 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1169 * if retrieving the given target failed. If non-%NULL,
1170 * this value must be freed with gtk_selection_data_free()
1171 * when you are finished with it.
1174 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1177 WaitResults results;
1179 g_return_val_if_fail (clipboard != NULL, NULL);
1180 g_return_val_if_fail (target != GDK_NONE, NULL);
1182 results.data = NULL;
1183 results.loop = g_main_loop_new (NULL, TRUE);
1185 gtk_clipboard_request_contents (clipboard, target,
1186 clipboard_received_func,
1189 if (g_main_loop_is_running (results.loop))
1191 GDK_THREADS_LEAVE ();
1192 g_main_loop_run (results.loop);
1193 GDK_THREADS_ENTER ();
1196 g_main_loop_unref (results.loop);
1198 return results.data;
1202 clipboard_text_received_func (GtkClipboard *clipboard,
1206 WaitResults *results = data;
1208 results->data = g_strdup (text);
1209 g_main_loop_quit (results->loop);
1213 * gtk_clipboard_wait_for_text:
1214 * @clipboard: a #GtkClipboard
1216 * Requests the contents of the clipboard as text and converts
1217 * the result to UTF-8 if necessary. This function waits for
1218 * the data to be received using the main loop, so events,
1219 * timeouts, etc, may be dispatched during the wait.
1221 * Return value: a newly-allocated UTF-8 string which must
1222 * be freed with g_free(), or %NULL if retrieving
1223 * the selection data failed. (This could happen
1224 * for various reasons, in particular if the
1225 * clipboard was empty or if the contents of the
1226 * clipboard could not be converted into text form.)
1229 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1231 WaitResults results;
1233 g_return_val_if_fail (clipboard != NULL, NULL);
1235 results.data = NULL;
1236 results.loop = g_main_loop_new (NULL, TRUE);
1238 gtk_clipboard_request_text (clipboard,
1239 clipboard_text_received_func,
1242 if (g_main_loop_is_running (results.loop))
1244 GDK_THREADS_LEAVE ();
1245 g_main_loop_run (results.loop);
1246 GDK_THREADS_ENTER ();
1249 g_main_loop_unref (results.loop);
1251 return results.data;
1255 clipboard_image_received_func (GtkClipboard *clipboard,
1259 WaitResults *results = data;
1262 results->data = g_object_ref (pixbuf);
1264 g_main_loop_quit (results->loop);
1268 * gtk_clipboard_wait_for_image:
1269 * @clipboard: a #GtkClipboard
1271 * Requests the contents of the clipboard as image and converts
1272 * the result to a #GdkPixbuf. This function waits for
1273 * the data to be received using the main loop, so events,
1274 * timeouts, etc, may be dispatched during the wait.
1276 * Return value: a newly-allocated #GdkPixbuf object which must
1277 * be disposed with g_object_unref(), or %NULL if
1278 * retrieving the selection data failed. (This
1279 * could happen for various reasons, in particular
1280 * if the clipboard was empty or if the contents of
1281 * the clipboard could not be converted into an image.)
1286 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1288 WaitResults results;
1290 g_return_val_if_fail (clipboard != NULL, NULL);
1292 results.data = NULL;
1293 results.loop = g_main_loop_new (NULL, TRUE);
1295 gtk_clipboard_request_image (clipboard,
1296 clipboard_image_received_func,
1299 if (g_main_loop_is_running (results.loop))
1301 GDK_THREADS_LEAVE ();
1302 g_main_loop_run (results.loop);
1303 GDK_THREADS_ENTER ();
1306 g_main_loop_unref (results.loop);
1308 return results.data;
1312 * gtk_clipboard_get_display:
1313 * @clipboard: a #GtkClipboard
1315 * Gets the #GdkDisplay associated with @clipboard
1317 * Return value: the #GdkDisplay associated with @clipboard
1322 gtk_clipboard_get_display (GtkClipboard *clipboard)
1324 g_return_val_if_fail (clipboard != NULL, NULL);
1326 return clipboard->display;
1330 * gtk_clipboard_wait_is_text_available:
1331 * @clipboard: a #GtkClipboard
1333 * Test to see if there is text available to be pasted
1334 * This is done by requesting the TARGETS atom and checking
1335 * if it contains any of the supported text targets. This function
1336 * waits for the data to be received using the main loop, so events,
1337 * timeouts, etc, may be dispatched during the wait.
1339 * This function is a little faster than calling
1340 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1343 * Return value: %TRUE is there is text available, %FALSE otherwise.
1346 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1348 GtkSelectionData *data;
1349 gboolean result = FALSE;
1351 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1354 result = gtk_selection_data_targets_include_text (data);
1355 gtk_selection_data_free (data);
1362 * gtk_clipboard_wait_is_image_available:
1363 * @clipboard: a #GtkClipboard
1365 * Test to see if there is an image available to be pasted
1366 * This is done by requesting the TARGETS atom and checking
1367 * if it contains any of the supported image targets. This function
1368 * waits for the data to be received using the main loop, so events,
1369 * timeouts, etc, may be dispatched during the wait.
1371 * This function is a little faster than calling
1372 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1373 * the actual image data.
1375 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1380 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1382 GtkSelectionData *data;
1383 gboolean result = FALSE;
1385 data = gtk_clipboard_wait_for_contents (clipboard,
1386 gdk_atom_intern ("TARGETS", FALSE));
1389 result = gtk_selection_data_targets_include_image (data, FALSE);
1390 gtk_selection_data_free (data);
1397 * gtk_clipboard_wait_for_targets
1398 * @clipboard: a #GtkClipboard
1399 * @targets: location to store an array of targets. The result
1400 * stored here must be freed with g_free().
1401 * @n_targets: location to store number of items in @targets.
1403 * Returns a list of targets that are present on the clipboard, or %NULL
1404 * if there aren't any targets available. The returned list must be
1405 * freed with g_free().
1406 * This function waits for the data to be received using the main
1407 * loop, so events, timeouts, etc, may be dispatched during the wait.
1409 * Return value: %TRUE if any targets are present on the clipboard,
1415 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1419 GtkSelectionData *data;
1420 gboolean result = FALSE;
1422 g_return_val_if_fail (clipboard != NULL, FALSE);
1424 /* If the display supports change notification we cache targets */
1425 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1426 clipboard->n_cached_targets != -1)
1429 *n_targets = clipboard->n_cached_targets;
1432 *targets = g_memdup (clipboard->cached_targets,
1433 clipboard->n_cached_targets * sizeof (GdkAtom));
1444 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1448 GdkAtom *tmp_targets;
1451 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1453 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1455 clipboard->n_cached_targets = tmp_n_targets;
1456 clipboard->cached_targets = g_memdup (tmp_targets,
1457 tmp_n_targets * sizeof (GdkAtom));
1461 *n_targets = tmp_n_targets;
1464 *targets = tmp_targets;
1466 g_free (tmp_targets);
1468 gtk_selection_data_free (data);
1474 static GtkClipboard *
1475 clipboard_peek (GdkDisplay *display,
1477 gboolean only_if_exists)
1479 GtkClipboard *clipboard = NULL;
1483 if (selection == GDK_NONE)
1484 selection = GDK_SELECTION_CLIPBOARD;
1486 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1488 tmp_list = clipboards;
1491 clipboard = tmp_list->data;
1492 if (clipboard->selection == selection)
1495 tmp_list = tmp_list->next;
1498 if (!tmp_list && !only_if_exists)
1500 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1501 clipboard->selection = selection;
1502 clipboard->display = display;
1503 clipboard->n_cached_targets = -1;
1504 clipboard->n_storable_targets = -1;
1505 clipboards = g_slist_prepend (clipboards, clipboard);
1506 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1507 g_signal_connect (display, "closed",
1508 G_CALLBACK (clipboard_display_closed), clipboard);
1509 gdk_display_request_selection_notification (display, selection);
1516 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1517 GdkEventOwnerChange *event)
1519 if (clipboard->n_cached_targets != -1)
1521 clipboard->n_cached_targets = -1;
1522 g_free (clipboard->cached_targets);
1527 * gtk_clipboard_wait_is_target_available:
1528 * @clipboard: a #GtkClipboard
1529 * @target: A #GdkAtom indicating which target to look for.
1531 * Checks if a clipboard supports pasting data of a given type. This
1532 * function can be used to determine if a "Paste" menu item should be
1533 * insensitive or not.
1535 * If you want to see if there's text available on the clipboard, use
1536 * gtk_clipboard_wait_is_text_available () instead.
1538 * Return value: %TRUE if the target is available, %FALSE otherwise.
1543 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1548 gboolean retval = FALSE;
1550 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1553 for (i = 0; i < n_targets; i++)
1555 if (targets[i] == target)
1568 * _gtk_clipboard_handle_event:
1569 * @event: a owner change event
1571 * Emits the ::owner_change signal on the appropriate @clipboard.
1576 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1578 GdkDisplay *display;
1579 GtkClipboard *clipboard;
1581 display = gdk_drawable_get_display (event->window);
1582 clipboard = clipboard_peek (display, event->selection, TRUE);
1585 g_signal_emit (clipboard,
1586 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1590 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1592 g_main_loop_quit (clipboard->store_loop);
1598 * gtk_clipboard_set_can_store:
1599 * @clipboard: a #GtkClipboard
1600 * @targets: array containing information about which forms should be stored
1601 * or %NULL to indicate that all forms should be stored.
1602 * @n_targets: number of elements in @targets
1604 * Hints that the clipboard data should be stored somewhere when the
1605 * application exits or when gtk_clipboard_store () is called.
1607 * This value is reset when the clipboard owner changes.
1608 * Where the clipboard data is stored is platform dependent,
1609 * see gdk_display_store_clipboard () for more information.
1614 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1615 const GtkTargetEntry *targets,
1618 GtkWidget *clipboard_widget;
1620 static const GtkTargetEntry save_targets[] = {
1621 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1624 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1625 g_return_if_fail (n_targets >= 0);
1627 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1630 g_free (clipboard->storable_targets);
1632 clipboard_widget = get_clipboard_widget (clipboard->display);
1634 /* n_storable_targets being -1 means that
1635 * gtk_clipboard_set_can_store hasn't been called since the
1636 * clipboard owner changed. We only want to add SAVE_TARGETS and
1637 * ref the owner once , so we do that here
1639 if (clipboard->n_storable_targets == -1)
1641 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1644 /* Ref the owner so it won't go away */
1645 if (clipboard->have_owner)
1646 g_object_ref (clipboard->user_data);
1649 clipboard->n_storable_targets = n_targets;
1650 clipboard->storable_targets = g_new (GdkAtom, n_targets);
1651 for (i = 0; i < n_targets; i++)
1652 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1656 gtk_clipboard_selection_notify (GtkWidget *widget,
1657 GdkEventSelection *event,
1658 GtkClipboard *clipboard)
1660 if (event->selection == gdk_atom_intern ("CLIPBOARD_MANAGER", FALSE) &&
1661 clipboard->storing_selection)
1662 g_main_loop_quit (clipboard->store_loop);
1668 * gtk_clipboard_store:
1669 * @clipboard: a #GtkClipboard
1671 * Stores the current clipboard data somewhere so that it will stay
1672 * around after the application has quit.
1677 gtk_clipboard_store (GtkClipboard *clipboard)
1679 GtkWidget *clipboard_widget;
1681 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1683 if (clipboard->n_storable_targets < 0)
1686 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
1689 clipboard_widget = get_clipboard_widget (clipboard->display);
1690 clipboard->notify_signal_id = g_signal_connect (clipboard_widget, "selection_notify_event",
1691 G_CALLBACK (gtk_clipboard_selection_notify), clipboard);
1693 gdk_display_store_clipboard (clipboard->display,
1694 clipboard_widget->window,
1695 clipboard_get_timestamp (clipboard),
1696 clipboard->storable_targets,
1697 clipboard->n_storable_targets);
1699 clipboard->storing_selection = TRUE;
1701 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
1702 clipboard->store_timeout = g_timeout_add (10000, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
1704 if (g_main_loop_is_running (clipboard->store_loop))
1706 GDK_THREADS_LEAVE ();
1707 g_main_loop_run (clipboard->store_loop);
1708 GDK_THREADS_ENTER ();
1711 g_main_loop_unref (clipboard->store_loop);
1712 clipboard->store_loop = NULL;
1714 g_source_remove (clipboard->store_timeout);
1715 clipboard->store_timeout = 0;
1716 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
1717 clipboard->notify_signal_id = 0;
1719 clipboard->storing_selection = FALSE;
1722 /* Stores all clipboard selections on all displays, called from
1726 _gtk_clipboard_store_all (void)
1728 GtkClipboard *clipboard;
1729 GSList *displays, *list;
1731 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
1736 GdkDisplay *display = list->data;
1738 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
1741 gtk_clipboard_store (clipboard);
1745 g_slist_free (displays);
1749 #define __GTK_CLIPBOARD_C__
1750 #include "gtkaliasdef.c"