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

对话框与窗口

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame 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 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 "Grep.h"
  22. #include "GrepView.h"
  23. #include "SearchOptions.h"
  24. #include "MainFrm.h"
  25. #include "SearchThread.h"
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31. #define TID_REFRESH 100
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CMainFrame
  34. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  35. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  36. //{{AFX_MSG_MAP(CMainFrame)
  37. ON_WM_CREATE()
  38. ON_WM_CLOSE()
  39. ON_COMMAND(ID_VIEW_OPTIONSPANE, OnViewOptionspane)
  40. //}}AFX_MSG_MAP
  41. ON_COMMAND(XTP_ID_CUSTOMIZE, OnCustomize)
  42. ON_MESSAGE(XTPWM_DOCKINGPANE_NOTIFY, OnDockingPaneNotify)
  43. ON_MESSAGE_VOID(WM_SEARCHFINISHED, OnFindDone)
  44. ON_WM_TIMER()
  45. END_MESSAGE_MAP()
  46. static UINT indicators[] =
  47. {
  48. ID_SEPARATOR,           // status line indicator
  49. ID_INDICATOR_CAPS,
  50. ID_INDICATOR_NUM,
  51. ID_INDICATOR_SCRL,
  52. };
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CMainFrame construction/destruction
  55. CMainFrame::CMainFrame()
  56. {
  57. // get path of executable
  58. TCHAR szBuff[_MAX_PATH];
  59. VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), szBuff, _MAX_PATH));
  60. LPTSTR lpszExt = _tcsrchr(szBuff, '.');
  61. lstrcpy(lpszExt, _T(".xml"));
  62. m_strIniFileName = szBuff;
  63. m_pSearchThread = NULL;
  64. m_nTimer = 0;
  65. }
  66. CMainFrame::~CMainFrame()
  67. {
  68. }
  69. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  70. {
  71. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  72. return -1;
  73. // Create Status bar.
  74. // Important: All control bars including the Status Bar
  75. // must be created before CommandBars....
  76. if (!m_wndStatusBar.Create(this) ||
  77. !m_wndStatusBar.SetIndicators(indicators,
  78. sizeof(indicators)/sizeof(UINT)))
  79. {
  80. TRACE0("Failed to create status barn");
  81. return -1;      // fail to create
  82. }
  83. // Initialize the command bars
  84. if (!InitCommandBars())
  85. return -1;
  86. // Get a pointer to the command bars object.
  87. CXTPCommandBars* pCommandBars = GetCommandBars();
  88. if(pCommandBars == NULL)
  89. {
  90. TRACE0("Failed to create command bars object.n");
  91. return -1;      // fail to create
  92. }
  93. // Add the menu bar
  94. CXTPCommandBar* pMenuBar = pCommandBars->SetMenu(
  95. _T("Menu Bar"), IDR_MAINFRAME);
  96. if(pMenuBar == NULL)
  97. {
  98. TRACE0("Failed to create menu bar.n");
  99. return -1;      // fail to create
  100. }
  101. // Create ToolBar
  102. CXTPToolBar* pToolBar = (CXTPToolBar*)
  103. pCommandBars->Add(_T("Standard"), xtpBarTop);
  104. if (!pToolBar || !pToolBar->LoadToolBar(IDR_MAINFRAME))
  105. {
  106. TRACE0("Failed to create toolbarn");
  107. return -1;
  108. }
  109. pCommandBars->GetCommandBarsOptions()->ShowKeyboardCues(xtpKeyboardCuesShowWindowsDefault);
  110. // Set Office 2003 Theme
  111. CXTPPaintManager::SetTheme(xtpThemeWhidbey);
  112. if (pCommandBars->GetImageManager()->IsAlphaIconsSupported())
  113. {
  114. pCommandBars->GetImageManager()->SetIcons(IDR_MAINFRAME, IDB_TOOLBAR_ALPHA);
  115. }
  116. // Load the previous state for toolbars and menus.
  117. LoadCommandBars(_T("CommandBars"));
  118. // Initialize the docking pane manager and set the
  119. // initial them for the docking panes.  Do this only after all
  120. // control bars objects have been created and docked.
  121. m_paneManager.InstallDockingPanes(this);
  122. // Set Office 2003 Theme
  123. m_paneManager.SetTheme(xtpPaneThemeWhidbey);
  124. m_paneManager.SetAlphaDockingContext(TRUE);
  125. m_paneManager.SetShowDockingContextStickers(TRUE);
  126. m_paneManager.SetShowContentsWhileDragging(TRUE);
  127. // Create docking panes.
  128. CXTPDockingPane* pwndPane1 = m_paneManager.CreatePane(
  129. IDR_PANE_OPTIONS, CRect(0, 0,200, 120), xtpPaneDockLeft);
  130. pwndPane1->SetMinTrackSize(CSize(177, 0));
  131. // Set the icons for the docking pane tabs.
  132. int nIDIcons[] = {IDR_PANE_OPTIONS, IDR_PANE_PROPERTIES};
  133. m_paneManager.SetIcons(IDB_BITMAP_ICONS, nIDIcons,
  134. _countof(nIDIcons), RGB(0, 255, 0));
  135. CXTPPropExchangeXMLNode px(TRUE, 0, _T("Settings"));
  136. if (px.LoadFromFile(m_strIniFileName))
  137. {
  138. CXTPPropExchangeSection pxCommandBars(px.GetSection(_T("CommandBars")));
  139. XTP_COMMANDBARS_PROPEXCHANGE_PARAM param; param.bSerializeControls = TRUE;
  140. GetCommandBars()->DoPropExchange(&pxCommandBars, &param);
  141. CXTPPropExchangeSection pxDockingPane(px.GetSection(_T("DockingPane")));
  142. CXTPDockingPaneLayout layoutNormal(&m_paneManager);
  143. if (layoutNormal.DoPropExchange(&pxDockingPane))
  144. {
  145. m_paneManager.SetLayout(&layoutNormal);
  146. }
  147. CXTPPropExchangeSection pxOptions(px.GetSection(_T("Options")));
  148. GetSearchOptions()->DoPropExchange(&pxOptions);
  149. }
  150. return 0;
  151. }
  152. void CMainFrame::OnClose()
  153. {
  154. if (m_pSearchThread)
  155. {
  156. m_pSearchThread->m_bCancel = TRUE;
  157. WaitForSingleObject(m_pSearchThread->m_hThread, INFINITE);
  158. m_pSearchThread = NULL;
  159. }
  160. CXTPPropExchangeXMLNode px(FALSE, 0, _T("Settings"));
  161. if (px.OnBeforeExchange()) 
  162. {
  163. CXTPPropExchangeSection pxCommandBars(px.GetSection(_T("CommandBars")));
  164. XTP_COMMANDBARS_PROPEXCHANGE_PARAM param; param.bSerializeControls = TRUE; param.bSaveOriginalControls = FALSE;
  165. GetCommandBars()->DoPropExchange(&pxCommandBars, &param);
  166. CXTPPropExchangeSection pxDockingPane(px.GetSection(_T("DockingPane")));
  167. CXTPDockingPaneLayout layoutNormal(&m_paneManager);
  168. m_paneManager.GetLayout(&layoutNormal);
  169. layoutNormal.DoPropExchange(&pxDockingPane);
  170. CXTPPropExchangeSection pxOptions(px.GetSection(_T("Options")));
  171. GetSearchOptions()->DoPropExchange(&pxOptions);
  172. px.SaveToFile(m_strIniFileName);
  173. }
  174. CFrameWnd::OnClose();
  175. }
  176. void CMainFrame::OnCustomize()
  177. {
  178. // Get a pointer to the command bars object.
  179. CXTPCommandBars* pCommandBars = GetCommandBars();
  180. if(pCommandBars != NULL)
  181. {
  182. // Instanciate the customize dialog object.
  183. CXTPCustomizeSheet dlg(pCommandBars);
  184. // Add the options page to the customize dialog.
  185. CXTPCustomizeOptionsPage pageOptions(&dlg);
  186. dlg.AddPage(&pageOptions);
  187. // Add the commands page to the customize dialog.
  188. CXTPCustomizeCommandsPage* pCommands = dlg.GetCommandsPage();
  189. pCommands->AddCategories(IDR_MAINFRAME);
  190. // Use the command bar manager to initialize the
  191. // customize dialog.
  192. pCommands->InsertAllCommandsCategory();
  193. pCommands->InsertBuiltInMenus(IDR_MAINFRAME);
  194. pCommands->InsertNewMenuCategory();
  195. // Dispaly the dialog.
  196. dlg.DoModal();
  197. }
  198. }
  199. LRESULT CMainFrame::OnDockingPaneNotify(WPARAM wParam, LPARAM lParam)
  200. {
  201. if (wParam == XTP_DPN_SHOWWINDOW)
  202. {
  203. CXTPDockingPane* pPane = (CXTPDockingPane*)lParam;
  204. if (!pPane->IsValid())
  205. {
  206. switch (pPane->GetID())
  207. {
  208. case IDR_PANE_OPTIONS:
  209. {
  210. CWnd* pView = pPane->AttachView(this, RUNTIME_CLASS(CSearchOptionsView));
  211. if (pView && ::IsWindow(pView->m_hWnd))
  212. {
  213. pView->SendMessage(WM_INITIALUPDATE);
  214. }
  215. break;
  216. }
  217. }
  218. }
  219. return TRUE;
  220. }
  221. return FALSE;
  222. }
  223. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  224. {
  225. if( !CFrameWnd::PreCreateWindow(cs) )
  226. return FALSE;
  227. cs.lpszClass = _T("XTPMainFrame");
  228. CXTPDrawHelpers::RegisterWndClass(AfxGetInstanceHandle(), cs.lpszClass, 
  229. CS_DBLCLKS, AfxGetApp()->LoadIcon(IDR_MAINFRAME));
  230. cs.style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
  231. return TRUE;
  232. }
  233. /////////////////////////////////////////////////////////////////////////////
  234. // CMainFrame diagnostics
  235. #ifdef _DEBUG
  236. void CMainFrame::AssertValid() const
  237. {
  238. CFrameWnd::AssertValid();
  239. }
  240. void CMainFrame::Dump(CDumpContext& dc) const
  241. {
  242. CFrameWnd::Dump(dc);
  243. }
  244. #endif //_DEBUG
  245. /////////////////////////////////////////////////////////////////////////////
  246. // CMainFrame message handlers
  247. void AddComboHistory(CComboBox& cmb, CString strText, CStringList& lstHistory)
  248. {
  249. if (strText.IsEmpty())
  250. return;
  251. int nIndex = cmb.FindString(-1, strText);
  252. if (nIndex == -1)
  253. {
  254. cmb.InsertString(0, strText);
  255. lstHistory.AddHead(strText);
  256. }
  257. }
  258. void CMainFrame::OnFindDone()
  259. {
  260. if (m_nTimer != NULL)
  261. {
  262. KillTimer(TID_REFRESH);
  263. m_nTimer = 0;
  264. }
  265. m_pSearchThread = NULL;
  266. CSearchOptionsView* pOptionsView = GetSearchOptionsView();
  267. pOptionsView->SetSearchMode(FALSE);
  268. CXTPReportView* pView = DYNAMIC_DOWNCAST(CXTPReportView, GetDescendantWindow(AFX_IDW_PANE_FIRST));
  269. ASSERT(pView);
  270. if (!pView)
  271. return;
  272. pOptionsView->GetDlgItem(IDC_BUTTON_REPLACEALL)->EnableWindow(pView->GetReportCtrl().GetRecords()->GetCount() > 0);
  273. pView->GetReportCtrl().Populate();
  274. }
  275. void CMainFrame::OnCancelFind()
  276. {
  277. if (m_nTimer != NULL)
  278. {
  279. KillTimer(TID_REFRESH);
  280. m_nTimer = 0;
  281. }
  282. ASSERT(m_pSearchThread != NULL);
  283. m_pSearchThread->m_bCancel = TRUE;
  284. m_pSearchThread = NULL;
  285. CSearchOptionsView* pOptionsView = GetSearchOptionsView();
  286. pOptionsView->GetDlgItem(IDC_BUTTON_REPLACEALL)->EnableWindow(FALSE);
  287. pOptionsView->SetSearchMode(FALSE);
  288. CXTPReportView* pView = DYNAMIC_DOWNCAST(CXTPReportView, GetDescendantWindow(AFX_IDW_PANE_FIRST));
  289. ASSERT(pView);
  290. if (!pView)
  291. return;
  292. pView->GetReportCtrl().Populate();
  293. SetMessageText(_T("Canceled"));
  294. }
  295. void CMainFrame::DelayPopulate()
  296. {
  297. if (m_nTimer == 0)
  298. {
  299. m_nTimer = SetTimer(TID_REFRESH, 100, NULL);
  300. }
  301. }
  302. void CMainFrame::OnTimer(UINT_PTR nIDEvent)
  303. {
  304. CFrameWnd::OnTimer(nIDEvent);
  305. if ((nIDEvent == TID_REFRESH) && (m_nTimer != 0))
  306. {
  307. CXTPReportView* pView = DYNAMIC_DOWNCAST(CXTPReportView, GetDescendantWindow(AFX_IDW_PANE_FIRST));
  308. ASSERT(pView);
  309. if (!pView)
  310. return;
  311. pView->GetReportCtrl().Populate();
  312. KillTimer(TID_REFRESH);
  313. m_nTimer = 0; 
  314. }
  315. }
  316. void CMainFrame::OnFindAll()
  317. {
  318. CSearchOptions* pOptions = GetSearchOptions();
  319. ASSERT(pOptions);
  320. CXTPReportView* pView = DYNAMIC_DOWNCAST(CXTPReportView, GetDescendantWindow(AFX_IDW_PANE_FIRST));
  321. ASSERT(pView);
  322. if (!pView)
  323. return;
  324. pView->GetReportCtrl().GetRecords()->RemoveAll();
  325. pView->GetReportCtrl().Populate();
  326. //////////////////////////////////////////////////////////////////////////
  327. m_pSearchThread = (CSearchThread*)AfxBeginThread(RUNTIME_CLASS(CSearchThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
  328. m_pSearchThread->m_pMainFrame = this;
  329. m_pSearchThread->m_pReportControl = &pView->GetReportCtrl();
  330. m_pSearchThread->ResumeThread();
  331. CSearchOptionsView* pOptionsView = GetSearchOptionsView();
  332. pOptionsView->SetSearchMode(TRUE);
  333. AddComboHistory(pOptionsView->m_cmbFind, pOptions->m_strFind, pOptions->m_lstFindHistory);
  334. AddComboHistory(pOptionsView->m_cmbReplace, pOptions->m_strReplace, pOptions->m_lstReplaceHistory);
  335. AddComboHistory(pOptionsView->m_cmbPath, pOptions->m_strPath, pOptions->m_lstPathHistory);
  336. AddComboHistory(pOptionsView->m_cmbFileTypes, pOptions->m_strFileTypes, pOptions->m_lstFileTypesHistory);
  337. }
  338. void CMainFrame::OnReplaceAll()
  339. {
  340. USES_CONVERSION;
  341. CXTPReportView* pView = DYNAMIC_DOWNCAST(CXTPReportView, GetDescendantWindow(AFX_IDW_PANE_FIRST));
  342. ASSERT(pView);
  343. if (!pView)
  344. return;
  345. CXTPReportRecords* pRecords = pView->GetReportCtrl().GetRecords();
  346. if (!pRecords)
  347. return;
  348. int nFiles = 0;
  349. int nChanges = 0;
  350. int i;
  351. for (i = 0; i < pRecords->GetCount(); i++)
  352. {
  353. CString strPath = ((CGrepRecord*)pRecords->GetAt(i))->m_strPath;
  354. BOOL bChanged = FALSE;
  355. int nSamePathCount = 0;
  356. while (i + nSamePathCount < pRecords->GetCount() && ((CGrepRecord*)pRecords->GetAt(i + nSamePathCount))->m_strPath ==
  357. strPath)
  358. {
  359. CGrepRecord* pRecord = ((CGrepRecord*)pRecords->GetAt(i + nSamePathCount));
  360. nSamePathCount++;
  361. if (pRecord->IsChanged() && pRecord->IsChecked())
  362. {
  363. bChanged = TRUE;
  364. nChanges++;
  365. }
  366. }
  367. if (!bChanged)
  368. {
  369. i += nSamePathCount - 1;
  370. continue;
  371. }
  372. i += nSamePathCount - 1;
  373. nFiles++;
  374. }
  375. if (nFiles == 0)
  376. {
  377. AfxMessageBox(_T("There are no selected records"));
  378. return;
  379. }
  380. CString str;
  381. str.Format(_T("Are you sure you want make %i replacement(s) in %i file(s)?"), nChanges, nFiles);
  382. if (AfxMessageBox(str, MB_YESNO) != IDYES)
  383. return;
  384. for (i = 0; i < pRecords->GetCount(); i++)
  385. {
  386. CString strPath = ((CGrepRecord*)pRecords->GetAt(i))->m_strPath;
  387. int nSamePathCount = 0;
  388. BOOL bChanged = FALSE;
  389. while (i + nSamePathCount < pRecords->GetCount() && ((CGrepRecord*)pRecords->GetAt(i + nSamePathCount))->m_strPath ==
  390. strPath)
  391. {
  392. CGrepRecord* pRecord = ((CGrepRecord*)pRecords->GetAt(i + nSamePathCount));
  393. nSamePathCount++;
  394. if (pRecord->IsChanged() && pRecord->IsChecked())
  395. {
  396. bChanged = TRUE;
  397. }
  398. }
  399. CFile file;
  400. if (!bChanged || !file.Open(strPath, CFile::modeRead))
  401. {
  402. i += nSamePathCount - 1;
  403. continue;
  404. }
  405. DWORD dwCount = (DWORD)file.GetLength();
  406. char* lpszFileBuffer = new char[dwCount];
  407. file.Read(lpszFileBuffer, dwCount);
  408. file.Close();
  409. //CFile::Rename(pRecord->m_strPath, pRecord->m_strPath + _T(".bak"));
  410. if (!file.Open(strPath, CFile::modeWrite|CFile::modeCreate))
  411. {
  412. i += nSamePathCount - 1;
  413. delete[] lpszFileBuffer;
  414. continue;
  415. }
  416. long nTail = 0;
  417. int k = 0;
  418. for  (int j = 0; j < nSamePathCount; j++)
  419. {
  420. CGrepRecord* pRecord = ((CGrepRecord*)pRecords->GetAt(i + j - k ));
  421. if (pRecord->IsChecked())
  422. {
  423. file.Write(lpszFileBuffer + nTail, pRecord->m_nIndex - nTail);
  424. if (!pRecord->m_strReplace.IsEmpty())
  425. {
  426. file.Write(T2A((LPTSTR)(LPCTSTR)(pRecord->m_strReplace)), pRecord->m_strReplace.GetLength());
  427. }
  428. nTail = pRecord->m_nIndex + pRecord->m_nLength;
  429. pRecords->RemoveAt(i + j - k);
  430. k++;
  431. }
  432. }
  433. i += nSamePathCount - k - 1;
  434. file.Write(lpszFileBuffer + nTail, dwCount - nTail);
  435. file.Close();
  436. delete[] lpszFileBuffer;
  437. }
  438. pView->GetReportCtrl().Populate();
  439. }
  440. CSearchOptionsView* CMainFrame::GetSearchOptionsView()
  441. {
  442. CXTPDockingPane* pPane = m_paneManager.FindPane(IDR_PANE_OPTIONS);
  443. if (!pPane)
  444. return 0;
  445. CWnd* pWnd = pPane->GetChild();
  446. if (!pWnd)
  447. return 0;
  448. return (CSearchOptionsView*)pWnd->GetDescendantWindow(AFX_IDW_PANE_FIRST);
  449. }
  450. void CMainFrame::OnSetPreviewMode(BOOL bPreview, CPrintPreviewState* pState)
  451. {
  452. // Toggle CommandBars
  453. GetCommandBars()->OnSetPreviewMode(bPreview);
  454. // Toggle Docking Panes.
  455. m_paneManager.OnSetPreviewMode(bPreview);
  456. CFrameWnd::OnSetPreviewMode(bPreview, pState);
  457. }
  458. void CMainFrame::OnViewOptionspane() 
  459. {
  460. m_paneManager.ShowPane(IDR_PANE_OPTIONS);
  461. }