summaryrefslogtreecommitdiff
path: root/eeschema/dialogs/dialog_eeschema_config.cpp
blob: 07dab497a0b107cd81918b565ab53c8f3715d110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2006-2013 KiCad Developers, see change_log.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file dialog_eeschema_config.cpp
 */

#include <fctsys.h>
#include <pgm_base.h>
#include <kiway.h>
#include <confirm.h>
#include <gestfich.h>
#include <schframe.h>
#include <invoke_sch_dialog.h>
#include <kiface_i.h>

#include <general.h>
#include <netlist.h>
#include <libeditframe.h>
#include <viewlib_frame.h>
#include <wildcards_and_files_ext.h>
#include <wx/tokenzr.h>
#include <dialog_eeschema_config_fbp.h>
#include <eeschema_config.h>


class SCH_EDIT_FRAME;
class EDA_DRAW_FRAME;


class DIALOG_EESCHEMA_CONFIG : public DIALOG_EESCHEMA_CONFIG_FBP
{
public:
    DIALOG_EESCHEMA_CONFIG( wxWindow* aParent,
            wxString* aCallersProjectSpecificLibPaths, wxArrayString* aCallersLibNames );

private:
    wxConfigBase*   m_config;
    wxString*       m_callers_project_specific_lib_paths;
    wxArrayString*  m_callers_lib_names;

    bool            m_lib_list_changed;
    bool            m_lib_path_changed;

    //------ event handlers, overiding the fbp handlers --------------

    void OnCloseWindow( wxCloseEvent& event );

    /* Remove a library to the library list.
     * The real list (m_Parent->m_ComponentLibFiles) is not changed, so the change can be canceled
     */
    void OnRemoveLibClick( wxCommandEvent& event );

    /* Insert or add a library to the library list:
     *   The new library is put in list before (insert button) the selection,
     *   or added (add button) to end of list
     * The real list (m_Parent->m_ComponentLibFiles) is not changed, so the change
     * can be canceled
     */
    void OnAddOrInsertLibClick( wxCommandEvent& event );

    void OnAddOrInsertPath( wxCommandEvent& event );
    void OnOkClick( wxCommandEvent& event );
    void OnCancelClick( wxCommandEvent& event );
    void OnRemoveUserPath( wxCommandEvent& event );
    void OnButtonUpClick( wxCommandEvent& event );
    void OnButtonDownClick( wxCommandEvent& event );
};


DIALOG_EESCHEMA_CONFIG::DIALOG_EESCHEMA_CONFIG( wxWindow* aParent,
            wxString* aCallersProjectSpecificLibPaths, wxArrayString* aCallersLibNames ) :
    DIALOG_EESCHEMA_CONFIG_FBP( aParent ),
    m_callers_project_specific_lib_paths( aCallersProjectSpecificLibPaths ),
    m_callers_lib_names( aCallersLibNames ),
    m_lib_list_changed( false ),
    m_lib_path_changed( false )
{
    m_ListLibr->InsertItems( *aCallersLibNames, 0 );

    // Load user libs paths:
    wxArrayString paths;

    SEARCH_STACK::Split( &paths, *aCallersProjectSpecificLibPaths );

    for( unsigned i=0; i<paths.GetCount();  ++i )
    {
        wxString path = paths[i];

        if( wxFileName::DirExists( Prj().AbsolutePath( path ) ) )
            m_listUserPaths->Append( path );
    }

    // Display actual library paths which come in part from KIFACE::KifaceSearch()
    // along with aCallersProjectSpecificLibPaths at the front.
    SEARCH_STACK* libpaths = Prj().SchSearchS();

    DBG( libpaths->Show( __func__ ); )

    for( unsigned ii = 0; ii < libpaths->GetCount(); ii++ )
    {
        m_DefaultLibraryPathslistBox->Append( (*libpaths)[ii] );
    }

    // select the first path after the current project's path
    if( libpaths->GetCount() > 1 )
        m_DefaultLibraryPathslistBox->Select( 1 );

    // Load setting for cache rescue
    m_config = Kiface().KifaceSettings();
    bool rescueNeverShow = false;
    m_config->Read( RESCUE_NEVER_SHOW_KEY, &rescueNeverShow, false );
    m_cbRescue->SetValue( !rescueNeverShow );

    wxString msg = wxString::Format( _(
        "Project '%s'" ),
        GetChars( Prj().GetProjectFullName() )
        );

    SetTitle( msg );

    if( GetSizer() )
        GetSizer()->SetSizeHints( this );

    m_sdbSizer1OK->SetDefault();
}


void DIALOG_EESCHEMA_CONFIG::OnButtonUpClick( wxCommandEvent& event )
{
    wxArrayInt selections;

    m_ListLibr->GetSelections( selections );

    if ( selections.GetCount() <= 0 )   // No selection.
        return;

    if( selections[0] == 0 )            // The first lib is selected. cannot move up it
        return;

    wxArrayString libnames = m_ListLibr->GetStrings();

    for( size_t ii = 0; ii < selections.GetCount(); ii++ )
    {
        int jj = selections[ii];
        std::swap( libnames[jj],  libnames[jj-1]);
    }

    m_ListLibr->Set(libnames);

    // Reselect previously selected names
    for( size_t ii = 0; ii < selections.GetCount(); ii++ )
    {
        int jj = selections[ii];
        m_ListLibr->SetSelection(jj-1);
    }

    m_lib_list_changed = true;
}


void DIALOG_EESCHEMA_CONFIG::OnButtonDownClick( wxCommandEvent& event )
{
    wxArrayInt selections;

    m_ListLibr->GetSelections(selections);

    if ( selections.GetCount() <= 0 )   // No selection.
        return;

    // The last lib is selected. cannot move down it
    if( selections.Last() == (int)(m_ListLibr->GetCount()-1) )
        return;

    wxArrayString libnames = m_ListLibr->GetStrings();

    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
        int jj = selections[ii];
        std::swap( libnames[jj],  libnames[jj+1]);
    }

    m_ListLibr->Set( libnames );

    // Reselect previously selected names
    for( size_t ii = 0; ii < selections.GetCount(); ii++ )
    {
        int jj = selections[ii];
        m_ListLibr->SetSelection(jj+1);
    }

    m_lib_list_changed = true;
}


void DIALOG_EESCHEMA_CONFIG::OnCancelClick( wxCommandEvent& event )
{
    EndModal( wxID_CANCEL );
}


void DIALOG_EESCHEMA_CONFIG::OnOkClick( wxCommandEvent& event )
{
    // Give caller the changed paths
    if( m_lib_path_changed )
    {
        wxString paths;

        for( unsigned ii = 0; ii < m_listUserPaths->GetCount(); ii++ )
        {
            if( ii > 0 )
                paths += wxT( ';' );

            paths += m_listUserPaths->GetString( ii );
        }

        *m_callers_project_specific_lib_paths = paths;
    }

    // Update caller's lib_names if changed.
    if( m_lib_list_changed )
    {
        wxArrayString list;

        for( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii ++ )
            list.Add( m_ListLibr->GetString( ii ) );

        // Recreate lib list
        *m_callers_lib_names = list;
    }

    m_config->Write( RESCUE_NEVER_SHOW_KEY, ! m_cbRescue->GetValue() );

    EndModal( wxID_OK );
}


void DIALOG_EESCHEMA_CONFIG::OnCloseWindow( wxCloseEvent& event )
{
    EndModal( wxID_CANCEL );
}


void DIALOG_EESCHEMA_CONFIG::OnRemoveLibClick( wxCommandEvent& event )
{
    wxArrayInt selections;

    m_ListLibr->GetSelections( selections );

    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
        m_ListLibr->Delete( selections[ii] );
        m_lib_list_changed = true;
    }

    // Select next item after deleted in m_ListLibr
    if( m_ListLibr->GetCount() > 0 && selections.GetCount() > 0 )
    {
        int pos = selections[selections.GetCount()-1];

        if( pos == (int)m_ListLibr->GetCount() )
            pos = m_ListLibr->GetCount() - 1;

        m_ListLibr->SetSelection( pos );
    }
}


void DIALOG_EESCHEMA_CONFIG::OnAddOrInsertLibClick( wxCommandEvent& event )
{
    int             ii;
    wxString        libfilename;
    wxArrayInt      selections;

    PROJECT&        prj = Prj();

    m_ListLibr->GetSelections( selections );

    ii = selections.GetCount();

    if( ii > 0 )
        ii = selections[0];
    else
        ii = 0;

    wxString selection = m_DefaultLibraryPathslistBox->GetStringSelection();
    wxString libpath   = Prj().AbsolutePath( selection );

    if( !libpath )
    {
        libpath = prj.GetRString( PROJECT::SCH_LIB_PATH );
    }

    wxFileDialog filesDialog( this, _( "Library files:" ), libpath,
                              wxEmptyString, SchematicLibraryFileWildcard,
                              wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );

    if( filesDialog.ShowModal() != wxID_OK )
        return;

    wxArrayString   filenames;

    filesDialog.GetPaths( filenames );

    wxFileName      fn;

    // Build libs paths, to find later a relative path:
    wxArrayString paths;

    for( unsigned ll=0; ll < m_DefaultLibraryPathslistBox->GetCount(); ++ll )
        paths.Add( m_DefaultLibraryPathslistBox->GetString( ll ) );

    for( unsigned ll=0; ll < m_listUserPaths->GetCount(); ++ll )
        paths.Add( m_listUserPaths->GetString( ll ) );

    for( unsigned jj = 0; jj < filenames.GetCount(); jj++ )
    {
        fn = filenames[jj];

        if( jj == 0 )
            prj.SetRString( PROJECT::SCH_LIB_PATH, fn.GetPath() );

        // Extension is not stored, so remove extension:
        fn.SetExt( wxEmptyString );

        // Try to use relative path:
        for( unsigned ll = 0; ll < paths.GetCount(); ll++ )
        {
            wxFileName relfn = fn;
            relfn.MakeRelativeTo( paths[ll] );

            if( relfn.GetPath()[0] != '.' )
            {
                fn = relfn;
                break;
            }
        }

        libfilename = fn.GetFullPath();

        // Add or insert new library name, if not already in list
        if( m_ListLibr->FindString( libfilename, fn.IsCaseSensitive() ) == wxNOT_FOUND )
        {
            m_lib_list_changed = true;

            if( event.GetId() == ID_ADD_LIB )
                m_ListLibr->Append( libfilename );
            else
                m_ListLibr->Insert( libfilename, ii++ );
        }
        else
        {
            wxString msg = wxString::Format( _(
                "'%s' : library already in use" ),
                GetChars( libfilename )
                );
            DisplayError( this, msg );
        }
    }
}


void DIALOG_EESCHEMA_CONFIG::OnAddOrInsertPath( wxCommandEvent& event )
{
    PROJECT&        prj = Prj();
    wxString        abs_path = prj.GetRString( PROJECT::SCH_LIB_PATH );
    wxString        path;

    bool select = EDA_PATH_SELECTOR( _( "Default Path for Libraries" ),
                                     abs_path, wxDD_DEFAULT_STYLE,
                                     this, wxDefaultPosition );

    if( !select )
        return;

    if( !wxFileName::DirExists( abs_path ) )    // Should not occur
        return;

    // Add or insert path if not already in list
    if( m_listUserPaths->FindString( abs_path ) == wxNOT_FOUND )
    {
        int ipos = m_listUserPaths->GetCount();

        if( event.GetId() == wxID_INSERT_PATH )
        {
            if( ipos  )
                ipos--;

            int jj = m_listUserPaths->GetSelection();

            if( jj >= 0 )
                ipos = jj;
        }

        // Ask the user if this is a relative path
        int diag = wxMessageBox( _( "Use a relative path?" ), _( "Path type" ),
                                wxYES_NO | wxICON_QUESTION, this );

        if( diag == wxYES )
        {
            // Make it relative
            wxFileName fn = abs_path;
            fn.MakeRelativeTo( wxPathOnly( Prj().GetProjectFullName() ) );
            path = fn.GetPathWithSep() + fn.GetFullName();
        }
        else
            path = abs_path;

        m_listUserPaths->Insert( path, ipos );
        m_lib_path_changed = true;

        m_DefaultLibraryPathslistBox->InsertItems( 1, &path, ipos+1 );
    }
    else
    {
        DisplayError( this, _("Path already in use") );
    }

    prj.SetRString( PROJECT::SCH_LIB_PATH, abs_path );
}


static void remove_from_listbox( wxListBox* aListBox, const wxString& aText )
{
    wxArrayString   a;

    for( int i=0, cnt = aListBox->GetCount();  i<cnt;  ++i )
    {
        wxString item = aListBox->GetString( i );

        if( item != aText )
            a.Add( item );
    }

    aListBox->Clear();

    aListBox->InsertItems( a, 0 );
}


void DIALOG_EESCHEMA_CONFIG::OnRemoveUserPath( wxCommandEvent& event )
{
    int ii = m_listUserPaths->GetSelection();

    if( ii < 0 )
        ii = m_listUserPaths->GetCount()-1;

    if( ii >= 0 )
    {
        wxString sel = m_listUserPaths->GetStringSelection();

        remove_from_listbox( m_DefaultLibraryPathslistBox, sel );

        m_listUserPaths->Delete( ii );
        m_lib_path_changed = true;
    }
}


bool InvokeEeschemaConfig( wxWindow* aParent,
        wxString* aCallersProjectSpecificLibPaths, wxArrayString* aCallersLibNames )
{
    DIALOG_EESCHEMA_CONFIG  dlg( aParent,
            aCallersProjectSpecificLibPaths, aCallersLibNames );

    int ret = dlg.ShowModal();

    return wxID_OK == ret;
}