2015-02-17 23:35:18 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014-2015 CERN
|
|
|
|
* Author: Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <wx/stattext.h>
|
2017-08-25 14:46:49 +00:00
|
|
|
#include <wx/textentry.h>
|
2015-02-17 23:35:18 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <base_units.h>
|
2018-02-03 09:09:53 +00:00
|
|
|
#include <draw_frame.h>
|
|
|
|
#include <confirm.h>
|
2015-02-17 23:35:18 +00:00
|
|
|
|
2017-11-10 23:27:46 +00:00
|
|
|
#include "widgets/unit_binder.h"
|
2015-02-17 23:35:18 +00:00
|
|
|
|
2018-02-03 09:09:53 +00:00
|
|
|
UNIT_BINDER::UNIT_BINDER( EDA_DRAW_FRAME* aParent,
|
2018-03-07 12:48:08 +00:00
|
|
|
wxStaticText* aLabel, wxWindow* aValue, wxStaticText* aUnitLabel,
|
2018-02-03 09:09:53 +00:00
|
|
|
bool aUseMils, int aMin, int aMax, bool allowEval ) :
|
|
|
|
m_label( aLabel ),
|
2018-03-07 12:48:08 +00:00
|
|
|
m_value( aValue ),
|
2015-03-11 16:04:20 +00:00
|
|
|
m_unitLabel( aUnitLabel ),
|
2018-03-07 12:48:08 +00:00
|
|
|
m_showMessage( true ),
|
2018-02-03 09:09:53 +00:00
|
|
|
m_eval( aParent->GetUserUnits(), aUseMils )
|
2015-02-17 23:35:18 +00:00
|
|
|
{
|
2018-02-03 09:09:53 +00:00
|
|
|
// Fix the units (to the current units) for the life of the binder
|
|
|
|
m_units = aParent->GetUserUnits();
|
|
|
|
m_useMils = aUseMils;
|
|
|
|
m_min = aMin;
|
|
|
|
m_max = aMax;
|
2018-03-07 12:48:08 +00:00
|
|
|
m_allowEval = allowEval && dynamic_cast<wxTextEntry*>( m_value );
|
2018-02-03 09:09:53 +00:00
|
|
|
|
2018-03-07 12:48:08 +00:00
|
|
|
auto textEntry = dynamic_cast<wxTextEntry*>( m_value );
|
|
|
|
if( textEntry )
|
2018-07-21 08:41:38 +00:00
|
|
|
{
|
|
|
|
// Use ChangeValue() instead of SetValue() so we don't generate events.
|
|
|
|
textEntry->ChangeValue( wxT( "0" ) );
|
|
|
|
}
|
2015-02-17 23:35:18 +00:00
|
|
|
|
2018-05-23 06:11:47 +00:00
|
|
|
m_unitLabel->SetLabel( GetAbbreviatedUnitsLabel( m_units, m_useMils ) );
|
2015-02-17 23:35:18 +00:00
|
|
|
|
2018-03-07 12:48:08 +00:00
|
|
|
m_value->Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( UNIT_BINDER::onSetFocus ), NULL, this );
|
|
|
|
m_value->Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( UNIT_BINDER::onKillFocus ), NULL, this );
|
2018-07-25 18:26:55 +00:00
|
|
|
|
|
|
|
// Connext wxEVT_TEXT_ENTER when (and *only when*) m_value has the wxTE_PROCESS_ENTER style
|
|
|
|
// because:
|
|
|
|
// 1 - it is useless
|
|
|
|
// 2 - it generate wxWidgets assert
|
|
|
|
if( m_value->HasFlag( wxTE_PROCESS_ENTER ) )
|
|
|
|
m_value->Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( UNIT_BINDER::onTextEnter ), NULL, this );
|
2015-02-17 23:35:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2018-05-23 06:11:47 +00:00
|
|
|
void UNIT_BINDER::SetUnits( EDA_UNITS_T aUnits, bool aUseMils )
|
|
|
|
{
|
|
|
|
m_units = aUnits;
|
|
|
|
m_useMils = aUseMils;
|
|
|
|
m_unitLabel->SetLabel( GetAbbreviatedUnitsLabel( m_units, m_useMils ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-03 09:09:53 +00:00
|
|
|
void UNIT_BINDER::onSetFocus( wxFocusEvent& aEvent )
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
auto textEntry = dynamic_cast<wxTextEntry*>( m_value );
|
|
|
|
|
|
|
|
if( m_allowEval && textEntry )
|
2018-02-03 09:09:53 +00:00
|
|
|
{
|
2018-02-25 16:14:24 +00:00
|
|
|
wxString oldStr = m_eval.OriginalText();
|
2018-02-03 09:09:53 +00:00
|
|
|
|
2018-02-25 16:14:24 +00:00
|
|
|
if( oldStr.length() )
|
2018-03-07 12:48:08 +00:00
|
|
|
textEntry->SetValue( oldStr );
|
2018-02-03 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UNIT_BINDER::onKillFocus( wxFocusEvent& aEvent )
|
|
|
|
{
|
|
|
|
// The ship is going down; no need to do anything...
|
|
|
|
if( !aEvent.GetWindow() || aEvent.GetWindow()->GetId() == wxID_CANCEL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( m_allowEval )
|
|
|
|
evaluate();
|
|
|
|
|
|
|
|
Validate( true );
|
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UNIT_BINDER::onTextEnter( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( m_allowEval )
|
|
|
|
evaluate();
|
|
|
|
|
|
|
|
// Send an OK event to the parent dialog
|
|
|
|
wxCommandEvent event( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK );
|
2018-03-07 12:48:08 +00:00
|
|
|
wxPostEvent( m_value->GetParent(), event );
|
2018-02-03 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UNIT_BINDER::evaluate()
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
auto textEntry = dynamic_cast<wxTextEntry*>( m_value );
|
|
|
|
|
|
|
|
if( !textEntry )
|
|
|
|
return;
|
2018-02-03 09:09:53 +00:00
|
|
|
|
2018-03-07 12:48:08 +00:00
|
|
|
if( textEntry->GetValue().IsEmpty() )
|
|
|
|
textEntry->ChangeValue( "0" );
|
|
|
|
|
|
|
|
if( m_eval.Process( textEntry->GetValue() ) )
|
|
|
|
textEntry->ChangeValue( m_eval.Result() );
|
2018-02-03 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString valueDescriptionFromLabel( wxStaticText* aLabel )
|
|
|
|
{
|
|
|
|
wxString desc = aLabel->GetLabel();
|
|
|
|
|
|
|
|
desc.EndsWith( wxT( ":" ), &desc );
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UNIT_BINDER::delayedFocusHandler( wxIdleEvent& )
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
m_value->SetFocus();
|
|
|
|
m_showMessage = true;
|
2018-02-03 09:09:53 +00:00
|
|
|
|
2018-03-07 12:48:08 +00:00
|
|
|
m_value->Unbind( wxEVT_IDLE, &UNIT_BINDER::delayedFocusHandler, this );
|
2018-02-03 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool UNIT_BINDER::Validate( bool setFocusOnError )
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
auto textEntry = dynamic_cast<wxTextEntry*>( m_value );
|
|
|
|
|
|
|
|
if( !textEntry || textEntry->GetValue() == INDETERMINATE )
|
|
|
|
return true;
|
2018-02-03 09:09:53 +00:00
|
|
|
|
|
|
|
if( m_min > INT_MIN && GetValue() < m_min )
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
if( m_showMessage )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format( _( "%s must be larger than %s." ),
|
|
|
|
valueDescriptionFromLabel( m_label ),
|
|
|
|
StringFromValue( m_units, m_min, true ) );
|
|
|
|
DisplayError( m_value->GetParent(), msg );
|
|
|
|
}
|
2018-02-03 09:09:53 +00:00
|
|
|
|
|
|
|
if( setFocusOnError )
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
textEntry->SelectAll();
|
2018-02-03 09:09:53 +00:00
|
|
|
// Don't focus directly; we might be inside a KillFocus event handler
|
2018-03-07 12:48:08 +00:00
|
|
|
m_value->Bind( wxEVT_IDLE, &UNIT_BINDER::delayedFocusHandler, this );
|
|
|
|
m_showMessage = false;
|
2018-02-03 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_max < INT_MAX && GetValue() > m_max )
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
if( m_showMessage )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format( _( "%s must be smaller than %s." ),
|
|
|
|
valueDescriptionFromLabel( m_label ),
|
|
|
|
StringFromValue( m_units, m_max, true ) );
|
|
|
|
DisplayError( m_value->GetParent(), msg );
|
|
|
|
}
|
2018-02-03 09:09:53 +00:00
|
|
|
|
|
|
|
if( setFocusOnError )
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
textEntry->SelectAll();
|
2018-02-03 09:09:53 +00:00
|
|
|
// Don't focus directly; we might be inside a KillFocus event handler
|
2018-03-07 12:48:08 +00:00
|
|
|
m_value->Bind( wxEVT_IDLE, &UNIT_BINDER::delayedFocusHandler, this );
|
|
|
|
m_showMessage = false;
|
2018-02-03 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-10 23:27:46 +00:00
|
|
|
void UNIT_BINDER::SetValue( int aValue )
|
2015-02-17 23:35:18 +00:00
|
|
|
{
|
2018-02-03 09:09:53 +00:00
|
|
|
SetValue( StringFromValue( m_units, aValue, false, m_useMils ) );
|
|
|
|
}
|
2015-02-17 23:35:18 +00:00
|
|
|
|
2015-07-09 11:35:50 +00:00
|
|
|
|
2018-02-03 09:09:53 +00:00
|
|
|
void UNIT_BINDER::SetValue( wxString aValue )
|
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
if( dynamic_cast<wxTextEntry*>( m_value ) )
|
|
|
|
dynamic_cast<wxTextEntry*>( m_value )->SetValue( aValue );
|
|
|
|
else if( dynamic_cast<wxStaticText*>( m_value ) )
|
|
|
|
dynamic_cast<wxStaticText*>( m_value )->SetLabel( aValue );
|
2018-02-03 09:09:53 +00:00
|
|
|
|
|
|
|
if( m_allowEval )
|
2018-02-25 16:14:24 +00:00
|
|
|
m_eval.Clear();
|
2018-02-03 09:09:53 +00:00
|
|
|
|
|
|
|
m_unitLabel->SetLabel( GetAbbreviatedUnitsLabel( m_units, m_useMils ) );
|
2015-02-17 23:35:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2017-11-10 23:27:46 +00:00
|
|
|
int UNIT_BINDER::GetValue() const
|
2015-02-17 23:35:18 +00:00
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
wxString s;
|
|
|
|
|
|
|
|
if( dynamic_cast<wxTextEntry*>( m_value ) )
|
|
|
|
s = dynamic_cast<wxTextEntry*>( m_value )->GetValue();
|
|
|
|
else if( dynamic_cast<wxStaticText*>( m_value ) )
|
|
|
|
s = dynamic_cast<wxStaticText*>( m_value )->GetLabel();
|
2015-02-17 23:35:18 +00:00
|
|
|
|
2018-02-03 09:09:53 +00:00
|
|
|
return ValueFromString( m_units, s, m_useMils );
|
2015-02-17 23:35:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2018-02-03 09:09:53 +00:00
|
|
|
bool UNIT_BINDER::IsIndeterminate() const
|
2015-07-09 11:35:50 +00:00
|
|
|
{
|
2018-03-07 12:48:08 +00:00
|
|
|
if( dynamic_cast<wxTextEntry*>( m_value ) )
|
|
|
|
return dynamic_cast<wxTextEntry*>( m_value )->GetValue() == INDETERMINATE;
|
|
|
|
|
|
|
|
return false;
|
2015-07-09 11:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-12 07:36:35 +00:00
|
|
|
void UNIT_BINDER::SetLabel( const wxString& aLabel )
|
|
|
|
{
|
|
|
|
m_label->SetLabel( aLabel );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-10 23:27:46 +00:00
|
|
|
void UNIT_BINDER::Enable( bool aEnable )
|
2015-02-17 23:35:18 +00:00
|
|
|
{
|
2018-02-03 09:09:53 +00:00
|
|
|
m_label->Enable( aEnable );
|
2018-03-07 12:48:08 +00:00
|
|
|
m_value->Enable( aEnable );
|
2015-02-18 16:53:46 +00:00
|
|
|
m_unitLabel->Enable( aEnable );
|
2015-03-11 16:04:20 +00:00
|
|
|
}
|
2017-08-25 14:46:49 +00:00
|
|
|
|
2018-05-23 06:11:47 +00:00
|
|
|
|
|
|
|
void UNIT_BINDER::Show( bool aShow )
|
|
|
|
{
|
|
|
|
m_label->Show( aShow );
|
|
|
|
m_value->Show( aShow );
|
|
|
|
m_unitLabel->Show( aShow );
|
|
|
|
}
|
|
|
|
|