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