-
-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathdialog_shim.cpp
More file actions
1896 lines (1573 loc) · 65.3 KB
/
Copy pathdialog_shim.cpp
File metadata and controls
1896 lines (1573 loc) · 65.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2023 CERN
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* 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, see <https://www.gnu.org/licenses/>.
*/
#include <app_monitor.h>
#include <dialog_shim.h>
#include <settings/common_settings.h>
#include <settings/common_settings_internals.h>
#include <core/ignore.h>
#include <kiway_player.h>
#include <kiway.h>
#include <pgm_base.h>
#include <project/project_local_settings.h>
#include <property_holder.h>
#include <settings/settings_manager.h>
#include <tool/tool_manager.h>
#include <kiplatform/ui.h>
#include <widgets/unit_binder.h>
#include <wx/display.h>
#include <wx/evtloop.h>
#include <wx/app.h>
#include <wx/event.h>
#include <wx/grid.h>
#include <widgets/wx_grid.h>
#include <wx/propgrid/propgrid.h>
#include <wx/checklst.h>
#include <wx/dataview.h>
#include <wx/bmpbuttn.h>
#include <wx/textctrl.h>
#include <wx/stc/stc.h>
#include <wx/combobox.h>
#include <wx/odcombo.h>
#include <wx/choice.h>
#include <wx/checkbox.h>
#include <wx/spinctrl.h>
#include <wx/splitter.h>
#include <wx/radiobox.h>
#include <wx/radiobut.h>
#include <wx/variant.h>
#include <algorithm>
#include <functional>
#include <nlohmann/json.hpp>
#include <typeinfo>
BEGIN_EVENT_TABLE( DIALOG_SHIM, wxDialog )
EVT_CHAR_HOOK( DIALOG_SHIM::OnCharHook )
END_EVENT_TABLE()
/**
* Strip parenthetical suffixes from dialog titles to create stable persistence keys.
*
* Dialog titles like "Choose Symbol (1234 items loaded)" would otherwise create unique
* keys for each item count, flooding the settings file with duplicate entries.
*/
static std::string getDialogKeyFromTitle( const wxString& aTitle )
{
std::string title = aTitle.ToStdString();
size_t parenPos = title.rfind( '(' );
if( parenPos != std::string::npos && parenPos > 0 )
{
size_t end = parenPos;
while( end > 0 && title[end - 1] == ' ' )
end--;
return title.substr( 0, end );
}
return title;
}
DIALOG_SHIM::DIALOG_SHIM( wxWindow* aParent, wxWindowID id, const wxString& title, const wxPoint& pos,
const wxSize& size, long style, const wxString& name ) :
wxDialog( aParent, id, title, pos, size, style, name ),
KIWAY_HOLDER( nullptr, KIWAY_HOLDER::DIALOG ),
m_units( EDA_UNITS::MM ),
m_useCalculatedSize( false ),
m_firstPaintEvent( true ),
m_initialFocusTarget( nullptr ),
m_isClosing( false ),
m_qmodal_loop( nullptr ),
m_qmodal_showing( false ),
m_qmodal_parent_disabler( nullptr ),
m_parentFrame( nullptr ),
m_userPositioned( false ),
m_userResized( false ),
m_handlingUndoRedo( false ),
m_childReleased( false )
{
KIWAY_HOLDER* kiwayHolder = nullptr;
m_initialSize = size;
if( aParent )
{
kiwayHolder = dynamic_cast<KIWAY_HOLDER*>( aParent );
while( !kiwayHolder && aParent->GetParent() )
{
aParent = aParent->GetParent();
kiwayHolder = dynamic_cast<KIWAY_HOLDER*>( aParent );
}
}
// Inherit units from parent
if( kiwayHolder && kiwayHolder->GetType() == KIWAY_HOLDER::FRAME )
m_units = static_cast<EDA_BASE_FRAME*>( kiwayHolder )->GetUserUnits();
else if( kiwayHolder && kiwayHolder->GetType() == KIWAY_HOLDER::DIALOG )
m_units = static_cast<DIALOG_SHIM*>( kiwayHolder )->GetUserUnits();
// Don't mouse-warp after a dialog run from the context menu
if( kiwayHolder && kiwayHolder->GetType() == KIWAY_HOLDER::FRAME )
{
m_parentFrame = static_cast<EDA_BASE_FRAME*>( kiwayHolder );
TOOL_MANAGER* toolMgr = m_parentFrame->GetToolManager();
if( toolMgr && toolMgr->IsContextMenuActive() )
toolMgr->VetoContextMenuMouseWarp();
}
// Set up the message bus
if( kiwayHolder )
SetKiway( this, &kiwayHolder->Kiway() );
if( HasKiway() )
Kiway().SetBlockingDialog( this );
Bind( wxEVT_CLOSE_WINDOW, &DIALOG_SHIM::OnCloseWindow, this );
Bind( wxEVT_BUTTON, &DIALOG_SHIM::OnButton, this );
Bind( wxEVT_SIZE, &DIALOG_SHIM::OnSize, this );
Bind( wxEVT_MOVE, &DIALOG_SHIM::OnMove, this );
Bind( wxEVT_INIT_DIALOG, &DIALOG_SHIM::onInitDialog, this );
#ifdef __WINDOWS__
// On Windows, the app top windows can be brought to the foreground (at least temporarily)
// in certain circumstances such as when calling an external tool in Eeschema BOM generation.
// So set the parent frame (if exists) to top window to avoid this annoying behavior.
if( kiwayHolder && kiwayHolder->GetType() == KIWAY_HOLDER::FRAME )
Pgm().App().SetTopWindow( (EDA_BASE_FRAME*) kiwayHolder );
#endif
Bind( wxEVT_PAINT, &DIALOG_SHIM::OnPaint, this );
wxString msg = wxString::Format( "Opening dialog %s", GetTitle() );
APP_MONITOR::AddNavigationBreadcrumb( msg, "dialog.open" );
}
DIALOG_SHIM::~DIALOG_SHIM()
{
m_isClosing = true;
Unbind( wxEVT_CLOSE_WINDOW, &DIALOG_SHIM::OnCloseWindow, this );
Unbind( wxEVT_BUTTON, &DIALOG_SHIM::OnButton, this );
Unbind( wxEVT_PAINT, &DIALOG_SHIM::OnPaint, this );
Unbind( wxEVT_SIZE, &DIALOG_SHIM::OnSize, this );
Unbind( wxEVT_MOVE, &DIALOG_SHIM::OnMove, this );
Unbind( wxEVT_INIT_DIALOG, &DIALOG_SHIM::onInitDialog, this );
std::function<void( wxWindowList& )> disconnectFocusHandlers =
[&]( wxWindowList& children )
{
for( wxWindow* child : children )
{
if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( child ) )
{
textCtrl->Disconnect( wxEVT_SET_FOCUS, wxFocusEventHandler( DIALOG_SHIM::onChildSetFocus ),
nullptr, this );
}
else if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( child ) )
{
scintilla->Disconnect( wxEVT_SET_FOCUS, wxFocusEventHandler( DIALOG_SHIM::onChildSetFocus ),
nullptr, this );
}
else
{
disconnectFocusHandlers( child->GetChildren() );
}
}
};
disconnectFocusHandlers( GetChildren() );
std::function<void( wxWindowList& )> disconnectUndoRedoHandlers =
[&]( wxWindowList& children )
{
for( wxWindow* child : children )
{
if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( child ) )
{
textCtrl->Unbind( wxEVT_TEXT, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( child ) )
{
scintilla->Unbind( wxEVT_STC_CHANGE, &DIALOG_SHIM::onStyledTextChanged, this );
}
else if( wxComboBox* combo = dynamic_cast<wxComboBox*>( child ) )
{
combo->Unbind( wxEVT_TEXT, &DIALOG_SHIM::onCommandEvent, this );
combo->Unbind( wxEVT_COMBOBOX, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxChoice* choice = dynamic_cast<wxChoice*>( child ) )
{
choice->Unbind( wxEVT_CHOICE, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxCheckBox* check = dynamic_cast<wxCheckBox*>( child ) )
{
check->Unbind( wxEVT_CHECKBOX, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxSpinCtrl* spin = dynamic_cast<wxSpinCtrl*>( child ) )
{
spin->Unbind( wxEVT_SPINCTRL, &DIALOG_SHIM::onSpinEvent, this );
spin->Unbind( wxEVT_TEXT, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxSpinCtrlDouble* spinD = dynamic_cast<wxSpinCtrlDouble*>( child ) )
{
spinD->Unbind( wxEVT_SPINCTRLDOUBLE, &DIALOG_SHIM::onSpinDoubleEvent, this );
spinD->Unbind( wxEVT_TEXT, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxRadioButton* radio = dynamic_cast<wxRadioButton*>( child ) )
{
radio->Unbind( wxEVT_RADIOBUTTON, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxRadioBox* radioBox = dynamic_cast<wxRadioBox*>( child ) )
{
radioBox->Unbind( wxEVT_RADIOBOX, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxGrid* grid = dynamic_cast<wxGrid*>( child ) )
{
grid->Unbind( wxEVT_GRID_CELL_CHANGED, &DIALOG_SHIM::onGridCellChanged, this );
}
else if( wxPropertyGrid* propGrid = dynamic_cast<wxPropertyGrid*>( child ) )
{
propGrid->Unbind( wxEVT_PG_CHANGED, &DIALOG_SHIM::onPropertyGridChanged, this );
}
else if( wxCheckListBox* checkList = dynamic_cast<wxCheckListBox*>( child ) )
{
checkList->Unbind( wxEVT_CHECKLISTBOX, &DIALOG_SHIM::onCommandEvent, this );
}
else if( wxDataViewListCtrl* dataList = dynamic_cast<wxDataViewListCtrl*>( child ) )
{
dataList->Unbind( wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &DIALOG_SHIM::onDataViewListChanged, this );
}
else
{
disconnectUndoRedoHandlers( child->GetChildren() );
}
}
};
disconnectUndoRedoHandlers( GetChildren() );
// The child controls (and the UNIT_BINDERs that live alongside them) outlive this dialog's
// member teardown, so sever their back-references now while m_unitBinders is still valid.
for( const auto& [window, binder] : m_unitBinders )
binder->DetachFromDialogShim();
// if the dialog is quasi-modal, this will end its event loop
if( IsQuasiModal() )
EndQuasiModal( wxID_CANCEL );
if( HasKiway() )
Kiway().SetBlockingDialog( nullptr );
delete m_qmodal_parent_disabler;
}
void DIALOG_SHIM::onInitDialog( wxInitDialogEvent& aEvent )
{
#ifdef __WXMAC__
CallAfter(
[this]
{
if( wxSizer* sz = GetSizer() )
sz->Layout();
} );
#endif
LoadControlState();
aEvent.Skip();
}
void DIALOG_SHIM::finishDialogSettings()
{
// must be called from the constructor of derived classes,
// when all widgets are initialized, and therefore their size fixed
// SetSizeHints fixes the minimal size of sizers in the dialog
// (SetSizeHints calls Fit(), so no need to call it)
GetSizer()->SetSizeHints( this );
}
wxRect ClampRectToDisplay( const wxRect& aRect, const wxRect& aClientArea )
{
wxRect rect = aRect;
// A window can never be larger than the display that holds it.
rect.width = std::min( rect.width, aClientArea.width );
rect.height = std::min( rect.height, aClientArea.height );
rect.x = std::clamp( rect.x, aClientArea.x, aClientArea.GetRight() - rect.width + 1 );
rect.y = std::clamp( rect.y, aClientArea.y, aClientArea.GetBottom() - rect.height + 1 );
return rect;
}
void DIALOG_SHIM::clampToWorkArea()
{
// A dialog not yet mapped onto a monitor reports no display, so fall back to the parent's
// monitor rather than blindly clamping against display zero on a multi-head setup.
int displayIdx = wxDisplay::GetFromWindow( this );
if( displayIdx == wxNOT_FOUND && m_parent )
displayIdx = wxDisplay::GetFromWindow( m_parent );
if( displayIdx == wxNOT_FOUND )
displayIdx = 0;
wxRect clientArea = wxDisplay( (unsigned int) displayIdx ).GetClientArea();
if( clientArea.width <= 0 || clientArea.height <= 0 )
return;
// The minimum size must shrink first, otherwise SetSize() below cannot honour a cap that
// is smaller than a stale minimum restored from a larger monitor.
wxSize minSize = GetMinSize();
wxSize clampedMin( std::min( minSize.x, clientArea.width ),
std::min( minSize.y, clientArea.height ) );
if( clampedMin != minSize )
SetMinSize( clampedMin );
// Cap the size to the work area and pull the whole dialog back on-screen. Geometry restored
// from a different (possibly higher-DPI) monitor can otherwise land off-screen or oversized.
wxRect current( GetPosition(), GetSize() );
wxRect clamped = ClampRectToDisplay( current, clientArea );
if( clamped != current )
SetSize( clamped.x, clamped.y, clamped.width, clamped.height, 0 );
}
void DIALOG_SHIM::setSizeInDU( int x, int y )
{
wxSize sz( x, y );
SetSize( ConvertDialogToPixels( sz ) );
}
int DIALOG_SHIM::horizPixelsFromDU( int x ) const
{
wxSize sz( x, 0 );
return ConvertDialogToPixels( sz ).x;
}
int DIALOG_SHIM::vertPixelsFromDU( int y ) const
{
wxSize sz( 0, y );
return ConvertDialogToPixels( sz ).y;
}
// our hashtable is an implementation secret, don't need or want it in a header file
#include <hashtables.h>
#include <typeinfo>
#include <grid_tricks.h>
void DIALOG_SHIM::SetPosition( const wxPoint& aNewPosition )
{
wxDialog::SetPosition( aNewPosition );
}
void DIALOG_SHIM::focusParentCanvas( bool aDeferUntilFrameActive )
{
wxWindow* toolCanvas = m_parentFrame ? m_parentFrame->GetToolCanvas() : nullptr;
wxWindow* target = toolCanvas ? toolCanvas : m_parent;
if( !target )
return;
target->SetFocus();
if( !aDeferUntilFrameActive || !toolCanvas )
return;
#ifdef __WXGTK__
// A quasi-modal dialog is still the active top-level window when its nested event loop exits,
// so the SetFocus() above is undone when the dialog is destroyed and GTK restores the frame's
// previously-focused widget. Re-assert focus once the event loop has settled, otherwise
// keyboard events keep routing to the stale owner until the mouse re-enters the canvas.
EDA_BASE_FRAME* frame = m_parentFrame;
frame->CallAfter(
[frame]()
{
// Skip if another dialog grabbed the activation in the meantime, otherwise we
// would raise the frame from behind a chained modal dialog.
if( !KIPLATFORM::UI::IsWindowActive( frame ) )
return;
if( wxWindow* canvas = frame->GetToolCanvas() )
canvas->SetFocus();
} );
#endif
}
bool DIALOG_SHIM::Show( bool show )
{
bool ret;
if( show )
{
KIPLATFORM::UI::StabilizeWindowPosition( this );
#ifndef __WINDOWS__
wxDialog::Raise(); // Needed on OS X and some other window managers (i.e. Unity)
#endif
ret = wxDialog::Show( show );
wxRect savedDialogRect;
std::string key = m_hash_key.empty() ? getDialogKeyFromTitle( GetTitle() ) : m_hash_key;
if( COMMON_SETTINGS* settings = Pgm().GetCommonSettings() )
{
auto dlgIt = settings->CsInternals().m_dialogControlValues.find( key );
if( dlgIt != settings->CsInternals().m_dialogControlValues.end() )
{
auto geoIt = dlgIt->second.find( "__geometry" );
if( geoIt != dlgIt->second.end() && geoIt->second.is_object() )
{
const nlohmann::json& g = geoIt->second;
savedDialogRect.SetPosition( wxPoint( g.value( "x", 0 ), g.value( "y", 0 ) ) );
savedDialogRect.SetSize( wxSize( g.value( "w", 500 ), g.value( "h", 300 ) ) );
}
}
}
if( savedDialogRect.GetSize().x != 0 && savedDialogRect.GetSize().y != 0 )
{
// Convert saved DIP size to logical pixels for the current monitor
wxSize restoredSize = FromDIP( savedDialogRect.GetSize() );
if( m_useCalculatedSize )
{
SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
wxDialog::GetSize().x, wxDialog::GetSize().y, 0 );
}
else
{
SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
std::max( wxDialog::GetSize().x, restoredSize.x ),
std::max( wxDialog::GetSize().y, restoredSize.y ), 0 );
// Reset minimum size so the user can resize the dialog smaller than
// the saved size. We must clear the current minimum and invalidate
// the cached best size so GetBestSize() returns the true sizer
// minimum rather than being constrained by the restored size.
SetMinSize( wxDefaultSize );
InvalidateBestSize();
SetMinSize( GetBestSize() );
}
#ifdef __WXMAC__
if( m_parent != nullptr )
{
if( wxDisplay::GetFromPoint( m_parent->GetPosition() )
!= wxDisplay::GetFromPoint( savedDialogRect.GetPosition() ) )
{
Centre();
}
}
#endif
}
else if( m_initialSize != wxDefaultSize )
{
SetSize( m_initialSize );
Centre();
}
// Re-center if the title bar would land on no display. Testing a point inside the title
// bar (not the window corner) ignores the negative border offset of maximized windows.
wxPoint grabPoint = GetPosition();
grabPoint.x += GetSize().x / 2;
grabPoint.y += FromDIP( 15 );
if( wxDisplay::GetFromPoint( grabPoint ) == wxNOT_FOUND )
Centre();
m_userPositioned = false;
m_userResized = false;
// Cap size and pull the dialog back on-screen here, after the minimum has been
// (re)established above, so the clamp is not overwritten.
clampToWorkArea();
KIPLATFORM::UI::EnsureVisible( this );
}
else
{
#ifdef __WXMAC__
if ( m_eventLoop )
m_eventLoop->Exit( GetReturnCode() ); // Needed for APP-MODAL dlgs on OSX
#endif
ret = wxDialog::Show( show );
SaveControlState();
focusParentCanvas();
}
return ret;
}
void DIALOG_SHIM::resetSize()
{
if( COMMON_SETTINGS* settings = Pgm().GetCommonSettings() )
{
std::string key = m_hash_key.empty() ? getDialogKeyFromTitle( GetTitle() ) : m_hash_key;
auto dlgIt = settings->CsInternals().m_dialogControlValues.find( key );
if( dlgIt == settings->CsInternals().m_dialogControlValues.end() )
return;
dlgIt->second.erase( "__geometry" );
}
}
void DIALOG_SHIM::OnSize( wxSizeEvent& aEvent )
{
m_userResized = true;
aEvent.Skip();
}
void DIALOG_SHIM::OnMove( wxMoveEvent& aEvent )
{
m_userPositioned = true;
#ifdef __WXMAC__
if( m_parent )
{
int parentDisplay = wxDisplay::GetFromWindow( m_parent );
int myDisplay = wxDisplay::GetFromWindow( this );
if( parentDisplay != wxNOT_FOUND && myDisplay != wxNOT_FOUND )
{
if( myDisplay != parentDisplay && !m_childReleased )
{
// Moving to different monitor - release child relationship
KIPLATFORM::UI::ReleaseChildWindow( this );
m_childReleased = true;
}
else if( myDisplay == parentDisplay && m_childReleased )
{
// Back on same monitor - restore child relationship
KIPLATFORM::UI::ReparentModal( this );
m_childReleased = false;
}
}
}
#endif
aEvent.Skip();
}
bool DIALOG_SHIM::Enable( bool enable )
{
// so we can do logging of this state change:
return wxDialog::Enable( enable );
}
std::string DIALOG_SHIM::generateKey( const wxWindow* aWin ) const
{
auto getSiblingIndex =
[]( const wxWindow* parent, const wxWindow* child )
{
wxString childClass = child->GetClassInfo()->GetClassName();
int index = 0;
for( const wxWindow* sibling : parent->GetChildren() )
{
if( sibling->GetClassInfo()->GetClassName() != childClass )
continue;
if( sibling == child )
break;
index++;
}
return index;
};
auto makeKey =
[&]( const wxWindow* window )
{
std::string key = wxString( window->GetClassInfo()->GetClassName() ).ToStdString();
if( window->GetParent() )
key += "_" + std::to_string( getSiblingIndex( window->GetParent(), window ) );
return key;
};
std::string key = makeKey( aWin );
for( const wxWindow* parent = aWin->GetParent(); parent && parent != this; parent = parent->GetParent() )
key = makeKey( parent ) + key;
return key;
}
void DIALOG_SHIM::SaveControlState()
{
COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
if( !settings )
return;
std::string dialogKey = m_hash_key.empty() ? getDialogKeyFromTitle( GetTitle() ) : m_hash_key;
std::map<std::string, nlohmann::json>& dlgMap = settings->CsInternals().m_dialogControlValues[ dialogKey ];
wxPoint pos = GetPosition();
wxSize dipSize = ToDIP( GetSize() );
nlohmann::json geom;
geom[ "x" ] = pos.x;
geom[ "y" ] = pos.y;
geom[ "w" ] = dipSize.x;
geom[ "h" ] = dipSize.y;
dlgMap[ "__geometry" ] = geom;
std::function<void( wxWindow* )> saveFn =
[&]( wxWindow* win )
{
if( PROPERTY_HOLDER* props = PROPERTY_HOLDER::SafeCast( win->GetClientData() ) )
{
if( !props->GetPropertyOr( "persist", false ) )
return;
}
std::string key = generateKey( win );
if( !key.empty() )
{
if( m_unitBinders.contains( win ) && !m_unitBinders[ win ]->UnitsInvariant() )
{
dlgMap[ key ] = m_unitBinders[ win ]->GetValue();
}
else if( wxComboBox* combo = dynamic_cast<wxComboBox*>( win ) )
{
dlgMap[ key ] = combo->GetValue();
}
else if( wxOwnerDrawnComboBox* od_combo = dynamic_cast<wxOwnerDrawnComboBox*>( win ) )
{
dlgMap[ key ] = od_combo->GetSelection();
}
else if( wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( win ) )
{
dlgMap[ key ] = textEntry->GetValue();
}
else if( wxChoice* choice = dynamic_cast<wxChoice*>( win ) )
{
dlgMap[ key ] = choice->GetSelection();
}
else if( wxCheckBox* check = dynamic_cast<wxCheckBox*>( win ) )
{
dlgMap[ key ] = check->GetValue();
}
else if( wxSpinCtrl* spin = dynamic_cast<wxSpinCtrl*>( win ) )
{
dlgMap[ key ] = spin->GetValue();
}
else if( wxRadioButton* radio = dynamic_cast<wxRadioButton*>( win ) )
{
dlgMap[ key ] = radio->GetValue();
}
else if( wxRadioBox* radioBox = dynamic_cast<wxRadioBox*>( win ) )
{
dlgMap[ key ] = radioBox->GetSelection();
}
else if( wxSplitterWindow* splitter = dynamic_cast<wxSplitterWindow*>( win ) )
{
dlgMap[ key ] = splitter->GetSashPosition();
}
else if( wxScrolledWindow* scrolled = dynamic_cast<wxScrolledWindow*>( win ) )
{
dlgMap[ key ] = scrolled->GetScrollPos( wxVERTICAL );
}
else if( wxNotebook* notebook = dynamic_cast<wxNotebook*>( win ) )
{
int index = notebook->GetSelection();
if( index >= 0 && index < (int) notebook->GetPageCount() )
dlgMap[ key ] = notebook->GetPageText( notebook->GetSelection() );
}
else if( wxAuiNotebook* auiNotebook = dynamic_cast<wxAuiNotebook*>( win ) )
{
int index = auiNotebook->GetSelection();
if( index >= 0 && index < (int) auiNotebook->GetPageCount() )
dlgMap[ key ] = auiNotebook->GetPageText( auiNotebook->GetSelection() );
}
else if( WX_GRID* grid = dynamic_cast<WX_GRID*>( win ) )
{
dlgMap[ key ] = grid->GetShownColumnsAsString();
}
}
for( wxWindow* child : win->GetChildren() )
saveFn( child );
};
if( PROPERTY_HOLDER* props = PROPERTY_HOLDER::SafeCast( GetClientData() ) )
{
if( !props->GetPropertyOr( "persist", false ) )
return;
}
for( wxWindow* child : GetChildren() )
saveFn( child );
}
void DIALOG_SHIM::LoadControlState()
{
COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
if( !settings )
return;
std::string dialogKey = m_hash_key.empty() ? getDialogKeyFromTitle( GetTitle() ) : m_hash_key;
auto dlgIt = settings->CsInternals().m_dialogControlValues.find( dialogKey );
if( dlgIt == settings->CsInternals().m_dialogControlValues.end() )
return;
const std::map<std::string, nlohmann::json>& dlgMap = dlgIt->second;
std::function<void( wxWindow* )> loadFn =
[&]( wxWindow* win )
{
if( PROPERTY_HOLDER* props = PROPERTY_HOLDER::SafeCast( win->GetClientData() ) )
{
if( !props->GetPropertyOr( "persist", false ) )
return;
}
std::string key = generateKey( win );
if( !key.empty() )
{
auto it = dlgMap.find( key );
if( it != dlgMap.end() )
{
const nlohmann::json& j = it->second;
if( m_unitBinders.contains( win ) )
{
if( j.is_number_integer() )
{
m_unitBinders[ win ]->ChangeValue( j.get<int>() );
}
else if( j.is_string() )
{
if( wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( win ) )
textEntry->ChangeValue( wxString::FromUTF8( j.get<std::string>().c_str() ) );
}
}
else if( wxComboBox* combo = dynamic_cast<wxComboBox*>( win ) )
{
if( j.is_string() )
combo->SetValue( wxString::FromUTF8( j.get<std::string>().c_str() ) );
}
else if( wxOwnerDrawnComboBox* od_combo = dynamic_cast<wxOwnerDrawnComboBox*>( win ) )
{
if( j.is_number_integer() )
{
int index = j.get<int>();
if( index >= 0 && index < (int) od_combo->GetCount() )
od_combo->SetSelection( index );
}
}
else if( wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( win ) )
{
if( j.is_string() )
textEntry->ChangeValue( wxString::FromUTF8( j.get<std::string>().c_str() ) );
}
else if( wxChoice* choice = dynamic_cast<wxChoice*>( win ) )
{
if( j.is_number_integer() )
{
int index = j.get<int>();
if( index >= 0 && index < (int) choice->GetCount() )
choice->SetSelection( index );
}
}
else if( wxCheckBox* check = dynamic_cast<wxCheckBox*>( win ) )
{
if( j.is_boolean() )
check->SetValue( j.get<bool>() );
}
else if( wxSpinCtrl* spin = dynamic_cast<wxSpinCtrl*>( win ) )
{
if( j.is_number_integer() )
spin->SetValue( j.get<int>() );
}
else if( wxRadioButton* radio = dynamic_cast<wxRadioButton*>( win ) )
{
if( j.is_boolean() )
{
// Only set active radio buttons. Let wxWidgets handle unsetting the inactive
// ones. This prevents all from being unset, which trips up wxWidgets in some
// cases.
if( j.get<bool>() )
radio->SetValue( true );
}
}
else if( wxRadioBox* radioBox = dynamic_cast<wxRadioBox*>( win ) )
{
if( j.is_number_integer() )
{
int index = j.get<int>();
if( index >= 0 && index < (int) radioBox->GetCount() )
radioBox->SetSelection( index );
}
}
else if( wxSplitterWindow* splitter = dynamic_cast<wxSplitterWindow*>( win ) )
{
if( j.is_number_integer() )
splitter->SetSashPosition( j.get<int>() );
}
else if( wxScrolledWindow* scrolled = dynamic_cast<wxScrolledWindow*>( win ) )
{
if( j.is_number_integer() )
scrolled->SetScrollPos( wxVERTICAL, j.get<int>() );
}
else if( wxNotebook* notebook = dynamic_cast<wxNotebook*>( win ) )
{
if( j.is_string() )
{
wxString pageTitle = wxString::FromUTF8( j.get<std::string>().c_str() );
for( int page = 0; page < (int) notebook->GetPageCount(); ++page )
{
if( notebook->GetPageText( page ) == pageTitle )
notebook->ChangeSelection( page );
}
}
}
else if( wxAuiNotebook* auiNotebook = dynamic_cast<wxAuiNotebook*>( win ) )
{
if( j.is_string() )
{
wxString pageTitle = wxString::FromUTF8( j.get<std::string>().c_str() );
for( int page = 0; page < (int) auiNotebook->GetPageCount(); ++page )
{
if( auiNotebook->GetPageText( page ) == pageTitle )
auiNotebook->ChangeSelection( page );
}
}
}
else if( WX_GRID* grid = dynamic_cast<WX_GRID*>( win ) )
{
if( j.is_string() )
grid->ShowHideColumns( wxString::FromUTF8( j.get<std::string>().c_str() ) );
}
}
}
for( wxWindow* child : win->GetChildren() )
loadFn( child );
};
if( PROPERTY_HOLDER* props = PROPERTY_HOLDER::SafeCast( GetClientData() ) )
{
if( !props->GetPropertyOr( "persist", false ) )
return;
}
for( wxWindow* child : GetChildren() )
loadFn( child );
}
void DIALOG_SHIM::OptOut( wxWindow* aWindow )
{
PROPERTY_HOLDER* props = new PROPERTY_HOLDER();
props->SetProperty( "persist", false );
aWindow->SetClientData( props );
}
void DIALOG_SHIM::ExcludeFromControlUndoRedo( wxWindow* aWindow )
{
m_noControlUndoRedo.insert( aWindow );
}
void DIALOG_SHIM::RegisterUnitBinder( UNIT_BINDER* aUnitBinder, wxWindow* aWindow )
{
m_unitBinders[ aWindow ] = aUnitBinder;
}
void DIALOG_SHIM::UnregisterUnitBinder( UNIT_BINDER* aUnitBinder )
{
// Erase by binder identity rather than window key, so that a stale entry whose window has
// already been reused by a newer binder is left untouched.
std::erase_if( m_unitBinders,
[aUnitBinder]( const auto& aEntry )
{
return aEntry.second == aUnitBinder;
} );
}
// Recursive descent doing a SelectAll() in wxTextCtrls.
// MacOS User Interface Guidelines state that when tabbing to a text control all its
// text should be selected. Since wxWidgets fails to implement this, we do it here.
void DIALOG_SHIM::SelectAllInTextCtrls( wxWindowList& children )
{
for( wxWindow* child : children )
{
if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( child ) )
{
m_beforeEditValues[ textCtrl ] = textCtrl->GetValue();
textCtrl->Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( DIALOG_SHIM::onChildSetFocus ),
nullptr, this );
// We don't currently run this on GTK because some window managers don't hide the
// selection in non-active controls, and other window managers do the selection
// automatically anyway.
#if defined( __WXMAC__ ) || defined( __WXMSW__ )
if( !textCtrl->GetStringSelection().IsEmpty() )
{
// Respect an existing selection
}
else if( textCtrl->IsEditable() )
{
textCtrl->SelectAll();
}
#else
ignore_unused( textCtrl );
#endif
}
else if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( child ) )
{
m_beforeEditValues[ scintilla ] = scintilla->GetText();
scintilla->Connect( wxEVT_SET_FOCUS,
wxFocusEventHandler( DIALOG_SHIM::onChildSetFocus ),
nullptr, this );
if( !scintilla->GetSelectedText().IsEmpty() )
{
// Respect an existing selection
}