KiCad PCB EDA Suite
Loading...
Searching...
No Matches
clipboard.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "clipboard.h"
25
26#include <wx/clipbrd.h>
27#include <wx/dataobj.h>
28#include <wx/image.h>
29#include <wx/log.h>
30#include <wx/string.h>
31#include <wx/sstream.h>
32
33#include <sstream>
34
35#include <io/csv.h>
36
37
38bool SaveClipboard( const std::string& aTextUTF8 )
39{
40 wxLogNull doNotLog; // disable logging of failed clipboard actions
41
42 if( wxTheClipboard->Open() )
43 {
44 // Store the UTF8 string as Unicode string in clipboard:
45 wxTheClipboard->SetData(
46 new wxTextDataObject( wxString( aTextUTF8.c_str(), wxConvUTF8 ) ) );
47
48 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
49 wxTheClipboard->Close();
50
51 return true;
52 }
53
54 return false;
55}
56
57
58std::string GetClipboardUTF8()
59{
60 std::string result;
61
62 wxLogNull doNotLog; // disable logging of failed clipboard actions
63
64 if( wxTheClipboard->Open() )
65 {
66 if( wxTheClipboard->IsSupported( wxDF_TEXT )
67 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
68 {
69 wxTextDataObject data;
70 wxTheClipboard->GetData( data );
71
72 // The clipboard is expected containing a Unicode string, so return it
73 // as UTF8 string
74 result = data.GetText().utf8_str();
75 }
76
77 wxTheClipboard->Close();
78 }
79
80 return result;
81}
82
83
84std::unique_ptr<wxImage> GetImageFromClipboard()
85{
86 std::unique_ptr<wxImage> image;
87 wxLogNull doNotLog; // disable logging of failed clipboard actions
88
89 // First try for an image
90 if( wxTheClipboard->Open() )
91 {
92 if( wxTheClipboard->IsSupported( wxDF_BITMAP ) )
93 {
94 wxBitmapDataObject data;
95 if( wxTheClipboard->GetData( data ) )
96 {
97 image = std::make_unique<wxImage>( data.GetBitmap().ConvertToImage() );
98 }
99 }
100 else if( wxTheClipboard->IsSupported( wxDF_FILENAME ) )
101 {
102 wxFileDataObject data;
103 if( wxTheClipboard->GetData( data ) && data.GetFilenames().size() == 1 )
104 {
105 image = std::make_unique<wxImage>( data.GetFilenames()[0] );
106
107 if( !image->IsOk() )
108 image.reset();
109 }
110 }
111
112 wxTheClipboard->Close();
113 }
114
115 return image;
116}
117
118
119bool SaveTabularDataToClipboard( const std::vector<std::vector<wxString>>& aData )
120{
121 wxLogNull doNotLog; // disable logging of failed clipboard actions
122
123 if( wxTheClipboard->Open() )
124 {
125 wxDataObjectComposite* data = new wxDataObjectComposite();
126
127 // Set plain text CSV
128 {
129 wxStringOutputStream os;
130 CSV_WRITER writer( os );
131 writer.WriteLines( aData );
132
133 data->Add( new wxTextDataObject( os.GetString() ), true );
134 }
135
136 // At this point, it would be great if we could add some format that spreadsheet
137 // programs can understand without asking the user for options: perhaps SYLK or DIF.
138 // But it doesn't seem like WX allows to put arbitrary MIME types on the clipboard,
139 // even with wxCustomDataObject( wxDataFormat( "mime/type" ) ), which just ends up as
140 // wxDF_PRIVATE, and wxDF_SYLK/DIF aren't mapped on GTK.
141
142 wxTheClipboard->SetData( data );
143 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
144 wxTheClipboard->Close();
145
146 return true;
147 }
148
149 return false;
150}
151
152
153bool GetTabularDataFromClipboard( std::vector<std::vector<wxString>>& aData )
154{
155 // Again, it would be ideal if we could detect a spreadsheet mimetype here,
156 // but WX doesn't seem to do that on Linux, at least.
157
158 bool ok = false;
159
160 // First try for text data
161 if( wxTheClipboard->Open() )
162 {
163 if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
164 {
165 wxTextDataObject data;
166 if( wxTheClipboard->GetData( data ) )
167 {
168 ok = AutoDecodeCSV( data.GetText(), aData );
169 }
170 }
171
172 // We could also handle .csv wxDF_FILENAMEs here
173
174 wxTheClipboard->Close();
175 }
176
177 return ok;
178}
void WriteLines(const std::vector< std::vector< wxString > > &aRows)
Write a vector of rows to the stream.
Definition csv.cpp:18
bool SaveTabularDataToClipboard(const std::vector< std::vector< wxString > > &aData)
Store tabular data to the system clipboard.
bool SaveClipboard(const std::string &aTextUTF8)
Store information to the system clipboard.
Definition clipboard.cpp:38
std::string GetClipboardUTF8()
Return the information currently stored in the system clipboard.
Definition clipboard.cpp:58
std::unique_ptr< wxImage > GetImageFromClipboard()
Get image data from the clipboard, if there is any.
Definition clipboard.cpp:84
bool GetTabularDataFromClipboard(std::vector< std::vector< wxString > > &aData)
Attempt to get tabular data from the clipboard.
bool AutoDecodeCSV(const wxString &aInput, std::vector< std::vector< wxString > > &aData)
Try to guess the format of a T/CSV file and decode it into aData.
Definition csv.cpp:66
wxString result
Test unit parsing edge cases and error handling.