1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 Wayne Stambaugh <stambaughw@gmail.com>
5  * Copyright (C) 2021 CERN
6  * Copyright (C) 2017-2021 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <set>
23 #include <wx/regex.h>
24 
25 #include <common.h>     // For ExpandEnvVarSubstitutions
26 #include <project.h>
27 #include <panel_sym_lib_table.h>
28 #include <lib_id.h>
29 #include <symbol_lib_table.h>
30 #include <lib_table_lexer.h>
31 #include <grid_tricks.h>
32 #include <widgets/wx_grid.h>
33 #include <confirm.h>
34 #include <bitmaps.h>
35 #include <lib_table_grid.h>
36 #include <wildcards_and_files_ext.h>
37 #include <env_paths.h>
38 #include <eeschema_id.h>
39 #include <symbol_edit_frame.h>
40 #include <symbol_viewer_frame.h>
41 #include <sch_edit_frame.h>
42 #include <kiway.h>
43 #include <paths.h>
44 #include <pgm_base.h>
45 #include <settings/settings_manager.h>
46 #include <widgets/grid_readonly_text_helpers.h>
47 #include <widgets/grid_text_button_helpers.h>
48 #include <sch_file_versions.h>
49 #include <wx/filedlg.h>
50 
51 
52 
53 // clang-format off
54 
55 /**
56  * Container that describes file type info for the add a library options
57  */
58 struct SUPPORTED_FILE_TYPE
59 {
60     wxString m_Description;            ///< Description shown in the file picker dialog
61     wxString m_FileFilter;             ///< Filter used for file pickers if m_IsFile is true
62     wxString m_FolderSearchExtension;  ///< In case of folders it stands for extensions of files stored inside
63     bool     m_IsFile;                 ///< Whether the library is a folder or a file
64     SCH_IO_MGR::SCH_FILE_T m_Plugin;
65 };
66 
67 
68 /**
69  * Event IDs for the menu items in the split button menu for add a library
70  */
71 enum {
72     ID_PANEL_SYM_LIB_KICAD = ID_END_EESCHEMA_ID_LIST,
73     ID_PANEL_SYM_LIB_LEGACY,
74 };
75 
76 
77 /**
78  * Build a wxGridTableBase by wrapping an #SYMBOL_LIB_TABLE object.
79  */
80 class SYMBOL_LIB_TABLE_GRID : public LIB_TABLE_GRID, public SYMBOL_LIB_TABLE
81 {
82     friend class SYMBOL_GRID_TRICKS;
83 
84 protected:
at(size_t aIndex)85     LIB_TABLE_ROW* at( size_t aIndex ) override { return &rows.at( aIndex ); }
86 
size() const87     size_t size() const override { return rows.size(); }
88 
makeNewRow()89     LIB_TABLE_ROW* makeNewRow() override
90     {
91         return dynamic_cast< LIB_TABLE_ROW* >( new SYMBOL_LIB_TABLE_ROW );
92     }
93 
begin()94     LIB_TABLE_ROWS_ITER begin() override { return rows.begin(); }
95 
insert(LIB_TABLE_ROWS_ITER aIterator,LIB_TABLE_ROW * aRow)96     LIB_TABLE_ROWS_ITER insert( LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW* aRow ) override
97     {
98         return rows.insert( aIterator, aRow );
99     }
100 
push_back(LIB_TABLE_ROW * aRow)101     void push_back( LIB_TABLE_ROW* aRow ) override { rows.push_back( aRow ); }
102 
erase(LIB_TABLE_ROWS_ITER aFirst,LIB_TABLE_ROWS_ITER aLast)103     LIB_TABLE_ROWS_ITER erase( LIB_TABLE_ROWS_ITER aFirst, LIB_TABLE_ROWS_ITER aLast ) override
104     {
105         return rows.erase( aFirst, aLast );
106     }
107 
108 public:
SetValue(int aRow,int aCol,const wxString & aValue)109     void SetValue( int aRow, int aCol, const wxString &aValue ) override
110     {
111         LIB_TABLE_GRID::SetValue( aRow, aCol, aValue );
112 
113         // If setting a filepath, attempt to auto-detect the format
114         if( aCol == COL_URI )
115         {
116             wxFileName fn( aValue );
117 
118             for( SCH_IO_MGR::SCH_FILE_T piType : SCH_IO_MGR::SCH_FILE_T_vector )
119             {
120                 if( SCH_IO_MGR::GetLibraryFileExtension( piType ).Lower() == fn.GetExt().Lower() )
121                 {
122                     SetValue( aRow, COL_TYPE, SCH_IO_MGR::ShowType( piType ) );
123                     break;
124                 }
125             }
126         }
127     }
128 
129 
SYMBOL_LIB_TABLE_GRID(const SYMBOL_LIB_TABLE & aTableToEdit)130     SYMBOL_LIB_TABLE_GRID( const SYMBOL_LIB_TABLE& aTableToEdit )
131     {
132         rows = aTableToEdit.rows;
133     }
134 };
135 
136 
137 class SYMBOL_GRID_TRICKS : public GRID_TRICKS
138 {
139 public:
SYMBOL_GRID_TRICKS(DIALOG_EDIT_LIBRARY_TABLES * aParent,WX_GRID * aGrid)140     SYMBOL_GRID_TRICKS( DIALOG_EDIT_LIBRARY_TABLES* aParent, WX_GRID* aGrid ) :
141         GRID_TRICKS( aGrid ),
142         m_dialog( aParent )
143     {
144     }
145 
146 protected:
147     DIALOG_EDIT_LIBRARY_TABLES* m_dialog;
148 
149     /// handle specialized clipboard text, with leading "(sym_lib_table" or
150     /// spreadsheet formatted text.
paste_text(const wxString & cb_text)151     virtual void paste_text( const wxString& cb_text ) override
152     {
153         SYMBOL_LIB_TABLE_GRID* tbl = (SYMBOL_LIB_TABLE_GRID*) m_grid->GetTable();
154         size_t                 ndx = cb_text.find( "(sym_lib_table" );
155 
156         if( ndx != std::string::npos )
157         {
158             // paste the SYMBOL_LIB_TABLE_ROWs of s-expression (sym_lib_table), starting
159             // at column 0 regardless of current cursor column.
160 
161             STRING_LINE_READER  slr( TO_UTF8( cb_text ), "Clipboard" );
162             LIB_TABLE_LEXER     lexer( &slr );
163             SYMBOL_LIB_TABLE    tmp_tbl;
164             bool                parsed = true;
165 
166             try
167             {
168                 tmp_tbl.Parse( &lexer );
169             }
170             catch( PARSE_ERROR& pe )
171             {
172                 DisplayError( m_dialog, pe.What() );
173                 parsed = false;
174             }
175 
176             if( parsed )
177             {
178                 // make sure the table is big enough...
179                 if( tmp_tbl.GetCount() > (unsigned) tbl->GetNumberRows() )
180                     tbl->AppendRows( tmp_tbl.GetCount() - tbl->GetNumberRows() );
181 
182                 for( unsigned i = 0;  i < tmp_tbl.GetCount();  ++i )
183                     tbl->rows.replace( i, tmp_tbl.At( i ).clone() );
184             }
185 
186             m_grid->AutoSizeColumns( false );
187         }
188         else
189         {
190             // paste spreadsheet formatted text.
191             GRID_TRICKS::paste_text( cb_text );
192 
193             m_grid->AutoSizeColumns( false );
194         }
195     }
196 };
197 
198 
PANEL_SYM_LIB_TABLE(DIALOG_EDIT_LIBRARY_TABLES * aParent,PROJECT * aProject,SYMBOL_LIB_TABLE * aGlobalTable,const wxString & aGlobalTablePath,SYMBOL_LIB_TABLE * aProjectTable,const wxString & aProjectTablePath)199 PANEL_SYM_LIB_TABLE::PANEL_SYM_LIB_TABLE( DIALOG_EDIT_LIBRARY_TABLES* aParent, PROJECT* aProject,
200                                           SYMBOL_LIB_TABLE* aGlobalTable,
201                                           const wxString& aGlobalTablePath,
202                                           SYMBOL_LIB_TABLE* aProjectTable,
203                                           const wxString& aProjectTablePath ) :
204     PANEL_SYM_LIB_TABLE_BASE( aParent ),
205     m_globalTable( aGlobalTable ),
206     m_projectTable( aProjectTable ),
207     m_project( aProject ),
208     m_parent( aParent )
209 {
210     // wxGrid only supports user owned tables if they exist past end of ~wxGrid(),
211     // so make it a grid owned table.
212     m_global_grid->SetTable( new SYMBOL_LIB_TABLE_GRID( *m_globalTable ), true );
213 
214     wxArrayString pluginChoices;
215 
216     pluginChoices.Add( SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_KICAD ) );
217     pluginChoices.Add( SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY ) );
218 
219     EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
220 
221     if( cfg->m_lastSymbolLibDir.IsEmpty() )
222         cfg->m_lastSymbolLibDir = PATHS::GetDefaultUserSymbolsPath();
223 
224     m_lastProjectLibDir = m_project->GetProjectPath();
225 
226     auto setupGrid =
227             [&]( WX_GRID* aGrid )
228             {
229                 // Give a bit more room for combobox editors
230                 aGrid->SetDefaultRowSize( aGrid->GetDefaultRowSize() + 4 );
231 
232                 // add Cut, Copy, and Paste to wxGrids
233                 aGrid->PushEventHandler( new SYMBOL_GRID_TRICKS( m_parent, aGrid ) );
234 
235                 aGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
236                 aGrid->AutoSizeColumns( false );
237 
238                 // Set special attributes
239                 wxGridCellAttr* attr;
240 
241                 attr = new wxGridCellAttr;
242 
243                 wxString wildcards = AllSymbolLibFilesWildcard()
244                                      + "|" + KiCadSymbolLibFileWildcard()
245                                      + "|" + LegacySymbolLibFileWildcard();
246                 attr->SetEditor( new GRID_CELL_PATH_EDITOR( m_parent, aGrid,
247                                                             &cfg->m_lastSymbolLibDir, wildcards,
248                                                             true, m_project->GetProjectPath() ) );
249                 aGrid->SetColAttr( COL_URI, attr );
250 
251                 attr = new wxGridCellAttr;
252                 attr->SetEditor( new wxGridCellChoiceEditor( pluginChoices ) );
253                 aGrid->SetColAttr( COL_TYPE, attr );
254 
255                 attr = new wxGridCellAttr;
256                 attr->SetRenderer( new wxGridCellBoolRenderer() );
257                 attr->SetReadOnly();    // not really; we delegate interactivity to GRID_TRICKS
258                 aGrid->SetColAttr( COL_ENABLED, attr );
259 
260                 // all but COL_OPTIONS, which is edited with Option Editor anyways.
261                 aGrid->AutoSizeColumn( COL_NICKNAME, false );
262                 aGrid->AutoSizeColumn( COL_TYPE, false );
263                 aGrid->AutoSizeColumn( COL_URI, false );
264                 aGrid->AutoSizeColumn( COL_DESCR, false );
265                 aGrid->AutoSizeColumn( COL_ENABLED, false );
266 
267                 // would set this to width of title, if it was easily known.
268                 aGrid->SetColSize( COL_OPTIONS, 80 );
269 
270                 // Gives a selection to each grid, mainly for delete button.  wxGrid's wake up with
271                 // a currentCell which is sometimes not highlighted.
272                 if( aGrid->GetNumberRows() > 0 )
273                     aGrid->SelectRow( 0 );
274             };
275 
276     setupGrid( m_global_grid );
277 
278     if( m_projectTable )
279     {
280         m_project_grid->SetTable( new SYMBOL_LIB_TABLE_GRID( *m_projectTable ), true );
281         setupGrid( m_project_grid );
282     }
283     else
284     {
285         m_pageNdx = 0;
286         m_notebook->DeletePage( 1 );
287         m_project_grid = nullptr;
288     }
289 
290     // add Cut, Copy, and Paste to wxGrids
291     m_path_subs_grid->PushEventHandler( new GRID_TRICKS( m_path_subs_grid ) );
292 
293     populateEnvironReadOnlyTable();
294 
295     // select the last selected page
296     m_notebook->SetSelection( m_pageNdx );
297     m_cur_grid = ( m_pageNdx == 0 ) ? m_global_grid : m_project_grid;
298 
299     m_path_subs_grid->SetColLabelValue( 0, _( "Name" ) );
300     m_path_subs_grid->SetColLabelValue( 1, _( "Value" ) );
301 
302     // for ALT+A handling, we want the initial focus to be on the first selected grid.
303     m_parent->SetInitialFocus( m_cur_grid );
304 
305     // Configure button logos
306     m_append_button->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
307     m_delete_button->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
308     m_move_up_button->SetBitmap( KiBitmap( BITMAPS::small_up ) );
309     m_move_down_button->SetBitmap( KiBitmap( BITMAPS::small_down ) );
310     m_browse_button->SetBitmap( KiBitmap( BITMAPS::small_folder ) );
311 }
312 
313 
~PANEL_SYM_LIB_TABLE()314 PANEL_SYM_LIB_TABLE::~PANEL_SYM_LIB_TABLE()
315 {
316     // When the dialog is closed it will hide the current notebook page first, which will
317     // in turn select the other one.  We then end up saving its index as the "current page".
318     // So flip them back again:
319     m_pageNdx = m_pageNdx == 1 ? 0 : 1;
320 
321     // Delete the GRID_TRICKS.
322     // Any additional event handlers should be popped before the window is deleted.
323     m_global_grid->PopEventHandler( true );
324 
325     if( m_project_grid )
326         m_project_grid->PopEventHandler( true );
327 
328     m_path_subs_grid->PopEventHandler( true );
329 }
330 
331 
verifyTables()332 bool PANEL_SYM_LIB_TABLE::verifyTables()
333 {
334     wxString msg;
335 
336     for( SYMBOL_LIB_TABLE_GRID* model : { global_model(), project_model() } )
337     {
338         if( !model )
339             continue;
340 
341         for( int r = 0; r < model->GetNumberRows(); )
342         {
343             wxString nick = model->GetValue( r, COL_NICKNAME ).Trim( false ).Trim();
344             wxString uri  = model->GetValue( r, COL_URI ).Trim( false ).Trim();
345             unsigned illegalCh = 0;
346 
347             if( !nick || !uri )
348             {
349                 if( !nick && !uri )
350                     msg = _( "A library table row nickname and path cells are empty." );
351                 else if( !nick )
352                     msg = _( "A library table row nickname cell is empty." );
353                 else
354                     msg = _( "A library table row path cell is empty." );
355 
356                 wxMessageDialog badCellDlg( this, msg, _( "Invalid Row Definition" ),
357                                             wxYES_NO | wxCENTER | wxICON_QUESTION | wxYES_DEFAULT );
358                 badCellDlg.SetExtendedMessage( _( "Empty cells will result in all rows that are "
359                                                   "invalid to be removed from the table." ) );
360                 badCellDlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( "Remove Invalid Cells" ),
361                                            wxMessageDialog::ButtonLabel( "Cancel Table Update" ) );
362 
363                 if( badCellDlg.ShowModal() == wxID_NO )
364                     return false;
365 
366                 // Delete the "empty" row, where empty means missing nick or uri.
367                 // This also updates the UI which could be slow, but there should only be a few
368                 // rows to delete, unless the user fell asleep on the Add Row
369                 // button.
370                 model->DeleteRows( r, 1 );
371             }
372             else if( ( illegalCh = LIB_ID::FindIllegalLibraryNameChar( nick ) ) )
373             {
374                 msg = wxString::Format( _( "Illegal character '%c' in nickname '%s'" ),
375                                         illegalCh,
376                                         nick );
377 
378                 // show the tabbed panel holding the grid we have flunked:
379                 if( model != cur_model() )
380                     m_notebook->SetSelection( model == global_model() ? 0 : 1 );
381 
382                 m_cur_grid->MakeCellVisible( r, 0 );
383                 m_cur_grid->SetGridCursor( r, 1 );
384 
385                 wxMessageDialog errdlg( this, msg, _( "Library Nickname Error" ) );
386                 errdlg.ShowModal();
387                 return false;
388             }
389             else
390             {
391                 // set the trimmed values back into the table so they get saved to disk.
392                 model->SetValue( r, COL_NICKNAME, nick );
393                 model->SetValue( r, COL_URI, uri );
394                 ++r;        // this row was OK.
395             }
396         }
397     }
398 
399     // check for duplicate nickNames, separately in each table.
400     for( SYMBOL_LIB_TABLE_GRID* model : { global_model(), project_model() } )
401     {
402         if( !model )
403             continue;
404 
405         for( int r1 = 0; r1 < model->GetNumberRows() - 1;  ++r1 )
406         {
407             wxString    nick1 = model->GetValue( r1, COL_NICKNAME );
408 
409             for( int r2=r1+1; r2 < model->GetNumberRows();  ++r2 )
410             {
411                 wxString    nick2 = model->GetValue( r2, COL_NICKNAME );
412 
413                 if( nick1 == nick2 )
414                 {
415                     msg = wxString::Format( _( "Multiple libraries cannot share the same "
416                                                "nickname ('%s')." ), nick1 );
417 
418                     // show the tabbed panel holding the grid we have flunked:
419                     if( model != cur_model() )
420                         m_notebook->SetSelection( model == global_model() ? 0 : 1 );
421 
422                     // go to the lower of the two rows, it is technically the duplicate:
423                     m_cur_grid->MakeCellVisible( r2, 0 );
424                     m_cur_grid->SetGridCursor( r2, 1 );
425 
426                     wxMessageDialog errdlg( this, msg, _( "Library Nickname Error" ) );
427                     errdlg.ShowModal();
428 
429                     return false;
430                 }
431             }
432         }
433     }
434 
435     for( SYMBOL_LIB_TABLE* table : { global_model(), project_model() } )
436     {
437         if( !table )
438             continue;
439 
440         for( unsigned int r = 0; r < table->GetCount(); ++r )
441         {
442             SYMBOL_LIB_TABLE_ROW& row = dynamic_cast<SYMBOL_LIB_TABLE_ROW&>( table->At( r ) );
443 
444             if( !row.GetIsEnabled() )
445                 continue;
446 
447             try
448             {
449                 if( row.Refresh() )
450                 {
451                     if( table == global_model() )
452                         m_parent->m_GlobalTableChanged = true;
453                     else
454                         m_parent->m_ProjectTableChanged = true;
455                 }
456             }
457             catch( const IO_ERROR& ioe )
458             {
459                 msg.Printf( _( "Symbol library '%s' failed to load." ), row.GetNickName() );
460 
461                 wxMessageDialog errdlg( this, msg + wxS( "\n" ) + ioe.What(),
462                                         _( "Error Loading Library" ) );
463                 errdlg.ShowModal();
464 
465                 return false;
466             }
467         }
468     }
469 
470     return true;
471 }
472 
473 
OnUpdateUI(wxUpdateUIEvent & event)474 void PANEL_SYM_LIB_TABLE::OnUpdateUI( wxUpdateUIEvent& event )
475 {
476     m_pageNdx = (unsigned) std::max( 0, m_notebook->GetSelection() );
477     m_cur_grid = m_pageNdx == 0 ? m_global_grid : m_project_grid;
478 }
479 
480 
browseLibrariesHandler(wxCommandEvent & event)481 void PANEL_SYM_LIB_TABLE::browseLibrariesHandler( wxCommandEvent& event )
482 {
483     wxString wildcards = AllSymbolLibFilesWildcard()
484                             + "|" + KiCadSymbolLibFileWildcard()
485                             + "|" + LegacySymbolLibFileWildcard();
486 
487     EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
488 
489     wxString openDir = cfg->m_lastSymbolLibDir;
490 
491     if( m_cur_grid == m_project_grid )
492         openDir = m_lastProjectLibDir;
493 
494     wxFileDialog dlg( this, _( "Select Library" ), openDir, wxEmptyString, wildcards,
495                       wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE );
496 
497     if( dlg.ShowModal() == wxID_CANCEL )
498         return;
499 
500     if( m_cur_grid == m_global_grid )
501         cfg->m_lastSymbolLibDir = dlg.GetDirectory();
502     else
503         m_lastProjectLibDir = dlg.GetDirectory();
504 
505     const ENV_VAR_MAP& envVars       = Pgm().GetLocalEnvVariables();
506     bool               addDuplicates = false;
507     bool               applyToAll    = false;
508     wxString           warning       = _( "Warning: Duplicate Nickname" );
509     wxString           msg           = _( "A library nicknamed '%s' already exists." );
510     wxString           detailedMsg   = _( "One of the nicknames will need to be changed after "
511                                           "adding this library." );
512 
513     wxArrayString files;
514     dlg.GetFilenames( files );
515 
516     for( const wxString& file : files )
517     {
518         wxString   filePath = dlg.GetDirectory() + wxFileName::GetPathSeparator() + file;
519         wxFileName fn( filePath );
520         wxString   nickname = LIB_ID::FixIllegalChars( fn.GetName(), true );
521         bool       doAdd = true;
522 
523         if( cur_model()->ContainsNickname( nickname ) )
524         {
525             if( !applyToAll )
526             {
527                 // The cancel button adds the library to the table anyway
528                 addDuplicates = OKOrCancelDialog( this, warning, wxString::Format( msg, nickname ),
529                                                   detailedMsg, _( "Skip" ), _( "Add Anyway" ),
530                                                   &applyToAll ) == wxID_CANCEL;
531             }
532 
533             doAdd = addDuplicates;
534         }
535 
536         if( doAdd && m_cur_grid->AppendRows( 1 ) )
537         {
538             int last_row = m_cur_grid->GetNumberRows() - 1;
539 
540             m_cur_grid->SetCellValue( last_row, COL_NICKNAME, nickname );
541 
542             // attempt to auto-detect the plugin type
543             for( SCH_IO_MGR::SCH_FILE_T piType : SCH_IO_MGR::SCH_FILE_T_vector )
544             {
545                 if( SCH_IO_MGR::GetLibraryFileExtension( piType ).Lower() == fn.GetExt().Lower() )
546                 {
547                     m_cur_grid->SetCellValue( last_row, COL_TYPE, SCH_IO_MGR::ShowType( piType ) );
548                     break;
549                 }
550             }
551 
552             // try to use path normalized to an environmental variable or project path
553             wxString path = NormalizePath( filePath, &envVars, m_project->GetProjectPath() );
554 
555             // Do not use the project path in the global library table.  This will almost
556             // assuredly be wrong for a different project.
557             if( m_pageNdx == 0 && path.Contains( "${KIPRJMOD}" ) )
558                 path = fn.GetFullPath();
559 
560             m_cur_grid->SetCellValue( last_row, COL_URI, path );
561         }
562     }
563 
564     if( !files.IsEmpty() )
565     {
566         m_cur_grid->MakeCellVisible( m_cur_grid->GetNumberRows() - 1, 0 );
567         m_cur_grid->SetGridCursor( m_cur_grid->GetNumberRows() - 1, 1 );
568     }
569 }
570 
571 
appendRowHandler(wxCommandEvent & event)572 void PANEL_SYM_LIB_TABLE::appendRowHandler( wxCommandEvent& event )
573 {
574     if( !m_cur_grid->CommitPendingChanges() )
575         return;
576 
577     if( m_cur_grid->AppendRows( 1 ) )
578     {
579         int row = m_cur_grid->GetNumberRows() - 1;
580 
581         // wx documentation is wrong, SetGridCursor does not make visible.
582         m_cur_grid->MakeCellVisible( row, 0 );
583         m_cur_grid->SetGridCursor( row, 1 );
584         m_cur_grid->EnableCellEditControl( true );
585         m_cur_grid->ShowCellEditControl();
586     }
587 }
588 
589 
deleteRowHandler(wxCommandEvent & event)590 void PANEL_SYM_LIB_TABLE::deleteRowHandler( wxCommandEvent& event )
591 {
592     if( !m_cur_grid->CommitPendingChanges() )
593         return;
594 
595     int curRow = m_cur_grid->GetGridCursorRow();
596     int curCol = m_cur_grid->GetGridCursorCol();
597 
598     // In a wxGrid, collect rows that have a selected cell, or are selected
599     // It is not so easy: it depends on the way the selection was made.
600     // Here, we collect rows selected by clicking on a row label, and rows that contain
601     // previously-selected cells.
602     // If no candidate, just delete the row with the grid cursor.
603     wxArrayInt selectedRows	= m_cur_grid->GetSelectedRows();
604     wxGridCellCoordsArray cells = m_cur_grid->GetSelectedCells();
605     wxGridCellCoordsArray blockTopLeft = m_cur_grid->GetSelectionBlockTopLeft();
606     wxGridCellCoordsArray blockBotRight = m_cur_grid->GetSelectionBlockBottomRight();
607 
608     // Add all row having cell selected to list:
609     for( unsigned ii = 0; ii < cells.GetCount(); ii++ )
610         selectedRows.Add( cells[ii].GetRow() );
611 
612     // Handle block selection
613     if( !blockTopLeft.IsEmpty() && !blockBotRight.IsEmpty() )
614     {
615         for( int i = blockTopLeft[0].GetRow(); i <= blockBotRight[0].GetRow(); ++i )
616             selectedRows.Add( i );
617     }
618 
619     // Use the row having the grid cursor only if we have no candidate:
620     if( selectedRows.size() == 0 && m_cur_grid->GetGridCursorRow() >= 0 )
621         selectedRows.Add( m_cur_grid->GetGridCursorRow() );
622 
623     if( selectedRows.size() == 0 )
624     {
625         wxBell();
626         return;
627     }
628 
629     std::sort( selectedRows.begin(), selectedRows.end() );
630 
631     // Remove selected rows (note: a row can be stored more than once in list)
632     int last_row = -1;
633 
634     for( int ii = selectedRows.GetCount()-1; ii >= 0; ii-- )
635     {
636         int row = selectedRows[ii];
637 
638         if( row != last_row )
639         {
640             last_row = row;
641             m_cur_grid->DeleteRows( row, 1 );
642         }
643     }
644 
645     if( m_cur_grid->GetNumberRows() > 0 && curRow >= 0 )
646         m_cur_grid->SetGridCursor( std::min( curRow, m_cur_grid->GetNumberRows() - 1 ), curCol );
647 }
648 
649 
moveUpHandler(wxCommandEvent & event)650 void PANEL_SYM_LIB_TABLE::moveUpHandler( wxCommandEvent& event )
651 {
652     if( !m_cur_grid->CommitPendingChanges() )
653         return;
654 
655     SYMBOL_LIB_TABLE_GRID* tbl = cur_model();
656     int curRow = m_cur_grid->GetGridCursorRow();
657 
658     // @todo: add multiple selection moves.
659     if( curRow >= 1 )
660     {
661         boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
662             tbl->rows.release( tbl->rows.begin() + curRow );
663 
664         --curRow;
665         tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
666 
667         if( tbl->GetView() )
668         {
669             // Update the wxGrid
670             wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow, 0 );
671             tbl->GetView()->ProcessTableMessage( msg );
672         }
673 
674         m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
675         m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
676     }
677 }
678 
679 
moveDownHandler(wxCommandEvent & event)680 void PANEL_SYM_LIB_TABLE::moveDownHandler( wxCommandEvent& event )
681 {
682     if( !m_cur_grid->CommitPendingChanges() )
683         return;
684 
685     SYMBOL_LIB_TABLE_GRID* tbl = cur_model();
686     int curRow = m_cur_grid->GetGridCursorRow();
687 
688     // @todo: add multiple selection moves.
689     if( unsigned( curRow + 1 ) < tbl->rows.size() )
690     {
691         boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
692             tbl->rows.release( tbl->rows.begin() + curRow );
693 
694         ++curRow;
695         tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
696 
697         if( tbl->GetView() )
698         {
699             // Update the wxGrid
700             wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow - 1, 0 );
701             tbl->GetView()->ProcessTableMessage( msg );
702         }
703 
704         m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
705         m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
706     }
707 }
708 
709 
onConvertLegacyLibraries(wxCommandEvent & event)710 void PANEL_SYM_LIB_TABLE::onConvertLegacyLibraries( wxCommandEvent& event )
711 {
712     if( !m_cur_grid->CommitPendingChanges() )
713         return;
714 
715     wxArrayInt selectedRows = m_cur_grid->GetSelectedRows();
716 
717     if( selectedRows.empty() && m_cur_grid->GetGridCursorRow() >= 0 )
718         selectedRows.push_back( m_cur_grid->GetGridCursorRow() );
719 
720     wxArrayInt legacyRows;
721     wxString   legacyType = SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY );
722     wxString   kicadType = SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_KICAD );
723     wxString   msg;
724 
725     for( int row : selectedRows )
726     {
727         if( m_cur_grid->GetCellValue( row, COL_TYPE ) == legacyType )
728             legacyRows.push_back( row );
729     }
730 
731     if( legacyRows.size() <= 0 )
732     {
733         wxMessageBox( _( "Select one or more rows containing libraries in Legacy format (*.lib) "
734                          "to save as current KiCad format (*.kicad_sym)." ) );
735         return;
736     }
737     else
738     {
739         if( legacyRows.size() == 1 )
740         {
741             msg.Printf( _( "Save '%s' as current KiCad format (*.kicad_sym) "
742                            "and replace legacy entry in table?" ),
743                         m_cur_grid->GetCellValue( legacyRows[0], COL_NICKNAME ) );
744         }
745         else
746         {
747             msg.Printf( _( "Save %d Legacy format libraries as current KiCad format (*.kicad_sym) "
748                            "and replace legacy entries in table?" ),
749                         (int) legacyRows.size() );
750         }
751 
752         if( !IsOK( m_parent, msg ) )
753             return;
754     }
755 
756     for( int row : legacyRows )
757     {
758         wxString   libName = m_cur_grid->GetCellValue( row, COL_NICKNAME );
759         wxString   relPath = m_cur_grid->GetCellValue( row, COL_URI );
760         wxString   resolvedPath = ExpandEnvVarSubstitutions( relPath, m_project );
761         wxFileName legacyLib( resolvedPath );
762 
763         if( !legacyLib.Exists() )
764         {
765             msg.Printf( _( "Library '%s' not found." ), relPath );
766             DisplayErrorMessage( this, msg );
767             continue;
768         }
769 
770         wxFileName newLib( resolvedPath );
771         newLib.SetExt( "kicad_sym" );
772 
773         if( newLib.Exists() )
774         {
775             msg.Printf( _( "File '%s' already exists. Do you want overwrite this file?" ),
776                         newLib.GetFullPath() );
777 
778             switch( wxMessageBox( msg, _( "Migrate Library" ),
779                                   wxYES_NO | wxCANCEL | wxICON_QUESTION, m_parent ) )
780             {
781             case wxYES:    break;
782             case wxNO:     continue;
783             case wxCANCEL: return;
784             }
785         }
786 
787         if( convertLibrary( libName, legacyLib.GetFullPath(), newLib.GetFullPath() ) )
788         {
789             relPath = NormalizePath( newLib.GetFullPath(), &Pgm().GetLocalEnvVariables(),
790                                      m_project );
791 
792             // Do not use the project path in the global library table.  This will almost
793             // assuredly be wrong for a different project.
794             if( m_cur_grid == m_global_grid && relPath.Contains( "${KIPRJMOD}" ) )
795                 relPath = newLib.GetFullPath();
796 
797             m_cur_grid->SetCellValue( row, COL_URI, relPath );
798             m_cur_grid->SetCellValue( row, COL_TYPE, kicadType );
799         }
800         else
801         {
802             msg.Printf( _( "Failed to save symbol library file '%s'." ), newLib.GetFullPath() );
803             DisplayErrorMessage( this, msg );
804         }
805     }
806 }
807 
808 
convertLibrary(const wxString & aLibrary,const wxString & legacyFilepath,const wxString & newFilepath)809 bool PANEL_SYM_LIB_TABLE::convertLibrary( const wxString& aLibrary, const wxString& legacyFilepath,
810                                           const wxString& newFilepath )
811 {
812     SCH_PLUGIN::SCH_PLUGIN_RELEASER    legacyPI( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
813     SCH_PLUGIN::SCH_PLUGIN_RELEASER    kicadPI( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
814     std::vector<LIB_SYMBOL*>           symbols;
815     std::vector<LIB_SYMBOL*>           newSymbols;
816     std::map<LIB_SYMBOL*, LIB_SYMBOL*> symbolMap;
817 
818     try
819     {
820         // Write a stub file; SaveSymbol() expects something to be there already.
821         FILE_OUTPUTFORMATTER* formatter = new FILE_OUTPUTFORMATTER( newFilepath );
822 
823         formatter->Print( 0, "(kicad_symbol_lib (version %d) (generator kicad_converter))",
824                           SEXPR_SYMBOL_LIB_FILE_VERSION );
825 
826         // This will write the file
827         delete formatter;
828 
829         legacyPI->EnumerateSymbolLib( symbols, legacyFilepath );
830 
831         // Copy non-aliases first so we can build a map from symbols to newSymbols
832         for( LIB_SYMBOL* symbol : symbols )
833         {
834             if( symbol->IsAlias() )
835                 continue;
836 
837             symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
838 
839             newSymbols.push_back( new LIB_SYMBOL( *symbol ) );
840             symbolMap[symbol] = newSymbols.back();
841         }
842 
843         // Now do the aliases using the map to hook them up to their newSymbol parents
844         for( LIB_SYMBOL* symbol : symbols )
845         {
846             if( !symbol->IsAlias() )
847                 continue;
848 
849             symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
850 
851             newSymbols.push_back( new LIB_SYMBOL( *symbol ) );
852             newSymbols.back()->SetParent( symbolMap[ symbol->GetParent().lock().get() ] );
853         }
854 
855         // Finally write out newSymbols
856         for( LIB_SYMBOL* symbol : newSymbols )
857         {
858             kicadPI->SaveSymbol( newFilepath, symbol );
859         }
860     }
861     catch( ... )
862     {
863         return false;
864     }
865 
866     return true;
867 }
868 
869 
TransferDataFromWindow()870 bool PANEL_SYM_LIB_TABLE::TransferDataFromWindow()
871 {
872     if( !m_cur_grid->CommitPendingChanges() )
873         return false;
874 
875     if( !verifyTables() )
876         return false;
877 
878     if( *global_model() != *m_globalTable )
879     {
880         m_parent->m_GlobalTableChanged = true;
881 
882         m_globalTable->Clear();
883         m_globalTable->rows.transfer( m_globalTable->rows.end(), global_model()->rows.begin(),
884                                       global_model()->rows.end(), global_model()->rows );
885         m_globalTable->reindex();
886     }
887 
888     if( project_model() && *project_model() != *m_projectTable )
889     {
890         m_parent->m_ProjectTableChanged = true;
891 
892         m_projectTable->Clear();
893         m_projectTable->rows.transfer( m_projectTable->rows.end(), project_model()->rows.begin(),
894                                        project_model()->rows.end(), project_model()->rows );
895         m_projectTable->reindex();
896     }
897 
898     return true;
899 }
900 
901 
populateEnvironReadOnlyTable()902 void PANEL_SYM_LIB_TABLE::populateEnvironReadOnlyTable()
903 {
904     wxRegEx re( ".*?(\\$\\{(.+?)\\})|(\\$\\((.+?)\\)).*?", wxRE_ADVANCED );
905     wxASSERT( re.IsValid() );   // wxRE_ADVANCED is required.
906 
907     std::set< wxString > unique;
908 
909     // clear the table
910     m_path_subs_grid->ClearRows();
911 
912     for( SYMBOL_LIB_TABLE_GRID* tbl : { global_model(), project_model() } )
913     {
914         if( !tbl )
915             continue;
916 
917         for( int row = 0; row < tbl->GetNumberRows(); ++row )
918         {
919             wxString uri = tbl->GetValue( row, COL_URI );
920 
921             while( re.Matches( uri ) )
922             {
923                 wxString envvar = re.GetMatch( uri, 2 );
924 
925                 // if not ${...} form then must be $(...)
926                 if( envvar.IsEmpty() )
927                     envvar = re.GetMatch( uri, 4 );
928 
929                 // ignore duplicates
930                 unique.insert( envvar );
931 
932                 // delete the last match and search again
933                 uri.Replace( re.GetMatch( uri, 0 ), wxEmptyString );
934             }
935         }
936     }
937 
938     // Make sure this special environment variable shows up even if it was
939     // not used yet.  It is automatically set by KiCad to the directory holding
940     // the current project.
941     unique.insert( PROJECT_VAR_NAME );
942     unique.insert( SYMBOL_LIB_TABLE::GlobalPathEnvVariableName() );
943 
944     for( const wxString& evName : unique )
945     {
946         int row = m_path_subs_grid->GetNumberRows();
947         m_path_subs_grid->AppendRows( 1 );
948 
949         m_path_subs_grid->SetCellValue( row, 0, wxT( "${" ) + evName + wxT( "}" ) );
950         m_path_subs_grid->SetCellEditor( row, 0, new GRID_CELL_READONLY_TEXT_EDITOR() );
951 
952         wxString evValue;
953         wxGetEnv( evName, &evValue );
954         m_path_subs_grid->SetCellValue( row, 1, evValue );
955         m_path_subs_grid->SetCellEditor( row, 1, new GRID_CELL_READONLY_TEXT_EDITOR() );
956     }
957 
958     // No combobox editors here, but it looks better if its consistent with the other
959     // grids in the dialog.
960     m_path_subs_grid->SetDefaultRowSize( m_path_subs_grid->GetDefaultRowSize() + 2 );
961 
962     adjustPathSubsGridColumns( m_path_subs_grid->GetRect().GetWidth() );
963 }
964 
965 
adjustPathSubsGridColumns(int aWidth)966 void PANEL_SYM_LIB_TABLE::adjustPathSubsGridColumns( int aWidth )
967 {
968     // Account for scroll bars
969     aWidth -= ( m_path_subs_grid->GetSize().x - m_path_subs_grid->GetClientSize().x );
970 
971     m_path_subs_grid->AutoSizeColumn( 0 );
972 
973     int remaining_width = aWidth - m_path_subs_grid->GetColSize( 0 );
974 
975     if( remaining_width < 0 )
976         m_path_subs_grid->SetColSize( 1, -1 );
977     else
978         m_path_subs_grid->SetColSize( 1, remaining_width );
979 }
980 
981 
onSizeGrid(wxSizeEvent & event)982 void PANEL_SYM_LIB_TABLE::onSizeGrid( wxSizeEvent& event )
983 {
984     adjustPathSubsGridColumns( event.GetSize().GetX() );
985 
986     event.Skip();
987 }
988 
989 
global_model() const990 SYMBOL_LIB_TABLE_GRID* PANEL_SYM_LIB_TABLE::global_model() const
991 {
992     return (SYMBOL_LIB_TABLE_GRID*) m_global_grid->GetTable();
993 }
994 
995 
project_model() const996 SYMBOL_LIB_TABLE_GRID* PANEL_SYM_LIB_TABLE::project_model() const
997 {
998     return m_project_grid ? (SYMBOL_LIB_TABLE_GRID*) m_project_grid->GetTable() : nullptr;
999 }
1000 
1001 
cur_model() const1002 SYMBOL_LIB_TABLE_GRID* PANEL_SYM_LIB_TABLE::cur_model() const
1003 {
1004     return (SYMBOL_LIB_TABLE_GRID*) m_cur_grid->GetTable();
1005 }
1006 
1007 
1008 size_t PANEL_SYM_LIB_TABLE::m_pageNdx = 0;
1009 
1010 
InvokeSchEditSymbolLibTable(KIWAY * aKiway,wxWindow * aParent)1011 void InvokeSchEditSymbolLibTable( KIWAY* aKiway, wxWindow *aParent )
1012 {
1013     auto* schEditor = (SCH_EDIT_FRAME*) aKiway->Player( FRAME_SCH, false );
1014     auto* symbolEditor = (SYMBOL_EDIT_FRAME*) aKiway->Player( FRAME_SCH_SYMBOL_EDITOR, false );
1015     auto* symbolViewer = (SYMBOL_VIEWER_FRAME*) aKiway->Player( FRAME_SCH_VIEWER, false );
1016 
1017     SYMBOL_LIB_TABLE* globalTable = &SYMBOL_LIB_TABLE::GetGlobalLibTable();
1018     wxString          globalTablePath = SYMBOL_LIB_TABLE::GetGlobalTableFileName();
1019     SYMBOL_LIB_TABLE* projectTable = nullptr;
1020     wxString          projectPath = aKiway->Prj().GetProjectPath();
1021     wxFileName        projectTableFn( projectPath, SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
1022     wxString          msg;
1023     wxString          currentLib;
1024 
1025     // Don't allow editing project tables if no project is open
1026     if( !aKiway->Prj().IsNullProject() )
1027         projectTable = aKiway->Prj().SchSymbolLibTable();
1028 
1029     if( symbolEditor )
1030     {
1031         currentLib = symbolEditor->GetCurLib();
1032 
1033         // This prevents an ugly crash on OSX (https://bugs.launchpad.net/kicad/+bug/1765286)
1034         symbolEditor->FreezeLibraryTree();
1035 
1036         if( symbolEditor->HasLibModifications() )
1037         {
1038             msg = _( "Modifications have been made to one or more symbol libraries.\n"
1039                      "Changes must be saved or discarded before the symbol library "
1040                      "table can be modified." );
1041 
1042             switch( UnsavedChangesDialog( aParent, msg ) )
1043             {
1044             case wxID_YES:    symbolEditor->SaveAll();         break;
1045             case wxID_NO:     symbolEditor->RevertAll();       break;
1046             default:
1047             case wxID_CANCEL: symbolEditor->ThawLibraryTree(); return;
1048             }
1049         }
1050     }
1051 
1052     DIALOG_EDIT_LIBRARY_TABLES dlg( aParent, _( "Symbol Libraries" ) );
1053     dlg.SetKiway( &dlg, aKiway );
1054 
1055     dlg.InstallPanel( new PANEL_SYM_LIB_TABLE( &dlg, &aKiway->Prj(), globalTable, globalTablePath,
1056                                                projectTable, projectTableFn.GetFullPath() ) );
1057 
1058     if( dlg.ShowModal() == wxID_CANCEL )
1059     {
1060         if( symbolEditor )
1061             symbolEditor->ThawLibraryTree();
1062 
1063         return;
1064     }
1065 
1066     if( dlg.m_GlobalTableChanged )
1067     {
1068         try
1069         {
1070             globalTable->Save( globalTablePath );
1071         }
1072         catch( const IO_ERROR& ioe )
1073         {
1074             msg.Printf( _( "Error saving global library table:\n\n%s" ), ioe.What() );
1075             wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
1076         }
1077     }
1078 
1079     if( projectTable && dlg.m_ProjectTableChanged )
1080     {
1081         try
1082         {
1083             projectTable->Save( projectTableFn.GetFullPath() );
1084         }
1085         catch( const IO_ERROR& ioe )
1086         {
1087             msg.Printf( _( "Error saving project-specific library table:\n\n%s" ), ioe.What() );
1088             wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
1089         }
1090     }
1091 
1092     if( schEditor )
1093         schEditor->SyncView();
1094 
1095     if( symbolEditor )
1096     {
1097         // Check if the currently selected symbol library been removed or disabled.
1098         if( !currentLib.empty() && projectTable && !projectTable->HasLibrary( currentLib, true ) )
1099         {
1100             symbolEditor->SetCurLib( wxEmptyString );
1101             symbolEditor->emptyScreen();
1102         }
1103 
1104         symbolEditor->SyncLibraries( true );
1105         symbolEditor->ThawLibraryTree();
1106         symbolEditor->RefreshLibraryTree();
1107     }
1108 
1109     if( symbolViewer )
1110         symbolViewer->ReCreateLibList();
1111 }
1112