]> Pileus Git - ~andy/gtk/blob - gtk/gtkclipboard-quartz.c
small formatting fix.
[~andy/gtk] / gtk / gtkclipboard-quartz.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  * Copyright (C) 2004 Nokia Corporation
4  * Copyright (C) 2006-2008 Imendio AB
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22
23 #include "config.h"
24 #include <string.h>
25
26 #import <Cocoa/Cocoa.h>
27
28 #include "gtkclipboard.h"
29 #include "gtkinvisible.h"
30 #include "gtkmain.h"
31 #include "gtkmarshalers.h"
32 #include "gtkintl.h"
33 #include "gtktextbuffer.h"
34 #include "gtkquartz.h"
35 #include "gtkalias.h"
36
37 enum {
38   OWNER_CHANGE,
39   LAST_SIGNAL
40 };
41
42 typedef struct _GtkClipboardClass GtkClipboardClass;
43
44 struct _GtkClipboard 
45 {
46   GObject parent_instance;
47
48   NSPasteboard *pasteboard;
49
50   GdkAtom selection;
51
52   GtkClipboardGetFunc get_func;
53   GtkClipboardClearFunc clear_func;
54   gpointer user_data;
55   gboolean have_owner;
56   GtkTargetList *target_list;
57
58   gboolean have_selection;
59   GdkDisplay *display;
60
61   GdkAtom *cached_targets;
62   gint     n_cached_targets;
63
64   guint      notify_signal_id;
65   gboolean   storing_selection;
66   GMainLoop *store_loop;
67   guint      store_timeout;
68   gint       n_storable_targets;
69   GdkAtom   *storable_targets;
70 };
71
72 struct _GtkClipboardClass
73 {
74   GObjectClass parent_class;
75
76   void (*owner_change) (GtkClipboard        *clipboard,
77                         GdkEventOwnerChange *event);
78 };
79
80 @interface GtkClipboardOwner : NSObject {
81   GtkClipboard *clipboard;
82
83   GtkClipboardGetFunc get_func;
84   GtkClipboardClearFunc clear_func;
85   gpointer user_data;
86   
87 }
88
89 @end
90
91 @implementation GtkClipboardOwner
92 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
93 {
94   GtkSelectionData selection_data;
95   guint info;
96
97   if (!clipboard->target_list)
98     return;
99
100   memset (&selection_data, 0, sizeof (GtkSelectionData));
101
102   selection_data.selection = clipboard->selection;
103   selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
104   selection_data.display = gdk_display_get_default ();
105   selection_data.length = -1;
106
107   if (gtk_target_list_find (clipboard->target_list, selection_data.target, &info))
108     {
109       clipboard->get_func (clipboard, &selection_data,
110                            info,
111                            clipboard->user_data);
112  
113       _gtk_quartz_set_selection_data_for_pasteboard (clipboard->pasteboard,
114                                                      &selection_data);
115
116       g_free (selection_data.data);
117     }
118 }
119
120 - (void)pasteboardChangedOwner:(NSPasteboard *)sender
121 {
122   if (clear_func)
123     clear_func (clipboard, user_data);
124
125   [self release];
126 }
127
128 - (id)initWithClipboard:(GtkClipboard *)aClipboard
129 {
130   self = [super init];
131
132   if (self) 
133     {
134       clipboard = aClipboard;
135     }
136
137   return self;
138 }
139
140 @end
141
142 static void gtk_clipboard_class_init   (GtkClipboardClass   *class);
143 static void gtk_clipboard_finalize     (GObject             *object);
144 static void gtk_clipboard_owner_change (GtkClipboard        *clipboard,
145                                         GdkEventOwnerChange *event);
146
147 static void          clipboard_unset      (GtkClipboard     *clipboard);
148 static GtkClipboard *clipboard_peek       (GdkDisplay       *display,
149                                            GdkAtom           selection,
150                                            gboolean          only_if_exists);
151
152 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
153 static GQuark clipboards_owned_key_id = 0;
154
155 static GObjectClass *parent_class;
156 static guint         clipboard_signals[LAST_SIGNAL] = { 0 };
157
158 GType
159 gtk_clipboard_get_type (void)
160 {
161   static GType clipboard_type = 0;
162   
163   if (!clipboard_type)
164     {
165       const GTypeInfo clipboard_info =
166       {
167         sizeof (GtkClipboardClass),
168         NULL,           /* base_init */
169         NULL,           /* base_finalize */
170         (GClassInitFunc) gtk_clipboard_class_init,
171         NULL,           /* class_finalize */
172         NULL,           /* class_data */
173         sizeof (GtkClipboard),
174         0,              /* n_preallocs */
175         (GInstanceInitFunc) NULL,
176       };
177       
178       clipboard_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkClipboard"),
179                                                &clipboard_info, 0);
180     }
181   
182   return clipboard_type;
183 }
184
185 static void
186 gtk_clipboard_class_init (GtkClipboardClass *class)
187 {
188   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
189
190   parent_class = g_type_class_peek_parent (class);
191   
192   gobject_class->finalize = gtk_clipboard_finalize;
193
194   class->owner_change = gtk_clipboard_owner_change;
195
196   clipboard_signals[OWNER_CHANGE] =
197     g_signal_new (I_("owner-change"),
198                   G_TYPE_FROM_CLASS (gobject_class),
199                   G_SIGNAL_RUN_FIRST,
200                   G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
201                   NULL, NULL,
202                   _gtk_marshal_VOID__BOXED,
203                   G_TYPE_NONE, 1,
204                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
205 }
206
207 static void
208 gtk_clipboard_finalize (GObject *object)
209 {
210   GtkClipboard *clipboard;
211   GSList *clipboards;
212
213   clipboard = GTK_CLIPBOARD (object);
214
215   clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
216   if (g_slist_index (clipboards, clipboard) >= 0)
217     g_warning ("GtkClipboard prematurely finalized");
218
219   clipboard_unset (clipboard);
220   
221   clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
222   clipboards = g_slist_remove (clipboards, clipboard);
223   g_object_set_data (G_OBJECT (clipboard->display), I_("gtk-clipboard-list"), clipboards);
224
225   if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
226     g_main_loop_quit (clipboard->store_loop);
227
228   if (clipboard->store_timeout != 0)
229     g_source_remove (clipboard->store_timeout);
230
231   g_free (clipboard->storable_targets);
232
233   G_OBJECT_CLASS (parent_class)->finalize (object);
234 }
235
236 static void
237 clipboard_display_closed (GdkDisplay   *display,
238                           gboolean      is_error,
239                           GtkClipboard *clipboard)
240 {
241   GSList *clipboards;
242
243   clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
244   g_object_run_dispose (G_OBJECT (clipboard));
245   clipboards = g_slist_remove (clipboards, clipboard);
246   g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
247   g_object_unref (clipboard);
248 }
249
250 /**
251  * gtk_clipboard_get_for_display:
252  * @display: the display for which the clipboard is to be retrieved or created
253  * @selection: a #GdkAtom which identifies the clipboard
254  *             to use.
255  * 
256  * Returns the clipboard object for the given selection.
257  * Cut/copy/paste menu items and keyboard shortcuts should use
258  * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
259  * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
260  * for backwards compatibility reasons.)
261  * The currently-selected object or text should be provided on the clipboard
262  * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
263  * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
264  * to the default clipboard, i.e. they copy the selection to what the
265  * user sees as the clipboard.
266  *
267  * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
268  * ("CLIPBOARD", FALSE)</literal>. See <ulink
269  * url="http://www.freedesktop.org/Standards/clipboards-spec">
270  * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
271  * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
272  * selections under the X window system. On Win32 the
273  * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
274  *
275  * It's possible to have arbitrary named clipboards; if you do invent
276  * new clipboards, you should prefix the selection name with an
277  * underscore (because the ICCCM requires that nonstandard atoms are
278  * underscore-prefixed), and namespace it as well. For example,
279  * if your application called "Foo" has a special-purpose
280  * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
281  * 
282  * Return value: the appropriate clipboard object. If no
283  *             clipboard already exists, a new one will
284  *             be created. Once a clipboard object has
285  *             been created, it is persistent and, since
286  *             it is owned by GTK+, must not be freed or
287  *             unrefd.
288  *
289  * Since: 2.2
290  **/
291 GtkClipboard *
292 gtk_clipboard_get_for_display (GdkDisplay *display, 
293                                GdkAtom     selection)
294 {
295   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
296   g_return_val_if_fail (!display->closed, NULL);
297
298   return clipboard_peek (display, selection, FALSE);
299 }
300
301
302 /**
303  * gtk_clipboard_get():
304  * @selection: a #GdkAtom which identifies the clipboard
305  *             to use.
306  * 
307  * Returns the clipboard object for the given selection.
308  * See gtk_clipboard_get_for_display() for complete details.
309  * 
310  * Return value: the appropriate clipboard object. If no
311  *             clipboard already exists, a new one will
312  *             be created. Once a clipboard object has
313  *             been created, it is persistent and, since
314  *             it is owned by GTK+, must not be freed or
315  *             unrefd.
316  **/
317 GtkClipboard *
318 gtk_clipboard_get (GdkAtom selection)
319 {
320   return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
321 }
322
323 static void
324 clipboard_owner_destroyed (gpointer data)
325 {
326   GSList *clipboards = data;
327   GSList *tmp_list;
328
329   tmp_list = clipboards;
330   while (tmp_list)
331     {
332       GtkClipboard *clipboard = tmp_list->data;
333
334       clipboard->get_func = NULL;
335       clipboard->clear_func = NULL;
336       clipboard->user_data = NULL;
337       clipboard->have_owner = FALSE;
338
339       if (clipboard->target_list)
340         {
341           gtk_target_list_unref (clipboard->target_list);
342           clipboard->target_list = NULL;
343         }
344
345       gtk_clipboard_clear (clipboard);
346
347       tmp_list = tmp_list->next;
348     }
349   
350   g_slist_free (clipboards);
351 }
352
353 static void
354 clipboard_add_owner_notify (GtkClipboard *clipboard)
355 {
356   if (!clipboards_owned_key_id)
357     clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
358   
359   if (clipboard->have_owner)
360     g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
361                              g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
362                                                                     clipboards_owned_key_id),
363                                               clipboard),
364                              clipboard_owner_destroyed);
365 }
366
367 static void
368 clipboard_remove_owner_notify (GtkClipboard *clipboard)
369 {
370   if (clipboard->have_owner)
371      g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
372                               g_slist_remove (g_object_steal_qdata (clipboard->user_data,
373                                                                     clipboards_owned_key_id),
374                                               clipboard),
375                               clipboard_owner_destroyed);
376 }
377
378 static gboolean
379 gtk_clipboard_set_contents (GtkClipboard         *clipboard,
380                             const GtkTargetEntry *targets,
381                             guint                 n_targets,
382                             GtkClipboardGetFunc   get_func,
383                             GtkClipboardClearFunc clear_func,
384                             gpointer              user_data,
385                             gboolean              have_owner)
386 {
387   GtkClipboardOwner *owner;
388   NSArray *types;
389   NSAutoreleasePool *pool;
390
391   pool = [[NSAutoreleasePool alloc] init];
392
393   owner = [[GtkClipboardOwner alloc] initWithClipboard:clipboard];
394
395   types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets);
396
397   if (!(clipboard->have_owner && have_owner) ||
398       clipboard->user_data != user_data)
399     {
400       clipboard_unset (clipboard);
401
402       if (clipboard->get_func)
403         {
404           /* Calling unset() caused the clipboard contents to be reset!
405            * Avoid leaking and return
406            */
407           if (!(clipboard->have_owner && have_owner) ||
408               clipboard->user_data != user_data)
409             {
410               (*clear_func) (clipboard, user_data);
411               return FALSE;
412             }
413           else
414             return TRUE;
415         }
416     }
417
418   clipboard->user_data = user_data;
419   clipboard->have_owner = have_owner;
420   if (have_owner)
421     clipboard_add_owner_notify (clipboard);
422   clipboard->get_func = get_func;
423   clipboard->clear_func = clear_func;
424
425   if (clipboard->target_list)
426     gtk_target_list_unref (clipboard->target_list);
427   clipboard->target_list = gtk_target_list_new (targets, n_targets);
428
429   [clipboard->pasteboard declareTypes:types owner:owner];
430
431   [pool release];
432
433   return TRUE;
434 }
435
436 /**
437  * gtk_clipboard_set_with_data:
438  * @clipboard:  a #GtkClipboard
439  * @targets:    array containing information about the available forms for the
440  *              clipboard data
441  * @n_targets:  number of elements in @targets
442  * @get_func:   function to call to get the actual clipboard data
443  * @clear_func: when the clipboard contents are set again, this function will
444  *              be called, and @get_func will not be subsequently called.
445  * @user_data:  user data to pass to @get_func and @clear_func.
446  * 
447  * Virtually sets the contents of the specified clipboard by providing
448  * a list of supported formats for the clipboard data and a function
449  * to call to get the actual data when it is requested.
450  * 
451  * Return value: %TRUE if setting the clipboard data succeeded. If setting
452  *               the clipboard data failed the provided callback functions
453  *               will be ignored.
454  **/
455 gboolean
456 gtk_clipboard_set_with_data (GtkClipboard          *clipboard,
457                              const GtkTargetEntry  *targets,
458                              guint                  n_targets,
459                              GtkClipboardGetFunc    get_func,
460                              GtkClipboardClearFunc  clear_func,
461                              gpointer               user_data)
462 {
463   g_return_val_if_fail (clipboard != NULL, FALSE);
464   g_return_val_if_fail (targets != NULL, FALSE);
465   g_return_val_if_fail (get_func != NULL, FALSE);
466
467   return gtk_clipboard_set_contents (clipboard, targets, n_targets,
468                                      get_func, clear_func, user_data,
469                                      FALSE);
470 }
471
472 /**
473  * gtk_clipboard_set_with_owner:
474  * @clipboard:  a #GtkClipboard
475  * @targets:    array containing information about the available forms for the
476  *              clipboard data
477  * @n_targets:  number of elements in @targets
478  * @get_func:   function to call to get the actual clipboard data
479  * @clear_func: when the clipboard contents are set again, this function will
480  *              be called, and @get_func will not be subsequently called.
481  * @owner:      an object that "owns" the data. This object will be passed
482  *              to the callbacks when called. 
483  * 
484  * Virtually sets the contents of the specified clipboard by providing
485  * a list of supported formats for the clipboard data and a function
486  * to call to get the actual data when it is requested.
487  *
488  * The difference between this function and gtk_clipboard_set_with_data()
489  * is that instead of an generic @user_data pointer, a #GObject is passed
490  * in. 
491  * 
492  * Return value: %TRUE if setting the clipboard data succeeded. If setting
493  *               the clipboard data failed the provided callback functions
494  *               will be ignored.
495  **/
496 gboolean
497 gtk_clipboard_set_with_owner (GtkClipboard          *clipboard,
498                               const GtkTargetEntry  *targets,
499                               guint                  n_targets,
500                               GtkClipboardGetFunc    get_func,
501                               GtkClipboardClearFunc  clear_func,
502                               GObject               *owner)
503 {
504   g_return_val_if_fail (clipboard != NULL, FALSE);
505   g_return_val_if_fail (targets != NULL, FALSE);
506   g_return_val_if_fail (get_func != NULL, FALSE);
507   g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
508
509   return gtk_clipboard_set_contents (clipboard, targets, n_targets,
510                                      get_func, clear_func, owner,
511                                      TRUE);
512 }
513
514 /**
515  * gtk_clipboard_get_owner:
516  * @clipboard: a #GtkClipboard
517  * 
518  * If the clipboard contents callbacks were set with 
519  * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or 
520  * gtk_clipboard_clear() has not subsequently called, returns the owner set 
521  * by gtk_clipboard_set_with_owner().
522  * 
523  * Return value: the owner of the clipboard, if any; otherwise %NULL.
524  **/
525 GObject *
526 gtk_clipboard_get_owner (GtkClipboard *clipboard)
527 {
528   g_return_val_if_fail (clipboard != NULL, NULL);
529
530   if (clipboard->have_owner)
531     return clipboard->user_data;
532   else
533     return NULL;
534 }
535
536 static void
537 clipboard_unset (GtkClipboard *clipboard)
538 {
539   GtkClipboardClearFunc old_clear_func;
540   gpointer old_data;
541   gboolean old_have_owner;
542   gint old_n_storable_targets;
543   
544   old_clear_func = clipboard->clear_func;
545   old_data = clipboard->user_data;
546   old_have_owner = clipboard->have_owner;
547   old_n_storable_targets = clipboard->n_storable_targets;
548   
549   if (old_have_owner)
550     {
551       clipboard_remove_owner_notify (clipboard);
552       clipboard->have_owner = FALSE;
553     }
554
555   clipboard->n_storable_targets = -1;
556   g_free (clipboard->storable_targets);
557   clipboard->storable_targets = NULL;
558       
559   clipboard->get_func = NULL;
560   clipboard->clear_func = NULL;
561   clipboard->user_data = NULL;
562   
563   if (old_clear_func)
564     old_clear_func (clipboard, old_data);
565
566   if (clipboard->target_list)
567     {
568       gtk_target_list_unref (clipboard->target_list);
569       clipboard->target_list = NULL;
570     }
571
572   /* If we've transferred the clipboard data to the manager,
573    * unref the owner
574    */
575   if (old_have_owner &&
576       old_n_storable_targets != -1)
577     g_object_unref (old_data);
578 }
579
580 /**
581  * gtk_clipboard_clear:
582  * @clipboard:  a #GtkClipboard
583  * 
584  * Clears the contents of the clipboard. Generally this should only
585  * be called between the time you call gtk_clipboard_set_with_owner()
586  * or gtk_clipboard_set_with_data(),
587  * and when the @clear_func you supplied is called. Otherwise, the
588  * clipboard may be owned by someone else.
589  **/
590 void
591 gtk_clipboard_clear (GtkClipboard *clipboard)
592 {
593   [clipboard->pasteboard declareTypes:nil owner:nil];
594 }
595
596 static void 
597 text_get_func (GtkClipboard     *clipboard,
598                GtkSelectionData *selection_data,
599                guint             info,
600                gpointer          data)
601 {
602   gtk_selection_data_set_text (selection_data, data, -1);
603 }
604
605 static void 
606 text_clear_func (GtkClipboard *clipboard,
607                  gpointer      data)
608 {
609   g_free (data);
610 }
611
612 /**
613  * gtk_clipboard_set_text:
614  * @clipboard: a #GtkClipboard object
615  * @text:      a UTF-8 string.
616  * @len:       length of @text, in bytes, or -1, in which case
617  *             the length will be determined with <function>strlen()</function>.
618  * 
619  * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
620  * make a copy of the text and take responsibility for responding
621  * for requests for the text, and for converting the text into
622  * the requested format.
623  **/
624 void 
625 gtk_clipboard_set_text (GtkClipboard *clipboard,
626                         const gchar  *text,
627                         gint          len)
628 {
629   GtkTargetEntry target = { "UTF8_STRING", 0, 0 };
630
631   g_return_if_fail (clipboard != NULL);
632   g_return_if_fail (text != NULL);
633   
634   if (len < 0)
635     len = strlen (text);
636   
637   gtk_clipboard_set_with_data (clipboard, 
638                                &target, 1,
639                                text_get_func, text_clear_func,
640                                g_strndup (text, len));
641   gtk_clipboard_set_can_store (clipboard, NULL, 0);
642 }
643
644
645 static void 
646 pixbuf_get_func (GtkClipboard     *clipboard,
647                  GtkSelectionData *selection_data,
648                  guint             info,
649                  gpointer          data)
650 {
651   gtk_selection_data_set_pixbuf (selection_data, data);
652 }
653
654 static void 
655 pixbuf_clear_func (GtkClipboard *clipboard,
656                    gpointer      data)
657 {
658   g_object_unref (data);
659 }
660
661 /**
662  * gtk_clipboard_set_image:
663  * @clipboard: a #GtkClipboard object
664  * @pixbuf:    a #GdkPixbuf 
665  * 
666  * Sets the contents of the clipboard to the given #GdkPixbuf. 
667  * GTK+ will take responsibility for responding for requests 
668  * for the image, and for converting the image into the 
669  * requested format.
670  * 
671  * Since: 2.6
672  **/
673 void
674 gtk_clipboard_set_image (GtkClipboard *clipboard,
675                          GdkPixbuf    *pixbuf)
676 {
677   GtkTargetList *list;
678   GList *l;
679   GtkTargetEntry *targets;
680   gint n_targets, i;
681
682   g_return_if_fail (clipboard != NULL);
683   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
684
685   list = gtk_target_list_new (NULL, 0);
686   gtk_target_list_add_image_targets (list, 0, TRUE);
687
688   n_targets = g_list_length (list->list);
689   targets = g_new0 (GtkTargetEntry, n_targets);
690   for (l = list->list, i = 0; l; l = l->next, i++)
691     {
692       GtkTargetPair *pair = (GtkTargetPair *)l->data;
693       targets[i].target = gdk_atom_name (pair->target);
694     }
695
696   gtk_clipboard_set_with_data (clipboard, 
697                                targets, n_targets,
698                                pixbuf_get_func, pixbuf_clear_func,
699                                g_object_ref (pixbuf));
700   gtk_clipboard_set_can_store (clipboard, NULL, 0);
701
702   for (i = 0; i < n_targets; i++)
703     g_free (targets[i].target);
704   g_free (targets);
705   gtk_target_list_unref (list);
706 }
707
708 /**
709  * gtk_clipboard_request_contents:
710  * @clipboard: a #GtkClipboard
711  * @target:    an atom representing the form into which the clipboard
712  *             owner should convert the selection.
713  * @callback:  A function to call when the results are received
714  *             (or the retrieval fails). If the retrieval fails
715  *             the length field of @selection_data will be
716  *             negative.
717  * @user_data: user data to pass to @callback
718  * 
719  * Requests the contents of clipboard as the given target.
720  * When the results of the result are later received the supplied callback
721  * will be called.
722  **/
723 void 
724 gtk_clipboard_request_contents (GtkClipboard            *clipboard,
725                                 GdkAtom                  target,
726                                 GtkClipboardReceivedFunc callback,
727                                 gpointer                 user_data)
728 {
729   GtkSelectionData *data;
730
731   data = gtk_clipboard_wait_for_contents (clipboard, target);
732
733   callback (clipboard, data, user_data);
734
735   gtk_selection_data_free (data);
736 }
737
738 /**
739  * gtk_clipboard_request_text:
740  * @clipboard: a #GtkClipboard
741  * @callback:  a function to call when the text is received,
742  *             or the retrieval fails. (It will always be called
743  *             one way or the other.)
744  * @user_data: user data to pass to @callback.
745  * 
746  * Requests the contents of the clipboard as text. When the text is
747  * later received, it will be converted to UTF-8 if necessary, and
748  * @callback will be called. 
749  *
750  * The @text parameter to @callback will contain the resulting text if
751  * the request succeeded, or %NULL if it failed. This could happen for
752  * various reasons, in particular if the clipboard was empty or if the
753  * contents of the clipboard could not be converted into text form.
754  **/
755 void 
756 gtk_clipboard_request_text (GtkClipboard                *clipboard,
757                             GtkClipboardTextReceivedFunc callback,
758                             gpointer                     user_data)
759 {
760   gchar *data = gtk_clipboard_wait_for_text (clipboard);
761
762   callback (clipboard, data, user_data);
763
764   g_free (data);
765 }
766
767 void
768 gtk_clipboard_request_rich_text (GtkClipboard                    *clipboard,
769                                  GtkTextBuffer                   *buffer,
770                                  GtkClipboardRichTextReceivedFunc callback,
771                                  gpointer                         user_data)
772 {
773   /* FIXME: Implement */
774 }
775
776
777 guint8 *
778 gtk_clipboard_wait_for_rich_text (GtkClipboard  *clipboard,
779                                   GtkTextBuffer *buffer,
780                                   GdkAtom       *format,
781                                   gsize         *length)
782 {
783   /* FIXME: Implement */
784   return NULL;
785 }
786
787 /**
788  * gtk_clipboard_request_image:
789  * @clipboard: a #GtkClipboard
790  * @callback:  a function to call when the image is received,
791  *             or the retrieval fails. (It will always be called
792  *             one way or the other.)
793  * @user_data: user data to pass to @callback.
794  * 
795  * Requests the contents of the clipboard as image. When the image is
796  * later received, it will be converted to a #GdkPixbuf, and
797  * @callback will be called. 
798  *
799  * The @pixbuf parameter to @callback will contain the resulting 
800  * #GdkPixbuf if the request succeeded, or %NULL if it failed. This 
801  * could happen for various reasons, in particular if the clipboard 
802  * was empty or if the contents of the clipboard could not be 
803  * converted into an image.
804  *
805  * Since: 2.6
806  **/
807 void 
808 gtk_clipboard_request_image (GtkClipboard                  *clipboard,
809                              GtkClipboardImageReceivedFunc  callback,
810                              gpointer                       user_data)
811 {
812   GdkPixbuf *pixbuf = gtk_clipboard_wait_for_image (clipboard);
813
814   callback (clipboard, pixbuf, user_data);
815
816   if (pixbuf)
817     g_object_unref (pixbuf);
818 }
819
820 void 
821 gtk_clipboard_request_uris (GtkClipboard                *clipboard,
822                             GtkClipboardURIReceivedFunc  callback,
823                             gpointer                     user_data)
824 {
825   gchar **uris = gtk_clipboard_wait_for_uris (clipboard);
826
827   callback (clipboard, uris, user_data);
828
829   g_strfreev (uris);
830 }
831
832 /**
833  * gtk_clipboard_request_targets:
834  * @clipboard: a #GtkClipboard
835  * @callback:  a function to call when the targets are received,
836  *             or the retrieval fails. (It will always be called
837  *             one way or the other.)
838  * @user_data: user data to pass to @callback.
839  * 
840  * Requests the contents of the clipboard as list of supported targets. 
841  * When the list is later received, @callback will be called. 
842  *
843  * The @targets parameter to @callback will contain the resulting targets if
844  * the request succeeded, or %NULL if it failed.
845  *
846  * Since: 2.4
847  **/
848 void 
849 gtk_clipboard_request_targets (GtkClipboard                *clipboard,
850                                GtkClipboardTargetsReceivedFunc callback,
851                                gpointer                     user_data)
852 {
853   GdkAtom *targets;
854   gint n_targets;
855
856   gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets);
857
858   callback (clipboard, targets, n_targets, user_data);
859 }
860
861
862 /**
863  * gtk_clipboard_wait_for_contents:
864  * @clipboard: a #GtkClipboard
865  * @target: an atom representing the form into which the clipboard
866  *          owner should convert the selection.
867  * 
868  * Requests the contents of the clipboard using the given target.
869  * This function waits for the data to be received using the main 
870  * loop, so events, timeouts, etc, may be dispatched during the wait.
871  * 
872  * Return value: a newly-allocated #GtkSelectionData object or %NULL
873  *               if retrieving the given target failed. If non-%NULL,
874  *               this value must be freed with gtk_selection_data_free() 
875  *               when you are finished with it.
876  **/
877 GtkSelectionData *
878 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
879                                  GdkAtom       target)
880 {
881   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
882   GtkSelectionData *selection_data = NULL;
883
884   if (target == gdk_atom_intern_static_string ("TARGETS")) 
885     {
886       NSArray *types = [clipboard->pasteboard types];
887       int i, length;
888       GList *atom_list, *l;
889       GdkAtom *atoms;
890
891       length = [types count] * sizeof (GdkAtom);
892       
893       selection_data = g_slice_new (GtkSelectionData);
894       selection_data->selection = clipboard->selection;
895       selection_data->target = target;
896
897       atoms = g_malloc (length);
898
899       atom_list = _gtk_quartz_pasteboard_types_to_atom_list (types);
900       for (l = atom_list, i = 0; l ; l = l->next, i++)
901         atoms[i] = GDK_POINTER_TO_ATOM (l->data);
902       g_list_free (atom_list);
903
904       gtk_selection_data_set (selection_data,
905                               GDK_SELECTION_TYPE_ATOM, 32,
906                               (guchar *)atoms, length);
907
908       [pool release];
909
910       return selection_data;
911     }
912
913   selection_data = _gtk_quartz_get_selection_data_from_pasteboard (clipboard->pasteboard,
914                                                                    target,
915                                                                    clipboard->selection);
916
917   [pool release];
918
919   return selection_data;
920 }
921
922 /**
923  * gtk_clipboard_wait_for_text:
924  * @clipboard: a #GtkClipboard
925  * 
926  * Requests the contents of the clipboard as text and converts
927  * the result to UTF-8 if necessary. This function waits for
928  * the data to be received using the main loop, so events,
929  * timeouts, etc, may be dispatched during the wait.
930  * 
931  * Return value: a newly-allocated UTF-8 string which must
932  *               be freed with g_free(), or %NULL if retrieving
933  *               the selection data failed. (This could happen
934  *               for various reasons, in particular if the
935  *               clipboard was empty or if the contents of the
936  *               clipboard could not be converted into text form.)
937  **/
938 gchar *
939 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
940 {
941   GtkSelectionData *data;
942   gchar *result;
943
944   data = gtk_clipboard_wait_for_contents (clipboard, 
945                                           gdk_atom_intern_static_string ("UTF8_STRING"));
946
947   result = (gchar *)gtk_selection_data_get_text (data);
948
949   gtk_selection_data_free (data);
950
951   return result;
952 }
953
954 /**
955  * gtk_clipboard_wait_for_image:
956  * @clipboard: a #GtkClipboard
957  * 
958  * Requests the contents of the clipboard as image and converts
959  * the result to a #GdkPixbuf. This function waits for
960  * the data to be received using the main loop, so events,
961  * timeouts, etc, may be dispatched during the wait.
962  * 
963  * Return value: a newly-allocated #GdkPixbuf object which must
964  *               be disposed with g_object_unref(), or %NULL if 
965  *               retrieving the selection data failed. (This 
966  *               could happen for various reasons, in particular 
967  *               if the clipboard was empty or if the contents of 
968  *               the clipboard could not be converted into an image.)
969  *
970  * Since: 2.6
971  **/
972 GdkPixbuf *
973 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
974 {
975   const gchar *priority[] = { "image/png", "image/tiff", "image/jpeg", "image/gif", "image/bmp" };
976   int i;
977   GtkSelectionData *data;
978
979   for (i = 0; i < G_N_ELEMENTS (priority); i++) 
980     {    
981       data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string (priority[i]));
982
983       if (data)
984         {
985           GdkPixbuf *pixbuf = gtk_selection_data_get_pixbuf (data);
986
987           gtk_selection_data_free (data);
988
989           return pixbuf;
990         }  
991   }
992
993   return NULL;
994 }
995
996 gchar **
997 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
998 {
999   GtkSelectionData *data;
1000
1001   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"));
1002   if (data)
1003     {
1004       gchar **uris;
1005
1006       uris = gtk_selection_data_get_uris (data);
1007       gtk_selection_data_free (data);
1008
1009       return uris;
1010     }  
1011
1012   return NULL;
1013 }
1014
1015 /**
1016  * gtk_clipboard_get_display:
1017  * @clipboard: a #GtkClipboard
1018  *
1019  * Gets the #GdkDisplay associated with @clipboard
1020  *
1021  * Return value: the #GdkDisplay associated with @clipboard
1022  *
1023  * Since: 2.2
1024  **/
1025 GdkDisplay *
1026 gtk_clipboard_get_display (GtkClipboard *clipboard)
1027 {
1028   g_return_val_if_fail (clipboard != NULL, NULL);
1029
1030   return clipboard->display;
1031 }
1032
1033 /**
1034  * gtk_clipboard_wait_is_text_available:
1035  * @clipboard: a #GtkClipboard
1036  * 
1037  * Test to see if there is text available to be pasted
1038  * This is done by requesting the TARGETS atom and checking
1039  * if it contains any of the supported text targets. This function 
1040  * waits for the data to be received using the main loop, so events, 
1041  * timeouts, etc, may be dispatched during the wait.
1042  *
1043  * This function is a little faster than calling
1044  * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1045  * the actual text.
1046  * 
1047  * Return value: %TRUE is there is text available, %FALSE otherwise.
1048  **/
1049 gboolean
1050 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1051 {
1052   GtkSelectionData *data;
1053   gboolean result = FALSE;
1054
1055   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1056   if (data)
1057     {
1058       result = gtk_selection_data_targets_include_text (data);
1059       gtk_selection_data_free (data);
1060     }
1061
1062   return result;
1063 }
1064
1065 gboolean
1066 gtk_clipboard_wait_is_rich_text_available (GtkClipboard  *clipboard,
1067                                            GtkTextBuffer *buffer)
1068 {
1069   GtkSelectionData *data;
1070   gboolean result = FALSE;
1071
1072   g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1073   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1074
1075   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1076   if (data)
1077     {
1078       result = gtk_selection_data_targets_include_rich_text (data, buffer);
1079       gtk_selection_data_free (data);
1080     }
1081
1082   return result;
1083 }
1084
1085 gboolean
1086 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1087 {
1088   GtkSelectionData *data;
1089   gboolean result = FALSE;
1090
1091   data = gtk_clipboard_wait_for_contents (clipboard, 
1092                                           gdk_atom_intern_static_string ("TARGETS"));
1093   if (data)
1094     {
1095       result = gtk_selection_data_targets_include_image (data, FALSE);
1096       gtk_selection_data_free (data);
1097     }
1098
1099   return result;
1100 }
1101
1102 gboolean
1103 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
1104 {
1105   GtkSelectionData *data;
1106   gboolean result = FALSE;
1107
1108   data = gtk_clipboard_wait_for_contents (clipboard, 
1109                                           gdk_atom_intern_static_string ("TARGETS"));
1110   if (data)
1111     {
1112       result = gtk_selection_data_targets_include_uri (data);
1113       gtk_selection_data_free (data);
1114     }
1115
1116   return result;
1117 }
1118
1119 /**
1120  * gtk_clipboard_wait_for_targets
1121  * @clipboard: a #GtkClipboard
1122  * @targets: location to store an array of targets. The result
1123  *           stored here must be freed with g_free().
1124  * @n_targets: location to store number of items in @targets.
1125  *
1126  * Returns a list of targets that are present on the clipboard, or %NULL
1127  * if there aren't any targets available. The returned list must be 
1128  * freed with g_free().
1129  * This function waits for the data to be received using the main 
1130  * loop, so events, timeouts, etc, may be dispatched during the wait.
1131  *
1132  * Return value: %TRUE if any targets are present on the clipboard,
1133  *               otherwise %FALSE.
1134  *
1135  * Since: 2.4
1136  */
1137 gboolean
1138 gtk_clipboard_wait_for_targets (GtkClipboard  *clipboard, 
1139                                 GdkAtom      **targets,
1140                                 gint          *n_targets)
1141 {
1142   GtkSelectionData *data;
1143   gboolean result = FALSE;
1144   
1145   g_return_val_if_fail (clipboard != NULL, FALSE);
1146
1147   /* If the display supports change notification we cache targets */
1148   if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1149       clipboard->n_cached_targets != -1)
1150     {
1151       if (n_targets)
1152         *n_targets = clipboard->n_cached_targets;
1153  
1154       if (targets)
1155         *targets = g_memdup (clipboard->cached_targets,
1156                              clipboard->n_cached_targets * sizeof (GdkAtom));
1157
1158        return TRUE;
1159     }
1160   
1161   if (n_targets)
1162     *n_targets = 0;
1163       
1164   if (targets)
1165     *targets = NULL;      
1166
1167   data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1168
1169   if (data)
1170     {
1171       GdkAtom *tmp_targets;
1172       gint tmp_n_targets;
1173        
1174       result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1175  
1176       if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1177         {
1178           clipboard->n_cached_targets = tmp_n_targets;
1179           clipboard->cached_targets = g_memdup (tmp_targets,
1180                                                 tmp_n_targets * sizeof (GdkAtom));
1181         }
1182  
1183       if (n_targets)
1184         *n_targets = tmp_n_targets;
1185  
1186       if (targets)
1187         *targets = tmp_targets;
1188       else
1189         g_free (tmp_targets);
1190       
1191       gtk_selection_data_free (data);
1192     }
1193
1194   return result;
1195 }
1196
1197 static GtkClipboard *
1198 clipboard_peek (GdkDisplay *display, 
1199                 GdkAtom     selection,
1200                 gboolean    only_if_exists)
1201 {
1202   GtkClipboard *clipboard = NULL;
1203   GSList *clipboards;
1204   GSList *tmp_list;
1205
1206   if (selection == GDK_NONE)
1207     selection = GDK_SELECTION_CLIPBOARD;
1208
1209   clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1210
1211   tmp_list = clipboards;
1212   while (tmp_list)
1213     {
1214       clipboard = tmp_list->data;
1215       if (clipboard->selection == selection)
1216         break;
1217
1218       tmp_list = tmp_list->next;
1219     }
1220
1221   if (!tmp_list && !only_if_exists)
1222     {
1223       NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1224       NSString *pasteboard_name;
1225       clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1226
1227       if (selection == GDK_SELECTION_CLIPBOARD) 
1228         pasteboard_name = NSGeneralPboard;
1229       else 
1230         {
1231           char *atom_string = gdk_atom_name (selection);
1232
1233           pasteboard_name = [NSString stringWithFormat:@"_GTK_%@", 
1234                              [NSString stringWithUTF8String:atom_string]];
1235           g_free (atom_string);
1236         }
1237
1238       clipboard->pasteboard = [NSPasteboard pasteboardWithName:pasteboard_name];
1239
1240       [pool release];
1241
1242       clipboard->selection = selection;
1243       clipboard->display = display;
1244       clipboard->n_cached_targets = -1;
1245       clipboard->n_storable_targets = -1;
1246       clipboards = g_slist_prepend (clipboards, clipboard);
1247       g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1248       g_signal_connect (display, "closed",
1249                         G_CALLBACK (clipboard_display_closed), clipboard);
1250       gdk_display_request_selection_notification (display, selection);
1251     }
1252   
1253   return clipboard;
1254 }
1255
1256 static void
1257 gtk_clipboard_owner_change (GtkClipboard        *clipboard,
1258                             GdkEventOwnerChange *event)
1259 {
1260   if (clipboard->n_cached_targets != -1)
1261     {
1262       clipboard->n_cached_targets = -1;
1263       g_free (clipboard->cached_targets);
1264     }
1265 }
1266
1267 /**
1268  * gtk_clipboard_wait_is_target_available:
1269  * @clipboard: a #GtkClipboard
1270  * @target:    A #GdkAtom indicating which target to look for.
1271  *
1272  * Checks if a clipboard supports pasting data of a given type. This
1273  * function can be used to determine if a "Paste" menu item should be
1274  * insensitive or not.
1275  *
1276  * If you want to see if there's text available on the clipboard, use
1277  * gtk_clipboard_wait_is_text_available () instead.
1278  *
1279  * Return value: %TRUE if the target is available, %FALSE otherwise.
1280  *
1281  * Since: 2.6
1282  */
1283 gboolean
1284 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1285                                         GdkAtom       target)
1286 {
1287   GdkAtom *targets;
1288   gint i, n_targets;
1289   gboolean retval = FALSE;
1290     
1291   if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1292     return FALSE;
1293
1294   for (i = 0; i < n_targets; i++)
1295     {
1296       if (targets[i] == target)
1297         {
1298           retval = TRUE;
1299           break;
1300         }
1301     }
1302
1303   g_free (targets);
1304   
1305   return retval;
1306 }
1307
1308 /**
1309  * _gtk_clipboard_handle_event:
1310  * @event: a owner change event
1311  * 
1312  * Emits the ::owner-change signal on the appropriate @clipboard.
1313  *
1314  * Since: 2.6
1315  **/
1316 void 
1317 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1318 {
1319 }
1320
1321
1322 /**
1323  * gtk_clipboard_set_can_store:
1324  * @clipboard: a #GtkClipboard
1325  * @targets: array containing information about which forms should be stored
1326  *           or %NULL to indicate that all forms should be stored.
1327  * @n_targets: number of elements in @targets
1328  *
1329  * Hints that the clipboard data should be stored somewhere when the
1330  * application exits or when gtk_clipboard_store () is called.
1331  *
1332  * This value is reset when the clipboard owner changes.
1333  * Where the clipboard data is stored is platform dependent,
1334  * see gdk_display_store_clipboard () for more information.
1335  * 
1336  * Since: 2.6
1337  */
1338 void
1339 gtk_clipboard_set_can_store (GtkClipboard         *clipboard,
1340                              const GtkTargetEntry *targets,
1341                              gint                  n_targets)
1342 {
1343   /* FIXME: Implement */
1344 }
1345
1346 /**
1347  * gtk_clipboard_store:
1348  * @clipboard: a #GtkClipboard
1349  *
1350  * Stores the current clipboard data somewhere so that it will stay
1351  * around after the application has quit.
1352  *
1353  * Since: 2.6
1354  */
1355 void
1356 gtk_clipboard_store (GtkClipboard *clipboard)
1357 {
1358   /* FIXME: Implement */
1359 }
1360
1361 /* Stores all clipboard selections on all displays, called from
1362  * gtk_main_quit ().
1363  */
1364 void
1365 _gtk_clipboard_store_all (void)
1366 {
1367   /* FIXME: Implement */
1368 }
1369
1370 #define __GTK_CLIPBOARD_C__
1371 #include "gtkaliasdef.c"