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"
32 #ifdef GDK_WINDOWING_X11
36 #ifdef GDK_WINDOWING_WIN32
37 #include "win32/gdkwin32.h"
45 typedef struct _GtkClipboardClass GtkClipboardClass;
47 typedef struct _RequestContentsInfo RequestContentsInfo;
48 typedef struct _RequestTextInfo RequestTextInfo;
49 typedef struct _RequestImageInfo RequestImageInfo;
50 typedef struct _RequestTargetsInfo RequestTargetsInfo;
54 GObject parent_instance;
58 GtkClipboardGetFunc get_func;
59 GtkClipboardClearFunc clear_func;
65 gboolean have_selection;
68 GdkAtom *cached_targets;
69 gint n_cached_targets;
71 guint notify_signal_id;
72 gboolean storing_selection;
73 GMainLoop *store_loop;
75 gint n_storable_targets;
76 GdkAtom *storable_targets;
79 struct _GtkClipboardClass
81 GObjectClass parent_class;
83 void (*owner_change) (GtkClipboard *clipboard,
84 GdkEventOwnerChange *event);
87 struct _RequestContentsInfo
89 GtkClipboardReceivedFunc callback;
93 struct _RequestTextInfo
95 GtkClipboardTextReceivedFunc callback;
99 struct _RequestImageInfo
101 GtkClipboardImageReceivedFunc callback;
105 struct _RequestTargetsInfo
107 GtkClipboardTargetsReceivedFunc callback;
111 static void gtk_clipboard_class_init (GtkClipboardClass *class);
112 static void gtk_clipboard_finalize (GObject *object);
113 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
114 GdkEventOwnerChange *event);
116 static void clipboard_unset (GtkClipboard *clipboard);
117 static void selection_received (GtkWidget *widget,
118 GtkSelectionData *selection_data,
120 static GtkClipboard *clipboard_peek (GdkDisplay *display,
122 gboolean only_if_exists);
123 static GtkWidget * get_clipboard_widget (GdkDisplay *display);
129 TARGET_COMPOUND_TEXT,
134 static const gchar request_contents_key[] = "gtk-request-contents";
135 static GQuark request_contents_key_id = 0;
137 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
138 static GQuark clipboards_owned_key_id = 0;
140 static GObjectClass *parent_class;
141 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
144 gtk_clipboard_get_type (void)
146 static GType clipboard_type = 0;
150 static const GTypeInfo clipboard_info =
152 sizeof (GtkClipboardClass),
153 NULL, /* base_init */
154 NULL, /* base_finalize */
155 (GClassInitFunc) gtk_clipboard_class_init,
156 NULL, /* class_finalize */
157 NULL, /* class_data */
158 sizeof (GtkClipboard),
160 (GInstanceInitFunc) NULL,
163 clipboard_type = g_type_register_static (G_TYPE_OBJECT, "GtkClipboard",
167 return clipboard_type;
171 gtk_clipboard_class_init (GtkClipboardClass *class)
173 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
175 parent_class = g_type_class_peek_parent (class);
177 gobject_class->finalize = gtk_clipboard_finalize;
179 class->owner_change = gtk_clipboard_owner_change;
181 clipboard_signals[OWNER_CHANGE] =
182 g_signal_new ("owner_change",
183 G_TYPE_FROM_CLASS (gobject_class),
185 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
187 _gtk_marshal_VOID__BOXED,
189 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
193 gtk_clipboard_finalize (GObject *object)
195 GtkClipboard *clipboard;
196 GtkWidget *clipboard_widget;
199 clipboard = GTK_CLIPBOARD (object);
201 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
202 if (g_slist_index (clipboards, clipboard) >= 0)
203 g_warning ("GtkClipboard prematurely finalized");
205 clipboard_widget = get_clipboard_widget (clipboard->display);
207 clipboard_unset (clipboard);
209 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
210 clipboards = g_slist_remove (clipboards, clipboard);
211 g_object_set_data (G_OBJECT (clipboard->display), "gtk-clipboard-list", clipboards);
213 if (g_main_loop_is_running (clipboard->store_loop))
215 g_main_loop_quit (clipboard->store_loop);
216 g_main_loop_unref (clipboard->store_loop);
219 if (clipboard->store_timeout != 0)
220 g_source_remove (clipboard->store_timeout);
222 if (clipboard->notify_signal_id != 0)
223 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
225 g_free (clipboard->storable_targets);
227 G_OBJECT_CLASS (parent_class)->finalize (object);
231 clipboard_display_closed (GdkDisplay *display,
233 GtkClipboard *clipboard)
237 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
238 g_object_run_dispose (G_OBJECT (clipboard));
239 clipboards = g_slist_remove (clipboards, clipboard);
240 g_object_set_data (G_OBJECT (display), "gtk-clipboard-list", clipboards);
241 g_object_unref (clipboard);
245 * gtk_clipboard_get_for_display:
246 * @display: the display for which the clipboard is to be retrieved or created
247 * @selection: a #GdkAtom which identifies the clipboard
250 * Returns the clipboard object for the given selection.
251 * Cut/copy/paste menu items and keyboard shortcuts should use
252 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
253 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
254 * for backwards compatibility reasons.)
255 * The currently-selected object or text should be provided on the clipboard
256 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
257 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
258 * to the default clipboard, i.e. they copy the selection to what the
259 * user sees as the clipboard.
261 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
262 * ("CLIPBOARD", FALSE)</literal>. See <ulink
263 * url="http://www.freedesktop.org/Standards/clipboards-spec">
264 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
265 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
266 * selections under the X window system. On Win32 the
267 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
269 * It's possible to have arbitrary named clipboards; if you do invent
270 * new clipboards, you should prefix the selection name with an
271 * underscore (because the ICCCM requires that nonstandard atoms are
272 * underscore-prefixed), and namespace it as well. For example,
273 * if your application called "Foo" has a special-purpose
274 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
276 * Return value: the appropriate clipboard object. If no
277 * clipboard already exists, a new one will
278 * be created. Once a clipboard object has
279 * been created, it is persistent and, since
280 * it is owned by GTK+, must not be freed or
286 gtk_clipboard_get_for_display (GdkDisplay *display,
289 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
290 g_return_val_if_fail (!display->closed, NULL);
292 return clipboard_peek (display, selection, FALSE);
297 * gtk_clipboard_get():
298 * @selection: a #GdkAtom which identifies the clipboard
301 * Returns the clipboard object for the given selection.
302 * See gtk_clipboard_get_for_display() for complete details.
304 * Return value: the appropriate clipboard object. If no
305 * clipboard already exists, a new one will
306 * be created. Once a clipboard object has
307 * been created, it is persistent and, since
308 * it is owned by GTK+, must not be freed or
312 gtk_clipboard_get (GdkAtom selection)
314 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
318 selection_get_cb (GtkWidget *widget,
319 GtkSelectionData *selection_data,
323 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, selection_data->selection);
325 if (clipboard && clipboard->get_func)
326 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
330 selection_clear_event_cb (GtkWidget *widget,
331 GdkEventSelection *event)
333 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
337 clipboard_unset (clipboard);
346 make_clipboard_widget (GdkDisplay *display,
349 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
351 g_signal_connect (widget, "selection_received",
352 G_CALLBACK (selection_received), NULL);
356 /* We need this for gdk_x11_get_server_time() */
357 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
359 g_signal_connect (widget, "selection_get",
360 G_CALLBACK (selection_get_cb), NULL);
361 g_signal_connect (widget, "selection_clear_event",
362 G_CALLBACK (selection_clear_event_cb), NULL);
369 get_clipboard_widget (GdkDisplay *display)
371 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
374 clip_widget = make_clipboard_widget (display, TRUE);
375 g_object_set_data (G_OBJECT (display), "gtk-clipboard-widget", clip_widget);
381 /* This function makes a very good guess at what the correct
382 * timestamp for a selection request should be. If there is
383 * a currently processed event, it uses the timestamp for that
384 * event, otherwise it uses the current server time. However,
385 * if the time resulting from that is older than the time used
386 * last time, it uses the time used last time instead.
388 * In order implement this correctly, we never use CurrentTime,
389 * but actually retrieve the actual timestamp from the server.
390 * This is a little slower but allows us to make the guarantee
391 * that the times used by this application will always ascend
392 * and we won't get selections being rejected just because
393 * we are using a correct timestamp from an event, but used
394 * CurrentTime previously.
397 clipboard_get_timestamp (GtkClipboard *clipboard)
399 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
400 guint32 timestamp = gtk_get_current_event_time ();
402 if (timestamp == GDK_CURRENT_TIME)
404 #ifdef GDK_WINDOWING_X11
405 timestamp = gdk_x11_get_server_time (clipboard_widget->window);
406 #elif defined GDK_WINDOWING_WIN32
407 timestamp = GetMessageTime ();
412 if (clipboard->timestamp != GDK_CURRENT_TIME)
414 /* Check to see if clipboard->timestamp is newer than
415 * timestamp, accounting for wraparound.
418 guint32 max = timestamp + 0x80000000;
420 if ((max > timestamp &&
421 (clipboard->timestamp > timestamp &&
422 clipboard->timestamp <= max)) ||
424 (clipboard->timestamp > timestamp ||
425 clipboard->timestamp <= max)))
427 timestamp = clipboard->timestamp;
432 clipboard->timestamp = timestamp;
438 clipboard_owner_destroyed (gpointer data)
440 GSList *clipboards = data;
443 tmp_list = clipboards;
446 GtkClipboard *clipboard = tmp_list->data;
448 clipboard->get_func = NULL;
449 clipboard->clear_func = NULL;
450 clipboard->user_data = NULL;
451 clipboard->have_owner = FALSE;
453 gtk_clipboard_clear (clipboard);
455 tmp_list = tmp_list->next;
458 g_slist_free (clipboards);
462 clipboard_add_owner_notify (GtkClipboard *clipboard)
464 if (!clipboards_owned_key_id)
465 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
467 if (clipboard->have_owner)
468 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
469 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
470 clipboards_owned_key_id),
472 clipboard_owner_destroyed);
476 clipboard_remove_owner_notify (GtkClipboard *clipboard)
478 if (clipboard->have_owner)
479 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
480 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
481 clipboards_owned_key_id),
483 clipboard_owner_destroyed);
487 gtk_clipboard_set_contents (GtkClipboard *clipboard,
488 const GtkTargetEntry *targets,
490 GtkClipboardGetFunc get_func,
491 GtkClipboardClearFunc clear_func,
495 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
497 if (gtk_selection_owner_set_for_display (clipboard->display,
499 clipboard->selection,
500 clipboard_get_timestamp (clipboard)))
502 clipboard->have_selection = TRUE;
504 if (!(clipboard->have_owner && have_owner) ||
505 clipboard->user_data != user_data)
507 clipboard_unset (clipboard);
509 if (clipboard->get_func)
511 /* Calling unset() caused the clipboard contents to be reset!
512 * Avoid leaking and return
514 if (!(clipboard->have_owner && have_owner) ||
515 clipboard->user_data != user_data)
517 (*clear_func) (clipboard, user_data);
525 clipboard->user_data = user_data;
526 clipboard->have_owner = have_owner;
528 clipboard_add_owner_notify (clipboard);
533 clipboard->get_func = get_func;
534 clipboard->clear_func = clear_func;
536 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
537 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
547 * gtk_clipboard_set_with_data:
548 * @clipboard: a #GtkClipboard
549 * @targets: array containing information about the available forms for the
551 * @n_targets: number of elements in @targets
552 * @get_func: function to call to get the actual clipboard data
553 * @clear_func: when the clipboard contents are set again, this function will
554 * be called, and @get_func will not be subsequently called.
555 * @user_data: user data to pass to @get_func and @clear_func.
557 * Virtually sets the contents of the specified clipboard by providing
558 * a list of supported formats for the clipboard data and a function
559 * to call to get the actual data when it is requested.
561 * Return value: %TRUE if setting the clipboard data succeeded. If setting
562 * the clipboard data failed the provided callback functions
566 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
567 const GtkTargetEntry *targets,
569 GtkClipboardGetFunc get_func,
570 GtkClipboardClearFunc clear_func,
573 g_return_val_if_fail (clipboard != NULL, FALSE);
574 g_return_val_if_fail (targets != NULL, FALSE);
575 g_return_val_if_fail (get_func != NULL, FALSE);
577 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
578 get_func, clear_func, user_data,
583 * gtk_clipboard_set_with_owner:
584 * @clipboard: a #GtkClipboard
585 * @targets: array containing information about the available forms for the
587 * @n_targets: number of elements in @targets
588 * @get_func: function to call to get the actual clipboard data
589 * @clear_func: when the clipboard contents are set again, this function will
590 * be called, and @get_func will not be subsequently called.
591 * @owner: an object that "owns" the data. This object will be passed
592 * to the callbacks when called.
594 * Virtually sets the contents of the specified clipboard by providing
595 * a list of supported formats for the clipboard data and a function
596 * to call to get the actual data when it is requested.
598 * The difference between this function and gtk_clipboard_set_with_data()
599 * is that instead of an generic @user_data pointer, a #GObject is passed
602 * Return value: %TRUE if setting the clipboard data succeeded. If setting
603 * the clipboard data failed the provided callback functions
607 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
608 const GtkTargetEntry *targets,
610 GtkClipboardGetFunc get_func,
611 GtkClipboardClearFunc clear_func,
614 g_return_val_if_fail (clipboard != NULL, FALSE);
615 g_return_val_if_fail (targets != NULL, FALSE);
616 g_return_val_if_fail (get_func != NULL, FALSE);
617 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
619 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
620 get_func, clear_func, owner,
625 * gtk_clipboard_get_owner:
626 * @clipboard: a #GtkClipboard
628 * If the clipboard contents callbacks were set with
629 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
630 * gtk_clipboard_clear() has not subsequently called, returns the owner set
631 * by gtk_clipboard_set_with_owner().
633 * Return value: the owner of the clipboard, if any; otherwise %NULL.
636 gtk_clipboard_get_owner (GtkClipboard *clipboard)
638 g_return_val_if_fail (clipboard != NULL, NULL);
640 if (clipboard->have_owner)
641 return clipboard->user_data;
647 clipboard_unset (GtkClipboard *clipboard)
649 GtkClipboardClearFunc old_clear_func;
651 gboolean old_have_owner;
652 gint old_n_storable_targets;
654 old_clear_func = clipboard->clear_func;
655 old_data = clipboard->user_data;
656 old_have_owner = clipboard->have_owner;
657 old_n_storable_targets = clipboard->n_storable_targets;
661 clipboard_remove_owner_notify (clipboard);
662 clipboard->have_owner = FALSE;
665 clipboard->n_storable_targets = -1;
666 g_free (clipboard->storable_targets);
667 clipboard->storable_targets = NULL;
669 clipboard->get_func = NULL;
670 clipboard->clear_func = NULL;
671 clipboard->user_data = NULL;
674 old_clear_func (clipboard, old_data);
676 /* If we've transferred the clipboard data to the manager,
679 if (old_have_owner &&
680 old_n_storable_targets != -1)
681 g_object_unref (old_data);
685 * gtk_clipboard_clear:
686 * @clipboard: a #GtkClipboard
688 * Clears the contents of the clipboard. Generally this should only
689 * be called between the time you call gtk_clipboard_set_with_owner()
690 * or gtk_clipboard_set_with_data(),
691 * and when the @clear_func you supplied is called. Otherwise, the
692 * clipboard may be owned by someone else.
695 gtk_clipboard_clear (GtkClipboard *clipboard)
697 g_return_if_fail (clipboard != NULL);
699 if (clipboard->have_selection)
700 gtk_selection_owner_set_for_display (clipboard->display,
702 clipboard->selection,
703 clipboard_get_timestamp (clipboard));
707 text_get_func (GtkClipboard *clipboard,
708 GtkSelectionData *selection_data,
712 gtk_selection_data_set_text (selection_data, data, -1);
716 text_clear_func (GtkClipboard *clipboard,
724 * gtk_clipboard_set_text:
725 * @clipboard: a #GtkClipboard object
726 * @text: a UTF-8 string.
727 * @len: length of @text, in bytes, or -1, in which case
728 * the length will be determined with <function>strlen()</function>.
730 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
731 * make a copy of the text and take responsibility for responding
732 * for requests for the text, and for converting the text into
733 * the requested format.
736 gtk_clipboard_set_text (GtkClipboard *clipboard,
742 GtkTargetEntry *targets;
745 g_return_if_fail (clipboard != NULL);
746 g_return_if_fail (text != NULL);
748 list = gtk_target_list_new (NULL, 0);
749 gtk_target_list_add_text_targets (list, 0);
751 n_targets = g_list_length (list->list);
752 targets = g_new0 (GtkTargetEntry, n_targets);
753 for (l = list->list, i = 0; l; l = l->next, i++)
755 GtkTargetPair *pair = (GtkTargetPair *)l->data;
756 targets[i].target = gdk_atom_name (pair->target);
762 gtk_clipboard_set_with_data (clipboard,
764 text_get_func, text_clear_func,
765 g_strndup (text, len));
766 gtk_clipboard_set_can_store (clipboard, NULL, 0);
769 gtk_target_list_unref (list);
773 pixbuf_get_func (GtkClipboard *clipboard,
774 GtkSelectionData *selection_data,
778 gtk_selection_data_set_pixbuf (selection_data, data);
782 pixbuf_clear_func (GtkClipboard *clipboard,
785 g_object_unref (data);
789 * gtk_clipboard_set_image:
790 * @clipboard: a #GtkClipboard object
791 * @pixbuf: a #GdkPixbuf
793 * Sets the contents of the clipboard to the given #GdkPixbuf.
794 * GTK+ will take responsibility for responding for requests
795 * for the image, and for converting the image into the
801 gtk_clipboard_set_image (GtkClipboard *clipboard,
806 GtkTargetEntry *targets;
809 g_return_if_fail (clipboard != NULL);
810 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
812 list = gtk_target_list_new (NULL, 0);
813 gtk_target_list_add_image_targets (list, 0, TRUE);
815 n_targets = g_list_length (list->list);
816 targets = g_new0 (GtkTargetEntry, n_targets);
817 for (l = list->list, i = 0; l; l = l->next, i++)
819 GtkTargetPair *pair = (GtkTargetPair *)l->data;
820 targets[i].target = gdk_atom_name (pair->target);
823 gtk_clipboard_set_with_data (clipboard,
825 pixbuf_get_func, pixbuf_clear_func,
826 g_object_ref (pixbuf));
827 gtk_clipboard_set_can_store (clipboard, NULL, 0);
830 gtk_target_list_unref (list);
834 set_request_contents_info (GtkWidget *widget,
835 RequestContentsInfo *info)
837 if (!request_contents_key_id)
838 request_contents_key_id = g_quark_from_static_string (request_contents_key);
840 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
843 static RequestContentsInfo *
844 get_request_contents_info (GtkWidget *widget)
846 if (!request_contents_key_id)
849 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
853 selection_received (GtkWidget *widget,
854 GtkSelectionData *selection_data,
857 RequestContentsInfo *request_info = get_request_contents_info (widget);
858 set_request_contents_info (widget, NULL);
860 request_info->callback (gtk_widget_get_clipboard (widget, selection_data->selection),
862 request_info->user_data);
864 g_free (request_info);
866 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
867 gtk_widget_destroy (widget);
871 * gtk_clipboard_request_contents:
872 * @clipboard: a #GtkClipboard
873 * @target: an atom representing the form into which the clipboard
874 * owner should convert the selection.
875 * @callback: A function to call when the results are received
876 * (or the retrieval fails). If the retrieval fails
877 * the length field of @selection_data will be
879 * @user_data: user data to pass to @callback
881 * Requests the contents of clipboard as the given target.
882 * When the results of the result are later received the supplied callback
886 gtk_clipboard_request_contents (GtkClipboard *clipboard,
888 GtkClipboardReceivedFunc callback,
891 RequestContentsInfo *info;
893 GtkWidget *clipboard_widget;
895 g_return_if_fail (clipboard != NULL);
896 g_return_if_fail (target != GDK_NONE);
897 g_return_if_fail (callback != NULL);
899 clipboard_widget = get_clipboard_widget (clipboard->display);
901 if (get_request_contents_info (clipboard_widget))
902 widget = make_clipboard_widget (clipboard->display, FALSE);
904 widget = clipboard_widget;
906 info = g_new (RequestContentsInfo, 1);
907 info->callback = callback;
908 info->user_data = user_data;
910 set_request_contents_info (widget, info);
912 gtk_selection_convert (widget, clipboard->selection, target,
913 clipboard_get_timestamp (clipboard));
917 request_text_received_func (GtkClipboard *clipboard,
918 GtkSelectionData *selection_data,
921 RequestTextInfo *info = data;
922 gchar *result = NULL;
924 result = gtk_selection_data_get_text (selection_data);
928 /* If we asked for UTF8 and didn't get it, try compound_text;
929 * if we asked for compound_text and didn't get it, try string;
930 * If we asked for anything else and didn't get it, give up.
932 if (selection_data->target == gdk_atom_intern ("UTF8_STRING", FALSE))
934 gtk_clipboard_request_contents (clipboard,
935 gdk_atom_intern ("COMPOUND_TEXT", FALSE),
936 request_text_received_func, info);
939 else if (selection_data->target == gdk_atom_intern ("COMPOUND_TEXT", FALSE))
941 gtk_clipboard_request_contents (clipboard,
943 request_text_received_func, info);
948 info->callback (clipboard, result, info->user_data);
954 * gtk_clipboard_request_text:
955 * @clipboard: a #GtkClipboard
956 * @callback: a function to call when the text is received,
957 * or the retrieval fails. (It will always be called
958 * one way or the other.)
959 * @user_data: user data to pass to @callback.
961 * Requests the contents of the clipboard as text. When the text is
962 * later received, it will be converted to UTF-8 if necessary, and
963 * @callback will be called.
965 * The @text parameter to @callback will contain the resulting text if
966 * the request succeeded, or %NULL if it failed. This could happen for
967 * various reasons, in particular if the clipboard was empty or if the
968 * contents of the clipboard could not be converted into text form.
971 gtk_clipboard_request_text (GtkClipboard *clipboard,
972 GtkClipboardTextReceivedFunc callback,
975 RequestTextInfo *info;
977 g_return_if_fail (clipboard != NULL);
978 g_return_if_fail (callback != NULL);
980 info = g_new (RequestTextInfo, 1);
981 info->callback = callback;
982 info->user_data = user_data;
984 gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("UTF8_STRING", FALSE),
985 request_text_received_func,
990 request_image_received_func (GtkClipboard *clipboard,
991 GtkSelectionData *selection_data,
994 RequestImageInfo *info = data;
995 GdkPixbuf *result = NULL;
997 result = gtk_selection_data_get_pixbuf (selection_data);
1001 /* If we asked for image/png and didn't get it, try image/jpeg;
1002 * if we asked for image/jpeg and didn't get it, try image/gif;
1003 * If we asked for anything else and didn't get it, give up.
1005 if (selection_data->target == gdk_atom_intern ("image/png", FALSE))
1007 gtk_clipboard_request_contents (clipboard,
1008 gdk_atom_intern ("image/jpeg", FALSE),
1009 request_image_received_func, info);
1012 else if (selection_data->target == gdk_atom_intern ("image/jpeg", FALSE))
1014 gtk_clipboard_request_contents (clipboard,
1015 gdk_atom_intern ("image/gif", FALSE),
1016 request_text_received_func, info);
1021 info->callback (clipboard, result, info->user_data);
1023 g_object_unref (result);
1027 * gtk_clipboard_request_image:
1028 * @clipboard: a #GtkClipboard
1029 * @callback: a function to call when the image is received,
1030 * or the retrieval fails. (It will always be called
1031 * one way or the other.)
1032 * @user_data: user data to pass to @callback.
1034 * Requests the contents of the clipboard as image. When the image is
1035 * later received, it will be converted to a #GdkPixbuf, and
1036 * @callback will be called.
1038 * The @pixbuf parameter to @callback will contain the resulting
1039 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1040 * could happen for various reasons, in particular if the clipboard
1041 * was empty or if the contents of the clipboard could not be
1042 * converted into an image.
1047 gtk_clipboard_request_image (GtkClipboard *clipboard,
1048 GtkClipboardImageReceivedFunc callback,
1051 RequestImageInfo *info;
1053 g_return_if_fail (clipboard != NULL);
1054 g_return_if_fail (callback != NULL);
1056 info = g_new (RequestImageInfo, 1);
1057 info->callback = callback;
1058 info->user_data = user_data;
1060 gtk_clipboard_request_contents (clipboard,
1061 gdk_atom_intern ("image/png", FALSE),
1062 request_image_received_func,
1067 request_targets_received_func (GtkClipboard *clipboard,
1068 GtkSelectionData *selection_data,
1071 RequestTargetsInfo *info = data;
1072 GdkAtom *targets = NULL;
1075 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1077 info->callback (clipboard, targets, n_targets, info->user_data);
1084 * gtk_clipboard_request_targets:
1085 * @clipboard: a #GtkClipboard
1086 * @callback: a function to call when the targets are received,
1087 * or the retrieval fails. (It will always be called
1088 * one way or the other.)
1089 * @user_data: user data to pass to @callback.
1091 * Requests the contents of the clipboard as list of supported targets.
1092 * When the list is later received, @callback will be called.
1094 * The @targets parameter to @callback will contain the resulting targets if
1095 * the request succeeded, or %NULL if it failed.
1100 gtk_clipboard_request_targets (GtkClipboard *clipboard,
1101 GtkClipboardTargetsReceivedFunc callback,
1104 RequestTargetsInfo *info;
1106 g_return_if_fail (clipboard != NULL);
1107 g_return_if_fail (callback != NULL);
1109 /* If the display supports change notification we cache targets */
1110 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1111 clipboard->n_cached_targets != -1)
1113 (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1117 info = g_new (RequestTargetsInfo, 1);
1118 info->callback = callback;
1119 info->user_data = user_data;
1121 gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE),
1122 request_targets_received_func,
1133 clipboard_received_func (GtkClipboard *clipboard,
1134 GtkSelectionData *selection_data,
1137 WaitResults *results = data;
1139 if (selection_data->length >= 0)
1140 results->data = gtk_selection_data_copy (selection_data);
1142 g_main_loop_quit (results->loop);
1146 * gtk_clipboard_wait_for_contents:
1147 * @clipboard: a #GtkClipboard
1148 * @target: an atom representing the form into which the clipboard
1149 * owner should convert the selection.
1151 * Requests the contents of the clipboard using the given target.
1152 * This function waits for the data to be received using the main
1153 * loop, so events, timeouts, etc, may be dispatched during the wait.
1155 * Return value: a newly-allocated #GtkSelectionData object or %NULL
1156 * if retrieving the given target failed. If non-%NULL,
1157 * this value must be freed with gtk_selection_data_free()
1158 * when you are finished with it.
1161 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1164 WaitResults results;
1166 g_return_val_if_fail (clipboard != NULL, NULL);
1167 g_return_val_if_fail (target != GDK_NONE, NULL);
1169 results.data = NULL;
1170 results.loop = g_main_loop_new (NULL, TRUE);
1172 gtk_clipboard_request_contents (clipboard, target,
1173 clipboard_received_func,
1176 if (g_main_loop_is_running (results.loop))
1178 GDK_THREADS_LEAVE ();
1179 g_main_loop_run (results.loop);
1180 GDK_THREADS_ENTER ();
1183 g_main_loop_unref (results.loop);
1185 return results.data;
1189 clipboard_text_received_func (GtkClipboard *clipboard,
1193 WaitResults *results = data;
1195 results->data = g_strdup (text);
1196 g_main_loop_quit (results->loop);
1200 * gtk_clipboard_wait_for_text:
1201 * @clipboard: a #GtkClipboard
1203 * Requests the contents of the clipboard as text and converts
1204 * the result to UTF-8 if necessary. This function waits for
1205 * the data to be received using the main loop, so events,
1206 * timeouts, etc, may be dispatched during the wait.
1208 * Return value: a newly-allocated UTF-8 string which must
1209 * be freed with g_free(), or %NULL if retrieving
1210 * the selection data failed. (This could happen
1211 * for various reasons, in particular if the
1212 * clipboard was empty or if the contents of the
1213 * clipboard could not be converted into text form.)
1216 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1218 WaitResults results;
1220 g_return_val_if_fail (clipboard != NULL, NULL);
1222 results.data = NULL;
1223 results.loop = g_main_loop_new (NULL, TRUE);
1225 gtk_clipboard_request_text (clipboard,
1226 clipboard_text_received_func,
1229 if (g_main_loop_is_running (results.loop))
1231 GDK_THREADS_LEAVE ();
1232 g_main_loop_run (results.loop);
1233 GDK_THREADS_ENTER ();
1236 g_main_loop_unref (results.loop);
1238 return results.data;
1242 clipboard_image_received_func (GtkClipboard *clipboard,
1246 WaitResults *results = data;
1249 results->data = g_object_ref (pixbuf);
1251 g_main_loop_quit (results->loop);
1255 * gtk_clipboard_wait_for_image:
1256 * @clipboard: a #GtkClipboard
1258 * Requests the contents of the clipboard as image and converts
1259 * the result to a #GdkPixbuf. This function waits for
1260 * the data to be received using the main loop, so events,
1261 * timeouts, etc, may be dispatched during the wait.
1263 * Return value: a newly-allocated #GdkPixbuf object which must
1264 * be disposed with g_object_unref(), or %NULL if
1265 * retrieving the selection data failed. (This
1266 * could happen for various reasons, in particular
1267 * if the clipboard was empty or if the contents of
1268 * the clipboard could not be converted into an image.)
1273 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1275 WaitResults results;
1277 g_return_val_if_fail (clipboard != NULL, NULL);
1279 results.data = NULL;
1280 results.loop = g_main_loop_new (NULL, TRUE);
1282 gtk_clipboard_request_image (clipboard,
1283 clipboard_image_received_func,
1286 if (g_main_loop_is_running (results.loop))
1288 GDK_THREADS_LEAVE ();
1289 g_main_loop_run (results.loop);
1290 GDK_THREADS_ENTER ();
1293 g_main_loop_unref (results.loop);
1295 return results.data;
1299 * gtk_clipboard_get_display:
1300 * @clipboard: a #GtkClipboard
1302 * Gets the #GdkDisplay associated with @clipboard
1304 * Return value: the #GdkDisplay associated with @clipboard
1309 gtk_clipboard_get_display (GtkClipboard *clipboard)
1311 g_return_val_if_fail (clipboard != NULL, NULL);
1313 return clipboard->display;
1317 * gtk_clipboard_wait_is_text_available:
1318 * @clipboard: a #GtkClipboard
1320 * Test to see if there is text available to be pasted
1321 * This is done by requesting the TARGETS atom and checking
1322 * if it contains any of the supported text targets. This function
1323 * waits for the data to be received using the main loop, so events,
1324 * timeouts, etc, may be dispatched during the wait.
1326 * This function is a little faster than calling
1327 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1330 * Return value: %TRUE is there is text available, %FALSE otherwise.
1333 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1335 GtkSelectionData *data;
1336 gboolean result = FALSE;
1338 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1341 result = gtk_selection_data_targets_include_text (data);
1342 gtk_selection_data_free (data);
1349 * gtk_clipboard_wait_is_image_available:
1350 * @clipboard: a #GtkClipboard
1352 * Test to see if there is an image available to be pasted
1353 * This is done by requesting the TARGETS atom and checking
1354 * if it contains any of the supported image targets. This function
1355 * waits for the data to be received using the main loop, so events,
1356 * timeouts, etc, may be dispatched during the wait.
1358 * This function is a little faster than calling
1359 * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1360 * the actual image data.
1362 * Return value: %TRUE is there is an image available, %FALSE otherwise.
1367 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1369 GtkSelectionData *data;
1370 gboolean result = FALSE;
1372 data = gtk_clipboard_wait_for_contents (clipboard,
1373 gdk_atom_intern ("TARGETS", FALSE));
1376 result = gtk_selection_data_targets_include_image (data, FALSE);
1377 gtk_selection_data_free (data);
1384 * gtk_clipboard_wait_for_targets
1385 * @clipboard: a #GtkClipboard
1386 * @targets: location to store an array of targets. The result
1387 * stored here must be freed with g_free().
1388 * @n_targets: location to store number of items in @targets.
1390 * Returns a list of targets that are present on the clipboard, or %NULL
1391 * if there aren't any targets available. The returned list must be
1392 * freed with g_free().
1393 * This function waits for the data to be received using the main
1394 * loop, so events, timeouts, etc, may be dispatched during the wait.
1396 * Return value: %TRUE if any targets are present on the clipboard,
1402 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1406 GtkSelectionData *data;
1407 gboolean result = FALSE;
1409 g_return_val_if_fail (clipboard != NULL, FALSE);
1411 /* If the display supports change notification we cache targets */
1412 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1413 clipboard->n_cached_targets != -1)
1416 *n_targets = clipboard->n_cached_targets;
1419 *targets = g_memdup (clipboard->cached_targets,
1420 clipboard->n_cached_targets * sizeof (GdkAtom));
1431 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1435 GdkAtom *tmp_targets;
1438 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1440 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1442 clipboard->n_cached_targets = tmp_n_targets;
1443 clipboard->cached_targets = g_memdup (tmp_targets,
1444 tmp_n_targets * sizeof (GdkAtom));
1448 *n_targets = tmp_n_targets;
1451 *targets = tmp_targets;
1453 g_free (tmp_targets);
1455 gtk_selection_data_free (data);
1461 static GtkClipboard *
1462 clipboard_peek (GdkDisplay *display,
1464 gboolean only_if_exists)
1466 GtkClipboard *clipboard = NULL;
1470 if (selection == GDK_NONE)
1471 selection = GDK_SELECTION_CLIPBOARD;
1473 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1475 tmp_list = clipboards;
1478 clipboard = tmp_list->data;
1479 if (clipboard->selection == selection)
1482 tmp_list = tmp_list->next;
1485 if (!tmp_list && !only_if_exists)
1487 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1488 clipboard->selection = selection;
1489 clipboard->display = display;
1490 clipboard->n_cached_targets = -1;
1491 clipboard->n_storable_targets = -1;
1492 clipboards = g_slist_prepend (clipboards, clipboard);
1493 g_object_set_data (G_OBJECT (display), "gtk-clipboard-list", clipboards);
1494 g_signal_connect (display, "closed",
1495 G_CALLBACK (clipboard_display_closed), clipboard);
1496 gdk_display_request_selection_notification (display, selection);
1503 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1504 GdkEventOwnerChange *event)
1506 if (clipboard->n_cached_targets != -1)
1508 clipboard->n_cached_targets = -1;
1509 g_free (clipboard->cached_targets);
1514 * gtk_clipboard_wait_is_target_available:
1515 * @clipboard: a #GtkClipboard
1516 * @target: A #GdkAtom indicating which target to look for.
1518 * Checks if a clipboard supports pasting data of a given type. This
1519 * function can be used to determine if a "Paste" menu item should be
1520 * insensitive or not.
1522 * If you want to see if there's text available on the clipboard, use
1523 * gtk_clipboard_wait_is_text_available () instead.
1525 * Return value: %TRUE if the target is available, %FALSE otherwise.
1530 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1535 gboolean retval = FALSE;
1537 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1540 for (i = 0; i < n_targets; i++)
1542 if (targets[i] == target)
1555 * _gtk_clipboard_handle_event:
1556 * @event: a owner change event
1558 * Emits the ::owner_change signal on the appropriate @clipboard.
1563 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1565 GdkDisplay *display;
1566 GtkClipboard *clipboard;
1568 display = gdk_drawable_get_display (event->window);
1569 clipboard = clipboard_peek (display, event->selection, TRUE);
1572 g_signal_emit (clipboard,
1573 clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1577 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1579 g_main_loop_quit (clipboard->store_loop);
1585 * gtk_clipboard_set_can_store:
1586 * @clipboard: a #GtkClipboard
1587 * @targets: array containing information about which forms should be stored
1588 * or %NULL to indicate that all forms should be stored.
1589 * @n_targets: number of elements in @targets
1591 * Hints that the clipboard data should be stored somewhere when the
1592 * application exits or when gtk_clipboard_store () is called.
1594 * This value is reset when the clipboard owner changes.
1595 * Where the clipboard data is stored is platform dependent,
1596 * see gdk_display_store_clipboard () for more information.
1601 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1602 const GtkTargetEntry *targets,
1605 GtkWidget *clipboard_widget;
1607 static const GtkTargetEntry save_targets[] = {
1608 { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1611 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1612 g_return_if_fail (n_targets >= 0);
1614 if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1617 g_free (clipboard->storable_targets);
1619 clipboard_widget = get_clipboard_widget (clipboard->display);
1621 /* n_storable_targets being -1 means that
1622 * gtk_clipboard_set_can_store hasn't been called since the
1623 * clipboard owner changed. We only want to add SAVE_TARGETS and
1624 * ref the owner once , so we do that here
1626 if (clipboard->n_storable_targets == -1)
1628 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1631 /* Ref the owner so it won't go away */
1632 if (clipboard->have_owner)
1633 g_object_ref (clipboard->user_data);
1636 clipboard->n_storable_targets = n_targets;
1637 clipboard->storable_targets = g_new (GdkAtom, n_targets);
1638 for (i = 0; i < n_targets; i++)
1639 clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1643 gtk_clipboard_selection_notify (GtkWidget *widget,
1644 GdkEventSelection *event,
1645 GtkClipboard *clipboard)
1647 if (event->selection == gdk_atom_intern ("CLIPBOARD_MANAGER", FALSE) &&
1648 clipboard->storing_selection)
1649 g_main_loop_quit (clipboard->store_loop);
1655 * gtk_clipboard_store:
1656 * @clipboard: a #GtkClipboard
1658 * Stores the current clipboard data somewhere so that it will stay
1659 * around after the application has quit.
1664 gtk_clipboard_store (GtkClipboard *clipboard)
1666 GtkWidget *clipboard_widget;
1668 g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1670 if (clipboard->n_storable_targets < 0)
1673 if (!gdk_display_supports_clipboard_persistence (clipboard->display))
1676 clipboard_widget = get_clipboard_widget (clipboard->display);
1677 clipboard->notify_signal_id = g_signal_connect (clipboard_widget, "selection_notify_event",
1678 G_CALLBACK (gtk_clipboard_selection_notify), clipboard);
1680 gdk_display_store_clipboard (clipboard->display,
1681 clipboard_widget->window,
1682 clipboard_get_timestamp (clipboard),
1683 clipboard->storable_targets,
1684 clipboard->n_storable_targets);
1686 clipboard->storing_selection = TRUE;
1688 clipboard->store_loop = g_main_loop_new (NULL, TRUE);
1689 clipboard->store_timeout = g_timeout_add (10000, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
1691 if (g_main_loop_is_running (clipboard->store_loop))
1693 GDK_THREADS_LEAVE ();
1694 g_main_loop_run (clipboard->store_loop);
1695 GDK_THREADS_ENTER ();
1698 g_main_loop_unref (clipboard->store_loop);
1699 clipboard->store_loop = NULL;
1701 g_source_remove (clipboard->store_timeout);
1702 clipboard->store_timeout = 0;
1703 g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
1704 clipboard->notify_signal_id = 0;
1706 clipboard->storing_selection = FALSE;
1709 /* Stores all clipboard selections on all displays, called from
1713 _gtk_clipboard_store_all (void)
1715 GtkClipboard *clipboard;
1716 GSList *displays, *list;
1718 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
1723 GdkDisplay *display = list->data;
1725 clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
1728 gtk_clipboard_store (clipboard);
1732 g_slist_free (displays);
1736 #define __GTK_CLIPBOARD_C__
1737 #include "gtkaliasdef.c"