dialog_cvpcb_config.cpp 13.8 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 33 34 35 36 37
#include <appl_wxstruct.h>
#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
DIALOG_CVPCB_CONFIG::DIALOG_CVPCB_CONFIG( CVPCB_MAINFRAME* parent ) :
47 48 49
    DIALOG_CVPCB_CONFIG_FBP( parent )
{
    wxString title;
50 51
    wxFileName fn = parent->m_NetlistFileName;
    fn.SetExt( ProjectFileExtension );
52 53

    m_Parent   = parent;
54
    m_Config = wxGetApp().GetCommonSettings();
55

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

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

    m_sdbSizer2OK->SetDefault();
66
}
67 68


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

73
    SetFocus();
74

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

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

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

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

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

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

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

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

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


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

117
        wxGetApp().InsertLibraryPath( m_Parent->m_UserLibraryPath, 1 );
118
    }
119 120

    EndModal( wxID_CANCEL );
121 122 123
}


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

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

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

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

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

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

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

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

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

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


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


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

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

    wxArrayInt selections;

187 188
    list->GetSelections( selections );

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

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

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

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


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

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

    wxArrayInt selections;

229 230
    list->GetSelections( selections );

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

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

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

255
    m_LibListChanged = true;
256 257 258
}


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

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

269
    wxArrayInt selections;
270

271 272
    list->GetSelections( selections );

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


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

292
    bool       insert = false;
293

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

297
    wildcard = FootprintAliasFileWildcard;
298
    wxListBox * list = m_ListEquiv;
299

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

306 307 308 309
    wxArrayInt selections;
    list->GetSelections(selections);

    ii = selections.GetCount();
310

311 312 313
    if( ii > 0 )
        ii = selections[0];
    else
314
        ii = 0;
315

316 317
    wxString libpath;
    libpath = m_DefaultLibraryPathslistBox->GetStringSelection();
318

319 320
    if( libpath.IsEmpty() )
        libpath = wxGetApp().ReturnLastVisitedLibraryPath();
321

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

326 327
    if( FilesDialog.ShowModal() != wxID_OK )
        return;
328

329 330
    wxArrayString Filenames;
    FilesDialog.GetPaths( Filenames );
331

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

336 337 338 339 340 341 342
        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,
343 344
         * because it preserve use of default libraries paths, when the path
         * is a sub path of these default paths
345
         */
346
        libfilename = wxGetApp().ReturnFilenameWithRelativePathInLibPath( fn.GetFullPath() );
347

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

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

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


373
void DIALOG_CVPCB_CONFIG::OnAddOrInsertPath( wxCommandEvent& event )
374
{
375
    wxString path = wxGetApp().ReturnLastVisitedLibraryPath();
376

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

383 384
    if( !select )
        return;
385

386 387
    if( !wxFileName::DirExists( path ) )     // Should not occurs
        return;
388

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

394 395 396 397
        if( event.GetId() == ID_INSERT_PATH )
        {
            if( ipos  )
                ipos--;
398

399
            int jj = m_listUserPaths->GetSelection();
400

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

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

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

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

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

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

435
    wxGetApp().SaveLastVisitedLibraryPath( path );
436 437 438
}


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

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

446 447 448 449 450 451
    if( ii >= 0 )
    {
        wxGetApp().RemoveLibraryPath( m_listUserPaths->GetStringSelection() );
        m_listUserPaths->Delete( ii );
        m_LibPathChanged = true;
    }
452

453 454 455
    // Display actual libraries paths:
    wxPathList libpaths = wxGetApp().GetLibraryPathList();
    m_DefaultLibraryPathslistBox->Clear();
456

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


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

469
    docpath = wxGetApp().ReturnLastVisitedLibraryPath( wxT( "doc" ) );
470

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

475 476
    if( FilesDialog.ShowModal() != wxID_OK )
        return;
477

478
    FullFileName = FilesDialog.GetPath();
479

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

490 491
    filename = wxGetApp().ReturnFilenameWithRelativePathInLibPath( FullFileName );
    m_TextHelpModulesFileName->SetValue( filename );
492
}