From 64636601b816ed0f8c1deaf264c78846fb35b57d Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sat, 21 Nov 2020 10:59:25 +0100 Subject: [PATCH] Eeschema: fix a Unicode/UTF8 management when pasting items from Clipboard. Copy to clipboard save items (using S expr description) using wide strings (not UTF8 encoding). But Paste from clipboard was not converting the wide string to UTF8 string when getting the stored data to send it as std::string to the S expr reader. Fixes #6449 https://gitlab.com/kicad/code/kicad/issues/6449 --- common/tool/tool_manager.cpp | 12 ++++++++---- eeschema/tools/lib_edit_tool.cpp | 6 +++--- eeschema/tools/sch_editor_control.cpp | 2 +- include/tool/tool_manager.h | 9 ++++++--- pagelayout_editor/tools/pl_edit_tool.cpp | 2 +- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index dff210da67..128d0588b7 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -971,11 +971,13 @@ void TOOL_MANAGER::ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, } -bool TOOL_MANAGER::SaveClipboard( const std::string& aText ) +bool TOOL_MANAGER::SaveClipboard( const std::string& aTextUTF8 ) { if( wxTheClipboard->Open() ) { - wxTheClipboard->SetData( new wxTextDataObject( wxString( aText.c_str(), wxConvUTF8 ) ) ); + // Store the UTF8 string as unicode string in clipboard: + wxTheClipboard->SetData( new wxTextDataObject( wxString( aTextUTF8.c_str(), + wxConvUTF8 ) ) ); wxTheClipboard->Close(); return true; @@ -985,7 +987,7 @@ bool TOOL_MANAGER::SaveClipboard( const std::string& aText ) } -std::string TOOL_MANAGER::GetClipboard() const +std::string TOOL_MANAGER::GetClipboardUTF8() const { std::string result; @@ -997,7 +999,9 @@ std::string TOOL_MANAGER::GetClipboard() const wxTextDataObject data; wxTheClipboard->GetData( data ); - result = data.GetText().mb_str(); + // The clipboard is expected containing a unicode string, so return it + // as UTF8 string + result = data.GetText().utf8_str(); } wxTheClipboard->Close(); diff --git a/eeschema/tools/lib_edit_tool.cpp b/eeschema/tools/lib_edit_tool.cpp index e3922ab416..43115772f0 100644 --- a/eeschema/tools/lib_edit_tool.cpp +++ b/eeschema/tools/lib_edit_tool.cpp @@ -668,8 +668,8 @@ int LIB_EDIT_TOOL::Paste( const TOOL_EVENT& aEvent ) if( !part || part->IsAlias() ) return 0; - std::string text = m_toolMgr->GetClipboard(); - STRING_LINE_READER reader( text, "Clipboard" ); + std::string text_utf8 = m_toolMgr->GetClipboardUTF8(); + STRING_LINE_READER reader( text_utf8, "Clipboard" ); LIB_PART* newPart; try @@ -681,7 +681,7 @@ int LIB_EDIT_TOOL::Paste( const TOOL_EVENT& aEvent ) // If it's not a part then paste as text newPart = new LIB_PART( "dummy_part" ); LIB_TEXT* newText = new LIB_TEXT( newPart ); - newText->SetText( text ); + newText->SetText( wxString::FromUTF8( text_utf8 ) ); newPart->AddDrawItem( newText ); } diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index 5363608a0d..decabd88e8 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -1353,7 +1353,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) } EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool(); - std::string text = m_toolMgr->GetClipboard(); + std::string text = m_toolMgr->GetClipboardUTF8(); if( text.empty() ) return 0; diff --git a/include/tool/tool_manager.h b/include/tool/tool_manager.h index a6d552980e..b321ed642e 100644 --- a/include/tool/tool_manager.h +++ b/include/tool/tool_manager.h @@ -381,16 +381,19 @@ public: /** * Stores an information to the system clipboard. - * @param aText is the information to be stored. + * @param aText is the information to be stored, expected UTF8 encoding. + * the text will be stored as Unicode string (not stored as UTF8 string) * @return False if error occurred. */ - bool SaveClipboard( const std::string& aText ); + bool SaveClipboard( const std::string& aTextUTF8 ); /** * Returns the information currently stored in the system clipboard. If data stored in the * clipboard is in non-text format, empty string is returned. + * Note also the clipboard is expected containing unicode chars, not only ASCII7 chars. + * The returned string is UTF8 encoded */ - std::string GetClipboard() const; + std::string GetClipboardUTF8() const; /** * Returns the view controls settings for the current tool or the general settings if there is diff --git a/pagelayout_editor/tools/pl_edit_tool.cpp b/pagelayout_editor/tools/pl_edit_tool.cpp index ae44db0886..53a8420088 100644 --- a/pagelayout_editor/tools/pl_edit_tool.cpp +++ b/pagelayout_editor/tools/pl_edit_tool.cpp @@ -477,7 +477,7 @@ int PL_EDIT_TOOL::Paste( const TOOL_EVENT& aEvent ) { PL_SELECTION& selection = m_selectionTool->GetSelection(); WS_DATA_MODEL& model = WS_DATA_MODEL::GetTheInstance(); - std::string sexpr = m_toolMgr->GetClipboard(); + std::string sexpr = m_toolMgr->GetClipboardUTF8(); m_selectionTool->ClearSelection();