2008-02-29 Cosimo Cecchi <cosimoc@gnome.org>
[nautilus.git] / libnautilus-private / nautilus-clipboard-monitor.c
blob3fb40ac04ad6ed79c4456771f7374fb1fbf4647b
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>
25 #include <config.h>
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
41 * other clients
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
46 * XFIXES.
49 enum {
50 CLIPBOARD_CHANGED,
51 LAST_SIGNAL
54 static guint signals[LAST_SIGNAL];
56 static void nautilus_clipboard_monitor_init (gpointer object,
57 gpointer klass);
58 static void nautilus_clipboard_monitor_class_init (gpointer klass);
60 EEL_CLASS_BOILERPLATE (NautilusClipboardMonitor,
61 nautilus_clipboard_monitor,
62 G_TYPE_OBJECT)
64 static NautilusClipboardMonitor *clipboard_monitor = NULL;
66 static void
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;
90 void
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);
100 static void
101 nautilus_clipboard_monitor_init (gpointer object, gpointer klass)
103 NautilusClipboardMonitor *monitor;
105 monitor = NAUTILUS_CLIPBOARD_MONITOR (object);
108 static void
109 clipboard_monitor_finalize (GObject *object)
111 NautilusClipboardMonitor *monitor;
113 monitor = NAUTILUS_CLIPBOARD_MONITOR (object);
115 EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
118 static void
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),
130 G_SIGNAL_RUN_LAST,
131 G_STRUCT_OFFSET (NautilusClipboardMonitorClass, clipboard_changed),
132 NULL, NULL,
133 g_cclosure_marshal_VOID__VOID,
134 G_TYPE_NONE, 0);