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

对话框与窗口

开发平台:

Visual C++

  1. // GallerySampleView.cpp : implementation of the CGallerySampleView class
  2. //
  3. #include "stdafx.h"
  4. #include "GallerySample.h"
  5. #include "GallerySampleDoc.h"
  6. #include "CntrItem.h"
  7. #include "GallerySampleView.h"
  8. #include "MainFrm.h"
  9. #include "GalleryItems.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CGallerySampleView
  17. IMPLEMENT_DYNCREATE(CGallerySampleView, CRichEditView)
  18. BEGIN_MESSAGE_MAP(CGallerySampleView, CRichEditView)
  19. //{{AFX_MSG_MAP(CGallerySampleView)
  20. ON_WM_DESTROY()
  21. ON_COMMAND(ID_FORMAT_FONTCOLOR_AUTOMATIC, OnFormatFontcolorAutomatic)
  22. ON_COMMAND(ID_FORMAT_FONTCOLOR_NOHIGHLIGHT, OnFormatFontcolorNoHighlight)
  23. ON_UPDATE_COMMAND_UI(ID_FORMAT_FONTCOLOR_AUTOMATIC, OnUpdateFormatFontcolorAutomatic)
  24. ON_COMMAND(ID_FORMAT_FONTCOLOR_MORECOLORS, OnFormatFontcolorMorecolors)
  25. //}}AFX_MSG_MAP
  26. // Standard printing commands
  27. ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint)
  29. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
  30. ON_UPDATE_COMMAND_UI(ID_FORMAT_BOLD, OnUpdateCharBold)
  31. ON_COMMAND(ID_FORMAT_BOLD, OnCharBold)
  32. ON_UPDATE_COMMAND_UI(ID_FORMAT_ITALIC, OnUpdateCharItalic)
  33. ON_COMMAND(ID_FORMAT_ITALIC, OnCharItalic)
  34. ON_UPDATE_COMMAND_UI(ID_FORMAT_UNDERLINE, OnUpdateCharUnderline)
  35. ON_COMMAND(ID_FORMAT_UNDERLINE, OnCharUnderline)
  36. ON_COMMAND(ID_TEXT_SUBSCRIPT, OnEmptyCommand)
  37. ON_COMMAND(ID_TEXT_SUPERSCRIPT, OnEmptyCommand)
  38. ON_COMMAND(ID_CHAR_STRIKETHROUGH, OnEmptyCommand)
  39. ON_COMMAND(ID_FONT_CLEAR, OnEmptyCommand)
  40. ON_COMMAND(ID_TEXT_CHANGECASE, OnEmptyCommand)
  41. ON_XTP_EXECUTE(ID_GALLERY_FONTBACKCOLOR, OnGalleryFontBackColor)
  42. ON_UPDATE_COMMAND_UI(ID_GALLERY_FONTBACKCOLOR, OnUpdateGalleryFontBackColor)
  43. ON_XTP_EXECUTE(ID_GALLERY_FONTTEXTCOLOR, OnGalleryFontTextColor)
  44. ON_UPDATE_COMMAND_UI(ID_GALLERY_FONTTEXTCOLOR, OnUpdateGalleryFontTextColor)
  45. ON_UPDATE_COMMAND_UI(ID_FORMAT_FONTCOLOR, OnUpdateButtonText)
  46. ON_COMMAND(ID_FORMAT_FONTCOLOR, OnButtonText)
  47. ON_UPDATE_COMMAND_UI(ID_FORMAT_BACKCOLOR, OnUpdateBack)
  48. ON_COMMAND(ID_FORMAT_BACKCOLOR, OnButtonBack)
  49. ON_UPDATE_COMMAND_UI(ID_GALLERY_FONTFACE, OnUpdateGalleryFontFace)
  50. ON_UPDATE_COMMAND_UI(ID_GALLERY_FONTSIZE, OnUpdateGalleryFontSize)
  51. ON_UPDATE_COMMAND_UI(ID_EDIT_FONT, OnUpdateComboFont)
  52. ON_XTP_EXECUTE(ID_EDIT_FONT, OnEditFont)
  53. ON_UPDATE_COMMAND_UI(ID_EDIT_SIZE, OnUpdateComboSize)
  54. ON_XTP_EXECUTE(ID_EDIT_SIZE, OnEditSize)
  55. ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
  56. ON_XTP_EXECUTE(ID_EDIT_UNDO, OnEditUndo)
  57. END_MESSAGE_MAP()
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CGallerySampleView construction/destruction
  60. CGallerySampleView::CGallerySampleView()
  61. {
  62. m_clrText = COLORREF_NULL;
  63. m_clrBack = COLORREF_NULL;
  64. }
  65. CGallerySampleView::~CGallerySampleView()
  66. {
  67. }
  68. void CGallerySampleView::OnEmptyCommand()
  69. {
  70. }
  71. BOOL CGallerySampleView::PreCreateWindow(CREATESTRUCT& cs)
  72. {
  73. // TODO: Modify the Window class or styles here by modifying
  74. //  the CREATESTRUCT cs
  75. return CRichEditView::PreCreateWindow(cs);
  76. }
  77. void CGallerySampleView::OnInitialUpdate()
  78. {
  79. CRichEditView::OnInitialUpdate();
  80. // Set the printing margins (720 twips = 1/2 inch).
  81. SetMargins(CRect(720, 720, 720, 720));
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CGallerySampleView printing
  85. BOOL CGallerySampleView::OnPreparePrinting(CPrintInfo* pInfo)
  86. {
  87. // default preparation
  88. return DoPreparePrinting(pInfo);
  89. }
  90. void CGallerySampleView::OnDestroy()
  91. {
  92. CRichEditView::OnDestroy();
  93. }
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CGallerySampleView diagnostics
  96. #ifdef _DEBUG
  97. void CGallerySampleView::AssertValid() const
  98. {
  99. CRichEditView::AssertValid();
  100. }
  101. void CGallerySampleView::Dump(CDumpContext& dc) const
  102. {
  103. CRichEditView::Dump(dc);
  104. }
  105. CGallerySampleDoc* CGallerySampleView::GetDocument() // non-debug version is inline
  106. {
  107. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGallerySampleDoc)));
  108. return (CGallerySampleDoc*)m_pDocument;
  109. }
  110. #endif //_DEBUG
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CGallerySampleView message handlers
  113. void CGallerySampleView::OnGalleryFontTextColor(NMHDR* pNMHDR, LRESULT* pResult)
  114. {
  115. NMXTPCONTROL* tagNMCONTROL = (NMXTPCONTROL*)pNMHDR;
  116. CXTPControlGallery* pControl = DYNAMIC_DOWNCAST(CXTPControlGallery, tagNMCONTROL->pControl);
  117. if (pControl)
  118. {
  119. CXTPControlGalleryItem* pItem = pControl->GetItem(pControl->GetSelectedItem());
  120. if (pItem)
  121. {
  122. m_clrText = (COLORREF)pItem->GetID();
  123. OnButtonText();
  124. }
  125. *pResult = TRUE; // Handled
  126. }
  127. }
  128. void CGallerySampleView::OnUpdateGalleryFontTextColor(CCmdUI* pCmdUI)
  129. {
  130. CXTPControlGallery* pControl =  DYNAMIC_DOWNCAST(CXTPControlGallery, CXTPControl::FromUI(pCmdUI));
  131. if (pControl)
  132. {
  133. pControl->SetCheckedItem(m_clrText);
  134. }
  135. pCmdUI->Enable(TRUE);
  136. }
  137. void CGallerySampleView::OnGalleryFontBackColor(NMHDR* pNMHDR, LRESULT* pResult)
  138. {
  139. NMXTPCONTROL* tagNMCONTROL = (NMXTPCONTROL*)pNMHDR;
  140. CXTPControlGallery* pControl = DYNAMIC_DOWNCAST(CXTPControlGallery, tagNMCONTROL->pControl);
  141. if (pControl)
  142. {
  143. CXTPControlGalleryItem* pItem = pControl->GetItem(pControl->GetSelectedItem());
  144. if (pItem)
  145. {
  146. m_clrBack = (COLORREF)pItem->GetID();
  147. OnButtonBack();
  148. }
  149. *pResult = TRUE; // Handled
  150. }
  151. }
  152. void CGallerySampleView::OnUpdateGalleryFontBackColor(CCmdUI* pCmdUI)
  153. {
  154. CXTPControlGallery* pControl =  DYNAMIC_DOWNCAST(CXTPControlGallery, CXTPControl::FromUI(pCmdUI));
  155. if (pControl)
  156. {
  157. pControl->SetCheckedItem(m_clrBack);
  158. }
  159. pCmdUI->Enable(TRUE);
  160. }
  161. void CGallerySampleView::OnUpdateGalleryFontFace(CCmdUI* pCmdUI)
  162. {
  163. pCmdUI->Enable(TRUE);
  164. }
  165. void CGallerySampleView::OnUpdateGalleryFontSize(CCmdUI* pCmdUI)
  166. {
  167. pCmdUI->Enable(TRUE);
  168. }
  169. void CGallerySampleView::OnFormatFontcolorAutomatic() 
  170. {
  171. CHARFORMAT& cfm = GetCharFormatSelection( );
  172. cfm.dwMask |= CFM_COLOR;
  173. cfm.dwEffects |= CFE_AUTOCOLOR ;
  174. GetRichEditCtrl().SetSelectionCharFormat(cfm);
  175. m_clrText = COLORREF_NULL;
  176. }
  177. void CGallerySampleView::OnFormatFontcolorNoHighlight()
  178. {
  179. CHARFORMAT2 cfm;
  180. cfm.cbSize =  sizeof(CHARFORMAT2);
  181. ::SendMessage(GetRichEditCtrl().GetSafeHwnd(), EM_GETCHARFORMAT, 1, (LPARAM)&cfm);
  182. cfm.dwMask |= CFM_BACKCOLOR;
  183. cfm.dwEffects |= CFE_AUTOBACKCOLOR ;
  184. ::SendMessage(GetRichEditCtrl().GetSafeHwnd(), EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfm);
  185. m_clrBack = COLORREF_NULL;
  186. }
  187. void CGallerySampleView::OnUpdateFormatFontcolorAutomatic(CCmdUI* pCmdUI) 
  188. {
  189. CHARFORMAT& cfm = GetCharFormatSelection( );
  190. pCmdUI->SetCheck(cfm.dwEffects & CFE_AUTOCOLOR? TRUE: FALSE);
  191. }
  192. void CGallerySampleView::OnUpdateButtonText(CCmdUI* pCmd)
  193. {
  194. CXTPControlPopupColor* pPopup = DYNAMIC_DOWNCAST(CXTPControlPopupColor, CXTPControl::FromUI(pCmd));
  195. if (pPopup)
  196. {
  197. pPopup->SetColor(m_clrText == COLORREF_NULL ? 0 : m_clrText);
  198. }
  199. pCmd->Enable(TRUE);
  200. }
  201. void CGallerySampleView::OnButtonText()
  202. {
  203. CHARFORMAT& cfm = GetCharFormatSelection( );
  204. cfm.dwMask |= CFM_COLOR;
  205. cfm.dwEffects &= ~CFE_AUTOCOLOR ;
  206. cfm.crTextColor = m_clrText;
  207. GetRichEditCtrl().SetSelectionCharFormat(cfm);
  208. }
  209. void CGallerySampleView::OnUpdateBack(CCmdUI* pCmdUI)
  210. {
  211. CXTPControlPopupColor* pPopup = DYNAMIC_DOWNCAST(CXTPControlPopupColor, CXTPControl::FromUI(pCmdUI));
  212. if (pPopup)
  213. {
  214. pPopup->SetColor(m_clrBack == COLORREF_NULL ? 0xFFFFFF : m_clrBack);
  215. }
  216. pCmdUI->Enable(TRUE);
  217. }
  218. void CGallerySampleView::OnButtonBack()
  219. {
  220. CHARFORMAT2 cfm;
  221. cfm.cbSize =  sizeof(CHARFORMAT2);
  222. ::SendMessage(GetRichEditCtrl().GetSafeHwnd(), EM_GETCHARFORMAT, 1, (LPARAM)&cfm);
  223. cfm.dwMask |= CFM_BACKCOLOR;
  224. cfm.dwEffects &= ~CFE_AUTOBACKCOLOR ;
  225. cfm.crBackColor = m_clrBack;
  226. ::SendMessage(GetRichEditCtrl().GetSafeHwnd(), EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfm);
  227. }
  228. void CGallerySampleView::OnFormatFontcolorMorecolors() 
  229. {
  230. CColorDialog cd(m_clrText);
  231. if (cd.DoModal())
  232. {
  233. m_clrText = cd.GetColor();
  234. OnButtonText();
  235. }
  236. }
  237. void CGallerySampleView::OnUpdateComboFont(CCmdUI* pCmd)
  238. {
  239. CXTPControlComboBox* pFontCombo = (CXTPControlComboBox*)CXTPControl::FromUI(pCmd);
  240. pCmd->Enable(TRUE);
  241. if (pFontCombo && pFontCombo->GetType() == xtpControlComboBox)
  242. {
  243. CHARFORMAT& cf = GetCharFormatSelection();
  244. if (pFontCombo->HasFocus())
  245. return;
  246. // the selection must be same font and charset to display correctly
  247. if ((cf.dwMask & (CFM_FACE | CFM_CHARSET)) == (CFM_FACE | CFM_CHARSET))
  248. pFontCombo->SetEditText(CString(cf.szFaceName));
  249. else
  250. pFontCombo->SetEditText(_T(""));
  251. }
  252. }
  253. void CGallerySampleView::OnEditFont(NMHDR* pNMHDR, LRESULT* pResult)
  254. {
  255. USES_CONVERSION;
  256. NMXTPCONTROL* tagNMCONTROL = (NMXTPCONTROL*)pNMHDR;
  257. CXTPControlComboBox* pControl = (CXTPControlComboBox*)tagNMCONTROL->pControl;
  258. if (pControl->GetType() == xtpControlComboBox)
  259. {
  260. CString strFont = pControl->GetEditText();
  261. #if (_MSC_VER < 1300)
  262. CHARFORMAT cf;
  263. #else
  264. CHARFORMAT2 cf;
  265. #endif
  266. cf.dwMask = CFM_FACE;
  267. #if (_RICHEDIT_VER >= 0x0200)
  268. lstrcpyn(cf.szFaceName, strFont, LF_FACESIZE);
  269. #else
  270. lstrcpynA(cf.szFaceName, T2A((LPTSTR)(LPCTSTR)strFont), LF_FACESIZE);
  271. #endif
  272. SetCharFormat(cf);
  273. *pResult = 1; // Handled;
  274. }
  275. }
  276. void CGallerySampleView::OnUpdateComboSize(CCmdUI* pCmd)
  277. {
  278. CXTPControlComboBox* pFontCombo = (CXTPControlComboBox*)CXTPControl::FromUI(pCmd);
  279. pCmd->Enable(TRUE);
  280. if (pFontCombo && pFontCombo->GetType() == xtpControlComboBox)
  281. {
  282. CHARFORMAT& cf = GetCharFormatSelection();
  283. if (pFontCombo->HasFocus())
  284. return;
  285. int nTwip = (cf.dwMask & CFM_SIZE) ? cf.yHeight : -1;
  286. pFontCombo->SetEditText(CXTPControlSizeComboBox::TwipsToPointString(nTwip));
  287. }
  288. }
  289. void CGallerySampleView::OnEditSize(NMHDR* pNMHDR, LRESULT* pResult)
  290. {
  291. NMXTPCONTROL* tagNMCONTROL = (NMXTPCONTROL*)pNMHDR;
  292. CXTPControlComboBox* pControl = (CXTPControlComboBox*)tagNMCONTROL->pControl;
  293. if (pControl->GetType() == xtpControlComboBox)
  294. {
  295. #if (_MSC_VER < 1300)
  296. CHARFORMAT cf;
  297. #else
  298. CHARFORMAT2 cf;
  299. #endif
  300. int nSize = CXTPControlSizeComboBox::GetTwipSize(pControl->GetEditText());
  301. if (nSize > 0)
  302. {
  303. cf.dwMask = CFM_SIZE;
  304. cf.yHeight = nSize;
  305. SetCharFormat(cf);
  306. }
  307. *pResult = 1; // Handled;
  308. }
  309. }
  310. void CGallerySampleView::OnUpdateEditUndo(CCmdUI* pCmd)
  311. {
  312. CXTPControlGallery* pControlUndo = DYNAMIC_DOWNCAST(CXTPControlGallery, CXTPControl::FromUI(pCmd));
  313. if (pControlUndo)
  314. {
  315. pCmd->Enable(GetRichEditCtrl().CanUndo());
  316. CXTPControlGalleryItems* pItems = pControlUndo->GetItems();
  317. pItems->RemoveAll();
  318. int nCount = RAND_S() % 20 + 3;
  319. for (int i = 0; i < nCount; i++)
  320. {
  321. CString str;
  322. str.Format(_T("Undo String %i"), i + 1);
  323. pItems->AddItem(new CGalleryItemUndo(str), i);
  324. }
  325. pControlUndo->OnSelectedItemChanged();
  326. pControlUndo->SetHeight(pItems->GetItemSize().cy * nCount + 2);
  327. }
  328. else
  329. {
  330. CRichEditView::OnUpdateEditUndo(pCmd);
  331. }
  332. }
  333. void CGallerySampleView::OnEditUndo(NMHDR* pNMHDR, LRESULT* pResult)
  334. {
  335. CXTPControlGallery* pControlUndo = DYNAMIC_DOWNCAST(CXTPControlGallery, ((NMXTPCONTROL*)pNMHDR)->pControl);
  336. if (pControlUndo)
  337. {
  338. CString str;
  339. str.Format(_T("Undo last %i actions"), pControlUndo->GetSelectedItem() + 1);
  340. AfxMessageBox(str);
  341. }
  342. else
  343. {
  344. CRichEditView::OnEditUndo();
  345. }
  346. *pResult = 1; // Handled;
  347. }