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