2013-10-23 18:56:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2022-05-08 11:00:49 +00:00
|
|
|
* Copyright (C) 2012-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-10-23 18:56:03 +00:00
|
|
|
*
|
|
|
|
* 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 <grid_tricks.h>
|
2021-07-28 15:40:35 +00:00
|
|
|
#include <wx/defs.h>
|
|
|
|
#include <wx/event.h>
|
2013-10-23 18:56:03 +00:00
|
|
|
#include <wx/tokenzr.h>
|
|
|
|
#include <wx/clipbrd.h>
|
2021-01-29 19:13:12 +00:00
|
|
|
#include <wx/log.h>
|
2018-09-12 12:59:33 +00:00
|
|
|
#include <widgets/grid_readonly_text_helpers.h>
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2016-05-28 16:46:29 +00:00
|
|
|
|
2021-04-05 13:24:57 +00:00
|
|
|
// It works for table data on clipboard for an Excel spreadsheet,
|
2013-10-23 18:56:03 +00:00
|
|
|
// why not us too for now.
|
|
|
|
#define COL_SEP wxT( '\t' )
|
|
|
|
#define ROW_SEP wxT( '\n' )
|
|
|
|
|
|
|
|
|
2019-03-04 11:02:12 +00:00
|
|
|
GRID_TRICKS::GRID_TRICKS( WX_GRID* aGrid ):
|
2013-10-23 18:56:03 +00:00
|
|
|
m_grid( aGrid )
|
|
|
|
{
|
2015-03-04 19:45:18 +00:00
|
|
|
m_sel_row_start = 0;
|
|
|
|
m_sel_col_start = 0;
|
|
|
|
m_sel_row_count = 0;
|
|
|
|
m_sel_col_count = 0;
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
aGrid->Connect( wxEVT_GRID_CELL_LEFT_CLICK,
|
|
|
|
wxGridEventHandler( GRID_TRICKS::onGridCellLeftClick ), nullptr, this );
|
|
|
|
aGrid->Connect( wxEVT_GRID_CELL_LEFT_DCLICK,
|
|
|
|
wxGridEventHandler( GRID_TRICKS::onGridCellLeftDClick ), nullptr, this );
|
|
|
|
aGrid->Connect( wxEVT_GRID_CELL_RIGHT_CLICK,
|
|
|
|
wxGridEventHandler( GRID_TRICKS::onGridCellRightClick ), nullptr, this );
|
|
|
|
aGrid->Connect( wxEVT_GRID_LABEL_RIGHT_CLICK,
|
|
|
|
wxGridEventHandler( GRID_TRICKS::onGridLabelRightClick ), nullptr, this );
|
|
|
|
aGrid->Connect( wxEVT_GRID_LABEL_LEFT_CLICK,
|
|
|
|
wxGridEventHandler( GRID_TRICKS::onGridLabelLeftClick ), nullptr, this );
|
|
|
|
aGrid->Connect( GRIDTRICKS_FIRST_ID, GRIDTRICKS_LAST_ID, wxEVT_COMMAND_MENU_SELECTED,
|
|
|
|
wxCommandEventHandler( GRID_TRICKS::onPopupSelection ), nullptr, this );
|
2021-10-23 14:48:12 +00:00
|
|
|
aGrid->Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( GRID_TRICKS::onCharHook ), nullptr, this );
|
2021-07-15 19:26:35 +00:00
|
|
|
aGrid->Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( GRID_TRICKS::onKeyDown ), nullptr, this );
|
|
|
|
aGrid->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( GRID_TRICKS::onUpdateUI ),
|
|
|
|
nullptr, this );
|
2021-07-28 15:40:35 +00:00
|
|
|
|
|
|
|
// The handlers that control the tooltips must be on the actual grid window, not the grid
|
|
|
|
aGrid->GetGridWindow()->Connect( wxEVT_MOTION,
|
|
|
|
wxMouseEventHandler( GRID_TRICKS::onGridMotion ), nullptr, this );
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-14 19:27:34 +00:00
|
|
|
bool GRID_TRICKS::toggleCell( int aRow, int aCol, bool aPreserveSelection )
|
2018-03-21 23:25:36 +00:00
|
|
|
{
|
|
|
|
auto renderer = m_grid->GetCellRenderer( aRow, aCol );
|
|
|
|
bool isCheckbox = ( dynamic_cast<wxGridCellBoolRenderer*>( renderer ) != nullptr );
|
|
|
|
renderer->DecRef();
|
|
|
|
|
|
|
|
if( isCheckbox )
|
|
|
|
{
|
2020-02-14 19:27:34 +00:00
|
|
|
if( !aPreserveSelection )
|
|
|
|
m_grid->ClearSelection();
|
|
|
|
|
2019-01-16 13:16:23 +00:00
|
|
|
m_grid->SetGridCursor( aRow, aCol );
|
|
|
|
|
2018-03-21 23:25:36 +00:00
|
|
|
wxGridTableBase* model = m_grid->GetTable();
|
|
|
|
|
|
|
|
if( model->CanGetValueAs( aRow, aCol, wxGRID_VALUE_BOOL )
|
2020-02-14 19:27:34 +00:00
|
|
|
&& model->CanSetValueAs( aRow, aCol, wxGRID_VALUE_BOOL ) )
|
2018-03-21 23:25:36 +00:00
|
|
|
{
|
2020-02-14 19:27:34 +00:00
|
|
|
model->SetValueAsBool( aRow, aCol, !model->GetValueAsBool( aRow, aCol ) );
|
2018-03-21 23:25:36 +00:00
|
|
|
}
|
|
|
|
else // fall back to string processing
|
|
|
|
{
|
2020-02-14 19:27:34 +00:00
|
|
|
if( model->GetValue( aRow, aCol ) == wxT( "1" ) )
|
2018-03-21 23:25:36 +00:00
|
|
|
model->SetValue( aRow, aCol, wxT( "0" ) );
|
|
|
|
else
|
|
|
|
model->SetValue( aRow, aCol, wxT( "1" ) );
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:33:17 +00:00
|
|
|
// Mac needs this for the keyboard events; Linux appears to always need it.
|
|
|
|
m_grid->ForceRefresh();
|
|
|
|
|
2018-03-24 13:11:02 +00:00
|
|
|
// Let any clients know
|
|
|
|
wxGridEvent event( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, aRow, aCol );
|
|
|
|
event.SetString( model->GetValue( aRow, aCol ) );
|
|
|
|
m_grid->GetEventHandler()->ProcessEvent( event );
|
|
|
|
|
2018-03-21 23:25:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-19 16:10:42 +00:00
|
|
|
bool GRID_TRICKS::showEditor( int aRow, int aCol )
|
|
|
|
{
|
2019-03-04 12:25:54 +00:00
|
|
|
if( m_grid->GetGridCursorRow() != aRow || m_grid->GetGridCursorCol() != aCol )
|
2019-03-04 11:02:12 +00:00
|
|
|
m_grid->SetGridCursor( aRow, aCol );
|
2019-01-16 13:16:23 +00:00
|
|
|
|
2018-08-19 16:10:42 +00:00
|
|
|
if( m_grid->IsEditable() && !m_grid->IsReadOnly( aRow, aCol ) )
|
|
|
|
{
|
2019-09-24 17:11:19 +00:00
|
|
|
m_grid->ClearSelection();
|
|
|
|
|
2018-09-12 12:59:33 +00:00
|
|
|
if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
|
2019-03-04 11:02:12 +00:00
|
|
|
{
|
|
|
|
wxArrayInt rows = m_grid->GetSelectedRows();
|
|
|
|
|
2019-03-04 14:11:44 +00:00
|
|
|
if( rows.size() != 1 || rows.Item( 0 ) != aRow )
|
2019-03-04 11:02:12 +00:00
|
|
|
m_grid->SelectRow( aRow );
|
|
|
|
}
|
2018-08-19 16:10:42 +00:00
|
|
|
|
|
|
|
// For several reasons we can't enable the control here. There's the whole
|
|
|
|
// SetInSetFocus() issue/hack in wxWidgets, and there's also wxGrid's MouseUp
|
|
|
|
// handler which doesn't notice it's processing a MouseUp until after it has
|
2019-03-04 11:02:12 +00:00
|
|
|
// disabled the editor yet again. So we re-use wxWidgets' slow-click hack,
|
|
|
|
// which is processed later in the MouseUp handler.
|
|
|
|
//
|
|
|
|
// It should be pointed out that the fact that it's wxWidgets' hack doesn't
|
|
|
|
// make it any less of a hack. Be extra careful with any modifications here.
|
|
|
|
// See, in particular, https://bugs.launchpad.net/kicad/+bug/1817965.
|
|
|
|
m_grid->ShowEditorOnMouseUp();
|
2018-08-19 16:10:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-21 23:25:36 +00:00
|
|
|
void GRID_TRICKS::onGridCellLeftClick( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
int row = aEvent.GetRow();
|
|
|
|
int col = aEvent.GetCol();
|
|
|
|
|
2019-03-04 11:02:12 +00:00
|
|
|
// Don't make users click twice to toggle a checkbox or edit a text cell
|
|
|
|
if( !aEvent.GetModifiers() )
|
2018-08-19 16:10:42 +00:00
|
|
|
{
|
|
|
|
if( toggleCell( row, col ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( showEditor( row, col ) )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aEvent.Skip();
|
2018-03-14 04:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRID_TRICKS::onGridCellLeftDClick( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( !handleDoubleClick( aEvent ) )
|
|
|
|
onGridCellLeftClick( aEvent );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-28 15:40:35 +00:00
|
|
|
void GRID_TRICKS::onGridMotion( wxMouseEvent& aEvent )
|
|
|
|
{
|
|
|
|
// Always skip the event
|
|
|
|
aEvent.Skip();
|
|
|
|
|
|
|
|
wxPoint pt = aEvent.GetPosition();
|
|
|
|
wxPoint pos = m_grid->CalcScrolledPosition( wxPoint( pt.x, pt.y ) );
|
|
|
|
|
|
|
|
int col = m_grid->XToCol( pos.x );
|
|
|
|
int row = m_grid->YToRow( pos.y );
|
|
|
|
|
2021-07-29 22:17:41 +00:00
|
|
|
// Empty tooltip if the cell doesn't exist or the column doesn't have tooltips
|
|
|
|
if( ( col == wxNOT_FOUND ) || ( row == wxNOT_FOUND ) || !m_tooltipEnabled[col] )
|
2021-07-28 15:40:35 +00:00
|
|
|
{
|
|
|
|
m_grid->GetGridWindow()->SetToolTip( "" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the tooltip to the string contained in the cell
|
|
|
|
m_grid->GetGridWindow()->SetToolTip( m_grid->GetCellValue( row, col ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
bool GRID_TRICKS::handleDoubleClick( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
// Double-click processing must be handled by specific sub-classes
|
|
|
|
return false;
|
2018-03-21 23:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-23 18:56:03 +00:00
|
|
|
void GRID_TRICKS::getSelectedArea()
|
|
|
|
{
|
|
|
|
wxGridCellCoordsArray topLeft = m_grid->GetSelectionBlockTopLeft();
|
|
|
|
wxGridCellCoordsArray botRight = m_grid->GetSelectionBlockBottomRight();
|
|
|
|
|
|
|
|
wxArrayInt cols = m_grid->GetSelectedCols();
|
|
|
|
wxArrayInt rows = m_grid->GetSelectedRows();
|
|
|
|
|
|
|
|
if( topLeft.Count() && botRight.Count() )
|
|
|
|
{
|
|
|
|
m_sel_row_start = topLeft[0].GetRow();
|
|
|
|
m_sel_col_start = topLeft[0].GetCol();
|
|
|
|
|
|
|
|
m_sel_row_count = botRight[0].GetRow() - m_sel_row_start + 1;
|
|
|
|
m_sel_col_count = botRight[0].GetCol() - m_sel_col_start + 1;
|
|
|
|
}
|
|
|
|
else if( cols.Count() )
|
|
|
|
{
|
|
|
|
m_sel_col_start = cols[0];
|
|
|
|
m_sel_col_count = cols.Count();
|
|
|
|
m_sel_row_start = 0;
|
|
|
|
m_sel_row_count = m_grid->GetNumberRows();
|
|
|
|
}
|
|
|
|
else if( rows.Count() )
|
|
|
|
{
|
|
|
|
m_sel_col_start = 0;
|
|
|
|
m_sel_col_count = m_grid->GetNumberCols();
|
|
|
|
m_sel_row_start = rows[0];
|
|
|
|
m_sel_row_count = rows.Count();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
m_sel_row_start = m_grid->GetGridCursorRow();
|
|
|
|
m_sel_col_start = m_grid->GetGridCursorCol();
|
|
|
|
m_sel_row_count = m_sel_row_start >= 0 ? 1 : 0;
|
|
|
|
m_sel_col_count = m_sel_col_start >= 0 ? 1 : 0;
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
2018-03-14 04:51:59 +00:00
|
|
|
}
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
|
|
|
|
void GRID_TRICKS::onGridCellRightClick( wxGridEvent& )
|
|
|
|
{
|
|
|
|
wxMenu menu;
|
|
|
|
|
|
|
|
showPopupMenu( menu );
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-10 22:47:50 +00:00
|
|
|
void GRID_TRICKS::onGridLabelLeftClick( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
m_grid->CommitPendingChanges();
|
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
void GRID_TRICKS::onGridLabelRightClick( wxGridEvent& )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
wxMenu menu;
|
|
|
|
|
|
|
|
for( int i = 0; i < m_grid->GetNumberCols(); ++i )
|
|
|
|
{
|
|
|
|
int id = GRIDTRICKS_FIRST_SHOWHIDE + i;
|
|
|
|
menu.AppendCheckItem( id, m_grid->GetColLabelValue( i ) );
|
|
|
|
menu.Check( id, m_grid->IsColShown( i ) );
|
|
|
|
}
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
m_grid->PopupMenu( &menu );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRID_TRICKS::showPopupMenu( wxMenu& menu )
|
|
|
|
{
|
2021-07-15 19:26:35 +00:00
|
|
|
menu.Append( GRIDTRICKS_ID_CUT, _( "Cut" ) + "\tCtrl+X",
|
|
|
|
_( "Clear selected cells placing original contents on clipboard" ) );
|
|
|
|
menu.Append( GRIDTRICKS_ID_COPY, _( "Copy" ) + "\tCtrl+C",
|
|
|
|
_( "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" ) );
|
2020-11-04 01:28:55 +00:00
|
|
|
menu.Append( GRIDTRICKS_ID_SELECT, _( "Select All" ) + "\tCtrl+A", _( "Select all cells" ) );
|
2013-10-23 18:56:03 +00:00
|
|
|
|
|
|
|
getSelectedArea();
|
|
|
|
|
2021-02-25 15:37:18 +00:00
|
|
|
// if nothing is selected, disable cut, copy and delete.
|
2013-10-23 18:56:03 +00:00
|
|
|
if( !m_sel_row_count && !m_sel_col_count )
|
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
menu.Enable( GRIDTRICKS_ID_CUT, false );
|
|
|
|
menu.Enable( GRIDTRICKS_ID_COPY, false );
|
2021-02-25 15:37:18 +00:00
|
|
|
menu.Enable( GRIDTRICKS_ID_DELETE, false );
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
2022-05-08 11:00:49 +00:00
|
|
|
else if( !m_grid->IsEditable() )
|
|
|
|
{
|
|
|
|
menu.Enable( GRIDTRICKS_ID_CUT, false );
|
|
|
|
menu.Enable( GRIDTRICKS_ID_DELETE, false );
|
|
|
|
}
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
menu.Enable( GRIDTRICKS_ID_PASTE, false );
|
|
|
|
|
2021-01-29 19:13:12 +00:00
|
|
|
wxLogNull doNotLog; // disable logging of failed clipboard actions
|
|
|
|
|
2022-05-08 11:00:49 +00:00
|
|
|
if( m_grid->IsEditable() && wxTheClipboard->Open() )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
2022-01-12 09:24:04 +00:00
|
|
|
if( wxTheClipboard->IsSupported( wxDF_TEXT )
|
|
|
|
|| wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
|
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
menu.Enable( GRIDTRICKS_ID_PASTE, true );
|
2022-01-12 09:24:04 +00:00
|
|
|
}
|
2013-10-23 18:56:03 +00:00
|
|
|
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_grid->PopupMenu( &menu );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRID_TRICKS::onPopupSelection( wxCommandEvent& event )
|
2018-03-14 04:51:59 +00:00
|
|
|
{
|
|
|
|
doPopupSelection( event );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
|
|
|
int menu_id = event.GetId();
|
|
|
|
|
|
|
|
// assume getSelectedArea() was called by rightClickPopupMenu() and there's
|
|
|
|
// no way to have gotten here without that having been called.
|
|
|
|
|
|
|
|
switch( menu_id )
|
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
case GRIDTRICKS_ID_CUT:
|
2021-02-25 15:37:18 +00:00
|
|
|
cutcopy( true, true );
|
|
|
|
break;
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
case GRIDTRICKS_ID_COPY:
|
2021-02-25 15:37:18 +00:00
|
|
|
cutcopy( true, false );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRIDTRICKS_ID_DELETE:
|
|
|
|
cutcopy( false, true );
|
2013-10-23 18:56:03 +00:00
|
|
|
break;
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
case GRIDTRICKS_ID_PASTE:
|
2013-10-23 18:56:03 +00:00
|
|
|
paste_clipboard();
|
|
|
|
break;
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
case GRIDTRICKS_ID_SELECT:
|
2013-10-23 18:56:03 +00:00
|
|
|
m_grid->SelectAll();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-03-14 04:51:59 +00:00
|
|
|
if( menu_id >= GRIDTRICKS_FIRST_SHOWHIDE )
|
|
|
|
{
|
|
|
|
int col = menu_id - GRIDTRICKS_FIRST_SHOWHIDE;
|
|
|
|
|
|
|
|
if( m_grid->IsColShown( col ) )
|
|
|
|
m_grid->HideCol( col );
|
|
|
|
else
|
|
|
|
m_grid->ShowCol( col );
|
|
|
|
}
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-23 14:48:12 +00:00
|
|
|
void GRID_TRICKS::onCharHook( wxKeyEvent& ev )
|
|
|
|
{
|
|
|
|
bool handled = false;
|
|
|
|
|
|
|
|
if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' )
|
|
|
|
{
|
|
|
|
if( m_grid->IsCellEditControlShown() && wxTheClipboard->Open() )
|
|
|
|
{
|
2022-01-12 09:24:04 +00:00
|
|
|
if( wxTheClipboard->IsSupported( wxDF_TEXT )
|
|
|
|
|| wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
|
2021-10-23 14:48:12 +00:00
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
wxTheClipboard->GetData( data );
|
|
|
|
|
|
|
|
if( data.GetText().Contains( COL_SEP ) || data.GetText().Contains( ROW_SEP ) )
|
|
|
|
{
|
|
|
|
m_grid->CommitPendingChanges( true /* quiet mode */ );
|
|
|
|
paste_text( data.GetText() );
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
m_grid->ForceRefresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !handled )
|
|
|
|
ev.Skip( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-23 18:56:03 +00:00
|
|
|
void GRID_TRICKS::onKeyDown( wxKeyEvent& ev )
|
|
|
|
{
|
2020-07-14 12:28:55 +00:00
|
|
|
if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'A' )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
|
|
|
m_grid->SelectAll();
|
2018-03-21 23:25:36 +00:00
|
|
|
return;
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
2020-07-14 12:28:55 +00:00
|
|
|
else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'C' )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
|
|
|
getSelectedArea();
|
2021-02-25 15:37:18 +00:00
|
|
|
cutcopy( true, false );
|
2018-03-21 23:25:36 +00:00
|
|
|
return;
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
2020-07-14 12:28:55 +00:00
|
|
|
else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
|
|
|
getSelectedArea();
|
|
|
|
paste_clipboard();
|
2018-03-21 23:25:36 +00:00
|
|
|
return;
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
2020-07-14 12:28:55 +00:00
|
|
|
else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'X' )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
|
|
|
getSelectedArea();
|
2021-02-25 15:37:18 +00:00
|
|
|
cutcopy( true, true );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if( !ev.GetModifiers() && ev.GetKeyCode() == WXK_DELETE )
|
|
|
|
{
|
|
|
|
getSelectedArea();
|
|
|
|
cutcopy( false, true );
|
2018-03-21 23:25:36 +00:00
|
|
|
return;
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
2018-03-14 04:51:59 +00:00
|
|
|
|
|
|
|
// space-bar toggling of checkboxes
|
2022-05-08 11:00:49 +00:00
|
|
|
if( m_grid->IsEditable() && ev.GetKeyCode() == ' ' )
|
2013-11-19 20:38:29 +00:00
|
|
|
{
|
2020-02-14 19:27:34 +00:00
|
|
|
bool retVal = false;
|
|
|
|
|
|
|
|
// If only rows can be selected, only toggle the first cell in a row
|
|
|
|
if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
|
|
|
|
{
|
|
|
|
wxArrayInt rowSel = m_grid->GetSelectedRows();
|
|
|
|
|
|
|
|
for( unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
|
|
|
|
{
|
|
|
|
retVal |= toggleCell( rowSel[rowInd], 0, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only columns can be selected, only toggle the first cell in a column
|
|
|
|
else if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectColumns )
|
|
|
|
{
|
|
|
|
wxArrayInt colSel = m_grid->GetSelectedCols();
|
|
|
|
|
|
|
|
for( unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
|
|
|
|
{
|
|
|
|
retVal |= toggleCell( 0, colSel[colInd], true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user can select the individual cells, toggle each cell selected
|
|
|
|
else if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectCells )
|
|
|
|
{
|
|
|
|
wxArrayInt rowSel = m_grid->GetSelectedRows();
|
|
|
|
wxArrayInt colSel = m_grid->GetSelectedCols();
|
|
|
|
wxGridCellCoordsArray cellSel = m_grid->GetSelectedCells();
|
|
|
|
wxGridCellCoordsArray topLeft = m_grid->GetSelectionBlockTopLeft();
|
|
|
|
wxGridCellCoordsArray botRight = m_grid->GetSelectionBlockBottomRight();
|
|
|
|
|
|
|
|
// Iterate over every individually selected cell and try to toggle it
|
|
|
|
for( unsigned int cellInd = 0; cellInd < cellSel.GetCount(); cellInd++ )
|
|
|
|
{
|
|
|
|
retVal |= toggleCell( cellSel[cellInd].GetRow(), cellSel[cellInd].GetCol(), true );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over every column and try to toggle each cell in it
|
|
|
|
for( unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
|
|
|
|
{
|
|
|
|
for( int row = 0; row < m_grid->GetNumberRows(); row++ )
|
|
|
|
{
|
|
|
|
retVal |= toggleCell( row, colSel[colInd], true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over every row and try to toggle each cell in it
|
|
|
|
for( unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
|
|
|
|
{
|
|
|
|
for( int col = 0; col < m_grid->GetNumberCols(); col++ )
|
|
|
|
{
|
|
|
|
retVal |= toggleCell( rowSel[rowInd], col, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the selection blocks
|
|
|
|
for( unsigned int blockInd = 0; blockInd < topLeft.GetCount(); blockInd++ )
|
|
|
|
{
|
|
|
|
wxGridCellCoords start = topLeft[blockInd];
|
|
|
|
wxGridCellCoords end = botRight[blockInd];
|
|
|
|
|
|
|
|
for( int row = start.GetRow(); row <= end.GetRow(); row++ )
|
|
|
|
{
|
|
|
|
for( int col = start.GetCol(); col <= end.GetCol(); col++ )
|
|
|
|
{
|
|
|
|
retVal |= toggleCell( row, col, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 23:25:36 +00:00
|
|
|
|
2020-02-14 19:27:34 +00:00
|
|
|
// Return if there were any cells toggled
|
|
|
|
if( retVal )
|
2018-03-21 23:25:36 +00:00
|
|
|
return;
|
2013-11-19 20:38:29 +00:00
|
|
|
}
|
2018-03-21 23:25:36 +00:00
|
|
|
|
2019-06-24 12:35:25 +00:00
|
|
|
// ctrl-tab for exit grid
|
|
|
|
#ifdef __APPLE__
|
|
|
|
bool ctrl = ev.RawControlDown();
|
|
|
|
#else
|
|
|
|
bool ctrl = ev.ControlDown();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( ctrl && ev.GetKeyCode() == WXK_TAB )
|
|
|
|
{
|
|
|
|
wxWindow* test = m_grid->GetNextSibling();
|
|
|
|
|
|
|
|
if( !test )
|
|
|
|
test = m_grid->GetParent()->GetNextSibling();
|
|
|
|
|
|
|
|
while( test && !test->IsTopLevel() )
|
|
|
|
{
|
|
|
|
test->SetFocus();
|
|
|
|
|
|
|
|
if( test->HasFocus() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( !test->GetChildren().empty() )
|
2021-07-15 19:26:35 +00:00
|
|
|
{
|
2019-06-24 12:35:25 +00:00
|
|
|
test = test->GetChildren().front();
|
2021-07-15 19:26:35 +00:00
|
|
|
}
|
2019-06-24 12:35:25 +00:00
|
|
|
else if( test->GetNextSibling() )
|
2021-07-15 19:26:35 +00:00
|
|
|
{
|
2019-06-24 12:35:25 +00:00
|
|
|
test = test->GetNextSibling();
|
2021-07-15 19:26:35 +00:00
|
|
|
}
|
2019-06-24 12:35:25 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while( test )
|
|
|
|
{
|
|
|
|
test = test->GetParent();
|
|
|
|
|
|
|
|
if( test && test->IsTopLevel() )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( test && test->GetNextSibling() )
|
|
|
|
{
|
|
|
|
test = test->GetNextSibling();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 23:25:36 +00:00
|
|
|
ev.Skip( true );
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRID_TRICKS::paste_clipboard()
|
|
|
|
{
|
2021-01-29 19:13:12 +00:00
|
|
|
wxLogNull doNotLog; // disable logging of failed clipboard actions
|
|
|
|
|
2022-05-08 11:00:49 +00:00
|
|
|
if( m_grid->IsEditable() && wxTheClipboard->Open() )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
2022-01-12 09:24:04 +00:00
|
|
|
if( wxTheClipboard->IsSupported( wxDF_TEXT )
|
|
|
|
|| wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
|
|
|
|
wxTheClipboard->GetData( data );
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
paste_text( data.GetText() );
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
m_grid->ForceRefresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRID_TRICKS::paste_text( const wxString& cb_text )
|
|
|
|
{
|
|
|
|
wxGridTableBase* tbl = m_grid->GetTable();
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
const int cur_row = m_grid->GetGridCursorRow();
|
|
|
|
const int cur_col = m_grid->GetGridCursorCol();
|
2019-01-14 15:27:15 +00:00
|
|
|
int start_row;
|
|
|
|
int end_row;
|
|
|
|
int start_col;
|
|
|
|
int end_col;
|
|
|
|
bool is_selection = false;
|
2018-03-14 04:51:59 +00:00
|
|
|
|
|
|
|
if( cur_row < 0 || cur_col < 0 )
|
|
|
|
{
|
|
|
|
wxBell();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2019-01-14 15:27:15 +00:00
|
|
|
if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
|
|
|
|
{
|
|
|
|
if( m_sel_row_count > 1 )
|
|
|
|
is_selection = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( m_grid->IsSelection() )
|
|
|
|
is_selection = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxStringTokenizer rows( cb_text, ROW_SEP, wxTOKEN_RET_EMPTY );
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2019-01-14 15:27:15 +00:00
|
|
|
// If selection of cells is present
|
|
|
|
// then a clipboard pastes to selected cells only.
|
|
|
|
if( is_selection )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
2019-01-14 15:27:15 +00:00
|
|
|
start_row = m_sel_row_start;
|
|
|
|
end_row = m_sel_row_start + m_sel_row_count;
|
|
|
|
start_col = m_sel_col_start;
|
|
|
|
end_col = m_sel_col_start + m_sel_col_count;
|
|
|
|
}
|
|
|
|
// Otherwise, paste whole clipboard
|
|
|
|
// starting from cell with cursor.
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start_row = cur_row;
|
|
|
|
end_row = cur_row + rows.CountTokens();
|
2019-01-21 07:54:13 +00:00
|
|
|
|
2019-01-14 15:27:15 +00:00
|
|
|
if( end_row > tbl->GetNumberRows() )
|
|
|
|
end_row = tbl->GetNumberRows();
|
2019-01-21 07:54:13 +00:00
|
|
|
|
2019-01-14 15:27:15 +00:00
|
|
|
start_col = cur_col;
|
2019-01-21 07:54:13 +00:00
|
|
|
end_col = start_col; // end_col actual value calculates later
|
2019-01-14 15:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for( int row = start_row; row < end_row; ++row )
|
|
|
|
{
|
2022-02-18 17:11:57 +00:00
|
|
|
// If number of selected rows is larger than the count of rows on the clipboard, paste
|
|
|
|
// again and again until the end of the selection is reached.
|
2019-01-14 15:27:15 +00:00
|
|
|
if( !rows.HasMoreTokens() )
|
|
|
|
rows.SetString( cb_text, ROW_SEP, wxTOKEN_RET_EMPTY );
|
2019-01-16 13:16:25 +00:00
|
|
|
|
2013-10-23 18:56:03 +00:00
|
|
|
wxString rowTxt = rows.GetNextToken();
|
|
|
|
|
2019-01-14 15:27:15 +00:00
|
|
|
wxStringTokenizer cols( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
|
|
|
|
|
|
|
|
if( !is_selection )
|
|
|
|
{
|
|
|
|
end_col = cur_col + cols.CountTokens();
|
2019-01-21 07:54:13 +00:00
|
|
|
|
2019-01-14 15:27:15 +00:00
|
|
|
if( end_col > tbl->GetNumberCols() )
|
|
|
|
end_col = tbl->GetNumberCols();
|
|
|
|
}
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2019-01-14 15:27:15 +00:00
|
|
|
for( int col = start_col; col < end_col; ++col )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
2022-02-18 17:11:57 +00:00
|
|
|
// Skip hidden columns
|
|
|
|
if( !m_grid->IsColShown( col ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If number of selected cols is larger than the count of cols on the clipboard,
|
|
|
|
// paste again and again until the end of the selection is reached.
|
2019-01-14 15:27:15 +00:00
|
|
|
if( !cols.HasMoreTokens() )
|
|
|
|
cols.SetString( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
|
2019-01-16 13:16:25 +00:00
|
|
|
|
2013-10-23 18:56:03 +00:00
|
|
|
wxString cellTxt = cols.GetNextToken();
|
2020-06-26 16:06:02 +00:00
|
|
|
|
|
|
|
if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) )
|
2020-08-05 00:21:36 +00:00
|
|
|
{
|
2020-06-26 16:06:02 +00:00
|
|
|
tbl->SetValue( row, col, cellTxt );
|
2020-08-05 00:21:36 +00:00
|
|
|
|
|
|
|
wxGridEvent evt( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
|
|
|
|
m_grid->GetEventHandler()->ProcessEvent( evt );
|
|
|
|
}
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-25 15:37:18 +00:00
|
|
|
void GRID_TRICKS::cutcopy( bool doCopy, bool doDelete )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
2021-01-29 19:13:12 +00:00
|
|
|
wxLogNull doNotLog; // disable logging of failed clipboard actions
|
|
|
|
|
2021-02-25 15:37:18 +00:00
|
|
|
if( doCopy && !wxTheClipboard->Open() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
wxGridTableBase* tbl = m_grid->GetTable();
|
|
|
|
wxString txt;
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2021-02-25 15:37:18 +00:00
|
|
|
// fill txt with a format that is compatible with most spreadsheets
|
|
|
|
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 )
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
2021-10-26 14:54:31 +00:00
|
|
|
if( !m_grid->IsColShown( col ) )
|
|
|
|
continue;
|
|
|
|
|
2021-02-25 15:37:18 +00:00
|
|
|
txt += tbl->GetValue( row, col );
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2021-02-25 15:37:18 +00:00
|
|
|
if( col < m_sel_col_start + m_sel_col_count - 1 ) // that was not last column
|
|
|
|
txt += COL_SEP;
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2022-05-08 11:00:49 +00:00
|
|
|
if( doDelete && m_grid->IsEditable() )
|
2021-02-25 15:37:18 +00:00
|
|
|
{
|
|
|
|
if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) )
|
|
|
|
tbl->SetValue( row, col, wxEmptyString );
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:37:18 +00:00
|
|
|
txt += ROW_SEP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( doCopy )
|
|
|
|
{
|
2013-10-23 18:56:03 +00:00
|
|
|
wxTheClipboard->SetData( new wxTextDataObject( txt ) );
|
2021-05-01 22:00:08 +00:00
|
|
|
wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
|
2013-10-23 18:56:03 +00:00
|
|
|
wxTheClipboard->Close();
|
2018-03-14 04:51:59 +00:00
|
|
|
}
|
2021-02-25 15:37:18 +00:00
|
|
|
|
|
|
|
if( doDelete )
|
|
|
|
m_grid->ForceRefresh();
|
2018-03-14 04:51:59 +00:00
|
|
|
}
|
2018-08-19 16:10:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
void GRID_TRICKS::onUpdateUI( wxUpdateUIEvent& event )
|
|
|
|
{
|
|
|
|
// Respect ROW selectionMode when moving cursor
|
|
|
|
|
2018-09-12 12:59:33 +00:00
|
|
|
if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
|
2018-08-19 16:10:42 +00:00
|
|
|
{
|
2018-08-30 07:07:33 +00:00
|
|
|
int cursorRow = m_grid->GetGridCursorRow();
|
2018-08-19 16:10:42 +00:00
|
|
|
bool cursorInSelectedRow = false;
|
|
|
|
|
|
|
|
for( int row : m_grid->GetSelectedRows() )
|
|
|
|
{
|
|
|
|
if( row == cursorRow )
|
|
|
|
{
|
|
|
|
cursorInSelectedRow = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:30:50 +00:00
|
|
|
if( !cursorInSelectedRow && cursorRow >= 0 )
|
2018-08-19 16:10:42 +00:00
|
|
|
m_grid->SelectRow( cursorRow );
|
|
|
|
}
|
|
|
|
}
|