+
+/**
+ * gtk_clipboard_wait_is_rich_text_available:
+ * @clipboard: a #GtkClipboard
+ * @buffer: a #GtkTextBuffer
+ *
+ * Test to see if there is rich text available to be pasted
+ * This is done by requesting the TARGETS atom and checking
+ * if it contains any of the supported rich text targets. This function
+ * waits for the data to be received using the main loop, so events,
+ * timeouts, etc, may be dispatched during the wait.
+ *
+ * This function is a little faster than calling
+ * gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
+ * the actual text.
+ *
+ * Return value: %TRUE is there is rich text available, %FALSE otherwise.
+ *
+ * Since: 2.10
+ **/
+gboolean
+gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
+ GtkTextBuffer *buffer)
+{
+ GtkSelectionData *data;
+ gboolean result = FALSE;
+
+ g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
+ g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
+
+ data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
+ if (data)
+ {
+ result = gtk_selection_data_targets_include_rich_text (data, buffer);
+ gtk_selection_data_free (data);
+ }
+
+ return result;
+}
+
+/**
+ * gtk_clipboard_wait_is_image_available:
+ * @clipboard: a #GtkClipboard
+ *
+ * Test to see if there is an image available to be pasted
+ * This is done by requesting the TARGETS atom and checking
+ * if it contains any of the supported image targets. This function
+ * waits for the data to be received using the main loop, so events,
+ * timeouts, etc, may be dispatched during the wait.
+ *
+ * This function is a little faster than calling
+ * gtk_clipboard_wait_for_image() since it doesn't need to retrieve
+ * the actual image data.
+ *
+ * Return value: %TRUE is there is an image available, %FALSE otherwise.
+ *
+ * Since: 2.6
+ **/
+gboolean
+gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
+{
+ GtkSelectionData *data;
+ gboolean result = FALSE;
+
+ data = gtk_clipboard_wait_for_contents (clipboard,
+ gdk_atom_intern_static_string ("TARGETS"));
+ if (data)
+ {
+ result = gtk_selection_data_targets_include_image (data, FALSE);
+ gtk_selection_data_free (data);
+ }
+
+ return result;
+}
+
+/**
+ * gtk_clipboard_wait_is_uris_available:
+ * @clipboard: a #GtkClipboard
+ *
+ * Test to see if there is a list of URIs available to be pasted
+ * This is done by requesting the TARGETS atom and checking
+ * if it contains the URI targets. This function
+ * waits for the data to be received using the main loop, so events,
+ * timeouts, etc, may be dispatched during the wait.
+ *
+ * This function is a little faster than calling
+ * gtk_clipboard_wait_for_uris() since it doesn't need to retrieve
+ * the actual URI data.
+ *
+ * Return value: %TRUE is there is an URI list available, %FALSE otherwise.
+ *
+ * Since: 2.14
+ **/
+gboolean
+gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
+{
+ GtkSelectionData *data;
+ gboolean result = FALSE;
+
+ data = gtk_clipboard_wait_for_contents (clipboard,
+ gdk_atom_intern_static_string ("TARGETS"));
+ if (data)
+ {
+ result = gtk_selection_data_targets_include_uri (data);
+ gtk_selection_data_free (data);
+ }
+
+ return result;
+}
+
+/**
+ * gtk_clipboard_wait_for_targets:
+ * @clipboard: a #GtkClipboard
+ * @targets: (out) (array length=n_targets) (transfer container): location
+ * to store an array of targets. The result stored here must
+ * be freed with g_free().
+ * @n_targets: location to store number of items in @targets.
+ *
+ * Returns a list of targets that are present on the clipboard, or %NULL
+ * if there aren't any targets available. The returned list must be
+ * freed with g_free().
+ * This function waits for the data to be received using the main
+ * loop, so events, timeouts, etc, may be dispatched during the wait.
+ *
+ * Return value: %TRUE if any targets are present on the clipboard,
+ * otherwise %FALSE.
+ *
+ * Since: 2.4
+ */
+gboolean
+gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
+ GdkAtom **targets,
+ gint *n_targets)
+{
+ GtkSelectionData *data;
+ gboolean result = FALSE;
+
+ g_return_val_if_fail (clipboard != NULL, FALSE);
+
+ /* If the display supports change notification we cache targets */
+ if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
+ clipboard->n_cached_targets != -1)
+ {
+ if (n_targets)
+ *n_targets = clipboard->n_cached_targets;
+
+ if (targets)
+ *targets = g_memdup (clipboard->cached_targets,
+ clipboard->n_cached_targets * sizeof (GdkAtom));
+
+ return TRUE;
+ }
+
+ if (n_targets)
+ *n_targets = 0;
+
+ if (targets)
+ *targets = NULL;
+
+ data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
+
+ if (data)
+ {
+ GdkAtom *tmp_targets;
+ gint tmp_n_targets;
+
+ result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
+
+ if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
+ {
+ clipboard->n_cached_targets = tmp_n_targets;
+ clipboard->cached_targets = g_memdup (tmp_targets,
+ tmp_n_targets * sizeof (GdkAtom));
+ }
+
+ if (n_targets)
+ *n_targets = tmp_n_targets;
+
+ if (targets)
+ *targets = tmp_targets;
+ else
+ g_free (tmp_targets);
+
+ gtk_selection_data_free (data);
+ }
+
+ return result;
+}
+
+static GtkClipboard *
+clipboard_peek (GdkDisplay *display,
+ GdkAtom selection,
+ gboolean only_if_exists)
+{
+ GtkClipboard *clipboard = NULL;
+ GSList *clipboards;
+ GSList *tmp_list;
+
+ if (selection == GDK_NONE)
+ selection = GDK_SELECTION_CLIPBOARD;
+
+ clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
+
+ tmp_list = clipboards;
+ while (tmp_list)
+ {
+ clipboard = tmp_list->data;
+ if (clipboard->selection == selection)
+ break;
+
+ tmp_list = tmp_list->next;
+ }
+
+ if (!tmp_list && !only_if_exists)
+ {
+ clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
+ clipboard->selection = selection;
+ clipboard->display = display;
+ clipboard->n_cached_targets = -1;
+ clipboard->n_storable_targets = -1;
+ clipboards = g_slist_prepend (clipboards, clipboard);
+ g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
+ g_signal_connect (display, "closed",
+ G_CALLBACK (clipboard_display_closed), clipboard);
+ gdk_display_request_selection_notification (display, selection);
+ }
+
+ return clipboard;
+}
+
+static void
+gtk_clipboard_owner_change (GtkClipboard *clipboard,
+ GdkEventOwnerChange *event)
+{
+ if (clipboard->n_cached_targets != -1)
+ {
+ g_free (clipboard->cached_targets);
+ clipboard->cached_targets = NULL;
+ clipboard->n_cached_targets = -1;
+ }
+}
+
+/**
+ * gtk_clipboard_wait_is_target_available:
+ * @clipboard: a #GtkClipboard
+ * @target: A #GdkAtom indicating which target to look for.
+ *
+ * Checks if a clipboard supports pasting data of a given type. This
+ * function can be used to determine if a "Paste" menu item should be
+ * insensitive or not.
+ *
+ * If you want to see if there's text available on the clipboard, use
+ * gtk_clipboard_wait_is_text_available () instead.
+ *
+ * Return value: %TRUE if the target is available, %FALSE otherwise.
+ *
+ * Since: 2.6
+ */
+gboolean
+gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
+ GdkAtom target)
+{
+ GdkAtom *targets;
+ gint i, n_targets;
+ gboolean retval = FALSE;
+
+ if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
+ return FALSE;
+
+ for (i = 0; i < n_targets; i++)
+ {
+ if (targets[i] == target)
+ {
+ retval = TRUE;
+ break;
+ }
+ }
+
+ g_free (targets);
+
+ return retval;
+}
+
+/**
+ * _gtk_clipboard_handle_event:
+ * @event: a owner change event
+ *
+ * Emits the #GtkClipboard::owner-change signal on the appropriate @clipboard.
+ *
+ * Since: 2.6
+ **/
+void
+_gtk_clipboard_handle_event (GdkEventOwnerChange *event)
+{
+ GdkDisplay *display;
+ GtkClipboard *clipboard;
+
+ display = gdk_window_get_display (event->window);
+ clipboard = clipboard_peek (display, event->selection, TRUE);
+
+ if (clipboard)
+ g_signal_emit (clipboard,
+ clipboard_signals[OWNER_CHANGE], 0, event, NULL);
+}
+
+static gboolean
+gtk_clipboard_store_timeout (GtkClipboard *clipboard)
+{
+ g_main_loop_quit (clipboard->store_loop);
+
+ return G_SOURCE_REMOVE;
+}
+
+/**
+ * gtk_clipboard_set_can_store:
+ * @clipboard: a #GtkClipboard
+ * @targets: (allow-none) (array length=n_targets): array containing
+ * information about which forms should be stored or %NULL
+ * to indicate that all forms should be stored.
+ * @n_targets: number of elements in @targets
+ *
+ * Hints that the clipboard data should be stored somewhere when the
+ * application exits or when gtk_clipboard_store () is called.
+ *
+ * This value is reset when the clipboard owner changes.
+ * Where the clipboard data is stored is platform dependent,
+ * see gdk_display_store_clipboard () for more information.
+ *
+ * Since: 2.6
+ */
+void
+gtk_clipboard_set_can_store (GtkClipboard *clipboard,
+ const GtkTargetEntry *targets,
+ gint n_targets)
+{
+ GtkWidget *clipboard_widget;
+ int i;
+ static const GtkTargetEntry save_targets[] = {
+ { "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
+ };
+
+ g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
+ g_return_if_fail (n_targets >= 0);
+
+ if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
+ return;
+
+ g_free (clipboard->storable_targets);
+
+ clipboard_widget = get_clipboard_widget (clipboard->display);
+
+ /* n_storable_targets being -1 means that
+ * gtk_clipboard_set_can_store hasn't been called since the
+ * clipboard owner changed. We only want to add SAVE_TARGETS and
+ * ref the owner once , so we do that here
+ */
+ if (clipboard->n_storable_targets == -1)
+ {
+ gtk_selection_add_targets (clipboard_widget, clipboard->selection,
+ save_targets, 1);
+
+ /* Ref the owner so it won't go away */
+ if (clipboard->have_owner)
+ g_object_ref (clipboard->user_data);
+ }
+
+ clipboard->n_storable_targets = n_targets;
+ clipboard->storable_targets = g_new (GdkAtom, n_targets);
+ for (i = 0; i < n_targets; i++)
+ clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
+}
+
+static gboolean
+gtk_clipboard_selection_notify (GtkWidget *widget,
+ GdkEventSelection *event,
+ GtkClipboard *clipboard)
+{
+ if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
+ clipboard->storing_selection)
+ g_main_loop_quit (clipboard->store_loop);
+
+ return FALSE;
+}
+
+/**
+ * gtk_clipboard_store:
+ * @clipboard: a #GtkClipboard
+ *
+ * Stores the current clipboard data somewhere so that it will stay
+ * around after the application has quit.
+ *
+ * Since: 2.6
+ */
+void
+gtk_clipboard_store (GtkClipboard *clipboard)
+{
+ GtkWidget *clipboard_widget;
+
+ g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
+
+ if (clipboard->n_storable_targets < 0)
+ return;
+
+ if (!gdk_display_supports_clipboard_persistence (clipboard->display))
+ return;
+
+ g_object_ref (clipboard);
+
+ clipboard_widget = get_clipboard_widget (clipboard->display);
+ clipboard->notify_signal_id = g_signal_connect (clipboard_widget,
+ "selection-notify-event",
+ G_CALLBACK (gtk_clipboard_selection_notify),
+ clipboard);
+
+ gdk_display_store_clipboard (clipboard->display,
+ gtk_widget_get_window (clipboard_widget),
+ clipboard_get_timestamp (clipboard),
+ clipboard->storable_targets,
+ clipboard->n_storable_targets);
+
+ clipboard->storing_selection = TRUE;
+
+ clipboard->store_loop = g_main_loop_new (NULL, TRUE);
+ clipboard->store_timeout = g_timeout_add_seconds (10, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
+
+ if (g_main_loop_is_running (clipboard->store_loop))
+ {
+ gdk_threads_leave ();
+ g_main_loop_run (clipboard->store_loop);
+ gdk_threads_enter ();
+ }
+
+ g_main_loop_unref (clipboard->store_loop);
+ clipboard->store_loop = NULL;
+
+ g_source_remove (clipboard->store_timeout);
+ clipboard->store_timeout = 0;
+ g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
+ clipboard->notify_signal_id = 0;
+
+ clipboard->storing_selection = FALSE;
+
+ g_object_unref (clipboard);
+}
+
+/* Stores all clipboard selections on all displays, called from
+ * gtk_main_quit ().
+ */
+void
+_gtk_clipboard_store_all (void)
+{
+ GtkClipboard *clipboard;
+ GSList *displays, *list;
+
+ displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
+
+ list = displays;
+ while (list)
+ {
+ GdkDisplay *display = list->data;
+
+ clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
+
+ if (clipboard)
+ gtk_clipboard_store (clipboard);
+
+ list = list->next;
+ }
+ g_slist_free (displays);
+
+}