You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
13 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dialog_cvpcb_config.cpp
  3. // Author: jean-pierre Charras
  4. // Licence: gpl
  5. /////////////////////////////////////////////////////////////////////////////
  6. #include "fctsys.h"
  7. #include <wx/tokenzr.h>
  8. #include "appl_wxstruct.h"
  9. #include "common.h"
  10. #include "confirm.h"
  11. #include "gestfich.h"
  12. #include "cvpcb.h"
  13. #include "cvstruct.h"
  14. #include "protos.h"
  15. #include "dialog_cvpcb_config.h"
  16. DIALOG_CVPCB_CONFIG::DIALOG_CVPCB_CONFIG( WinEDA_CvpcbFrame* parent ) :
  17. DIALOG_CVPCB_CONFIG_FBP( parent )
  18. {
  19. wxString title;
  20. wxFileName fn = parent->m_NetlistFileName;
  21. fn.SetExt( ProjectFileExtension );
  22. m_Parent = parent;
  23. m_Config = wxGetApp().m_EDA_CommonConfig;
  24. Init( );
  25. title = _( "Project file: " ) + fn.GetFullPath();
  26. SetTitle( title );
  27. if( GetSizer() )
  28. {
  29. GetSizer()->SetSizeHints( this );
  30. }
  31. }
  32. void DIALOG_CVPCB_CONFIG::Init()
  33. {
  34. wxString msg;
  35. SetFocus();
  36. m_LibListChanged = false;
  37. m_LibPathChanged = false;
  38. m_UserLibDirBufferImg = m_Parent->m_UserLibraryPath;
  39. m_ListLibr->InsertItems( m_Parent->m_ModuleLibNames, 0 );
  40. m_ListEquiv->InsertItems( m_Parent->m_AliasLibNames, 0 );
  41. m_TextHelpModulesFileName->SetValue( m_Parent->m_DocModulesFileName );
  42. // Load user libs paths:
  43. wxStringTokenizer Token( m_UserLibDirBufferImg, wxT( ";\n\r" ) );
  44. while( Token.HasMoreTokens() )
  45. {
  46. wxString path = Token.GetNextToken();
  47. if( wxFileName::DirExists( path ) )
  48. m_listUserPaths->Append( path );
  49. }
  50. // Display actual libraries paths:
  51. wxPathList libpaths = wxGetApp().GetLibraryPathList();
  52. for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
  53. {
  54. m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
  55. }
  56. // select the first path after the current path project
  57. if( libpaths.GetCount() > 1 )
  58. m_DefaultLibraryPathslistBox->Select( 1 );
  59. }
  60. void DIALOG_CVPCB_CONFIG::OnCancelClick( wxCommandEvent& event )
  61. {
  62. // Recreate the user lib path
  63. if( m_LibPathChanged )
  64. {
  65. for( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii++ )
  66. wxGetApp().RemoveLibraryPath( m_listUserPaths->GetString( ii ) );
  67. wxGetApp().InsertLibraryPath( m_Parent->m_UserLibraryPath, 1 );
  68. }
  69. EndModal( wxID_CANCEL );
  70. }
  71. void DIALOG_CVPCB_CONFIG::OnOkClick( wxCommandEvent& event )
  72. {
  73. m_Parent->m_DocModulesFileName = m_TextHelpModulesFileName->GetValue();
  74. // Recreate the user lib path
  75. if( m_LibPathChanged )
  76. {
  77. m_Parent->m_UserLibraryPath.Empty();
  78. for( unsigned ii = 0; ii < m_listUserPaths->GetCount(); ii++ )
  79. {
  80. if( ii > 0 )
  81. m_Parent->m_UserLibraryPath << wxT( ";" );
  82. m_Parent->m_UserLibraryPath << m_listUserPaths->GetString( ii );
  83. }
  84. }
  85. // Set new active library list if the lib list of if default path list
  86. // was modified
  87. if( m_LibListChanged || m_LibPathChanged )
  88. {
  89. // Recreate lib list
  90. m_Parent->m_ModuleLibNames.Clear();
  91. for( unsigned ii = 0; ii < m_ListLibr->GetCount(); ii++ )
  92. m_Parent->m_ModuleLibNames.Add( m_ListLibr->GetString( ii ) );
  93. // Recreate equ list
  94. m_Parent->m_AliasLibNames.Clear();
  95. for( unsigned ii = 0; ii < m_ListEquiv->GetCount(); ii++ )
  96. m_Parent->m_AliasLibNames.Add( m_ListEquiv->GetString( ii ) );
  97. LoadFootprintFiles( m_Parent->m_ModuleLibNames,
  98. m_Parent->m_footprints );
  99. m_Parent->BuildFOOTPRINTS_LISTBOX();
  100. }
  101. m_Parent->SaveProjectFile( m_Parent->m_NetlistFileName.GetFullPath() );
  102. EndModal( wxID_OK );
  103. }
  104. void DIALOG_CVPCB_CONFIG::OnCloseWindow( wxCloseEvent& event )
  105. {
  106. EndModal( 0 );
  107. }
  108. /********************************************************************/
  109. void DIALOG_CVPCB_CONFIG::OnButtonUpClick( wxCommandEvent& event )
  110. /********************************************************************/
  111. {
  112. wxListBox * list = m_ListLibr;
  113. if( (event.GetId() == ID_EQU_UP) || (event.GetId() == ID_EQU_DOWN) )
  114. {
  115. list = m_ListEquiv;
  116. }
  117. wxArrayInt selections;
  118. list->GetSelections(selections);
  119. if ( selections.GetCount() <= 0 ) // No selection.
  120. return;
  121. if( selections[0] == 0 ) // The first lib is selected. cannot move up it
  122. return;
  123. wxArrayString libnames = list->GetStrings();
  124. for( size_t ii = 0; ii < selections.GetCount(); ii++ )
  125. {
  126. int jj = selections[ii];
  127. EXCHG( libnames[jj], libnames[jj-1]);
  128. }
  129. list->Set(libnames);
  130. // Reselect previously selected names
  131. for( size_t ii = 0; ii < selections.GetCount(); ii++ )
  132. {
  133. int jj = selections[ii];
  134. list->SetSelection(jj-1);
  135. }
  136. m_LibListChanged = TRUE;
  137. }
  138. /*********************************************************************/
  139. void DIALOG_CVPCB_CONFIG::OnButtonDownClick( wxCommandEvent& event )
  140. /*********************************************************************/
  141. {
  142. wxListBox * list = m_ListLibr;
  143. if( (event.GetId() == ID_EQU_UP) || (event.GetId() == ID_EQU_DOWN) )
  144. {
  145. list = m_ListEquiv;
  146. }
  147. wxArrayInt selections;
  148. list->GetSelections(selections);
  149. if ( selections.GetCount() <= 0 ) // No selection.
  150. return;
  151. // The last lib is selected. cannot move down it
  152. if( selections.Last() == (int)(list->GetCount()-1) )
  153. return;
  154. wxArrayString libnames = list->GetStrings();
  155. for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
  156. {
  157. int jj = selections[ii];
  158. EXCHG( libnames[jj], libnames[jj+1]);
  159. }
  160. list->Set(libnames);
  161. // Reselect previously selected names
  162. for( size_t ii = 0; ii < selections.GetCount(); ii++ )
  163. {
  164. int jj = selections[ii];
  165. list->SetSelection(jj+1);
  166. }
  167. m_LibListChanged = TRUE;
  168. }
  169. /* Remove a library to the library list.
  170. * The real list (g_LibName_List) is not changed, so the change can be canceled
  171. */
  172. void DIALOG_CVPCB_CONFIG::OnRemoveLibClick( wxCommandEvent& event )
  173. {
  174. wxListBox * list = m_ListEquiv;
  175. if( event.GetId() == ID_REMOVE_LIB )
  176. list = m_ListLibr;
  177. wxArrayInt selections;
  178. list->GetSelections(selections);
  179. for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
  180. {
  181. list->Delete(selections[ii] );
  182. m_LibListChanged = TRUE;
  183. }
  184. }
  185. /* Insert or add a library to the library list:
  186. * The new library is put in list before (insert button) the selection,
  187. * or added (add button) to end of list
  188. * The real list (g_LibName_List) is not changed, so the change can be canceled
  189. */
  190. void DIALOG_CVPCB_CONFIG::OnAddOrInsertLibClick( wxCommandEvent& event )
  191. {
  192. int ii;
  193. wxString libfilename, wildcard;
  194. wxFileName fn;
  195. bool insert = false;
  196. if( (event.GetId() == ID_INSERT_EQU) || (event.GetId() == ID_INSERT_LIB) )
  197. insert = true;
  198. wildcard = FootprintAliasFileWildcard;
  199. wxListBox * list = m_ListEquiv;
  200. if( (event.GetId() == ID_ADD_LIB) || (event.GetId() == ID_INSERT_LIB) )
  201. {
  202. list = m_ListLibr;
  203. wildcard = ModuleFileWildcard;
  204. }
  205. wxArrayInt selections;
  206. list->GetSelections(selections);
  207. ii = selections.GetCount();
  208. if( ii > 0 )
  209. ii = selections[0];
  210. else
  211. ii = 0;
  212. wxString libpath;
  213. libpath = m_DefaultLibraryPathslistBox->GetStringSelection();
  214. if( libpath.IsEmpty() )
  215. libpath = wxGetApp().ReturnLastVisitedLibraryPath();
  216. wxFileDialog FilesDialog( this, _( "Footprint library files:" ), libpath,
  217. wxEmptyString, wildcard,
  218. wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );
  219. if( FilesDialog.ShowModal() != wxID_OK )
  220. return;
  221. wxArrayString Filenames;
  222. FilesDialog.GetPaths( Filenames );
  223. for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
  224. {
  225. fn = Filenames[jj];
  226. if( jj == 0 )
  227. wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
  228. /* If the library path is already in the library search paths
  229. * list, just add the library name to the list. Otherwise, add
  230. * the library name with the full or relative path.
  231. * the relative path, when possible is preferable,
  232. * because it preserve use of default libraries paths, when the path
  233. * is a sub path of these default paths
  234. */
  235. libfilename = wxGetApp().ReturnFilenameWithRelativePathInLibPath( fn.GetFullPath() );
  236. // Remove extension:
  237. fn = libfilename;
  238. fn.SetExt(wxEmptyString);
  239. libfilename = fn.GetFullPath();
  240. // Add or insert new library name, if not already in list
  241. if( list->FindString( libfilename, fn.IsCaseSensitive() ) == wxNOT_FOUND )
  242. {
  243. m_LibListChanged = TRUE;
  244. if( ! insert )
  245. list->Append( libfilename );
  246. else
  247. list->Insert( libfilename, ii++ );
  248. }
  249. else
  250. {
  251. wxString msg = wxT( "<" ) + libfilename + wxT( "> : " ) +
  252. _( "Library already in use" );
  253. DisplayError( this, msg );
  254. }
  255. }
  256. }
  257. void DIALOG_CVPCB_CONFIG::OnAddOrInsertPath( wxCommandEvent& event )
  258. {
  259. wxString path = wxGetApp().ReturnLastVisitedLibraryPath();
  260. bool select = EDA_DirectorySelector( _( "Default Path for Libraries" ),
  261. path,
  262. wxDD_DEFAULT_STYLE,
  263. this,
  264. wxDefaultPosition );
  265. if( !select )
  266. return;
  267. if( !wxFileName::DirExists( path ) ) // Should not occurs
  268. return;
  269. // Add or insert path if not already in list
  270. if( m_listUserPaths->FindString( path ) == wxNOT_FOUND )
  271. {
  272. int ipos = m_listUserPaths->GetCount();
  273. if( event.GetId() == ID_INSERT_PATH )
  274. {
  275. if( ipos )
  276. ipos--;
  277. int jj = m_listUserPaths->GetSelection();
  278. if( jj >= 0 )
  279. ipos = jj;
  280. }
  281. // Ask the user if this is a relative path
  282. int diag = wxMessageBox(
  283. _( "Use a relative path?" ),
  284. _( "Path type" ),
  285. wxYES_NO | wxICON_QUESTION, this );
  286. if( diag == wxYES )
  287. { // Make it relative
  288. wxFileName fn = path;
  289. fn.MakeRelativeTo( wxT(".") );
  290. path = fn.GetPathWithSep() + fn.GetFullName();
  291. }
  292. m_listUserPaths->Insert( path, ipos );
  293. m_LibPathChanged = true;
  294. wxGetApp().InsertLibraryPath( path, ipos + 1 );
  295. // Display actual libraries paths:
  296. wxPathList libpaths = wxGetApp().GetLibraryPathList();
  297. m_DefaultLibraryPathslistBox->Clear();
  298. for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
  299. {
  300. m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
  301. }
  302. }
  303. else
  304. DisplayError( this, _( "Path already in use" ) );
  305. wxGetApp().SaveLastVisitedLibraryPath( path );
  306. }
  307. void DIALOG_CVPCB_CONFIG::OnRemoveUserPath( wxCommandEvent& event )
  308. {
  309. int ii = m_listUserPaths->GetSelection();
  310. if( ii < 0 )
  311. ii = m_listUserPaths->GetCount() - 1;
  312. if( ii >= 0 )
  313. {
  314. wxGetApp().RemoveLibraryPath( m_listUserPaths->GetStringSelection() );
  315. m_listUserPaths->Delete( ii );
  316. m_LibPathChanged = true;
  317. }
  318. // Display actual libraries paths:
  319. wxPathList libpaths = wxGetApp().GetLibraryPathList();
  320. m_DefaultLibraryPathslistBox->Clear();
  321. for( unsigned ii = 0; ii < libpaths.GetCount(); ii++ )
  322. {
  323. m_DefaultLibraryPathslistBox->Append( libpaths[ii] );
  324. }
  325. }
  326. void DIALOG_CVPCB_CONFIG::OnBrowseModDocFile( wxCommandEvent& event )
  327. {
  328. wxString FullFileName;
  329. wxString docpath, filename;
  330. docpath = wxGetApp().ReturnLastVisitedLibraryPath( wxT( "doc" ) );
  331. wxFileDialog FilesDialog( this, _( "Footprint document file:" ), docpath,
  332. wxEmptyString, PdfFileWildcard,
  333. wxFD_DEFAULT_STYLE | wxFD_FILE_MUST_EXIST );
  334. if( FilesDialog.ShowModal() != wxID_OK )
  335. return;
  336. FullFileName = FilesDialog.GetPath();
  337. /* If the path is already in the library search paths
  338. * list, just add the library name to the list. Otherwise, add
  339. * the library name with the full or relative path.
  340. * the relative path, when possible is preferable,
  341. * because it preserve use of default libraries paths, when the path is
  342. * a sub path of these default paths
  343. */
  344. wxFileName fn = FullFileName;
  345. wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
  346. filename = wxGetApp().ReturnFilenameWithRelativePathInLibPath( FullFileName );
  347. m_TextHelpModulesFileName->SetValue( filename );
  348. }