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