XTPSyntaxEditLexColorFileReader.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:25k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPSyntaxEditLexColorFileReader.cpp: implementation of the CXTLexColorFileReader class.
  2. //
  3. // This file is a part of the XTREME TOOLKIT PRO MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME SYNTAX EDIT LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. //////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. // common includes
  22. #include "Common/XTPColorManager.h"
  23. #include "Common/XTPNotifyConnection.h"
  24. #include "Common/XTPSmartPtrInternalT.h"
  25. #include "Common/XTPVC80Helpers.h"
  26. // syntax editor includes
  27. #include "XTPSyntaxEditDefines.h"
  28. #include "XTPSyntaxEditStruct.h"
  29. #include "XTPSyntaxEditLexPtrs.h"
  30. #include "XTPSyntaxEditLexClassSubObjT.h"
  31. #include "XTPSyntaxEditTextIterator.h"
  32. #include "XTPSyntaxEditLexCfgFileReader.h"
  33. #include "XTPSyntaxEditLexClassSubObjDef.h"
  34. #include "XTPSyntaxEditLexClass.h"
  35. #include "XTPSyntaxEditLexParser.h"
  36. #include "XTPSyntaxEditSectionManager.h"
  37. #include "XTPSyntaxEditLexColorFileReader.h"
  38. #include "XTPSyntaxEditCtrl.h"
  39. #include "XTPSyntaxEditSectionManager.h"
  40. #ifdef _DEBUG
  41. #define new DEBUG_NEW
  42. #undef THIS_FILE
  43. static char THIS_FILE[] = __FILE__;
  44. #endif
  45. #define XTP_EMPTY_STRING        _T("")
  46. namespace XTPSyntaxEditLexAnalyser
  47. {
  48. extern void AddIfNeed_noCase(CStringArray& rarData, LPCTSTR strNew);
  49. extern BOOL IsEventSet(HANDLE hEvent);
  50. }
  51. //////////////////////////////////////////////////////////////////////
  52. // Construction/Destruction
  53. //////////////////////////////////////////////////////////////////////
  54. using namespace XTPSyntaxEditLexAnalyser;
  55. CXTPSyntaxEditColorInfo::CXTPSyntaxEditColorInfo(CXTPSyntaxEditColorTheme* pTheme) :
  56. m_pTheme(pTheme)
  57. {
  58. }
  59. CXTPSyntaxEditColorInfo::CXTPSyntaxEditColorInfo(const CString& strClassName,
  60.    CXTPSyntaxEditColorTheme* pTheme) :
  61. m_strClassName(strClassName), m_pTheme(pTheme)
  62. {
  63. }
  64. CXTPSyntaxEditColorInfo::~CXTPSyntaxEditColorInfo ()
  65. {
  66. }
  67. void CXTPSyntaxEditColorInfo::AddParam(const CString& strName, const CString& strValue)
  68. {
  69. CString strLowerName(strName);
  70. strLowerName.MakeLower();
  71. // save param-value pair
  72. m_mapParams[strLowerName] = strValue;
  73. }
  74. const CString CXTPSyntaxEditColorInfo::GetParam(const CString& strName,
  75.  BOOL bDynamic)
  76. {
  77. CString strLowerName(strName);
  78. strLowerName.MakeLower();
  79. CString strValue;
  80. if (!m_mapParams.Lookup(strLowerName, strValue) && bDynamic)
  81. {
  82. // do not search parent for main section parameters
  83. if (!m_strClassName.CompareNoCase(XTP_EDIT_LEXPARSER_SECTION_MAIN))
  84. {
  85. return strValue;
  86. }
  87. // try to get value from the parent schema
  88. CXTPSyntaxEditColorTheme* pParentTheme = m_pTheme ? m_pTheme->GetParentTheme() : NULL;
  89. if (m_pTheme && pParentTheme)
  90. {
  91. CXTPSyntaxEditColorInfo* pParentColorInfo = pParentTheme->GetColorInfo(m_strClassName, m_pTheme->GetFileName());
  92. if (pParentColorInfo)
  93. {
  94. strValue = pParentColorInfo->GetParam(strName);
  95. }
  96. }
  97. }
  98. return strValue;
  99. }
  100. DWORD CXTPSyntaxEditColorInfo::GetHexParam(const CString& strName, BOOL bDynamic)
  101. {
  102. // get hex value if exists
  103. DWORD dwHex = 0;
  104. SCANF_S(GetParam(strName, bDynamic), _T("%x"), &dwHex);
  105. return dwHex;
  106. }
  107. POSITION CXTPSyntaxEditColorInfo::GetFirstParamNamePosition()
  108. {
  109. return m_mapParams.GetStartPosition();
  110. }
  111. const CString CXTPSyntaxEditColorInfo::GetNextParamName(POSITION& pos)
  112. {
  113. CString strName, strValue;
  114. m_mapParams.GetNextAssoc(pos, strName, strValue);
  115. return strName;
  116. }
  117. /////////////////////////////////////////////////////////////////////////////
  118. //
  119. CXTPSyntaxEditColorTheme::CXTPSyntaxEditColorTheme(CXTPSyntaxEditColorThemesManager* pThemesManager) :
  120. m_pThemesManager(pThemesManager)
  121. {
  122. }
  123. CXTPSyntaxEditColorTheme::~CXTPSyntaxEditColorTheme()
  124. {
  125. Cleanup();
  126. }
  127. void CXTPSyntaxEditColorTheme::Cleanup()
  128. {
  129. // cleanup color info map
  130. POSITION pos = m_mapLexColorInfo.GetStartPosition();
  131. CString strKey;
  132. CXTPSyntaxEditColorInfo* pInf = NULL;
  133. while (pos != NULL)
  134. {
  135. m_mapLexColorInfo.GetNextAssoc(pos, strKey, pInf);
  136. if (pInf)
  137. delete pInf;
  138. }
  139. m_mapLexColorInfo.RemoveAll();
  140. }
  141. void CXTPSyntaxEditColorTheme::Load(const CString& csFileName)
  142. {
  143. Cleanup();
  144. m_csFileName = csFileName;
  145. // get sections list
  146. CStringArray arSections;
  147. CXTPSyntaxEditSectionManager().GetSectionNames(arSections, m_csFileName);
  148. if (arSections.GetSize() == 0)
  149. return;
  150. for (int i = 0; i < arSections.GetSize(); ++i)
  151. {
  152. CString csSection = arSections[i];
  153. csSection.MakeLower();
  154. CXTPSyntaxEditColorInfo* pOld = m_mapLexColorInfo[csSection];
  155. if (pOld)
  156. {
  157. delete pOld;
  158. }
  159. m_mapLexColorInfo[csSection] = ParseSection(csSection);
  160. }
  161. }
  162. BOOL CXTPSyntaxEditColorTheme::WriteCfgFile(CString strParentThemeName,
  163. CXTPSyntaxEditColorInfoArray* pColorInfoArray_new)
  164. {
  165. if (!pColorInfoArray_new)
  166. {
  167. ASSERT(FALSE);
  168. return FALSE;
  169. }
  170. CString strFileName = GetFileName();
  171. CString strFileName_new = strFileName + _T(".tmp");
  172. ::SetFileAttributes(strFileName_new, FILE_ATTRIBUTE_NORMAL);
  173. ::DeleteFile(strFileName_new);
  174. if (!::CopyFile(strFileName, strFileName_new,  FALSE))
  175. {
  176. TRACE(_T("ERROR! CXTPSyntaxEditColorTheme::WriteCfgFile() - Cannot copy file '%s' to '%s' n"), (LPCTSTR)strFileName, (LPCTSTR)strFileName_new);
  177. return FALSE;
  178. }
  179. CMapStringToPtr mapSaved_ClsProp;
  180. const CString cstrCNDelim = _T("-xtpEdit_ColorThemeClassPropDelimeter_xtpEdit-");
  181. void* pVoid = NULL;
  182. //---------------------------------------------------------------------------
  183. // (0) Update main section - parent schema property
  184. LPCTSTR pcszString = NULL;
  185. if (!strParentThemeName.IsEmpty())
  186. {
  187. pcszString = strParentThemeName;
  188. }
  189. BOOL bRes = ::WritePrivateProfileString(XTP_EDIT_LEXPARSER_SECTION_MAIN, XTP_EDIT_LEXPARSER_PARENT_SCHEMA,
  190. pcszString, strFileName_new);
  191. CString strSavedKey = XTP_EDIT_LEXPARSER_SECTION_MAIN + cstrCNDelim + XTP_EDIT_LEXPARSER_PARENT_SCHEMA;
  192. strSavedKey.MakeLower();
  193. ASSERT(mapSaved_ClsProp.Lookup(strSavedKey, pVoid) == FALSE);
  194. mapSaved_ClsProp.SetAt(strSavedKey, NULL);
  195. BOOL bClassPropRepeated_DbgAssertWas = FALSE;
  196. if (!bRes)
  197. {
  198. return FALSE;
  199. }
  200. // (1) Update Existing and add new properties
  201. int nCount = (int)pColorInfoArray_new->GetSize();
  202. for (int i = 0; i < nCount; i++)
  203. {
  204. CXTPSyntaxEditColorInfo* pClrInf_new = pColorInfoArray_new->GetAt(i);
  205. if (!pClrInf_new)
  206. {
  207. ASSERT(FALSE);
  208. continue;
  209. }
  210. CString csSection = pClrInf_new->GetClassName();
  211. POSITION posParam = pClrInf_new->GetFirstParamNamePosition();
  212. while (posParam)
  213. {
  214. CString strPrmName = pClrInf_new->GetNextParamName(posParam);
  215. CString strPrmValue = pClrInf_new->GetParam(strPrmName,  FALSE);
  216. if (!strPrmName.IsEmpty() && !strPrmValue.IsEmpty())
  217. {
  218. bRes = ::WritePrivateProfileString(csSection, strPrmName,
  219. strPrmValue, strFileName_new);
  220. if (!bRes)
  221. {
  222. return FALSE;
  223. }
  224. strSavedKey = csSection + cstrCNDelim + strPrmName;
  225. strSavedKey.MakeLower();
  226. if (mapSaved_ClsProp.Lookup(strSavedKey, pVoid))
  227. {
  228. if (!bClassPropRepeated_DbgAssertWas)
  229. {
  230. ASSERT(FALSE);
  231. bClassPropRepeated_DbgAssertWas = TRUE;
  232. }
  233. TRACE(_T("WARNING! Color theme class property repeated: %s->%s. n"), (LPCTSTR)csSection, (LPCTSTR)strPrmName);
  234. }
  235. mapSaved_ClsProp.SetAt(strSavedKey, NULL);
  236. }
  237. }
  238. }
  239. //===========================================================================
  240. // (2) Remove old properties
  241. POSITION posClrInfo_old = m_mapLexColorInfo.GetStartPosition();
  242. CString strClassName;
  243. CXTPSyntaxEditColorInfo* pClrInf_old = NULL;
  244. while (posClrInfo_old)
  245. {
  246. m_mapLexColorInfo.GetNextAssoc(posClrInfo_old, strClassName, pClrInf_old);
  247. if (!pClrInf_old)
  248. {
  249. ASSERT(FALSE);
  250. continue;
  251. }
  252. CString csSection = pClrInf_old->GetClassName();
  253. POSITION posParam = pClrInf_old->GetFirstParamNamePosition();
  254. while (posParam)
  255. {
  256. CString strPrmName = pClrInf_old->GetNextParamName(posParam);
  257. strSavedKey = csSection + cstrCNDelim + strPrmName;
  258. strSavedKey.MakeLower();
  259. if (!mapSaved_ClsProp.Lookup(strSavedKey, pVoid))
  260. {
  261. VERIFY( ::WritePrivateProfileString(csSection, strPrmName,
  262. NULL, strFileName_new) );
  263. }
  264. }
  265. }
  266. //===========================================================================
  267. CString strFileName_prev = strFileName + _T(".old");
  268. ::SetFileAttributes(strFileName_prev, FILE_ATTRIBUTE_NORMAL);
  269. ::DeleteFile(strFileName_prev);
  270. #ifdef _DEBUG
  271. //::MoveFile(strFileName, strFileName_prev);
  272. #endif
  273. ::SetFileAttributes(strFileName, FILE_ATTRIBUTE_NORMAL);
  274. ::DeleteFile(strFileName);
  275. if (!::MoveFile(strFileName_new, strFileName))
  276. {
  277. TRACE(_T("ERROR! CXTPSyntaxEditColorTheme::WriteCfgFile() - Cannot rename file '%s' to '%s' n"), (LPCTSTR)strFileName_new, (LPCTSTR)strFileName);
  278. return FALSE;
  279. }
  280. return TRUE;
  281. }
  282. CXTPSyntaxEditColorInfo* CXTPSyntaxEditColorTheme::ParseSection(const CString& csSection)
  283. {
  284. CXTPSyntaxEditColorInfo* pColorInfo = new CXTPSyntaxEditColorInfo(csSection, this);
  285. CXTPSyntaxEditSchemaFileInfoList infoList;
  286. CXTPSyntaxEditSectionManager().GetSectionKeyList(infoList, m_csFileName, csSection);
  287. for (POSITION pos = infoList.GetHeadPosition(); pos;)
  288. {
  289. XTP_EDIT_SCHEMAFILEINFO& info = infoList.GetNext(pos);
  290. pColorInfo->AddParam(info.csName, info.csValue);
  291. }
  292. return pColorInfo;
  293. }
  294. const CString CXTPSyntaxEditColorTheme::GetParentThemeName()
  295. {
  296. CXTPSyntaxEditColorInfo* pInfo = NULL;
  297. if (m_mapLexColorInfo.Lookup(XTP_EDIT_LEXPARSER_SECTION_MAIN, pInfo) && pInfo)
  298. {
  299. return pInfo->GetParam(XTP_EDIT_LEXPARSER_PARENT_SCHEMA);
  300. }
  301. return _T("");
  302. }
  303. CXTPSyntaxEditColorTheme* CXTPSyntaxEditColorTheme::GetParentTheme()
  304. {
  305. return m_pThemesManager ? m_pThemesManager->GetTheme(GetParentThemeName()) : NULL;
  306. }
  307. CXTPSyntaxEditColorInfo* CXTPSyntaxEditColorTheme::GetColorInfo(
  308. const CString& strLexClass,
  309. const CString& strThemeFilename,
  310. BOOL bDynamic)
  311. {
  312. CXTPSyntaxEditColorInfo* pInfo = NULL;
  313. CString strLexClass_lower = strLexClass;
  314. strLexClass_lower.MakeLower();
  315. if (!m_mapLexColorInfo.Lookup(strLexClass_lower, pInfo) && bDynamic)
  316. {
  317. // lookup color info on parent themes
  318. CXTPSyntaxEditColorTheme* pParentTheme = GetParentTheme();
  319. if (pParentTheme && pParentTheme->GetFileName().CompareNoCase(strThemeFilename))
  320. {
  321. pInfo = pParentTheme->GetColorInfo(strLexClass_lower, strThemeFilename, TRUE);
  322. }
  323. }
  324. return pInfo;
  325. }
  326. /////////////////////////////////////////////////////////////////////////////
  327. // CXTPSyntaxEditColorThemesManager
  328. CXTPSyntaxEditColorThemesManager::CXTPSyntaxEditColorThemesManager()
  329. {
  330. }
  331. CXTPSyntaxEditColorThemesManager::~CXTPSyntaxEditColorThemesManager()
  332. {
  333. RemoveAll();
  334. }
  335. void CXTPSyntaxEditColorThemesManager::LoadTheme(const CString& strThemeName,
  336. const CString& strThemeFilename)
  337. {
  338. // delete old theme
  339. CXTPSyntaxEditColorTheme* pTheme = NULL;
  340. if (m_mapThemes.Lookup(strThemeName, pTheme) && pTheme)
  341. {
  342. delete pTheme;
  343. // delete its name
  344. for (int i = 0; i < m_arThemeNames.GetSize(); i++)
  345. {
  346. if (!m_arThemeNames.GetAt(i).CompareNoCase(strThemeName))
  347. {
  348. m_arThemeNames.RemoveAt(i);
  349. break;
  350. }
  351. }
  352. }
  353. // create and load new theme
  354. pTheme = new CXTPSyntaxEditColorTheme(this);
  355. pTheme->Load(strThemeFilename);
  356. m_mapThemes[strThemeName] = pTheme;
  357. m_arThemeNames.Add(strThemeName);
  358. CString strTFNameLower = strThemeFilename;
  359. strTFNameLower.MakeLower();
  360. m_mapFileToTheme[strTFNameLower] = strThemeName;
  361. }
  362. void CXTPSyntaxEditColorThemesManager::AddThemeInfo(const CString& strThemeName,
  363.    const CString& strThemeFilename)
  364. {
  365. CString strTFNameLower = strThemeFilename;
  366. strTFNameLower.MakeLower();
  367. m_mapFileToTheme[strTFNameLower] = strThemeName;
  368. }
  369. CXTPSyntaxEditColorTheme* CXTPSyntaxEditColorThemesManager::GetTheme(const CString& strThemeName)
  370. {
  371. CXTPSyntaxEditColorTheme* pTheme = NULL;
  372. m_mapThemes.Lookup(strThemeName, pTheme);
  373. return pTheme;
  374. }
  375. CStringArray& CXTPSyntaxEditColorThemesManager::GetThemes()
  376. {
  377. return m_arThemeNames;
  378. }
  379. CString CXTPSyntaxEditColorThemesManager::ReloadFile(const CString& csFileName,
  380. int nCfgFlags)
  381. {
  382. CString strThemeName;
  383. // iterate all themes
  384. POSITION pos = m_mapThemes.GetStartPosition();
  385. CString strKey;
  386. CXTPSyntaxEditColorTheme* pTheme = NULL;
  387. while (pos != NULL)
  388. {
  389. m_mapThemes.GetNextAssoc(pos, strKey, pTheme);
  390. if (pTheme && !pTheme->GetFileName().CompareNoCase(csFileName))
  391. {
  392. strThemeName = strKey;
  393. if (nCfgFlags & xtpEditCfgFileRemove)
  394. {
  395. delete pTheme;
  396. m_mapThemes.RemoveKey(strKey);
  397. }
  398. else
  399. {
  400. pTheme->Load(csFileName);
  401. }
  402. return strThemeName;
  403. }
  404. }
  405. //------------------------------------------------------------------------
  406. if (nCfgFlags & xtpEditCfgFileAdd)
  407. {
  408. CString strTFileLower = csFileName;
  409. strTFileLower.MakeLower();
  410. if (m_mapFileToTheme.Lookup(strTFileLower, strThemeName))
  411. {
  412. LoadTheme(strThemeName, csFileName);
  413. }
  414. else
  415. {
  416. strThemeName.Empty();
  417. //ASSERT(FALSE);
  418. }
  419. }
  420. //------------------------------------------------------------------------
  421. return strThemeName;
  422. }
  423. void CXTPSyntaxEditColorThemesManager::RemoveAll()
  424. {
  425. m_arThemeNames.RemoveAll();
  426. m_mapFileToTheme.RemoveAll();
  427. // cleanup themes map
  428. POSITION pos = m_mapThemes.GetStartPosition();
  429. CString strKey;
  430. CXTPSyntaxEditColorTheme* pTheme = NULL;
  431. while (pos != NULL)
  432. {
  433. m_mapThemes.GetNextAssoc(pos, strKey, pTheme);
  434. if (pTheme)
  435. delete pTheme;
  436. }
  437. m_mapThemes.RemoveAll();
  438. }
  439. /////////////////////////////////////////////////////////////////////////////
  440. // CXTPSyntaxEditTextSchemesManager
  441. CXTPSyntaxEditTextSchemesManager::CXTPSyntaxEditTextSchemesManager()
  442. {
  443. }
  444. CXTPSyntaxEditTextSchemesManager::~CXTPSyntaxEditTextSchemesManager()
  445. {
  446. }
  447. void CXTPSyntaxEditTextSchemesManager::LoadTextScheme(XTP_EDIT_SCHEMAFILEINFO& info)
  448. {
  449. CString strSchemeNameLower(info.csName);
  450. strSchemeNameLower.MakeLower();
  451. CSingleLock lockReadFile(&m_csReadFile,  TRUE);
  452. if (info.uValue != (UINT)-1)
  453. XTPSyntaxEditLexConfig()->ReadSource(info.uValue);
  454. else
  455. XTPSyntaxEditLexConfig()->ReadSource(info.csValue, TRUE);
  456. CXTPSyntaxEditLexClassInfoArray& arLexClassInfo = XTPSyntaxEditLexConfig()->GetLexClassInfoArray();
  457. CXTPSyntaxEditTextSchemaPtr ptrTxtSch(new CXTPSyntaxEditTextSchema(info.csName));
  458. if (ptrTxtSch)
  459. {
  460. ptrTxtSch->LoadClassSchema(&arLexClassInfo);
  461. m_mapSchemes[strSchemeNameLower] = ptrTxtSch;
  462. AddTextSchemeInfo(info);
  463. }
  464. }
  465. void CXTPSyntaxEditTextSchemesManager::AddTextSchemeInfo(XTP_EDIT_SCHEMAFILEINFO& info)
  466. {
  467. if (GetSchemaFileName(info.csName).IsEmpty())
  468. m_listSchemes.AddHead(info);
  469. XTPSyntaxEditLexAnalyser::AddIfNeed_noCase(m_arSchemeNames, info.csName);
  470. }
  471. CXTPSyntaxEditTextSchemaPtr CXTPSyntaxEditTextSchemesManager::GetSchema(const CString& strSchemeName)
  472. {
  473. CString strSchemeNameLower(strSchemeName);
  474. strSchemeNameLower.MakeLower();
  475. CXTPSyntaxEditTextSchemaPtr ptrScheme;
  476. m_mapSchemes.Lookup(strSchemeNameLower, ptrScheme);
  477. return ptrScheme;
  478. }
  479. CXTPSyntaxEditTextSchemaPtr CXTPSyntaxEditTextSchemesManager::FindSchema(const CString& strFileExt)
  480. {
  481. // iterate all text schemes
  482. CString strSchemaName;
  483. CXTPSyntaxEditTextSchemaPtr ptrScheme;
  484. if (!strFileExt.IsEmpty())
  485. {
  486. POSITION pos = m_mapSchemes.GetStartPosition();
  487. while (pos != NULL)
  488. {
  489. m_mapSchemes.GetNextAssoc(pos, strSchemaName, ptrScheme);
  490. if (ptrScheme && ptrScheme->IsFileExtSupported(strFileExt))
  491. {
  492. return ptrScheme;
  493. }
  494. }
  495. }
  496. return NULL;
  497. }
  498. void CXTPSyntaxEditTextSchemesManager::SetTheme(CXTPSyntaxEditColorTheme* pTheme)
  499. {
  500. // iterate all text schemes
  501. CString strSchemaName;
  502. CXTPSyntaxEditTextSchemaPtr ptrScheme;
  503. POSITION pos = m_mapSchemes.GetStartPosition();
  504. while (pos != NULL)
  505. {
  506. m_mapSchemes.GetNextAssoc(pos, strSchemaName, ptrScheme);
  507. if (ptrScheme)
  508. {
  509. ptrScheme->ApplyTheme(pTheme);
  510. }
  511. }
  512. }
  513. const CString CXTPSyntaxEditTextSchemesManager::GetSchemaName(const CString& csFileName)
  514. {
  515. XTP_EDIT_SCHEMAFILEINFO info;
  516. if (m_listSchemes.LookupValue(csFileName, info))
  517. {
  518. return info.csName;
  519. }
  520. return _T("");
  521. }
  522. const CString CXTPSyntaxEditTextSchemesManager::GetSchemaFileName(const CString& strSchemeName)
  523. {
  524. XTP_EDIT_SCHEMAFILEINFO info;
  525. if (m_listSchemes.LookupName(strSchemeName, info))
  526. {
  527. return info.csValue;
  528. }
  529. return _T("");
  530. }
  531. BOOL CXTPSyntaxEditTextSchemesManager::ReloadFile(const CString& csFileName, int nCfgFlags)
  532. {
  533. XTP_EDIT_SCHEMAFILEINFO info;
  534. if (m_listSchemes.LookupValue(csFileName, info))
  535. {
  536. CString csSchemaName = info.csName;
  537. csSchemaName.MakeLower();
  538. m_mapSchemes.RemoveKey(csSchemaName);
  539. if ((nCfgFlags & xtpEditCfgFileRemove) == 0)
  540. {
  541. LoadTextScheme(info);
  542. }
  543. return TRUE;
  544. }
  545. return FALSE;
  546. }
  547. void CXTPSyntaxEditTextSchemesManager::RemoveAll()
  548. {
  549. m_mapSchemes.RemoveAll();
  550. m_listSchemes.RemoveAll();
  551. m_arSchemeNames.RemoveAll();
  552. }
  553. /////////////////////////////////////////////////////////////////////////////
  554. // CXTPSyntaxEditConfigurationManager
  555. CXTPSyntaxEditConfigurationManager::CXTPSyntaxEditConfigurationManager()
  556. {
  557. m_pConnectMT = new CXTPNotifyConnectionMT();
  558. m_hReloadThread = NULL;
  559. m_pBreakReloadEvent = NULL;
  560. m_strCurrentThemeName = XTP_EDIT_LEXPARSER_DEFTHEME;
  561. m_FolderMonitor.SetConfigurationManager(this);
  562. }
  563. CXTPSyntaxEditConfigurationManager::~CXTPSyntaxEditConfigurationManager()
  564. {
  565. Close();
  566. CMDTARGET_RELEASE(m_pConnectMT);
  567. }
  568. void CXTPSyntaxEditConfigurationManager::Close()
  569. {
  570. m_FolderMonitor.StopMonitoring();
  571. if (m_hReloadThread)
  572. {
  573. ASSERT(m_pBreakReloadEvent);
  574. if (m_pBreakReloadEvent)
  575. {
  576. m_pBreakReloadEvent->SetEvent();
  577. }
  578. DWORD dwThreadRes = ::WaitForSingleObject(m_hReloadThread, 30*1000);
  579. if (dwThreadRes == WAIT_TIMEOUT)
  580. {
  581. ::TerminateThread(m_hReloadThread, 0);
  582. TRACE(_T("ERROR! Configuration Manager reload thread was not ended by normal way. It was terminated. n"));
  583. }
  584. }
  585. m_hReloadThread = NULL;
  586. if (m_pBreakReloadEvent)
  587. {
  588. delete m_pBreakReloadEvent;
  589. m_pBreakReloadEvent = NULL;
  590. }
  591. m_FolderMonitor.StopMonitoring();
  592. m_ColorThemeManager.RemoveAll();
  593. m_TextSchemesManager.RemoveAll();
  594. }
  595. CString CXTPSyntaxEditConfigurationManager::GetConfigFile() const
  596. {
  597. return m_strMainIniFilename;
  598. }
  599. void CXTPSyntaxEditConfigurationManager::ReloadConfig(const CString& strConfigFilePath)
  600. {
  601. CSingleLock singleLock(&m_DataLockerCS, TRUE);
  602. m_strMainIniFilename = strConfigFilePath;
  603. ReloadConfig();
  604. }
  605. void CXTPSyntaxEditConfigurationManager::ReloadConfig()
  606. {
  607. CSingleLock singleLock(&m_DataLockerCS, TRUE);
  608. TRACE(_T("LOAD/reload configuration: %s n"), (LPCTSTR)m_strMainIniFilename);
  609. // restart folder monitor
  610. m_FolderMonitor.StopMonitoring();
  611. // Remove previous data
  612. m_ColorThemeManager.RemoveAll();
  613. m_TextSchemesManager.RemoveAll();
  614. if (!m_FolderMonitor.AddMonitorFile(m_strMainIniFilename, xtpEditCfgObjMainConfig))
  615. {
  616. m_pConnectMT->SendEvent(xtpEditAllConfigWasChanged, 0, 0);
  617. // start monitoring config files
  618. m_FolderMonitor.StartMonitoring();
  619. return;
  620. }
  621. CString strMainIniFolder;
  622. int nFLs = m_strMainIniFilename.ReverseFind(_T('\'));
  623. if (nFLs > 0)
  624. {
  625. strMainIniFolder = m_strMainIniFilename.Mid(0, nFLs + 1);
  626. }
  627. m_FolderMonitor.SetDefaultFolder(strMainIniFolder);
  628. CXTPSyntaxEditSchemaFileInfoList listThemes;
  629. CXTPSyntaxEditSectionManager().GetSectionKeyList(listThemes, m_strMainIniFilename, XTP_EDIT_LEXPARSER_SECTION_THEMES);
  630. for (POSITION posThemes = listThemes.GetHeadPosition(); posThemes;)
  631. {
  632. XTP_EDIT_SCHEMAFILEINFO& info = listThemes.GetNext(posThemes);
  633. if (m_hReloadThread && m_pBreakReloadEvent)
  634. {
  635. if (XTPSyntaxEditLexAnalyser::IsEventSet(*m_pBreakReloadEvent))
  636. {
  637. TRACE(_T("BREAK config reloading. n"));
  638. return;
  639. }
  640. }
  641. TRACE(_T("LOAD Theme: %s - %sn"), (LPCTSTR)info.csName, (LPCTSTR)info.csValue);
  642. if (m_FolderMonitor.AddMonitorFile(info.csValue, xtpEditCfgObjThemeMan))
  643. {
  644. m_ColorThemeManager.LoadTheme(info.csName, info.csValue);
  645. }
  646. else
  647. {
  648. m_ColorThemeManager.AddThemeInfo(info.csName, info.csValue);
  649. }
  650. }
  651. // load all schemes config files
  652. CXTPSyntaxEditSchemaFileInfoList listSchemes;
  653. CXTPSyntaxEditSectionManager().GetSectionKeyList(listSchemes, m_strMainIniFilename, XTP_EDIT_LEXPARSER_SECTION_SCHEMES);
  654. for (POSITION posSchemes = listSchemes.GetHeadPosition(); posSchemes;)
  655. {
  656. XTP_EDIT_SCHEMAFILEINFO& info = listSchemes.GetNext(posSchemes);
  657. if (m_hReloadThread && m_pBreakReloadEvent)
  658. {
  659. if (XTPSyntaxEditLexAnalyser::IsEventSet(*m_pBreakReloadEvent))
  660. {
  661. TRACE(_T("BREAK config reloading. n"));
  662. return;
  663. }
  664. }
  665. TRACE(_T("LOAD Scheme: %s - %sn"), (LPCTSTR)info.csName, (LPCTSTR)info.csValue);
  666. if (m_FolderMonitor.AddMonitorFile(info.csValue, xtpEditCfgObjSchMan))
  667. {
  668. m_TextSchemesManager.LoadTextScheme(info);
  669. }
  670. else
  671. {
  672. m_TextSchemesManager.AddTextSchemeInfo(info);
  673. }
  674. }
  675. // set default theme
  676. SetTheme(m_strCurrentThemeName);
  677. //========================================================================
  678. if (m_hReloadThread && m_pBreakReloadEvent)
  679. {
  680. if (XTPSyntaxEditLexAnalyser::IsEventSet(*m_pBreakReloadEvent))
  681. {
  682. TRACE(_T("BREAK config reloading. n"));
  683. return;
  684. }
  685. }
  686. //========================================================================
  687. m_pConnectMT->SendEvent(xtpEditAllConfigWasChanged, 0, 0);
  688. // start monitoring config files
  689. m_FolderMonitor.StartMonitoring();
  690. }
  691. void CXTPSyntaxEditConfigurationManager::ReloadFile(CString csFileName,
  692.    DWORD dwOwnerFlags,
  693.    int nCfgFlags)
  694. {
  695. CSingleLock singleLock(&m_DataLockerCS, TRUE);
  696. CString strItemName;
  697. // if file is color theme definition
  698. if ((dwOwnerFlags & xtpEditCfgObjThemeMan) || dwOwnerFlags == 0)
  699. {
  700. TRACE(_T("Reload Theme: %s n"), (LPCTSTR)csFileName);
  701. strItemName = m_ColorThemeManager.ReloadFile(csFileName, nCfgFlags);
  702. if (!strItemName.IsEmpty())
  703. {
  704. // Re-apply current theme after reloading
  705. if (!strItemName.CompareNoCase(m_strCurrentThemeName))
  706. {
  707. SetTheme(m_strCurrentThemeName);
  708. }
  709. CXTPSyntaxEditColorTheme* pTheme = m_ColorThemeManager.GetTheme(strItemName);
  710. m_pConnectMT->SendEvent(xtpEditThemeWasChanged,
  711. (WPARAM)(LPCTSTR)strItemName, (LPARAM)pTheme);
  712. return;
  713. }
  714. }
  715. // if file is lex text schema definition
  716. if ((dwOwnerFlags & xtpEditCfgObjSchMan) || dwOwnerFlags == 0)
  717. {
  718. TRACE(_T("Reload Scheme: %s n"), (LPCTSTR)csFileName);
  719. if (m_TextSchemesManager.ReloadFile(csFileName, nCfgFlags))
  720. {
  721. CString strSchName = m_TextSchemesManager.GetSchemaName(csFileName);
  722. CXTPSyntaxEditTextSchemaPtr ptrTxtSch;
  723. ptrTxtSch = m_TextSchemesManager.GetSchema(strSchName);
  724. CXTPSyntaxEditColorTheme* pTheme = m_ColorThemeManager.GetTheme(m_strCurrentThemeName);
  725. if (pTheme && ptrTxtSch)
  726. {
  727. ptrTxtSch->ApplyTheme(pTheme);
  728. }
  729. m_pConnectMT->SendEvent(xtpEditClassSchWasChanged, (WPARAM)(LPCTSTR)strSchName,
  730. (LPARAM)(CXTPSyntaxEditTextSchema*)ptrTxtSch);
  731. return;
  732. }
  733. }
  734. // else - file is the main schema definition
  735. if ((dwOwnerFlags & xtpEditCfgObjMainConfig) || dwOwnerFlags == 0)
  736. {
  737. ReloadConfigAsync();
  738. }
  739. }
  740. void CXTPSyntaxEditConfigurationManager::SetTheme(const CString& strThemeName,
  741.  CXTPSyntaxEditTextSchema* pActiveSch)
  742. {
  743. CSingleLock singleLock(&m_DataLockerCS, TRUE);
  744. CXTPSyntaxEditColorTheme* pTheme = m_ColorThemeManager.GetTheme(strThemeName);
  745. //if (!pTheme) {
  746. //  return;
  747. //}
  748. m_strCurrentThemeName = strThemeName;
  749. m_TextSchemesManager.SetTheme(pTheme);
  750. if (pActiveSch)
  751. {
  752. pActiveSch->ApplyTheme(pTheme);
  753. }
  754. }
  755. void CXTPSyntaxEditConfigurationManager::ReloadConfigAsync(int nAsyncThreadPriority)
  756. {
  757. InternalAddRef();
  758. CSingleLock singleLock(&m_DataLockerCS, TRUE);
  759. ASSERT(m_pBreakReloadEvent == NULL);
  760. m_pBreakReloadEvent = new CEvent(FALSE, TRUE);
  761. CWinThread* pThread = AfxBeginThread(ReloadConfigAsync_Proc, this, nAsyncThreadPriority);
  762. if (pThread)
  763. {
  764. ASSERT(m_hReloadThread == NULL);
  765. m_hReloadThread = pThread->m_hThread;
  766. }
  767. else
  768. {
  769. if (m_pBreakReloadEvent)
  770. {
  771. delete m_pBreakReloadEvent;
  772. m_pBreakReloadEvent = NULL;
  773. }
  774. }
  775. }
  776. UINT CXTPSyntaxEditConfigurationManager::ReloadConfigAsync_Proc(LPVOID pThis)
  777. {
  778. CXTPSyntaxEditConfigurationManager* pMan = (CXTPSyntaxEditConfigurationManager*)pThis;
  779. ASSERT(pMan);
  780. if (pMan)
  781. {
  782. pMan->ReloadConfig();
  783. pMan->m_hReloadThread = NULL;
  784. pMan->InternalRelease();
  785. }
  786. return 0;
  787. }