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 <kiplatform/ui.h>
23#include <wx/defs.h>
24#include <wx/event.h>
25#include <wx/tokenzr.h>
26#include <wx/clipbrd.h>
27#include <wx/combo.h>
28#include <wx/log.h>
29#include <wx/stc/stc.h>
30#include <wx/textctrl.h>
31#include <wx/toplevel.h>
32#include <wx/weakref.h>
36#include <search_stack.h>
38
39// It works for table data on clipboard for an Excel spreadsheet,
40// why not us too for now.
41#define COL_SEP wxT( '\t' )
42#define ROW_SEP wxT( '\n' )
43#define ROW_SEP_R wxT( '\r' )
44
45
46// wxBitmapComboBox is a native wxComboBox on GTK and MSW but a generic wxComboCtrl on macOS
47static bool isDropdown( wxWindow* aControl )
48{
49 return dynamic_cast<wxComboBox*>( aControl ) || dynamic_cast<wxComboCtrl*>( aControl );
50}
51
52
53static void dismissDropdown( wxWindow* aControl )
54{
55 if( wxComboBox* combo = dynamic_cast<wxComboBox*>( aControl ) )
56 combo->Dismiss();
57 else if( wxComboCtrl* comboCtrl = dynamic_cast<wxComboCtrl*>( aControl ) )
58 comboCtrl->Dismiss();
59}
60
61
63 m_grid( aGrid ),
64 m_addHandler( nullptr ),
67{
68 init();
69}
70
71
72GRID_TRICKS::GRID_TRICKS( WX_GRID* aGrid, std::function<void( wxCommandEvent& )> aAddHandler ) :
73 m_grid( aGrid ),
74 m_addHandler( aAddHandler ),
77{
78 init();
79}
80
81
83{
88
89 Bind( wxEVT_GRID_CELL_CHANGING, &GRID_TRICKS::onGridCellChanging, this );
90 Bind( wxEVT_GRID_CELL_CHANGED, &GRID_TRICKS::onGridCellChanged, this );
91 Bind( wxEVT_GRID_EDITOR_CREATED, &GRID_TRICKS::onGridEditorCreated, this );
92 Bind( wxEVT_GRID_EDITOR_SHOWN, &GRID_TRICKS::onGridEditorShown, this );
93 Bind( wxEVT_GRID_EDITOR_HIDDEN, &GRID_TRICKS::onGridEditorHidden, this );
94
95 m_grid->Bind( wxEVT_GRID_CELL_LEFT_CLICK, &GRID_TRICKS::onGridCellLeftClick, this );
96 m_grid->Bind( wxEVT_GRID_CELL_LEFT_DCLICK, &GRID_TRICKS::onGridCellLeftDClick, this );
97 m_grid->Bind( wxEVT_GRID_CELL_RIGHT_CLICK, &GRID_TRICKS::onGridCellRightClick, this );
98 m_grid->Bind( wxEVT_GRID_LABEL_RIGHT_CLICK, &GRID_TRICKS::onGridLabelRightClick, this );
99 m_grid->Bind( wxEVT_GRID_LABEL_LEFT_CLICK, &GRID_TRICKS::onGridLabelLeftClick, this );
101 m_grid->Bind( wxEVT_CHAR_HOOK, &GRID_TRICKS::onCharHook, this );
102 m_grid->Bind( wxEVT_KEY_DOWN, &GRID_TRICKS::onKeyDown, this );
103 m_grid->Bind( wxEVT_UPDATE_UI, &GRID_TRICKS::onUpdateUI, this );
104
105 // The handlers that control the tooltips must be on the actual grid window, not the grid
106 m_grid->GetGridWindow()->Bind( wxEVT_MOTION, &GRID_TRICKS::onGridMotion, this );
107}
108
109
110bool GRID_TRICKS::isTextEntry( int aRow, int aCol )
111{
112 wxGridCellEditor* editor = m_grid->GetCellEditor( aRow, aCol );
113 bool retval = ( dynamic_cast<wxGridCellTextEditor*>( editor )
114 || dynamic_cast<GRID_CELL_STC_EDITOR*>( editor )
115 || dynamic_cast<GRID_CELL_TEXT_BUTTON*>( editor ) );
116
117 editor->DecRef();
118 return retval;
119}
120
121
122bool GRID_TRICKS::isChoiceEditor( int aRow, int aCol )
123{
124 wxGridCellEditor* editor = m_grid->GetCellEditor( aRow, aCol );
125 bool retval = ( dynamic_cast<wxGridCellChoiceEditor*>( editor )
126 || dynamic_cast<GRID_CELL_ICON_TEXT_POPUP*>( editor )
127 || dynamic_cast<GRID_CELL_COMBOBOX*>( editor ) );
128
129 editor->DecRef();
130 return retval;
131}
132
133
134bool GRID_TRICKS::isCheckbox( int aRow, int aCol )
135{
136 wxGridCellRenderer* renderer = m_grid->GetCellRenderer( aRow, aCol );
137 bool retval = ( dynamic_cast<wxGridCellBoolRenderer*>( renderer ) );
138
139 renderer->DecRef();
140 return retval;
141}
142
143
144bool GRID_TRICKS::isReadOnly( int aRow, int aCol )
145{
146 return !m_grid->IsEditable() || m_grid->IsReadOnly( aRow, aCol );
147}
148
149
150bool GRID_TRICKS::toggleCell( int aRow, int aCol, bool aPreserveSelection )
151{
152 if( isCheckbox( aRow, aCol ) )
153 {
155 {
156 if( isReadOnly( aRow, aCol ) )
157 return false;
158
159 const wxString oldValue = m_grid->GetCellValue( aRow, aCol );
160 const wxString newValue = oldValue == wxT( "1" ) ? wxT( "0" ) : wxT( "1" );
161 wxGridEvent changing( m_grid->GetId(), wxEVT_GRID_CELL_CHANGING, m_grid, aRow, aCol );
162 changing.SetString( newValue );
163 m_grid->GetEventHandler()->ProcessEvent( changing );
164
165 if( !changing.IsAllowed() )
166 return true;
167
168 m_grid->SetCellValue( aRow, aCol, newValue );
169
170 wxGridEvent changed( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, aRow, aCol );
171 changed.SetString( oldValue );
172 m_grid->GetEventHandler()->ProcessEvent( changed );
173
174 if( !changed.IsAllowed() )
175 m_grid->SetCellValue( aRow, aCol, oldValue );
176
177 m_grid->ForceRefresh();
178 return true;
179 }
180
181 if( !aPreserveSelection )
182 {
183 m_grid->ClearSelection();
184 m_grid->SetGridCursor( aRow, aCol );
185 }
186
187 wxGridTableBase* model = m_grid->GetTable();
188
189 if( model->CanGetValueAs( aRow, aCol, wxGRID_VALUE_BOOL )
190 && model->CanSetValueAs( aRow, aCol, wxGRID_VALUE_BOOL ) )
191 {
192 model->SetValueAsBool( aRow, aCol, !model->GetValueAsBool( aRow, aCol ) );
193 }
194 else // fall back to string processing
195 {
196 if( model->GetValue( aRow, aCol ) == wxT( "1" ) )
197 model->SetValue( aRow, aCol, wxT( "0" ) );
198 else
199 model->SetValue( aRow, aCol, wxT( "1" ) );
200 }
201
202 // Mac needs this for the keyboard events; Linux appears to always need it.
203 m_grid->ForceRefresh();
204
205 // Let any clients know
206 wxGridEvent event( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, aRow, aCol );
207 event.SetString( model->GetValue( aRow, aCol ) );
208 m_grid->GetEventHandler()->ProcessEvent( event );
209
210 return true;
211 }
212
213 return false;
214}
215
216
217bool GRID_TRICKS::showEditor( int aRow, int aCol )
218{
219 m_bulkEditor.Selection.clear();
220
221 if( m_grid->GetGridCursorRow() != aRow || m_grid->GetGridCursorCol() != aCol )
222 m_grid->SetGridCursor( aRow, aCol );
223
224 if( !isReadOnly( aRow, aCol ) )
225 {
227 m_grid->ClearSelection();
228 else
229 {
230 if( !m_grid->IsInSelection( aRow, aCol ) )
231 m_grid->SelectBlock( aRow, aCol, aRow, aCol );
232
233 for( const wxGridBlockCoords& block : m_grid->GetSelectedBlocks() )
234 m_bulkEditor.Selection.push_back( block );
235 }
236
237 m_sel_row_start = aRow;
238 m_sel_col_start = aCol;
239 m_sel_row_count = 1;
240 m_sel_col_count = 1;
241
242 if( !m_columnBulkEditEnabled && m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
243 {
244 wxArrayInt rows = m_grid->GetSelectedRows();
245
246 if( rows.size() != 1 || rows.Item( 0 ) != aRow )
247 m_grid->SelectRow( aRow );
248 }
249
250 // For several reasons we can't enable the control here. There's the whole
251 // SetInSetFocus() issue/hack in wxWidgets, and there's also wxGrid's MouseUp
252 // handler which doesn't notice it's processing a MouseUp until after it has
253 // disabled the editor yet again. So we re-use wxWidgets' slow-click hack,
254 // which is processed later in the MouseUp handler.
255 //
256 // It should be pointed out that the fact that it's wxWidgets' hack doesn't
257 // make it any less of a hack. Be extra careful with any modifications here.
258 // See, in particular, https://bugs.launchpad.net/kicad/+bug/1817965.
259 m_grid->ShowEditorOnMouseUp();
260
261 return true;
262 }
263
264 return false;
265}
266
267
268void GRID_TRICKS::onGridCellLeftClick( wxGridEvent& aEvent )
269{
270 int row = aEvent.GetRow();
271 int col = aEvent.GetCol();
272
273 // Don't make users click twice to toggle a checkbox or edit a text cell
274 if( !aEvent.GetModifiers() )
275 {
276 // A plain click starts a new selection so an edit never fans out to cells the user forgot about
278 {
279 m_grid->SetGridCursor( row, col );
280 m_grid->SelectBlock( row, col, row, col );
281 }
282
283 bool toggled = false;
284
285 if( toggleCell( row, col, true ) )
286 {
288 return;
289
290 toggled = true;
291 }
292 else if( m_enableSingleClickEdit && showEditor( row, col ) )
293 return;
294
295 // Apply checkbox changes to multi-selection.
296 // Non-checkbox changes handled elsewhere
297 if( toggled )
298 {
300
301 // We only want to apply this to whole rows. If the grid allows selecting individual
302 // cells, and the selection contains dijoint cells, skip this logic.
303 if( !m_grid->GetSelectedCells().IsEmpty() || m_sel_row_count < 2 )
304 {
305 // We preserved the selection in toggleCell above; so clear it now that we know
306 // we aren't doing a multi-select edit
307 m_grid->ClearSelection();
308 return;
309 }
310
311 wxString newVal = m_grid->GetCellValue( row, col );
312
313 for( int otherRow = m_sel_row_start; otherRow < m_sel_row_start + m_sel_row_count; ++otherRow )
314 {
315 if( otherRow == row )
316 continue;
317
318 m_grid->SetCellValue( otherRow, col, newVal );
319 }
320
321 return;
322 }
323 }
324
325 aEvent.Skip();
326}
327
328
329void GRID_TRICKS::onGridCellChanging( wxGridEvent& aEvent )
330{
331 m_bulkEditRows.clear();
332
334 {
335 aEvent.Skip();
336 return;
337 }
338
339 const int row = aEvent.GetRow();
340 const int col = aEvent.GetCol();
341 std::vector<int> selectedRows;
342
343 if( m_bulkEditor.Commit && m_bulkEditor.Source == wxGridCellCoords( row, col ) )
344 {
345 // Copied, not moved, so a vetoed commit can be retried against the same rows
346 selectedRows = m_bulkEditor.Rows;
347 }
348 else if( m_grid->IsInSelection( row, col ) )
349 {
350 for( int otherRow = 0; otherRow < m_grid->GetNumberRows(); ++otherRow )
351 {
352 if( m_grid->IsInSelection( otherRow, col ) )
353 selectedRows.push_back( otherRow );
354 }
355 }
356
357 m_bulkEditor.Commit = false;
358 GetNextHandler()->ProcessEvent( aEvent );
359 aEvent.Skip( false );
360
361 if( !aEvent.IsAllowed() )
362 return;
363
364 for( int otherRow : selectedRows )
365 {
366 if( otherRow == row || otherRow >= m_grid->GetNumberRows() || isReadOnly( otherRow, col ) )
367 {
368 continue;
369 }
370
371 wxGridEvent changing( m_grid->GetId(), wxEVT_GRID_CELL_CHANGING, m_grid, otherRow, col );
372 changing.SetString( aEvent.GetString() );
373 GetNextHandler()->ProcessEvent( changing );
374
375 if( !changing.IsAllowed() )
376 {
377 m_bulkEditRows.clear();
378 aEvent.Veto();
379 return;
380 }
381
382 m_bulkEditRows.emplace_back( otherRow, m_grid->GetCellValue( otherRow, col ) );
383 }
384
385 m_bulkEditSource = wxGridCellCoords( row, col );
386 m_bulkEditValue = aEvent.GetString();
387}
388
389
390void GRID_TRICKS::onGridEditorCreated( wxGridEditorCreatedEvent& aEvent )
391{
393 {
394 wxWindow* control = aEvent.GetWindow();
395 control->Bind( wxEVT_TEXT, &GRID_TRICKS::onEditorInput, this );
396 control->Bind( wxEVT_COMBOBOX, &GRID_TRICKS::onEditorInput, this );
397
398 if( isDropdown( control ) )
399 {
400 control->Bind( wxEVT_TEXT_ENTER, &GRID_TRICKS::onEditorTextEnter, this );
401 control->GetEventHandler()->Bind( wxEVT_KEY_DOWN, &GRID_TRICKS::onEditorKeyDown, this );
402 control->GetEventHandler()->Bind( wxEVT_COMBOBOX_DROPDOWN, &GRID_TRICKS::onEditorDropdownOpened, this );
403 control->GetEventHandler()->Bind( wxEVT_COMBOBOX_CLOSEUP, &GRID_TRICKS::onEditorDropdownClosed, this );
404 }
405 }
406
407 aEvent.Skip();
408}
409
410
411void GRID_TRICKS::onGridEditorShown( wxGridEvent& aEvent )
412{
413 std::vector<wxGridBlockCoords> selection = std::move( m_bulkEditor.Selection );
414 m_bulkEditor.Begin( wxGridCellCoords( aEvent.GetRow(), aEvent.GetCol() ) );
415
416 for( const wxGridBlockCoords& block : selection )
417 m_grid->SelectBlock( block.GetTopRow(), block.GetLeftCol(), block.GetBottomRow(), block.GetRightCol(), true );
418
419 const int row = aEvent.GetRow();
420 const int col = aEvent.GetCol();
421
422 if( m_columnBulkEditEnabled && m_grid->IsInSelection( row, col ) )
423 {
424 for( int otherRow = 0; otherRow < m_grid->GetNumberRows(); ++otherRow )
425 {
426 if( m_grid->IsInSelection( otherRow, col ) )
427 m_bulkEditor.Rows.push_back( otherRow );
428 }
429 }
430
431 aEvent.Skip();
432}
433
434
435void GRID_TRICKS::onEditorInput( wxCommandEvent& aEvent )
436{
438 {
439 if( aEvent.GetEventType() == wxEVT_TEXT )
440 m_bulkEditor.TextInitialized = true;
441
442 aEvent.Skip();
443 return;
444 }
445
446 const int row = m_grid->GetGridCursorRow();
447 const int col = m_grid->GetGridCursorCol();
448
449 if( m_columnBulkEditEnabled && row >= 0 && col >= 0 )
450 {
451 if( aEvent.GetEventType() == wxEVT_TEXT && dynamic_cast<wxTextCtrl*>( aEvent.GetEventObject() ) )
452 {
453 if( m_bulkEditor.TextInitialized || aEvent.GetString() != m_grid->GetCellValue( row, col ) )
454 m_bulkEditor.Input = true;
455
456 m_bulkEditor.TextInitialized = true;
457 }
458 else if( aEvent.GetEventType() == wxEVT_COMBOBOX || aEvent.GetString() != m_grid->GetCellValue( row, col ) )
459 {
460 m_bulkEditor.Input = true;
461 }
462
463 if( aEvent.GetEventType() == wxEVT_COMBOBOX
464 && ( !m_bulkEditor.KeyboardNavigation || m_bulkEditor.PopupKey.GetKeyCode() != WXK_NONE ) )
465 {
467 }
468 }
469
470 aEvent.Skip();
471}
472
473
474void GRID_TRICKS::onEditorKeyDown( wxKeyEvent& aEvent )
475{
476 if( m_columnBulkEditEnabled && ( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER ) )
477 {
478 m_bulkEditorEnterHandled = !aEvent.GetModifiers();
479 }
480
481 if( wxComboCtrl* comboCtrl = shownGenericPopup( aEvent ) )
482 {
483 // The grid editor handler would take Enter before the combo, so hand it to the popup directly
484 m_bulkEditor.PopupKey = aEvent;
485 comboCtrl->GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent( aEvent );
486 return;
487 }
488
489 if( m_columnBulkEditEnabled && !aEvent.GetModifiers() )
490 {
491 switch( aEvent.GetKeyCode() )
492 {
493 case WXK_UP:
494 case WXK_DOWN:
495 case WXK_HOME:
496 case WXK_END:
497 case WXK_PAGEUP:
498 case WXK_PAGEDOWN: m_bulkEditor.KeyboardNavigation = true; break;
499
500 case WXK_RETURN:
501 case WXK_NUMPAD_ENTER: finishEditor( false, true ); return;
502
503 case WXK_ESCAPE: finishEditor( true ); return;
504 }
505 }
506
507 aEvent.Skip();
508}
509
510
511void GRID_TRICKS::onEditorTextEnter( wxCommandEvent& aEvent )
512{
514 aEvent.Skip();
515}
516
517
518void GRID_TRICKS::onEditorDropdownOpened( wxCommandEvent& aEvent )
519{
521 {
522 wxWindow* control = dynamic_cast<wxWindow*>( aEvent.GetEventObject() );
523 wxCHECK_RET( control, wxT( "Missing grid dropdown control" ) );
524
525 if( !KIPLATFORM::UI::PrepareChoicePopup( control ) )
526 {
527 CallAfter(
528 [control = wxWeakRef<wxWindow>( control )]()
529 {
530 if( control )
532 } );
533 }
534 }
535
537 aEvent.Skip();
538}
539
540
541void GRID_TRICKS::onEditorDropdownClosed( wxCommandEvent& aEvent )
542{
544 {
545 aEvent.Skip();
546 return;
547 }
548
549 if( m_bulkEditorClosing || !m_grid->IsCellEditControlEnabled() )
550 return;
551
552 // A generic popup closes before it reports the choice, so keep the Enter it was given
553 if( m_bulkEditor.PopupKey.GetKeyCode() == WXK_NONE )
555
556 wxWeakRef<WX_GRID> grid( m_grid );
557 const unsigned int generation = m_bulkEditor.Generation;
558
559 CallAfter(
560 [this, grid, generation]()
561 {
562 if( grid && m_bulkEditor.Generation == generation && grid->IsCellEditControlEnabled() )
564 } );
565}
566
567
569{
570 const wxKeyEvent key = m_bulkEditor.PopupKey;
571 const bool enter = key.GetKeyCode() == WXK_RETURN || key.GetKeyCode() == WXK_NUMPAD_ENTER;
572 const bool committed = finishEditor( key.GetKeyCode() == WXK_ESCAPE, enter && !key.GetModifiers() );
573
574 if( committed && enter && key.GetModifiers() == wxMOD_SHIFT )
575 {
576 if( wxTopLevelWindow* window = dynamic_cast<wxTopLevelWindow*>( wxGetTopLevelParent( m_grid ) ) )
577 {
578 wxWindow* button = window->GetDefaultItem();
579
580 if( button && button->IsEnabled() )
581 {
582 wxCommandEvent accept( wxEVT_BUTTON, button->GetId() );
583 accept.SetEventObject( button );
584 wxPostEvent( button, accept );
585 }
586 }
587 }
588}
589
590
591wxComboCtrl* GRID_TRICKS::shownGenericPopup( const wxKeyEvent& aEnterKey )
592{
593 if( !m_columnBulkEditEnabled || aEnterKey.GetModifiers() || !m_grid->IsCellEditControlEnabled()
594 || ( aEnterKey.GetKeyCode() != WXK_RETURN && aEnterKey.GetKeyCode() != WXK_NUMPAD_ENTER ) )
595 {
596 return nullptr;
597 }
598
599 wxGridCellEditor* editor = m_grid->GetCellEditor( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
600 wxComboCtrl* comboCtrl = dynamic_cast<wxComboCtrl*>( editor->GetWindow() );
601 editor->DecRef();
602
603 // Native popups grab the keyboard, so only a generic one can be open while its control sees keys
604 return comboCtrl && comboCtrl->IsPopupShown() ? comboCtrl : nullptr;
605}
606
607
608bool GRID_TRICKS::finishEditor( bool aCancel, bool aMoveDown )
609{
610 if( m_bulkEditorClosing || !m_grid->IsCellEditControlEnabled() )
611 return false;
612
613 m_bulkEditorClosing = true;
614
615 const int row = m_grid->GetGridCursorRow();
616 const int col = m_grid->GetGridCursorCol();
617 int nextRow = row + 1;
618
619 if( aMoveDown )
620 {
621 while( nextRow < m_grid->GetNumberRows()
622 && ( m_grid->IsInSelection( nextRow, col ) || !m_grid->IsRowShown( nextRow ) ) )
623 {
624 ++nextRow;
625 }
626 }
627
628 wxGridCellEditor* editor = m_grid->GetCellEditor( row, col );
629
630 dismissDropdown( editor->GetWindow() );
631
632 bool committed = false;
633
634 if( aCancel )
635 {
636 m_bulkEditor.Input = false;
637 m_bulkEditor.Rows.clear();
638 m_grid->CancelPendingChanges();
639 }
640 else
641 {
642 committed = m_grid->CommitPendingChanges();
643 }
644
645 editor->DecRef();
646
647 if( committed && aMoveDown && nextRow < m_grid->GetNumberRows() )
648 {
649 m_grid->GoToCell( nextRow, col );
650
651 if( m_grid->GetGridCursorRow() == nextRow && m_grid->GetGridCursorCol() == col )
652 m_grid->SelectBlock( nextRow, col, nextRow, col );
653 }
654
655 m_bulkEditorClosing = false;
656 return committed;
657}
658
659
660void GRID_TRICKS::onGridEditorHidden( wxGridEvent& aEvent )
661{
663 {
664 aEvent.Skip();
665 return;
666 }
667
668 const int row = aEvent.GetRow();
669 const int col = aEvent.GetCol();
670 wxGridCellEditor* editor = m_grid->GetCellEditor( row, col );
671
672 if( !editor->IsCreated() )
673 {
674 editor->DecRef();
675 m_bulkEditor.Rows.clear();
676 aEvent.Skip();
677 return;
678 }
679
680 const wxString value = editor->GetValue();
681 editor->DecRef();
682
683 if( value == m_grid->GetCellValue( row, col ) && !m_bulkEditor.Input )
684 {
685 m_bulkEditor.Rows.clear();
686 aEvent.Skip();
687 return;
688 }
689
690 m_bulkEditor.Commit = true;
691 GetNextHandler()->ProcessEvent( aEvent );
692 aEvent.Skip( false );
693
694 if( !aEvent.IsAllowed() )
695 {
696 m_bulkEditor.Commit = false;
697 return;
698 }
699
700 if( value != m_grid->GetCellValue( row, col ) )
701 {
702 m_bulkEditor.Input = false;
703 return;
704 }
705
706 wxGridEvent changing( m_grid->GetId(), wxEVT_GRID_CELL_CHANGING, m_grid, row, col );
707 changing.SetString( value );
708 m_grid->GetEventHandler()->ProcessEvent( changing );
709
710 if( !changing.IsAllowed() )
711 {
712 aEvent.Veto();
713 return;
714 }
715
716 m_bulkEditor.Input = false;
717
718 if( !m_bulkEditRows.empty() )
719 {
720 wxGridEvent changed( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
721 changed.SetString( value );
722 m_grid->GetEventHandler()->ProcessEvent( changed );
723
724 if( !changed.IsAllowed() )
725 aEvent.Veto();
726 }
727}
728
729
730void GRID_TRICKS::onGridCellChanged( wxGridEvent& aEvent )
731{
732 if( m_columnBulkEditEnabled && m_bulkEditSource == wxGridCellCoords( aEvent.GetRow(), aEvent.GetCol() )
733 && !m_bulkEditRows.empty() )
734 {
735 const int col = aEvent.GetCol();
736 const wxString sourceOldValue = aEvent.GetString();
737 std::vector<std::pair<int, wxString>> rows;
738 rows.swap( m_bulkEditRows );
739
740 wxGridUpdateLocker lock( m_grid );
741
742 for( const auto& [row, oldValue] : rows )
743 m_grid->SetCellValue( row, col, m_bulkEditValue );
744
745 bool allowed = true;
746
747 for( const auto& [row, oldValue] : rows )
748 {
749 wxGridEvent changed( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
750 changed.SetString( oldValue );
751 GetNextHandler()->ProcessEvent( changed );
752
753 if( !changed.IsAllowed() )
754 {
755 allowed = false;
756 break;
757 }
758 }
759
760 if( allowed )
761 {
762 GetNextHandler()->ProcessEvent( aEvent );
763 allowed = aEvent.IsAllowed();
764 }
765
766 aEvent.Skip( false );
767
768 if( !allowed )
769 {
770 for( const auto& [row, oldValue] : rows )
771 m_grid->SetCellValue( row, col, oldValue );
772
773 m_grid->SetCellValue( aEvent.GetRow(), col, sourceOldValue );
774 aEvent.Veto();
775 }
776 else
777 {
778 m_grid->EvaluateCell( aEvent.GetRow(), col );
779
780 for( const auto& [row, oldValue] : rows )
781 m_grid->EvaluateCell( row, col );
782 }
783
784 m_grid->ForceRefresh();
785 return;
786 }
787
788 aEvent.Skip();
789}
790
791
792void GRID_TRICKS::onGridCellLeftDClick( wxGridEvent& aEvent )
793{
794 if( !handleDoubleClick( aEvent ) )
795 onGridCellLeftClick( aEvent );
796}
797
798
799void GRID_TRICKS::onGridMotion( wxMouseEvent& aEvent )
800{
801 // Always skip the event
802 aEvent.Skip();
803
804 wxPoint pt = aEvent.GetPosition();
805 wxPoint pos = m_grid->CalcUnscrolledPosition( wxPoint( pt.x, pt.y ) );
806
807 int col = m_grid->XToCol( pos.x );
808 int row = m_grid->YToRow( pos.y );
809
810 // Empty tooltip if the cell doesn't exist or the column doesn't have tooltips
811 if( col < 0 || row < 0 || static_cast<size_t>( col ) >= m_tooltipEnabled.size() || !m_tooltipEnabled[col] )
812 {
813 m_grid->GetGridWindow()->SetToolTip( wxS( "" ) );
814 return;
815 }
816
817 // Set the tooltip to the string contained in the cell
818 m_grid->GetGridWindow()->SetToolTip( m_grid->GetCellValue( row, col ) );
819}
820
821
822bool GRID_TRICKS::handleDoubleClick( wxGridEvent& aEvent )
823{
824 // Double-click processing must be handled by specific sub-classes
825 return false;
826}
827
828
830{
831 if( m_columnBulkEditEnabled && m_grid->IsSelection() )
832 {
833 int firstRow = m_grid->GetNumberRows();
834 int firstCol = m_grid->GetNumberCols();
835 int lastRow = -1;
836 int lastCol = -1;
837
838 for( const wxGridBlockCoords& block : m_grid->GetSelectedBlocks() )
839 {
840 firstRow = std::min( firstRow, block.GetTopRow() );
841 firstCol = std::min( firstCol, block.GetLeftCol() );
842 lastRow = std::max( lastRow, block.GetBottomRow() );
843 lastCol = std::max( lastCol, block.GetRightCol() );
844 }
845
846 m_sel_row_start = firstRow;
847 m_sel_col_start = firstCol;
848 m_sel_row_count = lastRow - firstRow + 1;
849 m_sel_col_count = lastCol - firstCol + 1;
850 return;
851 }
852
853 wxGridCellCoordsArray topLeft = m_grid->GetSelectionBlockTopLeft();
854 wxGridCellCoordsArray botRight = m_grid->GetSelectionBlockBottomRight();
855
856 wxArrayInt cols = m_grid->GetSelectedCols();
857 wxArrayInt rows = m_grid->GetSelectedRows();
858
859 if( topLeft.Count() && botRight.Count() )
860 {
861 m_sel_row_start = topLeft[0].GetRow();
862 m_sel_col_start = topLeft[0].GetCol();
863
864 m_sel_row_count = botRight[0].GetRow() - m_sel_row_start + 1;
865 m_sel_col_count = botRight[0].GetCol() - m_sel_col_start + 1;
866 }
867 else if( cols.Count() )
868 {
869 m_sel_col_start = cols[0];
870 m_sel_col_count = cols.Count();
871 m_sel_row_start = 0;
872 m_sel_row_count = m_grid->GetNumberRows();
873 }
874 else if( rows.Count() )
875 {
876 m_sel_col_start = 0;
877 m_sel_col_count = m_grid->GetNumberCols();
879 m_sel_row_count = rows.Count();
880 }
881 else
882 {
883 m_sel_row_start = m_grid->GetGridCursorRow();
884 m_sel_col_start = m_grid->GetGridCursorCol();
885 m_sel_row_count = m_sel_row_start >= 0 ? 1 : 0;
886 m_sel_col_count = m_sel_col_start >= 0 ? 1 : 0;
887 }
888}
889
890
891void GRID_TRICKS::onGridCellRightClick( wxGridEvent& aEvent )
892{
893 if( !m_grid->CommitPendingChanges( !m_columnBulkEditEnabled ) )
894 return;
895
896 // Select the right-clicked row if it is not already part of the current selection
897 int row = aEvent.GetRow();
898
899 if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
900 {
901 wxArrayInt selectedRows = m_grid->GetSelectedRows();
902
903 if( selectedRows.Index( row ) == wxNOT_FOUND )
904 m_grid->SelectRow( row );
905 }
906 else
907 {
908 if( !m_grid->IsInSelection( row, aEvent.GetCol() ) )
909 {
910 m_grid->SetGridCursor( row, aEvent.GetCol() );
911
913 m_grid->SelectBlock( row, aEvent.GetCol(), row, aEvent.GetCol() );
914 }
915 }
916
917 wxMenu menu;
918
919 showPopupMenu( menu, aEvent );
920}
921
922
923void GRID_TRICKS::onGridLabelLeftClick( wxGridEvent& aEvent )
924{
925 m_grid->CommitPendingChanges();
926
927 aEvent.Skip();
928}
929
930
932{
933 wxMenu menu;
934
935 for( int i = 0; i < m_grid->GetNumberCols(); ++i )
936 {
937 int id = GRIDTRICKS_FIRST_SHOWHIDE + i;
938 menu.AppendCheckItem( id, m_grid->GetColLabelValue( i ) );
939 menu.Check( id, m_grid->IsColShown( i ) );
940 }
941
942 m_grid->PopupMenu( &menu );
943}
944
945
946void GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
947{
948 menu.Append( GRIDTRICKS_ID_CUT, _( "Cut" ) + "\tCtrl+X",
949 _( "Clear selected cells placing original contents on clipboard" ) );
950 menu.Append( GRIDTRICKS_ID_COPY, _( "Copy" ) + "\tCtrl+C",
951 _( "Copy selected cells to clipboard" ) );
952
954 {
955 menu.Append( GRIDTRICKS_ID_PASTE, _( "Paste" ) + "\tCtrl+V",
956 _( "Paste clipboard cells to matrix at current cell" ) );
957 menu.Append( GRIDTRICKS_ID_DELETE, _( "Delete" ) + "\tDel",
958 _( "Clear contents of selected cells" ) );
959 }
960
961 menu.Append( GRIDTRICKS_ID_SELECT, _( "Select All" ) + "\tCtrl+A",
962 _( "Select all cells" ) );
963
964 menu.Enable( GRIDTRICKS_ID_CUT, false );
965 menu.Enable( GRIDTRICKS_ID_DELETE, false );
966 menu.Enable( GRIDTRICKS_ID_PASTE, false );
967
969
970 auto anyCellsWritable =
971 [&]()
972 {
973 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
974 {
975 for( int col = m_sel_col_start; col < m_sel_col_start + m_sel_col_count; ++col )
976 {
977 if( !isReadOnly( row, col )
978 && ( isTextEntry( row, col ) || isChoiceEditor( row, col ) ) )
979 {
980 return true;
981 }
982 }
983 }
984
985 return false;
986 };
987
988 if( anyCellsWritable() )
989 {
990 menu.Enable( GRIDTRICKS_ID_CUT, true );
991 menu.Enable( GRIDTRICKS_ID_DELETE, true );
992 }
993
994 // Paste can overflow the selection, so don't depend on the particular cell being writeable.
995
996 wxLogNull doNotLog; // disable logging of failed clipboard actions
997
998 if( wxTheClipboard->Open() )
999 {
1000 if( wxTheClipboard->IsSupported( wxDF_TEXT )
1001 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
1002 {
1003 if( m_grid->IsEditable() )
1004 menu.Enable( GRIDTRICKS_ID_PASTE, true );
1005 }
1006
1007 wxTheClipboard->Close();
1008 }
1009
1010 appendPopupMenuItems( menu );
1011 m_grid->PopupMenu( &menu );
1012}
1013
1014
1015void GRID_TRICKS::onPopupSelection( wxCommandEvent& event )
1016{
1017 doPopupSelection( event );
1018}
1019
1020
1021void GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
1022{
1023 int menu_id = event.GetId();
1024
1025 // assume getSelectedArea() was called by rightClickPopupMenu() and there's
1026 // no way to have gotten here without that having been called.
1027
1028 switch( menu_id )
1029 {
1030 case GRIDTRICKS_ID_CUT:
1031 cutcopy( true, true );
1032 break;
1033
1034 case GRIDTRICKS_ID_COPY:
1035 cutcopy( true, false );
1036 break;
1037
1039 cutcopy( false, true );
1040 break;
1041
1044 break;
1045
1047 m_grid->SelectAll();
1048 break;
1049
1050 default:
1051 if( menu_id >= GRIDTRICKS_FIRST_SHOWHIDE && m_grid->CommitPendingChanges( false ) )
1052 {
1053 int col = menu_id - GRIDTRICKS_FIRST_SHOWHIDE;
1054
1055 if( m_grid->IsColShown( col ) )
1056 m_grid->HideCol( col );
1057 else
1058 m_grid->ShowCol( col );
1059 }
1060 }
1061}
1062
1063
1064void GRID_TRICKS::onCharHook( wxKeyEvent& ev )
1065{
1066 bool handled = false;
1067
1068 if( m_columnBulkEditEnabled && ( ev.GetKeyCode() == WXK_RETURN || ev.GetKeyCode() == WXK_NUMPAD_ENTER ) )
1069 {
1070 m_bulkEditorEnterHandled = !ev.GetModifiers();
1071 }
1072
1073 // Let Enter arrive as KEY_DOWN so onEditorKeyDown() can pass it to the open popup
1074 if( shownGenericPopup( ev ) )
1075 {
1076 ev.Skip();
1077 return;
1078 }
1079
1080 if( m_columnBulkEditEnabled && !ev.GetModifiers()
1081 && ( ev.GetKeyCode() == WXK_RETURN || ev.GetKeyCode() == WXK_NUMPAD_ENTER ) )
1082 {
1083 const int row = m_grid->GetGridCursorRow();
1084 const int col = m_grid->GetGridCursorCol();
1085
1086 if( m_grid->IsCellEditControlEnabled() )
1087 {
1088 finishEditor( false, true );
1089 }
1090 else if( m_addHandler && row == m_grid->GetNumberRows() - 1 && m_grid->GetSelectedRowIndices().size() <= 1 )
1091 {
1092 wxCommandEvent dummy;
1094 }
1095 else if( row >= 0 && col >= 0 && !isReadOnly( row, col ) )
1096 {
1097 if( !m_grid->IsInSelection( row, col ) )
1098 m_grid->SelectBlock( row, col, row, col );
1099
1100 m_bulkEditorOpening = true;
1101 m_grid->EnableCellEditControl();
1102 m_bulkEditorOpening = false;
1103
1104 if( m_grid->IsCellEditControlEnabled() )
1105 {
1106 wxGridCellEditor* editor = m_grid->GetCellEditor( row, col );
1107
1108#ifndef __WXOSX__
1109 // The macOS editors already open their popup in BeginEdit
1110 if( wxComboBox* combo = dynamic_cast<wxComboBox*>( editor->GetWindow() ) )
1111 {
1112 auto handler = static_cast<wxGridCellEditorEvtHandler*>( combo->GetEventHandler() );
1113 m_bulkEditorOpening = true;
1114 handler->SetInSetFocus( true );
1115 combo->Popup();
1116 handler->SetInSetFocus( false );
1117 m_bulkEditorOpening = false;
1118 }
1119#endif
1120
1121 editor->DecRef();
1122 }
1123 }
1124
1125 handled = true;
1126 }
1127 else if( ( ev.GetKeyCode() == WXK_RETURN || ev.GetKeyCode() == WXK_NUMPAD_ENTER ) && ev.GetModifiers() == wxMOD_NONE
1128 && m_grid->GetGridCursorRow() == m_grid->GetNumberRows() - 1 )
1129 {
1130 if( m_grid->IsCellEditControlShown() )
1131 {
1132 if( m_grid->CommitPendingChanges() )
1133 handled = true;
1134 }
1135 else
1136 {
1137 if( m_addHandler )
1138 {
1139 wxCommandEvent dummy;
1141 }
1142
1143 handled = true;
1144 }
1145 }
1146 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'C' )
1147 {
1148 if( m_grid->IsCellEditControlShown() )
1149 {
1150 wxTextEntry* te = dynamic_cast<wxTextEntry*>( ev.GetEventObject() );
1151
1152 if( te )
1153 {
1154 wxString selectedText = te->GetStringSelection();
1155
1156 if( !selectedText.IsEmpty() )
1157 {
1158 wxLogNull doNotLog;
1159
1160 if( wxTheClipboard->Open() )
1161 {
1162 wxTheClipboard->SetData( new wxTextDataObject( selectedText ) );
1163 wxTheClipboard->Flush();
1164 wxTheClipboard->Close();
1165 handled = true;
1166 }
1167 }
1168 }
1169
1170 if( !handled )
1171 {
1172 m_grid->CancelPendingChanges();
1174 cutcopy( true, false );
1175 handled = true;
1176 }
1177 }
1178 }
1179 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' )
1180 {
1181 if( m_grid->IsCellEditControlShown() )
1182 {
1183 wxLogNull doNotLog;
1184
1185 if( wxTheClipboard->Open() )
1186 {
1187 if( wxTheClipboard->IsSupported( wxDF_TEXT )
1188 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
1189 {
1190 wxTextDataObject data;
1191 wxTheClipboard->GetData( data );
1192 wxString text = data.GetText();
1193
1194 bool hasMultipleCells = text.Contains( COL_SEP ) || text.Contains( ROW_SEP );
1195
1196 if( hasMultipleCells )
1197 {
1198 text.Replace( ROW_SEP, wxS( " " ) );
1199 text.Replace( ROW_SEP_R, wxS( " " ) );
1200 text.Replace( COL_SEP, wxS( " " ) );
1201 }
1202
1203 wxTextEntry* te = dynamic_cast<wxTextEntry*>( ev.GetEventObject() );
1204
1205 if( te && te->IsEditable() )
1206 {
1207 te->WriteText( text );
1208 handled = true;
1209 }
1210 else
1211 {
1212 m_grid->CancelPendingChanges();
1214 paste_text( text );
1215 handled = true;
1216 }
1217 }
1218
1219 wxTheClipboard->Close();
1220 m_grid->ForceRefresh();
1221 }
1222 }
1223 }
1224 else if( ev.GetKeyCode() == WXK_ESCAPE )
1225 {
1226 if( m_grid->IsCellEditControlShown() )
1227 {
1229 finishEditor( true );
1230 else
1231 m_grid->CancelPendingChanges();
1232
1233 handled = true;
1234 }
1235 }
1236
1237 if( !handled )
1238 ev.Skip( true );
1239}
1240
1241
1242void GRID_TRICKS::onKeyDown( wxKeyEvent& ev )
1243{
1244 if( m_columnBulkEditEnabled && !ev.GetModifiers()
1245 && ( ev.GetKeyCode() == WXK_RETURN || ev.GetKeyCode() == WXK_NUMPAD_ENTER ) )
1246 {
1247 onCharHook( ev );
1248 return;
1249 }
1250 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'A' )
1251 {
1252 m_grid->SelectAll();
1253 return;
1254 }
1255 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'C' )
1256 {
1258 cutcopy( true, false );
1259 return;
1260 }
1261 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' )
1262 {
1265 return;
1266 }
1267 else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'X' )
1268 {
1270 cutcopy( true, true );
1271 return;
1272 }
1273 else if( !ev.GetModifiers() && ev.GetKeyCode() == WXK_DELETE )
1274 {
1276 cutcopy( false, true );
1277 return;
1278 }
1279
1280 // space-bar toggling of checkboxes
1281 if( m_grid->IsEditable() && ev.GetKeyCode() == ' ' )
1282 {
1284 {
1285 const int row = m_grid->GetGridCursorRow();
1286 const int col = m_grid->GetGridCursorCol();
1287
1288 if( row >= 0 && col >= 0 && toggleCell( row, col, true ) )
1289 return;
1290
1291 ev.Skip();
1292 return;
1293 }
1294
1295 bool retVal = false;
1296
1297 // If only rows can be selected, only toggle the first cell in a row
1298 if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
1299 {
1300 wxArrayInt rowSel = m_grid->GetSelectedRows();
1301
1302 for( unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
1303 retVal |= toggleCell( rowSel[rowInd], 0, true );
1304 }
1305
1306 // If only columns can be selected, only toggle the first cell in a column
1307 else if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectColumns )
1308 {
1309 wxArrayInt colSel = m_grid->GetSelectedCols();
1310
1311 for( unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
1312 retVal |= toggleCell( 0, colSel[colInd], true );
1313 }
1314
1315 // If the user can select the individual cells, toggle each cell selected
1316 else if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectCells )
1317 {
1318 wxArrayInt rowSel = m_grid->GetSelectedRows();
1319 wxArrayInt colSel = m_grid->GetSelectedCols();
1320 wxGridCellCoordsArray cellSel = m_grid->GetSelectedCells();
1321 wxGridCellCoordsArray topLeft = m_grid->GetSelectionBlockTopLeft();
1322 wxGridCellCoordsArray botRight = m_grid->GetSelectionBlockBottomRight();
1323
1324 // Iterate over every individually selected cell and try to toggle it
1325 for( unsigned int cellInd = 0; cellInd < cellSel.GetCount(); cellInd++ )
1326 {
1327 retVal |= toggleCell( cellSel[cellInd].GetRow(), cellSel[cellInd].GetCol(), true );
1328 }
1329
1330 // Iterate over every column and try to toggle each cell in it
1331 for( unsigned int colInd = 0; colInd < colSel.GetCount(); colInd++ )
1332 {
1333 for( int row = 0; row < m_grid->GetNumberRows(); row++ )
1334 retVal |= toggleCell( row, colSel[colInd], true );
1335 }
1336
1337 // Iterate over every row and try to toggle each cell in it
1338 for( unsigned int rowInd = 0; rowInd < rowSel.GetCount(); rowInd++ )
1339 {
1340 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
1341 retVal |= toggleCell( rowSel[rowInd], col, true );
1342 }
1343
1344 // Iterate over the selection blocks
1345 for( unsigned int blockInd = 0; blockInd < topLeft.GetCount(); blockInd++ )
1346 {
1347 wxGridCellCoords start = topLeft[blockInd];
1348 wxGridCellCoords end = botRight[blockInd];
1349
1350 for( int row = start.GetRow(); row <= end.GetRow(); row++ )
1351 {
1352 for( int col = start.GetCol(); col <= end.GetCol(); col++ )
1353 retVal |= toggleCell( row, col, true );
1354 }
1355 }
1356 }
1357
1358 // Return if there were any cells toggled
1359 if( retVal )
1360 return;
1361 }
1362
1363 // ctrl-tab for exit grid
1364#ifdef __APPLE__
1365 bool ctrl = ev.RawControlDown();
1366#else
1367 bool ctrl = ev.ControlDown();
1368#endif
1369
1370 if( ctrl && ev.GetKeyCode() == WXK_TAB )
1371 {
1372 wxWindow* test = m_grid->GetNextSibling();
1373
1374 if( !test )
1375 test = m_grid->GetParent()->GetNextSibling();
1376
1377 while( test && !test->IsTopLevel() )
1378 {
1379 test->SetFocus();
1380
1381 if( test->HasFocus() )
1382 break;
1383
1384 if( !test->GetChildren().empty() )
1385 {
1386 test = test->GetChildren().front();
1387 }
1388 else if( test->GetNextSibling() )
1389 {
1390 test = test->GetNextSibling();
1391 }
1392 else
1393 {
1394 while( test )
1395 {
1396 test = test->GetParent();
1397
1398 if( test && test->IsTopLevel() )
1399 {
1400 break;
1401 }
1402 else if( test && test->GetNextSibling() )
1403 {
1404 test = test->GetNextSibling();
1405 break;
1406 }
1407 }
1408 }
1409 }
1410
1411 return;
1412 }
1413
1414 ev.Skip( true );
1415}
1416
1417
1419{
1420 wxLogNull doNotLog; // disable logging of failed clipboard actions
1421
1422 if( m_grid->IsEditable() && ( wxTheClipboard->IsOpened() || wxTheClipboard->Open() ) )
1423 {
1424 if( wxTheClipboard->IsSupported( wxDF_TEXT )
1425 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
1426 {
1427 wxTextDataObject data;
1428
1429 wxTheClipboard->GetData( data );
1430
1431 wxString text = data.GetText();
1432
1433#ifdef __WXMAC__
1434 // Some editors use windows linefeeds (\r\n), which wx re-writes to \n\n
1435 text.Replace( "\n\n", "\n" );
1436#endif
1437 if( m_grid->CommitPendingChanges( !m_columnBulkEditEnabled ) )
1438 paste_text( text );
1439 }
1440
1441 wxTheClipboard->Close();
1442 m_grid->ForceRefresh();
1443 }
1444}
1445
1446
1447void GRID_TRICKS::paste_text( const wxString& cb_text )
1448{
1450 return;
1451
1452 wxGridTableBase* tbl = m_grid->GetTable();
1453
1454 const int cur_row = m_grid->GetGridCursorRow();
1455 const int cur_col = m_grid->GetGridCursorCol();
1456 int start_row;
1457 int end_row;
1458 int start_col;
1459 int end_col;
1460 bool is_selection = false;
1461
1462 if( cur_row < 0 || cur_col < 0 )
1463 {
1464 wxBell();
1465 return;
1466 }
1467
1468 if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
1469 {
1470 if( m_sel_row_count > 1 )
1471 is_selection = true;
1472 }
1473 else if( m_sel_col_count > 1 || m_sel_row_count > 1 )
1474 {
1475 is_selection = true;
1476 }
1477
1478 wxStringTokenizer rows( cb_text, ROW_SEP, wxTOKEN_RET_EMPTY );
1479
1480 // If selection of cells is present
1481 // then a clipboard pastes to selected cells only.
1482 if( is_selection )
1483 {
1484 start_row = m_sel_row_start;
1485 end_row = m_sel_row_start + m_sel_row_count;
1486 start_col = m_sel_col_start;
1487 end_col = m_sel_col_start + m_sel_col_count;
1488 }
1489 // Otherwise, paste whole clipboard
1490 // starting from cell with cursor.
1491 else
1492 {
1493 start_row = cur_row;
1494 end_row = cur_row + rows.CountTokens();
1495
1496 if( end_row > tbl->GetNumberRows() )
1497 {
1498 if( m_addHandler )
1499 {
1500 for( int ii = end_row - tbl->GetNumberRows(); ii > 0; --ii )
1501 {
1502 wxCommandEvent dummy;
1504 }
1505 }
1506
1507 end_row = tbl->GetNumberRows();
1508 }
1509
1510 start_col = cur_col;
1511 end_col = start_col; // end_col actual value calculates later
1512 }
1513
1514 for( int row = start_row; row < end_row; ++row )
1515 {
1516 // If number of selected rows is larger than the count of rows on the clipboard, paste
1517 // again and again until the end of the selection is reached.
1518 if( !rows.HasMoreTokens() )
1519 rows.SetString( cb_text, ROW_SEP, wxTOKEN_RET_EMPTY );
1520
1521 wxString rowTxt = rows.GetNextToken();
1522
1523 wxStringTokenizer cols( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
1524
1525 if( !is_selection )
1526 end_col = cur_col + cols.CountTokens();
1527
1528 for( int col = start_col; col < end_col && col < tbl->GetNumberCols(); ++col )
1529 {
1530 // Skip hidden columns
1531 if( !m_grid->IsColShown( col ) )
1532 {
1533 end_col++;
1534 continue;
1535 }
1536
1537 // If number of selected cols is larger than the count of cols on the clipboard,
1538 // paste again and again until the end of the selection is reached.
1539 if( !cols.HasMoreTokens() )
1540 cols.SetString( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
1541
1542 wxString cellTxt = cols.GetNextToken();
1543
1544 if( m_columnBulkEditEnabled && is_selection && !m_grid->IsInSelection( row, col ) )
1545 continue;
1546
1547 // Allow paste to anything that can take a string, including things like color
1548 // swatches and checkboxes
1549 if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_STRING ) && !isReadOnly( row, col ) )
1550 {
1551 tbl->SetValue( row, col, cellTxt );
1552
1553 wxGridEvent evt( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
1554 m_grid->GetEventHandler()->ProcessEvent( evt );
1555 }
1556 // Allow paste to any cell that can accept a boolean value
1557 else if( tbl->CanSetValueAs( row, col, wxGRID_VALUE_BOOL ) )
1558 {
1559 tbl->SetValueAsBool( row, col, cellTxt == wxT( "1" ) );
1560
1561 wxGridEvent evt( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
1562 m_grid->GetEventHandler()->ProcessEvent( evt );
1563 }
1564 }
1565 }
1566}
1567
1568
1569void GRID_TRICKS::cutcopy( bool doCopy, bool doDelete )
1570{
1571 wxLogNull doNotLog; // disable logging of failed clipboard actions
1572
1573 if( doCopy && !wxTheClipboard->Open() )
1574 return;
1575
1576 wxGridTableBase* tbl = m_grid->GetTable();
1577 wxString txt;
1578
1579 // fill txt with a format that is compatible with most spreadsheets
1580 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
1581 {
1582 if( row != m_sel_row_start )
1583 txt += ROW_SEP;
1584
1585 for( int col = m_sel_col_start; col < m_sel_col_start + m_sel_col_count; ++col )
1586 {
1587 if( !m_grid->IsColShown( col ) )
1588 continue;
1589
1590 const bool selected =
1591 !m_columnBulkEditEnabled || !m_grid->IsSelection() || m_grid->IsInSelection( row, col );
1592
1593 if( selected )
1594 txt += tbl->GetValue( row, col );
1595
1596 if( col < m_sel_col_start + m_sel_col_count - 1 ) // that was not last column
1597 txt += COL_SEP;
1598
1599 if( doDelete && selected )
1600 {
1601 // Do NOT allow clear of things that can take strings but aren't textEntries
1602 // (ie: color swatches, textboxes, etc.).
1603 if( isTextEntry( row, col ) && !isReadOnly( row, col ) )
1604 {
1605 bool hadContent = !tbl->GetValue( row, col ).IsEmpty();
1606
1607 tbl->SetValue( row, col, wxEmptyString );
1608
1609 if( hadContent )
1610 {
1611 wxGridEvent evt( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
1612 m_grid->GetEventHandler()->ProcessEvent( evt );
1613 }
1614 }
1615 }
1616 }
1617 }
1618
1619 if( doCopy )
1620 {
1621 wxTheClipboard->SetData( new wxTextDataObject( txt ) );
1622 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
1623 wxTheClipboard->Close();
1624 }
1625
1626 if( doDelete )
1627 m_grid->ForceRefresh();
1628}
1629
1630
1631void GRID_TRICKS::onUpdateUI( wxUpdateUIEvent& event )
1632{
1633 // Respect ROW selectionMode when moving cursor
1634
1635 if( m_grid->GetSelectionMode() == wxGrid::wxGridSelectRows )
1636 {
1637 int cursorRow = m_grid->GetGridCursorRow();
1638 bool cursorInSelectedRow = false;
1639
1640 for( int row : m_grid->GetSelectedRows() )
1641 {
1642 if( row == cursorRow )
1643 {
1644 cursorInSelectedRow = true;
1645 break;
1646 }
1647 }
1648
1649 if( !cursorInSelectedRow && cursorRow >= 0 )
1650 m_grid->SelectRow( cursorRow );
1651 }
1652
1653 event.Skip();
1654}
bool isReadOnly(int aRow, int aCol)
wxGridCellCoords m_bulkEditSource
void onGridMotion(wxMouseEvent &event)
void onGridLabelLeftClick(wxGridEvent &event)
virtual void paste_text(const wxString &cb_text)
void init()
Shared initialization for various ctors.
void onGridEditorShown(wxGridEvent &aEvent)
std::vector< std::pair< int, wxString > > m_bulkEditRows
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)
void onGridEditorHidden(wxGridEvent &aEvent)
bool m_bulkEditorClosing
bool isChoiceEditor(int aRow, int aCol)
bool m_bulkEditorOpening
void onKeyDown(wxKeyEvent &event)
BULK_EDIT_SESSION m_bulkEditor
void onPopupSelection(wxCommandEvent &event)
virtual void cutcopy(bool doCopy, bool doDelete)
void onEditorInput(wxCommandEvent &aEvent)
void onEditorKeyDown(wxKeyEvent &aEvent)
void onGridCellChanged(wxGridEvent &aEvent)
bool m_enableSingleClickEdit
void onGridCellChanging(wxGridEvent &aEvent)
virtual void doPopupSelection(wxCommandEvent &event)
bool isCheckbox(int aRow, int aCol)
std::function< void(wxCommandEvent &)> m_addHandler
bool m_bulkEditorEnterHandled
void onEditorDropdownOpened(wxCommandEvent &aEvent)
virtual void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent)
int m_sel_row_start
std::bitset< GRIDTRICKS_MAX_COL > m_tooltipEnabled
wxString m_bulkEditValue
wxComboCtrl * shownGenericPopup(const wxKeyEvent &aEnterKey)
void onEditorTextEnter(wxCommandEvent &aEvent)
void onUpdateUI(wxUpdateUIEvent &event)
void onGridCellRightClick(wxGridEvent &event)
virtual void appendPopupMenuItems(wxMenu &aMenu)
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 onGridEditorCreated(wxGridEditorCreatedEvent &aEvent)
void onGridCellLeftDClick(wxGridEvent &event)
bool m_columnBulkEditEnabled
virtual void onGridCellLeftClick(wxGridEvent &event)
void finishDropdown()
virtual bool handleDoubleClick(wxGridEvent &aEvent)
int m_sel_col_count
virtual void onCharHook(wxKeyEvent &event)
virtual void paste_clipboard()
bool showEditor(int aRow, int aCol)
void onEditorDropdownClosed(wxCommandEvent &aEvent)
bool finishEditor(bool aCancel, bool aMoveDown=false)
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
static void dismissDropdown(wxWindow *aControl)
#define COL_SEP
static bool isDropdown(wxWindow *aControl)
#define ROW_SEP
@ GRIDTRICKS_ID_PASTE
Definition grid_tricks.h:43
@ GRIDTRICKS_FIRST_ID
Definition grid_tricks.h:39
@ GRIDTRICKS_FIRST_SHOWHIDE
Definition grid_tricks.h:49
@ GRIDTRICKS_ID_SELECT
Definition grid_tricks.h:44
@ GRIDTRICKS_ID_CUT
Definition grid_tricks.h:40
@ GRIDTRICKS_LAST_ID
Definition grid_tricks.h:51
@ GRIDTRICKS_ID_COPY
Definition grid_tricks.h:41
@ GRIDTRICKS_ID_DELETE
Definition grid_tricks.h:42
bool PrepareChoicePopup(wxWindow *aControl)
Definition wxgtk/ui.cpp:558
wxKeyEvent GetNativePopupDismissalEvent()
Definition wxgtk/ui.cpp:573
std::vector< FAB_LAYER_COLOR > dummy
std::vector< DATABASE_CONNECTION::ROW > rows
KIBIS_MODEL * model
VECTOR2I end