dialog_pcbnew_config_libs_and_paths.cpp 11.5 KB
Newer Older
1 2 3 4 5 6 7
/////////////////////////////////////////////////////////////////////////////
// Name:        dialog_pcbnew_config_libs_and_paths.cpp
// Author:      jean-pierre Charras
// Created:     2009 apr 18
// Licence:     GPL
/////////////////////////////////////////////////////////////////////////////

8
/* Handle the Pcbnew library config (library list, and default lib path)
9 10
 */

11
#include <fctsys.h>
12
#include <wx/tokenzr.h>
13 14 15 16 17 18 19 20 21
#include <appl_wxstruct.h>
#include <confirm.h>
#include <gestfich.h>
#include <pcbnew.h>
#include <macros.h>
#include <wxPcbStruct.h>
#include <pcbcommon.h>

#include <dialog_pcbnew_config_libs_and_paths.h>
22 23


24
void PCB_EDIT_FRAME::InstallConfigFrame( )
25 26 27 28 29 30
{
    DIALOG_PCBNEW_CONFIG_LIBS dialog( this );
    dialog.ShowModal();
}


31
DIALOG_PCBNEW_CONFIG_LIBS::DIALOG_PCBNEW_CONFIG_LIBS( PCB_EDIT_FRAME* parent ):
32 33
    DIALOG_PCBNEW_CONFIG_LIBS_FBP(parent)
{
34
    m_Config = wxGetApp().GetCommonSettings();
35 36 37

    Init( );

38
    wxString title = _( "from " ) + wxGetApp().GetCurrentOptionFile();
39 40
    SetTitle( title );

41 42
    m_sdbSizer1OK->SetDefault();

43
    if( GetSizer() )
44
        GetSizer()->SetSizeHints( this );
45 46
}

47

48 49 50 51 52 53
void DIALOG_PCBNEW_CONFIG_LIBS::Init()
{
    SetFocus();

    m_LibListChanged = false;
    m_LibPathChanged = false;
54
    m_UserLibDirBufferImg = g_UserLibDirBuffer;  // Save the original lib path
55

56
    m_ListLibr->InsertItems( g_LibraryNames, 0 );
57 58 59 60 61 62 63

    // 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" ) );
64

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

69 70 71 72 73 74 75 76 77 78 79
        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]);
    }

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


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

94 95
        wxGetApp().InsertLibraryPath( g_UserLibDirBuffer, 1);
    }
96 97

    EndModal( wxID_CANCEL );
98 99 100 101 102
}


void DIALOG_PCBNEW_CONFIG_LIBS::OnOkClick( wxCommandEvent& event )
{
103
     m_Config->Write( wxT( "module_doc_file" ), m_TextHelpModulesFileName->GetValue() );
104 105 106 107 108

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

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

115 116 117 118
            g_UserLibDirBuffer << m_listUserPaths->GetString(ii);
        }
    }

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

125
        for ( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii ++ )
126
            g_LibraryNames.Add( m_ListLibr->GetString(ii) );
127
    }
128

129
    GetParent()->SaveProjectSettings();
130
    EndModal( wxID_OK );
131 132
}

133

134 135
void DIALOG_PCBNEW_CONFIG_LIBS::OnCloseWindow( wxCloseEvent& event )
{
136
    EndModal( wxID_CANCEL );
137 138 139
}


140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
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]);
    }
158

159 160 161 162 163 164 165 166 167
    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);
    }

168
    m_LibListChanged = true;
169 170 171 172 173 174 175 176
}


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

    m_ListLibr->GetSelections(selections);
177

178 179 180 181 182 183 184 185 186 187 188 189
    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];
190
        EXCHG( libnames[jj], libnames[jj+1] );
191
    }
192

193 194 195 196 197 198 199 200
    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);
    }
201

202
    m_LibListChanged = true;
203 204
}

205 206

/* Remove a library to the library list.
207
 * The real list (g_LibName_List) is not changed, so the change can be canceled
208
 */
209
void DIALOG_PCBNEW_CONFIG_LIBS::OnRemoveLibClick( wxCommandEvent& event )
210
{
211
    wxArrayInt selections;
212

213 214
    m_ListLibr->GetSelections( selections );

215 216
    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
217
        m_ListLibr->Delete( selections[ii] );
218
        m_LibListChanged = true;
219
    }
220 221 222 223 224 225
}


/* 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
226
 * The real list (g_LibName_List) is not changed, so the change can be canceled
227
 */
228
void DIALOG_PCBNEW_CONFIG_LIBS::OnAddOrInsertLibClick( wxCommandEvent& event )
229
{
230
    int        ii = 0;
231 232 233
    wxString   libfilename;
    wxFileName fn;

234 235 236 237
    wxArrayInt selections;
    m_ListLibr->GetSelections(selections);

    ii = selections.GetCount();
238

239 240 241
    if( ii > 0 )
        ii = selections[0];
    else
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        ii = 0;

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

    wxFileDialog FilesDialog( this, _( "Footprint library files:" ), libpath,
                              wxEmptyString, g_FootprintLibFileWildcard,
                              wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );

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

    wxArrayString Filenames;
    FilesDialog.GetPaths( Filenames );

259
    for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
260 261
    {
        fn = Filenames[jj];
262

263 264 265 266 267 268 269
        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,
270 271 272
         * because it preserve use of default libraries paths, when the
         * path is a sub path of these default paths
         */
273
        libfilename = wxGetApp().ReturnFilenameWithRelativePathInLibPath( fn.GetFullPath() );
274

275 276
        // Remove extension:
        fn = libfilename;
277
        fn.SetExt( wxEmptyString );
278
        libfilename = fn.GetFullPath();
279 280 281 282

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

285 286 287 288 289 290 291 292
            if( event.GetId() == ID_ADD_LIB )
                m_ListLibr->Append( libfilename );
            else
                m_ListLibr->Insert( libfilename, ii++ );
        }
        else
        {
            wxString msg = wxT( "<" ) + libfilename + wxT( "> : " ) +
293
                           _( "Library already in use" );
294 295 296 297 298 299 300 301 302 303
            DisplayError( this, msg );
        }
    }
}


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

304 305 306
    bool     select = EDA_DirectorySelector( _( "Default Path for Libraries" ),
                                             path, wxDD_DEFAULT_STYLE,
                                             this, wxDefaultPosition );
307 308 309 310 311 312 313 314 315 316 317

    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();
318

319 320
        if ( event.GetId() == wxID_INSERT_PATH )
        {
321 322 323
            if ( ipos  )
                ipos--;

324
            int jj = m_listUserPaths->GetSelection();
325

326 327 328
            if ( jj >= 0 )
                ipos = jj;
        }
charras's avatar
charras committed
329 330

        // Ask the user if this is a relative path
331 332 333
        int diag = wxMessageBox( _( "Use a relative path?" ),
                                 _( "Path type" ),
                                 wxYES_NO | wxICON_QUESTION, this );
charras's avatar
charras committed
334 335 336 337

        if( diag == wxYES )
        {   // Make it relative
            wxFileName fn = path;
338
            fn.MakeRelativeTo( wxT( "." ) );
charras's avatar
charras committed
339 340 341
            path = fn.GetPathWithSep() + fn.GetFullName();
        }

342 343 344 345 346 347 348
        m_listUserPaths->Insert(path, ipos);
        m_LibPathChanged = true;
        wxGetApp().InsertLibraryPath( path, ipos+1 );

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

350 351 352 353 354 355
        for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
        {
            m_DefaultLibraryPathslistBox->Append( libpaths[ii]);
        }
    }
    else
356 357 358
    {
        DisplayError( this, _( "Path already in use" ) );
    }
359 360 361 362 363 364 365 366

    wxGetApp().SaveLastVisitedLibraryPath( path );
}


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

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

371 372 373 374 375 376 377 378 379 380
    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();
381

382 383
    for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
    {
384
        m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
385 386 387
    }
}

388

389 390
void DIALOG_PCBNEW_CONFIG_LIBS::OnBrowseModDocFile( wxCommandEvent& event )
{
391
    wxString FullFileName;
392 393 394 395 396
    wxString docpath, filename;

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

    wxFileDialog FilesDialog( this, _( "Footprint document file:" ), docpath,
397
                              wxEmptyString, PdfFileWildcard, wxFD_DEFAULT_STYLE  );
398 399 400 401 402 403 404 405 406

    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,
407 408
     * because it preserve use of default libraries paths, when the path is
     * a sub path of these default paths
409 410 411 412 413 414 415
     */
    wxFileName fn = FullFileName;
    wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );

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