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
This commit is contained in:
Ian McInerney 2019-09-08 01:00:15 +02:00 committed by Seth Hillbrand
parent c3bd9b7b8f
commit 2b387f4b4d
2 changed files with 27 additions and 0 deletions

View File

@ -22,6 +22,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <wx/clipbrd.h>
#include <wx/stattext.h>
#include <wx/textentry.h>
#include <limits>
@ -46,6 +47,8 @@ UNIT_BINDER::UNIT_BINDER( EDA_DRAW_FRAME* aParent,
m_useMils = aUseMils;
m_allowEval = allowEval && dynamic_cast<wxTextEntry*>( m_value );
m_needsEval = false;
m_selStart = 0;
m_selEnd = 0;
auto textEntry = dynamic_cast<wxTextEntry*>( 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;
}

View File

@ -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_ */