]> Pileus Git - ~andy/gtk/blob - gtk/gtkclipboard.c
7b223ff4ed357f4bfe2bebe88f86601e8caa8990
[~andy/gtk] / gtk / gtkclipboard.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  * Copyright (C) 2004 Nokia Corporation
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Global clipboard abstraction. 
21  */
22
23 #include "config.h"
24 #include <string.h>
25
26 #include "gtkclipboard.h"
27 #include "gtkinvisible.h"
28 #include "gtkmain.h"
29 #include "gtkmarshalers.h"
30 #include "gtktextbufferrichtext.h"
31 #include "gtkintl.h"
32
33 #ifdef GDK_WINDOWING_X11
34 #include "x11/gdkx.h"
35 #endif
36
37 #ifdef GDK_WINDOWING_BROADWAY
38 #include "broadway/gdkbroadway.h"
39 #endif
40
41 #ifdef GDK_WINDOWING_WIN32
42 #include "win32/gdkwin32.h"
43 #endif
44
45
46 /**
47  * SECTION:gtkclipboard
48  * @Short_description: Storing data on clipboards
49  * @Title: Clipboards
50  * @See_also: #GtkSelection
51  *
52  * The #GtkClipboard object represents a clipboard of data shared
53  * between different processes or between different widgets in
54  * the same process. Each clipboard is identified by a name encoded as a
55  * #GdkAtom. (Conversion to and from strings can be done with
56  * gdk_atom_intern() and gdk_atom_name().) The default clipboard
57  * corresponds to the "CLIPBOARD" atom; another commonly used clipboard
58  * is the "PRIMARY" clipboard, which, in X, traditionally contains
59  * the currently selected text.
60  *
61  * To support having a number of different formats on the clipboard
62  * at the same time, the clipboard mechanism allows providing
63  * callbacks instead of the actual data.  When you set the contents
64  * of the clipboard, you can either supply the data directly (via
65  * functions like gtk_clipboard_set_text()), or you can supply a
66  * callback to be called at a later time when the data is needed (via
67  * gtk_clipboard_set_with_data() or gtk_clipboard_set_with_owner().)
68  * Providing a callback also avoids having to make copies of the data
69  * when it is not needed.
70  *
71  * gtk_clipboard_set_with_data() and gtk_clipboard_set_with_owner()
72  * are quite similar; the choice between the two depends mostly on
73  * which is more convenient in a particular situation.
74  * The former is most useful when you want to have a blob of data
75  * with callbacks to convert it into the various data types that you
76  * advertise. When the @clear_func you provided is called, you
77  * simply free the data blob. The latter is more useful when the
78  * contents of clipboard reflect the internal state of a #GObject
79  * (As an example, for the PRIMARY clipboard, when an entry widget
80  * provides the clipboard's contents the contents are simply the
81  * text within the selected region.) If the contents change, the
82  * entry widget can call gtk_clipboard_set_with_owner() to update
83  * the timestamp for clipboard ownership, without having to worry
84  * about @clear_func being called.
85  *
86  * Requesting the data from the clipboard is essentially
87  * asynchronous. If the contents of the clipboard are provided within
88  * the same process, then a direct function call will be made to
89  * retrieve the data, but if they are provided by another process,
90  * then the data needs to be retrieved from the other process, which
91  * may take some time. To avoid blocking the user interface, the call
92  * to request the selection, gtk_clipboard_request_contents() takes a
93  * callback that will be called when the contents are received (or
94  * when the request fails.) If you don't want to deal with providing
95  * a separate callback, you can also use gtk_clipboard_wait_for_contents().
96  * What this does is run the GLib main loop recursively waiting for
97  * the contents. This can simplify the code flow, but you still have
98  * to be aware that other callbacks in your program can be called
99  * while this recursive mainloop is running.
100  *
101  * Along with the functions to get the clipboard contents as an
102  * arbitrary data chunk, there are also functions to retrieve
103  * it as text, gtk_clipboard_request_text() and
104  * gtk_clipboard_wait_for_text(). These functions take care of
105  * determining which formats are advertised by the clipboard
106  * provider, asking for the clipboard in the best available format
107  * and converting the results into the UTF-8 encoding. (The standard
108  * form for representing strings in GTK+.)
109  */
110
111
112 enum {
113   OWNER_CHANGE,
114   LAST_SIGNAL
115 };
116
117 typedef struct _GtkClipboardClass GtkClipboardClass;
118
119 typedef struct _RequestContentsInfo RequestContentsInfo;
120 typedef struct _RequestTextInfo RequestTextInfo;
121 typedef struct _RequestRichTextInfo RequestRichTextInfo;
122 typedef struct _RequestImageInfo RequestImageInfo;
123 typedef struct _RequestURIInfo RequestURIInfo;
124 typedef struct _RequestTargetsInfo RequestTargetsInfo;
125
126 struct _GtkClipboard 
127 {
128   GObject parent_instance;
129
130   GdkAtom selection;
131
132   GtkClipboardGetFunc get_func;
133   GtkClipboardClearFunc clear_func;
134   gpointer user_data;
135   gboolean have_owner;
136
137   guint32 timestamp;
138
139   gboolean have_selection;
140   GdkDisplay *display;
141
142   GdkAtom *cached_targets;
143   gint     n_cached_targets;
144
145   gulong     notify_signal_id;
146   gboolean   storing_selection;
147   GMainLoop *store_loop;
148   guint      store_timeout;
149   gint       n_storable_targets;
150   GdkAtom   *storable_targets;
151 };
152
153 struct _GtkClipboardClass
154 {
155   GObjectClass parent_class;
156
157   void (*owner_change) (GtkClipboard        *clipboard,
158                         GdkEventOwnerChange *event);
159 };
160
161 struct _RequestContentsInfo
162 {
163   GtkClipboardReceivedFunc callback;
164   gpointer user_data;
165 };
166
167 struct _RequestTextInfo
168 {
169   GtkClipboardTextReceivedFunc callback;
170   gpointer user_data;
171 };
172
173 struct _RequestRichTextInfo
174 {
175   GtkClipboardRichTextReceivedFunc callback;
176   GdkAtom *atoms;
177   gint     n_atoms;
178   gint     current_atom;
179   gpointer user_data;
180 };
181
182 struct _RequestImageInfo
183 {
184   GtkClipboardImageReceivedFunc callback;
185   gpointer user_data;
186 };
187
188 struct _RequestURIInfo
189 {
190   GtkClipboardURIReceivedFunc callback;
191   gpointer user_data;
192 };
193
194 struct _RequestTargetsInfo
195 {
196   GtkClipboardTargetsReceivedFunc callback;
197   gpointer user_data;
198 };
199
200 static void gtk_clipboard_class_init   (GtkClipboardClass   *class);
201 static void gtk_clipboard_finalize     (GObject             *object);
202 static void gtk_clipboard_owner_change (GtkClipboard        *clipboard,
203                                         GdkEventOwnerChange *event);
204
205 static void          clipboard_unset      (GtkClipboard     *clipboard);
206 static void          selection_received   (GtkWidget        *widget,
207                                            GtkSelectionData *selection_data,
208                                            guint             time);
209 static GtkClipboard *clipboard_peek       (GdkDisplay       *display,
210                                            GdkAtom           selection,
211                                            gboolean          only_if_exists);
212 static GtkWidget *   get_clipboard_widget (GdkDisplay       *display);
213
214
215 enum {
216   TARGET_STRING,
217   TARGET_TEXT,
218   TARGET_COMPOUND_TEXT,
219   TARGET_UTF8_STRING,
220   TARGET_SAVE_TARGETS
221 };
222
223 static const gchar request_contents_key[] = "gtk-request-contents";
224 static GQuark request_contents_key_id = 0;
225
226 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
227 static GQuark clipboards_owned_key_id = 0;
228
229 static guint         clipboard_signals[LAST_SIGNAL] = { 0 };
230
231 G_DEFINE_TYPE (GtkClipboard, gtk_clipboard, G_TYPE_OBJECT)
232
233 static void
234 gtk_clipboard_init (GtkClipboard *object)
235 {
236 }
237
238 static void
239 gtk_clipboard_class_init (GtkClipboardClass *class)
240 {
241   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
242
243   gobject_class->finalize = gtk_clipboard_finalize;
244
245   class->owner_change = gtk_clipboard_owner_change;
246
247   /**
248    * GtkClipboard::owner-change:
249    * @clipboard: the #GtkClipboard on which the signal is emitted
250    * @event: (type Gdk.EventOwnerChange): the @GdkEventOwnerChange event
251    *
252    * The ::owner-change signal is emitted when GTK+ receives an
253    * event that indicates that the ownership of the selection
254    * associated with @clipboard has changed.
255    *
256    * Since: 2.6
257    */
258   clipboard_signals[OWNER_CHANGE] =
259     g_signal_new (I_("owner-change"),
260                   G_TYPE_FROM_CLASS (gobject_class),
261                   G_SIGNAL_RUN_FIRST,
262                   G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
263                   NULL, NULL,
264                   _gtk_marshal_VOID__BOXED,
265                   G_TYPE_NONE, 1,
266                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
267 }
268
269 static void
270 gtk_clipboard_finalize (GObject *object)
271 {
272   GtkClipboard *clipboard;
273   GtkWidget *clipboard_widget = NULL;
274   GSList *clipboards = NULL;
275
276   clipboard = GTK_CLIPBOARD (object);
277
278   if (clipboard->display)
279     {
280       clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
281
282       if (g_slist_index (clipboards, clipboard) >= 0)
283         g_warning ("GtkClipboard prematurely finalized");
284
285       clipboards = g_slist_remove (clipboards, clipboard);
286
287       g_object_set_data (G_OBJECT (clipboard->display), "gtk-clipboard-list", 
288                          clipboards);
289
290       /* don't use get_clipboard_widget() here because it would create the
291        * widget if it doesn't exist.
292        */
293       clipboard_widget = g_object_get_data (G_OBJECT (clipboard->display),
294                                             "gtk-clipboard-widget");
295     }
296
297   clipboard_unset (clipboard);
298
299   if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
300     g_main_loop_quit (clipboard->store_loop);
301
302   if (clipboard->store_timeout != 0)
303     g_source_remove (clipboard->store_timeout);
304
305   if (clipboard->notify_signal_id != 0)
306     g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
307
308   g_free (clipboard->storable_targets);
309   g_free (clipboard->cached_targets);
310
311   G_OBJECT_CLASS (gtk_clipboard_parent_class)->finalize (object);
312 }
313
314 static void
315 clipboard_display_closed (GdkDisplay   *display,
316                           gboolean      is_error,
317                           GtkClipboard *clipboard)
318 {
319   GSList *clipboards;
320
321   clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
322   g_object_run_dispose (G_OBJECT (clipboard));
323   clipboards = g_slist_remove (clipboards, clipboard);
324   g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
325   g_object_unref (clipboard);
326 }
327
328 /**
329  * gtk_clipboard_get_for_display:
330  * @display: the display for which the clipboard is to be retrieved or created
331  * @selection: a #GdkAtom which identifies the clipboard to use.
332  *
333  * Returns the clipboard object for the given selection.
334  * Cut/copy/paste menu items and keyboard shortcuts should use
335  * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
336  * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
337  * for backwards compatibility reasons.)
338  * The currently-selected object or text should be provided on the clipboard
339  * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
340  * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
341  * to the default clipboard, i.e. they copy the selection to what the
342  * user sees as the clipboard.
343  *
344  * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
345  * ("CLIPBOARD", FALSE)</literal>. See <ulink
346  * url="http://www.freedesktop.org/Standards/clipboards-spec">
347  * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
348  * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
349  * selections under the X window system. On Win32 the
350  * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
351  *
352  * It's possible to have arbitrary named clipboards; if you do invent
353  * new clipboards, you should prefix the selection name with an
354  * underscore (because the ICCCM requires that nonstandard atoms are
355  * underscore-prefixed), and namespace it as well. For example,
356  * if your application called "Foo" has a special-purpose
357  * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
358  *
359  * Return value: (transfer none): the appropriate clipboard object. If no
360  *   clipboard already exists, a new one will be created. Once a clipboard
361  *   object has been created, it is persistent and, since it is owned by
362  *   GTK+, must not be freed or unrefd.
363  *
364  * Since: 2.2
365  **/
366 GtkClipboard *
367 gtk_clipboard_get_for_display (GdkDisplay *display, 
368                                GdkAtom     selection)
369 {
370   g_return_val_if_fail (display != NULL, NULL); /* See bgo#463773; this is needed because Flash Player sucks */
371   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
372   g_return_val_if_fail (!gdk_display_is_closed (display), NULL);
373
374   return clipboard_peek (display, selection, FALSE);
375 }
376
377
378 /**
379  * gtk_clipboard_get:
380  * @selection: a #GdkAtom which identifies the clipboard to use
381  *
382  * Returns the clipboard object for the given selection.
383  * See gtk_clipboard_get_for_display() for complete details.
384  *
385  * Return value: (transfer none): the appropriate clipboard object. If no clipboard
386  *     already exists, a new one will be created. Once a clipboard
387  *     object has been created, it is persistent and, since it is
388  *     owned by GTK+, must not be freed or unreffed.
389  */
390 GtkClipboard *
391 gtk_clipboard_get (GdkAtom selection)
392 {
393   return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
394 }
395
396 static void 
397 selection_get_cb (GtkWidget          *widget,
398                   GtkSelectionData   *selection_data,
399                   guint               info,
400                   guint               time)
401 {
402   GtkClipboard *clipboard;
403
404   clipboard = gtk_widget_get_clipboard (widget,
405                                         gtk_selection_data_get_selection (selection_data));
406
407   if (clipboard && clipboard->get_func)
408     clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
409 }
410
411 static gboolean
412 selection_clear_event_cb (GtkWidget         *widget,
413                           GdkEventSelection *event)
414 {
415   GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
416
417   if (clipboard)
418     {
419       clipboard_unset (clipboard);
420
421       return TRUE;
422     }
423
424   return FALSE;
425 }
426
427 static GtkWidget *
428 make_clipboard_widget (GdkDisplay *display, 
429                        gboolean    provider)
430 {
431   GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
432
433   g_signal_connect (widget, "selection-received",
434                     G_CALLBACK (selection_received), NULL);
435
436   if (provider)
437     {
438       /* We need this for gdk_x11_get_server_time() */
439       gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
440       
441       g_signal_connect (widget, "selection-get",
442                         G_CALLBACK (selection_get_cb), NULL);
443       g_signal_connect (widget, "selection-clear-event",
444                         G_CALLBACK (selection_clear_event_cb), NULL);
445     }
446
447   return widget;
448 }
449
450 static GtkWidget *
451 get_clipboard_widget (GdkDisplay *display)
452 {
453   GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
454   if (!clip_widget)
455     {
456       clip_widget = make_clipboard_widget (display, TRUE);
457       g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
458     }
459
460   return clip_widget;
461 }
462
463 /* This function makes a very good guess at what the correct
464  * timestamp for a selection request should be. If there is
465  * a currently processed event, it uses the timestamp for that
466  * event, otherwise it uses the current server time. However,
467  * if the time resulting from that is older than the time used
468  * last time, it uses the time used last time instead.
469  *
470  * In order implement this correctly, we never use CurrentTime,
471  * but actually retrieve the actual timestamp from the server.
472  * This is a little slower but allows us to make the guarantee
473  * that the times used by this application will always ascend
474  * and we won't get selections being rejected just because
475  * we are using a correct timestamp from an event, but used
476  * CurrentTime previously.
477  */
478 static guint32
479 clipboard_get_timestamp (GtkClipboard *clipboard)
480 {
481   GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
482   guint32 timestamp = gtk_get_current_event_time ();
483   GdkWindow *window;
484
485   if (timestamp == GDK_CURRENT_TIME)
486     {
487       window = gtk_widget_get_window (clipboard_widget);
488 #ifdef GDK_WINDOWING_X11
489       if (GDK_IS_X11_WINDOW (window))
490         {
491           timestamp = gdk_x11_get_server_time (gtk_widget_get_window (clipboard_widget));
492         }
493       else
494 #endif
495 #if defined GDK_WINDOWING_WIN32
496       if (GDK_IS_WIN32_WINDOW (window))
497         {
498           timestamp = GetMessageTime ();
499         }
500       else
501 #endif
502 #if defined GDK_WINDOWING_BROADWAY
503       if (GDK_IS_BROADWAY_WINDOW (window))
504         {
505           timestamp = gdk_broadway_get_last_seen_time (window);
506         }
507       else
508 #endif
509         {
510           /* No implementation */
511         }
512     }
513   else
514     {
515       if (clipboard->timestamp != GDK_CURRENT_TIME)
516         {
517           /* Check to see if clipboard->timestamp is newer than
518            * timestamp, accounting for wraparound.
519            */
520
521           guint32 max = timestamp + 0x80000000;
522
523           if ((max > timestamp &&
524                (clipboard->timestamp > timestamp &&
525                 clipboard->timestamp <= max)) ||
526               (max <= timestamp &&
527                (clipboard->timestamp > timestamp ||
528                 clipboard->timestamp <= max)))
529             {
530               timestamp = clipboard->timestamp;
531             }
532         }
533     }
534
535   clipboard->timestamp = timestamp;
536
537   return timestamp;
538 }
539
540 static void
541 clipboard_owner_destroyed (gpointer data)
542 {
543   GSList *clipboards = data;
544   GSList *tmp_list;
545
546   tmp_list = clipboards;
547   while (tmp_list)
548     {
549       GtkClipboard *clipboard = tmp_list->data;
550
551       clipboard->get_func = NULL;
552       clipboard->clear_func = NULL;
553       clipboard->user_data = NULL;
554       clipboard->have_owner = FALSE;
555
556       gtk_clipboard_clear (clipboard);
557
558       tmp_list = tmp_list->next;
559     }
560   
561   g_slist_free (clipboards);
562 }
563
564 static void
565 clipboard_add_owner_notify (GtkClipboard *clipboard)
566 {
567   if (!clipboards_owned_key_id)
568     clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
569   
570   if (clipboard->have_owner)
571     g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
572                              g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
573                                                                     clipboards_owned_key_id),
574                                               clipboard),
575                              clipboard_owner_destroyed);
576 }
577
578 static void
579 clipboard_remove_owner_notify (GtkClipboard *clipboard)
580 {
581   if (clipboard->have_owner)
582      g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
583                               g_slist_remove (g_object_steal_qdata (clipboard->user_data,
584                                                                     clipboards_owned_key_id),
585                                               clipboard),
586                               clipboard_owner_destroyed);
587 }
588           
589 static gboolean
590 gtk_clipboard_set_contents (GtkClipboard         *clipboard,
591                             const GtkTargetEntry *targets,
592                             guint                 n_targets,
593                             GtkClipboardGetFunc   get_func,
594                             GtkClipboardClearFunc clear_func,
595                             gpointer              user_data,
596                             gboolean              have_owner)
597 {
598   GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
599
600   if (gtk_selection_owner_set_for_display (clipboard->display,
601                                            clipboard_widget,
602                                            clipboard->selection,
603                                            clipboard_get_timestamp (clipboard)))
604     {
605       clipboard->have_selection = TRUE;
606
607       if (clipboard->n_cached_targets != -1)
608         {
609           g_free (clipboard->cached_targets);
610           clipboard->cached_targets = NULL;
611           clipboard->n_cached_targets = -1;
612         }
613
614       if (!(clipboard->have_owner && have_owner) ||
615           clipboard->user_data != user_data)
616         {
617           clipboard_unset (clipboard);
618
619           if (clipboard->get_func)
620             {
621               /* Calling unset() caused the clipboard contents to be reset!
622                * Avoid leaking and return 
623                */
624               if (!(clipboard->have_owner && have_owner) ||
625                   clipboard->user_data != user_data)
626                 {
627                   (*clear_func) (clipboard, user_data);
628                   return FALSE;
629                 }
630               else
631                 return TRUE;
632             }
633           else
634             {
635               clipboard->user_data = user_data;
636               clipboard->have_owner = have_owner;
637               if (have_owner)
638                 clipboard_add_owner_notify (clipboard);
639             }
640           
641         }
642
643       clipboard->get_func = get_func;
644       clipboard->clear_func = clear_func;
645
646       gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
647       gtk_selection_add_targets (clipboard_widget, clipboard->selection,
648                                  targets, n_targets);
649
650       return TRUE;
651     }
652   else
653     return FALSE;
654 }
655
656 /**
657  * gtk_clipboard_set_with_data: (skip)
658  * @clipboard: a #GtkClipboard
659  * @targets: (array length=n_targets): array containing information
660  *     about the available forms for the clipboard data
661  * @n_targets: number of elements in @targets
662  * @get_func: (scope async): function to call to get the actual clipboard data
663  * @clear_func: (scope async): when the clipboard contents are set again,
664  *     this function will be called, and @get_func will not be subsequently
665  *     called.
666  * @user_data: user data to pass to @get_func and @clear_func.
667  *
668  * Virtually sets the contents of the specified clipboard by providing
669  * a list of supported formats for the clipboard data and a function
670  * to call to get the actual data when it is requested.
671  *
672  * Return value: %TRUE if setting the clipboard data succeeded.
673  *    If setting the clipboard data failed the provided callback
674  *    functions will be ignored.
675  **/
676 gboolean
677 gtk_clipboard_set_with_data (GtkClipboard          *clipboard,
678                              const GtkTargetEntry  *targets,
679                              guint                  n_targets,
680                              GtkClipboardGetFunc    get_func,
681                              GtkClipboardClearFunc  clear_func,
682                              gpointer               user_data)
683 {
684   g_return_val_if_fail (clipboard != NULL, FALSE);
685   g_return_val_if_fail (targets != NULL, FALSE);
686   g_return_val_if_fail (get_func != NULL, FALSE);
687
688   return gtk_clipboard_set_contents (clipboard, targets, n_targets,
689                                      get_func, clear_func, user_data,
690                                      FALSE);
691 }
692
693 /**
694  * gtk_clipboard_set_with_owner: (skip)
695  * @clipboard: a #GtkClipboard
696  * @targets: (array length=n_targets): array containing information
697  *     about the available forms for the clipboard data
698  * @n_targets: number of elements in @targets
699  * @get_func: (scope async): function to call to get the actual clipboard data
700  * @clear_func: (scope async): when the clipboard contents are set again,
701  *     this function will be called, and @get_func will not be subsequently
702  *     called
703  * @owner: an object that "owns" the data. This object will be passed
704  *     to the callbacks when called
705  *
706  * Virtually sets the contents of the specified clipboard by providing
707  * a list of supported formats for the clipboard data and a function
708  * to call to get the actual data when it is requested.
709  *
710  * The difference between this function and gtk_clipboard_set_with_data()
711  * is that instead of an generic @user_data pointer, a #GObject is passed
712  * in.
713  *
714  * Return value: %TRUE if setting the clipboard data succeeded.
715  *     If setting the clipboard data failed the provided callback
716  *     functions will be ignored.
717  **/
718 gboolean
719 gtk_clipboard_set_with_owner (GtkClipboard          *clipboard,
720                               const GtkTargetEntry  *targets,
721                               guint                  n_targets,
722                               GtkClipboardGetFunc    get_func,
723                               GtkClipboardClearFunc  clear_func,
724                               GObject               *owner)
725 {
726   g_return_val_if_fail (clipboard != NULL, FALSE);
727   g_return_val_if_fail (targets != NULL, FALSE);
728   g_return_val_if_fail (get_func != NULL, FALSE);
729   g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
730
731   return gtk_clipboard_set_contents (clipboard, targets, n_targets,
732                                      get_func, clear_func, owner,
733                                      TRUE);
734 }
735
736 /**
737  * gtk_clipboard_get_owner:
738  * @clipboard: a #GtkClipboard
739  *
740  * If the clipboard contents callbacks were set with
741  * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
742  * gtk_clipboard_clear() has not subsequently called, returns the owner set
743  * by gtk_clipboard_set_with_owner().
744  *
745  * Return value: (transfer none): the owner of the clipboard, if any;
746  *     otherwise %NULL.
747  **/
748 GObject *
749 gtk_clipboard_get_owner (GtkClipboard *clipboard)
750 {
751   g_return_val_if_fail (clipboard != NULL, NULL);
752
753   if (clipboard->have_owner)
754     return clipboard->user_data;
755   else
756     return NULL;
757 }
758
759 static void
760 clipboard_unset (GtkClipboard *clipboard)
761 {
762   GtkClipboardClearFunc old_clear_func;
763   gpointer old_data;
764   gboolean old_have_owner;
765   gint old_n_storable_targets;
766   
767   old_clear_func = clipboard->clear_func;
768   old_data = clipboard->user_data;
769   old_have_owner = clipboard->have_owner;
770   old_n_storable_targets = clipboard->n_storable_targets;
771   
772   if (old_have_owner)
773     {
774       clipboard_remove_owner_notify (clipboard);
775       clipboard->have_owner = FALSE;
776     }
777
778   clipboard->n_storable_targets = -1;
779   g_free (clipboard->storable_targets);
780   clipboard->storable_targets = NULL;
781       
782   clipboard->get_func = NULL;
783   clipboard->clear_func = NULL;
784   clipboard->user_data = NULL;
785   
786   if (old_clear_func)
787     old_clear_func (clipboard, old_data);
788
789   /* If we've transferred the clipboard data to the manager,
790    * unref the owner
791    */
792   if (old_have_owner &&
793       old_n_storable_targets != -1)
794     g_object_unref (old_data);
795 }
796
797 /**
798  * gtk_clipboard_clear:
799  * @clipboard:  a #GtkClipboard
800  * 
801  * Clears the contents of the clipboard. Generally this should only
802  * be called between the time you call gtk_clipboard_set_with_owner()
803  * or gtk_clipboard_set_with_data(),
804  * and when the @clear_func you supplied is called. Otherwise, the
805  * clipboard may be owned by someone else.
806  **/
807 void
808 gtk_clipboard_clear (GtkClipboard *clipboard)
809 {
810   g_return_if_fail (clipboard != NULL);
811
812   if (clipboard->have_selection)
813     gtk_selection_owner_set_for_display (clipboard->display, 
814                                          NULL,
815                                          clipboard->selection,
816                                          clipboard_get_timestamp (clipboard));
817 }
818
819 static void 
820 text_get_func (GtkClipboard     *clipboard,
821                GtkSelectionData *selection_data,
822                guint             info,
823                gpointer          data)
824 {
825   gtk_selection_data_set_text (selection_data, data, -1);
826 }
827
828 static void 
829 text_clear_func (GtkClipboard *clipboard,
830                  gpointer      data)
831 {
832   g_free (data);
833 }
834
835
836 /**
837  * gtk_clipboard_set_text:
838  * @clipboard: a #GtkClipboard object
839  * @text:      a UTF-8 string.
840  * @len:       length of @text, in bytes, or -1, in which case
841  *             the length will be determined with <function>strlen()</function>.
842  * 
843  * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
844  * make a copy of the text and take responsibility for responding
845  * for requests for the text, and for converting the text into
846  * the requested format.
847  **/
848 void 
849 gtk_clipboard_set_text (GtkClipboard *clipboard,
850                         const gchar  *text,
851                         gint          len)
852 {
853   GtkTargetList *list;
854   GtkTargetEntry *targets;
855   gint n_targets;
856
857   g_return_if_fail (clipboard != NULL);
858   g_return_if_fail (text != NULL);
859
860   list = gtk_target_list_new (NULL, 0);
861   gtk_target_list_add_text_targets (list, 0);
862
863   targets = gtk_target_table_new_from_list (list, &n_targets);
864   
865   if (len < 0)
866     len = strlen (text);
867   
868   gtk_clipboard_set_with_data (clipboard, 
869                                targets, n_targets,
870                                text_get_func, text_clear_func,
871                                g_strndup (text, len));
872   gtk_clipboard_set_can_store (clipboard, NULL, 0);
873
874   gtk_target_table_free (targets, n_targets);
875   gtk_target_list_unref (list);
876 }
877
878 static void 
879 pixbuf_get_func (GtkClipboard     *clipboard,
880                  GtkSelectionData *selection_data,
881                  guint             info,
882                  gpointer          data)
883 {
884   gtk_selection_data_set_pixbuf (selection_data, data);
885 }
886
887 static void 
888 pixbuf_clear_func (GtkClipboard *clipboard,
889                    gpointer      data)
890 {
891   g_object_unref (data);
892 }
893
894 /**
895  * gtk_clipboard_set_image:
896  * @clipboard: a #GtkClipboard object
897  * @pixbuf:    a #GdkPixbuf 
898  * 
899  * Sets the contents of the clipboard to the given #GdkPixbuf. 
900  * GTK+ will take responsibility for responding for requests 
901  * for the image, and for converting the image into the 
902  * requested format.
903  * 
904  * Since: 2.6
905  **/
906 void
907 gtk_clipboard_set_image (GtkClipboard *clipboard,
908                           GdkPixbuf    *pixbuf)
909 {
910   GtkTargetList *list;
911   GtkTargetEntry *targets;
912   gint n_targets;
913
914   g_return_if_fail (clipboard != NULL);
915   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
916
917   list = gtk_target_list_new (NULL, 0);
918   gtk_target_list_add_image_targets (list, 0, TRUE);
919
920   targets = gtk_target_table_new_from_list (list, &n_targets);
921
922   gtk_clipboard_set_with_data (clipboard, 
923                                targets, n_targets,
924                                pixbuf_get_func, pixbuf_clear_func,
925                                g_object_ref (pixbuf));
926   gtk_clipboard_set_can_store (clipboard, NULL, 0);
927
928   gtk_target_table_free (targets, n_targets);
929   gtk_target_list_unref (list);
930 }
931
932 static void
933 set_request_contents_info (GtkWidget           *widget,
934                            RequestContentsInfo *info)
935 {
936   if (!request_contents_key_id)
937     request_contents_key_id = g_quark_from_static_string (request_contents_key);
938
939   g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
940 }
941
942 static RequestContentsInfo *
943 get_request_contents_info (GtkWidget *widget)
944 {
945   if (!request_contents_key_id)
946     return NULL;
947   else
948     return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
949 }
950
951 static void 
952 selection_received (GtkWidget            *widget,
953                     GtkSelectionData     *selection_data,
954                     guint                 time)
955 {
956   RequestContentsInfo *request_info = get_request_contents_info (widget);
957   set_request_contents_info (widget, NULL);
958
959   request_info->callback (gtk_widget_get_clipboard (widget, gtk_selection_data_get_selection (selection_data)),
960                           selection_data,
961                           request_info->user_data);
962
963   g_free (request_info);
964
965   if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
966     gtk_widget_destroy (widget);
967 }
968
969 /**
970  * gtk_clipboard_request_contents:
971  * @clipboard: a #GtkClipboard
972  * @target: an atom representing the form into which the clipboard
973  *     owner should convert the selection.
974  * @callback: (scope async): A function to call when the results are received
975  *     (or the retrieval fails). If the retrieval fails the length field of
976  *     @selection_data will be negative.
977  * @user_data: user data to pass to @callback
978  *
979  * Requests the contents of clipboard as the given target.
980  * When the results of the result are later received the supplied callback
981  * will be called.
982  **/
983 void 
984 gtk_clipboard_request_contents (GtkClipboard            *clipboard,
985                                 GdkAtom                  target,
986                                 GtkClipboardReceivedFunc callback,
987                                 gpointer                 user_data)
988 {
989   RequestContentsInfo *info;
990   GtkWidget *widget;
991   GtkWidget *clipboard_widget;
992
993   g_return_if_fail (clipboard != NULL);
994   g_return_if_fail (target != GDK_NONE);
995   g_return_if_fail (callback != NULL);
996   
997   clipboard_widget = get_clipboard_widget (clipboard->display);
998
999   if (get_request_contents_info (clipboard_widget))
1000     widget = make_clipboard_widget (clipboard->display, FALSE);
1001   else
1002     widget = clipboard_widget;
1003
1004   info = g_new (RequestContentsInfo, 1);
1005   info->callback = callback;
1006   info->user_data = user_data;
1007
1008   set_request_contents_info (widget, info);
1009
1010   gtk_selection_convert (widget, clipboard->selection, target,
1011                          clipboard_get_timestamp (clipboard));
1012 }
1013
1014 static void 
1015 request_text_received_func (GtkClipboard     *clipboard,
1016                             GtkSelectionData *selection_data,
1017                             gpointer          data)
1018 {
1019   RequestTextInfo *info = data;
1020   gchar *result = NULL;
1021
1022   result = (gchar *) gtk_selection_data_get_text (selection_data);
1023
1024   if (!result)
1025     {
1026       /* If we asked for UTF8 and didn't get it, try compound_text;
1027        * if we asked for compound_text and didn't get it, try string;
1028        * If we asked for anything else and didn't get it, give up.
1029        */
1030       GdkAtom target = gtk_selection_data_get_target (selection_data);
1031
1032       if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
1033         {
1034           gtk_clipboard_request_contents (clipboard,
1035                                           gdk_atom_intern_static_string ("COMPOUND_TEXT"), 
1036                                           request_text_received_func, info);
1037           return;
1038         }
1039       else if (target == gdk_atom_intern_static_string ("COMPOUND_TEXT"))
1040         {
1041           gtk_clipboard_request_contents (clipboard,
1042                                           GDK_TARGET_STRING, 
1043                                           request_text_received_func, info);
1044           return;
1045         }
1046     }
1047
1048   info->callback (clipboard, result, info->user_data);
1049   g_free (info);
1050   g_free (result);
1051 }
1052
1053 /**
1054  * gtk_clipboard_request_text:
1055  * @clipboard: a #GtkClipboard
1056  * @callback: (scope async): a function to call when the text is received,
1057  *     or the retrieval fails. (It will always be called one way or the other.)
1058  * @user_data: user data to pass to @callback.
1059  *
1060  * Requests the contents of the clipboard as text. When the text is
1061  * later received, it will be converted to UTF-8 if necessary, and
1062  * @callback will be called.
1063  *
1064  * The @text parameter to @callback will contain the resulting text if
1065  * the request succeeded, or %NULL if it failed. This could happen for
1066  * various reasons, in particular if the clipboard was empty or if the
1067  * contents of the clipboard could not be converted into text form.
1068  **/
1069 void 
1070 gtk_clipboard_request_text (GtkClipboard                *clipboard,
1071                             GtkClipboardTextReceivedFunc callback,
1072                             gpointer                     user_data)
1073 {
1074   RequestTextInfo *info;
1075   
1076   g_return_if_fail (clipboard != NULL);
1077   g_return_if_fail (callback != NULL);
1078   
1079   info = g_new (RequestTextInfo, 1);
1080   info->callback = callback;
1081   info->user_data = user_data;
1082
1083   gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("UTF8_STRING"),
1084                                   request_text_received_func,
1085                                   info);
1086 }
1087
1088 static void
1089 request_rich_text_received_func (GtkClipboard     *clipboard,
1090                                  GtkSelectionData *selection_data,
1091                                  gpointer          data)
1092 {
1093   RequestRichTextInfo *info = data;
1094   guint8 *result = NULL;
1095   gsize length = 0;
1096
1097   result = (guint8 *) gtk_selection_data_get_data (selection_data);
1098   length = gtk_selection_data_get_length (selection_data);
1099
1100   info->current_atom++;
1101
1102   if ((!result || length < 1) && (info->current_atom < info->n_atoms))
1103     {
1104       gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1105                                       request_rich_text_received_func,
1106                                       info);
1107       return;
1108     }
1109
1110   info->callback (clipboard, gtk_selection_data_get_target (selection_data),
1111                   result, length,
1112                   info->user_data);
1113   g_free (info->atoms);
1114   g_free (info);
1115 }
1116
1117 /**
1118  * gtk_clipboard_request_rich_text:
1119  * @clipboard: a #GtkClipboard
1120  * @buffer: a #GtkTextBuffer
1121  * @callback: (scope async): a function to call when the text is received,
1122  *     or the retrieval fails. (It will always be called one way or the other.)
1123  * @user_data: user data to pass to @callback.
1124  *
1125  * Requests the contents of the clipboard as rich text. When the rich
1126  * text is later received, @callback will be called.
1127  *
1128  * The @text parameter to @callback will contain the resulting rich
1129  * text if the request succeeded, or %NULL if it failed. The @length
1130  * parameter will contain @text's length. This function can fail for
1131  * various reasons, in particular if the clipboard was empty or if the
1132  * contents of the clipboard could not be converted into rich text form.
1133  *
1134  * Since: 2.10
1135  **/
1136 void
1137 gtk_clipboard_request_rich_text (GtkClipboard                    *clipboard,
1138                                  GtkTextBuffer                   *buffer,
1139                                  GtkClipboardRichTextReceivedFunc callback,
1140                                  gpointer                         user_data)
1141 {
1142   RequestRichTextInfo *info;
1143
1144   g_return_if_fail (clipboard != NULL);
1145   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1146   g_return_if_fail (callback != NULL);
1147
1148   info = g_new (RequestRichTextInfo, 1);
1149   info->callback = callback;
1150   info->atoms = NULL;
1151   info->n_atoms = 0;
1152   info->current_atom = 0;
1153   info->user_data = user_data;
1154
1155   info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
1156
1157   gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1158                                   request_rich_text_received_func,
1159                                   info);
1160 }
1161
1162 static void 
1163 request_image_received_func (GtkClipboard     *clipboard,
1164                              GtkSelectionData *selection_data,
1165                              gpointer          data)
1166 {
1167   RequestImageInfo *info = data;
1168   GdkPixbuf *result = NULL;
1169
1170   result = gtk_selection_data_get_pixbuf (selection_data);
1171
1172   if (!result)
1173     {
1174       /* If we asked for image/png and didn't get it, try image/jpeg;
1175        * if we asked for image/jpeg and didn't get it, try image/gif;
1176        * if we asked for image/gif and didn't get it, try image/bmp;
1177        * If we asked for anything else and didn't get it, give up.
1178        */
1179       GdkAtom target = gtk_selection_data_get_target (selection_data);
1180
1181       if (target == gdk_atom_intern_static_string ("image/png"))
1182         {
1183           gtk_clipboard_request_contents (clipboard,
1184                                           gdk_atom_intern_static_string ("image/jpeg"), 
1185                                           request_image_received_func, info);
1186           return;
1187         }
1188       else if (target == gdk_atom_intern_static_string ("image/jpeg"))
1189         {
1190           gtk_clipboard_request_contents (clipboard,
1191                                           gdk_atom_intern_static_string ("image/gif"), 
1192                                           request_image_received_func, info);
1193           return;
1194         }
1195       else if (target == gdk_atom_intern_static_string ("image/gif"))
1196         {
1197           gtk_clipboard_request_contents (clipboard,
1198                                           gdk_atom_intern_static_string ("image/bmp"), 
1199                                           request_image_received_func, info);
1200           return;
1201         }
1202     }
1203
1204   info->callback (clipboard, result, info->user_data);
1205   g_free (info);
1206
1207   if (result)
1208     g_object_unref (result);
1209 }
1210
1211 /**
1212  * gtk_clipboard_request_image:
1213  * @clipboard: a #GtkClipboard
1214  * @callback: (scope async): a function to call when the image is received,
1215  *     or the retrieval fails. (It will always be called one way or the other.)
1216  * @user_data: user data to pass to @callback.
1217  *
1218  * Requests the contents of the clipboard as image. When the image is
1219  * later received, it will be converted to a #GdkPixbuf, and
1220  * @callback will be called.
1221  *
1222  * The @pixbuf parameter to @callback will contain the resulting
1223  * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1224  * could happen for various reasons, in particular if the clipboard
1225  * was empty or if the contents of the clipboard could not be
1226  * converted into an image.
1227  *
1228  * Since: 2.6
1229  **/
1230 void 
1231 gtk_clipboard_request_image (GtkClipboard                  *clipboard,
1232                              GtkClipboardImageReceivedFunc  callback,
1233                              gpointer                       user_data)
1234 {
1235   RequestImageInfo *info;
1236   
1237   g_return_if_fail (clipboard != NULL);
1238   g_return_if_fail (callback != NULL);
1239   
1240   info = g_new (RequestImageInfo, 1);
1241   info->callback = callback;
1242   info->user_data = user_data;
1243
1244   gtk_clipboard_request_contents (clipboard, 
1245                                   gdk_atom_intern_static_string ("image/png"),
1246                                   request_image_received_func,
1247                                   info);
1248 }
1249
1250 static void 
1251 request_uris_received_func (GtkClipboard     *clipboard,
1252                             GtkSelectionData *selection_data,
1253                             gpointer          data)
1254 {
1255   RequestURIInfo *info = data;
1256   gchar **uris;
1257
1258   uris = gtk_selection_data_get_uris (selection_data);
1259   info->callback (clipboard, uris, info->user_data);
1260   g_strfreev (uris);
1261
1262   g_slice_free (RequestURIInfo, info);
1263 }
1264
1265 /**
1266  * gtk_clipboard_request_uris:
1267  * @clipboard: a #GtkClipboard
1268  * @callback: (scope async): a function to call when the URIs are received,
1269  *     or the retrieval fails. (It will always be called one way or the other.)
1270  * @user_data: user data to pass to @callback.
1271  *
1272  * Requests the contents of the clipboard as URIs. When the URIs are
1273  * later received @callback will be called.
1274  *
1275  * The @uris parameter to @callback will contain the resulting array of
1276  * URIs if the request succeeded, or %NULL if it failed. This could happen
1277  * for various reasons, in particular if the clipboard was empty or if the
1278  * contents of the clipboard could not be converted into URI form.
1279  *
1280  * Since: 2.14
1281  **/
1282 void 
1283 gtk_clipboard_request_uris (GtkClipboard                *clipboard,
1284                             GtkClipboardURIReceivedFunc  callback,
1285                             gpointer                     user_data)
1286 {
1287   RequestURIInfo *info;
1288   
1289   g_return_if_fail (clipboard != NULL);
1290   g_return_if_fail (callback != NULL);
1291   
1292   info = g_slice_new (RequestURIInfo);
1293   info->callback = callback;
1294   info->user_data = user_data;
1295
1296   gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"),
1297                                   request_uris_received_func,
1298                                   info);
1299 }
1300
1301 static void 
1302 request_targets_received_func (GtkClipboard     *clipboard,
1303                                GtkSelectionData *selection_data,
1304                                gpointer          data)
1305 {
1306   RequestTargetsInfo *info = data;
1307   GdkAtom *targets = NULL;
1308   gint n_targets = 0;
1309
1310   gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1311
1312   info->callback (clipboard, targets, n_targets, info->user_data);
1313
1314   g_free (info);
1315   g_free (targets);
1316 }
1317
1318 /**
1319  * gtk_clipboard_request_targets:
1320  * @clipboard: a #GtkClipboard
1321  * @callback: (scope async): a function to call when the targets are
1322  *     received, or the retrieval fails. (It will always be called
1323  *     one way or the other.)
1324  * @user_data: user data to pass to @callback.
1325  *
1326  * Requests the contents of the clipboard as list of supported targets.
1327  * When the list is later received, @callback will be called.
1328  *
1329  * The @targets parameter to @callback will contain the resulting targets if
1330  * the request succeeded, or %NULL if it failed.
1331  *
1332  * Since: 2.4
1333  **/
1334 void 
1335 gtk_clipboard_request_targets (GtkClipboard                *clipboard,
1336                                GtkClipboardTargetsReceivedFunc callback,
1337                                gpointer                     user_data)
1338 {
1339   RequestTargetsInfo *info;
1340   
1341   g_return_if_fail (clipboard != NULL);
1342   g_return_if_fail (callback != NULL);
1343
1344   /* If the display supports change notification we cache targets */
1345   if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1346       clipboard->n_cached_targets != -1)
1347     {
1348       (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1349       return;
1350     }
1351   
1352   info = g_new (RequestTargetsInfo, 1);
1353   info->callback = callback;
1354   info->user_data = user_data;
1355
1356   gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"),
1357                                   request_targets_received_func,
1358                                   info);
1359 }
1360
1361 typedef struct
1362 {
1363   GMainLoop *loop;
1364   gpointer data;
1365   GdkAtom format; /* used by rich text */
1366   gsize length; /* used by rich text */
1367 } WaitResults;
1368
1369 static void 
1370 clipboard_received_func (GtkClipboard     *clipboard,
1371                          GtkSelectionData *selection_data,
1372                          gpointer          data)
1373 {
1374   WaitResults *results = data;
1375
1376   if (gtk_selection_data_get_length (selection_data) >= 0)
1377     results->data = gtk_selection_data_copy (selection_data);
1378   
1379   g_main_loop_quit (results->loop);
1380 }
1381
1382 /**
1383  * gtk_clipboard_wait_for_contents:
1384  * @clipboard: a #GtkClipboard
1385  * @target: an atom representing the form into which the clipboard
1386  *          owner should convert the selection.
1387  * 
1388  * Requests the contents of the clipboard using the given target.
1389  * This function waits for the data to be received using the main 
1390  * loop, so events, timeouts, etc, may be dispatched during the wait.
1391  * 
1392  * Return value: a newly-allocated #GtkSelectionData object or %NULL
1393  *               if retrieving the given target failed. If non-%NULL,
1394  *               this value must be freed with gtk_selection_data_free() 
1395  *               when you are finished with it.
1396  **/
1397 GtkSelectionData *
1398 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1399                                  GdkAtom       target)
1400 {
1401   WaitResults results;
1402
1403   g_return_val_if_fail (clipboard != NULL, NULL);
1404   g_return_val_if_fail (target != GDK_NONE, NULL);
1405   
1406   results.data = NULL;
1407   results.loop = g_main_loop_new (NULL, TRUE);
1408
1409   gtk_clipboard_request_contents (clipboard, target, 
1410                                   clipboard_received_func,
1411                                   &results);
1412
1413   if (g_main_loop_is_running (results.loop))
1414     {
1415       GDK_THREADS_LEAVE ();
1416       g_main_loop_run (results.loop);
1417       GDK_THREADS_ENTER ();
1418     }
1419
1420   g_main_loop_unref (results.loop);
1421
1422   return results.data;
1423 }
1424
1425 static void 
1426 clipboard_text_received_func (GtkClipboard *clipboard,
1427                               const gchar  *text,
1428                               gpointer      data)
1429 {
1430   WaitResults *results = data;
1431
1432   results->data = g_strdup (text);
1433   g_main_loop_quit (results->loop);
1434 }
1435
1436 /**
1437  * gtk_clipboard_wait_for_text:
1438  * @clipboard: a #GtkClipboard
1439  * 
1440  * Requests the contents of the clipboard as text and converts
1441  * the result to UTF-8 if necessary. This function waits for
1442  * the data to be received using the main loop, so events,
1443  * timeouts, etc, may be dispatched during the wait.
1444  * 
1445  * Return value: a newly-allocated UTF-8 string which must
1446  *               be freed with g_free(), or %NULL if retrieving
1447  *               the selection data failed. (This could happen
1448  *               for various reasons, in particular if the
1449  *               clipboard was empty or if the contents of the
1450  *               clipboard could not be converted into text form.)
1451  **/
1452 gchar *
1453 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1454 {
1455   WaitResults results;
1456
1457   g_return_val_if_fail (clipboard != NULL, NULL);
1458   
1459   results.data = NULL;
1460   results.loop = g_main_loop_new (NULL, TRUE);
1461
1462   gtk_clipboard_request_text (clipboard,
1463                               clipboard_text_received_func,
1464                               &results);
1465
1466   if (g_main_loop_is_running (results.loop))
1467     {
1468       GDK_THREADS_LEAVE ();
1469       g_main_loop_run (results.loop);
1470       GDK_THREADS_ENTER ();
1471     }
1472
1473   g_main_loop_unref (results.loop);
1474
1475   return results.data;
1476 }
1477
1478 static void
1479 clipboard_rich_text_received_func (GtkClipboard *clipboard,
1480                                    GdkAtom       format,
1481                                    const guint8 *text,
1482                                    gsize         length,
1483                                    gpointer      data)
1484 {
1485   WaitResults *results = data;
1486
1487   results->data = g_memdup (text, length);
1488   results->format = format;
1489   results->length = length;
1490   g_main_loop_quit (results->loop);
1491 }
1492
1493 /**
1494  * gtk_clipboard_wait_for_rich_text:
1495  * @clipboard: a #GtkClipboard
1496  * @buffer: a #GtkTextBuffer
1497  * @format: (out): return location for the format of the returned data
1498  * @length: return location for the length of the returned data
1499  *
1500  * Requests the contents of the clipboard as rich text.  This function
1501  * waits for the data to be received using the main loop, so events,
1502  * timeouts, etc, may be dispatched during the wait.
1503  *
1504  * Return value: (array length=length) (transfer full): a
1505  *               newly-allocated binary block of data which must be
1506  *               freed with g_free(), or %NULL if retrieving the
1507  *               selection data failed. (This could happen for various
1508  *               reasons, in particular if the clipboard was empty or
1509  *               if the contents of the clipboard could not be
1510  *               converted into text form.)
1511  *
1512  * Since: 2.10
1513  **/
1514 guint8 *
1515 gtk_clipboard_wait_for_rich_text (GtkClipboard  *clipboard,
1516                                   GtkTextBuffer *buffer,
1517                                   GdkAtom       *format,
1518                                   gsize         *length)
1519 {
1520   WaitResults results;
1521
1522   g_return_val_if_fail (clipboard != NULL, NULL);
1523   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1524   g_return_val_if_fail (format != NULL, NULL);
1525   g_return_val_if_fail (length != NULL, NULL);
1526
1527   results.data = NULL;
1528   results.loop = g_main_loop_new (NULL, TRUE);
1529
1530   gtk_clipboard_request_rich_text (clipboard, buffer,
1531                                    clipboard_rich_text_received_func,
1532                                    &results);
1533
1534   if (g_main_loop_is_running (results.loop))
1535     {
1536       GDK_THREADS_LEAVE ();
1537       g_main_loop_run (results.loop);
1538       GDK_THREADS_ENTER ();
1539     }
1540
1541   g_main_loop_unref (results.loop);
1542
1543   *format = results.format;
1544   *length = results.length;
1545
1546   return results.data;
1547 }
1548
1549 static void 
1550 clipboard_image_received_func (GtkClipboard *clipboard,
1551                                GdkPixbuf    *pixbuf,
1552                                gpointer      data)
1553 {
1554   WaitResults *results = data;
1555
1556   if (pixbuf)
1557     results->data = g_object_ref (pixbuf);
1558
1559   g_main_loop_quit (results->loop);
1560 }
1561
1562 /**
1563  * gtk_clipboard_wait_for_image:
1564  * @clipboard: a #GtkClipboard
1565  *
1566  * Requests the contents of the clipboard as image and converts
1567  * the result to a #GdkPixbuf. This function waits for
1568  * the data to be received using the main loop, so events,
1569  * timeouts, etc, may be dispatched during the wait.
1570  *
1571  * Return value: (transfer full): a newly-allocated #GdkPixbuf
1572  *     object which must be disposed with g_object_unref(), or
1573  *     %NULL if retrieving the selection data failed. (This could
1574  *     happen for various reasons, in particular if the clipboard
1575  *     was empty or if the contents of the clipboard could not be
1576  *     converted into an image.)
1577  *
1578  * Since: 2.6
1579  **/
1580 GdkPixbuf *
1581 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1582 {
1583   WaitResults results;
1584
1585   g_return_val_if_fail (clipboard != NULL, NULL);
1586   
1587   results.data = NULL;
1588   results.loop = g_main_loop_new (NULL, TRUE);
1589
1590   gtk_clipboard_request_image (clipboard,
1591                                clipboard_image_received_func,
1592                                &results);
1593
1594   if (g_main_loop_is_running (results.loop))
1595     {
1596       GDK_THREADS_LEAVE ();
1597       g_main_loop_run (results.loop);
1598       GDK_THREADS_ENTER ();
1599     }
1600
1601   g_main_loop_unref (results.loop);
1602
1603   return results.data;
1604 }
1605
1606 static void 
1607 clipboard_uris_received_func (GtkClipboard *clipboard,
1608                               gchar       **uris,
1609                               gpointer      data)
1610 {
1611   WaitResults *results = data;
1612
1613   results->data = g_strdupv (uris);
1614   g_main_loop_quit (results->loop);
1615 }
1616
1617 /**
1618  * gtk_clipboard_wait_for_uris:
1619  * @clipboard: a #GtkClipboard
1620  * 
1621  * Requests the contents of the clipboard as URIs. This function waits
1622  * for the data to be received using the main loop, so events,
1623  * timeouts, etc, may be dispatched during the wait.
1624  *
1625  * Return value: (array zero-terminated=1) (element-type utf8) (transfer full): a newly-allocated
1626  *               %NULL-terminated array of strings which must
1627  *               be freed with g_strfreev(), or %NULL if
1628  *               retrieving the selection data failed. (This
1629  *               could happen for various reasons, in particular
1630  *               if the clipboard was empty or if the contents of
1631  *               the clipboard could not be converted into URI form.)
1632  *
1633  * Since: 2.14
1634  **/
1635 gchar **
1636 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
1637 {
1638   WaitResults results;
1639
1640   g_return_val_if_fail (clipboard != NULL, NULL);
1641   
1642   results.data = NULL;
1643   results.loop = g_main_loop_new (NULL, TRUE);
1644
1645   gtk_clipboard_request_uris (clipboard,
1646                               clipboard_uris_received_func,
1647                               &results);
1648
1649   if (g_main_loop_is_running (results.loop))
1650     {
1651       GDK_THREADS_LEAVE ();
1652       g_main_loop_run (results.loop);
1653       GDK_THREADS_ENTER ();
1654     }
1655
1656   g_main_loop_unref (results.loop);
1657
1658   return results.data;
1659 }
1660
1661 /**
1662  * gtk_clipboard_get_display:
1663  * @clipboard: a #GtkClipboard
1664  *
1665  * Gets the #GdkDisplay associated with @clipboard
1666  *
1667  * Return value: (transfer none): the #GdkDisplay associated with @clipboard
1668  *
1669  * Since: 2.2
1670  **/
1671 GdkDisplay *
1672 gtk_clipboard_get_display (GtkClipboard *clipboard)
1673 {
1674   g_return_val_if_fail (clipboard != NULL, NULL);
1675
1676   return clipboard->display;
1677 }
1678
1679 /**
1680  * gtk_clipboard_wait_is_text_available:
1681  * @clipboard: a #GtkClipboard
1682  * 
1683  * Test to see if there is text available to be pasted
1684  * This is done by requesting the TARGETS atom and checking
1685  * if it contains any of the supported text targets. This function 
1686  * waits for the data to be received using the main loop, so events, 
1687  * timeouts, etc, may be dispatched during the wait.
1688  *
1689  * This function is a little faster than calling
1690  * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1691  * the actual text.
1692  * 
1693  * Return value: %TRUE is there is text available, %FALSE otherwise.
1694  **/
1695 gboolean
1696 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1697 {
1698   GtkSelectionData *data;
1699   gboolean result = FALSE;
1700
1701   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1702   if (data)
1703     {
1704       result = gtk_selection_data_targets_include_text (data);
1705       gtk_selection_data_free (data);
1706     }
1707
1708   return result;
1709 }
1710
1711 /**
1712  * gtk_clipboard_wait_is_rich_text_available:
1713  * @clipboard: a #GtkClipboard
1714  * @buffer: a #GtkTextBuffer
1715  *
1716  * Test to see if there is rich text available to be pasted
1717  * This is done by requesting the TARGETS atom and checking
1718  * if it contains any of the supported rich text targets. This function
1719  * waits for the data to be received using the main loop, so events,
1720  * timeouts, etc, may be dispatched during the wait.
1721  *
1722  * This function is a little faster than calling
1723  * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
1724  * the actual text.
1725  *
1726  * Return value: %TRUE is there is rich text available, %FALSE otherwise.
1727  *
1728  * Since: 2.10
1729  **/
1730 gboolean
1731 gtk_clipboard_wait_is_rich_text_available (GtkClipboard  *clipboard,
1732                                            GtkTextBuffer *buffer)
1733 {
1734   GtkSelectionData *data;
1735   gboolean result = FALSE;
1736
1737   g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1738   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1739
1740   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1741   if (data)
1742     {
1743       result = gtk_selection_data_targets_include_rich_text (data, buffer);
1744       gtk_selection_data_free (data);
1745     }
1746
1747   return result;
1748 }
1749
1750 /**
1751  * gtk_clipboard_wait_is_image_available:
1752  * @clipboard: a #GtkClipboard
1753  * 
1754  * Test to see if there is an image available to be pasted
1755  * This is done by requesting the TARGETS atom and checking
1756  * if it contains any of the supported image targets. This function 
1757  * waits for the data to be received using the main loop, so events, 
1758  * timeouts, etc, may be dispatched during the wait.
1759  *
1760  * This function is a little faster than calling
1761  * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1762  * the actual image data.
1763  * 
1764  * Return value: %TRUE is there is an image available, %FALSE otherwise.
1765  *
1766  * Since: 2.6
1767  **/
1768 gboolean
1769 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1770 {
1771   GtkSelectionData *data;
1772   gboolean result = FALSE;
1773
1774   data = gtk_clipboard_wait_for_contents (clipboard, 
1775                                           gdk_atom_intern_static_string ("TARGETS"));
1776   if (data)
1777     {
1778       result = gtk_selection_data_targets_include_image (data, FALSE);
1779       gtk_selection_data_free (data);
1780     }
1781
1782   return result;
1783 }
1784
1785 /**
1786  * gtk_clipboard_wait_is_uris_available:
1787  * @clipboard: a #GtkClipboard
1788  * 
1789  * Test to see if there is a list of URIs available to be pasted
1790  * This is done by requesting the TARGETS atom and checking
1791  * if it contains the URI targets. This function
1792  * waits for the data to be received using the main loop, so events, 
1793  * timeouts, etc, may be dispatched during the wait.
1794  *
1795  * This function is a little faster than calling
1796  * gtk_clipboard_wait_for_uris() since it doesn't need to retrieve
1797  * the actual URI data.
1798  * 
1799  * Return value: %TRUE is there is an URI list available, %FALSE otherwise.
1800  *
1801  * Since: 2.14
1802  **/
1803 gboolean
1804 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
1805 {
1806   GtkSelectionData *data;
1807   gboolean result = FALSE;
1808
1809   data = gtk_clipboard_wait_for_contents (clipboard, 
1810                                           gdk_atom_intern_static_string ("TARGETS"));
1811   if (data)
1812     {
1813       result = gtk_selection_data_targets_include_uri (data);
1814       gtk_selection_data_free (data);
1815     }
1816
1817   return result;
1818 }
1819
1820 /**
1821  * gtk_clipboard_wait_for_targets
1822  * @clipboard: a #GtkClipboard
1823  * @targets: (out) (array length=n_targets) (transfer container): location
1824  *           to store an array of targets. The result stored here must
1825  *           be freed with g_free().
1826  * @n_targets: location to store number of items in @targets.
1827  *
1828  * Returns a list of targets that are present on the clipboard, or %NULL
1829  * if there aren't any targets available. The returned list must be 
1830  * freed with g_free().
1831  * This function waits for the data to be received using the main 
1832  * loop, so events, timeouts, etc, may be dispatched during the wait.
1833  *
1834  * Return value: %TRUE if any targets are present on the clipboard,
1835  *               otherwise %FALSE.
1836  *
1837  * Since: 2.4
1838  */
1839 gboolean
1840 gtk_clipboard_wait_for_targets (GtkClipboard  *clipboard, 
1841                                 GdkAtom      **targets,
1842                                 gint          *n_targets)
1843 {
1844   GtkSelectionData *data;
1845   gboolean result = FALSE;
1846   
1847   g_return_val_if_fail (clipboard != NULL, FALSE);
1848
1849   /* If the display supports change notification we cache targets */
1850   if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1851       clipboard->n_cached_targets != -1)
1852     {
1853       if (n_targets)
1854         *n_targets = clipboard->n_cached_targets;
1855  
1856       if (targets)
1857         *targets = g_memdup (clipboard->cached_targets,
1858                              clipboard->n_cached_targets * sizeof (GdkAtom));
1859
1860        return TRUE;
1861     }
1862   
1863   if (n_targets)
1864     *n_targets = 0;
1865       
1866   if (targets)
1867     *targets = NULL;      
1868
1869   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1870
1871   if (data)
1872     {
1873       GdkAtom *tmp_targets;
1874       gint tmp_n_targets;
1875        
1876       result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1877  
1878       if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1879         {
1880           clipboard->n_cached_targets = tmp_n_targets;
1881           clipboard->cached_targets = g_memdup (tmp_targets,
1882                                                 tmp_n_targets * sizeof (GdkAtom));
1883         }
1884  
1885       if (n_targets)
1886         *n_targets = tmp_n_targets;
1887  
1888       if (targets)
1889         *targets = tmp_targets;
1890       else
1891         g_free (tmp_targets);
1892       
1893       gtk_selection_data_free (data);
1894     }
1895
1896   return result;
1897 }
1898
1899 static GtkClipboard *
1900 clipboard_peek (GdkDisplay *display, 
1901                 GdkAtom     selection,
1902                 gboolean    only_if_exists)
1903 {
1904   GtkClipboard *clipboard = NULL;
1905   GSList *clipboards;
1906   GSList *tmp_list;
1907
1908   if (selection == GDK_NONE)
1909     selection = GDK_SELECTION_CLIPBOARD;
1910
1911   clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1912
1913   tmp_list = clipboards;
1914   while (tmp_list)
1915     {
1916       clipboard = tmp_list->data;
1917       if (clipboard->selection == selection)
1918         break;
1919
1920       tmp_list = tmp_list->next;
1921     }
1922
1923   if (!tmp_list && !only_if_exists)
1924     {
1925       clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1926       clipboard->selection = selection;
1927       clipboard->display = display;
1928       clipboard->n_cached_targets = -1;
1929       clipboard->n_storable_targets = -1;
1930       clipboards = g_slist_prepend (clipboards, clipboard);
1931       g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1932       g_signal_connect (display, "closed",
1933                         G_CALLBACK (clipboard_display_closed), clipboard);
1934       gdk_display_request_selection_notification (display, selection);
1935     }
1936   
1937   return clipboard;
1938 }
1939
1940 static void
1941 gtk_clipboard_owner_change (GtkClipboard        *clipboard,
1942                             GdkEventOwnerChange *event)
1943 {
1944   if (clipboard->n_cached_targets != -1)
1945     {
1946       g_free (clipboard->cached_targets);
1947       clipboard->cached_targets = NULL;
1948       clipboard->n_cached_targets = -1;
1949     }
1950 }
1951
1952 /**
1953  * gtk_clipboard_wait_is_target_available:
1954  * @clipboard: a #GtkClipboard
1955  * @target:    A #GdkAtom indicating which target to look for.
1956  *
1957  * Checks if a clipboard supports pasting data of a given type. This
1958  * function can be used to determine if a "Paste" menu item should be
1959  * insensitive or not.
1960  *
1961  * If you want to see if there's text available on the clipboard, use
1962  * gtk_clipboard_wait_is_text_available () instead.
1963  *
1964  * Return value: %TRUE if the target is available, %FALSE otherwise.
1965  *
1966  * Since: 2.6
1967  */
1968 gboolean
1969 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1970                                         GdkAtom       target)
1971 {
1972   GdkAtom *targets;
1973   gint i, n_targets;
1974   gboolean retval = FALSE;
1975     
1976   if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1977     return FALSE;
1978
1979   for (i = 0; i < n_targets; i++)
1980     {
1981       if (targets[i] == target)
1982         {
1983           retval = TRUE;
1984           break;
1985         }
1986     }
1987
1988   g_free (targets);
1989   
1990   return retval;
1991 }
1992
1993 /**
1994  * _gtk_clipboard_handle_event:
1995  * @event: a owner change event
1996  * 
1997  * Emits the #GtkClipboard::owner-change signal on the appropriate @clipboard.
1998  *
1999  * Since: 2.6
2000  **/
2001 void 
2002 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
2003 {
2004   GdkDisplay *display;
2005   GtkClipboard *clipboard;
2006   
2007   display = gdk_window_get_display (event->window);
2008   clipboard = clipboard_peek (display, event->selection, TRUE);
2009       
2010   if (clipboard)
2011     g_signal_emit (clipboard, 
2012                    clipboard_signals[OWNER_CHANGE], 0, event, NULL);
2013 }
2014
2015 static gboolean
2016 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
2017 {
2018   g_main_loop_quit (clipboard->store_loop);
2019   
2020   return FALSE;
2021 }
2022
2023 /**
2024  * gtk_clipboard_set_can_store:
2025  * @clipboard: a #GtkClipboard
2026  * @targets: (allow-none) (array length=n_targets): array containing
2027  *           information about which forms should be stored or %NULL
2028  *           to indicate that all forms should be stored.
2029  * @n_targets: number of elements in @targets
2030  *
2031  * Hints that the clipboard data should be stored somewhere when the
2032  * application exits or when gtk_clipboard_store () is called.
2033  *
2034  * This value is reset when the clipboard owner changes.
2035  * Where the clipboard data is stored is platform dependent,
2036  * see gdk_display_store_clipboard () for more information.
2037  * 
2038  * Since: 2.6
2039  */
2040 void
2041 gtk_clipboard_set_can_store (GtkClipboard         *clipboard,
2042                              const GtkTargetEntry *targets,
2043                              gint                  n_targets)
2044 {
2045   GtkWidget *clipboard_widget;
2046   int i;
2047   static const GtkTargetEntry save_targets[] = {
2048     { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
2049   };
2050   
2051   g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2052   g_return_if_fail (n_targets >= 0);
2053
2054   if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
2055     return;
2056   
2057   g_free (clipboard->storable_targets);
2058   
2059   clipboard_widget = get_clipboard_widget (clipboard->display);
2060
2061   /* n_storable_targets being -1 means that
2062    * gtk_clipboard_set_can_store hasn't been called since the
2063    * clipboard owner changed. We only want to add SAVE_TARGETS and 
2064    * ref the owner once , so we do that here
2065    */  
2066   if (clipboard->n_storable_targets == -1)
2067     {
2068       gtk_selection_add_targets (clipboard_widget, clipboard->selection,
2069                                  save_targets, 1);
2070
2071       /* Ref the owner so it won't go away */
2072       if (clipboard->have_owner)
2073         g_object_ref (clipboard->user_data);
2074     }
2075   
2076   clipboard->n_storable_targets = n_targets;
2077   clipboard->storable_targets = g_new (GdkAtom, n_targets);
2078   for (i = 0; i < n_targets; i++)
2079     clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
2080 }
2081
2082 static gboolean
2083 gtk_clipboard_selection_notify (GtkWidget         *widget,
2084                                 GdkEventSelection *event,
2085                                 GtkClipboard      *clipboard)
2086 {
2087   if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
2088       clipboard->storing_selection)
2089     g_main_loop_quit (clipboard->store_loop);
2090
2091   return FALSE;
2092 }
2093
2094 /**
2095  * gtk_clipboard_store:
2096  * @clipboard: a #GtkClipboard
2097  *
2098  * Stores the current clipboard data somewhere so that it will stay
2099  * around after the application has quit.
2100  *
2101  * Since: 2.6
2102  */
2103 void
2104 gtk_clipboard_store (GtkClipboard *clipboard)
2105 {
2106   GtkWidget *clipboard_widget;
2107
2108   g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
2109
2110   if (clipboard->n_storable_targets < 0)
2111     return;
2112   
2113   if (!gdk_display_supports_clipboard_persistence (clipboard->display))
2114     return;
2115
2116   g_object_ref (clipboard);
2117
2118   clipboard_widget = get_clipboard_widget (clipboard->display);
2119   clipboard->notify_signal_id = g_signal_connect (clipboard_widget,
2120                                                   "selection-notify-event",
2121                                                   G_CALLBACK (gtk_clipboard_selection_notify),
2122                                                   clipboard);
2123
2124   gdk_display_store_clipboard (clipboard->display,
2125                                gtk_widget_get_window (clipboard_widget),
2126                                clipboard_get_timestamp (clipboard),
2127                                clipboard->storable_targets,
2128                                clipboard->n_storable_targets);
2129
2130   clipboard->storing_selection = TRUE;
2131
2132   clipboard->store_loop = g_main_loop_new (NULL, TRUE);
2133   clipboard->store_timeout = g_timeout_add_seconds (10, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
2134
2135   if (g_main_loop_is_running (clipboard->store_loop))
2136     {
2137       GDK_THREADS_LEAVE ();
2138       g_main_loop_run (clipboard->store_loop);
2139       GDK_THREADS_ENTER ();
2140     }
2141   
2142   g_main_loop_unref (clipboard->store_loop);
2143   clipboard->store_loop = NULL;
2144   
2145   g_source_remove (clipboard->store_timeout);
2146   clipboard->store_timeout = 0;
2147   g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
2148   clipboard->notify_signal_id = 0;
2149   
2150   clipboard->storing_selection = FALSE;
2151
2152   g_object_unref (clipboard);
2153 }
2154
2155 /* Stores all clipboard selections on all displays, called from
2156  * gtk_main_quit ().
2157  */
2158 void
2159 _gtk_clipboard_store_all (void)
2160 {
2161   GtkClipboard *clipboard;
2162   GSList *displays, *list;
2163   
2164   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
2165
2166   list = displays;
2167   while (list)
2168     {
2169       GdkDisplay *display = list->data;
2170
2171       clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
2172
2173       if (clipboard)
2174         gtk_clipboard_store (clipboard);
2175       
2176       list = list->next;
2177     }
2178   g_slist_free (displays);
2179   
2180 }