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/image.h>
28#include <wx/log.h>
29#include <wx/string.h>
30#include <wx/sstream.h>
31
32#include <sstream>
33
34#include <io/csv.h>
35
36
37bool SaveClipboard( const std::string& aTextUTF8 )
38{
39 wxLogNull doNotLog; // disable logging of failed clipboard actions
40
41 if( wxTheClipboard->Open() )
42 {
43 // Store the UTF8 string as Unicode string in clipboard:
44 wxTheClipboard->SetData(
45 new wxTextDataObject( wxString( aTextUTF8.c_str(), wxConvUTF8 ) ) );
46
47 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
48 wxTheClipboard->Close();
49
50 return true;
51 }
52
53 return false;
54}
55
56
57std::string GetClipboardUTF8()
58{
59 std::string result;
60
61 wxLogNull doNotLog; // disable logging of failed clipboard actions
62
63 if( wxTheClipboard->Open() )
64 {
65 if( wxTheClipboard->IsSupported( wxDF_TEXT )
66 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
67 {
68 wxTextDataObject data;
69 wxTheClipboard->GetData( data );
70
71 // The clipboard is expected containing a Unicode string, so return it
72 // as UTF8 string
73 result = data.GetText().utf8_str();
74 }
75
76 wxTheClipboard->Close();
77 }
78
79 return result;
80}
81
82
83std::unique_ptr<wxImage> GetImageFromClipboard()
84{
85 std::unique_ptr<wxImage> image;
86 wxLogNull doNotLog; // disable logging of failed clipboard actions
87
88 // First try for an image
89 if( wxTheClipboard->Open() )
90 {
91 if( wxTheClipboard->IsSupported( wxDF_BITMAP ) )
92 {
93 wxImageDataObject data;
94 if( wxTheClipboard->GetData( data ) )
95 {
96 image = std::make_unique<wxImage>( data.GetImage() );
97 }
98 }
99 else if( wxTheClipboard->IsSupported( wxDF_FILENAME ) )
100 {
101 wxFileDataObject data;
102 if( wxTheClipboard->GetData( data ) && data.GetFilenames().size() == 1 )
103 {
104 image = std::make_unique<wxImage>( data.GetFilenames()[0] );
105
106 if( !image->IsOk() )
107 image.reset();
108 }
109 }
110
111 wxTheClipboard->Close();
112 }
113
114 return image;
115}
116
117
118bool SaveTabularDataToClipboard( const std::vector<std::vector<wxString>>& aData )
119{
120 wxLogNull doNotLog; // disable logging of failed clipboard actions
121
122 if( wxTheClipboard->Open() )
123 {
124 wxDataObjectComposite* data = new wxDataObjectComposite();
125
126 // Set plain text CSV
127 {
128 wxStringOutputStream os;
129 CSV_WRITER writer( os );
130 writer.WriteLines( aData );
131
132 data->Add( new wxTextDataObject( os.GetString() ), true );
133 }
134
135 // At this point, it would be great if we could add some format that spreadsheet
136 // programs can understand without asking the user for options: perhaps SYLK or DIF.
137 // But it doesn't seem like WX allows to put arbitrary MIME types on the clipboard,
138 // even with wxCustomDataObject( wxDataFormat( "mime/type" ) ), which just ends up as
139 // wxDF_PRIVATE, and wxDF_SYLK/DIF aren't mapped on GTK.
140
141 wxTheClipboard->SetData( data );
142 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
143 wxTheClipboard->Close();
144
145 return true;
146 }
147
148 return false;
149}
150
151
152bool GetTabularDataFromClipboard( std::vector<std::vector<wxString>>& aData )
153{
154 // Again, it would be ideal if we could detect a spreadsheet mimetype here,
155 // but WX doesn't seem to do that on Linux, at least.
156
157 bool ok = false;
158
159 // First try for text data
160 if( wxTheClipboard->Open() )
161 {
162 if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
163 {
164 wxTextDataObject data;
165 if( wxTheClipboard->GetData( data ) )
166 {
167 ok = AutoDecodeCSV( data.GetText(), aData );
168 }
169 }
170
171 // We could also handle .csv wxDF_FILENAMEs here
172
173 wxTheClipboard->Close();
174 }
175
176 return ok;
177}
Definition: csv.h:33
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.
Definition: clipboard.cpp:118
bool SaveClipboard(const std::string &aTextUTF8)
Store information to the system clipboard.
Definition: clipboard.cpp:37
std::string GetClipboardUTF8()
Return the information currently stored in the system clipboard.
Definition: clipboard.cpp:57
std::unique_ptr< wxImage > GetImageFromClipboard()
Get image data from the clipboard, if there is any.
Definition: clipboard.cpp:83
bool GetTabularDataFromClipboard(std::vector< std::vector< wxString > > &aData)
Attempt to get tabular data from the clipboard.
Definition: clipboard.cpp:152
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