dialog_pcbnew_config_libs_and_paths.cpp 12.6 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 29
/**
 * @file ^pcbnew/dialogs/dialog_pcbnew_config_libs_and_paths.cpp
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004-2012 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2004-2012 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
 */


30

31
/* Handle the Pcbnew library config (library list, and default lib path)
32 33
 */

34
#include <fctsys.h>
35
#include <wx/tokenzr.h>
36 37 38 39 40 41 42
#include <appl_wxstruct.h>
#include <confirm.h>
#include <gestfich.h>
#include <pcbnew.h>
#include <macros.h>
#include <wxPcbStruct.h>
#include <pcbcommon.h>
43
#include <wildcards_and_files_ext.h>
44 45

#include <dialog_pcbnew_config_libs_and_paths.h>
46
#include <wildcards_and_files_ext.h>
47 48


49
void PCB_EDIT_FRAME::InstallConfigFrame( )
50 51 52 53 54 55
{
    DIALOG_PCBNEW_CONFIG_LIBS dialog( this );
    dialog.ShowModal();
}


56
DIALOG_PCBNEW_CONFIG_LIBS::DIALOG_PCBNEW_CONFIG_LIBS( PCB_EDIT_FRAME* parent ):
57 58
    DIALOG_PCBNEW_CONFIG_LIBS_FBP(parent)
{
59
    m_Config = wxGetApp().GetCommonSettings();
60 61 62

    Init( );

63 64
    wxString title;
    title.Printf( _( "from <%s>" ), GetChars( wxGetApp().GetCurrentOptionFile() ) );
65 66
    SetTitle( title );

67 68
    m_sdbSizer1OK->SetDefault();

69
    if( GetSizer() )
70
        GetSizer()->SetSizeHints( this );
71 72
}

73

74 75 76 77 78 79
void DIALOG_PCBNEW_CONFIG_LIBS::Init()
{
    SetFocus();

    m_LibListChanged = false;
    m_LibPathChanged = false;
80
    m_UserLibDirBufferImg = g_UserLibDirBuffer;  // Save the original lib path
81

82
    m_ListLibr->InsertItems( g_LibraryNames, 0 );
83 84 85 86 87 88 89

    // Display current modules doc file:
    m_Config->Read( wxT( "module_doc_file" ), g_DocModulesFileName );
    m_TextHelpModulesFileName->SetValue( g_DocModulesFileName );

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

91 92 93
    while( Token.HasMoreTokens() )
    {
        wxString path = Token.GetNextToken();
94

95 96 97 98 99 100 101 102 103 104 105
        if( wxFileName::DirExists( path ) )
            m_listUserPaths->Append(path);
    }

    // Display actual libraries paths:
    wxPathList libpaths = wxGetApp().GetLibraryPathList();
    for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
    {
        m_DefaultLibraryPathslistBox->Append( libpaths[ii]);
    }

106
    // select the first path after the current path project
107 108 109 110 111 112 113 114 115 116
    if ( libpaths.GetCount() > 1 )
        m_DefaultLibraryPathslistBox->Select( 1 );
}


void DIALOG_PCBNEW_CONFIG_LIBS::OnCancelClick( wxCommandEvent& event )
{
    // Recreate the user lib path
    if ( m_LibPathChanged )
    {
117 118 119
        for ( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii++ )
            wxGetApp().RemoveLibraryPath( m_listUserPaths->GetString(ii) );

120 121
        wxGetApp().InsertLibraryPath( g_UserLibDirBuffer, 1);
    }
122 123

    EndModal( wxID_CANCEL );
124 125 126 127 128
}


void DIALOG_PCBNEW_CONFIG_LIBS::OnOkClick( wxCommandEvent& event )
{
129
     m_Config->Write( wxT( "module_doc_file" ), m_TextHelpModulesFileName->GetValue() );
130 131 132 133 134

    // Recreate the user lib path
    if ( m_LibPathChanged )
    {
        g_UserLibDirBuffer.Empty();
135

136 137 138 139
        for ( unsigned ii = 0; ii < m_listUserPaths->GetCount(); ii ++ )
        {
            if ( ii > 0 )
                g_UserLibDirBuffer << wxT(";");
140

141 142 143 144
            g_UserLibDirBuffer << m_listUserPaths->GetString(ii);
        }
    }

145
    // Set new active library list if the lib list of if default path list was modified
146 147 148
    if( m_LibListChanged || m_LibPathChanged )
    {
        // Recreate lib list
149
        g_LibraryNames.Clear();
150

151
        for ( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii ++ )
152
            g_LibraryNames.Add( m_ListLibr->GetString(ii) );
153
    }
154

155
    GetParent()->SaveProjectSettings( true );
156
    EndModal( wxID_OK );
157 158
}

159

160 161
void DIALOG_PCBNEW_CONFIG_LIBS::OnCloseWindow( wxCloseEvent& event )
{
162
    EndModal( wxID_CANCEL );
163 164 165
}


166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
void DIALOG_PCBNEW_CONFIG_LIBS::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];
        EXCHG( libnames[jj],  libnames[jj-1]);
    }
184

185 186 187 188 189 190 191 192 193
    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);
    }

194
    m_LibListChanged = true;
195 196 197 198 199 200 201 202
}


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

    m_ListLibr->GetSelections(selections);
203

204 205 206 207 208 209 210 211 212 213 214 215
    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];
216
        EXCHG( libnames[jj], libnames[jj+1] );
217
    }
218

219 220 221 222 223 224 225 226
    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);
    }
227

228
    m_LibListChanged = true;
229 230
}

231 232

/* Remove a library to the library list.
233
 * The real list (g_LibName_List) is not changed, so the change can be canceled
234
 */
235
void DIALOG_PCBNEW_CONFIG_LIBS::OnRemoveLibClick( wxCommandEvent& event )
236
{
237
    wxArrayInt selections;
238

239 240
    m_ListLibr->GetSelections( selections );

241 242
    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
243
        m_ListLibr->Delete( selections[ii] );
244
        m_LibListChanged = true;
245
    }
246 247 248 249 250 251
}


/* 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
252
 * The real list (g_LibName_List) is not changed, so the change can be canceled
253
 */
254
void DIALOG_PCBNEW_CONFIG_LIBS::OnAddOrInsertLibClick( wxCommandEvent& event )
255
{
256
    int        ii = 0;
257 258 259
    wxString   libfilename;
    wxFileName fn;

260 261 262 263
    wxArrayInt selections;
    m_ListLibr->GetSelections(selections);

    ii = selections.GetCount();
264

265 266 267
    if( ii > 0 )
        ii = selections[0];
    else
268 269 270 271 272 273 274 275
        ii = 0;

    wxString libpath;
    libpath = m_DefaultLibraryPathslistBox->GetStringSelection();
    if ( libpath.IsEmpty() )
        libpath = wxGetApp().ReturnLastVisitedLibraryPath();

    wxFileDialog FilesDialog( this, _( "Footprint library files:" ), libpath,
276
                              wxEmptyString,
277
                              wxGetTranslation( LegacyFootprintLibPathWildcard ),
278 279 280 281 282 283 284 285
                              wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );

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

    wxArrayString Filenames;
    FilesDialog.GetPaths( Filenames );

286
    for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
287 288
    {
        fn = Filenames[jj];
289

290 291 292 293 294 295 296
        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,
297 298 299
         * because it preserve use of default libraries paths, when the
         * path is a sub path of these default paths
         */
300
        libfilename = wxGetApp().ReturnFilenameWithRelativePathInLibPath( fn.GetFullPath() );
301

302 303
        // Remove extension:
        fn = libfilename;
304
        fn.SetExt( wxEmptyString );
305
        libfilename = fn.GetFullPath();
306 307 308 309

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

312 313 314 315 316 317 318 319
            if( event.GetId() == ID_ADD_LIB )
                m_ListLibr->Append( libfilename );
            else
                m_ListLibr->Insert( libfilename, ii++ );
        }
        else
        {
            wxString msg = wxT( "<" ) + libfilename + wxT( "> : " ) +
320
                           _( "Library already in use" );
321 322 323 324 325 326 327 328 329 330
            DisplayError( this, msg );
        }
    }
}


void DIALOG_PCBNEW_CONFIG_LIBS::OnAddOrInsertPath( wxCommandEvent& event )
{
    wxString path = wxGetApp().ReturnLastVisitedLibraryPath();

331 332 333
    bool     select = EDA_DirectorySelector( _( "Default Path for Libraries" ),
                                             path, wxDD_DEFAULT_STYLE,
                                             this, wxDefaultPosition );
334 335 336 337 338 339 340 341 342 343 344

    if( !select )
        return;

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

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

346 347
        if ( event.GetId() == wxID_INSERT_PATH )
        {
348 349 350
            if ( ipos  )
                ipos--;

351
            int jj = m_listUserPaths->GetSelection();
352

353 354 355
            if ( jj >= 0 )
                ipos = jj;
        }
charras's avatar
charras committed
356 357

        // Ask the user if this is a relative path
358 359 360
        int diag = wxMessageBox( _( "Use a relative path?" ),
                                 _( "Path type" ),
                                 wxYES_NO | wxICON_QUESTION, this );
charras's avatar
charras committed
361 362 363 364

        if( diag == wxYES )
        {   // Make it relative
            wxFileName fn = path;
365
            fn.MakeRelativeTo( wxT( "." ) );
charras's avatar
charras committed
366 367 368
            path = fn.GetPathWithSep() + fn.GetFullName();
        }

369 370 371 372 373 374 375
        m_listUserPaths->Insert(path, ipos);
        m_LibPathChanged = true;
        wxGetApp().InsertLibraryPath( path, ipos+1 );

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

377 378 379 380 381 382
        for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
        {
            m_DefaultLibraryPathslistBox->Append( libpaths[ii]);
        }
    }
    else
383 384 385
    {
        DisplayError( this, _( "Path already in use" ) );
    }
386 387 388 389 390 391 392 393

    wxGetApp().SaveLastVisitedLibraryPath( path );
}


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

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

398 399 400 401 402 403 404 405 406 407
    if ( ii >= 0 )
    {
        wxGetApp().RemoveLibraryPath( m_listUserPaths->GetStringSelection() );
        m_listUserPaths->Delete( ii );
        m_LibPathChanged = true;
    }

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

409 410
    for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
    {
411
        m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
412 413 414
    }
}

415

416 417
void DIALOG_PCBNEW_CONFIG_LIBS::OnBrowseModDocFile( wxCommandEvent& event )
{
418
    wxString FullFileName;
419 420 421 422 423
    wxString docpath, filename;

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

    wxFileDialog FilesDialog( this, _( "Footprint document file:" ), docpath,
424
                              wxEmptyString, PdfFileWildcard, wxFD_DEFAULT_STYLE  );
425 426 427 428 429 430 431 432 433

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

    FullFileName = FilesDialog.GetPath( );
    /* 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,
434 435
     * because it preserve use of default libraries paths, when the path is
     * a sub path of these default paths
436 437 438 439 440 441 442
     */
    wxFileName fn = FullFileName;
    wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );

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