dialog_cvpcb_config.cpp 12.7 KB
Newer Older
1 2 3
/////////////////////////////////////////////////////////////////////////////
// Name:        dialog_cvpcb_config.cpp
// Author:      jean-pierre Charras
4
// Licence:     gpl
5 6
/////////////////////////////////////////////////////////////////////////////

7 8 9 10 11 12
#include "fctsys.h"
#include <wx/tokenzr.h>
#include "appl_wxstruct.h"
#include "common.h"
#include "confirm.h"
#include "gestfich.h"
13
#include "id.h"
14
#include "macros.h"
15

16
#include "cvpcb.h"
17
#include "cvpcb_mainframe.h"
18

19
#include "protos.h"
20

21 22 23
#include "dialog_cvpcb_config.h"


24
DIALOG_CVPCB_CONFIG::DIALOG_CVPCB_CONFIG( CVPCB_MAINFRAME* parent ) :
25 26 27
    DIALOG_CVPCB_CONFIG_FBP( parent )
{
    wxString title;
28 29
    wxFileName fn = parent->m_NetlistFileName;
    fn.SetExt( ProjectFileExtension );
30 31 32

    m_Parent   = parent;
    m_Config = wxGetApp().m_EDA_CommonConfig;
33

34
    Init( );
35
    title = _( "Project file: " ) + fn.GetFullPath();
36
    SetTitle( title );
37

38 39 40 41
    if( GetSizer() )
    {
        GetSizer()->SetSizeHints( this );
    }
42 43

    m_sdbSizer2OK->SetDefault();
44
}
45 46


47 48 49
void DIALOG_CVPCB_CONFIG::Init()
{
    wxString msg;
50

51
    SetFocus();
52

53 54
    m_LibListChanged = false;
    m_LibPathChanged = false;
55
    m_UserLibDirBufferImg = m_Parent->m_UserLibraryPath;
56

57 58
    m_ListLibr->InsertItems( m_Parent->m_ModuleLibNames, 0 );
    m_ListEquiv->InsertItems( m_Parent->m_AliasLibNames, 0 );
59

60
    m_TextHelpModulesFileName->SetValue( m_Parent->m_DocModulesFileName );
61

62 63
    // Load user libs paths:
    wxStringTokenizer Token( m_UserLibDirBufferImg, wxT( ";\n\r" ) );
64

65 66 67
    while( Token.HasMoreTokens() )
    {
        wxString path = Token.GetNextToken();
68

69 70 71
        if( wxFileName::DirExists( path ) )
            m_listUserPaths->Append( path );
    }
72

73 74
    // Display actual libraries paths:
    wxPathList libpaths = wxGetApp().GetLibraryPathList();
75

76 77 78 79
    for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
    {
        m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
    }
80

81
    // select the first path after the current path project
82 83
    if( libpaths.GetCount() > 1 )
        m_DefaultLibraryPathslistBox->Select( 1 );
84 85 86
}


87
void DIALOG_CVPCB_CONFIG::OnCancelClick( wxCommandEvent& event )
88
{
89 90
    // Recreate the user lib path
    if( m_LibPathChanged )
91
    {
92 93 94
        for( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii++ )
            wxGetApp().RemoveLibraryPath( m_listUserPaths->GetString( ii ) );

95
        wxGetApp().InsertLibraryPath( m_Parent->m_UserLibraryPath, 1 );
96
    }
97 98

    EndModal( wxID_CANCEL );
99 100 101
}


102
void DIALOG_CVPCB_CONFIG::OnOkClick( wxCommandEvent& event )
103
{
104
    m_Parent->m_DocModulesFileName = m_TextHelpModulesFileName->GetValue();
105

106 107 108
    // Recreate the user lib path
    if( m_LibPathChanged )
    {
109
        m_Parent->m_UserLibraryPath.Empty();
110

111 112 113
        for( unsigned ii = 0; ii < m_listUserPaths->GetCount(); ii++ )
        {
            if( ii > 0 )
114
                m_Parent->m_UserLibraryPath << wxT( ";" );
115

116
            m_Parent->m_UserLibraryPath << m_listUserPaths->GetString( ii );
117 118
        }
    }
119

120
    // Set new active library list if the lib list of if default path list was modified
121 122 123
    if( m_LibListChanged || m_LibPathChanged )
    {
        // Recreate lib list
124
        m_Parent->m_ModuleLibNames.Clear();
125

126
        for( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii++ )
127
            m_Parent->m_ModuleLibNames.Add( m_ListLibr->GetString( ii ) );
128 129

        // Recreate equ list
130
        m_Parent->m_AliasLibNames.Clear();
131

132
        for( unsigned ii = 0; ii < m_ListEquiv->GetCount(); ii++ )
133
            m_Parent->m_AliasLibNames.Add( m_ListEquiv->GetString( ii ) );
134

135
        m_Parent->LoadFootprintFiles();
136
        m_Parent->BuildFOOTPRINTS_LISTBOX();
137
    }
138

139 140
    wxCommandEvent evt( ID_SAVE_PROJECT );
    m_Parent->SaveProjectFile( evt );
141
    EndModal( wxID_OK );
142
}
143 144


145 146 147 148
void DIALOG_CVPCB_CONFIG::OnCloseWindow( wxCloseEvent& event )
{
    EndModal( 0 );
}
149 150


151 152 153 154 155
/********************************************************************/
void DIALOG_CVPCB_CONFIG::OnButtonUpClick( wxCommandEvent& event )
/********************************************************************/
{
    wxListBox * list = m_ListLibr;
156

157 158 159 160 161 162 163
    if( (event.GetId() == ID_EQU_UP) || (event.GetId() == ID_EQU_DOWN) )
    {
        list = m_ListEquiv;
    }

    wxArrayInt selections;

164 165
    list->GetSelections( selections );

166 167 168 169 170 171 172 173 174 175 176
    if ( selections.GetCount() <= 0 )   // No selection.
        return;

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

    wxArrayString libnames = list->GetStrings();

    for( size_t ii = 0; ii < selections.GetCount(); ii++ )
    {
        int jj = selections[ii];
177
        EXCHG( libnames[jj],  libnames[jj-1] );
178
    }
179 180

    list->Set( libnames );
181 182 183 184 185

    // Reselect previously selected names
    for( size_t ii = 0; ii < selections.GetCount(); ii++ )
    {
        int jj = selections[ii];
186
        list->SetSelection( jj-1 );
187 188 189 190 191 192 193 194 195 196 197
    }

    m_LibListChanged = TRUE;
}


/*********************************************************************/
void DIALOG_CVPCB_CONFIG::OnButtonDownClick( wxCommandEvent& event )
/*********************************************************************/
{
    wxListBox * list = m_ListLibr;
198

199 200 201 202 203 204 205
    if( (event.GetId() == ID_EQU_UP) || (event.GetId() == ID_EQU_DOWN) )
    {
        list = m_ListEquiv;
    }

    wxArrayInt selections;

206 207
    list->GetSelections( selections );

208 209 210 211 212 213 214 215 216 217 218 219 220 221
    if ( selections.GetCount() <= 0 )   // No selection.
        return;

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

    wxArrayString libnames = list->GetStrings();

    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
        int jj = selections[ii];
        EXCHG( libnames[jj],  libnames[jj+1]);
    }
222 223

    list->Set( libnames );
224 225 226 227 228 229 230

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

232 233 234 235
    m_LibListChanged = TRUE;
}


236
/* Remove a library to the library list.
237
 * The real list (g_LibName_List) is not changed, so the change can be canceled
238
 */
239
void DIALOG_CVPCB_CONFIG::OnRemoveLibClick( wxCommandEvent& event )
240 241
{
    wxListBox * list = m_ListEquiv;
242

243 244
    if( event.GetId() == ID_REMOVE_LIB )
        list = m_ListLibr;
245

246
    wxArrayInt selections;
247

248 249
    list->GetSelections( selections );

250 251 252 253 254
    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
        list->Delete(selections[ii] );
        m_LibListChanged = TRUE;
    }
255
}
256 257


258 259 260
/* 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
261
 * The real list (g_LibName_List) is not changed, so the change can be canceled
262
 */
263
void DIALOG_CVPCB_CONFIG::OnAddOrInsertLibClick( wxCommandEvent& event )
264 265 266 267
{
    int        ii;
    wxString   libfilename, wildcard;
    wxFileName fn;
268

269
    bool       insert = false;
270

271 272
    if( (event.GetId() == ID_INSERT_EQU) || (event.GetId() == ID_INSERT_LIB) )
        insert = true;
273

274
    wildcard = FootprintAliasFileWildcard;
275
    wxListBox * list = m_ListEquiv;
276

277 278 279
    if( (event.GetId() == ID_ADD_LIB) || (event.GetId() == ID_INSERT_LIB) )
    {
        list = m_ListLibr;
280
        wildcard = ModuleFileWildcard;
281
    }
282

283 284 285 286
    wxArrayInt selections;
    list->GetSelections(selections);

    ii = selections.GetCount();
287

288 289 290
    if( ii > 0 )
        ii = selections[0];
    else
291
        ii = 0;
292

293 294
    wxString libpath;
    libpath = m_DefaultLibraryPathslistBox->GetStringSelection();
295

296 297
    if( libpath.IsEmpty() )
        libpath = wxGetApp().ReturnLastVisitedLibraryPath();
298

299 300 301
    wxFileDialog FilesDialog( this, _( "Footprint library files:" ), libpath,
                              wxEmptyString, wildcard,
                              wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );
302

303 304
    if( FilesDialog.ShowModal() != wxID_OK )
        return;
305

306 307
    wxArrayString Filenames;
    FilesDialog.GetPaths( Filenames );
308

309 310 311
    for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
    {
        fn = Filenames[jj];
312

313 314 315 316 317 318 319
        if( jj == 0 )
            wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );

        /* If the library path is already in the library search paths
         * list, just add the library name to the list.  Otherwise, add
         * the library name with the full or relative path.
         * the relative path, when possible is preferable,
320 321
         * because it preserve use of default libraries paths, when the path
         * is a sub path of these default paths
322
         */
323
        libfilename = wxGetApp().ReturnFilenameWithRelativePathInLibPath( fn.GetFullPath() );
324

325 326
        // Remove extension:
        fn = libfilename;
327
        fn.SetExt( wxEmptyString );
328
        libfilename = fn.GetFullPath();
329

330
        // Add or insert new library name, if not already in list
331 332 333
        if( list->FindString( libfilename, fn.IsCaseSensitive() ) == wxNOT_FOUND )
        {
            m_LibListChanged = TRUE;
334

335 336 337 338 339 340 341 342 343 344 345 346
            if( ! insert )
                list->Append( libfilename );
            else
                list->Insert( libfilename, ii++ );
        }
        else
        {
            wxString msg = wxT( "<" ) + libfilename + wxT( "> : " ) +
                           _( "Library already in use" );
            DisplayError( this, msg );
        }
    }
347 348 349
}


350
void DIALOG_CVPCB_CONFIG::OnAddOrInsertPath( wxCommandEvent& event )
351
{
352
    wxString path = wxGetApp().ReturnLastVisitedLibraryPath();
353

354 355
    bool     select = EDA_DirectorySelector( _( "Default Path for Libraries" ),
                                             path,
356
                                             wxDD_DEFAULT_STYLE,
357
                                             this,
358
                                             wxDefaultPosition );
359

360 361
    if( !select )
        return;
362

363 364
    if( !wxFileName::DirExists( path ) )     // Should not occurs
        return;
365

366 367 368 369
    // Add or insert path if not already in list
    if( m_listUserPaths->FindString( path ) == wxNOT_FOUND )
    {
        int ipos = m_listUserPaths->GetCount();
370

371 372 373 374
        if( event.GetId() == ID_INSERT_PATH )
        {
            if( ipos  )
                ipos--;
375

376
            int jj = m_listUserPaths->GetSelection();
377

378 379 380
            if( jj >= 0 )
                ipos = jj;
        }
charras's avatar
charras committed
381 382

        // Ask the user if this is a relative path
383 384 385
        int diag = wxMessageBox( _( "Use a relative path?" ),
                                 _( "Path type" ),
                                 wxYES_NO | wxICON_QUESTION, this );
charras's avatar
charras committed
386 387 388 389

        if( diag == wxYES )
        {   // Make it relative
            wxFileName fn = path;
390
            fn.MakeRelativeTo( wxT( "." ) );
charras's avatar
charras committed
391 392 393
            path = fn.GetPathWithSep() + fn.GetFullName();
        }

394 395 396 397 398 399 400
        m_listUserPaths->Insert( path, ipos );
        m_LibPathChanged = true;
        wxGetApp().InsertLibraryPath( path, ipos + 1 );

        // Display actual libraries paths:
        wxPathList libpaths = wxGetApp().GetLibraryPathList();
        m_DefaultLibraryPathslistBox->Clear();
401

402 403 404 405 406 407
        for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
        {
            m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
        }
    }
    else
408
    {
409
        DisplayError( this, _( "Path already in use" ) );
410
    }
411

412
    wxGetApp().SaveLastVisitedLibraryPath( path );
413 414 415
}


416
void DIALOG_CVPCB_CONFIG::OnRemoveUserPath( wxCommandEvent& event )
417
{
418
    int ii = m_listUserPaths->GetSelection();
419

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

423 424 425 426 427 428
    if( ii >= 0 )
    {
        wxGetApp().RemoveLibraryPath( m_listUserPaths->GetStringSelection() );
        m_listUserPaths->Delete( ii );
        m_LibPathChanged = true;
    }
429

430 431 432
    // Display actual libraries paths:
    wxPathList libpaths = wxGetApp().GetLibraryPathList();
    m_DefaultLibraryPathslistBox->Clear();
433

434 435 436 437
    for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
    {
        m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
    }
438 439 440
}


441
void DIALOG_CVPCB_CONFIG::OnBrowseModDocFile( wxCommandEvent& event )
442
{
443
    wxString FullFileName;
444
    wxString docpath, filename;
445

446
    docpath = wxGetApp().ReturnLastVisitedLibraryPath( wxT( "doc" ) );
447

448
    wxFileDialog FilesDialog( this, _( "Footprint document file:" ), docpath,
449 450
                              wxEmptyString, PdfFileWildcard,
                              wxFD_DEFAULT_STYLE | wxFD_FILE_MUST_EXIST );
451

452 453
    if( FilesDialog.ShowModal() != wxID_OK )
        return;
454

455
    FullFileName = FilesDialog.GetPath();
456

457 458 459 460
    /* If the path is already in the library search paths
     * list, just add the library name to the list.  Otherwise, add
     * the library name with the full or relative path.
     * the relative path, when possible is preferable,
461 462
     * because it preserve use of default libraries paths, when the path is
     * a sub path of these default paths
463 464 465
     */
    wxFileName fn = FullFileName;
    wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
466

467 468
    filename = wxGetApp().ReturnFilenameWithRelativePathInLibPath( FullFileName );
    m_TextHelpModulesFileName->SetValue( filename );
469
}