Implement ESC processing for choose footprint dialog.

Also steals copy processing for details pane from symbol chooser.
This commit is contained in:
Jeff Young 2023-12-10 18:11:17 +00:00
parent 518be57516
commit 3efc191aa9
4 changed files with 88 additions and 13 deletions

View File

@ -42,10 +42,15 @@ DIALOG_FOOTPRINT_CHOOSER::DIALOG_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aParent,
{
return true;
},
// Close handler
// Accept handler
[this]()
{
EndModal( wxID_OK );
},
// Escape handler
[this]()
{
EndModal( wxID_CANCEL );
} );
sizer->Add( m_chooserPanel, 1, wxEXPAND, 5 );

View File

@ -99,11 +99,16 @@ FOOTPRINT_CHOOSER_FRAME::FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aPare
{
return filterFootprint( aNode );
},
// Close handler
// Accept handler
[this]()
{
wxCommandEvent dummy;
OnOK( dummy );
},
// Escape handler
[this]()
{
DismissModal( false );
} );
sizer->Add( m_chooserPanel, 1, wxEXPAND | wxBOTTOM, 2 );

View File

@ -25,6 +25,7 @@
#include <widgets/panel_footprint_chooser.h>
#include <wx/button.h>
#include <wx/clipbrd.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/splitter.h>
@ -45,13 +46,15 @@
PANEL_FOOTPRINT_CHOOSER::PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopLevelWindow* aParent,
const wxArrayString& aFootprintHistoryList,
std::function<bool( LIB_TREE_NODE& )> aFilter,
std::function<void()> aCloseHandler ) :
std::function<void()> aAcceptHandler,
std::function<void()> aEscapeHandler ) :
wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
m_hsplitter( nullptr ),
m_vsplitter( nullptr ),
m_frame( aFrame ),
m_filter( std::move( aFilter ) ),
m_closeHandler( std::move( aCloseHandler ) )
m_acceptHandler( std::move( aAcceptHandler ) ),
m_escapeHandler( std::move( aEscapeHandler ) )
{
FP_LIB_TABLE* fpTable = PROJECT_PCB::PcbFootprintLibs( &aFrame->Prj() );
@ -97,8 +100,7 @@ PANEL_FOOTPRINT_CHOOSER::PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopL
// Construct the actual panel
//
wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
HTML_WINDOW* details = nullptr;
wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
m_vsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
@ -114,8 +116,8 @@ PANEL_FOOTPRINT_CHOOSER::PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopL
auto detailsSizer = new wxBoxSizer( wxVERTICAL );
detailsPanel->SetSizer( detailsSizer );
details = new HTML_WINDOW( detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize );
detailsSizer->Add( details, 1, wxEXPAND, 5 );
m_details = new HTML_WINDOW( detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize );
detailsSizer->Add( m_details, 1, wxEXPAND, 5 );
detailsPanel->Layout();
detailsSizer->Fit( detailsPanel );
@ -126,7 +128,7 @@ PANEL_FOOTPRINT_CHOOSER::PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopL
sizer->Add( m_vsplitter, 1, wxEXPAND, 5 );
m_tree = new LIB_TREE( m_hsplitter, wxT( "footprints" ), fpTable, m_adapter,
LIB_TREE::FLAGS::ALL_WIDGETS, details );
LIB_TREE::FLAGS::ALL_WIDGETS, m_details );
m_hsplitter->SetSashGravity( 0.8 );
m_hsplitter->SetMinimumPaneSize( 20 );
@ -154,6 +156,36 @@ PANEL_FOOTPRINT_CHOOSER::PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopL
Bind( EVT_LIBITEM_SELECTED, &PANEL_FOOTPRINT_CHOOSER::onFootprintSelected, this );
Bind( EVT_LIBITEM_CHOSEN, &PANEL_FOOTPRINT_CHOOSER::onFootprintChosen, this );
m_details->Connect( wxEVT_CHAR_HOOK,
wxKeyEventHandler( PANEL_FOOTPRINT_CHOOSER::OnDetailsCharHook ),
nullptr, this );
Bind( wxEVT_CHAR_HOOK,
[&]( wxKeyEvent& aEvent )
{
if( aEvent.GetKeyCode() == WXK_ESCAPE )
{
wxObject* eventSource = aEvent.GetEventObject();
if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( eventSource ) )
{
// First escape cancels search string value
if( textCtrl->GetValue() == m_tree->GetSearchString()
&& !m_tree->GetSearchString().IsEmpty() )
{
m_tree->SetSearchString( wxEmptyString );
return;
}
}
m_escapeHandler();
}
else
{
aEvent.Skip();
}
} );
Layout();
}
@ -164,6 +196,10 @@ PANEL_FOOTPRINT_CHOOSER::~PANEL_FOOTPRINT_CHOOSER()
Unbind( EVT_LIBITEM_SELECTED, &PANEL_FOOTPRINT_CHOOSER::onFootprintSelected, this );
Unbind( EVT_LIBITEM_CHOSEN, &PANEL_FOOTPRINT_CHOOSER::onFootprintChosen, this );
m_details->Disconnect( wxEVT_CHAR_HOOK,
wxKeyEventHandler( PANEL_FOOTPRINT_CHOOSER::OnDetailsCharHook ),
nullptr, this );
// I am not sure the following two lines are necessary, but they will not hurt anyone
m_dbl_click_timer->Stop();
delete m_dbl_click_timer;
@ -259,7 +295,7 @@ void PANEL_FOOTPRINT_CHOOSER::onCloseTimer( wxTimerEvent& aEvent )
}
else
{
m_closeHandler();
m_acceptHandler();
}
}
@ -299,3 +335,27 @@ void PANEL_FOOTPRINT_CHOOSER::onFootprintChosen( wxCommandEvent& aEvent )
m_dbl_click_timer->StartOnce( PANEL_FOOTPRINT_CHOOSER::DblClickDelay );
}
}
void PANEL_FOOTPRINT_CHOOSER::OnDetailsCharHook( wxKeyEvent& e )
{
if( m_details && e.GetKeyCode() == 'C' && e.ControlDown() &&
!e.AltDown() && !e.ShiftDown() && !e.MetaDown() )
{
wxString txt = m_details->SelectionToText();
wxLogNull doNotLog; // disable logging of failed clipboard actions
if( wxTheClipboard->Open() )
{
wxTheClipboard->SetData( new wxTextDataObject( txt ) );
wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
wxTheClipboard->Close();
}
}
else
{
e.Skip();
}
}

View File

@ -43,12 +43,14 @@ public:
*
* @param aFrame the parent frame (usually a PCB_EDIT_FRAME or FOOTPRINT_CHOOSER_FRAME)
* @param aParent the parent window (usually a DIALOG_SHIM or FOOTPRINT_CHOOSER_FRAME)
* @param aCloseHandler a handler to be called on double-click of a footprint
* @param aAcceptHandler a handler to be called on double-click of a footprint
* @param aEscapeHandler a handler to be called on <ESC>
*/
PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopLevelWindow* aParent,
const wxArrayString& aFootprintHistoryList,
std::function<bool( LIB_TREE_NODE& )> aFilter,
std::function<void()> aCloseHandler );
std::function<void()> aAcceptHandler,
std::function<void()> aEscapeHandler );
~PANEL_FOOTPRINT_CHOOSER();
@ -72,6 +74,7 @@ public:
protected:
static constexpr int DblClickDelay = 100; // milliseconds
void OnDetailsCharHook( wxKeyEvent& aEvt );
void onCloseTimer( wxTimerEvent& aEvent );
void onFootprintSelected( wxCommandEvent& aEvent );
@ -93,10 +96,12 @@ protected:
FOOTPRINT_PREVIEW_WIDGET* m_preview_ctrl;
LIB_TREE* m_tree;
HTML_WINDOW* m_details;
PCB_BASE_FRAME* m_frame;
std::function<bool( LIB_TREE_NODE& )> m_filter;
std::function<void()> m_closeHandler;
std::function<void()> m_acceptHandler;
std::function<void()> m_escapeHandler;
};
#endif /* PANEL_FOOTPRINT_CHOOSER_H */