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