]> Pileus Git - ~andy/gtk/blob - gtk/gtkclipboard.c
c64d4c0b51c011af6a187cede87e2422f1f720e5
[~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 gdk_atom_intern ("CLIPBOARD", FALSE),
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 persistant 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 set 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 set 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. Because of this, 
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 gtk_clipboard_set_owner(),
437  * and the gtk_clipboard_set_with_data() or gtk_clipboard_clear() has not
438  * subsequently called, returns the @owner set by gtk_clipboard_set_owner().
439  * 
440  * Return value: the owner of the clipboard, if any; otherwise %NULL.
441  **/
442 GObject *
443 gtk_clipboard_get_owner (GtkClipboard *clipboard)
444 {
445   g_return_val_if_fail (clipboard != NULL, NULL);
446
447   if (clipboard->have_owner)
448     return clipboard->user_data;
449   else
450     return NULL;
451 }
452
453 static void
454 clipboard_unset (GtkClipboard *clipboard)
455 {
456   GtkClipboardClearFunc old_clear_func;
457   gpointer old_data;
458   
459   old_clear_func = clipboard->clear_func;
460   old_data = clipboard->user_data;
461           
462   if (clipboard->have_owner)
463     {
464       clipboard_remove_owner_notify (clipboard);
465       clipboard->have_owner = FALSE;
466     }
467   
468   clipboard->get_func = NULL;
469   clipboard->clear_func = NULL;
470   clipboard->user_data = NULL;
471   
472   if (old_clear_func)
473     old_clear_func (clipboard, old_data);
474 }
475
476 /**
477  * gtk_clipboard_clear:
478  * @clipboard:  a #GtkClipboard
479  * 
480  * Clear the contents of the clipboard. Generally this should only
481  * be called between the time you call gtk_clipboard_set_contents(),
482  * and when the @clear_func you supplied is called. Otherwise, the
483  * clipboard may be owned by someone else.
484  **/
485 void
486 gtk_clipboard_clear (GtkClipboard *clipboard)
487 {
488   g_return_if_fail (clipboard != NULL);
489
490   if (clipboard->have_selection)
491     gtk_selection_owner_set (NULL, clipboard->selection,
492                              clipboard_get_timestamp (clipboard));
493 }
494
495 static void 
496 text_get_func (GtkClipboard     *clipboard,
497                GtkSelectionData *selection_data,
498                guint             info,
499                gpointer          data)
500 {
501   gtk_selection_data_set_text (selection_data, data);
502 }
503
504 static void 
505 text_clear_func (GtkClipboard *clipboard,
506                  gpointer      data)
507 {
508   g_free (data);
509 }
510
511 /**
512  * gtk_clipboard_set_text:
513  * @clipboard: a #GtkClipboard object
514  * @text:      a UTF-8 string.
515  * @len:       length of @text, in bytes, or -1, in which case
516  *             the length will be determined with strlen().
517  * 
518  * Set the contents of the clipboard to the given UTF-8 string. GTK+ will
519  * make a copy of the text and take responsibility for responding
520  * for requests for the text, and for converting the text into
521  * the requested format.
522  **/
523 void 
524 gtk_clipboard_set_text (GtkClipboard *clipboard,
525                         const gchar  *text,
526                         gint          len)
527 {
528   static const GtkTargetEntry targets[] = {
529     { "STRING", 0, TARGET_STRING },
530     { "TEXT",   0, TARGET_TEXT }, 
531     { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
532     { "UTF8_STRING", 0, TARGET_UTF8_STRING }
533   };
534
535   g_return_if_fail (clipboard != NULL);
536   g_return_if_fail (text != NULL);
537   
538   if (len < 0)
539     len = strlen (text);
540   
541   gtk_clipboard_set_with_data (clipboard, 
542                                targets, G_N_ELEMENTS (targets),
543                                text_get_func, text_clear_func,
544                                g_strndup (text, len));
545 }
546
547 static void
548 set_request_contents_info (GtkWidget           *widget,
549                            RequestContentsInfo *info)
550 {
551   if (!request_contents_key_id)
552     request_contents_key_id = g_quark_from_static_string (request_contents_key);
553
554   gtk_object_set_data_by_id (GTK_OBJECT (widget),
555                              request_contents_key_id,
556                              info);
557 }
558
559 static RequestContentsInfo *
560 get_request_contents_info (GtkWidget *widget)
561 {
562   if (!request_contents_key_id)
563     return NULL;
564   else
565     return gtk_object_get_data_by_id (GTK_OBJECT (widget),
566                                       request_contents_key_id);
567 }
568
569 static void 
570 selection_received (GtkWidget            *widget,
571                     GtkSelectionData     *selection_data,
572                     guint                 time)
573 {
574   RequestContentsInfo *request_info = get_request_contents_info (widget);
575   set_request_contents_info (widget, NULL);
576   
577   request_info->callback (gtk_clipboard_get (selection_data->selection), 
578                           selection_data,
579                           request_info->user_data);
580
581   g_free (request_info);
582
583   if (widget != clipboard_widget)
584     gtk_widget_destroy (widget);
585 }
586
587 /**
588  * gtk_clipboard_request_contents:
589  * @clipboard: a #GtkClipboard
590  * @target:    an atom representing the form into which the clipboard
591  *             owner should convert the selection.
592  * @callback:  A function to call when the results are received
593  *             (or the retrieval fails.) If the retrieval fails
594  *             the length field of @selection_data will be
595  *             negative.
596  * @user_data: user data to pass to @callback
597  * 
598  * Requests the contents of clipboard as the given target.
599  * When the results of the result are later received the supplied callback
600  * will be called.
601  **/
602 void 
603 gtk_clipboard_request_contents (GtkClipboard            *clipboard,
604                                 GdkAtom                  target,
605                                 GtkClipboardReceivedFunc callback,
606                                 gpointer                 user_data)
607 {
608   RequestContentsInfo *info;
609   GtkWidget *widget;
610
611   g_return_if_fail (clipboard != NULL);
612   g_return_if_fail (target != GDK_NONE);
613   g_return_if_fail (callback != NULL);
614   
615   ensure_clipboard_widget ();
616
617   if (get_request_contents_info (clipboard_widget))
618     widget = make_clipboard_widget (FALSE);
619   else
620     widget = clipboard_widget;
621
622   info = g_new (RequestContentsInfo, 1);
623   info->callback = callback;
624   info->user_data = user_data;
625
626   set_request_contents_info (widget, info);
627
628   gtk_selection_convert (widget, clipboard->selection, target,
629                          clipboard_get_timestamp (clipboard));
630 }
631
632 static void 
633 request_text_received_func (GtkClipboard     *clipboard,
634                             GtkSelectionData *selection_data,
635                             gpointer          data)
636 {
637   RequestTextInfo *info = data;
638   gchar *result = NULL;
639
640   result = gtk_selection_data_get_text (selection_data);
641
642   if (!result)
643     {
644       /* If we asked for UTF8 and didn't get it, try text; if we asked
645        * for text and didn't get it, try string.  If we asked for
646        * anything else and didn't get it, give up.
647        */
648       if (selection_data->target == gdk_atom_intern ("UTF8_STRING", FALSE))
649         {
650           gtk_clipboard_request_contents (clipboard,
651                                           gdk_atom_intern ("TEXT", FALSE), 
652                                           request_text_received_func, info);
653           return;
654         }
655       else if (selection_data->target == gdk_atom_intern ("TEXT", FALSE))
656         {
657           gtk_clipboard_request_contents (clipboard,
658                                           GDK_TARGET_STRING, 
659                                           request_text_received_func, info);
660           return;
661         }
662     }
663
664   info->callback (clipboard, result, info->user_data);
665   g_free (info);
666   g_free (result);
667 }
668
669 /**
670  * gtk_clipboard_request_text:
671  * @clipboard: a #GtkClipboard
672  * @callback:  a function to call when the text is received,
673  *             or the retrieval fails. (It will always be called
674  *             one way or the other.)
675  * @user_data: user data to pass to @callback.
676  * 
677  * Requests the contents of the clipboard as text. When the text is
678  * later received, it will be converted to UTF-8 if necessary, and
679  * @callback will be called. 
680  *
681  * The @text parameter to @callback will contain the resulting text if
682  * the request succeeded, or %NULL if it failed. This could happen for
683  * various reasons, in particular if the clipboard was empty or if the
684  * contents of the clipboard could not be converted into text form.
685  **/
686 void 
687 gtk_clipboard_request_text (GtkClipboard                *clipboard,
688                             GtkClipboardTextReceivedFunc callback,
689                             gpointer                     user_data)
690 {
691   RequestTextInfo *info;
692   
693   g_return_if_fail (clipboard != NULL);
694   g_return_if_fail (callback != NULL);
695   
696   info = g_new (RequestTextInfo, 1);
697   info->callback = callback;
698   info->user_data = user_data;
699
700   gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("UTF8_STRING", FALSE),
701                                   request_text_received_func,
702                                   info);
703 }
704
705
706 typedef struct
707 {
708   GMainLoop *loop;
709   gpointer data;
710 } WaitResults;
711
712 static void 
713 clipboard_received_func (GtkClipboard     *clipboard,
714                          GtkSelectionData *selection_data,
715                          gpointer          data)
716 {
717   WaitResults *results = data;
718
719   if (selection_data->length >= 0)
720     results->data = gtk_selection_data_copy (selection_data);
721   
722   g_main_quit (results->loop);
723 }
724
725 /**
726  * gtk_clipboard_wait_for_contents:
727  * @clipboard: a #GtkClipboard
728  * @target: an atom representing the form into which the clipboard
729  *          owner should convert the selection.
730  * 
731  * Requests the contents of the clipboard using the given target.
732  * This function waits for the data to be received using the main 
733  * loop, so events, timeouts, etc, may be dispatched during the wait.
734  * 
735  * Return value: a newly allocated #GtkSelectionData object or %NULL
736  *               if retrieving the given target failed. If non-%NULL,
737  *               this value freed with gtk_selection_data_free() when
738  *               you are finished with it.
739  **/
740 GtkSelectionData *
741 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
742                                  GdkAtom       target)
743 {
744   WaitResults results;
745
746   g_return_val_if_fail (clipboard != NULL, NULL);
747   g_return_val_if_fail (target != GDK_NONE, NULL);
748   
749   results.data = NULL;
750   results.loop = g_main_new (TRUE);
751
752   gtk_clipboard_request_contents (clipboard, target, 
753                                   clipboard_received_func,
754                                   &results);
755
756   if (g_main_is_running (results.loop))
757     {
758       GDK_THREADS_LEAVE ();
759       g_main_run (results.loop);
760       GDK_THREADS_ENTER ();
761     }
762
763   g_main_destroy (results.loop);
764
765   return results.data;
766 }
767
768 static void 
769 clipboard_text_received_func (GtkClipboard *clipboard,
770                               const gchar  *text,
771                               gpointer      data)
772 {
773   WaitResults *results = data;
774
775   results->data = g_strdup (text);
776   g_main_quit (results->loop);
777 }
778
779
780 /**
781  * gtk_clipboard_wait_for_text:
782  * @clipboard: a #GtkClipboard
783  * 
784  * Requests the contents of the clipboard as text and converts
785  * the result to UTF-8 if necessary. This function waits for
786  * the data to be received using the main loop, so events,
787  * timeouts, etc, may be dispatched during the wait.
788  * 
789  * Return value: a newly allocated UTF-8 string which must
790  *               be freed with g_free(), or %NULL if retrieving
791  *               the selection data failed. (This could happen
792  *               for various reasons, in particular if the
793  *               clipboard was empty or if the contents of the
794  *               clipboard could not be converted into text form.)
795  **/
796 gchar *
797 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
798 {
799   WaitResults results;
800
801   g_return_val_if_fail (clipboard != NULL, NULL);
802   g_return_val_if_fail (clipboard != NULL, NULL);
803   
804   results.data = NULL;
805   results.loop = g_main_new (TRUE);
806
807   gtk_clipboard_request_text (clipboard,
808                               clipboard_text_received_func,
809                               &results);
810
811   if (g_main_is_running (results.loop))
812     {
813       GDK_THREADS_LEAVE ();
814       g_main_run (results.loop);
815       GDK_THREADS_ENTER ();
816     }
817
818   g_main_destroy (results.loop);
819
820   return results.data;
821 }
822