dialog_cvpcb_config.cpp.notused 13.7 KB
Newer Older
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
/**
 * @file dialog_cvpcb_config.cpp
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2012 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.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
 */
29

30
#include <fctsys.h>
31
#include <wx/tokenzr.h>
32
#include <pgm_base.h>
33 34 35 36 37
#include <common.h>
#include <confirm.h>
#include <gestfich.h>
#include <id.h>
#include <macros.h>
38

39 40
#include <cvpcb.h>
#include <cvpcb_mainframe.h>
41

42
#include <dialog_cvpcb_config.h>
43
#include <wildcards_and_files_ext.h>
44 45


46 47
DIALOG_CVPCB_CONFIG::DIALOG_CVPCB_CONFIG( CVPCB_MAINFRAME* aParent ) :
    DIALOG_CVPCB_CONFIG_FBP( aParent )
48
{
49 50 51
    wxString    title;
    wxFileName  fn = aParent->m_NetlistFileName;

52
    fn.SetExt( ProjectFileExtension );
53

54 55
    m_Parent = aParent;
    m_Config = Pgm().CommonSettings();
56

57
    Init( );
58
    title.Format( _( "Project file: '%s'" ), GetChars( fn.GetFullPath() ) );
59
    SetTitle( title );
60

61 62 63 64
    if( GetSizer() )
    {
        GetSizer()->SetSizeHints( this );
    }
65 66

    m_sdbSizer2OK->SetDefault();
67
}
68 69


70 71 72
void DIALOG_CVPCB_CONFIG::Init()
{
    wxString msg;
73

74
    SetFocus();
75

76 77
    m_LibListChanged = false;
    m_LibPathChanged = false;
78
    m_UserLibDirBufferImg = m_Parent->m_UserLibraryPath;
79

80 81
    m_ListLibr->InsertItems( m_Parent->m_ModuleLibNames, 0 );
    m_ListEquiv->InsertItems( m_Parent->m_AliasLibNames, 0 );
82

83
    m_TextHelpModulesFileName->SetValue( m_Parent->m_DocModulesFileName );
84

85 86
    // Load user libs paths:
    wxStringTokenizer Token( m_UserLibDirBufferImg, wxT( ";\n\r" ) );
87

88 89 90
    while( Token.HasMoreTokens() )
    {
        wxString path = Token.GetNextToken();
91

92 93 94
        if( wxFileName::DirExists( path ) )
            m_listUserPaths->Append( path );
    }
95

96
    // Display actual libraries paths:
97
    wxPathList libpaths = Pgm().GetLibraryPathList();
98

99 100 101 102
    for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
    {
        m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
    }
103

104
    // select the first path after the current path project
105 106
    if( libpaths.GetCount() > 1 )
        m_DefaultLibraryPathslistBox->Select( 1 );
107 108 109
}


110
void DIALOG_CVPCB_CONFIG::OnCancelClick( wxCommandEvent& event )
111
{
112 113
    // Recreate the user lib path
    if( m_LibPathChanged )
114
    {
115
        for( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii++ )
116
            Pgm().RemoveLibraryPath( m_listUserPaths->GetString( ii ) );
117

118
        Pgm().InsertLibraryPath( m_Parent->m_UserLibraryPath, 1 );
119
    }
120 121

    EndModal( wxID_CANCEL );
122 123 124
}


125
void DIALOG_CVPCB_CONFIG::OnOkClick( wxCommandEvent& event )
126
{
127
    m_Parent->m_DocModulesFileName = m_TextHelpModulesFileName->GetValue();
128

129 130 131
    // Recreate the user lib path
    if( m_LibPathChanged )
    {
132
        m_Parent->m_UserLibraryPath.Empty();
133

134 135 136
        for( unsigned ii = 0; ii < m_listUserPaths->GetCount(); ii++ )
        {
            if( ii > 0 )
137
                m_Parent->m_UserLibraryPath << wxT( ";" );
138

139
            m_Parent->m_UserLibraryPath << m_listUserPaths->GetString( ii );
140 141
        }
    }
142

143
    // Set new active library list if the lib list of if default path list was modified
144 145 146
    if( m_LibListChanged || m_LibPathChanged )
    {
        // Recreate lib list
147
        m_Parent->m_ModuleLibNames.Clear();
148

149
        for( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii++ )
150
            m_Parent->m_ModuleLibNames.Add( m_ListLibr->GetString( ii ) );
151 152

        // Recreate equ list
153
        m_Parent->m_AliasLibNames.Clear();
154

155
        for( unsigned ii = 0; ii < m_ListEquiv->GetCount(); ii++ )
156
            m_Parent->m_AliasLibNames.Add( m_ListEquiv->GetString( ii ) );
157

158
        m_Parent->LoadFootprintFiles();
159
        m_Parent->BuildFOOTPRINTS_LISTBOX();
160
        m_Parent->BuildLIBRARY_LISTBOX();
161
    }
162

163 164
    wxCommandEvent evt( ID_SAVE_PROJECT );
    m_Parent->SaveProjectFile( evt );
165
    EndModal( wxID_OK );
166
}
167 168


169 170 171 172
void DIALOG_CVPCB_CONFIG::OnCloseWindow( wxCloseEvent& event )
{
    EndModal( 0 );
}
173 174


175 176 177 178 179
/********************************************************************/
void DIALOG_CVPCB_CONFIG::OnButtonUpClick( wxCommandEvent& event )
/********************************************************************/
{
    wxListBox * list = m_ListLibr;
180

181 182 183 184 185 186 187
    if( (event.GetId() == ID_EQU_UP) || (event.GetId() == ID_EQU_DOWN) )
    {
        list = m_ListEquiv;
    }

    wxArrayInt selections;

188 189
    list->GetSelections( selections );

190 191 192 193 194 195 196 197 198 199 200
    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];
201
        EXCHG( libnames[jj],  libnames[jj-1] );
202
    }
203 204

    list->Set( libnames );
205 206 207 208 209

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

213
    m_LibListChanged = true;
214 215 216 217 218 219 220 221
}


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

223 224 225 226 227 228 229
    if( (event.GetId() == ID_EQU_UP) || (event.GetId() == ID_EQU_DOWN) )
    {
        list = m_ListEquiv;
    }

    wxArrayInt selections;

230 231
    list->GetSelections( selections );

232 233 234 235 236 237 238 239 240 241 242 243 244 245
    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]);
    }
246 247

    list->Set( libnames );
248 249 250 251 252 253 254

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

256
    m_LibListChanged = true;
257 258 259
}


260
/* Remove a library to the library list.
261
 * The real list (g_LibName_List) is not changed, so the change can be canceled
262
 */
263
void DIALOG_CVPCB_CONFIG::OnRemoveLibClick( wxCommandEvent& event )
264 265
{
    wxListBox * list = m_ListEquiv;
266

267 268
    if( event.GetId() == ID_REMOVE_LIB )
        list = m_ListLibr;
269

270
    wxArrayInt selections;
271

272 273
    list->GetSelections( selections );

274 275 276
    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
        list->Delete(selections[ii] );
277
        m_LibListChanged = true;
278
    }
279
}
280 281


282 283 284
/* 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
285
 * The real list (g_LibName_List) is not changed, so the change can be canceled
286
 */
287
void DIALOG_CVPCB_CONFIG::OnAddOrInsertLibClick( wxCommandEvent& event )
288 289 290 291
{
    int        ii;
    wxString   libfilename, wildcard;
    wxFileName fn;
292

293
    bool       insert = false;
294

295 296
    if( (event.GetId() == ID_INSERT_EQU) || (event.GetId() == ID_INSERT_LIB) )
        insert = true;
297

298
    wildcard = FootprintAliasFileWildcard;
299 300

    wxListBox* list = m_ListEquiv;
301

302 303 304
    if( (event.GetId() == ID_ADD_LIB) || (event.GetId() == ID_INSERT_LIB) )
    {
        list = m_ListLibr;
305
        wildcard = LegacyFootprintLibPathWildcard;
306
    }
307

308 309 310 311
    wxArrayInt selections;
    list->GetSelections(selections);

    ii = selections.GetCount();
312

313 314 315
    if( ii > 0 )
        ii = selections[0];
    else
316
        ii = 0;
317

318 319
    wxString libpath;
    libpath = m_DefaultLibraryPathslistBox->GetStringSelection();
320

321
    if( libpath.IsEmpty() )
322
        libpath = Pgm().LastVisitedLibraryPath();
323

324 325 326
    wxFileDialog FilesDialog( this, _( "Footprint library files:" ), libpath,
                              wxEmptyString, wildcard,
                              wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );
327

328 329
    if( FilesDialog.ShowModal() != wxID_OK )
        return;
330

331 332
    wxArrayString Filenames;
    FilesDialog.GetPaths( Filenames );
333

334 335 336
    for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
    {
        fn = Filenames[jj];
337

338
        if( jj == 0 )
339
            Pgm().SaveLastVisitedLibraryPath( fn.GetPath() );
340 341 342 343 344

        /* 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,
345 346
         * because it preserve use of default libraries paths, when the path
         * is a sub path of these default paths
347
         */
348
        libfilename = Pgm().FilenameWithRelativePathInSearchList( fn.GetFullPath() );
349

350 351
        // Remove extension:
        fn = libfilename;
352
        fn.SetExt( wxEmptyString );
353
        libfilename = fn.GetFullPath();
354

355
        // Add or insert new library name, if not already in list
356 357
        if( list->FindString( libfilename, fn.IsCaseSensitive() ) == wxNOT_FOUND )
        {
358
            m_LibListChanged = true;
359

360 361 362 363 364 365 366 367 368 369 370 371
            if( ! insert )
                list->Append( libfilename );
            else
                list->Insert( libfilename, ii++ );
        }
        else
        {
            wxString msg = wxT( "<" ) + libfilename + wxT( "> : " ) +
                           _( "Library already in use" );
            DisplayError( this, msg );
        }
    }
372 373 374
}


375
void DIALOG_CVPCB_CONFIG::OnAddOrInsertPath( wxCommandEvent& event )
376
{
377
    wxString path = Pgm().LastVisitedLibraryPath();
378

379 380
    bool     select = EDA_DirectorySelector( _( "Default Path for Libraries" ),
                                             path,
381
                                             wxDD_DEFAULT_STYLE,
382
                                             this,
383
                                             wxDefaultPosition );
384

385 386
    if( !select )
        return;
387

388 389
    if( !wxFileName::DirExists( path ) )     // Should not occurs
        return;
390

391 392 393 394
    // Add or insert path if not already in list
    if( m_listUserPaths->FindString( path ) == wxNOT_FOUND )
    {
        int ipos = m_listUserPaths->GetCount();
395

396 397 398 399
        if( event.GetId() == ID_INSERT_PATH )
        {
            if( ipos  )
                ipos--;
400

401
            int jj = m_listUserPaths->GetSelection();
402

403 404 405
            if( jj >= 0 )
                ipos = jj;
        }
charras's avatar
charras committed
406 407

        // Ask the user if this is a relative path
408 409 410
        int diag = wxMessageBox( _( "Use a relative path?" ),
                                 _( "Path type" ),
                                 wxYES_NO | wxICON_QUESTION, this );
charras's avatar
charras committed
411 412 413 414

        if( diag == wxYES )
        {   // Make it relative
            wxFileName fn = path;
415
            fn.MakeRelativeTo( wxT( "." ) );
charras's avatar
charras committed
416 417 418
            path = fn.GetPathWithSep() + fn.GetFullName();
        }

419 420
        m_listUserPaths->Insert( path, ipos );
        m_LibPathChanged = true;
421
        Pgm().InsertLibraryPath( path, ipos + 1 );
422 423

        // Display actual libraries paths:
424
        wxPathList libpaths = Pgm().GetLibraryPathList();
425
        m_DefaultLibraryPathslistBox->Clear();
426

427 428 429 430 431 432
        for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
        {
            m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
        }
    }
    else
433
    {
434
        DisplayError( this, _( "Path already in use" ) );
435
    }
436

437
    Pgm().SaveLastVisitedLibraryPath( path );
438 439 440
}


441
void DIALOG_CVPCB_CONFIG::OnRemoveUserPath( wxCommandEvent& event )
442
{
443
    int ii = m_listUserPaths->GetSelection();
444

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

448 449
    if( ii >= 0 )
    {
450
        Pgm().RemoveLibraryPath( m_listUserPaths->GetStringSelection() );
451 452 453
        m_listUserPaths->Delete( ii );
        m_LibPathChanged = true;
    }
454

455
    // Display actual libraries paths:
456
    wxPathList libpaths = Pgm().GetLibraryPathList();
457
    m_DefaultLibraryPathslistBox->Clear();
458

459 460 461 462
    for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
    {
        m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
    }
463 464 465
}


466
void DIALOG_CVPCB_CONFIG::OnBrowseModDocFile( wxCommandEvent& event )
467
{
468
    wxString FullFileName;
469
    wxString docpath, filename;
470

471
    docpath = Pgm().LastVisitedLibraryPath( wxT( "doc" ) );
472

473
    wxFileDialog FilesDialog( this, _( "Footprint document file:" ), docpath,
474 475
                              wxEmptyString, PdfFileWildcard,
                              wxFD_DEFAULT_STYLE | wxFD_FILE_MUST_EXIST );
476

477 478
    if( FilesDialog.ShowModal() != wxID_OK )
        return;
479

480
    FullFileName = FilesDialog.GetPath();
481

482 483 484 485
    /* 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,
486 487
     * because it preserve use of default libraries paths, when the path is
     * a sub path of these default paths
488 489
     */
    wxFileName fn = FullFileName;
490
    Pgm().SaveLastVisitedLibraryPath( fn.GetPath() );
491

492
    filename = Pgm().FilenameWithRelativePathInSearchList( FullFileName );
493
    m_TextHelpModulesFileName->SetValue( filename );
494
}