39#include <wx/display.h>
40#include <wx/evtloop.h>
45#include <wx/propgrid/propgrid.h>
46#include <wx/checklst.h>
47#include <wx/dataview.h>
48#include <wx/bmpbuttn.h>
49#include <wx/textctrl.h>
50#include <wx/stc/stc.h>
51#include <wx/combobox.h>
52#include <wx/odcombo.h>
54#include <wx/checkbox.h>
55#include <wx/spinctrl.h>
56#include <wx/splitter.h>
57#include <wx/radiobox.h>
58#include <wx/radiobut.h>
59#include <wx/datectrl.h>
61#include <wx/timectrl.h>
63#include <wx/variant.h>
64#include <wx/weakref.h>
68#include <nlohmann/json.hpp>
85 std::string title = aTitle.ToStdString();
86 size_t parenPos = title.rfind(
'(' );
88 if( parenPos != std::string::npos && parenPos > 0 )
90 size_t end = parenPos;
92 while(
end > 0 && title[
end - 1] ==
' ' )
95 return title.substr( 0,
end );
113#if wxUSE_DATEPICKCTRL
114 if(
dynamic_cast<const wxDatePickerCtrl*
>( aWin ) !=
nullptr )
118#if wxUSE_TIMEPICKCTRL
119 if(
dynamic_cast<const wxTimePickerCtrl*
>( aWin ) !=
nullptr )
128 const wxSize& size,
long style,
const wxString&
name ) :
129 wxDialog( aParent, id, title, pos, size, style,
name ),
152 while( !kiwayHolder && aParent->GetParent() )
154 aParent = aParent->GetParent();
168 m_parentFrame = static_cast<EDA_BASE_FRAME*>( kiwayHolder );
169 TOOL_MANAGER* toolMgr = m_parentFrame->GetToolManager();
171 if( toolMgr && toolMgr->IsContextMenuActive() )
172 toolMgr->VetoContextMenuMouseWarp();
180 Kiway().SetBlockingDialog(
this );
198 wxString msg = wxString::Format(
"Opening dialog %s", GetTitle() );
214 std::function<void( wxWindowList& )> clearOptOuts =
215 [&]( wxWindowList& children )
217 for( wxWindow* child : children )
220 child->SetClientData(
nullptr );
221 clearOptOuts( child->GetChildren() );
226 SetClientData(
nullptr );
227 clearOptOuts( GetChildren() );
229 std::function<void( wxWindowList& )> disconnectFocusHandlers =
230 [&]( wxWindowList& children )
232 for( wxWindow* child : children )
237 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( child ) )
242 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( child ) )
249 disconnectFocusHandlers( child->GetChildren() );
254 disconnectFocusHandlers( GetChildren() );
256 std::function<void( wxWindowList& )> disconnectUndoRedoHandlers =
257 [&]( wxWindowList& children )
259 for( wxWindow* child : children )
264 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( child ) )
268 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( child ) )
272 else if( wxComboBox* combo =
dynamic_cast<wxComboBox*
>( child ) )
277 else if( wxChoice* choice =
dynamic_cast<wxChoice*
>( child ) )
281 else if( wxCheckBox* check =
dynamic_cast<wxCheckBox*
>( child ) )
285 else if( wxSpinCtrl* spin =
dynamic_cast<wxSpinCtrl*
>( child ) )
290 else if( wxSpinCtrlDouble* spinD =
dynamic_cast<wxSpinCtrlDouble*
>( child ) )
295 else if( wxRadioButton* radio =
dynamic_cast<wxRadioButton*
>( child ) )
299 else if( wxRadioBox* radioBox =
dynamic_cast<wxRadioBox*
>( child ) )
303 else if( wxGrid*
grid =
dynamic_cast<wxGrid*
>( child ) )
307 else if( wxPropertyGrid* propGrid =
dynamic_cast<wxPropertyGrid*
>( child ) )
311 else if( wxCheckListBox* checkList =
dynamic_cast<wxCheckListBox*
>( child ) )
315 else if( wxDataViewListCtrl* dataList =
dynamic_cast<wxDataViewListCtrl*
>( child ) )
321 disconnectUndoRedoHandlers( child->GetChildren() );
326 disconnectUndoRedoHandlers( GetChildren() );
331 binder->DetachFromDialogShim();
350 if( wxSizer* sz = GetSizer() )
367 GetSizer()->SetSizeHints(
this );
376 rect.width = std::min(
rect.width, aClientArea.width );
377 rect.height = std::min(
rect.height, aClientArea.height );
379 rect.x = std::clamp(
rect.x, aClientArea.x, aClientArea.GetRight() -
rect.width + 1 );
380 rect.y = std::clamp(
rect.y, aClientArea.y, aClientArea.GetBottom() -
rect.height + 1 );
390 int displayIdx = wxDisplay::GetFromWindow(
this );
392 if( displayIdx == wxNOT_FOUND && m_parent )
393 displayIdx = wxDisplay::GetFromWindow( m_parent );
395 if( displayIdx == wxNOT_FOUND )
398 wxRect clientArea = wxDisplay( (
unsigned int) displayIdx ).GetClientArea();
400 if( clientArea.width <= 0 || clientArea.height <= 0 )
406 wxSize clampedMin( std::min(
minSize.x, clientArea.width ),
407 std::min(
minSize.y, clientArea.height ) );
410 SetMinSize( clampedMin );
414 wxRect current( GetPosition(), GetSize() );
417 if( clamped != current )
418 SetSize( clamped.x, clamped.y, clamped.width, clamped.height, 0 );
425 SetSize( ConvertDialogToPixels( sz ) );
432 return ConvertDialogToPixels( sz ).x;
439 return ConvertDialogToPixels( sz ).y;
451 wxDialog::SetPosition( aNewPosition );
463 wxWindow* target = toolCanvas ? toolCanvas : m_parent;
470 if( !aDeferUntilFrameActive || !toolCanvas )
506 ret = wxDialog::Show( show );
508 wxRect savedDialogRect;
509 wxSize savedClientSize;
514 auto dlgIt = settings->CsInternals().m_dialogControlValues.find( key );
516 if( dlgIt != settings->CsInternals().m_dialogControlValues.end() )
518 auto geoIt = dlgIt->second.find(
"__geometry" );
520 if( geoIt != dlgIt->second.end() && geoIt->second.is_object() )
522 const nlohmann::json& g = geoIt->second;
523 savedDialogRect.SetPosition( wxPoint( g.value(
"x", 0 ), g.value(
"y", 0 ) ) );
524 savedDialogRect.SetSize( wxSize( g.value(
"w", 500 ), g.value(
"h", 300 ) ) );
525 savedClientSize = wxSize( g.value(
"cw", 0 ), g.value(
"ch", 0 ) );
530 if( savedDialogRect.GetSize().x != 0 && savedDialogRect.GetSize().y != 0 )
533 wxSize restoredSize = FromDIP( savedDialogRect.GetSize() );
537 SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
538 wxDialog::GetSize().x, wxDialog::GetSize().y, 0 );
544 if( savedClientSize.x > 0 && savedClientSize.y > 0 )
546 Move( savedDialogRect.GetPosition() );
548 wxSize restoredClient = FromDIP( savedClientSize );
549 SetClientSize( std::max( wxDialog::GetClientSize().x, restoredClient.x ),
550 std::max( wxDialog::GetClientSize().y, restoredClient.y ) );
554 SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
555 std::max( wxDialog::GetSize().x, restoredSize.x ),
556 std::max( wxDialog::GetSize().y, restoredSize.y ), 0 );
563 SetMinSize( wxDefaultSize );
564 InvalidateBestSize();
565 SetMinSize( GetBestSize() );
569 if( m_parent !=
nullptr )
571 if( wxDisplay::GetFromPoint( m_parent->GetPosition() )
572 != wxDisplay::GetFromPoint( savedDialogRect.GetPosition() ) )
591 wxPoint grabPoint = GetPosition();
592 grabPoint.x += GetSize().x / 2;
593 grabPoint.y += FromDIP( 15 );
595 if( wxDisplay::GetFromPoint( grabPoint ) == wxNOT_FOUND )
610 m_eventLoop->Exit( GetReturnCode() );
614 std::optional<wxPoint> position;
619 ret = wxDialog::Show( show );
635 auto dlgIt = settings->CsInternals().m_dialogControlValues.find( key );
637 if( dlgIt == settings->CsInternals().m_dialogControlValues.end() )
640 dlgIt->second.erase(
"__geometry" );
659 int parentDisplay = wxDisplay::GetFromWindow( m_parent );
660 int myDisplay = wxDisplay::GetFromWindow(
this );
662 if( parentDisplay != wxNOT_FOUND && myDisplay != wxNOT_FOUND )
687 return wxDialog::Enable( enable );
693 auto getSiblingIndex =
694 [](
const wxWindow* parent,
const wxWindow* child )
696 wxString childClass = child->GetClassInfo()->GetClassName();
699 for(
const wxWindow* sibling : parent->GetChildren() )
701 if( sibling->GetClassInfo()->GetClassName() != childClass )
704 if( sibling == child )
714 [&](
const wxWindow* window )
716 std::string key = wxString( window->GetClassInfo()->GetClassName() ).ToStdString();
718 if( window->GetParent() )
719 key +=
"_" + std::to_string( getSiblingIndex( window->GetParent(), window ) );
724 std::string key =
makeKey( aWin );
726 for(
const wxWindow* parent = aWin->GetParent(); parent && parent !=
this; parent = parent->GetParent() )
744 if( aPosition || IsShown() )
747 wxSize dipSize = ToDIP( GetSize() );
748 wxSize dipClientSize = ToDIP( GetClientSize() );
752 geom[
"w" ] = dipSize.x;
753 geom[
"h" ] = dipSize.y;
754 geom[
"cw" ] = dipClientSize.x;
755 geom[
"ch" ] = dipClientSize.y;
756 dlgMap[
"__geometry" ] = geom;
759 std::function<void( wxWindow* )> saveFn =
764 if( !props->GetPropertyOr(
"persist",
false ) )
776 wxString formula = binder ? binder->
GetFormula() : wxString();
782 if( formula.IsEmpty() )
788 nlohmann::json state;
789 state[
"value" ] = binder->
GetValue();
790 state[
"formula" ] = formula;
791 dlgMap[ key ] = state;
794 else if( !formula.IsEmpty() )
796 dlgMap[ key ] = formula;
800 dlgMap[ key ] = evalCtrl->GetFormula();
802 else if( wxComboBox* combo =
dynamic_cast<wxComboBox*
>( win ) )
804 dlgMap[ key ] = combo->GetValue();
806 else if( wxOwnerDrawnComboBox* od_combo =
dynamic_cast<wxOwnerDrawnComboBox*
>( win ) )
808 dlgMap[ key ] = od_combo->GetSelection();
810 else if( wxSpinCtrl* spin =
dynamic_cast<wxSpinCtrl*
>( win ) )
812 dlgMap[ key ] = spin->GetValue();
814 else if( wxSpinCtrlDouble* spinDouble =
dynamic_cast<wxSpinCtrlDouble*
>( win ) )
816 dlgMap[ key ] = spinDouble->GetValue();
818 else if( wxTextEntry* textEntry =
dynamic_cast<wxTextEntry*
>( win ) )
820 dlgMap[ key ] = textEntry->GetValue();
822 else if( wxChoice* choice =
dynamic_cast<wxChoice*
>( win ) )
824 dlgMap[ key ] = choice->GetSelection();
826 else if( wxCheckBox* check =
dynamic_cast<wxCheckBox*
>( win ) )
828 dlgMap[ key ] = check->GetValue();
830 else if( wxRadioButton* radio =
dynamic_cast<wxRadioButton*
>( win ) )
832 dlgMap[ key ] = radio->GetValue();
834 else if( wxRadioBox* radioBox =
dynamic_cast<wxRadioBox*
>( win ) )
836 dlgMap[ key ] = radioBox->GetSelection();
838 else if( wxSplitterWindow* splitter =
dynamic_cast<wxSplitterWindow*
>( win ) )
841 if( splitter->IsSplit() )
842 dlgMap[ key ] = splitter->GetSashPosition();
844 else if( wxScrolledWindow* scrolled =
dynamic_cast<wxScrolledWindow*
>( win ) )
846 dlgMap[ key ] = scrolled->GetScrollPos( wxVERTICAL );
848 else if( wxNotebook* notebook =
dynamic_cast<wxNotebook*
>( win ) )
850 int index = notebook->GetSelection();
852 if(
index >= 0 &&
index < (
int) notebook->GetPageCount() )
853 dlgMap[ key ] = notebook->GetPageText( notebook->GetSelection() );
855 else if( wxAuiNotebook* auiNotebook =
dynamic_cast<wxAuiNotebook*
>( win ) )
857 int index = auiNotebook->GetSelection();
859 if(
index >= 0 &&
index < (
int) auiNotebook->GetPageCount() )
860 dlgMap[ key ] = auiNotebook->GetPageText( auiNotebook->GetSelection() );
864 dlgMap[ key ] =
grid->GetShownColumnsAsString();
868 for( wxWindow* child : win->GetChildren() )
874 if( !props->GetPropertyOr(
"persist",
false ) )
878 for( wxWindow* child : GetChildren() )
896 const std::map<std::string, nlohmann::json>& dlgMap = dlgIt->second;
898 std::function<void( wxWindow* )> loadFn =
903 if( !props->GetPropertyOr(
"persist",
false ) )
914 auto it = dlgMap.find( key );
916 if( it != dlgMap.end() )
918 const nlohmann::json& j = it->second;
924 if( j.is_number_integer() )
928 else if( j.is_object() && j.contains(
"value" ) && j[
"value" ].is_number_integer() )
932 if( j.contains(
"formula" ) && j[
"formula" ].is_string() )
933 formula = wxString::FromUTF8( j[
"formula" ].get<std::string>().c_str() );
935 binder->
RestoreValue( j[
"value" ].get<int>(), formula );
937 else if( j.is_string() )
939 wxString
text = wxString::FromUTF8( j.get<std::string>().c_str() );
943 if( wxTextEntry* textEntry =
dynamic_cast<wxTextEntry*
>( win ) )
944 textEntry->ChangeValue(
text );
952 wxString
text = wxString::FromUTF8( j.get<std::string>().c_str() );
954 if( !evalCtrl->RestoreFormula(
text ) )
955 evalCtrl->ChangeValue(
text );
958 else if( wxComboBox* combo =
dynamic_cast<wxComboBox*
>( win ) )
961 combo->SetValue( wxString::FromUTF8( j.get<std::string>().c_str() ) );
963 else if( wxOwnerDrawnComboBox* od_combo =
dynamic_cast<wxOwnerDrawnComboBox*
>( win ) )
965 if( j.is_number_integer() )
967 int index = j.get<
int>();
969 if(
index >= 0 &&
index < (
int) od_combo->GetCount() )
970 od_combo->SetSelection(
index );
973 else if( wxSpinCtrl* spin =
dynamic_cast<wxSpinCtrl*
>( win ) )
975 if( j.is_number_integer() )
976 spin->SetValue( j.get<
int>() );
978 else if( wxSpinCtrlDouble* spinDouble =
dynamic_cast<wxSpinCtrlDouble*
>( win ) )
981 spinDouble->SetValue( j.get<
double>() );
983 else if( wxTextEntry* textEntry =
dynamic_cast<wxTextEntry*
>( win ) )
986 textEntry->ChangeValue( wxString::FromUTF8( j.get<std::string>().c_str() ) );
988 else if( wxChoice* choice =
dynamic_cast<wxChoice*
>( win ) )
990 if( j.is_number_integer() )
992 int index = j.get<
int>();
994 if(
index >= 0 &&
index < (
int) choice->GetCount() )
995 choice->SetSelection(
index );
998 else if( wxCheckBox* check =
dynamic_cast<wxCheckBox*
>( win ) )
1000 if( j.is_boolean() )
1001 check->SetValue( j.get<
bool>() );
1003 else if( wxRadioButton* radio =
dynamic_cast<wxRadioButton*
>( win ) )
1005 if( j.is_boolean() )
1011 radio->SetValue(
true );
1014 else if( wxRadioBox* radioBox =
dynamic_cast<wxRadioBox*
>( win ) )
1016 if( j.is_number_integer() )
1018 int index = j.get<
int>();
1020 if(
index >= 0 &&
index < (
int) radioBox->GetCount() )
1021 radioBox->SetSelection(
index );
1024 else if( wxSplitterWindow* splitter =
dynamic_cast<wxSplitterWindow*
>( win ) )
1026 if( j.is_number_integer() )
1027 splitter->SetSashPosition( j.get<
int>() );
1029 else if( wxScrolledWindow* scrolled =
dynamic_cast<wxScrolledWindow*
>( win ) )
1031 if( j.is_number_integer() )
1032 scrolled->SetScrollPos( wxVERTICAL, j.get<
int>() );
1034 else if( wxNotebook* notebook =
dynamic_cast<wxNotebook*
>( win ) )
1038 wxString pageTitle = wxString::FromUTF8( j.get<std::string>().c_str() );
1040 for(
int page = 0; page < (int) notebook->GetPageCount(); ++page )
1042 if( notebook->GetPageText( page ) == pageTitle )
1043 notebook->ChangeSelection( page );
1047 else if( wxAuiNotebook* auiNotebook =
dynamic_cast<wxAuiNotebook*
>( win ) )
1051 wxString pageTitle = wxString::FromUTF8( j.get<std::string>().c_str() );
1053 for(
int page = 0; page < (int) auiNotebook->GetPageCount(); ++page )
1055 if( auiNotebook->GetPageText( page ) == pageTitle )
1056 auiNotebook->ChangeSelection( page );
1063 grid->ShowHideColumns( wxString::FromUTF8( j.get<std::string>().c_str() ) );
1068 for( wxWindow* child : win->GetChildren() )
1074 if( !props->GetPropertyOr(
"persist",
false ) )
1078 for( wxWindow* child : GetChildren() )
1096 const std::map<std::string, nlohmann::json>& dlgMap = dlgIt->second;
1098 std::function<void( wxWindow* )> loadFn =
1099 [&]( wxWindow* win )
1103 if( !props->GetPropertyOr(
"persist",
false ) )
1111 auto it = dlgMap.find( key );
1113 if( it != dlgMap.end() )
1115 const nlohmann::json& j = it->second;
1117 if( wxSplitterWindow* splitter =
dynamic_cast<wxSplitterWindow*
>( win ) )
1119 if( j.is_number_integer() )
1120 splitter->SetSashPosition( j.get<
int>() );
1125 for( wxWindow* child : win->GetChildren() )
1131 if( !props->GetPropertyOr(
"persist",
false ) )
1135 for( wxWindow* child : GetChildren() )
1144 aWindow->SetClientData( props );
1146 if( aWindow ==
this )
1157 std::erase_if( dlgIt->second,
1158 [](
const auto& aEntry )
1160 return aEntry.first !=
"__geometry";
1185 [aUnitBinder](
const auto& aEntry )
1187 return aEntry.second == aUnitBinder;
1197 for( wxWindow* child : children )
1202 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( child ) )
1211#if defined( __WXMAC__ ) || defined( __WXMSW__ )
1212 if( !textCtrl->GetStringSelection().IsEmpty() )
1216 else if( textCtrl->IsEditable() )
1218 textCtrl->SelectAll();
1224 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( child ) )
1227 scintilla->Connect( wxEVT_SET_FOCUS,
1231 if( !scintilla->GetSelectedText().IsEmpty() )
1235 else if( scintilla->GetMarginWidth( 0 ) > 0 )
1239 else if( scintilla->IsEditable() )
1241 scintilla->SelectAll();
1247 else if(
dynamic_cast<wxBitmapButton*
>( child ) !=
nullptr )
1250 wxRect
rect = child->GetRect();
1252 child->ConvertDialogToPixels(
minSize );
1257 child->SetMinSize(
rect.GetSize() );
1258 child->SetSize(
rect );
1271 for( wxWindow* child : children )
1279 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( child ) )
1284 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( child ) )
1289 else if( wxComboBox* combo =
dynamic_cast<wxComboBox*
>( child ) )
1295 else if( wxChoice* choice =
dynamic_cast<wxChoice*
>( child ) )
1298 m_currentValues[ choice ] =
static_cast<long>( choice->GetSelection() );
1300 else if( wxCheckBox* check =
dynamic_cast<wxCheckBox*
>( child ) )
1305 else if( wxSpinCtrl* spin =
dynamic_cast<wxSpinCtrl*
>( child ) )
1311 else if( wxSpinCtrlDouble* spinD =
dynamic_cast<wxSpinCtrlDouble*
>( child ) )
1317 else if( wxRadioButton* radio =
dynamic_cast<wxRadioButton*
>( child ) )
1322 else if( wxRadioBox* radioBox =
dynamic_cast<wxRadioBox*
>( child ) )
1325 m_currentValues[ radioBox ] =
static_cast<long>( radioBox->GetSelection() );
1327 else if( wxGrid*
grid =
dynamic_cast<wxGrid*
>( child ) )
1332 else if( wxPropertyGrid* propGrid =
dynamic_cast<wxPropertyGrid*
>( child ) )
1337 else if( wxCheckListBox* checkList =
dynamic_cast<wxCheckListBox*
>( child ) )
1342 else if( wxDataViewListCtrl* dataList =
dynamic_cast<wxDataViewListCtrl*
>( child ) )
1382 if( before != after )
1461 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( aCtrl ) )
1462 return wxVariant( textCtrl->GetValue() );
1463 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( aCtrl ) )
1464 return wxVariant( scintilla->GetText() );
1465 else if( wxComboBox* combo =
dynamic_cast<wxComboBox*
>( aCtrl ) )
1466 return wxVariant( combo->GetValue() );
1467 else if( wxChoice* choice =
dynamic_cast<wxChoice*
>( aCtrl ) )
1468 return wxVariant( (
long) choice->GetSelection() );
1469 else if( wxCheckBox* check =
dynamic_cast<wxCheckBox*
>( aCtrl ) )
1470 return wxVariant( check->GetValue() );
1471 else if( wxSpinCtrl* spin =
dynamic_cast<wxSpinCtrl*
>( aCtrl ) )
1472 return wxVariant( (
long) spin->GetValue() );
1473 else if( wxSpinCtrlDouble* spinD =
dynamic_cast<wxSpinCtrlDouble*
>( aCtrl ) )
1474 return wxVariant( spinD->GetValue() );
1475 else if( wxRadioButton* radio =
dynamic_cast<wxRadioButton*
>( aCtrl ) )
1476 return wxVariant( radio->GetValue() );
1477 else if( wxRadioBox* radioBox =
dynamic_cast<wxRadioBox*
>( aCtrl ) )
1478 return wxVariant( (
long) radioBox->GetSelection() );
1479 else if( wxGrid*
grid =
dynamic_cast<wxGrid*
>( aCtrl ) )
1483 table &&
table->HasUndoStateSerialization() )
1485 return wxVariant(
table->SerializeUndoState() );
1488 nlohmann::json j = nlohmann::json::array();
1490 int cols =
grid->GetNumberCols();
1492 for(
int r = 0; r <
rows; ++r )
1494 nlohmann::json row = nlohmann::json::array();
1496 for(
int c = 0; c < cols; ++c )
1497 row.push_back( std::string(
grid->GetCellValue( r, c ).ToUTF8() ) );
1502 return wxVariant( wxString( j.dump() ) );
1504 else if( wxPropertyGrid* propGrid =
dynamic_cast<wxPropertyGrid*
>( aCtrl ) )
1508 for( wxPropertyGridIterator it = propGrid->GetIterator(); !it.AtEnd(); ++it )
1510 wxPGProperty* prop = *it;
1511 j[ prop->GetName().ToStdString() ] = prop->GetValueAsString().ToStdString();
1514 return wxVariant( wxString( j.dump() ) );
1516 else if( wxCheckListBox* checkList =
dynamic_cast<wxCheckListBox*
>( aCtrl ) )
1518 nlohmann::json j = nlohmann::json::array();
1519 unsigned int count = checkList->GetCount();
1521 for(
unsigned int i = 0; i < count; ++i )
1523 if( checkList->IsChecked( i ) )
1527 return wxVariant( wxString( j.dump() ) );
1529 else if( wxDataViewListCtrl* dataList =
dynamic_cast<wxDataViewListCtrl*
>( aCtrl ) )
1531 nlohmann::json j = nlohmann::json::array();
1532 unsigned int rows = dataList->GetItemCount();
1533 unsigned int cols = dataList->GetColumnCount();
1535 for(
unsigned int r = 0; r <
rows; ++r )
1537 nlohmann::json row = nlohmann::json::array();
1539 for(
unsigned int c = 0; c < cols; ++c )
1542 dataList->GetValue( val, r, c );
1543 row.push_back( std::string( val.GetString().ToUTF8() ) );
1549 return wxVariant( wxString( j.dump() ) );
1558 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( aCtrl ) )
1559 textCtrl->SetValue( aValue.GetString() );
1560 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( aCtrl ) )
1561 scintilla->SetText( aValue.GetString() );
1562 else if( wxComboBox* combo =
dynamic_cast<wxComboBox*
>( aCtrl ) )
1563 combo->SetValue( aValue.GetString() );
1564 else if( wxChoice* choice =
dynamic_cast<wxChoice*
>( aCtrl ) )
1565 choice->SetSelection( (
int) aValue.GetLong() );
1566 else if( wxCheckBox* check =
dynamic_cast<wxCheckBox*
>( aCtrl ) )
1567 check->SetValue( aValue.GetBool() );
1568 else if( wxSpinCtrl* spin =
dynamic_cast<wxSpinCtrl*
>( aCtrl ) )
1569 spin->SetValue( (
int) aValue.GetLong() );
1570 else if( wxSpinCtrlDouble* spinD =
dynamic_cast<wxSpinCtrlDouble*
>( aCtrl ) )
1571 spinD->SetValue( aValue.GetDouble() );
1572 else if( wxRadioButton* radio =
dynamic_cast<wxRadioButton*
>( aCtrl ) )
1573 radio->SetValue( aValue.GetBool() );
1574 else if( wxRadioBox* radioBox =
dynamic_cast<wxRadioBox*
>( aCtrl ) )
1575 radioBox->SetSelection( (
int) aValue.GetLong() );
1576 else if( wxGrid*
grid =
dynamic_cast<wxGrid*
>( aCtrl ) )
1579 table &&
table->HasUndoStateSerialization() )
1581 table->RestoreUndoState( aValue.GetString() );
1585 nlohmann::json j = nlohmann::json::parse( aValue.GetString().ToStdString(),
nullptr,
false );
1589 int rows = std::min( (
int) j.size(),
grid->GetNumberRows() );
1591 std::vector<std::pair<int, int>> changedCells;
1593 for(
int r = 0; r <
rows; ++r )
1595 nlohmann::json row = j[r];
1596 int cols = std::min( (
int) row.size(),
grid->GetNumberCols() );
1598 for(
int c = 0; c < cols; ++c )
1600 wxString value = wxString( row[c].get<std::string>() );
1602 if(
grid->GetCellValue( r, c ) != value )
1604 grid->SetCellValue( r, c, value );
1605 changedCells.emplace_back( r, c );
1610 for(
const auto& [row, col] : changedCells )
1612 wxGridEvent evt(
grid->GetId(), wxEVT_GRID_CELL_CHANGED,
grid, row, col );
1613 evt.SetString(
grid->GetCellValue( row, col ) );
1614 grid->GetEventHandler()->ProcessEvent( evt );
1618 else if( wxPropertyGrid* propGrid =
dynamic_cast<wxPropertyGrid*
>( aCtrl ) )
1620 nlohmann::json j = nlohmann::json::parse( aValue.GetString().ToStdString(),
nullptr,
false );
1624 for(
auto it = j.begin(); it != j.end(); ++it )
1625 propGrid->SetPropertyValue( wxString( it.key() ), wxString( it.value().get<std::string>() ) );
1628 else if( wxCheckListBox* checkList =
dynamic_cast<wxCheckListBox*
>( aCtrl ) )
1630 nlohmann::json j = nlohmann::json::parse( aValue.GetString().ToStdString(),
nullptr,
false );
1634 unsigned int count = checkList->GetCount();
1636 for(
unsigned int i = 0; i < count; ++i )
1637 checkList->Check( i,
false );
1639 for(
auto& idx : j )
1641 unsigned int i = idx.get<
unsigned int>();
1644 checkList->Check( i,
true );
1648 else if( wxDataViewListCtrl* dataList =
dynamic_cast<wxDataViewListCtrl*
>( aCtrl ) )
1650 nlohmann::json j = nlohmann::json::parse( aValue.GetString().ToStdString(),
nullptr,
false );
1654 unsigned int rows = std::min(
static_cast<unsigned int>( j.size() ),
1655 static_cast<unsigned int>( dataList->GetItemCount() ) );
1657 for(
unsigned int r = 0; r <
rows; ++r )
1659 nlohmann::json row = j[r];
1660 unsigned int cols = std::min( (
unsigned int) row.size(), dataList->GetColumnCount() );
1662 for(
unsigned int c = 0; c < cols; ++c )
1664 wxVariant val( wxString( row[c].get<std::string>() ) );
1665 dataList->SetValue( val, r, c );
1746 wxWeakRef<DIALOG_SHIM> self(
this );
1754 && wxWindow::FindFocus() ==
nullptr )
1767 if( !GetTitle().StartsWith( wxS(
"*" ) ) )
1768 SetTitle( wxS(
"*" ) + GetTitle() );
1774 if( GetTitle().StartsWith( wxS(
"*" ) ) )
1775 SetTitle( GetTitle().AfterFirst(
'*' ) );
1789 return wxDialog::ShowModal();
1825 if( wxWindow* win = wxWindow::GetCapture() )
1826 win->ReleaseMouse();
1829 wxWindow* parent = GetParentForModalDialog( GetParent(), GetWindowStyle() );
1846 wxGUIEventLoop event_loop;
1855 return GetReturnCode();
1877 if( ( retCode == wxID_OK ) && ( !Validate() || !TransferDataFromWindow() ) )
1880 SetReturnCode( retCode );
1884 wxFAIL_MSG( wxT(
"Either DIALOG_SHIM::EndQuasiModal was called twice, or ShowQuasiModal wasn't called" ) );
1907 wxString msg = wxString::Format(
"Closing dialog %s", GetTitle() );
1925 const int id = aEvent.GetId();
1929 if(
id == GetAffirmativeId() )
1933 else if(
id == wxID_APPLY )
1942 else if(
id == wxID_CANCEL )
1965 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( aEvent.GetEventObject() ) )
1967 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( aEvent.GetEventObject() ) )
1977 int key = aEvt.GetKeyCode();
1980 if( aEvt.ControlDown() )
1982 if( aEvt.ShiftDown() )
1984 if( aEvt.AltDown() )
1987 int hotkey = key | mods;
1990 if( hotkey == (
MD_CTRL +
'Z') )
2001 if( aEvt.GetKeyCode() ==
'U' && aEvt.GetModifiers() == wxMOD_CONTROL )
2010 else if( ( aEvt.GetKeyCode() == WXK_RETURN || aEvt.GetKeyCode() == WXK_NUMPAD_ENTER ) && aEvt.ShiftDown() )
2012 wxObject* eventSource = aEvt.GetEventObject();
2014 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( eventSource ) )
2017 if( !textCtrl->IsMultiLine() )
2019 wxPostEvent(
this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
2023#if defined( __WXMAC__ ) || defined( __WXMSW__ )
2024 wxString eol =
"\r\n";
2026 wxString eol =
"\n";
2029 long pos = textCtrl->GetInsertionPoint();
2030 textCtrl->WriteText( eol );
2031 textCtrl->SetInsertionPoint( pos + eol.length() );
2034 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( eventSource ) )
2036 wxString eol =
"\n";
2038 switch( scintilla->GetEOLMode() )
2040 case wxSTC_EOL_CRLF: eol =
"\r\n";
break;
2041 case wxSTC_EOL_CR: eol =
"\r";
break;
2042 case wxSTC_EOL_LF: eol =
"\n";
break;
2045 long pos = scintilla->GetCurrentPos();
2046 scintilla->InsertText( pos, eol );
2047 scintilla->GotoPos( pos + eol.length() );
2053 else if( ( aEvt.GetKeyCode() == WXK_RETURN || aEvt.GetKeyCode() == WXK_NUMPAD_ENTER ) && aEvt.ControlDown() )
2055 wxPostEvent(
this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
2058 else if( aEvt.GetKeyCode() == WXK_TAB && !aEvt.ControlDown() )
2060 wxWindow* currentWindow = wxWindow::FindFocus();
2061 int currentIdx = -1;
2062 int delta = aEvt.ShiftDown() ? -1 : 1;
2069 idx = ( ( idx +
delta ) % size + size ) % size;
2072 for(
size_t i = 0; i <
m_tabOrder.size(); ++i )
2077 || ( currentWindow &&
m_tabOrder[i]->IsDescendant( currentWindow ) ) )
2079 currentIdx = (int) i;
2084 if( currentIdx >= 0 )
2086 advance( currentIdx );
2089 int startIdx = currentIdx;
2093 advance( currentIdx );
2095 if( currentIdx == startIdx )
2102 while(
dynamic_cast<wxTextEntry*
>(
m_tabOrder[ currentIdx ] ) ==
nullptr )
2103 advance( currentIdx );
2110 else if( aEvt.GetKeyCode() == WXK_ESCAPE )
2112 wxObject* eventSource = aEvt.GetEventObject();
2114 if( wxTextCtrl* textCtrl =
dynamic_cast<wxTextCtrl*
>( eventSource ) )
2120 textCtrl->SelectAll();
2124 else if( wxStyledTextCtrl* scintilla =
dynamic_cast<wxStyledTextCtrl*
>( eventSource ) )
2130 scintilla->SelectAll();
2142 wxStdDialogButtonSizer* sdbSizer =
dynamic_cast<wxStdDialogButtonSizer*
>( aSizer );
2145 [&]( wxButton* aButton )
2147 if( aLabels.count( aButton->GetId() ) > 0 )
2149 aButton->SetLabel( aLabels[ aButton->GetId() ] );
2155 switch( aButton->GetId() )
2157 case wxID_OK: aButton->SetLabel(
_(
"&OK" ) );
break;
2158 case wxID_CANCEL: aButton->SetLabel(
_(
"&Cancel" ) );
break;
2159 case wxID_YES: aButton->SetLabel(
_(
"&Yes" ) );
break;
2160 case wxID_NO: aButton->SetLabel(
_(
"&No" ) );
break;
2161 case wxID_APPLY: aButton->SetLabel(
_(
"&Apply" ) );
break;
2162 case wxID_SAVE: aButton->SetLabel(
_(
"&Save" ) );
break;
2163 case wxID_HELP: aButton->SetLabel(
_(
"&Help" ) );
break;
2164 case wxID_CONTEXT_HELP: aButton->SetLabel(
_(
"&Help" ) );
break;
2171 if( sdbSizer->GetAffirmativeButton() )
2172 setupButton( sdbSizer->GetAffirmativeButton() );
2174 if( sdbSizer->GetApplyButton() )
2175 setupButton( sdbSizer->GetApplyButton() );
2177 if( sdbSizer->GetNegativeButton() )
2178 setupButton( sdbSizer->GetNegativeButton() );
2180 if( sdbSizer->GetCancelButton() )
2181 setupButton( sdbSizer->GetCancelButton() );
2183 if( sdbSizer->GetHelpButton() )
2184 setupButton( sdbSizer->GetHelpButton() );
2188 if( sdbSizer->GetAffirmativeButton() )
2189 sdbSizer->GetAffirmativeButton()->SetDefault();
2192 for( wxSizerItem* item : aSizer->GetChildren() )
2194 if( item->GetSizer() )
2211 EndDialog( aReturnCode );
COMMON_SETTINGS_INTERNALS & CsInternals()
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
void SelectAllInTextCtrls(wxWindowList &children)
wxVariant getControlValue(wxWindow *aCtrl)
void onPropertyGridChanged(wxPropertyGridEvent &aEvent)
std::set< wxWindow * > m_noControlUndoRedo
std::vector< wxWindow * > m_tabOrder
void OnPaint(wxPaintEvent &event)
virtual void TearDownQuasiModal()
Override this method to perform dialog tear down actions not suitable for object dtor.
std::set< wxWindow * > m_controlsWithPendingChanges
void recordControlChange(wxWindow *aCtrl)
Tell the undo mechanism to snapshot the current value of a control and record it as an undo step when...
int vertPixelsFromDU(int y) const
Convert an integer number of dialog units to pixels, vertically.
bool Show(bool show) override
std::vector< UNDO_STEP > m_redoStack
void setControlValue(wxWindow *aCtrl, const wxVariant &aValue)
wxGUIEventLoop * m_qmodal_loop
void onChildSetFocus(wxFocusEvent &aEvent)
void LoadControlState()
Load persisted control values from the current project's local settings.
void ExcludeFromControlUndoRedo(wxWindow *aWindow)
Opt a control out of the dialog's generic Ctrl+Z/Ctrl+Y undo/redo.
void UnregisterUnitBinder(UNIT_BINDER *aUnitBinder)
Remove a UNIT_BINDER from the control-state save/restore map.
void OptOut(wxWindow *aWindow)
Opt out of control state saving.
void SetupStandardButtons(std::map< int, wxString > aLabels={})
WINDOW_DISABLER * m_qmodal_parent_disabler
void clampToWorkArea()
Constrain the dialog's minimum size, size and position to the work area of the display it occupies,...
void onInitDialog(wxInitDialogEvent &aEvent)
void OnActivate(wxActivateEvent &aEvent)
void onSpinDoubleEvent(wxSpinDoubleEvent &aEvent)
void SaveControlState(const std::optional< wxPoint > &aPosition=std::nullopt)
Save control values and geometry to the current project's local settings.
int horizPixelsFromDU(int x) const
Convert an integer number of dialog units to pixels, horizontally.
void resetSize()
Clear the existing dialog size and position.
std::map< wxWindow *, wxString > m_beforeEditValues
void setSizeInDU(int x, int y)
Set the dialog to the given dimensions in "dialog units".
void onDataViewListChanged(wxDataViewEvent &aEvent)
bool IsQuasiModal() const
std::map< wxWindow *, UNIT_BINDER * > m_unitBinders
void ReloadSashPositions()
While we did this in LoadControlState(), we need to re-apply if after setting the dialog size.
void EndQuasiModal(int retCode)
void RegisterUnitBinder(UNIT_BINDER *aUnitBinder, wxWindow *aWindow)
Register a UNIT_BINDER so that it can handle units in control-state save/restore.
void OnMove(wxMoveEvent &aEvent)
void EndDialogShim(int aReturnCode)
A mode agnostic way to close a dialog.
void onCommandEvent(wxCommandEvent &aEvent)
void CleanupAfterModalSubDialog()
std::string generateKey(const wxWindow *aWin) const
void PrepareForModalSubDialog()
void OnButton(wxCommandEvent &aEvent)
Properly handle the default button events when in the quasimodal mode when not calling EndQuasiModal ...
void forceInitialFocus()
Focus the requested initial target if it is visible, otherwise focus the dialog itself so keyboard ev...
void onGridCellChanged(wxGridEvent &aEvent)
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void registerUndoRedoHandlers(wxWindowList &aChildren)
wxWindow * m_initialFocusTarget
void OnSize(wxSizeEvent &aEvent)
bool Enable(bool enable) override
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
void focusParentCanvas(bool aDeferUntilFrameActive=false)
Set focus back to the parent frame's tool canvas if available, otherwise to the parent window.
void SetPosition(const wxPoint &aNewPosition)
Force the position of the dialog to a new position.
void onSpinEvent(wxSpinEvent &aEvent)
void OnCloseWindow(wxCloseEvent &aEvent)
Properly handle the wxCloseEvent when in the quasimodal mode when not calling EndQuasiModal which is ...
std::map< wxWindow *, wxVariant > m_currentValues
EDA_BASE_FRAME * m_parentFrame
virtual void OnCharHook(wxKeyEvent &aEvt)
std::vector< UNDO_STEP > m_undoStack
void onStyledTextChanged(wxStyledTextEvent &aEvent)
void flushPendingControlChanges()
Apply outstanding control changes to the undo stack, and clear the pending list.
The base frame for deriving all KiCad main window classes.
KIWAY_HOLDER(KIWAY *aKiway, HOLDER_TYPE aType)
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
void SetKiway(wxWindow *aDest, KIWAY *aKiway)
It is only used for debugging, since "this" is not a wxWindow*.
bool HasKiway() const
Safety check before asking for the Kiway reference.
HOLDER_TYPE GetType() const
void SetBlockingDialog(wxWindow *aWin)
virtual COMMON_SETTINGS * GetCommonSettings() const
virtual wxApp & App()
Return a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
bool SetProperty(const std::string &aKey, T &&aValue)
Set a property with the given key and value.
static PROPERTY_HOLDER * SafeCast(void *aPtr) noexcept
Safely cast a void pointer to PROPERTY_HOLDER*.
wxTextCtrl wrapper to handle math expression evaluation.
EDA_UNITS GetUserUnits() const
virtual wxString GetFormula() const
Return the expression the user entered if it is more than a plain number, otherwise an empty string.
virtual long long int GetValue() const
Return the current value in Internal Units.
virtual void RestoreValue(int aValue, const wxString &aFormula)
Set a value (in Internal Units) as ChangeValue() does and reinstate the expression that produced it.
virtual bool RestoreFormula(const wxString &aFormula)
Show the result of an expression returned by GetFormula() and retain the expression so that it reappe...
bool UnitsInvariant() const
virtual void ChangeValue(int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion WITHOUT trigger...
Temporarily disable a window, and then re-enable on destruction.
static std::string getDialogKeyFromTitle(const wxString &aTitle)
Strip parenthetical suffixes from dialog titles to create stable persistence keys.
static void recursiveDescent(wxSizer *aSizer, std::map< int, wxString > &aLabels)
static bool isCompoundDateTimePicker(const wxWindow *aWin)
Return true when the given window is a compound date/time picker whose internal children should be op...
wxRect ClampRectToDisplay(const wxRect &aRect, const wxRect &aClientArea)
Constrain a window rectangle so it fits entirely within a display's client area.
KICOMMON_API wxRect ClampRectToDisplay(const wxRect &aRect, const wxRect &aClientArea)
Constrain a window rectangle so it fits entirely within a display's client area.
const int minSize
Push and Shove router track width and via size dialog.
void ignore_unused(const T &)
void AddNavigationBreadcrumb(const wxString &aMsg, const wxString &aCategory)
Add a navigation breadcrumb.
static wxString makeKey(const wxString &aFirst, const wxString &aSecond)
Assemble a two part key as a simple concatenation of aFirst and aSecond parts, using a separator.
PGM_BASE & Pgm()
The global program "get" accessor.
std::map< std::string, std::map< std::string, nlohmann::json > > m_dialogControlValues
std::vector< DATABASE_CONNECTION::ROW > rows
static const long long MM
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)