diff --git a/common/lib_table_grid_tricks.cpp b/common/lib_table_grid_tricks.cpp index 11da165deb..2a60f68e57 100644 --- a/common/lib_table_grid_tricks.cpp +++ b/common/lib_table_grid_tricks.cpp @@ -19,11 +19,23 @@ #include "lib_table_grid_tricks.h" #include "lib_table_grid.h" +#include +#include LIB_TABLE_GRID_TRICKS::LIB_TABLE_GRID_TRICKS( WX_GRID* aGrid ) : GRID_TRICKS( aGrid ) { + m_grid->Disconnect( wxEVT_CHAR_HOOK ); + m_grid->Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( LIB_TABLE_GRID_TRICKS::onCharHook ), nullptr, this ); +} + +LIB_TABLE_GRID_TRICKS::LIB_TABLE_GRID_TRICKS( WX_GRID* aGrid, + std::function aAddHandler ) : + GRID_TRICKS( aGrid, aAddHandler ) +{ + m_grid->Disconnect( wxEVT_CHAR_HOOK ); + m_grid->Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( LIB_TABLE_GRID_TRICKS::onCharHook ), nullptr, this ); } @@ -134,6 +146,61 @@ void LIB_TABLE_GRID_TRICKS::doPopupSelection( wxCommandEvent& event ) GRID_TRICKS::doPopupSelection( event ); } } +void LIB_TABLE_GRID_TRICKS::onCharHook( wxKeyEvent& ev ) +{ + if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' && m_grid->IsCellEditControlShown() ) + { + wxLogNull doNotLog; + + if( wxTheClipboard->Open() ) + { + if( wxTheClipboard->IsSupported( wxDF_TEXT ) || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) ) + { + wxTextDataObject data; + wxTheClipboard->GetData( data ); + + wxString text = data.GetText(); + + if( !text.Contains( '\t' ) && text.Contains( ',' ) ) + text.Replace( ',', '\t' ); + + if( text.Contains( '\t' ) || text.Contains( '\n' ) || text.Contains( '\r' ) ) + { + m_grid->CancelPendingChanges(); + int row = m_grid->GetGridCursorRow(); + + // Check if the current row already has data (has a nickname) + wxGridTableBase* table = m_grid->GetTable(); + if( table && row >= 0 && row < table->GetNumberRows() ) + { + // Check if the row has a nickname (indicating it has existing data) + wxString nickname = table->GetValue( row, COL_NICKNAME ); + if( !nickname.IsEmpty() ) + { + // Row already has data, don't allow pasting over it + wxTheClipboard->Close(); + wxBell(); // Provide audio feedback + return; + } + } + + m_grid->ClearSelection(); + m_grid->SelectRow( row ); + m_grid->SetGridCursor( row, 0 ); + getSelectedArea(); + paste_text( text ); + wxTheClipboard->Close(); + m_grid->ForceRefresh(); + return; + } + } + + wxTheClipboard->Close(); + } + } + + GRID_TRICKS::onCharHook( ev ); +} bool LIB_TABLE_GRID_TRICKS::handleDoubleClick( wxGridEvent& aEvent ) diff --git a/eeschema/dialogs/panel_sym_lib_table.cpp b/eeschema/dialogs/panel_sym_lib_table.cpp index f0b5f18190..1a6f801420 100644 --- a/eeschema/dialogs/panel_sym_lib_table.cpp +++ b/eeschema/dialogs/panel_sym_lib_table.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -153,6 +154,13 @@ public: { } + SYMBOL_GRID_TRICKS( DIALOG_EDIT_LIBRARY_TABLES* aParent, WX_GRID* aGrid, + std::function aAddHandler ) : + LIB_TABLE_GRID_TRICKS( aGrid, aAddHandler ), + m_dialog( aParent ) + { + } + protected: DIALOG_EDIT_LIBRARY_TABLES* m_dialog; @@ -224,8 +232,21 @@ protected: } else { - // paste spreadsheet formatted text. - GRID_TRICKS::paste_text( cb_text ); + wxString text = cb_text; + + if( !text.Contains( '\t' ) && text.Contains( ',' ) ) + text.Replace( ',', '\t' ); + + if( text.Contains( '\t' ) ) + { + int row = m_grid->GetGridCursorRow(); + m_grid->ClearSelection(); + m_grid->SelectRow( row ); + m_grid->SetGridCursor( row, 0 ); + getSelectedArea(); + } + + GRID_TRICKS::paste_text( text ); m_grid->AutoSizeColumns( false ); } @@ -250,7 +271,8 @@ void PANEL_SYM_LIB_TABLE::setupGrid( WX_GRID* aGrid ) }; // add Cut, Copy, and Paste to wxGrids - aGrid->PushEventHandler( new SYMBOL_GRID_TRICKS( m_parent, aGrid ) ); + aGrid->PushEventHandler( new SYMBOL_GRID_TRICKS( m_parent, aGrid, + [this]( wxCommandEvent& event ) { appendRowHandler( event ); } ) ); aGrid->SetSelectionMode( wxGrid::wxGridSelectRows ); diff --git a/include/lib_table_grid_tricks.h b/include/lib_table_grid_tricks.h index db83ea3db1..772860458b 100644 --- a/include/lib_table_grid_tricks.h +++ b/include/lib_table_grid_tricks.h @@ -18,6 +18,7 @@ */ #include "grid_tricks.h" +#include class LIB_TABLE_GRID_TRICKS : public GRID_TRICKS { @@ -33,6 +34,7 @@ class LIB_TABLE_GRID_TRICKS : public GRID_TRICKS public: explicit LIB_TABLE_GRID_TRICKS( WX_GRID* aGrid ); + LIB_TABLE_GRID_TRICKS( WX_GRID* aGrid, std::function aAddHandler ); virtual ~LIB_TABLE_GRID_TRICKS(){}; @@ -43,5 +45,7 @@ protected: virtual void optionsEditor( int aRow ) = 0; bool handleDoubleClick( wxGridEvent& aEvent ) override; + void onCharHook( wxKeyEvent& ev ); + virtual bool supportsVisibilityColumn() { return false; } }; diff --git a/pcbnew/dialogs/panel_fp_lib_table.cpp b/pcbnew/dialogs/panel_fp_lib_table.cpp index c1d314e752..fb86433b2a 100644 --- a/pcbnew/dialogs/panel_fp_lib_table.cpp +++ b/pcbnew/dialogs/panel_fp_lib_table.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -147,6 +148,12 @@ public: m_dialog( aParent ) { } + FP_GRID_TRICKS( DIALOG_EDIT_LIBRARY_TABLES* aParent, WX_GRID* aGrid, + std::function aAddHandler ) : + LIB_TABLE_GRID_TRICKS( aGrid, aAddHandler ), + m_dialog( aParent ) + { } + protected: DIALOG_EDIT_LIBRARY_TABLES* m_dialog; @@ -217,8 +224,21 @@ protected: } else { - // paste spreadsheet formatted text. - GRID_TRICKS::paste_text( cb_text ); + wxString text = cb_text; + + if( !text.Contains( '\t' ) && text.Contains( ',' ) ) + text.Replace( ',', '\t' ); + + if( text.Contains( '\t' ) ) + { + int row = m_grid->GetGridCursorRow(); + m_grid->ClearSelection(); + m_grid->SelectRow( row ); + m_grid->SetGridCursor( row, 0 ); + getSelectedArea(); + } + + GRID_TRICKS::paste_text( text ); m_grid->AutoSizeColumns( false ); } @@ -254,7 +274,8 @@ void PANEL_FP_LIB_TABLE::setupGrid( WX_GRID* aGrid ) aGrid->SetRowSize( ii, aGrid->GetDefaultRowSize() + 4 ); // add Cut, Copy, and Paste to wxGrids - aGrid->PushEventHandler( new FP_GRID_TRICKS( m_parent, aGrid ) ); + aGrid->PushEventHandler( new FP_GRID_TRICKS( m_parent, aGrid, + [this]( wxCommandEvent& event ) { appendRowHandler( event ); } ) ); aGrid->SetSelectionMode( wxGrid::wxGridSelectRows );