1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Global clipboard abstraction.
24 #include "gtkclipboard.h"
25 #include "gtkinvisible.h"
27 #include "gtksignal.h"
29 #ifdef GDK_WINDOWING_X11
33 #ifdef GDK_WINDOWING_WIN32
34 #include "win32/gdkwin32.h"
37 typedef struct _RequestContentsInfo RequestContentsInfo;
38 typedef struct _RequestTextInfo RequestTextInfo;
44 GtkClipboardGetFunc get_func;
45 GtkClipboardClearFunc clear_func;
51 gboolean have_selection;
54 struct _RequestContentsInfo
56 GtkClipboardReceivedFunc callback;
60 struct _RequestTextInfo
62 GtkClipboardTextReceivedFunc callback;
66 static void clipboard_unset (GtkClipboard *clipboard);
67 static void selection_received (GtkWidget *widget,
68 GtkSelectionData *selection_data,
71 static GSList *clipboards;
72 static GtkWidget *clipboard_widget;
81 static const gchar *request_contents_key = "gtk-request-contents";
82 static GQuark request_contents_key_id = 0;
84 static const gchar *clipboards_owned_key = "gtk-clipboards-owned";
85 static GQuark clipboards_owned_key_id = 0;
90 * @selection: a #GdkAtom which identifies the clipboard
91 * to use. A value of GDK_NONE here is the
92 * same as gdk_atom_intern ("CLIPBOARD", FALSE),
93 * and provides the default clipboard. Another
94 * common value is GDK_SELECTION_PRIMARY, which
95 * identifies the primary X selection.
97 * Returns the clipboard object for the given selection.
99 * Return value: the appropriate clipboard object. If no
100 * clipboard already exists, a new one will
101 * be created. Once a clipboard object has
102 * been created, it is persistant for all time.
105 gtk_clipboard_get (GdkAtom selection)
107 GtkClipboard *clipboard = NULL;
110 if (selection == GDK_NONE)
111 selection = gdk_atom_intern ("CLIPBOARD", FALSE);
113 tmp_list = clipboards;
116 clipboard = tmp_list->data;
117 if (clipboard->selection == selection)
120 tmp_list = tmp_list->next;
125 clipboard = g_new0 (GtkClipboard, 1);
126 clipboard->selection = selection;
127 clipboards = g_slist_prepend (clipboards, clipboard);
134 selection_get_cb (GtkWidget *widget,
135 GtkSelectionData *selection_data,
139 GtkClipboard *clipboard = gtk_clipboard_get (selection_data->selection);
141 if (clipboard && clipboard->get_func)
142 clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
146 selection_clear_event_cb (GtkWidget *widget,
147 GdkEventSelection *event)
149 GtkClipboard *clipboard = gtk_clipboard_get (event->selection);
153 clipboard_unset (clipboard);
161 make_clipboard_widget (gboolean provider)
163 GtkWidget *widget = gtk_invisible_new ();
165 gtk_signal_connect (GTK_OBJECT (widget), "selection_received",
166 GTK_SIGNAL_FUNC (selection_received), NULL);
170 /* We need this for gdk_x11_get_server_time() */
171 gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
173 gtk_signal_connect (GTK_OBJECT (widget), "selection_get",
174 GTK_SIGNAL_FUNC (selection_get_cb), NULL);
175 gtk_signal_connect (GTK_OBJECT (widget), "selection_clear_event",
176 GTK_SIGNAL_FUNC (selection_clear_event_cb), NULL);
184 ensure_clipboard_widget ()
186 if (!clipboard_widget)
187 clipboard_widget = make_clipboard_widget (TRUE);
190 /* This function makes a very good guess at what the correct
191 * timestamp for a selection request should be. If there is
192 * a currently processed event, it uses the timestamp for that
193 * event, otherwise it uses the current server time. However,
194 * if the time resulting from that is older than the time used
195 * last time, it uses the time used last time instead.
197 * In order implement this correctly, we never use CurrentTime,
198 * but actually retrieve the actual timestamp from the server.
199 * This is a little slower but allows us to make the guarantee
200 * that the times used by this application will always ascend
201 * and we won't get selections being rejected just because
202 * we are using a correct timestamp from an event, but used
203 * CurrentTime previously.
206 clipboard_get_timestamp (GtkClipboard *clipboard)
208 guint32 timestamp = gtk_get_current_event_time ();
210 ensure_clipboard_widget ();
212 if (timestamp == GDK_CURRENT_TIME)
214 #ifdef GDK_WINDOWING_X11
215 timestamp = gdk_x11_get_server_time (clipboard_widget->window);
216 #elif defined GDK_WINDOWING_WIN32
217 timestamp = GetMessageTime ();
222 if (clipboard->timestamp != GDK_CURRENT_TIME)
224 /* Check to see if clipboard->timestamp is newer than
225 * timestamp, accounting for wraparound.
228 guint32 max = timestamp + 0x80000000;
230 if ((max > timestamp &&
231 (clipboard->timestamp > timestamp &&
232 clipboard->timestamp <= max)) ||
234 (clipboard->timestamp > timestamp ||
235 clipboard->timestamp <= max)))
237 timestamp = clipboard->timestamp;
242 clipboard->timestamp = timestamp;
248 clipboard_owner_destroyed (gpointer data)
250 GSList *clipboards = data;
253 tmp_list = clipboards;
256 GtkClipboard *clipboard = tmp_list->data;
258 clipboard->get_func = NULL;
259 clipboard->clear_func = NULL;
260 clipboard->user_data = NULL;
261 clipboard->have_owner = FALSE;
263 gtk_clipboard_clear (clipboard);
265 tmp_list = tmp_list->next;
268 g_slist_free (clipboards);
272 clipboard_add_owner_notify (GtkClipboard *clipboard)
274 if (!clipboards_owned_key_id)
275 clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
277 if (clipboard->have_owner)
278 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
279 g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
280 clipboards_owned_key_id),
282 clipboard_owner_destroyed);
286 clipboard_remove_owner_notify (GtkClipboard *clipboard)
288 if (clipboard->have_owner)
289 g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
290 g_slist_remove (g_object_steal_qdata (clipboard->user_data,
291 clipboards_owned_key_id),
293 clipboard_owner_destroyed);
297 gtk_clipboard_set_contents (GtkClipboard *clipboard,
298 const GtkTargetEntry *targets,
300 GtkClipboardGetFunc get_func,
301 GtkClipboardClearFunc clear_func,
305 ensure_clipboard_widget ();
307 if (gtk_selection_owner_set (clipboard_widget, clipboard->selection,
308 clipboard_get_timestamp (clipboard)))
310 clipboard->have_selection = TRUE;
312 if (!(clipboard->have_owner && have_owner) ||
313 clipboard->user_data != user_data)
315 clipboard_unset (clipboard);
317 if (clipboard->get_func)
319 /* Calling unset() caused the clipboard contents to be reset!
320 * Avoid leaking and return
322 if (!(clipboard->have_owner && have_owner) ||
323 clipboard->user_data != user_data)
325 (*clear_func) (clipboard, user_data);
333 clipboard->user_data = user_data;
334 clipboard->have_owner = have_owner;
336 clipboard_add_owner_notify (clipboard);
341 clipboard->get_func = get_func;
342 clipboard->clear_func = clear_func;
344 gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
345 gtk_selection_add_targets (clipboard_widget, clipboard->selection,
355 * gtk_clipboard_set_with_data:
356 * @clipboard: a #GtkClipboard
357 * @targets: array containing information about the available forms for the
359 * @n_targets: number of elements in @targets
360 * @get_func: function to call to get the actual clipboard data
361 * @clear_func: when the clipboard contents are set again, this function will
362 * be called, and get_func will not be subsequently called.
363 * @user_data: user data to pass to @get_func and @clear_func.
365 * Virtually set the contents of the specified clipboard by providing
366 * a list of supported formats for the clipboard data and a function
367 * to call to get the actual data when it is requested.
369 * Return value: %TRUE if setting the clipboard data succeeded. If setting
370 * the clipboard data failed the provided callback functions
374 gtk_clipboard_set_with_data (GtkClipboard *clipboard,
375 const GtkTargetEntry *targets,
377 GtkClipboardGetFunc get_func,
378 GtkClipboardClearFunc clear_func,
381 g_return_val_if_fail (clipboard != NULL, FALSE);
382 g_return_val_if_fail (targets != NULL, FALSE);
383 g_return_val_if_fail (get_func != NULL, FALSE);
385 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
386 get_func, clear_func, user_data,
391 * gtk_clipboard_set_with_owner:
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 * @owner: an object that "owns" the data. This object will be passed
400 * to the callbacks when called.
402 * Virtually set the contents of the specified clipboard by providing
403 * a list of supported formats for the clipboard data and a function
404 * to call to get the actual data when it is requested.
406 * The difference between this function and gtk_clipboard_set_with_data
407 * is that instead of an generic @user_data pointer, a #GObject is passed
408 * in. Because of this,
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_owner (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);
425 g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
427 return gtk_clipboard_set_contents (clipboard, targets, n_targets,
428 get_func, clear_func, owner,
433 * gtk_clipboard_get_owner:
434 * @clipboard: a #GtkClipboard
436 * If the clipboard contents callbacks were set with gtk_clipboard_set_owner(),
437 * and the gtk_clipboard_set_with_data() or gtk_clipboard_clear() has not
438 * subsequently called, returns the @owner set by gtk_clipboard_set_owner().
440 * Return value: the owner of the clipboard, if any; otherwise %NULL.
443 gtk_clipboard_get_owner (GtkClipboard *clipboard)
445 g_return_val_if_fail (clipboard != NULL, NULL);
447 if (clipboard->have_owner)
448 return clipboard->user_data;
454 clipboard_unset (GtkClipboard *clipboard)
456 GtkClipboardClearFunc old_clear_func;
459 old_clear_func = clipboard->clear_func;
460 old_data = clipboard->user_data;
462 if (clipboard->have_owner)
464 clipboard_remove_owner_notify (clipboard);
465 clipboard->have_owner = FALSE;
468 clipboard->get_func = NULL;
469 clipboard->clear_func = NULL;
470 clipboard->user_data = NULL;
473 old_clear_func (clipboard, old_data);
477 * gtk_clipboard_clear:
478 * @clipboard: a #GtkClipboard
480 * Clear the contents of the clipboard. Generally this should only
481 * be called between the time you call gtk_clipboard_set_contents(),
482 * and when the @clear_func you supplied is called. Otherwise, the
483 * clipboard may be owned by someone else.
486 gtk_clipboard_clear (GtkClipboard *clipboard)
488 g_return_if_fail (clipboard != NULL);
490 if (clipboard->have_selection)
491 gtk_selection_owner_set (NULL, clipboard->selection,
492 clipboard_get_timestamp (clipboard));
496 text_get_func (GtkClipboard *clipboard,
497 GtkSelectionData *selection_data,
501 gtk_selection_data_set_text (selection_data, data);
505 text_clear_func (GtkClipboard *clipboard,
512 * gtk_clipboard_set_text:
513 * @clipboard: a #GtkClipboard object
514 * @text: a UTF-8 string.
515 * @len: length of @text, in bytes, or -1, in which case
516 * the length will be determined with strlen().
518 * Set the contents of the clipboard to the given UTF-8 string. GTK+ will
519 * make a copy of the text and take responsibility for responding
520 * for requests for the text, and for converting the text into
521 * the requested format.
524 gtk_clipboard_set_text (GtkClipboard *clipboard,
528 static const GtkTargetEntry targets[] = {
529 { "STRING", 0, TARGET_STRING },
530 { "TEXT", 0, TARGET_TEXT },
531 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
532 { "UTF8_STRING", 0, TARGET_UTF8_STRING }
535 g_return_if_fail (clipboard != NULL);
536 g_return_if_fail (text != NULL);
541 gtk_clipboard_set_with_data (clipboard,
542 targets, G_N_ELEMENTS (targets),
543 text_get_func, text_clear_func,
544 g_strndup (text, len));
548 set_request_contents_info (GtkWidget *widget,
549 RequestContentsInfo *info)
551 if (!request_contents_key_id)
552 request_contents_key_id = g_quark_from_static_string (request_contents_key);
554 gtk_object_set_data_by_id (GTK_OBJECT (widget),
555 request_contents_key_id,
559 static RequestContentsInfo *
560 get_request_contents_info (GtkWidget *widget)
562 if (!request_contents_key_id)
565 return gtk_object_get_data_by_id (GTK_OBJECT (widget),
566 request_contents_key_id);
570 selection_received (GtkWidget *widget,
571 GtkSelectionData *selection_data,
574 RequestContentsInfo *request_info = get_request_contents_info (widget);
575 set_request_contents_info (widget, NULL);
577 request_info->callback (gtk_clipboard_get (selection_data->selection),
579 request_info->user_data);
581 g_free (request_info);
583 if (widget != clipboard_widget)
584 gtk_widget_destroy (widget);
588 * gtk_clipboard_request_contents:
589 * @clipboard: a #GtkClipboard
590 * @target: an atom representing the form into which the clipboard
591 * owner should convert the selection.
592 * @callback: A function to call when the results are received
593 * (or the retrieval fails.) If the retrieval fails
594 * the length field of @selection_data will be
596 * @user_data: user data to pass to @callback
598 * Requests the contents of clipboard as the given target.
599 * When the results of the result are later received the supplied callback
603 gtk_clipboard_request_contents (GtkClipboard *clipboard,
605 GtkClipboardReceivedFunc callback,
608 RequestContentsInfo *info;
611 g_return_if_fail (clipboard != NULL);
612 g_return_if_fail (target != GDK_NONE);
613 g_return_if_fail (callback != NULL);
615 ensure_clipboard_widget ();
617 if (get_request_contents_info (clipboard_widget))
618 widget = make_clipboard_widget (FALSE);
620 widget = clipboard_widget;
622 info = g_new (RequestContentsInfo, 1);
623 info->callback = callback;
624 info->user_data = user_data;
626 set_request_contents_info (widget, info);
628 gtk_selection_convert (widget, clipboard->selection, target,
629 clipboard_get_timestamp (clipboard));
633 request_text_received_func (GtkClipboard *clipboard,
634 GtkSelectionData *selection_data,
637 RequestTextInfo *info = data;
638 gchar *result = NULL;
640 result = gtk_selection_data_get_text (selection_data);
644 /* If we asked for UTF8 and didn't get it, try text; if we asked
645 * for text and didn't get it, try string. If we asked for
646 * anything else and didn't get it, give up.
648 if (selection_data->target == gdk_atom_intern ("UTF8_STRING", FALSE))
650 gtk_clipboard_request_contents (clipboard,
651 gdk_atom_intern ("TEXT", FALSE),
652 request_text_received_func, info);
655 else if (selection_data->target == gdk_atom_intern ("TEXT", FALSE))
657 gtk_clipboard_request_contents (clipboard,
659 request_text_received_func, info);
664 info->callback (clipboard, result, info->user_data);
670 * gtk_clipboard_request_text:
671 * @clipboard: a #GtkClipboard
672 * @callback: a function to call when the text is received,
673 * or the retrieval fails. (It will always be called
674 * one way or the other.)
675 * @user_data: user data to pass to @callback.
677 * Requests the contents of the clipboard as text. When the text is
678 * later received, it will be converted to UTF-8 if necessary, and
679 * @callback will be called.
681 * The @text parameter to @callback will contain the resulting text if
682 * the request succeeded, or %NULL if it failed. This could happen for
683 * various reasons, in particular if the clipboard was empty or if the
684 * contents of the clipboard could not be converted into text form.
687 gtk_clipboard_request_text (GtkClipboard *clipboard,
688 GtkClipboardTextReceivedFunc callback,
691 RequestTextInfo *info;
693 g_return_if_fail (clipboard != NULL);
694 g_return_if_fail (callback != NULL);
696 info = g_new (RequestTextInfo, 1);
697 info->callback = callback;
698 info->user_data = user_data;
700 gtk_clipboard_request_contents (clipboard, gdk_atom_intern ("UTF8_STRING", FALSE),
701 request_text_received_func,
713 clipboard_received_func (GtkClipboard *clipboard,
714 GtkSelectionData *selection_data,
717 WaitResults *results = data;
719 if (selection_data->length >= 0)
720 results->data = gtk_selection_data_copy (selection_data);
722 g_main_quit (results->loop);
726 * gtk_clipboard_wait_for_contents:
727 * @clipboard: a #GtkClipboard
728 * @target: an atom representing the form into which the clipboard
729 * owner should convert the selection.
731 * Requests the contents of the clipboard using the given target.
732 * This function waits for the data to be received using the main
733 * loop, so events, timeouts, etc, may be dispatched during the wait.
735 * Return value: a newly allocated #GtkSelectionData object or %NULL
736 * if retrieving the given target failed. If non-%NULL,
737 * this value freed with gtk_selection_data_free() when
738 * you are finished with it.
741 gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
746 g_return_val_if_fail (clipboard != NULL, NULL);
747 g_return_val_if_fail (target != GDK_NONE, NULL);
750 results.loop = g_main_new (TRUE);
752 gtk_clipboard_request_contents (clipboard, target,
753 clipboard_received_func,
756 if (g_main_is_running (results.loop))
758 GDK_THREADS_LEAVE ();
759 g_main_run (results.loop);
760 GDK_THREADS_ENTER ();
763 g_main_destroy (results.loop);
769 clipboard_text_received_func (GtkClipboard *clipboard,
773 WaitResults *results = data;
775 results->data = g_strdup (text);
776 g_main_quit (results->loop);
781 * gtk_clipboard_wait_for_text:
782 * @clipboard: a #GtkClipboard
784 * Requests the contents of the clipboard as text and converts
785 * the result to UTF-8 if necessary. This function waits for
786 * the data to be received using the main loop, so events,
787 * timeouts, etc, may be dispatched during the wait.
789 * Return value: a newly allocated UTF-8 string which must
790 * be freed with g_free(), or %NULL if retrieving
791 * the selection data failed. (This could happen
792 * for various reasons, in particular if the
793 * clipboard was empty or if the contents of the
794 * clipboard could not be converted into text form.)
797 gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
801 g_return_val_if_fail (clipboard != NULL, NULL);
802 g_return_val_if_fail (clipboard != NULL, NULL);
805 results.loop = g_main_new (TRUE);
807 gtk_clipboard_request_text (clipboard,
808 clipboard_text_received_func,
811 if (g_main_is_running (results.loop))
813 GDK_THREADS_LEAVE ();
814 g_main_run (results.loop);
815 GDK_THREADS_ENTER ();
818 g_main_destroy (results.loop);