1
/* GTK - The GIMP Toolkit
2
* Copyright (C) 2000 Red Hat, Inc.
3
* Copyright (C) 2004 Nokia Corporation
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the
17
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
* Boston, MA 02111-1307, USA.
20
* Global clipboard abstraction.
26
#include "gtkclipboard.h"
27
#include "gtkinvisible.h"
29
#include "gtkmarshalers.h"
30
#include "gtktextbufferrichtext.h"
34
#ifdef GDK_WINDOWING_X11
38
#ifdef GDK_WINDOWING_WIN32
39
#include "win32/gdkwin32.h"
47
typedef struct _GtkClipboardClass GtkClipboardClass;
49
typedef struct _RequestContentsInfo RequestContentsInfo;
50
typedef struct _RequestTextInfo RequestTextInfo;
51
typedef struct _RequestRichTextInfo RequestRichTextInfo;
52
typedef struct _RequestImageInfo RequestImageInfo;
53
typedef struct _RequestTargetsInfo RequestTargetsInfo;
57
GObject parent_instance;
61
GtkClipboardGetFunc get_func;
62
GtkClipboardClearFunc clear_func;
68
gboolean have_selection;
71
GdkAtom *cached_targets;
72
gint n_cached_targets;
74
guint notify_signal_id;
75
gboolean storing_selection;
76
GMainLoop *store_loop;
78
gint n_storable_targets;
79
GdkAtom *storable_targets;
82
struct _GtkClipboardClass
84
GObjectClass parent_class;
86
void (*owner_change) (GtkClipboard *clipboard,
87
GdkEventOwnerChange *event);
90
struct _RequestContentsInfo
92
GtkClipboardReceivedFunc callback;
96
struct _RequestTextInfo
98
GtkClipboardTextReceivedFunc callback;
102
struct _RequestRichTextInfo
104
GtkClipboardRichTextReceivedFunc callback;
111
struct _RequestImageInfo
113
GtkClipboardImageReceivedFunc callback;
117
struct _RequestTargetsInfo
119
GtkClipboardTargetsReceivedFunc callback;
123
static void gtk_clipboard_class_init (GtkClipboardClass *class);
124
static void gtk_clipboard_finalize (GObject *object);
125
static void gtk_clipboard_owner_change (GtkClipboard *clipboard,
126
GdkEventOwnerChange *event);
128
static void clipboard_unset (GtkClipboard *clipboard);
129
static void selection_received (GtkWidget *widget,
130
GtkSelectionData *selection_data,
132
static GtkClipboard *clipboard_peek (GdkDisplay *display,
134
gboolean only_if_exists);
135
static GtkWidget * get_clipboard_widget (GdkDisplay *display);
141
TARGET_COMPOUND_TEXT,
146
static const gchar request_contents_key[] = "gtk-request-contents";
147
static GQuark request_contents_key_id = 0;
149
static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
150
static GQuark clipboards_owned_key_id = 0;
152
static guint clipboard_signals[LAST_SIGNAL] = { 0 };
154
G_DEFINE_TYPE (GtkClipboard, gtk_clipboard, G_TYPE_OBJECT)
157
gtk_clipboard_init (GtkClipboard *object)
162
gtk_clipboard_class_init (GtkClipboardClass *class)
164
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
166
gobject_class->finalize = gtk_clipboard_finalize;
168
class->owner_change = gtk_clipboard_owner_change;
170
clipboard_signals[OWNER_CHANGE] =
171
g_signal_new (I_("owner_change"),
172
G_TYPE_FROM_CLASS (gobject_class),
174
G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
176
_gtk_marshal_VOID__BOXED,
178
GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
182
gtk_clipboard_finalize (GObject *object)
184
GtkClipboard *clipboard;
185
GtkWidget *clipboard_widget;
188
clipboard = GTK_CLIPBOARD (object);
190
clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
191
if (g_slist_index (clipboards, clipboard) >= 0)
192
g_warning ("GtkClipboard prematurely finalized");
194
/* don't use get_clipboard_widget() here because it would create the
195
* widget if it doesn't exist.
197
clipboard_widget = g_object_get_data (G_OBJECT (clipboard->display),
198
"gtk-clipboard-widget");
200
clipboard_unset (clipboard);
202
clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
203
clipboards = g_slist_remove (clipboards, clipboard);
204
g_object_set_data (G_OBJECT (clipboard->display), I_("gtk-clipboard-list"), clipboards);
206
if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
207
g_main_loop_quit (clipboard->store_loop);
209
if (clipboard->store_timeout != 0)
210
g_source_remove (clipboard->store_timeout);
212
if (clipboard->notify_signal_id != 0)
213
g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
215
g_free (clipboard->storable_targets);
217
G_OBJECT_CLASS (gtk_clipboard_parent_class)->finalize (object);
221
clipboard_display_closed (GdkDisplay *display,
223
GtkClipboard *clipboard)
227
clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
228
g_object_run_dispose (G_OBJECT (clipboard));
229
clipboards = g_slist_remove (clipboards, clipboard);
230
g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
231
g_object_unref (clipboard);
235
* gtk_clipboard_get_for_display:
236
* @display: the display for which the clipboard is to be retrieved or created
237
* @selection: a #GdkAtom which identifies the clipboard
240
* Returns the clipboard object for the given selection.
241
* Cut/copy/paste menu items and keyboard shortcuts should use
242
* the default clipboard, returned by passing %GDK_SELECTION_CLIPBOARD for @selection.
243
* (%GDK_NONE is supported as a synonym for GDK_SELECTION_CLIPBOARD
244
* for backwards compatibility reasons.)
245
* The currently-selected object or text should be provided on the clipboard
246
* identified by #GDK_SELECTION_PRIMARY. Cut/copy/paste menu items
247
* conceptually copy the contents of the #GDK_SELECTION_PRIMARY clipboard
248
* to the default clipboard, i.e. they copy the selection to what the
249
* user sees as the clipboard.
251
* (Passing #GDK_NONE is the same as using <literal>gdk_atom_intern
252
* ("CLIPBOARD", FALSE)</literal>. See <ulink
253
* url="http://www.freedesktop.org/Standards/clipboards-spec">
254
* http://www.freedesktop.org/Standards/clipboards-spec</ulink>
255
* for a detailed discussion of the "CLIPBOARD" vs. "PRIMARY"
256
* selections under the X window system. On Win32 the
257
* #GDK_SELECTION_PRIMARY clipboard is essentially ignored.)
259
* It's possible to have arbitrary named clipboards; if you do invent
260
* new clipboards, you should prefix the selection name with an
261
* underscore (because the ICCCM requires that nonstandard atoms are
262
* underscore-prefixed), and namespace it as well. For example,
263
* if your application called "Foo" has a special-purpose
264
* clipboard, you might call it "_FOO_SPECIAL_CLIPBOARD".
266
* Return value: the appropriate clipboard object. If no
267
* clipboard already exists, a new one will
268
* be created. Once a clipboard object has
269
* been created, it is persistent and, since
270
* it is owned by GTK+, must not be freed or
276
gtk_clipboard_get_for_display (GdkDisplay *display,
279
g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
280
g_return_val_if_fail (!display->closed, NULL);
282
return clipboard_peek (display, selection, FALSE);
287
* gtk_clipboard_get():
288
* @selection: a #GdkAtom which identifies the clipboard
291
* Returns the clipboard object for the given selection.
292
* See gtk_clipboard_get_for_display() for complete details.
294
* Return value: the appropriate clipboard object. If no
295
* clipboard already exists, a new one will
296
* be created. Once a clipboard object has
297
* been created, it is persistent and, since
298
* it is owned by GTK+, must not be freed or
302
gtk_clipboard_get (GdkAtom selection)
304
return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
308
selection_get_cb (GtkWidget *widget,
309
GtkSelectionData *selection_data,
313
GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, selection_data->selection);
315
if (clipboard && clipboard->get_func)
316
clipboard->get_func (clipboard, selection_data, info, clipboard->user_data);
320
selection_clear_event_cb (GtkWidget *widget,
321
GdkEventSelection *event)
323
GtkClipboard *clipboard = gtk_widget_get_clipboard (widget, event->selection);
327
clipboard_unset (clipboard);
336
make_clipboard_widget (GdkDisplay *display,
339
GtkWidget *widget = gtk_invisible_new_for_screen (gdk_display_get_default_screen (display));
341
g_signal_connect (widget, "selection_received",
342
G_CALLBACK (selection_received), NULL);
346
/* We need this for gdk_x11_get_server_time() */
347
gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
349
g_signal_connect (widget, "selection_get",
350
G_CALLBACK (selection_get_cb), NULL);
351
g_signal_connect (widget, "selection_clear_event",
352
G_CALLBACK (selection_clear_event_cb), NULL);
359
get_clipboard_widget (GdkDisplay *display)
361
GtkWidget *clip_widget = g_object_get_data (G_OBJECT (display), "gtk-clipboard-widget");
364
clip_widget = make_clipboard_widget (display, TRUE);
365
g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-widget"), clip_widget);
371
/* This function makes a very good guess at what the correct
372
* timestamp for a selection request should be. If there is
373
* a currently processed event, it uses the timestamp for that
374
* event, otherwise it uses the current server time. However,
375
* if the time resulting from that is older than the time used
376
* last time, it uses the time used last time instead.
378
* In order implement this correctly, we never use CurrentTime,
379
* but actually retrieve the actual timestamp from the server.
380
* This is a little slower but allows us to make the guarantee
381
* that the times used by this application will always ascend
382
* and we won't get selections being rejected just because
383
* we are using a correct timestamp from an event, but used
384
* CurrentTime previously.
387
clipboard_get_timestamp (GtkClipboard *clipboard)
389
GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
390
guint32 timestamp = gtk_get_current_event_time ();
392
if (timestamp == GDK_CURRENT_TIME)
394
#ifdef GDK_WINDOWING_X11
395
timestamp = gdk_x11_get_server_time (clipboard_widget->window);
396
#elif defined GDK_WINDOWING_WIN32
397
timestamp = GetMessageTime ();
402
if (clipboard->timestamp != GDK_CURRENT_TIME)
404
/* Check to see if clipboard->timestamp is newer than
405
* timestamp, accounting for wraparound.
408
guint32 max = timestamp + 0x80000000;
410
if ((max > timestamp &&
411
(clipboard->timestamp > timestamp &&
412
clipboard->timestamp <= max)) ||
414
(clipboard->timestamp > timestamp ||
415
clipboard->timestamp <= max)))
417
timestamp = clipboard->timestamp;
422
clipboard->timestamp = timestamp;
428
clipboard_owner_destroyed (gpointer data)
430
GSList *clipboards = data;
433
tmp_list = clipboards;
436
GtkClipboard *clipboard = tmp_list->data;
438
clipboard->get_func = NULL;
439
clipboard->clear_func = NULL;
440
clipboard->user_data = NULL;
441
clipboard->have_owner = FALSE;
443
gtk_clipboard_clear (clipboard);
445
tmp_list = tmp_list->next;
448
g_slist_free (clipboards);
452
clipboard_add_owner_notify (GtkClipboard *clipboard)
454
if (!clipboards_owned_key_id)
455
clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
457
if (clipboard->have_owner)
458
g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
459
g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
460
clipboards_owned_key_id),
462
clipboard_owner_destroyed);
466
clipboard_remove_owner_notify (GtkClipboard *clipboard)
468
if (clipboard->have_owner)
469
g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
470
g_slist_remove (g_object_steal_qdata (clipboard->user_data,
471
clipboards_owned_key_id),
473
clipboard_owner_destroyed);
477
gtk_clipboard_set_contents (GtkClipboard *clipboard,
478
const GtkTargetEntry *targets,
480
GtkClipboardGetFunc get_func,
481
GtkClipboardClearFunc clear_func,
485
GtkWidget *clipboard_widget = get_clipboard_widget (clipboard->display);
487
if (gtk_selection_owner_set_for_display (clipboard->display,
489
clipboard->selection,
490
clipboard_get_timestamp (clipboard)))
492
clipboard->have_selection = TRUE;
494
if (!(clipboard->have_owner && have_owner) ||
495
clipboard->user_data != user_data)
497
clipboard_unset (clipboard);
499
if (clipboard->get_func)
501
/* Calling unset() caused the clipboard contents to be reset!
502
* Avoid leaking and return
504
if (!(clipboard->have_owner && have_owner) ||
505
clipboard->user_data != user_data)
507
(*clear_func) (clipboard, user_data);
515
clipboard->user_data = user_data;
516
clipboard->have_owner = have_owner;
518
clipboard_add_owner_notify (clipboard);
523
clipboard->get_func = get_func;
524
clipboard->clear_func = clear_func;
526
gtk_selection_clear_targets (clipboard_widget, clipboard->selection);
527
gtk_selection_add_targets (clipboard_widget, clipboard->selection,
537
* gtk_clipboard_set_with_data:
538
* @clipboard: a #GtkClipboard
539
* @targets: array containing information about the available forms for the
541
* @n_targets: number of elements in @targets
542
* @get_func: function to call to get the actual clipboard data
543
* @clear_func: when the clipboard contents are set again, this function will
544
* be called, and @get_func will not be subsequently called.
545
* @user_data: user data to pass to @get_func and @clear_func.
547
* Virtually sets the contents of the specified clipboard by providing
548
* a list of supported formats for the clipboard data and a function
549
* to call to get the actual data when it is requested.
551
* Return value: %TRUE if setting the clipboard data succeeded. If setting
552
* the clipboard data failed the provided callback functions
556
gtk_clipboard_set_with_data (GtkClipboard *clipboard,
557
const GtkTargetEntry *targets,
559
GtkClipboardGetFunc get_func,
560
GtkClipboardClearFunc clear_func,
563
g_return_val_if_fail (clipboard != NULL, FALSE);
564
g_return_val_if_fail (targets != NULL, FALSE);
565
g_return_val_if_fail (get_func != NULL, FALSE);
567
return gtk_clipboard_set_contents (clipboard, targets, n_targets,
568
get_func, clear_func, user_data,
573
* gtk_clipboard_set_with_owner:
574
* @clipboard: a #GtkClipboard
575
* @targets: array containing information about the available forms for the
577
* @n_targets: number of elements in @targets
578
* @get_func: function to call to get the actual clipboard data
579
* @clear_func: when the clipboard contents are set again, this function will
580
* be called, and @get_func will not be subsequently called.
581
* @owner: an object that "owns" the data. This object will be passed
582
* to the callbacks when called.
584
* Virtually sets the contents of the specified clipboard by providing
585
* a list of supported formats for the clipboard data and a function
586
* to call to get the actual data when it is requested.
588
* The difference between this function and gtk_clipboard_set_with_data()
589
* is that instead of an generic @user_data pointer, a #GObject is passed
592
* Return value: %TRUE if setting the clipboard data succeeded. If setting
593
* the clipboard data failed the provided callback functions
597
gtk_clipboard_set_with_owner (GtkClipboard *clipboard,
598
const GtkTargetEntry *targets,
600
GtkClipboardGetFunc get_func,
601
GtkClipboardClearFunc clear_func,
604
g_return_val_if_fail (clipboard != NULL, FALSE);
605
g_return_val_if_fail (targets != NULL, FALSE);
606
g_return_val_if_fail (get_func != NULL, FALSE);
607
g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);
609
return gtk_clipboard_set_contents (clipboard, targets, n_targets,
610
get_func, clear_func, owner,
615
* gtk_clipboard_get_owner:
616
* @clipboard: a #GtkClipboard
618
* If the clipboard contents callbacks were set with
619
* gtk_clipboard_set_with_owner(), and the gtk_clipboard_set_with_data() or
620
* gtk_clipboard_clear() has not subsequently called, returns the owner set
621
* by gtk_clipboard_set_with_owner().
623
* Return value: the owner of the clipboard, if any; otherwise %NULL.
626
gtk_clipboard_get_owner (GtkClipboard *clipboard)
628
g_return_val_if_fail (clipboard != NULL, NULL);
630
if (clipboard->have_owner)
631
return clipboard->user_data;
637
clipboard_unset (GtkClipboard *clipboard)
639
GtkClipboardClearFunc old_clear_func;
641
gboolean old_have_owner;
642
gint old_n_storable_targets;
644
old_clear_func = clipboard->clear_func;
645
old_data = clipboard->user_data;
646
old_have_owner = clipboard->have_owner;
647
old_n_storable_targets = clipboard->n_storable_targets;
651
clipboard_remove_owner_notify (clipboard);
652
clipboard->have_owner = FALSE;
655
clipboard->n_storable_targets = -1;
656
g_free (clipboard->storable_targets);
657
clipboard->storable_targets = NULL;
659
clipboard->get_func = NULL;
660
clipboard->clear_func = NULL;
661
clipboard->user_data = NULL;
664
old_clear_func (clipboard, old_data);
666
/* If we've transferred the clipboard data to the manager,
669
if (old_have_owner &&
670
old_n_storable_targets != -1)
671
g_object_unref (old_data);
675
* gtk_clipboard_clear:
676
* @clipboard: a #GtkClipboard
678
* Clears the contents of the clipboard. Generally this should only
679
* be called between the time you call gtk_clipboard_set_with_owner()
680
* or gtk_clipboard_set_with_data(),
681
* and when the @clear_func you supplied is called. Otherwise, the
682
* clipboard may be owned by someone else.
685
gtk_clipboard_clear (GtkClipboard *clipboard)
687
g_return_if_fail (clipboard != NULL);
689
if (clipboard->have_selection)
690
gtk_selection_owner_set_for_display (clipboard->display,
692
clipboard->selection,
693
clipboard_get_timestamp (clipboard));
697
text_get_func (GtkClipboard *clipboard,
698
GtkSelectionData *selection_data,
702
gtk_selection_data_set_text (selection_data, data, -1);
706
text_clear_func (GtkClipboard *clipboard,
714
* gtk_clipboard_set_text:
715
* @clipboard: a #GtkClipboard object
716
* @text: a UTF-8 string.
717
* @len: length of @text, in bytes, or -1, in which case
718
* the length will be determined with <function>strlen()</function>.
720
* Sets the contents of the clipboard to the given UTF-8 string. GTK+ will
721
* make a copy of the text and take responsibility for responding
722
* for requests for the text, and for converting the text into
723
* the requested format.
726
gtk_clipboard_set_text (GtkClipboard *clipboard,
732
GtkTargetEntry *targets;
735
g_return_if_fail (clipboard != NULL);
736
g_return_if_fail (text != NULL);
738
list = gtk_target_list_new (NULL, 0);
739
gtk_target_list_add_text_targets (list, 0);
741
n_targets = g_list_length (list->list);
742
targets = g_new0 (GtkTargetEntry, n_targets);
743
for (l = list->list, i = 0; l; l = l->next, i++)
745
GtkTargetPair *pair = (GtkTargetPair *)l->data;
746
targets[i].target = gdk_atom_name (pair->target);
752
gtk_clipboard_set_with_data (clipboard,
754
text_get_func, text_clear_func,
755
g_strndup (text, len));
756
gtk_clipboard_set_can_store (clipboard, NULL, 0);
758
for (i = 0; i < n_targets; i++)
759
g_free (targets[i].target);
761
gtk_target_list_unref (list);
765
pixbuf_get_func (GtkClipboard *clipboard,
766
GtkSelectionData *selection_data,
770
gtk_selection_data_set_pixbuf (selection_data, data);
774
pixbuf_clear_func (GtkClipboard *clipboard,
777
g_object_unref (data);
781
* gtk_clipboard_set_image:
782
* @clipboard: a #GtkClipboard object
783
* @pixbuf: a #GdkPixbuf
785
* Sets the contents of the clipboard to the given #GdkPixbuf.
786
* GTK+ will take responsibility for responding for requests
787
* for the image, and for converting the image into the
793
gtk_clipboard_set_image (GtkClipboard *clipboard,
798
GtkTargetEntry *targets;
801
g_return_if_fail (clipboard != NULL);
802
g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
804
list = gtk_target_list_new (NULL, 0);
805
gtk_target_list_add_image_targets (list, 0, TRUE);
807
n_targets = g_list_length (list->list);
808
targets = g_new0 (GtkTargetEntry, n_targets);
809
for (l = list->list, i = 0; l; l = l->next, i++)
811
GtkTargetPair *pair = (GtkTargetPair *)l->data;
812
targets[i].target = gdk_atom_name (pair->target);
815
gtk_clipboard_set_with_data (clipboard,
817
pixbuf_get_func, pixbuf_clear_func,
818
g_object_ref (pixbuf));
819
gtk_clipboard_set_can_store (clipboard, NULL, 0);
821
for (i = 0; i < n_targets; i++)
822
g_free (targets[i].target);
824
gtk_target_list_unref (list);
828
set_request_contents_info (GtkWidget *widget,
829
RequestContentsInfo *info)
831
if (!request_contents_key_id)
832
request_contents_key_id = g_quark_from_static_string (request_contents_key);
834
g_object_set_qdata (G_OBJECT (widget), request_contents_key_id, info);
837
static RequestContentsInfo *
838
get_request_contents_info (GtkWidget *widget)
840
if (!request_contents_key_id)
843
return g_object_get_qdata (G_OBJECT (widget), request_contents_key_id);
847
selection_received (GtkWidget *widget,
848
GtkSelectionData *selection_data,
851
RequestContentsInfo *request_info = get_request_contents_info (widget);
852
set_request_contents_info (widget, NULL);
854
request_info->callback (gtk_widget_get_clipboard (widget, selection_data->selection),
856
request_info->user_data);
858
g_free (request_info);
860
if (widget != get_clipboard_widget (gtk_widget_get_display (widget)))
861
gtk_widget_destroy (widget);
865
* gtk_clipboard_request_contents:
866
* @clipboard: a #GtkClipboard
867
* @target: an atom representing the form into which the clipboard
868
* owner should convert the selection.
869
* @callback: A function to call when the results are received
870
* (or the retrieval fails). If the retrieval fails
871
* the length field of @selection_data will be
873
* @user_data: user data to pass to @callback
875
* Requests the contents of clipboard as the given target.
876
* When the results of the result are later received the supplied callback
880
gtk_clipboard_request_contents (GtkClipboard *clipboard,
882
GtkClipboardReceivedFunc callback,
885
RequestContentsInfo *info;
887
GtkWidget *clipboard_widget;
889
g_return_if_fail (clipboard != NULL);
890
g_return_if_fail (target != GDK_NONE);
891
g_return_if_fail (callback != NULL);
893
clipboard_widget = get_clipboard_widget (clipboard->display);
895
if (get_request_contents_info (clipboard_widget))
896
widget = make_clipboard_widget (clipboard->display, FALSE);
898
widget = clipboard_widget;
900
info = g_new (RequestContentsInfo, 1);
901
info->callback = callback;
902
info->user_data = user_data;
904
set_request_contents_info (widget, info);
906
gtk_selection_convert (widget, clipboard->selection, target,
907
clipboard_get_timestamp (clipboard));
911
request_text_received_func (GtkClipboard *clipboard,
912
GtkSelectionData *selection_data,
915
RequestTextInfo *info = data;
916
gchar *result = NULL;
918
result = (gchar *) gtk_selection_data_get_text (selection_data);
922
/* If we asked for UTF8 and didn't get it, try compound_text;
923
* if we asked for compound_text and didn't get it, try string;
924
* If we asked for anything else and didn't get it, give up.
926
if (selection_data->target == gdk_atom_intern_static_string ("UTF8_STRING"))
928
gtk_clipboard_request_contents (clipboard,
929
gdk_atom_intern_static_string ("COMPOUND_TEXT"),
930
request_text_received_func, info);
933
else if (selection_data->target == gdk_atom_intern_static_string ("COMPOUND_TEXT"))
935
gtk_clipboard_request_contents (clipboard,
937
request_text_received_func, info);
942
info->callback (clipboard, result, info->user_data);
948
* gtk_clipboard_request_text:
949
* @clipboard: a #GtkClipboard
950
* @callback: a function to call when the text is received,
951
* or the retrieval fails. (It will always be called
952
* one way or the other.)
953
* @user_data: user data to pass to @callback.
955
* Requests the contents of the clipboard as text. When the text is
956
* later received, it will be converted to UTF-8 if necessary, and
957
* @callback will be called.
959
* The @text parameter to @callback will contain the resulting text if
960
* the request succeeded, or %NULL if it failed. This could happen for
961
* various reasons, in particular if the clipboard was empty or if the
962
* contents of the clipboard could not be converted into text form.
965
gtk_clipboard_request_text (GtkClipboard *clipboard,
966
GtkClipboardTextReceivedFunc callback,
969
RequestTextInfo *info;
971
g_return_if_fail (clipboard != NULL);
972
g_return_if_fail (callback != NULL);
974
info = g_new (RequestTextInfo, 1);
975
info->callback = callback;
976
info->user_data = user_data;
978
gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("UTF8_STRING"),
979
request_text_received_func,
984
request_rich_text_received_func (GtkClipboard *clipboard,
985
GtkSelectionData *selection_data,
988
RequestRichTextInfo *info = data;
989
guint8 *result = NULL;
992
result = selection_data->data;
993
length = selection_data->length;
995
info->current_atom++;
997
if ((!result || length < 1) && (info->current_atom < info->n_atoms))
999
gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1000
request_rich_text_received_func,
1005
info->callback (clipboard, selection_data->target, result, length,
1007
g_free (info->atoms);
1012
* gtk_clipboard_request_rich_text:
1013
* @clipboard: a #GtkClipboard
1014
* @buffer: a #GtkTextBuffer
1015
* @callback: a function to call when the text is received,
1016
* or the retrieval fails. (It will always be called
1017
* one way or the other.)
1018
* @user_data: user data to pass to @callback.
1020
* Requests the contents of the clipboard as rich text. When the rich
1021
* text is later received, @callback will be called.
1023
* The @text parameter to @callback will contain the resulting rich
1024
* text if the request succeeded, or %NULL if it failed. The @length
1025
* parameter will contain @text's length. This function can fail for
1026
* various reasons, in particular if the clipboard was empty or if the
1027
* contents of the clipboard could not be converted into rich text form.
1032
gtk_clipboard_request_rich_text (GtkClipboard *clipboard,
1033
GtkTextBuffer *buffer,
1034
GtkClipboardRichTextReceivedFunc callback,
1037
RequestRichTextInfo *info;
1039
g_return_if_fail (clipboard != NULL);
1040
g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1041
g_return_if_fail (callback != NULL);
1043
info = g_new (RequestRichTextInfo, 1);
1044
info->callback = callback;
1047
info->current_atom = 0;
1048
info->user_data = user_data;
1050
info->atoms = gtk_text_buffer_get_deserialize_formats (buffer, &info->n_atoms);
1052
gtk_clipboard_request_contents (clipboard, info->atoms[info->current_atom],
1053
request_rich_text_received_func,
1058
request_image_received_func (GtkClipboard *clipboard,
1059
GtkSelectionData *selection_data,
1062
RequestImageInfo *info = data;
1063
GdkPixbuf *result = NULL;
1065
result = gtk_selection_data_get_pixbuf (selection_data);
1069
/* If we asked for image/png and didn't get it, try image/jpeg;
1070
* if we asked for image/jpeg and didn't get it, try image/gif;
1071
* if we asked for image/gif and didn't get it, try image/bmp;
1072
* If we asked for anything else and didn't get it, give up.
1074
if (selection_data->target == gdk_atom_intern_static_string ("image/png"))
1076
gtk_clipboard_request_contents (clipboard,
1077
gdk_atom_intern_static_string ("image/jpeg"),
1078
request_image_received_func, info);
1081
else if (selection_data->target == gdk_atom_intern_static_string ("image/jpeg"))
1083
gtk_clipboard_request_contents (clipboard,
1084
gdk_atom_intern_static_string ("image/gif"),
1085
request_image_received_func, info);
1088
else if (selection_data->target == gdk_atom_intern_static_string ("image/gif"))
1090
gtk_clipboard_request_contents (clipboard,
1091
gdk_atom_intern_static_string ("image/bmp"),
1092
request_image_received_func, info);
1097
info->callback (clipboard, result, info->user_data);
1101
g_object_unref (result);
1105
* gtk_clipboard_request_image:
1106
* @clipboard: a #GtkClipboard
1107
* @callback: a function to call when the image is received,
1108
* or the retrieval fails. (It will always be called
1109
* one way or the other.)
1110
* @user_data: user data to pass to @callback.
1112
* Requests the contents of the clipboard as image. When the image is
1113
* later received, it will be converted to a #GdkPixbuf, and
1114
* @callback will be called.
1116
* The @pixbuf parameter to @callback will contain the resulting
1117
* #GdkPixbuf if the request succeeded, or %NULL if it failed. This
1118
* could happen for various reasons, in particular if the clipboard
1119
* was empty or if the contents of the clipboard could not be
1120
* converted into an image.
1125
gtk_clipboard_request_image (GtkClipboard *clipboard,
1126
GtkClipboardImageReceivedFunc callback,
1129
RequestImageInfo *info;
1131
g_return_if_fail (clipboard != NULL);
1132
g_return_if_fail (callback != NULL);
1134
info = g_new (RequestImageInfo, 1);
1135
info->callback = callback;
1136
info->user_data = user_data;
1138
gtk_clipboard_request_contents (clipboard,
1139
gdk_atom_intern_static_string ("image/png"),
1140
request_image_received_func,
1145
request_targets_received_func (GtkClipboard *clipboard,
1146
GtkSelectionData *selection_data,
1149
RequestTargetsInfo *info = data;
1150
GdkAtom *targets = NULL;
1153
gtk_selection_data_get_targets (selection_data, &targets, &n_targets);
1155
info->callback (clipboard, targets, n_targets, info->user_data);
1162
* gtk_clipboard_request_targets:
1163
* @clipboard: a #GtkClipboard
1164
* @callback: a function to call when the targets are received,
1165
* or the retrieval fails. (It will always be called
1166
* one way or the other.)
1167
* @user_data: user data to pass to @callback.
1169
* Requests the contents of the clipboard as list of supported targets.
1170
* When the list is later received, @callback will be called.
1172
* The @targets parameter to @callback will contain the resulting targets if
1173
* the request succeeded, or %NULL if it failed.
1178
gtk_clipboard_request_targets (GtkClipboard *clipboard,
1179
GtkClipboardTargetsReceivedFunc callback,
1182
RequestTargetsInfo *info;
1184
g_return_if_fail (clipboard != NULL);
1185
g_return_if_fail (callback != NULL);
1187
/* If the display supports change notification we cache targets */
1188
if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1189
clipboard->n_cached_targets != -1)
1191
(* callback) (clipboard, clipboard->cached_targets, clipboard->n_cached_targets, user_data);
1195
info = g_new (RequestTargetsInfo, 1);
1196
info->callback = callback;
1197
info->user_data = user_data;
1199
gtk_clipboard_request_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"),
1200
request_targets_received_func,
1208
GdkAtom format; /* used by rich text */
1209
gsize length; /* used by rich text */
1213
clipboard_received_func (GtkClipboard *clipboard,
1214
GtkSelectionData *selection_data,
1217
WaitResults *results = data;
1219
if (selection_data->length >= 0)
1220
results->data = gtk_selection_data_copy (selection_data);
1222
g_main_loop_quit (results->loop);
1226
* gtk_clipboard_wait_for_contents:
1227
* @clipboard: a #GtkClipboard
1228
* @target: an atom representing the form into which the clipboard
1229
* owner should convert the selection.
1231
* Requests the contents of the clipboard using the given target.
1232
* This function waits for the data to be received using the main
1233
* loop, so events, timeouts, etc, may be dispatched during the wait.
1235
* Return value: a newly-allocated #GtkSelectionData object or %NULL
1236
* if retrieving the given target failed. If non-%NULL,
1237
* this value must be freed with gtk_selection_data_free()
1238
* when you are finished with it.
1241
gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
1244
WaitResults results;
1246
g_return_val_if_fail (clipboard != NULL, NULL);
1247
g_return_val_if_fail (target != GDK_NONE, NULL);
1249
results.data = NULL;
1250
results.loop = g_main_loop_new (NULL, TRUE);
1252
gtk_clipboard_request_contents (clipboard, target,
1253
clipboard_received_func,
1256
if (g_main_loop_is_running (results.loop))
1258
GDK_THREADS_LEAVE ();
1259
g_main_loop_run (results.loop);
1260
GDK_THREADS_ENTER ();
1263
g_main_loop_unref (results.loop);
1265
return results.data;
1269
clipboard_text_received_func (GtkClipboard *clipboard,
1273
WaitResults *results = data;
1275
results->data = g_strdup (text);
1276
g_main_loop_quit (results->loop);
1280
* gtk_clipboard_wait_for_text:
1281
* @clipboard: a #GtkClipboard
1283
* Requests the contents of the clipboard as text and converts
1284
* the result to UTF-8 if necessary. This function waits for
1285
* the data to be received using the main loop, so events,
1286
* timeouts, etc, may be dispatched during the wait.
1288
* Return value: a newly-allocated UTF-8 string which must
1289
* be freed with g_free(), or %NULL if retrieving
1290
* the selection data failed. (This could happen
1291
* for various reasons, in particular if the
1292
* clipboard was empty or if the contents of the
1293
* clipboard could not be converted into text form.)
1296
gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
1298
WaitResults results;
1300
g_return_val_if_fail (clipboard != NULL, NULL);
1302
results.data = NULL;
1303
results.loop = g_main_loop_new (NULL, TRUE);
1305
gtk_clipboard_request_text (clipboard,
1306
clipboard_text_received_func,
1309
if (g_main_loop_is_running (results.loop))
1311
GDK_THREADS_LEAVE ();
1312
g_main_loop_run (results.loop);
1313
GDK_THREADS_ENTER ();
1316
g_main_loop_unref (results.loop);
1318
return results.data;
1322
clipboard_rich_text_received_func (GtkClipboard *clipboard,
1328
WaitResults *results = data;
1330
results->data = g_memdup (text, length);
1331
results->format = format;
1332
results->length = length;
1333
g_main_loop_quit (results->loop);
1337
* gtk_clipboard_wait_for_rich_text:
1338
* @clipboard: a #GtkClipboard
1339
* @buffer: a #GtkTextBuffer
1340
* @format: return location for the format of the returned data
1341
* @length: return location for the length of the returned data
1343
* Requests the contents of the clipboard as rich text. This function
1344
* waits for the data to be received using the main loop, so events,
1345
* timeouts, etc, may be dispatched during the wait.
1347
* Return value: a newly-allocated binary block of data which must
1348
* be freed with g_free(), or %NULL if retrieving
1349
* the selection data failed. (This could happen
1350
* for various reasons, in particular if the
1351
* clipboard was empty or if the contents of the
1352
* clipboard could not be converted into text form.)
1357
gtk_clipboard_wait_for_rich_text (GtkClipboard *clipboard,
1358
GtkTextBuffer *buffer,
1362
WaitResults results;
1364
g_return_val_if_fail (clipboard != NULL, NULL);
1365
g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1366
g_return_val_if_fail (format != NULL, NULL);
1367
g_return_val_if_fail (length != NULL, NULL);
1369
results.data = NULL;
1370
results.loop = g_main_loop_new (NULL, TRUE);
1372
gtk_clipboard_request_rich_text (clipboard, buffer,
1373
clipboard_rich_text_received_func,
1376
if (g_main_loop_is_running (results.loop))
1378
GDK_THREADS_LEAVE ();
1379
g_main_loop_run (results.loop);
1380
GDK_THREADS_ENTER ();
1383
g_main_loop_unref (results.loop);
1385
*format = results.format;
1386
*length = results.length;
1388
return results.data;
1392
clipboard_image_received_func (GtkClipboard *clipboard,
1396
WaitResults *results = data;
1399
results->data = g_object_ref (pixbuf);
1401
g_main_loop_quit (results->loop);
1405
* gtk_clipboard_wait_for_image:
1406
* @clipboard: a #GtkClipboard
1408
* Requests the contents of the clipboard as image and converts
1409
* the result to a #GdkPixbuf. This function waits for
1410
* the data to be received using the main loop, so events,
1411
* timeouts, etc, may be dispatched during the wait.
1413
* Return value: a newly-allocated #GdkPixbuf object which must
1414
* be disposed with g_object_unref(), or %NULL if
1415
* retrieving the selection data failed. (This
1416
* could happen for various reasons, in particular
1417
* if the clipboard was empty or if the contents of
1418
* the clipboard could not be converted into an image.)
1423
gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
1425
WaitResults results;
1427
g_return_val_if_fail (clipboard != NULL, NULL);
1429
results.data = NULL;
1430
results.loop = g_main_loop_new (NULL, TRUE);
1432
gtk_clipboard_request_image (clipboard,
1433
clipboard_image_received_func,
1436
if (g_main_loop_is_running (results.loop))
1438
GDK_THREADS_LEAVE ();
1439
g_main_loop_run (results.loop);
1440
GDK_THREADS_ENTER ();
1443
g_main_loop_unref (results.loop);
1445
return results.data;
1449
* gtk_clipboard_get_display:
1450
* @clipboard: a #GtkClipboard
1452
* Gets the #GdkDisplay associated with @clipboard
1454
* Return value: the #GdkDisplay associated with @clipboard
1459
gtk_clipboard_get_display (GtkClipboard *clipboard)
1461
g_return_val_if_fail (clipboard != NULL, NULL);
1463
return clipboard->display;
1467
* gtk_clipboard_wait_is_text_available:
1468
* @clipboard: a #GtkClipboard
1470
* Test to see if there is text available to be pasted
1471
* This is done by requesting the TARGETS atom and checking
1472
* if it contains any of the supported text targets. This function
1473
* waits for the data to be received using the main loop, so events,
1474
* timeouts, etc, may be dispatched during the wait.
1476
* This function is a little faster than calling
1477
* gtk_clipboard_wait_for_text() since it doesn't need to retrieve
1480
* Return value: %TRUE is there is text available, %FALSE otherwise.
1483
gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
1485
GtkSelectionData *data;
1486
gboolean result = FALSE;
1488
data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1491
result = gtk_selection_data_targets_include_text (data);
1492
gtk_selection_data_free (data);
1499
* gtk_clipboard_wait_is_rich_text_available:
1500
* @clipboard: a #GtkClipboard
1501
* @buffer: a #GtkTextBuffer
1503
* Test to see if there is rich text available to be pasted
1504
* This is done by requesting the TARGETS atom and checking
1505
* if it contains any of the supported rich text targets. This function
1506
* waits for the data to be received using the main loop, so events,
1507
* timeouts, etc, may be dispatched during the wait.
1509
* This function is a little faster than calling
1510
* gtk_clipboard_wait_for_rich_text() since it doesn't need to retrieve
1513
* Return value: %TRUE is there is rich text available, %FALSE otherwise.
1518
gtk_clipboard_wait_is_rich_text_available (GtkClipboard *clipboard,
1519
GtkTextBuffer *buffer)
1521
GtkSelectionData *data;
1522
gboolean result = FALSE;
1524
g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
1525
g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1527
data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1530
result = gtk_selection_data_targets_include_rich_text (data, buffer);
1531
gtk_selection_data_free (data);
1538
* gtk_clipboard_wait_is_image_available:
1539
* @clipboard: a #GtkClipboard
1541
* Test to see if there is an image available to be pasted
1542
* This is done by requesting the TARGETS atom and checking
1543
* if it contains any of the supported image targets. This function
1544
* waits for the data to be received using the main loop, so events,
1545
* timeouts, etc, may be dispatched during the wait.
1547
* This function is a little faster than calling
1548
* gtk_clipboard_wait_for_image() since it doesn't need to retrieve
1549
* the actual image data.
1551
* Return value: %TRUE is there is an image available, %FALSE otherwise.
1556
gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
1558
GtkSelectionData *data;
1559
gboolean result = FALSE;
1561
data = gtk_clipboard_wait_for_contents (clipboard,
1562
gdk_atom_intern_static_string ("TARGETS"));
1565
result = gtk_selection_data_targets_include_image (data, FALSE);
1566
gtk_selection_data_free (data);
1573
* gtk_clipboard_wait_for_targets
1574
* @clipboard: a #GtkClipboard
1575
* @targets: location to store an array of targets. The result
1576
* stored here must be freed with g_free().
1577
* @n_targets: location to store number of items in @targets.
1579
* Returns a list of targets that are present on the clipboard, or %NULL
1580
* if there aren't any targets available. The returned list must be
1581
* freed with g_free().
1582
* This function waits for the data to be received using the main
1583
* loop, so events, timeouts, etc, may be dispatched during the wait.
1585
* Return value: %TRUE if any targets are present on the clipboard,
1591
gtk_clipboard_wait_for_targets (GtkClipboard *clipboard,
1595
GtkSelectionData *data;
1596
gboolean result = FALSE;
1598
g_return_val_if_fail (clipboard != NULL, FALSE);
1600
/* If the display supports change notification we cache targets */
1601
if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
1602
clipboard->n_cached_targets != -1)
1605
*n_targets = clipboard->n_cached_targets;
1608
*targets = g_memdup (clipboard->cached_targets,
1609
clipboard->n_cached_targets * sizeof (GdkAtom));
1620
data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
1624
GdkAtom *tmp_targets;
1627
result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
1629
if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
1631
clipboard->n_cached_targets = tmp_n_targets;
1632
clipboard->cached_targets = g_memdup (tmp_targets,
1633
tmp_n_targets * sizeof (GdkAtom));
1637
*n_targets = tmp_n_targets;
1640
*targets = tmp_targets;
1642
g_free (tmp_targets);
1644
gtk_selection_data_free (data);
1650
static GtkClipboard *
1651
clipboard_peek (GdkDisplay *display,
1653
gboolean only_if_exists)
1655
GtkClipboard *clipboard = NULL;
1659
if (selection == GDK_NONE)
1660
selection = GDK_SELECTION_CLIPBOARD;
1662
clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
1664
tmp_list = clipboards;
1667
clipboard = tmp_list->data;
1668
if (clipboard->selection == selection)
1671
tmp_list = tmp_list->next;
1674
if (!tmp_list && !only_if_exists)
1676
clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);
1677
clipboard->selection = selection;
1678
clipboard->display = display;
1679
clipboard->n_cached_targets = -1;
1680
clipboard->n_storable_targets = -1;
1681
clipboards = g_slist_prepend (clipboards, clipboard);
1682
g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
1683
g_signal_connect (display, "closed",
1684
G_CALLBACK (clipboard_display_closed), clipboard);
1685
gdk_display_request_selection_notification (display, selection);
1692
gtk_clipboard_owner_change (GtkClipboard *clipboard,
1693
GdkEventOwnerChange *event)
1695
if (clipboard->n_cached_targets != -1)
1697
clipboard->n_cached_targets = -1;
1698
g_free (clipboard->cached_targets);
1703
* gtk_clipboard_wait_is_target_available:
1704
* @clipboard: a #GtkClipboard
1705
* @target: A #GdkAtom indicating which target to look for.
1707
* Checks if a clipboard supports pasting data of a given type. This
1708
* function can be used to determine if a "Paste" menu item should be
1709
* insensitive or not.
1711
* If you want to see if there's text available on the clipboard, use
1712
* gtk_clipboard_wait_is_text_available () instead.
1714
* Return value: %TRUE if the target is available, %FALSE otherwise.
1719
gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
1724
gboolean retval = FALSE;
1726
if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
1729
for (i = 0; i < n_targets; i++)
1731
if (targets[i] == target)
1744
* _gtk_clipboard_handle_event:
1745
* @event: a owner change event
1747
* Emits the ::owner_change signal on the appropriate @clipboard.
1752
_gtk_clipboard_handle_event (GdkEventOwnerChange *event)
1754
GdkDisplay *display;
1755
GtkClipboard *clipboard;
1757
display = gdk_drawable_get_display (event->window);
1758
clipboard = clipboard_peek (display, event->selection, TRUE);
1761
g_signal_emit (clipboard,
1762
clipboard_signals[OWNER_CHANGE], 0, event, NULL);
1766
gtk_clipboard_store_timeout (GtkClipboard *clipboard)
1768
g_main_loop_quit (clipboard->store_loop);
1774
* gtk_clipboard_set_can_store:
1775
* @clipboard: a #GtkClipboard
1776
* @targets: array containing information about which forms should be stored
1777
* or %NULL to indicate that all forms should be stored.
1778
* @n_targets: number of elements in @targets
1780
* Hints that the clipboard data should be stored somewhere when the
1781
* application exits or when gtk_clipboard_store () is called.
1783
* This value is reset when the clipboard owner changes.
1784
* Where the clipboard data is stored is platform dependent,
1785
* see gdk_display_store_clipboard () for more information.
1790
gtk_clipboard_set_can_store (GtkClipboard *clipboard,
1791
const GtkTargetEntry *targets,
1794
GtkWidget *clipboard_widget;
1796
static const GtkTargetEntry save_targets[] = {
1797
{ "SAVE_TARGETS", 0, TARGET_SAVE_TARGETS }
1800
g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1801
g_return_if_fail (n_targets >= 0);
1803
if (clipboard->selection != GDK_SELECTION_CLIPBOARD)
1806
g_free (clipboard->storable_targets);
1808
clipboard_widget = get_clipboard_widget (clipboard->display);
1810
/* n_storable_targets being -1 means that
1811
* gtk_clipboard_set_can_store hasn't been called since the
1812
* clipboard owner changed. We only want to add SAVE_TARGETS and
1813
* ref the owner once , so we do that here
1815
if (clipboard->n_storable_targets == -1)
1817
gtk_selection_add_targets (clipboard_widget, clipboard->selection,
1820
/* Ref the owner so it won't go away */
1821
if (clipboard->have_owner)
1822
g_object_ref (clipboard->user_data);
1825
clipboard->n_storable_targets = n_targets;
1826
clipboard->storable_targets = g_new (GdkAtom, n_targets);
1827
for (i = 0; i < n_targets; i++)
1828
clipboard->storable_targets[i] = gdk_atom_intern (targets[i].target, FALSE);
1832
gtk_clipboard_selection_notify (GtkWidget *widget,
1833
GdkEventSelection *event,
1834
GtkClipboard *clipboard)
1836
if (event->selection == gdk_atom_intern_static_string ("CLIPBOARD_MANAGER") &&
1837
clipboard->storing_selection)
1838
g_main_loop_quit (clipboard->store_loop);
1844
* gtk_clipboard_store:
1845
* @clipboard: a #GtkClipboard
1847
* Stores the current clipboard data somewhere so that it will stay
1848
* around after the application has quit.
1853
gtk_clipboard_store (GtkClipboard *clipboard)
1855
GtkWidget *clipboard_widget;
1857
g_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
1859
if (clipboard->n_storable_targets < 0)
1862
if (!gdk_display_supports_clipboard_persistence (clipboard->display))
1865
clipboard_widget = get_clipboard_widget (clipboard->display);
1866
clipboard->notify_signal_id = g_signal_connect (clipboard_widget, "selection_notify_event",
1867
G_CALLBACK (gtk_clipboard_selection_notify), clipboard);
1869
gdk_display_store_clipboard (clipboard->display,
1870
clipboard_widget->window,
1871
clipboard_get_timestamp (clipboard),
1872
clipboard->storable_targets,
1873
clipboard->n_storable_targets);
1875
clipboard->storing_selection = TRUE;
1877
clipboard->store_loop = g_main_loop_new (NULL, TRUE);
1878
clipboard->store_timeout = g_timeout_add (10000, (GSourceFunc) gtk_clipboard_store_timeout, clipboard);
1880
if (g_main_loop_is_running (clipboard->store_loop))
1882
GDK_THREADS_LEAVE ();
1883
g_main_loop_run (clipboard->store_loop);
1884
GDK_THREADS_ENTER ();
1887
g_main_loop_unref (clipboard->store_loop);
1888
clipboard->store_loop = NULL;
1890
g_source_remove (clipboard->store_timeout);
1891
clipboard->store_timeout = 0;
1892
g_signal_handler_disconnect (clipboard_widget, clipboard->notify_signal_id);
1893
clipboard->notify_signal_id = 0;
1895
clipboard->storing_selection = FALSE;
1898
/* Stores all clipboard selections on all displays, called from
1902
_gtk_clipboard_store_all (void)
1904
GtkClipboard *clipboard;
1905
GSList *displays, *list;
1907
displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
1912
GdkDisplay *display = list->data;
1914
clipboard = clipboard_peek (display, GDK_SELECTION_CLIPBOARD, TRUE);
1917
gtk_clipboard_store (clipboard);
1921
g_slist_free (displays);
1925
#define __GTK_CLIPBOARD_C__
1926
#include "gtkaliasdef.c"