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