You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <properties/pg_editors.h>
  20. #include <properties/pg_properties.h>
  21. #include <widgets/unit_binder.h>
  22. #include <wx/log.h>
  23. PG_UNIT_EDITOR::PG_UNIT_EDITOR( EDA_DRAW_FRAME* aFrame ) :
  24. wxPGTextCtrlEditor(),
  25. m_frame( aFrame )
  26. {
  27. m_unitBinder = std::make_unique<PROPERTY_EDITOR_UNIT_BINDER>( m_frame );
  28. }
  29. PG_UNIT_EDITOR::~PG_UNIT_EDITOR()
  30. {
  31. }
  32. wxPGWindowList PG_UNIT_EDITOR::CreateControls( wxPropertyGrid* aPropGrid, wxPGProperty* aProperty,
  33. const wxPoint& aPos, const wxSize& aSize ) const
  34. {
  35. wxPGWindowList ret = wxPGTextCtrlEditor::CreateControls( aPropGrid, aProperty, aPos, aSize );
  36. m_unitBinder->SetControl( ret.m_primary );
  37. m_unitBinder->RequireEval();
  38. if( PGPROPERTY_DISTANCE* prop = dynamic_cast<PGPROPERTY_DISTANCE*>( aProperty ) )
  39. m_unitBinder->SetCoordType( prop->CoordType() );
  40. return ret;
  41. }
  42. bool PG_UNIT_EDITOR::GetValueFromControl( wxVariant& aVariant, wxPGProperty* aProperty,
  43. wxWindow* aCtrl ) const
  44. {
  45. wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aCtrl );
  46. wxCHECK_MSG( textCtrl, false, "PG_UNIT_EDITOR requires a text control!" );
  47. wxString textVal = textCtrl->GetValue();
  48. if( aProperty->UsesAutoUnspecified() && textVal.empty() )
  49. {
  50. aVariant.MakeNull();
  51. return true;
  52. }
  53. long result = m_unitBinder->GetValue();
  54. bool changed = ( aVariant.IsNull() || result != aVariant.GetLong() );
  55. if( changed )
  56. {
  57. aVariant = result;
  58. m_unitBinder->SetValue( result );
  59. }
  60. // Changing unspecified always causes event (returning
  61. // true here should be enough to trigger it).
  62. if( !changed && aVariant.IsNull() )
  63. changed = true;
  64. return changed;
  65. }