]> Pileus Git - ~andy/gtk/blob - gtk/gtkclipboard.c
ae4357f96f900a4aad46092748a6e7f08a540312
[~andy/gtk] / gtk / gtkclipboard.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Global clipboard abstraction. 
20  */
21
22 #include <string.h>
23
24 #include "gtkclipboard.h"
25 #include "gtkinvisible.h"
26 #include "gtkmain.h"
27 #include "gtksignal.h"
28
29 #ifdef GDK_WINDOWING_X11
30 #include "x11/gdkx.h"
31 #endif
32
33 #ifdef GDK_WINDOWING_WIN32
34 #include "win32/gdkwin32.h"
35 #endif
36
37 typedef struct _RequestContentsInfo RequestContentsInfo;
38 typedef struct _RequestTextInfo RequestTextInfo;
39
40 struct _GtkClipboard 
41 {
42   GdkAtom selection;
43
44   GtkClipboardGetFunc get_func;
45   GtkClipboardClearFunc clear_func;
46   gpointer user_data;
47   gboolean have_owner;
48
49   guint32 timestamp;
50
51   gboolean have_selection;
52 };
53
54 struct _RequestContentsInfo
55 {
56   GtkClipboardReceivedFunc callback;
57   gpointer user_data;
58 };
59
60 struct _RequestTextInfo
61 {
62   GtkClipboardTextReceivedFunc callback;
63   gpointer user_data;
64 };
65
66 static void clipboard_unset    (GtkClipboard     *clipboard);
67 static void selection_received (GtkWidget        *widget,
68                                 GtkSelectionData *selection_data,
69                                 guint             time);
70
71 static GSList *clipboards;
72 static GtkWidget *clipboard_widget;
73
74 enum {
75   TARGET_STRING,
76   TARGET_TEXT,
77   TARGET_COMPOUND_TEXT,
78   TARGET_UTF8_STRING
79 };
80
81 static const gchar *request_contents_key = "gtk-request-contents";
82 static GQuark request_contents_key_id = 0;
83
84 static const gchar *clipboards_owned_key = "gtk-clipboards-owned";
85 static GQuark clipboards_owned_key_id = 0;
86   
87
88 /**
89  * gtk_clipboard_get:
90  * @selection: a #GdkAtom which identifies the clipboard
91  *             to use. A value of %GDK_NONE here is the
92  *             same as <literal>gdk_atom_intern ("CLIPBOARD", FALSE)</literal>,
93  *             and provides the default clipboard. Another
94  *             common value is %GDK_SELECTION_PRIMARY, which
95  *             identifies the primary X selection. 
96  * 
97  * Returns the clipboard object for the given selection.
98  * 
99  * Return value: the appropriate clipboard object. If no
100  *             clipboard already exists, a new one will
101  *             be created. Once a clipboard object has
102  *             been created, it is persistent for all time.
103  **/
104 GtkClipboard *
105 gtk_clipboard_get (GdkAtom selection)
106 {
107   GtkClipboard *clipboard = NULL;
108   GSList *tmp_list;
109
110   if (selection == GDK_NONE)
111     selection = gdk_atom_intern ("CLIPBOARD", FALSE);
112
113   tmp_list = clipboards;
114   while (tmp_list)
115     {
116       clipboard = tmp_list->data;
117       if (clipboard->selection == selection)
118         break;
119
120       tmp_list = tmp_list->next;
121     }
122
123   if (!tmp_list)
124     {
125       clipboard = g_new0 (GtkClipboard, 1);
126       clipboard->selection = selection;
127       clipboards = g_slist_prepend (clipboards, clipboard);
128     }
129   
130   return clipboard;
131 }
132
133 static void 
134 selection_get_cb (GtkWidget          *widget,
135                   GtkSelectionData   *selection_data,
136                   guint               time,
137                   guint               info)
138 {
139   GtkClipboard *clipboard = gtk_clipboard_get (selection_data->selection);
140
141   if (clipboard && clipboard->get_func)
142     clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
143 }
144
145 static gboolean
146 selection_clear_event_cb (GtkWidget         *widget,
147                           GdkEventSelection *event)
148 {
149   GtkClipboard *clipboard = gtk_clipboard_get (event->selection);
150
151   if (clipboard)
152     {
153       clipboard_unset (clipboard);
154       return TRUE;
155     }
156
157   return FALSE;
158 }
159
160 static GtkWidget *
161 make_clipboard_widget (gboolean provider)
162 {
163   GtkWidget *widget = gtk_invisible_new ();
164
165   gtk_signal_connect (GTK_OBJECT (widget), "selection_received",
166                       GTK_SIGNAL_FUNC (selection_received), NULL);
167
168   if (provider)
169     {
170       /* We need this for gdk_x11_get_server_time() */
171       gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
172       
173       gtk_signal_connect (GTK_OBJECT (widget), "selection_get",
174                           GTK_SIGNAL_FUNC (selection_get_cb), NULL);
175       gtk_signal_connect (GTK_OBJECT (widget), "selection_clear_event",
176                           GTK_SIGNAL_FUNC (selection_clear_event_cb), NULL);
177     }
178
179   return widget;
180 }
181
182
183 static void
184 ensure_clipboard_widget ()
185 {
186   if (!clipboard_widget)
187     clipboard_widget = make_clipboard_widget (TRUE);
188 }
189
190 /* This function makes a very good guess at what the correct
191  * timestamp for a selection request should be. If there is
192  * a currently processed event, it uses the timestamp for that
193  * event, otherwise it uses the current server time. However,
194  * if the time resulting from that is older than the time used
195  * last time, it uses the time used last time instead.
196  *
197  * In order implement this correctly, we never use CurrentTime,
198  * but actually retrieve the actual timestamp from the server.
199  * This is a little slower but allows us to make the guarantee
200  * that the times used by this application will always ascend
201  * and we won't get selections being rejected just because
202  * we are using a correct timestamp from an event, but used
203  * CurrentTime previously.
204  */
205 static guint32
206 clipboard_get_timestamp (GtkClipboard *clipboard)
207 {
208   guint32 timestamp = gtk_get_current_event_time ();
209
210   ensure_clipboard_widget ();
211   
212   if (timestamp == GDK_CURRENT_TIME)
213     {
214 #ifdef GDK_WINDOWING_X11
215       timestamp = gdk_x11_get_server_time (clipboard_widget->window);
216 #elif defined GDK_WINDOWING_WIN32
217       timestamp = GetMessageTime ();
218 #endif
219     }
220   else
221     {
222       if (clipboard->timestamp != GDK_CURRENT_TIME)
223         {
224           /* Check to see if clipboard->timestamp is newer than
225            * timestamp, accounting for wraparound.
226            */
227
228           guint32 max = timestamp + 0x80000000;
229
230           if ((max > timestamp &&
231                (clipboard->timestamp > timestamp &&
232                 clipboard->timestamp <= max)) ||
233               (max <= timestamp &&
234                (clipboard->timestamp > timestamp ||
235                 clipboard->timestamp <= max)))
236             {
237               timestamp = clipboard->timestamp;
238             }
239         }
240     }
241
242   clipboard->timestamp = timestamp;
243
244   return timestamp;
245 }
246
247 static void
248 clipboard_owner_destroyed (gpointer data)
249 {
250   GSList *clipboards = data;
251   GSList *tmp_list;
252
253   tmp_list = clipboards;
254   while (tmp_list)
255     {
256       GtkClipboard *clipboard = tmp_list->data;
257
258       clipboard->get_func = NULL;
259       clipboard->clear_func = NULL;
260       clipboard->user_data = NULL;
261       clipboard->have_owner = FALSE;
262
263       gtk_clipboard_clear (clipboard);
264
265       tmp_list = tmp_list->next;
266     }
267   
268   g_slist_free (clipboards);
269 }
270
271 static void
272 clipboard_add_owner_notify (GtkClipboard *clipboard)
273 {
274   if (!clipboards_owned_key_id)
275     clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
276   
277   if (clipboard->have_owner)
278     g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
279                              g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
280                                                                     clipboards_owned_key_id),
281                                               clipboard),
282                              clipboard_owner_destroyed);
283 }
284
285 static void
286 clipboard_remove_owner_notify (GtkClipboard *clipboard)
287 {
288   if (clipboard->have_owner)
289      g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
290                               g_slist_remove (g_object_steal_qdata (clipboard->user_data,
291                                                                     clipboards_owned_key_id),
292                                               clipboard),
293                               clipboard_owner_destroyed);
294 }
295           
296 static gboolean
297 gtk_clipboard_set_contents (GtkClipboard         *clipboard,
298                             const GtkTargetEntry *targets,
299                             guint                 n_targets,
300                             GtkClipboardGetFunc   get_func,
301                             GtkClipboardClearFunc clear_func,
302                             gpointer              user_data,
303                             gboolean              have_owner)
304 {
305   ensure_clipboard_widget ();
306
307   if (gtk_selection_owner_set (clipboard_widget, clipboard->selection,
308                                clipboard_get_timestamp (clipboard)))
309     {
310       clipboard->have_selection = TRUE;
311
312       if (!(clipboard->have_owner && have_owner) ||
313           clipboard->user_data != user_data)
314         {
315           clipboard_unset (clipboard);
316
317           if (clipboard->get_func)
318             {
319               /* Calling unset() caused the clipboard contents to be reset!
320                * Avoid leaking and return 
321                */
322               if (!(clipboard->have_owner && have_owner) ||
323                   clipboard->user_data != user_data)
324                 {
325                   (*clear_func) (clipboard, user_data);
326                   return FALSE;
327                 }
328               else
329                 return TRUE;
330             }
331           else
332             {
333               clipboard->user_data = user_data;
334               clipboard->have_owner = have_owner;
335               if (have_owner)
336                 clipboard_add_owner_notify (clipboard);
337             }
338           
339         }
340
341       clipboard->get_func = get_func;
342       clipboard->clear_func = clear_func;
343
344       gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
345       gtk_selection_add_targets (clipboard_widget, clipboard->selection,
346                                  targets, n_targets);
347
348       return TRUE;
349     }
350   else
351     return FALSE;
352 }
353
354 /**
355  * gtk_clipboard_set_with_data:
356  * @clipboard:  a #GtkClipboard
357  * @targets:    array containing information about the available forms for the
358  *              clipboard data
359  * @n_targets:  number of elements in @targets
360  * @get_func:   function to call to get the actual clipboard data
361  * @clear_func: when the clipboard contents are set again, this function will
362  *              be called, and @get_func will not be subsequently called.
363  * @user_data:  user data to pass to @get_func and @clear_func.
364  * 
365  * Virtually sets the contents of the specified clipboard by providing
366  * a list of supported formats for the clipboard data and a function
367  * to call to get the actual data when it is requested.
368  * 
369  * Return value: %TRUE if setting the clipboard data succeeded. If setting
370  *               the clipboard data failed the provided callback functions
371  *               will be ignored.
372  **/
373 gboolean
374 gtk_clipboard_set_with_data (GtkClipboard          *clipboard,
375                              const GtkTargetEntry  *targets,
376                              guint                  n_targets,
377                              GtkClipboardGetFunc    get_func,
378                              GtkClipboardClearFunc  clear_func,
379                              gpointer               user_data)
380 {
381   g_return_val_if_fail (clipboard != NULL, FALSE);
382   g_return_val_if_fail (targets != NULL, FALSE);
383   g_return_val_if_fail (get_func != NULL, FALSE);
384
385   return gtk_clipboard_set_contents (clipboard, targets, n_targets,
386                                      get_func, clear_func, user_data,
387                                      FALSE);
388 }
389
390 /**
391  * gtk_clipboard_set_with_owner:
392  * @clipboard:  a #GtkClipboard
393  * @targets:    array containing information about the available forms for the
394  *              clipboard data
395  * @n_targets:  number of elements in @targets
396  * @get_func:   function to call to get the actual clipboard data
397  * @clear_func: when the clipboard contents are set again, this function will
398  *              be called, and @get_func will not be subsequently called.
399  * @owner:      an object that "owns" the data. This object will be passed
400  *              to the callbacks when called. 
401  * 
402  * Virtually sets the contents of the specified clipboard by providing
403  * a list of supported formats for the clipboard data and a function
404  * to call to get the actual data when it is requested.
405  *
406  * The difference between this function and gtk_clipboard_set_with_data()
407  * is that instead of an generic @user_data pointer, a #GObject is passed
408  * in. 
409  * 
410  * Return value: %TRUE if setting the clipboard data succeeded. If setting
411  *               the clipboard data failed the provided callback functions
412  *               will be ignored.
413  **/
414 gboolean
415 gtk_clipboard_set_with_owner (GtkClipboard          *clipboard,
416                               const GtkTargetEntry  *targets,
417                               guint                  n_targets,
418                               GtkClipboardGetFunc    get_func,
419                               GtkClipboardClearFunc  clear_func,
420                               GObject               *owner)
421 {
422   g_return_val_if_fail (clipboard != NULL, FALSE);
423   g_return_val_if_fail (targets != NULL, FALSE);
424   g_return_val_if_fail (get_func != NULL, FALSE);
425   g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
426
427   return gtk_clipboard_set_contents (clipboard, targets, n_targets,
428                                      get_func, clear_func, owner,
429                                      TRUE);
430 }
431
432 /**
433  * gtk_clipboard_get_owner:
434  * @clipboard: a #GtkClipboard
435  * 
436  * If the clipboard contents callbacks were set with 
437  * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or 
438  * gtk_clipboard_clear() has not subsequently called, returns the @owner set 
439  * by gtk_clipboard_set_with_owner().
440  * 
441  * Return value: the owner of the clipboard, if any; otherwise %NULL.
442  **/
443 GObject *
444 gtk_clipboard_get_owner (GtkClipboard *clipboard)
445 {
446   g_return_val_if_fail (clipboard != NULL, NULL);
447
448   if (clipboard->have_owner)
449     return clipboard->user_data;
450   else
451     return NULL;
452 }
453
454 static void
455 clipboard_unset (GtkClipboard *clipboard)
456 {
457   GtkClipboardClearFunc old_clear_func;
458   gpointer old_data;
459   
460   old_clear_func = clipboard->clear_func;
461   old_data = clipboard->user_data;
462           
463   if (clipboard->have_owner)
464     {
465       clipboard_remove_owner_notify (clipboard);
466       clipboard->have_owner = FALSE;
467     }
468   
469   clipboard->get_func = NULL;
470   clipboard->clear_func = NULL;
471   clipboard->user_data = NULL;
472   
473   if (old_clear_func)
474     old_clear_func (clipboard, old_data);
475 }
476
477 /**
478  * gtk_clipboard_clear:
479  * @clipboard:  a #GtkClipboard
480  * 
481  * Clears the contents of the clipboard. Generally this should only
482  * be called between the time you call gtk_clipboard_set_contents(),
483  * and when the @clear_func you supplied is called. Otherwise, the
484  * clipboard may be owned by someone else.
485  **/
486 void
487 gtk_clipboard_clear (GtkClipboard *clipboard)
488 {
489   g_return_if_fail (clipboard != NULL);
490
491   if (clipboard->have_selection)
492     gtk_selection_owner_set (NULL, clipboard->selection,
493                              clipboard_get_timestamp (clipboard));
494 }
495
496 static void 
497 text_get_func (GtkClipboard     *clipboard,
498                GtkSelectionData *selection_data,
499                guint             info,
500                gpointer          data)
501 {
502   gtk_selection_data_set_text (selection_data, data, -1);
503 }
504
505 static void 
506 text_clear_func (GtkClipboard *clipboard,
507                  gpointer      data)
508 {
509   g_free (data);
510 }
511
512 /**
513  * gtk_clipboard_set_text:
514  * @clipboard: a #GtkClipboard object
515  * @text:      a UTF-8 string.
516  * @len:       length of @text, in bytes, or -1, in which case
517  *             the length will be determined with <function>strlen()</function>.
518  * 
519  * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
520  * make a copy of the text and take responsibility for responding
521  * for requests for the text, and for converting the text into
522  * the requested format.
523  **/
524 void 
525 gtk_clipboard_set_text (GtkClipboard *clipboard,
526                         const gchar  *text,
527                         gint          len)
528 {
529   static const GtkTargetEntry targets[] = {
530     { "STRING", 0, TARGET_STRING },
531     { "TEXT",   0, TARGET_TEXT }, 
532     { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
533     { "UTF8_STRING", 0, TARGET_UTF8_STRING }
534   };
535
536   g_return_if_fail (clipboard != NULL);
537   g_return_if_fail (text != NULL);
538   
539   if (len < 0)
540     len = strlen (text);
541   
542   gtk_clipboard_set_with_data (clipboard, 
543                                targets, G_N_ELEMENTS (targets),
544                                text_get_func, text_clear_func,
545                                g_strndup (text, len));
546 }
547
548 static void
549 set_request_contents_info (GtkWidget           *widget,
550                            RequestContentsInfo *info)
551 {
552   if (!request_contents_key_id)
553     request_contents_key_id = g_quark_from_static_string (request_contents_key);
554
555   gtk_object_set_data_by_id (GTK_OBJECT (widget),
556                              request_contents_key_id,
557                              info);
558 }
559
560 static RequestContentsInfo *
561 get_request_contents_info (GtkWidget *widget)
562 {
563   if (!request_contents_key_id)
564     return NULL;
565   else
566     return gtk_object_get_data_by_id (GTK_OBJECT (widget),
567                                       request_contents_key_id);
568 }
569
570 static void 
571 selection_received (GtkWidget            *widget,
572                     GtkSelectionData     *selection_data,
573                     guint                 time)
574 {
575   RequestContentsInfo *request_info = get_request_contents_info (widget);
576   set_request_contents_info (widget, NULL);
577   
578   request_info->callback (gtk_clipboard_get (selection_data->selection), 
579                           selection_data,
580                           request_info->user_data);
581
582   g_free (request_info);
583
584   if (widget != clipboard_widget)
585     gtk_widget_destroy (widget);
586 }
587
588 /**
589  * gtk_clipboard_request_contents:
590  * @clipboard: a #GtkClipboard
591  * @target:    an atom representing the form into which the clipboard
592  *             owner should convert the selection.
593  * @callback:  A function to call when the results are received
594  *             (or the retrieval fails). If the retrieval fails
595  *             the length field of @selection_data will be
596  *             negative.
597  * @user_data: user data to pass to @callback
598  * 
599  * Requests the contents of clipboard as the given target.
600  * When the results of the result are later received the supplied callback
601  * will be called.
602  **/
603 void 
604 gtk_clipboard_request_contents (GtkClipboard            *clipboard,
605                                 GdkAtom                  target,
606                                 GtkClipboardReceivedFunc callback,
607                                 gpointer                 user_data)
608 {
609   RequestContentsInfo *info;
610   GtkWidget *widget;
611
612   g_return_if_fail (clipboard != NULL);
613   g_return_if_fail (target != GDK_NONE);
614   g_return_if_fail (callback != NULL);
615   
616   ensure_clipboard_widget ();
617
618   if (get_request_contents_info (clipboard_widget))
619     widget = make_clipboard_widget (FALSE);
620   else
621     widget = clipboard_widget;
622
623   info = g_new (RequestContentsInfo, 1);
624   info->callback = callback;
625   info->user_data = user_data;
626
627   set_request_contents_info (widget, info);
628
629   gtk_selection_convert (widget, clipboard->selection, target,
630                          clipboard_get_timestamp (clipboard));
631 }
632
633 static void 
634 request_text_received_func (GtkClipboard     *clipboard,
635                             GtkSelectionData *selection_data,
636                             gpointer          data)
637 {
638   RequestTextInfo *info = data;
639   gchar *result = NULL;
640
641   result = gtk_selection_data_get_text (selection_data);
642
643   if (!result)
644     {
645       /* If we asked for UTF8 and didn't get it, try compound_text;
646        * if we asked for compound_text and didn't get it, try string;
647        * If we asked for anything else and didn't get it, give up.
648        */
649       if (selection_data->target == gdk_atom_intern ("UTF8_STRING", FALSE))
650         {
651           gtk_clipboard_request_contents (clipboard,
652                                           gdk_atom_intern ("COMPOUND_TEXT", FALSE), 
653                                           request_text_received_func, info);
654           return;
655         }
656       else if (selection_data->target == gdk_atom_intern ("COMPOUND_TEXT", FALSE))
657         {
658           gtk_clipboard_request_contents (clipboard,
659                                           GDK_TARGET_STRING, 
660                                           request_text_received_func, info);
661           return;
662         }
663     }
664
665   info->callback (clipboard, result, info->user_data);
666   g_free (info);
667   g_free (result);
668 }
669
670 /**
671  * gtk_clipboard_request_text:
672  * @clipboard: a #GtkClipboard
673  * @callback:  a function to call when the text is received,
674  *             or the retrieval fails. (It will always be called
675  *             one way or the other.)
676  * @user_data: user data to pass to @callback.
677  * 
678  * Requests the contents of the clipboard as text. When the text is
679  * later received, it will be converted to UTF-8 if necessary, and
680  * @callback will be called. 
681  *
682  * The @text parameter to @callback will contain the resulting text if
683  * the request succeeded, or %NULL if it failed. This could happen for
684  * various reasons, in particular if the clipboard was empty or if the
685  * contents of the clipboard could not be converted into text form.
686  **/
687 void 
688 gtk_clipboard_request_text (GtkClipboard                *clipboard,
689                             GtkClipboardTextReceivedFunc callback,
690                             gpointer                     user_data)
691 {
692   RequestTextInfo *info;
693   
694   g_return_if_fail (clipboard != NULL);
695   g_return_if_fail (callback != NULL);
696   
697   info = g_new (RequestTextInfo, 1);
698   info->callback = callback;
699   info->user_data = user_data;
700
701   gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("UTF8_STRING", FALSE),
702                                   request_text_received_func,
703                                   info);
704 }
705
706
707 typedef struct
708 {
709   GMainLoop *loop;
710   gpointer data;
711 } WaitResults;
712
713 static void 
714 clipboard_received_func (GtkClipboard     *clipboard,
715                          GtkSelectionData *selection_data,
716                          gpointer          data)
717 {
718   WaitResults *results = data;
719
720   if (selection_data->length >= 0)
721     results->data = gtk_selection_data_copy (selection_data);
722   
723   g_main_quit (results->loop);
724 }
725
726 /**
727  * gtk_clipboard_wait_for_contents:
728  * @clipboard: a #GtkClipboard
729  * @target: an atom representing the form into which the clipboard
730  *          owner should convert the selection.
731  * 
732  * Requests the contents of the clipboard using the given target.
733  * This function waits for the data to be received using the main 
734  * loop, so events, timeouts, etc, may be dispatched during the wait.
735  * 
736  * Return value: a newly-allocated #GtkSelectionData object or %NULL
737  *               if retrieving the given target failed. If non-%NULL,
738  *               this value must be freed with gtk_selection_data_free() 
739  *               when you are finished with it.
740  **/
741 GtkSelectionData *
742 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
743                                  GdkAtom       target)
744 {
745   WaitResults results;
746
747   g_return_val_if_fail (clipboard != NULL, NULL);
748   g_return_val_if_fail (target != GDK_NONE, NULL);
749   
750   results.data = NULL;
751   results.loop = g_main_new (TRUE);
752
753   gtk_clipboard_request_contents (clipboard, target, 
754                                   clipboard_received_func,
755                                   &results);
756
757   if (g_main_is_running (results.loop))
758     {
759       GDK_THREADS_LEAVE ();
760       g_main_run (results.loop);
761       GDK_THREADS_ENTER ();
762     }
763
764   g_main_destroy (results.loop);
765
766   return results.data;
767 }
768
769 static void 
770 clipboard_text_received_func (GtkClipboard *clipboard,
771                               const gchar  *text,
772                               gpointer      data)
773 {
774   WaitResults *results = data;
775
776   results->data = g_strdup (text);
777   g_main_quit (results->loop);
778 }
779
780
781 /**
782  * gtk_clipboard_wait_for_text:
783  * @clipboard: a #GtkClipboard
784  * 
785  * Requests the contents of the clipboard as text and converts
786  * the result to UTF-8 if necessary. This function waits for
787  * the data to be received using the main loop, so events,
788  * timeouts, etc, may be dispatched during the wait.
789  * 
790  * Return value: a newly-allocated UTF-8 string which must
791  *               be freed with g_free(), or %NULL if retrieving
792  *               the selection data failed. (This could happen
793  *               for various reasons, in particular if the
794  *               clipboard was empty or if the contents of the
795  *               clipboard could not be converted into text form.)
796  **/
797 gchar *
798 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
799 {
800   WaitResults results;
801
802   g_return_val_if_fail (clipboard != NULL, NULL);
803   g_return_val_if_fail (clipboard != NULL, NULL);
804   
805   results.data = NULL;
806   results.loop = g_main_new (TRUE);
807
808   gtk_clipboard_request_text (clipboard,
809                               clipboard_text_received_func,
810                               &results);
811
812   if (g_main_is_running (results.loop))
813     {
814       GDK_THREADS_LEAVE ();
815       g_main_run (results.loop);
816       GDK_THREADS_ENTER ();
817     }
818
819   g_main_destroy (results.loop);
820
821   return results.data;
822 }
823
824 /**
825  * gtk_clipboard_wait_is_text_available:
826  * @clipboard: a #GtkClipboard
827  * 
828  * Test to see if there is text available to be pasted
829  * This is done by requesting the TARGETS atom and checking
830  * if it contains any of the names: STRING, TEXT, COMPOUND_TEXT,
831  * UTF8_STRING. This function waits for the data to be received
832  * using the main loop, so events, timeouts, etc, may be dispatched
833  * during the wait.
834  *
835  * This function is a little faster than calling
836  * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
837  * the actual text.
838  * 
839  * Return value: %TRUE is there is text available, %FALSE otherwise.
840  **/
841 gboolean
842 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
843 {
844   GtkSelectionData *data;
845   gboolean result = FALSE;
846
847   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
848   if (data)
849     {
850       result = gtk_selection_data_targets_include_text (data);
851       gtk_selection_data_free (data);
852     }
853
854   return result;
855 }