28 #include <wx/tokenzr.h> 29 #include <wx/clipbrd.h> 36 #define COL_SEP wxT( '\t' ) 37 #define ROW_SEP wxT( '\n' ) 48 aGrid->Connect( wxEVT_GRID_CELL_LEFT_CLICK,
50 aGrid->Connect( wxEVT_GRID_CELL_LEFT_DCLICK,
52 aGrid->Connect( wxEVT_GRID_CELL_RIGHT_CLICK,
54 aGrid->Connect( wxEVT_GRID_LABEL_RIGHT_CLICK,
56 aGrid->Connect( wxEVT_GRID_LABEL_LEFT_CLICK,
66 aGrid->GetGridWindow()->Connect( wxEVT_MOTION,
73 auto renderer =
m_grid->GetCellRenderer( aRow, aCol );
74 bool isCheckbox = ( dynamic_cast<wxGridCellBoolRenderer*>( renderer ) != nullptr );
79 if( !aPreserveSelection )
82 m_grid->SetGridCursor( aRow, aCol );
84 wxGridTableBase* model =
m_grid->GetTable();
86 if( model->CanGetValueAs( aRow, aCol, wxGRID_VALUE_BOOL )
87 && model->CanSetValueAs( aRow, aCol, wxGRID_VALUE_BOOL ) )
89 model->SetValueAsBool( aRow, aCol, !model->GetValueAsBool( aRow, aCol ) );
93 if( model->GetValue( aRow, aCol ) == wxT(
"1" ) )
94 model->SetValue( aRow, aCol, wxT(
"0" ) );
96 model->SetValue( aRow, aCol, wxT(
"1" ) );
103 wxGridEvent event(
m_grid->GetId(), wxEVT_GRID_CELL_CHANGED,
m_grid, aRow, aCol );
104 event.SetString( model->GetValue( aRow, aCol ) );
105 m_grid->GetEventHandler()->ProcessEvent( event );
116 if(
m_grid->GetGridCursorRow() != aRow ||
m_grid->GetGridCursorCol() != aCol )
117 m_grid->SetGridCursor( aRow, aCol );
119 if(
m_grid->IsEditable() && !
m_grid->IsReadOnly( aRow, aCol ) )
123 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
125 wxArrayInt rows =
m_grid->GetSelectedRows();
127 if( rows.size() != 1 || rows.Item( 0 ) != aRow )
128 m_grid->SelectRow( aRow );
151 int row = aEvent.GetRow();
152 int col = aEvent.GetCol();
155 if( !aEvent.GetModifiers() )
180 wxPoint pt = aEvent.GetPosition();
181 wxPoint pos =
m_grid->CalcScrolledPosition( wxPoint( pt.x, pt.y ) );
183 int col =
m_grid->XToCol( pos.x );
184 int row =
m_grid->YToRow( pos.y );
187 if( ( col == wxNOT_FOUND ) || ( row == wxNOT_FOUND ) || !
m_tooltipEnabled[col] )
189 m_grid->GetGridWindow()->SetToolTip(
"" );
194 m_grid->GetGridWindow()->SetToolTip(
m_grid->GetCellValue( row, col ) );
207 wxGridCellCoordsArray topLeft =
m_grid->GetSelectionBlockTopLeft();
208 wxGridCellCoordsArray botRight =
m_grid->GetSelectionBlockBottomRight();
210 wxArrayInt cols =
m_grid->GetSelectedCols();
211 wxArrayInt rows =
m_grid->GetSelectedRows();
213 if( topLeft.Count() && botRight.Count() )
221 else if( cols.Count() )
228 else if( rows.Count() )
265 for(
int i = 0; i <
m_grid->GetNumberCols(); ++i )
268 menu.AppendCheckItem(
id,
m_grid->GetColLabelValue( i ) );
269 menu.Check(
id,
m_grid->IsColShown( i ) );
272 m_grid->PopupMenu( &menu );
279 _(
"Clear selected cells placing original contents on clipboard" ) );
281 _(
"Copy selected cells to clipboard" ) );
283 _(
"Paste clipboard cells to matrix at current cell" ) );
301 if( wxTheClipboard->Open() )
303 if( wxTheClipboard->IsSupported( wxDF_TEXT )
304 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
309 wxTheClipboard->Close();
312 m_grid->PopupMenu( &menu );
324 int menu_id =
event.GetId();
356 if(
m_grid->IsColShown( col ) )
367 bool handled =
false;
369 if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'V' )
371 if(
m_grid->IsCellEditControlShown() && wxTheClipboard->Open() )
373 if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
375 wxTextDataObject data;
376 wxTheClipboard->GetData( data );
378 if( data.GetText().Contains(
COL_SEP ) || data.GetText().Contains(
ROW_SEP ) )
386 wxTheClipboard->Close();
398 if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'A' )
403 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'C' )
409 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'V' )
415 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() ==
'X' )
421 else if( !ev.GetModifiers() && ev.GetKeyCode() == WXK_DELETE )
429 if( ev.GetKeyCode() ==
' ' )
434 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
436 wxArrayInt rowSel =
m_grid->GetSelectedRows();
438 for(
unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
440 retVal |=
toggleCell( rowSel[rowInd], 0,
true );
445 else if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectColumns )
447 wxArrayInt colSel =
m_grid->GetSelectedCols();
449 for(
unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
451 retVal |=
toggleCell( 0, colSel[colInd],
true );
456 else if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectCells )
458 wxArrayInt rowSel =
m_grid->GetSelectedRows();
459 wxArrayInt colSel =
m_grid->GetSelectedCols();
460 wxGridCellCoordsArray cellSel =
m_grid->GetSelectedCells();
461 wxGridCellCoordsArray topLeft =
m_grid->GetSelectionBlockTopLeft();
462 wxGridCellCoordsArray botRight =
m_grid->GetSelectionBlockBottomRight();
465 for(
unsigned int cellInd = 0; cellInd < cellSel.GetCount(); cellInd++ )
467 retVal |=
toggleCell( cellSel[cellInd].GetRow(), cellSel[cellInd].GetCol(),
true );
471 for(
unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
473 for(
int row = 0; row <
m_grid->GetNumberRows(); row++ )
475 retVal |=
toggleCell( row, colSel[colInd],
true );
480 for(
unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
482 for(
int col = 0; col <
m_grid->GetNumberCols(); col++ )
484 retVal |=
toggleCell( rowSel[rowInd], col,
true );
489 for(
unsigned int blockInd = 0; blockInd < topLeft.GetCount(); blockInd++ )
491 wxGridCellCoords start = topLeft[blockInd];
492 wxGridCellCoords end = botRight[blockInd];
494 for(
int row = start.GetRow(); row <= end.GetRow(); row++ )
496 for(
int col = start.GetCol(); col <= end.GetCol(); col++ )
514 bool ctrl = ev.RawControlDown();
516 bool ctrl = ev.ControlDown();
519 if( ctrl && ev.GetKeyCode() == WXK_TAB )
526 while(
test && !
test->IsTopLevel() )
530 if(
test->HasFocus() )
533 if( !
test->GetChildren().empty() )
537 else if(
test->GetNextSibling() )
551 else if(
test &&
test->GetNextSibling() )
571 if( wxTheClipboard->Open() )
573 if( wxTheClipboard->IsSupported( wxDF_TEXT )
574 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
576 wxTextDataObject data;
578 wxTheClipboard->GetData( data );
583 wxTheClipboard->Close();
591 wxGridTableBase* tbl =
m_grid->GetTable();
593 const int cur_row =
m_grid->GetGridCursorRow();
594 const int cur_col =
m_grid->GetGridCursorCol();
599 bool is_selection =
false;
601 if( cur_row < 0 || cur_col < 0 )
607 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
614 if(
m_grid->IsSelection() )
618 wxStringTokenizer rows( cb_text,
ROW_SEP, wxTOKEN_RET_EMPTY );
634 end_row = cur_row + rows.CountTokens();
636 if( end_row > tbl->GetNumberRows() )
637 end_row = tbl->GetNumberRows();
643 for(
int row = start_row; row < end_row; ++row )
647 if( !rows.HasMoreTokens() )
648 rows.SetString( cb_text,
ROW_SEP, wxTOKEN_RET_EMPTY );
650 wxString rowTxt = rows.GetNextToken();
652 wxStringTokenizer cols( rowTxt,
COL_SEP, wxTOKEN_RET_EMPTY );
656 end_col = cur_col + cols.CountTokens();
658 if( end_col > tbl->GetNumberCols() )
659 end_col = tbl->GetNumberCols();
662 for(
int col = start_col; col < end_col; ++col )
665 if( !
m_grid->IsColShown( col ) )
670 if( !cols.HasMoreTokens() )
671 cols.SetString( rowTxt,
COL_SEP, wxTOKEN_RET_EMPTY );
673 wxString cellTxt = cols.GetNextToken();
675 if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) )
677 tbl->SetValue( row, col, cellTxt );
679 wxGridEvent evt(
m_grid->GetId(), wxEVT_GRID_CELL_CHANGED,
m_grid, row, col );
680 m_grid->GetEventHandler()->ProcessEvent( evt );
691 if( doCopy && !wxTheClipboard->Open() )
694 wxGridTableBase* tbl =
m_grid->GetTable();
702 if( !
m_grid->IsColShown( col ) )
705 txt += tbl->GetValue( row, col );
712 if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) )
713 tbl->SetValue( row, col, wxEmptyString );
722 wxTheClipboard->SetData(
new wxTextDataObject( txt ) );
723 wxTheClipboard->Flush();
724 wxTheClipboard->Close();
736 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
738 int cursorRow =
m_grid->GetGridCursorRow();
739 bool cursorInSelectedRow =
false;
741 for(
int row :
m_grid->GetSelectedRows() )
743 if( row == cursorRow )
745 cursorInSelectedRow =
true;
750 if( !cursorInSelectedRow && cursorRow >= 0 )
751 m_grid->SelectRow( cursorRow );
void onGridLabelLeftClick(wxGridEvent &event)
void getSelectedArea()
Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above.
void onGridCellLeftClick(wxGridEvent &event)
void onGridCellRightClick(wxGridEvent &event)
virtual bool handleDoubleClick(wxGridEvent &aEvent)
std::bitset< GRIDTRICKS_MAX_COL > m_tooltipEnabled
GRID_TRICKS(WX_GRID *aGrid)
void onGridCellLeftDClick(wxGridEvent &event)
WX_GRID * m_grid
I don't own the grid, but he owns me.
virtual void paste_text(const wxString &cb_text)
void onPopupSelection(wxCommandEvent &event)
bool toggleCell(int aRow, int aCol, bool aPreserveSelection=false)
bool showEditor(int aRow, int aCol)
virtual void cutcopy(bool doCopy, bool doDelete)
void onGridMotion(wxMouseEvent &event)
virtual void paste_clipboard()
void onUpdateUI(wxUpdateUIEvent &event)
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
void onCharHook(wxKeyEvent &event)
void onGridLabelRightClick(wxGridEvent &event)
void onKeyDown(wxKeyEvent &event)
virtual void doPopupSelection(wxCommandEvent &event)
virtual void showPopupMenu(wxMenu &menu)
void ShowEditorOnMouseUp()
WxWidgets has a bunch of bugs in its handling of wxGrid mouse events which close cell editors right a...