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

对话框与窗口

开发平台:

Visual C++

  1. // MarkupPadEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "MarkupPadEdit.h"
  6. #include "MarkupPadDoc.h"
  7. #include "MarkupPadView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CMarkupPadEdit
  15. IMPLEMENT_DYNCREATE(CMarkupPadEdit, CEditView)
  16. CMarkupPadEdit::CMarkupPadEdit()
  17. {
  18. m_hInstance = LoadLibraryA("RICHED20.DLL");
  19. if (m_hInstance)
  20. {
  21. #ifdef _UNICODE
  22. m_strClass = _T("RichEdit20W");
  23. #else
  24. m_strClass = _T("RichEdit20A");
  25. #endif
  26. }
  27. else
  28. {
  29. m_hInstance = LoadLibraryA("RICHED32.DLL");
  30. m_strClass = _T("RICHEDIT");
  31. }
  32. NONCLIENTMETRICS ncm = { sizeof(NONCLIENTMETRICS) };
  33. VERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
  34. STRCPY_S(ncm.lfMessageFont.lfFaceName, LF_FACESIZE, _T("Courier New"));
  35. ncm.lfMessageFont.lfHeight = -14;
  36. m_fnt.CreateFontIndirect(&ncm.lfMessageFont);
  37. m_bFirstDoc = TRUE;
  38. m_bError = FALSE;
  39. }
  40. CMarkupPadEdit::~CMarkupPadEdit()
  41. {
  42. }
  43. BEGIN_MESSAGE_MAP(CMarkupPadEdit, CEditView)
  44. //{{AFX_MSG_MAP(CMarkupPadEdit)
  45. ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
  46. ON_WM_CONTEXTMENU()
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CMarkupPadEdit diagnostics
  51. #ifdef _DEBUG
  52. void CMarkupPadEdit::AssertValid() const
  53. {
  54. CEditView::AssertValid();
  55. }
  56. void CMarkupPadEdit::Dump(CDumpContext& dc) const
  57. {
  58. CEditView::Dump(dc);
  59. }
  60. #endif //_DEBUG
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CMarkupPadEdit message handlers
  63. void CMarkupPadEdit::OnInitialUpdate() 
  64. {
  65. CEditView::OnInitialUpdate();
  66. SetFont(&m_fnt);
  67. SendMessage(EM_SETTEXTMODE, TM_PLAINTEXT | TM_MULTILEVELUNDO);
  68. SendMessage(EM_SETEVENTMASK, 0, ENM_CHANGE);
  69. if (m_bFirstDoc)
  70. {
  71. CFrameWnd* pMainFrame = (CFrameWnd*)AfxGetMainWnd();
  72. if (pMainFrame)
  73. {
  74. CXTPStatusBar* pStatusBar = (CXTPStatusBar*)pMainFrame->GetDlgItem(AFX_IDW_STATUS_BAR);
  75. pStatusBar->GetMarkupContext()->AddHandler(CXTPMarkupHyperlink::m_pClickEvent, CreateMarkupClassDelegate(this, &CMarkupPadEdit::OnStatusBarHyperlinkClick));
  76. }
  77. }
  78. if (GetDocument()->GetPathName().IsEmpty() && m_bFirstDoc)
  79. {
  80. SetWindowText(_T("<Page xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>rn  <TextBlock Cursor="Hand" VerticalAlignment="Center" HorizontalAlignment="Center">Enter Text Here</TextBlock>rn</Page>"));
  81. }
  82. m_bFirstDoc = FALSE;
  83. OnChange();
  84. GetDocument()->SetModifiedFlag(FALSE);
  85. }
  86. LPWSTR CMarkupPadEdit::GetUnicodeText()
  87. {
  88. GETTEXTLENGTHEX gtl =
  89. {
  90. GTL_DEFAULT, 1200
  91. };
  92. int nLen = (int)SendMessage(EM_GETTEXTLENGTHEX, (WPARAM)&gtl);
  93. if (nLen > 0)
  94. {
  95. GETTEXTEX gt =
  96. {
  97. (nLen + 1) * sizeof(WCHAR), GT_DEFAULT, 1200, 0, 0
  98. };
  99. LPWSTR lpBuffer = new WCHAR[nLen + 1];
  100. SendMessage(EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)lpBuffer);
  101. return lpBuffer;
  102. }
  103. return NULL;
  104. }
  105. CString CreateMarkupError(LPCTSTR lpszLastError)
  106. {
  107. LPCTSTR lpszIndex = _tcsstr(lpszLastError, _T(". Line "));
  108. if (lpszIndex == NULL)
  109. return lpszLastError;
  110. ((LPTSTR)lpszIndex)[1] = 0;
  111. CString str;
  112. str.Format(_T("<TextBlock VerticalAlignment='Center'>%s <Hyperlink>%s</Hyperlink></TextBlock>"), lpszLastError, lpszIndex + 2);
  113. ((LPTSTR)lpszIndex)[1] = ' ';
  114. return str;
  115. }
  116. void CMarkupPadEdit::OnChange() 
  117. {
  118. USES_CONVERSION;
  119. CMarkupPadDoc* pDocument = (CMarkupPadDoc*)GetDocument();
  120. POSITION pos = pDocument->GetFirstViewPosition();
  121. CMarkupPadView* pView = (CMarkupPadView*)pDocument->GetNextView(pos);
  122. LPWSTR lpszMarkup = GetUnicodeText();
  123. _int64 nPerfomanceEnd;
  124. _int64 nPerfomanceStart;
  125. QueryPerformanceCounter((LARGE_INTEGER*)&nPerfomanceStart);
  126. CXTPMarkupParser sc;
  127. if (lpszMarkup) sc.SetBuffer(lpszMarkup, lpszMarkup + wcslen(lpszMarkup));
  128. CXTPMarkupBuilder builder(pView);
  129. CXTPMarkupUIElement* pUIElement = builder.Parse(&sc);
  130. if (lpszMarkup)
  131. delete[] lpszMarkup;
  132. QueryPerformanceCounter((LARGE_INTEGER*)&nPerfomanceEnd);
  133. TRACE(_T("Parse   = %i n"), int(nPerfomanceEnd - nPerfomanceStart));
  134. BOOL bError = m_bError;
  135. if (pUIElement)
  136. {
  137. MARKUP_RELEASE(pView->m_pUIElement);
  138. pView->m_pUIElement = pUIElement;
  139. bError = FALSE;
  140. UpdateViews();
  141. GetParentFrame()->SetMessageText(_T("Done."));
  142. }
  143. else
  144. {
  145. bError = TRUE;
  146. GetParentFrame()->SetMessageText(CreateMarkupError(builder.GetLastError()));
  147. }
  148. if (m_bError != bError)
  149. {
  150. m_bError = bError;
  151. CHARFORMAT cf;
  152. ZeroMemory(&cf, sizeof(CHARFORMAT));
  153. cf.cbSize = sizeof(CHARFORMAT);
  154. cf.dwMask = CFM_COLOR;
  155. cf.crTextColor = m_bError ? RGB(255, 0, 0) : 0;
  156. SendMessage(EM_SETCHARFORMAT, 0, (LPARAM)&cf);
  157. }
  158. pDocument->SetModifiedFlag(TRUE);
  159. }
  160. void CMarkupPadEdit::UpdateViews() 
  161. {
  162. CDocument* pDocument = GetDocument();
  163. POSITION pos = pDocument->GetFirstViewPosition();
  164. while (pos)
  165. {
  166. CView* pView = pDocument->GetNextView(pos);
  167. if (pView != this)
  168. {
  169. pView->Invalidate(FALSE);
  170. break;
  171. }
  172. }
  173. }
  174. void CMarkupPadEdit::OnStatusBarHyperlinkClick(CXTPMarkupObject* pSender, CXTPMarkupRoutedEventArgs* pArgs)
  175. {
  176. if (pSender->IsKindOf(MARKUP_TYPE(CXTPMarkupHyperlink)))
  177. {
  178. CXTPMarkupRun* pRun = (CXTPMarkupRun*)((CXTPMarkupHyperlink*)pSender)->GetInlines()->GetInline(0);
  179. CString strText = pRun->GetText();
  180. int nLine = 0, nPosition = 0;
  181. if (SCANF_S(strText, _T("Line %i, position %i"), &nLine, &nPosition) == 2)
  182. {
  183. int nPos = nPosition + (int)SendMessage(EM_LINEINDEX, nLine - 1); 
  184. SendMessage(EM_SETSEL, nPos, nPos);
  185. }
  186. pArgs->SetHandled();
  187. }
  188. }
  189. void CMarkupPadEdit::OnContextMenu(CWnd* pWnd, CPoint point) 
  190. {
  191. CMenu menu;
  192. menu.LoadMenu(IDR_MENU_CONTEXT);
  193. if (point == CPoint(-1, -1))
  194. GetCursorPos(&point);
  195. ((CXTPFrameWnd*)AfxGetMainWnd())->GetCommandBars()->TrackPopupMenuEx(menu.GetSubMenu(0), 0, point.x, point.y, pWnd);
  196. }