File: dialog_shim.cpp

package info (click to toggle)
kicad 9.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 770,320 kB
  • sloc: cpp: 961,692; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (833 lines) | stat: -rw-r--r-- 26,236 bytes parent folder | download
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
/*
 * 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, 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 <dialog_shim.h>
#include <core/ignore.h>
#include <kiway_player.h>
#include <kiway.h>
#include <pgm_base.h>
#include <tool/tool_manager.h>
#include <kiplatform/ui.h>

#include <wx/display.h>
#include <wx/evtloop.h>
#include <wx/app.h>
#include <wx/event.h>
#include <wx/grid.h>
#include <wx/bmpbuttn.h>
#include <wx/textctrl.h>
#include <wx/stc/stc.h>

#include <algorithm>

BEGIN_EVENT_TABLE( DIALOG_SHIM, wxDialog )
    EVT_CHAR_HOOK( DIALOG_SHIM::OnCharHook )
END_EVENT_TABLE()


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 )
{
    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 );

#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 );
}


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 );

    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() );

    // 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::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 );
}


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>

static std::unordered_map<std::string, wxRect> class_map;


void DIALOG_SHIM::SetPosition( const wxPoint& aNewPosition )
{
    wxDialog::SetPosition( aNewPosition );

    // Now update the stored position:
    const char* hash_key;

    if( m_hash_key.size() )
    {
        // a special case like EDA_LIST_DIALOG, which has multiple uses.
        hash_key = m_hash_key.c_str();
    }
    else
    {
        hash_key = typeid(*this).name();
    }

    std::unordered_map<std::string, wxRect>::iterator it = class_map.find( hash_key );

    if( it == class_map.end() )
        return;

    wxRect rect = it->second;
    rect.SetPosition( aNewPosition );

    class_map[ hash_key ] = rect;
}


bool DIALOG_SHIM::Show( bool show )
{
    bool        ret;
    const char* hash_key;

    if( m_hash_key.size() )
    {
        // a special case like EDA_LIST_DIALOG, which has multiple uses.
        hash_key = m_hash_key.c_str();
    }
    else
    {
        hash_key = typeid(*this).name();
    }

    // Show or hide the window.  If hiding, save current position and size.
    // If showing, use previous position and size.
    if( show )
    {
#ifndef __WINDOWS__
        wxDialog::Raise();  // Needed on OS X and some other window managers (i.e. Unity)
#endif
        ret = wxDialog::Show( show );

        // classname is key, returns a zeroed-out default wxRect if none existed before.
        wxRect savedDialogRect = class_map[ hash_key ];

        if( savedDialogRect.GetSize().x != 0 && savedDialogRect.GetSize().y != 0 )
        {
            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, savedDialogRect.GetSize().x ),
                         std::max( wxDialog::GetSize().y, savedDialogRect.GetSize().y ),
                         0 );
            }
#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();
        }

        // Be sure that the dialog appears in a visible area
        // (the dialog position might have been stored at the time when it was
        // shown on another display)
        if( wxDisplay::GetFromWindow( this ) == wxNOT_FOUND )
            Centre();
    }
    else
    {
        // Save the dialog's position & size before hiding, using classname as key
        class_map[ hash_key ] = wxRect( wxDialog::GetPosition(), wxDialog::GetSize() );

#ifdef __WXMAC__
        if ( m_eventLoop )
            m_eventLoop->Exit( GetReturnCode() );   // Needed for APP-MODAL dlgs on OSX
#endif

        ret = wxDialog::Show( show );

        if( m_parent )
            m_parent->SetFocus();
    }

    return ret;
}


void DIALOG_SHIM::resetSize()
{
    const char* hash_key;

    if( m_hash_key.size() )
    {
        // a special case like EDA_LIST_DIALOG, which has multiple uses.
        hash_key = m_hash_key.c_str();
    }
    else
    {
        hash_key = typeid(*this).name();
    }

    std::unordered_map<std::string, wxRect>::iterator it = class_map.find( hash_key );

    if( it == class_map.end() )
        return;

    wxRect rect = it->second;
    rect.SetSize( wxSize( 0, 0 ) );
    class_map[ hash_key ] = rect;
}


bool DIALOG_SHIM::Enable( bool enable )
{
    // so we can do logging of this state change:
    return wxDialog::Enable( enable );
}


// 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
            }
            else if( scintilla->GetMarginWidth( 0 ) > 0 )
            {
                // Don't select-all in Custom Rules, etc.
            }
            else if( scintilla->IsEditable() )
            {
                scintilla->SelectAll();
            }
        }
#ifdef __WXMAC__
        // Temp hack for square (looking) buttons on OSX.  Will likely be made redundant
        // by the image store....
        else if( dynamic_cast<wxBitmapButton*>( child ) != nullptr )
        {
            wxSize minSize( 29, 27 );
            wxRect rect = child->GetRect();

            child->ConvertDialogToPixels( minSize );

            rect.Inflate( std::max( 0, minSize.x - rect.GetWidth() ),
                          std::max( 0, minSize.y - rect.GetHeight() ) );

            child->SetMinSize( rect.GetSize() );
            child->SetSize( rect );
        }
#endif
        else
        {
            SelectAllInTextCtrls( child->GetChildren() );
        }
    }
}


void DIALOG_SHIM::OnPaint( wxPaintEvent &event )
{
    if( m_firstPaintEvent )
    {
        KIPLATFORM::UI::FixupCancelButtonCmdKeyCollision( this );

        SelectAllInTextCtrls( GetChildren() );

        if( m_initialFocusTarget )
            KIPLATFORM::UI::ForceFocus( m_initialFocusTarget );
        else
            KIPLATFORM::UI::ForceFocus( this );     // Focus the dialog itself

        m_firstPaintEvent = false;
    }

    event.Skip();
}


void DIALOG_SHIM::OnModify()
{
    if( !GetTitle().StartsWith( wxS( "*" ) ) )
        SetTitle( wxS( "*" ) + GetTitle() );
}


void DIALOG_SHIM::ClearModify()
{
    if( GetTitle().StartsWith( wxS( "*" ) ) )
        SetTitle( GetTitle().AfterFirst( '*' ) );
}

int DIALOG_SHIM::ShowModal()
{
    // Apple in its infinite wisdom will raise a disabled window before even passing
    // us the event, so we have no way to stop it.  Instead, we must set an order on
    // the windows so that the modal will be pushed in front of the disabled
    // window when it is raised.
    KIPLATFORM::UI::ReparentModal( this );

    // Call the base class ShowModal() method
    return wxDialog::ShowModal();
}

/*
    Quasi-Modal Mode Explained:

    The gtk calls in wxDialog::ShowModal() cause event routing problems if that
    modal dialog then tries to use KIWAY_PLAYER::ShowModal().  The latter shows up
    and mostly works but does not respond to the window decoration close button.
    There is no way to get around this without reversing the gtk calls temporarily.

    Quasi-Modal mode is our own almost modal mode which disables only the parent
    of the DIALOG_SHIM, leaving other frames operable and while staying captured in the
    nested event loop.  This avoids the gtk calls and leaves event routing pure
    and sufficient to operate the KIWAY_PLAYER::ShowModal() properly.  When using
    ShowQuasiModal() you have to use EndQuasiModal() in your dialogs and not
    EndModal().  There is also IsQuasiModal() but its value can only be true
    when the nested event loop is active.  Do not mix the modal and quasi-modal
    functions.  Use one set or the other.

    You might find this behavior preferable over a pure modal mode, and it was said
    that only the Mac has this natively, but now other platforms have something
    similar.  You CAN use it anywhere for any dialog.  But you MUST use it when
    you want to use KIWAY_PLAYER::ShowModal() from a dialog event.
*/

int DIALOG_SHIM::ShowQuasiModal()
{
    NULLER raii_nuller( (void*&) m_qmodal_loop );

    // release the mouse if it's currently captured as the window having it
    // will be disabled when this dialog is shown -- but will still keep the
    // capture making it impossible to do anything in the modal dialog itself
    wxWindow* win = wxWindow::GetCapture();
    if( win )
        win->ReleaseMouse();

    // Get the optimal parent
    wxWindow* parent = GetParentForModalDialog( GetParent(), GetWindowStyle() );

    wxASSERT_MSG( !m_qmodal_parent_disabler, wxT( "Caller using ShowQuasiModal() twice on same "
                                                  "window?" ) );

    // quasi-modal: disable only my "optimal" parent
    m_qmodal_parent_disabler = new WINDOW_DISABLER( parent );

    // Apple in its infinite wisdom will raise a disabled window before even passing
    // us the event, so we have no way to stop it.  Instead, we must set an order on
    // the windows so that the quasi-modal will be pushed in front of the disabled
    // window when it is raised.
    KIPLATFORM::UI::ReparentModal( this );

    Show( true );

    m_qmodal_showing = true;

    wxGUIEventLoop event_loop;

    m_qmodal_loop = &event_loop;

    event_loop.Run();

    m_qmodal_showing = false;

    if( parent )
        parent->SetFocus();

    return GetReturnCode();
}


void DIALOG_SHIM::PrepareForModalSubDialog()
{
    if( m_qmodal_parent_disabler )
        m_qmodal_parent_disabler->SuspendForTrueModal();
}


void DIALOG_SHIM::CleanupAfterModalSubDialog()
{
    if( m_qmodal_parent_disabler )
        m_qmodal_parent_disabler->ResumeAfterTrueModal();
}


void DIALOG_SHIM::EndQuasiModal( int retCode )
{
    // Hook up validator and transfer data from controls handling so quasi-modal dialogs
    // handle validation in the same way as other dialogs.
    if( ( retCode == wxID_OK ) && ( !Validate() || !TransferDataFromWindow() ) )
        return;

    SetReturnCode( retCode );

    if( !IsQuasiModal() )
    {
        wxFAIL_MSG( wxT( "Either DIALOG_SHIM::EndQuasiModal was called twice, or ShowQuasiModal"
                         "wasn't called" ) );
        return;
    }

    TearDownQuasiModal();

    if( m_qmodal_loop )
    {
        if( m_qmodal_loop->IsRunning() )
            m_qmodal_loop->Exit( 0 );
        else
            m_qmodal_loop->ScheduleExit( 0 );
    }

    delete m_qmodal_parent_disabler;
    m_qmodal_parent_disabler = nullptr;

    Show( false );
}


void DIALOG_SHIM::OnCloseWindow( wxCloseEvent& aEvent )
{
    if( IsQuasiModal() )
    {
        EndQuasiModal( wxID_CANCEL );
        return;
    }

    // This is mandatory to allow wxDialogBase::OnCloseWindow() to be called.
    aEvent.Skip();
}


void DIALOG_SHIM::OnButton( wxCommandEvent& aEvent )
{
    const int id = aEvent.GetId();

    if( IsQuasiModal() )
    {
        if( id == GetAffirmativeId() )
        {
            EndQuasiModal( id );
        }
        else if( id == wxID_APPLY )
        {
            // Dialogs that provide Apply buttons should make sure data is valid before
            // allowing a transfer, as there is no other way to indicate failure
            // (i.e. the dialog can't refuse to close as it might with OK, because it
            // isn't closing anyway)
            if( Validate() )
            {
                ignore_unused( TransferDataFromWindow() );
            }
        }
        else if( id == wxID_CANCEL )
        {
            EndQuasiModal( wxID_CANCEL );
        }
        else // not a standard button
        {
            aEvent.Skip();
        }

        return;
    }

    // This is mandatory to allow wxDialogBase::OnButton() to be called.
    aEvent.Skip();
}


void DIALOG_SHIM::onChildSetFocus( wxFocusEvent& aEvent )
{
    // When setting focus to a text control reset the before-edit value.

    if( !m_isClosing )
    {
        if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aEvent.GetEventObject() ) )
            m_beforeEditValues[ textCtrl ] = textCtrl->GetValue();
        else if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aEvent.GetEventObject() ) )
            m_beforeEditValues[ scintilla ] = scintilla->GetText();
    }

    aEvent.Skip();
}


void DIALOG_SHIM::OnCharHook( wxKeyEvent& aEvt )
{
    if( aEvt.GetKeyCode() == 'U' && aEvt.GetModifiers() == wxMOD_CONTROL )
    {
        if( m_parentFrame )
        {
            m_parentFrame->ToggleUserUnits();
            return;
        }
    }
    // shift-return (Mac default) or Ctrl-Return (GTK) for new line input
    else if( ( aEvt.GetKeyCode() == WXK_RETURN || aEvt.GetKeyCode() == WXK_NUMPAD_ENTER ) && aEvt.ShiftDown() )
    {
        wxObject* eventSource = aEvt.GetEventObject();

        if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( eventSource ) )
        {
            // If the text control is not multi-line, we want to close the dialog
            if( !textCtrl->IsMultiLine() )
            {
                wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
                return;
            }

#if defined( __WXMAC__ ) || defined( __WXMSW__ )
            wxString eol = "\r\n";
#else
            wxString eol = "\n";
#endif

            long pos = textCtrl->GetInsertionPoint();
            textCtrl->WriteText( eol );
            textCtrl->SetInsertionPoint( pos + eol.length() );
            return;
        }
        else if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( eventSource ) )
        {
            wxString eol = "\n";
            switch( scintilla->GetEOLMode() )
            {
            case wxSTC_EOL_CRLF: eol = "\r\n"; break;
            case wxSTC_EOL_CR: eol = "\r"; break;
            case wxSTC_EOL_LF: eol = "\n"; break;
            }

            long pos = scintilla->GetCurrentPos();
            scintilla->InsertText( pos, eol );
            scintilla->GotoPos( pos + eol.length() );
            return;
        }
        return;
    }
    // command-return (Mac default) or Ctrl-Return (GTK) for OK
    else if( ( aEvt.GetKeyCode() == WXK_RETURN || aEvt.GetKeyCode() == WXK_NUMPAD_ENTER ) && aEvt.ControlDown() )
    {
        wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
        return;
    }
    else if( aEvt.GetKeyCode() == WXK_TAB && !aEvt.ControlDown() )
    {
        wxWindow* currentWindow = wxWindow::FindFocus();
        int       currentIdx = -1;
        int       delta = aEvt.ShiftDown() ? -1 : 1;

        auto advance =
                [&]( int& idx )
                {
                    // Wrap-around modulus
                    int size = (int) m_tabOrder.size();
                    idx = ( ( idx + delta ) % size + size ) % size;
                };

        for( size_t i = 0; i < m_tabOrder.size(); ++i )
        {
            if( m_tabOrder[i] == currentWindow )
            {
                currentIdx = (int) i;
                break;
            }
        }

        if( currentIdx >= 0 )
        {
            advance( currentIdx );

            //todo: We don't currently have non-textentry dialog boxes but this will break if
            // we add them.
#ifdef __APPLE__
            while( dynamic_cast<wxTextEntry*>( m_tabOrder[ currentIdx ] ) == nullptr )
                advance( currentIdx );
#endif

            m_tabOrder[ currentIdx ]->SetFocus();
            return;
        }
    }
    else if( aEvt.GetKeyCode() == WXK_ESCAPE )
    {
        wxObject* eventSource = aEvt.GetEventObject();

        if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( eventSource ) )
        {
            // First escape after an edit cancels edit
            if( textCtrl->GetValue() != m_beforeEditValues[ textCtrl ] )
            {
                textCtrl->SetValue( m_beforeEditValues[ textCtrl ] );
                textCtrl->SelectAll();
                return;
            }
        }
        else if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( eventSource ) )
        {
            // First escape after an edit cancels edit
            if( scintilla->GetText() != m_beforeEditValues[ scintilla ] )
            {
                scintilla->SetText( m_beforeEditValues[ scintilla ] );
                scintilla->SelectAll();
                return;
            }
        }
    }

    aEvt.Skip();
}


static void recursiveDescent( wxSizer* aSizer, std::map<int, wxString>& aLabels )
{
    wxStdDialogButtonSizer* sdbSizer = dynamic_cast<wxStdDialogButtonSizer*>( aSizer );

    auto setupButton =
            [&]( wxButton* aButton )
            {
                if( aLabels.count( aButton->GetId() ) > 0 )
                {
                    aButton->SetLabel( aLabels[ aButton->GetId() ] );
                }
                else
                {
                    // wxWidgets has an uneven track record when the language is changed on
                    // the fly so we set them even when they don't appear in the label map
                    switch( aButton->GetId() )
                    {
                    case wxID_OK:           aButton->SetLabel( _( "&OK" ) );     break;
                    case wxID_CANCEL:       aButton->SetLabel( _( "&Cancel" ) ); break;
                    case wxID_YES:          aButton->SetLabel( _( "&Yes" ) );    break;
                    case wxID_NO:           aButton->SetLabel( _( "&No" ) );     break;
                    case wxID_APPLY:        aButton->SetLabel( _( "&Apply" ) );  break;
                    case wxID_SAVE:         aButton->SetLabel( _( "&Save" ) );   break;
                    case wxID_HELP:         aButton->SetLabel( _( "&Help" ) );   break;
                    case wxID_CONTEXT_HELP: aButton->SetLabel( _( "&Help" ) );   break;
                    }
                }
            };

    if( sdbSizer )
    {
        if( sdbSizer->GetAffirmativeButton() )
            setupButton( sdbSizer->GetAffirmativeButton() );

        if( sdbSizer->GetApplyButton() )
            setupButton( sdbSizer->GetApplyButton() );

        if( sdbSizer->GetNegativeButton() )
            setupButton( sdbSizer->GetNegativeButton() );

        if( sdbSizer->GetCancelButton() )
            setupButton( sdbSizer->GetCancelButton() );

        if( sdbSizer->GetHelpButton() )
            setupButton( sdbSizer->GetHelpButton() );

        sdbSizer->Layout();

        if( sdbSizer->GetAffirmativeButton() )
            sdbSizer->GetAffirmativeButton()->SetDefault();
    }

    for( wxSizerItem* item : aSizer->GetChildren() )
    {
        if( item->GetSizer() )
            recursiveDescent( item->GetSizer(), aLabels );
    }
}


void DIALOG_SHIM::SetupStandardButtons( std::map<int, wxString> aLabels )
{
    recursiveDescent( GetSizer(), aLabels );
}