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.

523 lines
16 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /**************************************************/
  2. /* projet_config : routines de trace du cartouche */
  3. /**************************************************/
  4. #include "fctsys.h"
  5. #include "gr_basic.h"
  6. #include "common.h"
  7. #define CONFIG_VERSION 1
  8. #define FORCE_LOCAL_CONFIG TRUE
  9. /*********************************************************************/
  10. static bool ReCreatePrjConfig( const wxString& local_config_filename,
  11. const wxString& GroupName, bool ForceUseLocalConfig )
  12. /*********************************************************************/
  13. /* Cree ou recree la configuration locale de kicad (filename.pro)
  14. * initialise:
  15. * g_Prj_Config
  16. * g_Prj_Config_LocalFilename
  17. * g_Prj_Default_Config_FullFilename
  18. * return:
  19. * TRUE si config locale
  20. * FALSE si default config
  21. */
  22. {
  23. // free old config
  24. if( g_Prj_Config )
  25. delete g_Prj_Config;
  26. g_Prj_Config = NULL;
  27. // Init local Config filename
  28. if( local_config_filename.IsEmpty() )
  29. g_Prj_Config_LocalFilename = wxT( "kicad" );
  30. else
  31. g_Prj_Config_LocalFilename = local_config_filename;
  32. ChangeFileNameExt( g_Prj_Config_LocalFilename, g_Prj_Config_Filename_ext );
  33. // Init local config filename
  34. if( ForceUseLocalConfig || wxFileExists( g_Prj_Config_LocalFilename ) )
  35. {
  36. g_Prj_Default_Config_FullFilename.Empty();
  37. g_Prj_Config = new wxFileConfig( wxEmptyString, wxEmptyString,
  38. g_Prj_Config_LocalFilename, wxEmptyString,
  39. wxCONFIG_USE_RELATIVE_PATH );
  40. g_Prj_Config->DontCreateOnDemand();
  41. if( ForceUseLocalConfig )
  42. return TRUE;
  43. // Test de la bonne version du fichier (ou groupe) de configuration
  44. int version = -1, def_version = 0;
  45. g_Prj_Config->SetPath( GroupName );
  46. version = g_Prj_Config->Read( wxT( "version" ), def_version );
  47. g_Prj_Config->SetPath( UNIX_STRING_DIR_SEP );
  48. if( version > 0 )
  49. return TRUE;
  50. else
  51. delete g_Prj_Config; // Version incorrecte
  52. }
  53. // Fichier local non trouve ou invalide
  54. g_Prj_Config_LocalFilename.Empty();
  55. g_Prj_Default_Config_FullFilename =
  56. ReturnKicadDatasPath() +
  57. wxT( "template/kicad" ) +
  58. g_Prj_Config_Filename_ext;
  59. // Recreate new config
  60. g_Prj_Config = new wxFileConfig( wxEmptyString, wxEmptyString,
  61. wxEmptyString, g_Prj_Default_Config_FullFilename,
  62. wxCONFIG_USE_RELATIVE_PATH );
  63. g_Prj_Config->DontCreateOnDemand();
  64. return FALSE;
  65. }
  66. /***************************************************************************************/
  67. void WinEDA_App::WriteProjectConfig( const wxString& local_config_filename,
  68. const wxString& GroupName, PARAM_CFG_BASE** List )
  69. /***************************************************************************************/
  70. /* enregistrement de la config "projet"*/
  71. {
  72. const PARAM_CFG_BASE* pt_cfg;
  73. wxString msg;
  74. ReCreatePrjConfig( local_config_filename, GroupName,
  75. FORCE_LOCAL_CONFIG );
  76. /* Write date ( surtout pour eviter bug de wxFileConfig
  77. * qui se trompe de rubrique si declaration [xx] en premiere ligne
  78. * (en fait si groupe vide) */
  79. g_Prj_Config->SetPath( UNIX_STRING_DIR_SEP );
  80. msg = DateAndTime();
  81. g_Prj_Config->Write( wxT( "update" ), msg );
  82. msg = GetAppName();
  83. g_Prj_Config->Write( wxT( "last_client" ), msg );
  84. /* ecriture de la configuration */
  85. g_Prj_Config->DeleteGroup( GroupName ); // Erase all datas
  86. g_Prj_Config->Flush();
  87. g_Prj_Config->SetPath( GroupName );
  88. g_Prj_Config->Write( wxT( "version" ), CONFIG_VERSION );
  89. g_Prj_Config->SetPath( UNIX_STRING_DIR_SEP );
  90. for( ; *List != NULL; List++ )
  91. {
  92. pt_cfg = *List;
  93. if( pt_cfg->m_Group )
  94. g_Prj_Config->SetPath( pt_cfg->m_Group );
  95. else
  96. g_Prj_Config->SetPath( GroupName );
  97. switch( pt_cfg->m_Type )
  98. {
  99. case PARAM_INT:
  100. #undef PTCFG
  101. #define PTCFG ( (PARAM_CFG_INT*) pt_cfg )
  102. if( PTCFG->m_Pt_param == NULL )
  103. break;
  104. if( pt_cfg->m_Setup )
  105. m_EDA_Config->Write( pt_cfg->m_Ident, *PTCFG->m_Pt_param );
  106. else
  107. g_Prj_Config->Write( pt_cfg->m_Ident, *PTCFG->m_Pt_param );
  108. break;
  109. case PARAM_SETCOLOR:
  110. #undef PTCFG
  111. #define PTCFG ( (PARAM_CFG_SETCOLOR*) pt_cfg )
  112. if( PTCFG->m_Pt_param == NULL )
  113. break;
  114. if( pt_cfg->m_Setup )
  115. m_EDA_Config->Write( pt_cfg->m_Ident, *PTCFG->m_Pt_param );
  116. else
  117. g_Prj_Config->Write( pt_cfg->m_Ident, *PTCFG->m_Pt_param );
  118. break;
  119. case PARAM_DOUBLE:
  120. #undef PTCFG
  121. #define PTCFG ( (PARAM_CFG_DOUBLE*) pt_cfg )
  122. if( PTCFG->m_Pt_param == NULL )
  123. break;
  124. if( pt_cfg->m_Setup )
  125. m_EDA_Config->Write( pt_cfg->m_Ident, *PTCFG->m_Pt_param );
  126. else
  127. g_Prj_Config->Write( pt_cfg->m_Ident, *PTCFG->m_Pt_param );
  128. break;
  129. case PARAM_BOOL:
  130. #undef PTCFG
  131. #define PTCFG ( (PARAM_CFG_BOOL*) pt_cfg )
  132. if( PTCFG->m_Pt_param == NULL )
  133. break;
  134. if( pt_cfg->m_Setup )
  135. m_EDA_Config->Write( pt_cfg->m_Ident, (int) *PTCFG->m_Pt_param );
  136. else
  137. g_Prj_Config->Write( pt_cfg->m_Ident, (int) *PTCFG->m_Pt_param );
  138. break;
  139. case PARAM_WXSTRING:
  140. #undef PTCFG
  141. #define PTCFG ( (PARAM_CFG_WXSTRING*) pt_cfg )
  142. if( PTCFG->m_Pt_param == NULL )
  143. break;
  144. if( pt_cfg->m_Setup )
  145. m_EDA_Config->Write( pt_cfg->m_Ident, *PTCFG->m_Pt_param );
  146. else
  147. g_Prj_Config->Write( pt_cfg->m_Ident, *PTCFG->m_Pt_param );
  148. break;
  149. case PARAM_LIBNAME_LIST:
  150. {
  151. #undef PTCFG
  152. #define PTCFG ( (PARAM_CFG_LIBNAME_LIST*) pt_cfg )
  153. if( PTCFG->m_Pt_param == NULL )
  154. break;
  155. wxArrayString* libname_list = PTCFG->m_Pt_param;
  156. if( libname_list == NULL )
  157. break;
  158. unsigned indexlib = 0;
  159. wxString cle_config;
  160. for( ; indexlib < libname_list->GetCount(); indexlib++ )
  161. {
  162. cle_config = pt_cfg->m_Ident;
  163. // We use indexlib+1 because first lib name is LibName1
  164. cle_config << (indexlib + 1);
  165. g_Prj_Config->Write( cle_config, libname_list->Item( indexlib ) );
  166. }
  167. break;
  168. }
  169. case PARAM_COMMAND_ERASE: // Erase all datas
  170. if( pt_cfg->m_Ident )
  171. {
  172. m_EDA_Config->DeleteGroup( pt_cfg->m_Ident );
  173. g_Prj_Config->DeleteGroup( pt_cfg->m_Ident );
  174. }
  175. break;
  176. }
  177. }
  178. g_Prj_Config->SetPath( UNIX_STRING_DIR_SEP );
  179. delete g_Prj_Config;
  180. g_Prj_Config = NULL;
  181. }
  182. /***************************************************************************************/
  183. bool WinEDA_App::ReadProjectConfig( const wxString& local_config_filename,
  184. const wxString& GroupName, PARAM_CFG_BASE** List,
  185. bool Load_Only_if_New )
  186. /***************************************************************************************/
  187. /* Lecture de la config "projet"
  188. *** si Load_Only_if_New == TRUE, elle n'est lue que si elle
  189. *** est differente de la config actuelle (dates differentes)
  190. *
  191. * return:
  192. * TRUE si lue.
  193. * Met a jour en plus:
  194. * g_EDA_Appl->m_CurrentOptionFileDateAndTime
  195. * g_EDA_Appl->m_CurrentOptionFile
  196. */
  197. {
  198. const PARAM_CFG_BASE* pt_cfg;
  199. wxString timestamp;
  200. if( List == NULL )
  201. return FALSE;
  202. ReCreatePrjConfig( local_config_filename, GroupName, FALSE );
  203. g_Prj_Config->SetPath( UNIX_STRING_DIR_SEP );
  204. timestamp = g_Prj_Config->Read( wxT( "update" ) );
  205. if( Load_Only_if_New && ( !timestamp.IsEmpty() )
  206. && (timestamp == g_EDA_Appl->m_CurrentOptionFileDateAndTime) )
  207. {
  208. return FALSE;
  209. }
  210. g_EDA_Appl->m_CurrentOptionFileDateAndTime = timestamp;
  211. if( !g_Prj_Default_Config_FullFilename.IsEmpty() )
  212. g_EDA_Appl->m_CurrentOptionFile = g_Prj_Default_Config_FullFilename;
  213. else
  214. {
  215. if( wxPathOnly( g_Prj_Config_LocalFilename ).IsEmpty() )
  216. g_EDA_Appl->m_CurrentOptionFile =
  217. wxGetCwd() + STRING_DIR_SEP + g_Prj_Config_LocalFilename;
  218. else
  219. g_EDA_Appl->m_CurrentOptionFile = g_Prj_Config_LocalFilename;
  220. }
  221. for( ; *List != NULL; List++ )
  222. {
  223. pt_cfg = *List;
  224. if( pt_cfg->m_Group )
  225. g_Prj_Config->SetPath( pt_cfg->m_Group );
  226. else
  227. g_Prj_Config->SetPath( GroupName );
  228. switch( pt_cfg->m_Type )
  229. {
  230. case PARAM_INT:
  231. {
  232. #undef PTCFG
  233. #define PTCFG ( (PARAM_CFG_INT*) pt_cfg )
  234. int itmp;
  235. if( pt_cfg->m_Setup )
  236. itmp = m_EDA_Config->Read( pt_cfg->m_Ident, PTCFG->m_Default );
  237. else
  238. itmp = g_Prj_Config->Read( pt_cfg->m_Ident, PTCFG->m_Default );
  239. if( (itmp < PTCFG->m_Min) || (itmp > PTCFG->m_Max) )
  240. itmp = PTCFG->m_Default;
  241. *PTCFG->m_Pt_param = itmp;
  242. break;
  243. }
  244. case PARAM_SETCOLOR:
  245. {
  246. #undef PTCFG
  247. #define PTCFG ( (PARAM_CFG_SETCOLOR*) pt_cfg )
  248. int itmp;
  249. if( pt_cfg->m_Setup )
  250. itmp = m_EDA_Config->Read( pt_cfg->m_Ident, PTCFG->m_Default );
  251. else
  252. itmp = g_Prj_Config->Read( pt_cfg->m_Ident, PTCFG->m_Default );
  253. if( (itmp < 0) || (itmp > MAX_COLOR) )
  254. itmp = PTCFG->m_Default;
  255. *PTCFG->m_Pt_param = itmp;
  256. break;
  257. }
  258. case PARAM_DOUBLE:
  259. {
  260. #undef PTCFG
  261. #define PTCFG ( (PARAM_CFG_DOUBLE*) pt_cfg )
  262. double ftmp = 0; wxString msg;
  263. if( pt_cfg->m_Setup )
  264. msg = m_EDA_Config->Read( pt_cfg->m_Ident, wxT( "" ) );
  265. else
  266. msg = g_Prj_Config->Read( pt_cfg->m_Ident, wxT( "" ) );
  267. if( msg.IsEmpty() )
  268. ftmp = PTCFG->m_Default;
  269. else
  270. {
  271. msg.ToDouble( &ftmp );
  272. if( (ftmp < PTCFG->m_Min) || (ftmp > PTCFG->m_Max) )
  273. ftmp = PTCFG->m_Default;
  274. }
  275. *PTCFG->m_Pt_param = ftmp;
  276. break;
  277. }
  278. case PARAM_BOOL:
  279. {
  280. #undef PTCFG
  281. #define PTCFG ( (PARAM_CFG_BOOL*) pt_cfg )
  282. int itmp;
  283. if( pt_cfg->m_Setup )
  284. itmp = m_EDA_Config->Read( pt_cfg->m_Ident, PTCFG->m_Default );
  285. else
  286. itmp = g_Prj_Config->Read( pt_cfg->m_Ident, PTCFG->m_Default );
  287. *PTCFG->m_Pt_param = itmp ? TRUE : FALSE;
  288. break;
  289. }
  290. case PARAM_WXSTRING:
  291. {
  292. #undef PTCFG
  293. #define PTCFG ( (PARAM_CFG_WXSTRING*) pt_cfg )
  294. if( PTCFG->m_Pt_param == NULL )
  295. break;
  296. if( pt_cfg->m_Setup )
  297. *PTCFG->m_Pt_param = m_EDA_Config->Read( pt_cfg->m_Ident );
  298. else
  299. *PTCFG->m_Pt_param = g_Prj_Config->Read( pt_cfg->m_Ident );
  300. break;
  301. }
  302. case PARAM_LIBNAME_LIST:
  303. {
  304. #undef PTCFG
  305. #define PTCFG ( (PARAM_CFG_LIBNAME_LIST*) pt_cfg )
  306. int indexlib = 1; // We start indexlib to 1 because first lib name is LibName1
  307. wxString libname, id_lib;
  308. wxArrayString* libname_list = PTCFG->m_Pt_param;
  309. while( 1 )
  310. {
  311. id_lib = pt_cfg->m_Ident; id_lib << indexlib; indexlib++;
  312. libname = g_Prj_Config->Read( id_lib, wxT( "" ) );
  313. if( libname.IsEmpty() )
  314. break;
  315. libname_list->Add( libname );
  316. }
  317. break;
  318. }
  319. case PARAM_COMMAND_ERASE:
  320. break;
  321. }
  322. }
  323. delete g_Prj_Config;
  324. g_Prj_Config = NULL;
  325. return TRUE;
  326. }
  327. /**************************************************************/
  328. /* Constructeurs des descripteurs de structs de configuration */
  329. /**************************************************************/
  330. PARAM_CFG_BASE::PARAM_CFG_BASE( const wxChar* ident, const paramcfg_id type,
  331. const wxChar* group )
  332. {
  333. m_Ident = ident;
  334. m_Type = type;
  335. m_Group = group;
  336. m_Setup = FALSE;
  337. }
  338. PARAM_CFG_INT::PARAM_CFG_INT( const wxChar* ident, int* ptparam,
  339. int default_val, int min, int max,
  340. const wxChar* group ) :
  341. PARAM_CFG_BASE( ident, PARAM_INT, group )
  342. {
  343. m_Pt_param = ptparam;
  344. m_Default = default_val;
  345. m_Min = min;
  346. m_Max = max;
  347. }
  348. PARAM_CFG_INT::PARAM_CFG_INT( bool Insetup, const wxChar* ident, int* ptparam,
  349. int default_val, int min, int max,
  350. const wxChar* group ) :
  351. PARAM_CFG_BASE( ident, PARAM_INT, group )
  352. {
  353. m_Pt_param = ptparam;
  354. m_Default = default_val;
  355. m_Min = min;
  356. m_Max = max;
  357. m_Setup = Insetup;
  358. }
  359. PARAM_CFG_SETCOLOR::PARAM_CFG_SETCOLOR( const wxChar* ident, int* ptparam,
  360. int default_val, const wxChar* group ) :
  361. PARAM_CFG_BASE( ident, PARAM_SETCOLOR, group )
  362. {
  363. m_Pt_param = ptparam;
  364. m_Default = default_val;
  365. }
  366. PARAM_CFG_SETCOLOR::PARAM_CFG_SETCOLOR( bool Insetup, const wxChar* ident, int* ptparam,
  367. int default_val, const wxChar* group ) :
  368. PARAM_CFG_BASE( ident, PARAM_SETCOLOR, group )
  369. {
  370. m_Pt_param = ptparam;
  371. m_Default = default_val;
  372. m_Setup = Insetup;
  373. }
  374. PARAM_CFG_DOUBLE::PARAM_CFG_DOUBLE( const wxChar* ident, double* ptparam,
  375. double default_val, double min, double max,
  376. const wxChar* group ) :
  377. PARAM_CFG_BASE( ident, PARAM_DOUBLE, group )
  378. {
  379. m_Pt_param = ptparam;
  380. m_Default = default_val;
  381. m_Min = min;
  382. m_Max = max;
  383. }
  384. PARAM_CFG_DOUBLE::PARAM_CFG_DOUBLE( bool Insetup, const wxChar* ident, double* ptparam,
  385. double default_val, double min, double max,
  386. const wxChar* group ) :
  387. PARAM_CFG_BASE( ident, PARAM_DOUBLE, group )
  388. {
  389. m_Pt_param = ptparam;
  390. m_Default = default_val;
  391. m_Min = min;
  392. m_Max = max;
  393. m_Setup = Insetup;
  394. }
  395. PARAM_CFG_BOOL::PARAM_CFG_BOOL( const wxChar* ident, bool* ptparam,
  396. int default_val, const wxChar* group ) :
  397. PARAM_CFG_BASE( ident, PARAM_BOOL, group )
  398. {
  399. m_Pt_param = ptparam;
  400. m_Default = default_val ? TRUE : FALSE;
  401. }
  402. PARAM_CFG_BOOL::PARAM_CFG_BOOL( bool Insetup, const wxChar* ident, bool* ptparam,
  403. int default_val, const wxChar* group ) :
  404. PARAM_CFG_BASE( ident, PARAM_BOOL, group )
  405. {
  406. m_Pt_param = ptparam;
  407. m_Default = default_val ? TRUE : FALSE;
  408. m_Setup = Insetup;
  409. }
  410. PARAM_CFG_WXSTRING::PARAM_CFG_WXSTRING( const wxChar* ident,
  411. wxString* ptparam, const wxChar* group ) :
  412. PARAM_CFG_BASE( ident, PARAM_WXSTRING, group )
  413. {
  414. m_Pt_param = ptparam;
  415. }
  416. PARAM_CFG_WXSTRING::PARAM_CFG_WXSTRING( bool Insetup, const wxChar* ident,
  417. wxString* ptparam, const wxChar* group ) :
  418. PARAM_CFG_BASE( ident, PARAM_WXSTRING, group )
  419. {
  420. m_Pt_param = ptparam;
  421. m_Setup = Insetup;
  422. }
  423. PARAM_CFG_LIBNAME_LIST::PARAM_CFG_LIBNAME_LIST( const wxChar* ident,
  424. wxArrayString* ptparam, const wxChar* group ) :
  425. PARAM_CFG_BASE( ident, PARAM_LIBNAME_LIST, group )
  426. {
  427. m_Pt_param = ptparam;
  428. }