1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Global clipboard abstraction.
24 #include "gtkclipboard.h"
25 #include "gtkinvisible.h"
28 #ifdef GDK_WINDOWING_X11
32 #ifdef GDK_WINDOWING_WIN32
33 #include "win32/gdkwin32.h"
36 typedef struct _GtkClipboardClass GtkClipboardClass;
38 typedef struct _RequestContentsInfo RequestContentsInfo;
39 typedef struct _RequestTextInfo RequestTextInfo;
40 typedef struct _RequestTargetsInfo RequestTargetsInfo;
44 GObject parent_instance;
48 GtkClipboardGetFunc get_func;
49 GtkClipboardClearFunc clear_func;
55 gboolean have_selection;
59 struct _GtkClipboardClass
61 GObjectClass parent_class;
64 struct _RequestContentsInfo
66 GtkClipboardReceivedFunc callback;
70 struct _RequestTextInfo
72 GtkClipboardTextReceivedFunc callback;
76 struct _RequestTargetsInfo
78 GtkClipboardTargetsReceivedFunc callback;
82 static void gtk_clipboard_class_init (GtkClipboardClass *class);
83 static void gtk_clipboard_finalize (GObject *object);
85 static void clipboard_unset (GtkClipboard *clipboard);
86 static void selection_received (GtkWidget *widget,
87 GtkSelectionData *selection_data,
97 static const gchar request_contents_key[] = "gtk-request-contents";
98 static GQuark request_contents_key_id = 0;
100 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
101 static GQuark clipboards_owned_key_id = 0;
103 static GObjectClass *parent_class;
106 gtk_clipboard_get_type (void)
108 static GType clipboard_type = 0;
112 static const GTypeInfo clipboard_info =
114 sizeof (GtkClipboardClass),
115 NULL, /* base_init */
116 NULL, /* base_finalize */
117 (GClassInitFunc) gtk_clipboard_class_init,
118 NULL, /* class_finalize */
119 NULL, /* class_data */
120 sizeof (GtkClipboard),
122 (GInstanceInitFunc) NULL,
125 clipboard_type = g_type_register_static (G_TYPE_OBJECT, "GtkClipboard",
129 return clipboard_type;
133 gtk_clipboard_class_init (GtkClipboardClass *class)
135 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
137 parent_class = g_type_class_peek_parent (class);
139 gobject_class->finalize = gtk_clipboard_finalize;
143 gtk_clipboard_finalize (GObject *object)
145 clipboard_unset (GTK_CLIPBOARD (object));
149 clipboard_display_closed (GdkDisplay *display,
151 GtkClipboard *clipboard)
155 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
156 g_object_run_dispose (G_OBJECT (clipboard));
157 g_object_unref (clipboard);
158 clipboards = g_slist_remove (clipboards, clipboard);
160 g_object_set_data (G_OBJECT (display), "gtk-clipboard-list", clipboards);
164 * gtk_clipboard_get_for_display:
165 * @display: the display for which the clipboard is to be retrieved or created
166 * @selection: a #GdkAtom which identifies the clipboard
169 * Returns the clipboard object for the given selection.
170 * Cut/copy/paste menu items and keyboard shortcuts should use
171 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
172 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
173 * for backwards compatibility reasons.)
174 * The currently-selected object or text should be provided on the clipboard
175 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
176 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
177 * to the default clipboard, i.e. they copy the selection to what the
178 * user sees as the clipboard.
180 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
181 * ("CLIPBOARD", FALSE)</literal>. See <ulink
182 * url="http://www.freedesktop.org/standards/clipboards-spec/clipboards.txt">
183 * http://www.freedesktop.org/standards/clipboards-spec/clipboards.txt</ulink>
184 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
185 * selections under the X window system. On Win32 the
186 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
188 * It's possible to have arbitrary named clipboards; if you do invent
189 * new clipboards, you should prefix the selection name with an
190 * underscore (because the ICCCM requires that nonstandard atoms are
191 * underscore-prefixed), and namespace it as well. For example,
192 * if your application called "Foo" has a special-purpose
193 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
195 * Return value: the appropriate clipboard object. If no
196 * clipboard already exists, a new one will
197 * be created. Once a clipboard object has
198 * been created, it is persistent for all time and
204 gtk_clipboard_get_for_display (GdkDisplay *display, GdkAtom selection)
206 GtkClipboard *clipboard = NULL;
210 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
211 g_return_val_if_fail (!display->closed, NULL);
213 if (selection == GDK_NONE)
214 selection = GDK_SELECTION_CLIPBOARD;
216 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
218 tmp_list = clipboards;
221 clipboard = tmp_list->data;
222 if (clipboard->selection == selection)
225 tmp_list = tmp_list->next;
230 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
231 clipboard->selection = selection;
232 clipboard->display = display;
233 clipboards = g_slist_prepend (clipboards, clipboard);
234 g_object_set_data (G_OBJECT (display), "gtk-clipboard-list", clipboards);
235 g_signal_connect (display, "closed",
236 G_CALLBACK (clipboard_display_closed), clipboard);
243 * gtk_clipboard_get():
244 * @selection: a #GdkAtom which identifies the clipboard
247 * Returns the clipboard object for the given selection.
248 * See gtk_clipboard_get_for_display() for complete details.
250 * Return value: the appropriate clipboard object. If no
251 * clipboard already exists, a new one will
252 * be created. Once a clipboard object has
253 * been created, it is persistent for all time and
257 gtk_clipboard_get (GdkAtom selection)
259 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
263 selection_get_cb (GtkWidget *widget,
264 GtkSelectionData *selection_data,
268 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, selection_data->selection);
270 if (clipboard && clipboard->get_func)
271 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
275 selection_clear_event_cb (GtkWidget *widget,
276 GdkEventSelection *event)
278 GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
282 clipboard_unset (clipboard);
290 make_clipboard_widget (GdkDisplay *display,
293 GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
295 g_signal_connect (widget, "selection_received",
296 G_CALLBACK (selection_received), NULL);
300 /* We need this for gdk_x11_get_server_time() */
301 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
303 g_signal_connect (widget, "selection_get",
304 G_CALLBACK (selection_get_cb), NULL);
305 g_signal_connect (widget, "selection_clear_event",
306 G_CALLBACK (selection_clear_event_cb), NULL);
313 get_clipboard_widget (GdkDisplay *display)
315 GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
318 clip_widget = make_clipboard_widget (display, TRUE);
319 g_object_set_data (G_OBJECT (display), "gtk-clipboard-widget", clip_widget);
325 /* This function makes a very good guess at what the correct
326 * timestamp for a selection request should be. If there is
327 * a currently processed event, it uses the timestamp for that
328 * event, otherwise it uses the current server time. However,
329 * if the time resulting from that is older than the time used
330 * last time, it uses the time used last time instead.
332 * In order implement this correctly, we never use CurrentTime,
333 * but actually retrieve the actual timestamp from the server.
334 * This is a little slower but allows us to make the guarantee
335 * that the times used by this application will always ascend
336 * and we won't get selections being rejected just because
337 * we are using a correct timestamp from an event, but used
338 * CurrentTime previously.
341 clipboard_get_timestamp (GtkClipboard *clipboard)
343 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
344 guint32 timestamp = gtk_get_current_event_time ();
346 if (timestamp == GDK_CURRENT_TIME)
348 #ifdef GDK_WINDOWING_X11
349 timestamp = gdk_x11_get_server_time (clipboard_widget->window);
350 #elif defined GDK_WINDOWING_WIN32
351 timestamp = GetMessageTime ();
356 if (clipboard->timestamp != GDK_CURRENT_TIME)
358 /* Check to see if clipboard->timestamp is newer than
359 * timestamp, accounting for wraparound.
362 guint32 max = timestamp + 0x80000000;
364 if ((max > timestamp &&
365 (clipboard->timestamp > timestamp &&
366 clipboard->timestamp <= max)) ||
368 (clipboard->timestamp > timestamp ||
369 clipboard->timestamp <= max)))
371 timestamp = clipboard->timestamp;
376 clipboard->timestamp = timestamp;
382 clipboard_owner_destroyed (gpointer data)
384 GSList *clipboards = data;
387 tmp_list = clipboards;
390 GtkClipboard *clipboard = tmp_list->data;
392 clipboard->get_func = NULL;
393 clipboard->clear_func = NULL;
394 clipboard->user_data = NULL;
395 clipboard->have_owner = FALSE;
397 gtk_clipboard_clear (clipboard);
399 tmp_list = tmp_list->next;
402 g_slist_free (clipboards);
406 clipboard_add_owner_notify (GtkClipboard *clipboard)
408 if (!clipboards_owned_key_id)
409 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
411 if (clipboard->have_owner)
412 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
413 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
414 clipboards_owned_key_id),
416 clipboard_owner_destroyed);
420 clipboard_remove_owner_notify (GtkClipboard *clipboard)
422 if (clipboard->have_owner)
423 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
424 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
425 clipboards_owned_key_id),
427 clipboard_owner_destroyed);
431 gtk_clipboard_set_contents (GtkClipboard *clipboard,
432 const GtkTargetEntry *targets,
434 GtkClipboardGetFunc get_func,
435 GtkClipboardClearFunc clear_func,
439 GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
441 if (gtk_selection_owner_set_for_display (clipboard->display,
443 clipboard->selection,
444 clipboard_get_timestamp (clipboard)))
446 clipboard->have_selection = TRUE;
448 if (!(clipboard->have_owner && have_owner) ||
449 clipboard->user_data != user_data)
451 clipboard_unset (clipboard);
453 if (clipboard->get_func)
455 /* Calling unset() caused the clipboard contents to be reset!
456 * Avoid leaking and return
458 if (!(clipboard->have_owner && have_owner) ||
459 clipboard->user_data != user_data)
461 (*clear_func) (clipboard, user_data);
469 clipboard->user_data = user_data;
470 clipboard->have_owner = have_owner;
472 clipboard_add_owner_notify (clipboard);
477 clipboard->get_func = get_func;
478 clipboard->clear_func = clear_func;
480 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
481 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
491 * gtk_clipboard_set_with_data:
492 * @clipboard: a #GtkClipboard
493 * @targets: array containing information about the available forms for the
495 * @n_targets: number of elements in @targets
496 * @get_func: function to call to get the actual clipboard data
497 * @clear_func: when the clipboard contents are set again, this function will
498 * be called, and @get_func will not be subsequently called.
499 * @user_data: user data to pass to @get_func and @clear_func.
501 * Virtually sets the contents of the specified clipboard by providing
502 * a list of supported formats for the clipboard data and a function
503 * to call to get the actual data when it is requested.
505 * Return value: %TRUE if setting the clipboard data succeeded. If setting
506 * the clipboard data failed the provided callback functions
510 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
511 const GtkTargetEntry *targets,
513 GtkClipboardGetFunc get_func,
514 GtkClipboardClearFunc clear_func,
517 g_return_val_if_fail (clipboard != NULL, FALSE);
518 g_return_val_if_fail (targets != NULL, FALSE);
519 g_return_val_if_fail (get_func != NULL, FALSE);
521 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
522 get_func, clear_func, user_data,
527 * gtk_clipboard_set_with_owner:
528 * @clipboard: a #GtkClipboard
529 * @targets: array containing information about the available forms for the
531 * @n_targets: number of elements in @targets
532 * @get_func: function to call to get the actual clipboard data
533 * @clear_func: when the clipboard contents are set again, this function will
534 * be called, and @get_func will not be subsequently called.
535 * @owner: an object that "owns" the data. This object will be passed
536 * to the callbacks when called.
538 * Virtually sets the contents of the specified clipboard by providing
539 * a list of supported formats for the clipboard data and a function
540 * to call to get the actual data when it is requested.
542 * The difference between this function and gtk_clipboard_set_with_data()
543 * is that instead of an generic @user_data pointer, a #GObject is passed
546 * Return value: %TRUE if setting the clipboard data succeeded. If setting
547 * the clipboard data failed the provided callback functions
551 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
552 const GtkTargetEntry *targets,
554 GtkClipboardGetFunc get_func,
555 GtkClipboardClearFunc clear_func,
558 g_return_val_if_fail (clipboard != NULL, FALSE);
559 g_return_val_if_fail (targets != NULL, FALSE);
560 g_return_val_if_fail (get_func != NULL, FALSE);
561 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
563 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
564 get_func, clear_func, owner,
569 * gtk_clipboard_get_owner:
570 * @clipboard: a #GtkClipboard
572 * If the clipboard contents callbacks were set with
573 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
574 * gtk_clipboard_clear() has not subsequently called, returns the owner set
575 * by gtk_clipboard_set_with_owner().
577 * Return value: the owner of the clipboard, if any; otherwise %NULL.
580 gtk_clipboard_get_owner (GtkClipboard *clipboard)
582 g_return_val_if_fail (clipboard != NULL, NULL);
584 if (clipboard->have_owner)
585 return clipboard->user_data;
591 clipboard_unset (GtkClipboard *clipboard)
593 GtkClipboardClearFunc old_clear_func;
596 old_clear_func = clipboard->clear_func;
597 old_data = clipboard->user_data;
599 if (clipboard->have_owner)
601 clipboard_remove_owner_notify (clipboard);
602 clipboard->have_owner = FALSE;
605 clipboard->get_func = NULL;
606 clipboard->clear_func = NULL;
607 clipboard->user_data = NULL;
610 old_clear_func (clipboard, old_data);
614 * gtk_clipboard_clear:
615 * @clipboard: a #GtkClipboard
617 * Clears the contents of the clipboard. Generally this should only
618 * be called between the time you call gtk_clipboard_set_with_owner()
619 * or gtk_clipboard_set_with_data(),
620 * and when the @clear_func you supplied is called. Otherwise, the
621 * clipboard may be owned by someone else.
624 gtk_clipboard_clear (GtkClipboard *clipboard)
626 g_return_if_fail (clipboard != NULL);
628 if (clipboard->have_selection)
629 gtk_selection_owner_set_for_display (clipboard->display,
631 clipboard->selection,
632 clipboard_get_timestamp (clipboard));
636 text_get_func (GtkClipboard *clipboard,
637 GtkSelectionData *selection_data,
641 gtk_selection_data_set_text (selection_data, data, -1);
645 text_clear_func (GtkClipboard *clipboard,
652 * gtk_clipboard_set_text:
653 * @clipboard: a #GtkClipboard object
654 * @text: a UTF-8 string.
655 * @len: length of @text, in bytes, or -1, in which case
656 * the length will be determined with <function>strlen()</function>.
658 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
659 * make a copy of the text and take responsibility for responding
660 * for requests for the text, and for converting the text into
661 * the requested format.
664 gtk_clipboard_set_text (GtkClipboard *clipboard,
668 static const GtkTargetEntry targets[] = {
669 { "STRING", 0, TARGET_STRING },
670 { "TEXT", 0, TARGET_TEXT },
671 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
672 { "UTF8_STRING", 0, TARGET_UTF8_STRING }
675 g_return_if_fail (clipboard != NULL);
676 g_return_if_fail (text != NULL);
681 gtk_clipboard_set_with_data (clipboard,
682 targets, G_N_ELEMENTS (targets),
683 text_get_func, text_clear_func,
684 g_strndup (text, len));
688 set_request_contents_info (GtkWidget *widget,
689 RequestContentsInfo *info)
691 if (!request_contents_key_id)
692 request_contents_key_id = g_quark_from_static_string (request_contents_key);
694 g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
697 static RequestContentsInfo *
698 get_request_contents_info (GtkWidget *widget)
700 if (!request_contents_key_id)
703 return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
707 selection_received (GtkWidget *widget,
708 GtkSelectionData *selection_data,
711 RequestContentsInfo *request_info = get_request_contents_info (widget);
712 set_request_contents_info (widget, NULL);
714 request_info->callback (gtk_widget_get_clipboard (widget, selection_data->selection),
716 request_info->user_data);
718 g_free (request_info);
720 if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
721 gtk_widget_destroy (widget);
725 * gtk_clipboard_request_contents:
726 * @clipboard: a #GtkClipboard
727 * @target: an atom representing the form into which the clipboard
728 * owner should convert the selection.
729 * @callback: A function to call when the results are received
730 * (or the retrieval fails). If the retrieval fails
731 * the length field of @selection_data will be
733 * @user_data: user data to pass to @callback
735 * Requests the contents of clipboard as the given target.
736 * When the results of the result are later received the supplied callback
740 gtk_clipboard_request_contents (GtkClipboard *clipboard,
742 GtkClipboardReceivedFunc callback,
745 RequestContentsInfo *info;
747 GtkWidget *clipboard_widget;
749 g_return_if_fail (clipboard != NULL);
750 g_return_if_fail (target != GDK_NONE);
751 g_return_if_fail (callback != NULL);
753 clipboard_widget = get_clipboard_widget (clipboard->display);
755 if (get_request_contents_info (clipboard_widget))
756 widget = make_clipboard_widget (clipboard->display, FALSE);
758 widget = clipboard_widget;
760 info = g_new (RequestContentsInfo, 1);
761 info->callback = callback;
762 info->user_data = user_data;
764 set_request_contents_info (widget, info);
766 gtk_selection_convert (widget, clipboard->selection, target,
767 clipboard_get_timestamp (clipboard));
771 request_text_received_func (GtkClipboard *clipboard,
772 GtkSelectionData *selection_data,
775 RequestTextInfo *info = data;
776 gchar *result = NULL;
778 result = gtk_selection_data_get_text (selection_data);
782 /* If we asked for UTF8 and didn't get it, try compound_text;
783 * if we asked for compound_text and didn't get it, try string;
784 * If we asked for anything else and didn't get it, give up.
786 if (selection_data->target == gdk_atom_intern ("UTF8_STRING", FALSE))
788 gtk_clipboard_request_contents (clipboard,
789 gdk_atom_intern ("COMPOUND_TEXT", FALSE),
790 request_text_received_func, info);
793 else if (selection_data->target == gdk_atom_intern ("COMPOUND_TEXT", FALSE))
795 gtk_clipboard_request_contents (clipboard,
797 request_text_received_func, info);
802 info->callback (clipboard, result, info->user_data);
808 * gtk_clipboard_request_text:
809 * @clipboard: a #GtkClipboard
810 * @callback: a function to call when the text is received,
811 * or the retrieval fails. (It will always be called
812 * one way or the other.)
813 * @user_data: user data to pass to @callback.
815 * Requests the contents of the clipboard as text. When the text is
816 * later received, it will be converted to UTF-8 if necessary, and
817 * @callback will be called.
819 * The @text parameter to @callback will contain the resulting text if
820 * the request succeeded, or %NULL if it failed. This could happen for
821 * various reasons, in particular if the clipboard was empty or if the
822 * contents of the clipboard could not be converted into text form.
825 gtk_clipboard_request_text (GtkClipboard *clipboard,
826 GtkClipboardTextReceivedFunc callback,
829 RequestTextInfo *info;
831 g_return_if_fail (clipboard != NULL);
832 g_return_if_fail (callback != NULL);
834 info = g_new (RequestTextInfo, 1);
835 info->callback = callback;
836 info->user_data = user_data;
838 gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("UTF8_STRING", FALSE),
839 request_text_received_func,
844 request_targets_received_func (GtkClipboard *clipboard,
845 GtkSelectionData *selection_data,
848 RequestTargetsInfo *info = data;
849 GdkAtom *targets = NULL;
852 gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
854 info->callback (clipboard, targets, n_targets, info->user_data);
861 * gtk_clipboard_request_targets:
862 * @clipboard: a #GtkClipboard
863 * @callback: a function to call when the targets are received,
864 * or the retrieval fails. (It will always be called
865 * one way or the other.)
866 * @user_data: user data to pass to @callback.
868 * Requests the contents of the clipboard as list of supported targets.
869 * When the list is later received, @callback will be called.
871 * The @targets parameter to @callback will contain the resulting targets if
872 * the request succeeded, or %NULL if it failed.
877 gtk_clipboard_request_targets (GtkClipboard *clipboard,
878 GtkClipboardTargetsReceivedFunc callback,
881 RequestTargetsInfo *info;
883 g_return_if_fail (clipboard != NULL);
884 g_return_if_fail (callback != NULL);
886 info = g_new (RequestTargetsInfo, 1);
887 info->callback = callback;
888 info->user_data = user_data;
890 gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE),
891 request_targets_received_func,
902 clipboard_received_func (GtkClipboard *clipboard,
903 GtkSelectionData *selection_data,
906 WaitResults *results = data;
908 if (selection_data->length >= 0)
909 results->data = gtk_selection_data_copy (selection_data);
911 g_main_loop_quit (results->loop);
915 * gtk_clipboard_wait_for_contents:
916 * @clipboard: a #GtkClipboard
917 * @target: an atom representing the form into which the clipboard
918 * owner should convert the selection.
920 * Requests the contents of the clipboard using the given target.
921 * This function waits for the data to be received using the main
922 * loop, so events, timeouts, etc, may be dispatched during the wait.
924 * Return value: a newly-allocated #GtkSelectionData object or %NULL
925 * if retrieving the given target failed. If non-%NULL,
926 * this value must be freed with gtk_selection_data_free()
927 * when you are finished with it.
930 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
935 g_return_val_if_fail (clipboard != NULL, NULL);
936 g_return_val_if_fail (target != GDK_NONE, NULL);
939 results.loop = g_main_loop_new (NULL, TRUE);
941 gtk_clipboard_request_contents (clipboard, target,
942 clipboard_received_func,
945 if (g_main_loop_is_running (results.loop))
947 GDK_THREADS_LEAVE ();
948 g_main_loop_run (results.loop);
949 GDK_THREADS_ENTER ();
952 g_main_loop_unref (results.loop);
958 clipboard_text_received_func (GtkClipboard *clipboard,
962 WaitResults *results = data;
964 results->data = g_strdup (text);
965 g_main_loop_quit (results->loop);
970 * gtk_clipboard_wait_for_text:
971 * @clipboard: a #GtkClipboard
973 * Requests the contents of the clipboard as text and converts
974 * the result to UTF-8 if necessary. This function waits for
975 * the data to be received using the main loop, so events,
976 * timeouts, etc, may be dispatched during the wait.
978 * Return value: a newly-allocated UTF-8 string which must
979 * be freed with g_free(), or %NULL if retrieving
980 * the selection data failed. (This could happen
981 * for various reasons, in particular if the
982 * clipboard was empty or if the contents of the
983 * clipboard could not be converted into text form.)
986 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
990 g_return_val_if_fail (clipboard != NULL, NULL);
991 g_return_val_if_fail (clipboard != NULL, NULL);
994 results.loop = g_main_loop_new (NULL, TRUE);
996 gtk_clipboard_request_text (clipboard,
997 clipboard_text_received_func,
1000 if (g_main_loop_is_running (results.loop))
1002 GDK_THREADS_LEAVE ();
1003 g_main_loop_run (results.loop);
1004 GDK_THREADS_ENTER ();
1007 g_main_loop_unref (results.loop);
1009 return results.data;
1012 * gtk_clipboard_get_display:
1013 * @clipboard: a #GtkClipboard
1015 * Gets the #GdkDisplay associated with @clipboard
1017 * Return value: the #GdkDisplay associated with @clipboard
1022 gtk_clipboard_get_display (GtkClipboard *clipboard)
1024 g_return_val_if_fail (clipboard != NULL, NULL);
1026 return clipboard->display;
1030 * gtk_clipboard_wait_is_text_available:
1031 * @clipboard: a #GtkClipboard
1033 * Test to see if there is text available to be pasted
1034 * This is done by requesting the TARGETS atom and checking
1035 * if it contains any of the names: STRING, TEXT, COMPOUND_TEXT,
1036 * UTF8_STRING. This function waits for the data to be received
1037 * using the main loop, so events, timeouts, etc, may be dispatched
1040 * This function is a little faster than calling
1041 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1044 * Return value: %TRUE is there is text available, %FALSE otherwise.
1047 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1049 GtkSelectionData *data;
1050 gboolean result = FALSE;
1052 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1055 result = gtk_selection_data_targets_include_text (data);
1056 gtk_selection_data_free (data);
1063 * gtk_clipboard_wait_for_targets
1064 * @clipboard: a #GtkClipboard
1065 * @targets: location to store an array of targets. The result
1066 * stored here must be freed with g_free().
1067 * @n_targets: location to store number of items in @targets.
1069 * Returns a list of targets that are present on the clipboard, or %NULL
1070 * if there aren't any targets available. The returned list must be
1071 * freed with g_free().
1072 * This function waits for the data to be received using the main
1073 * loop, so events, timeouts, etc, may be dispatched during the wait.
1075 * Return value: %TRUE if any targets are present on the clipboard,
1081 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1085 GtkSelectionData *data;
1086 gboolean result = FALSE;
1088 g_return_val_if_fail (clipboard != NULL, FALSE);
1090 /* TODO: see http://bugzilla.gnome.org/show_bug.cgi?id=101774 with regard to XFIXES */
1097 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1101 result = gtk_selection_data_get_targets (data, targets, n_targets);
1102 gtk_selection_data_free (data);