-
-
Notifications
You must be signed in to change notification settings - Fork 611
Expand file tree
/
Copy pathclipboard.cpp
More file actions
363 lines (282 loc) · 11.8 KB
/
clipboard.cpp
File metadata and controls
363 lines (282 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "clipboard.h"
#include <wx/clipbrd.h>
#include <wx/dataobj.h>
#include <wx/image.h>
#include <wx/log.h>
#include <wx/mstream.h>
#include <wx/string.h>
#include <wx/sstream.h>
#include <io/csv.h>
bool SaveClipboard( const std::string& aTextUTF8 )
{
wxLogNull doNotLog; // disable logging of failed clipboard actions
if( wxTheClipboard->Open() )
{
// Store the UTF8 string as Unicode string in clipboard:
wxTheClipboard->SetData(
new wxTextDataObject( wxString( aTextUTF8.c_str(), wxConvUTF8 ) ) );
wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
wxTheClipboard->Close();
return true;
}
return false;
}
bool SaveClipboard( const std::string& aTextUTF8, const std::vector<CLIPBOARD_MIME_DATA>& aMimeData )
{
if( aMimeData.empty() )
return SaveClipboard( aTextUTF8 );
wxLogNull doNotLog; // disable logging of failed clipboard actions
if( wxTheClipboard->Open() )
{
wxDataObjectComposite* data = new wxDataObjectComposite();
data->Add( new wxTextDataObject( wxString( aTextUTF8.c_str(), wxConvUTF8 ) ), true );
for( const CLIPBOARD_MIME_DATA& entry : aMimeData )
{
// Skip entries with no data (check both buffer and image)
if( entry.m_data.GetDataLen() == 0 && !entry.m_image.has_value() )
continue;
// Handle pre-encoded PNG data (GTK optimization path).
// Use wxDF_BITMAP to prevent wx from setting the type to Private
if( entry.m_useRawPngData && entry.m_data.GetDataLen() > 0 )
{
wxCustomDataObject* custom = new wxCustomDataObject( wxDF_BITMAP );
custom->Alloc( entry.m_data.GetDataLen() );
custom->SetData( entry.m_data.GetDataLen(), entry.m_data.GetData() );
data->Add( custom );
continue;
}
// Handle bitmap image data using platform-native clipboard format.
// wxBitmapDataObject automatically converts to the appropriate format:
// - Windows: CF_DIB
// - macOS: kUTTypeTIFF
if( entry.m_image.has_value() && entry.m_image->IsOk() )
{
data->Add( new wxBitmapDataObject( *entry.m_image ) );
continue;
}
// Add custom format data - note that on GTK, custom MIME types may not work
// with all applications (they often become wxDF_PRIVATE internally), but they
// work for KiCad-to-KiCad transfers.
// We allocate and set data in a way that ensures the object is fully initialized.
wxDataFormat format( entry.m_mimeType );
wxCustomDataObject* custom = new wxCustomDataObject( format );
// Allocate buffer first, then copy data - this ensures proper initialization
custom->Alloc( entry.m_data.GetDataLen() );
custom->SetData( entry.m_data.GetDataLen(), entry.m_data.GetData() );
data->Add( custom );
}
wxTheClipboard->SetData( data );
wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
wxTheClipboard->Close();
return true;
}
return false;
}
std::string GetClipboardUTF8()
{
std::string result;
wxLogNull doNotLog; // disable logging of failed clipboard actions
// Check if clipboard is already open. This can happen if another event handler (such as
// wxTextEntry::CanPaste() called during UPDATE_UI processing) opened the clipboard and
// didn't close it. We handle this by reusing the existing session.
bool wasAlreadyOpen = wxTheClipboard->IsOpened();
bool isOpen = wasAlreadyOpen || wxTheClipboard->Open();
if( isOpen )
{
if( wxTheClipboard->IsSupported( wxDataFormat( wxS( "application/kicad" ) ) ) )
{
wxCustomDataObject data( wxDataFormat( wxS( "application/kicad" ) ) );
if( wxTheClipboard->GetData( data ) )
{
result.assign( static_cast<const char*>( data.GetData() ), data.GetSize() );
if( !wasAlreadyOpen )
wxTheClipboard->Close();
return result;
}
}
if( wxTheClipboard->IsSupported( wxDF_TEXT )
|| wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
{
wxTextDataObject data;
wxTheClipboard->GetData( data );
// The clipboard is expected containing a Unicode string, so return it
// as UTF8 string
result = data.GetText().utf8_str();
}
if( !wasAlreadyOpen )
wxTheClipboard->Close();
}
return result;
}
std::unique_ptr<wxBitmap> GetImageFromClipboard()
{
std::unique_ptr<wxBitmap> bitmap;
wxLogNull doNotLog; // disable logging of failed clipboard actions
// Check if clipboard is already open (same handling as GetClipboardUTF8)
bool wasAlreadyOpen = wxTheClipboard->IsOpened();
bool isOpen = wasAlreadyOpen || wxTheClipboard->Open();
if( isOpen )
{
if( wxTheClipboard->IsSupported( wxDF_BITMAP ) )
{
wxBitmapDataObject data;
if( wxTheClipboard->GetData( data ) )
{
bitmap = std::make_unique<wxBitmap>( data.GetBitmap() );
}
}
#ifdef __APPLE__
// macOS screenshots use PNG format with UTI "public.png"
else if( wxDataFormat pngFormat( wxS( "public.png" ) ); wxTheClipboard->IsSupported( pngFormat ) )
{
wxCustomDataObject pngData( pngFormat );
if( wxTheClipboard->GetData( pngData ) && pngData.GetSize() > 0 )
{
wxMemoryInputStream stream( pngData.GetData(), pngData.GetSize() );
wxImage img( stream, wxBITMAP_TYPE_PNG );
if( img.IsOk() )
bitmap = std::make_unique<wxBitmap>( img );
}
}
#endif
else if( wxTheClipboard->IsSupported( wxDF_FILENAME ) )
{
wxFileDataObject data;
if( wxTheClipboard->GetData( data ) && data.GetFilenames().size() == 1 )
{
wxImage img( data.GetFilenames()[0] );
bitmap = std::make_unique<wxBitmap>( img );
if( !bitmap->IsOk() )
bitmap.reset();
}
}
if( !wasAlreadyOpen )
wxTheClipboard->Close();
}
return bitmap;
}
bool SaveTabularDataToClipboard( const std::vector<std::vector<wxString>>& aData )
{
wxLogNull doNotLog; // disable logging of failed clipboard actions
if( wxTheClipboard->Open() )
{
wxDataObjectComposite* data = new wxDataObjectComposite();
// Set plain text CSV
{
wxStringOutputStream os;
CSV_WRITER writer( os );
writer.WriteLines( aData );
data->Add( new wxTextDataObject( os.GetString() ), true );
}
// At this point, it would be great if we could add some format that spreadsheet
// programs can understand without asking the user for options: perhaps SYLK or DIF.
// But it doesn't seem like WX allows to put arbitrary MIME types on the clipboard,
// even with wxCustomDataObject( wxDataFormat( "mime/type" ) ), which just ends up as
// wxDF_PRIVATE, and wxDF_SYLK/DIF aren't mapped on GTK.
wxTheClipboard->SetData( data );
wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
wxTheClipboard->Close();
return true;
}
return false;
}
bool GetTabularDataFromClipboard( std::vector<std::vector<wxString>>& aData )
{
// Again, it would be ideal if we could detect a spreadsheet mimetype here,
// but WX doesn't seem to do that on Linux, at least.
bool ok = false;
// Check if clipboard is already open (same handling as GetClipboardUTF8)
bool wasAlreadyOpen = wxTheClipboard->IsOpened();
bool isOpen = wasAlreadyOpen || wxTheClipboard->Open();
if( isOpen )
{
if( wxTheClipboard->IsSupported( wxDF_TEXT ) || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
{
wxTextDataObject data;
if( wxTheClipboard->GetData( data ) )
ok = AutoDecodeCSV( data.GetText(), aData );
}
// We could also handle .csv wxDF_FILENAMEs here
if( !wasAlreadyOpen )
wxTheClipboard->Close();
}
return ok;
}
bool EncodeImageToPng( const wxImage& aImage, wxMemoryBuffer& aOutput )
{
if( !aImage.IsOk() )
return false;
wxImage imageCopy( aImage );
// Fast PNG settings optimized for clipboard graphics
imageCopy.SetOption( wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL, 1 ); // Z_BEST_SPEED
imageCopy.SetOption( wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY, 3 ); // Z_RLE
imageCopy.SetOption( wxIMAGE_OPTION_PNG_FILTER, 0x08 ); // PNG_FILTER_NONE
wxMemoryOutputStream memStream;
wxBufferedOutputStream bufferedStream( memStream );
if( !imageCopy.SaveFile( bufferedStream, wxBITMAP_TYPE_PNG ) )
{
wxLogDebug( wxS( "Failed to encode PNG" ) );
return false;
}
bufferedStream.Close();
auto stBuf = memStream.GetOutputStreamBuffer();
aOutput.SetDataLen( 0 );
aOutput.AppendData( stBuf->GetBufferStart(), stBuf->GetIntPosition() );
return true;
}
bool AddPngToClipboardData( wxDataObjectComposite* aData, const wxMemoryBuffer& aPngData,
const wxImage* aFallbackImage )
{
wxCHECK( wxTheClipboard->IsOpened(), false );
wxCHECK( aPngData.GetDataLen() > 0, false );
#if defined( __WXMSW__ )
aData->Add( new wxCustomDataObject( wxDataFormat( wxDataFormatId::wxDF_BITMAP ) ) );
wxCustomDataObject* pngObj = new wxCustomDataObject( wxDataFormat( "PNG" ) );
pngObj->SetData( aPngData.GetDataLen(), aPngData.GetData() );
aData->Add( pngObj );
#elif defined( __WXGTK__ )
wxCustomDataObject* pngObj = new wxCustomDataObject( wxDF_BITMAP );
pngObj->SetData( aPngData.GetDataLen(), aPngData.GetData() );
aData->Add( pngObj );
#else // __WXOSX__
wxCHECK( aFallbackImage && aFallbackImage->IsOk(), false );
aData->Add( new wxBitmapDataObject( wxBitmap( *aFallbackImage ) ) );
#endif
return true;
}
bool AddTransparentImageToClipboardData( wxDataObjectComposite* aData, wxImage aImage )
{
wxCHECK( wxTheClipboard->IsOpened(), false );
wxCHECK( aImage.IsOk(), false );
#if defined( __WXGTK__ ) || defined( __WXMSW__ )
wxMemoryBuffer pngData;
if( !EncodeImageToPng( aImage, pngData ) )
return false;
return AddPngToClipboardData( aData, pngData );
#else // __WXOSX__
aData->Add( new wxBitmapDataObject( wxBitmap( aImage ) ) );
return true;
#endif
}