KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_tricks.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <grid_tricks.h>
26#include <wx/defs.h>
27#include <wx/event.h>
28#include <wx/tokenzr.h>
29#include <wx/clipbrd.h>
30#include <wx/log.h>
31#include <wx/stc/stc.h>
33
34
35// It works for table data on clipboard for an Excel spreadsheet,
36// why not us too for now.
37#define COL_SEP wxT( '\t' )
38#define ROW_SEP wxT( '\n' )
39#define ROW_SEP_R wxT( '\r' )
40
41
43 m_grid( aGrid ),
44 m_addHandler( []( wxCommandEvent& ) {} ),
45 m_enableSingleClickEdit( true ),
46 m_multiCellEditEnabled( true )
47{
48 init();
49}
50
51
52GRID_TRICKS::GRID_TRICKS( WX_GRID* aGrid, std::function<void( wxCommandEvent& )> aAddHandler ) :
53 m_grid( aGrid ),
54 m_addHandler( aAddHandler ),
55 m_enableSingleClickEdit( true ),
56 m_multiCellEditEnabled( true )
57{
58 init();
59}
60
61
63{
68
69 m_grid->Connect( wxEVT_GRID_CELL_LEFT_CLICK,
70 wxGridEventHandler( GRID_TRICKS::onGridCellLeftClick ), nullptr, this );
71 m_grid->Connect( wxEVT_GRID_CELL_LEFT_DCLICK,
72 wxGridEventHandler( GRID_TRICKS::onGridCellLeftDClick ), nullptr, this );
73 m_grid->Connect( wxEVT_GRID_CELL_RIGHT_CLICK,
74 wxGridEventHandler( GRID_TRICKS::onGridCellRightClick ), nullptr, this );
75 m_grid->Connect( wxEVT_GRID_LABEL_RIGHT_CLICK,
76 wxGridEventHandler( GRID_TRICKS::onGridLabelRightClick ), nullptr, this );
77 m_grid->Connect( wxEVT_GRID_LABEL_LEFT_CLICK,
78 wxGridEventHandler( GRID_TRICKS::onGridLabelLeftClick ), nullptr, this );
79 m_grid->Connect( GRIDTRICKS_FIRST_ID, GRIDTRICKS_LAST_ID, wxEVT_COMMAND_MENU_SELECTED,
80 wxCommandEventHandler( GRID_TRICKS::onPopupSelection ), nullptr, this );
81 m_grid->Connect( wxEVT_CHAR_HOOK,
82 wxCharEventHandler( GRID_TRICKS::onCharHook ), nullptr, this );
83 m_grid->Connect( wxEVT_KEY_DOWN,
84 wxKeyEventHandler( GRID_TRICKS::onKeyDown ), nullptr, this );
85 m_grid->Connect( wxEVT_UPDATE_UI,
86 wxUpdateUIEventHandler( GRID_TRICKS::onUpdateUI ), nullptr, this );
87
88 // The handlers that control the tooltips must be on the actual grid window, not the grid
89 m_grid->GetGridWindow()->Connect( wxEVT_MOTION,
90 wxMouseEventHandler( GRID_TRICKS::onGridMotion ), nullptr,
91 this );
92}
93
94
95bool GRID_TRICKS::isTextEntry( int aRow, int aCol )
96{
97 wxGridCellEditor* editor = m_grid->GetCellEditor( aRow, aCol );
98 bool retval = ( dynamic_cast<wxTextEntry*>( editor )
99 || dynamic_cast<GRID_CELL_STC_EDITOR*>( editor ) );
100
101 editor->DecRef();
102 return retval;
103}
104
105
106bool GRID_TRICKS::isCheckbox( int aRow, int aCol )
107{
108 wxGridCellRenderer* renderer = m_grid->GetCellRenderer( aRow, aCol );
109 bool retval = ( dynamic_cast<wxGridCellBoolRenderer*>( renderer ) );
110
111 renderer->DecRef();
112 return retval;
113}
114
115
116bool GRID_TRICKS::isReadOnly( int aRow, int aCol )
117{
118 return !m_grid->IsEditable() || m_grid->IsReadOnly( aRow, aCol );
119}
120
121
122bool GRID_TRICKS::toggleCell( int aRow, int aCol, bool aPreserveSelection )
123{
124 if( isCheckbox( aRow, aCol ) )
125 {
126 if( !aPreserveSelection )
127 {
128 m_grid->ClearSelection();
129 m_grid->SetGridCursor( aRow, aCol );
130 }
131
132 wxGridTableBase* model = m_grid->GetTable();
133
134 if( model->CanGetValueAs( aRow, aCol, wxGRID_VALUE_BOOL )
135 && model->CanSetValueAs( aRow, aCol, wxGRID_VALUE_BOOL ) )
136 {
137 model->SetValueAsBool( aRow, aCol, !model->GetValueAsBool( aRow, aCol ) );
138 }
139 else // fall back to string processing
140 {
141 if( model->GetValue( aRow, aCol ) == wxT( "1" ) )
142 model->SetValue( aRow, aCol, wxT( "0" ) );
143 else
144 model->SetValue( aRow, aCol, wxT( "1" ) );
145 }
146
147 // Mac needs this for the keyboard events; Linux appears to always need it.
148 m_grid->ForceRefresh();
149
150 // Let any clients know
151 wxGridEvent event( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, aRow, aCol );
152 event.SetString( model->GetValue( aRow, aCol ) );
153 m_grid->GetEventHandler()->ProcessEvent( event );
154
155 return true;
156 }
157
158 return false;
159}
160
161
162bool GRID_TRICKS::showEditor( int aRow, int aCol )
163{
164 if( m_grid->GetGridCursorRow() != aRow || m_grid->GetGridCursorCol() != aCol )
165 m_grid->SetGridCursor( aRow, aCol );
166
167 if( !isReadOnly( aRow, aCol ) )
168 {
169 m_grid->ClearSelection();
170
171 m_sel_row_start = aRow;
172 m_sel_col_start = aCol;
173 m_sel_row_count = 1;
174 m_sel_col_count = 1;
175
176 if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
177 {
178 wxArrayInt rows = m_grid->GetSelectedRows();
179
180 if( rows.size() != 1 || rows.Item( 0 ) != aRow )
181 m_grid->SelectRow( aRow );
182 }
183
184 // For several reasons we can't enable the control here. There's the whole
185 // SetInSetFocus() issue/hack in wxWidgets, and there's also wxGrid's MouseUp
186 // handler which doesn't notice it's processing a MouseUp until after it has
187 // disabled the editor yet again. So we re-use wxWidgets' slow-click hack,
188 // which is processed later in the MouseUp handler.
189 //
190 // It should be pointed out that the fact that it's wxWidgets' hack doesn't
191 // make it any less of a hack. Be extra careful with any modifications here.
192 // See, in particular, https://bugs.launchpad.net/kicad/+bug/1817965.
194
195 return true;
196 }
197
198 return false;
199}
200
201
202void GRID_TRICKS::onGridCellLeftClick( wxGridEvent& aEvent )
203{
204 int row = aEvent.GetRow();
205 int col = aEvent.GetCol();
206
207 // Don't make users click twice to toggle a checkbox or edit a text cell
208 if( !aEvent.GetModifiers() )
209 {
210 bool toggled = false;
211
212 if( toggleCell( row, col, true ) )
213 toggled = true;
214 else if( m_enableSingleClickEdit && showEditor( row, col ) )
215 return;
216
217 // Apply checkbox changes to multi-selection.
218 // Non-checkbox changes handled elsewhere
219 if( toggled )
220 {
222
223 // We only want to apply this to whole rows. If the grid allows selecting individual
224 // cells, and the selection contains dijoint cells, skip this logic.
225 if( !m_grid->GetSelectedCells().IsEmpty() || m_sel_row_count < 2 )
226 {
227 // We preserved the selection in toggleCell above; so clear it now that we know
228 // we aren't doing a multi-select edit
229 m_grid->ClearSelection();
230 return;
231 }
232
233 wxString newVal = m_grid->GetCellValue( row, col );
234
235 for( int affectedRow = m_sel_row_start; affectedRow < m_sel_row_count; ++affectedRow )
236 {
237 if( affectedRow == row )
238 continue;
239
240 m_grid->SetCellValue( affectedRow, col, newVal );
241 }
242 }
243 }
244
245 aEvent.Skip();
246}
247
248
249void GRID_TRICKS::onGridCellLeftDClick( wxGridEvent& aEvent )
250{
251 if( !handleDoubleClick( aEvent ) )
252 onGridCellLeftClick( aEvent );
253}
254
255
256void GRID_TRICKS::onGridMotion( wxMouseEvent& aEvent )
257{
258 // Always skip the event
259 aEvent.Skip();
260
261 wxPoint pt = aEvent.GetPosition();
262 wxPoint pos = m_grid->CalcScrolledPosition( wxPoint( pt.x, pt.y ) );
263
264 int col = m_grid->XToCol( pos.x );
265 int row = m_grid->YToRow( pos.y );
266
267 // Empty tooltip if the cell doesn't exist or the column doesn't have tooltips
268 if( ( col == wxNOT_FOUND ) || ( row == wxNOT_FOUND ) || !m_tooltipEnabled[col] )
269 {
270 m_grid->GetGridWindow()->SetToolTip( wxS( "" ) );
271 return;
272 }
273
274 // Set the tooltip to the string contained in the cell
275 m_grid->GetGridWindow()->SetToolTip( m_grid->GetCellValue( row, col ) );
276}
277
278
279bool GRID_TRICKS::handleDoubleClick( wxGridEvent& aEvent )
280{
281 // Double-click processing must be handled by specific sub-classes
282 return false;
283}
284
285
287{
288 wxGridCellCoordsArray topLeft = m_grid->GetSelectionBlockTopLeft();
289 wxGridCellCoordsArray botRight = m_grid->GetSelectionBlockBottomRight();
290
291 wxArrayInt cols = m_grid->GetSelectedCols();
292 wxArrayInt rows = m_grid->GetSelectedRows();
293
294 if( topLeft.Count() && botRight.Count() )
295 {
296 m_sel_row_start = topLeft[0].GetRow();
297 m_sel_col_start = topLeft[0].GetCol();
298
299 m_sel_row_count = botRight[0].GetRow() - m_sel_row_start + 1;
300 m_sel_col_count = botRight[0].GetCol() - m_sel_col_start + 1;
301 }
302 else if( cols.Count() )
303 {
304 m_sel_col_start = cols[0];
305 m_sel_col_count = cols.Count();
306 m_sel_row_start = 0;
307 m_sel_row_count = m_grid->GetNumberRows();
308 }
309 else if( rows.Count() )
310 {
311 m_sel_col_start = 0;
312 m_sel_col_count = m_grid->GetNumberCols();
313 m_sel_row_start = rows[0];
314 m_sel_row_count = rows.Count();
315 }
316 else
317 {
318 m_sel_row_start = m_grid->GetGridCursorRow();
319 m_sel_col_start = m_grid->GetGridCursorCol();
320 m_sel_row_count = m_sel_row_start >= 0 ? 1 : 0;
321 m_sel_col_count = m_sel_col_start >= 0 ? 1 : 0;
322 }
323}
324
325
326void GRID_TRICKS::onGridCellRightClick( wxGridEvent& aEvent )
327{
328 wxMenu menu;
329
330 showPopupMenu( menu, aEvent );
331}
332
333
334void GRID_TRICKS::onGridLabelLeftClick( wxGridEvent& aEvent )
335{
337
338 aEvent.Skip();
339}
340
341
343{
344 wxMenu menu;
345
346 for( int i = 0; i < m_grid->GetNumberCols(); ++i )
347 {
348 int id = GRIDTRICKS_FIRST_SHOWHIDE + i;
349 menu.AppendCheckItem( id, m_grid->GetColLabelValue( i ) );
350 menu.Check( id, m_grid->IsColShown( i ) );
351 }
352
353 m_grid->PopupMenu( &menu );
354}
355
356
357void GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
358{
359 menu.Append( GRIDTRICKS_ID_CUT, _( "Cut" ) + "\tCtrl+X",
360 _( "Clear selected cells placing original contents on clipboard" ) );
361 menu.Append( GRIDTRICKS_ID_COPY, _( "Copy" ) + "\tCtrl+C",
362 _( "Copy selected cells to clipboard" ) );
363
365 {
366 menu.Append( GRIDTRICKS_ID_PASTE, _( "Paste" ) + "\tCtrl+V",
367 _( "Paste clipboard cells to matrix at current cell" ) );
368 menu.Append( GRIDTRICKS_ID_DELETE, _( "Delete" ) + "\tDel",
369 _( "Clear contents of selected cells" ) );
370 }
371
372 menu.Append( GRIDTRICKS_ID_SELECT, _( "Select All" ) + "\tCtrl+A",
373 _( "Select all cells" ) );
374
375 menu.Enable( GRIDTRICKS_ID_CUT, false );
376 menu.Enable( GRIDTRICKS_ID_DELETE, false );
377 menu.Enable( GRIDTRICKS_ID_PASTE, false );
378
380
381 auto anyCellsWritable =
382 [&]()
383 {
384 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
385 {
386 for( int col = m_sel_col_start; col < m_sel_col_start + m_sel_col_count; ++col )
387 {
388 if( !isReadOnly( row, col ) && isTextEntry( row, col ) )
389 return true;
390 }
391 }
392
393 return false;
394 };
395
396 if( anyCellsWritable() )
397 {
398 menu.Enable( GRIDTRICKS_ID_CUT, true );
399 menu.Enable( GRIDTRICKS_ID_DELETE, true );
400 }
401
402 // Paste can overflow the selection, so don't depend on the particular cell being writeable.
403
404 wxLogNull doNotLog; // disable logging of failed clipboard actions
405
406 if( wxTheClipboard->Open() )
407 {
408 if( wxTheClipboard->IsSupported( wxDF_TEXT )
409 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
410 {
411 if( m_grid->IsEditable() )
412 menu.Enable( GRIDTRICKS_ID_PASTE, true );
413 }
414
415 wxTheClipboard->Close();
416 }
417
418 m_grid->PopupMenu( &menu );
419}
420
421
422void GRID_TRICKS::onPopupSelection( wxCommandEvent& event )
423{
424 doPopupSelection( event );
425}
426
427
428void GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
429{
430 int menu_id = event.GetId();
431
432 // assume getSelectedArea() was called by rightClickPopupMenu() and there's
433 // no way to have gotten here without that having been called.
434
435 switch( menu_id )
436 {
438 cutcopy( true, true );
439 break;
440
442 cutcopy( true, false );
443 break;
444
446 cutcopy( false, true );
447 break;
448
451 break;
452
454 m_grid->SelectAll();
455 break;
456
457 default:
458 if( menu_id >= GRIDTRICKS_FIRST_SHOWHIDE && m_grid->CommitPendingChanges( false ) )
459 {
460 int col = menu_id - GRIDTRICKS_FIRST_SHOWHIDE;
461
462 if( m_grid->IsColShown( col ) )
463 m_grid->HideCol( col );
464 else
465 m_grid->ShowCol( col );
466 }
467 }
468}
469
470
471void GRID_TRICKS::onCharHook( wxKeyEvent& ev )
472{
473 bool handled = false;
474
475 if( ( ev.GetKeyCode() == WXK_RETURN || ev.GetKeyCode() == WXK_NUMPAD_ENTER )
476 && ev.GetModifiers() == wxMOD_NONE
477 && m_grid->GetGridCursorRow() == m_grid->GetNumberRows() - 1 )
478 {
479 if( m_grid->IsCellEditControlShown() )
480 {
482 handled = true;
483 }
484 else
485 {
486 wxCommandEvent dummy;
488 handled = true;
489 }
490 }
491 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' )
492 {
493 if( m_grid->IsCellEditControlShown() && wxTheClipboard->Open() )
494 {
495 if( wxTheClipboard->IsSupported( wxDF_TEXT )
496 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
497 {
498 wxTextDataObject data;
499 wxTheClipboard->GetData( data );
500
501 if( data.GetText().Contains( COL_SEP ) || data.GetText().Contains( ROW_SEP ) )
502 {
503 wxString stripped( data.GetText() );
504 stripped.Replace( ROW_SEP, " " );
505 stripped.Replace( ROW_SEP_R, " " );
506 stripped.Replace( COL_SEP, " " );
507
508 // Write to the CellEditControl if we can
509 if( wxTextEntry* te = dynamic_cast<wxTextEntry*>( ev.GetEventObject() ) )
510 te->WriteText( stripped );
511 else
512 paste_text( stripped );
513
514 handled = true;
515 }
516 }
517
518 wxTheClipboard->Close();
519 m_grid->ForceRefresh();
520 }
521 }
522 else if( ev.GetKeyCode() == WXK_ESCAPE )
523 {
524 if( m_grid->IsCellEditControlShown() )
525 {
527 handled = true;
528 }
529 }
530
531 if( !handled )
532 ev.Skip( true );
533}
534
535
536void GRID_TRICKS::onKeyDown( wxKeyEvent& ev )
537{
538 if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'A' )
539 {
540 m_grid->SelectAll();
541 return;
542 }
543 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'C' )
544 {
546 cutcopy( true, false );
547 return;
548 }
549 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' )
550 {
553 return;
554 }
555 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'X' )
556 {
558 cutcopy( true, true );
559 return;
560 }
561 else if( !ev.GetModifiers() && ev.GetKeyCode() == WXK_DELETE )
562 {
564 cutcopy( false, true );
565 return;
566 }
567
568 // space-bar toggling of checkboxes
569 if( m_grid->IsEditable() && ev.GetKeyCode() == ' ' )
570 {
571 bool retVal = false;
572
573 // If only rows can be selected, only toggle the first cell in a row
574 if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
575 {
576 wxArrayInt rowSel = m_grid->GetSelectedRows();
577
578 for( unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
579 retVal |= toggleCell( rowSel[rowInd], 0, true );
580 }
581
582 // If only columns can be selected, only toggle the first cell in a column
583 else if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectColumns )
584 {
585 wxArrayInt colSel = m_grid->GetSelectedCols();
586
587 for( unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
588 retVal |= toggleCell( 0, colSel[colInd], true );
589 }
590
591 // If the user can select the individual cells, toggle each cell selected
592 else if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectCells )
593 {
594 wxArrayInt rowSel = m_grid->GetSelectedRows();
595 wxArrayInt colSel = m_grid->GetSelectedCols();
596 wxGridCellCoordsArray cellSel = m_grid->GetSelectedCells();
597 wxGridCellCoordsArray topLeft = m_grid->GetSelectionBlockTopLeft();
598 wxGridCellCoordsArray botRight = m_grid->GetSelectionBlockBottomRight();
599
600 // Iterate over every individually selected cell and try to toggle it
601 for( unsigned int cellInd = 0; cellInd < cellSel.GetCount(); cellInd++ )
602 {
603 retVal |= toggleCell( cellSel[cellInd].GetRow(), cellSel[cellInd].GetCol(), true );
604 }
605
606 // Iterate over every column and try to toggle each cell in it
607 for( unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
608 {
609 for( int row = 0; row < m_grid->GetNumberRows(); row++ )
610 retVal |= toggleCell( row, colSel[colInd], true );
611 }
612
613 // Iterate over every row and try to toggle each cell in it
614 for( unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
615 {
616 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
617 retVal |= toggleCell( rowSel[rowInd], col, true );
618 }
619
620 // Iterate over the selection blocks
621 for( unsigned int blockInd = 0; blockInd < topLeft.GetCount(); blockInd++ )
622 {
623 wxGridCellCoords start = topLeft[blockInd];
624 wxGridCellCoords end = botRight[blockInd];
625
626 for( int row = start.GetRow(); row <= end.GetRow(); row++ )
627 {
628 for( int col = start.GetCol(); col <= end.GetCol(); col++ )
629 retVal |= toggleCell( row, col, true );
630 }
631 }
632 }
633
634 // Return if there were any cells toggled
635 if( retVal )
636 return;
637 }
638
639 // ctrl-tab for exit grid
640#ifdef __APPLE__
641 bool ctrl = ev.RawControlDown();
642#else
643 bool ctrl = ev.ControlDown();
644#endif
645
646 if( ctrl && ev.GetKeyCode() == WXK_TAB )
647 {
648 wxWindow* test = m_grid->GetNextSibling();
649
650 if( !test )
651 test = m_grid->GetParent()->GetNextSibling();
652
653 while( test && !test->IsTopLevel() )
654 {
655 test->SetFocus();
656
657 if( test->HasFocus() )
658 break;
659
660 if( !test->GetChildren().empty() )
661 {
662 test = test->GetChildren().front();
663 }
664 else if( test->GetNextSibling() )
665 {
666 test = test->GetNextSibling();
667 }
668 else
669 {
670 while( test )
671 {
672 test = test->GetParent();
673
674 if( test && test->IsTopLevel() )
675 {
676 break;
677 }
678 else if( test && test->GetNextSibling() )
679 {
680 test = test->GetNextSibling();
681 break;
682 }
683 }
684 }
685 }
686
687 return;
688 }
689
690 ev.Skip( true );
691}
692
693
695{
696 wxLogNull doNotLog; // disable logging of failed clipboard actions
697
698 if( m_grid->IsEditable() && wxTheClipboard->Open() )
699 {
700 if( wxTheClipboard->IsSupported( wxDF_TEXT )
701 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
702 {
703 wxTextDataObject data;
704
705 wxTheClipboard->GetData( data );
706
707 wxString text = data.GetText();
708
709#ifdef __WXMAC__
710 // Some editors use windows linefeeds (\r\n), which wx re-writes to \n\n
711 text.Replace( "\n\n", "\n" );
712#endif
713
714 paste_text( text );
715 }
716
717 wxTheClipboard->Close();
718 m_grid->ForceRefresh();
719 }
720}
721
722
723void GRID_TRICKS::paste_text( const wxString& cb_text )
724{
726 return;
727
728 wxGridTableBase* tbl = m_grid->GetTable();
729
730 const int cur_row = m_grid->GetGridCursorRow();
731 const int cur_col = m_grid->GetGridCursorCol();
732 int start_row;
733 int end_row;
734 int start_col;
735 int end_col;
736 bool is_selection = false;
737
738 if( cur_row < 0 || cur_col < 0 )
739 {
740 wxBell();
741 return;
742 }
743
744 if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
745 {
746 if( m_sel_row_count > 1 )
747 is_selection = true;
748 }
749 else if( m_sel_col_count > 1 || m_sel_row_count > 1 )
750 {
751 is_selection = true;
752 }
753
754 wxStringTokenizer rows( cb_text, ROW_SEP, wxTOKEN_RET_EMPTY );
755
756 // If selection of cells is present
757 // then a clipboard pastes to selected cells only.
758 if( is_selection )
759 {
760 start_row = m_sel_row_start;
762 start_col = m_sel_col_start;
764 }
765 // Otherwise, paste whole clipboard
766 // starting from cell with cursor.
767 else
768 {
769 start_row = cur_row;
770 end_row = cur_row + rows.CountTokens();
771
772 if( end_row > tbl->GetNumberRows() )
773 {
774 if( m_addHandler )
775 {
776 for( int ii = end_row - tbl->GetNumberRows(); ii > 0; --ii )
777 {
778 wxCommandEvent dummy;
780 }
781 }
782
783 end_row = tbl->GetNumberRows();
784 }
785
786 start_col = cur_col;
787 end_col = start_col; // end_col actual value calculates later
788 }
789
790 for( int row = start_row; row < end_row; ++row )
791 {
792 // If number of selected rows is larger than the count of rows on the clipboard, paste
793 // again and again until the end of the selection is reached.
794 if( !rows.HasMoreTokens() )
795 rows.SetString( cb_text, ROW_SEP, wxTOKEN_RET_EMPTY );
796
797 wxString rowTxt = rows.GetNextToken();
798
799 wxStringTokenizer cols( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
800
801 if( !is_selection )
802 end_col = cur_col + cols.CountTokens();
803
804 for( int col = start_col; col < end_col && col < tbl->GetNumberCols(); ++col )
805 {
806 // Skip hidden columns
807 if( !m_grid->IsColShown( col ) )
808 {
809 end_col++;
810 continue;
811 }
812
813 // If number of selected cols is larger than the count of cols on the clipboard,
814 // paste again and again until the end of the selection is reached.
815 if( !cols.HasMoreTokens() )
816 cols.SetString( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
817
818 wxString cellTxt = cols.GetNextToken();
819
820 // Allow paste to anything that can take a string, including things like color
821 // swatches and checkboxes
822 if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) && !isReadOnly( row, col ) )
823 {
824 tbl->SetValue( row, col, cellTxt );
825
826 wxGridEvent evt( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
827 m_grid->GetEventHandler()->ProcessEvent( evt );
828 }
829 // Allow paste to any cell that can accept a boolean value
830 else if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_BOOL ) )
831 {
832 tbl->SetValueAsBool( row, col, cellTxt == wxT( "1" ) );
833
834 wxGridEvent evt( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
835 m_grid->GetEventHandler()->ProcessEvent( evt );
836 }
837 }
838 }
839}
840
841
842void GRID_TRICKS::cutcopy( bool doCopy, bool doDelete )
843{
844 wxLogNull doNotLog; // disable logging of failed clipboard actions
845
846 if( doCopy && !wxTheClipboard->Open() )
847 return;
848
849 wxGridTableBase* tbl = m_grid->GetTable();
850 wxString txt;
851
852 // fill txt with a format that is compatible with most spreadsheets
853 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
854 {
855 for( int col = m_sel_col_start; col < m_sel_col_start + m_sel_col_count; ++col )
856 {
857 if( !m_grid->IsColShown( col ) )
858 continue;
859
860 txt += tbl->GetValue( row, col );
861
862 if( col < m_sel_col_start + m_sel_col_count - 1 ) // that was not last column
863 txt += COL_SEP;
864
865 if( doDelete )
866 {
867 // Do NOT allow clear of things that can take strings but aren't textEntries
868 // (ie: color swatches, textboxes, etc.).
869 if( isTextEntry( row, col ) && !isReadOnly( row, col ) )
870 tbl->SetValue( row, col, wxEmptyString );
871 }
872 }
873
874 txt += ROW_SEP;
875 }
876
877 if( doCopy )
878 {
879 wxTheClipboard->SetData( new wxTextDataObject( txt ) );
880 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
881 wxTheClipboard->Close();
882 }
883
884 if( doDelete )
885 m_grid->ForceRefresh();
886}
887
888
889void GRID_TRICKS::onUpdateUI( wxUpdateUIEvent& event )
890{
891 // Respect ROW selectionMode when moving cursor
892
893 if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
894 {
895 int cursorRow = m_grid->GetGridCursorRow();
896 bool cursorInSelectedRow = false;
897
898 for( int row : m_grid->GetSelectedRows() )
899 {
900 if( row == cursorRow )
901 {
902 cursorInSelectedRow = true;
903 break;
904 }
905 }
906
907 if( !cursorInSelectedRow && cursorRow >= 0 )
908 m_grid->SelectRow( cursorRow );
909 }
910}
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.
Definition: grid_tricks.cpp:62
void getSelectedArea()
Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above.
bool m_multiCellEditEnabled
Definition: grid_tricks.h:139
GRID_TRICKS(WX_GRID *aGrid)
Definition: grid_tricks.cpp:42
void onKeyDown(wxKeyEvent &event)
void onPopupSelection(wxCommandEvent &event)
virtual void cutcopy(bool doCopy, bool doDelete)
bool m_enableSingleClickEdit
Definition: grid_tricks.h:138
virtual void doPopupSelection(wxCommandEvent &event)
bool isCheckbox(int aRow, int aCol)
std::function< void(wxCommandEvent &)> m_addHandler
Definition: grid_tricks.h:134
virtual void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent)
int m_sel_row_start
Definition: grid_tricks.h:129
std::bitset< GRIDTRICKS_MAX_COL > m_tooltipEnabled
Definition: grid_tricks.h:136
void onUpdateUI(wxUpdateUIEvent &event)
void onGridCellRightClick(wxGridEvent &event)
WX_GRID * m_grid
I don't own the grid, but he owns me.
Definition: grid_tricks.h:125
int m_sel_row_count
Definition: grid_tricks.h:131
bool isTextEntry(int aRow, int aCol)
Definition: grid_tricks.cpp:95
void onGridCellLeftDClick(wxGridEvent &event)
void onGridCellLeftClick(wxGridEvent &event)
virtual bool handleDoubleClick(wxGridEvent &aEvent)
int m_sel_col_count
Definition: grid_tricks.h:132
void onCharHook(wxKeyEvent &event)
virtual void paste_clipboard()
bool showEditor(int aRow, int aCol)
int m_sel_col_start
Definition: grid_tricks.h:130
void onGridLabelRightClick(wxGridEvent &event)
virtual bool toggleCell(int aRow, int aCol, bool aPreserveSelection=false)
bool CancelPendingChanges()
Definition: wx_grid.cpp:618
void ShowEditorOnMouseUp()
WxWidgets has a bunch of bugs in its handling of wxGrid mouse events which close cell editors right a...
Definition: wx_grid.h:185
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:646
#define _(s)
#define ROW_SEP_R
Definition: grid_tricks.cpp:39
#define COL_SEP
Definition: grid_tricks.cpp:37
#define ROW_SEP
Definition: grid_tricks.cpp:38
@ GRIDTRICKS_ID_PASTE
Definition: grid_tricks.h:45
@ GRIDTRICKS_FIRST_ID
Definition: grid_tricks.h:41
@ GRIDTRICKS_FIRST_SHOWHIDE
Definition: grid_tricks.h:51
@ GRIDTRICKS_ID_SELECT
Definition: grid_tricks.h:46
@ GRIDTRICKS_ID_CUT
Definition: grid_tricks.h:42
@ GRIDTRICKS_LAST_ID
Definition: grid_tricks.h:53
@ GRIDTRICKS_ID_COPY
Definition: grid_tricks.h:43
@ GRIDTRICKS_ID_DELETE
Definition: grid_tricks.h:44
std::vector< FAB_LAYER_COLOR > dummy