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
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.
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.
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.
26 #import <Cocoa/Cocoa.h>
28 #include "gtkclipboard.h"
29 #include "gtkinvisible.h"
31 #include "gtkmarshalers.h"
33 #include "gtktextbuffer.h"
34 #include "gtkquartz.h"
42 typedef struct _GtkClipboardClass GtkClipboardClass;
46 GObject parent_instance;
48 NSPasteboard *pasteboard;
52 GtkClipboardGetFunc get_func;
53 GtkClipboardClearFunc clear_func;
56 GtkTargetList *target_list;
58 gboolean have_selection;
61 GdkAtom *cached_targets;
62 gint n_cached_targets;
64 guint notify_signal_id;
65 gboolean storing_selection;
66 GMainLoop *store_loop;
68 gint n_storable_targets;
69 GdkAtom *storable_targets;
72 struct _GtkClipboardClass
74 GObjectClass parent_class;
76 void (*owner_change) (GtkClipboard *clipboard,
77 GdkEventOwnerChange *event);
80 @interface GtkClipboardOwner : NSObject {
81 GtkClipboard *clipboard;
83 GtkClipboardGetFunc get_func;
84 GtkClipboardClearFunc clear_func;
91 @implementation GtkClipboardOwner
92 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
94 GtkSelectionData selection_data;
97 if (!clipboard->target_list)
100 memset (&selection_data, 0, sizeof (GtkSelectionData));
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;
107 if (gtk_target_list_find (clipboard->target_list, selection_data.target, &info))
109 clipboard->get_func (clipboard, &selection_data,
111 clipboard->user_data);
113 _gtk_quartz_set_selection_data_for_pasteboard (clipboard->pasteboard,
116 g_free (selection_data.data);
120 - (void)pasteboardChangedOwner:(NSPasteboard *)sender
123 clear_func (clipboard, user_data);
128 - (id)initWithClipboard:(GtkClipboard *)aClipboard
134 clipboard = aClipboard;
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);
147 static void clipboard_unset (GtkClipboard *clipboard);
148 static GtkClipboard *clipboard_peek (GdkDisplay *display,
150 gboolean only_if_exists);
152 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
153 static GQuark clipboards_owned_key_id = 0;
155 static GObjectClass *parent_class;
156 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
159 gtk_clipboard_get_type (void)
161 static GType clipboard_type = 0;
165 const GTypeInfo clipboard_info =
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),
175 (GInstanceInitFunc) NULL,
178 clipboard_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkClipboard"),
182 return clipboard_type;
186 gtk_clipboard_class_init (GtkClipboardClass *class)
188 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
190 parent_class = g_type_class_peek_parent (class);
192 gobject_class->finalize = gtk_clipboard_finalize;
194 class->owner_change = gtk_clipboard_owner_change;
196 clipboard_signals[OWNER_CHANGE] =
197 g_signal_new (I_("owner-change"),
198 G_TYPE_FROM_CLASS (gobject_class),
200 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
202 _gtk_marshal_VOID__BOXED,
204 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
208 gtk_clipboard_finalize (GObject *object)
210 GtkClipboard *clipboard;
213 clipboard = GTK_CLIPBOARD (object);
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");
219 clipboard_unset (clipboard);
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);
225 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
226 g_main_loop_quit (clipboard->store_loop);
228 if (clipboard->store_timeout != 0)
229 g_source_remove (clipboard->store_timeout);
231 g_free (clipboard->storable_targets);
233 G_OBJECT_CLASS (parent_class)->finalize (object);
237 clipboard_display_closed (GdkDisplay *display,
239 GtkClipboard *clipboard)
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);
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
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.
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.)
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".
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
292 gtk_clipboard_get_for_display (GdkDisplay *display,
295 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
296 g_return_val_if_fail (!display->closed, NULL);
298 return clipboard_peek (display, selection, FALSE);
303 * gtk_clipboard_get():
304 * @selection: a #GdkAtom which identifies the clipboard
307 * Returns the clipboard object for the given selection.
308 * See gtk_clipboard_get_for_display() for complete details.
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
318 gtk_clipboard_get (GdkAtom selection)
320 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
324 clipboard_owner_destroyed (gpointer data)
326 GSList *clipboards = data;
329 tmp_list = clipboards;
332 GtkClipboard *clipboard = tmp_list->data;
334 clipboard->get_func = NULL;
335 clipboard->clear_func = NULL;
336 clipboard->user_data = NULL;
337 clipboard->have_owner = FALSE;
339 if (clipboard->target_list)
341 gtk_target_list_unref (clipboard->target_list);
342 clipboard->target_list = NULL;
345 gtk_clipboard_clear (clipboard);
347 tmp_list = tmp_list->next;
350 g_slist_free (clipboards);
354 clipboard_add_owner_notify (GtkClipboard *clipboard)
356 if (!clipboards_owned_key_id)
357 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
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),
364 clipboard_owner_destroyed);
368 clipboard_remove_owner_notify (GtkClipboard *clipboard)
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),
375 clipboard_owner_destroyed);
379 gtk_clipboard_set_contents (GtkClipboard *clipboard,
380 const GtkTargetEntry *targets,
382 GtkClipboardGetFunc get_func,
383 GtkClipboardClearFunc clear_func,
387 GtkClipboardOwner *owner;
389 NSAutoreleasePool *pool;
391 pool = [[NSAutoreleasePool alloc] init];
393 owner = [[GtkClipboardOwner alloc] initWithClipboard:clipboard];
395 types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets);
397 if (!(clipboard->have_owner && have_owner) ||
398 clipboard->user_data != user_data)
400 clipboard_unset (clipboard);
402 if (clipboard->get_func)
404 /* Calling unset() caused the clipboard contents to be reset!
405 * Avoid leaking and return
407 if (!(clipboard->have_owner && have_owner) ||
408 clipboard->user_data != user_data)
410 (*clear_func) (clipboard, user_data);
418 clipboard->user_data = user_data;
419 clipboard->have_owner = have_owner;
421 clipboard_add_owner_notify (clipboard);
422 clipboard->get_func = get_func;
423 clipboard->clear_func = clear_func;
425 if (clipboard->target_list)
426 gtk_target_list_unref (clipboard->target_list);
427 clipboard->target_list = gtk_target_list_new (targets, n_targets);
429 [clipboard->pasteboard declareTypes:types owner:owner];
437 * gtk_clipboard_set_with_data:
438 * @clipboard: a #GtkClipboard
439 * @targets: array containing information about the available forms for the
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.
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.
451 * Return value: %TRUE if setting the clipboard data succeeded. If setting
452 * the clipboard data failed the provided callback functions
456 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
457 const GtkTargetEntry *targets,
459 GtkClipboardGetFunc get_func,
460 GtkClipboardClearFunc clear_func,
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);
467 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
468 get_func, clear_func, user_data,
473 * gtk_clipboard_set_with_owner:
474 * @clipboard: a #GtkClipboard
475 * @targets: array containing information about the available forms for the
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.
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.
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
492 * Return value: %TRUE if setting the clipboard data succeeded. If setting
493 * the clipboard data failed the provided callback functions
497 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
498 const GtkTargetEntry *targets,
500 GtkClipboardGetFunc get_func,
501 GtkClipboardClearFunc clear_func,
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);
509 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
510 get_func, clear_func, owner,
515 * gtk_clipboard_get_owner:
516 * @clipboard: a #GtkClipboard
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().
523 * Return value: the owner of the clipboard, if any; otherwise %NULL.
526 gtk_clipboard_get_owner (GtkClipboard *clipboard)
528 g_return_val_if_fail (clipboard != NULL, NULL);
530 if (clipboard->have_owner)
531 return clipboard->user_data;
537 clipboard_unset (GtkClipboard *clipboard)
539 GtkClipboardClearFunc old_clear_func;
541 gboolean old_have_owner;
542 gint old_n_storable_targets;
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;
551 clipboard_remove_owner_notify (clipboard);
552 clipboard->have_owner = FALSE;
555 clipboard->n_storable_targets = -1;
556 g_free (clipboard->storable_targets);
557 clipboard->storable_targets = NULL;
559 clipboard->get_func = NULL;
560 clipboard->clear_func = NULL;
561 clipboard->user_data = NULL;
564 old_clear_func (clipboard, old_data);
566 if (clipboard->target_list)
568 gtk_target_list_unref (clipboard->target_list);
569 clipboard->target_list = NULL;
572 /* If we've transferred the clipboard data to the manager,
575 if (old_have_owner &&
576 old_n_storable_targets != -1)
577 g_object_unref (old_data);
581 * gtk_clipboard_clear:
582 * @clipboard: a #GtkClipboard
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.
591 gtk_clipboard_clear (GtkClipboard *clipboard)
593 [clipboard->pasteboard declareTypes:nil owner:nil];
597 text_get_func (GtkClipboard *clipboard,
598 GtkSelectionData *selection_data,
602 gtk_selection_data_set_text (selection_data, data, -1);
606 text_clear_func (GtkClipboard *clipboard,
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>.
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.
625 gtk_clipboard_set_text (GtkClipboard *clipboard,
629 GtkTargetEntry target = { "UTF8_STRING", 0, 0 };
631 g_return_if_fail (clipboard != NULL);
632 g_return_if_fail (text != NULL);
637 gtk_clipboard_set_with_data (clipboard,
639 text_get_func, text_clear_func,
640 g_strndup (text, len));
641 gtk_clipboard_set_can_store (clipboard, NULL, 0);
646 pixbuf_get_func (GtkClipboard *clipboard,
647 GtkSelectionData *selection_data,
651 gtk_selection_data_set_pixbuf (selection_data, data);
655 pixbuf_clear_func (GtkClipboard *clipboard,
658 g_object_unref (data);
662 * gtk_clipboard_set_image:
663 * @clipboard: a #GtkClipboard object
664 * @pixbuf: a #GdkPixbuf
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
674 gtk_clipboard_set_image (GtkClipboard *clipboard,
679 GtkTargetEntry *targets;
682 g_return_if_fail (clipboard != NULL);
683 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
685 list = gtk_target_list_new (NULL, 0);
686 gtk_target_list_add_image_targets (list, 0, TRUE);
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++)
692 GtkTargetPair *pair = (GtkTargetPair *)l->data;
693 targets[i].target = gdk_atom_name (pair->target);
696 gtk_clipboard_set_with_data (clipboard,
698 pixbuf_get_func, pixbuf_clear_func,
699 g_object_ref (pixbuf));
700 gtk_clipboard_set_can_store (clipboard, NULL, 0);
702 for (i = 0; i < n_targets; i++)
703 g_free (targets[i].target);
705 gtk_target_list_unref (list);
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
717 * @user_data: user data to pass to @callback
719 * Requests the contents of clipboard as the given target.
720 * When the results of the result are later received the supplied callback
724 gtk_clipboard_request_contents (GtkClipboard *clipboard,
726 GtkClipboardReceivedFunc callback,
729 GtkSelectionData *data;
731 data = gtk_clipboard_wait_for_contents (clipboard, target);
733 callback (clipboard, data, user_data);
735 gtk_selection_data_free (data);
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.
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.
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.
756 gtk_clipboard_request_text (GtkClipboard *clipboard,
757 GtkClipboardTextReceivedFunc callback,
760 gchar *data = gtk_clipboard_wait_for_text (clipboard);
762 callback (clipboard, data, user_data);
768 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
769 GtkTextBuffer *buffer,
770 GtkClipboardRichTextReceivedFunc callback,
773 /* FIXME: Implement */
778 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
779 GtkTextBuffer *buffer,
783 /* FIXME: Implement */
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.
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.
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.
808 gtk_clipboard_request_image (GtkClipboard *clipboard,
809 GtkClipboardImageReceivedFunc callback,
812 GdkPixbuf *pixbuf = gtk_clipboard_wait_for_image (clipboard);
814 callback (clipboard, pixbuf, user_data);
817 g_object_unref (pixbuf);
821 gtk_clipboard_request_uris (GtkClipboard *clipboard,
822 GtkClipboardURIReceivedFunc callback,
825 gchar **uris = gtk_clipboard_wait_for_uris (clipboard);
827 callback (clipboard, uris, user_data);
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.
840 * Requests the contents of the clipboard as list of supported targets.
841 * When the list is later received, @callback will be called.
843 * The @targets parameter to @callback will contain the resulting targets if
844 * the request succeeded, or %NULL if it failed.
849 gtk_clipboard_request_targets (GtkClipboard *clipboard,
850 GtkClipboardTargetsReceivedFunc callback,
856 gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets);
858 callback (clipboard, targets, n_targets, user_data);
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.
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.
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.
878 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
881 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
882 GtkSelectionData *selection_data = NULL;
884 if (target == gdk_atom_intern_static_string ("TARGETS"))
886 NSArray *types = [clipboard->pasteboard types];
888 GList *atom_list, *l;
891 length = [types count] * sizeof (GdkAtom);
893 selection_data = g_slice_new (GtkSelectionData);
894 selection_data->selection = clipboard->selection;
895 selection_data->target = target;
897 atoms = g_malloc (length);
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);
904 gtk_selection_data_set (selection_data,
905 GDK_SELECTION_TYPE_ATOM, 32,
906 (guchar *)atoms, length);
910 return selection_data;
913 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (clipboard->pasteboard,
915 clipboard->selection);
919 return selection_data;
923 * gtk_clipboard_wait_for_text:
924 * @clipboard: a #GtkClipboard
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.
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.)
939 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
941 GtkSelectionData *data;
944 data = gtk_clipboard_wait_for_contents (clipboard,
945 gdk_atom_intern_static_string ("UTF8_STRING"));
947 result = (gchar *)gtk_selection_data_get_text (data);
949 gtk_selection_data_free (data);
955 * gtk_clipboard_wait_for_image:
956 * @clipboard: a #GtkClipboard
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.
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.)
973 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
975 const gchar *priority[] = { "image/png", "image/tiff", "image/jpeg", "image/gif", "image/bmp" };
977 GtkSelectionData *data;
979 for (i = 0; i < G_N_ELEMENTS (priority); i++)
981 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string (priority[i]));
985 GdkPixbuf *pixbuf = gtk_selection_data_get_pixbuf (data);
987 gtk_selection_data_free (data);
997 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
999 GtkSelectionData *data;
1001 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"));
1006 uris = gtk_selection_data_get_uris (data);
1007 gtk_selection_data_free (data);
1016 * gtk_clipboard_get_display:
1017 * @clipboard: a #GtkClipboard
1019 * Gets the #GdkDisplay associated with @clipboard
1021 * Return value: the #GdkDisplay associated with @clipboard
1026 gtk_clipboard_get_display (GtkClipboard *clipboard)
1028 g_return_val_if_fail (clipboard != NULL, NULL);
1030 return clipboard->display;
1034 * gtk_clipboard_wait_is_text_available:
1035 * @clipboard: a #GtkClipboard
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.
1043 * This function is a little faster than calling
1044 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1047 * Return value: %TRUE is there is text available, %FALSE otherwise.
1050 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1052 GtkSelectionData *data;
1053 gboolean result = FALSE;
1055 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1058 result = gtk_selection_data_targets_include_text (data);
1059 gtk_selection_data_free (data);
1066 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
1067 GtkTextBuffer *buffer)
1069 GtkSelectionData *data;
1070 gboolean result = FALSE;
1072 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1073 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1075 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1078 result = gtk_selection_data_targets_include_rich_text (data, buffer);
1079 gtk_selection_data_free (data);
1086 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1088 GtkSelectionData *data;
1089 gboolean result = FALSE;
1091 data = gtk_clipboard_wait_for_contents (clipboard,
1092 gdk_atom_intern_static_string ("TARGETS"));
1095 result = gtk_selection_data_targets_include_image (data, FALSE);
1096 gtk_selection_data_free (data);
1103 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
1105 GtkSelectionData *data;
1106 gboolean result = FALSE;
1108 data = gtk_clipboard_wait_for_contents (clipboard,
1109 gdk_atom_intern_static_string ("TARGETS"));
1112 result = gtk_selection_data_targets_include_uri (data);
1113 gtk_selection_data_free (data);
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.
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.
1132 * Return value: %TRUE if any targets are present on the clipboard,
1138 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1142 GtkSelectionData *data;
1143 gboolean result = FALSE;
1145 g_return_val_if_fail (clipboard != NULL, FALSE);
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)
1152 *n_targets = clipboard->n_cached_targets;
1155 *targets = g_memdup (clipboard->cached_targets,
1156 clipboard->n_cached_targets * sizeof (GdkAtom));
1167 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1171 GdkAtom *tmp_targets;
1174 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1176 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1178 clipboard->n_cached_targets = tmp_n_targets;
1179 clipboard->cached_targets = g_memdup (tmp_targets,
1180 tmp_n_targets * sizeof (GdkAtom));
1184 *n_targets = tmp_n_targets;
1187 *targets = tmp_targets;
1189 g_free (tmp_targets);
1191 gtk_selection_data_free (data);
1197 static GtkClipboard *
1198 clipboard_peek (GdkDisplay *display,
1200 gboolean only_if_exists)
1202 GtkClipboard *clipboard = NULL;
1206 if (selection == GDK_NONE)
1207 selection = GDK_SELECTION_CLIPBOARD;
1209 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1211 tmp_list = clipboards;
1214 clipboard = tmp_list->data;
1215 if (clipboard->selection == selection)
1218 tmp_list = tmp_list->next;
1221 if (!tmp_list && !only_if_exists)
1223 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1224 NSString *pasteboard_name;
1225 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1227 if (selection == GDK_SELECTION_CLIPBOARD)
1228 pasteboard_name = NSGeneralPboard;
1231 char *atom_string = gdk_atom_name (selection);
1233 pasteboard_name = [NSString stringWithFormat:@"_GTK_%@",
1234 [NSString stringWithUTF8String:atom_string]];
1235 g_free (atom_string);
1238 clipboard->pasteboard = [NSPasteboard pasteboardWithName:pasteboard_name];
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);
1257 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1258 GdkEventOwnerChange *event)
1260 if (clipboard->n_cached_targets != -1)
1262 clipboard->n_cached_targets = -1;
1263 g_free (clipboard->cached_targets);
1268 * gtk_clipboard_wait_is_target_available:
1269 * @clipboard: a #GtkClipboard
1270 * @target: A #GdkAtom indicating which target to look for.
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.
1276 * If you want to see if there's text available on the clipboard, use
1277 * gtk_clipboard_wait_is_text_available () instead.
1279 * Return value: %TRUE if the target is available, %FALSE otherwise.
1284 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1289 gboolean retval = FALSE;
1291 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1294 for (i = 0; i < n_targets; i++)
1296 if (targets[i] == target)
1309 * _gtk_clipboard_handle_event:
1310 * @event: a owner change event
1312 * Emits the ::owner-change signal on the appropriate @clipboard.
1317 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
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
1329 * Hints that the clipboard data should be stored somewhere when the
1330 * application exits or when gtk_clipboard_store () is called.
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.
1339 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1340 const GtkTargetEntry *targets,
1343 /* FIXME: Implement */
1347 * gtk_clipboard_store:
1348 * @clipboard: a #GtkClipboard
1350 * Stores the current clipboard data somewhere so that it will stay
1351 * around after the application has quit.
1356 gtk_clipboard_store (GtkClipboard *clipboard)
1358 /* FIXME: Implement */
1361 /* Stores all clipboard selections on all displays, called from
1365 _gtk_clipboard_store_all (void)
1367 /* FIXME: Implement */
1370 #define __GTK_CLIPBOARD_C__
1371 #include "gtkaliasdef.c"