Respect readOnly cells in GRID_TRICKS.

Also fixes setting colors in the signals grid as it allows us to make
it editable again.

Fixes https://gitlab.com/kicad/code/kicad/issues/14079
This commit is contained in:
Jeff Young 2023-02-25 10:58:05 +00:00
parent bb62c21abc
commit ee956673b1
4 changed files with 73 additions and 25 deletions

View File

@ -86,13 +86,35 @@ void GRID_TRICKS::init()
}
bool GRID_TRICKS::toggleCell( int aRow, int aCol, bool aPreserveSelection )
bool GRID_TRICKS::isTextEntry( int aRow, int aCol )
{
wxGridCellEditor* editor = m_grid->GetCellEditor( aRow, aCol );
bool retval = ( dynamic_cast<wxTextEntry*>( editor ) );
editor->DecRef();
return retval;
}
bool GRID_TRICKS::isCheckbox( int aRow, int aCol )
{
wxGridCellRenderer* renderer = m_grid->GetCellRenderer( aRow, aCol );
bool isCheckbox = ( dynamic_cast<wxGridCellBoolRenderer*>( renderer ) );
renderer->DecRef();
bool retval = ( dynamic_cast<wxGridCellBoolRenderer*>( renderer ) );
if( isCheckbox )
renderer->DecRef();
return retval;
}
bool GRID_TRICKS::isReadOnly( int aRow, int aCol )
{
return !m_grid->IsEditable() || m_grid->IsReadOnly( aRow, aCol );
}
bool GRID_TRICKS::toggleCell( int aRow, int aCol, bool aPreserveSelection )
{
if( isCheckbox( aRow, aCol ) )
{
if( !aPreserveSelection )
{
@ -135,7 +157,7 @@ bool GRID_TRICKS::showEditor( int aRow, int aCol )
if( m_grid->GetGridCursorRow() != aRow || m_grid->GetGridCursorCol() != aCol )
m_grid->SetGridCursor( aRow, aCol );
if( m_grid->IsEditable() && !m_grid->IsReadOnly( aRow, aCol ) )
if( !isReadOnly( aRow, aCol ) )
{
m_grid->ClearSelection();
@ -328,7 +350,7 @@ void GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
_( "Copy selected cells to clipboard" ) );
menu.Append( GRIDTRICKS_ID_PASTE, _( "Paste" ) + "\tCtrl+V",
_( "Paste clipboard cells to matrix at current cell" ) );
menu.Append( GRIDTRICKS_ID_DELETE, _( "Delete" ) + "\tDel", _( "Delete selected cells" ) );
menu.Append( GRIDTRICKS_ID_DELETE, _( "Delete" ) + "\tDel", _( "Clear contents of selected cells" ) );
menu.Append( GRIDTRICKS_ID_SELECT, _( "Select All" ) + "\tCtrl+A", _( "Select all cells" ) );
getSelectedArea();
@ -340,25 +362,43 @@ void GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
menu.Enable( GRIDTRICKS_ID_COPY, false );
menu.Enable( GRIDTRICKS_ID_DELETE, false );
}
else if( !m_grid->IsEditable() )
{
menu.Enable( GRIDTRICKS_ID_CUT, false );
menu.Enable( GRIDTRICKS_ID_DELETE, false );
}
menu.Enable( GRIDTRICKS_ID_CUT, false );
menu.Enable( GRIDTRICKS_ID_DELETE, false );
menu.Enable( GRIDTRICKS_ID_PASTE, false );
wxLogNull doNotLog; // disable logging of failed clipboard actions
auto anyCellsWritable =
[&]()
{
for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
{
for( int col = m_sel_col_start; col < m_sel_col_start + m_sel_col_count; ++col )
{
if( !isReadOnly( row, col ) && isTextEntry( row, col ) )
return true;
}
}
if( m_grid->IsEditable() && wxTheClipboard->Open() )
return false;
};
if( anyCellsWritable() )
{
if( wxTheClipboard->IsSupported( wxDF_TEXT )
|| wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
{
menu.Enable( GRIDTRICKS_ID_PASTE, true );
}
menu.Enable( GRIDTRICKS_ID_CUT, true );
menu.Enable( GRIDTRICKS_ID_DELETE, true );
wxTheClipboard->Close();
wxLogNull doNotLog; // disable logging of failed clipboard actions
if( wxTheClipboard->Open() )
{
if( wxTheClipboard->IsSupported( wxDF_TEXT )
|| wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
{
menu.Enable( GRIDTRICKS_ID_PASTE, true );
}
wxTheClipboard->Close();
}
}
m_grid->PopupMenu( &menu );
@ -730,7 +770,9 @@ void GRID_TRICKS::paste_text( const wxString& cb_text )
wxString cellTxt = cols.GetNextToken();
if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) )
// Allow paste to anything that can take a string, including things like color
// swatches and checkboxes
if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) && !isReadOnly( row, col ) )
{
tbl->SetValue( row, col, cellTxt );
@ -765,9 +807,11 @@ void GRID_TRICKS::cutcopy( bool doCopy, bool doDelete )
if( col < m_sel_col_start + m_sel_col_count - 1 ) // that was not last column
txt += COL_SEP;
if( doDelete && m_grid->IsEditable() )
if( doDelete )
{
if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) )
// Do NOT allow clear of things that can take strings but aren't textEntries
// (ie: color swatches, textboxes, etc.).
if( isTextEntry( row, col ) && !isReadOnly( row, col ) )
tbl->SetValue( row, col, wxEmptyString );
}
}

View File

@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version 3.10.1-88b0f50)
// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
@ -102,7 +102,7 @@ SIM_PLOT_FRAME_BASE::SIM_PLOT_FRAME_BASE( wxWindow* parent, wxWindowID id, const
// Grid
m_signalsGrid->CreateGrid( 0, 5 );
m_signalsGrid->EnableEditing( false );
m_signalsGrid->EnableEditing( true );
m_signalsGrid->EnableGridLines( true );
m_signalsGrid->EnableDragGridSize( false );
m_signalsGrid->SetMargins( 0, 0 );

View File

@ -843,7 +843,7 @@
<property name="drag_col_size">1</property>
<property name="drag_grid_size">0</property>
<property name="drag_row_size">1</property>
<property name="editing">0</property>
<property name="editing">1</property>
<property name="enabled">1</property>
<property name="fg"></property>
<property name="floatable">1</property>

View File

@ -110,6 +110,10 @@ protected:
virtual void showPopupMenu( wxMenu& menu, wxGridEvent& aEvent );
virtual void doPopupSelection( wxCommandEvent& event );
bool isTextEntry( int aRow, int aCol );
bool isCheckbox( int aRow, int aCol );
bool isReadOnly( int aRow, int aCol );
bool toggleCell( int aRow, int aCol, bool aPreserveSelection = false );
bool showEditor( int aRow, int aCol );