1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
3 * Copyright (C) 2004 Nokia Corporation
4 * Copyright (C) 2006 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"
34 #include "gtktextbuffer.h"
36 #include "gtkquartz.h"
43 typedef struct _GtkClipboardClass GtkClipboardClass;
47 GObject parent_instance;
49 NSPasteboard *pasteboard;
53 GtkClipboardGetFunc get_func;
54 GtkClipboardClearFunc clear_func;
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;
96 selection_data.selection = clipboard->selection;
97 selection_data.data = NULL;
98 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
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
104 clipboard->get_func (clipboard, &selection_data, 0, clipboard->user_data);
106 _gtk_quartz_set_selection_data_for_pasteboard (clipboard->pasteboard, &selection_data);
108 g_free (selection_data.data);
111 - (void)pasteboardChangedOwner:(NSPasteboard *)sender
114 clear_func (clipboard, user_data);
119 - (id)initWithClipboard:(GtkClipboard *)aClipboard
125 clipboard = aClipboard;
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);
138 static void clipboard_unset (GtkClipboard *clipboard);
139 static GtkClipboard *clipboard_peek (GdkDisplay *display,
141 gboolean only_if_exists);
143 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
144 static GQuark clipboards_owned_key_id = 0;
146 static GObjectClass *parent_class;
147 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
150 gtk_clipboard_get_type (void)
152 static GType clipboard_type = 0;
156 static const GTypeInfo clipboard_info =
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),
166 (GInstanceInitFunc) NULL,
169 clipboard_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkClipboard"),
173 return clipboard_type;
177 gtk_clipboard_class_init (GtkClipboardClass *class)
179 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
181 parent_class = g_type_class_peek_parent (class);
183 gobject_class->finalize = gtk_clipboard_finalize;
185 class->owner_change = gtk_clipboard_owner_change;
187 clipboard_signals[OWNER_CHANGE] =
188 g_signal_new (I_("owner_change"),
189 G_TYPE_FROM_CLASS (gobject_class),
191 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
193 _gtk_marshal_VOID__BOXED,
195 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
199 gtk_clipboard_finalize (GObject *object)
201 GtkClipboard *clipboard;
204 clipboard = GTK_CLIPBOARD (object);
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");
210 clipboard_unset (clipboard);
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);
216 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
217 g_main_loop_quit (clipboard->store_loop);
219 if (clipboard->store_timeout != 0)
220 g_source_remove (clipboard->store_timeout);
222 g_free (clipboard->storable_targets);
224 G_OBJECT_CLASS (parent_class)->finalize (object);
228 clipboard_display_closed (GdkDisplay *display,
230 GtkClipboard *clipboard)
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);
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
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.
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.)
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".
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
283 gtk_clipboard_get_for_display (GdkDisplay *display,
286 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
287 g_return_val_if_fail (!display->closed, NULL);
289 return clipboard_peek (display, selection, FALSE);
294 * gtk_clipboard_get():
295 * @selection: a #GdkAtom which identifies the clipboard
298 * Returns the clipboard object for the given selection.
299 * See gtk_clipboard_get_for_display() for complete details.
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
309 gtk_clipboard_get (GdkAtom selection)
311 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
315 clipboard_owner_destroyed (gpointer data)
317 GSList *clipboards = data;
320 tmp_list = clipboards;
323 GtkClipboard *clipboard = tmp_list->data;
325 clipboard->get_func = NULL;
326 clipboard->clear_func = NULL;
327 clipboard->user_data = NULL;
328 clipboard->have_owner = FALSE;
330 gtk_clipboard_clear (clipboard);
332 tmp_list = tmp_list->next;
335 g_slist_free (clipboards);
339 clipboard_add_owner_notify (GtkClipboard *clipboard)
341 if (!clipboards_owned_key_id)
342 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
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),
349 clipboard_owner_destroyed);
353 clipboard_remove_owner_notify (GtkClipboard *clipboard)
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),
360 clipboard_owner_destroyed);
364 gtk_clipboard_set_contents (GtkClipboard *clipboard,
365 const GtkTargetEntry *targets,
367 GtkClipboardGetFunc get_func,
368 GtkClipboardClearFunc clear_func,
372 GtkClipboardOwner *owner;
375 owner = [[GtkClipboardOwner alloc] initWithClipboard:clipboard];
376 types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets);
378 clipboard->user_data = user_data;
379 clipboard->have_owner = have_owner;
381 clipboard_add_owner_notify (clipboard);
382 clipboard->get_func = get_func;
383 clipboard->clear_func = clear_func;
385 [clipboard->pasteboard declareTypes:types owner:owner];
391 * gtk_clipboard_set_with_data:
392 * @clipboard: a #GtkClipboard
393 * @targets: array containing information about the available forms for the
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.
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.
405 * Return value: %TRUE if setting the clipboard data succeeded. If setting
406 * the clipboard data failed the provided callback functions
410 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
411 const GtkTargetEntry *targets,
413 GtkClipboardGetFunc get_func,
414 GtkClipboardClearFunc clear_func,
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);
421 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
422 get_func, clear_func, user_data,
427 * gtk_clipboard_set_with_owner:
428 * @clipboard: a #GtkClipboard
429 * @targets: array containing information about the available forms for the
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.
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.
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
446 * Return value: %TRUE if setting the clipboard data succeeded. If setting
447 * the clipboard data failed the provided callback functions
451 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
452 const GtkTargetEntry *targets,
454 GtkClipboardGetFunc get_func,
455 GtkClipboardClearFunc clear_func,
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);
463 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
464 get_func, clear_func, owner,
469 * gtk_clipboard_get_owner:
470 * @clipboard: a #GtkClipboard
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().
477 * Return value: the owner of the clipboard, if any; otherwise %NULL.
480 gtk_clipboard_get_owner (GtkClipboard *clipboard)
482 g_return_val_if_fail (clipboard != NULL, NULL);
484 if (clipboard->have_owner)
485 return clipboard->user_data;
491 clipboard_unset (GtkClipboard *clipboard)
493 GtkClipboardClearFunc old_clear_func;
495 gboolean old_have_owner;
496 gint old_n_storable_targets;
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;
505 clipboard->have_owner = FALSE;
508 clipboard->n_storable_targets = -1;
509 g_free (clipboard->storable_targets);
510 clipboard->storable_targets = NULL;
512 clipboard->get_func = NULL;
513 clipboard->clear_func = NULL;
514 clipboard->user_data = NULL;
517 old_clear_func (clipboard, old_data);
519 /* If we've transferred the clipboard data to the manager,
522 if (old_have_owner &&
523 old_n_storable_targets != -1)
524 g_object_unref (old_data);
528 * gtk_clipboard_clear:
529 * @clipboard: a #GtkClipboard
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.
538 gtk_clipboard_clear (GtkClipboard *clipboard)
540 [clipboard->pasteboard declareTypes:nil owner:nil];
544 text_get_func (GtkClipboard *clipboard,
545 GtkSelectionData *selection_data,
549 gtk_selection_data_set_text (selection_data, data, -1);
553 text_clear_func (GtkClipboard *clipboard,
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>.
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.
572 gtk_clipboard_set_text (GtkClipboard *clipboard,
576 GtkTargetEntry target = { "UTF8_STRING", 0, 0 };
578 g_return_if_fail (clipboard != NULL);
579 g_return_if_fail (text != NULL);
584 gtk_clipboard_set_with_data (clipboard,
586 text_get_func, text_clear_func,
587 g_strndup (text, len));
588 gtk_clipboard_set_can_store (clipboard, NULL, 0);
593 pixbuf_get_func (GtkClipboard *clipboard,
594 GtkSelectionData *selection_data,
598 gtk_selection_data_set_pixbuf (selection_data, data);
602 pixbuf_clear_func (GtkClipboard *clipboard,
605 g_object_unref (data);
609 * gtk_clipboard_set_image:
610 * @clipboard: a #GtkClipboard object
611 * @pixbuf: a #GdkPixbuf
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
621 gtk_clipboard_set_image (GtkClipboard *clipboard,
626 GtkTargetEntry *targets;
629 g_return_if_fail (clipboard != NULL);
630 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
632 list = gtk_target_list_new (NULL, 0);
633 gtk_target_list_add_image_targets (list, 0, TRUE);
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++)
639 GtkTargetPair *pair = (GtkTargetPair *)l->data;
640 targets[i].target = gdk_atom_name (pair->target);
643 gtk_clipboard_set_with_data (clipboard,
645 pixbuf_get_func, pixbuf_clear_func,
646 g_object_ref (pixbuf));
647 gtk_clipboard_set_can_store (clipboard, NULL, 0);
649 for (i = 0; i < n_targets; i++)
650 g_free (targets[i].target);
652 gtk_target_list_unref (list);
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
664 * @user_data: user data to pass to @callback
666 * Requests the contents of clipboard as the given target.
667 * When the results of the result are later received the supplied callback
671 gtk_clipboard_request_contents (GtkClipboard *clipboard,
673 GtkClipboardReceivedFunc callback,
676 GtkSelectionData *data;
678 data = gtk_clipboard_wait_for_contents (clipboard, target);
680 callback (clipboard, data, user_data);
682 gtk_selection_data_free (data);
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.
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.
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.
703 gtk_clipboard_request_text (GtkClipboard *clipboard,
704 GtkClipboardTextReceivedFunc callback,
707 gchar *data = gtk_clipboard_wait_for_text (clipboard);
709 callback (clipboard, data, user_data);
715 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
716 GtkTextBuffer *buffer,
717 GtkClipboardRichTextReceivedFunc callback,
720 /* FIXME: Implement */
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.
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.
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.
744 gtk_clipboard_request_image (GtkClipboard *clipboard,
745 GtkClipboardImageReceivedFunc callback,
748 GdkPixbuf *pixbuf = gtk_clipboard_wait_for_image (clipboard);
750 callback (clipboard, pixbuf, user_data);
753 g_object_unref (pixbuf);
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.
764 * Requests the contents of the clipboard as list of supported targets.
765 * When the list is later received, @callback will be called.
767 * The @targets parameter to @callback will contain the resulting targets if
768 * the request succeeded, or %NULL if it failed.
773 gtk_clipboard_request_targets (GtkClipboard *clipboard,
774 GtkClipboardTargetsReceivedFunc callback,
780 gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets);
782 callback (clipboard, targets, n_targets, user_data);
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.
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.
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.
802 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
805 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
808 GtkSelectionData *selection_data = NULL;
810 if (target == gdk_atom_intern_static_string ("TARGETS"))
812 NSArray *types = [clipboard->pasteboard types];
814 GList *atom_list, *l;
817 count = [types count];
818 atom_list = _gtk_quartz_pasteboard_types_to_atom_list (types);
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);
827 atoms = g_malloc (selection_data->length + 1);
829 for (l = atom_list, i = 0; l ; l = l->next, i++)
830 atoms[i] = GDK_POINTER_TO_ATOM (l->data);
832 selection_data->data = (guchar *)atoms;
833 selection_data->data[selection_data->length] = '\0';
837 g_list_free (atom_list);
838 return selection_data;
841 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (clipboard->pasteboard,
843 clipboard->selection);
846 return selection_data;
850 * gtk_clipboard_wait_for_text:
851 * @clipboard: a #GtkClipboard
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.
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.)
866 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
868 GtkSelectionData *data;
871 data = gtk_clipboard_wait_for_contents (clipboard,
872 gdk_atom_intern_static_string ("UTF8_STRING"));
874 result = (gchar *)gtk_selection_data_get_text (data);
876 gtk_selection_data_free (data);
882 * gtk_clipboard_wait_for_image:
883 * @clipboard: a #GtkClipboard
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.
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.)
900 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
902 const gchar *priority[] = { "image/png", "image/tiff", "image/jpeg", "image/gif", "image/bmp" };
904 GtkSelectionData *data;
906 for (i = 0; i < G_N_ELEMENTS (priority); i++)
908 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string (priority[i]));
912 GdkPixbuf *pixbuf = gtk_selection_data_get_pixbuf (data);
914 gtk_selection_data_free (data);
924 * gtk_clipboard_get_display:
925 * @clipboard: a #GtkClipboard
927 * Gets the #GdkDisplay associated with @clipboard
929 * Return value: the #GdkDisplay associated with @clipboard
934 gtk_clipboard_get_display (GtkClipboard *clipboard)
936 g_return_val_if_fail (clipboard != NULL, NULL);
938 return clipboard->display;
942 * gtk_clipboard_wait_is_text_available:
943 * @clipboard: a #GtkClipboard
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.
951 * This function is a little faster than calling
952 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
955 * Return value: %TRUE is there is text available, %FALSE otherwise.
958 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
960 GtkSelectionData *data;
961 gboolean result = FALSE;
963 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
966 result = gtk_selection_data_targets_include_text (data);
967 gtk_selection_data_free (data);
974 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
975 GtkTextBuffer *buffer)
977 GtkSelectionData *data;
978 gboolean result = FALSE;
980 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
981 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
983 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
986 result = gtk_selection_data_targets_include_rich_text (data, buffer);
987 gtk_selection_data_free (data);
994 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
996 GtkSelectionData *data;
997 gboolean result = FALSE;
999 data = gtk_clipboard_wait_for_contents (clipboard,
1000 gdk_atom_intern_static_string ("TARGETS"));
1003 result = gtk_selection_data_targets_include_image (data, FALSE);
1004 gtk_selection_data_free (data);
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.
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.
1023 * Return value: %TRUE if any targets are present on the clipboard,
1029 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1033 GtkSelectionData *data;
1034 gboolean result = FALSE;
1036 g_return_val_if_fail (clipboard != NULL, FALSE);
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)
1043 *n_targets = clipboard->n_cached_targets;
1046 *targets = g_memdup (clipboard->cached_targets,
1047 clipboard->n_cached_targets * sizeof (GdkAtom));
1058 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1062 GdkAtom *tmp_targets;
1065 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1067 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1069 clipboard->n_cached_targets = tmp_n_targets;
1070 clipboard->cached_targets = g_memdup (tmp_targets,
1071 tmp_n_targets * sizeof (GdkAtom));
1075 *n_targets = tmp_n_targets;
1078 *targets = tmp_targets;
1080 g_free (tmp_targets);
1082 gtk_selection_data_free (data);
1088 static GtkClipboard *
1089 clipboard_peek (GdkDisplay *display,
1091 gboolean only_if_exists)
1093 GtkClipboard *clipboard = NULL;
1097 if (selection == GDK_NONE)
1098 selection = GDK_SELECTION_CLIPBOARD;
1100 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1102 tmp_list = clipboards;
1105 clipboard = tmp_list->data;
1106 if (clipboard->selection == selection)
1109 tmp_list = tmp_list->next;
1112 if (!tmp_list && !only_if_exists)
1114 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1115 NSString *pasteboard_name;
1116 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1118 if (selection == GDK_SELECTION_CLIPBOARD)
1119 pasteboard_name = NSGeneralPboard;
1122 char *atom_string = gdk_atom_name (selection);
1124 pasteboard_name = [NSString stringWithFormat:@"_GTK_%@",
1125 [NSString stringWithUTF8String:atom_string]];
1126 g_free (atom_string);
1129 clipboard->pasteboard = [NSPasteboard pasteboardWithName:pasteboard_name];
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);
1148 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1149 GdkEventOwnerChange *event)
1151 if (clipboard->n_cached_targets != -1)
1153 clipboard->n_cached_targets = -1;
1154 g_free (clipboard->cached_targets);
1159 * gtk_clipboard_wait_is_target_available:
1160 * @clipboard: a #GtkClipboard
1161 * @target: A #GdkAtom indicating which target to look for.
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.
1167 * If you want to see if there's text available on the clipboard, use
1168 * gtk_clipboard_wait_is_text_available () instead.
1170 * Return value: %TRUE if the target is available, %FALSE otherwise.
1175 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1180 gboolean retval = FALSE;
1182 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1185 for (i = 0; i < n_targets; i++)
1187 if (targets[i] == target)
1200 * _gtk_clipboard_handle_event:
1201 * @event: a owner change event
1203 * Emits the ::owner_change signal on the appropriate @clipboard.
1208 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
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
1220 * Hints that the clipboard data should be stored somewhere when the
1221 * application exits or when gtk_clipboard_store () is called.
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.
1230 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1231 const GtkTargetEntry *targets,
1234 /* FIXME: Implement */
1238 * gtk_clipboard_store:
1239 * @clipboard: a #GtkClipboard
1241 * Stores the current clipboard data somewhere so that it will stay
1242 * around after the application has quit.
1247 gtk_clipboard_store (GtkClipboard *clipboard)
1249 /* FIXME: Implement */
1252 /* Stores all clipboard selections on all displays, called from
1256 _gtk_clipboard_store_all (void)
1258 /* FIXME: Implement */
1261 #define __GTK_CLIPBOARD_C__
1262 #include "gtkaliasdef.c"