]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/clipboard.cpp
Remove -Wno-pedantic
[xonotic/netradiant.git] / libs / gtkutil / clipboard.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "clipboard.h"
23
24 #include "stream/memstream.h"
25 #include "stream/textstream.h"
26
27
28 /// \file
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.
31 #if defined( WIN32 )
32
33 const char* c_clipboard_format = "RadiantClippings";
34
35 #include <windows.h>
36
37 void clipboard_copy( ClipboardCopyFunc copy ){
38         BufferOutputStream ostream;
39         copy( ostream );
40
41         bool bClipped = false;
42         UINT nClipboard = ::RegisterClipboardFormat( c_clipboard_format );
43         if ( nClipboard > 0 ) {
44                 if ( ::OpenClipboard( 0 ) ) {
45                         EmptyClipboard();
46                         std::size_t length = ostream.size();
47                         HANDLE h = ::GlobalAlloc( GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, length + sizeof( std::size_t ) );
48                         if ( h != 0 ) {
49                                 char *buffer = reinterpret_cast<char*>( ::GlobalLock( h ) );
50                                 *reinterpret_cast<std::size_t*>( buffer ) = length;
51                                 buffer += sizeof( std::size_t );
52                                 memcpy( buffer, ostream.data(), length );
53                                 ::GlobalUnlock( h );
54                                 ::SetClipboardData( nClipboard, h );
55                                 ::CloseClipboard();
56                                 bClipped = true;
57                         }
58                 }
59         }
60
61         if ( !bClipped ) {
62                 globalOutputStream() << "Unable to register Windows clipboard formats, copy/paste between editors will not be possible\n";
63         }
64 }
65
66 void clipboard_paste( ClipboardPasteFunc paste ){
67         UINT nClipboard = ::RegisterClipboardFormat( c_clipboard_format );
68         if ( nClipboard > 0 && ::OpenClipboard( 0 ) ) {
69                 if ( IsClipboardFormatAvailable( nClipboard ) ) {
70                         HANDLE h = ::GetClipboardData( nClipboard );
71                         if ( h ) {
72                                 const char *buffer = reinterpret_cast<const char*>( ::GlobalLock( h ) );
73                                 std::size_t length = *reinterpret_cast<const std::size_t*>( buffer );
74                                 buffer += sizeof( std::size_t );
75                                 BufferInputStream istream( buffer, length );
76                                 paste( istream );
77                                 ::GlobalUnlock( h );
78                         }
79                 }
80                 ::CloseClipboard();
81         }
82 }
83
84 #else
85
86 #include <gtk/gtk.h>
87
88 enum
89 {
90         RADIANT_CLIPPINGS = 23,
91 };
92
93 static char RADIANT_CLIPPINGS_STR[] = "RADIANT_CLIPPINGS";
94
95 static const GtkTargetEntry clipboard_targets[] = {
96         {RADIANT_CLIPPINGS_STR, 0, RADIANT_CLIPPINGS, },
97 };
98
99 static void clipboard_get( GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, gpointer data ){
100         std::size_t len = *reinterpret_cast<std::size_t*>( data );
101         const char* buffer = ( len != 0 ) ? reinterpret_cast<const char*>( data ) + sizeof( std::size_t ) : 0;
102
103         GdkAtom type = GDK_NONE;
104         if ( info == clipboard_targets[0].info ) {
105                 type = gdk_atom_intern( clipboard_targets[0].target, FALSE );
106         }
107
108         gtk_selection_data_set( selection_data, type, 8, reinterpret_cast<const guchar*>( buffer ), static_cast<gint>( len ) );
109 }
110
111 static void clipboard_clear( GtkClipboard *clipboard, gpointer data ){
112         delete [] reinterpret_cast<const char*>( data );
113 }
114
115 static void clipboard_received( GtkClipboard *clipboard, GtkSelectionData *data, gpointer user_data ){
116         if ( gtk_selection_data_get_length(data) < 0 ) {
117                 globalErrorStream() << "Error retrieving selection\n";
118         }
119         else if ( strcmp( gdk_atom_name( gtk_selection_data_get_data_type(data) ), clipboard_targets[0].target ) == 0 ) {
120                 BufferInputStream istream( reinterpret_cast<const char*>( gtk_selection_data_get_data(data) ), gtk_selection_data_get_length(data) );
121                 ( *reinterpret_cast<ClipboardPasteFunc*>( user_data ) )( istream );
122         }
123 }
124
125 void clipboard_copy( ClipboardCopyFunc copy ){
126         GtkClipboard* clipboard = gtk_clipboard_get( GDK_SELECTION_CLIPBOARD );
127
128         BufferOutputStream ostream;
129         copy( ostream );
130         std::size_t length = ostream.size();
131         char* data = new char[length + sizeof( std::size_t )];
132         *reinterpret_cast<std::size_t*>( data ) = length;
133         memcpy( data + sizeof( std::size_t ), ostream.data(), length );
134
135         gtk_clipboard_set_with_data( clipboard, clipboard_targets, 1, clipboard_get, clipboard_clear, data );
136 }
137
138 ClipboardPasteFunc g_clipboardPasteFunc = 0;
139 void clipboard_paste( ClipboardPasteFunc paste ){
140         GtkClipboard* clipboard = gtk_clipboard_get( GDK_SELECTION_CLIPBOARD );
141
142         g_clipboardPasteFunc = paste;
143         gtk_clipboard_request_contents( clipboard, gdk_atom_intern( clipboard_targets[0].target, FALSE ), clipboard_received, &g_clipboardPasteFunc );
144 }
145
146
147 #endif