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

对话框与窗口

开发平台:

Visual C++

  1. // XTPSyntaxEditUndoManager.cpp : implementation file
  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. #include "Resource.h"
  22. // common includes
  23. #include "Common/XTPNotifyConnection.h"
  24. #include "Common/XTPSmartPtrInternalT.h"
  25. #include "Common/XTPResourceManager.h"
  26. // syntax editor includes
  27. #include "XTPSyntaxEditDefines.h"
  28. #include "XTPSyntaxEditStruct.h"
  29. #include "XTPSyntaxEditUndoManager.h"
  30. #include "XTPSyntaxEditLexPtrs.h"
  31. #include "XTPSyntaxEditLexClassSubObjT.h"
  32. #include "XTPSyntaxEditTextIterator.h"
  33. #include "XTPSyntaxEditSectionManager.h"
  34. #include "XTPSyntaxEditLexCfgFileReader.h"
  35. #include "XTPSyntaxEditLexClassSubObjDef.h"
  36. #include "XTPSyntaxEditLexClass.h"
  37. #include "XTPSyntaxEditLexParser.h"
  38. #include "XTPSyntaxEditBufferManager.h"
  39. #include "XTPSyntaxEditCtrl.h"
  40. #ifdef _DEBUG
  41. #define new DEBUG_NEW
  42. #undef THIS_FILE
  43. static char THIS_FILE[] = __FILE__;
  44. #endif
  45. /////////////////////////////////////////////////////////////////////////////
  46. CXTPSyntaxEditUndoRedoManager::CXTPSyntaxEditUndoRedoManager()
  47. {
  48. m_posSavedMark = m_posFirstUndo = NULL;
  49. //m_posSavedMark = BEFORE_START_POSITION;
  50. m_bGroupInsertMode = FALSE;
  51. m_nDataSizeLimit = 50 * 1024 * 1024;
  52. }
  53. CXTPSyntaxEditUndoRedoManager::~CXTPSyntaxEditUndoRedoManager()
  54. {
  55. Clear();
  56. }
  57. int CXTPSyntaxEditUndoRedoManager::DoUndo(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  58. {
  59. int nRet = 0;
  60. if (m_posFirstUndo != NULL)
  61. {
  62. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetAt(m_posFirstUndo);
  63. if (pCmd != NULL)
  64. {
  65. nRet = pCmd->UnExecute(lcFrom, lcTo, pEditCtrl);
  66. }
  67. m_CommandList.GetPrev(m_posFirstUndo);
  68. }
  69. return nRet;
  70. }
  71. int CXTPSyntaxEditUndoRedoManager::DoRedo(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  72. {
  73. int nRet = 0;
  74. // get next item
  75. if (m_posFirstUndo != NULL)
  76. {
  77. m_CommandList.GetNext(m_posFirstUndo);
  78. }
  79. else
  80. {
  81. m_posFirstUndo = m_CommandList.GetHeadPosition();
  82. }
  83. // do action
  84. if (m_posFirstUndo != NULL)
  85. {
  86. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetAt(m_posFirstUndo);
  87. if (pCmd != NULL)
  88. {
  89. nRet = pCmd->Execute(lcFrom, lcTo, pEditCtrl);
  90. }
  91. }
  92. return nRet;
  93. }
  94. void CXTPSyntaxEditUndoRedoManager::RemoveTail()
  95. {
  96. // remove tail of the list
  97. for (POSITION posTail = m_CommandList.GetTailPosition();
  98. posTail != m_posFirstUndo;
  99. posTail = m_CommandList.GetTailPosition())
  100. {
  101. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetTail();
  102. m_CommandList.RemoveTail();
  103. delete pCmd;
  104. }
  105. }
  106. int CXTPSyntaxEditUndoRedoManager::GetDataSize()
  107. {
  108. int nDataSize = 0;
  109. POSITION pos = m_CommandList.GetHeadPosition();
  110. while (pos)
  111. {
  112. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetNext(pos);
  113. if (pCmd)
  114. nDataSize += pCmd->GetDataSize();
  115. }
  116. return nDataSize;
  117. }
  118. void CXTPSyntaxEditUndoRedoManager::LimitDataSize(int nNewCommandData, int nDataSizeLimit)
  119. {
  120. int nDataSize = GetDataSize();
  121. if (nDataSize + nNewCommandData < nDataSizeLimit)
  122. return;
  123. POSITION pos = m_CommandList.GetHeadPosition();
  124. while (pos)
  125. {
  126. if (pos == m_posFirstUndo)
  127. {
  128. Clear();
  129. return;
  130. }
  131. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetNext(pos);
  132. if (pCmd)
  133. nDataSize -= pCmd->GetDataSize();
  134. if (nDataSize + nNewCommandData < nDataSizeLimit)
  135. break;
  136. }
  137. }
  138. void CXTPSyntaxEditUndoRedoManager::AddCommand(CXTPSyntaxEditCommand* pCommand)
  139. {
  140. RemoveTail();
  141. LimitDataSize(pCommand->GetDataSize(), m_nDataSizeLimit);
  142. // insert undo command
  143. if (!m_bGroupInsertMode)
  144. {
  145. m_CommandList.AddTail(pCommand);
  146. }
  147. else
  148. {
  149. CXTPSyntaxEditCommand* pCmd = m_CommandList.GetTailPosition() ?
  150. (CXTPSyntaxEditCommand*)m_CommandList.GetTail() : NULL;
  151. CXTPSyntaxEditBatchCommand* pBatchCmd = DYNAMIC_DOWNCAST(CXTPSyntaxEditBatchCommand, pCmd);
  152. if (!pBatchCmd)
  153. {
  154. pBatchCmd = new CXTPSyntaxEditBatchCommand();
  155. m_CommandList.AddTail(pBatchCmd);
  156. }
  157. pBatchCmd->AddCommand(pCommand);
  158. }
  159. m_posFirstUndo = m_CommandList.GetTailPosition();
  160. }
  161. void CXTPSyntaxEditUndoRedoManager::Clear()
  162. {
  163. for (POSITION posTail = m_CommandList.GetTailPosition();
  164. posTail != NULL;
  165. posTail = m_CommandList.GetTailPosition())
  166. {
  167. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetTail();
  168. m_CommandList.RemoveTail();
  169. delete pCmd;
  170. }
  171. m_posFirstUndo = NULL;
  172. }
  173. BOOL CXTPSyntaxEditUndoRedoManager::CanUndo()
  174. {
  175. if (m_posFirstUndo == NULL)
  176. return FALSE;
  177. return TRUE;
  178. }
  179. BOOL CXTPSyntaxEditUndoRedoManager::CanRedo()
  180. {
  181. POSITION posLast = m_CommandList.GetTailPosition();
  182. if (posLast == m_posFirstUndo)
  183. return FALSE;
  184. return TRUE;
  185. }
  186. void CXTPSyntaxEditUndoRedoManager::MarkSaved()
  187. {
  188. m_posSavedMark = m_posFirstUndo;
  189. }
  190. BOOL CXTPSyntaxEditUndoRedoManager::IsModified()
  191. {
  192. return m_posSavedMark != m_posFirstUndo;
  193. }
  194. void CXTPSyntaxEditUndoRedoManager::SetGroupInsertMode(BOOL bInsertInGroup)
  195. {
  196. if (m_bGroupInsertMode == bInsertInGroup)
  197. return;
  198. m_bGroupInsertMode = bInsertInGroup;
  199. if (m_bGroupInsertMode)
  200. {
  201. // add empty batch command
  202. RemoveTail();
  203. m_CommandList.AddTail(new CXTPSyntaxEditBatchCommand());
  204. m_posFirstUndo = m_CommandList.GetTailPosition();
  205. }
  206. else
  207. {
  208. // delete empty batch command if necessary
  209. CXTPSyntaxEditCommand* pCmd = m_CommandList.GetTailPosition() ?
  210. (CXTPSyntaxEditCommand*)m_CommandList.GetTail() : NULL;
  211. CXTPSyntaxEditBatchCommand* pBatchCmd = DYNAMIC_DOWNCAST(CXTPSyntaxEditBatchCommand, pCmd);
  212. if (pBatchCmd && pBatchCmd->GetCommandsCount() < 1)
  213. {
  214. m_CommandList.RemoveTail();
  215. delete pBatchCmd;
  216. m_posFirstUndo = m_CommandList.GetTailPosition();
  217. }
  218. }
  219. }
  220. const CStringList& CXTPSyntaxEditUndoRedoManager::GetUndoTextList()
  221. {
  222. m_lstUndoText.RemoveAll();
  223. // iterate all undo items
  224. POSITION posHead = m_CommandList.GetHeadPosition();
  225. do
  226. {
  227. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetAt(posHead);
  228. if (pCmd)
  229. {
  230. m_lstUndoText.AddTail(pCmd->GetCommandText());
  231. }
  232. if (posHead == m_posFirstUndo)
  233. break;
  234. else
  235. m_CommandList.GetNext(posHead);
  236. }
  237. while (posHead != NULL);
  238. return m_lstUndoText;
  239. }
  240. const CStringList& CXTPSyntaxEditUndoRedoManager::GetRedoTextList()
  241. {
  242. m_lstRedoText.RemoveAll();
  243. // iterate all redo items
  244. for (POSITION posTail = m_CommandList.GetTailPosition();
  245. posTail != m_posFirstUndo;
  246. m_CommandList.GetPrev(posTail))
  247. {
  248. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetAt(posTail);
  249. if (pCmd)
  250. {
  251. m_lstRedoText.AddHead(pCmd->GetCommandText());
  252. }
  253. }
  254. return m_lstRedoText;
  255. }
  256. void CXTPSyntaxEditUndoRedoManager::ChainLastCommand()
  257. {
  258. POSITION posPrev = m_posFirstUndo;
  259. // Take latest command and the previous, if any not exists - return
  260. CXTPSyntaxEditCommand* pCmd = posPrev ?
  261. (CXTPSyntaxEditCommand*)m_CommandList.GetPrev(posPrev) : NULL;
  262. if (!pCmd || !posPrev)
  263. return;
  264. CXTPSyntaxEditCommand* pPrevCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetAt(posPrev);
  265. if (!pPrevCmd)
  266. return;
  267. // Convert previous command to batch, or create new batch if not exist
  268. CXTPSyntaxEditBatchCommand* pBatchCmd = DYNAMIC_DOWNCAST(CXTPSyntaxEditBatchCommand, pPrevCmd);
  269. if (!pBatchCmd)
  270. {
  271. // Create batch command
  272. pBatchCmd = new CXTPSyntaxEditBatchCommand();
  273. // add previous command to the batch
  274. pBatchCmd->AddCommand(pPrevCmd);
  275. // reinsert batch command instead of usual previous command
  276. m_CommandList.SetAt(posPrev, pBatchCmd);
  277. }
  278. pBatchCmd->AddCommand(pCmd);
  279. // make previous batch command as latest undo position
  280. m_CommandList.RemoveAt(m_posFirstUndo);
  281. m_posFirstUndo = posPrev;
  282. }
  283. void CXTPSyntaxEditUndoRedoManager::SetLastCommandText(LPCTSTR szText)
  284. {
  285. // Take the latest command
  286. CXTPSyntaxEditCommand* pCmd = m_posFirstUndo ?
  287. (CXTPSyntaxEditCommand*)m_CommandList.GetAt(m_posFirstUndo) : NULL;
  288. if (!pCmd)
  289. return;
  290. // Set its text
  291. pCmd->SetCommandText(szText);
  292. }
  293. void CXTPSyntaxEditUndoRedoManager::SetLastCommandText(UINT nTextId)
  294. {
  295. // load text from resources
  296. CString strText;
  297. XTPResourceManager()->LoadString(&strText, nTextId);
  298. // run text changing
  299. SetLastCommandText(strText);
  300. }
  301. //////////////////////////////////////////////////////////////////////
  302. // CXTPSyntaxEditCommand
  303. //////////////////////////////////////////////////////////////////////
  304. IMPLEMENT_DYNAMIC(CXTPSyntaxEditCommand, CObject)
  305. CXTPSyntaxEditCommand::CXTPSyntaxEditCommand()
  306. {
  307. XTPResourceManager()->LoadString(&m_strCommandText, XTP_IDS_EDIT_UNDEFINED);
  308. }
  309. CXTPSyntaxEditCommand::~CXTPSyntaxEditCommand()
  310. {
  311. }
  312. void CXTPSyntaxEditCommand::SetPositionInternally(CXTPSyntaxEditCtrl* pEditCtrl, const XTP_EDIT_LINECOL& lcPos)
  313. {
  314. if (pEditCtrl)
  315. {
  316. pEditCtrl->SetCurrentDocumentRow(lcPos.nLine);
  317. pEditCtrl->m_nCurrentCol = lcPos.nCol;
  318. }
  319. }
  320. //////////////////////////////////////////////////////////////////////
  321. // CXTPSyntaxEditBatchCommand
  322. //////////////////////////////////////////////////////////////////////
  323. IMPLEMENT_DYNAMIC(CXTPSyntaxEditBatchCommand, CXTPSyntaxEditCommand)
  324. CXTPSyntaxEditBatchCommand::CXTPSyntaxEditBatchCommand()
  325. {
  326. XTPResourceManager()->LoadString(&m_strCommandText, XTP_IDS_EDIT_MULTIPLE);
  327. }
  328. CXTPSyntaxEditBatchCommand::~CXTPSyntaxEditBatchCommand()
  329. {
  330. Clear();
  331. }
  332. int CXTPSyntaxEditBatchCommand::Execute(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  333. {
  334. int nRet = 0;
  335. lcFrom = XTP_EDIT_LINECOL::MAXPOS;
  336. lcTo = XTP_EDIT_LINECOL::MINPOS;
  337. POSITION posCmd = m_CommandList.GetTailPosition();
  338. for ( int i = (int)m_CommandList.GetCount() - 1; i >= 0; i-- )
  339. {
  340. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetPrev(posCmd);
  341. if (pCmd != NULL)
  342. {
  343. XTP_EDIT_LINECOL lcTmpFrom, lcTmpTo;
  344. nRet |= pCmd->Execute(lcTmpFrom, lcTmpTo, pEditCtrl);
  345. lcFrom = min(lcFrom, lcTmpFrom);
  346. lcTo = max(lcTo, lcTmpTo);
  347. }
  348. }
  349. return nRet;
  350. }
  351. int CXTPSyntaxEditBatchCommand::UnExecute(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  352. {
  353. int nRet = 0;
  354. lcFrom = XTP_EDIT_LINECOL::MAXPOS;
  355. lcTo = XTP_EDIT_LINECOL::MINPOS;
  356. POSITION posCmd = m_CommandList.GetHeadPosition();
  357. for ( int i = 0; i < m_CommandList.GetCount(); i++ )
  358. {
  359. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetNext(posCmd);
  360. if (pCmd != NULL)
  361. {
  362. XTP_EDIT_LINECOL lcTmpFrom, lcTmpTo;
  363. nRet |= pCmd->UnExecute(lcTmpFrom, lcTmpTo, pEditCtrl);
  364. lcFrom = min(lcFrom, lcTmpFrom);
  365. lcTo = max(lcTo, lcTmpTo);
  366. }
  367. }
  368. return nRet;
  369. }
  370. POSITION CXTPSyntaxEditBatchCommand::AddCommand(CXTPSyntaxEditCommand* pCmd)
  371. {
  372. // insert command
  373. return m_CommandList.AddHead(pCmd);
  374. }
  375. void CXTPSyntaxEditBatchCommand::Clear()
  376. {
  377. for ( POSITION posTail = m_CommandList.GetTailPosition();
  378. posTail != NULL;
  379. posTail = m_CommandList.GetTailPosition() )
  380. {
  381. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetTail();
  382. m_CommandList.RemoveTail();
  383. delete pCmd;
  384. }
  385. }
  386. int CXTPSyntaxEditBatchCommand::GetCommandsCount()
  387. {
  388. return (int)m_CommandList.GetCount();
  389. }
  390. int CXTPSyntaxEditBatchCommand::GetDataSize()
  391. {
  392. int nDataSize = 0;
  393. POSITION pos = m_CommandList.GetHeadPosition();
  394. while (pos)
  395. {
  396. CXTPSyntaxEditCommand* pCmd = (CXTPSyntaxEditCommand*)m_CommandList.GetNext(pos);
  397. if (pCmd)
  398. nDataSize += pCmd->GetDataSize();
  399. }
  400. return nDataSize;
  401. }
  402. //////////////////////////////////////////////////////////////////////
  403. // CXTPSyntaxEditBufferCommand
  404. //////////////////////////////////////////////////////////////////////
  405. IMPLEMENT_DYNAMIC(CXTPSyntaxEditBufferCommand, CXTPSyntaxEditCommand)
  406. CXTPSyntaxEditBufferCommand::CXTPSyntaxEditBufferCommand(
  407. CXTPSyntaxEditBufferManager* pMgr,
  408. LPCTSTR szText, const XTP_EDIT_LINECOL& lcFrom, const XTP_EDIT_LINECOL& lcTo)
  409. : m_pBufferMgr(pMgr), m_strText(szText), m_lcFrom(lcFrom), m_lcTo(lcTo)
  410. {
  411. }
  412. CXTPSyntaxEditBufferCommand::~CXTPSyntaxEditBufferCommand()
  413. {
  414. }
  415. CXTPSyntaxEditBufferCommand::CXTPSyntaxEditBufferKeepOverwriteSettings::CXTPSyntaxEditBufferKeepOverwriteSettings(CXTPSyntaxEditBufferManager* pBufferMgr)
  416. : m_pBufMgr(pBufferMgr)
  417. {
  418. m_bOldSettings = m_pBufMgr ? m_pBufMgr->GetOverwriteFlag() : FALSE;
  419. if (m_pBufMgr)
  420. {
  421. m_pBufMgr->SetOverwriteFlag(FALSE);
  422. }
  423. }
  424. CXTPSyntaxEditBufferCommand::CXTPSyntaxEditBufferKeepOverwriteSettings::~CXTPSyntaxEditBufferKeepOverwriteSettings()
  425. {
  426. if (m_pBufMgr)
  427. {
  428. m_pBufMgr->SetOverwriteFlag(m_bOldSettings);
  429. }
  430. }
  431. int CXTPSyntaxEditBufferCommand::DoDeleteText(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  432. {
  433. if (!m_pBufferMgr)
  434. return 0;
  435. CXTPSyntaxEditBufferKeepOverwriteSettings bufOwr(m_pBufferMgr);
  436. m_pBufferMgr->GetLexParser()->OnBeforeEditChanged();
  437. // performs undoing operation
  438. m_pBufferMgr->DeleteText(m_lcFrom.nLine, m_lcFrom.nCol, m_lcTo.nLine, m_lcTo.nCol, FALSE);
  439. // send corresponding notification
  440. m_pBufferMgr->GetLexParser()->OnEditChanged(m_lcFrom, m_lcTo, xtpEditActDelete, m_pBufferMgr);
  441. // adjust cursor position
  442. SetPositionInternally(pEditCtrl, m_lcFrom);
  443. // calculate edit action
  444. int nEditAction = XTP_EDIT_EDITACTION_DELETETEXT;
  445. if (m_lcFrom.nLine != m_lcTo.nLine)
  446. nEditAction |= XTP_EDIT_EDITACTION_DELETEROW | XTP_EDIT_EDITACTION_MODIFYROW;
  447. else if (m_lcFrom.nCol != m_lcTo.nCol)
  448. nEditAction |= XTP_EDIT_EDITACTION_MODIFYROW;
  449. // set edit action bounds
  450. lcFrom = min(m_lcFrom, m_lcTo);
  451. lcTo = max(m_lcFrom, m_lcTo);
  452. return nEditAction;
  453. }
  454. int CXTPSyntaxEditBufferCommand::DoInsertText(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  455. {
  456. if (!m_pBufferMgr)
  457. return 0;
  458. // performs undoing operation
  459. CXTPSyntaxEditBufferKeepOverwriteSettings bufOwr(m_pBufferMgr);
  460. m_pBufferMgr->GetLexParser()->OnBeforeEditChanged();
  461. m_pBufferMgr->InsertText(m_strText, m_lcFrom.nLine, m_lcFrom.nCol, FALSE);
  462. // send corresponding notification
  463. m_pBufferMgr->GetLexParser()->OnEditChanged(m_lcFrom, m_lcTo, xtpEditActInsert, m_pBufferMgr);
  464. // adjust cursor position
  465. SetPositionInternally(pEditCtrl, m_lcTo);
  466. // calculate edit action
  467. int nEditAction = XTP_EDIT_EDITACTION_INSERTTEXT;
  468. if (m_lcFrom.nLine != m_lcTo.nLine)
  469. nEditAction |= XTP_EDIT_EDITACTION_INSERTROW | XTP_EDIT_EDITACTION_MODIFYROW;
  470. else if (m_lcFrom.nCol != m_lcTo.nCol)
  471. nEditAction |= XTP_EDIT_EDITACTION_MODIFYROW;
  472. // set edit action bounds
  473. lcFrom = min(m_lcFrom, m_lcTo);
  474. lcTo = max(m_lcFrom, m_lcTo);
  475. return nEditAction;
  476. }
  477. //////////////////////////////////////////////////////////////////////
  478. // CXTPSyntaxEditDeleteStringCommand
  479. //////////////////////////////////////////////////////////////////////
  480. IMPLEMENT_DYNAMIC(CXTPSyntaxEditDeleteStringCommand, CXTPSyntaxEditBufferCommand)
  481. CXTPSyntaxEditDeleteStringCommand::CXTPSyntaxEditDeleteStringCommand(
  482. CXTPSyntaxEditBufferManager* pMgr,
  483. LPCTSTR szText, const XTP_EDIT_LINECOL& lcFrom, const XTP_EDIT_LINECOL& lcTo)
  484. : CXTPSyntaxEditBufferCommand(pMgr, szText, lcFrom, lcTo)
  485. {
  486. XTPResourceManager()->LoadString(&m_strCommandText, XTP_IDS_EDIT_DELETE);
  487. m_strCommandText += m_strText;
  488. }
  489. CXTPSyntaxEditDeleteStringCommand::~CXTPSyntaxEditDeleteStringCommand()
  490. {
  491. }
  492. int CXTPSyntaxEditDeleteStringCommand::Execute(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  493. {
  494. return DoDeleteText(lcFrom, lcTo, pEditCtrl);
  495. }
  496. int CXTPSyntaxEditDeleteStringCommand::UnExecute(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  497. {
  498. int nResult = DoInsertText(lcFrom, lcTo, pEditCtrl);
  499. return nResult;
  500. }
  501. //////////////////////////////////////////////////////////////////////
  502. // CXTPSyntaxEditInsertStringCommand
  503. //////////////////////////////////////////////////////////////////////
  504. IMPLEMENT_DYNAMIC(CXTPSyntaxEditInsertStringCommand, CXTPSyntaxEditBufferCommand)
  505. CXTPSyntaxEditInsertStringCommand::CXTPSyntaxEditInsertStringCommand(
  506. CXTPSyntaxEditBufferManager* pMgr,
  507. LPCTSTR szText, const XTP_EDIT_LINECOL& lcFrom, const XTP_EDIT_LINECOL& lcTo)
  508.    : CXTPSyntaxEditBufferCommand(pMgr, szText, lcFrom, lcTo)
  509. {
  510. XTPResourceManager()->LoadString(&m_strCommandText, XTP_IDS_EDIT_INSERT);
  511. m_strCommandText += m_strText;
  512. }
  513. CXTPSyntaxEditInsertStringCommand::~CXTPSyntaxEditInsertStringCommand()
  514. {
  515. }
  516. int CXTPSyntaxEditInsertStringCommand::Execute(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  517. {
  518. return DoInsertText(lcFrom, lcTo, pEditCtrl);
  519. }
  520. int CXTPSyntaxEditInsertStringCommand::UnExecute(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  521. {
  522. return DoDeleteText(lcFrom, lcTo, pEditCtrl);
  523. }
  524. //////////////////////////////////////////////////////////////////////
  525. // CXTPSyntaxEditReplaceStringCommand
  526. //////////////////////////////////////////////////////////////////////
  527. IMPLEMENT_DYNAMIC(CXTPSyntaxEditReplaceStringCommand, CXTPSyntaxEditBufferCommand)
  528. CXTPSyntaxEditReplaceStringCommand::CXTPSyntaxEditReplaceStringCommand(
  529. CXTPSyntaxEditBufferManager* pMgr,
  530. LPCTSTR szText, LPCTSTR szReplacedText,
  531. const XTP_EDIT_LINECOL& lcFrom, const XTP_EDIT_LINECOL& lcTo)
  532. : CXTPSyntaxEditBufferCommand(pMgr, szText, lcFrom, lcTo), m_strReplacedText(szReplacedText)
  533. {
  534. XTPResourceManager()->LoadString(&m_strCommandText, XTP_IDS_EDIT_MULTIPLE);
  535. m_strCommandText += m_strText;
  536. }
  537. CXTPSyntaxEditReplaceStringCommand::~CXTPSyntaxEditReplaceStringCommand()
  538. {
  539. }
  540. int CXTPSyntaxEditReplaceStringCommand::Execute(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  541. {
  542. return DoReplaceText(m_strText, lcFrom, lcTo, pEditCtrl);
  543. }
  544. int CXTPSyntaxEditReplaceStringCommand::UnExecute(XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  545. {
  546. return DoReplaceText(m_strReplacedText, lcFrom, lcTo, pEditCtrl);
  547. }
  548. int CXTPSyntaxEditReplaceStringCommand::DoReplaceText(LPCTSTR szText, XTP_EDIT_LINECOL& lcFrom, XTP_EDIT_LINECOL& lcTo, CXTPSyntaxEditCtrl* pEditCtrl)
  549. {
  550. if (!m_pBufferMgr)
  551. return 0;
  552. // performs undoing operation
  553. CXTPSyntaxEditBufferKeepOverwriteSettings bufOwr(m_pBufferMgr);
  554. m_pBufferMgr->SetOverwriteFlag(TRUE);
  555. m_pBufferMgr->GetLexParser()->OnBeforeEditChanged();
  556. XTP_EDIT_LINECOL lcBegin = min(m_lcFrom, m_lcTo);
  557. m_pBufferMgr->InsertText(szText, lcBegin.nLine, lcBegin.nCol, FALSE);
  558. // send corresponding notification
  559. m_pBufferMgr->GetLexParser()->OnEditChanged(m_lcFrom, m_lcTo, xtpEditActInsert, m_pBufferMgr);
  560. // adjust cursor position
  561. SetPositionInternally(pEditCtrl, m_lcFrom);
  562. // calculate edit action
  563. int nEditAction = XTP_EDIT_EDITACTION_INSERTTEXT | XTP_EDIT_EDITACTION_DELETETEXT;
  564. if (m_lcFrom.nLine != m_lcTo.nLine)
  565. nEditAction |= XTP_EDIT_EDITACTION_INSERTROW | XTP_EDIT_EDITACTION_DELETEROW | XTP_EDIT_EDITACTION_MODIFYROW;
  566. else if (m_lcFrom.nCol != m_lcTo.nCol)
  567. nEditAction |= XTP_EDIT_EDITACTION_MODIFYROW;
  568. // set edit action bounds
  569. lcFrom = min(m_lcFrom, m_lcTo);
  570. lcTo = max(m_lcFrom, m_lcTo);
  571. return nEditAction;
  572. }