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 );
700 wxTheClipboard->Close();
708 wxGridTableBase* tbl =
m_grid->GetTable();
710 const int cur_row =
m_grid->GetGridCursorRow();
711 const int cur_col =
m_grid->GetGridCursorCol();
716 bool is_selection =
false;
718 if( cur_row < 0 || cur_col < 0 )
724 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
734 wxStringTokenizer rows( cb_text,
ROW_SEP, wxTOKEN_RET_EMPTY );
750 end_row = cur_row + rows.CountTokens();
752 if( end_row > tbl->GetNumberRows() )
756 for(
int ii = end_row - tbl->GetNumberRows(); ii > 0; --ii )
758 wxCommandEvent
dummy;
763 end_row = tbl->GetNumberRows();
770 for(
int row = start_row; row < end_row; ++row )
774 if( !rows.HasMoreTokens() )
775 rows.SetString( cb_text,
ROW_SEP, wxTOKEN_RET_EMPTY );
777 wxString rowTxt = rows.GetNextToken();
779 wxStringTokenizer cols( rowTxt,
COL_SEP, wxTOKEN_RET_EMPTY );
782 end_col = cur_col + cols.CountTokens();
784 for(
int col = start_col; col < end_col && col < tbl->GetNumberCols(); ++col )
787 if( !
m_grid->IsColShown( col ) )
795 if( !cols.HasMoreTokens() )
796 cols.SetString( rowTxt,
COL_SEP, wxTOKEN_RET_EMPTY );
798 wxString cellTxt = cols.GetNextToken();
802 if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) && !
isReadOnly( row, col ) )
804 tbl->SetValue( row, col, cellTxt );
806 wxGridEvent evt(
m_grid->GetId(), wxEVT_GRID_CELL_CHANGED,
m_grid, row, col );
807 m_grid->GetEventHandler()->ProcessEvent( evt );
818 if( doCopy && !wxTheClipboard->Open() )
821 wxGridTableBase* tbl =
m_grid->GetTable();
829 if( !
m_grid->IsColShown( col ) )
832 txt += tbl->GetValue( row, col );
842 tbl->SetValue( row, col, wxEmptyString );
851 wxTheClipboard->SetData(
new wxTextDataObject( txt ) );
852 wxTheClipboard->Flush();
853 wxTheClipboard->Close();
865 if(
m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
867 int cursorRow =
m_grid->GetGridCursorRow();
868 bool cursorInSelectedRow =
false;
870 for(
int row :
m_grid->GetSelectedRows() )
872 if( row == cursorRow )
874 cursorInSelectedRow =
true;
879 if( !cursorInSelectedRow && cursorRow >= 0 )
880 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