]> Pileus Git - ~andy/gtk/blob - gtk/gtkclipboard.c
Intern some more strings.
[~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 "gtkintl.h"
31 #include "gtkalias.h"
32
33 #ifdef GDK_WINDOWING_X11
34 #include "x11/gdkx.h"
35 #endif
36
37 #ifdef GDK_WINDOWING_WIN32
38 #include "win32/gdkwin32.h"
39 #endif
40
41 enum {
42   OWNER_CHANGE,
43   LAST_SIGNAL
44 };
45
46 typedef struct _GtkClipboardClass GtkClipboardClass;
47
48 typedef struct _RequestContentsInfo RequestContentsInfo;
49 typedef struct _RequestTextInfo RequestTextInfo;
50 typedef struct _RequestImageInfo RequestImageInfo;
51 typedef struct _RequestTargetsInfo RequestTargetsInfo;
52
53 struct _GtkClipboard 
54 {
55   GObject parent_instance;
56
57   GdkAtom selection;
58
59   GtkClipboardGetFunc get_func;
60   GtkClipboardClearFunc clear_func;
61   gpointer user_data;
62   gboolean have_owner;
63
64   guint32 timestamp;
65
66   gboolean have_selection;
67   GdkDisplay *display;
68
69   GdkAtom *cached_targets;
70   gint     n_cached_targets;
71
72   guint      notify_signal_id;
73   gboolean   storing_selection;
74   GMainLoop *store_loop;
75   guint      store_timeout;
76   gint       n_storable_targets;
77   GdkAtom   *storable_targets;
78 };
79
80 struct _GtkClipboardClass
81 {
82   GObjectClass parent_class;
83
84   void (*owner_change) (GtkClipboard        *clipboard,
85                         GdkEventOwnerChange *event);
86 };
87
88 struct _RequestContentsInfo
89 {
90   GtkClipboardReceivedFunc callback;
91   gpointer user_data;
92 };
93
94 struct _RequestTextInfo
95 {
96   GtkClipboardTextReceivedFunc callback;
97   gpointer user_data;
98 };
99
100 struct _RequestImageInfo
101 {
102   GtkClipboardImageReceivedFunc callback;
103   gpointer user_data;
104 };
105
106 struct _RequestTargetsInfo
107 {
108   GtkClipboardTargetsReceivedFunc callback;
109   gpointer user_data;
110 };
111
112 static void gtk_clipboard_class_init   (GtkClipboardClass   *class);
113 static void gtk_clipboard_finalize     (GObject             *object);
114 static void gtk_clipboard_owner_change (GtkClipboard        *clipboard,
115                                         GdkEventOwnerChange *event);
116
117 static void          clipboard_unset      (GtkClipboard     *clipboard);
118 static void          selection_received   (GtkWidget        *widget,
119                                            GtkSelectionData *selection_data,
120                                            guint             time);
121 static GtkClipboard *clipboard_peek       (GdkDisplay       *display,
122                                            GdkAtom           selection,
123                                            gboolean          only_if_exists);
124 static GtkWidget *   get_clipboard_widget (GdkDisplay       *display);
125
126
127 enum {
128   TARGET_STRING,
129   TARGET_TEXT,
130   TARGET_COMPOUND_TEXT,
131   TARGET_UTF8_STRING,
132   TARGET_SAVE_TARGETS
133 };
134
135 static const gchar request_contents_key[] = "gtk-request-contents";
136 static GQuark request_contents_key_id = 0;
137
138 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
139 static GQuark clipboards_owned_key_id = 0;
140
141 static GObjectClass *parent_class;
142 static guint         clipboard_signals[LAST_SIGNAL] = { 0 };
143
144 GType
145 gtk_clipboard_get_type (void)
146 {
147   static GType clipboard_type = 0;
148   
149   if (!clipboard_type)
150     {
151       static const GTypeInfo clipboard_info =
152       {
153         sizeof (GtkClipboardClass),
154         NULL,           /* base_init */
155         NULL,           /* base_finalize */
156         (GClassInitFunc) gtk_clipboard_class_init,
157         NULL,           /* class_finalize */
158         NULL,           /* class_data */
159         sizeof (GtkClipboard),
160         0,              /* n_preallocs */
161         (GInstanceInitFunc) NULL,
162       };
163       
164       clipboard_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkClipboard"),
165                                                &clipboard_info, 0);
166     }
167   
168   return clipboard_type;
169 }
170
171 static void
172 gtk_clipboard_class_init (GtkClipboardClass *class)
173 {
174   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
175
176   parent_class = g_type_class_peek_parent (class);
177   
178   gobject_class->finalize = gtk_clipboard_finalize;
179
180   class->owner_change = gtk_clipboard_owner_change;
181
182   clipboard_signals[OWNER_CHANGE] =
183     g_signal_new (I_("owner_change"),
184                   G_TYPE_FROM_CLASS (gobject_class),
185                   G_SIGNAL_RUN_FIRST,
186                   G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
187                   NULL, NULL,
188                   _gtk_marshal_VOID__BOXED,
189                   G_TYPE_NONE, 1,
190                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
191 }
192
193 static void
194 gtk_clipboard_finalize (GObject *object)
195 {
196   GtkClipboard *clipboard;
197   GtkWidget *clipboard_widget;
198   GSList *clipboards;
199
200   clipboard = GTK_CLIPBOARD (object);
201
202   clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
203   if (g_slist_index (clipboards, clipboard) >= 0)
204     g_warning ("GtkClipboard prematurely finalized");
205
206   clipboard_widget = get_clipboard_widget (clipboard->display);
207   
208   clipboard_unset (clipboard);
209   
210   clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
211   clipboards = g_slist_remove (clipboards, clipboard);
212   g_object_set_data (G_OBJECT (clipboard->display), I_("gtk-clipboard-list"), clipboards);
213
214   if (g_main_loop_is_running (clipboard->store_loop))
215     {
216       g_main_loop_quit (clipboard->store_loop);
217       g_main_loop_unref (clipboard->store_loop);
218     }
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 (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   GList *l;
743   GtkTargetEntry *targets;
744   gint n_targets, i;
745
746   g_return_if_fail (clipboard != NULL);
747   g_return_if_fail (text != NULL);
748
749   list = gtk_target_list_new (NULL, 0);
750   gtk_target_list_add_text_targets (list, 0);
751
752   n_targets = g_list_length (list->list);
753   targets = g_new0 (GtkTargetEntry, n_targets);
754   for (l = list->list, i = 0; l; l = l->next, i++)
755     {
756       GtkTargetPair *pair = (GtkTargetPair *)l->data;
757       targets[i].target = gdk_atom_name (pair->target);
758     }
759   
760   if (len < 0)
761     len = strlen (text);
762   
763   gtk_clipboard_set_with_data (clipboard, 
764                                targets, n_targets,
765                                text_get_func, text_clear_func,
766                                g_strndup (text, len));
767   gtk_clipboard_set_can_store (clipboard, NULL, 0);
768
769   for (i = 0; i < n_targets; i++)
770     g_free (targets[i].target);
771   g_free (targets);
772   gtk_target_list_unref (list);
773 }
774
775 static void 
776 pixbuf_get_func (GtkClipboard     *clipboard,
777                  GtkSelectionData *selection_data,
778                  guint             info,
779                  gpointer          data)
780 {
781   gtk_selection_data_set_pixbuf (selection_data, data);
782 }
783
784 static void 
785 pixbuf_clear_func (GtkClipboard *clipboard,
786                    gpointer      data)
787 {
788   g_object_unref (data);
789 }
790
791 /**
792  * gtk_clipboard_set_image:
793  * @clipboard: a #GtkClipboard object
794  * @pixbuf:    a #GdkPixbuf 
795  * 
796  * Sets the contents of the clipboard to the given #GdkPixbuf. 
797  * GTK+ will take responsibility for responding for requests 
798  * for the image, and for converting the image into the 
799  * requested format.
800  * 
801  * Since: 2.6
802  **/
803 void
804 gtk_clipboard_set_image (GtkClipboard *clipboard,
805                           GdkPixbuf    *pixbuf)
806 {
807   GtkTargetList *list;
808   GList *l;
809   GtkTargetEntry *targets;
810   gint n_targets, i;
811
812   g_return_if_fail (clipboard != NULL);
813   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
814
815   list = gtk_target_list_new (NULL, 0);
816   gtk_target_list_add_image_targets (list, 0, TRUE);
817
818   n_targets = g_list_length (list->list);
819   targets = g_new0 (GtkTargetEntry, n_targets);
820   for (l = list->list, i = 0; l; l = l->next, i++)
821     {
822       GtkTargetPair *pair = (GtkTargetPair *)l->data;
823       targets[i].target = gdk_atom_name (pair->target);
824     }
825
826   gtk_clipboard_set_with_data (clipboard, 
827                                targets, n_targets,
828                                pixbuf_get_func, pixbuf_clear_func,
829                                g_object_ref (pixbuf));
830   gtk_clipboard_set_can_store (clipboard, NULL, 0);
831
832   for (i = 0; i < n_targets; i++)
833     g_free (targets[i].target);
834   g_free (targets);
835   gtk_target_list_unref (list);
836 }
837
838 static void
839 set_request_contents_info (GtkWidget           *widget,
840                            RequestContentsInfo *info)
841 {
842   if (!request_contents_key_id)
843     request_contents_key_id = g_quark_from_static_string (request_contents_key);
844
845   g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
846 }
847
848 static RequestContentsInfo *
849 get_request_contents_info (GtkWidget *widget)
850 {
851   if (!request_contents_key_id)
852     return NULL;
853   else
854     return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
855 }
856
857 static void 
858 selection_received (GtkWidget            *widget,
859                     GtkSelectionData     *selection_data,
860                     guint                 time)
861 {
862   RequestContentsInfo *request_info = get_request_contents_info (widget);
863   set_request_contents_info (widget, NULL);
864   
865   request_info->callback (gtk_widget_get_clipboard (widget, selection_data->selection), 
866                           selection_data,
867                           request_info->user_data);
868
869   g_free (request_info);
870
871   if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
872     gtk_widget_destroy (widget);
873 }
874
875 /**
876  * gtk_clipboard_request_contents:
877  * @clipboard: a #GtkClipboard
878  * @target:    an atom representing the form into which the clipboard
879  *             owner should convert the selection.
880  * @callback:  A function to call when the results are received
881  *             (or the retrieval fails). If the retrieval fails
882  *             the length field of @selection_data will be
883  *             negative.
884  * @user_data: user data to pass to @callback
885  * 
886  * Requests the contents of clipboard as the given target.
887  * When the results of the result are later received the supplied callback
888  * will be called.
889  **/
890 void 
891 gtk_clipboard_request_contents (GtkClipboard            *clipboard,
892                                 GdkAtom                  target,
893                                 GtkClipboardReceivedFunc callback,
894                                 gpointer                 user_data)
895 {
896   RequestContentsInfo *info;
897   GtkWidget *widget;
898   GtkWidget *clipboard_widget;
899
900   g_return_if_fail (clipboard != NULL);
901   g_return_if_fail (target != GDK_NONE);
902   g_return_if_fail (callback != NULL);
903   
904   clipboard_widget = get_clipboard_widget (clipboard->display);
905
906   if (get_request_contents_info (clipboard_widget))
907     widget = make_clipboard_widget (clipboard->display, FALSE);
908   else
909     widget = clipboard_widget;
910
911   info = g_new (RequestContentsInfo, 1);
912   info->callback = callback;
913   info->user_data = user_data;
914
915   set_request_contents_info (widget, info);
916
917   gtk_selection_convert (widget, clipboard->selection, target,
918                          clipboard_get_timestamp (clipboard));
919 }
920
921 static void 
922 request_text_received_func (GtkClipboard     *clipboard,
923                             GtkSelectionData *selection_data,
924                             gpointer          data)
925 {
926   RequestTextInfo *info = data;
927   gchar *result = NULL;
928
929   result = gtk_selection_data_get_text (selection_data);
930
931   if (!result)
932     {
933       /* If we asked for UTF8 and didn't get it, try compound_text;
934        * if we asked for compound_text and didn't get it, try string;
935        * If we asked for anything else and didn't get it, give up.
936        */
937       if (selection_data->target == gdk_atom_intern ("UTF8_STRING", FALSE))
938         {
939           gtk_clipboard_request_contents (clipboard,
940                                           gdk_atom_intern ("COMPOUND_TEXT", FALSE), 
941                                           request_text_received_func, info);
942           return;
943         }
944       else if (selection_data->target == gdk_atom_intern ("COMPOUND_TEXT", FALSE))
945         {
946           gtk_clipboard_request_contents (clipboard,
947                                           GDK_TARGET_STRING, 
948                                           request_text_received_func, info);
949           return;
950         }
951     }
952
953   info->callback (clipboard, result, info->user_data);
954   g_free (info);
955   g_free (result);
956 }
957
958 /**
959  * gtk_clipboard_request_text:
960  * @clipboard: a #GtkClipboard
961  * @callback:  a function to call when the text is received,
962  *             or the retrieval fails. (It will always be called
963  *             one way or the other.)
964  * @user_data: user data to pass to @callback.
965  * 
966  * Requests the contents of the clipboard as text. When the text is
967  * later received, it will be converted to UTF-8 if necessary, and
968  * @callback will be called. 
969  *
970  * The @text parameter to @callback will contain the resulting text if
971  * the request succeeded, or %NULL if it failed. This could happen for
972  * various reasons, in particular if the clipboard was empty or if the
973  * contents of the clipboard could not be converted into text form.
974  **/
975 void 
976 gtk_clipboard_request_text (GtkClipboard                *clipboard,
977                             GtkClipboardTextReceivedFunc callback,
978                             gpointer                     user_data)
979 {
980   RequestTextInfo *info;
981   
982   g_return_if_fail (clipboard != NULL);
983   g_return_if_fail (callback != NULL);
984   
985   info = g_new (RequestTextInfo, 1);
986   info->callback = callback;
987   info->user_data = user_data;
988
989   gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("UTF8_STRING", FALSE),
990                                   request_text_received_func,
991                                   info);
992 }
993
994 static void 
995 request_image_received_func (GtkClipboard     *clipboard,
996                              GtkSelectionData *selection_data,
997                              gpointer          data)
998 {
999   RequestImageInfo *info = data;
1000   GdkPixbuf *result = NULL;
1001
1002   result = gtk_selection_data_get_pixbuf (selection_data);
1003
1004   if (!result)
1005     {
1006       /* If we asked for image/png and didn't get it, try image/jpeg;
1007        * if we asked for image/jpeg and didn't get it, try image/gif;
1008        * if we asked for image/gif and didn't get it, try image/bmp;
1009        * If we asked for anything else and didn't get it, give up.
1010        */
1011       if (selection_data->target == gdk_atom_intern ("image/png", FALSE))
1012         {
1013           gtk_clipboard_request_contents (clipboard,
1014                                           gdk_atom_intern ("image/jpeg", FALSE), 
1015                                           request_image_received_func, info);
1016           return;
1017         }
1018       else if (selection_data->target == gdk_atom_intern ("image/jpeg", FALSE))
1019         {
1020           gtk_clipboard_request_contents (clipboard,
1021                                           gdk_atom_intern ("image/gif", FALSE), 
1022                                           request_image_received_func, info);
1023           return;
1024         }
1025       else if (selection_data->target == gdk_atom_intern ("image/gif", FALSE))
1026         {
1027           gtk_clipboard_request_contents (clipboard,
1028                                           gdk_atom_intern ("image/bmp", FALSE), 
1029                                           request_image_received_func, info);
1030           return;
1031         }
1032     }
1033
1034   info->callback (clipboard, result, info->user_data);
1035   g_free (info);
1036   g_object_unref (result);
1037 }
1038
1039 /**
1040  * gtk_clipboard_request_image:
1041  * @clipboard: a #GtkClipboard
1042  * @callback:  a function to call when the image is received,
1043  *             or the retrieval fails. (It will always be called
1044  *             one way or the other.)
1045  * @user_data: user data to pass to @callback.
1046  * 
1047  * Requests the contents of the clipboard as image. When the image is
1048  * later received, it will be converted to a #GdkPixbuf, and
1049  * @callback will be called. 
1050  *
1051  * The @pixbuf parameter to @callback will contain the resulting 
1052  * #GdkPixbuf if the request succeeded, or %NULL if it failed. This 
1053  * could happen for various reasons, in particular if the clipboard 
1054  * was empty or if the contents of the clipboard could not be 
1055  * converted into an image.
1056  *
1057  * Since: 2.6
1058  **/
1059 void 
1060 gtk_clipboard_request_image (GtkClipboard                  *clipboard,
1061                              GtkClipboardImageReceivedFunc  callback,
1062                              gpointer                       user_data)
1063 {
1064   RequestImageInfo *info;
1065   
1066   g_return_if_fail (clipboard != NULL);
1067   g_return_if_fail (callback != NULL);
1068   
1069   info = g_new (RequestImageInfo, 1);
1070   info->callback = callback;
1071   info->user_data = user_data;
1072
1073   gtk_clipboard_request_contents (clipboard, 
1074                                   gdk_atom_intern ("image/png", FALSE),
1075                                   request_image_received_func,
1076                                   info);
1077 }
1078
1079 static void 
1080 request_targets_received_func (GtkClipboard     *clipboard,
1081                                GtkSelectionData *selection_data,
1082                                gpointer          data)
1083 {
1084   RequestTargetsInfo *info = data;
1085   GdkAtom *targets = NULL;
1086   gint n_targets = 0;
1087
1088   gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1089
1090   info->callback (clipboard, targets, n_targets, info->user_data);
1091
1092   g_free (info);
1093   g_free (targets);
1094 }
1095
1096 /**
1097  * gtk_clipboard_request_targets:
1098  * @clipboard: a #GtkClipboard
1099  * @callback:  a function to call when the targets are received,
1100  *             or the retrieval fails. (It will always be called
1101  *             one way or the other.)
1102  * @user_data: user data to pass to @callback.
1103  * 
1104  * Requests the contents of the clipboard as list of supported targets. 
1105  * When the list is later received, @callback will be called. 
1106  *
1107  * The @targets parameter to @callback will contain the resulting targets if
1108  * the request succeeded, or %NULL if it failed.
1109  *
1110  * Since: 2.4
1111  **/
1112 void 
1113 gtk_clipboard_request_targets (GtkClipboard                *clipboard,
1114                                GtkClipboardTargetsReceivedFunc callback,
1115                                gpointer                     user_data)
1116 {
1117   RequestTargetsInfo *info;
1118   
1119   g_return_if_fail (clipboard != NULL);
1120   g_return_if_fail (callback != NULL);
1121
1122   /* If the display supports change notification we cache targets */
1123   if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1124       clipboard->n_cached_targets != -1)
1125     {
1126       (* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1127       return;
1128     }
1129   
1130   info = g_new (RequestTargetsInfo, 1);
1131   info->callback = callback;
1132   info->user_data = user_data;
1133
1134   gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE),
1135                                   request_targets_received_func,
1136                                   info);
1137 }
1138
1139 typedef struct
1140 {
1141   GMainLoop *loop;
1142   gpointer data;
1143 } WaitResults;
1144
1145 static void 
1146 clipboard_received_func (GtkClipboard     *clipboard,
1147                          GtkSelectionData *selection_data,
1148                          gpointer          data)
1149 {
1150   WaitResults *results = data;
1151
1152   if (selection_data->length >= 0)
1153     results->data = gtk_selection_data_copy (selection_data);
1154   
1155   g_main_loop_quit (results->loop);
1156 }
1157
1158 /**
1159  * gtk_clipboard_wait_for_contents:
1160  * @clipboard: a #GtkClipboard
1161  * @target: an atom representing the form into which the clipboard
1162  *          owner should convert the selection.
1163  * 
1164  * Requests the contents of the clipboard using the given target.
1165  * This function waits for the data to be received using the main 
1166  * loop, so events, timeouts, etc, may be dispatched during the wait.
1167  * 
1168  * Return value: a newly-allocated #GtkSelectionData object or %NULL
1169  *               if retrieving the given target failed. If non-%NULL,
1170  *               this value must be freed with gtk_selection_data_free() 
1171  *               when you are finished with it.
1172  **/
1173 GtkSelectionData *
1174 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1175                                  GdkAtom       target)
1176 {
1177   WaitResults results;
1178
1179   g_return_val_if_fail (clipboard != NULL, NULL);
1180   g_return_val_if_fail (target != GDK_NONE, NULL);
1181   
1182   results.data = NULL;
1183   results.loop = g_main_loop_new (NULL, TRUE);
1184
1185   gtk_clipboard_request_contents (clipboard, target, 
1186                                   clipboard_received_func,
1187                                   &results);
1188
1189   if (g_main_loop_is_running (results.loop))
1190     {
1191       GDK_THREADS_LEAVE ();
1192       g_main_loop_run (results.loop);
1193       GDK_THREADS_ENTER ();
1194     }
1195
1196   g_main_loop_unref (results.loop);
1197
1198   return results.data;
1199 }
1200
1201 static void 
1202 clipboard_text_received_func (GtkClipboard *clipboard,
1203                               const gchar  *text,
1204                               gpointer      data)
1205 {
1206   WaitResults *results = data;
1207
1208   results->data = g_strdup (text);
1209   g_main_loop_quit (results->loop);
1210 }
1211
1212 /**
1213  * gtk_clipboard_wait_for_text:
1214  * @clipboard: a #GtkClipboard
1215  * 
1216  * Requests the contents of the clipboard as text and converts
1217  * the result to UTF-8 if necessary. This function waits for
1218  * the data to be received using the main loop, so events,
1219  * timeouts, etc, may be dispatched during the wait.
1220  * 
1221  * Return value: a newly-allocated UTF-8 string which must
1222  *               be freed with g_free(), or %NULL if retrieving
1223  *               the selection data failed. (This could happen
1224  *               for various reasons, in particular if the
1225  *               clipboard was empty or if the contents of the
1226  *               clipboard could not be converted into text form.)
1227  **/
1228 gchar *
1229 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1230 {
1231   WaitResults results;
1232
1233   g_return_val_if_fail (clipboard != NULL, NULL);
1234   
1235   results.data = NULL;
1236   results.loop = g_main_loop_new (NULL, TRUE);
1237
1238   gtk_clipboard_request_text (clipboard,
1239                               clipboard_text_received_func,
1240                               &results);
1241
1242   if (g_main_loop_is_running (results.loop))
1243     {
1244       GDK_THREADS_LEAVE ();
1245       g_main_loop_run (results.loop);
1246       GDK_THREADS_ENTER ();
1247     }
1248
1249   g_main_loop_unref (results.loop);
1250
1251   return results.data;
1252 }
1253
1254 static void 
1255 clipboard_image_received_func (GtkClipboard *clipboard,
1256                                GdkPixbuf    *pixbuf,
1257                                gpointer      data)
1258 {
1259   WaitResults *results = data;
1260
1261   if (pixbuf)
1262     results->data = g_object_ref (pixbuf);
1263
1264   g_main_loop_quit (results->loop);
1265 }
1266
1267 /**
1268  * gtk_clipboard_wait_for_image:
1269  * @clipboard: a #GtkClipboard
1270  * 
1271  * Requests the contents of the clipboard as image and converts
1272  * the result to a #GdkPixbuf. This function waits for
1273  * the data to be received using the main loop, so events,
1274  * timeouts, etc, may be dispatched during the wait.
1275  * 
1276  * Return value: a newly-allocated #GdkPixbuf object which must
1277  *               be disposed with g_object_unref(), or %NULL if 
1278  *               retrieving the selection data failed. (This 
1279  *               could happen for various reasons, in particular 
1280  *               if the clipboard was empty or if the contents of 
1281  *               the clipboard could not be converted into an image.)
1282  *
1283  * Since: 2.6
1284  **/
1285 GdkPixbuf *
1286 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1287 {
1288   WaitResults results;
1289
1290   g_return_val_if_fail (clipboard != NULL, NULL);
1291   
1292   results.data = NULL;
1293   results.loop = g_main_loop_new (NULL, TRUE);
1294
1295   gtk_clipboard_request_image (clipboard,
1296                                clipboard_image_received_func,
1297                                &results);
1298
1299   if (g_main_loop_is_running (results.loop))
1300     {
1301       GDK_THREADS_LEAVE ();
1302       g_main_loop_run (results.loop);
1303       GDK_THREADS_ENTER ();
1304     }
1305
1306   g_main_loop_unref (results.loop);
1307
1308   return results.data;
1309 }
1310
1311 /**
1312  * gtk_clipboard_get_display:
1313  * @clipboard: a #GtkClipboard
1314  *
1315  * Gets the #GdkDisplay associated with @clipboard
1316  *
1317  * Return value: the #GdkDisplay associated with @clipboard
1318  *
1319  * Since: 2.2
1320  **/
1321 GdkDisplay *
1322 gtk_clipboard_get_display (GtkClipboard *clipboard)
1323 {
1324   g_return_val_if_fail (clipboard != NULL, NULL);
1325
1326   return clipboard->display;
1327 }
1328
1329 /**
1330  * gtk_clipboard_wait_is_text_available:
1331  * @clipboard: a #GtkClipboard
1332  * 
1333  * Test to see if there is text available to be pasted
1334  * This is done by requesting the TARGETS atom and checking
1335  * if it contains any of the supported text targets. This function 
1336  * waits for the data to be received using the main loop, so events, 
1337  * timeouts, etc, may be dispatched during the wait.
1338  *
1339  * This function is a little faster than calling
1340  * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1341  * the actual text.
1342  * 
1343  * Return value: %TRUE is there is text available, %FALSE otherwise.
1344  **/
1345 gboolean
1346 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1347 {
1348   GtkSelectionData *data;
1349   gboolean result = FALSE;
1350
1351   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1352   if (data)
1353     {
1354       result = gtk_selection_data_targets_include_text (data);
1355       gtk_selection_data_free (data);
1356     }
1357
1358   return result;
1359 }
1360
1361 /**
1362  * gtk_clipboard_wait_is_image_available:
1363  * @clipboard: a #GtkClipboard
1364  * 
1365  * Test to see if there is an image available to be pasted
1366  * This is done by requesting the TARGETS atom and checking
1367  * if it contains any of the supported image targets. This function 
1368  * waits for the data to be received using the main loop, so events, 
1369  * timeouts, etc, may be dispatched during the wait.
1370  *
1371  * This function is a little faster than calling
1372  * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1373  * the actual image data.
1374  * 
1375  * Return value: %TRUE is there is an image available, %FALSE otherwise.
1376  *
1377  * Since: 2.6
1378  **/
1379 gboolean
1380 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1381 {
1382   GtkSelectionData *data;
1383   gboolean result = FALSE;
1384
1385   data = gtk_clipboard_wait_for_contents (clipboard, 
1386                                           gdk_atom_intern ("TARGETS", FALSE));
1387   if (data)
1388     {
1389       result = gtk_selection_data_targets_include_image (data, FALSE);
1390       gtk_selection_data_free (data);
1391     }
1392
1393   return result;
1394 }
1395
1396 /**
1397  * gtk_clipboard_wait_for_targets
1398  * @clipboard: a #GtkClipboard
1399  * @targets: location to store an array of targets. The result
1400  *           stored here must be freed with g_free().
1401  * @n_targets: location to store number of items in @targets.
1402  *
1403  * Returns a list of targets that are present on the clipboard, or %NULL
1404  * if there aren't any targets available. The returned list must be 
1405  * freed with g_free().
1406  * This function waits for the data to be received using the main 
1407  * loop, so events, timeouts, etc, may be dispatched during the wait.
1408  *
1409  * Return value: %TRUE if any targets are present on the clipboard,
1410  *               otherwise %FALSE.
1411  *
1412  * Since: 2.4
1413  */
1414 gboolean
1415 gtk_clipboard_wait_for_targets (GtkClipboard  *clipboard, 
1416                                 GdkAtom      **targets,
1417                                 gint          *n_targets)
1418 {
1419   GtkSelectionData *data;
1420   gboolean result = FALSE;
1421   
1422   g_return_val_if_fail (clipboard != NULL, FALSE);
1423
1424   /* If the display supports change notification we cache targets */
1425   if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1426       clipboard->n_cached_targets != -1)
1427     {
1428       if (n_targets)
1429         *n_targets = clipboard->n_cached_targets;
1430  
1431       if (targets)
1432         *targets = g_memdup (clipboard->cached_targets,
1433                              clipboard->n_cached_targets * sizeof (GdkAtom));
1434
1435        return TRUE;
1436     }
1437   
1438   if (n_targets)
1439     *n_targets = 0;
1440       
1441   if (targets)
1442     *targets = NULL;      
1443
1444   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern ("TARGETS", FALSE));
1445
1446   if (data)
1447     {
1448       GdkAtom *tmp_targets;
1449       gint tmp_n_targets;
1450        
1451       result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1452  
1453       if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1454         {
1455           clipboard->n_cached_targets = tmp_n_targets;
1456           clipboard->cached_targets = g_memdup (tmp_targets,
1457                                                 tmp_n_targets * sizeof (GdkAtom));
1458         }
1459  
1460       if (n_targets)
1461         *n_targets = tmp_n_targets;
1462  
1463       if (targets)
1464         *targets = tmp_targets;
1465       else
1466         g_free (tmp_targets);
1467       
1468       gtk_selection_data_free (data);
1469     }
1470
1471   return result;
1472 }
1473
1474 static GtkClipboard *
1475 clipboard_peek (GdkDisplay *display, 
1476                 GdkAtom     selection,
1477                 gboolean    only_if_exists)
1478 {
1479   GtkClipboard *clipboard = NULL;
1480   GSList *clipboards;
1481   GSList *tmp_list;
1482
1483   if (selection == GDK_NONE)
1484     selection = GDK_SELECTION_CLIPBOARD;
1485
1486   clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1487
1488   tmp_list = clipboards;
1489   while (tmp_list)
1490     {
1491       clipboard = tmp_list->data;
1492       if (clipboard->selection == selection)
1493         break;
1494
1495       tmp_list = tmp_list->next;
1496     }
1497
1498   if (!tmp_list && !only_if_exists)
1499     {
1500       clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1501       clipboard->selection = selection;
1502       clipboard->display = display;
1503       clipboard->n_cached_targets = -1;
1504       clipboard->n_storable_targets = -1;
1505       clipboards = g_slist_prepend (clipboards, clipboard);
1506       g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1507       g_signal_connect (display, "closed",
1508                         G_CALLBACK (clipboard_display_closed), clipboard);
1509       gdk_display_request_selection_notification (display, selection);
1510     }
1511   
1512   return clipboard;
1513 }
1514
1515 static void
1516 gtk_clipboard_owner_change (GtkClipboard        *clipboard,
1517                             GdkEventOwnerChange *event)
1518 {
1519   if (clipboard->n_cached_targets != -1)
1520     {
1521       clipboard->n_cached_targets = -1;
1522       g_free (clipboard->cached_targets);
1523     }
1524 }
1525
1526 /**
1527  * gtk_clipboard_wait_is_target_available:
1528  * @clipboard: a #GtkClipboard
1529  * @target:    A #GdkAtom indicating which target to look for.
1530  *
1531  * Checks if a clipboard supports pasting data of a given type. This
1532  * function can be used to determine if a "Paste" menu item should be
1533  * insensitive or not.
1534  *
1535  * If you want to see if there's text available on the clipboard, use
1536  * gtk_clipboard_wait_is_text_available () instead.
1537  *
1538  * Return value: %TRUE if the target is available, %FALSE otherwise.
1539  *
1540  * Since: 2.6
1541  */
1542 gboolean
1543 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1544                                         GdkAtom       target)
1545 {
1546   GdkAtom *targets;
1547   gint i, n_targets;
1548   gboolean retval = FALSE;
1549     
1550   if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1551     return FALSE;
1552
1553   for (i = 0; i < n_targets; i++)
1554     {
1555       if (targets[i] == target)
1556         {
1557           retval = TRUE;
1558           break;
1559         }
1560     }
1561
1562   g_free (targets);
1563   
1564   return retval;
1565 }
1566
1567 /**
1568  * _gtk_clipboard_handle_event:
1569  * @event: a owner change event
1570  * 
1571  * Emits the ::owner_change signal on the appropriate @clipboard.
1572  *
1573  * Since: 2.6
1574  **/
1575 void 
1576 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1577 {
1578   GdkDisplay *display;
1579   GtkClipboard *clipboard;
1580   
1581   display = gdk_drawable_get_display (event->window);
1582   clipboard = clipboard_peek (display, event->selection, TRUE);
1583       
1584   if (clipboard)
1585     g_signal_emit (clipboard, 
1586                    clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1587 }
1588
1589 static gboolean
1590 gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1591 {
1592   g_main_loop_quit (clipboard->store_loop);
1593   
1594   return FALSE;
1595 }
1596
1597 /**
1598  * gtk_clipboard_set_can_store:
1599  * @clipboard: a #GtkClipboard
1600  * @targets: array containing information about which forms should be stored
1601  *           or %NULL to indicate that all forms should be stored.
1602  * @n_targets: number of elements in @targets
1603  *
1604  * Hints that the clipboard data should be stored somewhere when the
1605  * application exits or when gtk_clipboard_store () is called.
1606  *
1607  * This value is reset when the clipboard owner changes.
1608  * Where the clipboard data is stored is platform dependent,
1609  * see gdk_display_store_clipboard () for more information.
1610  * 
1611  * Since: 2.6
1612  */
1613 void
1614 gtk_clipboard_set_can_store (GtkClipboard         *clipboard,
1615                              const GtkTargetEntry *targets,
1616                              gint                  n_targets)
1617 {
1618   GtkWidget *clipboard_widget;
1619   int i;
1620   static const GtkTargetEntry save_targets[] = {
1621     { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1622   };
1623   
1624   g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1625   g_return_if_fail (n_targets >= 0);
1626
1627   if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1628     return;
1629   
1630   g_free (clipboard->storable_targets);
1631   
1632   clipboard_widget = get_clipboard_widget (clipboard->display);
1633
1634   /* n_storable_targets being -1 means that
1635    * gtk_clipboard_set_can_store hasn't been called since the
1636    * clipboard owner changed. We only want to add SAVE_TARGETS and 
1637    * ref the owner once , so we do that here
1638    */  
1639   if (clipboard->n_storable_targets == -1)
1640     {
1641       gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1642                                  save_targets, 1);
1643
1644       /* Ref the owner so it won't go away */
1645       if (clipboard->have_owner)
1646         g_object_ref (clipboard->user_data);
1647     }
1648   
1649   clipboard->n_storable_targets = n_targets;
1650   clipboard->storable_targets = g_new (GdkAtom, n_targets);
1651   for (i = 0; i < n_targets; i++)
1652     clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1653 }
1654
1655 static gboolean
1656 gtk_clipboard_selection_notify (GtkWidget         *widget,
1657                                 GdkEventSelection *event,
1658                                 GtkClipboard      *clipboard)
1659 {
1660   if (event->selection == gdk_atom_intern ("CLIPBOARD_MANAGER", FALSE) &&
1661       clipboard->storing_selection)
1662     g_main_loop_quit (clipboard->store_loop);
1663
1664   return FALSE;
1665 }
1666
1667 /**
1668  * gtk_clipboard_store:
1669  * @clipboard: a #GtkClipboard
1670  *
1671  * Stores the current clipboard data somewhere so that it will stay
1672  * around after the application has quit.
1673  *
1674  * Since: 2.6
1675  */
1676 void
1677 gtk_clipboard_store (GtkClipboard *clipboard)
1678 {
1679   GtkWidget *clipboard_widget;
1680
1681   g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1682
1683   if (clipboard->n_storable_targets < 0)
1684     return;
1685   
1686   if (!gdk_display_supports_clipboard_persistence (clipboard->display))
1687     return;
1688
1689   clipboard_widget = get_clipboard_widget (clipboard->display);
1690   clipboard->notify_signal_id = g_signal_connect (clipboard_widget, "selection_notify_event",
1691                                                   G_CALLBACK (gtk_clipboard_selection_notify), clipboard);
1692   
1693   gdk_display_store_clipboard (clipboard->display,
1694                                clipboard_widget->window,
1695                                clipboard_get_timestamp (clipboard),
1696                                clipboard->storable_targets,
1697                                clipboard->n_storable_targets);
1698
1699   clipboard->storing_selection = TRUE;
1700
1701   clipboard->store_loop = g_main_loop_new (NULL, TRUE);
1702   clipboard->store_timeout = g_timeout_add (10000, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
1703
1704   if (g_main_loop_is_running (clipboard->store_loop))
1705     {
1706       GDK_THREADS_LEAVE ();
1707       g_main_loop_run (clipboard->store_loop);
1708       GDK_THREADS_ENTER ();
1709     }
1710   
1711   g_main_loop_unref (clipboard->store_loop);
1712   clipboard->store_loop = NULL;
1713   
1714   g_source_remove (clipboard->store_timeout);
1715   clipboard->store_timeout = 0;
1716   g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
1717   clipboard->notify_signal_id = 0;
1718   
1719   clipboard->storing_selection = FALSE;
1720 }
1721
1722 /* Stores all clipboard selections on all displays, called from
1723  * gtk_main_quit ().
1724  */
1725 void
1726 _gtk_clipboard_store_all (void)
1727 {
1728   GtkClipboard *clipboard;
1729   GSList *displays, *list;
1730   
1731   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
1732
1733   list = displays;
1734   while (list)
1735     {
1736       GdkDisplay *display = list->data;
1737
1738       clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
1739
1740       if (clipboard)
1741         gtk_clipboard_store (clipboard);
1742       
1743       list = list->next;
1744     }
1745   g_slist_free (displays);
1746   
1747 }
1748
1749 #define __GTK_CLIPBOARD_C__
1750 #include "gtkaliasdef.c"