2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant 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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "clipboard.h"
24 #include "stream/memstream.h"
25 #include "stream/textstream.h"
29 /// \brief Platform-independent GTK clipboard support.
30 /// \todo Using GDK_SELECTION_CLIPBOARD fails on win32, so we use the win32 API directly for now.
33 const char* c_clipboard_format = "RadiantClippings";
37 void clipboard_copy(ClipboardCopyFunc copy)
39 BufferOutputStream ostream;
42 bool bClipped = false;
43 UINT nClipboard = ::RegisterClipboardFormat(c_clipboard_format);
46 if (::OpenClipboard(0))
49 std::size_t length = ostream.size();
50 HANDLE h = ::GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, length + sizeof(std::size_t));
53 char *buffer = reinterpret_cast<char*>(::GlobalLock(h));
54 *reinterpret_cast<std::size_t*>(buffer) = length;
55 buffer += sizeof(std::size_t);
56 memcpy(buffer, ostream.data(), length);
58 ::SetClipboardData(nClipboard, h);
67 globalOutputStream() << "Unable to register Windows clipboard formats, copy/paste between editors will not be possible\n";
71 void clipboard_paste(ClipboardPasteFunc paste)
73 UINT nClipboard = ::RegisterClipboardFormat(c_clipboard_format);
74 if (nClipboard > 0 && ::OpenClipboard(0))
76 if(IsClipboardFormatAvailable(nClipboard))
78 HANDLE h = ::GetClipboardData(nClipboard);
81 const char *buffer = reinterpret_cast<const char*>(::GlobalLock(h));
82 std::size_t length = *reinterpret_cast<const std::size_t*>(buffer);
83 buffer += sizeof(std::size_t);
84 BufferInputStream istream(buffer, length);
95 #include <gtk/gtkclipboard.h>
99 RADIANT_CLIPPINGS = 23,
102 static const GtkTargetEntry clipboard_targets[] = {
103 { "RADIANT_CLIPPINGS", 0, RADIANT_CLIPPINGS, },
106 static void clipboard_get (GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, gpointer data)
108 std::size_t len = *reinterpret_cast<std::size_t*>(data);
109 const char* buffer = (len != 0) ? reinterpret_cast<const char*>(data) + sizeof(std::size_t) : 0;
111 GdkAtom type = GDK_NONE;
112 if(info == clipboard_targets[0].info)
114 type = gdk_atom_intern(clipboard_targets[0].target, FALSE);
117 gtk_selection_data_set (selection_data, type, 8, reinterpret_cast<const guchar*>(buffer), static_cast<gint>(len));
120 static void clipboard_clear (GtkClipboard *clipboard, gpointer data)
122 delete [] reinterpret_cast<const char*>(data);
125 static void clipboard_received (GtkClipboard *clipboard, GtkSelectionData *data, gpointer user_data)
127 if (data->length < 0)
129 globalErrorStream() << "Error retrieving selection\n";
131 else if(strcmp(gdk_atom_name(data->type), clipboard_targets[0].target) == 0)
133 BufferInputStream istream(reinterpret_cast<const char*>(data->data), data->length);
134 (*reinterpret_cast<ClipboardPasteFunc*>(user_data))(istream);
138 void clipboard_copy(ClipboardCopyFunc copy)
140 GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
142 BufferOutputStream ostream;
144 std::size_t length = ostream.size();
145 char* data = new char[length + sizeof(std::size_t)];
146 *reinterpret_cast<std::size_t*>(data) = length;
147 memcpy(data + sizeof(std::size_t), ostream.data(), length);
149 gtk_clipboard_set_with_data (clipboard, clipboard_targets, 1, clipboard_get, clipboard_clear, data);
152 ClipboardPasteFunc g_clipboardPasteFunc = 0;
153 void clipboard_paste(ClipboardPasteFunc paste)
155 GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
157 g_clipboardPasteFunc = paste;
158 gtk_clipboard_request_contents (clipboard, gdk_atom_intern(clipboard_targets[0].target, FALSE), clipboard_received, &g_clipboardPasteFunc);