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 "gtkselectionprivate.h"
35 #include "gtkquartz.h"
43 typedef struct _GtkClipboardClass GtkClipboardClass;
47 GObject parent_instance;
49 NSPasteboard *pasteboard;
53 GtkClipboardGetFunc get_func;
54 GtkClipboardClearFunc clear_func;
57 GtkTargetList *target_list;
59 gboolean have_selection;
62 GdkAtom *cached_targets;
63 gint n_cached_targets;
65 guint notify_signal_id;
66 gboolean storing_selection;
67 GMainLoop *store_loop;
69 gint n_storable_targets;
70 GdkAtom *storable_targets;
73 struct _GtkClipboardClass
75 GObjectClass parent_class;
77 void (*owner_change) (GtkClipboard *clipboard,
78 GdkEventOwnerChange *event);
81 @interface GtkClipboardOwner : NSObject {
82 GtkClipboard *clipboard;
84 GtkClipboardGetFunc get_func;
85 GtkClipboardClearFunc clear_func;
92 @implementation GtkClipboardOwner
93 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
95 GtkSelectionData selection_data;
98 if (!clipboard->target_list)
101 memset (&selection_data, 0, sizeof (GtkSelectionData));
103 selection_data.selection = clipboard->selection;
104 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
105 selection_data.display = gdk_display_get_default ();
106 selection_data.length = -1;
108 if (gtk_target_list_find (clipboard->target_list, selection_data.target, &info))
110 clipboard->get_func (clipboard, &selection_data,
112 clipboard->user_data);
114 _gtk_quartz_set_selection_data_for_pasteboard (clipboard->pasteboard,
117 g_free (selection_data.data);
121 - (void)pasteboardChangedOwner:(NSPasteboard *)sender
124 clear_func (clipboard, user_data);
129 - (id)initWithClipboard:(GtkClipboard *)aClipboard
135 clipboard = aClipboard;
143 static void gtk_clipboard_class_init (GtkClipboardClass *class);
144 static void gtk_clipboard_finalize (GObject *object);
145 static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
146 GdkEventOwnerChange *event);
148 static void clipboard_unset (GtkClipboard *clipboard);
149 static GtkClipboard *clipboard_peek (GdkDisplay *display,
151 gboolean only_if_exists);
153 static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
154 static GQuark clipboards_owned_key_id = 0;
156 static GObjectClass *parent_class;
157 static guint clipboard_signals[LAST_SIGNAL] = { 0 };
160 gtk_clipboard_get_type (void)
162 static GType clipboard_type = 0;
166 const GTypeInfo clipboard_info =
168 sizeof (GtkClipboardClass),
169 NULL, /* base_init */
170 NULL, /* base_finalize */
171 (GClassInitFunc) gtk_clipboard_class_init,
172 NULL, /* class_finalize */
173 NULL, /* class_data */
174 sizeof (GtkClipboard),
176 (GInstanceInitFunc) NULL,
179 clipboard_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkClipboard"),
183 return clipboard_type;
187 gtk_clipboard_class_init (GtkClipboardClass *class)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
191 parent_class = g_type_class_peek_parent (class);
193 gobject_class->finalize = gtk_clipboard_finalize;
195 class->owner_change = gtk_clipboard_owner_change;
197 clipboard_signals[OWNER_CHANGE] =
198 g_signal_new (I_("owner-change"),
199 G_TYPE_FROM_CLASS (gobject_class),
201 G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
203 _gtk_marshal_VOID__BOXED,
205 GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
209 gtk_clipboard_finalize (GObject *object)
211 GtkClipboard *clipboard;
214 clipboard = GTK_CLIPBOARD (object);
216 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
217 if (g_slist_index (clipboards, clipboard) >= 0)
218 g_warning ("GtkClipboard prematurely finalized");
220 clipboard_unset (clipboard);
222 clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
223 clipboards = g_slist_remove (clipboards, clipboard);
224 g_object_set_data (G_OBJECT (clipboard->display), I_("gtk-clipboard-list"), clipboards);
226 if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
227 g_main_loop_quit (clipboard->store_loop);
229 if (clipboard->store_timeout != 0)
230 g_source_remove (clipboard->store_timeout);
232 g_free (clipboard->storable_targets);
234 G_OBJECT_CLASS (parent_class)->finalize (object);
238 clipboard_display_closed (GdkDisplay *display,
240 GtkClipboard *clipboard)
244 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
245 g_object_run_dispose (G_OBJECT (clipboard));
246 clipboards = g_slist_remove (clipboards, clipboard);
247 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
248 g_object_unref (clipboard);
252 gtk_clipboard_get_for_display (GdkDisplay *display,
255 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
256 g_return_val_if_fail (!gdk_display_is_closed (display), NULL);
258 return clipboard_peek (display, selection, FALSE);
262 gtk_clipboard_get (GdkAtom selection)
264 return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
268 clipboard_owner_destroyed (gpointer data)
270 GSList *clipboards = data;
273 tmp_list = clipboards;
276 GtkClipboard *clipboard = tmp_list->data;
278 clipboard->get_func = NULL;
279 clipboard->clear_func = NULL;
280 clipboard->user_data = NULL;
281 clipboard->have_owner = FALSE;
283 if (clipboard->target_list)
285 gtk_target_list_unref (clipboard->target_list);
286 clipboard->target_list = NULL;
289 gtk_clipboard_clear (clipboard);
291 tmp_list = tmp_list->next;
294 g_slist_free (clipboards);
298 clipboard_add_owner_notify (GtkClipboard *clipboard)
300 if (!clipboards_owned_key_id)
301 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
303 if (clipboard->have_owner)
304 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
305 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
306 clipboards_owned_key_id),
308 clipboard_owner_destroyed);
312 clipboard_remove_owner_notify (GtkClipboard *clipboard)
314 if (clipboard->have_owner)
315 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
316 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
317 clipboards_owned_key_id),
319 clipboard_owner_destroyed);
323 gtk_clipboard_set_contents (GtkClipboard *clipboard,
324 const GtkTargetEntry *targets,
326 GtkClipboardGetFunc get_func,
327 GtkClipboardClearFunc clear_func,
331 GtkClipboardOwner *owner;
333 NSAutoreleasePool *pool;
335 pool = [[NSAutoreleasePool alloc] init];
337 owner = [[GtkClipboardOwner alloc] initWithClipboard:clipboard];
339 if (!(clipboard->have_owner && have_owner) ||
340 clipboard->user_data != user_data)
342 clipboard_unset (clipboard);
344 if (clipboard->get_func)
346 /* Calling unset() caused the clipboard contents to be reset!
347 * Avoid leaking and return
349 if (!(clipboard->have_owner && have_owner) ||
350 clipboard->user_data != user_data)
352 (*clear_func) (clipboard, user_data);
364 clipboard->user_data = user_data;
365 clipboard->have_owner = have_owner;
367 clipboard_add_owner_notify (clipboard);
368 clipboard->get_func = get_func;
369 clipboard->clear_func = clear_func;
371 if (clipboard->target_list)
372 gtk_target_list_unref (clipboard->target_list);
373 clipboard->target_list = gtk_target_list_new (targets, n_targets);
375 types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets);
377 [clipboard->pasteboard declareTypes:[types allObjects] owner:owner];
385 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
386 const GtkTargetEntry *targets,
388 GtkClipboardGetFunc get_func,
389 GtkClipboardClearFunc clear_func,
392 g_return_val_if_fail (clipboard != NULL, FALSE);
393 g_return_val_if_fail (targets != NULL, FALSE);
394 g_return_val_if_fail (get_func != NULL, FALSE);
396 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
397 get_func, clear_func, user_data,
402 gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
403 const GtkTargetEntry *targets,
405 GtkClipboardGetFunc get_func,
406 GtkClipboardClearFunc clear_func,
409 g_return_val_if_fail (clipboard != NULL, FALSE);
410 g_return_val_if_fail (targets != NULL, FALSE);
411 g_return_val_if_fail (get_func != NULL, FALSE);
412 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
414 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
415 get_func, clear_func, owner,
420 gtk_clipboard_get_owner (GtkClipboard *clipboard)
422 g_return_val_if_fail (clipboard != NULL, NULL);
424 if (clipboard->have_owner)
425 return clipboard->user_data;
431 clipboard_unset (GtkClipboard *clipboard)
433 GtkClipboardClearFunc old_clear_func;
435 gboolean old_have_owner;
436 gint old_n_storable_targets;
438 old_clear_func = clipboard->clear_func;
439 old_data = clipboard->user_data;
440 old_have_owner = clipboard->have_owner;
441 old_n_storable_targets = clipboard->n_storable_targets;
445 clipboard_remove_owner_notify (clipboard);
446 clipboard->have_owner = FALSE;
449 clipboard->n_storable_targets = -1;
450 g_free (clipboard->storable_targets);
451 clipboard->storable_targets = NULL;
453 clipboard->get_func = NULL;
454 clipboard->clear_func = NULL;
455 clipboard->user_data = NULL;
458 old_clear_func (clipboard, old_data);
460 if (clipboard->target_list)
462 gtk_target_list_unref (clipboard->target_list);
463 clipboard->target_list = NULL;
466 /* If we've transferred the clipboard data to the manager,
469 if (old_have_owner &&
470 old_n_storable_targets != -1)
471 g_object_unref (old_data);
475 gtk_clipboard_clear (GtkClipboard *clipboard)
477 [clipboard->pasteboard declareTypes:nil owner:nil];
481 text_get_func (GtkClipboard *clipboard,
482 GtkSelectionData *selection_data,
486 gtk_selection_data_set_text (selection_data, data, -1);
490 text_clear_func (GtkClipboard *clipboard,
497 gtk_clipboard_set_text (GtkClipboard *clipboard,
501 GtkTargetEntry target = { "UTF8_STRING", 0, 0 };
503 g_return_if_fail (clipboard != NULL);
504 g_return_if_fail (text != NULL);
509 gtk_clipboard_set_with_data (clipboard,
511 text_get_func, text_clear_func,
512 g_strndup (text, len));
513 gtk_clipboard_set_can_store (clipboard, NULL, 0);
518 pixbuf_get_func (GtkClipboard *clipboard,
519 GtkSelectionData *selection_data,
523 gtk_selection_data_set_pixbuf (selection_data, data);
527 pixbuf_clear_func (GtkClipboard *clipboard,
530 g_object_unref (data);
534 gtk_clipboard_set_image (GtkClipboard *clipboard,
539 GtkTargetEntry *targets;
542 g_return_if_fail (clipboard != NULL);
543 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
545 list = gtk_target_list_new (NULL, 0);
546 gtk_target_list_add_image_targets (list, 0, TRUE);
548 n_targets = g_list_length (list->list);
549 targets = g_new0 (GtkTargetEntry, n_targets);
550 for (l = list->list, i = 0; l; l = l->next, i++)
552 GtkTargetPair *pair = (GtkTargetPair *)l->data;
553 targets[i].target = gdk_atom_name (pair->target);
556 gtk_clipboard_set_with_data (clipboard,
558 pixbuf_get_func, pixbuf_clear_func,
559 g_object_ref (pixbuf));
560 gtk_clipboard_set_can_store (clipboard, NULL, 0);
562 for (i = 0; i < n_targets; i++)
563 g_free (targets[i].target);
565 gtk_target_list_unref (list);
569 gtk_clipboard_request_contents (GtkClipboard *clipboard,
571 GtkClipboardReceivedFunc callback,
574 GtkSelectionData *data;
576 data = gtk_clipboard_wait_for_contents (clipboard, target);
578 callback (clipboard, data, user_data);
580 gtk_selection_data_free (data);
584 gtk_clipboard_request_text (GtkClipboard *clipboard,
585 GtkClipboardTextReceivedFunc callback,
588 gchar *data = gtk_clipboard_wait_for_text (clipboard);
590 callback (clipboard, data, user_data);
596 gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
597 GtkTextBuffer *buffer,
598 GtkClipboardRichTextReceivedFunc callback,
601 /* FIXME: Implement */
606 gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
607 GtkTextBuffer *buffer,
611 /* FIXME: Implement */
616 gtk_clipboard_request_image (GtkClipboard *clipboard,
617 GtkClipboardImageReceivedFunc callback,
620 GdkPixbuf *pixbuf = gtk_clipboard_wait_for_image (clipboard);
622 callback (clipboard, pixbuf, user_data);
625 g_object_unref (pixbuf);
629 gtk_clipboard_request_uris (GtkClipboard *clipboard,
630 GtkClipboardURIReceivedFunc callback,
633 gchar **uris = gtk_clipboard_wait_for_uris (clipboard);
635 callback (clipboard, uris, user_data);
641 gtk_clipboard_request_targets (GtkClipboard *clipboard,
642 GtkClipboardTargetsReceivedFunc callback,
648 gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets);
650 callback (clipboard, targets, n_targets, user_data);
655 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
658 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
659 GtkSelectionData *selection_data = NULL;
661 if (target == gdk_atom_intern_static_string ("TARGETS"))
663 NSArray *types = [clipboard->pasteboard types];
665 GList *atom_list, *l;
668 length = [types count] * sizeof (GdkAtom);
670 selection_data = g_slice_new0 (GtkSelectionData);
671 selection_data->selection = clipboard->selection;
672 selection_data->target = target;
674 atoms = g_malloc (length);
676 atom_list = _gtk_quartz_pasteboard_types_to_atom_list (types);
677 for (l = atom_list, i = 0; l ; l = l->next, i++)
678 atoms[i] = GDK_POINTER_TO_ATOM (l->data);
679 g_list_free (atom_list);
681 gtk_selection_data_set (selection_data,
682 GDK_SELECTION_TYPE_ATOM, 32,
683 (guchar *)atoms, length);
687 return selection_data;
690 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (clipboard->pasteboard,
692 clipboard->selection);
696 return selection_data;
700 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
702 GtkSelectionData *data;
705 data = gtk_clipboard_wait_for_contents (clipboard,
706 gdk_atom_intern_static_string ("UTF8_STRING"));
708 result = (gchar *)gtk_selection_data_get_text (data);
710 gtk_selection_data_free (data);
716 gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
718 const gchar *priority[] = { "image/png", "image/tiff", "image/jpeg", "image/gif", "image/bmp" };
720 GtkSelectionData *data;
722 for (i = 0; i < G_N_ELEMENTS (priority); i++)
724 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string (priority[i]));
728 GdkPixbuf *pixbuf = gtk_selection_data_get_pixbuf (data);
730 gtk_selection_data_free (data);
740 gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
742 GtkSelectionData *data;
744 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"));
749 uris = gtk_selection_data_get_uris (data);
750 gtk_selection_data_free (data);
759 gtk_clipboard_get_display (GtkClipboard *clipboard)
761 g_return_val_if_fail (clipboard != NULL, NULL);
763 return clipboard->display;
767 gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
769 GtkSelectionData *data;
770 gboolean result = FALSE;
772 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
775 result = gtk_selection_data_targets_include_text (data);
776 gtk_selection_data_free (data);
783 gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
784 GtkTextBuffer *buffer)
786 GtkSelectionData *data;
787 gboolean result = FALSE;
789 g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
790 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
792 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
795 result = gtk_selection_data_targets_include_rich_text (data, buffer);
796 gtk_selection_data_free (data);
803 gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
805 GtkSelectionData *data;
806 gboolean result = FALSE;
808 data = gtk_clipboard_wait_for_contents (clipboard,
809 gdk_atom_intern_static_string ("TARGETS"));
812 result = gtk_selection_data_targets_include_image (data, FALSE);
813 gtk_selection_data_free (data);
820 gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
822 GtkSelectionData *data;
823 gboolean result = FALSE;
825 data = gtk_clipboard_wait_for_contents (clipboard,
826 gdk_atom_intern_static_string ("TARGETS"));
829 result = gtk_selection_data_targets_include_uri (data);
830 gtk_selection_data_free (data);
837 gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
841 GtkSelectionData *data;
842 gboolean result = FALSE;
844 g_return_val_if_fail (clipboard != NULL, FALSE);
846 /* If the display supports change notification we cache targets */
847 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
848 clipboard->n_cached_targets != -1)
851 *n_targets = clipboard->n_cached_targets;
854 *targets = g_memdup (clipboard->cached_targets,
855 clipboard->n_cached_targets * sizeof (GdkAtom));
866 data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
870 GdkAtom *tmp_targets;
873 result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
875 if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
877 clipboard->n_cached_targets = tmp_n_targets;
878 clipboard->cached_targets = g_memdup (tmp_targets,
879 tmp_n_targets * sizeof (GdkAtom));
883 *n_targets = tmp_n_targets;
886 *targets = tmp_targets;
888 g_free (tmp_targets);
890 gtk_selection_data_free (data);
896 static GtkClipboard *
897 clipboard_peek (GdkDisplay *display,
899 gboolean only_if_exists)
901 GtkClipboard *clipboard = NULL;
905 if (selection == GDK_NONE)
906 selection = GDK_SELECTION_CLIPBOARD;
908 clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
910 tmp_list = clipboards;
913 clipboard = tmp_list->data;
914 if (clipboard->selection == selection)
917 tmp_list = tmp_list->next;
920 if (!tmp_list && !only_if_exists)
922 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
923 NSString *pasteboard_name;
924 clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
926 if (selection == GDK_SELECTION_CLIPBOARD)
927 pasteboard_name = NSGeneralPboard;
930 char *atom_string = gdk_atom_name (selection);
932 pasteboard_name = [NSString stringWithFormat:@"_GTK_%@",
933 [NSString stringWithUTF8String:atom_string]];
934 g_free (atom_string);
937 clipboard->pasteboard = [NSPasteboard pasteboardWithName:pasteboard_name];
941 clipboard->selection = selection;
942 clipboard->display = display;
943 clipboard->n_cached_targets = -1;
944 clipboard->n_storable_targets = -1;
945 clipboards = g_slist_prepend (clipboards, clipboard);
946 g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
947 g_signal_connect (display, "closed",
948 G_CALLBACK (clipboard_display_closed), clipboard);
949 gdk_display_request_selection_notification (display, selection);
956 gtk_clipboard_owner_change (GtkClipboard *clipboard,
957 GdkEventOwnerChange *event)
959 if (clipboard->n_cached_targets != -1)
961 clipboard->n_cached_targets = -1;
962 g_free (clipboard->cached_targets);
967 gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
972 gboolean retval = FALSE;
974 if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
977 for (i = 0; i < n_targets; i++)
979 if (targets[i] == target)
992 _gtk_clipboard_handle_event (GdkEventOwnerChange *event)
997 gtk_clipboard_set_can_store (GtkClipboard *clipboard,
998 const GtkTargetEntry *targets,
1001 /* FIXME: Implement */
1005 gtk_clipboard_store (GtkClipboard *clipboard)
1007 /* FIXME: Implement */
1011 _gtk_clipboard_store_all (void)
1013 /* FIXME: Implement */