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

对话框与窗口

开发平台:

Visual C++

  1. // XTPSyntaxEditFindReplaceDlg.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 TOOLKIT PRO 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/XTPDrawHelpers.h"
  24. #include "Common/XTPNotifyConnection.h"
  25. #include "Common/XTPSmartPtrInternalT.h"
  26. #include "Common/XTPVc50Helpers.h"
  27. #include "Common/XTPResourceManager.h"
  28. // syntax editor includes
  29. #include "XTPSyntaxEditDefines.h"
  30. #include "XTPSyntaxEditStruct.h"
  31. #include "XTPSyntaxEditLexPtrs.h"
  32. #include "XTPSyntaxEditLexClassSubObjT.h"
  33. #include "XTPSyntaxEditTextIterator.h"
  34. #include "XTPSyntaxEditSectionManager.h"
  35. #include "XTPSyntaxEditLexCfgFileReader.h"
  36. #include "XTPSyntaxEditLexClassSubObjDef.h"
  37. #include "XTPSyntaxEditLexClass.h"
  38. #include "XTPSyntaxEditLexParser.h"
  39. #include "XTPSyntaxEditFindReplaceDlg.h"
  40. #include "XTPSyntaxEditCtrl.h"
  41. #include "XTPSyntaxEditDoc.h"
  42. #include "XTPSyntaxEditView.h"
  43. #ifdef _DEBUG
  44. #define new DEBUG_NEW
  45. #undef THIS_FILE
  46. static char THIS_FILE[] = __FILE__;
  47. #endif
  48. const TCHAR XTP_EDIT_FINDDLG_MATCHWHOLEWORD[]      = _T("MatchWholeWord");
  49. const TCHAR XTP_EDIT_FINDDLG_MATCHCASE[]           = _T("MatchCase");
  50. const TCHAR XTP_EDIT_FINDDLG_SEARCHDIRECTION[]     = _T("SearchDirection");
  51. const TCHAR XTP_EDIT_FINDDLG_FINDHISTORYCOUNT[]    = _T("FindHistoryCount");
  52. const TCHAR XTP_EDIT_FINDDLG_FINDHISTORYITEM[]     = _T("FindHistory_%d");
  53. const TCHAR XTP_EDIT_FINDDLG_REPLACEHISTORYCOUNT[] = _T("ReplaceHistoryCount");
  54. const TCHAR XTP_EDIT_FINDDLG_REPLACEHISTORYITEM[]  = _T("ReplaceHistory_%d");
  55. //===========================================================================
  56. // CXTPSyntaxEditFindReplaceDlg
  57. //===========================================================================
  58. IMPLEMENT_DYNAMIC(CXTPSyntaxEditFindReplaceDlg, CDialog)
  59. CXTPSyntaxEditFindReplaceDlg::CXTPSyntaxEditFindReplaceDlg(CWnd* pParentWnd)
  60. : //CDialog(CXTPSyntaxEditFindReplaceDlg::IDD, pParentWnd),
  61. m_ptWndPos(CPoint(-1,-1))
  62. , m_pEditCtrl(NULL)
  63. , m_bReplaceDlg(FALSE)
  64. {
  65. InitModalIndirect(XTPResourceManager()->LoadDialogTemplate(IDD), pParentWnd);
  66. m_csFindText = _T("");
  67. m_csReplaceText = _T("");
  68. m_bMatchWholeWord = FALSE;
  69. m_bMatchCase = FALSE;
  70. m_nSearchDirection = 1;
  71. }
  72. void CXTPSyntaxEditFindReplaceDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74. CDialog::DoDataExchange(pDX);
  75. //{{AFX_DATA_MAP(CXTPSyntaxEditFindReplaceDlg)
  76. DDX_Text(pDX, XTP_IDC_EDIT_COMBO_FIND, m_csFindText);
  77. DDX_Check(pDX, XTP_IDC_EDIT_CHK_MATCH_CASE, m_bMatchCase);
  78. DDX_Check(pDX, XTP_IDC_EDIT_CHK_MATCH_WHOLE_WORD, m_bMatchWholeWord);
  79. DDX_Control(pDX, XTP_IDC_EDIT_COMBO_FIND, m_wndFindCombo);
  80. DDX_Control(pDX, XTP_IDC_EDIT_BTN_FIND_NEXT, m_btnFindNext);
  81. DDX_Control(pDX, XTP_IDC_EDIT_RADIO_UP, m_btnRadioUp);
  82. DDX_Control(pDX, XTP_IDC_EDIT_RADIO_DOWN, m_btnRadioDown);
  83. //}}AFX_DATA_MAP
  84. if (m_bReplaceDlg)
  85. {
  86. DDX_Text(pDX, XTP_IDC_EDIT_COMBO_REPLACE, m_csReplaceText);
  87. DDX_Control(pDX, XTP_IDC_EDIT_BTN_REPLACE, m_btnReplace);
  88. DDX_Control(pDX, XTP_IDC_EDIT_BTN_REPLACE_ALL, m_btnReplaceAll);
  89. DDX_Control(pDX, XTP_IDC_EDIT_COMBO_REPLACE, m_wndReplaceCombo);
  90. }
  91. }
  92. BEGIN_MESSAGE_MAP(CXTPSyntaxEditFindReplaceDlg, CDialog)
  93. //{{AFX_MSG_MAP(CXTPSyntaxEditFindReplaceDlg)
  94. ON_CBN_EDITCHANGE(XTP_IDC_EDIT_COMBO_FIND, OnEditChangeComboFind)
  95. ON_CBN_EDITCHANGE(XTP_IDC_EDIT_COMBO_REPLACE, OnEditChangeComboReplace)
  96. ON_CBN_SELENDOK(XTP_IDC_EDIT_COMBO_REPLACE, OnSelendOkComboReplace)
  97. ON_CBN_SELENDOK(XTP_IDC_EDIT_COMBO_FIND, OnSelendOkComboFind)
  98. ON_BN_CLICKED(XTP_IDC_EDIT_BTN_FIND_NEXT, OnBtnFindNext)
  99. ON_BN_CLICKED(XTP_IDC_EDIT_BTN_REPLACE, OnBtnReplace)
  100. ON_BN_CLICKED(XTP_IDC_EDIT_BTN_REPLACE_ALL, OnBtnReplaceAll)
  101. ON_BN_CLICKED(XTP_IDC_EDIT_CHK_MATCH_WHOLE_WORD, OnChkMatchWholeWord)
  102. ON_BN_CLICKED(XTP_IDC_EDIT_CHK_MATCH_CASE, OnChkMatchCase)
  103. ON_BN_CLICKED(XTP_IDC_EDIT_RADIO_UP, OnRadioUp)
  104. ON_BN_CLICKED(XTP_IDC_EDIT_RADIO_DOWN, OnRadioDown)
  105. //}}AFX_MSG_MAP
  106. END_MESSAGE_MAP()
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CXTPSyntaxEditFindReplaceDlg message handlers
  109. BOOL CXTPSyntaxEditFindReplaceDlg::OnInitDialog ()
  110. {
  111. CDialog::OnInitDialog();
  112. SetDefID(m_bReplaceDlg ? XTP_IDC_EDIT_BTN_REPLACE : XTP_IDC_EDIT_BTN_FIND_NEXT);
  113. return TRUE;
  114. }
  115. void CXTPSyntaxEditFindReplaceDlg::UpdateHistoryCombo(const CString& csText, CStringArray& arHistory)
  116. {
  117. if (csText.IsEmpty())
  118. {
  119. return;
  120. }
  121. int nCount = (int)arHistory.GetSize();
  122. for (int i = nCount-1; i >= 0; i--)
  123. {
  124. if (arHistory[i] == csText)
  125. {
  126. arHistory.RemoveAt(i);
  127. }
  128. }
  129. arHistory.InsertAt(0, csText);
  130. }
  131. void CXTPSyntaxEditFindReplaceDlg::UpdateHistoryCombo(const CString& csText, CStringArray& arHistory, CComboBox& wndCombo)
  132. {
  133. UpdateHistoryCombo(csText, arHistory);
  134. if (::IsWindow(wndCombo.m_hWnd))
  135. {
  136. CString strLBText;
  137. int nCount = wndCombo.GetCount();
  138. for (int i = nCount-1; i >= 0; i--)
  139. {
  140. wndCombo.GetLBText(i, strLBText);
  141. if (strLBText == csText)
  142. {
  143. wndCombo.DeleteString(i);
  144. }
  145. }
  146. wndCombo.InsertString(0, csText);
  147. wndCombo.SetWindowText(csText);
  148. }
  149. }
  150. void CXTPSyntaxEditFindReplaceDlg::InitHistoryCombo(CStringArray& arHistory, CComboBox& wndCombo)
  151. {
  152. wndCombo.ResetContent();
  153. int nCount = (int)arHistory.GetSize();
  154. for (int i = 0; i < nCount; i++)
  155. {
  156. wndCombo.AddString(arHistory[i]);
  157. }
  158. }
  159. BOOL CXTPSyntaxEditFindReplaceDlg::ShowDialog(CXTPSyntaxEditCtrl* pEditCtrl, BOOL bReplaceDlg)
  160. {
  161. ASSERT_VALID(pEditCtrl);
  162. m_pEditCtrl = pEditCtrl;
  163. m_bReplaceDlg = bReplaceDlg;
  164. if (!::IsWindow(m_pEditCtrl->GetSafeHwnd()))
  165. return FALSE;
  166. // already created, bring to foreground.
  167. if (::IsWindow(m_hWnd))
  168. {
  169. ::ShowWindow(m_hWnd, SW_RESTORE);
  170. ::BringWindowToTop(m_hWnd);
  171. ::SetForegroundWindow(m_hWnd);
  172. }
  173. else
  174. {
  175. CWnd* pParent = m_pEditCtrl->GetParentFrame();
  176. UINT uID = bReplaceDlg? XTP_IDD_EDIT_SEARCH_REPLACE: XTP_IDD_EDIT_SEARCH_FIND;
  177. if (!CreateIndirect(XTPResourceManager()->LoadDialogTemplate2(MAKEINTRESOURCE(uID)), pParent))
  178. {
  179. TRACE0("Error creating find replace dialog.n");
  180. return FALSE;
  181. }
  182. LoadHistory();
  183. UpdateData(FALSE);
  184. EnableControls();
  185. m_btnRadioUp.SetCheck(m_nSearchDirection == 0);
  186. m_btnRadioDown.SetCheck(m_nSearchDirection == 1);
  187. CXTPWindowRect rc(this);
  188. ::MoveWindow(m_hWnd, m_ptWndPos.x, m_ptWndPos.y, rc.Width(), rc.Height(), FALSE);
  189. ::ShowWindow(m_hWnd, SW_SHOW);
  190. }
  191. m_wndFindCombo.SetFocus();
  192. m_wndFindCombo.SetEditSel(0,-1);
  193. return TRUE;
  194. }
  195. void CXTPSyntaxEditFindReplaceDlg::EnableControls()
  196. {
  197. m_btnFindNext.EnableWindow(!m_csFindText.IsEmpty());
  198. if (m_bReplaceDlg)
  199. {
  200. m_btnReplace.EnableWindow(TRUE);
  201. m_btnReplaceAll.EnableWindow(TRUE);
  202. }
  203. }
  204. void CXTPSyntaxEditFindReplaceDlg::OnCancel()
  205. {
  206. SaveHistory();
  207. CDialog::OnCancel();
  208. if (::IsWindow(m_hWnd))
  209. ::DestroyWindow(m_hWnd);
  210. }
  211. BOOL CXTPSyntaxEditFindReplaceDlg::LoadHistory()
  212. {
  213. CWinApp* pWinApp = AfxGetApp();
  214. if (!pWinApp)
  215. return FALSE;
  216. m_bMatchWholeWord = pWinApp->GetProfileInt(
  217. XTP_EDIT_REG_SETTINGS, XTP_EDIT_FINDDLG_MATCHWHOLEWORD, m_bMatchWholeWord);
  218. m_bMatchCase = pWinApp->GetProfileInt(
  219. XTP_EDIT_REG_SETTINGS, XTP_EDIT_FINDDLG_MATCHCASE, m_bMatchCase);
  220. m_nSearchDirection = pWinApp->GetProfileInt(
  221. XTP_EDIT_REG_SETTINGS, XTP_EDIT_FINDDLG_SEARCHDIRECTION, m_nSearchDirection);
  222. if (::IsWindow(m_wndFindCombo.m_hWnd))
  223. {
  224. int nCount = pWinApp->GetProfileInt(
  225. XTP_EDIT_REG_SETTINGS, XTP_EDIT_FINDDLG_FINDHISTORYCOUNT, 0);
  226. for (int i = 0; i < nCount; i++)
  227. {
  228. CString csEntry;
  229. csEntry.Format(XTP_EDIT_FINDDLG_FINDHISTORYITEM, i);
  230. CString csValue;
  231. csValue = pWinApp->GetProfileString(XTP_EDIT_REG_SETTINGS, csEntry);
  232. if (!csValue.IsEmpty())
  233. {
  234. if (m_wndFindCombo.FindStringExact(-1, csValue) == CB_ERR)
  235. {
  236. m_wndFindCombo.AddString(csValue);
  237. }
  238. }
  239. }
  240. }
  241. if (::IsWindow(m_wndReplaceCombo.m_hWnd))
  242. {
  243. int nCount = pWinApp->GetProfileInt(
  244. XTP_EDIT_REG_SETTINGS, XTP_EDIT_FINDDLG_REPLACEHISTORYCOUNT, 0);
  245. for (int i = 0; i < nCount; i++)
  246. {
  247. CString csEntry;
  248. csEntry.Format(XTP_EDIT_FINDDLG_REPLACEHISTORYITEM, i);
  249. CString csValue;
  250. csValue = pWinApp->GetProfileString(XTP_EDIT_REG_SETTINGS, csEntry);
  251. if (!csValue.IsEmpty())
  252. {
  253. if (m_wndReplaceCombo.FindStringExact(-1, csValue) == CB_ERR)
  254. {
  255. m_wndReplaceCombo.AddString(csValue);
  256. }
  257. }
  258. }
  259. }
  260. if (::IsWindow(m_hWnd))
  261. {
  262. if ((m_ptWndPos.x == -1 || m_ptWndPos.y == -1))
  263. {
  264. CXTPWindowRect r1(this);
  265. CXTPWindowRect r2(m_pEditCtrl);
  266. m_ptWndPos.x = r2.left + ((r2.Width()-r1.Width())/2);
  267. m_ptWndPos.y = r2.top + ((r2.Height()-r1.Height())/2);
  268. }
  269. }
  270. return TRUE;
  271. }
  272. BOOL CXTPSyntaxEditFindReplaceDlg::SaveHistory()
  273. {
  274. CWinApp* pWinApp = AfxGetApp();
  275. if (!pWinApp)
  276. return FALSE;
  277. pWinApp->WriteProfileInt(XTP_EDIT_REG_SETTINGS,
  278. XTP_EDIT_FINDDLG_MATCHWHOLEWORD, m_bMatchWholeWord);
  279. pWinApp->WriteProfileInt(XTP_EDIT_REG_SETTINGS,
  280. XTP_EDIT_FINDDLG_MATCHCASE, m_bMatchCase);
  281. pWinApp->WriteProfileInt(XTP_EDIT_REG_SETTINGS,
  282. XTP_EDIT_FINDDLG_SEARCHDIRECTION, m_nSearchDirection);
  283. if (::IsWindow(m_wndFindCombo.m_hWnd))
  284. {
  285. int nCount = m_wndFindCombo.GetCount();
  286. pWinApp->WriteProfileInt(XTP_EDIT_REG_SETTINGS,
  287. XTP_EDIT_FINDDLG_FINDHISTORYCOUNT, nCount);
  288. for (int i = 0; i < nCount; i++)
  289. {
  290. CString csEntry;
  291. csEntry.Format(XTP_EDIT_FINDDLG_FINDHISTORYITEM, i);
  292. CString csValue;
  293. m_wndFindCombo.GetLBText(i, csValue);
  294. pWinApp->WriteProfileString(XTP_EDIT_REG_SETTINGS,
  295. csEntry, csValue);
  296. }
  297. }
  298. if (::IsWindow(m_wndReplaceCombo.m_hWnd))
  299. {
  300. int nCount = m_wndReplaceCombo.GetCount();
  301. pWinApp->WriteProfileInt(XTP_EDIT_REG_SETTINGS,
  302. XTP_EDIT_FINDDLG_REPLACEHISTORYCOUNT, nCount);
  303. for (int i = 0; i < nCount; i++)
  304. {
  305. CString csEntry;
  306. csEntry.Format(XTP_EDIT_FINDDLG_REPLACEHISTORYITEM, i);
  307. CString csValue;
  308. m_wndReplaceCombo.GetLBText(i, csValue);
  309. pWinApp->WriteProfileString(XTP_EDIT_REG_SETTINGS,
  310. csEntry, csValue);
  311. }
  312. }
  313. if (::IsWindow(m_hWnd))
  314. {
  315. CXTPWindowRect rc(this);
  316. m_ptWndPos = rc.TopLeft();
  317. }
  318. return TRUE;
  319. }
  320. void CXTPSyntaxEditFindReplaceDlg::OnEditChangeComboFind()
  321. {
  322. UpdateData();
  323. EnableControls();
  324. }
  325. void CXTPSyntaxEditFindReplaceDlg::OnSelendOkComboFind()
  326. {
  327. m_wndFindCombo.GetLBText(m_wndFindCombo.GetCurSel(), m_csFindText);
  328. EnableControls();
  329. }
  330. void CXTPSyntaxEditFindReplaceDlg::OnEditChangeComboReplace()
  331. {
  332. UpdateData();
  333. EnableControls();
  334. }
  335. void CXTPSyntaxEditFindReplaceDlg::OnSelendOkComboReplace()
  336. {
  337. m_wndReplaceCombo.GetLBText(
  338. m_wndReplaceCombo.GetCurSel(), m_csReplaceText);
  339. EnableControls();
  340. }
  341. void CXTPSyntaxEditFindReplaceDlg::OnChkMatchWholeWord()
  342. {
  343. m_bMatchWholeWord = !m_bMatchWholeWord;
  344. }
  345. void CXTPSyntaxEditFindReplaceDlg::OnChkMatchCase()
  346. {
  347. m_bMatchCase = !m_bMatchCase;
  348. }
  349. void CXTPSyntaxEditFindReplaceDlg::OnRadioUp()
  350. {
  351. m_nSearchDirection = 0;
  352. }
  353. void CXTPSyntaxEditFindReplaceDlg::OnRadioDown()
  354. {
  355. m_nSearchDirection = 1;
  356. }
  357. void CXTPSyntaxEditFindReplaceDlg::OnBtnFindNext()
  358. {
  359. if (::IsWindow(m_pEditCtrl->GetSafeHwnd()) && !m_csFindText.IsEmpty())
  360. {
  361. BOOL bFound = m_pEditCtrl->Find(m_csFindText,
  362. m_bMatchWholeWord, m_bMatchCase, m_nSearchDirection);
  363. if (bFound)
  364. {
  365. m_pEditCtrl->Invalidate(FALSE);
  366. }
  367. else
  368. {
  369. AfxMessageBox(XTPResourceManager()->LoadString(XTP_IDS_EDIT_MSG_FSEARCH));
  370. }
  371. if (m_wndFindCombo.FindStringExact(-1, m_csFindText) == CB_ERR)
  372. {
  373. m_wndFindCombo.AddString(m_csFindText);
  374. }
  375. }
  376. }
  377. void CXTPSyntaxEditFindReplaceDlg::OnBtnReplace()
  378. {
  379. CWaitCursor wait;
  380. if (::IsWindow(m_pEditCtrl->GetSafeHwnd()) && m_bReplaceDlg)
  381. {
  382. CString csSelectedText;
  383. m_pEditCtrl->GetSelectionText(csSelectedText);
  384. csSelectedText = csSelectedText.SpanExcluding(_T("rn"));
  385. REPLACE_S(csSelectedText, _T("t"), _T("    "));
  386. BOOL bFoundText = FALSE;
  387. if (m_bMatchCase)
  388. {
  389. bFoundText = (csSelectedText.Compare(m_csFindText) == 0);
  390. }
  391. else
  392. {
  393. bFoundText = (csSelectedText.CompareNoCase(m_csFindText) == 0);
  394. }
  395. BOOL bFoundTextPrior = bFoundText;
  396. if (!bFoundText)
  397. {
  398. bFoundText = m_pEditCtrl->Find(m_csFindText, m_bMatchWholeWord,
  399. m_bMatchCase, m_nSearchDirection);
  400. }
  401. if (!bFoundText)
  402. {
  403. AfxMessageBox(XTPResourceManager()->LoadString(XTP_IDS_EDIT_MSG_FSEARCH));
  404. }
  405. else
  406. {
  407. if (bFoundTextPrior)
  408. {
  409. m_pEditCtrl->ReplaceSel(m_csReplaceText);
  410. bFoundText = m_pEditCtrl->Find(m_csFindText, m_bMatchWholeWord,
  411.    m_bMatchCase, m_nSearchDirection);
  412. if (!bFoundText)
  413. {
  414. AfxMessageBox(XTPResourceManager()->LoadString(XTP_IDS_EDIT_MSG_FSEARCH));
  415. }
  416. }
  417. m_pEditCtrl->Invalidate(FALSE);
  418. }
  419. if (m_wndReplaceCombo.FindStringExact(-1, m_csReplaceText) == CB_ERR)
  420. {
  421. m_wndReplaceCombo.AddString(m_csReplaceText);
  422. }
  423. }
  424. }
  425. void CXTPSyntaxEditFindReplaceDlg::OnBtnReplaceAll()
  426. {
  427. CWaitCursor wait;
  428. if (::IsWindow(m_pEditCtrl->GetSafeHwnd()))
  429. {
  430. int nMatchFound = m_pEditCtrl->ReplaceAll(m_csFindText, m_csReplaceText,
  431. m_bMatchWholeWord, m_bMatchCase);
  432. m_pEditCtrl->Invalidate(FALSE);
  433. CString csMatchFound;
  434. csMatchFound.Format(XTPResourceManager()->LoadString(XTP_IDS_EDIT_MSG_FREPLACED), nMatchFound);
  435. AfxMessageBox(csMatchFound);
  436. if (m_wndReplaceCombo.FindStringExact(-1, m_csReplaceText) == CB_ERR)
  437. {
  438. m_wndReplaceCombo.AddString(m_csReplaceText);
  439. }
  440. }
  441. }