From 2b387f4b4d472fa90f9ea548a56334eaec035f42 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Sun, 8 Sep 2019 01:00:15 +0200 Subject: [PATCH] Manually put the UNIT_BINDER text on the primary selection clipboard The OnKillFocus handler of the UNIT_BINDER replaces the text in the control with the evaluated string, which removes the selection. To get the original text on the primary selection clipboard, we must add it ourselves. Fixes: lp:1794623 * https://bugs.launchpad.net/kicad/+bug/1794623 --- common/widgets/unit_binder.cpp | 23 +++++++++++++++++++++++ include/widgets/unit_binder.h | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/common/widgets/unit_binder.cpp b/common/widgets/unit_binder.cpp index 0516eec249..d6a8b0e72d 100644 --- a/common/widgets/unit_binder.cpp +++ b/common/widgets/unit_binder.cpp @@ -22,6 +22,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ +#include #include #include #include @@ -46,6 +47,8 @@ UNIT_BINDER::UNIT_BINDER( EDA_DRAW_FRAME* aParent, m_useMils = aUseMils; m_allowEval = allowEval && dynamic_cast( m_value ); m_needsEval = false; + m_selStart = 0; + m_selEnd = 0; auto textEntry = dynamic_cast( m_value ); if( textEntry ) @@ -79,7 +82,10 @@ void UNIT_BINDER::onSetFocus( wxFocusEvent& aEvent ) wxString oldStr = m_eval.OriginalText(); if( oldStr.length() ) + { textEntry->SetValue( oldStr ); + textEntry->SetSelection( m_selStart, m_selEnd ); + } m_needsEval = true; } @@ -95,8 +101,25 @@ void UNIT_BINDER::onKillFocus( wxFocusEvent& aEvent ) if( m_allowEval && textEntry ) { if( m_eval.Process( textEntry->GetValue() ) ) + { + textEntry->GetSelection( &m_selStart, &m_selEnd ); + wxString sel = textEntry->GetStringSelection(); + textEntry->ChangeValue( m_eval.Result() ); +#ifdef __WXGTK__ + // Manually copy the selected text to the primary selection clipboard + if( wxTheClipboard->Open() ) + { + bool clipTarget = wxTheClipboard->IsUsingPrimarySelection(); + wxTheClipboard->UsePrimarySelection( true ); + wxTheClipboard->SetData( new wxTextDataObject( sel ) ); + wxTheClipboard->UsePrimarySelection( clipTarget ); + wxTheClipboard->Close(); + } +#endif + } + m_needsEval = false; } diff --git a/include/widgets/unit_binder.h b/include/widgets/unit_binder.h index 8ea36b75b4..3eea2eaa16 100644 --- a/include/widgets/unit_binder.h +++ b/include/widgets/unit_binder.h @@ -143,6 +143,10 @@ protected: NUMERIC_EVALUATOR m_eval; bool m_allowEval; bool m_needsEval; + + ///> Selection start and end of the original text + long m_selStart; + long m_selEnd; }; #endif /* __UNIT_BINDER_H_ */