1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
3 nautilus-clipboard-monitor.c: catch clipboard changes.
5 Copyright (C) 2004 Red Hat, Inc.
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public
18 License along with this program; if not, write to the
19 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Author: Alexander Larsson <alexl@redhat.com>
26 #include "nautilus-clipboard-monitor.h"
28 #include <eel/eel-debug.h>
29 #include <eel/eel-gtk-macros.h>
30 #include <eel/eel-glib-extensions.h>
31 #include <eel/eel-glib-extensions.h>
32 #include <gtk/gtkclipboard.h>
34 /* X11 has a weakness when it comes to clipboard handling,
35 * there is no way to get told when the owner of the clipboard
36 * changes. This is often needed, for instance to set the
37 * sensitivity of the paste menu item. We work around this
38 * internally in an app by telling the clipboard monitor when
39 * we changed the clipboard. Unfortunately this doesn't give
40 * us perfect results, we still don't catch changes made by
43 * This is fixed with the XFIXES extensions, which recent versions
44 * of Gtk+ supports as the owner_change signal on GtkClipboard. We
45 * use this now, but keep the old code since not all X servers support
54 static guint signals
[LAST_SIGNAL
];
56 static void nautilus_clipboard_monitor_init (gpointer object
,
58 static void nautilus_clipboard_monitor_class_init (gpointer klass
);
60 EEL_CLASS_BOILERPLATE (NautilusClipboardMonitor
,
61 nautilus_clipboard_monitor
,
64 static NautilusClipboardMonitor
*clipboard_monitor
= NULL
;
67 destroy_clipboard_monitor (void)
69 if (clipboard_monitor
!= NULL
) {
70 g_object_unref (clipboard_monitor
);
74 NautilusClipboardMonitor
*
75 nautilus_clipboard_monitor_get (void)
77 GtkClipboard
*clipboard
;
79 if (clipboard_monitor
== NULL
) {
80 clipboard_monitor
= NAUTILUS_CLIPBOARD_MONITOR (g_object_new (NAUTILUS_TYPE_CLIPBOARD_MONITOR
, NULL
));
81 eel_debug_call_at_shutdown (destroy_clipboard_monitor
);
83 clipboard
= gtk_clipboard_get (GDK_SELECTION_CLIPBOARD
);
84 g_signal_connect (clipboard
, "owner_change",
85 G_CALLBACK (nautilus_clipboard_monitor_emit_changed
), NULL
);
87 return clipboard_monitor
;
91 nautilus_clipboard_monitor_emit_changed (void)
93 NautilusClipboardMonitor
*monitor
;
95 monitor
= nautilus_clipboard_monitor_get ();
97 g_signal_emit (monitor
, signals
[CLIPBOARD_CHANGED
], 0);
101 nautilus_clipboard_monitor_init (gpointer object
, gpointer klass
)
103 NautilusClipboardMonitor
*monitor
;
105 monitor
= NAUTILUS_CLIPBOARD_MONITOR (object
);
109 clipboard_monitor_finalize (GObject
*object
)
111 NautilusClipboardMonitor
*monitor
;
113 monitor
= NAUTILUS_CLIPBOARD_MONITOR (object
);
115 EEL_CALL_PARENT (G_OBJECT_CLASS
, finalize
, (object
));
119 nautilus_clipboard_monitor_class_init (gpointer klass
)
121 GObjectClass
*object_class
;
123 object_class
= G_OBJECT_CLASS (klass
);
125 object_class
->finalize
= clipboard_monitor_finalize
;
127 signals
[CLIPBOARD_CHANGED
] =
128 g_signal_new ("clipboard_changed",
129 G_TYPE_FROM_CLASS (klass
),
131 G_STRUCT_OFFSET (NautilusClipboardMonitorClass
, clipboard_changed
),
133 g_cclosure_marshal_VOID__VOID
,