When accessing text that's in the clipboard, on macOS any Carriage Return character (ASCII 0x0D) is converted to a Line Feed character (ASCII 0x0A):
wxTextDataObject pasted;
wxTheClipboard->GetData(pasted);
wxString text = pasted.GetText(); // in 'text', 0x0D has been replaced by 0x0A
I know that FLTK converts \r\n to a single \n, which would be much better. And yes, I'm aware that \r\n isn't used on macOS, but for my application it's essential that pasting \r\n doesn't create two line feed characters.
Here's a working example. On launch, it writes the string "ab\r\ncd" to the clipboard and shows this string (as ASCII codes) in the static text display. When you click "Paste", it fetches the current clipboard content and shows it in the static text instead. You see that the "13" (carriage return) is converted to "10" (line feed).
// g++ --std=c++17 -Wall -pedantic `wx-config --cxxflags` -o clipboard clipboard.cpp `wx-config --libs`
#include "wx/wx.h"
#include <wx/clipbrd.h>
class MyApp : public wxApp {
public:
virtual bool OnInit() wxOVERRIDE;
};
class MyFrame : public wxFrame {
public:
MyFrame(const wxString& title);
void OnPaste(wxCommandEvent &event);
private:
wxStaticText *m_output_control;
void WriteText(wxString text);
wxDECLARE_EVENT_TABLE();
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit() {
if ( !wxApp::OnInit() )
return false;
MyFrame *frame = new MyFrame("Paste Test");
frame->Show(true);
return true;
}
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON(wxID_ANY, MyFrame::OnPaste)
wxEND_EVENT_TABLE()
void MyFrame::OnPaste(wxCommandEvent &event) {
if( wxTheClipboard->Open() ) {
wxTextDataObject pasted;
wxTheClipboard->GetData( pasted );
wxTheClipboard->Close();
WriteText( pasted.GetText() );
}
}
MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title) {
m_output_control = new wxStaticText(this, wxID_ANY, "", wxPoint(20, 50), wxSize(320,20));
new wxButton(this, wxID_ANY, "Paste", wxPoint(20, 100));
if( wxTheClipboard->Open() ) {
wxString test_data = "ab\r\ncd";
WriteText(test_data);
wxTheClipboard->SetData( new wxTextDataObject(test_data) );
wxTheClipboard->Close();
}
}
void MyFrame::WriteText(wxString text) {
wxString output = "";
for( const int c : text )
output << c << " ";
m_output_control->SetLabel( output );
}
Used versions:
- macOS 12.6
- wxWidgets 3.2.0 built from source:
../configure --enable-debug --disable-shared --disable-sys-libs --enable-universal_binary=arm64,x86_64make