28#include <wx/tokenzr.h>
29#include <wx/clipbrd.h>
31#include <wx/stc/stc.h>
37#define COL_SEP wxT( '\t' )
38#define ROW_SEP wxT( '\n' )
39#define ROW_SEP_R wxT( '\r' )
44 m_addHandler( []( wxCommandEvent& ) {} )
52 m_addHandler( aAddHandler )
65 m_grid->Connect( wxEVT_GRID_CELL_LEFT_CLICK,
67 m_grid->Connect( wxEVT_GRID_CELL_LEFT_DCLICK,
69 m_grid->Connect( wxEVT_GRID_CELL_RIGHT_CLICK,
71 m_grid->Connect( wxEVT_GRID_LABEL_RIGHT_CLICK,
73 m_grid->Connect( wxEVT_GRID_LABEL_LEFT_CLICK,
77 m_grid->Connect( wxEVT_CHAR_HOOK,
79 m_grid->Connect( wxEVT_KEY_DOWN,
81 m_grid->Connect( wxEVT_UPDATE_UI,
85 m_grid->GetGridWindow()->Connect( wxEVT_MOTION,
93 wxGridCellEditor*
editor =
m_grid->GetCellEditor( aRow, aCol );
94 bool retval = (
dynamic_cast<wxTextEntry*
>(
editor )
104 wxGridCellRenderer* renderer =
m_grid->GetCellRenderer( aRow, aCol );
105 bool retval = (
dynamic_cast<wxGridCellBoolRenderer*
>( renderer ) );
114 return !
m_grid->IsEditable() ||
m_grid->IsReadOnly( aRow, aCol );
122 if( !aPreserveSelection )
125 m_grid->SetGridCursor( aRow, aCol );
128 wxGridTableBase* model =
m_grid->GetTable();
130 if( model->CanGetValueAs( aRow, aCol, wxGRID_VALUE_BOOL )
131 && model->CanSetValueAs( aRow, aCol, wxGRID_VALUE_BOOL ) )
133 model->SetValueAsBool( aRow, aCol, !model->GetValueAsBool( aRow, aCol ) );
137 if( model->GetValue( aRow, aCol ) == wxT(
"1" ) )
138 model->SetValue( aRow, aCol, wxT(
"0" ) );
140 model->SetValue( aRow, aCol, wxT(
"1" ) );
147 wxGridEvent event(
m_grid->GetId(), wxEVT_GRID_CELL_CHANGED,
m_grid, aRow, aCol );
148 event.SetString( model->GetValue( aRow, aCol ) );
149 m_grid->GetEventHandler()->ProcessEvent( event );
160 if(
m_grid->GetGridCursorRow() != aRow ||
m_grid->GetGridCursorCol() != aCol )
161 m_grid->SetGridCursor( aRow, aCol );
172 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
174 wxArrayInt rows =
m_grid->GetSelectedRows();
176 if( rows.size() != 1 || rows.Item( 0 ) != aRow )
177 m_grid->SelectRow( aRow );
200 int row = aEvent.GetRow();
201 int col = aEvent.GetCol();
204 if( !aEvent.GetModifiers() )
206 bool toggled =
false;
229 wxString newVal =
m_grid->GetCellValue( row, col );
233 if( affectedRow == row )
236 m_grid->SetCellValue( affectedRow, col, newVal );
257 wxPoint pt = aEvent.GetPosition();
258 wxPoint pos =
m_grid->CalcScrolledPosition( wxPoint( pt.x, pt.y ) );
260 int col =
m_grid->XToCol( pos.x );
261 int row =
m_grid->YToRow( pos.y );
264 if( ( col == wxNOT_FOUND ) || ( row == wxNOT_FOUND ) || !
m_tooltipEnabled[col] )
266 m_grid->GetGridWindow()->SetToolTip( wxS(
"" ) );
271 m_grid->GetGridWindow()->SetToolTip(
m_grid->GetCellValue( row, col ) );
284 wxGridCellCoordsArray topLeft =
m_grid->GetSelectionBlockTopLeft();
285 wxGridCellCoordsArray botRight =
m_grid->GetSelectionBlockBottomRight();
287 wxArrayInt cols =
m_grid->GetSelectedCols();
288 wxArrayInt rows =
m_grid->GetSelectedRows();
290 if( topLeft.Count() && botRight.Count() )
298 else if( cols.Count() )
305 else if( rows.Count() )
342 for(
int i = 0; i <
m_grid->GetNumberCols(); ++i )
345 menu.AppendCheckItem(
id,
m_grid->GetColLabelValue( i ) );
346 menu.Check(
id,
m_grid->IsColShown( i ) );
349 m_grid->PopupMenu( &menu );
356 _(
"Clear selected cells placing original contents on clipboard" ) );
358 _(
"Copy selected cells to clipboard" ) );
360 _(
"Paste clipboard cells to matrix at current cell" ) );
362 _(
"Clear contents of selected cells" ) );
364 _(
"Select all cells" ) );
372 auto anyCellsWritable =
387 if( anyCellsWritable() )
397 if( wxTheClipboard->Open() )
399 if( wxTheClipboard->IsSupported( wxDF_TEXT )
400 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
405 wxTheClipboard->Close();
408 m_grid->PopupMenu( &menu );
420 int menu_id =
event.GetId();
452 if(
m_grid->IsColShown( col ) )
463 bool handled =
false;
465 if( ( ev.GetKeyCode() == WXK_RETURN || ev.GetKeyCode() == WXK_NUMPAD_ENTER )
466 && ev.GetModifiers() == wxMOD_NONE
467 &&
m_grid->GetGridCursorRow() ==
m_grid->GetNumberRows() - 1 )
469 if(
m_grid->IsCellEditControlShown() )
476 wxCommandEvent
dummy;
481 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'V' )
483 if(
m_grid->IsCellEditControlShown() && wxTheClipboard->Open() )
485 if( wxTheClipboard->IsSupported( wxDF_TEXT )
486 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
488 wxTextDataObject data;
489 wxTheClipboard->GetData( data );
491 if( data.GetText().Contains(
COL_SEP ) || data.GetText().Contains(
ROW_SEP ) )
493 wxString stripped( data.GetText() );
494 stripped.Replace(
ROW_SEP,
" " );
496 stripped.Replace(
COL_SEP,
" " );
499 if( wxTextEntry* te =
dynamic_cast<wxTextEntry*
>( ev.GetEventObject() ) )
500 te->WriteText( stripped );
508 wxTheClipboard->Close();
512 else if( ev.GetKeyCode() == WXK_ESCAPE )
514 if(
m_grid->IsCellEditControlShown() )
528 if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'A' )
533 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'C' )
539 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'V' )
545 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'X' )
551 else if( !ev.GetModifiers() && ev.GetKeyCode() == WXK_DELETE )
559 if(
m_grid->IsEditable() && ev.GetKeyCode() ==
' ' )
564 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
566 wxArrayInt rowSel =
m_grid->GetSelectedRows();
568 for(
unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
569 retVal |=
toggleCell( rowSel[rowInd], 0,
true );
573 else if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectColumns )
575 wxArrayInt colSel =
m_grid->GetSelectedCols();
577 for(
unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
578 retVal |=
toggleCell( 0, colSel[colInd],
true );
582 else if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectCells )
584 wxArrayInt rowSel =
m_grid->GetSelectedRows();
585 wxArrayInt colSel =
m_grid->GetSelectedCols();
586 wxGridCellCoordsArray cellSel =
m_grid->GetSelectedCells();
587 wxGridCellCoordsArray topLeft =
m_grid->GetSelectionBlockTopLeft();
588 wxGridCellCoordsArray botRight =
m_grid->GetSelectionBlockBottomRight();
591 for(
unsigned int cellInd = 0; cellInd < cellSel.GetCount(); cellInd++ )
593 retVal |=
toggleCell( cellSel[cellInd].GetRow(), cellSel[cellInd].GetCol(),
true );
597 for(
unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
599 for(
int row = 0; row <
m_grid->GetNumberRows(); row++ )
600 retVal |=
toggleCell( row, colSel[colInd],
true );
604 for(
unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
606 for(
int col = 0; col <
m_grid->GetNumberCols(); col++ )
607 retVal |=
toggleCell( rowSel[rowInd], col,
true );
611 for(
unsigned int blockInd = 0; blockInd < topLeft.GetCount(); blockInd++ )
613 wxGridCellCoords start = topLeft[blockInd];
614 wxGridCellCoords end = botRight[blockInd];
616 for(
int row = start.GetRow(); row <= end.GetRow(); row++ )
618 for(
int col = start.GetCol(); col <= end.GetCol(); col++ )
631 bool ctrl = ev.RawControlDown();
633 bool ctrl = ev.ControlDown();
636 if( ctrl && ev.GetKeyCode() == WXK_TAB )
643 while(
test && !
test->IsTopLevel() )
647 if(
test->HasFocus() )
650 if( !
test->GetChildren().empty() )
654 else if(
test->GetNextSibling() )
668 else if(
test &&
test->GetNextSibling() )
688 if(
m_grid->IsEditable() && wxTheClipboard->Open() )
690 if( wxTheClipboard->IsSupported( wxDF_TEXT )
691 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
693 wxTextDataObject data;
695 wxTheClipboard->GetData( data );
697 wxString
text = data.GetText();
701 text.Replace(
"\n\n",
"\n" );
707 wxTheClipboard->Close();
715 wxGridTableBase* tbl =
m_grid->GetTable();
717 const int cur_row =
m_grid->GetGridCursorRow();
718 const int cur_col =
m_grid->GetGridCursorCol();
723 bool is_selection =
false;
725 if( cur_row < 0 || cur_col < 0 )
731 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
741 wxStringTokenizer rows( cb_text,
ROW_SEP, wxTOKEN_RET_EMPTY );
757 end_row = cur_row + rows.CountTokens();
759 if( end_row > tbl->GetNumberRows() )
763 for(
int ii = end_row - tbl->GetNumberRows(); ii > 0; --ii )
765 wxCommandEvent
dummy;
770 end_row = tbl->GetNumberRows();
777 for(
int row = start_row; row < end_row; ++row )
781 if( !rows.HasMoreTokens() )
782 rows.SetString( cb_text,
ROW_SEP, wxTOKEN_RET_EMPTY );
784 wxString rowTxt = rows.GetNextToken();
786 wxStringTokenizer cols( rowTxt,
COL_SEP, wxTOKEN_RET_EMPTY );
789 end_col = cur_col + cols.CountTokens();
791 for(
int col = start_col; col < end_col && col < tbl->GetNumberCols(); ++col )
794 if( !
m_grid->IsColShown( col ) )
802 if( !cols.HasMoreTokens() )
803 cols.SetString( rowTxt,
COL_SEP, wxTOKEN_RET_EMPTY );
805 wxString cellTxt = cols.GetNextToken();
809 if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) && !
isReadOnly( row, col ) )
811 tbl->SetValue( row, col, cellTxt );
813 wxGridEvent evt(
m_grid->GetId(), wxEVT_GRID_CELL_CHANGED,
m_grid, row, col );
814 m_grid->GetEventHandler()->ProcessEvent( evt );
817 else if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_BOOL ) )
819 tbl->SetValueAsBool( row, col, cellTxt == wxT(
"1" ) );
821 wxGridEvent evt(
m_grid->GetId(), wxEVT_GRID_CELL_CHANGED,
m_grid, row, col );
822 m_grid->GetEventHandler()->ProcessEvent( evt );
833 if( doCopy && !wxTheClipboard->Open() )
836 wxGridTableBase* tbl =
m_grid->GetTable();
844 if( !
m_grid->IsColShown( col ) )
847 txt += tbl->GetValue( row, col );
857 tbl->SetValue( row, col, wxEmptyString );
866 wxTheClipboard->SetData(
new wxTextDataObject( txt ) );
867 wxTheClipboard->Flush();
868 wxTheClipboard->Close();
880 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
882 int cursorRow =
m_grid->GetGridCursorRow();
883 bool cursorInSelectedRow =
false;
885 for(
int row :
m_grid->GetSelectedRows() )
887 if( row == cursorRow )
889 cursorInSelectedRow =
true;
894 if( !cursorInSelectedRow && cursorRow >= 0 )
895 m_grid->SelectRow( cursorRow );
bool isReadOnly(int aRow, int aCol)
void onGridMotion(wxMouseEvent &event)
void onGridLabelLeftClick(wxGridEvent &event)
virtual void paste_text(const wxString &cb_text)
void init()
Shared initialization for various ctors.
void getSelectedArea()
Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above.
GRID_TRICKS(WX_GRID *aGrid)
void onKeyDown(wxKeyEvent &event)
void onPopupSelection(wxCommandEvent &event)
virtual void cutcopy(bool doCopy, bool doDelete)
virtual void doPopupSelection(wxCommandEvent &event)
bool isCheckbox(int aRow, int aCol)
std::function< void(wxCommandEvent &)> m_addHandler
virtual void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent)
std::bitset< GRIDTRICKS_MAX_COL > m_tooltipEnabled
void onUpdateUI(wxUpdateUIEvent &event)
void onGridCellRightClick(wxGridEvent &event)
WX_GRID * m_grid
I don't own the grid, but he owns me.
bool isTextEntry(int aRow, int aCol)
void onGridCellLeftDClick(wxGridEvent &event)
void onGridCellLeftClick(wxGridEvent &event)
virtual bool handleDoubleClick(wxGridEvent &aEvent)
void onCharHook(wxKeyEvent &event)
virtual void paste_clipboard()
bool showEditor(int aRow, int aCol)
void onGridLabelRightClick(wxGridEvent &event)
virtual bool toggleCell(int aRow, int aCol, bool aPreserveSelection=false)
bool CancelPendingChanges()
void ShowEditorOnMouseUp()
WxWidgets has a bunch of bugs in its handling of wxGrid mouse events which close cell editors right a...
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
@ GRIDTRICKS_FIRST_SHOWHIDE
std::vector< FAB_LAYER_COLOR > dummy