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

对话框与窗口

开发平台:

Visual C++

  1. // MDITextEditorView.cpp : implementation of the CMDITextEditorView class
  2. //
  3. #include "stdafx.h"
  4. #include "MDITextEditor.h"
  5. #include "MDITextEditorDoc.h"
  6. #include "MDITextEditorView.h"
  7. #include "MainFrm.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. typedef enum SyntaxEditUpdateViewHint
  14. {
  15. xtpHintWhiteSpace = (xtpEditHintLast + 1),
  16. };
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CMDITextEditorView
  19. IMPLEMENT_DYNCREATE(CMDITextEditorView, CViewBase)
  20. BEGIN_MESSAGE_MAP(CMDITextEditorView, CViewBase)
  21. //{{AFX_MSG_MAP(CMDITextEditorView)
  22. ON_WM_CONTEXTMENU()
  23. ON_COMMAND(ID_EDIT_FIND_SILENT, OnEditFindSilent)
  24. ON_COMMAND(ID_EDIT_TABIFY, OnEditTabify)
  25. ON_COMMAND(ID_EDIT_UNTABIFY, OnEditUnTabify)
  26. ON_COMMAND(ID_EDIT_UPPERCASE, OnEditUppercase)
  27. ON_COMMAND(ID_EDIT_LOWERCASE, OnEditLowercase)
  28. ON_COMMAND(ID_EDIT_WHITESPACE, OnEditWhiteSpace)
  29. ON_UPDATE_COMMAND_UI(ID_EDIT_WHITESPACE, OnUpdateEditWhiteSpace)
  30. ON_COMMAND(ID_EDIT_COLLAPSEALL, OnEditCollapseAll)
  31. ON_COMMAND(ID_EDIT_EXPANDALL, OnEditExpandAll)
  32. ON_COMMAND(ID_TOOLS_BOOKMARK, OnToolsBookmark)
  33. ON_COMMAND(ID_TOOLS_BREAKPOINT, OnToolsBreakpoint)
  34. ON_COMMAND(ID_TOOLS_NEXT_BOOKMARK, OnToolsNextBookmark)
  35. ON_UPDATE_COMMAND_UI(ID_TOOLS_NEXT_BOOKMARK, OnUpdateToolsNextBookmark)
  36. ON_COMMAND(ID_TOOLS_PREV_BOOKMARK, OnToolsPrevBookmark)
  37. ON_UPDATE_COMMAND_UI(ID_TOOLS_PREV_BOOKMARK, OnUpdateToolsPrevBookmark)
  38. ON_COMMAND(ID_TOOLS_OPTIONS, OnToolsOptions)
  39. ON_COMMAND(ID_FILE_GOTO, OnFileGoTo)
  40. ON_COMMAND(ID_FILE_PAGE_SETUP, CViewBase::OnFilePageSetup)
  41. ON_COMMAND(ID_EDIT_READONLY, OnReadOnly)
  42. ON_UPDATE_COMMAND_UI(ID_EDIT_READONLY, OnUpdateReadOnly)
  43. //}}AFX_MSG_MAP
  44. // Standard printing commands
  45. ON_COMMAND(ID_FILE_PRINT, CViewBase::OnFilePrint)
  46. ON_COMMAND(ID_FILE_PRINT_DIRECT, CViewBase::OnFilePrint)
  47. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CViewBase::OnFilePrintPreview)
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CMDITextEditorView construction/destruction
  51. CMDITextEditorView::CMDITextEditorView()
  52. : m_dwLastUpdate(0)
  53. , m_dwParserStart(0)
  54. {
  55. m_rcMargin.left   = 25;
  56. m_rcMargin.right  = 25;
  57. m_rcMargin.top    = 50;
  58. m_rcMargin.bottom = 50;
  59. GetEditCtrl().SetConfigFile(CXTPSyntaxEditCtrl::GetModulePath() + _T("EditConfig\SyntaxEdit.ini"));
  60. // Add _tsetlocale call to allow some text functions (like isleadbyte) works correctly
  61. // A good place for this is CMainFrame implementation (see CMainFrame constructor).
  62. // EXAMPLE: 
  63. // _tsetlocale(LC_ALL, _T(""));
  64. }
  65. CMDITextEditorView::~CMDITextEditorView()
  66. {
  67. }
  68. BOOL CMDITextEditorView::PreCreateWindow(CREATESTRUCT& cs)
  69. {
  70. // TODO: Modify the Window class or styles here by modifying
  71. //  the CREATESTRUCT cs
  72. return CViewBase::PreCreateWindow(cs);
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CMDITextEditorView printing
  76. BOOL CMDITextEditorView::OnPreparePrinting(CPrintInfo* pInfo)
  77. {
  78. // default preparation
  79. return CViewBase::OnPreparePrinting(pInfo);
  80. }
  81. void CMDITextEditorView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
  82. {
  83. CViewBase::OnBeginPrinting(pDC, pInfo);
  84. }
  85. void CMDITextEditorView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
  86. {
  87. CViewBase::OnEndPrinting(pDC, pInfo);
  88. }
  89. void CMDITextEditorView::OnPrintHeader(CDC* pDC, CPrintInfo* pInfo)
  90. {
  91. CXTPFontDC fontDC(pDC, GetEditCtrl().GetPaintManager()->GetFont(),
  92. ::GetSysColor(COLOR_WINDOWTEXT));
  93. CPoint ptFrom;
  94. ptFrom.x = m_rcMargin.left;
  95. ptFrom.y = m_rcMargin.top - 5;
  96. pDC->MoveTo(ptFrom);
  97. CPoint ptTo;
  98. ptTo.x = pInfo->m_rectDraw.right - m_rcMargin.right;
  99. ptTo.y = m_rcMargin.top - 5;
  100. pDC->LineTo(ptTo);
  101. CString csPathName = GetDocument()->GetPathName();
  102. CString csFileName = csPathName.Right(csPathName.GetLength()-(csPathName.ReverseFind('\')+1));
  103. if (csFileName.IsEmpty())
  104.     {
  105. csFileName = GetDocument()->GetTitle();
  106. csFileName.Remove('*');
  107.     }
  108. CSize sizeText = pDC->GetTextExtent(csFileName);
  109. int x = m_rcMargin.left;
  110. int y = m_rcMargin.top - 10 - sizeText.cy;
  111. pDC->TextOut(x, y, csFileName);
  112. CTime tmCurrent = CTime::GetCurrentTime();
  113. CString csTime = tmCurrent.Format(_T("%m-%d-%Y %I:%M%p"));
  114. sizeText = pDC->GetTextExtent(csTime);
  115. pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
  116. pDC->TextOut((ptTo.x - sizeText.cx), y, csTime);
  117. }
  118. void CMDITextEditorView::OnPrintFooter(CDC* pDC, CPrintInfo* pInfo)
  119. {
  120. CXTPFontDC fontDC(pDC, GetEditCtrl().GetPaintManager()->GetFont(),
  121. ::GetSysColor(COLOR_WINDOWTEXT));
  122. int iBottomPos = (pInfo->m_rectDraw.Height() - m_rcMargin.bottom + 5);
  123. CPoint ptFrom;
  124. ptFrom.x = m_rcMargin.left;
  125. ptFrom.y = iBottomPos;
  126. pDC->MoveTo(ptFrom);
  127. CPoint ptTo;
  128. ptTo.x = pInfo->m_rectDraw.right - m_rcMargin.right;
  129. ptTo.y = iBottomPos;
  130. pDC->LineTo(ptTo);
  131. CString csPageNo;
  132. csPageNo.Format(_T("Page %d of %d"), pInfo->m_nCurPage, pInfo->GetMaxPage());
  133. pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
  134. pDC->TextOut(m_rcMargin.left, iBottomPos + 5, csPageNo);
  135. }
  136. void CMDITextEditorView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
  137. {
  138. // Print header in application-customized mode
  139. // OnPrintHeader(pDC, pInfo);
  140. // CRect rcDraw_orig = pInfo->m_rectDraw;
  141. // pInfo->m_rectDraw.top += m_rcMargin.top;
  142. // pInfo->m_rectDraw.bottom -= m_rcMargin.bottom;
  143. // Print body
  144. CViewBase::OnPrint(pDC, pInfo);
  145. // pInfo->m_rectDraw = rcDraw_orig;
  146. // Print footer
  147. // OnPrintFooter(pDC, pInfo);
  148. }
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CMDITextEditorView diagnostics
  151. #ifdef _DEBUG
  152. void CMDITextEditorView::AssertValid() const
  153. {
  154. CViewBase::AssertValid();
  155. }
  156. void CMDITextEditorView::Dump(CDumpContext& dc) const
  157. {
  158. CViewBase::Dump(dc);
  159. }
  160. CMDITextEditorDoc* CMDITextEditorView::GetDocument() // non-debug version is inline
  161. {
  162. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDITextEditorDoc)));
  163. return (CMDITextEditorDoc*)m_pDocument;
  164. }
  165. #endif //_DEBUG
  166. /////////////////////////////////////////////////////////////////////////////
  167. // CMDITextEditorView message handlers
  168. BOOL CMDITextEditorView::OnRowColChanged(NMHDR* pNMHDR, LRESULT* pResult)
  169. {
  170. if (!CViewBase::OnRowColChanged(pNMHDR, pResult))
  171. return FALSE;
  172. XTP_EDIT_NMHDR_ROWCOLCHANGED* pNMHDR_RCC = (XTP_EDIT_NMHDR_ROWCOLCHANGED*)pNMHDR;
  173. if (!pNMHDR_RCC)
  174. return FALSE;
  175. theApp.m_pMainFrame->SetRowCol(pNMHDR_RCC->nRow, pNMHDR_RCC->nCol);
  176. *pResult = 0;
  177. return TRUE;
  178. }
  179. BOOL CMDITextEditorView::OnParseEvent(NMHDR* pNMHDR, LRESULT* pResult)
  180. {
  181. if (!CViewBase::OnParseEvent(pNMHDR, pResult))
  182. return FALSE;
  183. XTP_EDIT_NMHDR_PARSEEVENT* pNMParseEvent = (XTP_EDIT_NMHDR_PARSEEVENT*)pNMHDR;
  184. if (!pNMParseEvent)
  185. return FALSE;
  186. CDocument* pDoc = GetDocument();
  187. if (!pDoc)
  188. return FALSE;
  189. const int cnUpdateTimeOut = 500;
  190. CString strMsg;
  191. BOOL bUpdate = TRUE;
  192. if (pNMParseEvent->code == xtpEditOnTextBlockParsed) 
  193. {
  194. DWORD dwTime = ::GetTickCount();
  195. bUpdate = labs(dwTime - m_dwLastUpdate) >= cnUpdateTimeOut;
  196. CXTPSyntaxEditLexTextBlock* pTBended = (CXTPSyntaxEditLexTextBlock*)pNMParseEvent->wParam;
  197. if (pTBended && bUpdate)
  198. {
  199. m_dwLastUpdate = dwTime;
  200. DWORD dwTime1 = GetTickCount();//DEBUG
  201. double dTime = labs(dwTime1-m_dwParserStart)/1000.0;
  202. strMsg.Format(_T(" Parsing time(%.1f sec). Line(%d)."), dTime, pTBended->m_PosEndLC.nLine);
  203. // strMsg.Format(_T(" Parsing time(%.3f sec). Last parsed block: (%d,%d - %d,%d)::%s"), 
  204. // dTime,
  205. // pTBended->m_PosStartLC.nLine, pTBended->m_PosStartLC.nCol, 
  206. // pTBended->m_PosEndLC.nLine, pTBended->m_PosEndLC.nCol, 
  207. // pTBended->m_ptrLexClass ? pTBended->m_ptrLexClass->GetClassName() 
  208. // : _T("?<NULL>") );
  209. }
  210. }
  211. else if (pNMParseEvent->code == xtpEditOnParserStarted) 
  212. {
  213. strMsg = _T("*** Parser Started");
  214. m_dwParserStart = GetTickCount();
  215. }
  216. else if (pNMParseEvent->code == xtpEditOnParserEnded) 
  217. {
  218. if(!pNMParseEvent->wParam || (pNMParseEvent->wParam & xtpEditLPR_RunFinished)) 
  219. {
  220. DWORD dwTime1 = GetTickCount();
  221. double dTime = labs(dwTime1-m_dwParserStart)/1000.0;
  222. strMsg.Format(_T(" Parsing time(%.3f sec)."), dTime);
  223. }
  224. }
  225. if (bUpdate)
  226. {
  227. CString strTitle = pDoc->GetTitle();
  228. int nFIdx = strTitle.Find(_T(" :.: "));
  229. if(nFIdx >= 0) {
  230. strTitle = strTitle.Left(nFIdx);
  231. nFIdx = strTitle.Find(_T("*"));
  232. if(nFIdx >= 0) {
  233. strTitle = strTitle.Left(nFIdx);
  234. }
  235. strTitle.TrimRight();
  236. }
  237. if (pDoc->IsModified()) {
  238. strTitle += _T(" * ");
  239. }
  240. if(strMsg.GetLength() > 0) {
  241. strTitle += _T(" :.: ");
  242. strTitle += strMsg;
  243. }
  244.         
  245. pDoc->SetTitle(strTitle);
  246. }
  247. *pResult = 0;
  248. return TRUE;
  249. }
  250. void CMDITextEditorView::OnContextMenu(CWnd* /*pWnd*/, CPoint point) 
  251. {
  252. if (point.x == -1 && point.y == -1)
  253. {
  254. // keystroke invocation
  255. CRect rect;
  256. GetClientRect(rect);
  257. ClientToScreen(rect);
  258. point = rect.TopLeft();
  259. point.Offset(5, 5);
  260. }
  261. CMenu menu;
  262. VERIFY(menu.LoadMenu(IDR_MDITEXTYPE));
  263. CMenu* pPopup = menu.GetSubMenu(1);
  264. ASSERT(pPopup != NULL);
  265. CWnd* pWndPopupOwner = this;
  266. while (pWndPopupOwner->GetStyle() & WS_CHILD)
  267. pWndPopupOwner = pWndPopupOwner->GetParent();
  268. ((CXTPMDIFrameWnd*)AfxGetMainWnd())->GetCommandBars()->
  269. TrackPopupMenuEx(pPopup, TPM_RIGHTBUTTON, point.x, point.y);
  270. }
  271. void CMDITextEditorView::OnEditFindSilent() 
  272. {
  273. if (!GetFindReplaceDlg())
  274. return;
  275. CString csSelText;
  276. GetEditCtrl().GetSelectionText(csSelText);
  277. if (csSelText.IsEmpty()) 
  278. {
  279. CPoint pt(GetCaretPos());
  280. pt.y += 2;
  281. GetEditCtrl().SelectWord(pt);
  282. GetEditCtrl().GetSelectionText(csSelText);
  283. }
  284. csSelText = csSelText.SpanExcluding(_T("rn"));
  285. csSelText.Replace(_T("t"), _T("    "));
  286. if (csSelText.IsEmpty()) 
  287. {
  288. csSelText = _T(" ");
  289. }
  290. else
  291. {
  292. GetFindReplaceDlg()->m_csFindText = csSelText;
  293. }
  294.     
  295. BOOL bShiftKey  = (::GetKeyState(VK_SHIFT) & KF_UP) != 0;
  296. if (GetEditCtrl().Find(csSelText,
  297. GetFindReplaceDlg()->m_bMatchWholeWord,
  298. GetFindReplaceDlg()->m_bMatchCase, !bShiftKey))
  299. {
  300. SetDirty();
  301. }
  302. }
  303. void CMDITextEditorView::OnEditTabify() 
  304. {
  305. GetEditCtrl().TabifySelection();
  306. }
  307. void CMDITextEditorView::OnEditUnTabify() 
  308. {
  309. GetEditCtrl().UnTabifySelection();
  310. }
  311. void CMDITextEditorView::OnEditUppercase() 
  312. {
  313. GetEditCtrl().UppercaseSelection();
  314. }
  315. void CMDITextEditorView::OnEditLowercase() 
  316. {
  317. GetEditCtrl().LowercaseSelection();
  318. }
  319. void CMDITextEditorView::OnEditWhiteSpace() 
  320. {
  321. // toggle whitespace.
  322. GetEditCtrl().EnableWhiteSpace(
  323. !GetEditCtrl().IsEnabledWhiteSpace());
  324. CDocument* pDoc = GetDocument();
  325. if (pDoc)
  326. {
  327. pDoc->UpdateAllViews(this, xtpHintWhiteSpace,
  328. GetEditCtrl().IsEnabledWhiteSpace() ? (CObject*)1 : NULL);
  329. }
  330. }
  331. void CMDITextEditorView::OnUpdateEditWhiteSpace(CCmdUI* pCmdUI) 
  332. {
  333. pCmdUI->SetCheck(GetEditCtrl().IsEnabledWhiteSpace());
  334. }
  335. void CMDITextEditorView::OnEditCollapseAll() 
  336. {
  337. GetEditCtrl().CollapseAll();
  338. }
  339. void CMDITextEditorView::OnEditExpandAll() 
  340. {
  341. GetEditCtrl().ExpandAll();
  342. }
  343. void CMDITextEditorView::OnToolsBookmark() 
  344. {
  345. int iRow = GetEditCtrl().GetCurRow();
  346. GetEditCtrl().AddRemoveBookmark(iRow);
  347. UpdateAllViews();
  348. }
  349. void CMDITextEditorView::OnToolsBreakpoint() 
  350. {
  351. int iRow = GetEditCtrl().GetCurRow();
  352. GetEditCtrl().AddRemoveBreakPoint(iRow);
  353. UpdateAllViews();
  354. }
  355. void CMDITextEditorView::OnToolsNextBookmark() 
  356. {
  357. GetEditCtrl().NextBookmark();
  358. }
  359. void CMDITextEditorView::OnUpdateToolsNextBookmark(CCmdUI* pCmdUI) 
  360. {
  361. pCmdUI->Enable(GetEditCtrl().HasBookmarks());
  362. }
  363. void CMDITextEditorView::OnToolsPrevBookmark() 
  364. {
  365. GetEditCtrl().PrevBookmark();
  366. }
  367. void CMDITextEditorView::OnUpdateToolsPrevBookmark(CCmdUI* pCmdUI) 
  368. {
  369. pCmdUI->Enable(GetEditCtrl().HasBookmarks());
  370. }
  371. void CMDITextEditorView::OnToolsOptions() 
  372. {
  373. // Create dialog object
  374. CXTPSyntaxEditPropertiesDlg dlg(this);
  375. // Instantiate dialog
  376. if (dlg.DoModal() == IDOK)
  377. {
  378. // Copy settings to remaining views.
  379. theApp.UpdateAllViews(this);
  380. }
  381. }
  382. void CMDITextEditorView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  383. {
  384. CViewBase::OnUpdate(pSender, lHint, pHint);
  385. if(pSender == this) {
  386. return;
  387. }
  388. switch (lHint)
  389. {
  390. case xtpHintWhiteSpace:
  391. GetEditCtrl().EnableWhiteSpace(pHint != NULL);
  392. break;
  393. case xtpEditHintInitView:
  394. GetEditCtrl().SetOverwriteMode(theApp.m_bInsertKey);
  395. break;
  396. }
  397. }
  398. void CMDITextEditorView::OnFileGoTo() 
  399. {
  400. m_dlgGoTo.ShowDialog(&GetEditCtrl(),
  401. FALSE/*select line*/, TRUE/*hide after search*/);
  402. }
  403. void CMDITextEditorView::OnReadOnly()
  404. {
  405. GetEditCtrl().SetReadOnly(!GetEditCtrl().IsReadOnly());
  406. }
  407. void CMDITextEditorView::OnUpdateReadOnly(CCmdUI* pCmdUI)
  408. {
  409. pCmdUI->SetCheck(GetEditCtrl().IsReadOnly() ? 1 : 0);
  410. }