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"
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;
57 gboolean have_selection;
60 GdkAtom *cached_targets;
61 gint n_cached_targets;
63 guint notify_signal_id;
64 gboolean storing_selection;
65 GMainLoop *store_loop;
67 gint n_storable_targets;
68 GdkAtom *storable_targets;
71 struct _GtkClipboardClass
73 GObjectClass parent_class;
75 void (*owner_change) (GtkClipboard *clipboard,
76 GdkEventOwnerChange *event);
79 @interface GtkClipboardOwner : NSObject {
80 GtkClipboard *clipboard;
82 GtkClipboardGetFunc get_func;
83 GtkClipboardClearFunc clear_func;
90 @implementation GtkClipboardOwner
91 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
93 GtkSelectionData selection_data;
95 selection_data.selection = clipboard->selection;
96 selection_data.data = NULL;
97 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
99 /* FIXME: We need to find out what the info argument should be
100 * here by storing it in the clipboard object in
101 * gtk_clipboard_set_contents
103 clipboard->get_func (clipboard, &selection_data, 0, clipboard->user_data);
105 _gtk_quartz_set_selection_data_for_pasteboard (clipboard->pasteboard, &selection_data);
107 g_free (selection_data.data);
110 - (void)pasteboardChangedOwner:(NSPasteboard *)sender
113 clear_func (clipboard, user_data);
118 - (id)initWithClipboard:(GtkClipboard *)aClipboard
124 clipboard = aClipboard;
132 static void gtk_clipboard_class_init (GtkClipboardClass *class);
133 static void gtk_clipboard_finalize (GObject *object);
134 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
135 GdkEventOwnerChange *event);
137 static void clipboard_unset (GtkClipboard *clipboard);
138 static GtkClipboard *clipboard_peek (GdkDisplay *display,
140 gboolean only_if_exists);
142 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
143 static GQuark clipboards_owned_key_id = 0;
145 static GObjectClass *parent_class;
146 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
149 gtk_clipboard_get_type (void)
151 static GType clipboard_type = 0;
155 const GTypeInfo clipboard_info =
157 sizeof (GtkClipboardClass),
158 NULL, /* base_init */
159 NULL, /* base_finalize */
160 (GClassInitFunc) gtk_clipboard_class_init,
161 NULL, /* class_finalize */
162 NULL, /* class_data */
163 sizeof (GtkClipboard),
165 (GInstanceInitFunc) NULL,
168 clipboard_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkClipboard"),
172 return clipboard_type;
176 gtk_clipboard_class_init (GtkClipboardClass *class)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
180 parent_class = g_type_class_peek_parent (class);
182 gobject_class->finalize = gtk_clipboard_finalize;
184 class->owner_change = gtk_clipboard_owner_change;
186 clipboard_signals[OWNER_CHANGE] =
187 g_signal_new (I_("owner_change"),
188 G_TYPE_FROM_CLASS (gobject_class),
190 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
192 _gtk_marshal_VOID__BOXED,
194 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
198 gtk_clipboard_finalize (GObject *object)
200 GtkClipboard *clipboard;
203 clipboard = GTK_CLIPBOARD (object);
205 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
206 if (g_slist_index (clipboards, clipboard) >= 0)
207 g_warning ("GtkClipboard prematurely finalized");
209 clipboard_unset (clipboard);
211 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
212 clipboards = g_slist_remove (clipboards, clipboard);
213 g_object_set_data (G_OBJECT (clipboard->display), I_("gtk-clipboard-list"), clipboards);
215 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
216 g_main_loop_quit (clipboard->store_loop);
218 if (clipboard->store_timeout != 0)
219 g_source_remove (clipboard->store_timeout);
221 g_free (clipboard->storable_targets);
223 G_OBJECT_CLASS (parent_class)->finalize (object);
227 clipboard_display_closed (GdkDisplay *display,
229 GtkClipboard *clipboard)
233 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
234 g_object_run_dispose (G_OBJECT (clipboard));
235 clipboards = g_slist_remove (clipboards, clipboard);
236 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
237 g_object_unref (clipboard);
241 * gtk_clipboard_get_for_display:
242 * @display: the display for which the clipboard is to be retrieved or created
243 * @selection: a #GdkAtom which identifies the clipboard
246 * Returns the clipboard object for the given selection.
247 * Cut/copy/paste menu items and keyboard shortcuts should use
248 * the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
249 * (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
250 * for backwards compatibility reasons.)
251 * The currently-selected object or text should be provided on the clipboard
252 * identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
253 * conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
254 * to the default clipboard, i.e. they copy the selection to what the
255 * user sees as the clipboard.
257 * (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
258 * ("CLIPBOARD", FALSE)</literal>. See <ulink
259 * url="http://www.freedesktop.org/Standards/clipboards-spec">
260 * http://www.freedesktop.org/Standards/clipboards-spec</ulink>
261 * for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
262 * selections under the X window system. On Win32 the
263 * #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
265 * It's possible to have arbitrary named clipboards; if you do invent
266 * new clipboards, you should prefix the selection name with an
267 * underscore (because the ICCCM requires that nonstandard atoms are
268 * underscore-prefixed), and namespace it as well. For example,
269 * if your application called "Foo" has a special-purpose
270 * clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
272 * Return value: the appropriate clipboard object. If no
273 * clipboard already exists, a new one will
274 * be created. Once a clipboard object has
275 * been created, it is persistent and, since
276 * it is owned by GTK+, must not be freed or
282 gtk_clipboard_get_for_display (GdkDisplay *display,
285 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
286 g_return_val_if_fail (!display->closed, NULL);
288 return clipboard_peek (display, selection, FALSE);
293 * gtk_clipboard_get():
294 * @selection: a #GdkAtom which identifies the clipboard
297 * Returns the clipboard object for the given selection.
298 * See gtk_clipboard_get_for_display() for complete details.
300 * Return value: the appropriate clipboard object. If no
301 * clipboard already exists, a new one will
302 * be created. Once a clipboard object has
303 * been created, it is persistent and, since
304 * it is owned by GTK+, must not be freed or
308 gtk_clipboard_get (GdkAtom selection)
310 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
314 clipboard_owner_destroyed (gpointer data)
316 GSList *clipboards = data;
319 tmp_list = clipboards;
322 GtkClipboard *clipboard = tmp_list->data;
324 clipboard->get_func = NULL;
325 clipboard->clear_func = NULL;
326 clipboard->user_data = NULL;
327 clipboard->have_owner = FALSE;
329 gtk_clipboard_clear (clipboard);
331 tmp_list = tmp_list->next;
334 g_slist_free (clipboards);
338 clipboard_add_owner_notify (GtkClipboard *clipboard)
340 if (!clipboards_owned_key_id)
341 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
343 if (clipboard->have_owner)
344 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
345 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
346 clipboards_owned_key_id),
348 clipboard_owner_destroyed);
352 clipboard_remove_owner_notify (GtkClipboard *clipboard)
354 if (clipboard->have_owner)
355 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
356 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
357 clipboards_owned_key_id),
359 clipboard_owner_destroyed);
363 gtk_clipboard_set_contents (GtkClipboard *clipboard,
364 const GtkTargetEntry *targets,
366 GtkClipboardGetFunc get_func,
367 GtkClipboardClearFunc clear_func,
371 GtkClipboardOwner *owner;
373 NSAutoreleasePool *pool;
375 pool = [[NSAutoreleasePool alloc] init];
377 owner = [[GtkClipboardOwner alloc] initWithClipboard:clipboard];
379 types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets);
381 clipboard->user_data = user_data;
382 clipboard->have_owner = have_owner;
384 clipboard_add_owner_notify (clipboard);
385 clipboard->get_func = get_func;
386 clipboard->clear_func = clear_func;
388 [clipboard->pasteboard declareTypes:types owner:owner];
396 * gtk_clipboard_set_with_data:
397 * @clipboard: a #GtkClipboard
398 * @targets: array containing information about the available forms for the
400 * @n_targets: number of elements in @targets
401 * @get_func: function to call to get the actual clipboard data
402 * @clear_func: when the clipboard contents are set again, this function will
403 * be called, and @get_func will not be subsequently called.
404 * @user_data: user data to pass to @get_func and @clear_func.
406 * Virtually sets the contents of the specified clipboard by providing
407 * a list of supported formats for the clipboard data and a function
408 * to call to get the actual data when it is requested.
410 * Return value: %TRUE if setting the clipboard data succeeded. If setting
411 * the clipboard data failed the provided callback functions
415 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
416 const GtkTargetEntry *targets,
418 GtkClipboardGetFunc get_func,
419 GtkClipboardClearFunc clear_func,
422 g_return_val_if_fail (clipboard != NULL, FALSE);
423 g_return_val_if_fail (targets != NULL, FALSE);
424 g_return_val_if_fail (get_func != NULL, FALSE);
426 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
427 get_func, clear_func, user_data,
432 * gtk_clipboard_set_with_owner:
433 * @clipboard: a #GtkClipboard
434 * @targets: array containing information about the available forms for the
436 * @n_targets: number of elements in @targets
437 * @get_func: function to call to get the actual clipboard data
438 * @clear_func: when the clipboard contents are set again, this function will
439 * be called, and @get_func will not be subsequently called.
440 * @owner: an object that "owns" the data. This object will be passed
441 * to the callbacks when called.
443 * Virtually sets the contents of the specified clipboard by providing
444 * a list of supported formats for the clipboard data and a function
445 * to call to get the actual data when it is requested.
447 * The difference between this function and gtk_clipboard_set_with_data()
448 * is that instead of an generic @user_data pointer, a #GObject is passed
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_owner (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);
466 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
468 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
469 get_func, clear_func, owner,
474 * gtk_clipboard_get_owner:
475 * @clipboard: a #GtkClipboard
477 * If the clipboard contents callbacks were set with
478 * gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
479 * gtk_clipboard_clear() has not subsequently called, returns the owner set
480 * by gtk_clipboard_set_with_owner().
482 * Return value: the owner of the clipboard, if any; otherwise %NULL.
485 gtk_clipboard_get_owner (GtkClipboard *clipboard)
487 g_return_val_if_fail (clipboard != NULL, NULL);
489 if (clipboard->have_owner)
490 return clipboard->user_data;
496 clipboard_unset (GtkClipboard *clipboard)
498 GtkClipboardClearFunc old_clear_func;
500 gboolean old_have_owner;
501 gint old_n_storable_targets;
503 old_clear_func = clipboard->clear_func;
504 old_data = clipboard->user_data;
505 old_have_owner = clipboard->have_owner;
506 old_n_storable_targets = clipboard->n_storable_targets;
510 clipboard->have_owner = FALSE;
513 clipboard->n_storable_targets = -1;
514 g_free (clipboard->storable_targets);
515 clipboard->storable_targets = NULL;
517 clipboard->get_func = NULL;
518 clipboard->clear_func = NULL;
519 clipboard->user_data = NULL;
522 old_clear_func (clipboard, old_data);
524 /* If we've transferred the clipboard data to the manager,
527 if (old_have_owner &&
528 old_n_storable_targets != -1)
529 g_object_unref (old_data);
533 * gtk_clipboard_clear:
534 * @clipboard: a #GtkClipboard
536 * Clears the contents of the clipboard. Generally this should only
537 * be called between the time you call gtk_clipboard_set_with_owner()
538 * or gtk_clipboard_set_with_data(),
539 * and when the @clear_func you supplied is called. Otherwise, the
540 * clipboard may be owned by someone else.
543 gtk_clipboard_clear (GtkClipboard *clipboard)
545 [clipboard->pasteboard declareTypes:nil owner:nil];
549 text_get_func (GtkClipboard *clipboard,
550 GtkSelectionData *selection_data,
554 gtk_selection_data_set_text (selection_data, data, -1);
558 text_clear_func (GtkClipboard *clipboard,
565 * gtk_clipboard_set_text:
566 * @clipboard: a #GtkClipboard object
567 * @text: a UTF-8 string.
568 * @len: length of @text, in bytes, or -1, in which case
569 * the length will be determined with <function>strlen()</function>.
571 * Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
572 * make a copy of the text and take responsibility for responding
573 * for requests for the text, and for converting the text into
574 * the requested format.
577 gtk_clipboard_set_text (GtkClipboard *clipboard,
581 GtkTargetEntry target = { "UTF8_STRING", 0, 0 };
583 g_return_if_fail (clipboard != NULL);
584 g_return_if_fail (text != NULL);
589 gtk_clipboard_set_with_data (clipboard,
591 text_get_func, text_clear_func,
592 g_strndup (text, len));
593 gtk_clipboard_set_can_store (clipboard, NULL, 0);
598 pixbuf_get_func (GtkClipboard *clipboard,
599 GtkSelectionData *selection_data,
603 gtk_selection_data_set_pixbuf (selection_data, data);
607 pixbuf_clear_func (GtkClipboard *clipboard,
610 g_object_unref (data);
614 * gtk_clipboard_set_image:
615 * @clipboard: a #GtkClipboard object
616 * @pixbuf: a #GdkPixbuf
618 * Sets the contents of the clipboard to the given #GdkPixbuf.
619 * GTK+ will take responsibility for responding for requests
620 * for the image, and for converting the image into the
626 gtk_clipboard_set_image (GtkClipboard *clipboard,
631 GtkTargetEntry *targets;
634 g_return_if_fail (clipboard != NULL);
635 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
637 list = gtk_target_list_new (NULL, 0);
638 gtk_target_list_add_image_targets (list, 0, TRUE);
640 n_targets = g_list_length (list->list);
641 targets = g_new0 (GtkTargetEntry, n_targets);
642 for (l = list->list, i = 0; l; l = l->next, i++)
644 GtkTargetPair *pair = (GtkTargetPair *)l->data;
645 targets[i].target = gdk_atom_name (pair->target);
648 gtk_clipboard_set_with_data (clipboard,
650 pixbuf_get_func, pixbuf_clear_func,
651 g_object_ref (pixbuf));
652 gtk_clipboard_set_can_store (clipboard, NULL, 0);
654 for (i = 0; i < n_targets; i++)
655 g_free (targets[i].target);
657 gtk_target_list_unref (list);
661 * gtk_clipboard_request_contents:
662 * @clipboard: a #GtkClipboard
663 * @target: an atom representing the form into which the clipboard
664 * owner should convert the selection.
665 * @callback: A function to call when the results are received
666 * (or the retrieval fails). If the retrieval fails
667 * the length field of @selection_data will be
669 * @user_data: user data to pass to @callback
671 * Requests the contents of clipboard as the given target.
672 * When the results of the result are later received the supplied callback
676 gtk_clipboard_request_contents (GtkClipboard *clipboard,
678 GtkClipboardReceivedFunc callback,
681 GtkSelectionData *data;
683 data = gtk_clipboard_wait_for_contents (clipboard, target);
685 callback (clipboard, data, user_data);
687 gtk_selection_data_free (data);
691 * gtk_clipboard_request_text:
692 * @clipboard: a #GtkClipboard
693 * @callback: a function to call when the text is received,
694 * or the retrieval fails. (It will always be called
695 * one way or the other.)
696 * @user_data: user data to pass to @callback.
698 * Requests the contents of the clipboard as text. When the text is
699 * later received, it will be converted to UTF-8 if necessary, and
700 * @callback will be called.
702 * The @text parameter to @callback will contain the resulting text if
703 * the request succeeded, or %NULL if it failed. This could happen for
704 * various reasons, in particular if the clipboard was empty or if the
705 * contents of the clipboard could not be converted into text form.
708 gtk_clipboard_request_text (GtkClipboard *clipboard,
709 GtkClipboardTextReceivedFunc callback,
712 gchar *data = gtk_clipboard_wait_for_text (clipboard);
714 callback (clipboard, data, user_data);
720 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
721 GtkTextBuffer *buffer,
722 GtkClipboardRichTextReceivedFunc callback,
725 /* FIXME: Implement */
730 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
731 GtkTextBuffer *buffer,
735 /* FIXME: Implement */
740 * gtk_clipboard_request_image:
741 * @clipboard: a #GtkClipboard
742 * @callback: a function to call when the image is received,
743 * or the retrieval fails. (It will always be called
744 * one way or the other.)
745 * @user_data: user data to pass to @callback.
747 * Requests the contents of the clipboard as image. When the image is
748 * later received, it will be converted to a #GdkPixbuf, and
749 * @callback will be called.
751 * The @pixbuf parameter to @callback will contain the resulting
752 * #GdkPixbuf if the request succeeded, or %NULL if it failed. This
753 * could happen for various reasons, in particular if the clipboard
754 * was empty or if the contents of the clipboard could not be
755 * converted into an image.
760 gtk_clipboard_request_image (GtkClipboard *clipboard,
761 GtkClipboardImageReceivedFunc callback,
764 GdkPixbuf *pixbuf = gtk_clipboard_wait_for_image (clipboard);
766 callback (clipboard, pixbuf, user_data);
769 g_object_unref (pixbuf);
773 * gtk_clipboard_request_targets:
774 * @clipboard: a #GtkClipboard
775 * @callback: a function to call when the targets are received,
776 * or the retrieval fails. (It will always be called
777 * one way or the other.)
778 * @user_data: user data to pass to @callback.
780 * Requests the contents of the clipboard as list of supported targets.
781 * When the list is later received, @callback will be called.
783 * The @targets parameter to @callback will contain the resulting targets if
784 * the request succeeded, or %NULL if it failed.
789 gtk_clipboard_request_targets (GtkClipboard *clipboard,
790 GtkClipboardTargetsReceivedFunc callback,
796 gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets);
798 callback (clipboard, targets, n_targets, user_data);
803 * gtk_clipboard_wait_for_contents:
804 * @clipboard: a #GtkClipboard
805 * @target: an atom representing the form into which the clipboard
806 * owner should convert the selection.
808 * Requests the contents of the clipboard using the given target.
809 * This function waits for the data to be received using the main
810 * loop, so events, timeouts, etc, may be dispatched during the wait.
812 * Return value: a newly-allocated #GtkSelectionData object or %NULL
813 * if retrieving the given target failed. If non-%NULL,
814 * this value must be freed with gtk_selection_data_free()
815 * when you are finished with it.
818 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
821 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
824 GtkSelectionData *selection_data = NULL;
826 if (target == gdk_atom_intern_static_string ("TARGETS"))
828 NSArray *types = [clipboard->pasteboard types];
830 GList *atom_list, *l;
833 count = [types count];
834 atom_list = _gtk_quartz_pasteboard_types_to_atom_list (types);
836 selection_data = g_new (GtkSelectionData, 1);
837 selection_data->selection = clipboard->selection;
838 selection_data->target = target;
839 selection_data->type = GDK_SELECTION_TYPE_ATOM;
840 selection_data->format = 32;
841 selection_data->length = count * sizeof (GdkAtom);
843 atoms = g_malloc (selection_data->length + 1);
845 for (l = atom_list, i = 0; l ; l = l->next, i++)
846 atoms[i] = GDK_POINTER_TO_ATOM (l->data);
848 selection_data->data = (guchar *)atoms;
849 selection_data->data[selection_data->length] = '\0';
853 g_list_free (atom_list);
854 return selection_data;
857 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (clipboard->pasteboard,
859 clipboard->selection);
862 return selection_data;
866 * gtk_clipboard_wait_for_text:
867 * @clipboard: a #GtkClipboard
869 * Requests the contents of the clipboard as text and converts
870 * the result to UTF-8 if necessary. This function waits for
871 * the data to be received using the main loop, so events,
872 * timeouts, etc, may be dispatched during the wait.
874 * Return value: a newly-allocated UTF-8 string which must
875 * be freed with g_free(), or %NULL if retrieving
876 * the selection data failed. (This could happen
877 * for various reasons, in particular if the
878 * clipboard was empty or if the contents of the
879 * clipboard could not be converted into text form.)
882 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
884 GtkSelectionData *data;
887 data = gtk_clipboard_wait_for_contents (clipboard,
888 gdk_atom_intern_static_string ("UTF8_STRING"));
890 result = (gchar *)gtk_selection_data_get_text (data);
892 gtk_selection_data_free (data);
898 * gtk_clipboard_wait_for_image:
899 * @clipboard: a #GtkClipboard
901 * Requests the contents of the clipboard as image and converts
902 * the result to a #GdkPixbuf. This function waits for
903 * the data to be received using the main loop, so events,
904 * timeouts, etc, may be dispatched during the wait.
906 * Return value: a newly-allocated #GdkPixbuf object which must
907 * be disposed with g_object_unref(), or %NULL if
908 * retrieving the selection data failed. (This
909 * could happen for various reasons, in particular
910 * if the clipboard was empty or if the contents of
911 * the clipboard could not be converted into an image.)
916 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
918 const gchar *priority[] = { "image/png", "image/tiff", "image/jpeg", "image/gif", "image/bmp" };
920 GtkSelectionData *data;
922 for (i = 0; i < G_N_ELEMENTS (priority); i++)
924 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string (priority[i]));
928 GdkPixbuf *pixbuf = gtk_selection_data_get_pixbuf (data);
930 gtk_selection_data_free (data);
940 * gtk_clipboard_get_display:
941 * @clipboard: a #GtkClipboard
943 * Gets the #GdkDisplay associated with @clipboard
945 * Return value: the #GdkDisplay associated with @clipboard
950 gtk_clipboard_get_display (GtkClipboard *clipboard)
952 g_return_val_if_fail (clipboard != NULL, NULL);
954 return clipboard->display;
958 * gtk_clipboard_wait_is_text_available:
959 * @clipboard: a #GtkClipboard
961 * Test to see if there is text available to be pasted
962 * This is done by requesting the TARGETS atom and checking
963 * if it contains any of the supported text targets. This function
964 * waits for the data to be received using the main loop, so events,
965 * timeouts, etc, may be dispatched during the wait.
967 * This function is a little faster than calling
968 * gtk_clipboard_wait_for_text() since it doesn't need to retrieve
971 * Return value: %TRUE is there is text available, %FALSE otherwise.
974 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
976 GtkSelectionData *data;
977 gboolean result = FALSE;
979 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
982 result = gtk_selection_data_targets_include_text (data);
983 gtk_selection_data_free (data);
990 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
991 GtkTextBuffer *buffer)
993 GtkSelectionData *data;
994 gboolean result = FALSE;
996 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
997 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
999 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1002 result = gtk_selection_data_targets_include_rich_text (data, buffer);
1003 gtk_selection_data_free (data);
1010 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1012 GtkSelectionData *data;
1013 gboolean result = FALSE;
1015 data = gtk_clipboard_wait_for_contents (clipboard,
1016 gdk_atom_intern_static_string ("TARGETS"));
1019 result = gtk_selection_data_targets_include_image (data, FALSE);
1020 gtk_selection_data_free (data);
1027 * gtk_clipboard_wait_for_targets
1028 * @clipboard: a #GtkClipboard
1029 * @targets: location to store an array of targets. The result
1030 * stored here must be freed with g_free().
1031 * @n_targets: location to store number of items in @targets.
1033 * Returns a list of targets that are present on the clipboard, or %NULL
1034 * if there aren't any targets available. The returned list must be
1035 * freed with g_free().
1036 * This function waits for the data to be received using the main
1037 * loop, so events, timeouts, etc, may be dispatched during the wait.
1039 * Return value: %TRUE if any targets are present on the clipboard,
1045 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1049 GtkSelectionData *data;
1050 gboolean result = FALSE;
1052 g_return_val_if_fail (clipboard != NULL, FALSE);
1054 /* If the display supports change notification we cache targets */
1055 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1056 clipboard->n_cached_targets != -1)
1059 *n_targets = clipboard->n_cached_targets;
1062 *targets = g_memdup (clipboard->cached_targets,
1063 clipboard->n_cached_targets * sizeof (GdkAtom));
1074 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1078 GdkAtom *tmp_targets;
1081 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1083 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1085 clipboard->n_cached_targets = tmp_n_targets;
1086 clipboard->cached_targets = g_memdup (tmp_targets,
1087 tmp_n_targets * sizeof (GdkAtom));
1091 *n_targets = tmp_n_targets;
1094 *targets = tmp_targets;
1096 g_free (tmp_targets);
1098 gtk_selection_data_free (data);
1104 static GtkClipboard *
1105 clipboard_peek (GdkDisplay *display,
1107 gboolean only_if_exists)
1109 GtkClipboard *clipboard = NULL;
1113 if (selection == GDK_NONE)
1114 selection = GDK_SELECTION_CLIPBOARD;
1116 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1118 tmp_list = clipboards;
1121 clipboard = tmp_list->data;
1122 if (clipboard->selection == selection)
1125 tmp_list = tmp_list->next;
1128 if (!tmp_list && !only_if_exists)
1130 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1131 NSString *pasteboard_name;
1132 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1134 if (selection == GDK_SELECTION_CLIPBOARD)
1135 pasteboard_name = NSGeneralPboard;
1138 char *atom_string = gdk_atom_name (selection);
1140 pasteboard_name = [NSString stringWithFormat:@"_GTK_%@",
1141 [NSString stringWithUTF8String:atom_string]];
1142 g_free (atom_string);
1145 clipboard->pasteboard = [NSPasteboard pasteboardWithName:pasteboard_name];
1149 clipboard->selection = selection;
1150 clipboard->display = display;
1151 clipboard->n_cached_targets = -1;
1152 clipboard->n_storable_targets = -1;
1153 clipboards = g_slist_prepend (clipboards, clipboard);
1154 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1155 g_signal_connect (display, "closed",
1156 G_CALLBACK (clipboard_display_closed), clipboard);
1157 gdk_display_request_selection_notification (display, selection);
1164 gtk_clipboard_owner_change (GtkClipboard *clipboard,
1165 GdkEventOwnerChange *event)
1167 if (clipboard->n_cached_targets != -1)
1169 clipboard->n_cached_targets = -1;
1170 g_free (clipboard->cached_targets);
1175 * gtk_clipboard_wait_is_target_available:
1176 * @clipboard: a #GtkClipboard
1177 * @target: A #GdkAtom indicating which target to look for.
1179 * Checks if a clipboard supports pasting data of a given type. This
1180 * function can be used to determine if a "Paste" menu item should be
1181 * insensitive or not.
1183 * If you want to see if there's text available on the clipboard, use
1184 * gtk_clipboard_wait_is_text_available () instead.
1186 * Return value: %TRUE if the target is available, %FALSE otherwise.
1191 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1196 gboolean retval = FALSE;
1198 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1201 for (i = 0; i < n_targets; i++)
1203 if (targets[i] == target)
1216 * _gtk_clipboard_handle_event:
1217 * @event: a owner change event
1219 * Emits the ::owner_change signal on the appropriate @clipboard.
1224 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1230 * gtk_clipboard_set_can_store:
1231 * @clipboard: a #GtkClipboard
1232 * @targets: array containing information about which forms should be stored
1233 * or %NULL to indicate that all forms should be stored.
1234 * @n_targets: number of elements in @targets
1236 * Hints that the clipboard data should be stored somewhere when the
1237 * application exits or when gtk_clipboard_store () is called.
1239 * This value is reset when the clipboard owner changes.
1240 * Where the clipboard data is stored is platform dependent,
1241 * see gdk_display_store_clipboard () for more information.
1246 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1247 const GtkTargetEntry *targets,
1250 /* FIXME: Implement */
1254 * gtk_clipboard_store:
1255 * @clipboard: a #GtkClipboard
1257 * Stores the current clipboard data somewhere so that it will stay
1258 * around after the application has quit.
1263 gtk_clipboard_store (GtkClipboard *clipboard)
1265 /* FIXME: Implement */
1268 /* Stores all clipboard selections on all displays, called from
1272 _gtk_clipboard_store_all (void)
1274 /* FIXME: Implement */
1277 #define __GTK_CLIPBOARD_C__
1278 #include "gtkaliasdef.c"