summaryrefslogtreecommitdiff
path: root/cvpcb/dialogs/dialog_config_equfiles.cpp
blob: 3ae03fd68043cb1dbd8d042203eec6679bd25dea (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
 * @file dialog_config_equfiles.cpp
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 1992-2015 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
 */

#include <fctsys.h>
#include <pgm_base.h>
#include <common.h>
#include <confirm.h>
#include <gestfich.h>
#include <id.h>
#include <project.h>            // For PROJECT_VAR_NAME definition
#include <fp_lib_table.h>       // For KISYSMOD definition

#include <cvpcb.h>
#include <cvpcb_mainframe.h>

#include <dialog_config_equfiles.h>
#include <wildcards_and_files_ext.h>


DIALOG_CONFIG_EQUFILES::DIALOG_CONFIG_EQUFILES( CVPCB_MAINFRAME* aParent ) :
    DIALOG_CONFIG_EQUFILES_BASE( aParent )
{
    m_Parent = aParent;
    m_Config = Pgm().CommonSettings();

    PROJECT&    prj = Prj();
    SetTitle( wxString::Format( _( "Project file: '%s'" ), GetChars( prj.GetProjectFullName() ) ) );

    Init( );

    GetSizer()->SetSizeHints( this );
    Center();
}

void CVPCB_MAINFRAME::OnEditEquFilesList( wxCommandEvent& aEvent )
{
    DIALOG_CONFIG_EQUFILES dlg( this );

    dlg.ShowModal();
}


void DIALOG_CONFIG_EQUFILES::Init()
{
    m_sdbSizerOK->SetDefault();
    m_ListChanged = false;
    m_ListEquiv->InsertItems( m_Parent->m_EquFilesNames, 0 );

    if( getEnvVarCount() < 2 )
        m_gridEnvVars->AppendRows(2 - getEnvVarCount() );

    wxString evValue;
    int row = 0;

    m_gridEnvVars->SetCellValue( row++, 0, PROJECT_VAR_NAME );
    m_gridEnvVars->SetCellValue( row, 0, FP_LIB_TABLE::GlobalPathEnvVariableName() );

    for( row = 0; row < getEnvVarCount(); row++ )
    {
        if( wxGetEnv( m_gridEnvVars->GetCellValue( row, 0 ), &evValue ) )
            m_gridEnvVars->SetCellValue( row, 1, evValue );
    }

    m_gridEnvVars->AutoSizeColumns();

}

void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
{
    wxString    editorname = Pgm().GetEditorName();

    if( editorname.IsEmpty() )
    {
        wxMessageBox( _( "No editor defined in Kicad. Please chose it" ) );
        return;
    }

    wxArrayInt selections;
    m_ListEquiv->GetSelections( selections );

    wxString fullFileNames, tmp;

    for( unsigned ii = 0; ii < selections.GetCount(); ii++ )
    {
        tmp = m_ListEquiv->GetString( selections[ii] );
        fullFileNames << wxT( " \"" ) << wxExpandEnvVars( tmp ) << wxT( "\"" );
        m_ListChanged = true;
    }

    ExecuteFile( this, editorname, fullFileNames );
}


void DIALOG_CONFIG_EQUFILES::OnCancelClick( wxCommandEvent& event )
{
    EndModal( wxID_CANCEL );
}


void DIALOG_CONFIG_EQUFILES::OnOkClick( wxCommandEvent& event )
{
    // Save new equ file list if the files list was modified
    if( m_ListChanged  )
    {
        // Recreate equ list
        m_Parent->m_EquFilesNames.Clear();

        for( unsigned ii = 0; ii < m_ListEquiv->GetCount(); ii++ )
            m_Parent->m_EquFilesNames.Add( m_ListEquiv->GetString( ii ) );

        wxCommandEvent evt( ID_SAVE_PROJECT );
        m_Parent->SaveProjectFile( evt );
    }

    EndModal( wxID_OK );
}


void DIALOG_CONFIG_EQUFILES::OnCloseWindow( wxCloseEvent& event )
{
    EndModal( wxID_CANCEL );
}


/********************************************************************/
void DIALOG_CONFIG_EQUFILES::OnButtonMoveUp( wxCommandEvent& event )
/********************************************************************/
{
    wxArrayInt selections;

    m_ListEquiv->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_ListEquiv->GetStrings();

    for( size_t ii = 0; ii < selections.GetCount(); ii++ )
    {
        int jj = selections[ii];
        std::swap( libnames[jj],  libnames[jj-1] );
    }

    m_ListEquiv->Set( libnames );

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

    m_ListChanged = true;
}


/*********************************************************************/
void DIALOG_CONFIG_EQUFILES::OnButtonMoveDown( wxCommandEvent& event )
/*********************************************************************/
{
    wxArrayInt selections;
    m_ListEquiv->GetSelections( selections );

    if ( selections.GetCount() <= 0 )   // No selection.
        return;

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

    wxArrayString libnames = m_ListEquiv->GetStrings();

    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
        int jj = selections[ii];
        std::swap( libnames[jj],  libnames[jj+1]);
    }

    m_ListEquiv->Set( libnames );

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

    m_ListChanged = true;
}


/* Remove a library to the library list.
 * The real list (g_LibName_List) is not changed, so the change can be canceled
 */
void DIALOG_CONFIG_EQUFILES::OnRemoveFiles( wxCommandEvent& event )
{
    wxArrayInt selections;
    m_ListEquiv->GetSelections( selections );

    std::sort( selections.begin(), selections.end() );

    for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
    {
        m_ListEquiv->Delete(selections[ii] );
        m_ListChanged = true;
    }
}


/* 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
 */
void DIALOG_CONFIG_EQUFILES::OnAddFiles( wxCommandEvent& event )
{
    wxString   equFilename, wildcard;
    wxFileName fn;

    wildcard = EquFilesWildcard;
    wxListBox* list = m_ListEquiv;

    // Get a default path to open the file dialog:
    wxString libpath;
    wxArrayInt selectedRows	= m_gridEnvVars->GetSelectedRows();

    int row = selectedRows.GetCount() ? selectedRows[0] :
                m_gridEnvVars->GetGridCursorRow();

    libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );

    wxFileDialog FilesDialog( this, _( "Equ files:" ), libpath,
                              wxEmptyString, wildcard,
                              wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );

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

    wxArrayString Filenames;
    FilesDialog.GetPaths( Filenames );

    for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
    {
        fn = Filenames[jj];
        equFilename.Empty();

        if( isPathRelativeAllowed() ) // try to use relative path
        {
            for( row = 0; row < getEnvVarCount(); row++ )
            {
                libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );

                if( fn.MakeRelativeTo( libpath ) )
                {
                    equFilename.Printf( wxT("${%s}%c%s"),
                                        GetChars( m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 0 ) ) ),
                                        fn.GetPathSeparator(),
                                        GetChars( fn.GetFullPath() ) );
                    break;
                }
            }
        }

        if( equFilename.IsEmpty() )
            equFilename = Filenames[jj];

        // Add or insert new library name, if not already in list
        if( list->FindString( equFilename, fn.IsCaseSensitive() ) == wxNOT_FOUND )
        {
            m_ListChanged = true;
            equFilename.Replace( wxT("\\"), wxT("/") );     // Use unix separators only.
            list->Append( equFilename );
        }
        else
        {
            wxString msg;
            msg.Printf( _( "File '%s' already exists in list" ), equFilename.GetData() );
            DisplayError( this, msg );
        }
    }
}